| 1 |
/* |
| 2 |
* mbuf.h |
| 3 |
* Memory buffer operations. |
| 4 |
* |
| 5 |
* Copyright (c) 2005 Marko Kreen |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
* SUCH DAMAGE. |
| 28 |
* |
| 29 |
* contrib/pgcrypto/mbuf.h |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifndef __PX_MBUF_H |
| 33 |
#define __PX_MBUF_H |
| 34 |
|
| 35 |
typedef struct MBuf MBuf; |
| 36 |
typedef struct PushFilter PushFilter; |
| 37 |
typedef struct PullFilter PullFilter; |
| 38 |
typedef struct PushFilterOps PushFilterOps; |
| 39 |
typedef struct PullFilterOps PullFilterOps; |
| 40 |
|
| 41 |
struct PushFilterOps |
| 42 |
{ |
| 43 |
/* |
| 44 |
* should return needed buffer size, 0- no buffering, <0 on error if NULL, |
| 45 |
* no buffering, and priv=init_arg |
| 46 |
*/ |
| 47 |
int (*init) (PushFilter *next, void *init_arg, void **priv_p); |
| 48 |
|
| 49 |
/* |
| 50 |
* send data to next. should consume all? if null, it will be simply |
| 51 |
* copied (in-place) returns 0 on error |
| 52 |
*/ |
| 53 |
int (*push) (PushFilter *next, void *priv, |
| 54 |
const uint8 *src, int len); |
| 55 |
int (*flush) (PushFilter *next, void *priv); |
| 56 |
void (*free) (void *priv); |
| 57 |
}; |
| 58 |
|
| 59 |
struct PullFilterOps |
| 60 |
{ |
| 61 |
/* |
| 62 |
* should return needed buffer size, 0- no buffering, <0 on error if NULL, |
| 63 |
* no buffering, and priv=init_arg |
| 64 |
*/ |
| 65 |
int (*init) (void **priv_p, void *init_arg, PullFilter *src); |
| 66 |
|
| 67 |
/* |
| 68 |
* request data from src, put result ptr to data_p can use ptr from src or |
| 69 |
* use buf as work area if NULL in-place copy |
| 70 |
*/ |
| 71 |
int (*pull) (void *priv, PullFilter *src, int len, |
| 72 |
uint8 **data_p, uint8 *buf, int buflen); |
| 73 |
void (*free) (void *priv); |
| 74 |
}; |
| 75 |
|
| 76 |
/* |
| 77 |
* Memory buffer |
| 78 |
*/ |
| 79 |
MBuf *mbuf_create(int len); |
| 80 |
MBuf *mbuf_create_from_data(uint8 *data, int len); |
| 81 |
int mbuf_avail(MBuf *mbuf); |
| 82 |
int mbuf_size(MBuf *mbuf); |
| 83 |
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p); |
| 84 |
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p); |
| 85 |
int mbuf_append(MBuf *dst, const uint8 *buf, int len); |
| 86 |
int mbuf_free(MBuf *mbuf); |
| 87 |
|
| 88 |
/* |
| 89 |
* Push filter |
| 90 |
*/ |
| 91 |
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, |
| 92 |
void *init_arg, PushFilter *next); |
| 93 |
int pushf_write(PushFilter *mp, const uint8 *data, int len); |
| 94 |
void pushf_free_all(PushFilter *mp); |
| 95 |
void pushf_free(PushFilter *mp); |
| 96 |
int pushf_flush(PushFilter *mp); |
| 97 |
|
| 98 |
int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst); |
| 99 |
|
| 100 |
/* |
| 101 |
* Pull filter |
| 102 |
*/ |
| 103 |
int pullf_create(PullFilter **pf_p, const PullFilterOps *op, |
| 104 |
void *init_arg, PullFilter *src); |
| 105 |
int pullf_read(PullFilter *pf, int len, uint8 **data_p); |
| 106 |
int pullf_read_max(PullFilter *pf, int len, |
| 107 |
uint8 **data_p, uint8 *tmpbuf); |
| 108 |
void pullf_free(PullFilter *pf); |
| 109 |
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst); |
| 110 |
|
| 111 |
int pullf_create_mbuf_reader(PullFilter **mp_p, MBuf *src); |
| 112 |
|
| 113 |
#define GETBYTE(pf, dst) \ |
| 114 |
do { \ |
| 115 |
uint8 __b; \ |
| 116 |
int __res = pullf_read_fixed(pf, 1, &__b); \ |
| 117 |
if (__res < 0) \ |
| 118 |
return __res; \ |
| 119 |
(dst) = __b; \ |
| 120 |
} while (0) |
| 121 |
|
| 122 |
#endif /* __PX_MBUF_H */ |