7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @buf_init@ --- *
41 * Arguments: @buf *b@ = pointer to a buffer block
42 * @void *p@ = pointer to a buffer
43 * @size_t sz@ = size of the buffer
47 * Use: Initializes the buffer block appropriately.
50 void buf_init(buf
*b
, void *p
, size_t sz
)
57 /* --- @buf_break@ --- *
59 * Arguments: @buf *b@ = pointer to a buffer block
61 * Returns: Some negative value.
63 * Use: Marks a buffer as broken.
66 int buf_break(buf
*b
) { b
->f
|= BF_BROKEN
; return (-1); }
68 /* --- @buf_flip@ --- *
70 * Arguments: @buf *b@ = pointer to a buffer block
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.
84 /* --- @buf_ensure@ --- *
86 * Arguments: @buf *b@ = pointer to a buffer block
87 * @size_t sz@ = size of data wanted
89 * Returns: Zero if it worked, nonzero if there wasn't enough space.
91 * Use: Ensures that there are @sz@ bytes still in the buffer.
94 int buf_ensure(buf
*b
, size_t sz
) { return (BENSURE(b
, sz
)); }
96 /* --- @buf_get@ --- *
98 * Arguments: @buf *b@ = pointer to a buffer block
99 * @size_t sz@ = size of the buffer
101 * Returns: Pointer to the place in the buffer.
103 * Use: Reserves a space in the buffer of the requested size, and
104 * returns its start address.
107 void *buf_get(buf
*b
, size_t sz
)
117 /* --- @buf_put@ --- *
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
123 * Returns: Zero if it worked, nonzero if there wasn't enough space.
125 * Use: Fetches data from some place and puts it in the buffer
128 int buf_put(buf
*b
, const void *p
, size_t sz
)
132 memcpy(BCUR(b
), p
, sz
);
137 /* --- @buf_getbyte@ --- *
139 * Arguments: @buf *b@ = pointer to a buffer block
141 * Returns: A byte, or less than zero if there wasn't a byte there.
143 * Use: Gets a single byte from a buffer.
146 int buf_getbyte(buf
*b
)
153 /* --- @buf_putbyte@ --- *
155 * Arguments: @buf *b@ = pointer to a buffer block
156 * @int ch@ = byte to write
158 * Returns: Zero if OK, nonzero if there wasn't enough space.
160 * Use: Puts a single byte in a buffer.
163 int buf_putbyte(buf
*b
, int ch
)
171 /* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
173 * Arguments: @buf *b@ = pointer to a buffer block
174 * @uintSZ *w@ = where to put the word
176 * Returns: Zero if OK, or nonzero if there wasn't a word there.
178 * Use: Gets a word of appropriate size and order from a buffer.
181 #define BUF_GETU_(n, W, w) \
182 int buf_getu##w(buf *b, uint##n *ww) \
184 if (BENSURE(b, SZ_##W)) return (-1); \
185 *ww = LOAD##W(b->p); \
189 DOUINTCONV(BUF_GETU_
)
191 /* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
193 * Arguments: @buf *b@ = pointer to a buffer block
194 * @uintSZ w@ = word to write
196 * Returns: Zero if OK, or nonzero if there wasn't enough space
198 * Use: Puts a word into a buffer with appropriate size and order.
201 #define BUF_PUTU_(n, W, w) \
202 int buf_putu##w(buf *b, uint##n ww) \
204 if (BENSURE(b, SZ_##W)) return (-1); \
205 STORE##W(b->p, ww); \
209 DOUINTCONV(BUF_PUTU_
)
213 * Arguments: @buf *b@ = pointer to a buffer block
214 * @size_t *nn@ = where to put the length
216 * Returns: Zero if OK, nonzero if there wasn't a null byte to be found.
218 * Use: Finds a terminating null byte.
221 static int findz(buf
*b
, size_t *nn
)
225 if ((p
= memchr(BCUR(b
), 0, BLEFT(b
))) == 0) {
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 assert(sz <= MASK##W); \
278 if (buf_putu##w(b, sz) || buf_put(b, p, sz)) \
282 DOUINTCONV(BUF_PUTMEM_
)
284 int buf_putmemz(buf
*b
, const void *p
, size_t n
)
288 assert(!memchr(p
, 0, n
));
289 if ((q
= buf_get(b
, n
+ 1)) == 0)
296 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
298 * Arguments: @buf *b@ = pointer to a buffer block
299 * @buf *bb@ = where to put the result
301 * Returns: Zero if it worked, nonzero if there wasn't enough space.
303 * Use: Gets a block of data from a buffer, and writes its bounds to
307 #define BUF_GETBUF_(n, W, w) \
308 int buf_getbuf##w(buf *b, buf *bb) \
313 if ((p = buf_getmem##w(b, &sz)) == 0) \
315 buf_init(bb, p, sz); \
318 BUF_DOSUFFIXES(BUF_GETBUF_
)
320 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
322 * Arguments: @buf *b@ = pointer to a buffer block
323 * @buf *bb@ = buffer to write
325 * Returns: Zero if it worked, nonzero if there wasn't enough space.
327 * Use: Puts the contents of a buffer to a buffer.
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_
)
335 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
337 * Arguments: @buf *b@ = pointer to a buffer block
338 * @const char *p@ = string to write
340 * Returns: Zero if it worked, nonzero if there wasn't enough space.
342 * Use: Puts a null-terminated string to a buffer.
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_
)
350 /*----- That's all, folks -------------------------------------------------*/