42 #include <sys/types.h>
44 #ifdef HAVE_SYS_TIME_H
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
73 buffer = calloc(1,
sizeof(
struct evbuffer));
81 if (buffer->orig_buffer != NULL)
82 free(buffer->orig_buffer);
91 #define SWAP(x,y) do { \
92 (x)->buffer = (y)->buffer; \
93 (x)->orig_buffer = (y)->orig_buffer; \
94 (x)->misalign = (y)->misalign; \
95 (x)->totallen = (y)->totallen; \
96 (x)->off = (y)->off; \
105 if (outbuf->off == 0) {
107 size_t oldoff = inbuf->off;
119 if (inbuf->off != oldoff && inbuf->cb != NULL)
120 (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg);
121 if (oldoff && outbuf->cb != NULL)
122 (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
127 res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
130 evbuffer_drain(inbuf, inbuf->off);
141 size_t oldoff = buf->off;
146 evbuffer_expand(buf, 64);
148 size_t used = buf->misalign + buf->off;
149 buffer = (
char *)buf->buffer + buf->off;
150 assert(buf->totallen >= used);
151 space = buf->totallen - used;
154 #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list))
158 sz = evutil_vsnprintf(buffer, space, fmt, aq);
164 if ((
size_t)sz < space) {
167 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
170 if (evbuffer_expand(buf, sz + 1) == -1)
184 res = evbuffer_add_vprintf(buf, fmt, ap);
195 size_t nread = datlen;
196 if (nread >= buf->off)
199 memcpy(data, buf->buffer, nread);
200 evbuffer_drain(buf, nread);
213 u_char *data = EVBUFFER_DATA(buffer);
214 size_t len = EVBUFFER_LENGTH(buffer);
218 for (i = 0; i < len; i++) {
219 if (data[i] ==
'\r' || data[i] ==
'\n')
226 if ((line = malloc(i + 1)) == NULL) {
227 fprintf(stderr,
"%s: out of memory\n", __func__);
228 evbuffer_drain(buffer, i);
232 memcpy(line, data, i);
240 char fch = data[
i], sch = data[i+1];
243 if ( (sch ==
'\r' || sch ==
'\n') && sch != fch )
247 evbuffer_drain(buffer, i + 1);
257 memmove(buf->orig_buffer, buf->buffer, buf->off);
258 buf->buffer = buf->orig_buffer;
267 size_t need = buf->misalign + buf->off + datlen;
270 if (buf->totallen >= need)
277 if (buf->misalign >= datlen) {
281 size_t length = buf->totallen;
285 while (length < need)
288 if (buf->orig_buffer != buf->buffer)
290 if ((newbuf = realloc(buf->buffer, length)) == NULL)
293 buf->orig_buffer = buf->buffer = newbuf;
294 buf->totallen = length;
301 evbuffer_add(
struct evbuffer *
buf,
const void *data,
size_t datlen)
303 size_t need = buf->misalign + buf->off + datlen;
304 size_t oldoff = buf->off;
306 if (buf->totallen < need) {
307 if (evbuffer_expand(buf, datlen) == -1)
311 memcpy(buf->buffer + buf->off, data, datlen);
314 if (datlen && buf->cb != NULL)
315 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
323 size_t oldoff = buf->off;
325 if (len >= buf->off) {
327 buf->buffer = buf->orig_buffer;
333 buf->misalign += len;
339 if (buf->off != oldoff && buf->cb != NULL)
340 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
348 #define EVBUFFER_MAX_READ 4096
354 size_t oldoff = buf->off;
355 int n = EVBUFFER_MAX_READ;
357 #if defined(FIONREAD)
360 if (ioctlsocket(fd, FIONREAD, &lng) == -1 || (n=lng) == 0) {
362 if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
364 n = EVBUFFER_MAX_READ;
365 }
else if (n > EVBUFFER_MAX_READ && n > howmuch) {
373 if ((
size_t)n > buf->totallen << 2)
374 n = buf->totallen << 2;
375 if (n < EVBUFFER_MAX_READ)
376 n = EVBUFFER_MAX_READ;
379 if (howmuch < 0 || howmuch > n)
383 if (evbuffer_expand(buf, howmuch) == -1)
387 p = buf->buffer + buf->off;
390 n = read(fd, p, howmuch);
392 n = recv(fd, p, howmuch, 0);
402 if (buf->off != oldoff && buf->cb != NULL)
403 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
414 n = write(fd, buffer->buffer, buffer->off);
416 n = send(fd, buffer->buffer, buffer->off, 0);
422 evbuffer_drain(buffer, n);
428 evbuffer_find(
struct evbuffer *buffer,
const u_char *what,
size_t len)
430 u_char *search = buffer->buffer, *end = search + buffer->off;
433 while (search < end &&
434 (p = memchr(search, *what, end - search)) != NULL) {
437 if (memcmp(p, what, len) == 0)
446 void (*cb)(
struct evbuffer *,
size_t,
size_t,
void *),
450 buffer->cbarg = cbarg;