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:67
#define PXE_BUG
Definition: px.h:70
uint8 * data_end
Definition: mbuf.c:42
bool no_write
Definition: mbuf.c:45
void px_debug(const char *fmt,...)
Definition: px.c:152
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, px_alloc, 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 = px_alloc(sizeof *mbuf);
125  mbuf->data = px_alloc(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
#define px_alloc(s)
Definition: px.h:44
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, px_alloc, and MBuf::read_pos.

Referenced by create_mbuf_from_vardata(), and decrypt_internal().

138 {
139  MBuf *mbuf;
140 
141  mbuf = px_alloc(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:357
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_free()

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:126

◆ 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, 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 }
#define STEP
Definition: mbuf.c:37
unsigned char uint8
Definition: c.h:357
uint8 * buf_end
Definition: mbuf.c:44
#define px_realloc(p, s)
Definition: px.h:45
uint8 * read_pos
Definition: mbuf.c:43
uint8 * data_end
Definition: mbuf.c:42
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 337 of file mbuf.c.

References arg, and mbuf_grab().

339 {
340  MBuf *mbuf = arg;
341 
342  return mbuf_grab(mbuf, len, data_p);
343 }
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, 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().

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 = px_alloc(sizeof(*pf));
223  memset(pf, 0, sizeof(*pf));
224  pf->buflen = res;
225  pf->op = op;
226  pf->priv = priv;
227  pf->src = src;
228  if (pf->buflen > 0)
229  {
230  pf->buf = px_alloc(pf->buflen);
231  pf->pos = 0;
232  }
233  else
234  {
235  pf->buf = NULL;
236  pf->pos = 0;
237  }
238  *pf_p = pf;
239  return 0;
240 }
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
int buflen
Definition: mbuf.c:197
#define px_alloc(s)
Definition: px.h:44
void * priv
Definition: mbuf.c:200

◆ pullf_create_mbuf_reader()

int pullf_create_mbuf_reader ( PullFilter **  mp_p,
MBuf src 
)

Definition at line 350 of file mbuf.c.

References pullf_create().

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

351 {
352  return pullf_create(mp_p, &mbuf_reader, src, NULL);
353 }
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:345

◆ pullf_free()

void pullf_free ( PullFilter pf)

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

244 {
245  if (pf->op->free)
246  pf->op->free(pf->priv);
247 
248  if (pf->buf)
249  {
250  px_memset(pf->buf, 0, pf->buflen);
251  px_free(pf->buf);
252  }
253 
254  px_memset(pf, 0, sizeof(*pf));
255  px_free(pf);
256 }
#define px_free(p)
Definition: px.h:46
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:126

◆ pullf_read()

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

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

261 {
262  int res;
263 
264  if (pf->op->pull)
265  {
266  if (pf->buflen && len > pf->buflen)
267  len = pf->buflen;
268  res = pf->op->pull(pf->priv, pf->src, len, data_p,
269  pf->buf, pf->buflen);
270  }
271  else
272  res = pullf_read(pf->src, len, data_p);
273  return res;
274 }
int pullf_read(PullFilter *pf, int len, uint8 **data_p)
Definition: mbuf.c:260
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 315 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().

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

◆ pullf_read_max()

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

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

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

◆ push_into_mbuf()

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

Definition at line 544 of file mbuf.c.

References arg, and mbuf_append().

545 {
546  int res = 0;
547  MBuf *mbuf = arg;
548 
549  if (len > 0)
550  res = mbuf_append(mbuf, data, len);
551  return res < 0 ? res : 0;
552 }
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 371 of file mbuf.c.

References PushFilter::block_size, PushFilter::buf, PushFilterOps::init, next, PushFilter::next, 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().

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

◆ pushf_create_mbuf_writer()

int pushf_create_mbuf_writer ( PushFilter **  res,
MBuf dst 
)

Definition at line 559 of file mbuf.c.

References pushf_create().

Referenced by pgp_encrypt().

560 {
561  return pushf_create(res, &mbuf_filter, dst, NULL);
562 }
static const struct PushFilterOps mbuf_filter
Definition: mbuf.c:554
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:371

◆ pushf_flush()

int pushf_flush ( PushFilter mp)

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

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

◆ pushf_free()

void pushf_free ( PushFilter mp)

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

411 {
412  if (mp->op->free)
413  mp->op->free(mp->priv);
414 
415  if (mp->buf)
416  {
417  px_memset(mp->buf, 0, mp->block_size);
418  px_free(mp->buf);
419  }
420 
421  px_memset(mp, 0, sizeof(*mp));
422  px_free(mp);
423 }
const PushFilterOps * op
Definition: mbuf.c:363
#define px_free(p)
Definition: px.h:46
void * priv
Definition: mbuf.c:367
void(* free)(void *priv)
Definition: mbuf.h:56
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:126
uint8 * buf
Definition: mbuf.c:365
int block_size
Definition: mbuf.c:364

◆ pushf_free_all()

void pushf_free_all ( PushFilter mp)

Definition at line 426 of file mbuf.c.

References PushFilter::next, and pushf_free().

Referenced by pgp_encrypt().

427 {
428  PushFilter *tmp;
429 
430  while (mp)
431  {
432  tmp = mp->next;
433  pushf_free(mp);
434  mp = tmp;
435  }
436 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:410
PushFilter * next
Definition: mbuf.c:362

◆ pushf_write()

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

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

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

◆ wrap_process()

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

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

440 {
441  int res;
442 
443  if (mp->op->push != NULL)
444  res = mp->op->push(mp->next, mp->priv, data, len);
445  else
446  res = pushf_write(mp->next, data, len);
447  if (res > 0)
448  return PXE_BUG;
449  return res;
450 }
const PushFilterOps * op
Definition: mbuf.c:363
void * priv
Definition: mbuf.c:367
#define PXE_BUG
Definition: px.h:70
PushFilter * next
Definition: mbuf.c:362
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:454

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:544

Definition at line 554 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:337

Definition at line 345 of file mbuf.c.