From: Willy Tarreau Date: Tue, 20 Jan 2015 18:35:24 +0000 (+0100) Subject: MINOR: samples: provide a "crc32" converter X-Git-Tag: v1.6-dev1~180 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8059977d3ecf19ab7f288b59ea4fb501b3a041ca;p=thirdparty%2Fhaproxy.git MINOR: samples: provide a "crc32" converter This converter hashes a binary input sample into an unsigned 32-bit quantity using the CRC32 hash function. Optionally, it is possible to apply a full avalanche hash function to the output if the optional argument equals 1. This converter uses the same functions as used by the various hash- based load balancing algorithms, so it will provide exactly the same results. It is provided for compatibility with other software which want a CRC32 to be computed on some input keys, so it follows the most common implementation as found in Ethernet, Gzip, PNG, etc... It is slower than the other algorithms but may provide a better or at least less predictable distribution. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 48ed813c9b..13f1f724f9 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -10123,6 +10123,19 @@ bytes([,]) sample starting at an offset (in bytes) of the original sample and optionnaly truncated at the given length. +crc32([]) + Hashes a binary input sample into an unsigned 32-bit quantity using the CRC32 + hash function. Optionally, it is possible to apply a full avalanche hash + function to the output if the optional argument equals 1. This + converter uses the same functions as used by the various hash-based load + balancing algorithms, so it will provide exactly the same results. It is + provided for compatibility with other software which want a CRC32 to be + computed on some input keys, so it follows the most common implementation as + found in Ethernet, Gzip, PNG, etc... It is slower than the other algorithms + but may provide a better or at least less predictable distribution. It must + not be used for security purposes as a 32-bit hash is trivial to break. See + also "djb2", "sdbm", "wt6" and the "hash-type" directive. + 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 @@ -10131,8 +10144,8 @@ djb2([]) balancing algorithms, so it will provide exactly the same results. It is mostly intended for debugging, but can be used as a stick-table entry to collect rough statistics. It must not be used for security purposes as a - 32-bit hash is trivial to break. See also "sdbm", "wt6" and the "hash-type" - directive. + 32-bit hash is trivial to break. See also "crc32", "sdbm", "wt6" and the + "hash-type" directive. field(,) Extracts the substring at the given index considering given delimiters from @@ -10317,8 +10330,8 @@ sdbm([]) balancing algorithms, so it will provide exactly the same results. It is mostly intended for debugging, but can be used as a stick-table entry to collect rough statistics. It must not be used for security purposes as a - 32-bit hash is trivial to break. See also "djb2", "wt6" and the "hash-type" - directive. + 32-bit hash is trivial to break. See also "crc32", "djb2", "wt6" and the + "hash-type" directive. table_bytes_in_rate() Uses the string representation of the input sample to perform a look up in @@ -10490,8 +10503,8 @@ wt6([]) balancing algorithms, so it will provide exactly the same results. It is mostly intended for debugging, but can be used as a stick-table entry to collect rough statistics. It must not be used for security purposes as a - 32-bit hash is trivial to break. See also "djb2", "sdbm", and the "hash-type" - directive. + 32-bit hash is trivial to break. See also "crc32", "djb2", "sdbm", and the + "hash-type" directive. 7.3.2. Fetching samples from internal states diff --git a/src/sample.c b/src/sample.c index 00345a8940..377c4c8f3f 100644 --- a/src/sample.c +++ b/src/sample.c @@ -1380,6 +1380,16 @@ static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp) return 1; } +/* hashes the binary input into a 32-bit unsigned int */ +static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp) +{ + smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len); + if (arg_p && arg_p->data.uint) + smp->data.uint = full_hash(smp->data.uint); + smp->type = SMP_T_UINT; + return 1; +} + /* This function escape special json characters. The returned string can be * safely set between two '"' and used as json string. The json string is * defined like this: @@ -1867,6 +1877,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 }, { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, + { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, { "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 },