PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mbuf.c File Reference
#include "postgres.h"
#include "px.h"
#include "mbuf.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

#define STEP   (16*1024)

Definition at line 37 of file mbuf.c.

Referenced by prepare_room().

Function Documentation

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

Definition at line 102 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().

103 {
104  if (dst->no_write)
105  {
106  px_debug("mbuf_append: no_write");
107  return PXE_BUG;
108  }
109 
110  prepare_room(dst, len);
111 
112  memcpy(dst->data_end, buf, len);
113  dst->data_end += len;
114 
115  return 0;
116 }
static char * buf
Definition: pg_test_fsync.c:65
#define PXE_BUG
Definition: px.h:74
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
void px_debug(const char *fmt,...)
Definition: px.c:160
static void prepare_room(MBuf *mbuf, int block_len)
Definition: mbuf.c:80
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* mbuf_create ( int  len)

Definition at line 119 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, MBuf::no_write, MBuf::own_data, px_alloc, and MBuf::read_pos.

Referenced by decrypt_internal(), and encrypt_internal().

120 {
121  MBuf *mbuf;
122 
123  if (!len)
124  len = 8192;
125 
126  mbuf = px_alloc(sizeof *mbuf);
127  mbuf->data = px_alloc(len);
128  mbuf->buf_end = mbuf->data + len;
129  mbuf->data_end = mbuf->data;
130  mbuf->read_pos = mbuf->data;
131 
132  mbuf->no_write = false;
133  mbuf->own_data = true;
134 
135  return mbuf;
136 }
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
#define px_alloc(s)
Definition: px.h:45
uint8 * data
Definition: mbuf.c:41
MBuf* mbuf_create_from_data ( uint8 data,
int  len 
)

Definition at line 139 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::data_end, MBuf::no_write, MBuf::own_data, px_alloc, and MBuf::read_pos.

Referenced by create_mbuf_from_vardata(), and decrypt_internal().

140 {
141  MBuf *mbuf;
142 
143  mbuf = px_alloc(sizeof *mbuf);
144  mbuf->data = (uint8 *) data;
145  mbuf->buf_end = mbuf->data + len;
146  mbuf->data_end = mbuf->data + len;
147  mbuf->read_pos = mbuf->data;
148 
149  mbuf->no_write = true;
150  mbuf->own_data = false;
151 
152  return mbuf;
153 }
unsigned char uint8
Definition: c.h:263
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
#define px_alloc(s)
Definition: px.h:45
uint8 * data
Definition: mbuf.c:41
int mbuf_free ( MBuf mbuf)

Definition at line 68 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::own_data, px_free, 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  px_free(mbuf->data);
74  }
75  px_free(mbuf);
76  return 0;
77 }
#define px_free(p)
Definition: px.h:47
uint8 * buf_end
Definition: mbuf.c:44
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:134
int mbuf_grab ( MBuf mbuf,
int  len,
uint8 **  data_p 
)

Definition at line 157 of file mbuf.c.

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

Referenced by pgp_encrypt(), and pull_from_mbuf().

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

Definition at line 170 of file mbuf.c.

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

171 {
172  mbuf->read_pos = mbuf->data;
173  return 0;
174 }
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data
Definition: mbuf.c:41
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
int mbuf_steal_data ( MBuf mbuf,
uint8 **  data_p 
)

Definition at line 177 of file mbuf.c.

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

Referenced by decrypt_internal(), and encrypt_internal().

178 {
179  int len = mbuf_size(mbuf);
180 
181  mbuf->no_write = true;
182  mbuf->own_data = false;
183 
184  *data_p = mbuf->data;
185 
186  mbuf->data = mbuf->data_end = mbuf->read_pos = mbuf->buf_end = NULL;
187 
188  return len;
189 }
uint8 * buf_end
Definition: mbuf.c:44
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
#define NULL
Definition: c.h:226
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
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
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, px_realloc, MBuf::read_pos, 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 = px_realloc(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 
98  return;
99 }
#define STEP
Definition: mbuf.c:37
unsigned char uint8
Definition: c.h:263
uint8 * buf_end
Definition: mbuf.c:44
#define px_realloc(p, s)
Definition: px.h:46
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
uint8 * data
Definition: mbuf.c:41
static int pull_from_mbuf ( void *  arg,
PullFilter src,
int  len,
uint8 **  data_p,
uint8 buf,
int  buflen 
)
static

Definition at line 339 of file mbuf.c.

References arg, and mbuf_grab().

