5 * Reading and writing packet buffers
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /*----- Header files ------------------------------------------------------*/
49 /*----- Data structures ---------------------------------------------------*/
53 * Buffers provide a simple stream-like interface for building and parsing
58 octet *base, *p, *limit; /* Pointers to the buffer */
59 unsigned f; /* Various flags */
62 #define BF_BROKEN 1u /* Buffer is broken */
64 /*----- Useful macros -----------------------------------------------------*/
66 #define BBASE(b) ((b)->base)
67 #define BLIM(b) ((b)->limit)
68 #define BCUR(b) ((b)->p)
69 #define BSZ(b) ((b)->limit - (b)->base)
70 #define BLEN(b) ((b)->p - (b)->base)
71 #define BLEFT(b) ((b)->limit - (b)->p)
72 #define BSTEP(b, sz) ((b)->p += (sz))
73 #define BBAD(b) ((b)->f & BF_BROKEN)
74 #define BOK(b) (!BBAD(b))
76 #define BENSURE(b, sz) \
77 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
79 #define BUF_DOSUFFIXES(_) DOUINTCONV(_) _(z, z, z)
81 /*----- Functions provided ------------------------------------------------*/
83 /* --- @buf_init@ --- *
85 * Arguments: @buf *b@ = pointer to a buffer block
86 * @void *p@ = pointer to a buffer
87 * @size_t sz@ = size of the buffer
91 * Use: Initializes the buffer block appropriately.
94 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
96 /* --- @buf_break@ --- *
98 * Arguments: @buf *b@ = pointer to a buffer block
100 * Returns: Some negative value.
102 * Use: Marks a buffer as broken.
105 extern int buf_break(buf */*b*/);
107 /* --- @buf_flip@ --- *
109 * Arguments: @buf *b@ = pointer to a buffer block
113 * Use: Flips a buffer so that if you've just been writing to it,
114 * you can now read from the bit you've written.
117 extern void buf_flip(buf */*b*/);
119 /* --- @buf_ensure@ --- *
121 * Arguments: @buf *b@ = pointer to a buffer block
122 * @size_t sz@ = size of data wanted
124 * Returns: Zero if it worked, nonzero if there wasn't enough space.
126 * Use: Ensures that there are @sz@ bytes still in the buffer.
129 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
131 /* --- @buf_get@ --- *
133 * Arguments: @buf *b@ = pointer to a buffer block
134 * @size_t sz@ = size of the buffer
136 * Returns: Pointer to the place in the buffer.
138 * Use: Reserves a space in the buffer of the requested size, and
139 * returns its start address.
142 extern void *buf_get(buf */*b*/, size_t /*sz*/);
144 /* --- @buf_put@ --- *
146 * Arguments: @buf *b@ = pointer to a buffer block
147 * @const void *p@ = pointer to a buffer
148 * @size_t sz@ = size of the buffer
150 * Returns: Zero if it worked, nonzero if there wasn't enough space.
152 * Use: Fetches data from some place and puts it in the buffer
155 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
157 /* --- @buf_getbyte@ --- *
159 * Arguments: @buf *b@ = pointer to a buffer block
161 * Returns: A byte, or less than zero if there wasn't a byte there.
163 * Use: Gets a single byte from a buffer.
166 extern int buf_getbyte(buf */*b*/);
168 /* --- @buf_putbyte@ --- *
170 * Arguments: @buf *b@ = pointer to a buffer block
171 * @int ch@ = byte to write
173 * Returns: Zero if OK, nonzero if there wasn't enough space.
175 * Use: Puts a single byte in a buffer.
178 extern int buf_putbyte(buf */*b*/, int /*ch*/);
180 /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
182 * Arguments: @buf *b@ = pointer to a buffer block
183 * @uintSZ *w@ = where to put the word
185 * Returns: Zero if OK, or nonzero if there wasn't a word there.
187 * Use: Gets a word of appropriate size and order from a buffer.
190 #define BUF_DECL_GETU_(n, W, w) \
191 extern int buf_getu##w(buf */*b*/, uint##n */*w*/);
192 DOUINTCONV(BUF_DECL_GETU_)
194 /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
196 * Arguments: @buf *b@ = pointer to a buffer block
197 * @uintSZ w@ = word to write
199 * Returns: Zero if OK, or nonzero if there wasn't enough space
201 * Use: Puts a word into a buffer with appropriate size and order.
204 #define BUF_DECL_PUTU_(n, W, w) \
205 extern int buf_putu##w(buf */*b*/, uint##n /*w*/);
206 DOUINTCONV(BUF_DECL_PUTU_)
208 /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
210 * Arguments: @buf *b@ = pointer to a buffer block
211 * @size_t *nn@ = where to put the length
213 * Returns: Pointer to the buffer data, or null.
215 * Use: Gets a chunk of memory from a buffer. The suffix is the
216 * width and byte order of the length; @z@ means null-
220 #define BUF_DECL_GETMEM_(n, W, w) \
221 extern void *buf_getmem##w(buf */*b*/, size_t */*nn*/);
222 BUF_DOSUFFIXES(BUF_DECL_GETMEM_)
224 /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
226 * Arguments: @buf *b@ = pointer to a buffer block
227 * @const void *p@ = pointer to data to write
228 * @size_t n@ = length to write
230 * Returns: Zero if OK, nonzero if there wasn't enough space.
232 * Use: Writes a chunk of data to a buffer. The suffix is the
233 * width and byte order of the length; @z@ means null-
237 #define BUF_DECL_PUTMEM_(n, W, w) \
238 extern int buf_putmem##w(buf */*b*/, const void */*p*/, size_t /*nn*/);
239 BUF_DOSUFFIXES(BUF_DECL_PUTMEM_)
241 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
243 * Arguments: @buf *b@ = pointer to a buffer block
244 * @buf *bb@ = where to put the result
246 * Returns: Zero if it worked, nonzero if there wasn't enough space.
248 * Use: Gets a block of data from a buffer, and writes its bounds to
252 #define BUF_DECL_GETBUF_(n, W, w) \
253 extern int buf_getbuf##w(buf */*b*/, buf */*bb*/);
254 BUF_DOSUFFIXES(BUF_DECL_GETBUF_)
256 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
258 * Arguments: @buf *b@ = pointer to a buffer block
259 * @buf *bb@ = buffer to write
261 * Returns: Zero if it worked, nonzero if there wasn't enough space.
263 * Use: Puts the contents of a buffer to a buffer.
266 #define BUF_DECL_PUTBUF_(n, W, w) \
267 extern int buf_putbuf##w(buf */*b*/, buf */*bb*/);
268 BUF_DOSUFFIXES(BUF_DECL_PUTBUF_)
270 /* --- @buf_getdstr{8,{16,24,32,64}{,l,b},z} --- *
272 * Arguments: @buf *b@ = pointer to a buffer block
273 * @dstr *d@ = where to put the result
275 * Returns: Zero if it worked, nonzero if there wasn't enough space.
277 * Use: Gets a block of data from a buffer, and writes its contents
281 #define BUF_DECL_GETDSTR_(n, W, w) \
282 extern int buf_getdstr##w(buf */*b*/, dstr */*d*/);
283 BUF_DOSUFFIXES(BUF_DECL_GETDSTR_)
285 /* --- @buf_putdstr{8,{16,24,32,64}{,l,b},z} --- *
287 * Arguments: @buf *b@ = pointer to a buffer block
288 * @dstr *d@ = string to write
290 * Returns: Zero if it worked, nonzero if there wasn't enough space.
292 * Use: Puts a dynamic string to a buffer.
295 #define BUF_DECL_PUTDSTR_(n, W, w) \
296 extern int buf_putdstr##w(buf */*b*/, dstr */*d*/);
297 BUF_DOSUFFIXES(BUF_DECL_PUTDSTR_)
299 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
301 * Arguments: @buf *b@ = pointer to a buffer block
302 * @const char *p@ = string to write
304 * Returns: Zero if it worked, nonzero if there wasn't enough space.
306 * Use: Puts a null-terminated string to a buffer.
309 #define BUF_DECL_PUTSTR_(n, W, w) \
310 extern int buf_putstr##w(buf */*b*/, const char */*p*/); \
311 BUF_DOSUFFIXES(BUF_DECL_PUTSTR_)
313 /*----- That's all, folks -------------------------------------------------*/