2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 <openssl/err.h>
16 #include "internal/ctype.h"
17 #include "internal/lhash.h"
18 #include "lhash_lcl.h"
21 * A hashing implementation that appears to be based on the linear hashing
23 * https://en.wikipedia.org/wiki/Linear_hashing
25 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
26 * addressing", Proc. 6th Conference on Very Large Databases: 212-223
27 * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
29 * From the wikipedia article "Linear hashing is used in the BDB Berkeley
30 * database system, which in turn is used by many software systems such as
31 * OpenLDAP, using a C implementation derived from the CACM article and first
32 * published on the Usenet in 1988 by Esmond Pitt."
34 * The CACM paper is available here:
35 * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
40 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
43 static int expand(OPENSSL_LHASH *lh);
44 static void contract(OPENSSL_LHASH *lh);
45 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
47 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
51 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
53 * Do not set the error code, because the ERR code uses LHASH
54 * and we want to avoid possible endless error loop.
55 * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE);
59 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
61 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63 ret->num_nodes = MIN_NODES / 2;
64 ret->num_alloc_nodes = MIN_NODES;
65 ret->pmax = MIN_NODES / 2;
66 ret->up_load = UP_LOAD;
67 ret->down_load = DOWN_LOAD;
76 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
86 void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
89 OPENSSL_LH_NODE *n, *nn;
94 for (i = 0; i < lh->num_nodes; i++) {
104 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
107 OPENSSL_LH_NODE *nn, **rn;
111 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
112 return NULL; /* 'lh->error++' already done in 'expand' */
114 rn = getrn(lh, data, &hash);
117 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
128 } else { /* replace same key */
136 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
139 OPENSSL_LH_NODE *nn, **rn;
143 rn = getrn(lh, data, &hash);
157 if ((lh->num_nodes > MIN_NODES) &&
158 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
164 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
167 OPENSSL_LH_NODE **rn;
170 tsan_store((TSAN_QUALIFIER int *)&lh->error, 0);
172 rn = getrn(lh, data, &hash);
175 tsan_counter(&lh->num_retrieve_miss);
179 tsan_counter(&lh->num_retrieve);
185 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
186 OPENSSL_LH_DOALL_FUNC func,
187 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
190 OPENSSL_LH_NODE *a, *n;
196 * reverse the order so we search from 'top to bottom' We were having
197 * memory leaks otherwise
199 for (i = lh->num_nodes - 1; i >= 0; i--) {
204 func_arg(a->data, arg);
212 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
214 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
217 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
219 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
222 static int expand(OPENSSL_LHASH *lh)
224 OPENSSL_LH_NODE **n, **n1, **n2, *np;
225 unsigned int p, pmax, nni, j;
228 nni = lh->num_alloc_nodes;
233 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
239 memset(n + nni, 0, sizeof(*n) * (j - nni));
241 lh->num_alloc_nodes = j;
242 lh->num_expand_reallocs++;
251 n2 = &(lh->b[p + pmax]);
254 for (np = *n1; np != NULL;) {
256 if ((hash % nni) != p) { /* move it */
268 static void contract(OPENSSL_LHASH *lh)
270 OPENSSL_LH_NODE **n, *n1, *np;
272 np = lh->b[lh->p + lh->pmax - 1];
273 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
275 n = OPENSSL_realloc(lh->b,
276 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
278 /* fputs("realloc error in lhash",stderr); */
282 lh->num_contract_reallocs++;
283 lh->num_alloc_nodes /= 2;
285 lh->p = lh->pmax - 1;
293 n1 = lh->b[(int)lh->p];
295 lh->b[(int)lh->p] = np;
297 while (n1->next != NULL)
303 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
304 const void *data, unsigned long *rhash)
306 OPENSSL_LH_NODE **ret, *n1;
307 unsigned long hash, nn;
308 OPENSSL_LH_COMPFUNC cf;
310 hash = (*(lh->hash)) (data);
311 tsan_counter(&lh->num_hash_calls);
314 nn = hash % lh->pmax;
316 nn = hash % lh->num_alloc_nodes;
319 ret = &(lh->b[(int)nn]);
320 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
321 tsan_counter(&lh->num_hash_comps);
322 if (n1->hash != hash) {
326 tsan_counter(&lh->num_comp_calls);
327 if (cf(n1->data, data) == 0)
335 * The following hash seems to work very well on normal text strings no
336 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
337 * as good as MD5, but still good.
339 unsigned long OPENSSL_LH_strhash(const char *c)
341 unsigned long ret = 0;
346 if ((c == NULL) || (*c == '\0'))
353 r = (int)((v >> 2) ^ v) & 0x0f;
354 ret = (ret << r) | (ret >> (32 - r));
359 return (ret >> 16) ^ ret;
362 unsigned long openssl_lh_strcasehash(const char *c)
364 unsigned long ret = 0;
369 if (c == NULL || *c == '\0')
372 for (n = 0x100; *c != '\0'; n += 0x100) {
373 v = n | ossl_tolower(*c);
374 r = (int)((v >> 2) ^ v) & 0x0f;
375 ret = (ret << r) | (ret >> (32 - r));
380 return (ret >> 16) ^ ret;
383 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
385 return lh ? lh->num_items : 0;
388 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
390 return lh->down_load;
393 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
395 lh->down_load = down_load;
398 int OPENSSL_LH_error(OPENSSL_LHASH *lh)