Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / lwres / include / lwres / lwbuffer.h
bloba14caeb67bac3daa804ebfd4294bc8f85071eca7
1 /*
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.2.1 2004/03/09 06:12:37 marka Exp $ */
20 #ifndef LWRES_LWBUFFER_H
21 #define LWRES_LWBUFFER_H 1
23 /*****
24 ***** Module Info
25 *****/
28 * Buffers
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 * +----------------------------------------+
58 * a b c d e
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.
63 * d == used pointer.
64 * e == length of buffer.
66 * a-e == entire (length) of buffer.
67 * a-d == used region.
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:
74 * length > 0
76 * base is a valid pointer to length bytes of memory
78 * 0 <= used <= length
80 * 0 <= current <= used
82 * 0 <= active <= used
83 * (although active < current implies empty active region)
85 * MP:
86 * Buffers have no synchronization. Clients must ensure exclusive
87 * access.
89 * Reliability:
90 * No anticipated impact.
92 * Resources:
93 * Memory: 1 pointer + 6 unsigned integers per buffer.
95 * Security:
96 * No anticipated impact.
98 * Standards:
99 * None.
102 /***
103 *** Imports
104 ***/
106 #include <lwres/lang.h>
107 #include <lwres/int.h>
109 LWRES_LANG_BEGINDECLS
111 /***
112 *** Magic numbers
113 ***/
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
123 * another macro.)
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 {
146 unsigned int magic;
147 unsigned char *base;
148 /* The following integers are byte offsets from 'base'. */
149 unsigned int length;
150 unsigned int used;
151 unsigned int current;
152 unsigned int active;
155 /***
156 *** Functions
157 ***/
159 void
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.
164 * Requires:
166 * 'length' > 0
168 * 'base' is a pointer to a sequence of 'length' bytes.
172 void
173 lwres_buffer_invalidate(lwres_buffer_t *b);
175 * Make 'b' an invalid buffer.
177 * Requires:
178 * 'b' is a valid buffer.
180 * Ensures:
181 * If assertion checking is enabled, future attempts to use 'b' without
182 * calling lwres_buffer_init() on it will cause an assertion failure.
185 void
186 lwres_buffer_add(lwres_buffer_t *b, unsigned int n);
188 * Increase the 'used' region of 'b' by 'n' bytes.
190 * Requires:
192 * 'b' is a valid buffer
194 * used + n <= length
198 void
199 lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n);
201 * Decrease the 'used' region of 'b' by 'n' bytes.
203 * Requires:
205 * 'b' is a valid buffer
207 * used >= n
211 void
212 lwres_buffer_clear(lwres_buffer_t *b);
214 * Make the used region empty.
216 * Requires:
218 * 'b' is a valid buffer
220 * Ensures:
222 * used = 0
226 void
227 lwres_buffer_first(lwres_buffer_t *b);
229 * Make the consumed region empty.
231 * Requires:
233 * 'b' is a valid buffer
235 * Ensures:
237 * current == 0
241 void
242 lwres_buffer_forward(lwres_buffer_t *b, unsigned int n);
244 * Increase the 'consumed' region of 'b' by 'n' bytes.
246 * Requires:
248 * 'b' is a valid buffer
250 * current + n <= used
254 void
255 lwres_buffer_back(lwres_buffer_t *b, unsigned int n);
257 * Decrease the 'consumed' region of 'b' by 'n' bytes.
259 * Requires:
261 * 'b' is a valid buffer
263 * n <= current
267 lwres_uint8_t
268 lwres_buffer_getuint8(lwres_buffer_t *b);
270 * Read an unsigned 8-bit integer from 'b' and return it.
272 * Requires:
274 * 'b' is a valid buffer.
276 * The length of the available region of 'b' is at least 1.
278 * Ensures:
280 * The current pointer in 'b' is advanced by 1.
282 * Returns:
284 * A 8-bit unsigned integer.
287 void
288 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val);
290 * Store an unsigned 8-bit integer from 'val' into 'b'.
292 * Requires:
293 * 'b' is a valid buffer.
295 * The length of the unused region of 'b' is at least 1.
297 * Ensures:
298 * The used pointer in 'b' is advanced by 1.
301 lwres_uint16_t
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.
307 * Requires:
309 * 'b' is a valid buffer.
311 * The length of the available region of 'b' is at least 2.
313 * Ensures:
315 * The current pointer in 'b' is advanced by 2.
317 * Returns:
319 * A 16-bit unsigned integer.
322 void
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.
328 * Requires:
329 * 'b' is a valid buffer.
331 * The length of the unused region of 'b' is at least 2.
333 * Ensures:
334 * The used pointer in 'b' is advanced by 2.
337 lwres_uint32_t
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.
343 * Requires:
345 * 'b' is a valid buffer.
347 * The length of the available region of 'b' is at least 2.
349 * Ensures:
351 * The current pointer in 'b' is advanced by 2.
353 * Returns:
355 * A 32-bit unsigned integer.
358 void
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.
364 * Requires:
365 * 'b' is a valid buffer.
367 * The length of the unused region of 'b' is at least 4.
369 * Ensures:
370 * The used pointer in 'b' is advanced by 4.
373 void
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'.
379 * Requires:
380 * 'b' is a valid buffer.
382 * 'base' points to 'length' bytes of valid memory.
386 void
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'.
392 * Requires:
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.
400 LWRES_LANG_ENDDECLS
402 #endif /* LWRES_LWBUFFER_H */