buffer.c 3.87 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (C) 1998  Internet Software Consortium.
 * 
 * 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
25
26
27
#include <string.h>

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

#define VALID_BUFFER(b)			((b) != NULL)

void
Bob Halley's avatar
Bob Halley committed
28
isc_buffer_init(isc_buffer_t *b, unsigned char *base, unsigned int length) {
Bob Halley's avatar
Bob Halley committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	/*
	 * Make 'b' refer to the 'length'-byte region starting at base.
	 */

	REQUIRE(VALID_BUFFER(b));
	REQUIRE(length > 0);

	b->base = base;
	b->length = length;
	b->used = 0;
	b->current = base;
}

void
Bob Halley's avatar
Bob Halley committed
43
isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
44
45
46
47
48
49
50
51
52
53
54
55
	/*
	 * 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
56
isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
57
58
59
60
61
62
63
64
65
66
67
68
	/*
	 * 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
69
isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
70
71
72
73
74
75
76
77
78
79
80
81
82
	/*
	 * Make 'r' refer to the available region of 'b'.
	 */

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

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


void
Bob Halley's avatar
Bob Halley committed
83
isc_buffer_add(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
84
85
86
87
88
89
90
91
92
93
94
	/*
	 * 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
95
isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
96
97
98
99
100
101
102
103
104
105
106
	/*
	 * 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
107
isc_buffer_clear(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
108
109
110
111
112
113
114
115
116
	/*
	 * Make the used region empty.
	 */

	REQUIRE(VALID_BUFFER(b));
	b->used = 0;
}

void
Bob Halley's avatar
Bob Halley committed
117
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
118
119
120
121
122
123
124
125
126
127
128
129
	/*
	 * Make 'r' refer to the consumed region of 'b'.
	 */

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

	r->base = b->base;
	r->length = (unsigned int)(b->current - b->base);
}

void
Bob Halley's avatar
Bob Halley committed
130
isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
Bob Halley's avatar
Bob Halley committed
131
132
133
134
135
136
137
138
139
140
141
142
	/*
	 * Make 'r' refer to the remaining region of 'b'.
	 */

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

	r->base = b->current;
	r->length = (unsigned int)((b->base + b->used) - b->current);
}

void
Bob Halley's avatar
Bob Halley committed
143
isc_buffer_first(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
144
145
146
147
148
149
150
151
152
153
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

	b->current = b->base;
}

void
Bob Halley's avatar
Bob Halley committed
154
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
155
156
157
158
159
160
161
162
163
164
165
	/*
	 * Decrease the 'used' region of 'b' by 'n' bytes.
	 */

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

	b->current += n;
}

void
Bob Halley's avatar
Bob Halley committed
166
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
Bob Halley's avatar
Bob Halley committed
167
168
169
170
171
172
173
174
175
176
177
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

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

	b->current -= n;
}

void
Bob Halley's avatar
Bob Halley committed
178
isc_buffer_compact(isc_buffer_t *b) {
Bob Halley's avatar
Bob Halley committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	unsigned int length;

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

	length = (unsigned int)((b->base + b->used) - b->current);

	(void)memmove(b->base, b->current, length);

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