Skip to content

Commit 5d9abc6

Browse files
maxmoehlwtarreau
authored andcommitted
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.
1 parent a6986e1 commit 5d9abc6

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

doc/configuration.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19962,6 +19962,7 @@ aes_gcm_dec(bits,nonce,key,aead_tag) binary binary
1996219962
aes_gcm_enc(bits,nonce,key,aead_tag) binary binary
1996319963
and(value) integer integer
1996419964
b64dec string binary
19965+
base2 binary string
1996519966
base64 binary string
1996619967
be2dec(separator,chunk_size[,truncate]) binary string
1996719968
le2dec(separator,chunk_size[,truncate]) binary string
@@ -20186,6 +20187,12 @@ b64dec
2018620187
For base64url("URL and Filename Safe Alphabet" (RFC 4648)) variant
2018720188
see "ub64dec".
2018820189

20190+
base2
20191+
Converts a binary input sample to a binary string containing eight binary
20192+
digits per input byte. It is used to be able to perform longest prefix match
20193+
on types where the native representation does not allow prefix matching, for
20194+
example IP prefixes.
20195+
2018920196
base64
2019020197
Converts a binary input sample to a base64 string. It is used to log or
2019120198
transfer binary content in a way that can be reliably transferred (e.g.

src/sample.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,25 @@ static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void
21512151
return 1;
21522152
}
21532153

2154+
static int sample_conv_bin2base2(const struct arg *arg_p, struct sample *smp, void *private)
2155+
{
2156+
struct buffer *trash = get_trash_chunk();
2157+
unsigned char c;
2158+
int ptr = 0;
2159+
int bit = 0;
2160+
2161+
trash->data = 0;
2162+
while (ptr < smp->data.u.str.data && trash->data <= trash->size - 8) {
2163+
c = smp->data.u.str.area[ptr++];
2164+
for (bit = 7; bit >= 0; bit--)
2165+
trash->area[trash->data++] = c & (1 << bit) ? '1' : '0';
2166+
}
2167+
smp->data.u.str = *trash;
2168+
smp->data.type = SMP_T_STR;
2169+
smp->flags &= ~SMP_F_CONST;
2170+
return 1;
2171+
}
2172+
21542173
static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
21552174
{
21562175
long long int n = 0;
@@ -5441,6 +5460,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
54415460
{ "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
54425461
{ "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
54435462
{ "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
5463+
{ "base2", sample_conv_bin2base2, 0, NULL, SMP_T_BIN, SMP_T_STR },
54445464
{ "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT), sample_conv_2dec_check, SMP_T_BIN, SMP_T_STR },
54455465
{ "le2dec", sample_conv_le2dec, ARG3(1,STR,SINT,SINT), sample_conv_2dec_check, SMP_T_BIN, SMP_T_STR },
54465466
{ "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR },

0 commit comments

Comments
 (0)