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_tell(MBuf *mbuf); 82 int mbuf_avail(MBuf *mbuf); 83 int mbuf_size(MBuf *mbuf); 84 int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p); 85 int mbuf_steal_data(MBuf *mbuf, uint8 **data_p); 86 int mbuf_append(MBuf *dst, const uint8 *buf, int cnt); 87 int mbuf_rewind(MBuf *mbuf); 88 int mbuf_free(MBuf *mbuf); 89 90 /* 91 * Push filter 92 */ 93 int pushf_create(PushFilter **res, const PushFilterOps *ops, void *init_arg, 94 PushFilter *next); 95 int pushf_write(PushFilter *mp, const uint8 *data, int len); 96 void pushf_free_all(PushFilter *mp); 97 void pushf_free(PushFilter *mp); 98 int pushf_flush(PushFilter *mp); 99 100 int pushf_create_mbuf_writer(PushFilter **mp_p, MBuf *mbuf); 101 102 /* 103 * Pull filter 104 */ 105 int pullf_create(PullFilter **res, const PullFilterOps *ops, 106 void *init_arg, PullFilter *src); 107 int pullf_read(PullFilter *mp, int len, uint8 **data_p); 108 int pullf_read_max(PullFilter *mp, int len, 109 uint8 **data_p, uint8 *tmpbuf); 110 void pullf_free(PullFilter *mp); 111 int pullf_read_fixed(PullFilter *src, int len, uint8 *dst); 112 113 int pullf_create_mbuf_reader(PullFilter **pf_p, MBuf *mbuf); 114 115 #define GETBYTE(pf, dst) \ 116 do { \ 117 uint8 __b; \ 118 int __res = pullf_read_fixed(pf, 1, &__b); \ 119 if (__res < 0) \ 120 return __res; \ 121 (dst) = __b; \ 122 } while (0) 123 124 #endif /* __PX_MBUF_H */ 125