This commit adds the base2 converter to turn binary input into it's
string representation. Each input byte is converted into a series of
eight characters which are either 0s and 1s by bit-wise comparison.
---
Switched to bit-wise comparison instead of a lookup table as suggested by Willy.
I was unsure whether the added run time would be an issue so I initially opted
for the lookup table like it's done for hex, but it makes sense that this huge
table could cause more issues.
Last time I submitted an updated patch used the In-Reply-To header, but that
seems to be discouraged for patches. Adding the v2 seems the preferred option
so I did that now, hope this is the right way.
doc/configuration.txt | 7 +++++++
src/sample.c | 20 ++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/doc/configuration.txt b/doc/configuration.txt
index dff2b491e8..62c79c5058 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -19932,6 +19932,7 @@ aes_gcm_dec(bits,nonce,key,aead_tag)
binary binary
aes_gcm_enc(bits,nonce,key,aead_tag) binary binary
and(value) integer integer
b64dec string binary
+base2 binary string
base64 binary string
be2dec(separator,chunk_size[,truncate]) binary string
le2dec(separator,chunk_size[,truncate]) binary string
@@ -20156,6 +20157,12 @@ b64dec
For base64url("URL and Filename Safe Alphabet" (RFC 4648)) variant
see "ub64dec".
+base2
+ Converts a binary input sample to a binary string containing eight binary
+ digits per input byte. It is used to be able to perform longest prefix match
+ on types where the native representation does not allow prefix matching, for
+ example IP prefixes.
+
base64
Converts a binary input sample to a base64 string. It is used to log or
transfer binary content in a way that can be reliably transferred (e.g.
diff --git a/src/sample.c b/src/sample.c
index c5ee623fa5..650652c695 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -2151,6 +2151,25 @@ static int sample_conv_bin2hex(const struct arg *arg_p,
struct sample *smp, void
return 1;
}
+static int sample_conv_bin2base2(const struct arg *arg_p, struct sample *smp,
void *private)
+{
+ struct buffer *trash = get_trash_chunk();
+ unsigned char c;
+ int ptr = 0;
+ int bit = 0;
+
+ trash->data = 0;
+ while (ptr < smp->data.u.str.data && trash->data <= trash->size - 8) {
+ c = smp->data.u.str.area[ptr++];
+ for (bit = 7; bit >= 0; bit--)
+ trash->area[trash->data++] = c & (1 << bit) ? '1' :
'0';
+ }
+ smp->data.u.str = *trash;
+ smp->data.type = SMP_T_STR;
+ smp->flags &= ~SMP_F_CONST;
+ return 1;
+}
+
static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp,
void *private)
{
long long int n = 0;
@@ -5441,6 +5460,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 },
{ "length", sample_conv_length, 0, NULL,
SMP_T_STR, SMP_T_SINT },
+ { "base2", sample_conv_bin2base2, 0, NULL,
SMP_T_BIN, SMP_T_STR },
{ "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT),
sample_conv_2dec_check, SMP_T_BIN, SMP_T_STR },
{ "le2dec", sample_conv_le2dec, ARG3(1,STR,SINT,SINT),
sample_conv_2dec_check, SMP_T_BIN, SMP_T_STR },
{ "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT),
sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },
--
2.51.0