2 * Copyright 1995-2016 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
11 * Code for dynamic hash table routines
12 * Author - Eric Young v 2.0
14 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
15 * present. eay 18-Jun-98
17 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
19 * 2.0 eay - Fixed a bug that occurred when using lh_delete
20 * from inside lh_doall(). As entries were deleted,
21 * the 'table' was 'contract()ed', making some entries
22 * jump from the end of the table to the start, there by
23 * skipping the lh_doall() processing. eay - 4/12/95
25 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
26 * were not being free()ed. 21/11/95
28 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
31 * 1.7 eay - Removed the fputs() for realloc failures - the code
32 * should silently tolerate them. I have also fixed things
33 * lint complained about 04/05/95
35 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
37 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
39 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
41 * 1.3 eay - Fixed a few lint problems 19/3/1991
43 * 1.2 eay - Fixed lh_doall problem 13/3/1991
45 * 1.1 eay - Added lh_doall
47 * 1.0 eay - First version
52 #include <openssl/crypto.h>
53 #include <openssl/lhash.h>
57 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
58 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
60 static void expand(_LHASH *lh);
61 static void contract(_LHASH *lh);
62 static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
64 _LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
68 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
70 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
72 ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
73 ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
74 ret->num_nodes = MIN_NODES / 2;
75 ret->num_alloc_nodes = MIN_NODES;
76 ret->pmax = MIN_NODES / 2;
77 ret->up_load = UP_LOAD;
78 ret->down_load = DOWN_LOAD;
87 void lh_free(_LHASH *lh)
95 for (i = 0; i < lh->num_nodes; i++) {
107 void *lh_insert(_LHASH *lh, void *data)
110 LHASH_NODE *nn, **rn;
114 if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
117 rn = getrn(lh, data, &hash);
120 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
131 } else { /* replace same key */
140 void *lh_delete(_LHASH *lh, const void *data)
143 LHASH_NODE *nn, **rn;
147 rn = getrn(lh, data, &hash);
161 if ((lh->num_nodes > MIN_NODES) &&
162 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
168 void *lh_retrieve(_LHASH *lh, const void *data)
175 rn = getrn(lh, data, &hash);
178 lh->num_retrieve_miss++;
187 static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
188 LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
197 * reverse the order so we search from 'top to bottom' We were having
198 * memory leaks otherwise
200 for (i = lh->num_nodes - 1; i >= 0; i--) {
204 * 28/05/91 - eay - n added so items can be deleted via lh_doall
207 * 22/05/08 - ben - eh? since a is not passed, this should not be
212 func_arg(a->data, arg);
220 void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
222 doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
225 void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
227 doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
230 static void expand(_LHASH *lh)
232 LHASH_NODE **n, **n1, **n2, *np;
233 unsigned int p, i, j;
234 unsigned long hash, nni;
240 n2 = &(lh->b[p + (int)lh->pmax]);
241 *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */
242 nni = lh->num_alloc_nodes;
244 for (np = *n1; np != NULL;) {
246 if ((hash % nni) != p) { /* move it */
255 if ((lh->p) >= lh->pmax) {
256 j = (int)lh->num_alloc_nodes * 2;
257 n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
259 /* fputs("realloc error in lhash",stderr); */
264 for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
265 n[i] = NULL; /* 02/03/92 eay */
266 lh->pmax = lh->num_alloc_nodes;
267 lh->num_alloc_nodes = j;
268 lh->num_expand_reallocs++;
274 static void contract(_LHASH *lh)
276 LHASH_NODE **n, *n1, *np;
278 np = lh->b[lh->p + lh->pmax - 1];
279 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
281 n = OPENSSL_realloc(lh->b,
282 (unsigned int)(sizeof(LHASH_NODE *) * lh->pmax));
284 /* fputs("realloc error in lhash",stderr); */
288 lh->num_contract_reallocs++;
289 lh->num_alloc_nodes /= 2;
291 lh->p = lh->pmax - 1;
299 n1 = lh->b[(int)lh->p];
301 lh->b[(int)lh->p] = np;
303 while (n1->next != NULL)
309 static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
311 LHASH_NODE **ret, *n1;
312 unsigned long hash, nn;
313 LHASH_COMP_FN_TYPE cf;
315 hash = (*(lh->hash)) (data);
316 lh->num_hash_calls++;
319 nn = hash % lh->pmax;
321 nn = hash % lh->num_alloc_nodes;
324 ret = &(lh->b[(int)nn]);
325 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
326 lh->num_hash_comps++;
327 if (n1->hash != hash) {
331 lh->num_comp_calls++;
332 if (cf(n1->data, data) == 0)
340 * The following hash seems to work very well on normal text strings no
341 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
342 * as good as MD5, but still good.
344 unsigned long lh_strhash(const char *c)
346 unsigned long ret = 0;
351 if ((c == NULL) || (*c == '\0'))
356 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
363 r = (int)((v >> 2) ^ v) & 0x0f;
364 ret = (ret << r) | (ret >> (32 - r));
369 return ((ret >> 16) ^ ret);
372 unsigned long lh_num_items(const _LHASH *lh)
374 return lh ? lh->num_items : 0;
377 unsigned long lh_get_down_load(const _LHASH *lh)
379 return lh->down_load;
382 void lh_set_down_load(_LHASH *lh, unsigned long down_load)
384 lh->down_load = down_load;
387 int lh_error(_LHASH *lh)