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

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

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

38
	REQUIRE(b != NULL);
Bob Halley's avatar
Bob Halley committed
39

40
41
	b->magic = BUFFER_MAGIC;
	b->type = type;
Bob Halley's avatar
Bob Halley committed
42
43
44
	b->base = base;
	b->length = length;
	b->used = 0;
45
	b->current = 0;
46
	b->active = 0;
47
48
	b->mctx = NULL;
	ISC_LINK_INIT(b, link);
49
50
51
52
53
54
55
56
57
}

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

	REQUIRE(VALID_BUFFER(b));
58
59
	REQUIRE(!ISC_LINK_LINKED(b, link));
	REQUIRE(b->mctx == NULL);
60
61
62
63
64
65
66
	
	b->magic = 0;
	b->type = 0;
	b->base = NULL;
	b->length = 0;
	b->used = 0;
	b->current = 0;
67
	b->active = 0;
68
69
70
71
72
73
74
75
76
77
78
}

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

void
Bob Halley's avatar
Bob Halley committed
82
isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
83
84
85
86
87
88
89
90
91
92
93
94
	/*
	 * 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
95
isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
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;
}

107
108
109
110
111
112
113
114
unsigned int
isc_buffer_usedcount(isc_buffer_t *b)
{
	REQUIRE(VALID_BUFFER(b));

	return (b->used);
}

Bob Halley's avatar
Bob Halley committed
115
void
Bob Halley's avatar
Bob Halley committed
116
isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
117
118
119
120
121
122
123
	/*
	 * Make 'r' refer to the available region of 'b'.
	 */

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

Bob Halley's avatar
lint    
Bob Halley committed
124
	r->base = (unsigned char *)b->base + b->used;
Bob Halley's avatar
Bob Halley committed
125
126
127
	r->length = b->length - b->used;
}

128
129
130
131
132
133
134
unsigned int
isc_buffer_availablecount(isc_buffer_t *b)
{
	REQUIRE(VALID_BUFFER(b));

	return (b->length - b->used);
}
Bob Halley's avatar
Bob Halley committed
135
136

void
Bob Halley's avatar
Bob Halley committed
137
isc_buffer_add(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
138
139
140
141
142
143
144
145
146
147
148
	/*
	 * 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
149
isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
150
151
152
153
154
155
156
157
	/*
	 * Decrease the 'used' region of 'b' by 'n' bytes.
	 */

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

	b->used -= n;
158
159
160
161
	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
162
163
164
}

void
Bob Halley's avatar
Bob Halley committed
165
isc_buffer_clear(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
166
167
168
169
170
	/*
	 * Make the used region empty.
	 */

	REQUIRE(VALID_BUFFER(b));
171

Bob Halley's avatar
Bob Halley committed
172
	b->used = 0;
173
174
	b->current = 0;
	b->active = 0;
Bob Halley's avatar
Bob Halley committed
175
176
177
}

void
Bob Halley's avatar
Bob Halley committed
178
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
179
180
181
182
183
184
185
186
	/*
	 * Make 'r' refer to the consumed region of 'b'.
	 */

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

	r->base = b->base;
187
	r->length = b->current;
Bob Halley's avatar
Bob Halley committed
188
189
190
}

void
Bob Halley's avatar
Bob Halley committed
191
isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
192
193
194
195
196
197
198
	/*
	 * Make 'r' refer to the remaining region of 'b'.
	 */

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

199
200
	r->base = (unsigned char *)b->base + b->current;
	r->length = b->used - b->current;
Bob Halley's avatar
Bob Halley committed
201
202
}

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
236
void
Bob Halley's avatar
Bob Halley committed
237
isc_buffer_first(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
238
239
240
241
242
243
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

244
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
245
246
247
}

void
Bob Halley's avatar
Bob Halley committed
248
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
249
	/*
250
	 * Increase the 'consumed' region of 'b' by 'n' bytes.
Bob Halley's avatar
Bob Halley committed
251
252
253
	 */

	REQUIRE(VALID_BUFFER(b));
254
	REQUIRE(b->current + n <= b->used);
Bob Halley's avatar
Bob Halley committed
255
256
257
258
259

	b->current += n;
}

void
Bob Halley's avatar
Bob Halley committed
260
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
261
262
263
264
265
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
266
	REQUIRE(n <= b->current);
Bob Halley's avatar
Bob Halley committed
267
268
269
270
271

	b->current -= n;
}

void
Bob Halley's avatar
Bob Halley committed
272
isc_buffer_compact(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
273
	unsigned int length;
274
	void *src;
Bob Halley's avatar
Bob Halley committed
275
276
277
278
279
280
281
282
283

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

284
285
286
	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
287

288
289
290
291
	if (b->active > b->current)
		b->active -= b->current;
	else
		b->active = 0;
292
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
293
294
	b->used = length;
}
Bob Halley's avatar
Bob Halley committed
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
320
321
322
323
324
325
326
327
328
329
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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);
}

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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);
}
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

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);
}
407
408

isc_result_t
409
410
isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
		    unsigned int length, unsigned int type)
411
{
412
	isc_buffer_t *dbuf;
413
414
415
416

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

417
	dbuf = isc_mem_get(mctx, length + sizeof(isc_buffer_t));
418
419
420
	if (dbuf == NULL)
		return (ISC_R_NOMEMORY);

421
	isc_buffer_init(dbuf, ((unsigned char *)dbuf) + sizeof(isc_buffer_t),
422
			length, type);
423
	dbuf->mctx = mctx;
424
425
426
427
428
429

	*dynbuffer = dbuf;

	return (ISC_R_SUCCESS);
}

430
void
431
isc_buffer_free(isc_buffer_t **dynbuffer)
432
433
{
	unsigned int real_length;
434
435
	isc_buffer_t *dbuf;
	isc_mem_t *mctx;
436
437

	REQUIRE(dynbuffer != NULL);
438
439
	REQUIRE(VALID_BUFFER(*dynbuffer));
	REQUIRE((*dynbuffer)->mctx != NULL);
440
441
442
443

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

444
445
446
447
	real_length = dbuf->length + sizeof(isc_buffer_t);
	mctx = dbuf->mctx;
	dbuf->mctx = NULL;
	isc_buffer_invalidate(dbuf);
448
449
450

	isc_mem_put(mctx, dbuf, real_length);
}