Merge from vendor branch FILE:
[dragonfly.git] / contrib / bind-9.3 / lib / lwres / lwbuffer.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: lwbuffer.c,v 1.10.206.1 2004/03/06 08:15:31 marka Exp $ */
19
20 #include <config.h>
21
22 #include <string.h>
23
24 #include <lwres/lwbuffer.h>
25
26 #include "assert_p.h"
27
28 void
29 lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
30 {
31         /*
32          * Make 'b' refer to the 'length'-byte region starting at base.
33          */
34
35         REQUIRE(b != NULL);
36
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;
43 }
44
45 void
46 lwres_buffer_invalidate(lwres_buffer_t *b)
47 {
48         /*
49          * Make 'b' an invalid buffer.
50          */
51
52         REQUIRE(LWRES_BUFFER_VALID(b));
53
54         b->magic = 0;
55         b->base = NULL;
56         b->length = 0;
57         b->used = 0;
58         b->current = 0;
59         b->active = 0;
60 }
61
62 void
63 lwres_buffer_add(lwres_buffer_t *b, unsigned int n)
64 {
65         /*
66          * Increase the 'used' region of 'b' by 'n' bytes.
67          */
68
69         REQUIRE(LWRES_BUFFER_VALID(b));
70         REQUIRE(b->used + n <= b->length);
71
72         b->used += n;
73 }
74
75 void
76 lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
77 {
78         /*
79          * Decrease the 'used' region of 'b' by 'n' bytes.
80          */
81
82         REQUIRE(LWRES_BUFFER_VALID(b));
83         REQUIRE(b->used >= n);
84
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;
90 }
91
92 void
93 lwres_buffer_clear(lwres_buffer_t *b)
94 {
95         /*
96          * Make the used region empty.
97          */
98
99         REQUIRE(LWRES_BUFFER_VALID(b));
100
101         b->used = 0;
102         b->current = 0;
103         b->active = 0;
104 }
105
106 void
107 lwres_buffer_first(lwres_buffer_t *b)
108 {
109         /*
110          * Make the consumed region empty.
111          */
112
113         REQUIRE(LWRES_BUFFER_VALID(b));
114
115         b->current = 0;
116 }
117
118 void
119 lwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
120 {
121         /*
122          * Increase the 'consumed' region of 'b' by 'n' bytes.
123          */
124
125         REQUIRE(LWRES_BUFFER_VALID(b));
126         REQUIRE(b->current + n <= b->used);
127
128         b->current += n;
129 }
130
131 void
132 lwres_buffer_back(lwres_buffer_t *b, unsigned int n)
133 {
134         /*
135          * Decrease the 'consumed' region of 'b' by 'n' bytes.
136          */
137
138         REQUIRE(LWRES_BUFFER_VALID(b));
139         REQUIRE(n <= b->current);
140
141         b->current -= n;
142 }
143
144 lwres_uint8_t
145 lwres_buffer_getuint8(lwres_buffer_t *b)
146 {
147         unsigned char *cp;
148         lwres_uint8_t result;
149
150         /*
151          * Read an unsigned 8-bit integer from 'b' and return it.
152          */
153
154         REQUIRE(LWRES_BUFFER_VALID(b));
155         REQUIRE(b->used - b->current >= 1);
156
157         cp = b->base;
158         cp += b->current;
159         b->current += 1;
160         result = ((unsigned int)(cp[0]));
161
162         return (result);
163 }
164
165 void
166 lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
167 {
168         unsigned char *cp;
169
170         REQUIRE(LWRES_BUFFER_VALID(b));
171         REQUIRE(b->used + 1 <= b->length);
172
173         cp = b->base;
174         cp += b->used;
175         b->used += 1;
176         cp[0] = (val & 0x00ff);
177 }
178
179 lwres_uint16_t
180 lwres_buffer_getuint16(lwres_buffer_t *b)
181 {
182         unsigned char *cp;
183         lwres_uint16_t result;
184
185         /*
186          * Read an unsigned 16-bit integer in network byte order from 'b',
187          * convert it to host byte order, and return it.
188          */
189
190         REQUIRE(LWRES_BUFFER_VALID(b));
191         REQUIRE(b->used - b->current >= 2);
192
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]));
198
199         return (result);
200 }
201
202 void
203 lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
204 {
205         unsigned char *cp;
206
207         REQUIRE(LWRES_BUFFER_VALID(b));
208         REQUIRE(b->used + 2 <= b->length);
209
210         cp = b->base;
211         cp += b->used;
212         b->used += 2;
213         cp[0] = (val & 0xff00) >> 8;
214         cp[1] = (val & 0x00ff);
215 }
216
217 lwres_uint32_t
218 lwres_buffer_getuint32(lwres_buffer_t *b)
219 {
220         unsigned char *cp;
221         lwres_uint32_t result;
222
223         /*
224          * Read an unsigned 32-bit integer in network byte order from 'b',
225          * convert it to host byte order, and return it.
226          */
227
228         REQUIRE(LWRES_BUFFER_VALID(b));
229         REQUIRE(b->used - b->current >= 4);
230
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]));
238
239         return (result);
240 }
241
242 void
243 lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
244 {
245         unsigned char *cp;
246
247         REQUIRE(LWRES_BUFFER_VALID(b));
248         REQUIRE(b->used + 4 <= b->length);
249
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);
257 }
258
259 void
260 lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
261                     unsigned int length)
262 {
263         unsigned char *cp;
264
265         REQUIRE(LWRES_BUFFER_VALID(b));
266         REQUIRE(b->used + length <= b->length);
267
268         cp = (unsigned char *)b->base + b->used;
269         memcpy(cp, base, length);
270         b->used += length;
271 }
272
273 void
274 lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
275                     unsigned int length)
276 {
277         unsigned char *cp;
278
279         REQUIRE(LWRES_BUFFER_VALID(b));
280         REQUIRE(b->used - b->current >= length);
281
282         cp = b->base;
283         cp += b->current;
284         b->current += length;
285
286         memcpy(base, cp, length);
287 }