]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
2e8b5d75 | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
d02b48c6 RE |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include <stdlib.h> | |
ec577822 BM |
13 | #include <openssl/crypto.h> |
14 | #include <openssl/lhash.h> | |
fe1128dc | 15 | #include <openssl/err.h> |
739a1eb1 RS |
16 | #include "lhash_lcl.h" |
17 | ||
4ce8bebc MC |
18 | /* |
19 | * A hashing implementation that appears to be based on the linear hashing | |
20 | * alogrithm: | |
21 | * https://en.wikipedia.org/wiki/Linear_hashing | |
22 | * | |
23 | * Litwin, Witold (1980), "Linear hashing: A new tool for file and table | |
b4d0fa49 | 24 | * addressing", Proc. 6th Conference on Very Large Databases: 212-223 |
4ce8bebc MC |
25 | * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf |
26 | * | |
27 | * From the wikipedia article "Linear hashing is used in the BDB Berkeley | |
28 | * database system, which in turn is used by many software systems such as | |
29 | * OpenLDAP, using a C implementation derived from the CACM article and first | |
30 | * published on the Usenet in 1988 by Esmond Pitt." | |
31 | * | |
32 | * The CACM paper is available here: | |
33 | * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf | |
34 | */ | |
d02b48c6 | 35 | |
0f113f3e MC |
36 | #undef MIN_NODES |
37 | #define MIN_NODES 16 | |
38 | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ | |
39 | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ | |
d02b48c6 | 40 | |
0a1d3a81 | 41 | static int expand(OPENSSL_LHASH *lh); |
739a1eb1 RS |
42 | static void contract(OPENSSL_LHASH *lh); |
43 | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash); | |
d02b48c6 | 44 | |
739a1eb1 | 45 | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
0f113f3e | 46 | { |
739a1eb1 | 47 | OPENSSL_LHASH *ret; |
0f113f3e | 48 | |
fe1128dc RS |
49 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
50 | /* | |
51 | * Do not set the error code, because the ERR code uses LHASH | |
52 | * and we want to avoid possible endless error loop. | |
53 | * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE); | |
54 | */ | |
be606c01 | 55 | return NULL; |
fe1128dc | 56 | } |
64b25758 | 57 | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
be606c01 | 58 | goto err; |
739a1eb1 RS |
59 | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
60 | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); | |
0f113f3e MC |
61 | ret->num_nodes = MIN_NODES / 2; |
62 | ret->num_alloc_nodes = MIN_NODES; | |
0f113f3e MC |
63 | ret->pmax = MIN_NODES / 2; |
64 | ret->up_load = UP_LOAD; | |
65 | ret->down_load = DOWN_LOAD; | |
2e8b5d75 | 66 | return ret; |
64b25758 | 67 | |
be606c01 RS |
68 | err: |
69 | OPENSSL_free(ret->b); | |
0f113f3e | 70 | OPENSSL_free(ret); |
be606c01 | 71 | return NULL; |
0f113f3e | 72 | } |
d02b48c6 | 73 | |
739a1eb1 | 74 | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
0f113f3e MC |
75 | { |
76 | unsigned int i; | |
739a1eb1 | 77 | OPENSSL_LH_NODE *n, *nn; |
0f113f3e MC |
78 | |
79 | if (lh == NULL) | |
80 | return; | |
81 | ||
82 | for (i = 0; i < lh->num_nodes; i++) { | |
83 | n = lh->b[i]; | |
84 | while (n != NULL) { | |
85 | nn = n->next; | |
86 | OPENSSL_free(n); | |
87 | n = nn; | |
88 | } | |
89 | } | |
90 | OPENSSL_free(lh->b); | |
91 | OPENSSL_free(lh); | |
92 | } | |
d02b48c6 | 93 | |
739a1eb1 | 94 | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
0f113f3e MC |
95 | { |
96 | unsigned long hash; | |
739a1eb1 | 97 | OPENSSL_LH_NODE *nn, **rn; |
0f113f3e | 98 | void *ret; |
152d2646 | 99 | |
0f113f3e | 100 | lh->error = 0; |
152d2646 | 101 | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
102 | return NULL; /* 'lh->error++' already done in 'expand' */ | |
103 | ||
0f113f3e MC |
104 | rn = getrn(lh, data, &hash); |
105 | ||
106 | if (*rn == NULL) { | |
b4faea50 | 107 | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
0f113f3e | 108 | lh->error++; |
2e8b5d75 | 109 | return NULL; |
0f113f3e MC |
110 | } |
111 | nn->data = data; | |
112 | nn->next = NULL; | |
0f113f3e | 113 | nn->hash = hash; |
0f113f3e MC |
114 | *rn = nn; |
115 | ret = NULL; | |
116 | lh->num_insert++; | |
117 | lh->num_items++; | |
118 | } else { /* replace same key */ | |
0f113f3e MC |
119 | ret = (*rn)->data; |
120 | (*rn)->data = data; | |
121 | lh->num_replace++; | |
122 | } | |
2e8b5d75 | 123 | return ret; |
0f113f3e | 124 | } |
d02b48c6 | 125 | |
739a1eb1 | 126 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
0f113f3e MC |
127 | { |
128 | unsigned long hash; | |
739a1eb1 | 129 | OPENSSL_LH_NODE *nn, **rn; |
0f113f3e MC |
130 | void *ret; |
131 | ||
132 | lh->error = 0; | |
133 | rn = getrn(lh, data, &hash); | |
134 | ||
135 | if (*rn == NULL) { | |
136 | lh->num_no_delete++; | |
2e8b5d75 | 137 | return NULL; |
0f113f3e MC |
138 | } else { |
139 | nn = *rn; | |
140 | *rn = nn->next; | |
141 | ret = nn->data; | |
142 | OPENSSL_free(nn); | |
143 | lh->num_delete++; | |
144 | } | |
145 | ||
146 | lh->num_items--; | |
147 | if ((lh->num_nodes > MIN_NODES) && | |
148 | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) | |
149 | contract(lh); | |
150 | ||
2e8b5d75 | 151 | return ret; |
0f113f3e | 152 | } |
d02b48c6 | 153 | |
739a1eb1 | 154 | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
0f113f3e MC |
155 | { |
156 | unsigned long hash; | |
739a1eb1 | 157 | OPENSSL_LH_NODE **rn; |
0f113f3e MC |
158 | void *ret; |
159 | ||
160 | lh->error = 0; | |
161 | rn = getrn(lh, data, &hash); | |
162 | ||
163 | if (*rn == NULL) { | |
2e8b5d75 | 164 | lh->num_retrieve_miss++; |
be606c01 | 165 | return NULL; |
0f113f3e MC |
166 | } else { |
167 | ret = (*rn)->data; | |
2e8b5d75 | 168 | lh->num_retrieve++; |
0f113f3e | 169 | } |
be606c01 | 170 | return ret; |
0f113f3e | 171 | } |
d02b48c6 | 172 | |
739a1eb1 RS |
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) | |
0f113f3e MC |
176 | { |
177 | int i; | |
739a1eb1 | 178 | OPENSSL_LH_NODE *a, *n; |
0f113f3e MC |
179 | |
180 | if (lh == NULL) | |
181 | return; | |
182 | ||
183 | /* | |
184 | * reverse the order so we search from 'top to bottom' We were having | |
185 | * memory leaks otherwise | |
186 | */ | |
187 | for (i = lh->num_nodes - 1; i >= 0; i--) { | |
188 | a = lh->b[i]; | |
189 | while (a != NULL) { | |
0f113f3e MC |
190 | n = a->next; |
191 | if (use_arg) | |
192 | func_arg(a->data, arg); | |
193 | else | |
194 | func(a->data); | |
195 | a = n; | |
196 | } | |
197 | } | |
198 | } | |
d02b48c6 | 199 | |
739a1eb1 | 200 | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
0f113f3e | 201 | { |
739a1eb1 | 202 | doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); |
0f113f3e | 203 | } |
18602745 | 204 | |
739a1eb1 | 205 | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
0f113f3e | 206 | { |
739a1eb1 | 207 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); |
0f113f3e | 208 | } |
18602745 | 209 | |
0a1d3a81 | 210 | static int expand(OPENSSL_LHASH *lh) |
0f113f3e | 211 | { |
739a1eb1 | 212 | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
4ce8bebc MC |
213 | unsigned int p, pmax, nni, j; |
214 | unsigned long hash; | |
215 | ||
216 | nni = lh->num_alloc_nodes; | |
217 | p = lh->p; | |
218 | pmax = lh->pmax; | |
219 | if (p + 1 >= pmax) { | |
220 | j = nni * 2; | |
221 | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); | |
222 | if (n == NULL) { | |
223 | lh->error++; | |
224 | return 0; | |
225 | } | |
226 | lh->b = n; | |
227 | memset(n + nni, 0, sizeof(*n) * (j - nni)); | |
228 | lh->pmax = nni; | |
229 | lh->num_alloc_nodes = j; | |
230 | lh->num_expand_reallocs++; | |
231 | lh->p = 0; | |
232 | } else { | |
233 | lh->p++; | |
234 | } | |
0f113f3e MC |
235 | |
236 | lh->num_nodes++; | |
237 | lh->num_expands++; | |
0f113f3e | 238 | n1 = &(lh->b[p]); |
4ce8bebc | 239 | n2 = &(lh->b[p + pmax]); |
739a1eb1 | 240 | *n2 = NULL; |
0f113f3e MC |
241 | |
242 | for (np = *n1; np != NULL;) { | |
0f113f3e | 243 | hash = np->hash; |
0f113f3e MC |
244 | if ((hash % nni) != p) { /* move it */ |
245 | *n1 = (*n1)->next; | |
246 | np->next = *n2; | |
247 | *n2 = np; | |
248 | } else | |
249 | n1 = &((*n1)->next); | |
250 | np = *n1; | |
251 | } | |
252 | ||
152d2646 | 253 | return 1; |
0f113f3e | 254 | } |
d02b48c6 | 255 | |
739a1eb1 | 256 | static void contract(OPENSSL_LHASH *lh) |
0f113f3e | 257 | { |
739a1eb1 | 258 | OPENSSL_LH_NODE **n, *n1, *np; |
0f113f3e MC |
259 | |
260 | np = lh->b[lh->p + lh->pmax - 1]; | |
261 | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ | |
262 | if (lh->p == 0) { | |
b196e7d9 | 263 | n = OPENSSL_realloc(lh->b, |
739a1eb1 | 264 | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
0f113f3e | 265 | if (n == NULL) { |
b196e7d9 | 266 | /* fputs("realloc error in lhash",stderr); */ |
0f113f3e MC |
267 | lh->error++; |
268 | return; | |
269 | } | |
270 | lh->num_contract_reallocs++; | |
271 | lh->num_alloc_nodes /= 2; | |
272 | lh->pmax /= 2; | |
273 | lh->p = lh->pmax - 1; | |
274 | lh->b = n; | |
275 | } else | |
276 | lh->p--; | |
277 | ||
278 | lh->num_nodes--; | |
279 | lh->num_contracts++; | |
280 | ||
281 | n1 = lh->b[(int)lh->p]; | |
282 | if (n1 == NULL) | |
283 | lh->b[(int)lh->p] = np; | |
284 | else { | |
285 | while (n1->next != NULL) | |
286 | n1 = n1->next; | |
287 | n1->next = np; | |
288 | } | |
289 | } | |
d02b48c6 | 290 | |
739a1eb1 RS |
291 | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
292 | const void *data, unsigned long *rhash) | |
0f113f3e | 293 | { |
739a1eb1 | 294 | OPENSSL_LH_NODE **ret, *n1; |
0f113f3e | 295 | unsigned long hash, nn; |
739a1eb1 | 296 | OPENSSL_LH_COMPFUNC cf; |
0f113f3e MC |
297 | |
298 | hash = (*(lh->hash)) (data); | |
2e8b5d75 | 299 | lh->num_hash_calls++; |
0f113f3e MC |
300 | *rhash = hash; |
301 | ||
302 | nn = hash % lh->pmax; | |
303 | if (nn < lh->p) | |
304 | nn = hash % lh->num_alloc_nodes; | |
305 | ||
306 | cf = lh->comp; | |
307 | ret = &(lh->b[(int)nn]); | |
308 | for (n1 = *ret; n1 != NULL; n1 = n1->next) { | |
2e8b5d75 | 309 | lh->num_hash_comps++; |
0f113f3e MC |
310 | if (n1->hash != hash) { |
311 | ret = &(n1->next); | |
312 | continue; | |
313 | } | |
2e8b5d75 | 314 | lh->num_comp_calls++; |
0f113f3e MC |
315 | if (cf(n1->data, data) == 0) |
316 | break; | |
317 | ret = &(n1->next); | |
318 | } | |
2e8b5d75 | 319 | return ret; |
0f113f3e MC |
320 | } |
321 | ||
322 | /* | |
323 | * The following hash seems to work very well on normal text strings no | |
324 | * collisions on /usr/dict/words and it distributes on %2^n quite well, not | |
325 | * as good as MD5, but still good. | |
d02b48c6 | 326 | */ |
739a1eb1 | 327 | unsigned long OPENSSL_LH_strhash(const char *c) |
0f113f3e MC |
328 | { |
329 | unsigned long ret = 0; | |
330 | long n; | |
331 | unsigned long v; | |
332 | int r; | |
333 | ||
334 | if ((c == NULL) || (*c == '\0')) | |
2e8b5d75 | 335 | return ret; |
d02b48c6 | 336 | |
0f113f3e MC |
337 | n = 0x100; |
338 | while (*c) { | |
339 | v = n | (*c); | |
340 | n += 0x100; | |
341 | r = (int)((v >> 2) ^ v) & 0x0f; | |
342 | ret = (ret << r) | (ret >> (32 - r)); | |
343 | ret &= 0xFFFFFFFFL; | |
344 | ret ^= v * v; | |
345 | c++; | |
346 | } | |
2e8b5d75 | 347 | return (ret >> 16) ^ ret; |
0f113f3e | 348 | } |
d02b48c6 | 349 | |
739a1eb1 | 350 | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
0f113f3e MC |
351 | { |
352 | return lh ? lh->num_items : 0; | |
353 | } | |
e6b5c341 | 354 | |
739a1eb1 | 355 | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
e6b5c341 DSH |
356 | { |
357 | return lh->down_load; | |
358 | } | |
359 | ||
739a1eb1 | 360 | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
e6b5c341 DSH |
361 | { |
362 | lh->down_load = down_load; | |
363 | } | |
364 | ||
739a1eb1 | 365 | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
e6b5c341 DSH |
366 | { |
367 | return lh->error; | |
368 | } |