Import bind-9.3.4
[dragonfly.git] / contrib / bind-9.3 / lib / lwres / lwbuffer.c
blob69009f00aebc79b5401b2f0e3369451816844983
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.c,v 1.10.206.1 2004/03/06 08:15:31 marka Exp $ */
20 #include <config.h>
22 #include <string.h>
24 #include <lwres/lwbuffer.h>
26 #include "assert_p.h"
28 void
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.
35 REQUIRE(b != NULL);
37 b->magic = LWRES_BUFFER_MAGIC;
38 b->base = base;
39 b->length = length;
40 b->used = 0;
41 b->current = 0;
42 b->active = 0;
45 void
46 lwres_buffer_invalidate(lwres_buffer_t *b)
49 * Make 'b' an invalid buffer.
52 REQUIRE(LWRES_BUFFER_VALID(b));
54 b->magic = 0;
55 b->base = NULL;
56 b->length = 0;
57 b->used = 0;
58 b->current = 0;
59 b->active = 0;
62 void
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);
72 b->used += n;
75 void
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);
85 b->used -= n;
86 if (b->current > b->used)
87 b->current = b->used;
88 if (b->active > b->used)
89 b->active = b->used;
92 void
93 lwres_buffer_clear(lwres_buffer_t *b)
96 * Make the used region empty.
99 REQUIRE(LWRES_BUFFER_VALID(b));
101 b->used = 0;
102 b->current = 0;
103 b->active = 0;
106 void
107 lwres_buffer_first(lwres_buffer_t *b)
110 * Make the consumed region empty.
113 REQUIRE(LWRES_BUFFER_VALID(b));
115 b->current = 0;
118 void
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);
128 b->current += n;
131 void
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);
141 b->current -= n;
144 lwres_uint8_t
145 lwres_buffer_getuint8(lwres_buffer_t *b)
147 unsigned char *cp;
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);
157 cp = b->base;
158 cp += b->current;
159 b->current += 1;
160 result = ((unsigned int)(cp[0]));
162 return (result);
165 void
166 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
168 unsigned char *cp;
170 REQUIRE(LWRES_BUFFER_VALID(b));
171 REQUIRE(b->used + 1 <= b->length);
173 cp = b->base;
174 cp += b->used;
175 b->used += 1;
176 cp[0] = (val & 0x00ff);
179 lwres_uint16_t
180 lwres_buffer_getuint16(lwres_buffer_t *b)
182 unsigned char *cp;
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);
193 cp = b->base;
194 cp += b->current;
195 b->current += 2;
196 result = ((unsigned int)(cp[0])) << 8;
197 result |= ((unsigned int)(cp[1]));
199 return (result);
202 void
203 lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
205 unsigned char *cp;
207 REQUIRE(LWRES_BUFFER_VALID(b));
208 REQUIRE(b->used + 2 <= b->length);
210 cp = b->base;
211 cp += b->used;
212 b->used += 2;
213 cp[0] = (val & 0xff00) >> 8;
214 cp[1] = (val & 0x00ff);
217 lwres_uint32_t
218 lwres_buffer_getuint32(lwres_buffer_t *b)
220 unsigned char *cp;
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);
231 cp = b->base;
232 cp += b->current;
233 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]));
239 return (result);
242 void
243 lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
245 unsigned char *cp;
247 REQUIRE(LWRES_BUFFER_VALID(b));
248 REQUIRE(b->used + 4 <= b->length);
250 cp = b->base;
251 cp += b->used;
252 b->used += 4;
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);
259 void
260 lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
261 unsigned int length)
263 unsigned char *cp;
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);
270 b->used += length;
273 void
274 lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
275 unsigned int length)
277 unsigned char *cp;
279 REQUIRE(LWRES_BUFFER_VALID(b));
280 REQUIRE(b->used - b->current >= length);
282 cp = b->base;
283 cp += b->current;
284 b->current += length;
286 memcpy(base, cp, length);