PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mbuf.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PushFilterOps
 
struct  PullFilterOps
 

Macros

#define GETBYTE(pf, dst)
 

Typedefs

typedef struct MBuf MBuf
 
typedef struct PushFilter PushFilter
 
typedef struct PullFilter PullFilter
 
typedef struct PushFilterOps PushFilterOps
 
typedef struct PullFilterOps PullFilterOps
 

Functions

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

Macro Definition Documentation

#define GETBYTE (   pf,
  dst 
)
Value:
do { \
uint8 __b; \
int __res = pullf_read_fixed(pf, 1, &__b); \
if (__res < 0) \
return __res; \
(dst) = __b; \
} while (0)
unsigned char uint8
Definition: c.h:266
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:317

Definition at line 115 of file mbuf.h.

Typedef Documentation

typedef struct MBuf MBuf

Definition at line 35 of file mbuf.h.

Definition at line 37 of file mbuf.h.

Definition at line 39 of file mbuf.h.

Definition at line 36 of file mbuf.h.

Definition at line 38 of file mbuf.h.

Function Documentation

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

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:66
#define PXE_BUG
Definition: px.h:73
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:44
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:266
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:44
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:46
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:229
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
int pullf_create ( PullFilter **  res,
const PullFilterOps ops,
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
#define NULL
Definition: c.h:229
int buflen
Definition: mbuf.c:199
#define px_alloc(s)
Definition: px.h:44
void * priv
Definition: mbuf.c:202
int pullf_create_mbuf_reader ( PullFilter **  pf_p,
MBuf mbuf 
)

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:229
void pullf_free ( PullFilter mp)

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 }
#define px_free(p)
Definition: px.h:46
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
int pullf_read ( PullFilter mp,
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
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:81
unsigned char uint8
Definition: c.h:266
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 mp,
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:266
static StringInfoData tmpbuf
Definition: walsender.c:155
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
int pushf_create ( PushFilter **  res,
const PushFilterOps ops,
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 pos
Definition: mbuf.c:368
PushFilter * next
Definition: mbuf.c:364
#define NULL
Definition: c.h:229
#define px_alloc(s)
Definition: px.h:44
uint8 * buf
Definition: mbuf.c:367
int block_size
Definition: mbuf.c:366
int pushf_create_mbuf_writer ( PushFilter **  mp_p,
MBuf mbuf 
)

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:229
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:46
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