picohttpparser.c 26 KB

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