REORG: spoe: move spoe_encode_varint / spoe_decode_varint from spoe to common
These encoding functions does general stuff and can be used in
other context than spoe. This patch moves the function spoe_encode_varint
and spoe_decode_varint from spoe to common. It also remove the prefix spoe.
These functions will be used for encoding values in new binary sample fetch.
diff --git a/include/proto/spoe.h b/include/proto/spoe.h
index da19db1..1372a7d 100644
--- a/include/proto/spoe.h
+++ b/include/proto/spoe.h
@@ -22,87 +22,12 @@
#ifndef _PROTO_SPOE_H
#define _PROTO_SPOE_H
+#include <common/standard.h>
+
#include <types/spoe.h>
#include <proto/sample.h>
-
-/* Encode the integer <i> into a varint (variable-length integer). The encoded
- * value is copied in <*buf>. Here is the encoding format:
- *
- * 0 <= X < 240 : 1 byte (7.875 bits) [ XXXX XXXX ]
- * 240 <= X < 2288 : 2 bytes (11 bits) [ 1111 XXXX ] [ 0XXX XXXX ]
- * 2288 <= X < 264432 : 3 bytes (18 bits) [ 1111 XXXX ] [ 1XXX XXXX ] [ 0XXX XXXX ]
- * 264432 <= X < 33818864 : 4 bytes (25 bits) [ 1111 XXXX ] [ 1XXX XXXX ]*2 [ 0XXX XXXX ]
- * 33818864 <= X < 4328786160 : 5 bytes (32 bits) [ 1111 XXXX ] [ 1XXX XXXX ]*3 [ 0XXX XXXX ]
- * ...
- *
- * On success, it returns the number of written bytes and <*buf> is moved after
- * the encoded value. Otherwise, it returns -1. */
-static inline int
-spoe_encode_varint(uint64_t i, char **buf, char *end)
-{
- unsigned char *p = (unsigned char *)*buf;
- int r;
-
- if (p >= (unsigned char *)end)
- return -1;
-
- if (i < 240) {
- *p++ = i;
- *buf = (char *)p;
- return 1;
- }
-
- *p++ = (unsigned char)i | 240;
- i = (i - 240) >> 4;
- while (i >= 128) {
- if (p >= (unsigned char *)end)
- return -1;
- *p++ = (unsigned char)i | 128;
- i = (i - 128) >> 7;
- }
-
- if (p >= (unsigned char *)end)
- return -1;
- *p++ = (unsigned char)i;
-
- r = ((char *)p - *buf);
- *buf = (char *)p;
- return r;
-}
-
-/* Decode a varint from <*buf> and save the decoded value in <*i>. See
- * 'spoe_encode_varint' for details about varint.
- * On success, it returns the number of read bytes and <*buf> is moved after the
- * varint. Otherwise, it returns -1. */
-static inline int
-spoe_decode_varint(char **buf, char *end, uint64_t *i)
-{
- unsigned char *p = (unsigned char *)*buf;
- int r;
-
- if (p >= (unsigned char *)end)
- return -1;
-
- *i = *p++;
- if (*i < 240) {
- *buf = (char *)p;
- return 1;
- }
-
- r = 4;
- do {
- if (p >= (unsigned char *)end)
- return -1;
- *i += (uint64_t)*p << r;
- r += 7;
- } while (*p++ >= 128);
-
- r = ((char *)p - *buf);
- *buf = (char *)p;
- return r;
-}
/* Encode a buffer. Its length <len> is encoded as a varint, followed by a copy
* of <str>. It must have enough space in <*buf> to encode the buffer, else an
@@ -124,7 +49,7 @@
return 0;
}
- ret = spoe_encode_varint(len, &p, end);
+ ret = encode_varint(len, &p, end);
if (ret == -1 || p + len > end)
return -1;
@@ -152,7 +77,7 @@
return 0;
}
- ret = spoe_encode_varint(len, &p, end);
+ ret = encode_varint(len, &p, end);
if (ret == -1 || p >= end)
return -1;
@@ -176,7 +101,7 @@
*str = NULL;
*len = 0;
- ret = spoe_decode_varint(&p, end, &sz);
+ ret = decode_varint(&p, end, &sz);
if (ret == -1 || p + sz > end)
return -1;
@@ -217,7 +142,7 @@
case SMP_T_SINT:
*p++ = SPOE_DATA_T_INT64;
- if (spoe_encode_varint(smp->data.u.sint, &p, end) == -1)
+ if (encode_varint(smp->data.u.sint, &p, end) == -1)
return -1;
break;
@@ -313,7 +238,7 @@
*
* A types data is composed of a type (1 byte) and corresponding data:
* - boolean: non additional data (0 bytes)
- * - integers: a variable-length integer (see spoe_decode_varint)
+ * - integers: a variable-length integer (see decode_varint)
* - ipv4: 4 bytes
* - ipv6: 16 bytes
* - binary and string: a buffer prefixed by its size, a variable-length
@@ -337,7 +262,7 @@
case SPOE_DATA_T_INT64:
case SPOE_DATA_T_UINT32:
case SPOE_DATA_T_UINT64:
- if (spoe_decode_varint(&p, end, &v) == -1)
+ if (decode_varint(&p, end, &v) == -1)
return -1;
break;
case SPOE_DATA_T_IPV4:
@@ -386,7 +311,7 @@
case SPOE_DATA_T_INT64:
case SPOE_DATA_T_UINT32:
case SPOE_DATA_T_UINT64:
- if (spoe_decode_varint(&p, end, (uint64_t *)&smp->data.u.sint) == -1)
+ if (decode_varint(&p, end, (uint64_t *)&smp->data.u.sint) == -1)
return -1;
smp->data.type = SMP_T_SINT;
break;