/src/openssl34/crypto/lhash/lhash.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2024 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 | 6.38M | #define MIN_NODES 16 |
40 | 899k | #define UP_LOAD (2 * LH_LOAD_MULT) /* load times 256 (default 2) */ |
41 | 899k | #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_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) |
52 | 889k | { |
53 | | |
54 | 889k | if (lh == NULL) |
55 | 0 | return NULL; |
56 | 889k | lh->compw = cw; |
57 | 889k | lh->hashw = hw; |
58 | 889k | lh->daw = daw; |
59 | 889k | lh->daaw = daaw; |
60 | 889k | return lh; |
61 | 889k | } |
62 | | |
63 | | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
64 | 899k | { |
65 | 899k | OPENSSL_LHASH *ret; |
66 | | |
67 | 899k | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) |
68 | 0 | return NULL; |
69 | 899k | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
70 | 0 | goto err; |
71 | 899k | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
72 | 899k | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); |
73 | 899k | ret->num_nodes = MIN_NODES / 2; |
74 | 899k | ret->num_alloc_nodes = MIN_NODES; |
75 | 899k | ret->pmax = MIN_NODES / 2; |
76 | 899k | ret->up_load = UP_LOAD; |
77 | 899k | ret->down_load = DOWN_LOAD; |
78 | 899k | return ret; |
79 | | |
80 | 0 | err: |
81 | 0 | OPENSSL_free(ret->b); |
82 | 0 | OPENSSL_free(ret); |
83 | 0 | return NULL; |
84 | 899k | } |
85 | | |
86 | | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
87 | 918k | { |
88 | 918k | if (lh == NULL) |
89 | 27.4k | return; |
90 | | |
91 | 891k | OPENSSL_LH_flush(lh); |
92 | 891k | OPENSSL_free(lh->b); |
93 | 891k | OPENSSL_free(lh); |
94 | 891k | } |
95 | | |
96 | | void OPENSSL_LH_flush(OPENSSL_LHASH *lh) |
97 | 895k | { |
98 | 895k | unsigned int i; |
99 | 895k | OPENSSL_LH_NODE *n, *nn; |
100 | | |
101 | 895k | if (lh == NULL) |
102 | 0 | return; |
103 | | |
104 | 10.1M | for (i = 0; i < lh->num_nodes; i++) { |
105 | 9.22M | n = lh->b[i]; |
106 | 12.4M | while (n != NULL) { |
107 | 3.18M | nn = n->next; |
108 | 3.18M | OPENSSL_free(n); |
109 | 3.18M | n = nn; |
110 | 3.18M | } |
111 | 9.22M | lh->b[i] = NULL; |
112 | 9.22M | } |
113 | | |
114 | 895k | lh->num_items = 0; |
115 | 895k | } |
116 | | |
117 | | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
118 | 8.45M | { |
119 | 8.45M | unsigned long hash; |
120 | 8.45M | OPENSSL_LH_NODE *nn, **rn; |
121 | 8.45M | void *ret; |
122 | | |
123 | 8.45M | lh->error = 0; |
124 | 8.45M | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
125 | 0 | return NULL; /* 'lh->error++' already done in 'expand' */ |
126 | | |
127 | 8.45M | rn = getrn(lh, data, &hash); |
128 | | |
129 | 8.45M | if (*rn == NULL) { |
130 | 7.19M | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
131 | 0 | lh->error++; |
132 | 0 | return NULL; |
133 | 0 | } |
134 | 7.19M | nn->data = data; |
135 | 7.19M | nn->next = NULL; |
136 | 7.19M | nn->hash = hash; |
137 | 7.19M | *rn = nn; |
138 | 7.19M | ret = NULL; |
139 | 7.19M | lh->num_items++; |
140 | 7.19M | } else { /* replace same key */ |
141 | 1.26M | ret = (*rn)->data; |
142 | 1.26M | (*rn)->data = data; |
143 | 1.26M | } |
144 | 8.45M | return ret; |
145 | 8.45M | } |
146 | | |
147 | | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
148 | 3.68M | { |
149 | 3.68M | unsigned long hash; |
150 | 3.68M | OPENSSL_LH_NODE *nn, **rn; |
151 | 3.68M | void *ret; |
152 | | |
153 | 3.68M | lh->error = 0; |
154 | 3.68M | rn = getrn(lh, data, &hash); |
155 | | |
156 | 3.68M | if (*rn == NULL) { |
157 | 0 | return NULL; |
158 | 3.68M | } else { |
159 | 3.68M | nn = *rn; |
160 | 3.68M | *rn = nn->next; |
161 | 3.68M | ret = nn->data; |
162 | 3.68M | OPENSSL_free(nn); |
163 | 3.68M | } |
164 | | |
165 | 3.68M | lh->num_items--; |
166 | 3.68M | if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
167 | 606k | contract(lh); |
168 | | |
169 | 3.68M | return ret; |
170 | 3.68M | } |
171 | | |
172 | | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
173 | 90.8M | { |
174 | 90.8M | unsigned long hash; |
175 | 90.8M | OPENSSL_LH_NODE **rn; |
176 | | |
177 | 90.8M | if (lh->error != 0) |
178 | 0 | lh->error = 0; |
179 | | |
180 | 90.8M | rn = getrn(lh, data, &hash); |
181 | | |
182 | 90.8M | return *rn == NULL ? NULL : (*rn)->data; |
183 | 90.8M | } |
184 | | |
185 | | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
186 | | OPENSSL_LH_DOALL_FUNC_THUNK wfunc, |
187 | | OPENSSL_LH_DOALL_FUNC func, |
188 | | OPENSSL_LH_DOALL_FUNCARG func_arg, |
189 | | OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg, |
190 | | void *arg) |
191 | 15.4M | { |
192 | 15.4M | int i; |
193 | 15.4M | OPENSSL_LH_NODE *a, *n; |
194 | | |
195 | 15.4M | if (lh == NULL) |
196 | 0 | return; |
197 | | |
198 | | /* |
199 | | * reverse the order so we search from 'top to bottom' We were having |
200 | | * memory leaks otherwise |
201 | | */ |
202 | 5.08G | for (i = lh->num_nodes - 1; i >= 0; i--) { |
203 | 5.06G | a = lh->b[i]; |
204 | 15.1G | while (a != NULL) { |
205 | 10.0G | n = a->next; |
206 | 10.0G | if (use_arg) |
207 | 10.0G | wfunc_arg(a->data, arg, func_arg); |
208 | 1.00M | else |
209 | 1.00M | wfunc(a->data, func); |
210 | 10.0G | a = n; |
211 | 10.0G | } |
212 | 5.06G | } |
213 | 15.4M | } |
214 | | |
215 | | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
216 | 160k | { |
217 | 160k | if (lh == NULL) |
218 | 0 | return; |
219 | | |
220 | 160k | doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL, |
221 | 160k | (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL); |
222 | 160k | } |
223 | | |
224 | | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, |
225 | | OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
226 | 15.2M | { |
227 | 15.2M | if (lh == NULL) |
228 | 0 | return; |
229 | | |
230 | 15.2M | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL, |
231 | 15.2M | (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg); |
232 | 15.2M | } |
233 | | |
234 | | void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh, |
235 | | OPENSSL_LH_DOALL_FUNCARG_THUNK daaw, |
236 | | OPENSSL_LH_DOALL_FUNCARG fn, void *arg) |
237 | 46.5k | { |
238 | 46.5k | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL, |
239 | 46.5k | (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg); |
240 | 46.5k | } |
241 | | |
242 | | static int expand(OPENSSL_LHASH *lh) |
243 | 2.82M | { |
244 | 2.82M | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
245 | 2.82M | unsigned int p, pmax, nni, j; |
246 | 2.82M | unsigned long hash; |
247 | | |
248 | 2.82M | nni = lh->num_alloc_nodes; |
249 | 2.82M | p = lh->p; |
250 | 2.82M | pmax = lh->pmax; |
251 | 2.82M | if (p + 1 >= pmax) { |
252 | 43.3k | j = nni * 2; |
253 | 43.3k | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); |
254 | 43.3k | if (n == NULL) { |
255 | 0 | lh->error++; |
256 | 0 | return 0; |
257 | 0 | } |
258 | 43.3k | lh->b = n; |
259 | 43.3k | memset(n + nni, 0, sizeof(*n) * (j - nni)); |
260 | 43.3k | lh->pmax = nni; |
261 | 43.3k | lh->num_alloc_nodes = j; |
262 | 43.3k | lh->p = 0; |
263 | 2.78M | } else { |
264 | 2.78M | lh->p++; |
265 | 2.78M | } |
266 | | |
267 | 2.82M | lh->num_nodes++; |
268 | 2.82M | n1 = &(lh->b[p]); |
269 | 2.82M | n2 = &(lh->b[p + pmax]); |
270 | 2.82M | *n2 = NULL; |
271 | | |
272 | 11.2M | for (np = *n1; np != NULL;) { |
273 | 8.39M | hash = np->hash; |
274 | 8.39M | if ((hash % nni) != p) { /* move it */ |
275 | 3.37M | *n1 = (*n1)->next; |
276 | 3.37M | np->next = *n2; |
277 | 3.37M | *n2 = np; |
278 | 3.37M | } else |
279 | 5.01M | n1 = &((*n1)->next); |
280 | 8.39M | np = *n1; |
281 | 8.39M | } |
282 | | |
283 | 2.82M | return 1; |
284 | 2.82M | } |
285 | | |
286 | | static void contract(OPENSSL_LHASH *lh) |
287 | 606k | { |
288 | 606k | OPENSSL_LH_NODE **n, *n1, *np; |
289 | | |
290 | 606k | np = lh->b[lh->p + lh->pmax - 1]; |
291 | 606k | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
292 | 606k | if (lh->p == 0) { |
293 | 8.47k | n = OPENSSL_realloc(lh->b, |
294 | 8.47k | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
295 | 8.47k | if (n == NULL) { |
296 | | /* fputs("realloc error in lhash", stderr); */ |
297 | 0 | lh->error++; |
298 | 8.47k | } else { |
299 | 8.47k | lh->b = n; |
300 | 8.47k | } |
301 | 8.47k | lh->num_alloc_nodes /= 2; |
302 | 8.47k | lh->pmax /= 2; |
303 | 8.47k | lh->p = lh->pmax - 1; |
304 | 8.47k | } else |
305 | 597k | lh->p--; |
306 | | |
307 | 606k | lh->num_nodes--; |
308 | | |
309 | 606k | n1 = lh->b[(int)lh->p]; |
310 | 606k | if (n1 == NULL) |
311 | 224k | lh->b[(int)lh->p] = np; |
312 | 381k | else { |
313 | 905k | while (n1->next != NULL) |
314 | 524k | n1 = n1->next; |
315 | 381k | n1->next = np; |
316 | 381k | } |
317 | 606k | } |
318 | | |
319 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
320 | | const void *data, unsigned long *rhash) |
321 | 102M | { |
322 | 102M | OPENSSL_LH_NODE **ret, *n1; |
323 | 102M | unsigned long hash, nn; |
324 | | |
325 | 102M | if (lh->hashw != NULL) |
326 | 102M | hash = lh->hashw(data, lh->hash); |
327 | 14.9k | else |
328 | 14.9k | hash = lh->hash(data); |
329 | | |
330 | 102M | *rhash = hash; |
331 | | |
332 | 102M | nn = hash % lh->pmax; |
333 | 102M | if (nn < lh->p) |
334 | 16.0M | nn = hash % lh->num_alloc_nodes; |
335 | | |
336 | 102M | ret = &(lh->b[(int)nn]); |
337 | 742M | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
338 | 675M | if (n1->hash != hash) { |
339 | 639M | ret = &(n1->next); |
340 | 639M | continue; |
341 | 639M | } |
342 | | |
343 | 36.0M | if (lh->compw != NULL) { |
344 | 36.0M | if (lh->compw(n1->data, data, lh->comp) == 0) |
345 | 35.5M | break; |
346 | 36.0M | } else { |
347 | 34 | if (lh->comp(n1->data, data) == 0) |
348 | 34 | break; |
349 | 34 | } |
350 | 529k | ret = &(n1->next); |
351 | 529k | } |
352 | 102M | return ret; |
353 | 102M | } |
354 | | |
355 | | /* |
356 | | * The following hash seems to work very well on normal text strings no |
357 | | * collisions on /usr/dict/words and it distributes on %2^n quite well, not |
358 | | * as good as MD5, but still good. |
359 | | */ |
360 | | unsigned long OPENSSL_LH_strhash(const char *c) |
361 | 36.5M | { |
362 | 36.5M | unsigned long ret = 0; |
363 | 36.5M | long n; |
364 | 36.5M | unsigned long v; |
365 | 36.5M | int r; |
366 | | |
367 | 36.5M | if ((c == NULL) || (*c == '\0')) |
368 | 17.8M | return ret; |
369 | | |
370 | 18.7M | n = 0x100; |
371 | 879M | while (*c) { |
372 | 860M | v = n | (*c); |
373 | 860M | n += 0x100; |
374 | 860M | r = (int)((v >> 2) ^ v) & 0x0f; |
375 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
376 | 860M | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
377 | 860M | ret &= 0xFFFFFFFFL; |
378 | 860M | ret ^= v * v; |
379 | 860M | c++; |
380 | 860M | } |
381 | 18.7M | return (ret >> 16) ^ ret; |
382 | 36.5M | } |
383 | | |
384 | | /* |
385 | | * Case insensitive string hashing. |
386 | | * |
387 | | * The lower/upper case bit is masked out (forcing all letters to be capitals). |
388 | | * The major side effect on non-alpha characters is mapping the symbols and |
389 | | * digits into the control character range (which should be harmless). |
390 | | * The duplication (with respect to the hash value) of printable characters |
391 | | * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^' |
392 | | * respectively (which seems tolerable). |
393 | | * |
394 | | * For EBCDIC, the alpha mapping is to lower case, most symbols go to control |
395 | | * characters. The only duplication is '0' mapping to '^', which is better |
396 | | * than for ASCII. |
397 | | */ |
398 | | unsigned long ossl_lh_strcasehash(const char *c) |
399 | 4.18M | { |
400 | 4.18M | unsigned long ret = 0; |
401 | 4.18M | long n; |
402 | 4.18M | unsigned long v; |
403 | 4.18M | int r; |
404 | | #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST) |
405 | | const long int case_adjust = ~0x40; |
406 | | #else |
407 | 4.18M | const long int case_adjust = ~0x20; |
408 | 4.18M | #endif |
409 | | |
410 | 4.18M | if (c == NULL || *c == '\0') |
411 | 1.18k | return ret; |
412 | | |
413 | 81.8M | for (n = 0x100; *c != '\0'; n += 0x100) { |
414 | 77.6M | v = n | (case_adjust & *c); |
415 | 77.6M | r = (int)((v >> 2) ^ v) & 0x0f; |
416 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
417 | 77.6M | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
418 | 77.6M | ret &= 0xFFFFFFFFL; |
419 | 77.6M | ret ^= v * v; |
420 | 77.6M | c++; |
421 | 77.6M | } |
422 | 4.18M | return (ret >> 16) ^ ret; |
423 | 4.18M | } |
424 | | |
425 | | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
426 | 5.32k | { |
427 | 5.32k | return lh ? lh->num_items : 0; |
428 | 5.32k | } |
429 | | |
430 | | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
431 | 178k | { |
432 | 178k | return lh->down_load; |
433 | 178k | } |
434 | | |
435 | | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
436 | 670k | { |
437 | 670k | lh->down_load = down_load; |
438 | 670k | } |
439 | | |
440 | | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
441 | 6.83M | { |
442 | 6.83M | return lh->error; |
443 | 6.83M | } |