buffer.c 6.07 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) 1998, 1999  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
Bob Halley's avatar
Bob Halley committed
17

Bob Halley's avatar
Bob Halley committed
18
19
#include <config.h>

Bob Halley's avatar
Bob Halley committed
20
21
22
23
24
#include <string.h>

#include <isc/assertions.h>
#include <isc/buffer.h>

25
26
27
#define VALID_BUFFER(b)			((b) != NULL && \
					 (b)->magic == BUFFER_MAGIC)
#define BUFFER_MAGIC			0x42756621U	/* Buf!. */
Bob Halley's avatar
Bob Halley committed
28
29

void
Bob Halley's avatar
lint    
Bob Halley committed
30
isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
31
		unsigned int type) {
Bob Halley's avatar
Bob Halley committed
32
33
34
35
	/*
	 * Make 'b' refer to the 'length'-byte region starting at base.
	 */

36
	REQUIRE(b != NULL);
Bob Halley's avatar
Bob Halley committed
37

38
39
	b->magic = BUFFER_MAGIC;
	b->type = type;
Bob Halley's avatar
Bob Halley committed
40
41
42
	b->base = base;
	b->length = length;
	b->used = 0;
43
	b->current = 0;
44
	b->active = 0;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}

void
isc_buffer_invalidate(isc_buffer_t *b) {
	/*
	 * Make 'b' an invalid buffer.
	 */

	REQUIRE(VALID_BUFFER(b));
	
	b->magic = 0;
	b->type = 0;
	b->base = NULL;
	b->length = 0;
	b->used = 0;
	b->current = 0;
61
	b->active = 0;
62
63
64
65
66
67
68
69
70
71
72
}

unsigned int
isc_buffer_type(isc_buffer_t *b) {
	/*
	 * The type of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));

	return (b->type);
Bob Halley's avatar
Bob Halley committed
73
74
75
}

void
Bob Halley's avatar
Bob Halley committed
76
isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
77
78
79
80
81
82
83
84
85
86
87
88
	/*
	 * Make 'r' refer to the region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

	r->base = b->base;
	r->length = b->length;
}

void
Bob Halley's avatar
Bob Halley committed
89
isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
90
91
92
93
94
95
96
97
98
99
100
101
	/*
	 * Make 'r' refer to the used region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

	r->base = b->base;
	r->length = b->used;
}

void
Bob Halley's avatar
Bob Halley committed
102
isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
103
104
105
106
107
108
109
	/*
	 * Make 'r' refer to the available region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

Bob Halley's avatar
lint    
Bob Halley committed
110
	r->base = (unsigned char *)b->base + b->used;
Bob Halley's avatar
Bob Halley committed
111
112
113
114
115
	r->length = b->length - b->used;
}


void
Bob Halley's avatar
Bob Halley committed
116
isc_buffer_add(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
117
118
119
120
121
122
123
124
125
126
127
	/*
	 * Increase the 'used' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(b->used + n <= b->length);

	b->used += n;
}

void
Bob Halley's avatar
Bob Halley committed
128
isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
129
130
131
132
133
134
135
136
	/*
	 * Decrease the 'used' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(b->used >= n);

	b->used -= n;
137
138
139
140
	if (b->current > b->used)
		b->current = b->used;
	if (b->active > b->used)
		b->active = b->used;
Bob Halley's avatar
Bob Halley committed
141
142
143
}

void
Bob Halley's avatar
Bob Halley committed
144
isc_buffer_clear(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
145
146
147
148
149
	/*
	 * Make the used region empty.
	 */

	REQUIRE(VALID_BUFFER(b));
150

Bob Halley's avatar
Bob Halley committed
151
	b->used = 0;
152
153
	b->current = 0;
	b->active = 0;
Bob Halley's avatar
Bob Halley committed
154
155
156
}

