]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: manage binary to string type convertion in stick-table and samples.
authorEmeric Brun <ebrun@exceliance.fr>
Wed, 17 Oct 2012 11:36:06 +0000 (13:36 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 22 Oct 2012 16:54:15 +0000 (18:54 +0200)
Binary type is converted to a null terminated hexa string.

src/sample.c
src/stick_table.c

index 3eef4c3342833c72a6c925b8b5a37669075cdcce..b9e3c18b43fede32c18a8532c8e894c6e970a379 100644 (file)
@@ -187,6 +187,22 @@ static int c_str2ipv6(struct sample *smp)
        return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6);
 }
 
+static int c_bin2str(struct sample *smp)
+{
+       struct chunk *trash = get_trash_chunk();
+       unsigned char c;
+       int ptr = 0;
+
+       trash->len = 0;
+       while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
+               c = smp->data.str.str[ptr++];
+               trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
+               trash->str[trash->len++] = hextab[c & 0xF];
+       }
+       smp->data.str = *trash;
+       return 1;
+}
+
 static int c_int2str(struct sample *smp)
 {
        struct chunk *trash = get_trash_chunk();
@@ -253,9 +269,9 @@ static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
 /*       IPV4 */ { NULL,      c_ip2int,  c_ip2int,  c_none,   c_ip2ipv6,  c_ip2str,   NULL,      c_ip2str,   NULL   },
 /*       IPV6 */ { NULL,      NULL,      NULL,      NULL,     c_none,     c_ipv62str, NULL,      c_ipv62str, NULL   },
 /*        STR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_none,     c_none,    c_none,     c_none },
-/*        BIN */ { NULL,      NULL,      NULL,      NULL,     NULL,       NULL,       c_none,    NULL,       c_none },
+/*        BIN */ { NULL,      NULL,      NULL,      NULL,     NULL,       c_bin2str,  c_none,    c_bin2str,  c_none },
 /*       CSTR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_datadup,  c_datadup, c_none,     c_none },
-/*       CBIN */ { NULL,      NULL,      NULL,      NULL,     NULL,       NULL,       c_datadup, NULL,       c_none },
+/*       CBIN */ { NULL,      NULL,      NULL,      NULL,     NULL,       c_bin2str,  c_datadup, c_bin2str,  c_none },
 };
 
 /*
index 52ea7cfdb54056a21aa5bb0f9e66f080d1caa65c..75267e1d38db2bf0eb0ca52429b4200cbbce0ae4 100644 (file)
@@ -504,6 +504,21 @@ static void *k_ip2str(struct sample *smp, union stktable_key_data *kdata, size_t
        return (void *)kdata->buf;
 }
 
+static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
+{
+       unsigned char c;
+       int ptr = 0;
+
+       *len = 0;
+       while (ptr < smp->data.str.len && *len <= sizeof(kdata->buf) - 2) {
+               c = smp->data.str.str[ptr++];
+               kdata->buf[(*len)++] = hextab[(c >> 4) & 0xF];
+               kdata->buf[(*len)++] = hextab[c & 0xF];
+       }
+
+       return (void *)kdata->buf;
+}
+
 static void *k_ipv62str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
 {
        if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, sizeof(kdata->buf)))
@@ -578,9 +593,9 @@ static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = {
 /*             IPV4 */ { k_ip2ip,  k_ip2ipv6,   k_ip2int,  k_ip2str,   NULL      },
 /*             IPV6 */ { NULL,     k_ipv62ipv6, NULL,      k_ipv62str, NULL      },
 /*              STR */ { k_str2ip, k_str2ipv6,  k_str2int, k_str2str,  k_str2str },
-/*              BIN */ { NULL,     NULL,        NULL,      NULL,       k_str2str },
+/*              BIN */ { NULL,     NULL,        NULL,      k_bin2str,  k_str2str },
 /*             CSTR */ { k_str2ip, k_str2ipv6,  k_str2int, k_str2str,  k_str2str },
-/*             CBIN */ { NULL,     NULL,        NULL,      NULL     ,  k_str2str },
+/*             CBIN */ { NULL,     NULL,        NULL,      k_bin2str,  k_str2str },
 };