diff options
author | Luca Deri <deri@ntop.org> | 2023-08-31 09:14:17 +0200 |
---|---|---|
committer | Luca Deri <deri@ntop.org> | 2023-08-31 09:14:17 +0200 |
commit | 16b0ce37100242b6cbaafe65d05ee4940b5aab3f (patch) | |
tree | 3275f107daeb62c09da6b2409db01aa381a49b27 | |
parent | f82493966286e4ec88f909baa5b5066df12f73e6 (diff) |
Code cleanup
-rw-r--r-- | src/include/ndpi_api.h | 17 | ||||
-rw-r--r-- | src/include/ndpi_typedefs.h | 5 | ||||
-rw-r--r-- | src/lib/ndpi_domain_classify.c | 23 | ||||
-rw-r--r-- | src/lib/ndpi_filter.c | 4 | ||||
-rw-r--r-- | src/lib/ndpi_string_search.c | 139 | ||||
-rw-r--r-- | src/lib/ndpi_utils.c | 23 |
6 files changed, 27 insertions, 184 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index a1f4e387b..fa7f77f33 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -1799,7 +1799,8 @@ extern "C" { u_int32_t ndpi_crc32(const void* data, size_t n_bytes); u_int32_t ndpi_nearest_power_of_two(u_int32_t x); - + u_int32_t ndpi_hash_string(char *str); + /* ******************************* */ int ndpi_des_init(struct ndpi_des_struct *des, double alpha, double beta, float significance); @@ -2059,20 +2060,6 @@ extern "C" { u_int32_t ndpi_filter_cardinality(ndpi_filter *f); /* ******************************* */ - - /* - Efficient (space and speed) probabilitic datastructure - for exact string searching with a false positive rate - of 5 * 10 ^ -8 - */ - ndpi_string_search* ndpi_string_search_alloc(); - void ndpi_string_search_free(ndpi_string_search *s); - u_int32_t ndpi_string_search_size(ndpi_string_search *s); - bool ndpi_string_search_add(ndpi_string_search *s, char *string); - bool ndpi_string_search_contains(ndpi_string_search *s, char *string); - u_int32_t ndpi_string_search_cardinality(ndpi_string_search *f); - - /* ******************************* */ /* Efficient (space and speed) probabilitic datastructure diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 75df402ef..bece570cc 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -2031,11 +2031,6 @@ typedef void ndpi_bitmap; typedef void ndpi_bitmap_iterator; typedef void ndpi_filter; -typedef struct { - ndpi_filter *filter[2 /* direct and reverse */]; -} ndpi_string_search; - - #define MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS 16 /* **************************************** */ diff --git a/src/lib/ndpi_domain_classify.c b/src/lib/ndpi_domain_classify.c index 8b66c940e..df82cdabb 100644 --- a/src/lib/ndpi_domain_classify.c +++ b/src/lib/ndpi_domain_classify.c @@ -103,29 +103,6 @@ static u_int32_t ndpi_domain_search_size(ndpi_domain_search *search) { /* ********************************************************** */ -/* - https://en.wikipedia.org/wiki/Jenkins_hash_function - - See also http://burtleburtle.net/bob/hash/spooky.html -*/ -static inline u_int32_t ndpi_hash_string(char *domain) { - u_int32_t hash, i; - - for(hash = i = 0; domain[i] != '\0'; ++i) { - hash += domain[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } - - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); - - return(hash); -} - -/* ********************************************************** */ - /* NOTE: domain will be modified: copy it if necessary */ static bool ndpi_domain_search_add(ndpi_domain_search *search, char *domain) { char *elem; diff --git a/src/lib/ndpi_filter.c b/src/lib/ndpi_filter.c index 5c4ff62bd..334d041b8 100644 --- a/src/lib/ndpi_filter.c +++ b/src/lib/ndpi_filter.c @@ -58,7 +58,7 @@ bool ndpi_filter_add(ndpi_filter *f, u_int32_t value) { /* ******************************************* */ bool ndpi_filter_add_string(ndpi_filter *f, char *string) { - return(ndpi_filter_add(f, MurmurHash(string, strlen(string), 0xD6DFE7))); + return(ndpi_filter_add(f, ndpi_hash_string(string))); } /* ******************************************* */ @@ -76,7 +76,7 @@ bool ndpi_filter_contains(ndpi_filter *f, u_int32_t value) { /* ******************************************* */ bool ndpi_filter_contains_string(ndpi_filter *f, char *string) { - return(ndpi_filter_contains(f, MurmurHash(string, strlen(string), 0xD6DFE7))); + return(ndpi_filter_contains(f, ndpi_hash_string(string))); } /* ******************************************* */ diff --git a/src/lib/ndpi_string_search.c b/src/lib/ndpi_string_search.c deleted file mode 100644 index 76c9bb0de..000000000 --- a/src/lib/ndpi_string_search.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * ndpi_string_search.c - * - * Copyright (C) 2011-23 - ntop.org and contributors - * - * This file is part of nDPI, an open source deep packet inspection - * library based on the OpenDPI and PACE technology by ipoque GmbH - * - * nDPI is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * nDPI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with nDPI. If not, see <http://www.gnu.org/licenses/>. - * - */ - - -#include <stdlib.h> -#include <errno.h> -#include <math.h> -#include <sys/types.h> - -#include "ndpi_config.h" -#include "ndpi_api.h" -#include "ndpi_includes.h" -#include "ndpi_encryption.h" - -/* ******************************************* */ - -ndpi_string_search* ndpi_string_search_alloc() { - ndpi_string_search *s = (ndpi_string_search*)ndpi_malloc(sizeof(ndpi_string_search)); - int i; - - if(!s) return(NULL); - - for(i=0; i<2; i++) - s->filter[i] = ndpi_filter_alloc(); - - return(s); -} - -/* ******************************************* */ - -void ndpi_string_search_free(ndpi_string_search *_s) { - if(_s != NULL) { - ndpi_string_search *s = (ndpi_string_search*)_s; - int i; - - for(i=0; i<2; i++) - ndpi_string_search_free(s->filter[i]); - - ndpi_free(s); - } -} - -/* ******************************************* */ - -u_int32_t ndpi_string_search_size(ndpi_string_search *s) { - if(s != NULL) { - int i; - u_int32_t total_len = 0; - - for(i=0; i<2; i++) - total_len += ndpi_filter_size(s->filter[i]); - - return(total_len); - } else - return(0); -} - -/* ******************************************* */ - -u_int32_t ndpi_string_search_cardinality(ndpi_string_search *s) { - return(s ? ndpi_filter_cardinality(s) : 0); -} - -/* ********************************************************** */ - -static u_int32_t hashval(char *domain, bool revert_string) { - u_int32_t ret = 0, shift_bit = 1; - int i; - - if(revert_string) { - i = strlen(domain) - 1; - - while(i >= 0) { - u_int32_t v = ((u_int32_t)domain[i]) << shift_bit; - - i--, ret += v; - if(++shift_bit == 25) shift_bit = 0; - } - } else { - i = 0; - - while(domain[i] != '\0') { - u_int32_t v = ((u_int32_t)domain[i]) << shift_bit; - - i++, ret += v; - if(++shift_bit == 25) shift_bit = 0; - } - } - - return(ret + i); -} - -/* ******************************************* */ - -bool ndpi_string_search_add(ndpi_string_search *s, char *string) { - if(s != NULL) { - u_int32_t h0 = hashval(string, false), h1 = hashval(string, true); - - ndpi_filter_add(s->filter[0], h0), ndpi_filter_add(s->filter[1], h1); - return(true); - } else - return(false); -} - -/* ******************************************* */ - -bool ndpi_string_search_contains(ndpi_string_search *s, char *string) { - if(s != NULL) { - u_int32_t h0 = hashval(string, false), h1; - - if(!ndpi_filter_contains(s->filter[0], h0)) return(false); - - h1 = hashval(string, true); - if(!ndpi_filter_contains(s->filter[1], h1)) return(false); - - return(true); - } else - return(false); -} diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index 35c0410e2..c62d82edf 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -3006,3 +3006,26 @@ u_int32_t ndpi_nearest_power_of_two(u_int32_t x) { x++; return(x); } + +/* ********************************************************** */ + +/* + https://en.wikipedia.org/wiki/Jenkins_hash_function + + See also http://burtleburtle.net/bob/hash/spooky.html +*/ +u_int32_t ndpi_hash_string(char *str) { + u_int32_t hash, i; + + for(hash = i = 0; str[i] != '\0'; ++i) { + hash += str[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + + return(hash); +} |