Code cleanup

This commit is contained in:
Luca Deri
2023-08-31 09:14:17 +02:00
parent f824939662
commit 16b0ce3710
6 changed files with 27 additions and 184 deletions
+2 -15
View File
@@ -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);
@@ -2058,20 +2059,6 @@ extern "C" {
size_t ndpi_filter_size(ndpi_filter *f);
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);
/* ******************************* */
/*
-5
View File
@@ -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
/* **************************************** */
-23
View File
@@ -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;
+2 -2
View File
@@ -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)));
}
/* ******************************************* */
-139
View File
@@ -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);
}
+23
View File
@@ -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);
}