GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libevent/buffer.c Lines: 157 210 74.8 %
Date: 2017-11-13 Branches: 95 157 60.5 %

Line Branch Exec Source
1
/*	$OpenBSD: buffer.c,v 1.31 2017/03/18 01:48:43 deraadt Exp $	*/
2
3
/*
4
 * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. The name of the author may not be used to endorse or promote products
16
 *    derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#include <sys/types.h>
31
#include <sys/time.h>
32
#include <sys/ioctl.h>
33
34
#include <assert.h>
35
#include <errno.h>
36
#include <stdint.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <stdarg.h>
41
#include <unistd.h>
42
43
#include "event.h"
44
#include "log.h"
45
46
struct evbuffer *
47
evbuffer_new(void)
48
{
49
	struct evbuffer *buffer;
50
51
504
	buffer = calloc(1, sizeof(struct evbuffer));
52
53
252
	return (buffer);
54
}
55
56
void
57
evbuffer_free(struct evbuffer *buffer)
58
{
59
486
	free(buffer->orig_buffer);
60
243
	free(buffer);
61
243
}
62
63
/*
64
 * This is a destructive add.  The data from one buffer moves into
65
 * the other buffer.
66
 */
67
68
#define SWAP(x,y) do { \
69
	(x)->buffer = (y)->buffer; \
70
	(x)->orig_buffer = (y)->orig_buffer; \
71
	(x)->misalign = (y)->misalign; \
72
	(x)->totallen = (y)->totallen; \
73
	(x)->off = (y)->off; \
74
} while (0)
75
76
int
77
evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
78
{
79
	int res;
80
81
	/* Short cut for better performance */
82
144
	if (outbuf->off == 0) {
83
		struct evbuffer tmp;
84
36
		size_t oldoff = inbuf->off;
85
86
		/* Swap them directly */
87
36
		SWAP(&tmp, outbuf);
88
36
		SWAP(outbuf, inbuf);
89
36
		SWAP(inbuf, &tmp);
90
91
		/*
92
		 * Optimization comes with a price; we need to notify the
93
		 * buffer if necessary of the changes. oldoff is the amount
94
		 * of data that we transferred from inbuf to outbuf
95
		 */
96

72
		if (inbuf->off != oldoff && inbuf->cb != NULL)
97
			(*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg);
98

72
		if (oldoff && outbuf->cb != NULL)
99
			(*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
100
101
		return (0);
102
	}
103
104
36
	res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
105
36
	if (res == 0) {
106
		/* We drain the input buffer on success */
107
36
		evbuffer_drain(inbuf, inbuf->off);
108
36
	}
109
110
36
	return (res);
111
72
}
112
113
int
114
evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
115
{
116
	char *buffer;
117
	size_t space;
118
36
	size_t oldoff = buf->off;
119
	int sz;
120
18
	va_list aq;
121
122
	/* make sure that at least some space is available */
123
18
	if (evbuffer_expand(buf, 64) < 0)
124
		return (-1);
125
18
	for (;;) {
126
18
		size_t used = buf->misalign + buf->off;
127
18
		buffer = (char *)buf->buffer + buf->off;
128
		assert(buf->totallen >= used);
129
18
		space = buf->totallen - used;
130
131
18
		va_copy(aq, ap);
132
133
18
		sz = vsnprintf(buffer, space, fmt, aq);
134
135
18
		va_end(aq);
136
137
18
		if (sz < 0)
138
			return (-1);
139
18
		if ((size_t)sz < space) {
140
18
			buf->off += sz;
141
18
			if (buf->cb != NULL)
142
				(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
143
18
			return (sz);
144
		}
145
		if (evbuffer_expand(buf, sz + 1) == -1)
146
			return (-1);
147
148
	}
149
	/* NOTREACHED */
150
18
}
151
152
int
153
evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
154
{
155
	int res = -1;
156
36
	va_list ap;
157
158
18
	va_start(ap, fmt);
159
18
	res = evbuffer_add_vprintf(buf, fmt, ap);
160
18
	va_end(ap);
161
162
18
	return (res);
163
18
}
164
165
/* Reads data from an event buffer and drains the bytes read */
166
167
int
168
evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen)
169
{
170
	size_t nread = datlen;
171
	if (nread >= buf->off)
172
		nread = buf->off;
173
174
	memcpy(data, buf->buffer, nread);
175
	evbuffer_drain(buf, nread);
176
177
	return (nread);
178
}
179
180
/*
181
 * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
182
 * The returned buffer needs to be freed by the called.
183
 */
184
185
char *
186
evbuffer_readline(struct evbuffer *buffer)
187
{
188
	u_char *data = EVBUFFER_DATA(buffer);
189
	size_t len = EVBUFFER_LENGTH(buffer);
190
	char *line;
191
	unsigned int i;
192
193
	for (i = 0; i < len; i++) {
194
		if (data[i] == '\r' || data[i] == '\n')
195
			break;
196
	}
197
198
	if (i == len)
199
		return (NULL);
200
201
	if ((line = malloc(i + 1)) == NULL) {
202
		event_warn("%s: out of memory", __func__);
203
		return (NULL);
204
	}
205
206
	memcpy(line, data, i);
207
	line[i] = '\0';
208
209
	/*
210
	 * Some protocols terminate a line with '\r\n', so check for
211
	 * that, too.
212
	 */
213
	if ( i < len - 1 ) {
214
		char fch = data[i], sch = data[i+1];
215
216
		/* Drain one more character if needed */
217
		if ( (sch == '\r' || sch == '\n') && sch != fch )
218
			i += 1;
219
	}
220
221
	evbuffer_drain(buffer, i + 1);
222
223
	return (line);
224
}
225
226
227
char *
228
evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
229
		enum evbuffer_eol_style eol_style)
