diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-04-21 20:02:41 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-04-21 20:02:41 +0000 |
commit | 483c556c961377c65bc37a43cba770bfb7f24db2 (patch) | |
tree | f12eebcedd009fa08292bf672d9230e65b401f59 /lib/libevent/buffer.c | |
parent | 478d2f807269d4a1dc2066ed302f9716adb8b3f9 (diff) |
Update libevent to 1.4.13.
This is the core library only, the DNS parts are removed and it does not
include the other extra bits (HTTP, DNS, and RPC), a separate port for
these will appear in due course.
Thanks to jsg, sthen, alek, gilles, jacekm, bernd and any others I've
forgotten for testing/comments.
Note that /usr/include/evdns.h should be removed after updating.
ok deraadt
Diffstat (limited to 'lib/libevent/buffer.c')
-rw-r--r-- | lib/libevent/buffer.c | 150 |
1 files changed, 112 insertions, 38 deletions
diff --git a/lib/libevent/buffer.c b/lib/libevent/buffer.c index 0f531f979ea..142d5b33a4f 100644 --- a/lib/libevent/buffer.c +++ b/lib/libevent/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.16 2009/10/13 12:16:33 jacekm Exp $ */ +/* $OpenBSD: buffer.c,v 1.17 2010/04/21 20:02:40 nicm Exp $ */ /* * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu> @@ -31,6 +31,11 @@ #include "config.h" #endif +#ifdef WIN32 +#include <winsock2.h> +#include <windows.h> +#endif + #ifdef HAVE_VASPRINTF /* If we have vasprintf, we need to define this before we include stdio.h. */ #define _GNU_SOURCE @@ -59,12 +64,13 @@ #endif #include "event.h" +#include "evutil.h" struct evbuffer * evbuffer_new(void) { struct evbuffer *buffer; - + buffer = calloc(1, sizeof(struct evbuffer)); return (buffer); @@ -78,7 +84,7 @@ evbuffer_free(struct evbuffer *buffer) free(buffer); } -/* +/* * This is a destructive add. The data from one buffer moves into * the other buffer. */ @@ -106,7 +112,7 @@ evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) SWAP(outbuf, inbuf); SWAP(inbuf, &tmp); - /* + /* * Optimization comes with a price; we need to notify the * buffer if necessary of the changes. oldoff is the amount * of data that we transferred from inbuf to outbuf @@ -115,7 +121,7 @@ evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg); if (oldoff && outbuf->cb != NULL) (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg); - + return (0); } @@ -150,18 +156,13 @@ evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap) #endif va_copy(aq, ap); -#ifdef WIN32 - sz = vsnprintf(buffer, space - 1, fmt, aq); - buffer[space - 1] = '\0'; -#else - sz = vsnprintf(buffer, space, fmt, aq); -#endif + sz = evutil_vsnprintf(buffer, space, fmt, aq); va_end(aq); if (sz < 0) return (-1); - if (sz < space) { + if ((size_t)sz < space) { buf->off += sz; if (buf->cb != NULL) (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); @@ -198,7 +199,7 @@ evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen) memcpy(data, buf->buffer, nread); evbuffer_drain(buf, nread); - + return (nread); } @@ -248,6 +249,90 @@ evbuffer_readline(struct evbuffer *buffer) return (line); } + +char * +evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, + enum evbuffer_eol_style eol_style) +{ + u_char *data = EVBUFFER_DATA(buffer); + u_char *start_of_eol, *end_of_eol; + size_t len = EVBUFFER_LENGTH(buffer); + char *line; + unsigned int i, n_to_copy, n_to_drain; + + /* depending on eol_style, set start_of_eol to the first character + * in the newline, and end_of_eol to one after the last character. */ + switch (eol_style) { + case EVBUFFER_EOL_ANY: + for (i = 0; i < len; i++) { + if (data[i] == '\r' || data[i] == '\n') + break; + } + if (i == len) + return (NULL); + start_of_eol = data+i; + ++i; + for ( ; i < len; i++) { + if (data[i] != '\r' && data[i] != '\n') + break; + } + end_of_eol = data+i; + break; + case EVBUFFER_EOL_CRLF: + end_of_eol = memchr(data, '\n', len); + if (!end_of_eol) + return (NULL); + if (end_of_eol > data && *(end_of_eol-1) == '\r') + start_of_eol = end_of_eol - 1; + else + start_of_eol = end_of_eol; + end_of_eol++; /*point to one after the LF. */ + break; + case EVBUFFER_EOL_CRLF_STRICT: { + u_char *cp = data; + while ((cp = memchr(cp, '\r', len-(cp-data)))) { + if (cp < data+len-1 && *(cp+1) == '\n') + break; + if (++cp >= data+len) { + cp = NULL; + break; + } + } + if (!cp) + return (NULL); + start_of_eol = cp; + end_of_eol = cp+2; + break; + } + case EVBUFFER_EOL_LF: + start_of_eol = memchr(data, '\n', len); + if (!start_of_eol) + return (NULL); + end_of_eol = start_of_eol + 1; + break; + default: + return (NULL); + } + + n_to_copy = start_of_eol - data; + n_to_drain = end_of_eol - data; + + if ((line = malloc(n_to_copy+1)) == NULL) { + fprintf(stderr, "%s: out of memory\n", __func__); + evbuffer_drain(buffer, n_to_drain); + return (NULL); + } + + memcpy(line, data, n_to_copy); + line[n_to_copy] = '\0'; + + evbuffer_drain(buffer, n_to_drain); + if (n_read_out) + *n_read_out = (size_t)n_to_copy; + + return (line); +} + /* Adds data to an event buffer */ static void @@ -352,12 +437,14 @@ evbuffer_read(struct evbuffer *buf, int fd, int howmuch) u_char *p; size_t oldoff = buf->off; int n = EVBUFFER_MAX_READ; + +#if defined(FIONREAD) #ifdef WIN32 - DWORD dwBytesRead; + long lng = n; + if (ioctlsocket(fd, FIONREAD, &lng) == -1 || (n=lng) <= 0) { +#else + if (ioctl(fd, FIONREAD, &n) == -1 || n <= 0) { #endif - -#ifdef FIONREAD - if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) { n = EVBUFFER_MAX_READ; } else if (n > EVBUFFER_MAX_READ && n > howmuch) { /* @@ -367,12 +454,12 @@ evbuffer_read(struct evbuffer *buf, int fd, int howmuch) * about it. If the reader does not tell us how much * data we should read, we artifically limit it. */ - if (n > buf->totallen << 2) + if ((size_t)n > buf->totallen << 2) n = buf->totallen << 2; if (n < EVBUFFER_MAX_READ) n = EVBUFFER_MAX_READ; } -#endif +#endif if (howmuch < 0 || howmuch > n) howmuch = n; @@ -385,18 +472,13 @@ evbuffer_read(struct evbuffer *buf, int fd, int howmuch) #ifndef WIN32 n = read(fd, p, howmuch); +#else + n = recv(fd, p, howmuch, 0); +#endif if (n == -1) return (-1); if (n == 0) return (0); -#else - n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL); - if (n == 0) - return (-1); - if (dwBytesRead == 0) - return (0); - n = dwBytesRead; -#endif buf->off += n; @@ -411,24 +493,16 @@ int evbuffer_write(struct evbuffer *buffer, int fd) { int n; -#ifdef WIN32 - DWORD dwBytesWritten; -#endif #ifndef WIN32 n = write(fd, buffer->buffer, buffer->off); +#else + n = send(fd, buffer->buffer, buffer->off, 0); +#endif if (n == -1) return (-1); if (n == 0) return (0); -#else - n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL); - if (n == 0) - return (-1); - if (dwBytesWritten == 0) - return (0); - n = dwBytesWritten; -#endif evbuffer_drain(buffer, n); return (n); |