]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: Add base2 converter
authorMaximilian Moehl <maximilian@moehl.eu>
Thu, 4 Sep 2025 07:19:39 +0000 (09:19 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 5 Sep 2025 06:51:51 +0000 (08:51 +0200)
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.

doc/configuration.txt
src/sample.c

index a364d7e13136646118780971359b0979c8cde4e7..bc775b0cc4f5ec654135df77b32b250780826c60 100644 (file)
@@ -19962,6 +19962,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
@@ -20186,6 +20187,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.
index c5ee623fa5d0cd61b776c416c40c88c00d816c58..650652c695eaf32132e24282ad5411bf2a8d4d8b 100644 (file)
@@ -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  },