From b8a709cbe7b95d7921c6690a456e08034c6da350 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Mon, 27 Sep 2010 14:57:33 +0200 Subject: [PATCH] Fix setting hash size in the config for b3g. Part of fix for bug #222. --- src/util-mpm-b3g.c | 39 +++++++++++++++++++++++++++++++++++---- src/util-mpm-b3g.h | 18 ++++++++++++------ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/util-mpm-b3g.c b/src/util-mpm-b3g.c index 2b45c61770..a76135aaee 100644 --- a/src/util-mpm-b3g.c +++ b/src/util-mpm-b3g.c @@ -49,8 +49,12 @@ static uint32_t b3g_hash_size = 0; static uint32_t b3g_bloom_size = 0; +static uint8_t b3g_hash_shift = 0; +static uint8_t b3g_hash_shift2 = 0; static void *b3g_func; +#define B3G_HASH(a,b,c) (((a) << b3g_hash_shift) | (b) << (b3g_hash_shift2) |(c)) + void B3gInitCtx (MpmCtx *, int); void B3gThreadInitCtx(MpmCtx *, MpmThreadCtx *, uint32_t); void B3gDestroyCtx(MpmCtx *); @@ -419,7 +423,7 @@ static void B3gPrepareHash(MpmCtx *mpm_ctx) { } ctx->pat_1_cnt++; } else if(ctx->parray[i]->len == 2) { - idx = (uint16_t)(ctx->parray[i]->ci[0] << B3G_HASHSHIFT | ctx->parray[i]->ci[1]); + idx = (uint16_t)(ctx->parray[i]->ci[0] << b3g_hash_shift | ctx->parray[i]->ci[1]); if (ctx->hash2[idx] == NULL) { B3gHashItem *hi = B3gAllocHashItem(mpm_ctx); if (hi == NULL) @@ -676,8 +680,35 @@ void B3gGetConfig() } } - if (hash_val != NULL) + if (hash_val != NULL) { b3g_hash_size = MpmGetHashSize(hash_val); + switch (b3g_hash_size) { + case HASHSIZE_LOWEST: + b3g_hash_shift = B3G_HASHSHIFT_LOWEST; + b3g_hash_shift2 = B3G_HASHSHIFT_LOWEST2; + break; + case HASHSIZE_LOW: + b3g_hash_shift = B3G_HASHSHIFT_LOW; + b3g_hash_shift2 = B3G_HASHSHIFT_LOW2; + break; + case HASHSIZE_MEDIUM: + b3g_hash_shift = B3G_HASHSHIFT_MEDIUM; + b3g_hash_shift2 = B3G_HASHSHIFT_MEDIUM2; + break; + case HASHSIZE_HIGH: + b3g_hash_shift = B3G_HASHSHIFT_HIGH; + b3g_hash_shift2 = B3G_HASHSHIFT_HIGH2; + break; + case HASHSIZE_HIGHEST: + b3g_hash_shift = B3G_HASHSHIFT_HIGHEST; + b3g_hash_shift2 = B3G_HASHSHIFT_HIGHEST2; + break; + case HASHSIZE_MAX: + b3g_hash_shift = B3G_HASHSHIFT_MAX; + b3g_hash_shift2 = B3G_HASHSHIFT_MAX2; + break; + } + } if (bloom_val != NULL) b3g_bloom_size = MpmGetBloomSize(bloom_val); @@ -1061,7 +1092,7 @@ uint32_t B3gSearch12(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, PatternMatch if (buf != bufend) { /* save one conversion by reusing h8 */ - uint16_t h16 = (uint16_t)(h8 << B3G_HASHSHIFT | u8_tolower(*(buf+1))); + uint16_t h16 = (uint16_t)(h8 << b3g_hash_shift | u8_tolower(*(buf+1))); hi = ctx->hash2[h16]; for (thi = hi; thi != NULL; thi = thi->nxt) { @@ -1105,7 +1136,7 @@ uint32_t B3gSearch2(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, PatternMatche //printf("BUF "); prt(buf,buflen); printf("\n"); while (buf <= bufend) { - uint16_t h = u8_tolower(*buf) << B3G_HASHSHIFT | u8_tolower(*(buf+1)); + uint16_t h = u8_tolower(*buf) << b3g_hash_shift | u8_tolower(*(buf+1)); hi = ctx->hash2[h]; if (hi != NULL) { diff --git a/src/util-mpm-b3g.h b/src/util-mpm-b3g.h index e0600076b7..dd9a35a216 100644 --- a/src/util-mpm-b3g.h +++ b/src/util-mpm-b3g.h @@ -27,11 +27,18 @@ #include "util-mpm.h" #include "util-bloomfilter.h" -//#define B3G_HASHSHIFT 8 -//#define B3G_HASHSHIFT 7 -//#define B3G_HASHSHIFT 6 -//#define B3G_HASHSHIFT 5 -#define B3G_HASHSHIFT 4 +#define B3G_HASHSHIFT_MAX 8 +#define B3G_HASHSHIFT_MAX2 5 +#define B3G_HASHSHIFT_HIGHEST 7 +#define B3G_HASHSHIFT_HIGHEST2 4 +#define B3G_HASHSHIFT_HIGH 6 +#define B3G_HASHSHIFT_HIGH2 3 +#define B3G_HASHSHIFT_MEDIUM 5 +#define B3G_HASHSHIFT_MEDIUM2 2 +#define B3G_HASHSHIFT_LOW 4 +#define B3G_HASHSHIFT_LOW2 1 +#define B3G_HASHSHIFT_LOWEST 3 +#define B3G_HASHSHIFT_LOWEST2 1 #define B3G_TYPE uint32_t //#define B3G_TYPE uint16_t @@ -40,7 +47,6 @@ //#define B3G_WORD_SIZE 8 #define B3G_WORD_SIZE 32 -#define B3G_HASH(a,b,c) (((a)<