buffer.c 3.02 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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#include <string.h>

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

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

void
isc_buffer_init(isc_buffer_t b,	unsigned char *base, unsigned int length) {
	/*
	 * 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
isc_buffer_region(isc_buffer_t b, isc_region_t r) {
	/*
	 * Make 'r' refer to the region of 'b'.
	 */

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

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

void
isc_buffer_used(isc_buffer_t b, isc_region_t r) {
	/*
	 * 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
isc_buffer_available(isc_buffer_t b, isc_region_t r) {
	/*
	 * 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
isc_buffer_add(isc_buffer_t b, unsigned int n) {
	/*
	 * Increase the 'used' region of 'b' by 'n' bytes.
	 */

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

	b->used += n;
}

void
isc_buffer_subtract(isc_buffer_t b, unsigned int n) {
	/*
	 * Decrease the 'used' region of 'b' by 'n' bytes.
	 */

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

	b->used -= n;
}

void
isc_buffer_clear(isc_buffer_t b) {
	/*
	 * Make the used region empty.
	 */

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

void
isc_buffer_consumed(isc_buffer_t b, isc_region_t r) {
	/*
	 * 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
isc_buffer_remaining(isc_buffer_t b, isc_region_t r) {
	/*
	 * 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
isc_buffer_first(isc_buffer_t b) {
	/*
	 * Make the consumed region empty.
	 */

	REQUIRE(VALID_BUFFER(b));

	b->current = b->base;
}

void
isc_buffer_forward(isc_buffer_t b, unsigned int n) {
	/*
	 * 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
isc_buffer_back(isc_buffer_t b, unsigned int n) {
	/*
	 * Decrease the 'consumed' region of 'b' by 'n' bytes.
	 */

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

	b->current -= n;
}

void
isc_buffer_compact(isc_buffer_t b) {
	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;
}