341 {
342  MBuf *mbuf = arg;
343 
344  return mbuf_grab(mbuf, len, data_p);
345 }
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)
Definition: mbuf.c:157
Definition: mbuf.c:39
void * arg
int pullf_create ( PullFilter **  pf_p,
const PullFilterOps op,
void *  init_arg,
PullFilter src 
)

Definition at line 206 of file mbuf.c.

References PullFilter::buf, PullFilter::buflen, PullFilterOps::init, NULL, PullFilter::op, PullFilter::pos, PullFilter::priv, px_alloc, 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().

207 {
208  PullFilter *pf;
209  void *priv;
210  int res;
211 
212  if (op->init != NULL)
213  {
214  res = op->init(&priv, init_arg, src);
215  if (res < 0)
216  return res;
217  }
218  else
219  {
220  priv = init_arg;
221  res = 0;
222  }
223 
224  pf = px_alloc(sizeof(*pf));
225  memset(pf, 0, sizeof(*pf));
226  pf->buflen = res;
227  pf->op = op;
228  pf->priv = priv;
229  pf->src = src;
230  if (pf->buflen > 0)
231  {
232  pf->buf = px_alloc(pf->buflen);
233  pf->pos = 0;
234  }
235  else
236  {
237  pf->buf = NULL;
238  pf->pos = 0;
239  }
240  *pf_p = pf;
241  return 0;
242 }
PullFilter * src
Definition: mbuf.c:197
int pos
Definition: mbuf.c:201
const PullFilterOps * op
Definition: mbuf.c:198
uint8 * buf
Definition: mbuf.c:200
int(* init)(void **priv_p, void *init_arg, PullFilter *src)
Definition: mbuf.h:65
#define NULL
Definition: c.h:226
int buflen
Definition: mbuf.c:199
#define px_alloc(s)
Definition: px.h:45
void * priv
Definition: mbuf.c:202
int pullf_create_mbuf_reader ( PullFilter **  mp_p,
MBuf src 
)

Definition at line 352 of file mbuf.c.

References NULL, and pullf_create().

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

353 {
354  return pullf_create(mp_p, &mbuf_reader, src, NULL);
355 }
int pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
Definition: mbuf.c:206
static const struct PullFilterOps mbuf_reader
Definition: mbuf.c:347
#define NULL
Definition: c.h:226
void pullf_free ( PullFilter pf)

Definition at line 245 of file mbuf.c.

References PullFilter::buf, PullFilter::buflen, PullFilterOps::free, PullFilter::op, PullFilter::priv, px_free, 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().

246 {
247  if (pf->op->free)
248  pf->op->free(pf->priv);
249 
250  if (pf->buf)
251  {
252  px_memset(pf->buf, 0, pf->buflen);
253  px_free(pf->buf);
254  }
255 
256  px_memset(pf, 0, sizeof(*pf));
257  px_free(pf);
258 }
void(* free)(void *priv)
Definition: mbuf.h:73
#define px_free(p)
Definition: px.h:47
const PullFilterOps * op
Definition: mbuf.c:198
uint8 * buf
Definition: mbuf.c:200
int buflen
Definition: mbuf.c:199
void * priv
Definition: mbuf.c:202
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
int pullf_read ( PullFilter pf,
int  len,
uint8 **  data_p 
)

Definition at line 262 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().

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

Definition at line 317 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().

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

Definition at line 279 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().

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

Definition at line 546 of file mbuf.c.

References arg, and mbuf_append().

547 {
548  int res = 0;
549  MBuf *mbuf = arg;
550 
551  if (len > 0)
552  res = mbuf_append(mbuf, data, len);
553  return res < 0 ? res : 0;
554 }
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:102
Definition: mbuf.c:39
void * arg
int pushf_create ( PushFilter **  mp_p,
const PushFilterOps op,
void *  init_arg,
PushFilter next 
)

Definition at line 373 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilterOps::init, next, PushFilter::next, NULL, PushFilter::op, PushFilter::pos, PushFilter::priv, and px_alloc.

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

