OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu> | 2 * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu> |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list)) | 154 #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list)) |
155 #endif | 155 #endif |
156 va_copy(aq, ap); | 156 va_copy(aq, ap); |
157 | 157 |
158 sz = evutil_vsnprintf(buffer, space, fmt, aq); | 158 sz = evutil_vsnprintf(buffer, space, fmt, aq); |
159 | 159 |
160 va_end(aq); | 160 va_end(aq); |
161 | 161 |
162 if (sz < 0) | 162 if (sz < 0) |
163 return (-1); | 163 return (-1); |
164 » » if (sz < space) { | 164 » » if ((size_t)sz < space) { |
165 buf->off += sz; | 165 buf->off += sz; |
166 if (buf->cb != NULL) | 166 if (buf->cb != NULL) |
167 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); | 167 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); |
168 return (sz); | 168 return (sz); |
169 } | 169 } |
170 if (evbuffer_expand(buf, sz + 1) == -1) | 170 if (evbuffer_expand(buf, sz + 1) == -1) |
171 return (-1); | 171 return (-1); |
172 | 172 |
173 } | 173 } |
174 /* NOTREACHED */ | 174 /* NOTREACHED */ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 for (i = 0; i < len; i++) { | 218 for (i = 0; i < len; i++) { |
219 if (data[i] == '\r' || data[i] == '\n') | 219 if (data[i] == '\r' || data[i] == '\n') |
220 break; | 220 break; |
221 } | 221 } |
222 | 222 |
223 if (i == len) | 223 if (i == len) |
224 return (NULL); | 224 return (NULL); |
225 | 225 |
226 if ((line = malloc(i + 1)) == NULL) { | 226 if ((line = malloc(i + 1)) == NULL) { |
227 fprintf(stderr, "%s: out of memory\n", __func__); | 227 fprintf(stderr, "%s: out of memory\n", __func__); |
228 evbuffer_drain(buffer, i); | |
229 return (NULL); | 228 return (NULL); |
230 } | 229 } |
231 | 230 |
232 memcpy(line, data, i); | 231 memcpy(line, data, i); |
233 line[i] = '\0'; | 232 line[i] = '\0'; |
234 | 233 |
235 /* | 234 /* |
236 * Some protocols terminate a line with '\r\n', so check for | 235 * Some protocols terminate a line with '\r\n', so check for |
237 * that, too. | 236 * that, too. |
238 */ | 237 */ |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 int | 349 int |
351 evbuffer_read(struct evbuffer *buf, int fd, int howmuch) | 350 evbuffer_read(struct evbuffer *buf, int fd, int howmuch) |
352 { | 351 { |
353 u_char *p; | 352 u_char *p; |
354 size_t oldoff = buf->off; | 353 size_t oldoff = buf->off; |
355 int n = EVBUFFER_MAX_READ; | 354 int n = EVBUFFER_MAX_READ; |
356 | 355 |
357 #if defined(FIONREAD) | 356 #if defined(FIONREAD) |
358 #ifdef WIN32 | 357 #ifdef WIN32 |
359 long lng = n; | 358 long lng = n; |
360 » if (ioctlsocket(fd, FIONREAD, &lng) == -1 || (n=lng) == 0) { | 359 » if (ioctlsocket(fd, FIONREAD, &lng) == -1 || (n=lng) <= 0) { |
361 #else | 360 #else |
362 » if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) { | 361 » if (ioctl(fd, FIONREAD, &n) == -1 || n <= 0) { |
363 #endif | 362 #endif |
364 n = EVBUFFER_MAX_READ; | 363 n = EVBUFFER_MAX_READ; |
365 } else if (n > EVBUFFER_MAX_READ && n > howmuch) { | 364 } else if (n > EVBUFFER_MAX_READ && n > howmuch) { |
366 /* | 365 /* |
367 * It's possible that a lot of data is available for | 366 * It's possible that a lot of data is available for |
368 * reading. We do not want to exhaust resources | 367 * reading. We do not want to exhaust resources |
369 * before the reader has a chance to do something | 368 * before the reader has a chance to do something |
370 * about it. If the reader does not tell us how much | 369 * about it. If the reader does not tell us how much |
371 * data we should read, we artifically limit it. | 370 * data we should read, we artifically limit it. |
372 */ | 371 */ |
373 » » if (n > buf->totallen << 2) | 372 » » if ((size_t)n > buf->totallen << 2) |
374 n = buf->totallen << 2; | 373 n = buf->totallen << 2; |
375 if (n < EVBUFFER_MAX_READ) | 374 if (n < EVBUFFER_MAX_READ) |
376 n = EVBUFFER_MAX_READ; | 375 n = EVBUFFER_MAX_READ; |
377 } | 376 } |
378 #endif | 377 #endif |
379 if (howmuch < 0 || howmuch > n) | 378 if (howmuch < 0 || howmuch > n) |
380 howmuch = n; | 379 howmuch = n; |
381 | 380 |
382 /* If we don't have FIONREAD, we might waste some space here */ | 381 /* If we don't have FIONREAD, we might waste some space here */ |
383 if (evbuffer_expand(buf, howmuch) == -1) | 382 if (evbuffer_expand(buf, howmuch) == -1) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 return (NULL); | 441 return (NULL); |
443 } | 442 } |
444 | 443 |
445 void evbuffer_setcb(struct evbuffer *buffer, | 444 void evbuffer_setcb(struct evbuffer *buffer, |
446 void (*cb)(struct evbuffer *, size_t, size_t, void *), | 445 void (*cb)(struct evbuffer *, size_t, size_t, void *), |
447 void *cbarg) | 446 void *cbarg) |
448 { | 447 { |
449 buffer->cb = cb; | 448 buffer->cb = cb; |
450 buffer->cbarg = cbarg; | 449 buffer->cbarg = cbarg; |
451 } | 450 } |
OLD | NEW |