From 802e46f8ea36c344f112d7e1dd8d64d17a4cc939 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:59:37 +0200 Subject: Run clang-format again --- plugins/picohttpparser/picohttpparser.c | 243 ++++++++++++++++++-------------- 1 file changed, 137 insertions(+), 106 deletions(-) (limited to 'plugins/picohttpparser/picohttpparser.c') diff --git a/plugins/picohttpparser/picohttpparser.c b/plugins/picohttpparser/picohttpparser.c index 2ae92d66..e87388b0 100644 --- a/plugins/picohttpparser/picohttpparser.c +++ b/plugins/picohttpparser/picohttpparser.c @@ -50,59 +50,61 @@ # define ALIGNED(n) __attribute__((aligned(n))) #endif -#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u) +#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c) - 040u < 0137u) -#define CHECK_EOF() \ - if (buf == buf_end) { \ - *ret = -2; \ - return NULL; \ +#define CHECK_EOF() \ + if (buf == buf_end) { \ + *ret = -2; \ + return NULL; \ } -#define EXPECT_CHAR_NO_CHECK(ch) \ - if (*buf++ != ch) { \ - *ret = -1; \ - return NULL; \ +#define EXPECT_CHAR_NO_CHECK(ch) \ + if (*buf++ != ch) { \ + *ret = -1; \ + return NULL; \ } -#define EXPECT_CHAR(ch) \ - CHECK_EOF(); \ +#define EXPECT_CHAR(ch) \ + CHECK_EOF(); \ EXPECT_CHAR_NO_CHECK(ch); -#define ADVANCE_TOKEN(tok, toklen) \ - do { \ - const char *tok_start = buf; \ - static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \ - int found2; \ - buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \ - if (!found2) { \ - CHECK_EOF(); \ - } \ - while (1) { \ - if (*buf == ' ') { \ - break; \ - } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \ - if ((unsigned char)*buf < '\040' || *buf == '\177') { \ - *ret = -1; \ - return NULL; \ - } \ - } \ - ++buf; \ - CHECK_EOF(); \ - } \ - tok = tok_start; \ - toklen = buf - tok_start; \ +#define ADVANCE_TOKEN(tok, toklen) \ + do { \ + const char *tok_start = buf; \ + static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \ + int found2; \ + buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \ + if (!found2) { \ + CHECK_EOF(); \ + } \ + while (1) { \ + if (*buf == ' ') { \ + break; \ + } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \ + if ((unsigned char)*buf < '\040' || *buf == '\177') { \ + *ret = -1; \ + return NULL; \ + } \ + } \ + ++buf; \ + CHECK_EOF(); \ + } \ + tok = tok_start; \ + toklen = buf - tok_start; \ } while (0) -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" - "\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" - "\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" - "\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" - "\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" - "\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" - "\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" - "\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"; - -static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found) { +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" + "\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" + "\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" + "\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" + "\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" + "\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" + "\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" + "\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"; + +static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, + size_t ranges_size, int *found) { *found = 0; #if __SSE4_2__ if (likely(buf_end - buf >= 16)) { @@ -111,7 +113,8 @@ static const char *findchar_fast(const char *buf, const char *buf_end, const cha size_t left = (buf_end - buf) & ~15; do { __m128i b16 = _mm_loadu_si128((const __m128i *)buf); - int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS); + int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, + _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS); if (unlikely(r != 16)) { buf += r; *found = 1; @@ -130,25 +133,29 @@ static const char *findchar_fast(const char *buf, const char *buf_end, const cha return buf; } -static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret) { +static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, + size_t *token_len, int *ret) { const char *token_start = buf; #ifdef __SSE4_2__ - static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */ - "\012\037" /* allow SP and up to but not including DEL */ - "\177\177"; /* allow chars w. MSB set */ + static const char ALIGNED(16) ranges1[16] = + "\0\010" /* allow HT */ + "\012\037" /* allow SP and up to but not including DEL */ + "\177\177"; /* allow chars w. MSB set */ int found; buf = findchar_fast(buf, buf_end, ranges1, 6, &found); - if (found) + if (found) { goto FOUND_CTL; + } #else - /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */ + /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined + */ while (likely(buf_end - buf >= 8)) { -# define DOIT() \ - do { \ - if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \ - goto NonPrintable; \ - ++buf; \ +# define DOIT() \ + do { \ + if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \ + goto NonPrintable; \ + ++buf; \ } while (0) DOIT(); DOIT(); @@ -161,7 +168,8 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const # undef DOIT continue; NonPrintable: - if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { + if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || + unlikely(*buf == '\177')) { goto FOUND_CTL; } ++buf; @@ -170,7 +178,8 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const for (;; ++buf) { CHECK_EOF(); if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { - if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) { + if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || + unlikely(*buf == '\177')) { goto FOUND_CTL; } } @@ -219,27 +228,28 @@ static const char *is_complete(const char *buf, const char *buf_end, size_t last return NULL; } -#define PARSE_INT(valp_, mul_) \ - if (*buf < '0' || '9' < *buf) { \ - buf++; \ - *ret = -1; \ - return NULL; \ - } \ +#define PARSE_INT(valp_, mul_) \ + if (*buf < '0' || '9' < *buf) { \ + buf++; \ + *ret = -1; \ + return NULL; \ + } \ *(valp_) = (mul_) * (*buf++ - '0'); -#define PARSE_INT_3(valp_) \ - do { \ - int res_ = 0; \ - PARSE_INT(&res_, 100) \ - *valp_ = res_; \ - PARSE_INT(&res_, 10) \ - *valp_ += res_; \ - PARSE_INT(&res_, 1) \ - *valp_ += res_; \ +#define PARSE_INT_3(valp_) \ + do { \ + int res_ = 0; \ + PARSE_INT(&res_, 100) \ + *valp_ = res_; \ + PARSE_INT(&res_, 10) \ + *valp_ += res_; \ + PARSE_INT(&res_, 1) \ + *valp_ += res_; \ } while (0) /* returned pointer is always within [buf, buf_end), or null */ -static const char *parse_http_version(const char *buf, const char *buf_end, int *major_version, int *minor_version, int *ret) { +static const char *parse_http_version(const char *buf, const char *buf_end, int *major_version, + int *minor_version, int *ret) { /* we want at least [HTTP/1.] to try to parse */ if (buf_end - buf < 9) { *ret = -2; @@ -260,8 +270,8 @@ static const char *parse_http_version(const char *buf, const char *buf_end, int return buf; } -static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers, size_t max_headers, - int *ret) { +static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, + size_t *num_headers, size_t max_headers, int *ret) { for (;; ++*num_headers) { CHECK_EOF(); if (*buf == '\015') { @@ -337,9 +347,10 @@ static const char *parse_headers(const char *buf, const char *buf_end, struct ph return buf; } -static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path, - size_t *path_len, int *major_version, int *minor_version, struct phr_header *headers, size_t *num_headers, - size_t max_headers, int *ret) { +static const char *parse_request(const char *buf, const char *buf_end, const char **method, + size_t *method_len, const char **path, size_t *path_len, + int *major_version, int *minor_version, struct phr_header *headers, + size_t *num_headers, size_t max_headers, int *ret) { /* skip first empty line (some clients add CRLF after POST content) */ CHECK_EOF(); if (*buf == '\015') { @@ -378,8 +389,9 @@ static const char *parse_request(const char *buf, const char *buf_end, const cha return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); } -int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len, - int *major_version, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len) { +int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, + const char **path, size_t *path_len, int *major_version, int *minor_version, + struct phr_header *headers, size_t *num_headers, size_t last_len) { const char *buf = buf_start, *buf_end = buf_start + len; size_t max_headers = *num_headers; int r; @@ -398,17 +410,18 @@ int phr_parse_request(const char *buf_start, size_t len, const char **method, si return r; } - if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, major_version, minor_version, headers, num_headers, - max_headers, &r)) == NULL) { + if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, major_version, + minor_version, headers, num_headers, max_headers, &r)) == NULL) { return r; } return (int)(buf - buf_start); } -static const char *parse_response(const char *buf, const char *buf_end, int *major_version, int *minor_version, int *status, - const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, - int *ret) { +static const char *parse_response(const char *buf, const char *buf_end, int *major_version, + int *minor_version, int *status, const char **msg, + size_t *msg_len, struct phr_header *headers, size_t *num_headers, + size_t max_headers, int *ret) { /* parse "HTTP/1.x" */ if ((buf = parse_http_version(buf, buf_end, major_version, minor_version, ret)) == NULL) { return NULL; @@ -421,7 +434,8 @@ static const char *parse_response(const char *buf, const char *buf_end, int *maj do { ++buf; } while (*buf == ' '); - /* parse status code, we want at least [:digit:][:digit:][:digit:] to try to parse */ + /* parse status code, we want at least [:digit:][:digit:][:digit:] to try to parse + */ if (buf_end - buf < 4) { *ret = -2; return NULL; @@ -449,8 +463,9 @@ static const char *parse_response(const char *buf, const char *buf_end, int *maj return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); } -int phr_parse_response(const char *buf_start, size_t len, int *major_version, int *minor_version, int *status, const char **msg, - size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t last_len) { +int phr_parse_response(const char *buf_start, size_t len, int *major_version, int *minor_version, + int *status, const char **msg, size_t *msg_len, struct phr_header *headers, + size_t *num_headers, size_t last_len) { const char *buf = buf_start, *buf_end = buf + len; size_t max_headers = *num_headers; int r; @@ -468,15 +483,16 @@ int phr_parse_response(const char *buf_start, size_t len, int *major_version, in return r; } - if ((buf = parse_response(buf, buf_end, major_version, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == - NULL) { + if ((buf = parse_response(buf, buf_end, major_version, minor_version, status, msg, msg_len, + headers, num_headers, max_headers, &r)) == NULL) { return r; } return (int)(buf - buf_start); } -int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len) { +int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, + size_t *num_headers, size_t last_len) { const char *buf = buf_start, *buf_end = buf + len; size_t max_headers = *num_headers; int r; @@ -526,8 +542,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_SIZE: for (;; ++src) { int v; - if (src == bufsz) + if (src == bufsz) { goto Exit; + } if ((v = decode_hex(buf[src])) == -1) { if (decoder->_hex_count == 0) { ret = -1; @@ -548,10 +565,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_EXT: /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */ for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] == '\012') + } + if (buf[src] == '\012') { break; + } } ++src; if (decoder->bytes_left_in_chunk == 0) { @@ -567,15 +586,17 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_DATA: { size_t avail = bufsz - src; if (avail < decoder->bytes_left_in_chunk) { - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, avail); + } src += avail; dst += avail; decoder->bytes_left_in_chunk -= avail; goto Exit; } - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk); + } src += decoder->bytes_left_in_chunk; dst += decoder->bytes_left_in_chunk; decoder->bytes_left_in_chunk = 0; @@ -584,10 +605,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ /* fallthru */ case CHUNKED_IN_CHUNK_CRLF: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] != '\015') + } + if (buf[src] != '\015') { break; + } } if (buf[src] != '\012') { ret = -1; @@ -598,21 +621,26 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ break; case CHUNKED_IN_TRAILERS_LINE_HEAD: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] != '\015') + } + if (buf[src] != '\015') { break; + } } - if (buf[src++] == '\012') + if (buf[src++] == '\012') { goto Complete; + } decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE; /* fallthru */ case CHUNKED_IN_TRAILERS_LINE_MIDDLE: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] == '\012') + } + if (buf[src] == '\012') { break; + } } ++src; decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; @@ -625,13 +653,16 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ Complete: ret = bufsz - src; Exit: - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, bufsz - src); + } *_bufsz = dst; return ret; } -int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder) { return decoder->_state == CHUNKED_IN_CHUNK_DATA; } +int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder) { + return decoder->_state == CHUNKED_IN_CHUNK_DATA; +} #undef CHECK_EOF #undef EXPECT_CHAR -- cgit v1.2.3-74-g34f1