/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 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 |
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 "lhash_lcl.h" |
17 | | |
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 |
24 | | * addressing", Proc. 6th Conference on Very Large Databases: 212-223 |
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 | | */ |
35 | | |
36 | | #undef MIN_NODES |
37 | 2.71k | #define MIN_NODES 16 |
38 | 16 | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ |
39 | 16 | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ |
40 | | |
41 | | static int expand(OPENSSL_LHASH *lh); |
42 | | static void contract(OPENSSL_LHASH *lh); |
43 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash); |
44 | | |
45 | | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
46 | 16 | { |
47 | 16 | OPENSSL_LHASH *ret; |
48 | 16 | |
49 | 16 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
50 | 0 | /* |
51 | 0 | * Do not set the error code, because the ERR code uses LHASH |
52 | 0 | * and we want to avoid possible endless error loop. |
53 | 0 | * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE); |
54 | 0 | */ |
55 | 0 | return NULL; |
56 | 0 | } |
57 | 16 | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
58 | 16 | goto err; |
59 | 16 | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
60 | 16 | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); |
61 | 16 | ret->num_nodes = MIN_NODES / 2; |
62 | 16 | ret->num_alloc_nodes = MIN_NODES; |
63 | 16 | ret->pmax = MIN_NODES / 2; |
64 | 16 | ret->up_load = UP_LOAD; |
65 | 16 | ret->down_load = DOWN_LOAD; |
66 | 16 | return ret; |
67 | 0 | |
68 | 0 | err: |
69 | 0 | OPENSSL_free(ret->b); |
70 | 0 | OPENSSL_free(ret); |
71 | 0 | return NULL; |
72 | 16 | } |
73 | | |
74 | | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
75 | 24 | { |
76 | 24 | unsigned int i; |
77 | 24 | OPENSSL_LH_NODE *n, *nn; |
78 | 24 | |
79 | 24 | if (lh == NULL) |
80 | 24 | return; |
81 | 16 | |
82 | 10.9k | for (i = 0; i < lh->num_nodes; i++) { |
83 | 10.9k | n = lh->b[i]; |
84 | 30.1k | while (n != NULL) { |
85 | 19.2k | nn = n->next; |
86 | 19.2k | OPENSSL_free(n); |
87 | 19.2k | n = nn; |
88 | 19.2k | } |
89 | 10.9k | } |
90 | 16 | OPENSSL_free(lh->b); |
91 | 16 | OPENSSL_free(lh); |
92 | 16 | } |
93 | | |
94 | | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
95 | 62.7k | { |
96 | 62.7k | unsigned long hash; |
97 | 62.7k | OPENSSL_LH_NODE *nn, **rn; |
98 | 62.7k | void *ret; |
99 | 62.7k | |
100 | 62.7k | lh->error = 0; |
101 | 62.7k | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
102 | 0 | return NULL; /* 'lh->error++' already done in 'expand' */ |
103 | 62.7k | |
104 | 62.7k | rn = getrn(lh, data, &hash); |
105 | 62.7k | |
106 | 62.7k | if (*rn == NULL) { |
107 | 21.8k | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
108 | 0 | lh->error++; |
109 | 0 | return NULL; |
110 | 0 | } |
111 | 21.8k | nn->data = data; |
112 | 21.8k | nn->next = NULL; |
113 | 21.8k | nn->hash = hash; |
114 | 21.8k | *rn = nn; |
115 | 21.8k | ret = NULL; |
116 | 21.8k | lh->num_insert++; |
117 | 21.8k | lh->num_items++; |
118 | 40.8k | } else { /* replace same key */ |
119 | 40.8k | ret = (*rn)->data; |
120 | 40.8k | (*rn)->data = data; |
121 | 40.8k | lh->num_replace++; |
122 | 40.8k | } |
123 | 62.7k | return ret; |
124 | 62.7k | } |
125 | | |
126 | | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
127 | 2.66k | { |
128 | 2.66k | unsigned long hash; |
129 | 2.66k | OPENSSL_LH_NODE *nn, **rn; |
130 | 2.66k | void *ret; |
131 | 2.66k | |
132 | 2.66k | lh->error = 0; |
133 | 2.66k | rn = getrn(lh, data, &hash); |
134 | 2.66k | |
135 | 2.66k | if (*rn == NULL) { |
136 | 0 | lh->num_no_delete++; |
137 | 0 | return NULL; |
138 | 2.66k | } else { |
139 | 2.66k | nn = *rn; |
140 | 2.66k | *rn = nn->next; |
141 | 2.66k | ret = nn->data; |
142 | 2.66k | OPENSSL_free(nn); |
143 | 2.66k | lh->num_delete++; |
144 | 2.66k | } |
145 | 2.66k | |
146 | 2.66k | lh->num_items--; |
147 | 2.66k | if ((lh->num_nodes > MIN_NODES) && |
148 | 2.66k | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
149 | 8 | contract(lh); |
150 | 2.66k | |
151 | 2.66k | return ret; |
152 | 2.66k | } |
153 | | |
154 | | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
155 | 1.90M | { |
156 | 1.90M | unsigned long hash; |
157 | 1.90M | OPENSSL_LH_NODE **rn; |
158 | 1.90M | void *ret; |
159 | 1.90M | |
160 | 1.90M | tsan_store((TSAN_QUALIFIER int *)&lh->error, 0); |
161 | 1.90M | |
162 | 1.90M | rn = getrn(lh, data, &hash); |
163 | 1.90M | |
164 | 1.90M | if (*rn == NULL) { |
165 | 325k | tsan_counter(&lh->num_retrieve_miss); |
166 | 325k | return NULL; |
167 | 1.57M | } else { |
168 | 1.57M | ret = (*rn)->data; |
169 | 1.57M | tsan_counter(&lh->num_retrieve); |
170 | 1.57M | } |
171 | 1.90M | |
172 | 1.90M | return ret; |
173 | 1.90M | } |
174 | | |
175 | | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
176 | | OPENSSL_LH_DOALL_FUNC func, |
177 | | OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) |
178 | 24 | { |
179 | 24 | int i; |
180 | 24 | OPENSSL_LH_NODE *a, *n; |
181 | 24 | |
182 | 24 | if (lh == NULL) |
183 | 24 | return; |
184 | 24 | |
185 | 24 | /* |
186 | 24 | * reverse the order so we search from 'top to bottom' We were having |
187 | 24 | * memory leaks otherwise |
188 | 24 | */ |
189 | 4.01k | for (i = lh->num_nodes - 1; i >= 0; i--) { |
190 | 3.99k | a = lh->b[i]; |
191 | 6.65k | while (a != NULL) { |
192 | 2.66k | n = a->next; |
193 | 2.66k | if (use_arg) |
194 | 0 | func_arg(a->data, arg); |
195 | 2.66k | else |
196 | 2.66k | func(a->data); |
197 | 2.66k | a = n; |
198 | 2.66k | } |
199 | 3.99k | } |
200 | 24 | } |
201 | | |
202 | | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
203 | 24 | { |
204 | 24 | doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); |
205 | 24 | } |
206 | | |
207 | | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
208 | 0 | { |
209 | 0 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); |
210 | 0 | } |
211 | | |
212 | | static int expand(OPENSSL_LHASH *lh) |
213 | 10.8k | { |
214 | 10.8k | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
215 | 10.8k | unsigned int p, pmax, nni, j; |
216 | 10.8k | unsigned long hash; |
217 | 10.8k | |
218 | 10.8k | nni = lh->num_alloc_nodes; |
219 | 10.8k | p = lh->p; |
220 | 10.8k | pmax = lh->pmax; |
221 | 10.8k | if (p + 1 >= pmax) { |
222 | 88 | j = nni * 2; |
223 | 88 | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); |
224 | 88 | if (n == NULL) { |
225 | 0 | lh->error++; |
226 | 0 | return 0; |
227 | 0 | } |
228 | 88 | lh->b = n; |
229 | 88 | memset(n + nni, 0, sizeof(*n) * (j - nni)); |
230 | 88 | lh->pmax = nni; |
231 | 88 | lh->num_alloc_nodes = j; |
232 | 88 | lh->num_expand_reallocs++; |
233 | 88 | lh->p = 0; |
234 | 10.7k | } else { |
235 | 10.7k | lh->p++; |
236 | 10.7k | } |
237 | 10.8k | |
238 | 10.8k | lh->num_nodes++; |
239 | 10.8k | lh->num_expands++; |
240 | 10.8k | n1 = &(lh->b[p]); |
241 | 10.8k | n2 = &(lh->b[p + pmax]); |
242 | 10.8k | *n2 = NULL; |
243 | 10.8k | |
244 | 49.1k | for (np = *n1; np != NULL;) { |
245 | 38.3k | hash = np->hash; |
246 | 38.3k | if ((hash % nni) != p) { /* move it */ |
247 | 4.65k | *n1 = (*n1)->next; |
248 | 4.65k | np->next = *n2; |
249 | 4.65k | *n2 = np; |
250 | 4.65k | } else |
251 | 33.6k | n1 = &((*n1)->next); |
252 | 38.3k | np = *n1; |
253 | 38.3k | } |
254 | 10.8k | |
255 | 10.8k | return 1; |
256 | 10.8k | } |
257 | | |
258 | | static void contract(OPENSSL_LHASH *lh) |
259 | 8 | { |
260 | 8 | OPENSSL_LH_NODE **n, *n1, *np; |
261 | 8 | |
262 | 8 | np = lh->b[lh->p + lh->pmax - 1]; |
263 | 8 | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
264 | 8 | if (lh->p == 0) { |
265 | 0 | n = OPENSSL_realloc(lh->b, |
266 | 0 | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
267 | 0 | if (n == NULL) { |
268 | 0 | /* fputs("realloc error in lhash",stderr); */ |
269 | 0 | lh->error++; |
270 | 0 | return; |
271 | 0 | } |
272 | 0 | lh->num_contract_reallocs++; |
273 | 0 | lh->num_alloc_nodes /= 2; |
274 | 0 | lh->pmax /= 2; |
275 | 0 | lh->p = lh->pmax - 1; |
276 | 0 | lh->b = n; |
277 | 0 | } else |
278 | 8 | lh->p--; |
279 | 8 | |
280 | 8 | lh->num_nodes--; |
281 | 8 | lh->num_contracts++; |
282 | 8 | |
283 | 8 | n1 = lh->b[(int)lh->p]; |
284 | 8 | if (n1 == NULL) |
285 | 8 | lh->b[(int)lh->p] = np; |
286 | 0 | else { |
287 | 0 | while (n1->next != NULL) |
288 | 0 | n1 = n1->next; |
289 | 0 | n1->next = np; |
290 | 0 | } |
291 | 8 | } |
292 | | |
293 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
294 | | const void *data, unsigned long *rhash) |
295 | 1.96M | { |
296 | 1.96M | OPENSSL_LH_NODE **ret, *n1; |
297 | 1.96M | unsigned long hash, nn; |
298 | 1.96M | OPENSSL_LH_COMPFUNC cf; |
299 | 1.96M | |
300 | 1.96M | hash = (*(lh->hash)) (data); |
301 | 1.96M | tsan_counter(&lh->num_hash_calls); |
302 | 1.96M | *rhash = hash; |
303 | 1.96M | |
304 | 1.96M | nn = hash % lh->pmax; |
305 | 1.96M | if (nn < lh->p) |
306 | 1.67M | nn = hash % lh->num_alloc_nodes; |
307 | 1.96M | |
308 | 1.96M | cf = lh->comp; |
309 | 1.96M | ret = &(lh->b[(int)nn]); |
310 | 6.93M | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
311 | 6.58M | tsan_counter(&lh->num_hash_comps); |
312 | 6.58M | if (n1->hash != hash) { |
313 | 4.74M | ret = &(n1->next); |
314 | 4.74M | continue; |
315 | 4.74M | } |
316 | 1.84M | tsan_counter(&lh->num_comp_calls); |
317 | 1.84M | if (cf(n1->data, data) == 0) |
318 | 1.62M | break; |
319 | 220k | ret = &(n1->next); |
320 | 220k | } |
321 | 1.96M | return ret; |
322 | 1.96M | } |
323 | | |
324 | | /* |
325 | | * The following hash seems to work very well on normal text strings no |
326 | | * collisions on /usr/dict/words and it distributes on %2^n quite well, not |
327 | | * as good as MD5, but still good. |
328 | | */ |
329 | | unsigned long OPENSSL_LH_strhash(const char *c) |
330 | 11.7k | { |
331 | 11.7k | unsigned long ret = 0; |
332 | 11.7k | long n; |
333 | 11.7k | unsigned long v; |
334 | 11.7k | int r; |
335 | 11.7k | |
336 | 11.7k | if ((c == NULL) || (*c == '\0')) |
337 | 574 | return ret; |
338 | 11.1k | |
339 | 11.1k | n = 0x100; |
340 | 4.61M | while (*c) { |
341 | 4.60M | v = n | (*c); |
342 | 4.60M | n += 0x100; |
343 | 4.60M | r = (int)((v >> 2) ^ v) & 0x0f; |
344 | 4.60M | ret = (ret << r) | (ret >> (32 - r)); |
345 | 4.60M | ret &= 0xFFFFFFFFL; |
346 | 4.60M | ret ^= v * v; |
347 | 4.60M | c++; |
348 | 4.60M | } |
349 | 11.1k | return (ret >> 16) ^ ret; |
350 | 11.1k | } |
351 | | |
352 | | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
353 | 0 | { |
354 | 0 | return lh ? lh->num_items : 0; |
355 | 0 | } |
356 | | |
357 | | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
358 | 24 | { |
359 | 24 | return lh->down_load; |
360 | 24 | } |
361 | | |
362 | | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
363 | 40 | { |
364 | 40 | lh->down_load = down_load; |
365 | 40 | } |
366 | | |
367 | | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
368 | 2.66k | { |
369 | 2.66k | return lh->error; |
370 | 2.66k | } |