Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions | Variables

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

Defines

#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 struct PullFilterOps mbuf_reader
static struct PushFilterOps mbuf_filter

Define 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(), and px_debug().

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

{
    if (dst->no_write)
    {
        px_debug("mbuf_append: no_write");
        return PXE_BUG;
    }

    prepare_room(dst, len);

    memcpy(dst->data_end, buf, len);
    dst->data_end += len;

    return 0;
}

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

{
    return mbuf->data_end - mbuf->read_pos;
}

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

{
    MBuf       *mbuf;

    if (!len)
        len = 8192;

    mbuf = px_alloc(sizeof *mbuf);
    mbuf->data = px_alloc(len);
    mbuf->buf_end = mbuf->data + len;
    mbuf->data_end = mbuf->data;
    mbuf->read_pos = mbuf->data;

    mbuf->no_write = false;
    mbuf->own_data = true;

    return mbuf;
}

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

{
    MBuf       *mbuf;

    mbuf = px_alloc(sizeof *mbuf);
    mbuf->data = (uint8 *) data;
    mbuf->buf_end = mbuf->data + len;
    mbuf->data_end = mbuf->data + len;
    mbuf->read_pos = mbuf->data;

    mbuf->no_write = true;
    mbuf->own_data = false;

    return mbuf;
}

int mbuf_free ( MBuf mbuf  ) 

Definition at line 68 of file mbuf.c.

References MBuf::buf_end, MBuf::data, MBuf::own_data, and px_free.

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

{
    if (mbuf->own_data)
    {
        memset(mbuf->data, 0, mbuf->buf_end - mbuf->data);
        px_free(mbuf->data);
    }
    px_free(mbuf);
    return 0;
}

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

{
    if (len > mbuf_avail(mbuf))
        len = mbuf_avail(mbuf);

    mbuf->no_write = true;

    *data_p = mbuf->read_pos;
    mbuf->read_pos += len;
    return len;
}

int mbuf_rewind ( MBuf mbuf  ) 

Definition at line 170 of file mbuf.c.

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

{
    mbuf->read_pos = mbuf->data;
    return 0;
}

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

{
    return mbuf->data_end - mbuf->data;
}

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, MBuf::own_data, and MBuf::read_pos.

Referenced by decrypt_internal(), and encrypt_internal().

{
    int         len = mbuf_size(mbuf);

    mbuf->no_write = true;
    mbuf->own_data = false;

    *data_p = mbuf->data;

    mbuf->data = mbuf->data_end = mbuf->read_pos = mbuf->buf_end = NULL;

    return len;
}

int mbuf_tell ( MBuf mbuf  ) 

Definition at line 62 of file mbuf.c.

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

{
    return mbuf->read_pos - mbuf->data;
}

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

{
    uint8      *newbuf;
    unsigned    newlen;

    if (mbuf->data_end + block_len <= mbuf->buf_end)
        return;

    newlen = (mbuf->buf_end - mbuf->data)
        + ((block_len + STEP + STEP - 1) & -STEP);

    newbuf = px_realloc(mbuf->data, newlen);

    mbuf->buf_end = newbuf + newlen;
    mbuf->data_end = newbuf + (mbuf->data_end - mbuf->data);
    mbuf->read_pos = newbuf + (mbuf->read_pos - mbuf->data);
    mbuf->data = newbuf;

    return;
}

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

Definition at line 338 of file mbuf.c.

References mbuf_grab().

{
    MBuf       *mbuf = arg;

    return mbuf_grab(mbuf, len, data_p);
}

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

{
    PullFilter *pf;
    void       *priv;
    int         res;

    if (op->init != NULL)
    {
        res = op->init(&priv, init_arg, src);
        if (res < 0)
            return res;
    }
    else
    {
        priv = init_arg;
        res = 0;
    }

    pf = px_alloc(sizeof(*pf));
    memset(pf, 0, sizeof(*pf));
    pf->buflen = res;
    pf->op = op;
    pf->priv = priv;
    pf->src = src;
    if (pf->buflen > 0)
    {
        pf->buf = px_alloc(pf->buflen);
        pf->pos = 0;
    }
    else
    {
        pf->buf = NULL;
        pf->pos = 0;
    }
    *pf_p = pf;
    return 0;
}

int pullf_create_mbuf_reader ( PullFilter **  mp_p,
MBuf src 
)

Definition at line 351 of file mbuf.c.

References NULL, and pullf_create().

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

{
    return pullf_create(mp_p, &mbuf_reader, src, NULL);
}

void pullf_free ( PullFilter pf  ) 
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_literal_data(), pgp_expect_packet_end(), pgp_parse_pkt_hdr(), pgp_skip_packet(), pktreader_pull(), process_data_packets(), pullf_read(), and pullf_read_max().

{
    int         res;

    if (pf->op->pull)
    {
        if (pf->buflen && len > pf->buflen)
            len = pf->buflen;
        res = pf->op->pull(pf->priv, pf->src, len, data_p,
                           pf->buf, pf->buflen);
    }
    else
        res = pullf_read(pf->src, len, data_p);
    return res;
}

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

Definition at line 316 of file mbuf.c.

References pullf_read_max(), and px_debug().

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

