Header And Logo

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

mbuf.h

Go to the documentation of this file.
00001 /*
00002  * mbuf.h
00003  *      Memory buffer operations.
00004  *
00005  * Copyright (c) 2005 Marko Kreen
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * contrib/pgcrypto/mbuf.h
00030  */
00031 
00032 #ifndef __PX_MBUF_H
00033 #define __PX_MBUF_H
00034 
00035 typedef struct MBuf MBuf;
00036 typedef struct PushFilter PushFilter;
00037 typedef struct PullFilter PullFilter;
00038 typedef struct PushFilterOps PushFilterOps;
00039 typedef struct PullFilterOps PullFilterOps;
00040 
00041 struct PushFilterOps
00042 {
00043     /*
00044      * should return needed buffer size, 0- no buffering, <0 on error if NULL,
00045      * no buffering, and priv=init_arg
00046      */
00047     int         (*init) (PushFilter *next, void *init_arg, void **priv_p);
00048 
00049     /*
00050      * send data to next.  should consume all? if null, it will be simply
00051      * copied (in-place) returns 0 on error
00052      */
00053     int         (*push) (PushFilter *next, void *priv,
00054                                      const uint8 *src, int len);
00055     int         (*flush) (PushFilter *next, void *priv);
00056     void        (*free) (void *priv);
00057 };
00058 
00059 struct PullFilterOps
00060 {
00061     /*
00062      * should return needed buffer size, 0- no buffering, <0 on error if NULL,
00063      * no buffering, and priv=init_arg
00064      */
00065     int         (*init) (void **priv_p, void *init_arg, PullFilter *src);
00066 
00067     /*
00068      * request data from src, put result ptr to data_p can use ptr from src or
00069      * use buf as work area if NULL in-place copy
00070      */
00071     int         (*pull) (void *priv, PullFilter *src, int len,
00072                                      uint8 **data_p, uint8 *buf, int buflen);
00073     void        (*free) (void *priv);
00074 };
00075 
00076 /*
00077  * Memory buffer
00078  */
00079 MBuf       *mbuf_create(int len);
00080 MBuf       *mbuf_create_from_data(uint8 *data, int len);
00081 int         mbuf_tell(MBuf *mbuf);
00082 int         mbuf_avail(MBuf *mbuf);
00083 int         mbuf_size(MBuf *mbuf);
00084 int         mbuf_grab(MBuf *mbuf, int len, uint8 **data_p);
00085 int         mbuf_steal_data(MBuf *mbuf, uint8 **data_p);
00086 int         mbuf_append(MBuf *dst, const uint8 *buf, int cnt);
00087 int         mbuf_rewind(MBuf *mbuf);
00088 int         mbuf_free(MBuf *mbuf);
00089 
00090 /*
00091  * Push filter
00092  */
00093 int pushf_create(PushFilter **res, const PushFilterOps *ops, void *init_arg,
00094              PushFilter *next);
00095 int         pushf_write(PushFilter *mp, const uint8 *data, int len);
00096 void        pushf_free_all(PushFilter *mp);
00097 void        pushf_free(PushFilter *mp);
00098 int         pushf_flush(PushFilter *mp);
00099 
00100 int         pushf_create_mbuf_writer(PushFilter **mp_p, MBuf *mbuf);
00101 
00102 /*
00103  * Pull filter
00104  */
00105 int pullf_create(PullFilter **res, const PullFilterOps *ops,
00106              void *init_arg, PullFilter *src);
00107 int         pullf_read(PullFilter *mp, int len, uint8 **data_p);
00108 int pullf_read_max(PullFilter *mp, int len,
00109                uint8 **data_p, uint8 *tmpbuf);
00110 void        pullf_free(PullFilter *mp);
00111 int         pullf_read_fixed(PullFilter *src, int len, uint8 *dst);
00112 
00113 int         pullf_create_mbuf_reader(PullFilter **pf_p, MBuf *mbuf);
00114 
00115 #define GETBYTE(pf, dst) \
00116     do { \
00117         uint8 __b; \
00118         int __res = pullf_read_fixed(pf, 1, &__b); \
00119         if (__res < 0) \
00120             return __res; \
00121         (dst) = __b; \
00122     } while (0)
00123 
00124 #endif   /* __PX_MBUF_H */