]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add the hex2i converter
authorDragan Dosen <ddosen@haproxy.com>
Tue, 24 Oct 2017 07:27:34 +0000 (09:27 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Oct 2017 02:46:08 +0000 (04:46 +0200)
Converts a hex string containing two hex digits per input byte to an
integer. If the input value can not be converted, then zero is returned.

doc/configuration.txt
src/sample.c

index 818931e37e3d93bf969b06e70e42ed7524d9e36c..bba695afd83735a83cbc0f731aa5919622568da2 100644 (file)
@@ -12617,6 +12617,10 @@ hex
   in a way that can be reliably transferred (eg: an SSL ID can be copied in a
   header).
 
+hex2i
+  Converts a hex string containing two hex digits per input byte to an
+  integer. If the input value can not be converted, then zero is returned.
+
 http_date([<offset>])
   Converts an integer supposed to contain a date since epoch to a string
   representing this date in a format suitable for use in HTTP header fields. If
index 6b767a17db09fd05679feb8cceb2224c3386a78a..62259ef3eaeb36cd27320a224ea1d4a9aeafc5c3 100644 (file)
@@ -1541,6 +1541,23 @@ static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void
        return 1;
 }
 
+static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
+{
+       long long int n = 0;
+       int i, c;
+
+       for (i = 0; i < smp->data.u.str.len; i++) {
+               if ((c = hex2i(smp->data.u.str.str[i])) < 0)
+                       return 0;
+               n = (n << 4) + c;
+       }
+
+       smp->data.u.sint = n;
+       smp->data.type = SMP_T_SINT;
+       smp->flags &= ~SMP_F_CONST;
+       return 1;
+}
+
 /* hashes the binary input into a 32-bit unsigned int */
 static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
 {
@@ -2761,6 +2778,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
        { "upper",  sample_conv_str2upper, 0,            NULL, SMP_T_STR,  SMP_T_STR  },
        { "lower",  sample_conv_str2lower, 0,            NULL, SMP_T_STR,  SMP_T_STR  },
        { "hex",    sample_conv_bin2hex,   0,            NULL, SMP_T_BIN,  SMP_T_STR  },
+       { "hex2i",  sample_conv_hex2int,   0,            NULL, SMP_T_STR,  SMP_T_SINT },
        { "ipmask", sample_conv_ipmask,    ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
        { "ltime",  sample_conv_ltime,     ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
        { "utime",  sample_conv_utime,     ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },