aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/ndpi_main.c9
-rw-r--r--src/lib/third_party/src/hash.c83
2 files changed, 47 insertions, 45 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index 7767f0b74..8f4cc8660 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -2120,7 +2120,7 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(void) {
ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
#endif
- ndpi_str->custom_categories.hostnames_hash = (void*)ht_create(65536);
+ ndpi_str->custom_categories.hostnames_hash = NULL;
ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */);
ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
@@ -3924,7 +3924,12 @@ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct
return(-1);
if(!ndpi_struct->enable_category_substring_match) {
- ht_set((hashtable_t*)ndpi_struct->custom_categories.hostnames_hash, name, (u_int16_t)category);
+ if(ndpi_struct->custom_categories.hostnames_hash == NULL)
+ ndpi_struct->custom_categories.hostnames_hash = (void*)ht_create(65536);
+
+ if(ndpi_struct->custom_categories.hostnames_hash)
+ ht_set((hashtable_t*)ndpi_struct->custom_categories.hostnames_hash, name, (u_int16_t)category);
+
return(0);
} else {
AC_PATTERN_t ac_pattern;
diff --git a/src/lib/third_party/src/hash.c b/src/lib/third_party/src/hash.c
index 510fce7e5..792d6086e 100644
--- a/src/lib/third_party/src/hash.c
+++ b/src/lib/third_party/src/hash.c
@@ -10,25 +10,24 @@
/* #define HASH_DEBUG 1 */
/* Create a new hashtable. */
-hashtable_t *ht_create( int size ) {
+hashtable_t *ht_create(int size) {
hashtable_t *hashtable = NULL;
int i;
- if( size < 1 ) return NULL;
+ if(size < 1) return NULL;
/* Allocate the table itself. */
- if( ( hashtable = ndpi_malloc( sizeof( hashtable_t ) ) ) == NULL ) {
+ if((hashtable = ndpi_malloc(sizeof(hashtable_t))) == NULL)
return NULL;
- }
/* Allocate pointers to the head nodes. */
- if( ( hashtable->table = ndpi_malloc( sizeof( entry_t * ) * size ) ) == NULL ) {
+ if((hashtable->table = ndpi_malloc(sizeof(entry_t *) * size)) == NULL)
return NULL;
+ else {
+ for(i = 0; i < size; i++)
+ hashtable->table[i] = NULL;
}
- for( i = 0; i < size; i++ ) {
- hashtable->table[i] = NULL;
- }
-
+
hashtable->size = size;
return hashtable;
@@ -37,13 +36,12 @@ hashtable_t *ht_create( int size ) {
/* **************************************************** */
/* Hash a string for a particular hash table. */
-int ht_hash( hashtable_t *hashtable, char *key ) {
-
- unsigned long int hashval;
+int ht_hash(hashtable_t *hashtable, char *key) {
+ unsigned long int hashval = 0;
int i = 0;
/* Convert our string to an integer */
- while( hashval < ULONG_MAX && i < strlen( key ) ) {
+ while(hashval < ULONG_MAX && i < strlen(key)) {
hashval = hashval << 8;
hashval += key[ i ];
i++;
@@ -55,16 +53,14 @@ int ht_hash( hashtable_t *hashtable, char *key ) {
/* **************************************************** */
/* Create a key-value pair. */
-entry_t *ht_newpair( char *key, u_int16_t value ) {
+entry_t *ht_newpair(char *key, u_int16_t value) {
entry_t *newpair;
- if( ( newpair = ndpi_malloc( sizeof( entry_t ) ) ) == NULL ) {
- return NULL;
- }
-
- if( ( newpair->key = ndpi_strdup( key ) ) == NULL ) {
- return NULL;
- }
+ if((newpair = ndpi_malloc(sizeof(entry_t))) == NULL)
+ return NULL;
+
+ if((newpair->key = ndpi_strdup(key)) == NULL)
+ return NULL;
newpair->value = value, newpair->next = NULL;
@@ -74,7 +70,7 @@ entry_t *ht_newpair( char *key, u_int16_t value ) {
/* **************************************************** */
/* Insert a key-value pair into a hash table. */
-void ht_set( hashtable_t *hashtable, char *key, u_int16_t value ) {
+void ht_set(hashtable_t *hashtable, char *key, u_int16_t value) {
int bin = 0;
entry_t *newpair = NULL;
entry_t *next = NULL;
@@ -84,30 +80,30 @@ void ht_set( hashtable_t *hashtable, char *key, u_int16_t value ) {
printf("*** %s() %s = %u ***\n", __FUNCTION__, key, value);
#endif
- bin = ht_hash( hashtable, key );
+ bin = ht_hash(hashtable, key);
next = hashtable->table[ bin ];
- while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
+ while(next != NULL && next->key != NULL && strcmp(key, next->key) > 0) {
last = next;
next = next->next;
}
/* There's already a pair. Let's replace that string. */
- if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {
+ if(next != NULL && next->key != NULL && strcmp(key, next->key) == 0) {
next->value = value;
/* Nope, could't find it. Time to grow a pair. */
} else {
- newpair = ht_newpair( key, value );
+ newpair = ht_newpair(key, value);
/* We're at the start of the linked list in this bin. */
- if( next == hashtable->table[ bin ] ) {
+ if(next == hashtable->table[ bin ]) {
newpair->next = next;
hashtable->table[ bin ] = newpair;
/* We're at the end of the linked list in this bin. */
- } else if ( next == NULL ) {
+ } else if (next == NULL) {
last->next = newpair;
/* We're in the middle of the list. */
@@ -121,7 +117,7 @@ void ht_set( hashtable_t *hashtable, char *key, u_int16_t value ) {
/* **************************************************** */
/* Retrieve a key-value pair from a hash table. */
-u_int16_t ht_get( hashtable_t *hashtable, char *key ) {
+u_int16_t ht_get(hashtable_t *hashtable, char *key) {
int bin = 0;
entry_t *pair;
@@ -129,16 +125,16 @@ u_int16_t ht_get( hashtable_t *hashtable, char *key ) {
printf("*** %s() %s = %u ***\n", __FUNCTION__, key, pair->value);
#endif
- bin = ht_hash( hashtable, key );
+ bin = ht_hash(hashtable, key);
/* Step through the bin, looking for our value. */
pair = hashtable->table[ bin ];
- while( pair != NULL && pair->key != NULL && strcmp( key, pair->key ) > 0 ) {
+ while(pair != NULL && pair->key != NULL && strcmp(key, pair->key) > 0) {
pair = pair->next;
}
/* Did we actually find anything? */
- if( pair == NULL || pair->key == NULL || strcmp( key, pair->key ) != 0 ) {
+ if(pair == NULL || pair->key == NULL || strcmp(key, pair->key) != 0) {
return 0;
} else {
return pair->value;
@@ -149,7 +145,7 @@ u_int16_t ht_get( hashtable_t *hashtable, char *key ) {
void ht_free(hashtable_t *hashtable) {
int i;
-
+
for(i=0; i<hashtable->size; i++) {
struct entry_s *t = hashtable->table[i];
@@ -163,6 +159,7 @@ void ht_free(hashtable_t *hashtable) {
}
}
+ ndpi_free(hashtable->table);
ndpi_free(hashtable);
}
@@ -170,18 +167,18 @@ void ht_free(hashtable_t *hashtable) {
#ifdef HASH_TEST
-int main( int argc, char **argv ) {
- hashtable_t *hashtable = ht_create( 65536 );
+int main(int argc, char **argv) {
+ hashtable_t *hashtable = ht_create(65536);
- ht_set( hashtable, "key1", 32 );
- ht_set( hashtable, "key2", 34 );
- ht_set( hashtable, "key3", 124 );
- ht_set( hashtable, "key4", 98 );
+ ht_set(hashtable, "key1", 32);
+ ht_set(hashtable, "key2", 34);
+ ht_set(hashtable, "key3", 124);
+ ht_set(hashtable, "key4", 98);
- printf( "%u\n", ht_get( hashtable, "key1" ) );
- printf( "%u\n", ht_get( hashtable, "key2" ) );
- printf( "%u\n", ht_get( hashtable, "key3" ) );
- printf( "%u\n", ht_get( hashtable, "key4" ) );
+ printf("%u\n", ht_get(hashtable, "key1"));
+ printf("%u\n", ht_get(hashtable, "key2"));
+ printf("%u\n", ht_get(hashtable, "key3"));
+ printf("%u\n", ht_get(hashtable, "key4"));
return 0;
}