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.c,v 1.10.206.1 2004/03/06 08:15:31 marka Exp $ */
24 #include <lwres/lwbuffer.h>
29 lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
32 * Make 'b' refer to the 'length'-byte region starting at base.
37 b->magic = LWRES_BUFFER_MAGIC;
46 lwres_buffer_invalidate(lwres_buffer_t *b)
49 * Make 'b' an invalid buffer.
52 REQUIRE(LWRES_BUFFER_VALID(b));
63 lwres_buffer_add(lwres_buffer_t *b, unsigned int n)
66 * Increase the 'used' region of 'b' by 'n' bytes.
69 REQUIRE(LWRES_BUFFER_VALID(b));
70 REQUIRE(b->used + n <= b->length);
76 lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
79 * Decrease the 'used' region of 'b' by 'n' bytes.
82 REQUIRE(LWRES_BUFFER_VALID(b));
83 REQUIRE(b->used >= n);
86 if (b->current > b->used)
88 if (b->active > b->used)
93 lwres_buffer_clear(lwres_buffer_t *b)
96 * Make the used region empty.
99 REQUIRE(LWRES_BUFFER_VALID(b));
107 lwres_buffer_first(lwres_buffer_t *b)
110 * Make the consumed region empty.
113 REQUIRE(LWRES_BUFFER_VALID(b));
119 lwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
122 * Increase the 'consumed' region of 'b' by 'n' bytes.
125 REQUIRE(LWRES_BUFFER_VALID(b));
126 REQUIRE(b->current + n <= b->used);
132 lwres_buffer_back(lwres_buffer_t *b, unsigned int n)
135 * Decrease the 'consumed' region of 'b' by 'n' bytes.
138 REQUIRE(LWRES_BUFFER_VALID(b));
139 REQUIRE(n <= b->current);
145 lwres_buffer_getuint8(lwres_buffer_t *b)
148 lwres_uint8_t result;
151 * Read an unsigned 8-bit integer from 'b' and return it.
154 REQUIRE(LWRES_BUFFER_VALID(b));
155 REQUIRE(b->used - b->current >= 1);
160 result = ((unsigned int)(cp[0]));
166 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
170 REQUIRE(LWRES_BUFFER_VALID(b));
171 REQUIRE(b->used + 1 <= b->length);
176 cp[0] = (val & 0x00ff);
180 lwres_buffer_getuint16(lwres_buffer_t *b)
183 lwres_uint16_t result;
186 * Read an unsigned 16-bit integer in network byte order from 'b',
187 * convert it to host byte order, and return it.
190 REQUIRE(LWRES_BUFFER_VALID(b));
191 REQUIRE(b->used - b->current >= 2);
196 result = ((unsigned int)(cp[0])) << 8;
197 result |= ((unsigned int)(cp[1]));
203 lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
207 REQUIRE(LWRES_BUFFER_VALID(b));
208 REQUIRE(b->used + 2 <= b->length);
213 cp[0] = (val & 0xff00) >> 8;
214 cp[1] = (val & 0x00ff);
218 lwres_buffer_getuint32(lwres_buffer_t *b)
221 lwres_uint32_t result;
224 * Read an unsigned 32-bit integer in network byte order from 'b',
225 * convert it to host byte order, and return it.
228 REQUIRE(LWRES_BUFFER_VALID(b));
229 REQUIRE(b->used - b->current >= 4);
234 result = ((unsigned int)(cp[0])) << 24;
235 result |= ((unsigned int)(cp[1])) << 16;
236 result |= ((unsigned int)(cp[2])) << 8;
237 result |= ((unsigned int)(cp[3]));
243 lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
247 REQUIRE(LWRES_BUFFER_VALID(b));
248 REQUIRE(b->used + 4 <= b->length);
253 cp[0] = (unsigned char)((val & 0xff000000) >> 24);
254 cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
255 cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
256 cp[3] = (unsigned char)(val & 0x000000ff);
260 lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
265 REQUIRE(LWRES_BUFFER_VALID(b));
266 REQUIRE(b->used + length <= b->length);
268 cp = (unsigned char *)b->base + b->used;
269 memcpy(cp, base, length);
274 lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
279 REQUIRE(LWRES_BUFFER_VALID(b));
280 REQUIRE(b->used - b->current >= length);
284 b->current += length;
286 memcpy(base, cp, length);