void
Bob Halley's avatar
Bob Halley committed
157
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
158
159
160
161
162
163
164
165
	/*
	 * Make 'r' refer to the consumed region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

	r->base = b->base;
166
	r->length = b->current;
Bob Halley's avatar
Bob Halley committed
167
168
169
}

void
Bob Halley's avatar
Bob Halley committed
170
isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
171
172
173
174
175
176
177
	/*
	 * Make 'r' refer to the remaining region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

178
179
	r->base = (unsigned char *)b->base + b->current;
	r->length = b->used - b->current;
Bob Halley's avatar
Bob Halley committed
180
181
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
void
isc_buffer_active(isc_buffer_t *b, isc_region_t *r) {
	/*
	 * Make 'r' refer to the active region of 'b'.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(r != NULL);

	if (b->current < b->active) {
		r->base = (unsigned char *)b->base + b->current;
		r->length = b->active - b->current;
	} else {
		r->base = NULL;
		r->length = 0;
	}
}

void
isc_buffer_setactive(isc_buffer_t *b, unsigned int n) {
	unsigned int active;

	/*
	 * Sets the end of the active region 'n' bytes after current.
	 */

	REQUIRE(VALID_BUFFER(b));
	active = b->current + n;
	REQUIRE(active <= b->used);

	b->active = active;
}

Bob Halley's avatar
Bob Halley committed
215
void
Bob Halley's avatar
Bob Halley committed
216
isc_buffer_first(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
217
218
219
220
221
222
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

223
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
224
225
226
}

void
Bob Halley's avatar
Bob Halley committed
227
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
228
	/*
229
	 * Increase the 'consumed' region of 'b' by 'n' bytes.
Bob Halley's avatar
Bob Halley committed
230
231
232
	 */

	REQUIRE(VALID_BUFFER(b));
233
	REQUIRE(b->current + n <= b->used);
Bob Halley's avatar
Bob Halley committed
234
235
236
237
238

	b->current += n;
}

void
Bob Halley's avatar
Bob Halley committed
239
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
240
241
242
243
244
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
245
	REQUIRE(n <= b->current);
Bob Halley's avatar
Bob Halley committed
246
247
248
249
250

	b->current -= n;
}

void
Bob Halley's avatar
Bob Halley committed
251
isc_buffer_compact(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
252
	unsigned int length;
253
	void *src;
Bob Halley's avatar
Bob Halley committed
254
255
256
257
258
259
260
261
262

	/*
	 * Compact the used region by moving the remaining region so it occurs
	 * at the start of the buffer.  The used region is shrunk by the size
	 * of the consumed region, and the consumed region is then made empty.
	 */

	REQUIRE(VALID_BUFFER(b));

263
264
265
	src = (unsigned char *)b->base + b->current;
	length = b->used - b->current;
	(void)memmove(b->base, src, (size_t)length);
Bob Halley's avatar
Bob Halley committed
266

267
268
269
270
	if (b->active > b->current)
		b->active -= b->current;
	else
		b->active = 0;
271
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
272
273
	b->used = length;
}
Bob Halley's avatar
Bob Halley committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

isc_uint16_t
isc_buffer_getuint16(isc_buffer_t *b) {
	unsigned char *cp;
	isc_uint16_t result;

	/*
	 * Read an unsigned 16-bit integer in network byte order from 'b',
	 * convert it to host byte order, and return it.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(b->used - b->current >= 2);

	cp = b->base;
	cp += b->current;
	b->current += 2;
	result = ((unsigned int)(cp[0])) << 8;
	result |= ((unsigned int)(cp[1]));

	return (result);
}

isc_uint32_t
isc_buffer_getuint32(isc_buffer_t *b) {
	unsigned char *cp;
	isc_uint32_t result;

	/*
	 * Read an unsigned 32-bit integer in network byte order from 'b',
	 * convert it to host byte order, and return it.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(b->used - b->current >= 4);

	cp = b->base;
	cp += b->current;
	b->current += 4;
	result = ((unsigned int)(cp[0])) << 24;
	result |= ((unsigned int)(cp[1])) << 16;
	result |= ((unsigned int)(cp[2])) << 8;
	result |= ((unsigned int)(cp[3]));

	return (result);
}