1 /* $OpenBSD: buffer.c,v 1.12 2006/12/21 02:28:47 krw Exp $ */ 2 3 /* 4 * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifdef HAVE_CONFIG_H 31 #include "config.h" 32 #endif 33 34 #ifdef HAVE_VASPRINTF 35 /* If we have vasprintf, we need to define this before we include stdio.h. */ 36 #define _GNU_SOURCE 37 #endif 38 39 #include <sys/types.h> 40 41 #ifdef HAVE_SYS_TIME_H 42 #include <sys/time.h> 43 #endif 44 45 #ifdef HAVE_SYS_IOCTL_H 46 #include <sys/ioctl.h> 47 #endif 48 49 #include <errno.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #ifdef HAVE_STDARG_H 54 #include <stdarg.h> 55 #endif 56 #ifdef HAVE_UNISTD_H 57 #include <unistd.h> 58 #endif 59 60 #include "event.h" 61 62 struct evbuffer * 63 evbuffer_new(void) 64 { 65 struct evbuffer *buffer; 66 67 buffer = calloc(1, sizeof(struct evbuffer)); 68 69 return (buffer); 70 } 71 72 void 73 evbuffer_free(struct evbuffer *buffer) 74 { 75 if (buffer->orig_buffer != NULL) 76 free(buffer->orig_buffer); 77 free(buffer); 78 } 79 80 /* 81 * This is a destructive add. The data from one buffer moves into 82 * the other buffer. 83 */ 84 85 #define SWAP(x,y) do { \ 86 (x)->buffer = (y)->buffer; \ 87 (x)->orig_buffer = (y)->orig_buffer; \ 88 (x)->misalign = (y)->misalign; \ 89 (x)->totallen = (y)->totallen; \ 90 (x)->off = (y)->off; \ 91 } while (0) 92 93 int 94 evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) 95 { 96 int res; 97 98 /* Short cut for better performance */ 99 if (outbuf->off == 0) { 100 struct evbuffer tmp; 101 size_t oldoff = inbuf->off; 102 103 /* Swap them directly */ 104 SWAP(&tmp, outbuf); 105 SWAP(outbuf, inbuf); 106 SWAP(inbuf, &tmp); 107 108 /* 109 * Optimization comes with a price; we need to notify the 110 * buffer if necessary of the changes. oldoff is the amount 111 * of data that we transferred from inbuf to outbuf 112 */ 113 if (inbuf->off != oldoff && inbuf->cb != NULL) 114 (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg); 115 if (oldoff && outbuf->cb != NULL) 116 (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg); 117 118 return (0); 119 } 120 121 res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off); 122 if (res == 0) { 123 /* We drain the input buffer on success */ 124 evbuffer_drain(inbuf, inbuf->off); 125 } 126 127 return (res); 128 } 129 130 int 131 evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap) 132 { 133 char *buffer; 134 size_t space; 135 size_t oldoff = buf->off; 136 int sz; 137 va_list aq; 138 139 for (;;) { 140 buffer = (char *)buf->buffer + buf->off; 141 space = buf->totallen - buf->misalign - buf->off; 142 143 va_copy(aq, ap); 144 145 #ifdef WIN32 146 sz = vsnprintf(buffer, space - 1, fmt, aq); 147 buffer[space - 1] = '\0'; 148 #else 149 sz = vsnprintf(buffer, space, fmt, aq); 150 #endif 151 152 va_end(aq); 153 154 if (sz == -1) 155 return (-1); 156 if (sz < space) { 157 buf->off += sz; 158 if (buf->cb != NULL) 159 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); 160 return (sz); 161 } 162 if (evbuffer_expand(buf, sz + 1) == -1) 163 return (-1); 164 165 } 166 /* NOTREACHED */ 167 } 168 169 int 170 evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) 171 { 172 int res = -1; 173 va_list ap; 174 175 va_start(ap, fmt); 176 res = evbuffer_add_vprintf(buf, fmt, ap); 177 va_end(ap); 178 179 return (res); 180 } 181 182 /* Reads data from an event buffer and drains the bytes read */ 183 184 int 185 evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen) 186 { 187 size_t nread = datlen; 188 if (nread >= buf->off) 189 nread = buf->off; 190 191 memcpy(data, buf->buffer, nread); 192 evbuffer_drain(buf, nread); 193 194 return (nread); 195 } 196 197 /* 198 * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'. 199 * The returned buffer needs to be freed by the called. 200 */ 201 202 char * 203 evbuffer_readline(struct evbuffer *buffer) 204 { 205 u_char *data = EVBUFFER_DATA(buffer); 206 size_t len = EVBUFFER_LENGTH(buffer); 207 char *line; 208 u_int i; 209 210 for (i = 0; i < len; i++) { 211 if (data[i] == '\r' || data[i] == '\n') 212 break; 213 } 214 215 if (i == len) 216 return (NULL); 217 218 if ((line = malloc(i + 1)) == NULL) { 219 fprintf(stderr, "%s: out of memory\n", __func__); 220 evbuffer_drain(buffer, i); 221 return (NULL); 222 } 223 224 memcpy(line, data, i); 225 line[i] = '\0'; 226 227 /* 228 * Some protocols terminate a line with '\r\n', so check for 229 * that, too. 230 */ 231 if ( i < len - 1 ) { 232 char fch = data[i], sch = data[i+1]; 233 234 /* Drain one more character if needed */ 235 if ( (sch == '\r' || sch == '\n') && sch != fch ) 236 i += 1; 237 } 238 239 evbuffer_drain(buffer, i + 1); 240 241 return (line); 242 } 243 244 /* Adds data to an event buffer */ 245 246 static inline void 247 evbuffer_align(struct evbuffer *buf) 248 { 249 memmove(buf->orig_buffer, buf->buffer, buf->off); 250 buf->buffer = buf->orig_buffer; 251 buf->misalign = 0; 252 } 253 254 /* Expands the available space in the event buffer to at least datlen */ 255 256 int 257 evbuffer_expand(struct evbuffer *buf, size_t datlen) 258 { 259 size_t need = buf->misalign + buf->off + datlen; 260 261 /* If we can fit all the data, then we don't have to do anything */ 262 if (buf->totallen >= need) 263 return (0); 264 265 /* 266 * If the misalignment fulfills our data needs, we just force an 267 * alignment to happen. Afterwards, we have enough space. 268 */ 269 if (buf->misalign >= datlen) { 270 evbuffer_align(buf); 271 } else { 272 void *newbuf; 273 size_t length = buf->totallen; 274 275 if (length < 256) 276 length = 256; 277 while (length < need) 278 length <<= 1; 279 280 if (buf->orig_buffer != buf->buffer) 281 evbuffer_align(buf); 282 if ((newbuf = realloc(buf->buffer, length)) == NULL) 283 return (-1); 284 285 buf->orig_buffer = buf->buffer = newbuf; 286 buf->totallen = length; 287 } 288 289 return (0); 290 } 291 292 int 293 evbuffer_add(struct evbuffer *buf, void *data, size_t datlen) 294 { 295 size_t need = buf->misalign + buf->off + datlen; 296 size_t oldoff = buf->off; 297 298 if (buf->totallen < need) { 299 if (evbuffer_expand(buf, datlen) == -1) 300 return (-1); 301 } 302 303 memcpy(buf->buffer + buf->off, data, datlen); 304 buf->off += datlen; 305 306 if (datlen && buf->cb != NULL) 307 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); 308 309 return (0); 310 } 311 312 void 313 evbuffer_drain(struct evbuffer *buf, size_t len) 314 { 315 size_t oldoff = buf->off; 316 317 if (len >= buf->off) { 318 buf->off = 0; 319 buf->buffer = buf->orig_buffer; 320 buf->misalign = 0; 321 goto done; 322 } 323 324 buf->buffer += len; 325 buf->misalign += len; 326 327 buf->off -= len; 328 329 done: 330 /* Tell someone about changes in this buffer */ 331 if (buf->off != oldoff && buf->cb != NULL) 332 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); 333 334 } 335 336 /* 337 * Reads data from a file descriptor into a buffer. 338 */ 339 340 #define EVBUFFER_MAX_READ 4096 341 342 int 343 evbuffer_read(struct evbuffer *buf, int fd, int howmuch) 344 { 345 u_char *p; 346 size_t oldoff = buf->off; 347 int n = EVBUFFER_MAX_READ; 348 #ifdef WIN32 349 DWORD dwBytesRead; 350 #endif 351 352 #ifdef FIONREAD 353 if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) { 354 n = EVBUFFER_MAX_READ; 355 } else if (n > EVBUFFER_MAX_READ && n > howmuch) { 356 /* 357 * It's possible that a lot of data is available for 358 * reading. We do not want to exhaust resources 359 * before the reader has a chance to do something 360 * about it. If the reader does not tell us how much 361 * data we should read, we artifically limit it. 362 */ 363 if (n > buf->totallen << 2) 364 n = buf->totallen << 2; 365 if (n < EVBUFFER_MAX_READ) 366 n = EVBUFFER_MAX_READ; 367 } 368 #endif 369 if (howmuch < 0 || howmuch > n) 370 howmuch = n; 371 372 /* If we don't have FIONREAD, we might waste some space here */ 373 if (evbuffer_expand(buf, howmuch) == -1) 374 return (-1); 375 376 /* We can append new data at this point */ 377 p = buf->buffer + buf->off; 378 379 #ifndef WIN32 380 n = read(fd, p, howmuch); 381 if (n == -1) 382 return (-1); 383 if (n == 0) 384 return (0); 385 #else 386 n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL); 387 if (n == 0) 388 return (-1); 389 if (dwBytesRead == 0) 390 return (0); 391 n = dwBytesRead; 392 #endif 393 394 buf->off += n; 395 396 /* Tell someone about changes in this buffer */ 397 if (buf->off != oldoff && buf->cb != NULL) 398 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); 399 400 return (n); 401 } 402 403 int 404 evbuffer_write(struct evbuffer *buffer, int fd) 405 { 406 int n; 407 #ifdef WIN32 408 DWORD dwBytesWritten; 409 #endif 410 411 #ifndef WIN32 412 n = write(fd, buffer->buffer, buffer->off); 413 if (n == -1) 414 return (-1); 415 if (n == 0) 416 return (0); 417 #else 418 n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL); 419 if (n == 0) 420 return (-1); 421 if (dwBytesWritten == 0) 422 return (0); 423 n = dwBytesWritten; 424 #endif 425 evbuffer_drain(buffer, n); 426 427 return (n); 428 } 429 430 u_char * 431 evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len) 432 { 433 size_t remain = buffer->off; 434 u_char *search = buffer->buffer; 435 u_char *p; 436 437 while ((p = memchr(search, *what, remain)) != NULL && remain >= len) { 438 if (memcmp(p, what, len) == 0) 439 return (p); 440 441 search = p + 1; 442 remain = buffer->off - (size_t)(search - buffer->buffer); 443 } 444 445 return (NULL); 446 } 447 448 void evbuffer_setcb(struct evbuffer *buffer, 449 void (*cb)(struct evbuffer *, size_t, size_t, void *), 450 void *cbarg) 451 { 452 buffer->cb = cb; 453 buffer->cbarg = cbarg; 454 } 455