1
2
3
4
5
6
7
8
9#include <common.h>
10#include <errno.h>
11#include <log.h>
12#include <malloc.h>
13#include "membuff.h"
14
15void membuff_purge(struct membuff *mb)
16{
17
18 mb->head = mb->start;
19 mb->tail = mb->start;
20}
21
22static int membuff_putrawflex(struct membuff *mb, int maxlen, bool update,
23 char ***data, int *offsetp)
24{
25 int len;
26
27
28 assert(data && offsetp);
29 *data = &mb->start;
30 *offsetp = mb->head - mb->start;
31
32
33 if (!mb->start)
34 return 0;
35
36
37
38
39
40 if (mb->head >= mb->tail) {
41
42 len = mb->end - mb->head - 1;
43 if (maxlen >= 0 && len > maxlen)
44 len = maxlen;
45
46
47 if (update)
48 mb->head += len;
49
50
51
52
53
54 if ((maxlen < 0 || len < maxlen) && mb->tail != mb->start) {
55 len++;
56 if (update)
57 mb->head = mb->start;
58 }
59
60
61 } else {
62
63 len = mb->tail - mb->head - 1;
64 if (maxlen >= 0 && len > maxlen)
65 len = maxlen;
66
67
68 if (update)
69 mb->head += len;
70 }
71
72
73 return len;
74}
75
76int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data)
77{
78 char **datap;
79 int offset;
80 int size;
81
82 size = membuff_putrawflex(mb, maxlen, update, &datap, &offset);
83 *data = *datap + offset;
84
85 return size;
86}
87
88bool membuff_putbyte(struct membuff *mb, int ch)
89{
90 char *data;
91
92 if (membuff_putraw(mb, 1, true, &data) != 1)
93 return false;
94 *data = ch;
95
96 return true;
97}
98
99int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data)
100{
101 int len;
102
103
104 len = 0;
105
106
107
108
109
110 if (mb->head > mb->tail) {
111
112 *data = mb->tail;
113 len = mb->head - mb->tail;
114
115
116 if (maxlen >= 0 && len > maxlen)
117 len = maxlen;
118
119
120 if (update)
121 mb->tail += len;
122 }
123
124
125
126
127
128
129 else if (mb->head < mb->tail) {
130
131 *data = mb->tail;
132 len = mb->end - mb->tail;
133 if (maxlen >= 0 && len > maxlen)
134 len = maxlen;
135 if (update) {
136 mb->tail += len;
137 if (mb->tail == mb->end)
138 mb->tail = mb->start;
139 }
140 }
141
142 debug("getraw: maxlen=%d, update=%d, head=%d, tail=%d, data=%d, len=%d",
143 maxlen, update, (int)(mb->head - mb->start),
144 (int)(mb->tail - mb->start), (int)(*data - mb->start), len);
145
146
147 return len;
148}
149
150int membuff_getbyte(struct membuff *mb)
151{
152 char *data = 0;
153
154 return membuff_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data;
155}
156
157int membuff_peekbyte(struct membuff *mb)
158{
159 char *data = 0;
160
161 return membuff_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data;
162}
163
164int membuff_get(struct membuff *mb, char *buff, int maxlen)
165{
166 char *data = 0, *buffptr = buff;
167 int len = 1, i;
168
169
170
171
172
173 for (i = 0; len && i < 2; i++) {
174
175 len = membuff_getraw(mb, maxlen, true, &data);
176
177
178 memcpy(buffptr, data, len);
179 buffptr += len;
180 maxlen -= len;
181 }
182
183
184 return buffptr - buff;
185}
186
187int membuff_put(struct membuff *mb, const char *buff, int length)
188{
189 char *data;
190 int towrite, i, written;
191
192 for (i = written = 0; i < 2; i++) {
193
194 towrite = membuff_putraw(mb, length, true, &data);
195
196
197 memcpy(data, buff, towrite);
198 written += towrite;
199 buff += towrite;
200 length -= towrite;
201 }
202
203
204 return written;
205}
206
207bool membuff_isempty(struct membuff *mb)
208{
209 return mb->head == mb->tail;
210}
211
212int membuff_avail(struct membuff *mb)
213{
214 struct membuff copy;
215 int i, avail;
216 char *data = 0;
217
218
219 copy = *mb;
220
221
222 for (i = avail = 0; i < 2; i++)
223 avail += membuff_getraw(©, -1, true, &data);
224
225
226 return avail;
227}
228
229int membuff_size(struct membuff *mb)
230{
231 return mb->end - mb->start;
232}
233
234bool membuff_makecontig(struct membuff *mb)
235{
236 int topsize, botsize;
237
238 debug("makecontig: head=%d, tail=%d, size=%d",
239 (int)(mb->head - mb->start), (int)(mb->tail - mb->start),
240 (int)(mb->end - mb->start));
241
242
243
244
245
246 if (mb->tail > mb->head) {
247
248
249
250
251
252 topsize = mb->end - mb->tail;
253 botsize = mb->head - mb->start;
254
255
256
257
258
259 if (mb->head + topsize >= mb->tail)
260 return false;
261 memmove(mb->start + topsize, mb->start, botsize);
262 debug(" - memmove(%d, %d, %d)", topsize, 0, botsize);
263
264
265 } else {
266 topsize = mb->head - mb->tail;
267 botsize = 0;
268 }
269
270
271 memcpy(mb->start, mb->tail, topsize);
272 debug(" - memcpy(%d, %d, %d)", 0, (int)(mb->tail - mb->start), topsize);
273
274
275 mb->tail = mb->start;
276 mb->head = mb->start + topsize + botsize;
277
278 debug(" - head=%d, tail=%d", (int)(mb->head - mb->start),
279 (int)(mb->tail - mb->start));
280
281
282 return true;
283}
284
285int membuff_free(struct membuff *mb)
286{
287 return mb->end == mb->start ? 0 :
288 (mb->end - mb->start) - 1 - membuff_avail(mb);
289}
290
291int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch)
292{
293 int len;
294 char *s, *end;
295 bool ok = false;
296 char *orig = str;
297
298 end = mb->head >= mb->tail ? mb->head : mb->end;
299 for (len = 0, s = mb->tail; s < end && len < maxlen - 1; str++) {
300 *str = *s++;
301 len++;
302 if (*str == '\n' || *str < minch) {
303 ok = true;
304 break;
305 }
306 if (s == end && mb->tail > mb->head) {
307 s = mb->start;
308 end = mb->head;
309 }
310 }
311
312
313 if (!ok) {
314 if (maxlen)
315 *orig = '\0';
316 return 0;
317 }
318
319
320 *str = '\0';
321 mb->tail = s == mb->end ? mb->start : s;
322
323 return len;
324}
325
326int membuff_extend_by(struct membuff *mb, int by, int max)
327{
328 int oldhead, oldtail;
329 int size, orig;
330 char *ptr;
331
332
333 assert(by >= 0);
334 for (orig = mb->end - mb->start, size = orig; size < orig + by;)
335 size *= 2;
336 if (max != -1)
337 size = min(size, max);
338 by = size - orig;
339
340
341 if (by <= 0)
342 return -E2BIG;
343
344 oldhead = mb->head - mb->start;
345 oldtail = mb->tail - mb->start;
346 ptr = realloc(mb->start, size);
347 if (!ptr)
348 return -ENOMEM;
349 mb->start = ptr;
350 mb->head = mb->start + oldhead;
351 mb->tail = mb->start + oldtail;
352
353 if (mb->head < mb->tail) {
354 memmove(mb->tail + by, mb->tail, orig - oldtail);
355 mb->tail += by;
356 }
357 mb->end = mb->start + size;
358
359 return 0;
360}
361
362void membuff_init(struct membuff *mb, char *buff, int size)
363{
364 mb->start = buff;
365 mb->end = mb->start + size;
366 membuff_purge(mb);
367}
368
369int membuff_new(struct membuff *mb, int size)
370{
371 mb->start = malloc(size);
372 if (!mb->start)
373 return -ENOMEM;
374
375 membuff_init(mb, mb->start, size);
376 return 0;
377}
378
379void membuff_uninit(struct membuff *mb)
380{
381 mb->end = NULL;
382 mb->start = NULL;
383 membuff_purge(mb);
384}
385
386void membuff_dispose(struct membuff *mb)
387{
388 free(&mb->start);
389 membuff_uninit(mb);
390}
391