buffer.c 5.25 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	b->current = 0;
}

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;
}

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
71
72
73
}

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

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

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


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

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

	b->used -= n;
}

void
Bob Halley's avatar
Bob Halley committed
138
isc_buffer_clear(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
139
140
141
142
143
	/*
	 * Make the used region empty.
	 */

	REQUIRE(VALID_BUFFER(b));
144

Bob Halley's avatar
Bob Halley committed
145
146
147
148
	b->used = 0;
}

void
Bob Halley's avatar
Bob Halley committed
149
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
150
151
152
153
154
155
156
157
	/*
	 * Make 'r' refer to the consumed region of 'b'.
	 */

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

	r->base = b->base;
158
	r->length = b->current;
Bob Halley's avatar
Bob Halley committed
159
160
161
}

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

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

170
171
	r->base = (unsigned char *)b->base + b->current;
	r->length = b->used - b->current;
Bob Halley's avatar
Bob Halley committed
172
173
174
}

void
Bob Halley's avatar
Bob Halley committed
175
isc_buffer_first(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
176
177
178
179
180
181
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

182
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
183
184
185
}

void
Bob Halley's avatar
Bob Halley committed
186
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
187
	/*
188
	 * Increase the 'consumed' region of 'b' by 'n' bytes.
Bob Halley's avatar
Bob Halley committed
189
190
191
	 */

	REQUIRE(VALID_BUFFER(b));
192
	REQUIRE(b->current + n <= b->used);
Bob Halley's avatar
Bob Halley committed
193
194
195
196
197

	b->current += n;
}

void
Bob Halley's avatar
Bob Halley committed
198
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
199
200
201
202
203
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

	REQUIRE(VALID_BUFFER(b));
204
	REQUIRE(n <= b->current);
Bob Halley's avatar
Bob Halley committed
205
206
207
208
209

	b->current -= n;
}

void
Bob Halley's avatar
Bob Halley committed
210
isc_buffer_compact(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
211
	unsigned int length;
212
	void *src;
Bob Halley's avatar
Bob Halley committed
213
214
215
216
217
218
219
220
221

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

222
223
224
	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
225

226
	b->current = 0;
Bob Halley's avatar
Bob Halley committed
227
228
	b->used = length;
}
Bob Halley's avatar
Bob Halley committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

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