#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#include <openssl/lhash.h>
#undef MIN_NODES
#define MIN_NODES 16
#define UP_LOAD (2*LH_LOAD_MULT)
#define DOWN_LOAD (LH_LOAD_MULT)
typedef struct lhash_node_st {
void *data;
struct lhash_node_st *next;
#ifndef OPENSSL_NO_HASH_COMP
unsigned long hash;
#endif
} LHASH_NODE;
struct lhash_st {
LHASH_NODE **b;
LHASH_COMP_FN_TYPE comp;
LHASH_HASH_FN_TYPE hash;
unsigned int num_nodes;
unsigned int num_alloc_nodes;
unsigned int p;
unsigned int pmax;
unsigned long up_load;
unsigned long down_load;
unsigned long num_items;
int error;
} ;
static void
expand(_LHASH *lh)
{
LHASH_NODE **n, **n1, **n2, *np;
unsigned int p, i, j;
unsigned long hash, nni;
lh->num_nodes++;
p = (int)lh->p++;
n1 = &(lh->b[p]);
n2 = &(lh->b[p + (int)lh->pmax]);
*n2 = NULL;
nni = lh->num_alloc_nodes;
for (np = *n1; np != NULL; ) {
#ifndef OPENSSL_NO_HASH_COMP
hash = np->hash;
#else
hash = lh->hash(np->data);
#endif
if ((hash % nni) != p) {
*n1 = (*n1)->next;
np->next = *n2;
*n2 = np;
} else
n1 = &((*n1)->next);
np = *n1;
}
if ((lh->p) >= lh->pmax) {
j = (int)lh->num_alloc_nodes * 2;
n = reallocarray(lh->b, j, sizeof(LHASH_NODE *));
if (n == NULL) {
lh->error++;
lh->p = 0;
return;
}
for (i = (int)lh->num_alloc_nodes; i < j; i++)
n[i] = NULL;
lh->pmax = lh->num_alloc_nodes;
lh->num_alloc_nodes = j;
lh->p = 0;
lh->b = n;
}
}
static void
contract(_LHASH *lh)
{
LHASH_NODE **n, *n1, *np;
np = lh->b[lh->p + lh->pmax - 1];
lh->b[lh->p+lh->pmax - 1] = NULL;
if (lh->p == 0) {
n = reallocarray(lh->b, lh->pmax, sizeof(LHASH_NODE *));
if (n == NULL) {
lh->error++;
return;
}
lh->num_alloc_nodes /= 2;
lh->pmax /= 2;
lh->p = lh->pmax - 1;
lh->b = n;
} else
lh->p--;
lh->num_nodes--;
n1 = lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p] = np;
else {
while (n1->next != NULL)
n1 = n1->next;
n1->next = np;
}
}
static LHASH_NODE **
getrn(_LHASH *lh, const void *data, unsigned long *rhash)
{
LHASH_NODE **ret, *n1;
unsigned long hash, nn;
LHASH_COMP_FN_TYPE cf;
hash = (*(lh->hash))(data);
*rhash = hash;
nn = hash % lh->pmax;
if (nn < lh->p)
nn = hash % lh->num_alloc_nodes;
cf = lh->comp;
ret = &(lh->b[(int)nn]);
for (n1 = *ret; n1 != NULL; n1 = n1->next) {
#ifndef OPENSSL_NO_HASH_COMP
if (n1->hash != hash) {
ret = &(n1->next);
continue;
}
#endif
if (cf(n1->data, data) == 0)
break;
ret = &(n1->next);
}
return (ret);
}
_LHASH *
lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
{
_LHASH *ret;
if ((ret = calloc(1, sizeof(_LHASH))) == NULL)
return NULL;
if ((ret->b = calloc(MIN_NODES, sizeof(LHASH_NODE *))) == NULL) {
free(ret);
return 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->pmax = MIN_NODES / 2;
ret->up_load = UP_LOAD;
ret->down_load = DOWN_LOAD;
return (ret);
}
LCRYPTO_ALIAS(lh_new);
void
lh_free(_LHASH *lh)
{
unsigned int i;
LHASH_NODE *n, *nn;
if (lh == NULL)
return;
for (i = 0; i < lh->num_nodes; i++) {
n = lh->b[i];
while (n != NULL) {
nn = n->next;
free(n);
n = nn;
}
}
free(lh->b);
free(lh);
}
LCRYPTO_ALIAS(lh_free);
int
lh_error(_LHASH *lh)
{
return lh->error;
}
LCRYPTO_ALIAS(lh_error);
void *
lh_insert(_LHASH *lh, void *data)
{
unsigned long hash;
LHASH_NODE *nn, **rn;
void *ret;
lh->error = 0;
if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
expand(lh);
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
if ((nn = malloc(sizeof(LHASH_NODE))) == 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_items++;
}
else
{
ret = (*rn)->data;
(*rn)->data = data;
}
return (ret);
}
LCRYPTO_ALIAS(lh_insert);
void *
lh_delete(_LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
free(nn);
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
}
LCRYPTO_ALIAS(lh_delete);
void *
lh_retrieve(_LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
return (NULL);
} else {
ret = (*rn)->data;
}
return (ret);
}
LCRYPTO_ALIAS(lh_retrieve);
static void
doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
{
LHASH_NODE *a, *n;
int down_load;
int i;
if (lh == NULL)
return;
down_load = lh->down_load;
lh->down_load = 0;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
lh->down_load = down_load;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
}
void
lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
{
doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
}
LCRYPTO_ALIAS(lh_doall);
void
lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
{
doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
}
LCRYPTO_ALIAS(lh_doall_arg);
unsigned long
lh_strhash(const char *c)
{
unsigned long ret = 0;
unsigned long n, v;
unsigned int r;
if (c == NULL || *c == '\0')
return ret;
n = 0x100;
while (*c) {
v = n | *c;
n += 0x100;
if ((r = ((v >> 2) ^ v) & 0x0f) != 0)
ret = (ret << r) | (ret >> (32 - r));
ret &= 0xFFFFFFFFUL;
ret ^= v * v;
c++;
}
return (ret >> 16) ^ ret;
}
LCRYPTO_ALIAS(lh_strhash);
unsigned long
lh_num_items(const _LHASH *lh)
{
return lh ? lh->num_items : 0;
}
LCRYPTO_ALIAS(lh_num_items);