2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: lwbuffer.h,v 1.15.206.1 2004/03/06 08:15:35 marka Exp $ */
20 #ifndef LWRES_LWBUFFER_H
21 #define LWRES_LWBUFFER_H 1
30 * A buffer is a region of memory, together with a set of related subregions.
31 * Buffers are used for parsing and I/O operations.
33 * The 'used region' and the 'available' region are disjoint, and their
34 * union is the buffer's region. The used region extends from the beginning
35 * of the buffer region to the last used byte. The available region
36 * extends from one byte greater than the last used byte to the end of the
37 * buffer's region. The size of the used region can be changed using various
38 * buffer commands. Initially, the used region is empty.
40 * The used region is further subdivided into two disjoint regions: the
41 * 'consumed region' and the 'remaining region'. The union of these two
42 * regions is the used region. The consumed region extends from the beginning
43 * of the used region to the byte before the 'current' offset (if any). The
44 * 'remaining' region the current pointer to the end of the used
45 * region. The size of the consumed region can be changed using various
46 * buffer commands. Initially, the consumed region is empty.
48 * The 'active region' is an (optional) subregion of the remaining region.
49 * It extends from the current offset to an offset in the remaining region
50 * that is selected with lwres_buffer_setactive(). Initially, the active
51 * region is empty. If the current offset advances beyond the chosen offset,
52 * the active region will also be empty.
54 * /----- used region -----\/-- available --\
55 * +----------------------------------------+
56 * | consumed | remaining | |
57 * +----------------------------------------+
60 * a == base of buffer.
61 * b == current pointer. Can be anywhere between a and d.
62 * c == active pointer. Meaningful between b and d.
64 * e == length of buffer.
66 * a-e == entire (length) of buffer.
68 * a-b == consumed region.
69 * b-d == remaining region.
70 * b-c == optional active region.
72 * The following invariants are maintained by all routines:
76 * base is a valid pointer to length bytes of memory
80 * 0 <= current <= used
83 * (although active < current implies empty active region)
86 * Buffers have no synchronization. Clients must ensure exclusive
90 * No anticipated impact.
93 * Memory: 1 pointer + 6 unsigned integers per buffer.
96 * No anticipated impact.
106 #include <lwres/lang.h>
107 #include <lwres/int.h>
109 LWRES_LANG_BEGINDECLS
114 #define LWRES_BUFFER_MAGIC 0x4275663fU /* Buf?. */
116 #define LWRES_BUFFER_VALID(b) ((b) != NULL && \
117 (b)->magic == LWRES_BUFFER_MAGIC)
120 * The following macros MUST be used only on valid buffers. It is the
121 * caller's responsibility to ensure this by using the LWRES_BUFFER_VALID
122 * check above, or by calling another lwres_buffer_*() function (rather than
127 * Get the length of the used region of buffer "b"
129 #define LWRES_BUFFER_USEDCOUNT(b) ((b)->used)
132 * Get the length of the available region of buffer "b"
134 #define LWRES_BUFFER_AVAILABLECOUNT(b) ((b)->length - (b)->used)
136 #define LWRES_BUFFER_REMAINING(b) ((b)->used - (b)->current)
139 * Note that the buffer structure is public. This is principally so buffer
140 * operations can be implemented using macros. Applications are strongly
141 * discouraged from directly manipulating the structure.
144 typedef struct lwres_buffer lwres_buffer_t;
145 struct lwres_buffer {
148 /* The following integers are byte offsets from 'base'. */
151 unsigned int current;
160 lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length);
162 * Make 'b' refer to the 'length'-byte region starting at base.
168 * 'base' is a pointer to a sequence of 'length' bytes.
173 lwres_buffer_invalidate(lwres_buffer_t *b);
175 * Make 'b' an invalid buffer.
178 * 'b' is a valid buffer.
181 * If assertion checking is enabled, future attempts to use 'b' without
182 * calling lwres_buffer_init() on it will cause an assertion failure.
186 lwres_buffer_add(lwres_buffer_t *b, unsigned int n);
188 * Increase the 'used' region of 'b' by 'n' bytes.
192 * 'b' is a valid buffer
199 lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n);
201 * Decrease the 'used' region of 'b' by 'n' bytes.
205 * 'b' is a valid buffer
212 lwres_buffer_clear(lwres_buffer_t *b);
214 * Make the used region empty.
218 * 'b' is a valid buffer
227 lwres_buffer_first(lwres_buffer_t *b);
229 * Make the consumed region empty.
233 * 'b' is a valid buffer
242 lwres_buffer_forward(lwres_buffer_t *b, unsigned int n);
244 * Increase the 'consumed' region of 'b' by 'n' bytes.
248 * 'b' is a valid buffer
250 * current + n <= used
255 lwres_buffer_back(lwres_buffer_t *b, unsigned int n);
257 * Decrease the 'consumed' region of 'b' by 'n' bytes.
261 * 'b' is a valid buffer
268 lwres_buffer_getuint8(lwres_buffer_t *b);
270 * Read an unsigned 8-bit integer from 'b' and return it.
274 * 'b' is a valid buffer.
276 * The length of the available region of 'b' is at least 1.
280 * The current pointer in 'b' is advanced by 1.
284 * A 8-bit unsigned integer.
288 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val);
290 * Store an unsigned 8-bit integer from 'val' into 'b'.
293 * 'b' is a valid buffer.
295 * The length of the unused region of 'b' is at least 1.
298 * The used pointer in 'b' is advanced by 1.
302 lwres_buffer_getuint16(lwres_buffer_t *b);
304 * Read an unsigned 16-bit integer in network byte order from 'b', convert
305 * it to host byte order, and return it.
309 * 'b' is a valid buffer.
311 * The length of the available region of 'b' is at least 2.
315 * The current pointer in 'b' is advanced by 2.
319 * A 16-bit unsigned integer.
323 lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val);
325 * Store an unsigned 16-bit integer in host byte order from 'val'
326 * into 'b' in network byte order.
329 * 'b' is a valid buffer.
331 * The length of the unused region of 'b' is at least 2.
334 * The used pointer in 'b' is advanced by 2.
338 lwres_buffer_getuint32(lwres_buffer_t *b);
340 * Read an unsigned 32-bit integer in network byte order from 'b', convert
341 * it to host byte order, and return it.
345 * 'b' is a valid buffer.
347 * The length of the available region of 'b' is at least 2.
351 * The current pointer in 'b' is advanced by 2.
355 * A 32-bit unsigned integer.
359 lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val);
361 * Store an unsigned 32-bit integer in host byte order from 'val'
362 * into 'b' in network byte order.
365 * 'b' is a valid buffer.
367 * The length of the unused region of 'b' is at least 4.
370 * The used pointer in 'b' is advanced by 4.
374 lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
375 unsigned int length);
377 * Copy 'length' bytes of memory at 'base' into 'b'.
380 * 'b' is a valid buffer.
382 * 'base' points to 'length' bytes of valid memory.
387 lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
388 unsigned int length);
390 * Copy 'length' bytes of memory from 'b' into 'base'.
393 * 'b' is a valid buffer.
395 * 'base' points to at least 'length' bytes of valid memory.
397 * 'b' have at least 'length' bytes remaining.
402 #endif /* LWRES_LWBUFFER_H */