Imported Upstream version 2.7
[ossec-hids.git] / src / shared / hash_op.c
index 322fb1c..20b7392 100755 (executable)
@@ -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;
     }