summaryrefslogtreecommitdiff
path: root/lib/membuff.c
blob: 45dae2d576fdcfb8fdc27e2eb48080d1c5e1613e (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2015 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 *
 * Copyright (c) 1992 Simon Glass
 */

#include <common.h>
#include <errno.h>
#include <malloc.h>
#include "membuff.h"

void membuff_purge(struct membuff *mb)
{
	/* set mb->head and mb->tail so the buffers look empty */
	mb->head = mb->start;
	mb->tail = mb->start;
}

static int membuff_putrawflex(struct membuff *mb, int maxlen, bool update,
			      char ***data, int *offsetp)
{
	int len;

	/* always write to 'mb->head' */
	assert(data && offsetp);
	*data = &mb->start;
	*offsetp = mb->head - mb->start;

	/* if there is no buffer, we can do nothing */
	if (!mb->start)
		return 0;

	/*
	 * if head is ahead of tail, we can write from head until the end of
	 * the buffer
	 */
	if (mb->head >= mb->tail) {
		/* work out how many bytes can fit here */
		len = mb->end - mb->head - 1;
		if (maxlen >= 0 && len > maxlen)
			len = maxlen;

		/* update the head pointer to mark these bytes as written */
		if (update)
			mb->head += len;

		/*
		 * if the tail isn't at start of the buffer, then we can
		 * write one more byte right at the end
		 */
		if ((maxlen < 0 || len < maxlen) && mb->tail != mb->start) {
			len++;
			if (update)
				mb->head = mb->start;
		}

	/* otherwise now we can write until head almost reaches tail */
	} else {
		/* work out how many bytes can fit here */
		len = mb->tail - mb->head - 1;
		if (maxlen >= 0 && len > maxlen)
			len = maxlen;

		/* update the head pointer to mark these bytes as written */
		if (update)
			mb->head += len;
	}

	/* return the number of bytes which can be/must be written */
	return len;
}

int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data)
{
	char **datap;
	int offset;
	int size;

	size = membuff_putrawflex(mb, maxlen, update, &datap, &offset);
	*data = *datap + offset;

	return size;
}

bool membuff_putbyte(struct membuff *mb, int ch)
{
	char *data;

	if (membuff_putraw(mb, 1, true, &data) != 1)
		return false;
	*data = ch;

	return true;
}

int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data)
{
	int len;

	/* assume for now there is no data to get */
	len = 0;

	/*
	 * in this case head is ahead of tail, so we must return data between
	 *'tail' and 'head'
	 */
	if (mb->head > mb->tail) {
		/* work out the amount of data */
		*data = mb->tail;
		len = mb->head - mb->tail;

		/* check it isn't too much */
		if (maxlen >= 0 && len > maxlen)
			len = maxlen;

		/* & mark it as read from the buffer */
		if (update)
			mb->tail += len;
	}

	/*
	 * if head is before tail, then we have data between 'tail' and 'end'
	 * and some more data between 'start' and 'head'(which we can't
	 * return this time
	 */
	else if (mb->head < mb->tail) {
		/* work out the amount of data */
		*data = mb->tail;
		len = mb->end - mb->tail;
		if (maxlen >= 0 && len > maxlen)
			len = maxlen;
		if (update) {
			mb->tail += len;
			if (mb->tail == mb->end)
				mb->tail = mb->start;
		}
	}

	debug("getraw: maxlen=%d, update=%d, head=%d, tail=%d, data=%d, len=%d",
	      maxlen, update, (int)(mb->head - mb->start),
	      (int)(mb->tail - mb->start), (int)(*data - mb->start), len);

	/* return the number of bytes we found */
	return len;
}

int membuff_getbyte(struct membuff *mb)
{
	char *data = 0;

	return membuff_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data;
}

int membuff_peekbyte(struct membuff *mb)
{
	char *data = 0;

	return membuff_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data;
}

int membuff_get(struct membuff *mb, char *buff, int maxlen)
{
	char *data = 0, *buffptr = buff;
	int len = 1, i;

	/*
	 * do this in up to two lots(see GetRaw for why) stopping when there
	 * is no more data
	 */
	for (i = 0; len && i < 2; i++) {
		/* get a pointer to the data available */
		len = membuff_getraw(mb, maxlen, true, &data);

		/* copy it into the buffer */
		memcpy(buffptr, data, len);
		buffptr += len;
		maxlen -= len;
	}

	/* return the number of bytes read */
	return buffptr - buff;
}

int membuff_put(struct membuff *mb, const char *buff, int length)
{
	char *data;
	int towrite, i, written;

	for (i = written = 0; i < 2; i++) {
		/* ask where some data can be written */
		towrite = membuff_putraw(mb, length, true, &data);

		/* and write it, updating the bytes length */
		memcpy(data, buff, towrite);
		written += towrite;
		buff += towrite;
		length -= towrite;
	}

	/* return the number of bytes written */
	return written;
}

