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