MINOR: sample: Add base2 converter

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.
This commit is contained in:
Maximilian Moehl 2025-09-04 09:19:39 +02:00 committed by Willy Tarreau
parent a6986e1cd6
commit 5d9abc68b4
2 changed files with 27 additions and 0 deletions

View 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 aes_gcm_enc(bits,nonce,key,aead_tag) binary binary
and(value) integer integer and(value) integer integer
b64dec string binary b64dec string binary
base2 binary string
base64 binary string base64 binary string
be2dec(separator,chunk_size[,truncate]) binary string be2dec(separator,chunk_size[,truncate]) binary string
le2dec(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 For base64url("URL and Filename Safe Alphabet" (RFC 4648)) variant
see "ub64dec". 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 base64
Converts a binary input sample to a base64 string. It is used to log or 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. transfer binary content in a way that can be reliably transferred (e.g.

View File

@ -2151,6 +2151,25 @@ static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void
return 1; 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) static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
{ {
long long int n = 0; 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 }, { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
{ "lower", sample_conv_str2lower, 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 }, { "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 }, { "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 }, { "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 }, { "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },