PostgreSQL Source Code  git master
mbuf.c File Reference
#include "postgres.h"
#include "mbuf.h"
#include "px.h"
Include dependency graph for mbuf.c:

Go to the source code of this file.

Data Structures

struct  MBuf
 
struct  PullFilter
 
struct  PushFilter
 

Macros

#define STEP   (16*1024)
 

Functions

int mbuf_avail (MBuf *mbuf)
 
int mbuf_size (MBuf *mbuf)
 
int mbuf_tell (MBuf *mbuf)
 
int mbuf_free (MBuf *mbuf)
 
static void prepare_room (MBuf *mbuf, int block_len)
 
int mbuf_append (MBuf *dst, const uint8 *buf, int len)
 
MBufmbuf_create (int len)
 
MBufmbuf_create_from_data (uint8 *data, int len)
 
int mbuf_grab (MBuf *mbuf, int len, uint8 **data_p)
 
int mbuf_rewind (MBuf *mbuf)
 
int mbuf_steal_data (MBuf *mbuf, uint8 **data_p)
 
int pullf_create (PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
 
void pullf_free (PullFilter *pf)
 
int pullf_read (PullFilter *pf, int len, uint8 **data_p)
 
int pullf_read_max (PullFilter *pf, int len, uint8 **data_p, uint8 *tmpbuf)
 
int pullf_read_fixed (PullFilter *src, int len, uint8 *dst)
 
static int pull_from_mbuf (void *arg, PullFilter *src, int len, uint8 **data_p, uint8 *buf, int buflen)
 
int pullf_create_mbuf_reader (PullFilter **mp_p, MBuf *src)
 
int pushf_create (PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
 
void pushf_free (PushFilter *mp)
 
void pushf_free_all (PushFilter *mp)
 
static int wrap_process (PushFilter *mp, const uint8 *data, int len)
 
int pushf_write (PushFilter *mp, const uint8 *data, int len)
 
int pushf_flush (PushFilter *mp)
 
static int push_into_mbuf (PushFilter *next, void *arg, const uint8 *data, int len)
 
int pushf_create_mbuf_writer (PushFilter **res, MBuf *dst)
 

Variables

static const struct PullFilterOps mbuf_reader
 
static const struct PushFilterOps mbuf_filter
 

Macro Definition Documentation

◆ STEP

#define STEP   (16*1024)

Definition at line 37 of file mbuf.c.

Referenced by prepare_room().

Function Documentation

◆ mbuf_append()

int mbuf_append ( MBuf dst,
const uint8 buf,
int  len 
)

Definition at line 100 of file mbuf.c.

References MBuf::data_end, MBuf::no_write, prepare_room(), px_debug(), and PXE_BUG.

Referenced by copy_crlf(), decrypt_internal(), encrypt_internal(), parse_literal_data(), and push_into_mbuf().

101 {
102  if (dst->no_write)
103  {
104  px_debug("mbuf_append: no_write");
105  return PXE_BUG;
106  }
107 
108  prepare_room(dst, len);
109 
110  memcpy(dst->data_end, buf, len);
111  dst->data_end += len;
112 
113  return 0;
114 }
static char * buf
Definition: pg_test_fsync.c:68
#define PXE_BUG
Definition: px.h:57
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
void px_debug(const char *fmt,...)
Definition: px.c:153
static void prepare_room(MBuf *mbuf, int block_len)
Definition: mbuf.c:80

◆ mbuf_avail()

int mbuf_avail ( MBuf mbuf)

Definition at line 50 of file mbuf.c.

References MBuf::data_end, and MBuf::read_pos.

Referenced by mbuf_grab(), and pgp_encrypt().

51 {
52  return mbuf->data_end - mbuf->read_pos;
53 }
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42

◆ mbuf_create()

MBuf* mbuf_create ( int  len)

Definition at line 117 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, MBuf::no_write, MBuf::own_data, palloc(), and MBuf::read_pos.

Referenced by decrypt_internal(), and encrypt_internal().

118 {
119  MBuf *mbuf;
120 
121  if (!len)
122  len = 8192;
123 
124  mbuf = palloc(sizeof *mbuf);
125  mbuf->data = palloc(len);
126  mbuf->buf_end = mbuf->data + len;
127  mbuf->data_end = mbuf->data;
128  mbuf->read_pos = mbuf->data;
129 
130  mbuf->no_write = false;
131  mbuf->own_data = true;
132 
133  return mbuf;
134 }
uint8 * buf_end
Definition: mbuf.c:44
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
Definition: mbuf.c:39
bool own_data
Definition: mbuf.c:46
void * palloc(Size size)
Definition: mcxt.c:950
uint8 * data
Definition: mbuf.c:41

◆ mbuf_create_from_data()

MBuf* mbuf_create_from_data ( uint8 data,
int  len 
)

Definition at line 137 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, MBuf::no_write, MBuf::own_data, palloc(), and MBuf::read_pos.

Referenced by create_mbuf_from_vardata(), and decrypt_internal().

138 {
139  MBuf *mbuf;
140 
141  mbuf = palloc(sizeof *mbuf);
142  mbuf->data = (uint8 *) data;
143  mbuf->buf_end = mbuf->data + len;
144  mbuf->data_end = mbuf->data + len;
145  mbuf->read_pos = mbuf->data;
146 
147  mbuf->no_write = true;
148  mbuf->own_data = false;
149 
150  return mbuf;
151 }
unsigned char uint8
Definition: c.h:427
uint8 * buf_end
Definition: mbuf.c:44
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
Definition: mbuf.c:39
bool own_data
Definition: mbuf.c:46
void * palloc(Size size)
Definition: mcxt.c:950
uint8 * data
Definition: mbuf.c:41

◆ mbuf_free()

int mbuf_free ( MBuf mbuf)

Definition at line 68 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::own_data, pfree(), and px_memset().

Referenced by decrypt_internal(), encrypt_internal(), and pgp_key_id_w().

69 {
70  if (mbuf->own_data)
71  {
72  px_memset(mbuf->data, 0, mbuf->buf_end - mbuf->data);
73  pfree(mbuf->data);
74  }
75  pfree(mbuf);
76  return 0;
77 }
uint8 * buf_end
Definition: mbuf.c:44
void pfree(void *pointer)
Definition: mcxt.c:1057
bool own_data
Definition: mbuf.c:46
uint8 * data
Definition: mbuf.c:41
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ mbuf_grab()

int mbuf_grab ( MBuf mbuf,
int  len,
uint8 **  data_p 
)

Definition at line 155 of file mbuf.c.

References mbuf_avail(), MBuf::no_write, and MBuf::read_pos.

Referenced by pgp_encrypt(), and pull_from_mbuf().

156 {
157  if (len > mbuf_avail(mbuf))
158  len = mbuf_avail(mbuf);
159 
160  mbuf->no_write = true;
161 
162  *data_p = mbuf->read_pos;
163  mbuf->read_pos += len;
164  return len;
165 }
int mbuf_avail(MBuf *mbuf)
Definition: mbuf.c:50
uint8 * read_pos
Definition: mbuf.c:43
bool no_write
Definition: mbuf.c:45

◆ mbuf_rewind()

int mbuf_rewind ( MBuf mbuf)

Definition at line 168 of file mbuf.c.

References MBuf::data, and MBuf::read_pos.

169 {
170  mbuf->read_pos = mbuf->data;
171  return 0;
172 }
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data
Definition: mbuf.c:41

◆ mbuf_size()

int mbuf_size ( MBuf mbuf)

Definition at line 56 of file mbuf.c.

References MBuf::data, and MBuf::data_end.

Referenced by mbuf_steal_data().

57 {
58  return mbuf->data_end - mbuf->data;
59 }
uint8 * data_end
Definition: mbuf.c:42
uint8 * data
Definition: mbuf.c:41

◆ mbuf_steal_data()

int mbuf_steal_data ( MBuf mbuf,
uint8 **  data_p 
)

Definition at line 175 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, mbuf_size(), MBuf::no_write, MBuf::own_data, and MBuf::read_pos.

Referenced by decrypt_internal(), and encrypt_internal().

176 {
177  int len = mbuf_size(mbuf);
178 
179  mbuf->no_write = true;
180  mbuf->own_data = false;
181 
182  *data_p = mbuf->data;
183 
184  mbuf->data = mbuf->data_end = mbuf->read_pos = mbuf->buf_end = NULL;
185 
186  return len;
187 }
uint8 * buf_end
Definition: mbuf.c:44
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
bool own_data
Definition: mbuf.c:46
int mbuf_size(MBuf *mbuf)
Definition: mbuf.c:56
uint8 * data
Definition: mbuf.c:41

◆ mbuf_tell()

int mbuf_tell ( MBuf mbuf)

Definition at line 62 of file mbuf.c.

References MBuf::data, and MBuf::read_pos.

63 {
64  return mbuf->read_pos - mbuf->data;
65 }
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data
Definition: mbuf.c:41

◆ prepare_room()

static void prepare_room ( MBuf mbuf,
int  block_len 
)
static

Definition at line 80 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, MBuf::read_pos, repalloc(), and STEP.

Referenced by mbuf_append().

81 {
82  uint8 *newbuf;
83  unsigned newlen;
84 
85  if (mbuf->data_end + block_len <= mbuf->buf_end)
86  return;
87 
88  newlen = (mbuf->buf_end - mbuf->data)
89  + ((block_len + STEP + STEP - 1) & -STEP);
90 
91  newbuf = repalloc(mbuf->data, newlen);
92 
93  mbuf->buf_end = newbuf + newlen;
94  mbuf->data_end = newbuf + (mbuf->data_end - mbuf->data);
95  mbuf->read_pos = newbuf + (mbuf->read_pos - mbuf->data);
96  mbuf->data = newbuf;
97 }
#define STEP
Definition: mbuf.c:37
unsigned char uint8
Definition: c.h:427
uint8 * buf_end
Definition: mbuf.c:44
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
uint8 * data
Definition: mbuf.c:41

◆ pull_from_mbuf()

static int pull_from_mbuf ( void *  arg,
PullFilter src,
int  len,
uint8 **  data_p,
uint8 buf,
int  buflen 
)
static

Definition at line 336 of file mbuf.c.

References arg, and mbuf_grab().

338 {
339  MBuf *mbuf = arg;
340 
341  return mbuf_grab(mbuf, len, data_p);
342 }
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)
Definition: mbuf.c:155
Definition: mbuf.c:39
void * arg

◆ pullf_create()

int pullf_create ( PullFilter **  pf_p,
const PullFilterOps op,
void *  init_arg,
PullFilter src 
)

Definition at line 204 of file mbuf.c.

References PullFilter::buf, PullFilter::buflen, PullFilterOps::init, PullFilter::op, palloc(), palloc0(), PullFilter::pos, PullFilter::priv, and PullFilter::src.

Referenced by parse_symenc_data(), parse_symenc_mdc_data(), pgp_create_pkt_reader(), process_data_packets(), process_secret_key(), and pullf_create_mbuf_reader().

205 {
206  PullFilter *pf;
207  void *priv;
208  int res;
209 
210  if (op->init != NULL)
211  {
212  res = op->init(&priv, init_arg, src);
213  if (res < 0)
214  return res;
215  }
216  else
217  {
218  priv = init_arg;
219  res = 0;
220  }
221 
222  pf = palloc0(sizeof(*pf));
223  pf->buflen = res;
224  pf->op = op;
225  pf->priv = priv;
226  pf->src = src;
227  if (pf->buflen > 0)
228  {
229  pf->buf = palloc(pf->buflen);
230  pf->pos = 0;
231  }
232  else
233  {
234  pf->buf = NULL;
235  pf->pos = 0;
236  }
237  *pf_p = pf;
238  return 0;
239 }
int(* init)(void **priv_p, void *init_arg, PullFilter *src)
Definition: mbuf.h:65
PullFilter * src
Definition: mbuf.c:195
int pos
Definition: mbuf.c:199
const PullFilterOps * op
Definition: mbuf.c:196
uint8 * buf
Definition: mbuf.c:198
void * palloc0(Size size)
Definition: mcxt.c:981
int buflen
Definition: mbuf.c:197
void * priv
Definition: mbuf.c:200
void * palloc(Size size)
Definition: mcxt.c:950

◆ pullf_create_mbuf_reader()

int pullf_create_mbuf_reader ( PullFilter **  mp_p,
MBuf src 
)

Definition at line 349 of file mbuf.c.

References pullf_create().

Referenced by pgp_decrypt(), pgp_get_keyid(), and pgp_set_pubkey().

350 {
351  return pullf_create(mp_p, &mbuf_reader, src, NULL);
352 }
int pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
Definition: mbuf.c:204
static const struct PullFilterOps mbuf_reader
Definition: mbuf.c:344

◆ pullf_free()

void pullf_free ( PullFilter pf)

Definition at line 242 of file mbuf.c.

References PullFilter::buf, PullFilter::buflen, PullFilterOps::free, PullFilter::op, pfree(), PullFilter::priv, and px_memset().

Referenced by internal_read_key(), parse_compressed_data(), parse_symenc_data(), parse_symenc_mdc_data(), pgp_decrypt(), pgp_get_keyid(), pgp_set_pubkey(), process_data_packets(), and process_secret_key().

243 {
244  if (pf->op->free)
245  pf->op->free(pf->priv);
246 
247  if (pf->buf)
248  {
249  px_memset(pf->buf, 0, pf->buflen);
250  pfree(pf->buf);
251  }
252 
253  px_memset(pf, 0, sizeof(*pf));
254  pfree(pf);
255 }
void pfree(void *pointer)
Definition: mcxt.c:1057
const PullFilterOps * op
Definition: mbuf.c:196
uint8 * buf
Definition: mbuf.c:198
void(* free)(void *priv)
Definition: mbuf.h:73
int buflen
Definition: mbuf.c:197
void * priv
Definition: mbuf.c:200
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ pullf_read()

int pullf_read ( PullFilter pf,
int  len,
uint8 **  data_p 
)

Definition at line 259 of file mbuf.c.

References PullFilter::buf, PullFilter::buflen, PullFilter::op, PullFilter::priv, PullFilterOps::pull, pullf_read(), and PullFilter::src.

Referenced by decrypt_read(), mdc_read(), mdcbuf_refill(), parse_compressed_data(), parse_literal_data(), pgp_expect_packet_end(), pgp_parse_pkt_hdr(), pgp_skip_packet(), pktreader_pull(), pullf_read(), and pullf_read_max().

260 {
261  int res;
262 
263  if (pf->op->pull)
264  {
265  if (pf->buflen && len > pf->buflen)
266  len = pf->buflen;
267  res = pf->op->pull(pf->priv, pf->src, len, data_p,
268  pf->buf, pf->buflen);
269  }
270  else
271  res = pullf_read(pf->src, len, data_p);
272  return res;
273 }
int pullf_read(PullFilter *pf, int len, uint8 **data_p)
Definition: mbuf.c:259
PullFilter * src
Definition: mbuf.c:195
const PullFilterOps * op
Definition: mbuf.c:196
uint8 * buf
Definition: mbuf.c:198
int(* pull)(void *priv, PullFilter *src, int len, uint8 **data_p, uint8 *buf, int buflen)
Definition: mbuf.h:71
int buflen
Definition: mbuf.c:197
void * priv
Definition: mbuf.c:200

◆ pullf_read_fixed()

int pullf_read_fixed ( PullFilter src,
int  len,
uint8 dst 
)

Definition at line 314 of file mbuf.c.

References pullf_read_max(), px_debug(), and PXE_PGP_CORRUPT_DATA.

Referenced by _pgp_read_public_key(), check_key_cksum(), check_key_sha1(), pgp_mpi_read(), pgp_parse_pubenc_sesskey(), pgp_s2k_read(), process_secret_key(), and read_pubenc_keyid().

315 {
316  int res;
317  uint8 *p;
318 
319  res = pullf_read_max(src, len, &p, dst);
320  if (res < 0)
321  return res;
322  if (res != len)
323  {
324  px_debug("pullf_read_fixed: need=%d got=%d", len, res);
325  return PXE_PGP_CORRUPT_DATA;
326  }
327  if (p != dst)
328  memcpy(dst, p, len);
329  return 0;
330 }
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:66
unsigned char uint8
Definition: c.h:427
void px_debug(const char *fmt,...)
Definition: px.c:153
int pullf_read_max(PullFilter *pf, int len, uint8 **data_p, uint8 *tmpbuf)
Definition: mbuf.c:276

◆ pullf_read_max()

int pullf_read_max ( PullFilter pf,
int  len,
uint8 **  data_p,
uint8 tmpbuf 
)

Definition at line 276 of file mbuf.c.

References pullf_read(), px_memset(), and tmpbuf.

Referenced by mdc_finish(), parse_literal_data(), parse_symenc_sesskey(), prefix_init(), and pullf_read_fixed().

277 {
278  int res,
279  total;
280  uint8 *tmp;
281 
282  res = pullf_read(pf, len, data_p);
283  if (res <= 0 || res == len)
284  return res;
285 
286  /* read was shorter, use tmpbuf */
287  memcpy(tmpbuf, *data_p, res);
288  *data_p = tmpbuf;
289  len -= res;
290  total = res;
291 
292  while (len > 0)
293  {
294  res = pullf_read(pf, len, &tmp);
295  if (res < 0)
296  {
297  /* so the caller must clear only on success */
298  px_memset(tmpbuf, 0, total);
299  return res;
300  }
301  if (res == 0)
302  break;
303  memcpy(tmpbuf + total, tmp, res);
304  total += res;
305  len -= res;
306  }
307  return total;
308 }
int pullf_read(PullFilter *pf, int len, uint8 **data_p)
Definition: mbuf.c:259
unsigned char uint8
Definition: c.h:427
static StringInfoData tmpbuf
Definition: walsender.c:159
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ push_into_mbuf()

static int push_into_mbuf ( PushFilter next,
void *  arg,
const uint8 data,
int  len 
)
static

Definition at line 542 of file mbuf.c.

References arg, and mbuf_append().

543 {
544  int res = 0;
545  MBuf *mbuf = arg;
546 
547  if (len > 0)
548  res = mbuf_append(mbuf, data, len);
549  return res < 0 ? res : 0;
550 }
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:100
Definition: mbuf.c:39
void * arg

◆ pushf_create()

int pushf_create ( PushFilter **  mp_p,
const PushFilterOps op,
void *  init_arg,
PushFilter next 
)

Definition at line 370 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilterOps::init, next, PushFilter::next, PushFilter::op, palloc(), palloc0(), PushFilter::pos, and PushFilter::priv.

Referenced by init_compress(), init_encdata_packet(), init_litdata_packet(), pgp_create_pkt_writer(), pgp_encrypt(), and pushf_create_mbuf_writer().

371 {
372  PushFilter *mp;
373  void *priv;
374  int res;
375 
376  if (op->init != NULL)
377  {
378  res = op->init(next, init_arg, &priv);
379  if (res < 0)
380  return res;
381  }
382  else
383  {
384  priv = init_arg;
385  res = 0;
386  }
387 
388  mp = palloc0(sizeof(*mp));
389  mp->block_size = res;
390  mp->op = op;
391  mp->priv = priv;
392  mp->next = next;
393  if (mp->block_size > 0)
394  {
395  mp->buf = palloc(mp->block_size);
396  mp->pos = 0;
397  }
398  else
399  {
400  mp->buf = NULL;
401  mp->pos = 0;
402  }
403  *mp_p = mp;
404  return 0;
405 }
static int32 next
Definition: blutils.c:219
const PushFilterOps * op
Definition: mbuf.c:362
void * priv
Definition: mbuf.c:366
int pos
Definition: mbuf.c:365
void * palloc0(Size size)
Definition: mcxt.c:981
PushFilter * next
Definition: mbuf.c:361
void * palloc(Size size)
Definition: mcxt.c:950
int(* init)(PushFilter *next, void *init_arg, void **priv_p)
Definition: mbuf.h:47
uint8 * buf
Definition: mbuf.c:364
int block_size
Definition: mbuf.c:363

◆ pushf_create_mbuf_writer()

int pushf_create_mbuf_writer ( PushFilter **  res,
MBuf dst 
)

Definition at line 557 of file mbuf.c.

References pushf_create().

Referenced by pgp_encrypt().

558 {
559  return pushf_create(res, &mbuf_filter, dst, NULL);
560 }
static const struct PushFilterOps mbuf_filter
Definition: mbuf.c:552
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:370

◆ pushf_flush()

int pushf_flush ( PushFilter mp)

Definition at line 512 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilterOps::flush, PushFilter::next, PushFilter::op, PushFilter::pos, PushFilter::priv, and wrap_process().

Referenced by pgp_encrypt(), and pgp_write_pubenc_sesskey().

513 {
514  int res;
515 
516  while (mp)
517  {
518  if (mp->block_size > 0)
519  {
520  res = wrap_process(mp, mp->buf, mp->pos);
521  if (res < 0)
522  return res;
523  }
524 
525  if (mp->op->flush)
526  {
527  res = mp->op->flush(mp->next, mp->priv);
528  if (res < 0)
529  return res;
530  }
531 
532  mp = mp->next;
533  }
534  return 0;
535 }
const PushFilterOps * op
Definition: mbuf.c:362
static int wrap_process(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:437
void * priv
Definition: mbuf.c:366
int pos
Definition: mbuf.c:365
PushFilter * next
Definition: mbuf.c:361
int(* flush)(PushFilter *next, void *priv)
Definition: mbuf.h:55
uint8 * buf
Definition: mbuf.c:364
int block_size
Definition: mbuf.c:363

◆ pushf_free()

void pushf_free ( PushFilter mp)

Definition at line 408 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilterOps::free, PushFilter::op, pfree(), PushFilter::priv, and px_memset().

Referenced by init_compress(), init_litdata_packet(), pgp_write_pubenc_sesskey(), and pushf_free_all().

409 {
410  if (mp->op->free)
411  mp->op->free(mp->priv);
412 
413  if (mp->buf)
414  {
415  px_memset(mp->buf, 0, mp->block_size);
416  pfree(mp->buf);
417  }
418 
419  px_memset(mp, 0, sizeof(*mp));
420  pfree(mp);
421 }
const PushFilterOps * op
Definition: mbuf.c:362
void * priv
Definition: mbuf.c:366
void pfree(void *pointer)
Definition: mcxt.c:1057
void(* free)(void *priv)
Definition: mbuf.h:56
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127
uint8 * buf
Definition: mbuf.c:364
int block_size
Definition: mbuf.c:363

◆ pushf_free_all()

void pushf_free_all ( PushFilter mp)

Definition at line 424 of file mbuf.c.

References PushFilter::next, and pushf_free().

Referenced by pgp_encrypt().

425 {
426  PushFilter *tmp;
427 
428  while (mp)
429  {
430  tmp = mp->next;
431  pushf_free(mp);
432  mp = tmp;
433  }
434 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:408
PushFilter * next
Definition: mbuf.c:361

◆ pushf_write()

int pushf_write ( PushFilter mp,
const uint8 data,
int  len 
)

Definition at line 452 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilter::pos, and wrap_process().

Referenced by crlf_process(), encrypt_init(), encrypt_process(), init_compress(), init_litdata_packet(), mdc_flush(), mdc_write(), pgp_encrypt(), pgp_mpi_write(), pgp_write_pubenc_sesskey(), pkt_stream_flush(), pkt_stream_process(), wrap_process(), write_normal_header(), write_prefix(), write_symenc_sesskey(), and write_tag_only().

453 {
454  int need,
455  res;
456 
457  /*
458  * no buffering
459  */
460  if (mp->block_size <= 0)
461  return wrap_process(mp, data, len);
462 
463  /*
464  * try to empty buffer
465  */
466  need = mp->block_size - mp->pos;
467  if (need > 0)
468  {
469  if (len < need)
470  {
471  memcpy(mp->buf + mp->pos, data, len);
472  mp->pos += len;
473  return 0;
474  }
475  memcpy(mp->buf + mp->pos, data, need);
476  len -= need;
477  data += need;
478  }
479 
480  /*
481  * buffer full, process
482  */
483  res = wrap_process(mp, mp->buf, mp->block_size);
484  if (res < 0)
485  return res;
486  mp->pos = 0;
487 
488  /*
489  * now process directly from data
490  */
491  while (len > 0)
492  {
493  if (len > mp->block_size)
494  {
495  res = wrap_process(mp, data, mp->block_size);
496  if (res < 0)
497  return res;
498  data += mp->block_size;
499  len -= mp->block_size;
500  }
501  else
502  {
503  memcpy(mp->buf, data, len);
504  mp->pos += len;
505  break;
506  }
507  }
508  return 0;
509 }
static int wrap_process(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:437
int pos
Definition: mbuf.c:365
uint8 * buf
Definition: mbuf.c:364
int block_size
Definition: mbuf.c:363

◆ wrap_process()

static int wrap_process ( PushFilter mp,
const uint8 data,
int  len 
)
static

Definition at line 437 of file mbuf.c.

References PushFilter::next, PushFilter::op, PushFilter::priv, PushFilterOps::push, pushf_write(), and PXE_BUG.

Referenced by pushf_flush(), and pushf_write().

438 {
439  int res;
440 
441  if (mp->op->push != NULL)
442  res = mp->op->push(mp->next, mp->priv, data, len);
443  else
444  res = pushf_write(mp->next, data, len);
445  if (res > 0)
446  return PXE_BUG;
447  return res;
448 }
const PushFilterOps * op
Definition: mbuf.c:362
void * priv
Definition: mbuf.c:366
#define PXE_BUG
Definition: px.h:57
PushFilter * next
Definition: mbuf.c:361
int(* push)(PushFilter *next, void *priv, const uint8 *src, int len)
Definition: mbuf.h:53
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:452

Variable Documentation

◆ mbuf_filter

const struct PushFilterOps mbuf_filter
static
Initial value:
= {
NULL, push_into_mbuf, NULL, NULL
}
static int push_into_mbuf(PushFilter *next, void *arg, const uint8 *data, int len)
Definition: mbuf.c:542

Definition at line 552 of file mbuf.c.

◆ mbuf_reader

const struct PullFilterOps mbuf_reader
static
Initial value:
= {
NULL, pull_from_mbuf, NULL
}
static int pull_from_mbuf(void *arg, PullFilter *src, int len, uint8 **data_p, uint8 *buf, int buflen)
Definition: mbuf.c:336

Definition at line 344 of file mbuf.c.