374 {
375  PushFilter *mp;
376  void *priv;
377  int res;
378 
379  if (op->init != NULL)
380  {
381  res = op->init(next, init_arg, &priv);
382  if (res < 0)
383  return res;
384  }
385  else
386  {
387  priv = init_arg;
388  res = 0;
389  }
390 
391  mp = px_alloc(sizeof(*mp));
392  memset(mp, 0, sizeof(*mp));
393  mp->block_size = res;
394  mp->op = op;
395  mp->priv = priv;
396  mp->next = next;
397  if (mp->block_size > 0)
398  {
399  mp->buf = px_alloc(mp->block_size);
400  mp->pos = 0;
401  }
402  else
403  {
404  mp->buf = NULL;
405  mp->pos = 0;
406  }
407  *mp_p = mp;
408  return 0;
409 }
static int32 next
Definition: blutils.c:210
const PushFilterOps * op
Definition: mbuf.c:365
void * priv
Definition: mbuf.c:369
int(* init)(PushFilter *next, void *init_arg, void **priv_p)
Definition: mbuf.h:47
int pos
Definition: mbuf.c:368
PushFilter * next
Definition: mbuf.c:364
#define NULL
Definition: c.h:226
#define px_alloc(s)
Definition: px.h:45
uint8 * buf
Definition: mbuf.c:367
int block_size
Definition: mbuf.c:366
int pushf_create_mbuf_writer ( PushFilter **  res,
MBuf dst 
)

Definition at line 561 of file mbuf.c.

References NULL, and pushf_create().

Referenced by pgp_encrypt().

562 {
563  return pushf_create(res, &mbuf_filter, dst, NULL);
564 }
static const struct PushFilterOps mbuf_filter
Definition: mbuf.c:556
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
#define NULL
Definition: c.h:226
int pushf_flush ( PushFilter mp)

Definition at line 516 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().

517 {
518  int res;
519 
520  while (mp)
521  {
522  if (mp->block_size > 0)
523  {
524  res = wrap_process(mp, mp->buf, mp->pos);
525  if (res < 0)
526  return res;
527  }
528 
529  if (mp->op->flush)
530  {
531  res = mp->op->flush(mp->next, mp->priv);
532  if (res < 0)
533  return res;
534  }
535 
536  mp = mp->next;
537  }
538  return 0;
539 }
const PushFilterOps * op
Definition: mbuf.c:365
static int wrap_process(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:441
void * priv
Definition: mbuf.c:369
int pos
Definition: mbuf.c:368
int(* flush)(PushFilter *next, void *priv)
Definition: mbuf.h:55
PushFilter * next
Definition: mbuf.c:364
uint8 * buf
Definition: mbuf.c:367
int block_size
Definition: mbuf.c:366
void pushf_free ( PushFilter mp)

Definition at line 412 of file mbuf.c.

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

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

413 {
414  if (mp->op->free)
415  mp->op->free(mp->priv);
416 
417  if (mp->buf)
418  {
419  px_memset(mp->buf, 0, mp->block_size);
420  px_free(mp->buf);
421  }
422 
423  px_memset(mp, 0, sizeof(*mp));
424  px_free(mp);
425 }
const PushFilterOps * op
Definition: mbuf.c:365
#define px_free(p)
Definition: px.h:47
void * priv
Definition: mbuf.c:369
void(* free)(void *priv)
Definition: mbuf.h:56
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
uint8 * buf
Definition: mbuf.c:367
int block_size
Definition: mbuf.c:366
void pushf_free_all ( PushFilter mp)

Definition at line 428 of file mbuf.c.

References PushFilter::next, and pushf_free().

Referenced by pgp_encrypt().

429 {
430  PushFilter *tmp;
431 
432  while (mp)
433  {
434  tmp = mp->next;
435  pushf_free(mp);
436  mp = tmp;
437  }
438 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
PushFilter * next
Definition: mbuf.c:364
int pushf_write ( PushFilter mp,
const uint8 data,
int  len 
)

Definition at line 456 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().

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

Definition at line 441 of file mbuf.c.

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

Referenced by pushf_flush(), and pushf_write().

442 {
443  int res;
444 
445  if (mp->op->push != NULL)
446  res = mp->op->push(mp->next, mp->priv, data, len);
447  else
448  res = pushf_write(mp->next, data, len);
449  if (res > 0)
450  return PXE_BUG;
451  return res;
452 }
const PushFilterOps * op
Definition: mbuf.c:365
void * priv
Definition: mbuf.c:369
#define PXE_BUG
Definition: px.h:74
int(* push)(PushFilter *next, void *priv, const uint8 *src, int len)
Definition: mbuf.h:53
PushFilter * next
Definition: mbuf.c:364
#define NULL
Definition: c.h:226
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456

Variable Documentation

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

Definition at line 556 of file mbuf.c.

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

Definition at line 347 of file mbuf.c.