From: Emeric Brun Date: Mon, 3 Nov 2014 14:32:43 +0000 (+0100) Subject: MINOR: samples: adds the bytes converter. X-Git-Tag: v1.6-dev1~260 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=54c4ac8417aeb8735a0a07952a8ba563ed4e62b5;p=thirdparty%2Fhaproxy.git MINOR: samples: adds the bytes converter. bytes([,]) Extracts a some bytes from an input binary sample. The result is a binary sample starting at an offset (in bytes) of the original sample and optionnaly truncated at the given length. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index ec412d4c91..0892bc254c 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -10036,6 +10036,11 @@ base64 transfer binary content in a way that can be reliably transferred (eg: an SSL ID can be copied in a header). +bytes([,]) + Extracts some bytes from an input binary sample. The result is a binary + sample starting at an offset (in bytes) of the original sample and + optionnaly truncated at the given length. + djb2([]) Hashes a binary input sample into an unsigned 32-bit quantity using the DJB2 hash function. Optionally, it is possible to apply a full avalanche hash diff --git a/src/sample.c b/src/sample.c index be00c3c182..02e3961668 100644 --- a/src/sample.c +++ b/src/sample.c @@ -1562,6 +1562,27 @@ static int sample_conv_json(const struct arg *arg_p, struct sample *smp) return 1; } +/* This sample function is designed to extract some bytes from an input buffer. + * First arg is the offset. + * Optional second arg is the length to truncate */ +static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp) +{ + if (smp->data.str.len <= arg_p[0].data.uint) { + smp->data.str.len = 0; + return 1; + } + + if (smp->data.str.size) + smp->data.str.size -= arg_p[0].data.uint; + smp->data.str.len -= arg_p[0].data.uint; + smp->data.str.str += arg_p[0].data.uint; + + if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len)) + smp->data.str.len = arg_p[1].data.uint; + + return 1; +} + /************************************************************************/ /* All supported sample fetch functions must be declared here */ /************************************************************************/ @@ -1703,6 +1724,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, + { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN }, { NULL, NULL, 0, 0, 0 }, }};