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 ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @buf_init@ --- *
40 * Arguments: @buf *b@ = pointer to a buffer block
41 * @void *p@ = pointer to a buffer
42 * @size_t sz@ = size of the buffer
46 * Use: Initializes the buffer block appropriately.
49 void buf_init(buf *b, void *p, size_t sz)
56 /* --- @buf_break@ --- *
58 * Arguments: @buf *b@ = pointer to a buffer block
60 * Returns: Some negative value.
62 * Use: Marks a buffer as broken.
65 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
67 /* --- @buf_flip@ --- *
69 * Arguments: @buf *b@ = pointer to a buffer block
73 * Use: Flips a buffer so that if you've just been writing to it,
74 * you can now read from the bit you've written.
83 /* --- @buf_ensure@ --- *
85 * Arguments: @buf *b@ = pointer to a buffer block
86 * @size_t sz@ = size of data wanted
88 * Returns: Zero if it worked, nonzero if there wasn't enough space.
90 * Use: Ensures that there are @sz@ bytes still in the buffer.
93 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
95 /* --- @buf_get@ --- *
97 * Arguments: @buf *b@ = pointer to a buffer block
98 * @size_t sz@ = size of the buffer
100 * Returns: Pointer to the place in the buffer.
102 * Use: Reserves a space in the buffer of the requested size, and
103 * returns its start address.
106 void *buf_get(buf *b, size_t sz)
116 /* --- @buf_put@ --- *
118 * Arguments: @buf *b@ = pointer to a buffer block
119 * @const void *p@ = pointer to a buffer
120 * @size_t sz@ = size of the buffer
122 * Returns: Zero if it worked, nonzero if there wasn't enough space.
124 * Use: Fetches data from some place and puts it in the buffer
127 int buf_put(buf *b, const void *p, size_t sz)
131 memcpy(BCUR(b), p, sz);
136 /* --- @buf_getbyte@ --- *
138 * Arguments: @buf *b@ = pointer to a buffer block
140 * Returns: A byte, or less than zero if there wasn't a byte there.
142 * Use: Gets a single byte from a buffer.
145 int buf_getbyte(buf *b)
152 /* --- @buf_putbyte@ --- *
154 * Arguments: @buf *b@ = pointer to a buffer block
155 * @int ch@ = byte to write
157 * Returns: Zero if OK, nonzero if there wasn't enough space.
159 * Use: Puts a single byte in a buffer.
162 int buf_putbyte(buf *b, int ch)
170 /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
172 * Arguments: @buf *b@ = pointer to a buffer block
173 * @uintSZ *w@ = where to put the word
175 * Returns: Zero if OK, or nonzero if there wasn't a word there.
177 * Use: Gets a word of appropriate size and order from a buffer.
180 #define BUF_GETU_(n, W, w) \
181 int buf_getu##w(buf *b, uint##n *ww) \
183 if (BENSURE(b, SZ_##W)) return (-1); \
184 *ww = LOAD##W(b->p); \
188 DOUINTCONV(BUF_GETU_)
190 /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
192 * Arguments: @buf *b@ = pointer to a buffer block
193 * @uintSZ w@ = word to write
195 * Returns: Zero if OK, or nonzero if there wasn't enough space
197 * Use: Puts a word into a buffer with appropriate size and order.
200 #define BUF_PUTU_(n, W, w) \
201 int buf_putu##w(buf *b, uint##n ww) \
203 if (BENSURE(b, SZ_##W)) return (-1); \
204 STORE##W(b->p, ww); \
208 DOUINTCONV(BUF_PUTU_)
212 * Arguments: @buf *b@ = pointer to a buffer block
213 * @size_t *nn@ = where to put the length
215 * Returns: Zero if OK, nonzero if there wasn't a null byte to be found.
217 * Use: Finds a terminating null byte. The length includes this
221 static int findz(buf *b, size_t *nn)
225 if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) {
229 *nn = p - BCUR(b) + 1;
233 /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
235 * Arguments: @buf *b@ = pointer to a buffer block
236 * @size_t *nn@ = where to put the length
238 * Returns: Pointer to the buffer data, or null.
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-
245 #define BUF_GETMEM_(n, W, w) \
246 void *buf_getmem##w(buf *b, size_t *nn) \
249 if (buf_getu##w(b, &sz)) return (0); \
251 return (buf_get(b, sz)); \
253 DOUINTCONV(BUF_GETMEM_)
255 void *buf_getmemz(buf *b, size_t *nn)
257 if (findz(b, nn)) return (0);
258 return (buf_get(b, *nn));
261 /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
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
267 * Returns: Zero if OK, nonzero if there wasn't enough space.
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-
274 #define BUF_PUTMEM_(n, W, w) \
275 int buf_putmem##w(buf *b, const void *p, size_t sz) \
277 MUFFLE_WARNINGS_STMT \
278 (CLANG_WARNING("-Wtautological-constant-out-of-range-compare"), \
279 { assert(sz <= MASK##W); }); \
280 if (buf_putu##w(b, sz) || buf_put(b, p, sz)) \
284 DOUINTCONV(BUF_PUTMEM_)
286 int buf_putmemz(buf *b, const void *p, size_t n)
290 assert(!memchr(p, 0, n));
291 if ((q = buf_get(b, n + 1)) == 0)
298 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
300 * Arguments: @buf *b@ = pointer to a buffer block
301 * @buf *bb@ = where to put the result
303 * Returns: Zero if it worked, nonzero if there wasn't enough space.
305 * Use: Gets a block of data from a buffer, and writes its bounds to
309 #define BUF_GETBUF_(n, W, w) \
310 int buf_getbuf##w(buf *b, buf *bb) \
315 if ((p = buf_getmem##w(b, &sz)) == 0) \
317 buf_init(bb, p, sz); \
320 BUF_DOSUFFIXES(BUF_GETBUF_)
322 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
324 * Arguments: @buf *b@ = pointer to a buffer block
325 * @buf *bb@ = buffer to write
327 * Returns: Zero if it worked, nonzero if there wasn't enough space.
329 * Use: Puts the contents of a buffer to a buffer.
332 #define BUF_PUTBUF_(n, W, w) \
333 int buf_putbuf##w(buf *b, buf *bb) \
334 { return (buf_putmem##w(b, BBASE(bb), BLEN(bb))); }
335 BUF_DOSUFFIXES(BUF_PUTBUF_)
337 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
339 * Arguments: @buf *b@ = pointer to a buffer block
340 * @const char *p@ = string to write
342 * Returns: Zero if it worked, nonzero if there wasn't enough space.
344 * Use: Puts a null-terminated string to a buffer.
347 #define BUF_PUTSTR_(n, W, w) \
348 int buf_putstr##w(buf *b, const char *p) \
349 { return (buf_putmem##w(b, p, strlen(p))); }
350 BUF_DOSUFFIXES(BUF_PUTSTR_)
352 /*----- That's all, folks -------------------------------------------------*/