PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_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 len)
 
int mbuf_free (MBuf *mbuf)
 
int pushf_create (PushFilter **mp_p, const PushFilterOps *op, 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 **res, MBuf *dst)
 
int pullf_create (PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
 
int pullf_read (PullFilter *pf, int len, uint8 **data_p)
 
int pullf_read_max (PullFilter *pf, int len, uint8 **data_p, uint8 *tmpbuf)
 
void pullf_free (PullFilter *pf)
 
int pullf_read_fixed (PullFilter *src, int len, uint8 *dst)
 
int pullf_create_mbuf_reader (PullFilter **mp_p, MBuf *src)
 

Macro Definition Documentation

◆ GETBYTE

#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)
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:301

Definition at line 113 of file mbuf.h.

Typedef Documentation

◆ MBuf

typedef struct MBuf MBuf

Definition at line 1 of file mbuf.h.

◆ PullFilter

typedef struct PullFilter PullFilter

Definition at line 1 of file mbuf.h.

◆ PullFilterOps

typedef struct PullFilterOps PullFilterOps

Definition at line 1 of file mbuf.h.

◆ PushFilter

typedef struct PushFilter PushFilter

Definition at line 1 of file mbuf.h.

◆ PushFilterOps

typedef struct PushFilterOps PushFilterOps

Definition at line 1 of file mbuf.h.

Function Documentation

◆ mbuf_append()

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

Definition at line 94 of file mbuf.c.

95 {
96  if (dst->no_write)
97  {
98  px_debug("mbuf_append: no_write");
99  return PXE_BUG;
100  }
101 
102  prepare_room(dst, len);
103 
104  memcpy(dst->data_end, buf, len);
105  dst->data_end += len;
106 
107  return 0;
108 }
static void prepare_room(MBuf *mbuf, int block_len)
Definition: mbuf.c:74
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
void px_debug(const char *fmt,...)
Definition: px.c:149
#define PXE_BUG
Definition: px.h:58
bool no_write
Definition: mbuf.c:45
uint8 * data_end
Definition: mbuf.c:42

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

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

◆ mbuf_avail()

int mbuf_avail ( MBuf mbuf)

Definition at line 50 of file mbuf.c.

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

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

Referenced by mbuf_grab(), and pgp_encrypt().

◆ mbuf_create()

MBuf* mbuf_create ( int  len)

Definition at line 111 of file mbuf.c.

112 {
113  MBuf *mbuf;
114 
115  if (!len)
116  len = 8192;
117 
118  mbuf = palloc(sizeof *mbuf);
119  mbuf->data = palloc(len);
120  mbuf->buf_end = mbuf->data + len;
121  mbuf->data_end = mbuf->data;
122  mbuf->read_pos = mbuf->data;
123 
124  mbuf->no_write = false;
125  mbuf->own_data = true;
126 
127  return mbuf;
128 }
void * palloc(Size size)
Definition: mcxt.c:1317
Definition: mbuf.c:40
uint8 * data
Definition: mbuf.c:41
uint8 * buf_end
Definition: mbuf.c:44
bool own_data
Definition: mbuf.c:46

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

Referenced by decrypt_internal(), and encrypt_internal().

◆ mbuf_create_from_data()

MBuf* mbuf_create_from_data ( uint8 data,
int  len 
)

Definition at line 131 of file mbuf.c.

132 {
133  MBuf *mbuf;
134 
135  mbuf = palloc(sizeof *mbuf);
136  mbuf->data = (uint8 *) data;
137  mbuf->buf_end = mbuf->data + len;
138  mbuf->data_end = mbuf->data + len;
139  mbuf->read_pos = mbuf->data;
140 
141  mbuf->no_write = true;
142  mbuf->own_data = false;
143 
144  return mbuf;
145 }
unsigned char uint8
Definition: c.h:516
const void * data

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

Referenced by create_mbuf_from_vardata(), and decrypt_internal().

◆ mbuf_free()

int mbuf_free ( MBuf mbuf)

Definition at line 62 of file mbuf.c.

63 {
64  if (mbuf->own_data)
65  {
66  px_memset(mbuf->data, 0, mbuf->buf_end - mbuf->data);
67  pfree(mbuf->data);
68  }
69  pfree(mbuf);
70  return 0;
71 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123

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

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

◆ mbuf_grab()

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

Definition at line 149 of file mbuf.c.

150 {
151  if (len > mbuf_avail(mbuf))
152  len = mbuf_avail(mbuf);
153 
154  mbuf->no_write = true;
155 
156  *data_p = mbuf->read_pos;
157  mbuf->read_pos += len;
158  return len;
159 }
int mbuf_avail(MBuf *mbuf)
Definition: mbuf.c:50

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

Referenced by pgp_encrypt().

◆ mbuf_size()

int mbuf_size ( MBuf mbuf)

Definition at line 56 of file mbuf.c.

57 {
58  return mbuf->data_end - mbuf->data;
59 }

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

Referenced by mbuf_steal_data().

◆ mbuf_steal_data()

int mbuf_steal_data ( MBuf mbuf,
uint8 **  data_p 
)

Definition at line 162 of file mbuf.c.

163 {
164  int len = mbuf_size(mbuf);
165 
166  mbuf->no_write = true;
167  mbuf->own_data = false;
168 
169  *data_p = mbuf->data;
170 
171  mbuf->data = mbuf->data_end = mbuf->read_pos = mbuf->buf_end = NULL;
172 
173  return len;
174 }
int mbuf_size(MBuf *mbuf)
Definition: mbuf.c:56

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

Referenced by decrypt_internal(), and encrypt_internal().

◆ pullf_create()

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

Definition at line 191 of file mbuf.c.

192 {
193  PullFilter *pf;
194  void *priv;
195  int res;
196 
197  if (op->init != NULL)
198  {
199  res = op->init(&priv, init_arg, src);
200  if (res < 0)
201  return res;
202  }
203  else
204  {
205  priv = init_arg;
206  res = 0;
207  }
208 
209  pf = palloc0(sizeof(*pf));
210  pf->buflen = res;
211  pf->op = op;
212  pf->priv = priv;
213  pf->src = src;
214  if (pf->buflen > 0)
215  {
216  pf->buf = palloc(pf->buflen);
217  pf->pos = 0;
218  }
219  else
220  {
221  pf->buf = NULL;
222  pf->pos = 0;
223  }
224  *pf_p = pf;
225  return 0;
226 }
void * palloc0(Size size)
Definition: mcxt.c:1347
int(* init)(void **priv_p, void *init_arg, PullFilter *src)
Definition: mbuf.h:65
PullFilter * src
Definition: mbuf.c:182
int buflen
Definition: mbuf.c:184
uint8 * buf
Definition: mbuf.c:185
int pos
Definition: mbuf.c:186
void * priv
Definition: mbuf.c:187
const PullFilterOps * op
Definition: mbuf.c:183

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

◆ pullf_create_mbuf_reader()

int pullf_create_mbuf_reader ( PullFilter **  mp_p,
MBuf src 
)

Definition at line 336 of file mbuf.c.

337 {
338  return pullf_create(mp_p, &mbuf_reader, src, NULL);
339 }
int pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
Definition: mbuf.c:191
static const struct PullFilterOps mbuf_reader
Definition: mbuf.c:331

References mbuf_reader, and pullf_create().

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

◆ pullf_free()

void pullf_free ( PullFilter pf)

Definition at line 229 of file mbuf.c.

230 {
231  if (pf->op->free)
232  pf->op->free(pf->priv);
233 
234  if (pf->buf)
235  {
236  px_memset(pf->buf, 0, pf->buflen);
237  pfree(pf->buf);
238  }
239 
240  px_memset(pf, 0, sizeof(*pf));
241  pfree(pf);
242 }
void(* free)(void *priv)
Definition: mbuf.h:73

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().

◆ pullf_read()

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

Definition at line 246 of file mbuf.c.

247 {
248  int res;
249 
250  if (pf->op->pull)
251  {
252  if (pf->buflen && len > pf->buflen)
253  len = pf->buflen;
254  res = pf->op->pull(pf->priv, pf->src, len, data_p,
255  pf->buf, pf->buflen);
256  }
257  else
258  res = pullf_read(pf->src, len, data_p);
259  return res;
260 }
int pullf_read(PullFilter *pf, int len, uint8 **data_p)
Definition: mbuf.c:246
int(* pull)(void *priv, PullFilter *src, int len, uint8 **data_p, uint8 *buf, int buflen)
Definition: mbuf.h:71

References PullFilter::buf, PullFilter::buflen, len, PullFilter::op, PullFilter::priv, PullFilterOps::pull, res, and PullFilter::src.

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

◆ pullf_read_fixed()

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

Definition at line 301 of file mbuf.c.

302 {
303  int res;
304  uint8 *p;
305 
306  res = pullf_read_max(src, len, &p, dst);
307  if (res < 0)
308  return res;
309  if (res != len)
310  {
311  px_debug("pullf_read_fixed: need=%d got=%d", len, res);
312  return PXE_PGP_CORRUPT_DATA;
313  }
314  if (p != dst)
315  memcpy(dst, p, len);
316  return 0;
317 }
int pullf_read_max(PullFilter *pf, int len, uint8 **data_p, uint8 *tmpbuf)
Definition: mbuf.c:263
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:67

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

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().

◆ pullf_read_max()

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

Definition at line 263 of file mbuf.c.

264 {
265  int res,
266  total;
267  uint8 *tmp;
268 
269  res = pullf_read(pf, len, data_p);
270  if (res <= 0 || res == len)
271  return res;
272 
273  /* read was shorter, use tmpbuf */
274  memcpy(tmpbuf, *data_p, res);
275  *data_p = tmpbuf;
276  len -= res;
277  total = res;
278 
279  while (len > 0)
280  {
281  res = pullf_read(pf, len, &tmp);
282  if (res < 0)
283  {
284  /* so the caller must clear only on success */
285  px_memset(tmpbuf, 0, total);
286  return res;
287  }
288  if (res == 0)
289  break;
290  memcpy(tmpbuf + total, tmp, res);
291  total += res;
292  len -= res;
293  }
294  return total;
295 }
static StringInfoData tmpbuf
Definition: walsender.c:170

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

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

◆ pushf_create()

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

Definition at line 357 of file mbuf.c.

358 {
359  PushFilter *mp;
360  void *priv;
361  int res;
362 
363  if (op->init != NULL)
364  {
365  res = op->init(next, init_arg, &priv);
366  if (res < 0)
367  return res;
368  }
369  else
370  {
371  priv = init_arg;
372  res = 0;
373  }
374 
375  mp = palloc0(sizeof(*mp));
376  mp->block_size = res;
377  mp->op = op;
378  mp->priv = priv;
379  mp->next = next;
380  if (mp->block_size > 0)
381  {
382  mp->buf = palloc(mp->block_size);
383  mp->pos = 0;
384  }
385  else
386  {
387  mp->buf = NULL;
388  mp->pos = 0;
389  }
390  *mp_p = mp;
391  return 0;
392 }
static int32 next
Definition: blutils.c:219
int(* init)(PushFilter *next, void *init_arg, void **priv_p)
Definition: mbuf.h:47
void * priv
Definition: mbuf.c:353
int pos
Definition: mbuf.c:352
PushFilter * next
Definition: mbuf.c:348
int block_size
Definition: mbuf.c:350
const PushFilterOps * op
Definition: mbuf.c:349
uint8 * buf
Definition: mbuf.c:351

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

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

◆ pushf_create_mbuf_writer()

int pushf_create_mbuf_writer ( PushFilter **  res,
MBuf dst 
)

Definition at line 544 of file mbuf.c.

545 {
546  return pushf_create(res, &mbuf_filter, dst, NULL);
547 }
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:357
static const struct PushFilterOps mbuf_filter
Definition: mbuf.c:539

References mbuf_filter, pushf_create(), and res.

Referenced by pgp_encrypt().

◆ pushf_flush()

int pushf_flush ( PushFilter mp)

Definition at line 499 of file mbuf.c.

500 {
501  int res;
502 
503  while (mp)
504  {
505  if (mp->block_size > 0)
506  {
507  res = wrap_process(mp, mp->buf, mp->pos);
508  if (res < 0)
509  return res;
510  }
511 
512  if (mp->op->flush)
513  {
514  res = mp->op->flush(mp->next, mp->priv);
515  if (res < 0)
516  return res;
517  }
518 
519  mp = mp->next;
520  }
521  return 0;
522 }
static int wrap_process(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:424
int(* flush)(PushFilter *next, void *priv)
Definition: mbuf.h:55

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

Referenced by pgp_encrypt(), and pgp_write_pubenc_sesskey().

◆ pushf_free()

void pushf_free ( PushFilter mp)

Definition at line 395 of file mbuf.c.

396 {
397  if (mp->op->free)
398  mp->op->free(mp->priv);
399 
400  if (mp->buf)
401  {
402  px_memset(mp->buf, 0, mp->block_size);
403  pfree(mp->buf);
404  }
405 
406  px_memset(mp, 0, sizeof(*mp));
407  pfree(mp);
408 }
void(* free)(void *priv)
Definition: mbuf.h:56

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().

◆ pushf_free_all()

void pushf_free_all ( PushFilter mp)

Definition at line 411 of file mbuf.c.

412 {
413  PushFilter *tmp;
414 
415  while (mp)
416  {
417  tmp = mp->next;
418  pushf_free(mp);
419  mp = tmp;
420  }
421 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:395

References PushFilter::next, and pushf_free().

Referenced by pgp_encrypt().

◆ pushf_write()

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

Definition at line 439 of file mbuf.c.

440 {
441  int need,
442  res;
443 
444  /*
445  * no buffering
446  */
447  if (mp->block_size <= 0)
448  return wrap_process(mp, data, len);
449 
450  /*
451  * try to empty buffer
452  */
453  need = mp->block_size - mp->pos;
454  if (need > 0)
455  {
456  if (len < need)
457  {
458  memcpy(mp->buf + mp->pos, data, len);
459  mp->pos += len;
460  return 0;
461  }
462  memcpy(mp->buf + mp->pos, data, need);
463  len -= need;
464  data += need;
465  }
466 
467  /*
468  * buffer full, process
469  */
470  res = wrap_process(mp, mp->buf, mp->block_size);
471  if (res < 0)
472  return res;
473  mp->pos = 0;
474 
475  /*
476  * now process directly from data
477  */
478  while (len > 0)
479  {
480  if (len > mp->block_size)
481  {
482  res = wrap_process(mp, data, mp->block_size);
483  if (res < 0)
484  return res;
485  data += mp->block_size;
486  len -= mp->block_size;
487  }
488  else
489  {
490  memcpy(mp->buf, data, len);
491  mp->pos += len;
492  break;
493  }
494  }
495  return 0;
496 }

References PushFilter::block_size, PushFilter::buf, data, len, PushFilter::pos, res, 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().