2
0

picohttpparser.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
  3. * Shigeo Mitsunari
  4. *
  5. * The software is licensed under either the MIT License (below) or the Perl
  6. * license.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #include <assert.h> // for assert
  27. #include <stddef.h> // for NULL, size_t
  28. #include <string.h> // for memmove
  29. #include <sys/types.h> // for ssize_t
  30. #ifdef __SSE4_2__
  31. #ifdef _MSC_VER
  32. #include <nmmintrin.h>
  33. #else
  34. #include <x86intrin.h>
  35. #endif
  36. #endif
  37. #include "picohttpparser.h" // for phr_chunked_decoder, phr_header, phr_dec...
  38. #if __GNUC__ >= 3
  39. #define likely(x) __builtin_expect(!!(x), 1)
  40. #define unlikely(x) __builtin_expect(!!(x), 0)
  41. #else
  42. #define likely(x) (x)
  43. #define unlikely(x) (x)
  44. #endif
  45. #ifdef _MSC_VER
  46. #define ALIGNED(n) _declspec(align(n))
  47. #else
  48. #define ALIGNED(n) __attribute__((aligned(n)))
  49. #endif
  50. #define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
  51. #define CHECK_EOF() \
  52. if (buf == buf_end) { \
  53. *ret = -2; \
  54. return NULL; \
  55. }
  56. #define EXPECT_CHAR_NO_CHECK(ch) \
  57. if (*buf++ != ch) { \
  58. *ret = -1; \
  59. return NULL; \
  60. }
  61. #define EXPECT_CHAR(ch) \
  62. CHECK_EOF(); \
  63. EXPECT_CHAR_NO_CHECK(ch);
  64. #define ADVANCE_TOKEN(tok, toklen) \
  65. do { \
  66. const char* tok_start = buf; \
  67. static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
  68. int found2; \
  69. buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
  70. if (!found2) { \
  71. CHECK_EOF(); \
  72. } \
  73. while (1) { \
  74. if (*buf == ' ') { \
  75. break; \
  76. } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
  77. if ((unsigned char)*buf < '\040' || *buf == '\177') { \
  78. *ret = -1; \
  79. return NULL; \
  80. } \
  81. } \
  82. ++buf; \
  83. CHECK_EOF(); \
  84. } \
  85. tok = tok_start; \
  86. toklen = buf - tok_start; \
  87. } while (0)
  88. static const char* token_char_map =
  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\1\0\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"
  91. "\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"
  92. "\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"
  93. "\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"
  94. "\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"
  95. "\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"
  96. "\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";
  97. static const char* findchar_fast(const char* buf, const char* buf_end,
  98. const char* ranges, size_t ranges_size,
  99. int* found) {
  100. *found = 0;
  101. #if __SSE4_2__
  102. if (likely(buf_end - buf >= 16)) {
  103. __m128i ranges16 = _mm_loadu_si128((const __m128i*)ranges);
  104. size_t left = (buf_end - buf) & ~15;
  105. do {
  106. __m128i b16 = _mm_loadu_si128((const __m128i*)buf);
  107. int r = _mm_cmpestri(
  108. ranges16, ranges_size, b16, 16,
  109. _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
  110. if (unlikely(r != 16)) {
  111. buf += r;
  112. *found = 1;
  113. break;
  114. }
  115. buf += 16;
  116. left -= 16;
  117. } while (likely(left != 0));
  118. }
  119. #else
  120. /* suppress unused parameter warning */
  121. (void)buf_end;
  122. (void)ranges;
  123. (void)ranges_size;
  124. #endif
  125. return buf;
  126. }
  127. static const char* get_token_to_eol(const char* buf, const char* buf_end,
  128. const char** token, size_t* token_len,
  129. int* ret) {
  130. const char* token_start = buf;
  131. #ifdef __SSE4_2__
  132. static const char ALIGNED(16) ranges1[16] =
  133. "\0\010" /* allow HT */
  134. "\012\037" /* allow SP and up to but not including DEL */
  135. "\177\177"; /* allow chars w. MSB set */
  136. int found;
  137. buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
  138. if (found)
  139. goto FOUND_CTL;
  140. #else
  141. /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
  142. while (likely(buf_end - buf >= 8)) {
  143. #define DOIT() \
  144. do { \
  145. if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
  146. goto NonPrintable; \
  147. ++buf; \
  148. } while (0)
  149. DOIT();
  150. DOIT();
  151. DOIT();
  152. DOIT();
  153. DOIT();
  154. DOIT();
  155. DOIT();
  156. DOIT();
  157. #undef DOIT
  158. continue;
  159. NonPrintable:
  160. if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) ||
  161. unlikely(*buf == '\177')) {
  162. goto FOUND_CTL;
  163. }
  164. ++buf;
  165. }
  166. #endif
  167. for (;; ++buf) {
  168. CHECK_EOF();
  169. if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
  170. if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) ||
  171. unlikely(*buf == '\177')) {
  172. goto FOUND_CTL;
  173. }
  174. }
  175. }
  176. FOUND_CTL:
  177. if (likely(*buf == '\015')) {
  178. ++buf;
  179. EXPECT_CHAR('\012');
  180. *token_len = buf - 2 - token_start;
  181. } else if (*buf == '\012') {
  182. *token_len = buf - token_start;
  183. ++buf;
  184. } else {
  185. *ret = -1;
  186. return NULL;
  187. }
  188. *token = token_start;
  189. return buf;
  190. }
  191. static const char* is_complete(const char* buf, const char* buf_end,
  192. size_t last_len, int* ret) {
  193. int ret_cnt = 0;
  194. buf = last_len < 3 ? buf : buf + last_len - 3;
  195. while (1) {
  196. CHECK_EOF();
  197. if (*buf == '\015') {
  198. ++buf;
  199. CHECK_EOF();
  200. EXPECT_CHAR('\012');
  201. ++ret_cnt;
  202. } else if (*buf == '\012') {
  203. ++buf;
  204. ++ret_cnt;
  205. } else {
  206. ++buf;
  207. ret_cnt = 0;
  208. }
  209. if (ret_cnt == 2) {
  210. return buf;
  211. }
  212. }
  213. *ret = -2;
  214. return NULL;
  215. }
  216. #define PARSE_INT(valp_, mul_) \
  217. if (*buf < '0' || '9' < *buf) { \
  218. buf++; \
  219. *ret = -1; \
  220. return NULL; \
  221. } \
  222. *(valp_) = (mul_) * (*buf++ - '0');
  223. #define PARSE_INT_3(valp_) \
  224. do { \
  225. int res_ = 0; \
  226. PARSE_INT(&res_, 100) \
  227. *valp_ = res_; \
  228. PARSE_INT(&res_, 10) \
  229. *valp_ += res_; \
  230. PARSE_INT(&res_, 1) \
  231. *valp_ += res_; \
  232. } while (0)
  233. /* returned pointer is always within [buf, buf_end), or null */
  234. static const char* parse_token(const char* buf, const char* buf_end,
  235. const char** token, size_t* token_len,
  236. char next_char, int* ret) {
  237. /* We use pcmpestri to detect non-token characters. This instruction can take no more than eight character ranges (8*2*8=128
  238. * bits that is the size of a SSE register). Due to this restriction, characters `|` and `~` are handled in the slow loop. */
  239. static const char ALIGNED(16) ranges[] =
  240. "\x00 " /* control chars and up to SP */
  241. "\"\"" /* 0x22 */
  242. "()" /* 0x28,0x29 */
  243. ",," /* 0x2c */
  244. "//" /* 0x2f */
  245. ":@" /* 0x3a-0x40 */
  246. "[]" /* 0x5b-0x5d */
  247. "{\xff"; /* 0x7b-0xff */
  248. const char* buf_start = buf;
  249. int found;
  250. buf = findchar_fast(buf, buf_end, ranges, sizeof(ranges) - 1, &found);
  251. if (!found) {
  252. CHECK_EOF();
  253. }
  254. while (1) {
  255. if (*buf == next_char) {
  256. break;
  257. } else if (!token_char_map[(unsigned char)*buf]) {
  258. *ret = -1;
  259. return NULL;
  260. }
  261. ++buf;
  262. CHECK_EOF();
  263. }
  264. *token = buf_start;
  265. *token_len = buf - buf_start;
  266. return buf;
  267. }
  268. /* returned pointer is always within [buf, buf_end), or null */
  269. static const char* parse_http_version(const char* buf, const char* buf_end,
  270. int* minor_version, int* ret) {
  271. /* we want at least [HTTP/1.<two chars>] to try to parse */
  272. if (buf_end - buf < 9) {
  273. *ret = -2;
  274. return NULL;
  275. }
  276. EXPECT_CHAR_NO_CHECK('H');
  277. EXPECT_CHAR_NO_CHECK('T');
  278. EXPECT_CHAR_NO_CHECK('T');
  279. EXPECT_CHAR_NO_CHECK('P');
  280. EXPECT_CHAR_NO_CHECK('/');
  281. EXPECT_CHAR_NO_CHECK('1');
  282. EXPECT_CHAR_NO_CHECK('.');
  283. PARSE_INT(minor_version, 1);
  284. return buf;
  285. }
  286. static const char* parse_headers(const char* buf, const char* buf_end,
  287. struct phr_header* headers,
  288. size_t* num_headers, size_t max_headers,
  289. int* ret) {
  290. for (;; ++*num_headers) {
  291. CHECK_EOF();
  292. if (*buf == '\015') {
  293. ++buf;
  294. EXPECT_CHAR('\012');
  295. break;
  296. } else if (*buf == '\012') {
  297. ++buf;
  298. break;
  299. }
  300. if (*num_headers == max_headers) {
  301. *ret = -1;
  302. return NULL;
  303. }
  304. if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
  305. /* parsing name, but do not discard SP before colon, see
  306. * http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
  307. if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name,
  308. &headers[*num_headers].name_len, ':', ret)) ==
  309. NULL) {
  310. return NULL;
  311. }
  312. if (headers[*num_headers].name_len == 0) {
  313. *ret = -1;
  314. return NULL;
  315. }
  316. ++buf;
  317. for (;; ++buf) {
  318. CHECK_EOF();
  319. if (!(*buf == ' ' || *buf == '\t')) {
  320. break;
  321. }
  322. }
  323. } else {
  324. headers[*num_headers].name = NULL;
  325. headers[*num_headers].name_len = 0;
  326. }
  327. const char* value;
  328. size_t value_len;
  329. if ((buf = get_token_to_eol(buf, buf_end, &value, &value_len, ret)) ==
  330. NULL) {
  331. return NULL;
  332. }
  333. /* remove trailing SPs and HTABs */
  334. const char* value_end = value + value_len;
  335. for (; value_end != value; --value_end) {
  336. const char c = *(value_end - 1);
  337. if (!(c == ' ' || c == '\t')) {
  338. break;
  339. }
  340. }
  341. headers[*num_headers].value = value;
  342. headers[*num_headers].value_len = value_end - value;
  343. }
  344. return buf;
  345. }
  346. static const char* parse_request(const char* buf, const char* buf_end,
  347. const char** method, size_t* method_len,
  348. const char** path, size_t* path_len,
  349. int* minor_version, struct phr_header* headers,
  350. size_t* num_headers, size_t max_headers,
  351. int* ret) {
  352. /* skip first empty line (some clients add CRLF after POST content) */
  353. CHECK_EOF();
  354. if (*buf == '\015') {
  355. ++buf;
  356. EXPECT_CHAR('\012');
  357. } else if (*buf == '\012') {
  358. ++buf;
  359. }
  360. /* parse request line */
  361. if ((buf = parse_token(buf, buf_end, method, method_len, ' ', ret)) == NULL) {
  362. return NULL;
  363. }
  364. do {
  365. ++buf;
  366. CHECK_EOF();
  367. } while (*buf == ' ');
  368. ADVANCE_TOKEN(*path, *path_len);
  369. do {
  370. ++buf;
  371. CHECK_EOF();
  372. } while (*buf == ' ');
  373. if (*method_len == 0 || *path_len == 0) {
  374. *ret = -1;
  375. return NULL;
  376. }
  377. if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
  378. return NULL;
  379. }
  380. if (*buf == '\015') {
  381. ++buf;
  382. EXPECT_CHAR('\012');
  383. } else if (*buf == '\012') {
  384. ++buf;
  385. } else {
  386. *ret = -1;
  387. return NULL;
  388. }
  389. return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
  390. }
  391. int phr_parse_request(const char* buf_start, size_t len, const char** method,
  392. size_t* method_len, const char** path, size_t* path_len,
  393. int* minor_version, struct phr_header* headers,
  394. size_t* num_headers, size_t last_len) {
  395. const char *buf = buf_start, *buf_end = buf_start + len;
  396. size_t max_headers = *num_headers;
  397. int r;
  398. *method = NULL;
  399. *method_len = 0;
  400. *path = NULL;
  401. *path_len = 0;
  402. *minor_version = -1;
  403. *num_headers = 0;
  404. /* if last_len != 0, check if the request is complete (a fast countermeasure
  405. againt slowloris */
  406. if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
  407. return r;
  408. }
  409. if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len,
  410. minor_version, headers, num_headers, max_headers,
  411. &r)) == NULL) {
  412. return r;
  413. }
  414. return (int)(buf - buf_start);
  415. }
  416. static const char* parse_response(const char* buf, const char* buf_end,
  417. int* minor_version, int* status,
  418. const char** msg, size_t* msg_len,
  419. struct phr_header* headers,
  420. size_t* num_headers, size_t max_headers,
  421. int* ret) {
  422. /* parse "HTTP/1.x" */
  423. if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
  424. return NULL;
  425. }
  426. /* skip space */
  427. if (*buf != ' ') {
  428. *ret = -1;
  429. return NULL;
  430. }
  431. do {
  432. ++buf;
  433. CHECK_EOF();
  434. } while (*buf == ' ');
  435. /* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
  436. if (buf_end - buf < 4) {
  437. *ret = -2;
  438. return NULL;
  439. }
  440. PARSE_INT_3(status);
  441. /* get message including preceding space */
  442. if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
  443. return NULL;
  444. }
  445. if (*msg_len == 0) {
  446. /* ok */
  447. } else if (**msg == ' ') {
  448. /* Remove preceding space. Successful return from `get_token_to_eol` guarantees that we would hit something other than SP
  449. * before running past the end of the given buffer. */
  450. do {
  451. ++*msg;
  452. --*msg_len;
  453. } while (**msg == ' ');
  454. } else {
  455. /* garbage found after status code */
  456. *ret = -1;
  457. return NULL;
  458. }
  459. return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
  460. }
  461. int phr_parse_response(const char* buf_start, size_t len, int* minor_version,
  462. int* status, const char** msg, size_t* msg_len,
  463. struct phr_header* headers, size_t* num_headers,
  464. size_t last_len) {
  465. const char *buf = buf_start, *buf_end = buf + len;
  466. size_t max_headers = *num_headers;
  467. int r;
  468. *minor_version = -1;
  469. *status = 0;
  470. *msg = NULL;
  471. *msg_len = 0;
  472. *num_headers = 0;
  473. /* if last_len != 0, check if the response is complete (a fast countermeasure
  474. against slowloris */
  475. if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
  476. return r;
  477. }
  478. if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len,
  479. headers, num_headers, max_headers, &r)) == NULL) {
  480. return r;
  481. }
  482. return (int)(buf - buf_start);
  483. }
  484. int phr_parse_headers(const char* buf_start, size_t len,
  485. struct phr_header* headers, size_t* num_headers,
  486. size_t last_len) {
  487. const char *buf = buf_start, *buf_end = buf + len;
  488. size_t max_headers = *num_headers;
  489. int r;
  490. *num_headers = 0;
  491. /* if last_len != 0, check if the response is complete (a fast countermeasure
  492. against slowloris */
  493. if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
  494. return r;
  495. }
  496. if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers,
  497. &r)) == NULL) {
  498. return r;
  499. }
  500. return (int)(buf - buf_start);
  501. }
  502. enum {
  503. CHUNKED_IN_CHUNK_SIZE,
  504. CHUNKED_IN_CHUNK_EXT,
  505. CHUNKED_IN_CHUNK_DATA,
  506. CHUNKED_IN_CHUNK_CRLF,
  507. CHUNKED_IN_TRAILERS_LINE_HEAD,
  508. CHUNKED_IN_TRAILERS_LINE_MIDDLE
  509. };
  510. static int decode_hex(int ch) {
  511. if ('0' <= ch && ch <= '9') {
  512. return ch - '0';
  513. } else if ('A' <= ch && ch <= 'F') {
  514. return ch - 'A' + 0xa;
  515. } else if ('a' <= ch && ch <= 'f') {
  516. return ch - 'a' + 0xa;
  517. } else {
  518. return -1;
  519. }
  520. }
  521. ssize_t phr_decode_chunked(struct phr_chunked_decoder* decoder, char* buf,
  522. size_t* _bufsz) {
  523. size_t dst = 0, src = 0, bufsz = *_bufsz;
  524. ssize_t ret = -2; /* incomplete */
  525. while (1) {
  526. switch (decoder->_state) {
  527. case CHUNKED_IN_CHUNK_SIZE:
  528. for (;; ++src) {
  529. int v;
  530. if (src == bufsz)
  531. goto Exit;
  532. if ((v = decode_hex(buf[src])) == -1) {
  533. if (decoder->_hex_count == 0) {
  534. ret = -1;
  535. goto Exit;
  536. }
  537. break;
  538. }
  539. if (decoder->_hex_count == sizeof(size_t) * 2) {
  540. ret = -1;
  541. goto Exit;
  542. }
  543. decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
  544. ++decoder->_hex_count;
  545. }
  546. decoder->_hex_count = 0;
  547. decoder->_state = CHUNKED_IN_CHUNK_EXT;
  548. /* fallthru */
  549. case CHUNKED_IN_CHUNK_EXT:
  550. /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
  551. for (;; ++src) {
  552. if (src == bufsz)
  553. goto Exit;
  554. if (buf[src] == '\012')
  555. break;
  556. }
  557. ++src;
  558. if (decoder->bytes_left_in_chunk == 0) {
  559. if (decoder->consume_trailer) {
  560. decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
  561. break;
  562. } else {
  563. goto Complete;
  564. }
  565. }
  566. decoder->_state = CHUNKED_IN_CHUNK_DATA;
  567. /* fallthru */
  568. case CHUNKED_IN_CHUNK_DATA: {
  569. size_t avail = bufsz - src;
  570. if (avail < decoder->bytes_left_in_chunk) {
  571. if (dst != src)
  572. memmove(buf + dst, buf + src, avail);
  573. src += avail;
  574. dst += avail;
  575. decoder->bytes_left_in_chunk -= avail;
  576. goto Exit;
  577. }
  578. if (dst != src)
  579. memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
  580. src += decoder->bytes_left_in_chunk;
  581. dst += decoder->bytes_left_in_chunk;
  582. decoder->bytes_left_in_chunk = 0;
  583. decoder->_state = CHUNKED_IN_CHUNK_CRLF;
  584. }
  585. /* fallthru */
  586. case CHUNKED_IN_CHUNK_CRLF:
  587. for (;; ++src) {
  588. if (src == bufsz)
  589. goto Exit;
  590. if (buf[src] != '\015')
  591. break;
  592. }
  593. if (buf[src] != '\012') {
  594. ret = -1;
  595. goto Exit;
  596. }
  597. ++src;
  598. decoder->_state = CHUNKED_IN_CHUNK_SIZE;
  599. break;
  600. case CHUNKED_IN_TRAILERS_LINE_HEAD:
  601. for (;; ++src) {
  602. if (src == bufsz)
  603. goto Exit;
  604. if (buf[src] != '\015')
  605. break;
  606. }
  607. if (buf[src++] == '\012')
  608. goto Complete;
  609. decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
  610. /* fallthru */
  611. case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
  612. for (;; ++src) {
  613. if (src == bufsz)
  614. goto Exit;
  615. if (buf[src] == '\012')
  616. break;
  617. }
  618. ++src;
  619. decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
  620. break;
  621. default:
  622. assert(!"decoder is corrupt");
  623. }
  624. }
  625. Complete:
  626. ret = bufsz - src;
  627. Exit:
  628. if (dst != src)
  629. memmove(buf + dst, buf + src, bufsz - src);
  630. *_bufsz = dst;
  631. return ret;
  632. }
  633. int phr_decode_chunked_is_in_data(struct phr_chunked_decoder* decoder) {
  634. return decoder->_state == CHUNKED_IN_CHUNK_DATA;
  635. }
  636. #undef CHECK_EOF
  637. #undef EXPECT_CHAR
  638. #undef ADVANCE_TOKEN