5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
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.
17 * mLib is distributed in the hope that it will be useful,
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.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @buf_init@ --- *
39 * Arguments: @buf *b@ = pointer to a buffer block
40 * @void *p@ = pointer to a buffer
41 * @size_t sz@ = size of the buffer
45 * Use: Initializes the buffer block appropriately.
48 void buf_init(buf *b, void *p, size_t sz)
55 /* --- @buf_break@ --- *
57 * Arguments: @buf *b@ = pointer to a buffer block
59 * Returns: Some negative value.
61 * Use: Marks a buffer as broken.
64 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
66 /* --- @buf_flip@ --- *
68 * Arguments: @buf *b@ = pointer to a buffer block
72 * Use: Flips a buffer so that if you've just been writing to it,
73 * you can now read from the bit you've written.
82 /* --- @buf_ensure@ --- *
84 * Arguments: @buf *b@ = pointer to a buffer block
85 * @size_t sz@ = size of data wanted
87 * Returns: Zero if it worked, nonzero if there wasn't enough space.
89 * Use: Ensures that there are @sz@ bytes still in the buffer.
92 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
94 /* --- @buf_get@ --- *
96 * Arguments: @buf *b@ = pointer to a buffer block
97 * @size_t sz@ = size of the buffer
99 * Returns: Pointer to the place in the buffer.
101 * Use: Reserves a space in the buffer of the requested size, and
102 * returns its start address.
105 void *buf_get(buf *b, size_t sz)
115 /* --- @buf_put@ --- *
117 * Arguments: @buf *b@ = pointer to a buffer block
118 * @const void *p@ = pointer to a buffer
119 * @size_t sz@ = size of the buffer
121 * Returns: Zero if it worked, nonzero if there wasn't enough space.
123 * Use: Fetches data from some place and puts it in the buffer
126 int buf_put(buf *b, const void *p, size_t sz)
130 memcpy(BCUR(b), p, sz);
135 /* --- @buf_getbyte@ --- *
137 * Arguments: @buf *b@ = pointer to a buffer block
139 * Returns: A byte, or less than zero if there wasn't a byte there.
141 * Use: Gets a single byte from a buffer.
144 int buf_getbyte(buf *b)
151 /* --- @buf_putbyte@ --- *
153 * Arguments: @buf *b@ = pointer to a buffer block
154 * @int ch@ = byte to write
156 * Returns: Zero if OK, nonzero if there wasn't enough space.
158 * Use: Puts a single byte in a buffer.
161 int buf_putbyte(buf *b, int ch)
169 /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
171 * Arguments: @buf *b@ = pointer to a buffer block
172 * @uintSZ *w@ = where to put the word
174 * Returns: Zero if OK, or nonzero if there wasn't a word there.
176 * Use: Gets a word of appropriate size and order from a buffer.
179 #define BUF_GETU_(n, W, w) \
180 int buf_getu##w(buf *b, uint##n *ww) \
182 if (BENSURE(b, SZ_##W)) return (-1); \
183 *ww = LOAD##W(b->p); \
187 DOUINTCONV(BUF_GETU_)
189 /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
191 * Arguments: @buf *b@ = pointer to a buffer block
192 * @uintSZ w@ = word to write
194 * Returns: Zero if OK, or nonzero if there wasn't enough space
196 * Use: Puts a word into a buffer with appropriate size and order.
199 #define BUF_PUTU_(n, W, w) \
200 int buf_putu##w(buf *b, uint##n ww) \
202 if (BENSURE(b, SZ_##W)) return (-1); \
203 STORE##W(b->p, ww); \
207 DOUINTCONV(BUF_PUTU_)
211 * Arguments: @buf *b@ = pointer to a buffer block
212 * @size_t *nn@ = where to put the length
214 * Returns: Zero if OK, nonzero if there wasn't a null byte to be found.
216 * Use: Finds a terminating null byte. The length includes this
220 static int findz(buf *b, size_t *nn)
224 if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) {
228 *nn = p - BCUR(b) + 1;
232 /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
234 * Arguments: @buf *b@ = pointer to a buffer block
235 * @size_t *nn@ = where to put the length
237 * Returns: Pointer to the buffer data, or null.
239 * Use: Gets a chunk of memory from a buffer. The suffix is the
240 * width and byte order of the length; @z@ means null-
244 #define BUF_GETMEM_(n, W, w) \
245 void *buf_getmem##w(buf *b, size_t *nn) \
248 if (buf_getu##w(b, &sz)) return (0); \
250 return (buf_get(b, sz)); \
252 DOUINTCONV(BUF_GETMEM_)
254 void *buf_getmemz(buf *b, size_t *nn)
256 if (findz(b, nn)) return (0);
257 return (buf_get(b, *nn));
260 /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
262 * Arguments: @buf *b@ = pointer to a buffer block
263 * @const void *p@ = pointer to data to write
264 * @size_t n@ = length to write
266 * Returns: Zero if OK, nonzero if there wasn't enough space.
268 * Use: Writes a chunk of data to a buffer. The suffix is the
269 * width and byte order of the length; @z@ means null-
273 #define BUF_PUTMEM_(n, W, w) \
274 int buf_putmem##w(buf *b, const void *p, size_t sz) \
276 assert(sz <= MASK##W); \
277 if (buf_putu##w(b, sz) || buf_put(b, p, sz)) \
281 DOUINTCONV(BUF_PUTMEM_)
283 int buf_putmemz(buf *b, const void *p, size_t n)
287 assert(!memchr(p, 0, n));
288 if ((q = buf_get(b, n + 1)) == 0)
295 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
297 * Arguments: @buf *b@ = pointer to a buffer block
298 * @buf *bb@ = where to put the result
300 * Returns: Zero if it worked, nonzero if there wasn't enough space.
302 * Use: Gets a block of data from a buffer, and writes its bounds to
306 #define BUF_GETBUF_(n, W, w) \
307 int buf_getbuf##w(buf *b, buf *bb) \
312 if ((p = buf_getmem##w(b, &sz)) == 0) \
314 buf_init(bb, p, sz); \
317 BUF_DOSUFFIXES(BUF_GETBUF_)
319 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
321 * Arguments: @buf *b@ = pointer to a buffer block
322 * @buf *bb@ = buffer to write
324 * Returns: Zero if it worked, nonzero if there wasn't enough space.
326 * Use: Puts the contents of a buffer to a buffer.
329 #define BUF_PUTBUF_(n, W, w) \
330 int buf_putbuf##w(buf *b, buf *bb) \
331 { return (buf_putmem##w(b, BBASE(bb), BLEN(bb))); }
332 BUF_DOSUFFIXES(BUF_PUTBUF_)
334 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
336 * Arguments: @buf *b@ = pointer to a buffer block
337 * @const char *p@ = string to write
339 * Returns: Zero if it worked, nonzero if there wasn't enough space.
341 * Use: Puts a null-terminated string to a buffer.
344 #define BUF_PUTSTR_(n, W, w) \
345 int buf_putstr##w(buf *b, const char *p) \
346 { return (buf_putmem##w(b, p, strlen(p))); }
347 BUF_DOSUFFIXES(BUF_PUTSTR_)
349 /*----- That's all, folks -------------------------------------------------*/