#include <openssl/crypto.h>
#include <openssl/lhash.h>
-const char lh_version[] = "lhash" OPENSSL_VERSION_PTEXT;
-
#undef MIN_NODES
#define MIN_NODES 16
#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
{
_LHASH *ret;
- int i;
- if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)
+ if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
goto err0;
- if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)
+ if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
goto err1;
- for (i = 0; i < MIN_NODES; i++)
- ret->b[i] = NULL;
ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
ret->num_nodes = MIN_NODES / 2;
ret->num_alloc_nodes = MIN_NODES;
- ret->p = 0;
ret->pmax = MIN_NODES / 2;
ret->up_load = UP_LOAD;
ret->down_load = DOWN_LOAD;
- ret->num_items = 0;
-
- ret->num_expands = 0;
- ret->num_expand_reallocs = 0;
- ret->num_contracts = 0;
- ret->num_contract_reallocs = 0;
- ret->num_hash_calls = 0;
- ret->num_comp_calls = 0;
- ret->num_insert = 0;
- ret->num_replace = 0;
- ret->num_delete = 0;
- ret->num_no_delete = 0;
- ret->num_retrieve = 0;
- ret->num_retrieve_miss = 0;
- ret->num_hash_comps = 0;
-
- ret->error = 0;
return (ret);
+
err1:
OPENSSL_free(ret);
err0:
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
- if ((nn = (LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) {
+ if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
lh->error++;
return (NULL);
}
nn->data = data;
nn->next = NULL;
-#ifndef OPENSSL_NO_HASH_COMP
nn->hash = hash;
-#endif
*rn = nn;
ret = NULL;
lh->num_insert++;
nni = lh->num_alloc_nodes;
for (np = *n1; np != NULL;) {
-#ifndef OPENSSL_NO_HASH_COMP
hash = np->hash;
-#else
- hash = lh->hash(np->data);
- lh->num_hash_calls++;
-#endif
if ((hash % nni) != p) { /* move it */
*n1 = (*n1)->next;
np->next = *n2;
if ((lh->p) >= lh->pmax) {
j = (int)lh->num_alloc_nodes * 2;
- n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
- (int)(sizeof(LHASH_NODE *) * j));
+ n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
if (n == NULL) {
-/* fputs("realloc error in lhash",stderr); */
+ /* fputs("realloc error in lhash",stderr); */
lh->error++;
lh->p = 0;
return;
}
- /* else */
for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
n[i] = NULL; /* 02/03/92 eay */
lh->pmax = lh->num_alloc_nodes;
np = lh->b[lh->p + lh->pmax - 1];
lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
if (lh->p == 0) {
- n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
- (unsigned int)(sizeof(LHASH_NODE *)
- * lh->pmax));
+ n = OPENSSL_realloc(lh->b,
+ (unsigned int)(sizeof(LHASH_NODE *) * lh->pmax));
if (n == NULL) {
-/* fputs("realloc error in lhash",stderr); */
+ /* fputs("realloc error in lhash",stderr); */
lh->error++;
return;
}
cf = lh->comp;
ret = &(lh->b[(int)nn]);
for (n1 = *ret; n1 != NULL; n1 = n1->next) {
-#ifndef OPENSSL_NO_HASH_COMP
lh->num_hash_comps++;
if (n1->hash != hash) {
ret = &(n1->next);
continue;
}
-#endif
lh->num_comp_calls++;
if (cf(n1->data, data) == 0)
break;
if ((c == NULL) || (*c == '\0'))
return (ret);
/*-
- unsigned char b[16];
- MD5(c,strlen(c),b);
- return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
+ unsigned char b[16];
+ MD5(c,strlen(c),b);
+ return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
*/
n = 0x100;
{
return lh ? lh->num_items : 0;
}
+
+unsigned long lh_get_down_load(const _LHASH *lh)
+{
+ return lh->down_load;
+}
+
+void lh_set_down_load(_LHASH *lh, unsigned long down_load)
+{
+ lh->down_load = down_load;
+}
+
+int lh_error(_LHASH *lh)
+{
+ return lh->error;
+}