buffer.c 8.39 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 BUFFER_MAGIC			0x42756621U	/* Buf!. */
#define DYNBUFFER_MAGIC			0x64427566U	/* dBuf. */

28
29
#define VALID_BUFFER(b)			((b) != NULL && \
					 (b)->magic == BUFFER_MAGIC)
30
31
32

#define VALID_DYNBUFFER(b)		((b) != NULL && \
					 (b)->magic == DYNBUFFER_MAGIC)
Bob Halley's avatar
Bob Halley committed
33
34

void
Bob Halley's avatar
lint    
Bob Halley committed
35
isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
36
		unsigned int type) {
Bob Halley's avatar
Bob Halley committed
37
38
39
40
	/*
	 * Make 'b' refer to the 'length'-byte region starting at base.
	 */

41
	REQUIRE(b != NULL);
Bob Halley's avatar
Bob Halley committed
42

43
44
	b->magic = BUFFER_MAGIC;
	b->type = type;
Bob Halley's avatar
Bob Halley committed
45
46
47
	b->base = base;
	b->length = length;
	b->used = 0;
48
	b->current = 0;
49
	b->active = 0;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}

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;
66
	b->active = 0;
67
68
69
70
71
72
73
74
75
76
77
}

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
78
79
80
}

void
Bob Halley's avatar
Bob Halley committed
81
isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
82
83
84
85
86
87
88
89
90
91
92
93
	/*
	 * 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
94
isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
95
96
97
98
99
100
101
102
103
104
105
106
	/*
	 * 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
107
isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
108
109
110
111
112
113
114
	/*
	 * Make 'r' refer to the available region of 'b'.
	 */

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

Bob Halley's avatar
lint    
Bob Halley committed
115
	r->base = (unsigned char *)b->base + b->used;
Bob Halley's avatar
Bob Halley committed
116
117
118
119
120
	r->length = b->length - b->used;
}


void
Bob Halley's avatar
Bob Halley committed
121
isc_buffer_add(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
122
123
124
125
126
127
128
129
130
131
132
	/*
	 * 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
133
isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
134
135
136
137
138
139
140
141
	/*
	 * Decrease the 'used' region of 'b' by 'n' bytes.
	 */

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

	b->used -= n;
142
143
144
145
	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
146
147
148
}

void
Bob Halley's avatar
Bob Halley committed
149
isc_buffer_clear(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
150
151
152
153
154
	/*
	 * Make the used region empty.
	 */

	REQUIRE(VALID_BUFFER(b));
155

Bob Halley's avatar
Bob Halley committed
156
	b->used = 0;
157
158
	b->current = 0;
	b->active = 0;
Bob Halley's avatar
Bob Halley committed
159
160
161
}

void
Bob Halley's avatar
Bob Halley committed
162
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
163
164
165
166
167
168
169
170
	/*
	 * Make 'r' refer to the consumed region of 'b'.
	 */

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

	r->base = b->base;
171
	r->length = b->current;
Bob Halley's avatar
Bob Halley committed
172
173
174
}

void
Bob Halley's avatar
Bob Halley committed
175
isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
176
177
178
179
180
181
182
	/*
	 * Make 'r' refer to the remaining region of 'b'.
	 */

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

183
184
	r->base = (unsigned char *)b->base + b->current;
	r->length = b->used - b->current;
Bob Halley's avatar
Bob Halley committed
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
215
216
217
218
219
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
220
void
Bob Halley's avatar
Bob Halley committed
221
isc_buffer_first(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
222
223
224
225
226
227
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

228
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
229
230
231
}

void
Bob Halley's avatar
Bob Halley committed
232
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
233
	/*
234
	 * Increase the 'consumed' region of 'b' by 'n' bytes.
Bob Halley's avatar
Bob Halley committed
235
236
237
	 */

	REQUIRE(VALID_BUFFER(b));
238
	REQUIRE(b->current + n <= b->used);
