2 * Copyright 1995-2017 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>
15 #include "lhash_lcl.h"
18 * A hashing implementation that appears to be based on the linear hashing
20 * https://en.wikipedia.org/wiki/Linear_hashing
22 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
23 * addressing", Proc. 6th Conference on Very Large Databases: 212–223
24 * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
26 * From the wikipedia article "Linear hashing is used in the BDB Berkeley
27 * database system, which in turn is used by many software systems such as
28 * OpenLDAP, using a C implementation derived from the CACM article and first
29 * published on the Usenet in 1988 by Esmond Pitt."
31 * The CACM paper is available here:
32 * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
37 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
38 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
40 static int expand(OPENSSL_LHASH *lh);
41 static void contract(OPENSSL_LHASH *lh);
42 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
44 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
48 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
50 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
52 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
53 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
54 ret->num_nodes = MIN_NODES / 2;
55 ret->num_alloc_nodes = MIN_NODES;
56 ret->pmax = MIN_NODES / 2;
57 ret->up_load = UP_LOAD;
58 ret->down_load = DOWN_LOAD;
67 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
70 OPENSSL_LH_NODE *n, *nn;
75 for (i = 0; i < lh->num_nodes; i++) {
87 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
90 OPENSSL_LH_NODE *nn, **rn;
94 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
95 return NULL; /* 'lh->error++' already done in 'expand' */
97 rn = getrn(lh, data, &hash);
100 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
111 } else { /* replace same key */
119 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
122 OPENSSL_LH_NODE *nn, **rn;
126 rn = getrn(lh, data, &hash);
140 if ((lh->num_nodes > MIN_NODES) &&
141 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
147 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
150 OPENSSL_LH_NODE **rn;
154 rn = getrn(lh, data, &hash);
157 lh->num_retrieve_miss++;
166 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
167 OPENSSL_LH_DOALL_FUNC func,
168 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
171 OPENSSL_LH_NODE *a, *n;
177 * reverse the order so we search from 'top to bottom' We were having
178 * memory leaks otherwise
180 for (i = lh->num_nodes - 1; i >= 0; i--) {
185 func_arg(a->data, arg);
193 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
195 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
198 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
200 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
203 static int expand(OPENSSL_LHASH *lh)
205 OPENSSL_LH_NODE **n, **n1, **n2, *np;
206 unsigned int p, pmax, nni, j;
209 nni = lh->num_alloc_nodes;
214 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
220 memset(n + nni, 0, sizeof(*n) * (j - nni));
222 lh->num_alloc_nodes = j;
223 lh->num_expand_reallocs++;
232 n2 = &(lh->b[p + pmax]);
235 for (np = *n1; np != NULL;) {
237 if ((hash % nni) != p) { /* move it */
249 static void contract(OPENSSL_LHASH *lh)
251 OPENSSL_LH_NODE **n, *n1, *np;
253 np = lh->b[lh->p + lh->pmax - 1];
254 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
256 n = OPENSSL_realloc(lh->b,
257 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
259 /* fputs("realloc error in lhash",stderr); */
263 lh->num_contract_reallocs++;
264 lh->num_alloc_nodes /= 2;
266 lh->p = lh->pmax - 1;
274 n1 = lh->b[(int)lh->p];
276 lh->b[(int)lh->p] = np;
278 while (n1->next != NULL)
284 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285 const void *data, unsigned long *rhash)
287 OPENSSL_LH_NODE **ret, *n1;
288 unsigned long hash, nn;
289 OPENSSL_LH_COMPFUNC cf;
291 hash = (*(lh->hash)) (data);
292 lh->num_hash_calls++;
295 nn = hash % lh->pmax;
297 nn = hash % lh->num_alloc_nodes;
300 ret = &(lh->b[(int)nn]);
301 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
302 lh->num_hash_comps++;
303 if (n1->hash != hash) {
307 lh->num_comp_calls++;
308 if (cf(n1->data, data) == 0)
316 * The following hash seems to work very well on normal text strings no
317 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
318 * as good as MD5, but still good.
320 unsigned long OPENSSL_LH_strhash(const char *c)
322 unsigned long ret = 0;
327 if ((c == NULL) || (*c == '\0'))
334 r = (int)((v >> 2) ^ v) & 0x0f;
335 ret = (ret << r) | (ret >> (32 - r));
340 return (ret >> 16) ^ ret;
343 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
345 return lh ? lh->num_items : 0;
348 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
350 return lh->down_load;
353 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
355 lh->down_load = down_load;
358 int OPENSSL_LH_error(OPENSSL_LHASH *lh)