| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Reading and writing packet buffers |
| 4 | * |
| 5 | * (c) 2001 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MLIB_BUF_H |
| 29 | #define MLIB_BUF_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <stddef.h> |
| 38 | |
| 39 | #ifndef MLIB_BITS_H |
| 40 | # include "bits.h" |
| 41 | #endif |
| 42 | |
| 43 | #ifndef MLIB_DSTR_H |
| 44 | # include "dstr.h" |
| 45 | #endif |
| 46 | |
| 47 | /*----- Data structures ---------------------------------------------------*/ |
| 48 | |
| 49 | /* --- Buffers --- * |
| 50 | * |
| 51 | * Buffers provide a simple stream-like interface for building and parsing |
| 52 | * packets. |
| 53 | */ |
| 54 | |
| 55 | typedef struct buf { |
| 56 | octet *base, *p, *limit; /* Pointers to the buffer */ |
| 57 | unsigned f; /* Various flags */ |
| 58 | } buf; |
| 59 | |
| 60 | #define BF_BROKEN 1u /* Buffer is broken */ |
| 61 | |
| 62 | /*----- Useful macros -----------------------------------------------------*/ |
| 63 | |
| 64 | #define BBASE(b) ((b)->base) |
| 65 | #define BLIM(b) ((b)->limit) |
| 66 | #define BCUR(b) ((b)->p) |
| 67 | #define BSZ(b) ((b)->limit - (b)->base) |
| 68 | #define BLEN(b) ((b)->p - (b)->base) |
| 69 | #define BLEFT(b) ((b)->limit - (b)->p) |
| 70 | #define BSTEP(b, sz) ((b)->p += (sz)) |
| 71 | #define BBAD(b) ((b)->f & BF_BROKEN) |
| 72 | #define BOK(b) (!BBAD(b)) |
| 73 | |
| 74 | #define BENSURE(b, sz) \ |
| 75 | (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0) |
| 76 | |
| 77 | #define BUF_DOSUFFIXES(_) DOUINTCONV(_) _(z, z, z) |
| 78 | |
| 79 | /*----- Functions provided ------------------------------------------------*/ |
| 80 | |
| 81 | /* --- @buf_init@ --- * |
| 82 | * |
| 83 | * Arguments: @buf *b@ = pointer to a buffer block |
| 84 | * @void *p@ = pointer to a buffer |
| 85 | * @size_t sz@ = size of the buffer |
| 86 | * |
| 87 | * Returns: --- |
| 88 | * |
| 89 | * Use: Initializes the buffer block appropriately. |
| 90 | */ |
| 91 | |
| 92 | extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/); |
| 93 | |
| 94 | /* --- @buf_break@ --- * |
| 95 | * |
| 96 | * Arguments: @buf *b@ = pointer to a buffer block |
| 97 | * |
| 98 | * Returns: Some negative value. |
| 99 | * |
| 100 | * Use: Marks a buffer as broken. |
| 101 | */ |
| 102 | |
| 103 | extern int buf_break(buf */*b*/); |
| 104 | |
| 105 | /* --- @buf_flip@ --- * |
| 106 | * |
| 107 | * Arguments: @buf *b@ = pointer to a buffer block |
| 108 | * |
| 109 | * Returns: --- |
| 110 | * |
| 111 | * Use: Flips a buffer so that if you've just been writing to it, |
| 112 | * you can now read from the bit you've written. |
| 113 | */ |
| 114 | |
| 115 | extern void buf_flip(buf */*b*/); |
| 116 | |
| 117 | /* --- @buf_ensure@ --- * |
| 118 | * |
| 119 | * Arguments: @buf *b@ = pointer to a buffer block |
| 120 | * @size_t sz@ = size of data wanted |
| 121 | * |
| 122 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 123 | * |
| 124 | * Use: Ensures that there are @sz@ bytes still in the buffer. |
| 125 | */ |
| 126 | |
| 127 | extern int buf_ensure(buf */*b*/, size_t /*sz*/); |
| 128 | |
| 129 | /* --- @buf_get@ --- * |
| 130 | * |
| 131 | * Arguments: @buf *b@ = pointer to a buffer block |
| 132 | * @size_t sz@ = size of the buffer |
| 133 | * |
| 134 | * Returns: Pointer to the place in the buffer. |
| 135 | * |
| 136 | * Use: Reserves a space in the buffer of the requested size, and |
| 137 | * returns its start address. |
| 138 | */ |
| 139 | |
| 140 | extern void *buf_get(buf */*b*/, size_t /*sz*/); |
| 141 | |
| 142 | /* --- @buf_put@ --- * |
| 143 | * |
| 144 | * Arguments: @buf *b@ = pointer to a buffer block |
| 145 | * @const void *p@ = pointer to a buffer |
| 146 | * @size_t sz@ = size of the buffer |
| 147 | * |
| 148 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 149 | * |
| 150 | * Use: Fetches data from some place and puts it in the buffer |
| 151 | */ |
| 152 | |
| 153 | extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/); |
| 154 | |
| 155 | /* --- @buf_getbyte@ --- * |
| 156 | * |
| 157 | * Arguments: @buf *b@ = pointer to a buffer block |
| 158 | * |
| 159 | * Returns: A byte, or less than zero if there wasn't a byte there. |
| 160 | * |
| 161 | * Use: Gets a single byte from a buffer. |
| 162 | */ |
| 163 | |
| 164 | extern int buf_getbyte(buf */*b*/); |
| 165 | |
| 166 | /* --- @buf_putbyte@ --- * |
| 167 | * |
| 168 | * Arguments: @buf *b@ = pointer to a buffer block |
| 169 | * @int ch@ = byte to write |
| 170 | * |
| 171 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 172 | * |
| 173 | * Use: Puts a single byte in a buffer. |
| 174 | */ |
| 175 | |
| 176 | extern int buf_putbyte(buf */*b*/, int /*ch*/); |
| 177 | |
| 178 | /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- * |
| 179 | * |
| 180 | * Arguments: @buf *b@ = pointer to a buffer block |
| 181 | * @uintSZ *w@ = where to put the word |
| 182 | * |
| 183 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 184 | * |
| 185 | * Use: Gets a word of appropriate size and order from a buffer. |
| 186 | */ |
| 187 | |
| 188 | #define BUF_DECL_GETU_(n, W, w) \ |
| 189 | extern int buf_getu##w(buf */*b*/, uint##n */*w*/); |
| 190 | DOUINTCONV(BUF_DECL_GETU_) |
| 191 | |
| 192 | /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- * |
| 193 | * |
| 194 | * Arguments: @buf *b@ = pointer to a buffer block |
| 195 | * @uintSZ w@ = word to write |
| 196 | * |
| 197 | * Returns: Zero if OK, or nonzero if there wasn't enough space |
| 198 | * |
| 199 | * Use: Puts a word into a buffer with appropriate size and order. |
| 200 | */ |
| 201 | |
| 202 | #define BUF_DECL_PUTU_(n, W, w) \ |
| 203 | extern int buf_putu##w(buf */*b*/, uint##n /*w*/); |
| 204 | DOUINTCONV(BUF_DECL_PUTU_) |
| 205 | |
| 206 | /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- * |
| 207 | * |
| 208 | * Arguments: @buf *b@ = pointer to a buffer block |
| 209 | * @size_t *nn@ = where to put the length |
| 210 | * |
| 211 | * Returns: Pointer to the buffer data, or null. |
| 212 | * |
| 213 | * Use: Gets a chunk of memory from a buffer. The suffix is the |
| 214 | * width and byte order of the length; @z@ means null- |
| 215 | * terminated. |
| 216 | */ |
| 217 | |
| 218 | #define BUF_DECL_GETMEM_(n, W, w) \ |
| 219 | extern void *buf_getmem##w(buf */*b*/, size_t */*nn*/); |
| 220 | BUF_DOSUFFIXES(BUF_DECL_GETMEM_) |
| 221 | |
| 222 | /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- * |
| 223 | * |
| 224 | * Arguments: @buf *b@ = pointer to a buffer block |
| 225 | * @const void *p@ = pointer to data to write |
| 226 | * @size_t n@ = length to write |
| 227 | * |
| 228 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 229 | * |
| 230 | * Use: Writes a chunk of data to a buffer. The suffix is the |
| 231 | * width and byte order of the length; @z@ means null- |
| 232 | * terminated. |
| 233 | */ |
| 234 | |
| 235 | #define BUF_DECL_PUTMEM_(n, W, w) \ |
| 236 | extern int buf_putmem##w(buf */*b*/, const void */*p*/, size_t /*nn*/); |
| 237 | BUF_DOSUFFIXES(BUF_DECL_PUTMEM_) |
| 238 | |
| 239 | /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- * |
| 240 | * |
| 241 | * Arguments: @buf *b@ = pointer to a buffer block |
| 242 | * @buf *bb@ = where to put the result |
| 243 | * |
| 244 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 245 | * |
| 246 | * Use: Gets a block of data from a buffer, and writes its bounds to |
| 247 | * another buffer. |
| 248 | */ |
| 249 | |
| 250 | #define BUF_DECL_GETBUF_(n, W, w) \ |
| 251 | extern int buf_getbuf##w(buf */*b*/, buf */*bb*/); |
| 252 | BUF_DOSUFFIXES(BUF_DECL_GETBUF_) |
| 253 | |
| 254 | /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- * |
| 255 | * |
| 256 | * Arguments: @buf *b@ = pointer to a buffer block |
| 257 | * @buf *bb@ = buffer to write |
| 258 | * |
| 259 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 260 | * |
| 261 | * Use: Puts the contents of a buffer to a buffer. |
| 262 | */ |
| 263 | |
| 264 | #define BUF_DECL_PUTBUF_(n, W, w) \ |
| 265 | extern int buf_putbuf##w(buf */*b*/, buf */*bb*/); |
| 266 | BUF_DOSUFFIXES(BUF_DECL_PUTBUF_) |
| 267 | |
| 268 | /* --- @buf_getdstr{8,{16,24,32,64}{,l,b},z} --- * |
| 269 | * |
| 270 | * Arguments: @buf *b@ = pointer to a buffer block |
| 271 | * @dstr *d@ = where to put the result |
| 272 | * |
| 273 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 274 | * |
| 275 | * Use: Gets a block of data from a buffer, and writes its contents |
| 276 | * to a string. |
| 277 | */ |
| 278 | |
| 279 | #define BUF_DECL_GETDSTR_(n, W, w) \ |
| 280 | extern int buf_getdstr##w(buf */*b*/, dstr */*d*/); |
| 281 | BUF_DOSUFFIXES(BUF_DECL_GETDSTR_) |
| 282 | |
| 283 | /* --- @buf_putdstr{8,{16,24,32,64}{,l,b},z} --- * |
| 284 | * |
| 285 | * Arguments: @buf *b@ = pointer to a buffer block |
| 286 | * @dstr *d@ = string to write |
| 287 | * |
| 288 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 289 | * |
| 290 | * Use: Puts a dynamic string to a buffer. |
| 291 | */ |
| 292 | |
| 293 | #define BUF_DECL_PUTDSTR_(n, W, w) \ |
| 294 | extern int buf_putdstr##w(buf */*b*/, dstr */*d*/); |
| 295 | BUF_DOSUFFIXES(BUF_DECL_PUTDSTR_) |
| 296 | |
| 297 | /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- * |
| 298 | * |
| 299 | * Arguments: @buf *b@ = pointer to a buffer block |
| 300 | * @const char *p@ = string to write |
| 301 | * |
| 302 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 303 | * |
| 304 | * Use: Puts a null-terminated string to a buffer. |
| 305 | */ |
| 306 | |
| 307 | #define BUF_DECL_PUTSTR_(n, W, w) \ |
| 308 | extern int buf_putstr##w(buf */*b*/, const char */*p*/); |
| 309 | BUF_DOSUFFIXES(BUF_DECL_PUTSTR_) |
| 310 | |
| 311 | /*----- That's all, folks -------------------------------------------------*/ |
| 312 | |
| 313 | #ifdef __cplusplus |
| 314 | } |
| 315 | #endif |
| 316 | |
| 317 | #endif |