2 * Copyright (C) 2004, 2005, 2007, 2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.c,v 1.15 2007/06/19 23:47:22 tbox Exp $ */
23 * These functions provide bounds checked access to a region of memory
24 * where data is being read or written. They are based on, and similar
25 * to, the isc_buffer_ functions in the ISC library.
27 * A buffer is a region of memory, together with a set of related
28 * subregions. The used region and the available region are disjoint, and
29 * their union is the buffer's region. The used region extends from the
30 * beginning of the buffer region to the last used byte. The available
31 * region extends from one byte greater than the last used byte to the
32 * end of the buffer's region. The size of the used region can be changed
33 * using various buffer commands. Initially, the used region is empty.
35 * The used region is further subdivided into two disjoint regions: the
36 * consumed region and the remaining region. The union of these two
37 * regions is the used region. The consumed region extends from the
38 * beginning of the used region to the byte before the current offset (if
39 * any). The remaining region the current pointer to the end of the used
40 * region. The size of the consumed region can be changed using various
41 * buffer commands. Initially, the consumed region is empty.
43 * The active region is an (optional) subregion of the remaining region.
44 * It extends from the current offset to an offset in the remaining
45 * region. Initially, the active region is empty. If the current offset
46 * advances beyond the chosen offset, the active region will also be
51 * /------------entire length---------------\\
52 * /----- used region -----\\/-- available --\\
53 * +----------------------------------------+
54 * | consumed | remaining | |
55 * +----------------------------------------+
58 * a == base of buffer.
59 * b == current pointer. Can be anywhere between a and d.
60 * c == active pointer. Meaningful between b and d.
62 * e == length of buffer.
64 * a-e == entire length of buffer.
66 * a-b == consumed region.
67 * b-d == remaining region.
68 * b-c == optional active region.
71 * lwres_buffer_init() initializes the lwres_buffer_t *b and assocates it
72 * with the memory region of size length bytes starting at location base.
74 * lwres_buffer_invalidate() marks the buffer *b as invalid. Invalidating
75 * a buffer after use is not required, but makes it possible to catch its
76 * possible accidental use.
78 * The functions lwres_buffer_add() and lwres_buffer_subtract()
79 * respectively increase and decrease the used space in buffer *b by n
80 * bytes. lwres_buffer_add() checks for buffer overflow and
81 * lwres_buffer_subtract() checks for underflow. These functions do not
82 * allocate or deallocate memory. They just change the value of used.
84 * A buffer is re-initialised by lwres_buffer_clear(). The function sets
85 * used , current and active to zero.
87 * lwres_buffer_first() makes the consumed region of buffer *p empty by
88 * setting current to zero (the start of the buffer).
90 * lwres_buffer_forward() increases the consumed region of buffer *b by n
91 * bytes, checking for overflow. Similarly, lwres_buffer_back() decreases
92 * buffer b's consumed region by n bytes and checks for underflow.
94 * lwres_buffer_getuint8() reads an unsigned 8-bit integer from *b and
95 * returns it. lwres_buffer_putuint8() writes the unsigned 8-bit integer
98 * lwres_buffer_getuint16() and lwres_buffer_getuint32() are identical to
99 * lwres_buffer_putuint8() except that they respectively read an unsigned
100 * 16-bit or 32-bit integer in network byte order from b. Similarly,
101 * lwres_buffer_putuint16() and lwres_buffer_putuint32() writes the
102 * unsigned 16-bit or 32-bit integer val to buffer b, in network byte
105 * Arbitrary amounts of data are read or written from a lightweight
106 * resolver buffer with lwres_buffer_getmem() and lwres_buffer_putmem()
107 * respectively. lwres_buffer_putmem() copies length bytes of memory at
108 * base to b. Conversely, lwres_buffer_getmem() copies length bytes of
109 * memory from b to base.
116 #include <lwres/lwbuffer.h>
118 #include "assert_p.h"
121 lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
124 * Make 'b' refer to the 'length'-byte region starting at base.
129 b->magic = LWRES_BUFFER_MAGIC;
137 /* Make 'b' an invalid buffer. */
139 lwres_buffer_invalidate(lwres_buffer_t *b)
142 REQUIRE(LWRES_BUFFER_VALID(b));
152 /* Increase the 'used' region of 'b' by 'n' bytes. */
154 lwres_buffer_add(lwres_buffer_t *b, unsigned int n)
157 REQUIRE(LWRES_BUFFER_VALID(b));
158 REQUIRE(b->used + n <= b->length);
163 /* Decrease the 'used' region of 'b' by 'n' bytes. */
165 lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
168 REQUIRE(LWRES_BUFFER_VALID(b));
169 REQUIRE(b->used >= n);
172 if (b->current > b->used)
173 b->current = b->used;
174 if (b->active > b->used)
178 /* Make the used region empty. */
180 lwres_buffer_clear(lwres_buffer_t *b)
183 REQUIRE(LWRES_BUFFER_VALID(b));
190 /* Make the consumed region empty. */
192 lwres_buffer_first(lwres_buffer_t *b)
195 REQUIRE(LWRES_BUFFER_VALID(b));
200 /* Increase the 'consumed' region of 'b' by 'n' bytes. */
202 lwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
205 REQUIRE(LWRES_BUFFER_VALID(b));
206 REQUIRE(b->current + n <= b->used);
211 /* Decrease the 'consumed' region of 'b' by 'n' bytes. */
213 lwres_buffer_back(lwres_buffer_t *b, unsigned int n)
216 REQUIRE(LWRES_BUFFER_VALID(b));
217 REQUIRE(n <= b->current);
222 /* Read an unsigned 8-bit integer from 'b' and return it. */
224 lwres_buffer_getuint8(lwres_buffer_t *b)
227 lwres_uint8_t result;
230 REQUIRE(LWRES_BUFFER_VALID(b));
231 REQUIRE(b->used - b->current >= 1);
236 result = ((unsigned int)(cp[0]));
241 /* Put an unsigned 8-bit integer */
243 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
247 REQUIRE(LWRES_BUFFER_VALID(b));
248 REQUIRE(b->used + 1 <= b->length);
253 cp[0] = (val & 0x00ff);
256 /* Read an unsigned 16-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
258 lwres_buffer_getuint16(lwres_buffer_t *b)
261 lwres_uint16_t result;
264 REQUIRE(LWRES_BUFFER_VALID(b));
265 REQUIRE(b->used - b->current >= 2);
270 result = ((unsigned int)(cp[0])) << 8;
271 result |= ((unsigned int)(cp[1]));
276 /* Put an unsigned 16-bit integer. */
278 lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
282 REQUIRE(LWRES_BUFFER_VALID(b));
283 REQUIRE(b->used + 2 <= b->length);
288 cp[0] = (val & 0xff00) >> 8;
289 cp[1] = (val & 0x00ff);
292 /* Read an unsigned 32-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
294 lwres_buffer_getuint32(lwres_buffer_t *b)
297 lwres_uint32_t result;
299 REQUIRE(LWRES_BUFFER_VALID(b));
300 REQUIRE(b->used - b->current >= 4);
305 result = ((unsigned int)(cp[0])) << 24;
306 result |= ((unsigned int)(cp[1])) << 16;
307 result |= ((unsigned int)(cp[2])) << 8;
308 result |= ((unsigned int)(cp[3]));
313 /* Put an unsigned 32-bit integer. */
315 lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
319 REQUIRE(LWRES_BUFFER_VALID(b));
320 REQUIRE(b->used + 4 <= b->length);
325 cp[0] = (unsigned char)((val & 0xff000000) >> 24);
326 cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
327 cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
328 cp[3] = (unsigned char)(val & 0x000000ff);
331 /* copies length bytes of memory at base to b */
333 lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
338 REQUIRE(LWRES_BUFFER_VALID(b));
339 REQUIRE(b->used + length <= b->length);
341 cp = (unsigned char *)b->base + b->used;
342 memmove(cp, base, length);
346 /* copies length bytes of memory at b to base */
348 lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
353 REQUIRE(LWRES_BUFFER_VALID(b));
354 REQUIRE(b->used - b->current >= length);
358 b->current += length;
360 memmove(base, cp, length);