chiark / gitweb /
Various manual fixes.
[mLib] / buf.h
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id$
4 *
5 * Reading and writing packet buffers
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30#ifndef MLIB_BUF_H
31#define MLIB_BUF_H
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
39#include <stddef.h>
40
41#ifndef MLIB_BITS_H
42# include "bits.h"
43#endif
44
45#ifndef MLIB_DSTR_H
46# include "dstr.h"
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
57typedef 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
79#define BUF_DOSUFFIXES(_) DOUINTCONV(_) _(z, z, z)
80
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
94extern 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
105extern 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
117extern 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
129extern 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
142extern 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
155extern 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
166extern 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
178extern int buf_putbyte(buf */*b*/, int /*ch*/);
179
180/* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
181 *
182 * Arguments: @buf *b@ = pointer to a buffer block
183 * @uintSZ *w@ = where to put the word
184 *
185 * Returns: Zero if OK, or nonzero if there wasn't a word there.
186 *
187 * Use: Gets a word of appropriate size and order from a buffer.
188 */
189
190#define BUF_DECL_GETU_(n, W, w) \
191 extern int buf_getu##w(buf */*b*/, uint##n */*w*/);
192DOUINTCONV(BUF_DECL_GETU_)
193
194/* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
195 *
196 * Arguments: @buf *b@ = pointer to a buffer block
197 * @uintSZ w@ = word to write
198 *
199 * Returns: Zero if OK, or nonzero if there wasn't enough space
200 *
201 * Use: Puts a word into a buffer with appropriate size and order.
202 */
203
204#define BUF_DECL_PUTU_(n, W, w) \
205 extern int buf_putu##w(buf */*b*/, uint##n /*w*/);
206DOUINTCONV(BUF_DECL_PUTU_)
207
208/* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
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 *
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.
218 */
219
220#define BUF_DECL_GETMEM_(n, W, w) \
221 extern void *buf_getmem##w(buf */*b*/, size_t */*nn*/);
222BUF_DOSUFFIXES(BUF_DECL_GETMEM_)
223
224/* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
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 *
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.
235 */
236
237#define BUF_DECL_PUTMEM_(n, W, w) \
238 extern int buf_putmem##w(buf */*b*/, const void */*p*/, size_t /*nn*/);
239BUF_DOSUFFIXES(BUF_DECL_PUTMEM_)
240
241/* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
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
249 * another buffer.
250 */
251
252#define BUF_DECL_GETBUF_(n, W, w) \
253 extern int buf_getbuf##w(buf */*b*/, buf */*bb*/);
254BUF_DOSUFFIXES(BUF_DECL_GETBUF_)
255
256/* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
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 *
263 * Use: Puts the contents of a buffer to a buffer.
264 */
265
266#define BUF_DECL_PUTBUF_(n, W, w) \
267 extern int buf_putbuf##w(buf */*b*/, buf */*bb*/);
268BUF_DOSUFFIXES(BUF_DECL_PUTBUF_)
269
270/* --- @buf_getdstr{8,{16,24,32,64}{,l,b},z} --- *
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
278 * to a string.
279 */
280
281#define BUF_DECL_GETDSTR_(n, W, w) \
282 extern int buf_getdstr##w(buf */*b*/, dstr */*d*/);
283BUF_DOSUFFIXES(BUF_DECL_GETDSTR_)
284
285/* --- @buf_putdstr{8,{16,24,32,64}{,l,b},z} --- *
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 *
292 * Use: Puts a dynamic string to a buffer.
293 */
294
295#define BUF_DECL_PUTDSTR_(n, W, w) \
296 extern int buf_putdstr##w(buf */*b*/, dstr */*d*/);
297BUF_DOSUFFIXES(BUF_DECL_PUTDSTR_)
298
299/* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
300 *
301 * Arguments: @buf *b@ = pointer to a buffer block
302 * @const char *p@ = string to write
303 *
304 * Returns: Zero if it worked, nonzero if there wasn't enough space.
305 *
306 * Use: Puts a null-terminated string to a buffer.
307 */
308
309#define BUF_DECL_PUTSTR_(n, W, w) \
310 extern int buf_putstr##w(buf */*b*/, const char */*p*/); \
311BUF_DOSUFFIXES(BUF_DECL_PUTSTR_)
312
313/*----- That's all, folks -------------------------------------------------*/
314
315#ifdef __cplusplus
316 }
317#endif
318
319#endif