bool membuff_isempty(struct membuff *mb)
{
	return mb->head == mb->tail;
}

int membuff_avail(struct membuff *mb)
{
	struct membuff copy;
	int i, avail;
	char *data = 0;

	/* make a copy of this buffer's control data */
	copy = *mb;

	/* now read everything out of the copied buffer */
	for (i = avail = 0; i < 2; i++)
		avail += membuff_getraw(&copy, -1, true, &data);

	/* and return how much we read */
	return avail;
}

int membuff_size(struct membuff *mb)
{
	return mb->end - mb->start;
}

bool membuff_makecontig(struct membuff *mb)
{
	int topsize, botsize;

	debug("makecontig: head=%d, tail=%d, size=%d",
	      (int)(mb->head - mb->start), (int)(mb->tail - mb->start),
	      (int)(mb->end - mb->start));

	/*
	 * first we move anything at the start of the buffer into the correct
	 * place some way along
	 */
	if (mb->tail > mb->head) {
		/*
		 * the data is split into two parts, from 0 to ->head and
		 * from ->tail to ->end. We move the stuff from 0 to ->head
		 * up to make space for the other data before it
		 */
		topsize = mb->end - mb->tail;
		botsize = mb->head - mb->start;

		/*
		 * must move data at bottom up by 'topsize' bytes - check if
		 * there's room
		 */
		if (mb->head + topsize >= mb->tail)
			return false;
		memmove(mb->start + topsize, mb->start, botsize);
		debug("	- memmove(%d, %d, %d)", topsize, 0, botsize);

	/* nothing at the start, so skip that step */
	} else {
		topsize = mb->head - mb->tail;
		botsize = 0;
	}

	/* now move data at top down to the bottom */
	memcpy(mb->start, mb->tail, topsize);
	debug("	- memcpy(%d, %d, %d)", 0, (int)(mb->tail - mb->start), topsize);

	/* adjust pointers */
	mb->tail = mb->start;
	mb->head = mb->start + topsize + botsize;

	debug("	- head=%d, tail=%d", (int)(mb->head - mb->start),
	      (int)(mb->tail - mb->start));

	/* all ok */
	return true;
}

int membuff_free(struct membuff *mb)
{
	return mb->end == mb->start ? 0 :
			(mb->end - mb->start) - 1 - membuff_avail(mb);
}

int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch)
{
	int len;  /* number of bytes read (!= string length) */
	char *s, *end;
	bool ok = false;
	char *orig = str;

	end = mb->head >= mb->tail ? mb->head : mb->end;
	for (len = 0, s = mb->tail; s < end && len < maxlen - 1; str++) {
		*str = *s++;
		len++;
		if (*str == '\n' || *str < minch) {
			ok = true;
			break;
		}
		if (s == end && mb->tail > mb->head) {
			s = mb->start;
			end = mb->head;
		}
	}

	/* couldn't get the whole string */
	if (!ok) {
		if (maxlen)
			*orig = '\0';
		return 0;
	}

	/* terminate the string, update the membuff and return success */
	*str = '\0';
	mb->tail = s == mb->end ? mb->start : s;

	return len;
}

int membuff_extend_by(struct membuff *mb, int by, int max)
{
	int oldhead, oldtail;
	int size, orig;
	char *ptr;

	/* double the buffer size until it is big enough */
	assert(by >= 0);
	for (orig = mb->end - mb->start, size = orig; size < orig + by;)
		size *= 2;
	if (max != -1)
		size = min(size, max);
	by = size - orig;

	/* if we're already at maximum, give up */
	if (by <= 0)
		return -E2BIG;

	oldhead = mb->head - mb->start;
	oldtail = mb->tail - mb->start;
	ptr = realloc(mb->start, size);
	if (!ptr)
		return -ENOMEM;
	mb->start = ptr;
	mb->head = mb->start + oldhead;
	mb->tail = mb->start + oldtail;

	if (mb->head < mb->tail) {
		memmove(mb->tail + by, mb->tail, orig - oldtail);
		mb->tail += by;
	}
	mb->end = mb->start + size;

	return 0;
}

void membuff_init(struct membuff *mb, char *buff, int size)
{
	mb->start = buff;
	mb->end = mb->start + size;
	membuff_purge(mb);
}

int membuff_new(struct membuff *mb, int size)
{
	mb->start = malloc(size);
	if (!mb->start)
		return -ENOMEM;

	membuff_init(mb, mb->start, size);
	return 0;
}

void membuff_uninit(struct membuff *mb)
{
	mb->end = NULL;
	mb->start = NULL;
	membuff_purge(mb);
}

void membuff_dispose(struct membuff *mb)
{
	free(&mb->start);
	membuff_uninit(mb);
}