]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: samples: adds the bytes converter.
authorEmeric Brun <ebrun@haproxy.com>
Mon, 3 Nov 2014 14:32:43 +0000 (15:32 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 24 Nov 2014 16:44:02 +0000 (17:44 +0100)
bytes(<offset>[,<length>])
  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.

doc/configuration.txt
src/sample.c

index ec412d4c911c5f5ecfa843a75a38ef5134d261ff..0892bc254cac24b22a83364635d7b5a2ee99e3e1 100644 (file)
@@ -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(<offset>[,<length>])
+  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([<avalanche>])
   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
index be00c3c182ae856ce54b707b61ed6e3e6bf34411..02e39616687c032be90cf5a36da83d2d9c80e579 100644 (file)
@@ -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 },
 }};