Coverage Report

Created: 2025-08-26 06:48

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