{
    int         res;
    uint8      *p;

    res = pullf_read_max(src, len, &p, dst);
    if (res < 0)
        return res;
    if (res != len)
    {
        px_debug("pullf_read_fixed: need=%d got=%d", len, res);
        return PXE_MBUF_SHORT_READ;
    }
    if (p != dst)
        memcpy(dst, p, len);
    return 0;
}

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

Definition at line 279 of file mbuf.c.

References pullf_read().

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

{
    int         res,
                total;
    uint8      *tmp;

    res = pullf_read(pf, len, data_p);
    if (res <= 0 || res == len)
        return res;

    /* read was shorter, use tmpbuf */
    memcpy(tmpbuf, *data_p, res);
    *data_p = tmpbuf;
    len -= res;
    total = res;

    while (len > 0)
    {
        res = pullf_read(pf, len, &tmp);
        if (res < 0)
        {
            /* so the caller must clear only on success */
            memset(tmpbuf, 0, total);
            return res;
        }
        if (res == 0)
            break;
        memcpy(tmpbuf + total, tmp, res);
        total += res;
    }
    return total;
}

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

Definition at line 545 of file mbuf.c.

References mbuf_append().

{
    int         res = 0;
    MBuf       *mbuf = arg;

    if (len > 0)
        res = mbuf_append(mbuf, data, len);
    return res < 0 ? res : 0;
}

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

Definition at line 372 of file mbuf.c.

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

{
    PushFilter *mp;
    void       *priv;
    int         res;

    if (op->init != NULL)
    {
        res = op->init(next, init_arg, &priv);
        if (res < 0)
            return res;
    }
    else
    {
        priv = init_arg;
        res = 0;
    }

    mp = px_alloc(sizeof(*mp));
    memset(mp, 0, sizeof(*mp));
    mp->block_size = res;
    mp->op = op;
    mp->priv = priv;
    mp->next = next;
    if (mp->block_size > 0)
    {
        mp->buf = px_alloc(mp->block_size);
        mp->pos = 0;
    }
    else
    {
        mp->buf = NULL;
        mp->pos = 0;
    }
    *mp_p = mp;
    return 0;
}

int pushf_create_mbuf_writer ( PushFilter **  res,
MBuf dst 
)

Definition at line 560 of file mbuf.c.

References NULL, and pushf_create().

Referenced by pgp_encrypt().

{
    return pushf_create(res, &mbuf_filter, dst, NULL);
}

int pushf_flush ( PushFilter mp  ) 

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

{
    int         res;

    while (mp)
    {
        if (mp->block_size > 0)
        {
            res = wrap_process(mp, mp->buf, mp->pos);
            if (res < 0)
                return res;
        }

        if (mp->op->flush)
        {
            res = mp->op->flush(mp->next, mp->priv);
            if (res < 0)
                return res;
        }

        mp = mp->next;
    }
    return 0;
}

void pushf_free ( PushFilter mp  ) 

Definition at line 411 of file mbuf.c.

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

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

{
    if (mp->op->free)
        mp->op->free(mp->priv);

    if (mp->buf)
    {
        memset(mp->buf, 0, mp->block_size);
        px_free(mp->buf);
    }

    memset(mp, 0, sizeof(*mp));
    px_free(mp);
}

void pushf_free_all ( PushFilter mp  ) 

Definition at line 427 of file mbuf.c.

References PushFilter::next, and pushf_free().

Referenced by pgp_encrypt().

{
    PushFilter *tmp;

    while (mp)
    {
        tmp = mp->next;
        pushf_free(mp);
        mp = tmp;
    }
}

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

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

{
    int         need,
                res;

    /*
     * no buffering
     */
    if (mp->block_size <= 0)
        return wrap_process(mp, data, len);

    /*
     * try to empty buffer
     */
    need = mp->block_size - mp->pos;
    if (need > 0)
    {
        if (len < need)
        {
            memcpy(mp->buf + mp->pos, data, len);
            mp->pos += len;
            return 0;
        }
        memcpy(mp->buf + mp->pos, data, need);
        len -= need;
        data += need;
    }

    /*
     * buffer full, process
     */
    res = wrap_process(mp, mp->buf, mp->block_size);
    if (res < 0)
        return res;
    mp->pos = 0;

    /*
     * now process directly from data
     */
    while (len > 0)
    {
        if (len > mp->block_size)
        {
            res = wrap_process(mp, data, mp->block_size);
            if (res < 0)
                return res;
            data += mp->block_size;
            len -= mp->block_size;
        }
        else
        {
            memcpy(mp->buf, data, len);
            mp->pos += len;
            break;
        }
    }
    return 0;
}

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

Definition at line 440 of file mbuf.c.

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

Referenced by pushf_flush(), and pushf_write().

{
    int         res;

    if (mp->op->push != NULL)
        res = mp->op->push(mp->next, mp->priv, data, len);
    else
        res = pushf_write(mp->next, data, len);
    if (res > 0)
        return PXE_BUG;
    return res;
}


Variable Documentation

struct PushFilterOps mbuf_filter [static]
Initial value:
 {
    NULL, push_into_mbuf, NULL, NULL
}

Definition at line 555 of file mbuf.c.

struct PullFilterOps mbuf_reader [static]
Initial value:
 {
    NULL, pull_from_mbuf, NULL
}

Definition at line 346 of file mbuf.c.