2 * Copyright 1995-2024 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_set_thunks(OPENSSL_LHASH *lh,
48 OPENSSL_LH_HASHFUNCTHUNK hw,
49 OPENSSL_LH_COMPFUNCTHUNK cw,
50 OPENSSL_LH_DOALL_FUNC_THUNK daw,
51 OPENSSL_LH_DOALL_FUNCARG_THUNK daaw)
63 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
67 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
69 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
71 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
72 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
73 ret->num_nodes = MIN_NODES / 2;
74 ret->num_alloc_nodes = MIN_NODES;
75 ret->pmax = MIN_NODES / 2;
76 ret->up_load = UP_LOAD;
77 ret->down_load = DOWN_LOAD;
86 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
96 void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
99 OPENSSL_LH_NODE *n, *nn;
104 for (i = 0; i < lh->num_nodes; i++) {
117 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
120 OPENSSL_LH_NODE *nn, **rn;
124 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
125 return NULL; /* 'lh->error++' already done in 'expand' */
127 rn = getrn(lh, data, &hash);
130 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
140 } else { /* replace same key */
147 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
150 OPENSSL_LH_NODE *nn, **rn;
154 rn = getrn(lh, data, &hash);
166 if ((lh->num_nodes > MIN_NODES) &&
167 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
173 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
176 OPENSSL_LH_NODE **rn;
181 rn = getrn(lh, data, &hash);
183 return *rn == NULL ? NULL : (*rn)->data;
186 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
187 OPENSSL_LH_DOALL_FUNC_THUNK wfunc,
188 OPENSSL_LH_DOALL_FUNC func,
189 OPENSSL_LH_DOALL_FUNCARG func_arg,
190 OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg,
194 OPENSSL_LH_NODE *a, *n;
200 * reverse the order so we search from 'top to bottom' We were having
201 * memory leaks otherwise
203 for (i = lh->num_nodes - 1; i >= 0; i--) {
208 wfunc_arg(a->data, arg, func_arg);
210 wfunc(a->data, func);
216 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
221 doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL,
222 (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL);
225 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh,
226 OPENSSL_LH_DOALL_FUNCARG func, void *arg)
231 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
232 (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg);
235 void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh,
236 OPENSSL_LH_DOALL_FUNCARG_THUNK daaw,
237 OPENSSL_LH_DOALL_FUNCARG fn, void *arg)
239 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
240 (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg);
243 static int expand(OPENSSL_LHASH *lh)
245 OPENSSL_LH_NODE **n, **n1, **n2, *np;
246 unsigned int p, pmax, nni, j;
249 nni = lh->num_alloc_nodes;
254 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
260 memset(n + nni, 0, sizeof(*n) * (j - nni));
262 lh->num_alloc_nodes = j;
270 n2 = &(lh->b[p + pmax]);
273 for (np = *n1; np != NULL;) {
275 if ((hash % nni) != p) { /* move it */
287 static void contract(OPENSSL_LHASH *lh)
289 OPENSSL_LH_NODE **n, *n1, *np;
291 np = lh->b[lh->p + lh->pmax - 1];
292 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
294 n = OPENSSL_realloc(lh->b,
295 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
297 /* fputs("realloc error in lhash", stderr); */
302 lh->num_alloc_nodes /= 2;
304 lh->p = lh->pmax - 1;
310 n1 = lh->b[(int)lh->p];
312 lh->b[(int)lh->p] = np;
314 while (n1->next != NULL)
320 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
321 const void *data, unsigned long *rhash)
323 OPENSSL_LH_NODE **ret, *n1;
324 unsigned long hash, nn;
326 if (lh->hashw != NULL)
327 hash = lh->hashw(data, lh->hash);
329 hash = lh->hash(data);
333 nn = hash % lh->pmax;
335 nn = hash % lh->num_alloc_nodes;
337 ret = &(lh->b[(int)nn]);
338 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
339 if (n1->hash != hash) {
344 if (lh->compw != NULL) {
345 if (lh->compw(n1->data, data, lh->comp) == 0)
348 if (lh->comp(n1->data, data) == 0)
357 * The following hash seems to work very well on normal text strings no
358 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
359 * as good as MD5, but still good.
361 unsigned long OPENSSL_LH_strhash(const char *c)
363 unsigned long ret = 0;
368 if ((c == NULL) || (*c == '\0'))
375 r = (int)((v >> 2) ^ v) & 0x0f;
376 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
377 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
382 return (ret >> 16) ^ ret;
386 * Case insensitive string hashing.
388 * The lower/upper case bit is masked out (forcing all letters to be capitals).
389 * The major side effect on non-alpha characters is mapping the symbols and
390 * digits into the control character range (which should be harmless).
391 * The duplication (with respect to the hash value) of printable characters
392 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
393 * respectively (which seems tolerable).
395 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
396 * characters. The only duplication is '0' mapping to '^', which is better
399 unsigned long ossl_lh_strcasehash(const char *c)
401 unsigned long ret = 0;
405 #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
406 const long int case_adjust = ~0x40;
408 const long int case_adjust = ~0x20;
411 if (c == NULL || *c == '\0')
414 for (n = 0x100; *c != '\0'; n += 0x100) {
415 v = n | (case_adjust & *c);
416 r = (int)((v >> 2) ^ v) & 0x0f;
417 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
418 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
423 return (ret >> 16) ^ ret;
426 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
428 return lh ? lh->num_items : 0;
431 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
433 return lh->down_load;
436 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
438 lh->down_load = down_load;
441 int OPENSSL_LH_error(OPENSSL_LHASH *lh)