2 * Copyright 1995-2018 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>
16 #include "internal/lhash.h"
17 #include "lhash_lcl.h"
20 * A hashing implementation that appears to be based on the linear hashing
22 * https://en.wikipedia.org/wiki/Linear_hashing
24 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
25 * addressing", Proc. 6th Conference on Very Large Databases: 212-223
26 * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
28 * From the wikipedia article "Linear hashing is used in the BDB Berkeley
29 * database system, which in turn is used by many software systems such as
30 * OpenLDAP, using a C implementation derived from the CACM article and first
31 * published on the Usenet in 1988 by Esmond Pitt."
33 * The CACM paper is available here:
34 * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
39 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
40 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
42 static int expand(OPENSSL_LHASH *lh);
43 static void contract(OPENSSL_LHASH *lh);
44 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
46 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
50 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54 if ((ret->retrieve_stats_lock = CRYPTO_THREAD_lock_new()) == NULL)
56 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
57 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
58 ret->num_nodes = MIN_NODES / 2;
59 ret->num_alloc_nodes = MIN_NODES;
60 ret->pmax = MIN_NODES / 2;
61 ret->up_load = UP_LOAD;
62 ret->down_load = DOWN_LOAD;
71 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
74 OPENSSL_LH_NODE *n, *nn;
79 for (i = 0; i < lh->num_nodes; i++) {
87 CRYPTO_THREAD_lock_free(lh->retrieve_stats_lock);
92 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
95 OPENSSL_LH_NODE *nn, **rn;
99 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
100 return NULL; /* 'lh->error++' already done in 'expand' */
102 rn = getrn(lh, data, &hash);
105 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
116 } else { /* replace same key */
125 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
128 OPENSSL_LH_NODE *nn, **rn;
132 rn = getrn(lh, data, &hash);
146 if ((lh->num_nodes > MIN_NODES) &&
147 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
153 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
156 OPENSSL_LH_NODE **rn;
161 rn = getrn(lh, data, &hash);
164 CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);
168 CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
173 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
174 OPENSSL_LH_DOALL_FUNC func,
175 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
178 OPENSSL_LH_NODE *a, *n;
184 * reverse the order so we search from 'top to bottom' We were having
185 * memory leaks otherwise
187 for (i = lh->num_nodes - 1; i >= 0; i--) {
192 func_arg(a->data, arg);
200 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
202 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
205 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
207 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
210 static int expand(OPENSSL_LHASH *lh)
212 OPENSSL_LH_NODE **n, **n1, **n2, *np;
213 unsigned int p, pmax, nni, j;
216 nni = lh->num_alloc_nodes;
221 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
227 memset(n + nni, 0, sizeof(*n) * (j - nni));
229 lh->num_alloc_nodes = j;
230 lh->num_expand_reallocs++;
239 n2 = &(lh->b[p + pmax]);
242 for (np = *n1; np != NULL;) {
244 if ((hash % nni) != p) { /* move it */
256 static void contract(OPENSSL_LHASH *lh)
258 OPENSSL_LH_NODE **n, *n1, *np;
260 np = lh->b[lh->p + lh->pmax - 1];
261 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
263 n = OPENSSL_realloc(lh->b,
264 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
266 /* fputs("realloc error in lhash",stderr); */
270 lh->num_contract_reallocs++;
271 lh->num_alloc_nodes /= 2;
273 lh->p = lh->pmax - 1;
281 n1 = lh->b[(int)lh->p];
283 lh->b[(int)lh->p] = np;
285 while (n1->next != NULL)
291 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
292 const void *data, unsigned long *rhash)
294 OPENSSL_LH_NODE **ret, *n1;
295 unsigned long hash, nn;
296 OPENSSL_LH_COMPFUNC cf;
299 hash = (*(lh->hash)) (data);
300 CRYPTO_atomic_add(&lh->num_hash_calls, 1, &scratch, lh->retrieve_stats_lock);
303 nn = hash % lh->pmax;
305 nn = hash % lh->num_alloc_nodes;
308 ret = &(lh->b[(int)nn]);
309 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
310 CRYPTO_atomic_add(&lh->num_hash_comps, 1, &scratch, lh->retrieve_stats_lock);
311 if (n1->hash != hash) {
315 CRYPTO_atomic_add(&lh->num_comp_calls, 1, &scratch, lh->retrieve_stats_lock);
316 if (cf(n1->data, data) == 0)
324 * The following hash seems to work very well on normal text strings no
325 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
326 * as good as MD5, but still good.
328 unsigned long OPENSSL_LH_strhash(const char *c)
330 unsigned long ret = 0;
335 if ((c == NULL) || (*c == '\0'))
340 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
347 r = (int)((v >> 2) ^ v) & 0x0f;
348 ret = (ret << r) | (ret >> (32 - r));
353 return ((ret >> 16) ^ ret);
356 unsigned long openssl_lh_strcasehash(const char *c)
358 unsigned long ret = 0;
363 if (c == NULL || *c == '\0')
366 for (n = 0x100; *c != '\0'; n += 0x100) {
368 r = (int)((v >> 2) ^ v) & 0x0f;
369 ret = (ret << r) | (ret >> (32 - r));
374 return (ret >> 16) ^ ret;
377 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
379 return lh ? lh->num_items : 0;
382 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
384 return lh->down_load;
387 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
389 lh->down_load = down_load;
392 int OPENSSL_LH_error(OPENSSL_LHASH *lh)