chiark / gitweb /
84c4121b2f58a9167bca30dfbc198e8483004ba6
[mLib] / buf.h
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 Catacomb.
13  *
14  * Catacomb 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  * Catacomb 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 Catacomb; 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 CATACOMB_BUF_H
31 #define CATACOMB_BUF_H
32
33 #ifdef __cplusplus
34   extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <stddef.h>
40
41 #include <mLib/bits.h>
42
43 #ifndef CATACOMB_MP_H
44 #  include "mp.h"
45 #endif
46
47 #ifndef CATACOMB_EC_H
48 #  include "ec.h"
49 #endif
50
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
78 #define BENSURE(b, sz)                                                  \
79   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
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
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
180 /* --- @buf_getu16@ --- *
181  *
182  * Arguments:   @buf *b@ = pointer to a buffer block
183  *              @uint16 *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 16-bit word from a buffer.
188  */
189
190 extern int buf_getu16(buf */*b*/, uint16 */*w*/);
191
192 /* --- @buf_putu16@ --- *
193  *
194  * Arguments:   @buf *b@ = pointer to a buffer block
195  *              @uint16 w@ = word to write
196  *
197  * Returns:     Zero if OK, nonzero if there wasn't enough space.
198  *
199  * Use:         Puts a 16-but word in a buffer.
200  */
201
202 extern int buf_putu16(buf */*b*/, uint16 /*w*/);
203
204 /* --- @buf_getu32@ --- *
205  *
206  * Arguments:   @buf *b@ = pointer to a buffer block
207  *              @uint32 *w@ = where to put the word
208  *
209  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
210  *
211  * Use:         Gets a 32-bit word from a buffer.
212  */
213
214 extern int buf_getu32(buf */*b*/, uint32 */*w*/);
215
216 /* --- @buf_putu32@ --- *
217  *
218  * Arguments:   @buf *b@ = pointer to a buffer block
219  *              @uint32 w@ = word to write
220  *
221  * Returns:     Zero if OK, nonzero if there wasn't enough space.
222  *
223  * Use:         Puts a 32-but word in a buffer.
224  */
225
226 extern int buf_putu32(buf */*b*/, uint32 /*w*/);
227
228 /* --- @buf_getmem{8,16,32,z} --- *
229  *
230  * Arguments:   @buf *b@ = pointer to a buffer block
231  *              @size_t *nn@ = where to put the length
232  *
233  * Returns:     Pointer to the buffer data, or null.
234  *
235  * Use:         Gets a chunk of memory from a buffer.  The number, @16@ or
236  *              @32@, is the width of the length; @z@ means null-terminated.
237  */
238
239 extern void *buf_getmem8(buf */*b*/, size_t */*nn*/);
240 extern void *buf_getmem16(buf */*b*/, size_t */*nn*/);
241 extern void *buf_getmem32(buf */*b*/, size_t */*nn*/);
242 extern void *buf_getmemz(buf */*b*/, size_t */*nn*/);
243
244 /* --- @buf_putmem{8,16,32,z} --- *
245  *
246  * Arguments:   @buf *b@ = pointer to a buffer block
247  *              @const void *p@ = pointer to data to write
248  *              @size_t n@ = length to write
249  *
250  * Returns:     Zero if OK, nonzero if there wasn't enough space.
251  *
252  * Use:         Writes a chunk of data to a buffer.  The number, @16@ or
253  *              @32@, is the width of the length; @z@ means null-terminated.
254  */
255
256 extern int buf_putmem8(buf */*b*/, const void */*p*/, size_t /*n*/);
257 extern int buf_putmem16(buf */*b*/, const void */*p*/, size_t /*n*/);
258 extern int buf_putmem32(buf */*b*/, const void */*p*/, size_t /*n*/);
259 extern int buf_putmemz(buf */*b*/, const void */*p*/, size_t /*n*/);
260
261 /* --- @buf_getbuf{8,16,32,z}@ --- *
262  *
263  * Arguments:   @buf *b@ = pointer to a buffer block
264  *              @buf *bb@ = where to put the result
265  *
266  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
267  *
268  * Use:         Gets a block of data from a buffer, and writes its bounds to
269  *              another buffer.  The number, @16@ or @32@, is the width of
270  *              the length; @z@ means null-terminated.
271  */
272
273 extern int buf_getbuf8(buf */*b*/, buf */*bb*/);
274 extern int buf_getbuf16(buf */*b*/, buf */*bb*/);
275 extern int buf_getbuf32(buf */*b*/, buf */*bb*/);
276 extern int buf_getbufz(buf */*b*/, buf */*bb*/);
277
278 /* --- @buf_putbuf{8,16,32,z}@ --- *
279  *
280  * Arguments:   @buf *b@ = pointer to a buffer block
281  *              @buf *bb@ = buffer to write
282  *
283  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
284  *
285  * Use:         Puts the contents of a buffer to a buffer.  The number, @16@
286  *              or @32@, is the width of the length; @z@ means null-
287  *              terminated.
288  */
289
290 extern int buf_putbuf8(buf */*b*/, buf */*bb*/);
291 extern int buf_putbuf16(buf */*b*/, buf */*bb*/);
292 extern int buf_putbuf32(buf */*b*/, buf */*bb*/);
293 extern int buf_putbufz(buf */*b*/, buf */*bb*/);
294
295 /* --- @buf_getstr{8,16,32,z}@ --- *
296  *
297  * Arguments:   @buf *b@ = pointer to a buffer block
298  *              @dstr *d@ = where to put the result
299  *
300  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
301  *
302  * Use:         Gets a block of data from a buffer, and writes its contents
303  *              to a string.  The number, @16@ or @32@, is the width of the
304  *              length; @z@ means null-terminated.
305  */
306
307 extern int buf_getstr8(buf */*b*/, dstr */*d*/);
308 extern int buf_getstr16(buf */*b*/, dstr */*d*/);
309 extern int buf_getstr32(buf */*b*/, dstr */*d*/);
310 extern int buf_getstrz(buf */*b*/, dstr */*d*/);
311
312 /* --- @buf_putstr{8,16,32,z}@ --- *
313  *
314  * Arguments:   @buf *b@ = pointer to a buffer block
315  *              @dstr *d@ = string to write
316  *
317  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
318  *
319  * Use:         Puts a string to a buffer, and writes its bounds to
320  *              another buffer.  The number, @16@ or @32@, is the width of
321  *              the length; @z@ means null-terminated.
322  */
323
324 extern int buf_putstr8(buf */*b*/, dstr */*d*/);
325 extern int buf_putstr16(buf */*b*/, dstr */*d*/);
326 extern int buf_putstr32(buf */*b*/, dstr */*d*/);
327 extern int buf_putstrz(buf */*b*/, dstr */*d*/);
328
329 /* --- @buf_getmp@ --- *
330  *
331  * Arguments:   @buf *b@ = pointer to a buffer block
332  *
333  * Returns:     A multiprecision integer, or null if there wasn't one there.
334  *
335  * Use:         Gets a multiprecision integer from a buffer.
336  */
337
338 extern mp *buf_getmp(buf */*b*/);
339
340 /* --- @buf_putmp@ --- *
341  *
342  * Arguments:   @buf *b@ = pointer to a buffer block
343  *              @mp *m@ = a multiprecision integer
344  *
345  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
346  *
347  * Use:         Puts a multiprecision integer to a buffer.
348  */
349
350 extern int buf_putmp(buf */*b*/, mp */*m*/);
351
352 /* --- @buf_getec@ --- *
353  *
354  * Arguments:   @buf *b@ = pointer to a buffer block
355  *              @ec *p@ = where to put the point
356  *
357  * Returns:     Zero if it worked, nonzero if it failed.
358  *
359  * Use:         Gets a multiprecision integer from a buffer.  The point must
360  *              be initialized.
361  */
362
363 extern int buf_getec(buf */*b*/, ec */*p*/);
364
365 /* --- @buf_putec@ --- *
366  *
367  * Arguments:   @buf *b@ = pointer to a buffer block
368  *              @ec *p@ = an elliptic curve point
369  *
370  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
371  *
372  * Use:         Puts an elliptic curve point to a buffer.
373  */
374
375 extern int buf_putec(buf */*b*/, ec */*p*/);
376
377 /*----- That's all, folks -------------------------------------------------*/
378
379 #ifdef __cplusplus
380   }
381 #endif
382
383 #endif