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.
This commit is contained in:
Willy Tarreau 2018-12-11 07:44:00 +01:00
parent 2c3139489c
commit bad0a381d3
2 changed files with 46 additions and 44 deletions

View File

@ -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 <len>. 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 <len> into <out>+<pos> 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 */

View File

@ -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 <len>. 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 <len> into <out>+<pos> 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 <n> and value <v> into the chunk <out>.
* 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;
}