GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libevent/buffer.c Lines: 156 209 74.6 %
Date: 2017-11-07 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
976
	buffer = calloc(1, sizeof(struct evbuffer));
52
53
488
	return (buffer);
54
}
55
56
void
57
evbuffer_free(struct evbuffer *buffer)
58
{
59
942
	free(buffer->orig_buffer);
60
471
	free(buffer);
61
471
}
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
280
	if (outbuf->off == 0) {
83
		struct evbuffer tmp;
84
70
		size_t oldoff = inbuf->off;
85
86
		/* Swap them directly */
87
70
		SWAP(&tmp, outbuf);
88
70
		SWAP(outbuf, inbuf);
89
70
		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

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

140
		if (oldoff && outbuf->cb != NULL)
99
			(*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
100
101
		return (0);
102
	}
103
104
70
	res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
105
70
	if (res == 0) {
106
		/* We drain the input buffer on success */
107
70
		evbuffer_drain(inbuf, inbuf->off);
108
70
	}
109
110
70
	return (res);
111
140
}
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
70
	size_t oldoff = buf->off;
119
	int sz;
120
35
	va_list aq;
121
122
	/* make sure that at least some space is available */
123
35
	if (evbuffer_expand(buf, 64) < 0)
124
		return (-1);
125
	for (;;) {
126
35
		size_t used = buf->misalign + buf->off;
127
35
		buffer = (char *)buf->buffer + buf->off;
128
		assert(buf->totallen >= used);
129
35
		space = buf->totallen - used;
130
131
35
		va_copy(aq, ap);
132
133
35
		sz = vsnprintf(buffer, space, fmt, aq);
134
135
35
		va_end(aq);
136
137
35
		if (sz < 0)
138
			return (-1);
139
35
		if ((size_t)sz < space) {
140
35
			buf->off += sz;
141
35
			if (buf->cb != NULL)
142
				(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
143
35
			return (sz);
144
		}
145
		if (evbuffer_expand(buf, sz + 1) == -1)
146
			return (-1);
147
148
	}
149
	/* NOTREACHED */
150
35
}
151
152
int
153
evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
154
{
155
	int res = -1;
156
70
	va_list ap;
157
158
35
	va_start(ap, fmt);
159
35
	res = evbuffer_add_vprintf(buf, fmt, ap);
160
35
	va_end(ap);
161
162
35
	return (res);
163
35
}
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
1820
	u_char *data = EVBUFFER_DATA(buffer);
232
	u_char *start_of_eol, *end_of_eol;
233
910
	size_t len = EVBUFFER_LENGTH(buffer);
234
	char *line;
235
	unsigned int i, n_to_copy, n_to_drain;
236
237
910
	if (n_read_out)
238
910
		*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

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

2695
			if (data[i] == '\r' || data[i] == '\n')
246
				break;
247
		}
248
140
		if (i == len)
249
35
			return (NULL);
250
105
		start_of_eol = data+i;
251
105
		++i;
252
630
		for ( ; i < len; i++) {
253

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

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

700
			if (cp < data+len-1 && *(cp+1) == '\n')
272
				break;
273
105
			if (++cp >= data+len) {
274
				cp = NULL;
275
70
				break;
276
			}
277
		}
278
385
		if (!cp)
279
105
			return (NULL);
280
		start_of_eol = cp;
281
280
		end_of_eol = cp+2;
282
280
		break;
283
	}
284
	case EVBUFFER_EOL_LF:
285
210
		start_of_eol = memchr(data, '\n', len);
286
210
		if (!start_of_eol)
287
35
			return (NULL);
288
175
		end_of_eol = start_of_eol + 1;
289
175
		break;
290
	default:
291
		return (NULL);
292
	}
293
294
700
	n_to_copy = start_of_eol - data;
295
700
	n_to_drain = end_of_eol - data;
296
297
700
	if ((line = malloc(n_to_copy+1)) == NULL) {
298
		event_warn("%s: out of memory", __func__);
299
		return (NULL);
300
	}
301
302
700
	memcpy(line, data, n_to_copy);
303
700
	line[n_to_copy] = '\0';
304
305
700
	evbuffer_drain(buffer, n_to_drain);
306
700
	if (n_read_out)
307
700
		*n_read_out = (size_t)n_to_copy;
308
309
700
	return (line);
310
910
}
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
228334
	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
114167
	if (buf->totallen - used >= datlen)
333
113715
		return (0);
334
	/* If we would need to overflow to fit this much data, we can't
335
	 * do anything. */
336
452
	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
452
	if (buf->totallen - buf->off >= datlen) {
344
		evbuffer_align(buf);
345
	} else {
346
		void *newbuf;
347
		size_t length = buf->totallen;
348
452
		size_t need = buf->off + datlen;
349
350
452
		if (length < 256)
351
348
			length = 256;
352
452
		if (need < SIZE_MAX / 2) {
353
2158
			while (length < need) {
354
853
				length <<= 1;
355
			}
356
		} else {
357
			length = need;
358
		}
359
360
452
		if (buf->orig_buffer != buf->buffer)
361
			evbuffer_align(buf);
362
904
		if ((newbuf = recallocarray(buf->buffer, buf->totallen,
363
452
		    length, 1)) == NULL)
364
			return (-1);
365
366
452
		buf->orig_buffer = buf->buffer = newbuf;
367
452
		buf->totallen = length;
368
452
	}
369
370
452
	return (0);
371
114167
}
372
373
int
374
evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen)
375
{
376
6144
	size_t used = buf->misalign + buf->off;
377
	size_t oldoff = buf->off;
378
379
3072
	if (buf->totallen - used < datlen) {
380
277
		if (evbuffer_expand(buf, datlen) == -1)
381
			return (-1);
382
	}
383
384
3072
	memcpy(buf->buffer + buf->off, data, datlen);
385
3072
	buf->off += datlen;
386
387

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

243138
	if (buf->off != oldoff && buf->cb != NULL)
413
113750
		(*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
414
415
122299
}
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
227710
	size_t oldoff = buf->off;
428
113855
	int n = EVBUFFER_MAX_READ;
429
430
113855
	if (ioctl(fd, FIONREAD, &n) == -1 || n <= 0) {
431
		n = EVBUFFER_MAX_READ;
432

113855
	} 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

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

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

26985
	while (search < end &&
492
8995
	    (p = memchr(search, *what, end - search)) != NULL) {
493
8995
		if (p + len > end)
494
			break;
495
8925
		if (memcmp(p, what, len) == 0)
496
35
			return (p);
497
8890
		search = p + 1;
498
	}
499
500
70
	return (NULL);
501
105
}
502
503
void evbuffer_setcb(struct evbuffer *buffer,
504
    void (*cb)(struct evbuffer *, size_t, size_t, void *),
505
    void *cbarg)
506
{
507
455070
	buffer->cb = cb;
508
227535
	buffer->cbarg = cbarg;
509
227535
}