void
evbuffer_free(struct evbuffer *buffer)
{
- if (buffer->orig_buffer != NULL)
- event_free(buffer->orig_buffer);
+ if (buffer->buffer != NULL)
+ event_free(buffer->buffer);
event_free(buffer);
}
#define SWAP(x,y) do { \
(x)->buffer = (y)->buffer; \
- (x)->orig_buffer = (y)->orig_buffer; \
(x)->misalign = (y)->misalign; \
(x)->totallen = (y)->totallen; \
(x)->off = (y)->off; \
return (0);
}
- res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
+ res = evbuffer_add(outbuf, inbuf->buffer + inbuf->misalign, inbuf->off);
if (res == 0) {
/* We drain the input buffer on success */
evbuffer_drain(inbuf, inbuf->off);
evbuffer_expand(buf, 64);
for (;;) {
size_t used = buf->misalign + buf->off;
- buffer = (char *)buf->buffer + buf->off;
+ buffer = (char *)buf->buffer + buf->misalign + buf->off;
assert(buf->totallen >= used);
space = buf->totallen - used;
if (nread >= buf->off)
nread = buf->off;
- memcpy(data, buf->buffer, nread);
+ memcpy(data, buf->buffer + buf->misalign, nread);
evbuffer_drain(buf, nread);
return (nread);
static void
evbuffer_align(struct evbuffer *buf)
{
- memmove(buf->orig_buffer, buf->buffer, buf->off);
- buf->buffer = buf->orig_buffer;
+ memmove(buf->buffer, buf->buffer + buf->misalign, buf->off);
buf->misalign = 0;
}
while (length < need)
length <<= 1;
- if (buf->orig_buffer != buf->buffer)
+ if (buf->misalign)
evbuffer_align(buf);
if ((newbuf = event_realloc(buf->buffer, length)) == NULL)
return (-1);
- buf->orig_buffer = buf->buffer = newbuf;
+ buf->buffer = newbuf;
buf->totallen = length;
}
return (-1);
}
- memcpy(buf->buffer + buf->off, data, datlen);
+ memcpy(buf->buffer + buf->misalign + buf->off, data, datlen);
buf->off += datlen;
if (datlen && buf->cb != NULL)
if (len >= buf->off) {
buf->off = 0;
- buf->buffer = buf->orig_buffer;
buf->misalign = 0;
goto done;
}
- buf->buffer += len;
buf->misalign += len;
buf->off -= len;
return (-1);
/* We can append new data at this point */
- p = buf->buffer + buf->off;
+ p = buf->buffer + buf->misalign + buf->off;
#ifndef WIN32
n = read(fd, p, howmuch);
int n;
#ifndef WIN32
- n = write(fd, buffer->buffer, buffer->off);
+ n = write(fd, buffer->buffer + buffer->misalign, buffer->off);
#else
- n = send(fd, buffer->buffer, buffer->off, 0);
+ n = send(fd, buffer->buffer + buffer->misalign, buffer->off, 0);
#endif
if (n == -1)
return (-1);
u_char *
evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len)
{
- u_char *search = buffer->buffer, *end = search + buffer->off;
+ u_char *search = buffer->buffer + buffer->misalign;
+ u_char *end = search + buffer->off;
u_char *p;
while (search < end &&
test_evbuffer(void) {
struct evbuffer *evb = evbuffer_new();
+ struct evbuffer *evb_two = evbuffer_new();
setup_test("Testing Evbuffer: ");
evbuffer_add_printf(evb, "%s/%d", "hello", 1);
- if (EVBUFFER_LENGTH(evb) == 7 &&
- strcmp((char*)EVBUFFER_DATA(evb), "hello/1") == 0)
- test_ok = 1;
+ if (EVBUFFER_LENGTH(evb) != 7 ||
+ strcmp((char*)EVBUFFER_DATA(evb), "hello/1") != 0)
+ goto out;
+
+ evbuffer_drain(evb, strlen("hello/"));
+ if (EVBUFFER_LENGTH(evb) != 1 ||
+ strcmp((char*)EVBUFFER_DATA(evb), "1") != 0)
+ goto out;
+
+ evbuffer_add_printf(evb_two, "%s", "/hello");
+ evbuffer_add_buffer(evb, evb_two);
+
+ if (EVBUFFER_LENGTH(evb_two) != 0 ||
+ EVBUFFER_LENGTH(evb) != 7 ||
+ strcmp((char*)EVBUFFER_DATA(evb), "1/hello") != 0)
+ goto out;
+
+ test_ok = 1;
+out:
evbuffer_free(evb);
+ evbuffer_free(evb_two);
cleanup_test();
}