chiark / gitweb /
testrig: Provide useful interface for more complicated test rigs.
[mLib] / buf.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Buffer handling
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 /*----- Header files ------------------------------------------------------*/
31
32 #include <assert.h>
33 #include <string.h>
34
35 #include "buf.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @buf_init@ --- *
40  *
41  * Arguments:   @buf *b@ = pointer to a buffer block
42  *              @void *p@ = pointer to a buffer
43  *              @size_t sz@ = size of the buffer
44  *
45  * Returns:     ---
46  *
47  * Use:         Initializes the buffer block appropriately.
48  */
49
50 void buf_init(buf *b, void *p, size_t sz)
51 {
52   b->base = b->p = p;
53   b->limit = b->p + sz;
54   b->f = 0;
55 }
56
57 /* --- @buf_break@ --- *
58  *
59  * Arguments:   @buf *b@ = pointer to a buffer block
60  *
61  * Returns:     Some negative value.
62  *
63  * Use:         Marks a buffer as broken.
64  */
65
66 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
67
68 /* --- @buf_flip@ --- *
69  *
70  * Arguments:   @buf *b@ = pointer to a buffer block
71  *
72  * Returns:     ---
73  *
74  * Use:         Flips a buffer so that if you've just been writing to it,
75  *              you can now read from the bit you've written.
76  */
77
78 void buf_flip(buf *b)
79 {
80   b->limit = b->p;
81   b->p = b->base;
82 }
83
84 /* --- @buf_ensure@ --- *
85  *
86  * Arguments:   @buf *b@ = pointer to a buffer block
87  *              @size_t sz@ = size of data wanted
88  *
89  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
90  *
91  * Use:         Ensures that there are @sz@ bytes still in the buffer.
92  */
93
94 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
95
96 /* --- @buf_get@ --- *
97  *
98  * Arguments:   @buf *b@ = pointer to a buffer block
99  *              @size_t sz@ = size of the buffer
100  *
101  * Returns:     Pointer to the place in the buffer.
102  *
103  * Use:         Reserves a space in the buffer of the requested size, and
104  *              returns its start address.
105  */
106
107 void *buf_get(buf *b, size_t sz)
108 {
109   void *p;
110   if (BENSURE(b, sz))
111     return (0);
112   p = BCUR(b);
113   BSTEP(b, sz);
114   return (p);
115 }
116
117 /* --- @buf_put@ --- *
118  *
119  * Arguments:   @buf *b@ = pointer to a buffer block
120  *              @const void *p@ = pointer to a buffer
121  *              @size_t sz@ = size of the buffer
122  *
123  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
124  *
125  * Use:         Fetches data from some place and puts it in the buffer
126  */
127
128 int buf_put(buf *b, const void *p, size_t sz)
129 {
130   if (BENSURE(b, sz))
131     return (-1);
132   memcpy(BCUR(b), p, sz);
133   BSTEP(b, sz);
134   return (0);
135 }
136
137 /* --- @buf_getbyte@ --- *
138  *
139  * Arguments:   @buf *b@ = pointer to a buffer block
140  *
141  * Returns:     A byte, or less than zero if there wasn't a byte there.
142  *
143  * Use:         Gets a single byte from a buffer.
144  */
145
146 int buf_getbyte(buf *b)
147 {
148   if (BENSURE(b, 1))
149     return (-1);
150   return (*b->p++);
151 }
152
153 /* --- @buf_putbyte@ --- *
154  *
155  * Arguments:   @buf *b@ = pointer to a buffer block
156  *              @int ch@ = byte to write
157  *
158  * Returns:     Zero if OK, nonzero if there wasn't enough space.
159  *
160  * Use:         Puts a single byte in a buffer.
161  */
162
163 int buf_putbyte(buf *b, int ch)
164 {
165   if (BENSURE(b, 1))
166     return (-1);
167   *b->p++ = ch;
168   return (0);
169 }
170
171 /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
172  *
173  * Arguments:   @buf *b@ = pointer to a buffer block
174  *              @uintSZ *w@ = where to put the word
175  *
176  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
177  *
178  * Use:         Gets a word of appropriate size and order from a buffer.
179  */
180
181 #define BUF_GETU_(n, W, w)                                              \
182   int buf_getu##w(buf *b, uint##n *ww)                                  \
183   {                                                                     \
184     if (BENSURE(b, SZ_##W)) return (-1);                                \
185     *ww = LOAD##W(b->p);                                                \
186     BSTEP(b, SZ_##W);                                                   \
187     return (0);                                                         \
188   }
189 DOUINTCONV(BUF_GETU_)
190
191 /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
192  *
193  * Arguments:   @buf *b@ = pointer to a buffer block
194  *              @uintSZ w@ = word to write
195  *
196  * Returns:     Zero if OK, or nonzero if there wasn't enough space
197  *
198  * Use:         Puts a word into a buffer with appropriate size and order.
199  */
200
201 #define BUF_PUTU_(n, W, w)                                              \
202   int buf_putu##w(buf *b, uint##n ww)                                   \
203   {                                                                     \
204     if (BENSURE(b, SZ_##W)) return (-1);                                \
205     STORE##W(b->p, ww);                                                 \
206     BSTEP(b, SZ_##W);                                                   \
207     return (0);                                                         \
208   }
209 DOUINTCONV(BUF_PUTU_)
210
211 /* --- @findz@ --- *
212  *
213  * Arguments:   @buf *b@ = pointer to a buffer block
214  *              @size_t *nn@ = where to put the length
215  *
216  * Returns:     Zero if OK, nonzero if there wasn't a null byte to be found.
217  *
218  * Use:         Finds a terminating null byte.
219  */
220
221 static int findz(buf *b, size_t *nn)
222 {
223   octet *p;
224
225   if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) {
226     buf_break(b);
227     return (-1);
228   }
229   *nn = p - BCUR(b);
230   return (0);
231 }
232
233 /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
234  *
235  * Arguments:   @buf *b@ = pointer to a buffer block
236  *              @size_t *nn@ = where to put the length
237  *
238  * Returns:     Pointer to the buffer data, or null.
239  *
240  * Use:         Gets a chunk of memory from a buffer.  The suffix is the
241  *              width and byte order of the length; @z@ means null-
242  *              terminated.
243  */
244
245 #define BUF_GETMEM_(n, W, w)                                            \
246   void *buf_getmem##w(buf *b, size_t *nn)                               \
247   {                                                                     \
248     uint##n sz;                                                         \
249     if (buf_getu##w(b, &sz)) return (0);                                \
250     *nn = sz;                                                           \
251     return (buf_get(b, sz));                                            \
252   }
253 DOUINTCONV(BUF_GETMEM_)
254
255 void *buf_getmemz(buf *b, size_t *nn)
256 {
257   if (findz(b, nn)) return (0);
258   return (buf_get(b, *nn));
259 }
260
261 /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
262  *
263  * Arguments:   @buf *b@ = pointer to a buffer block
264  *              @const void *p@ = pointer to data to write
265  *              @size_t n@ = length to write
266  *
267  * Returns:     Zero if OK, nonzero if there wasn't enough space.
268  *
269  * Use:         Writes a chunk of data to a buffer.  The suffix is the
270  *              width and byte order of the length; @z@ means null-
271  *              terminated.
272  */
273
274 #define BUF_PUTMEM_(n, W, w)                                            \
275   int buf_putmem##w(buf *b, const void *p, size_t sz)                   \
276   {                                                                     \
277     assert(sz <= MASK##W);                                              \
278     if (buf_putu##w(b, sz) || buf_put(b, p, sz))                        \
279       return (-1);                                                      \
280     return (0);                                                         \
281   }
282 DOUINTCONV(BUF_PUTMEM_)
283
284 int buf_putmemz(buf *b, const void *p, size_t n)
285 {
286   octet *q;
287
288   assert(!memchr(p, 0, n));
289   if ((q = buf_get(b, n + 1)) == 0)
290     return (-1);
291   memcpy(q, p, n);
292   q[n] = 0;
293   return (0);
294 }
295
296 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
297  *
298  * Arguments:   @buf *b@ = pointer to a buffer block
299  *              @buf *bb@ = where to put the result
300  *
301  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
302  *
303  * Use:         Gets a block of data from a buffer, and writes its bounds to
304  *              another buffer.
305  */
306
307 #define BUF_GETBUF_(n, W, w)                                            \
308   int buf_getbuf##w(buf *b, buf *bb)                                    \
309   {                                                                     \
310     void *p;                                                            \
311     size_t sz;                                                          \
312                                                                         \
313     if ((p = buf_getmem##w(b, &sz)) == 0)                               \
314       return (-1);                                                      \
315     buf_init(bb, p, sz);                                                \
316     return (0);                                                         \
317   }
318 BUF_DOSUFFIXES(BUF_GETBUF_)
319
320 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
321  *
322  * Arguments:   @buf *b@ = pointer to a buffer block
323  *              @buf *bb@ = buffer to write
324  *
325  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
326  *
327  * Use:         Puts the contents of a buffer to a buffer.
328  */
329
330 #define BUF_PUTBUF_(n, W, w)                                            \
331   int buf_putbuf##w(buf *b, buf *bb)                                    \
332     { return (buf_putmem##w(b, BBASE(bb), BLEN(bb))); }
333 BUF_DOSUFFIXES(BUF_PUTBUF_)
334
335 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
336  *
337  * Arguments:   @buf *b@ = pointer to a buffer block
338  *              @const char *p@ = string to write
339  *
340  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
341  *
342  * Use:         Puts a null-terminated string to a buffer.
343  */
344
345 #define BUF_PUTSTR_(n, W, w)                                            \
346   int buf_putstr##w(buf *b, const char *p)                              \
347     { return (buf_putmem##w(b, p, strlen(p))); }
348 BUF_DOSUFFIXES(BUF_PUTSTR_)
349
350 /*----- That's all, folks -------------------------------------------------*/