230
{
231
936
	u_char *data = EVBUFFER_DATA(buffer);
232
	u_char *start_of_eol, *end_of_eol;
233
468
	size_t len = EVBUFFER_LENGTH(buffer);
234
	char *line;
235
	unsigned int i, n_to_copy, n_to_drain;
236
237
468
	if (n_read_out)
238
468
		*n_read_out = 0;
239
240
	/* depending on eol_style, set start_of_eol to the first character
241
	 * in the newline, and end_of_eol to one after the last character. */
242

468
	switch (eol_style) {
243
	case EVBUFFER_EOL_ANY:
244
1440
		for (i = 0; i < len; i++) {
245

1386
			if (data[i] == '\r' || data[i] == '\n')
246
				break;
247
		}
248
72
		if (i == len)
249
18
			return (NULL);
250
54
		start_of_eol = data+i;
251
54
		++i;
252
324
		for ( ; i < len; i++) {
253

252
			if (data[i] != '\r' && data[i] != '\n')
254
				break;
255
		}
256
54
		end_of_eol = data+i;
257
54
		break;
258
	case EVBUFFER_EOL_CRLF:
259
90
		end_of_eol = memchr(data, '\n', len);
260
90
		if (!end_of_eol)
261
18
			return (NULL);
262

126
		if (end_of_eol > data && *(end_of_eol-1) == '\r')
263
18
			start_of_eol = end_of_eol - 1;
264
		else
265
			start_of_eol = end_of_eol;
266
72
		end_of_eol++; /*point to one after the LF. */
267
72
		break;
268
	case EVBUFFER_EOL_CRLF_STRICT: {
269
		u_char *cp = data;
270
432
		while ((cp = memchr(cp, '\r', len-(cp-data)))) {
271

360
			if (cp < data+len-1 && *(cp+1) == '\n')
272
				break;
273
54
			if (++cp >= data+len) {
274
				cp = NULL;
275
36
				break;
276
			}
277
		}
278
198
		if (!cp)
279
54
			return (NULL);
280
		start_of_eol = cp;
281
144
		end_of_eol = cp+2;
282
144
		break;
283
	}
284
	case EVBUFFER_EOL_LF:
285
108
		start_of_eol = memchr(data, '\n', len);
286
108
		if (!start_of_eol)
287
18
			return (NULL);
288
90
		end_of_eol = start_of_eol + 1;
289
90
		break;
290
	default:
291
		return (NULL);
292
	}
293
294
360
	n_to_copy = start_of_eol - data;
295
360
	n_to_drain = end_of_eol - data;
296
297
360
	if ((line = malloc(n_to_copy+1)) == NULL) {
298
		event_warn("%s: out of memory", __func__);
299
		return (NULL);
300
	}
301
302
360
	memcpy(line, data, n_to_copy);
303
360
	line[n_to_copy] = '\0';
304
305
360
	evbuffer_drain(buffer, n_to_drain);
306
360
	if (n_read_out)
307
360
		*n_read_out = (size_t)n_to_copy;
308
309
360
	return (line);
310
468
}
311
312
/* Adds data to an event buffer */
313
314
static void
315
evbuffer_align(struct evbuffer *buf)
316
{
317
	memmove(buf->orig_buffer, buf->buffer, buf->off);
318
	buf->buffer = buf->orig_buffer;
319
	buf->misalign = 0;
320
}
321
322
/* Expands the available space in the event buffer to at least datlen */
323
324
int
325
evbuffer_expand(struct evbuffer *buf, size_t datlen)
326
{
327
117440
	size_t used = buf->misalign + buf->off;
328
329
	assert(buf->totallen >= used);
330
331
	/* If we can fit all the data, then we don't have to do anything */
332
58720
	if (buf->totallen - used >= datlen)
333
58482
		return (0);
334
	/* If we would need to overflow to fit this much data, we can't
335
	 * do anything. */
336
238
	if (datlen > SIZE_MAX - buf->off)
337
		return (-1);
338
339
	/*
340
	 * If the misalignment fulfills our data needs, we just force an
341
	 * alignment to happen.  Afterwards, we have enough space.
342
	 */
343
238
	if (buf->totallen - buf->off >= datlen) {
344
		evbuffer_align(buf);
345
	} else {
346
		void *newbuf;
347
		size_t length = buf->totallen;
348
238
		size_t need = buf->off + datlen;
349
350
238
		if (length < 256)
351
180
			length = 256;
352
238
		if (need < SIZE_MAX / 2) {
353
1358
			while (length < need) {
354
441
				length <<= 1;
355
			}
356
		} else {
357
			length = need;
358
		}
359
360
238
		if (buf->orig_buffer != buf->buffer)
361
			evbuffer_align(buf);
362
476
		if ((newbuf = recallocarray(buf->buffer, buf->totallen,
363
238
		    length, 1)) == NULL)
364
			return (-1);
365
366
238
		buf->orig_buffer = buf->buffer = newbuf;
367
238
		buf->totallen = length;
368
238
	}
369
370
238
	return (0);
371
58720
}
372
373
int
374
evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen)
375
{
376
3210
	size_t used = buf->misalign + buf->off;
377
	size_t oldoff = buf->off;
378
379
1605
	if (buf->totallen - used < datlen) {
380
148
		if (evbuffer_expand(buf, datlen) == -1)
381
			return (-1);
382
	}
383
384
1605
	memcpy(buf->buffer + buf->off, data, datlen);
385
1605
	buf->off += datlen;
386
387

3209
	if (datlen && buf->cb != NULL)
388
		(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
389
390
1605
	return (0);
391
1605
}
392
393
void
394
evbuffer_drain(struct evbuffer *buf, size_t len)
395
{
396
125972
	size_t oldoff = buf->off;
397
398
62986
	if (len >= buf->off) {
399
60579
		buf->off = 0;
400
60579
		buf->buffer = buf->orig_buffer;
401
60579
		buf->misalign = 0;
402
60579
		goto done;
403
	}
404
405
2407
	buf->buffer += len;
406
2407
	buf->misalign += len;
407
408
2407
	buf->off -= len;
409
410
 done:
411
	/* Tell someone about changes in this buffer */
412

125199
	if (buf->off != oldoff && buf->cb != NULL)
413
58500
		(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
414
415
62986
}
416
417
/*
418
 * Reads data from a file descriptor into a buffer.
419
 */
420
421
#define EVBUFFER_MAX_READ	4096
422
423
int
424
evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
425
{
426
	u_char *p;
427
117108
	size_t oldoff = buf->off;
428
58554
	int n = EVBUFFER_MAX_READ;
429
430
58554
	if (ioctl(fd, FIONREAD, &n) == -1 || n <= 0) {
431
		n = EVBUFFER_MAX_READ;
432

58554
	} else if (n > EVBUFFER_MAX_READ && n > howmuch) {
433
		/*
434
		 * It's possible that a lot of data is available for
435
		 * reading.  We do not want to exhaust resources
436
		 * before the reader has a chance to do something
437
		 * about it.  If the reader does not tell us how much
438
		 * data we should read, we artifically limit it.
439
		 */
440
		if ((size_t)n > buf->totallen << 2)
441
			n = buf->totallen << 2;
442
		if (n < EVBUFFER_MAX_READ)
443
			n = EVBUFFER_MAX_READ;
444
	}
445

117054
	if (howmuch < 0 || howmuch > n)
446
54
		howmuch = n;
447
448
	/* If we don't have FIONREAD, we might waste some space here */
449
58554
	if (evbuffer_expand(buf, howmuch) == -1)
450
		return (-1);
451
452
	/* We can append new data at this point */
453
58554
	p = buf->buffer + buf->off;
454
455
58554
	n = read(fd, p, howmuch);
456
58554
	if (n == -1)
457
		return (-1);
458
58554
	if (n == 0)
459
		return (0);
460
461
58554
	buf->off += n;
462
463
	/* Tell someone about changes in this buffer */
464

117108
	if (buf->off != oldoff && buf->cb != NULL)
465
		(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
466
467
58554
	return (n);
468
58554
}
469
470
int
471
evbuffer_write(struct evbuffer *buffer, int fd)
472
{
473
	int n;
474
475
1224
	n = write(fd, buffer->buffer, buffer->off);
476
612
	if (n == -1)
477
		return (-1);
478
612
	if (n == 0)
479
		return (0);
480
612
	evbuffer_drain(buffer, n);
481
482
612
	return (n);
483
612
}
484
485
u_char *
486
evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len)
487
{
488
108
	u_char *search = buffer->buffer, *end = search + buffer->off;
489
	u_char *p;
490
491

13878
	while (search < end &&
492
4626
	    (p = memchr(search, *what, end - search)) != NULL) {
493
4626
		if (p + len > end)
494
			break;
495
4590
		if (memcmp(p, what, len) == 0)
496
18
			return (p);
497
4572
		search = p + 1;
498
	}
499
500
36
	return (NULL);
501
54
}
502
503
void evbuffer_setcb(struct evbuffer *buffer,
504
    void (*cb)(struct evbuffer *, size_t, size_t, void *),
505
    void *cbarg)
506
{
507
234036
	buffer->cb = cb;
508
117018
	buffer->cbarg = cbarg;
509
117018
}