From: Frédéric Lécaille Date: Tue, 26 Feb 2019 13:09:08 +0000 (+0100) Subject: MINOR: sample: Add two sample converters for protocol buffers. X-Git-Tag: v2.0-dev1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd95c62f1b07ee0a553458a83c201b7094d7aa6b;p=thirdparty%2Fhaproxy.git MINOR: sample: Add two sample converters for protocol buffers. Add "varint" to convert all the protocol buffers binary varints excepted the signed ones ("sint32" and "sint64") to an integer. The binary signed varints may be converted to an integer with "svarint" converter implemented by this patch. These two new converters do not take any argument. --- diff --git a/src/sample.c b/src/sample.c index 16251f0061..1f323bdc78 100644 --- a/src/sample.c +++ b/src/sample.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -1778,6 +1779,33 @@ static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void return 1; } +/* Decode an unsigned protocol buffers varint */ +static int sample_conv_varint(const struct arg *arg_p, struct sample *smp, void *private) +{ + uint64_t varint; + + if (!protobuf_varint(&varint, (unsigned char *)smp->data.u.str.area, smp->data.u.str.data)) + return 0; + + smp->data.u.sint = varint; + smp->data.type = SMP_T_SINT; + return 1; +} + +/* Decode a signed protocol buffers varint encoded as (zigzag + varint). */ +static int sample_conv_svarint(const struct arg *arg_p, struct sample *smp, void *private) +{ + uint64_t varint; + + if (!protobuf_varint(&varint, (unsigned char *)smp->data.u.str.area, smp->data.u.str.data)) + return 0; + + /* zigzag decoding. */ + smp->data.u.sint = (varint >> 1) ^ -(varint & 1); + smp->data.type = SMP_T_SINT; + return 1; +} + /* This function escape special json characters. The returned string can be * safely set between two '"' and used as json string. The json string is * defined like this: @@ -3133,6 +3161,8 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN }, { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR }, { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT }, + { "varint", sample_conv_varint, 0, NULL, SMP_T_BIN, SMP_T_SINT }, + { "svarint", sample_conv_svarint, 0, NULL, SMP_T_BIN, SMP_T_SINT }, { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },