]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hpack: move the length computation and encoding functions to .h
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 06:44:00 +0000 (07:44 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 08:06:46 +0000 (09:06 +0100)
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.

include/common/hpack-enc.h
src/hpack-enc.c

index 23c886a5338409bab4f51cc749c0545e53056c87..f04b78c244a3a4a7038a4bf18c61c70175dce73d 100644 (file)
 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 */
index fc93bc4d6ef00461b361cd8a8163d2971b657af9..818a0abd7b7b4cd4de85892d44f18c8637ad7458 100644 (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;
        }