| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <stddef.h> |
| 29 |
#include <string.h> |
| 30 |
#ifdef __SSE4_2__ |
| 31 |
# include <x86intrin.h> |
| 32 |
#endif |
| 33 |
#include "picohttpparser.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if __GNUC__ >= 3 |
| 38 |
# define likely(x) __builtin_expect(!!(x), 1) |
| 39 |
# define unlikely(x) __builtin_expect(!!(x), 0) |
| 40 |
#else |
| 41 |
# define likely(x) (x) |
| 42 |
# define unlikely(x) (x) |
| 43 |
#endif |
| 44 |
|
| 45 |
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c) - 040u < 0137u) |
| 46 |
|
| 47 |
#define CHECK_EOF() \ |
| 48 |
if (buf == buf_end) { \ |
| 49 |
*ret = -2; \ |
| 50 |
return NULL; \ |
| 51 |
} |
| 52 |
|
| 53 |
#define EXPECT_CHAR(ch) \ |
| 54 |
CHECK_EOF(); \ |
| 55 |
if (*buf++ != ch) { \ |
| 56 |
*ret = -1; \ |
| 57 |
return NULL; \ |
| 58 |
} |
| 59 |
|
| 60 |
#define ADVANCE_TOKEN(tok, toklen) do { \ |
| 61 |
const char* tok_start = buf; \ |
| 62 |
static const char ranges2[] __attribute__((aligned(16))) = "\000\040\177\177"; \ |
| 63 |
int found2; \ |
| 64 |
buf = findchar_fast(buf, buf_end, ranges2, sizeof(ranges2) - 1, &found2); \ |
| 65 |
if (! found2) { \ |
| 66 |
CHECK_EOF(); \ |
| 67 |
} \ |
| 68 |
while (1) { \ |
| 69 |
if (*buf == ' ') { \ |
| 70 |
break; \ |
| 71 |
} else if (unlikely(! IS_PRINTABLE_ASCII(*buf))) { \ |
| 72 |
if ((unsigned char)*buf < '\040' || *buf == '\177') { \ |
| 73 |
*ret = -1; \ |
| 74 |
return NULL; \ |
| 75 |
} \ |
| 76 |
} \ |
| 77 |
++buf; \ |
| 78 |
CHECK_EOF(); \ |
| 79 |
} \ |
| 80 |
tok = tok_start; \ |
| 81 |
toklen = buf - tok_start; \ |
| 82 |
} while (0) |
| 83 |
|
| 84 |
static const char* token_char_map = |
| 85 |
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 86 |
"\0\1\1\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" |
| 87 |
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" |
| 88 |
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" |
| 89 |
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 90 |
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 91 |
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 92 |
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 93 |
|
| 94 |
static const char* findchar_fast(const char* buf, const char* buf_end, const char *ranges, size_t ranges_size, int* found) |
| 95 |
{ |
| 96 |
*found = 0; |
| 97 |
#if __SSE4_2__ |
| 98 |
if (likely(buf_end - buf >= 16)) { |
| 99 |
__m128i ranges16 = _mm_loadu_si128((const __m128i*)ranges); |
| 100 |
|
| 101 |
size_t left = (buf_end - buf) & ~15; |
| 102 |
do { |
| 103 |
__m128i b16 = _mm_loadu_si128((void*)buf); |
| 104 |
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS); |
| 105 |
if (unlikely(r != 16)) { |
| 106 |
buf += r; |
| 107 |
*found = 1; |
| 108 |
break; |
| 109 |
} |
| 110 |
buf += 16; |
| 111 |
left -= 16; |
| 112 |
} while (likely(left != 0)); |
| 113 |
} |
| 114 |
#endif |
| 115 |
return buf; |
| 116 |
} |
| 117 |
|
| 118 |
static const char* get_token_to_eol(const char* buf, const char* buf_end, |
| 119 |
const char** token, size_t* token_len, |
| 120 |
int* ret) |
| 121 |
{ |
| 122 |
const char* token_start = buf; |
| 123 |
|
| 124 |
#ifdef __SSE4_2__ |
| 125 |
static const char ranges1[] = |
| 126 |
"\0\010" |
| 127 |
|
| 128 |
"\012\037" |
| 129 |
|
| 130 |
"\177\177" |
| 131 |
|
| 132 |
; |
| 133 |
int found; |
| 134 |
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found); |
| 135 |
if (found) |
| 136 |
goto FOUND_CTL; |
| 137 |
#else |
| 138 |
|
| 139 |
while (likely(buf_end - buf >= 8)) { |
| 140 |
#define DOIT() if (unlikely(! IS_PRINTABLE_ASCII(*buf))) goto NonPrintable; ++buf |
| 141 |
DOIT(); DOIT(); DOIT(); DOIT(); |
| 142 |
DOIT(); DOIT(); DOIT(); DOIT(); |
| 143 |
#undef DOIT |
| 144 |
continue; |
| 145 |
NonPrintable: |
| 146 |
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { |
| 147 |
goto FOUND_CTL; |
| 148 |
} |
| 149 |
++buf; |
| 150 |
} |
| 151 |
#endif |
| 152 |
for (; ; ++buf) { |
| 153 |
CHECK_EOF(); |
| 154 |
if (unlikely(! IS_PRINTABLE_ASCII(*buf))) { |
| 155 |
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { |
| 156 |
goto FOUND_CTL; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
FOUND_CTL: |
| 161 |
if (likely(*buf == '\015')) { |
| 162 |
++buf; |
| 163 |
EXPECT_CHAR('\012'); |
| 164 |
*token_len = buf - 2 - token_start; |
| 165 |
} else if (*buf == '\012') { |
| 166 |
*token_len = buf - token_start; |
| 167 |
++buf; |
| 168 |
} else { |
| 169 |
*ret = -1; |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
*token = token_start; |
| 173 |
|
| 174 |
return buf; |
| 175 |
} |
| 176 |
|
| 177 |
static const char* is_complete(const char* buf, const char* buf_end, |
| 178 |
size_t last_len, int* ret) |
| 179 |
{ |
| 180 |
int ret_cnt = 0; |
| 181 |
buf = last_len < 3 ? buf : buf + last_len - 3; |
| 182 |
|
| 183 |
while (1) { |
| 184 |
CHECK_EOF(); |
| 185 |
if (*buf == '\015') { |
| 186 |
++buf; |
| 187 |
CHECK_EOF(); |
| 188 |
EXPECT_CHAR('\012'); |
| 189 |
++ret_cnt; |
| 190 |
} else if (*buf == '\012') { |
| 191 |
++buf; |
| 192 |
++ret_cnt; |
| 193 |
} else { |
| 194 |
++buf; |
| 195 |
ret_cnt = 0; |
| 196 |
} |
| 197 |
if (ret_cnt == 2) { |
| 198 |
return buf; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
*ret = -2; |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
static const char* parse_int(const char* buf, const char* buf_end, int* value, |
| 208 |
int* ret) |
| 209 |
{ |
| 210 |
int v; |
| 211 |
CHECK_EOF(); |
| 212 |
if (! ('0' <= *buf && *buf <= '9')) { |
| 213 |
*ret = -1; |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
v = 0; |
| 217 |
for (; ; ++buf) { |
| 218 |
CHECK_EOF(); |
| 219 |
if ('0' <= *buf && *buf <= '9') { |
| 220 |
v = v * 10 + *buf - '0'; |
| 221 |
} else { |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
*value = v; |
| 227 |
return buf; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
static const char* parse_http_version(const char* buf, const char* buf_end, |
| 232 |
int* minor_version, int* ret) |
| 233 |
{ |
| 234 |
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
| 235 |
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
| 236 |
return parse_int(buf, buf_end, minor_version, ret); |
| 237 |
} |
| 238 |
|
| 239 |
static const char* parse_headers(const char* buf, const char* buf_end, |
| 240 |
struct phr_header* headers, |
| 241 |
size_t* num_headers, size_t max_headers, |
| 242 |
int* ret) |
| 243 |
{ |
| 244 |
for (; ; ++*num_headers) { |
| 245 |
CHECK_EOF(); |
| 246 |
if (*buf == '\015') { |
| 247 |
++buf; |
| 248 |
EXPECT_CHAR('\012'); |
| 249 |
break; |
| 250 |
} else if (*buf == '\012') { |
| 251 |
++buf; |
| 252 |
break; |
| 253 |
} |
| 254 |
if (*num_headers == max_headers) { |
| 255 |
*ret = -1; |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) { |
| 259 |
if (! token_char_map[(unsigned char)*buf]) { |
| 260 |
*ret = -1; |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
headers[*num_headers].name = buf; |
| 266 |
static const char ranges1[] __attribute__((aligned(16))) = "::\x00\037"; |
| 267 |
int found; |
| 268 |
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found); |
| 269 |
if (! found) { |
| 270 |
CHECK_EOF(); |
| 271 |
} |
| 272 |
while (1) { |
| 273 |
if (*buf == ':') { |
| 274 |
break; |
| 275 |
} else if (*buf < ' ') { |
| 276 |
*ret = -1; |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
++buf; |
| 280 |
CHECK_EOF(); |
| 281 |
} |
| 282 |
headers[*num_headers].name_len = buf - headers[*num_headers].name; |
| 283 |
++buf; |
| 284 |
for (; ; ++buf) { |
| 285 |
CHECK_EOF(); |
| 286 |
if (! (*buf == ' ' || *buf == '\t')) { |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
} else { |
| 291 |
headers[*num_headers].name = NULL; |
| 292 |
headers[*num_headers].name_len = 0; |
| 293 |
} |
| 294 |
if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, |
| 295 |
&headers[*num_headers].value_len, ret)) |
| 296 |
== NULL) { |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
} |
| 300 |
return buf; |
| 301 |
} |
| 302 |
|
| 303 |
const char* parse_request(const char* buf, const char* buf_end, |
| 304 |
const char** method, size_t* method_len, |
| 305 |
const char** path, size_t* path_len, |
| 306 |
int* minor_version, struct phr_header* headers, |
| 307 |
size_t* num_headers, size_t max_headers, int* ret) |
| 308 |
{ |
| 309 |
|
| 310 |
CHECK_EOF(); |
| 311 |
if (*buf == '\015') { |
| 312 |
++buf; |
| 313 |
EXPECT_CHAR('\012'); |
| 314 |
} else if (*buf == '\012') { |
| 315 |
++buf; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
ADVANCE_TOKEN(*method, *method_len); |
| 320 |
++buf; |
| 321 |
ADVANCE_TOKEN(*path, *path_len); |
| 322 |
++buf; |
| 323 |
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
| 324 |
return NULL; |
| 325 |
} |
| 326 |
if (*buf == '\015') { |
| 327 |
++buf; |
| 328 |
EXPECT_CHAR('\012'); |
| 329 |
} else if (*buf == '\012') { |
| 330 |
++buf; |
| 331 |
} else { |
| 332 |
*ret = -1; |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
|
| 336 |
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 337 |
} |
| 338 |
|
| 339 |
int phr_parse_request(const char* buf_start, size_t len, const char** method, |
| 340 |
size_t* method_len, const char** path, size_t* path_len, |
| 341 |
int* minor_version, struct phr_header* headers, |
| 342 |
size_t* num_headers, size_t last_len) |
| 343 |
{ |
| 344 |
const char * buf = buf_start, * buf_end = buf_start + len; |
| 345 |
size_t max_headers = *num_headers; |
| 346 |
int r; |
| 347 |
|
| 348 |
*method = NULL; |
| 349 |
*method_len = 0; |
| 350 |
*path = NULL; |
| 351 |
*path_len = 0; |
| 352 |
*minor_version = -1; |
| 353 |
*num_headers = 0; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
| 358 |
return r; |
| 359 |
} |
| 360 |
|
| 361 |
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, |
| 362 |
minor_version, headers, num_headers, max_headers, |
| 363 |
&r)) |
| 364 |
== NULL) { |
| 365 |
return r; |
| 366 |
} |
| 367 |
|
| 368 |
return (int)(buf - buf_start); |
| 369 |
} |
| 370 |
|
| 371 |
static const char* parse_response(const char* buf, const char* buf_end, |
| 372 |
int* minor_version, int* status, |
| 373 |
const char** msg, size_t* msg_len, |
| 374 |
struct phr_header* headers, |
| 375 |
size_t* num_headers, size_t max_headers, |
| 376 |
int* ret) |
| 377 |
{ |
| 378 |
|
| 379 |
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
| 380 |
return NULL; |
| 381 |
} |
| 382 |
|
| 383 |
if (*buf++ != ' ') { |
| 384 |
*ret = -1; |
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
if (*buf++ != ' ') { |
| 393 |
*ret = -1; |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 402 |
} |
| 403 |
|
| 404 |
int phr_parse_response(const char* buf_start, size_t len, int* minor_version, |
| 405 |
int* status, const char** msg, size_t* msg_len, |
| 406 |
struct phr_header* headers, size_t* num_headers, |
| 407 |
size_t last_len) |
| 408 |
{ |
| 409 |
const char * buf = buf_start, * buf_end = buf + len; |
| 410 |
size_t max_headers = *num_headers; |
| 411 |
int r; |
| 412 |
|
| 413 |
*minor_version = -1; |
| 414 |
*status = 0; |
| 415 |
*msg = NULL; |
| 416 |
*msg_len = 0; |
| 417 |
*num_headers = 0; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
| 422 |
return r; |
| 423 |
} |
| 424 |
|
| 425 |
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, |
| 426 |
headers, num_headers, max_headers, &r)) |
| 427 |
== NULL) { |
| 428 |
return r; |
| 429 |
} |
| 430 |
|
| 431 |
return (int)(buf - buf_start); |
| 432 |
} |
| 433 |
|
| 434 |
int phr_parse_headers(const char* buf_start, size_t len, |
| 435 |
struct phr_header* headers, size_t* num_headers, |
| 436 |
size_t last_len) |
| 437 |
{ |
| 438 |
const char* buf = buf_start, * buf_end = buf + len; |
| 439 |
size_t max_headers = *num_headers; |
| 440 |
int r; |
| 441 |
|
| 442 |
*num_headers = 0; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
| 447 |
return r; |
| 448 |
} |
| 449 |
|
| 450 |
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) |
| 451 |
== NULL) { |
| 452 |
return r; |
| 453 |
} |
| 454 |
|
| 455 |
return (int)(buf - buf_start); |
| 456 |
} |
| 457 |
|
| 458 |
enum { |
| 459 |
CHUNKED_IN_CHUNK_SIZE, |
| 460 |
CHUNKED_IN_CHUNK_EXT, |
| 461 |
CHUNKED_IN_CHUNK_DATA, |
| 462 |
CHUNKED_IN_CHUNK_CRLF, |
| 463 |
CHUNKED_IN_TRAILERS_LINE_HEAD, |
| 464 |
CHUNKED_IN_TRAILERS_LINE_MIDDLE |
| 465 |
}; |
| 466 |
|
| 467 |
static int decode_hex(int ch) |
| 468 |
{ |
| 469 |
if ('0' <= ch && ch <= '9') { |
| 470 |
return ch - '0'; |
| 471 |
} else if ('A' <= ch && ch <= 'F') { |
| 472 |
return ch - 'A' + 0xa; |
| 473 |
} else if ('a' <= ch && ch <= 'f') { |
| 474 |
return ch - 'a' + 0xa; |
| 475 |
} else { |
| 476 |
return -1; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, |
| 481 |
size_t *_bufsz) |
| 482 |
{ |
| 483 |
size_t dst = 0, src = 0, bufsz = *_bufsz; |
| 484 |
ssize_t ret = -2; |
| 485 |
|
| 486 |
while (1) { |
| 487 |
switch (decoder->_state) { |
| 488 |
case CHUNKED_IN_CHUNK_SIZE: |
| 489 |
for (; ; ++src) { |
| 490 |
int v; |
| 491 |
if (src == bufsz) |
| 492 |
goto Exit; |
| 493 |
if ((v = decode_hex(buf[src])) == -1) { |
| 494 |
if (decoder->_hex_count == 0) { |
| 495 |
ret = -1; |
| 496 |
goto Exit; |
| 497 |
} |
| 498 |
break; |
| 499 |
} |
| 500 |
if (decoder->_hex_count == sizeof(size_t) * 2) { |
| 501 |
ret = -1; |
| 502 |
goto Exit; |
| 503 |
} |
| 504 |
decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v; |
| 505 |
++decoder->_hex_count; |
| 506 |
} |
| 507 |
decoder->_hex_count = 0; |
| 508 |
decoder->_state = CHUNKED_IN_CHUNK_EXT; |
| 509 |
|
| 510 |
case CHUNKED_IN_CHUNK_EXT: |
| 511 |
|
| 512 |
for (; ; ++src) { |
| 513 |
if (src == bufsz) |
| 514 |
goto Exit; |
| 515 |
if (buf[src] == '\012') |
| 516 |
break; |
| 517 |
} |
| 518 |
++src; |
| 519 |
if (decoder->bytes_left_in_chunk == 0) { |
| 520 |
if (decoder->consume_trailer) { |
| 521 |
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; |
| 522 |
break; |
| 523 |
} else { |
| 524 |
goto Complete; |
| 525 |
} |
| 526 |
} |
| 527 |
decoder->_state = CHUNKED_IN_CHUNK_DATA; |
| 528 |
|
| 529 |
case CHUNKED_IN_CHUNK_DATA: |
| 530 |
{ |
| 531 |
size_t avail = bufsz - src; |
| 532 |
if (avail < decoder->bytes_left_in_chunk) { |
| 533 |
if (dst != src) |
| 534 |
memmove(buf + dst, buf + src, avail); |
| 535 |
src += avail; |
| 536 |
dst += avail; |
| 537 |
decoder->bytes_left_in_chunk -= avail; |
| 538 |
goto Exit; |
| 539 |
} |
| 540 |
if (dst != src) |
| 541 |
memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk); |
| 542 |
src += decoder->bytes_left_in_chunk; |
| 543 |
dst += decoder->bytes_left_in_chunk; |
| 544 |
decoder->bytes_left_in_chunk = 0; |
| 545 |
decoder->_state = CHUNKED_IN_CHUNK_CRLF; |
| 546 |
} |
| 547 |
|
| 548 |
case CHUNKED_IN_CHUNK_CRLF: |
| 549 |
for (; ; ++src) { |
| 550 |
if (src == bufsz) |
| 551 |
goto Exit; |
| 552 |
if (buf[src] != '\015') |
| 553 |
break; |
| 554 |
} |
| 555 |
if (buf[src] != '\012') { |
| 556 |
ret = -1; |
| 557 |
goto Exit; |
| 558 |
} |
| 559 |
++src; |
| 560 |
decoder->_state = CHUNKED_IN_CHUNK_SIZE; |
| 561 |
break; |
| 562 |
case CHUNKED_IN_TRAILERS_LINE_HEAD: |
| 563 |
for (; ; ++src) { |
| 564 |
if (src == bufsz) |
| 565 |
goto Exit; |
| 566 |
if (buf[src] != '\015') |
| 567 |
break; |
| 568 |
} |
| 569 |
if (buf[src++] == '\012') |
| 570 |
goto Complete; |
| 571 |
decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE; |
| 572 |
|
| 573 |
case CHUNKED_IN_TRAILERS_LINE_MIDDLE: |
| 574 |
for (; ; ++src) { |
| 575 |
if (src == bufsz) |
| 576 |
goto Exit; |
| 577 |
if (buf[src] == '\012') |
| 578 |
break; |
| 579 |
} |
| 580 |
++src; |
| 581 |
decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; |
| 582 |
break; |
| 583 |
default: |
| 584 |
assert(!"decoder is corrupt"); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
Complete: |
| 589 |
ret = bufsz - src; |
| 590 |
Exit: |
| 591 |
if (dst != src) |
| 592 |
memmove(buf + dst, buf + src, bufsz - src); |
| 593 |
*_bufsz = dst; |
| 594 |
return ret; |
| 595 |
} |
| 596 |
|
| 597 |
#undef CHECK_EOF |
| 598 |
#undef EXPECT_CHAR |
| 599 |
#undef ADVANCE_TOKEN |