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