xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/buffer.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: buffer.c,v 1.6 2020/05/25 20:47:33 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
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 "event2/event-config.h"
31 #include "evconfig-private.h"
32 
33 #ifdef _WIN32
34 #include <winsock2.h>
35 #include <windows.h>
36 #include <io.h>
37 #endif
38 
39 #ifdef EVENT__HAVE_VASPRINTF
40 /* If we have vasprintf, we need to define _GNU_SOURCE before we include
41  * stdio.h.  This comes from evconfig-private.h.
42  */
43 #endif
44 
45 #include <sys/types.h>
46 
47 #ifdef EVENT__HAVE_SYS_TIME_H
48 #include <sys/time.h>
49 #endif
50 
51 #ifdef EVENT__HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #endif
54 
55 #ifdef EVENT__HAVE_SYS_UIO_H
56 #include <sys/uio.h>
57 #endif
58 
59 #ifdef EVENT__HAVE_SYS_IOCTL_H
60 #include <sys/ioctl.h>
61 #endif
62 
63 #ifdef EVENT__HAVE_SYS_MMAN_H
64 #include <sys/mman.h>
65 #endif
66 
67 #ifdef EVENT__HAVE_SYS_SENDFILE_H
68 #include <sys/sendfile.h>
69 #endif
70 #ifdef EVENT__HAVE_SYS_STAT_H
71 #include <sys/stat.h>
72 #endif
73 
74 
75 #include <errno.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #ifdef EVENT__HAVE_STDARG_H
80 #include <stdarg.h>
81 #endif
82 #ifdef EVENT__HAVE_UNISTD_H
83 #include <unistd.h>
84 #endif
85 #include <limits.h>
86 
87 #include "event2/event.h"
88 #include "event2/buffer.h"
89 #include "event2/buffer_compat.h"
90 #include "event2/bufferevent.h"
91 #include "event2/bufferevent_compat.h"
92 #include "event2/bufferevent_struct.h"
93 #include "event2/thread.h"
94 #include "log-internal.h"
95 #include "mm-internal.h"
96 #include "util-internal.h"
97 #include "evthread-internal.h"
98 #include "evbuffer-internal.h"
99 #include "bufferevent-internal.h"
100 
101 /* some systems do not have MAP_FAILED */
102 #ifndef MAP_FAILED
103 #define MAP_FAILED	((void *)-1)
104 #endif
105 
106 /* send file support */
107 #if defined(EVENT__HAVE_SYS_SENDFILE_H) && defined(EVENT__HAVE_SENDFILE) && defined(__linux__)
108 #define USE_SENDFILE		1
109 #define SENDFILE_IS_LINUX	1
110 #elif defined(EVENT__HAVE_SENDFILE) && defined(__FreeBSD__)
111 #define USE_SENDFILE		1
112 #define SENDFILE_IS_FREEBSD	1
113 #elif defined(EVENT__HAVE_SENDFILE) && defined(__APPLE__)
114 #define USE_SENDFILE		1
115 #define SENDFILE_IS_MACOSX	1
116 #elif defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
117 #define USE_SENDFILE		1
118 #define SENDFILE_IS_SOLARIS	1
119 #endif
120 
121 /* Mask of user-selectable callback flags. */
122 #define EVBUFFER_CB_USER_FLAGS	    0xffff
123 /* Mask of all internal-use-only flags. */
124 #define EVBUFFER_CB_INTERNAL_FLAGS  0xffff0000
125 
126 /* Flag set if the callback is using the cb_obsolete function pointer  */
127 #define EVBUFFER_CB_OBSOLETE	       0x00040000
128 
129 /* evbuffer_chain support */
130 #define CHAIN_SPACE_PTR(ch) ((ch)->buffer + (ch)->misalign + (ch)->off)
131 #define CHAIN_SPACE_LEN(ch) ((ch)->flags & EVBUFFER_IMMUTABLE ? \
132 	    0 : (ch)->buffer_len - ((ch)->misalign + (ch)->off))
133 
134 #define CHAIN_PINNED(ch)  (((ch)->flags & EVBUFFER_MEM_PINNED_ANY) != 0)
135 #define CHAIN_PINNED_R(ch)  (((ch)->flags & EVBUFFER_MEM_PINNED_R) != 0)
136 
137 /* evbuffer_ptr support */
138 #define PTR_NOT_FOUND(ptr) do {			\
139 	(ptr)->pos = -1;					\
140 	(ptr)->internal_.chain = NULL;		\
141 	(ptr)->internal_.pos_in_chain = 0;	\
142 } while (0)
143 
144 static void evbuffer_chain_align(struct evbuffer_chain *chain);
145 static int evbuffer_chain_should_realign(struct evbuffer_chain *chain,
146     size_t datalen);
147 static void evbuffer_deferred_callback(struct event_callback *cb, void *arg);
148 static int evbuffer_ptr_memcmp(const struct evbuffer *buf,
149     const struct evbuffer_ptr *pos, const char *mem, size_t len);
150 static struct evbuffer_chain *evbuffer_expand_singlechain(struct evbuffer *buf,
151     size_t datlen);
152 static int evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
153     size_t howfar);
154 static int evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg);
155 static inline void evbuffer_chain_incref(struct evbuffer_chain *chain);
156 
157 static struct evbuffer_chain *
158 evbuffer_chain_new(size_t size)
159 {
160 	struct evbuffer_chain *chain;
161 	size_t to_alloc;
162 
163 	if (size > EVBUFFER_CHAIN_MAX - EVBUFFER_CHAIN_SIZE)
164 		return (NULL);
165 
166 	size += EVBUFFER_CHAIN_SIZE;
167 
168 	/* get the next largest memory that can hold the buffer */
169 	if (size < EVBUFFER_CHAIN_MAX / 2) {
170 		to_alloc = MIN_BUFFER_SIZE;
171 		while (to_alloc < size) {
172 			to_alloc <<= 1;
173 		}
174 	} else {
175 		to_alloc = size;
176 	}
177 
178 	/* we get everything in one chunk */
179 	if ((chain = mm_malloc(to_alloc)) == NULL)
180 		return (NULL);
181 
182 	memset(chain, 0, EVBUFFER_CHAIN_SIZE);
183 
184 	chain->buffer_len = to_alloc - EVBUFFER_CHAIN_SIZE;
185 
186 	/* this way we can manipulate the buffer to different addresses,
187 	 * which is required for mmap for example.
188 	 */
189 	chain->buffer = EVBUFFER_CHAIN_EXTRA(u_char, chain);
190 
191 	chain->refcnt = 1;
192 
193 	return (chain);
194 }
195 
196 static inline void
197 evbuffer_chain_free(struct evbuffer_chain *chain)
198 {
199 	EVUTIL_ASSERT(chain->refcnt > 0);
200 	if (--chain->refcnt > 0) {
201 		/* chain is still referenced by other chains */
202 		return;
203 	}
204 
205 	if (CHAIN_PINNED(chain)) {
206 		/* will get freed once no longer dangling */
207 		chain->refcnt++;
208 		chain->flags |= EVBUFFER_DANGLING;
209 		return;
210 	}
211 
212 	/* safe to release chain, it's either a referencing
213 	 * chain or all references to it have been freed */
214 	if (chain->flags & EVBUFFER_REFERENCE) {
215 		struct evbuffer_chain_reference *info =
216 		    EVBUFFER_CHAIN_EXTRA(
217 			    struct evbuffer_chain_reference,
218 			    chain);
219 		if (info->cleanupfn)
220 			(*info->cleanupfn)(chain->buffer,
221 			    chain->buffer_len,
222 			    info->extra);
223 	}
224 	if (chain->flags & EVBUFFER_FILESEGMENT) {
225 		struct evbuffer_chain_file_segment *info =
226 		    EVBUFFER_CHAIN_EXTRA(
227 			    struct evbuffer_chain_file_segment,
228 			    chain);
229 		if (info->segment) {
230 #ifdef _WIN32
231 			if (info->segment->is_mapping)
232 				UnmapViewOfFile(chain->buffer);
233 #endif
234 			evbuffer_file_segment_free(info->segment);
235 		}
236 	}
237 	if (chain->flags & EVBUFFER_MULTICAST) {
238 		struct evbuffer_multicast_parent *info =
239 		    EVBUFFER_CHAIN_EXTRA(
240 			    struct evbuffer_multicast_parent,
241 			    chain);
242 		/* referencing chain is being freed, decrease
243 		 * refcounts of source chain and associated
244 		 * evbuffer (which get freed once both reach
245 		 * zero) */
246 		EVUTIL_ASSERT(info->source != NULL);
247 		EVUTIL_ASSERT(info->parent != NULL);
248 		EVBUFFER_LOCK(info->source);
249 		evbuffer_chain_free(info->parent);
250 		evbuffer_decref_and_unlock_(info->source);
251 	}
252 
253 	mm_free(chain);
254 }
255 
256 static void
257 evbuffer_free_all_chains(struct evbuffer_chain *chain)
258 {
259 	struct evbuffer_chain *next;
260 	for (; chain; chain = next) {
261 		next = chain->next;
262 		evbuffer_chain_free(chain);
263 	}
264 }
265 
266 #ifndef NDEBUG
267 static int
268 evbuffer_chains_all_empty(struct evbuffer_chain *chain)
269 {
270 	for (; chain; chain = chain->next) {
271 		if (chain->off)
272 			return 0;
273 	}
274 	return 1;
275 }
276 #else
277 /* The definition is needed for EVUTIL_ASSERT, which uses sizeof to avoid
278 "unused variable" warnings. */
279 static inline int evbuffer_chains_all_empty(struct evbuffer_chain *chain) {
280 	return 1;
281 }
282 #endif
283 
284 /* Free all trailing chains in 'buf' that are neither pinned nor empty, prior
285  * to replacing them all with a new chain.  Return a pointer to the place
286  * where the new chain will go.
287  *
288  * Internal; requires lock.  The caller must fix up buf->last and buf->first
289  * as needed; they might have been freed.
290  */
291 static struct evbuffer_chain **
292 evbuffer_free_trailing_empty_chains(struct evbuffer *buf)
293 {
294 	struct evbuffer_chain **ch = buf->last_with_datap;
295 	/* Find the first victim chain.  It might be *last_with_datap */
296 	while ((*ch) && ((*ch)->off != 0 || CHAIN_PINNED(*ch)))
297 		ch = &(*ch)->next;
298 	if (*ch) {
299 		EVUTIL_ASSERT(evbuffer_chains_all_empty(*ch));
300 		evbuffer_free_all_chains(*ch);
301 		*ch = NULL;
302 	}
303 	return ch;
304 }
305 
306 /* Add a single chain 'chain' to the end of 'buf', freeing trailing empty
307  * chains as necessary.  Requires lock.  Does not schedule callbacks.
308  */
309 static void
310 evbuffer_chain_insert(struct evbuffer *buf,
311     struct evbuffer_chain *chain)
312 {
313 	ASSERT_EVBUFFER_LOCKED(buf);
314 	if (*buf->last_with_datap == NULL) {
315 		/* There are no chains data on the buffer at all. */
316 		EVUTIL_ASSERT(buf->last_with_datap == &buf->first);
317 		EVUTIL_ASSERT(buf->first == NULL);
318 		buf->first = buf->last = chain;
319 	} else {
320 		struct evbuffer_chain **chp;
321 		chp = evbuffer_free_trailing_empty_chains(buf);
322 		*chp = chain;
323 		if (chain->off)
324 			buf->last_with_datap = chp;
325 		buf->last = chain;
326 	}
327 	buf->total_len += chain->off;
328 }
329 
330 static inline struct evbuffer_chain *
331 evbuffer_chain_insert_new(struct evbuffer *buf, size_t datlen)
332 {
333 	struct evbuffer_chain *chain;
334 	if ((chain = evbuffer_chain_new(datlen)) == NULL)
335 		return NULL;
336 	evbuffer_chain_insert(buf, chain);
337 	return chain;
338 }
339 
340 void
341 evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag)
342 {
343 	EVUTIL_ASSERT((chain->flags & flag) == 0);
344 	chain->flags |= flag;
345 }
346 
347 void
348 evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag)
349 {
350 	EVUTIL_ASSERT((chain->flags & flag) != 0);
351 	chain->flags &= ~flag;
352 	if (chain->flags & EVBUFFER_DANGLING)
353 		evbuffer_chain_free(chain);
354 }
355 
356 static inline void
357 evbuffer_chain_incref(struct evbuffer_chain *chain)
358 {
359     ++chain->refcnt;
360 }
361 
362 struct evbuffer *
363 evbuffer_new(void)
364 {
365 	struct evbuffer *buffer;
366 
367 	buffer = mm_calloc(1, sizeof(struct evbuffer));
368 	if (buffer == NULL)
369 		return (NULL);
370 
371 	LIST_INIT(&buffer->callbacks);
372 	buffer->refcnt = 1;
373 	buffer->last_with_datap = &buffer->first;
374 
375 	return (buffer);
376 }
377 
378 int
379 evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags)
380 {
381 	EVBUFFER_LOCK(buf);
382 	buf->flags |= (ev_uint32_t)flags;
383 	EVBUFFER_UNLOCK(buf);
384 	return 0;
385 }
386 
387 int
388 evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags)
389 {
390 	EVBUFFER_LOCK(buf);
391 	buf->flags &= ~(ev_uint32_t)flags;
392 	EVBUFFER_UNLOCK(buf);
393 	return 0;
394 }
395 
396 void
397 evbuffer_incref_(struct evbuffer *buf)
398 {
399 	EVBUFFER_LOCK(buf);
400 	++buf->refcnt;
401 	EVBUFFER_UNLOCK(buf);
402 }
403 
404 void
405 evbuffer_incref_and_lock_(struct evbuffer *buf)
406 {
407 	EVBUFFER_LOCK(buf);
408 	++buf->refcnt;
409 }
410 
411 int
412 evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
413 {
414 	EVBUFFER_LOCK(buffer);
415 	buffer->cb_queue = base;
416 	buffer->deferred_cbs = 1;
417 	event_deferred_cb_init_(&buffer->deferred,
418 	    event_base_get_npriorities(base) / 2,
419 	    evbuffer_deferred_callback, buffer);
420 	EVBUFFER_UNLOCK(buffer);
421 	return 0;
422 }
423 
424 int
425 evbuffer_enable_locking(struct evbuffer *buf, void *lock)
426 {
427 #ifdef EVENT__DISABLE_THREAD_SUPPORT
428 	return -1;
429 #else
430 	if (buf->lock)
431 		return -1;
432 
433 	if (!lock) {
434 		EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
435 		if (!lock)
436 			return -1;
437 		buf->lock = lock;
438 		buf->own_lock = 1;
439 	} else {
440 		buf->lock = lock;
441 		buf->own_lock = 0;
442 	}
443 
444 	return 0;
445 #endif
446 }
447 
448 void
449 evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev)
450 {
451 	EVBUFFER_LOCK(buf);
452 	buf->parent = bev;
453 	EVBUFFER_UNLOCK(buf);
454 }
455 
456 static void
457 evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
458 {
459 	struct evbuffer_cb_entry *cbent, *next;
460 	struct evbuffer_cb_info info;
461 	size_t new_size;
462 	ev_uint32_t mask, masked_val;
463 	int clear = 1;
464 
465 	if (running_deferred) {
466 		mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
467 		masked_val = EVBUFFER_CB_ENABLED;
468 	} else if (buffer->deferred_cbs) {
469 		mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
470 		masked_val = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
471 		/* Don't zero-out n_add/n_del, since the deferred callbacks
472 		   will want to see them. */
473 		clear = 0;
474 	} else {
475 		mask = EVBUFFER_CB_ENABLED;
476 		masked_val = EVBUFFER_CB_ENABLED;
477 	}
478 
479 	ASSERT_EVBUFFER_LOCKED(buffer);
480 
481 	if (LIST_EMPTY(&buffer->callbacks)) {
482 		buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
483 		return;
484 	}
485 	if (buffer->n_add_for_cb == 0 && buffer->n_del_for_cb == 0)
486 		return;
487 
488 	new_size = buffer->total_len;
489 	info.orig_size = new_size + buffer->n_del_for_cb - buffer->n_add_for_cb;
490 	info.n_added = buffer->n_add_for_cb;
491 	info.n_deleted = buffer->n_del_for_cb;
492 	if (clear) {
493 		buffer->n_add_for_cb = 0;
494 		buffer->n_del_for_cb = 0;
495 	}
496 	for (cbent = LIST_FIRST(&buffer->callbacks);
497 	     cbent != LIST_END(&buffer->callbacks);
498 	     cbent = next) {
499 		/* Get the 'next' pointer now in case this callback decides
500 		 * to remove itself or something. */
501 		next = LIST_NEXT(cbent, next);
502 
503 		if ((cbent->flags & mask) != masked_val)
504 			continue;
505 
506 		if ((cbent->flags & EVBUFFER_CB_OBSOLETE))
507 			cbent->cb.cb_obsolete(buffer,
508 			    info.orig_size, new_size, cbent->cbarg);
509 		else
510 			cbent->cb.cb_func(buffer, &info, cbent->cbarg);
511 	}
512 }
513 
514 void
515 evbuffer_invoke_callbacks_(struct evbuffer *buffer)
516 {
517 	if (LIST_EMPTY(&buffer->callbacks)) {
518 		buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
519 		return;
520 	}
521 
522 	if (buffer->deferred_cbs) {
523 		if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) {
524 			evbuffer_incref_and_lock_(buffer);
525 			if (buffer->parent)
526 				bufferevent_incref_(buffer->parent);
527 		}
528 		EVBUFFER_UNLOCK(buffer);
529 	}
530 
531 	evbuffer_run_callbacks(buffer, 0);
532 }
533 
534 static void
535 evbuffer_deferred_callback(struct event_callback *cb, void *arg)
536 {
537 	struct bufferevent *parent = NULL;
538 	struct evbuffer *buffer = arg;
539 
540 	/* XXXX It would be better to run these callbacks without holding the
541 	 * lock */
542 	EVBUFFER_LOCK(buffer);
543 	parent = buffer->parent;
544 	evbuffer_run_callbacks(buffer, 1);
545 	evbuffer_decref_and_unlock_(buffer);
546 	if (parent)
547 		bufferevent_decref_(parent);
548 }
549 
550 static void
551 evbuffer_remove_all_callbacks(struct evbuffer *buffer)
552 {
553 	struct evbuffer_cb_entry *cbent;
554 
555 	while ((cbent = LIST_FIRST(&buffer->callbacks))) {
556 		LIST_REMOVE(cbent, next);
557 		mm_free(cbent);
558 	}
559 }
560 
561 void
562 evbuffer_decref_and_unlock_(struct evbuffer *buffer)
563 {
564 	struct evbuffer_chain *chain, *next;
565 	ASSERT_EVBUFFER_LOCKED(buffer);
566 
567 	EVUTIL_ASSERT(buffer->refcnt > 0);
568 
569 	if (--buffer->refcnt > 0) {
570 		EVBUFFER_UNLOCK(buffer);
571 		return;
572 	}
573 
574 	for (chain = buffer->first; chain != NULL; chain = next) {
575 		next = chain->next;
576 		evbuffer_chain_free(chain);
577 	}
578 	evbuffer_remove_all_callbacks(buffer);
579 	if (buffer->deferred_cbs)
580 		event_deferred_cb_cancel_(buffer->cb_queue, &buffer->deferred);
581 
582 	EVBUFFER_UNLOCK(buffer);
583 	if (buffer->own_lock)
584 		EVTHREAD_FREE_LOCK(buffer->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
585 	mm_free(buffer);
586 }
587 
588 void
589 evbuffer_free(struct evbuffer *buffer)
590 {
591 	EVBUFFER_LOCK(buffer);
592 	evbuffer_decref_and_unlock_(buffer);
593 }
594 
595 void
596 evbuffer_lock(struct evbuffer *buf)
597 {
598 	EVBUFFER_LOCK(buf);
599 }
600 
601 void
602 evbuffer_unlock(struct evbuffer *buf)
603 {
604 	EVBUFFER_UNLOCK(buf);
605 }
606 
607 size_t
608 evbuffer_get_length(const struct evbuffer *buffer)
609 {
610 	size_t result;
611 
612 	EVBUFFER_LOCK(buffer);
613 
614 	result = (buffer->total_len);
615 
616 	EVBUFFER_UNLOCK(buffer);
617 
618 	return result;
619 }
620 
621 size_t
622 evbuffer_get_contiguous_space(const struct evbuffer *buf)
623 {
624 	struct evbuffer_chain *chain;
625 	size_t result;
626 
627 	EVBUFFER_LOCK(buf);
628 	chain = buf->first;
629 	result = (chain != NULL ? chain->off : 0);
630 	EVBUFFER_UNLOCK(buf);
631 
632 	return result;
633 }
634 
635 size_t
636 evbuffer_add_iovec(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) {
637 	int n;
638 	size_t res;
639 	size_t to_alloc;
640 
641 	EVBUFFER_LOCK(buf);
642 
643 	res = to_alloc = 0;
644 
645 	for (n = 0; n < n_vec; n++) {
646 		to_alloc += vec[n].iov_len;
647 	}
648 
649 	if (evbuffer_expand_fast_(buf, to_alloc, 2) < 0) {
650 		goto done;
651 	}
652 
653 	for (n = 0; n < n_vec; n++) {
654 		/* XXX each 'add' call here does a bunch of setup that's
655 		 * obviated by evbuffer_expand_fast_, and some cleanup that we
656 		 * would like to do only once.  Instead we should just extract
657 		 * the part of the code that's needed. */
658 
659 		if (evbuffer_add(buf, vec[n].iov_base, vec[n].iov_len) < 0) {
660 			goto done;
661 		}
662 
663 		res += vec[n].iov_len;
664 	}
665 
666 done:
667     EVBUFFER_UNLOCK(buf);
668     return res;
669 }
670 
671 int
672 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
673     struct evbuffer_iovec *vec, int n_vecs)
674 {
675 	struct evbuffer_chain *chain, **chainp;
676 	int n = -1;
677 
678 	EVBUFFER_LOCK(buf);
679 	if (buf->freeze_end)
680 		goto done;
681 	if (n_vecs < 1)
682 		goto done;
683 	if (n_vecs == 1) {
684 		if ((chain = evbuffer_expand_singlechain(buf, size)) == NULL)
685 			goto done;
686 
687 		vec[0].iov_base = CHAIN_SPACE_PTR(chain);
688 		vec[0].iov_len = (size_t) CHAIN_SPACE_LEN(chain);
689 		EVUTIL_ASSERT(size<0 || (size_t)vec[0].iov_len >= (size_t)size);
690 		n = 1;
691 	} else {
692 		if (evbuffer_expand_fast_(buf, size, n_vecs)<0)
693 			goto done;
694 		n = evbuffer_read_setup_vecs_(buf, size, vec, n_vecs,
695 				&chainp, 0);
696 	}
697 
698 done:
699 	EVBUFFER_UNLOCK(buf);
700 	return n;
701 
702 }
703 
704 static int
705 advance_last_with_data(struct evbuffer *buf)
706 {
707 	int n = 0;
708 	ASSERT_EVBUFFER_LOCKED(buf);
709 
710 	if (!*buf->last_with_datap)
711 		return 0;
712 
713 	while ((*buf->last_with_datap)->next && (*buf->last_with_datap)->next->off) {
714 		buf->last_with_datap = &(*buf->last_with_datap)->next;
715 		++n;
716 	}
717 	return n;
718 }
719 
720 int
721 evbuffer_commit_space(struct evbuffer *buf,
722     struct evbuffer_iovec *vec, int n_vecs)
723 {
724 	struct evbuffer_chain *chain, **firstchainp, **chainp;
725 	int result = -1;
726 	size_t added = 0;
727 	int i;
728 
729 	EVBUFFER_LOCK(buf);
730 
731 	if (buf->freeze_end)
732 		goto done;
733 	if (n_vecs == 0) {
734 		result = 0;
735 		goto done;
736 	} else if (n_vecs == 1 &&
737 	    (buf->last && vec[0].iov_base == (void*)CHAIN_SPACE_PTR(buf->last))) {
738 		/* The user only got or used one chain; it might not
739 		 * be the first one with space in it. */
740 		if ((size_t)vec[0].iov_len > (size_t)CHAIN_SPACE_LEN(buf->last))
741 			goto done;
742 		buf->last->off += vec[0].iov_len;
743 		added = vec[0].iov_len;
744 		if (added)
745 			advance_last_with_data(buf);
746 		goto okay;
747 	}
748 
749 	/* Advance 'firstchain' to the first chain with space in it. */
750 	firstchainp = buf->last_with_datap;
751 	if (!*firstchainp)
752 		goto done;
753 	if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
754 		firstchainp = &(*firstchainp)->next;
755 	}
756 
757 	chain = *firstchainp;
758 	/* pass 1: make sure that the pointers and lengths of vecs[] are in
759 	 * bounds before we try to commit anything. */
760 	for (i=0; i<n_vecs; ++i) {
761 		if (!chain)
762 			goto done;
763 		if (vec[i].iov_base != (void*)CHAIN_SPACE_PTR(chain) ||
764 		    (size_t)vec[i].iov_len > CHAIN_SPACE_LEN(chain))
765 			goto done;
766 		chain = chain->next;
767 	}
768 	/* pass 2: actually adjust all the chains. */
769 	chainp = firstchainp;
770 	for (i=0; i<n_vecs; ++i) {
771 		(*chainp)->off += vec[i].iov_len;
772 		added += vec[i].iov_len;
773 		if (vec[i].iov_len) {
774 			buf->last_with_datap = chainp;
775 		}
776 		chainp = &(*chainp)->next;
777 	}
778 
779 okay:
780 	buf->total_len += added;
781 	buf->n_add_for_cb += added;
782 	result = 0;
783 	evbuffer_invoke_callbacks_(buf);
784 
785 done:
786 	EVBUFFER_UNLOCK(buf);
787 	return result;
788 }
789 
790 static inline int
791 HAS_PINNED_R(struct evbuffer *buf)
792 {
793 	return (buf->last && CHAIN_PINNED_R(buf->last));
794 }
795 
796 static inline void
797 ZERO_CHAIN(struct evbuffer *dst)
798 {
799 	ASSERT_EVBUFFER_LOCKED(dst);
800 	dst->first = NULL;
801 	dst->last = NULL;
802 	dst->last_with_datap = &(dst)->first;
803 	dst->total_len = 0;
804 }
805 
806 /* Prepares the contents of src to be moved to another buffer by removing
807  * read-pinned chains. The first pinned chain is saved in first, and the
808  * last in last. If src has no read-pinned chains, first and last are set
809  * to NULL. */
810 static int
811 PRESERVE_PINNED(struct evbuffer *src, struct evbuffer_chain **first,
812 		struct evbuffer_chain **last)
813 {
814 	struct evbuffer_chain *chain, **pinned;
815 
816 	ASSERT_EVBUFFER_LOCKED(src);
817 
818 	if (!HAS_PINNED_R(src)) {
819 		*first = *last = NULL;
820 		return 0;
821 	}
822 
823 	pinned = src->last_with_datap;
824 	if (!CHAIN_PINNED_R(*pinned))
825 		pinned = &(*pinned)->next;
826 	EVUTIL_ASSERT(CHAIN_PINNED_R(*pinned));
827 	chain = *first = *pinned;
828 	*last = src->last;
829 
830 	/* If there's data in the first pinned chain, we need to allocate
831 	 * a new chain and copy the data over. */
832 	if (chain->off) {
833 		struct evbuffer_chain *tmp;
834 
835 		EVUTIL_ASSERT(pinned == src->last_with_datap);
836 		tmp = evbuffer_chain_new(chain->off);
837 		if (!tmp)
838 			return -1;
839 		memcpy(tmp->buffer, chain->buffer + chain->misalign,
840 			chain->off);
841 		tmp->off = chain->off;
842 		*src->last_with_datap = tmp;
843 		src->last = tmp;
844 		chain->misalign += chain->off;
845 		chain->off = 0;
846 	} else {
847 		src->last = *src->last_with_datap;
848 		*pinned = NULL;
849 	}
850 
851 	return 0;
852 }
853 
854 static inline void
855 RESTORE_PINNED(struct evbuffer *src, struct evbuffer_chain *pinned,
856 		struct evbuffer_chain *last)
857 {
858 	ASSERT_EVBUFFER_LOCKED(src);
859 
860 	if (!pinned) {
861 		ZERO_CHAIN(src);
862 		return;
863 	}
864 
865 	src->first = pinned;
866 	src->last = last;
867 	src->last_with_datap = &src->first;
868 	src->total_len = 0;
869 }
870 
871 static inline void
872 COPY_CHAIN(struct evbuffer *dst, struct evbuffer *src)
873 {
874 	ASSERT_EVBUFFER_LOCKED(dst);
875 	ASSERT_EVBUFFER_LOCKED(src);
876 	dst->first = src->first;
877 	if (src->last_with_datap == &src->first)
878 		dst->last_with_datap = &dst->first;
879 	else
880 		dst->last_with_datap = src->last_with_datap;
881 	dst->last = src->last;
882 	dst->total_len = src->total_len;
883 }
884 
885 static void
886 APPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
887 {
888 	ASSERT_EVBUFFER_LOCKED(dst);
889 	ASSERT_EVBUFFER_LOCKED(src);
890 	dst->last->next = src->first;
891 	if (src->last_with_datap == &src->first)
892 		dst->last_with_datap = &dst->last->next;
893 	else
894 		dst->last_with_datap = src->last_with_datap;
895 	dst->last = src->last;
896 	dst->total_len += src->total_len;
897 }
898 
899 static inline void
900 APPEND_CHAIN_MULTICAST(struct evbuffer *dst, struct evbuffer *src)
901 {
902 	struct evbuffer_chain *tmp;
903 	struct evbuffer_chain *chain = src->first;
904 	struct evbuffer_multicast_parent *extra;
905 
906 	ASSERT_EVBUFFER_LOCKED(dst);
907 	ASSERT_EVBUFFER_LOCKED(src);
908 
909 	for (; chain; chain = chain->next) {
910 		if (!chain->off || chain->flags & EVBUFFER_DANGLING) {
911 			/* skip empty chains */
912 			continue;
913 		}
914 
915 		tmp = evbuffer_chain_new(sizeof(struct evbuffer_multicast_parent));
916 		if (!tmp) {
917 			event_warn("%s: out of memory", __func__);
918 			return;
919 		}
920 		extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_multicast_parent, tmp);
921 		/* reference evbuffer containing source chain so it
922 		 * doesn't get released while the chain is still
923 		 * being referenced to */
924 		evbuffer_incref_(src);
925 		extra->source = src;
926 		/* reference source chain which now becomes immutable */
927 		evbuffer_chain_incref(chain);
928 		extra->parent = chain;
929 		chain->flags |= EVBUFFER_IMMUTABLE;
930 		tmp->buffer_len = chain->buffer_len;
931 		tmp->misalign = chain->misalign;
932 		tmp->off = chain->off;
933 		tmp->flags |= EVBUFFER_MULTICAST|EVBUFFER_IMMUTABLE;
934 		tmp->buffer = chain->buffer;
935 		evbuffer_chain_insert(dst, tmp);
936 	}
937 }
938 
939 static void
940 PREPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
941 {
942 	ASSERT_EVBUFFER_LOCKED(dst);
943 	ASSERT_EVBUFFER_LOCKED(src);
944 	src->last->next = dst->first;
945 	dst->first = src->first;
946 	dst->total_len += src->total_len;
947 	if (*dst->last_with_datap == NULL) {
948 		if (src->last_with_datap == &(src)->first)
949 			dst->last_with_datap = &dst->first;
950 		else
951 			dst->last_with_datap = src->last_with_datap;
952 	} else if (dst->last_with_datap == &dst->first) {
953 		dst->last_with_datap = &src->last->next;
954 	}
955 }
956 
957 int
958 evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
959 {
960 	struct evbuffer_chain *pinned, *last;
961 	size_t in_total_len, out_total_len;
962 	int result = 0;
963 
964 	EVBUFFER_LOCK2(inbuf, outbuf);
965 	in_total_len = inbuf->total_len;
966 	out_total_len = outbuf->total_len;
967 
968 	if (in_total_len == 0 || outbuf == inbuf)
969 		goto done;
970 
971 	if (outbuf->freeze_end || inbuf->freeze_start) {
972 		result = -1;
973 		goto done;
974 	}
975 
976 	if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
977 		result = -1;
978 		goto done;
979 	}
980 
981 	if (out_total_len == 0) {
982 		/* There might be an empty chain at the start of outbuf; free
983 		 * it. */
984 		evbuffer_free_all_chains(outbuf->first);
985 		COPY_CHAIN(outbuf, inbuf);
986 	} else {
987 		APPEND_CHAIN(outbuf, inbuf);
988 	}
989 
990 	RESTORE_PINNED(inbuf, pinned, last);
991 
992 	inbuf->n_del_for_cb += in_total_len;
993 	outbuf->n_add_for_cb += in_total_len;
994 
995 	evbuffer_invoke_callbacks_(inbuf);
996 	evbuffer_invoke_callbacks_(outbuf);
997 
998 done:
999 	EVBUFFER_UNLOCK2(inbuf, outbuf);
1000 	return result;
1001 }
1002 
1003 int
1004 evbuffer_add_buffer_reference(struct evbuffer *outbuf, struct evbuffer *inbuf)
1005 {
1006 	size_t in_total_len, out_total_len;
1007 	struct evbuffer_chain *chain;
1008 	int result = 0;
1009 
1010 	EVBUFFER_LOCK2(inbuf, outbuf);
1011 	in_total_len = inbuf->total_len;
1012 	out_total_len = outbuf->total_len;
1013 	chain = inbuf->first;
1014 
1015 	if (in_total_len == 0)
1016 		goto done;
1017 
1018 	if (outbuf->freeze_end || outbuf == inbuf) {
1019 		result = -1;
1020 		goto done;
1021 	}
1022 
1023 	for (; chain; chain = chain->next) {
1024 		if ((chain->flags & (EVBUFFER_FILESEGMENT|EVBUFFER_SENDFILE|EVBUFFER_MULTICAST)) != 0) {
1025 			/* chain type can not be referenced */
1026 			result = -1;
1027 			goto done;
1028 		}
1029 	}
1030 
1031 	if (out_total_len == 0) {
1032 		/* There might be an empty chain at the start of outbuf; free
1033 		 * it. */
1034 		evbuffer_free_all_chains(outbuf->first);
1035 	}
1036 	APPEND_CHAIN_MULTICAST(outbuf, inbuf);
1037 
1038 	outbuf->n_add_for_cb += in_total_len;
1039 	evbuffer_invoke_callbacks_(outbuf);
1040 
1041 done:
1042 	EVBUFFER_UNLOCK2(inbuf, outbuf);
1043 	return result;
1044 }
1045 
1046 int
1047 evbuffer_prepend_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
1048 {
1049 	struct evbuffer_chain *pinned, *last;
1050 	size_t in_total_len, out_total_len;
1051 	int result = 0;
1052 
1053 	EVBUFFER_LOCK2(inbuf, outbuf);
1054 
1055 	in_total_len = inbuf->total_len;
1056 	out_total_len = outbuf->total_len;
1057 
1058 	if (!in_total_len || inbuf == outbuf)
1059 		goto done;
1060 
1061 	if (outbuf->freeze_start || inbuf->freeze_start) {
1062 		result = -1;
1063 		goto done;
1064 	}
1065 
1066 	if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
1067 		result = -1;
1068 		goto done;
1069 	}
1070 
1071 	if (out_total_len == 0) {
1072 		/* There might be an empty chain at the start of outbuf; free
1073 		 * it. */
1074 		evbuffer_free_all_chains(outbuf->first);
1075 		COPY_CHAIN(outbuf, inbuf);
1076 	} else {
1077 		PREPEND_CHAIN(outbuf, inbuf);
1078 	}
1079 
1080 	RESTORE_PINNED(inbuf, pinned, last);
1081 
1082 	inbuf->n_del_for_cb += in_total_len;
1083 	outbuf->n_add_for_cb += in_total_len;
1084 
1085 	evbuffer_invoke_callbacks_(inbuf);
1086 	evbuffer_invoke_callbacks_(outbuf);
1087 done:
1088 	EVBUFFER_UNLOCK2(inbuf, outbuf);
1089 	return result;
1090 }
1091 
1092 int
1093 evbuffer_drain(struct evbuffer *buf, size_t len)
1094 {
1095 	struct evbuffer_chain *chain, *next;
1096 	size_t remaining, old_len;
1097 	int result = 0;
1098 
1099 	EVBUFFER_LOCK(buf);
1100 	old_len = buf->total_len;
1101 
1102 	if (old_len == 0)
1103 		goto done;
1104 
1105 	if (buf->freeze_start) {
1106 		result = -1;
1107 		goto done;
1108 	}
1109 
1110 	if (len >= old_len && !HAS_PINNED_R(buf)) {
1111 		len = old_len;
1112 		for (chain = buf->first; chain != NULL; chain = next) {
1113 			next = chain->next;
1114 			evbuffer_chain_free(chain);
1115 		}
1116 
1117 		ZERO_CHAIN(buf);
1118 	} else {
1119 		if (len >= old_len)
1120 			len = old_len;
1121 
1122 		buf->total_len -= len;
1123 		remaining = len;
1124 		for (chain = buf->first;
1125 		     remaining >= chain->off;
1126 		     chain = next) {
1127 			next = chain->next;
1128 			remaining -= chain->off;
1129 
1130 			if (chain == *buf->last_with_datap) {
1131 				buf->last_with_datap = &buf->first;
1132 			}
1133 			if (&chain->next == buf->last_with_datap)
1134 				buf->last_with_datap = &buf->first;
1135 
1136 			if (CHAIN_PINNED_R(chain)) {
1137 				EVUTIL_ASSERT(remaining == 0);
1138 				chain->misalign += chain->off;
1139 				chain->off = 0;
1140 				break;
1141 			} else
1142 				evbuffer_chain_free(chain);
1143 		}
1144 
1145 		buf->first = chain;
1146 		EVUTIL_ASSERT(chain && remaining <= chain->off);
1147 		chain->misalign += remaining;
1148 		chain->off -= remaining;
1149 	}
1150 
1151 	buf->n_del_for_cb += len;
1152 	/* Tell someone about changes in this buffer */
1153 	evbuffer_invoke_callbacks_(buf);
1154 
1155 done:
1156 	EVBUFFER_UNLOCK(buf);
1157 	return result;
1158 }
1159 
1160 /* Reads data from an event buffer and drains the bytes read */
1161 int
1162 evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen)
1163 {
1164 	ev_ssize_t n;
1165 	EVBUFFER_LOCK(buf);
1166 	n = evbuffer_copyout_from(buf, NULL, data_out, datlen);
1167 	if (n > 0) {
1168 		if (evbuffer_drain(buf, n)<0)
1169 			n = -1;
1170 	}
1171 	EVBUFFER_UNLOCK(buf);
1172 	return (int)n;
1173 }
1174 
1175 ev_ssize_t
1176 evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
1177 {
1178 	return evbuffer_copyout_from(buf, NULL, data_out, datlen);
1179 }
1180 
1181 ev_ssize_t
1182 evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos,
1183     void *data_out, size_t datlen)
1184 {
1185 	/*XXX fails badly on sendfile case. */
1186 	struct evbuffer_chain *chain;
1187 	char *data = data_out;
1188 	size_t nread;
1189 	ev_ssize_t result = 0;
1190 	size_t pos_in_chain;
1191 
1192 	EVBUFFER_LOCK(buf);
1193 
1194 	if (pos) {
1195 		if (datlen > (size_t)(EV_SSIZE_MAX - pos->pos)) {
1196 			result = -1;
1197 			goto done;
1198 		}
1199 		chain = pos->internal_.chain;
1200 		pos_in_chain = pos->internal_.pos_in_chain;
1201 		if (datlen + pos->pos > buf->total_len)
1202 			datlen = buf->total_len - pos->pos;
1203 	} else {
1204 		chain = buf->first;
1205 		pos_in_chain = 0;
1206 		if (datlen > buf->total_len)
1207 			datlen = buf->total_len;
1208 	}
1209 
1210 
1211 	if (datlen == 0)
1212 		goto done;
1213 
1214 	if (buf->freeze_start) {
1215 		result = -1;
1216 		goto done;
1217 	}
1218 
1219 	nread = datlen;
1220 
1221 	while (datlen && datlen >= chain->off - pos_in_chain) {
1222 		size_t copylen = chain->off - pos_in_chain;
1223 		memcpy(data,
1224 		    chain->buffer + chain->misalign + pos_in_chain,
1225 		    copylen);
1226 		data += copylen;
1227 		datlen -= copylen;
1228 
1229 		chain = chain->next;
1230 		pos_in_chain = 0;
1231 		EVUTIL_ASSERT(chain || datlen==0);
1232 	}
1233 
1234 	if (datlen) {
1235 		EVUTIL_ASSERT(chain);
1236 		EVUTIL_ASSERT(datlen+pos_in_chain <= chain->off);
1237 
1238 		memcpy(data, chain->buffer + chain->misalign + pos_in_chain,
1239 		    datlen);
1240 	}
1241 
1242 	result = nread;
1243 done:
1244 	EVBUFFER_UNLOCK(buf);
1245 	return result;
1246 }
1247 
1248 /* reads data from the src buffer to the dst buffer, avoids memcpy as
1249  * possible. */
1250 /*  XXXX should return ev_ssize_t */
1251 int
1252 evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
1253     size_t datlen)
1254 {
1255 	/*XXX We should have an option to force this to be zero-copy.*/
1256 
1257 	/*XXX can fail badly on sendfile case. */
1258 	struct evbuffer_chain *chain, *previous;
1259 	size_t nread = 0;
1260 	int result;
1261 
1262 	EVBUFFER_LOCK2(src, dst);
1263 
1264 	chain = previous = src->first;
1265 
1266 	if (datlen == 0 || dst == src) {
1267 		result = 0;
1268 		goto done;
1269 	}
1270 
1271 	if (dst->freeze_end || src->freeze_start) {
1272 		result = -1;
1273 		goto done;
1274 	}
1275 
1276 	/* short-cut if there is no more data buffered */
1277 	if (datlen >= src->total_len) {
1278 		datlen = src->total_len;
1279 		evbuffer_add_buffer(dst, src);
1280 		result = (int)datlen; /*XXXX should return ev_ssize_t*/
1281 		goto done;
1282 	}
1283 
1284 	/* removes chains if possible */
1285 	while (chain->off <= datlen) {
1286 		/* We can't remove the last with data from src unless we
1287 		 * remove all chains, in which case we would have done the if
1288 		 * block above */
1289 		EVUTIL_ASSERT(chain != *src->last_with_datap);
1290 		nread += chain->off;
1291 		datlen -= chain->off;
1292 		previous = chain;
1293 		if (src->last_with_datap == &chain->next)
1294 			src->last_with_datap = &src->first;
1295 		chain = chain->next;
1296 	}
1297 
1298 	if (nread) {
1299 		/* we can remove the chain */
1300 		struct evbuffer_chain **chp;
1301 		chp = evbuffer_free_trailing_empty_chains(dst);
1302 
1303 		if (dst->first == NULL) {
1304 			dst->first = src->first;
1305 		} else {
1306 			*chp = src->first;
1307 		}
1308 		dst->last = previous;
1309 		previous->next = NULL;
1310 		src->first = chain;
1311 		advance_last_with_data(dst);
1312 
1313 		dst->total_len += nread;
1314 		dst->n_add_for_cb += nread;
1315 	}
1316 
1317 	/* we know that there is more data in the src buffer than
1318 	 * we want to read, so we manually drain the chain */
1319 	evbuffer_add(dst, chain->buffer + chain->misalign, datlen);
1320 	chain->misalign += datlen;
1321 	chain->off -= datlen;
1322 	nread += datlen;
1323 
1324 	/* You might think we would want to increment dst->n_add_for_cb
1325 	 * here too.  But evbuffer_add above already took care of that.
1326 	 */
1327 	src->total_len -= nread;
1328 	src->n_del_for_cb += nread;
1329 
1330 	if (nread) {
1331 		evbuffer_invoke_callbacks_(dst);
1332 		evbuffer_invoke_callbacks_(src);
1333 	}
1334 	result = (int)nread;/*XXXX should change return type */
1335 
1336 done:
1337 	EVBUFFER_UNLOCK2(src, dst);
1338 	return result;
1339 }
1340 
1341 unsigned char *
1342 evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size)
1343 {
1344 	struct evbuffer_chain *chain, *next, *tmp, *last_with_data;
1345 	unsigned char *buffer, *result = NULL;
1346 	ev_ssize_t remaining;
1347 	int removed_last_with_data = 0;
1348 	int removed_last_with_datap = 0;
1349 
1350 	EVBUFFER_LOCK(buf);
1351 
1352 	chain = buf->first;
1353 
1354 	if (size < 0)
1355 		size = buf->total_len;
1356 	/* if size > buf->total_len, we cannot guarantee to the user that she
1357 	 * is going to have a long enough buffer afterwards; so we return
1358 	 * NULL */
1359 	if (size == 0 || (size_t)size > buf->total_len)
1360 		goto done;
1361 
1362 	/* No need to pull up anything; the first size bytes are
1363 	 * already here. */
1364 	if (chain->off >= (size_t)size) {
1365 		result = chain->buffer + chain->misalign;
1366 		goto done;
1367 	}
1368 
1369 	/* Make sure that none of the chains we need to copy from is pinned. */
1370 	remaining = size - chain->off;
1371 	EVUTIL_ASSERT(remaining >= 0);
1372 	for (tmp=chain->next; tmp; tmp=tmp->next) {
1373 		if (CHAIN_PINNED(tmp))
1374 			goto done;
1375 		if (tmp->off >= (size_t)remaining)
1376 			break;
1377 		remaining -= tmp->off;
1378 	}
1379 
1380 	if (CHAIN_PINNED(chain)) {
1381 		size_t old_off = chain->off;
1382 		if (CHAIN_SPACE_LEN(chain) < size - chain->off) {
1383 			/* not enough room at end of chunk. */
1384 			goto done;
1385 		}
1386 		buffer = CHAIN_SPACE_PTR(chain);
1387 		tmp = chain;
1388 		tmp->off = size;
1389 		size -= old_off;
1390 		chain = chain->next;
1391 	} else if (chain->buffer_len - chain->misalign >= (size_t)size) {
1392 		/* already have enough space in the first chain */
1393 		size_t old_off = chain->off;
1394 		buffer = chain->buffer + chain->misalign + chain->off;
1395 		tmp = chain;
1396 		tmp->off = size;
1397 		size -= old_off;
1398 		chain = chain->next;
1399 	} else {
1400 		if ((tmp = evbuffer_chain_new(size)) == NULL) {
1401 			event_warn("%s: out of memory", __func__);
1402 			goto done;
1403 		}
1404 		buffer = tmp->buffer;
1405 		tmp->off = size;
1406 		buf->first = tmp;
1407 	}
1408 
1409 	/* TODO(niels): deal with buffers that point to NULL like sendfile */
1410 
1411 	/* Copy and free every chunk that will be entirely pulled into tmp */
1412 	last_with_data = *buf->last_with_datap;
1413 	for (; chain != NULL && (size_t)size >= chain->off; chain = next) {
1414 		next = chain->next;
1415 
1416 		memcpy(buffer, chain->buffer + chain->misalign, chain->off);
1417 		size -= chain->off;
1418 		buffer += chain->off;
1419 		if (chain == last_with_data)
1420 			removed_last_with_data = 1;
1421 		if (&chain->next == buf->last_with_datap)
1422 			removed_last_with_datap = 1;
1423 
1424 		evbuffer_chain_free(chain);
1425 	}
1426 
1427 	if (chain != NULL) {
1428 		memcpy(buffer, chain->buffer + chain->misalign, size);
1429 		chain->misalign += size;
1430 		chain->off -= size;
1431 	} else {
1432 		buf->last = tmp;
1433 	}
1434 
1435 	tmp->next = chain;
1436 
1437 	if (removed_last_with_data) {
1438 		buf->last_with_datap = &buf->first;
1439 	} else if (removed_last_with_datap) {
1440 		if (buf->first->next && buf->first->next->off)
1441 			buf->last_with_datap = &buf->first->next;
1442 		else
1443 			buf->last_with_datap = &buf->first;
1444 	}
1445 
1446 	result = (tmp->buffer + tmp->misalign);
1447 
1448 done:
1449 	EVBUFFER_UNLOCK(buf);
1450 	return result;
1451 }
1452 
1453 /*
1454  * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
1455  * The returned buffer needs to be freed by the called.
1456  */
1457 char *
1458 evbuffer_readline(struct evbuffer *buffer)
1459 {
1460 	return evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY);
1461 }
1462 
1463 static inline ev_ssize_t
1464 evbuffer_strchr(struct evbuffer_ptr *it, const char chr)
1465 {
1466 	struct evbuffer_chain *chain = it->internal_.chain;
1467 	size_t i = it->internal_.pos_in_chain;
1468 	while (chain != NULL) {
1469 		char *buffer = (char *)chain->buffer + chain->misalign;
1470 		char *cp = memchr(buffer+i, chr, chain->off-i);
1471 		if (cp) {
1472 			it->internal_.chain = chain;
1473 			it->internal_.pos_in_chain = cp - buffer;
1474 			it->pos += (cp - buffer - i);
1475 			return it->pos;
1476 		}
1477 		it->pos += chain->off - i;
1478 		i = 0;
1479 		chain = chain->next;
1480 	}
1481 
1482 	return (-1);
1483 }
1484 
1485 static inline char *
1486 find_eol_char(char *s, size_t len)
1487 {
1488 #define CHUNK_SZ 128
1489 	/* Lots of benchmarking found this approach to be faster in practice
1490 	 * than doing two memchrs over the whole buffer, doin a memchr on each
1491 	 * char of the buffer, or trying to emulate memchr by hand. */
1492 	char *s_end, *cr, *lf;
1493 	s_end = s+len;
1494 	while (s < s_end) {
1495 		size_t chunk = (s + CHUNK_SZ < s_end) ? CHUNK_SZ : (s_end - s);
1496 		cr = memchr(s, '\r', chunk);
1497 		lf = memchr(s, '\n', chunk);
1498 		if (cr) {
1499 			if (lf && lf < cr)
1500 				return lf;
1501 			return cr;
1502 		} else if (lf) {
1503 			return lf;
1504 		}
1505 		s += CHUNK_SZ;
1506 	}
1507 
1508 	return NULL;
1509 #undef CHUNK_SZ
1510 }
1511 
1512 static ev_ssize_t
1513 evbuffer_find_eol_char(struct evbuffer_ptr *it)
1514 {
1515 	struct evbuffer_chain *chain = it->internal_.chain;
1516 	size_t i = it->internal_.pos_in_chain;
1517 	while (chain != NULL) {
1518 		char *buffer = (char *)chain->buffer + chain->misalign;
1519 		char *cp = find_eol_char(buffer+i, chain->off-i);
1520 		if (cp) {
1521 			it->internal_.chain = chain;
1522 			it->internal_.pos_in_chain = cp - buffer;
1523 			it->pos += (cp - buffer) - i;
1524 			return it->pos;
1525 		}
1526 		it->pos += chain->off - i;
1527 		i = 0;
1528 		chain = chain->next;
1529 	}
1530 
1531 	return (-1);
1532 }
1533 
1534 static inline int
1535 evbuffer_strspn(
1536 	struct evbuffer_ptr *ptr, const char *chrset)
1537 {
1538 	int count = 0;
1539 	struct evbuffer_chain *chain = ptr->internal_.chain;
1540 	size_t i = ptr->internal_.pos_in_chain;
1541 
1542 	if (!chain)
1543 		return 0;
1544 
1545 	while (1) {
1546 		char *buffer = (char *)chain->buffer + chain->misalign;
1547 		for (; i < chain->off; ++i) {
1548 			const char *p = chrset;
1549 			while (*p) {
1550 				if (buffer[i] == *p++)
1551 					goto next;
1552 			}
1553 			ptr->internal_.chain = chain;
1554 			ptr->internal_.pos_in_chain = i;
1555 			ptr->pos += count;
1556 			return count;
1557 		next:
1558 			++count;
1559 		}
1560 		i = 0;
1561 
1562 		if (! chain->next) {
1563 			ptr->internal_.chain = chain;
1564 			ptr->internal_.pos_in_chain = i;
1565 			ptr->pos += count;
1566 			return count;
1567 		}
1568 
1569 		chain = chain->next;
1570 	}
1571 }
1572 
1573 
1574 static inline int
1575 evbuffer_getchr(struct evbuffer_ptr *it)
1576 {
1577 	struct evbuffer_chain *chain = it->internal_.chain;
1578 	size_t off = it->internal_.pos_in_chain;
1579 
1580 	if (chain == NULL)
1581 		return -1;
1582 
1583 	return (unsigned char)chain->buffer[chain->misalign + off];
1584 }
1585 
1586 struct evbuffer_ptr
1587 evbuffer_search_eol(struct evbuffer *buffer,
1588     struct evbuffer_ptr *start, size_t *eol_len_out,
1589     enum evbuffer_eol_style eol_style)
1590 {
1591 	struct evbuffer_ptr it, it2;
1592 	size_t extra_drain = 0;
1593 	int ok = 0;
1594 
1595 	/* Avoid locking in trivial edge cases */
1596 	if (start && start->internal_.chain == NULL) {
1597 		PTR_NOT_FOUND(&it);
1598 		if (eol_len_out)
1599 			*eol_len_out = extra_drain;
1600 		return it;
1601 	}
1602 
1603 	EVBUFFER_LOCK(buffer);
1604 
1605 	if (start) {
1606 		memcpy(&it, start, sizeof(it));
1607 	} else {
1608 		it.pos = 0;
1609 		it.internal_.chain = buffer->first;
1610 		it.internal_.pos_in_chain = 0;
1611 	}
1612 
1613 	/* the eol_style determines our first stop character and how many
1614 	 * characters we are going to drain afterwards. */
1615 	switch (eol_style) {
1616 	case EVBUFFER_EOL_ANY:
1617 		if (evbuffer_find_eol_char(&it) < 0)
1618 			goto done;
1619 		memcpy(&it2, &it, sizeof(it));
1620 		extra_drain = evbuffer_strspn(&it2, "\r\n");
1621 		break;
1622 	case EVBUFFER_EOL_CRLF_STRICT: {
1623 		it = evbuffer_search(buffer, "\r\n", 2, &it);
1624 		if (it.pos < 0)
1625 			goto done;
1626 		extra_drain = 2;
1627 		break;
1628 	}
1629 	case EVBUFFER_EOL_CRLF: {
1630 		ev_ssize_t start_pos = it.pos;
1631 		/* Look for a LF ... */
1632 		if (evbuffer_strchr(&it, '\n') < 0)
1633 			goto done;
1634 		extra_drain = 1;
1635 		/* ... optionally preceeded by a CR. */
1636 		if (it.pos == start_pos)
1637 			break; /* If the first character is \n, don't back up */
1638 		/* This potentially does an extra linear walk over the first
1639 		 * few chains.  Probably, that's not too expensive unless you
1640 		 * have a really pathological setup. */
1641 		memcpy(&it2, &it, sizeof(it));
1642 		if (evbuffer_ptr_subtract(buffer, &it2, 1)<0)
1643 			break;
1644 		if (evbuffer_getchr(&it2) == '\r') {
1645 			memcpy(&it, &it2, sizeof(it));
1646 			extra_drain = 2;
1647 		}
1648 		break;
1649 	}
1650 	case EVBUFFER_EOL_LF:
1651 		if (evbuffer_strchr(&it, '\n') < 0)
1652 			goto done;
1653 		extra_drain = 1;
1654 		break;
1655 	case EVBUFFER_EOL_NUL:
1656 		if (evbuffer_strchr(&it, '\0') < 0)
1657 			goto done;
1658 		extra_drain = 1;
1659 		break;
1660 	default:
1661 		goto done;
1662 	}
1663 
1664 	ok = 1;
1665 done:
1666 	EVBUFFER_UNLOCK(buffer);
1667 
1668 	if (!ok)
1669 		PTR_NOT_FOUND(&it);
1670 	if (eol_len_out)
1671 		*eol_len_out = extra_drain;
1672 
1673 	return it;
1674 }
1675 
1676 char *
1677 evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
1678 		enum evbuffer_eol_style eol_style)
1679 {
1680 	struct evbuffer_ptr it;
1681 	char *line;
1682 	size_t n_to_copy=0, extra_drain=0;
1683 	char *result = NULL;
1684 
1685 	EVBUFFER_LOCK(buffer);
1686 
1687 	if (buffer->freeze_start) {
1688 		goto done;
1689 	}
1690 
1691 	it = evbuffer_search_eol(buffer, NULL, &extra_drain, eol_style);
1692 	if (it.pos < 0)
1693 		goto done;
1694 	n_to_copy = it.pos;
1695 
1696 	if ((line = mm_malloc(n_to_copy+1)) == NULL) {
1697 		event_warn("%s: out of memory", __func__);
1698 		goto done;
1699 	}
1700 
1701 	evbuffer_remove(buffer, line, n_to_copy);
1702 	line[n_to_copy] = '\0';
1703 
1704 	evbuffer_drain(buffer, extra_drain);
1705 	result = line;
1706 done:
1707 	EVBUFFER_UNLOCK(buffer);
1708 
1709 	if (n_read_out)
1710 		*n_read_out = result ? n_to_copy : 0;
1711 
1712 	return result;
1713 }
1714 
1715 #define EVBUFFER_CHAIN_MAX_AUTO_SIZE 4096
1716 
1717 /* Adds data to an event buffer */
1718 
1719 int
1720 evbuffer_add(struct evbuffer *buf, const void *data_in, size_t datlen)
1721 {
1722 	struct evbuffer_chain *chain, *tmp;
1723 	const unsigned char *data = data_in;
1724 	size_t remain, to_alloc;
1725 	int result = -1;
1726 
1727 	EVBUFFER_LOCK(buf);
1728 
1729 	if (buf->freeze_end) {
1730 		goto done;
1731 	}
1732 	/* Prevent buf->total_len overflow */
1733 	if (datlen > EV_SIZE_MAX - buf->total_len) {
1734 		goto done;
1735 	}
1736 
1737 	chain = buf->last;
1738 
1739 	/* If there are no chains allocated for this buffer, allocate one
1740 	 * big enough to hold all the data. */
1741 	if (chain == NULL) {
1742 		chain = evbuffer_chain_new(datlen);
1743 		if (!chain)
1744 			goto done;
1745 		evbuffer_chain_insert(buf, chain);
1746 	}
1747 
1748 	if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1749 		/* Always true for mutable buffers */
1750 		EVUTIL_ASSERT(chain->misalign >= 0 &&
1751 		    (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX);
1752 		remain = chain->buffer_len - (size_t)chain->misalign - chain->off;
1753 		if (remain >= datlen) {
1754 			/* there's enough space to hold all the data in the
1755 			 * current last chain */
1756 			memcpy(chain->buffer + chain->misalign + chain->off,
1757 			    data, datlen);
1758 			chain->off += datlen;
1759 			buf->total_len += datlen;
1760 			buf->n_add_for_cb += datlen;
1761 			goto out;
1762 		} else if (!CHAIN_PINNED(chain) &&
1763 		    evbuffer_chain_should_realign(chain, datlen)) {
1764 			/* we can fit the data into the misalignment */
1765 			evbuffer_chain_align(chain);
1766 
1767 			memcpy(chain->buffer + chain->off, data, datlen);
1768 			chain->off += datlen;
1769 			buf->total_len += datlen;
1770 			buf->n_add_for_cb += datlen;
1771 			goto out;
1772 		}
1773 	} else {
1774 		/* we cannot write any data to the last chain */
1775 		remain = 0;
1776 	}
1777 
1778 	/* we need to add another chain */
1779 	to_alloc = chain->buffer_len;
1780 	if (to_alloc <= EVBUFFER_CHAIN_MAX_AUTO_SIZE/2)
1781 		to_alloc <<= 1;
1782 	if (datlen > to_alloc)
1783 		to_alloc = datlen;
1784 	tmp = evbuffer_chain_new(to_alloc);
1785 	if (tmp == NULL)
1786 		goto done;
1787 
1788 	if (remain) {
1789 		memcpy(chain->buffer + chain->misalign + chain->off,
1790 		    data, remain);
1791 		chain->off += remain;
1792 		buf->total_len += remain;
1793 		buf->n_add_for_cb += remain;
1794 	}
1795 
1796 	data += remain;
1797 	datlen -= remain;
1798 
1799 	memcpy(tmp->buffer, data, datlen);
1800 	tmp->off = datlen;
1801 	evbuffer_chain_insert(buf, tmp);
1802 	buf->n_add_for_cb += datlen;
1803 
1804 out:
1805 	evbuffer_invoke_callbacks_(buf);
1806 	result = 0;
1807 done:
1808 	EVBUFFER_UNLOCK(buf);
1809 	return result;
1810 }
1811 
1812 int
1813 evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
1814 {
1815 	struct evbuffer_chain *chain, *tmp;
1816 	int result = -1;
1817 
1818 	EVBUFFER_LOCK(buf);
1819 
1820 	if (buf->freeze_start) {
1821 		goto done;
1822 	}
1823 	if (datlen > EV_SIZE_MAX - buf->total_len) {
1824 		goto done;
1825 	}
1826 
1827 	chain = buf->first;
1828 
1829 	if (chain == NULL) {
1830 		chain = evbuffer_chain_new(datlen);
1831 		if (!chain)
1832 			goto done;
1833 		evbuffer_chain_insert(buf, chain);
1834 	}
1835 
1836 	/* we cannot touch immutable buffers */
1837 	if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1838 		/* Always true for mutable buffers */
1839 		EVUTIL_ASSERT(chain->misalign >= 0 &&
1840 		    (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX);
1841 
1842 		/* If this chain is empty, we can treat it as
1843 		 * 'empty at the beginning' rather than 'empty at the end' */
1844 		if (chain->off == 0)
1845 			chain->misalign = chain->buffer_len;
1846 
1847 		if ((size_t)chain->misalign >= datlen) {
1848 			/* we have enough space to fit everything */
1849 			memcpy(chain->buffer + chain->misalign - datlen,
1850 			    data, datlen);
1851 			chain->off += datlen;
1852 			chain->misalign -= datlen;
1853 			buf->total_len += datlen;
1854 			buf->n_add_for_cb += datlen;
1855 			goto out;
1856 		} else if (chain->misalign) {
1857 			/* we can only fit some of the data. */
1858 			memcpy(chain->buffer,
1859 			    (char*)data + datlen - chain->misalign,
1860 			    (size_t)chain->misalign);
1861 			chain->off += (size_t)chain->misalign;
1862 			buf->total_len += (size_t)chain->misalign;
1863 			buf->n_add_for_cb += (size_t)chain->misalign;
1864 			datlen -= (size_t)chain->misalign;
1865 			chain->misalign = 0;
1866 		}
1867 	}
1868 
1869 	/* we need to add another chain */
1870 	if ((tmp = evbuffer_chain_new(datlen)) == NULL)
1871 		goto done;
1872 	buf->first = tmp;
1873 	if (buf->last_with_datap == &buf->first)
1874 		buf->last_with_datap = &tmp->next;
1875 
1876 	tmp->next = chain;
1877 
1878 	tmp->off = datlen;
1879 	EVUTIL_ASSERT(datlen <= tmp->buffer_len);
1880 	tmp->misalign = tmp->buffer_len - datlen;
1881 
1882 	memcpy(tmp->buffer + tmp->misalign, data, datlen);
1883 	buf->total_len += datlen;
1884 	buf->n_add_for_cb += (size_t)chain->misalign;
1885 
1886 out:
1887 	evbuffer_invoke_callbacks_(buf);
1888 	result = 0;
1889 done:
1890 	EVBUFFER_UNLOCK(buf);
1891 	return result;
1892 }
1893 
1894 /** Helper: realigns the memory in chain->buffer so that misalign is 0. */
1895 static void
1896 evbuffer_chain_align(struct evbuffer_chain *chain)
1897 {
1898 	EVUTIL_ASSERT(!(chain->flags & EVBUFFER_IMMUTABLE));
1899 	EVUTIL_ASSERT(!(chain->flags & EVBUFFER_MEM_PINNED_ANY));
1900 	memmove(chain->buffer, chain->buffer + chain->misalign, chain->off);
1901 	chain->misalign = 0;
1902 }
1903 
1904 #define MAX_TO_COPY_IN_EXPAND 4096
1905 #define MAX_TO_REALIGN_IN_EXPAND 2048
1906 
1907 /** Helper: return true iff we should realign chain to fit datalen bytes of
1908     data in it. */
1909 static int
1910 evbuffer_chain_should_realign(struct evbuffer_chain *chain,
1911     size_t datlen)
1912 {
1913 	return chain->buffer_len - chain->off >= datlen &&
1914 	    (chain->off < chain->buffer_len / 2) &&
1915 	    (chain->off <= MAX_TO_REALIGN_IN_EXPAND);
1916 }
1917 
1918 /* Expands the available space in the event buffer to at least datlen, all in
1919  * a single chunk.  Return that chunk. */
1920 static struct evbuffer_chain *
1921 evbuffer_expand_singlechain(struct evbuffer *buf, size_t datlen)
1922 {
1923 	struct evbuffer_chain *chain, **chainp;
1924 	struct evbuffer_chain *result = NULL;
1925 	ASSERT_EVBUFFER_LOCKED(buf);
1926 
1927 	chainp = buf->last_with_datap;
1928 
1929 	/* XXX If *chainp is no longer writeable, but has enough space in its
1930 	 * misalign, this might be a bad idea: we could still use *chainp, not
1931 	 * (*chainp)->next. */
1932 	if (*chainp && CHAIN_SPACE_LEN(*chainp) == 0)
1933 		chainp = &(*chainp)->next;
1934 
1935 	/* 'chain' now points to the first chain with writable space (if any)
1936 	 * We will either use it, realign it, replace it, or resize it. */
1937 	chain = *chainp;
1938 
1939 	if (chain == NULL ||
1940 	    (chain->flags & (EVBUFFER_IMMUTABLE|EVBUFFER_MEM_PINNED_ANY))) {
1941 		/* We can't use the last_with_data chain at all.  Just add a
1942 		 * new one that's big enough. */
1943 		goto insert_new;
1944 	}
1945 
1946 	/* If we can fit all the data, then we don't have to do anything */
1947 	if (CHAIN_SPACE_LEN(chain) >= datlen) {
1948 		result = chain;
1949 		goto ok;
1950 	}
1951 
1952 	/* If the chain is completely empty, just replace it by adding a new
1953 	 * empty chain. */
1954 	if (chain->off == 0) {
1955 		goto insert_new;
1956 	}
1957 
1958 	/* If the misalignment plus the remaining space fulfills our data
1959 	 * needs, we could just force an alignment to happen.  Afterwards, we
1960 	 * have enough space.  But only do this if we're saving a lot of space
1961 	 * and not moving too much data.  Otherwise the space savings are
1962 	 * probably offset by the time lost in copying.
1963 	 */
1964 	if (evbuffer_chain_should_realign(chain, datlen)) {
1965 		evbuffer_chain_align(chain);
1966 		result = chain;
1967 		goto ok;
1968 	}
1969 
1970 	/* At this point, we can either resize the last chunk with space in
1971 	 * it, use the next chunk after it, or   If we add a new chunk, we waste
1972 	 * CHAIN_SPACE_LEN(chain) bytes in the former last chunk.  If we
1973 	 * resize, we have to copy chain->off bytes.
1974 	 */
1975 
1976 	/* Would expanding this chunk be affordable and worthwhile? */
1977 	if (CHAIN_SPACE_LEN(chain) < chain->buffer_len / 8 ||
1978 	    chain->off > MAX_TO_COPY_IN_EXPAND ||
1979 	    (datlen < EVBUFFER_CHAIN_MAX &&
1980 		EVBUFFER_CHAIN_MAX - datlen >= chain->off)) {
1981 		/* It's not worth resizing this chain. Can the next one be
1982 		 * used? */
1983 		if (chain->next && CHAIN_SPACE_LEN(chain->next) >= datlen) {
1984 			/* Yes, we can just use the next chain (which should
1985 			 * be empty. */
1986 			result = chain->next;
1987 			goto ok;
1988 		} else {
1989 			/* No; append a new chain (which will free all
1990 			 * terminal empty chains.) */
1991 			goto insert_new;
1992 		}
1993 	} else {
1994 		/* Okay, we're going to try to resize this chain: Not doing so
1995 		 * would waste at least 1/8 of its current allocation, and we
1996 		 * can do so without having to copy more than
1997 		 * MAX_TO_COPY_IN_EXPAND bytes. */
1998 		/* figure out how much space we need */
1999 		size_t length = chain->off + datlen;
2000 		struct evbuffer_chain *tmp = evbuffer_chain_new(length);
2001 		if (tmp == NULL)
2002 			goto err;
2003 
2004 		/* copy the data over that we had so far */
2005 		tmp->off = chain->off;
2006 		memcpy(tmp->buffer, chain->buffer + chain->misalign,
2007 		    chain->off);
2008 		/* fix up the list */
2009 		EVUTIL_ASSERT(*chainp == chain);
2010 		result = *chainp = tmp;
2011 
2012 		if (buf->last == chain)
2013 			buf->last = tmp;
2014 
2015 		tmp->next = chain->next;
2016 		evbuffer_chain_free(chain);
2017 		goto ok;
2018 	}
2019 
2020 insert_new:
2021 	result = evbuffer_chain_insert_new(buf, datlen);
2022 	if (!result)
2023 		goto err;
2024 ok:
2025 	EVUTIL_ASSERT(result);
2026 	EVUTIL_ASSERT(CHAIN_SPACE_LEN(result) >= datlen);
2027 err:
2028 	return result;
2029 }
2030 
2031 /* Make sure that datlen bytes are available for writing in the last n
2032  * chains.  Never copies or moves data. */
2033 int
2034 evbuffer_expand_fast_(struct evbuffer *buf, size_t datlen, int n)
2035 {
2036 	struct evbuffer_chain *chain = buf->last, *tmp, *next;
2037 	size_t avail;
2038 	int used;
2039 
2040 	ASSERT_EVBUFFER_LOCKED(buf);
2041 	EVUTIL_ASSERT(n >= 2);
2042 
2043 	if (chain == NULL || (chain->flags & EVBUFFER_IMMUTABLE)) {
2044 		/* There is no last chunk, or we can't touch the last chunk.
2045 		 * Just add a new chunk. */
2046 		chain = evbuffer_chain_new(datlen);
2047 		if (chain == NULL)
2048 			return (-1);
2049 
2050 		evbuffer_chain_insert(buf, chain);
2051 		return (0);
2052 	}
2053 
2054 	used = 0; /* number of chains we're using space in. */
2055 	avail = 0; /* how much space they have. */
2056 	/* How many bytes can we stick at the end of buffer as it is?  Iterate
2057 	 * over the chains at the end of the buffer, tring to see how much
2058 	 * space we have in the first n. */
2059 	for (chain = *buf->last_with_datap; chain; chain = chain->next) {
2060 		if (chain->off) {
2061 			size_t space = (size_t) CHAIN_SPACE_LEN(chain);
2062 			EVUTIL_ASSERT(chain == *buf->last_with_datap);
2063 			if (space) {
2064 				avail += space;
2065 				++used;
2066 			}
2067 		} else {
2068 			/* No data in chain; realign it. */
2069 			chain->misalign = 0;
2070 			avail += chain->buffer_len;
2071 			++used;
2072 		}
2073 		if (avail >= datlen) {
2074 			/* There is already enough space.  Just return */
2075 			return (0);
2076 		}
2077 		if (used == n)
2078 			break;
2079 	}
2080 
2081 	/* There wasn't enough space in the first n chains with space in
2082 	 * them. Either add a new chain with enough space, or replace all
2083 	 * empty chains with one that has enough space, depending on n. */
2084 	if (used < n) {
2085 		/* The loop ran off the end of the chains before it hit n
2086 		 * chains; we can add another. */
2087 		EVUTIL_ASSERT(chain == NULL);
2088 
2089 		tmp = evbuffer_chain_new(datlen - avail);
2090 		if (tmp == NULL)
2091 			return (-1);
2092 
2093 		buf->last->next = tmp;
2094 		buf->last = tmp;
2095 		/* (we would only set last_with_data if we added the first
2096 		 * chain. But if the buffer had no chains, we would have
2097 		 * just allocated a new chain earlier) */
2098 		return (0);
2099 	} else {
2100 		/* Nuke _all_ the empty chains. */
2101 		int rmv_all = 0; /* True iff we removed last_with_data. */
2102 		chain = *buf->last_with_datap;
2103 		if (!chain->off) {
2104 			EVUTIL_ASSERT(chain == buf->first);
2105 			rmv_all = 1;
2106 			avail = 0;
2107 		} else {
2108 			/* can't overflow, since only mutable chains have
2109 			 * huge misaligns. */
2110 			avail = (size_t) CHAIN_SPACE_LEN(chain);
2111 			chain = chain->next;
2112 		}
2113 
2114 
2115 		for (; chain; chain = next) {
2116 			next = chain->next;
2117 			EVUTIL_ASSERT(chain->off == 0);
2118 			evbuffer_chain_free(chain);
2119 		}
2120 		EVUTIL_ASSERT(datlen >= avail);
2121 		tmp = evbuffer_chain_new(datlen - avail);
2122 		if (tmp == NULL) {
2123 			if (rmv_all) {
2124 				ZERO_CHAIN(buf);
2125 			} else {
2126 				buf->last = *buf->last_with_datap;
2127 				(*buf->last_with_datap)->next = NULL;
2128 			}
2129 			return (-1);
2130 		}
2131 
2132 		if (rmv_all) {
2133 			buf->first = buf->last = tmp;
2134 			buf->last_with_datap = &buf->first;
2135 		} else {
2136 			(*buf->last_with_datap)->next = tmp;
2137 			buf->last = tmp;
2138 		}
2139 		return (0);
2140 	}
2141 }
2142 
2143 int
2144 evbuffer_expand(struct evbuffer *buf, size_t datlen)
2145 {
2146 	struct evbuffer_chain *chain;
2147 
2148 	EVBUFFER_LOCK(buf);
2149 	chain = evbuffer_expand_singlechain(buf, datlen);
2150 	EVBUFFER_UNLOCK(buf);
2151 	return chain ? 0 : -1;
2152 }
2153 
2154 /*
2155  * Reads data from a file descriptor into a buffer.
2156  */
2157 
2158 #if defined(EVENT__HAVE_SYS_UIO_H) || defined(_WIN32)
2159 #define USE_IOVEC_IMPL
2160 #endif
2161 
2162 #ifdef USE_IOVEC_IMPL
2163 
2164 #ifdef EVENT__HAVE_SYS_UIO_H
2165 /* number of iovec we use for writev, fragmentation is going to determine
2166  * how much we end up writing */
2167 
2168 #define DEFAULT_WRITE_IOVEC 128
2169 
2170 #if defined(UIO_MAXIOV) && UIO_MAXIOV < DEFAULT_WRITE_IOVEC
2171 #define NUM_WRITE_IOVEC UIO_MAXIOV
2172 #elif defined(IOV_MAX) && IOV_MAX < DEFAULT_WRITE_IOVEC
2173 #define NUM_WRITE_IOVEC IOV_MAX
2174 #else
2175 #define NUM_WRITE_IOVEC DEFAULT_WRITE_IOVEC
2176 #endif
2177 
2178 #define IOV_TYPE struct iovec
2179 #define IOV_PTR_FIELD iov_base
2180 #define IOV_LEN_FIELD iov_len
2181 #define IOV_LEN_TYPE size_t
2182 #else
2183 #define NUM_WRITE_IOVEC 16
2184 #define IOV_TYPE WSABUF
2185 #define IOV_PTR_FIELD buf
2186 #define IOV_LEN_FIELD len
2187 #define IOV_LEN_TYPE unsigned long
2188 #endif
2189 #endif
2190 #define NUM_READ_IOVEC 4
2191 
2192 #define EVBUFFER_MAX_READ	4096
2193 
2194 /** Helper function to figure out which space to use for reading data into
2195     an evbuffer.  Internal use only.
2196 
2197     @param buf The buffer to read into
2198     @param howmuch How much we want to read.
2199     @param vecs An array of two or more iovecs or WSABUFs.
2200     @param n_vecs_avail The length of vecs
2201     @param chainp A pointer to a variable to hold the first chain we're
2202       reading into.
2203     @param exact Boolean: if true, we do not provide more than 'howmuch'
2204       space in the vectors, even if more space is available.
2205     @return The number of buffers we're using.
2206  */
2207 int
2208 evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
2209     struct evbuffer_iovec *vecs, int n_vecs_avail,
2210     struct evbuffer_chain ***chainp, int exact)
2211 {
2212 	struct evbuffer_chain *chain;
2213 	struct evbuffer_chain **firstchainp;
2214 	size_t so_far;
2215 	int i;
2216 	ASSERT_EVBUFFER_LOCKED(buf);
2217 
2218 	if (howmuch < 0)
2219 		return -1;
2220 
2221 	so_far = 0;
2222 	/* Let firstchain be the first chain with any space on it */
2223 	firstchainp = buf->last_with_datap;
2224 	if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
2225 		firstchainp = &(*firstchainp)->next;
2226 	}
2227 
2228 	chain = *firstchainp;
2229 	for (i = 0; i < n_vecs_avail && so_far < (size_t)howmuch; ++i) {
2230 		size_t avail = (size_t) CHAIN_SPACE_LEN(chain);
2231 		if (avail > (howmuch - so_far) && exact)
2232 			avail = howmuch - so_far;
2233 		vecs[i].iov_base = CHAIN_SPACE_PTR(chain);
2234 		vecs[i].iov_len = avail;
2235 		so_far += avail;
2236 		chain = chain->next;
2237 	}
2238 
2239 	*chainp = firstchainp;
2240 	return i;
2241 }
2242 
2243 static int
2244 get_n_bytes_readable_on_socket(evutil_socket_t fd)
2245 {
2246 #if defined(FIONREAD) && defined(_WIN32)
2247 	unsigned long lng = EVBUFFER_MAX_READ;
2248 	if (ioctlsocket(fd, FIONREAD, &lng) < 0)
2249 		return -1;
2250 	/* Can overflow, but mostly harmlessly. XXXX */
2251 	return (int)lng;
2252 #elif defined(FIONREAD)
2253 	int n = EVBUFFER_MAX_READ;
2254 	if (ioctl(fd, FIONREAD, &n) < 0)
2255 		return -1;
2256 	return n;
2257 #else
2258 	return EVBUFFER_MAX_READ;
2259 #endif
2260 }
2261 
2262 /* TODO(niels): should this function return ev_ssize_t and take ev_ssize_t
2263  * as howmuch? */
2264 int
2265 evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
2266 {
2267 	struct evbuffer_chain **chainp;
2268 	int n;
2269 	int result;
2270 
2271 #ifdef USE_IOVEC_IMPL
2272 	int nvecs, i, remaining;
2273 #else
2274 	struct evbuffer_chain *chain;
2275 	unsigned char *p;
2276 #endif
2277 
2278 	EVBUFFER_LOCK(buf);
2279 
2280 	if (buf->freeze_end) {
2281 		result = -1;
2282 		goto done;
2283 	}
2284 
2285 	n = get_n_bytes_readable_on_socket(fd);
2286 	if (n <= 0 || n > EVBUFFER_MAX_READ)
2287 		n = EVBUFFER_MAX_READ;
2288 	if (howmuch < 0 || howmuch > n)
2289 		howmuch = n;
2290 
2291 #ifdef USE_IOVEC_IMPL
2292 	/* Since we can use iovecs, we're willing to use the last
2293 	 * NUM_READ_IOVEC chains. */
2294 	if (evbuffer_expand_fast_(buf, howmuch, NUM_READ_IOVEC) == -1) {
2295 		result = -1;
2296 		goto done;
2297 	} else {
2298 		IOV_TYPE vecs[NUM_READ_IOVEC];
2299 #ifdef EVBUFFER_IOVEC_IS_NATIVE_
2300 		nvecs = evbuffer_read_setup_vecs_(buf, howmuch, vecs,
2301 		    NUM_READ_IOVEC, &chainp, 1);
2302 #else
2303 		/* We aren't using the native struct iovec.  Therefore,
2304 		   we are on win32. */
2305 		struct evbuffer_iovec ev_vecs[NUM_READ_IOVEC];
2306 		nvecs = evbuffer_read_setup_vecs_(buf, howmuch, ev_vecs, 2,
2307 		    &chainp, 1);
2308 
2309 		for (i=0; i < nvecs; ++i)
2310 			WSABUF_FROM_EVBUFFER_IOV(&vecs[i], &ev_vecs[i]);
2311 #endif
2312 
2313 #ifdef _WIN32
2314 		{
2315 			DWORD bytesRead;
2316 			DWORD flags=0;
2317 			if (WSARecv(fd, vecs, nvecs, &bytesRead, &flags, NULL, NULL)) {
2318 				/* The read failed. It might be a close,
2319 				 * or it might be an error. */
2320 				if (WSAGetLastError() == WSAECONNABORTED)
2321 					n = 0;
2322 				else
2323 					n = -1;
2324 			} else
2325 				n = bytesRead;
2326 		}
2327 #else
2328 		n = readv(fd, vecs, nvecs);
2329 #endif
2330 	}
2331 
2332 #else /*!USE_IOVEC_IMPL*/
2333 	/* If we don't have FIONREAD, we might waste some space here */
2334 	/* XXX we _will_ waste some space here if there is any space left
2335 	 * over on buf->last. */
2336 	if ((chain = evbuffer_expand_singlechain(buf, howmuch)) == NULL) {
2337 		result = -1;
2338 		goto done;
2339 	}
2340 
2341 	/* We can append new data at this point */
2342 	p = chain->buffer + chain->misalign + chain->off;
2343 
2344 #ifndef _WIN32
2345 	n = read(fd, p, howmuch);
2346 #else
2347 	n = recv(fd, p, howmuch, 0);
2348 #endif
2349 #endif /* USE_IOVEC_IMPL */
2350 
2351 	if (n == -1) {
2352 		result = -1;
2353 		goto done;
2354 	}
2355 	if (n == 0) {
2356 		result = 0;
2357 		goto done;
2358 	}
2359 
2360 #ifdef USE_IOVEC_IMPL
2361 	remaining = n;
2362 	for (i=0; i < nvecs; ++i) {
2363 		/* can't overflow, since only mutable chains have
2364 		 * huge misaligns. */
2365 		size_t space = (size_t) CHAIN_SPACE_LEN(*chainp);
2366 		/* XXXX This is a kludge that can waste space in perverse
2367 		 * situations. */
2368 		if (space > EVBUFFER_CHAIN_MAX)
2369 			space = EVBUFFER_CHAIN_MAX;
2370 		if ((ev_ssize_t)space < remaining) {
2371 			(*chainp)->off += space;
2372 			remaining -= (int)space;
2373 		} else {
2374 			(*chainp)->off += remaining;
2375 			buf->last_with_datap = chainp;
2376 			break;
2377 		}
2378 		chainp = &(*chainp)->next;
2379 	}
2380 #else
2381 	chain->off += n;
2382 	advance_last_with_data(buf);
2383 #endif
2384 	buf->total_len += n;
2385 	buf->n_add_for_cb += n;
2386 
2387 	/* Tell someone about changes in this buffer */
2388 	evbuffer_invoke_callbacks_(buf);
2389 	result = n;
2390 done:
2391 	EVBUFFER_UNLOCK(buf);
2392 	return result;
2393 }
2394 
2395 #ifdef USE_IOVEC_IMPL
2396 static inline int
2397 evbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd,
2398     ev_ssize_t howmuch)
2399 {
2400 	IOV_TYPE iov[NUM_WRITE_IOVEC];
2401 	struct evbuffer_chain *chain = buffer->first;
2402 	int n, i = 0;
2403 
2404 	if (howmuch < 0)
2405 		return -1;
2406 
2407 	ASSERT_EVBUFFER_LOCKED(buffer);
2408 	/* XXX make this top out at some maximal data length?  if the
2409 	 * buffer has (say) 1MB in it, split over 128 chains, there's
2410 	 * no way it all gets written in one go. */
2411 	while (chain != NULL && i < NUM_WRITE_IOVEC && howmuch) {
2412 #ifdef USE_SENDFILE
2413 		/* we cannot write the file info via writev */
2414 		if (chain->flags & EVBUFFER_SENDFILE)
2415 			break;
2416 #endif
2417 		iov[i].IOV_PTR_FIELD = (void *) (chain->buffer + chain->misalign);
2418 		if ((size_t)howmuch >= chain->off) {
2419 			/* XXXcould be problematic when windows supports mmap*/
2420 			iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)chain->off;
2421 			howmuch -= chain->off;
2422 		} else {
2423 			/* XXXcould be problematic when windows supports mmap*/
2424 			iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)howmuch;
2425 			break;
2426 		}
2427 		chain = chain->next;
2428 	}
2429 	if (! i)
2430 		return 0;
2431 
2432 #ifdef _WIN32
2433 	{
2434 		DWORD bytesSent;
2435 		if (WSASend(fd, iov, i, &bytesSent, 0, NULL, NULL))
2436 			n = -1;
2437 		else
2438 			n = bytesSent;
2439 	}
2440 #else
2441 	n = writev(fd, iov, i);
2442 #endif
2443 	return (n);
2444 }
2445 #endif
2446 
2447 #ifdef USE_SENDFILE
2448 static inline int
2449 evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t dest_fd,
2450     ev_ssize_t howmuch)
2451 {
2452 	struct evbuffer_chain *chain = buffer->first;
2453 	struct evbuffer_chain_file_segment *info =
2454 	    EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment,
2455 		chain);
2456 	const int source_fd = info->segment->fd;
2457 #if defined(SENDFILE_IS_MACOSX) || defined(SENDFILE_IS_FREEBSD)
2458 	int res;
2459 	ev_off_t len = chain->off;
2460 #elif defined(SENDFILE_IS_LINUX) || defined(SENDFILE_IS_SOLARIS)
2461 	ev_ssize_t res;
2462 	ev_off_t offset = chain->misalign;
2463 #endif
2464 
2465 	ASSERT_EVBUFFER_LOCKED(buffer);
2466 
2467 #if defined(SENDFILE_IS_MACOSX)
2468 	res = sendfile(source_fd, dest_fd, chain->misalign, &len, NULL, 0);
2469 	if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2470 		return (-1);
2471 
2472 	return (len);
2473 #elif defined(SENDFILE_IS_FREEBSD)
2474 	res = sendfile(source_fd, dest_fd, chain->misalign, chain->off, NULL, &len, 0);
2475 	if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2476 		return (-1);
2477 
2478 	return (len);
2479 #elif defined(SENDFILE_IS_LINUX)
2480 	/* TODO(niels): implement splice */
2481 	res = sendfile(dest_fd, source_fd, &offset, chain->off);
2482 	if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2483 		/* if this is EAGAIN or EINTR return 0; otherwise, -1 */
2484 		return (0);
2485 	}
2486 	return (res);
2487 #elif defined(SENDFILE_IS_SOLARIS)
2488 	{
2489 		const off_t offset_orig = offset;
2490 		res = sendfile(dest_fd, source_fd, &offset, chain->off);
2491 		if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2492 			if (offset - offset_orig)
2493 				return offset - offset_orig;
2494 			/* if this is EAGAIN or EINTR and no bytes were
2495 			 * written, return 0 */
2496 			return (0);
2497 		}
2498 		return (res);
2499 	}
2500 #endif
2501 }
2502 #endif
2503 
2504 int
2505 evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
2506     ev_ssize_t howmuch)
2507 {
2508 	int n = -1;
2509 
2510 	EVBUFFER_LOCK(buffer);
2511 
2512 	if (buffer->freeze_start) {
2513 		goto done;
2514 	}
2515 
2516 	if (howmuch < 0 || (size_t)howmuch > buffer->total_len)
2517 		howmuch = buffer->total_len;
2518 
2519 	if (howmuch > 0) {
2520 #ifdef USE_SENDFILE
2521 		struct evbuffer_chain *chain = buffer->first;
2522 		if (chain != NULL && (chain->flags & EVBUFFER_SENDFILE))
2523 			n = evbuffer_write_sendfile(buffer, fd, howmuch);
2524 		else {
2525 #endif
2526 #ifdef USE_IOVEC_IMPL
2527 		n = evbuffer_write_iovec(buffer, fd, howmuch);
2528 #elif defined(_WIN32)
2529 		/* XXX(nickm) Don't disable this code until we know if
2530 		 * the WSARecv code above works. */
2531 		void *p = evbuffer_pullup(buffer, howmuch);
2532 		EVUTIL_ASSERT(p || !howmuch);
2533 		n = send(fd, p, howmuch, 0);
2534 #else
2535 		void *p = evbuffer_pullup(buffer, howmuch);
2536 		EVUTIL_ASSERT(p || !howmuch);
2537 		n = write(fd, p, howmuch);
2538 #endif
2539 #ifdef USE_SENDFILE
2540 		}
2541 #endif
2542 	}
2543 
2544 	if (n > 0)
2545 		evbuffer_drain(buffer, n);
2546 
2547 done:
2548 	EVBUFFER_UNLOCK(buffer);
2549 	return (n);
2550 }
2551 
2552 int
2553 evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd)
2554 {
2555 	return evbuffer_write_atmost(buffer, fd, -1);
2556 }
2557 
2558 unsigned char *
2559 evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len)
2560 {
2561 	unsigned char *search;
2562 	struct evbuffer_ptr ptr;
2563 
2564 	EVBUFFER_LOCK(buffer);
2565 
2566 	ptr = evbuffer_search(buffer, (const char *)what, len, NULL);
2567 	if (ptr.pos < 0) {
2568 		search = NULL;
2569 	} else {
2570 		search = evbuffer_pullup(buffer, ptr.pos + len);
2571 		if (search)
2572 			search += ptr.pos;
2573 	}
2574 	EVBUFFER_UNLOCK(buffer);
2575 	return search;
2576 }
2577 
2578 /* Subract <b>howfar</b> from the position of <b>pos</b> within
2579  * <b>buf</b>. Returns 0 on success, -1 on failure.
2580  *
2581  * This isn't exposed yet, because of potential inefficiency issues.
2582  * Maybe it should be. */
2583 static int
2584 evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
2585     size_t howfar)
2586 {
2587 	if (pos->pos < 0)
2588 		return -1;
2589 	if (howfar > (size_t)pos->pos)
2590 		return -1;
2591 	if (pos->internal_.chain && howfar <= pos->internal_.pos_in_chain) {
2592 		pos->internal_.pos_in_chain -= howfar;
2593 		pos->pos -= howfar;
2594 		return 0;
2595 	} else {
2596 		const size_t newpos = pos->pos - howfar;
2597 		/* Here's the inefficient part: it walks over the
2598 		 * chains until we hit newpos. */
2599 		return evbuffer_ptr_set(buf, pos, newpos, EVBUFFER_PTR_SET);
2600 	}
2601 }
2602 
2603 int
2604 evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
2605     size_t position, enum evbuffer_ptr_how how)
2606 {
2607 	size_t left = position;
2608 	struct evbuffer_chain *chain = NULL;
2609 	int result = 0;
2610 
2611 	EVBUFFER_LOCK(buf);
2612 
2613 	switch (how) {
2614 	case EVBUFFER_PTR_SET:
2615 		chain = buf->first;
2616 		pos->pos = position;
2617 		position = 0;
2618 		break;
2619 	case EVBUFFER_PTR_ADD:
2620 		/* this avoids iterating over all previous chains if
2621 		   we just want to advance the position */
2622 		if (pos->pos < 0 || EV_SIZE_MAX - position < (size_t)pos->pos) {
2623 			EVBUFFER_UNLOCK(buf);
2624 			return -1;
2625 		}
2626 		chain = pos->internal_.chain;
2627 		pos->pos += position;
2628 		position = pos->internal_.pos_in_chain;
2629 		break;
2630 	}
2631 
2632 	EVUTIL_ASSERT(EV_SIZE_MAX - left >= position);
2633 	while (chain && position + left >= chain->off) {
2634 		left -= chain->off - position;
2635 		chain = chain->next;
2636 		position = 0;
2637 	}
2638 	if (chain) {
2639 		pos->internal_.chain = chain;
2640 		pos->internal_.pos_in_chain = position + left;
2641 	} else if (left == 0) {
2642 		/* The first byte in the (nonexistent) chain after the last chain */
2643 		pos->internal_.chain = NULL;
2644 		pos->internal_.pos_in_chain = 0;
2645 	} else {
2646 		PTR_NOT_FOUND(pos);
2647 		result = -1;
2648 	}
2649 
2650 	EVBUFFER_UNLOCK(buf);
2651 
2652 	return result;
2653 }
2654 
2655 /**
2656    Compare the bytes in buf at position pos to the len bytes in mem.  Return
2657    less than 0, 0, or greater than 0 as memcmp.
2658  */
2659 static int
2660 evbuffer_ptr_memcmp(const struct evbuffer *buf, const struct evbuffer_ptr *pos,
2661     const char *mem, size_t len)
2662 {
2663 	struct evbuffer_chain *chain;
2664 	size_t position;
2665 	int r;
2666 
2667 	ASSERT_EVBUFFER_LOCKED(buf);
2668 
2669 	if (pos->pos < 0 ||
2670 	    EV_SIZE_MAX - len < (size_t)pos->pos ||
2671 	    pos->pos + len > buf->total_len)
2672 		return -1;
2673 
2674 	chain = pos->internal_.chain;
2675 	position = pos->internal_.pos_in_chain;
2676 	while (len && chain) {
2677 		size_t n_comparable;
2678 		if (len + position > chain->off)
2679 			n_comparable = chain->off - position;
2680 		else
2681 			n_comparable = len;
2682 		r = memcmp(chain->buffer + chain->misalign + position, mem,
2683 		    n_comparable);
2684 		if (r)
2685 			return r;
2686 		mem += n_comparable;
2687 		len -= n_comparable;
2688 		position = 0;
2689 		chain = chain->next;
2690 	}
2691 
2692 	return 0;
2693 }
2694 
2695 struct evbuffer_ptr
2696 evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start)
2697 {
2698 	return evbuffer_search_range(buffer, what, len, start, NULL);
2699 }
2700 
2701 struct evbuffer_ptr
2702 evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end)
2703 {
2704 	struct evbuffer_ptr pos;
2705 	struct evbuffer_chain *chain, *last_chain = NULL;
2706 	const unsigned char *p;
2707 	char first;
2708 
2709 	EVBUFFER_LOCK(buffer);
2710 
2711 	if (start) {
2712 		memcpy(&pos, start, sizeof(pos));
2713 		chain = pos.internal_.chain;
2714 	} else {
2715 		pos.pos = 0;
2716 		chain = pos.internal_.chain = buffer->first;
2717 		pos.internal_.pos_in_chain = 0;
2718 	}
2719 
2720 	if (end)
2721 		last_chain = end->internal_.chain;
2722 
2723 	if (!len || len > EV_SSIZE_MAX)
2724 		goto done;
2725 
2726 	first = what[0];
2727 
2728 	while (chain) {
2729 		const unsigned char *start_at =
2730 		    chain->buffer + chain->misalign +
2731 		    pos.internal_.pos_in_chain;
2732 		p = memchr(start_at, first,
2733 		    chain->off - pos.internal_.pos_in_chain);
2734 		if (p) {
2735 			pos.pos += p - start_at;
2736 			pos.internal_.pos_in_chain += p - start_at;
2737 			if (!evbuffer_ptr_memcmp(buffer, &pos, what, len)) {
2738 				if (end && pos.pos + (ev_ssize_t)len > end->pos)
2739 					goto not_found;
2740 				else
2741 					goto done;
2742 			}
2743 			++pos.pos;
2744 			++pos.internal_.pos_in_chain;
2745 			if (pos.internal_.pos_in_chain == chain->off) {
2746 				chain = pos.internal_.chain = chain->next;
2747 				pos.internal_.pos_in_chain = 0;
2748 			}
2749 		} else {
2750 			if (chain == last_chain)
2751 				goto not_found;
2752 			pos.pos += chain->off - pos.internal_.pos_in_chain;
2753 			chain = pos.internal_.chain = chain->next;
2754 			pos.internal_.pos_in_chain = 0;
2755 		}
2756 	}
2757 
2758 not_found:
2759 	PTR_NOT_FOUND(&pos);
2760 done:
2761 	EVBUFFER_UNLOCK(buffer);
2762 	return pos;
2763 }
2764 
2765 int
2766 evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
2767     struct evbuffer_ptr *start_at,
2768     struct evbuffer_iovec *vec, int n_vec)
2769 {
2770 	struct evbuffer_chain *chain;
2771 	int idx = 0;
2772 	ev_ssize_t len_so_far = 0;
2773 
2774 	/* Avoid locking in trivial edge cases */
2775 	if (start_at && start_at->internal_.chain == NULL)
2776 		return 0;
2777 
2778 	EVBUFFER_LOCK(buffer);
2779 
2780 	if (start_at) {
2781 		chain = start_at->internal_.chain;
2782 		len_so_far = chain->off
2783 		    - start_at->internal_.pos_in_chain;
2784 		idx = 1;
2785 		if (n_vec > 0) {
2786 			vec[0].iov_base = chain->buffer + chain->misalign
2787 			    + start_at->internal_.pos_in_chain;
2788 			vec[0].iov_len = len_so_far;
2789 		}
2790 		chain = chain->next;
2791 	} else {
2792 		chain = buffer->first;
2793 	}
2794 
2795 	if (n_vec == 0 && len < 0) {
2796 		/* If no vectors are provided and they asked for "everything",
2797 		 * pretend they asked for the actual available amount. */
2798 		len = buffer->total_len;
2799 		if (start_at) {
2800 			len -= start_at->pos;
2801 		}
2802 	}
2803 
2804 	while (chain) {
2805 		if (len >= 0 && len_so_far >= len)
2806 			break;
2807 		if (idx<n_vec) {
2808 			vec[idx].iov_base = chain->buffer + chain->misalign;
2809 			vec[idx].iov_len = chain->off;
2810 		} else if (len<0) {
2811 			break;
2812 		}
2813 		++idx;
2814 		len_so_far += chain->off;
2815 		chain = chain->next;
2816 	}
2817 
2818 	EVBUFFER_UNLOCK(buffer);
2819 
2820 	return idx;
2821 }
2822 
2823 
2824 int
2825 evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
2826 {
2827 	char *buffer;
2828 	size_t space;
2829 	int sz, result = -1;
2830 	va_list aq;
2831 	struct evbuffer_chain *chain;
2832 
2833 
2834 	EVBUFFER_LOCK(buf);
2835 
2836 	if (buf->freeze_end) {
2837 		goto done;
2838 	}
2839 
2840 	/* make sure that at least some space is available */
2841 	if ((chain = evbuffer_expand_singlechain(buf, 64)) == NULL)
2842 		goto done;
2843 
2844 	for (;;) {
2845 #if 0
2846 		size_t used = chain->misalign + chain->off;
2847 		buffer = (char *)chain->buffer + chain->misalign + chain->off;
2848 		EVUTIL_ASSERT(chain->buffer_len >= used);
2849 		space = chain->buffer_len - used;
2850 #endif
2851 		buffer = (char*) CHAIN_SPACE_PTR(chain);
2852 		space = (size_t) CHAIN_SPACE_LEN(chain);
2853 
2854 #ifndef va_copy
2855 #define	va_copy(dst, src)	memcpy(&(dst), &(src), sizeof(va_list))
2856 #endif
2857 		va_copy(aq, ap);
2858 
2859 		sz = evutil_vsnprintf(buffer, space, fmt, aq);
2860 
2861 		va_end(aq);
2862 
2863 		if (sz < 0)
2864 			goto done;
2865 		if (INT_MAX >= EVBUFFER_CHAIN_MAX &&
2866 		    (size_t)sz >= EVBUFFER_CHAIN_MAX)
2867 			goto done;
2868 		if ((size_t)sz < space) {
2869 			chain->off += sz;
2870 			buf->total_len += sz;
2871 			buf->n_add_for_cb += sz;
2872 
2873 			advance_last_with_data(buf);
2874 			evbuffer_invoke_callbacks_(buf);
2875 			result = sz;
2876 			goto done;
2877 		}
2878 		if ((chain = evbuffer_expand_singlechain(buf, sz + 1)) == NULL)
2879 			goto done;
2880 	}
2881 	/* NOTREACHED */
2882 
2883 done:
2884 	EVBUFFER_UNLOCK(buf);
2885 	return result;
2886 }
2887 
2888 int
2889 evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
2890 {
2891 	int res = -1;
2892 	va_list ap;
2893 
2894 	va_start(ap, fmt);
2895 	res = evbuffer_add_vprintf(buf, fmt, ap);
2896 	va_end(ap);
2897 
2898 	return (res);
2899 }
2900 
2901 int
2902 evbuffer_add_reference(struct evbuffer *outbuf,
2903     const void *data, size_t datlen,
2904     evbuffer_ref_cleanup_cb cleanupfn, void *extra)
2905 {
2906 	struct evbuffer_chain *chain;
2907 	struct evbuffer_chain_reference *info;
2908 	int result = -1;
2909 
2910 	chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_reference));
2911 	if (!chain)
2912 		return (-1);
2913 	chain->flags |= EVBUFFER_REFERENCE | EVBUFFER_IMMUTABLE;
2914 	chain->buffer = (u_char *)data;
2915 	chain->buffer_len = datlen;
2916 	chain->off = datlen;
2917 
2918 	info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_reference, chain);
2919 	info->cleanupfn = cleanupfn;
2920 	info->extra = extra;
2921 
2922 	EVBUFFER_LOCK(outbuf);
2923 	if (outbuf->freeze_end) {
2924 		/* don't call chain_free; we do not want to actually invoke
2925 		 * the cleanup function */
2926 		mm_free(chain);
2927 		goto done;
2928 	}
2929 	evbuffer_chain_insert(outbuf, chain);
2930 	outbuf->n_add_for_cb += datlen;
2931 
2932 	evbuffer_invoke_callbacks_(outbuf);
2933 
2934 	result = 0;
2935 done:
2936 	EVBUFFER_UNLOCK(outbuf);
2937 
2938 	return result;
2939 }
2940 
2941 /* TODO(niels): we may want to add to automagically convert to mmap, in
2942  * case evbuffer_remove() or evbuffer_pullup() are being used.
2943  */
2944 struct evbuffer_file_segment *
2945 evbuffer_file_segment_new(
2946 	int fd, ev_off_t offset, ev_off_t length, unsigned flags)
2947 {
2948 	struct evbuffer_file_segment *seg =
2949 	    mm_calloc(sizeof(struct evbuffer_file_segment), 1);
2950 	if (!seg)
2951 		return NULL;
2952 	seg->refcnt = 1;
2953 	seg->fd = fd;
2954 	seg->flags = flags;
2955 	seg->file_offset = offset;
2956 	seg->cleanup_cb = NULL;
2957 	seg->cleanup_cb_arg = NULL;
2958 #ifdef _WIN32
2959 #ifndef lseek
2960 #define lseek _lseeki64
2961 #endif
2962 #ifndef fstat
2963 #define fstat _fstat
2964 #endif
2965 #ifndef stat
2966 #define stat _stat
2967 #endif
2968 #endif
2969 	if (length == -1) {
2970 		struct stat st;
2971 		if (fstat(fd, &st) < 0)
2972 			goto err;
2973 		length = st.st_size;
2974 	}
2975 	seg->length = length;
2976 
2977 	if (offset < 0 || length < 0 ||
2978 	    ((ev_uint64_t)length > EVBUFFER_CHAIN_MAX) ||
2979 	    (ev_uint64_t)offset > (ev_uint64_t)(EVBUFFER_CHAIN_MAX - length))
2980 		goto err;
2981 
2982 #if defined(USE_SENDFILE)
2983 	if (!(flags & EVBUF_FS_DISABLE_SENDFILE)) {
2984 		seg->can_sendfile = 1;
2985 		goto done;
2986 	}
2987 #endif
2988 
2989 	if (evbuffer_file_segment_materialize(seg)<0)
2990 		goto err;
2991 
2992 #if defined(USE_SENDFILE)
2993 done:
2994 #endif
2995 	if (!(flags & EVBUF_FS_DISABLE_LOCKING)) {
2996 		EVTHREAD_ALLOC_LOCK(seg->lock, 0);
2997 	}
2998 	return seg;
2999 err:
3000 	mm_free(seg);
3001 	return NULL;
3002 }
3003 
3004 #ifdef EVENT__HAVE_MMAP
3005 static long
3006 get_page_size(void)
3007 {
3008 #ifdef SC_PAGE_SIZE
3009 	return sysconf(SC_PAGE_SIZE);
3010 #elif defined(_SC_PAGE_SIZE)
3011 	return sysconf(_SC_PAGE_SIZE);
3012 #else
3013 	return 1;
3014 #endif
3015 }
3016 #endif
3017 
3018 /* DOCDOC */
3019 /* Requires lock */
3020 static int
3021 evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg)
3022 {
3023 	const unsigned flags = seg->flags;
3024 	const int fd = seg->fd;
3025 	const ev_off_t length = seg->length;
3026 	const ev_off_t offset = seg->file_offset;
3027 
3028 	if (seg->contents)
3029 		return 0; /* already materialized */
3030 
3031 #if defined(EVENT__HAVE_MMAP)
3032 	if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
3033 		off_t offset_rounded = 0, offset_leftover = 0;
3034 		void *mapped;
3035 		if (offset) {
3036 			/* mmap implementations don't generally like us
3037 			 * to have an offset that isn't a round  */
3038 			long page_size = get_page_size();
3039 			if (page_size == -1)
3040 				goto err;
3041 			offset_leftover = offset % page_size;
3042 			offset_rounded = offset - offset_leftover;
3043 		}
3044 		mapped = mmap(NULL, length + offset_leftover,
3045 		    PROT_READ,
3046 #ifdef MAP_NOCACHE
3047 		    MAP_NOCACHE | /* ??? */
3048 #endif
3049 #ifdef MAP_FILE
3050 		    MAP_FILE |
3051 #endif
3052 		    MAP_PRIVATE,
3053 		    fd, offset_rounded);
3054 		if (mapped == MAP_FAILED) {
3055 			event_warn("%s: mmap(%d, %d, %zu) failed",
3056 			    __func__, fd, 0, (size_t)(offset + length));
3057 		} else {
3058 			seg->mapping = mapped;
3059 			seg->contents = (char*)mapped+offset_leftover;
3060 			seg->mmap_offset = 0;
3061 			seg->is_mapping = 1;
3062 			goto done;
3063 		}
3064 	}
3065 #endif
3066 #ifdef _WIN32
3067 	if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
3068 		intptr_t h = _get_osfhandle(fd);
3069 		HANDLE m;
3070 		ev_uint64_t total_size = length+offset;
3071 		if ((HANDLE)h == INVALID_HANDLE_VALUE)
3072 			goto err;
3073 		m = CreateFileMapping((HANDLE)h, NULL, PAGE_READONLY,
3074 		    (total_size >> 32), total_size & 0xfffffffful,
3075 		    NULL);
3076 		if (m != INVALID_HANDLE_VALUE) { /* Does h leak? */
3077 			seg->mapping_handle = m;
3078 			seg->mmap_offset = offset;
3079 			seg->is_mapping = 1;
3080 			goto done;
3081 		}
3082 	}
3083 #endif
3084 	{
3085 		ev_off_t start_pos = lseek(fd, 0, SEEK_CUR), pos;
3086 		ev_off_t read_so_far = 0;
3087 		char *mem;
3088 		int e;
3089 		ev_ssize_t n = 0;
3090 		if (!(mem = mm_malloc(length)))
3091 			goto err;
3092 		if (start_pos < 0) {
3093 			mm_free(mem);
3094 			goto err;
3095 		}
3096 		if (lseek(fd, offset, SEEK_SET) < 0) {
3097 			mm_free(mem);
3098 			goto err;
3099 		}
3100 		while (read_so_far < length) {
3101 			n = read(fd, mem+read_so_far, length-read_so_far);
3102 			if (n <= 0)
3103 				break;
3104 			read_so_far += n;
3105 		}
3106 
3107 		e = errno;
3108 		pos = lseek(fd, start_pos, SEEK_SET);
3109 		if (n < 0 || (n == 0 && length > read_so_far)) {
3110 			mm_free(mem);
3111 			errno = e;
3112 			goto err;
3113 		} else if (pos < 0) {
3114 			mm_free(mem);
3115 			goto err;
3116 		}
3117 
3118 		seg->contents = mem;
3119 	}
3120 
3121 done:
3122 	return 0;
3123 err:
3124 	return -1;
3125 }
3126 
3127 void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
3128 	evbuffer_file_segment_cleanup_cb cb, void* arg)
3129 {
3130 	EVUTIL_ASSERT(seg->refcnt > 0);
3131 	seg->cleanup_cb = cb;
3132 	seg->cleanup_cb_arg = arg;
3133 }
3134 
3135 void
3136 evbuffer_file_segment_free(struct evbuffer_file_segment *seg)
3137 {
3138 	int refcnt;
3139 	EVLOCK_LOCK(seg->lock, 0);
3140 	refcnt = --seg->refcnt;
3141 	EVLOCK_UNLOCK(seg->lock, 0);
3142 	if (refcnt > 0)
3143 		return;
3144 	EVUTIL_ASSERT(refcnt == 0);
3145 
3146 	if (seg->is_mapping) {
3147 #ifdef _WIN32
3148 		CloseHandle(seg->mapping_handle);
3149 #elif defined (EVENT__HAVE_MMAP)
3150 		off_t offset_leftover;
3151 		offset_leftover = seg->file_offset % get_page_size();
3152 		if (munmap(seg->mapping, seg->length + offset_leftover) == -1)
3153 			event_warn("%s: munmap failed", __func__);
3154 #endif
3155 	} else if (seg->contents) {
3156 		mm_free(seg->contents);
3157 	}
3158 
3159 	if ((seg->flags & EVBUF_FS_CLOSE_ON_FREE) && seg->fd >= 0) {
3160 		close(seg->fd);
3161 	}
3162 
3163 	if (seg->cleanup_cb) {
3164 		(*seg->cleanup_cb)((struct evbuffer_file_segment const*)seg,
3165 		    seg->flags, seg->cleanup_cb_arg);
3166 		seg->cleanup_cb = NULL;
3167 		seg->cleanup_cb_arg = NULL;
3168 	}
3169 
3170 	EVTHREAD_FREE_LOCK(seg->lock, 0);
3171 	mm_free(seg);
3172 }
3173 
3174 int
3175 evbuffer_add_file_segment(struct evbuffer *buf,
3176     struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length)
3177 {
3178 	struct evbuffer_chain *chain;
3179 	struct evbuffer_chain_file_segment *extra;
3180 	int can_use_sendfile = 0;
3181 
3182 	EVBUFFER_LOCK(buf);
3183 	EVLOCK_LOCK(seg->lock, 0);
3184 	if (buf->flags & EVBUFFER_FLAG_DRAINS_TO_FD) {
3185 		can_use_sendfile = 1;
3186 	} else {
3187 		if (!seg->contents) {
3188 			if (evbuffer_file_segment_materialize(seg)<0) {
3189 				EVLOCK_UNLOCK(seg->lock, 0);
3190 				EVBUFFER_UNLOCK(buf);
3191 				return -1;
3192 			}
3193 		}
3194 	}
3195 	++seg->refcnt;
3196 	EVLOCK_UNLOCK(seg->lock, 0);
3197 
3198 	if (buf->freeze_end)
3199 		goto err;
3200 
3201 	if (length < 0) {
3202 		if (offset > seg->length)
3203 			goto err;
3204 		length = seg->length - offset;
3205 	}
3206 
3207 	/* Can we actually add this? */
3208 	if (offset+length > seg->length)
3209 		goto err;
3210 
3211 	chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_file_segment));
3212 	if (!chain)
3213 		goto err;
3214 	extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, chain);
3215 
3216 	chain->flags |= EVBUFFER_IMMUTABLE|EVBUFFER_FILESEGMENT;
3217 	if (can_use_sendfile && seg->can_sendfile) {
3218 		chain->flags |= EVBUFFER_SENDFILE;
3219 		chain->misalign = seg->file_offset + offset;
3220 		chain->off = length;
3221 		chain->buffer_len = chain->misalign + length;
3222 	} else if (seg->is_mapping) {
3223 #ifdef _WIN32
3224 		ev_uint64_t total_offset = seg->mmap_offset+offset;
3225 		ev_uint64_t offset_rounded=0, offset_remaining=0;
3226 		LPVOID data;
3227 		if (total_offset) {
3228 			SYSTEM_INFO si;
3229 			memset(&si, 0, sizeof(si)); /* cargo cult */
3230 			GetSystemInfo(&si);
3231 			offset_remaining = total_offset % si.dwAllocationGranularity;
3232 			offset_rounded = total_offset - offset_remaining;
3233 		}
3234 		data = MapViewOfFile(
3235 			seg->mapping_handle,
3236 			FILE_MAP_READ,
3237 			offset_rounded >> 32,
3238 			offset_rounded & 0xfffffffful,
3239 			length + offset_remaining);
3240 		if (data == NULL) {
3241 			mm_free(chain);
3242 			goto err;
3243 		}
3244 		chain->buffer = (unsigned char*) data;
3245 		chain->buffer_len = length+offset_remaining;
3246 		chain->misalign = offset_remaining;
3247 		chain->off = length;
3248 #else
3249 		chain->buffer = (unsigned char*)(seg->contents + offset);
3250 		chain->buffer_len = length;
3251 		chain->off = length;
3252 #endif
3253 	} else {
3254 		chain->buffer = (unsigned char*)(seg->contents + offset);
3255 		chain->buffer_len = length;
3256 		chain->off = length;
3257 	}
3258 
3259 	extra->segment = seg;
3260 	buf->n_add_for_cb += length;
3261 	evbuffer_chain_insert(buf, chain);
3262 
3263 	evbuffer_invoke_callbacks_(buf);
3264 
3265 	EVBUFFER_UNLOCK(buf);
3266 
3267 	return 0;
3268 err:
3269 	EVBUFFER_UNLOCK(buf);
3270 	evbuffer_file_segment_free(seg); /* Lowers the refcount */
3271 	return -1;
3272 }
3273 
3274 int
3275 evbuffer_add_file(struct evbuffer *buf, int fd, ev_off_t offset, ev_off_t length)
3276 {
3277 	struct evbuffer_file_segment *seg;
3278 	unsigned flags = EVBUF_FS_CLOSE_ON_FREE;
3279 	int r;
3280 
3281 	seg = evbuffer_file_segment_new(fd, offset, length, flags);
3282 	if (!seg)
3283 		return -1;
3284 	r = evbuffer_add_file_segment(buf, seg, 0, length);
3285 	if (r == 0)
3286 		evbuffer_file_segment_free(seg);
3287 	return r;
3288 }
3289 
3290 void
3291 evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
3292 {
3293 	EVBUFFER_LOCK(buffer);
3294 
3295 	if (!LIST_EMPTY(&buffer->callbacks))
3296 		evbuffer_remove_all_callbacks(buffer);
3297 
3298 	if (cb) {
3299 		struct evbuffer_cb_entry *ent =
3300 		    evbuffer_add_cb(buffer, NULL, cbarg);
3301 		ent->cb.cb_obsolete = cb;
3302 		ent->flags |= EVBUFFER_CB_OBSOLETE;
3303 	}
3304 	EVBUFFER_UNLOCK(buffer);
3305 }
3306 
3307 struct evbuffer_cb_entry *
3308 evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3309 {
3310 	struct evbuffer_cb_entry *e;
3311 	if (! (e = mm_calloc(1, sizeof(struct evbuffer_cb_entry))))
3312 		return NULL;
3313 	EVBUFFER_LOCK(buffer);
3314 	e->cb.cb_func = cb;
3315 	e->cbarg = cbarg;
3316 	e->flags = EVBUFFER_CB_ENABLED;
3317 	LIST_INSERT_HEAD(&buffer->callbacks, e, next);
3318 	EVBUFFER_UNLOCK(buffer);
3319 	return e;
3320 }
3321 
3322 int
3323 evbuffer_remove_cb_entry(struct evbuffer *buffer,
3324 			 struct evbuffer_cb_entry *ent)
3325 {
3326 	EVBUFFER_LOCK(buffer);
3327 	LIST_REMOVE(ent, next);
3328 	EVBUFFER_UNLOCK(buffer);
3329 	mm_free(ent);
3330 	return 0;
3331 }
3332 
3333 int
3334 evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3335 {
3336 	struct evbuffer_cb_entry *cbent;
3337 	int result = -1;
3338 	EVBUFFER_LOCK(buffer);
3339 	LIST_FOREACH(cbent, &buffer->callbacks, next) {
3340 		if (cb == cbent->cb.cb_func && cbarg == cbent->cbarg) {
3341 			result = evbuffer_remove_cb_entry(buffer, cbent);
3342 			goto done;
3343 		}
3344 	}
3345 done:
3346 	EVBUFFER_UNLOCK(buffer);
3347 	return result;
3348 }
3349 
3350 int
3351 evbuffer_cb_set_flags(struct evbuffer *buffer,
3352 		      struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3353 {
3354 	/* the user isn't allowed to mess with these. */
3355 	flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3356 	EVBUFFER_LOCK(buffer);
3357 	cb->flags |= flags;
3358 	EVBUFFER_UNLOCK(buffer);
3359 	return 0;
3360 }
3361 
3362 int
3363 evbuffer_cb_clear_flags(struct evbuffer *buffer,
3364 		      struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3365 {
3366 	/* the user isn't allowed to mess with these. */
3367 	flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3368 	EVBUFFER_LOCK(buffer);
3369 	cb->flags &= ~flags;
3370 	EVBUFFER_UNLOCK(buffer);
3371 	return 0;
3372 }
3373 
3374 int
3375 evbuffer_freeze(struct evbuffer *buffer, int start)
3376 {
3377 	EVBUFFER_LOCK(buffer);
3378 	if (start)
3379 		buffer->freeze_start = 1;
3380 	else
3381 		buffer->freeze_end = 1;
3382 	EVBUFFER_UNLOCK(buffer);
3383 	return 0;
3384 }
3385 
3386 int
3387 evbuffer_unfreeze(struct evbuffer *buffer, int start)
3388 {
3389 	EVBUFFER_LOCK(buffer);
3390 	if (start)
3391 		buffer->freeze_start = 0;
3392 	else
3393 		buffer->freeze_end = 0;
3394 	EVBUFFER_UNLOCK(buffer);
3395 	return 0;
3396 }
3397 
3398 #if 0
3399 void
3400 evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3401 {
3402 	if (!(cb->flags & EVBUFFER_CB_SUSPENDED)) {
3403 		cb->size_before_suspend = evbuffer_get_length(buffer);
3404 		cb->flags |= EVBUFFER_CB_SUSPENDED;
3405 	}
3406 }
3407 
3408 void
3409 evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3410 {
3411 	if ((cb->flags & EVBUFFER_CB_SUSPENDED)) {
3412 		unsigned call = (cb->flags & EVBUFFER_CB_CALL_ON_UNSUSPEND);
3413 		size_t sz = cb->size_before_suspend;
3414 		cb->flags &= ~(EVBUFFER_CB_SUSPENDED|
3415 			       EVBUFFER_CB_CALL_ON_UNSUSPEND);
3416 		cb->size_before_suspend = 0;
3417 		if (call && (cb->flags & EVBUFFER_CB_ENABLED)) {
3418 			cb->cb(buffer, sz, evbuffer_get_length(buffer), cb->cbarg);
3419 		}
3420 	}
3421 }
3422 #endif
3423 
3424 int
3425 evbuffer_get_callbacks_(struct evbuffer *buffer, struct event_callback **cbs,
3426     int max_cbs)
3427 {
3428 	int r = 0;
3429 	EVBUFFER_LOCK(buffer);
3430 	if (buffer->deferred_cbs) {
3431 		if (max_cbs < 1) {
3432 			r = -1;
3433 			goto done;
3434 		}
3435 		cbs[0] = &buffer->deferred;
3436 		r = 1;
3437 	}
3438 done:
3439 	EVBUFFER_UNLOCK(buffer);
3440 	return r;
3441 }
3442