Skip to content

Commit 3d2171d

Browse files
committed
add tfw_hash_str() that takes the maximum length for calculate hash
1 parent e30a57f commit 3d2171d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tempesta_fw/hash.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,26 @@
3030
* properly stored/restored in the caller.
3131
*/
3232
unsigned long
33-
tfw_hash_str(const TfwStr *str)
33+
tfw_hash_str_len(const TfwStr *str, unsigned long str_len)
3434
{
3535
unsigned long crc0 = 0, crc1 = 0;
3636

3737
if (likely(TFW_STR_PLAIN(str))) {
38-
__hash_calc(&crc0, &crc1, str->data, str->len);
38+
__hash_calc(&crc0, &crc1, str->data, min(str->len, str_len));
3939
}
4040
else {
4141
const TfwStr *c = str->chunks;
4242
const TfwStr *end = c + str->nchunks;
4343
unsigned char *p, *e;
4444
unsigned int tail = 0;
4545

46-
while (c < end) {
46+
while (c < end && str_len) {
47+
unsigned long len;
48+
4749
p = c->data;
48-
e = p + c->len;
50+
len = min(c->len, str_len);
51+
e = p + len;
52+
str_len -= len;
4953

5054
if (tail) {
5155
for ( ; tail < 8; ++p, ++tail) {
@@ -69,3 +73,9 @@ tfw_hash_str(const TfwStr *str)
6973

7074
return (crc1 << 32) | crc0;
7175
}
76+
77+
unsigned long
78+
tfw_hash_str(const TfwStr *str)
79+
{
80+
return tfw_hash_str_len(str, ULONG_MAX);
81+
}

0 commit comments

Comments
 (0)