Bob Halley's avatar
Bob Halley committed
239
240
241
242
243

	b->current += n;
}

void
Bob Halley's avatar
Bob Halley committed
244
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
245
246
247
248
249
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
250
	REQUIRE(n <= b->current);
Bob Halley's avatar
Bob Halley committed
251
252
253
254
255

	b->current -= n;
}

void
Bob Halley's avatar
Bob Halley committed
256
isc_buffer_compact(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
257
	unsigned int length;
258
	void *src;
Bob Halley's avatar
Bob Halley committed
259
260
261
262
263
264
265
266
267

	/*
	 * 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));

268
269
270
	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
271

272
273
274
275
	if (b->active > b->current)
		b->active -= b->current;
	else
		b->active = 0;
276
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
277
278
	b->used = length;
}
Bob Halley's avatar
Bob Halley committed
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
isc_uint8_t
isc_buffer_getuint8(isc_buffer_t *b) {
	unsigned char *cp;
	isc_uint8_t result;

	/*
	 * Read an unsigned 8-bit integer from 'b' and return it.
	 */

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

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

	return (result);
}

void
isc_buffer_putuint8(isc_buffer_t *b, isc_uint8_t val)
{
	unsigned char *cp;

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

	cp = b->base;
	cp += b->used;
	b->used += 1;
	cp[0] = (val & 0x00ff);
}

Bob Halley's avatar
Bob Halley committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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);
}

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
void
isc_buffer_putuint16(isc_buffer_t *b, isc_uint16_t val)
{
	unsigned char *cp;

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

	cp = b->base;
	cp += b->used;
	b->used += 2;
	cp[0] = (val & 0xff00) >> 8;
	cp[1] = (val & 0x00ff);
}

Bob Halley's avatar
Bob Halley committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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);
}
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

void
isc_buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
{
	unsigned char *cp;

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

	cp = b->base;
	cp += b->used;
	b->used += 4;
	cp[0] = (val & 0xff000000) >> 24;
	cp[1] = (val & 0x00ff0000) >> 16;
	cp[2] = (val & 0x0000ff00) >> 8;
	cp[3] = (val & 0x000000ff);
}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

isc_result_t
isc_dynbuffer_allocate(isc_mem_t *mctx, isc_dynbuffer_t **dynbuffer,
		       unsigned int length, unsigned int type)
{
	isc_dynbuffer_t *dbuf;

	REQUIRE(dynbuffer != NULL);
	REQUIRE(*dynbuffer == NULL);

	dbuf = isc_mem_get(mctx, length + sizeof(isc_dynbuffer_t));
	if (dbuf == NULL)
		return (ISC_R_NOMEMORY);

	dbuf->magic = DYNBUFFER_MAGIC;
	ISC_LINK_INIT(dbuf, link);
	isc_buffer_init(&dbuf->buffer,
			((unsigned char *)dbuf) + sizeof(isc_dynbuffer_t),
			length, type);

	*dynbuffer = dbuf;

	return (ISC_R_SUCCESS);
}

416
417
418
419
420
421
422
423
void
isc_dynbuffer_reset(isc_dynbuffer_t *dynbuffer)
{
	REQUIRE(VALID_DYNBUFFER(dynbuffer));

	isc_buffer_clear(&dynbuffer->buffer);
}

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
void
isc_dynbuffer_free(isc_mem_t *mctx, isc_dynbuffer_t **dynbuffer)
{
	unsigned int real_length;
	isc_dynbuffer_t *dbuf;

	REQUIRE(dynbuffer != NULL);
	REQUIRE(VALID_DYNBUFFER(*dynbuffer));

	dbuf = *dynbuffer;
	*dynbuffer = NULL;	/* destroy external reference */

	real_length = dbuf->buffer.length + sizeof(isc_dynbuffer_t);
	isc_buffer_invalidate(&dbuf->buffer);
	dbuf->magic = 0;

	isc_mem_put(mctx, dbuf, real_length);
}