From: Willy Tarreau Date: Tue, 11 Dec 2018 06:44:00 +0000 (+0100) Subject: MINOR: hpack: move the length computation and encoding functions to .h X-Git-Tag: v1.9-dev11~126 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bad0a381d3a6b632d3fe97747bf0d5b845da998f;p=thirdparty%2Fhaproxy.git MINOR: hpack: move the length computation and encoding functions to .h We'll need these functions from other inline functions, let's make them accessible. len_to_bytes() was renamed to hpack_len_to_bytes() since it's now exposed. --- diff --git a/include/common/hpack-enc.h b/include/common/hpack-enc.h index 23c886a533..f04b78c244 100644 --- a/include/common/hpack-enc.h +++ b/include/common/hpack-enc.h @@ -36,4 +36,46 @@ int hpack_encode_header(struct buffer *out, const struct ist n, const struct ist v); +/* Returns the number of bytes required to encode the string length . The + * number of usable bits is an integral multiple of 7 plus 6 for the last byte. + * The maximum number of bytes returned is 4 (2097279 max length). Larger values + * return 0. + */ +static inline int hpack_len_to_bytes(size_t len) +{ + ssize_t slen = len; + + slen -= 127; + if (__builtin_expect(slen < 0, 1)) + return 1; + if (slen < (1 << 14)) { + if (__builtin_expect(slen < (1 << 7), 1)) + return 2; + else + return 3; + } + if (slen < (1 << 21)) + return 4; + return 0; +} + +/* Encodes into + and return the new position. The caller is + * responsible for checking for available room using hpack_len_to_bytes() + * first. + */ +static inline int hpack_encode_len(char *out, int pos, int len) +{ + int code = len - 127; + + if (code < 0) { + out[pos++] = len; + } else { + out[pos++] = 127; + for (; code >= 128; code >>= 7) + out[pos++] = code | 128; + out[pos++] = code; + } + return pos; +} + #endif /* _COMMON_HPACK_ENC_H */ diff --git a/src/hpack-enc.c b/src/hpack-enc.c index fc93bc4d6e..818a0abd7b 100644 --- a/src/hpack-enc.c +++ b/src/hpack-enc.c @@ -140,48 +140,6 @@ const signed short hpack_pos_len[32] = { /* 24: */ -1, 609, -1, 636, -1, -1, -1, -1, }; -/* returns the number of bytes required to encode the string length . The - * number of usable bits is an integral multiple of 7 plus 6 for the last byte. - * The maximum number of bytes returned is 4 (2097279 max length). Larger values - * return 0. - */ -static inline int len_to_bytes(size_t len) -{ - ssize_t slen = len; - - slen -= 127; - if (__builtin_expect(slen < 0, 1)) - return 1; - if (slen < (1 << 14)) { - if (__builtin_expect(slen < (1 << 7), 1)) - return 2; - else - return 3; - } - if (slen < (1 << 21)) - return 4; - return 0; -} - -/* Encode into + and return the new position. The caller is - * responsible for checking for available room using len_to_bytes() first. - */ -static inline int hpack_encode_len(char *out, int pos, int len) -{ - int code = len - 127; - - if (code < 0) { - out[pos++] = len; - } else { - out[pos++] = 127; - for (; code >= 128; code >>= 7) - out[pos++] = code | 128; - out[pos++] = code; - } - return pos; -} - - /* Tries to encode header whose name is and value into the chunk . * Returns non-zero on success, 0 on failure (buffer full). */ @@ -225,7 +183,8 @@ int hpack_encode_header(struct buffer *out, const struct ist n, ist2bin(out->area + len, n); len += n.len; } - else if (len_to_bytes(n.len) && len + 1 + len_to_bytes(n.len) + n.len <= size) { + else if (hpack_len_to_bytes(n.len) && + len + 1 + hpack_len_to_bytes(n.len) + n.len <= size) { out->area[len++] = 0x00; /* literal without indexing -- new name */ len = hpack_encode_len(out->area, len, n.len); ist2bin(out->area + len, n); @@ -238,7 +197,8 @@ int hpack_encode_header(struct buffer *out, const struct ist n, emit_value: /* copy literal header field value */ - if (!len_to_bytes(v.len) || len + len_to_bytes(v.len) + v.len > size) { + if (!hpack_len_to_bytes(v.len) || + len + hpack_len_to_bytes(v.len) + v.len > size) { /* header value too large for the buffer */ return 0; }