2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/crypto.h>
14 #include <openssl/lhash.h>
15 #include "lhash_lcl.h"
20 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
21 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
23 static int expand(OPENSSL_LHASH *lh);
24 static void contract(OPENSSL_LHASH *lh);
25 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
27 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
31 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
33 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
35 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
36 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
37 ret->num_nodes = MIN_NODES / 2;
38 ret->num_alloc_nodes = MIN_NODES;
39 ret->pmax = MIN_NODES / 2;
40 ret->up_load = UP_LOAD;
41 ret->down_load = DOWN_LOAD;
50 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
53 OPENSSL_LH_NODE *n, *nn;
58 for (i = 0; i < lh->num_nodes; i++) {
70 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
73 OPENSSL_LH_NODE *nn, **rn;
77 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
78 return NULL; /* 'lh->error++' already done in 'expand' */
80 rn = getrn(lh, data, &hash);
83 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
94 } else { /* replace same key */
102 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
105 OPENSSL_LH_NODE *nn, **rn;
109 rn = getrn(lh, data, &hash);
123 if ((lh->num_nodes > MIN_NODES) &&
124 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
130 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
133 OPENSSL_LH_NODE **rn;
137 rn = getrn(lh, data, &hash);
140 lh->num_retrieve_miss++;
149 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
150 OPENSSL_LH_DOALL_FUNC func,
151 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
154 OPENSSL_LH_NODE *a, *n;
160 * reverse the order so we search from 'top to bottom' We were having
161 * memory leaks otherwise
163 for (i = lh->num_nodes - 1; i >= 0; i--) {
168 func_arg(a->data, arg);
176 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
178 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
181 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
183 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
186 static int expand(OPENSSL_LHASH *lh)
188 OPENSSL_LH_NODE **n, **n1, **n2, *np;
189 unsigned int p, i, j;
190 unsigned long hash, nni;
196 n2 = &(lh->b[p + (int)lh->pmax]);
198 nni = lh->num_alloc_nodes;
200 for (np = *n1; np != NULL;) {
202 if ((hash % nni) != p) { /* move it */
211 if ((lh->p) >= lh->pmax) {
212 j = (int)lh->num_alloc_nodes * 2;
213 n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
220 for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
221 n[i] = NULL; /* 02/03/92 eay */
222 lh->pmax = lh->num_alloc_nodes;
223 lh->num_alloc_nodes = j;
224 lh->num_expand_reallocs++;
231 static void contract(OPENSSL_LHASH *lh)
233 OPENSSL_LH_NODE **n, *n1, *np;
235 np = lh->b[lh->p + lh->pmax - 1];
236 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
238 n = OPENSSL_realloc(lh->b,
239 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
241 /* fputs("realloc error in lhash",stderr); */
245 lh->num_contract_reallocs++;
246 lh->num_alloc_nodes /= 2;
248 lh->p = lh->pmax - 1;
256 n1 = lh->b[(int)lh->p];
258 lh->b[(int)lh->p] = np;
260 while (n1->next != NULL)
266 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
267 const void *data, unsigned long *rhash)
269 OPENSSL_LH_NODE **ret, *n1;
270 unsigned long hash, nn;
271 OPENSSL_LH_COMPFUNC cf;
273 hash = (*(lh->hash)) (data);
274 lh->num_hash_calls++;
277 nn = hash % lh->pmax;
279 nn = hash % lh->num_alloc_nodes;
282 ret = &(lh->b[(int)nn]);
283 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
284 lh->num_hash_comps++;
285 if (n1->hash != hash) {
289 lh->num_comp_calls++;
290 if (cf(n1->data, data) == 0)
298 * The following hash seems to work very well on normal text strings no
299 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
300 * as good as MD5, but still good.
302 unsigned long OPENSSL_LH_strhash(const char *c)
304 unsigned long ret = 0;
309 if ((c == NULL) || (*c == '\0'))
316 r = (int)((v >> 2) ^ v) & 0x0f;
317 ret = (ret << r) | (ret >> (32 - r));
322 return (ret >> 16) ^ ret;
325 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
327 return lh ? lh->num_items : 0;
330 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
332 return lh->down_load;
335 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
337 lh->down_load = down_load;
340 int OPENSSL_LH_error(OPENSSL_LHASH *lh)