X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=ossec-hids.git;a=blobdiff_plain;f=src%2Fshared%2Fhash_op.c;h=20b7392e550bd1aa33b736e91b799b1b2a645734;hp=322fb1cbbd5c94ffb8d67c85672b00b23fe417b3;hb=6ef2f786c6c8ead94841b5f93baf9f43421f08c8;hpb=301048b51990573e58a30dc4a5bb4ec285cad554 diff --git a/src/shared/hash_op.c b/src/shared/hash_op.c index 322fb1c..20b7392 100755 --- a/src/shared/hash_op.c +++ b/src/shared/hash_op.c @@ -1,4 +1,5 @@ -/* @(#) $Id$ */ +/* @(#) $Id: ./src/shared/hash_op.c, 2011/09/08 dcid Exp $ + */ /* Copyright (C) 2009 Trend Micro Inc. * All rights reserved. @@ -11,9 +12,9 @@ * License details at the LICENSE file included with OSSEC or * online at: http://www.ossec.net/en/licensing.html */ - -/* Common API for dealing with hashes/maps */ + +/* Common API for dealing with hashes/maps */ #include "shared.h" @@ -67,7 +68,7 @@ OSHash *OSHash_Create() self->initial_seed = os_getprime(random() % self->rows); self->constant = os_getprime(random() % self->rows); - + return(self); } @@ -81,8 +82,8 @@ void *OSHash_Free(OSHash *self) int i = 0; OSHashNode *curr_node; OSHashNode *next_node; - - + + /* Freeing each entry */ while(i <= self->rows) { @@ -102,7 +103,7 @@ void *OSHash_Free(OSHash *self) free(self->table); free(self); - return(NULL); + return(NULL); } @@ -143,7 +144,7 @@ int OSHash_setSize(OSHash *self, int new_size) return(1); } - + /* Getting next prime */ self->rows = os_getprime(new_size); if(self->rows == 0) @@ -151,7 +152,7 @@ int OSHash_setSize(OSHash *self, int new_size) return(0); } - + /* If we fail, the hash should not be used anymore */ self->table = realloc(self->table, (self->rows +1) * sizeof(OSHashNode *)); if(!self->table) @@ -175,6 +176,44 @@ int OSHash_setSize(OSHash *self, int new_size) } +/** int OSHash_Update(OSHash *self, char *key, void *data) + * Returns 0 on error (not found). + * Returns 1 on successduplicated key (not added) + * Key must not be NULL. + */ +int OSHash_Update(OSHash *self, char *key, void *data) +{ + unsigned int hash_key; + unsigned int index; + + OSHashNode *curr_node; + + + /* Generating hash of the message */ + hash_key = _os_genhash(self, key); + + + /* Getting array index */ + index = hash_key % self->rows; + + + /* Checking for duplicated entries in the index */ + curr_node = self->table[index]; + while(curr_node) + { + /* Checking for duplicated key -- not adding */ + if(strcmp(curr_node->key, key) == 0) + { + free(curr_node->data); + curr_node->data = data; + return(1); + } + curr_node = curr_node->next; + } + return(0); +} + + /** int OSHash_Add(OSHash *self, char *key, void *data) * Returns 0 on error. @@ -189,7 +228,7 @@ int OSHash_Add(OSHash *self, char *key, void *data) OSHashNode *curr_node; OSHashNode *new_node; - + /* Generating hash of the message */ hash_key = _os_genhash(self, key); @@ -197,7 +236,7 @@ int OSHash_Add(OSHash *self, char *key, void *data) /* Getting array index */ index = hash_key % self->rows; - + /* Checking for duplicated entries in the index */ curr_node = self->table[index]; @@ -212,7 +251,7 @@ int OSHash_Add(OSHash *self, char *key, void *data) curr_node = curr_node->next; } - + /* Creating new node */ new_node = calloc(1, sizeof(OSHashNode)); if(!new_node) @@ -235,7 +274,7 @@ int OSHash_Add(OSHash *self, char *key, void *data) new_node->next = self->table[index]; self->table[index] = new_node; } - + return(2); } @@ -252,7 +291,7 @@ void *OSHash_Get(OSHash *self, char *key) unsigned int index; OSHashNode *curr_node; - + /* Generating hash of the message */ hash_key = _os_genhash(self, key); @@ -260,7 +299,7 @@ void *OSHash_Get(OSHash *self, char *key) /* Getting array index */ index = hash_key % self->rows; - + /* Getting entry */ curr_node = self->table[index]; @@ -271,7 +310,7 @@ void *OSHash_Get(OSHash *self, char *key) { return(curr_node->data); } - + curr_node = curr_node->next; }