MINOR: sample: add the hex2i converter
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.
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 818931e..bba695a 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -12617,6 +12617,10 @@
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
diff --git a/src/sample.c b/src/sample.c
index 6b767a1..62259ef 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -1541,6 +1541,23 @@
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 @@
{ "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 },