-/* @(#) $Id: hash_op.c,v 1.5 2009/06/24 18:53:08 dcid Exp $ */
+/* @(#) $Id: ./src/shared/hash_op.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All rights reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation.
*
* 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"
self->initial_seed = os_getprime(random() % self->rows);
self->constant = os_getprime(random() % self->rows);
-
+
return(self);
}
int i = 0;
OSHashNode *curr_node;
OSHashNode *next_node;
-
-
+
+
/* Freeing each entry */
while(i <= self->rows)
{
free(self->table);
free(self);
- return(NULL);
+ return(NULL);
}
return(1);
}
-
+
/* Getting next prime */
self->rows = os_getprime(new_size);
if(self->rows == 0)
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)
}
+/** 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.
OSHashNode *curr_node;
OSHashNode *new_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];
curr_node = curr_node->next;
}
-
+
/* Creating new node */
new_node = calloc(1, sizeof(OSHashNode));
if(!new_node)
new_node->next = self->table[index];
self->table[index] = new_node;
}
-
+
return(2);
}
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;
-
+
/* Getting entry */
curr_node = self->table[index];
{
return(curr_node->data);
}
-
+
curr_node = curr_node->next;
}