2 * Copyright 1995-2022 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 "crypto/ctype.h"
17 #include "crypto/lhash.h"
18 #include "lhash_local.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 * https://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 * ERR_raise(ERR_LIB_CRYPTO, 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++) {
107 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
110 OPENSSL_LH_NODE *nn, **rn;
114 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
115 return NULL; /* 'lh->error++' already done in 'expand' */
117 rn = getrn(lh, data, &hash);
120 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
130 } else { /* replace same key */
137 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
140 OPENSSL_LH_NODE *nn, **rn;
144 rn = getrn(lh, data, &hash);
156 if ((lh->num_nodes > MIN_NODES) &&
157 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
163 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
166 OPENSSL_LH_NODE **rn;
171 rn = getrn(lh, data, &hash);
173 return *rn == NULL ? NULL : (*rn)->data;
176 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
177 OPENSSL_LH_DOALL_FUNC func,
178 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
181 OPENSSL_LH_NODE *a, *n;
187 * reverse the order so we search from 'top to bottom' We were having
188 * memory leaks otherwise
190 for (i = lh->num_nodes - 1; i >= 0; i--) {
195 func_arg(a->data, arg);
203 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
205 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
208 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
210 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
213 static int expand(OPENSSL_LHASH *lh)
215 OPENSSL_LH_NODE **n, **n1, **n2, *np;
216 unsigned int p, pmax, nni, j;
219 nni = lh->num_alloc_nodes;
224 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
230 memset(n + nni, 0, sizeof(*n) * (j - nni));
232 lh->num_alloc_nodes = j;
240 n2 = &(lh->b[p + pmax]);
243 for (np = *n1; np != NULL;) {
245 if ((hash % nni) != p) { /* move it */
257 static void contract(OPENSSL_LHASH *lh)
259 OPENSSL_LH_NODE **n, *n1, *np;
261 np = lh->b[lh->p + lh->pmax - 1];
262 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
264 n = OPENSSL_realloc(lh->b,
265 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
267 /* fputs("realloc error in lhash", stderr); */
271 lh->num_alloc_nodes /= 2;
273 lh->p = lh->pmax - 1;
280 n1 = lh->b[(int)lh->p];
282 lh->b[(int)lh->p] = np;
284 while (n1->next != NULL)
290 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
291 const void *data, unsigned long *rhash)
293 OPENSSL_LH_NODE **ret, *n1;
294 unsigned long hash, nn;
295 OPENSSL_LH_COMPFUNC cf;
297 hash = (*(lh->hash)) (data);
300 nn = hash % lh->pmax;
302 nn = hash % lh->num_alloc_nodes;
305 ret = &(lh->b[(int)nn]);
306 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
307 if (n1->hash != hash) {
311 if (cf(n1->data, data) == 0)
319 * The following hash seems to work very well on normal text strings no
320 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
321 * as good as MD5, but still good.
323 unsigned long OPENSSL_LH_strhash(const char *c)
325 unsigned long ret = 0;
330 if ((c == NULL) || (*c == '\0'))
337 r = (int)((v >> 2) ^ v) & 0x0f;
338 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
339 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
344 return (ret >> 16) ^ ret;
347 unsigned long ossl_lh_strcasehash(const char *c)
349 unsigned long ret = 0;
354 if (c == NULL || *c == '\0')
357 for (n = 0x100; *c != '\0'; n += 0x100) {
358 v = n | ossl_tolower(*c);
359 r = (int)((v >> 2) ^ v) & 0x0f;
360 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
361 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
366 return (ret >> 16) ^ ret;
369 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
371 return lh ? lh->num_items : 0;
374 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
376 return lh->down_load;
379 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
381 lh->down_load = down_load;
384 int OPENSSL_LH_error(OPENSSL_LHASH *lh)