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. The length includes this
222 static int findz(buf *b, size_t *nn)
226 if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) {
230 *nn = p - BCUR(b) + 1;
234 /* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
236 * Arguments: @buf *b@ = pointer to a buffer block
237 * @size_t *nn@ = where to put the length
239 * Returns: Pointer to the buffer data, or null.
241 * Use: Gets a chunk of memory from a buffer. The suffix is the
242 * width and byte order of the length; @z@ means null-
246 #define BUF_GETMEM_(n, W, w) \
247 void *buf_getmem##w(buf *b, size_t *nn) \
250 if (buf_getu##w(b, &sz)) return (0); \
252 return (buf_get(b, sz)); \
254 DOUINTCONV(BUF_GETMEM_)
256 void *buf_getmemz(buf *b, size_t *nn)
258 if (findz(b, nn)) return (0);
259 return (buf_get(b, *nn));
262 /* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
264 * Arguments: @buf *b@ = pointer to a buffer block
265 * @const void *p@ = pointer to data to write
266 * @size_t n@ = length to write
268 * Returns: Zero if OK, nonzero if there wasn't enough space.
270 * Use: Writes a chunk of data to a buffer. The suffix is the
271 * width and byte order of the length; @z@ means null-
275 #define BUF_PUTMEM_(n, W, w) \
276 int buf_putmem##w(buf *b, const void *p, size_t sz) \
278 assert(sz <= MASK##W); \
279 if (buf_putu##w(b, sz) || buf_put(b, p, sz)) \
283 DOUINTCONV(BUF_PUTMEM_)
285 int buf_putmemz(buf *b, const void *p, size_t n)
289 assert(!memchr(p, 0, n));
290 if ((q = buf_get(b, n + 1)) == 0)
297 /* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
299 * Arguments: @buf *b@ = pointer to a buffer block
300 * @buf *bb@ = where to put the result
302 * Returns: Zero if it worked, nonzero if there wasn't enough space.
304 * Use: Gets a block of data from a buffer, and writes its bounds to
308 #define BUF_GETBUF_(n, W, w) \
309 int buf_getbuf##w(buf *b, buf *bb) \
314 if ((p = buf_getmem##w(b, &sz)) == 0) \
316 buf_init(bb, p, sz); \
319 BUF_DOSUFFIXES(BUF_GETBUF_)
321 /* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
323 * Arguments: @buf *b@ = pointer to a buffer block
324 * @buf *bb@ = buffer to write
326 * Returns: Zero if it worked, nonzero if there wasn't enough space.
328 * Use: Puts the contents of a buffer to a buffer.
331 #define BUF_PUTBUF_(n, W, w) \
332 int buf_putbuf##w(buf *b, buf *bb) \
333 { return (buf_putmem##w(b, BBASE(bb), BLEN(bb))); }
334 BUF_DOSUFFIXES(BUF_PUTBUF_)
336 /* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
338 * Arguments: @buf *b@ = pointer to a buffer block
339 * @const char *p@ = string to write
341 * Returns: Zero if it worked, nonzero if there wasn't enough space.
343 * Use: Puts a null-terminated string to a buffer.
346 #define BUF_PUTSTR_(n, W, w) \
347 int buf_putstr##w(buf *b, const char *p) \
348 { return (buf_putmem##w(b, p, strlen(p))); }
349 BUF_DOSUFFIXES(BUF_PUTSTR_)
351 /*----- That's all, folks -------------------------------------------------*/