/src/openssl/crypto/lhash/lhash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <string.h> |
12 | | #include <stdlib.h> |
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" |
19 | | |
20 | | /* |
21 | | * A hashing implementation that appears to be based on the linear hashing |
22 | | * algorithm: |
23 | | * https://en.wikipedia.org/wiki/Linear_hashing |
24 | | * |
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 |
28 | | * |
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." |
33 | | * |
34 | | * The CACM paper is available here: |
35 | | * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf |
36 | | */ |
37 | | |
38 | | #undef MIN_NODES |
39 | 8.65k | #define MIN_NODES 16 |
40 | 2.88k | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ |
41 | 2.88k | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ |
42 | | |
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); |
46 | | |
47 | | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
48 | 2.88k | { |
49 | 2.88k | OPENSSL_LHASH *ret; |
50 | | |
51 | 2.88k | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
52 | | /* |
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); |
56 | | */ |
57 | 0 | return NULL; |
58 | 0 | } |
59 | 2.88k | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
60 | 0 | goto err; |
61 | 2.88k | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
62 | 2.88k | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); |
63 | 2.88k | ret->num_nodes = MIN_NODES / 2; |
64 | 2.88k | ret->num_alloc_nodes = MIN_NODES; |
65 | 2.88k | ret->pmax = MIN_NODES / 2; |
66 | 2.88k | ret->up_load = UP_LOAD; |
67 | 2.88k | ret->down_load = DOWN_LOAD; |
68 | 2.88k | return ret; |
69 | | |
70 | 0 | err: |
71 | 0 | OPENSSL_free(ret->b); |
72 | 0 | OPENSSL_free(ret); |
73 | 0 | return NULL; |
74 | 2.88k | } |
75 | | |
76 | | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
77 | 1.92k | { |
78 | 1.92k | if (lh == NULL) |
79 | 482 | return; |
80 | | |
81 | 1.44k | OPENSSL_LH_flush(lh); |
82 | 1.44k | OPENSSL_free(lh->b); |
83 | 1.44k | OPENSSL_free(lh); |
84 | 1.44k | } |
85 | | |
86 | | void OPENSSL_LH_flush(OPENSSL_LHASH *lh) |
87 | 1.44k | { |
88 | 1.44k | unsigned int i; |
89 | 1.44k | OPENSSL_LH_NODE *n, *nn; |
90 | | |
91 | 1.44k | if (lh == NULL) |
92 | 0 | return; |
93 | | |
94 | 323k | for (i = 0; i < lh->num_nodes; i++) { |
95 | 322k | n = lh->b[i]; |
96 | 956k | while (n != NULL) { |
97 | 634k | nn = n->next; |
98 | 634k | OPENSSL_free(n); |
99 | 634k | n = nn; |
100 | 634k | } |
101 | 322k | lh->b[i] = NULL; |
102 | 322k | } |
103 | 1.44k | } |
104 | | |
105 | | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
106 | 3.55M | { |
107 | 3.55M | unsigned long hash; |
108 | 3.55M | OPENSSL_LH_NODE *nn, **rn; |
109 | 3.55M | void *ret; |
110 | | |
111 | 3.55M | lh->error = 0; |
112 | 3.55M | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
113 | 0 | return NULL; /* 'lh->error++' already done in 'expand' */ |
114 | | |
115 | 3.55M | rn = getrn(lh, data, &hash); |
116 | | |
117 | 3.55M | if (*rn == NULL) { |
118 | 1.26M | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
119 | 0 | lh->error++; |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 1.26M | nn->data = data; |
123 | 1.26M | nn->next = NULL; |
124 | 1.26M | nn->hash = hash; |
125 | 1.26M | *rn = nn; |
126 | 1.26M | ret = NULL; |
127 | 1.26M | lh->num_insert++; |
128 | 1.26M | lh->num_items++; |
129 | 2.28M | } else { /* replace same key */ |
130 | 2.28M | ret = (*rn)->data; |
131 | 2.28M | (*rn)->data = data; |
132 | 2.28M | lh->num_replace++; |
133 | 2.28M | } |
134 | 3.55M | return ret; |
135 | 3.55M | } |
136 | | |
137 | | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
138 | 0 | { |
139 | 0 | unsigned long hash; |
140 | 0 | OPENSSL_LH_NODE *nn, **rn; |
141 | 0 | void *ret; |
142 | |
|
143 | 0 | lh->error = 0; |
144 | 0 | rn = getrn(lh, data, &hash); |
145 | |
|
146 | 0 | if (*rn == NULL) { |
147 | 0 | lh->num_no_delete++; |
148 | 0 | return NULL; |
149 | 0 | } else { |
150 | 0 | nn = *rn; |
151 | 0 | *rn = nn->next; |
152 | 0 | ret = nn->data; |
153 | 0 | OPENSSL_free(nn); |
154 | 0 | lh->num_delete++; |
155 | 0 | } |
156 | | |
157 | 0 | lh->num_items--; |
158 | 0 | if ((lh->num_nodes > MIN_NODES) && |
159 | 0 | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
160 | 0 | contract(lh); |
161 | |
|
162 | 0 | return ret; |
163 | 0 | } |
164 | | |
165 | | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
166 | 323k | { |
167 | 323k | unsigned long hash; |
168 | 323k | OPENSSL_LH_NODE **rn; |
169 | 323k | void *ret; |
170 | | |
171 | 323k | tsan_store((TSAN_QUALIFIER int *)&lh->error, 0); |
172 | | |
173 | 323k | rn = getrn(lh, data, &hash); |
174 | | |
175 | 323k | if (*rn == NULL) { |
176 | 102k | tsan_counter(&lh->num_retrieve_miss); |
177 | 102k | return NULL; |
178 | 221k | } else { |
179 | 221k | ret = (*rn)->data; |
180 | 221k | tsan_counter(&lh->num_retrieve); |
181 | 221k | } |
182 | | |
183 | 221k | return ret; |
184 | 323k | } |
185 | | |
186 | | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
187 | | OPENSSL_LH_DOALL_FUNC func, |
188 | | OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) |
189 | 964 | { |
190 | 964 | int i; |
191 | 964 | OPENSSL_LH_NODE *a, *n; |
192 | | |
193 | 964 | if (lh == NULL) |
194 | 0 | return; |
195 | | |
196 | | /* |
197 | | * reverse the order so we search from 'top to bottom' We were having |
198 | | * memory leaks otherwise |
199 | | */ |
200 | 8.67k | for (i = lh->num_nodes - 1; i >= 0; i--) { |
201 | 7.71k | a = lh->b[i]; |
202 | 12.5k | while (a != NULL) { |
203 | 4.82k | n = a->next; |
204 | 4.82k | if (use_arg) |
205 | 0 | func_arg(a->data, arg); |
206 | 4.82k | else |
207 | 4.82k | func(a->data); |
208 | 4.82k | a = n; |
209 | 4.82k | } |
210 | 7.71k | } |
211 | 964 | } |
212 | | |
213 | | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
214 | 964 | { |
215 | 964 | doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); |
216 | 964 | } |
217 | | |
218 | | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
219 | 0 | { |
220 | 0 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); |
221 | 0 | } |
222 | | |
223 | | static int expand(OPENSSL_LHASH *lh) |
224 | 620k | { |
225 | 620k | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
226 | 620k | unsigned int p, pmax, nni, j; |
227 | 620k | unsigned long hash; |
228 | | |
229 | 620k | nni = lh->num_alloc_nodes; |
230 | 620k | p = lh->p; |
231 | 620k | pmax = lh->pmax; |
232 | 620k | if (p + 1 >= pmax) { |
233 | 5.77k | j = nni * 2; |
234 | 5.77k | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); |
235 | 5.77k | if (n == NULL) { |
236 | 0 | lh->error++; |
237 | 0 | return 0; |
238 | 0 | } |
239 | 5.77k | lh->b = n; |
240 | 5.77k | memset(n + nni, 0, sizeof(*n) * (j - nni)); |
241 | 5.77k | lh->pmax = nni; |
242 | 5.77k | lh->num_alloc_nodes = j; |
243 | 5.77k | lh->num_expand_reallocs++; |
244 | 5.77k | lh->p = 0; |
245 | 614k | } else { |
246 | 614k | lh->p++; |
247 | 614k | } |
248 | | |
249 | 620k | lh->num_nodes++; |
250 | 620k | lh->num_expands++; |
251 | 620k | n1 = &(lh->b[p]); |
252 | 620k | n2 = &(lh->b[p + pmax]); |
253 | 620k | *n2 = NULL; |
254 | | |
255 | 2.69M | for (np = *n1; np != NULL;) { |
256 | 2.07M | hash = np->hash; |
257 | 2.07M | if ((hash % nni) != p) { /* move it */ |
258 | 354k | *n1 = (*n1)->next; |
259 | 354k | np->next = *n2; |
260 | 354k | *n2 = np; |
261 | 354k | } else |
262 | 1.72M | n1 = &((*n1)->next); |
263 | 2.07M | np = *n1; |
264 | 2.07M | } |
265 | | |
266 | 620k | return 1; |
267 | 620k | } |
268 | | |
269 | | static void contract(OPENSSL_LHASH *lh) |
270 | 0 | { |
271 | 0 | OPENSSL_LH_NODE **n, *n1, *np; |
272 | |
|
273 | 0 | np = lh->b[lh->p + lh->pmax - 1]; |
274 | 0 | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
275 | 0 | if (lh->p == 0) { |
276 | 0 | n = OPENSSL_realloc(lh->b, |
277 | 0 | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
278 | 0 | if (n == NULL) { |
279 | | /* fputs("realloc error in lhash",stderr); */ |
280 | 0 | lh->error++; |
281 | 0 | return; |
282 | 0 | } |
283 | 0 | lh->num_contract_reallocs++; |
284 | 0 | lh->num_alloc_nodes /= 2; |
285 | 0 | lh->pmax /= 2; |
286 | 0 | lh->p = lh->pmax - 1; |
287 | 0 | lh->b = n; |
288 | 0 | } else |
289 | 0 | lh->p--; |
290 | | |
291 | 0 | lh->num_nodes--; |
292 | 0 | lh->num_contracts++; |
293 | |
|
294 | 0 | n1 = lh->b[(int)lh->p]; |
295 | 0 | if (n1 == NULL) |
296 | 0 | lh->b[(int)lh->p] = np; |
297 | 0 | else { |
298 | 0 | while (n1->next != NULL) |
299 | 0 | n1 = n1->next; |
300 | 0 | n1->next = np; |
301 | 0 | } |
302 | 0 | } |
303 | | |
304 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
305 | | const void *data, unsigned long *rhash) |
306 | 3.87M | { |
307 | 3.87M | OPENSSL_LH_NODE **ret, *n1; |
308 | 3.87M | unsigned long hash, nn; |
309 | 3.87M | OPENSSL_LH_COMPFUNC cf; |
310 | | |
311 | 3.87M | hash = (*(lh->hash)) (data); |
312 | 3.87M | tsan_counter(&lh->num_hash_calls); |
313 | 3.87M | *rhash = hash; |
314 | | |
315 | 3.87M | nn = hash % lh->pmax; |
316 | 3.87M | if (nn < lh->p) |
317 | 1.85M | nn = hash % lh->num_alloc_nodes; |
318 | | |
319 | 3.87M | cf = lh->comp; |
320 | 3.87M | ret = &(lh->b[(int)nn]); |
321 | 9.41M | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
322 | 8.04M | tsan_counter(&lh->num_hash_comps); |
323 | 8.04M | if (n1->hash != hash) { |
324 | 5.36M | ret = &(n1->next); |
325 | 5.36M | continue; |
326 | 5.36M | } |
327 | 8.04M | tsan_counter(&lh->num_comp_calls); |
328 | 2.67M | if (cf(n1->data, data) == 0) |
329 | 2.50M | break; |
330 | 168k | ret = &(n1->next); |
331 | 168k | } |
332 | 3.87M | return ret; |
333 | 3.87M | } |
334 | | |
335 | | /* |
336 | | * The following hash seems to work very well on normal text strings no |
337 | | * collisions on /usr/dict/words and it distributes on %2^n quite well, not |
338 | | * as good as MD5, but still good. |
339 | | */ |
340 | | unsigned long OPENSSL_LH_strhash(const char *c) |
341 | 19.2k | { |
342 | 19.2k | unsigned long ret = 0; |
343 | 19.2k | long n; |
344 | 19.2k | unsigned long v; |
345 | 19.2k | int r; |
346 | | |
347 | 19.2k | if ((c == NULL) || (*c == '\0')) |
348 | 0 | return ret; |
349 | | |
350 | 19.2k | n = 0x100; |
351 | 121k | while (*c) { |
352 | 101k | v = n | (*c); |
353 | 101k | n += 0x100; |
354 | 101k | r = (int)((v >> 2) ^ v) & 0x0f; |
355 | 101k | ret = (ret << r) | (ret >> (32 - r)); |
356 | 101k | ret &= 0xFFFFFFFFL; |
357 | 101k | ret ^= v * v; |
358 | 101k | c++; |
359 | 101k | } |
360 | 19.2k | return (ret >> 16) ^ ret; |
361 | 19.2k | } |
362 | | |
363 | | unsigned long openssl_lh_strcasehash(const char *c) |
364 | 0 | { |
365 | 0 | unsigned long ret = 0; |
366 | 0 | long n; |
367 | 0 | unsigned long v; |
368 | 0 | int r; |
369 | |
|
370 | 0 | if (c == NULL || *c == '\0') |
371 | 0 | return ret; |
372 | | |
373 | 0 | for (n = 0x100; *c != '\0'; n += 0x100) { |
374 | 0 | v = n | ossl_tolower(*c); |
375 | 0 | r = (int)((v >> 2) ^ v) & 0x0f; |
376 | 0 | ret = (ret << r) | (ret >> (32 - r)); |
377 | 0 | ret &= 0xFFFFFFFFL; |
378 | 0 | ret ^= v * v; |
379 | 0 | c++; |
380 | 0 | } |
381 | 0 | return (ret >> 16) ^ ret; |
382 | 0 | } |
383 | | |
384 | | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
385 | 482 | { |
386 | 482 | return lh ? lh->num_items : 0; |
387 | 482 | } |
388 | | |
389 | | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
390 | 0 | { |
391 | 0 | return lh->down_load; |
392 | 0 | } |
393 | | |
394 | | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
395 | 0 | { |
396 | 0 | lh->down_load = down_load; |
397 | 0 | } |
398 | | |
399 | | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
400 | 9.62k | { |
401 | 9.62k | return lh->error; |
402 | 9.62k | } |