/src/libressl/crypto/lhash/lhash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: lhash.c,v 1.19 2019/05/12 00:09:59 beck Exp $ */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
11 | | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
18 | | * If this package is used in a product, Eric Young should be given attribution |
19 | | * as the author of the parts of the library used. |
20 | | * This can be in the form of a textual message at program startup or |
21 | | * in documentation (online or textual) provided with the package. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
37 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | | * the apps directory (application code) you must include an acknowledgement: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | | * SUCH DAMAGE. |
52 | | * |
53 | | * The licence and distribution terms for any publically available version or |
54 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | |
59 | | /* Code for dynamic hash table routines |
60 | | * Author - Eric Young v 2.0 |
61 | | * |
62 | | * 2.2 eay - added #include "crypto.h" so the memory leak checking code is |
63 | | * present. eay 18-Jun-98 |
64 | | * |
65 | | * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98 |
66 | | * |
67 | | * 2.0 eay - Fixed a bug that occurred when using lh_delete |
68 | | * from inside lh_doall(). As entries were deleted, |
69 | | * the 'table' was 'contract()ed', making some entries |
70 | | * jump from the end of the table to the start, there by |
71 | | * skipping the lh_doall() processing. eay - 4/12/95 |
72 | | * |
73 | | * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs |
74 | | * were not being free()ed. 21/11/95 |
75 | | * |
76 | | * 1.8 eay - Put the stats routines into a separate file, lh_stats.c |
77 | | * 19/09/95 |
78 | | * |
79 | | * 1.7 eay - Removed the fputs() for realloc failures - the code |
80 | | * should silently tolerate them. I have also fixed things |
81 | | * lint complained about 04/05/95 |
82 | | * |
83 | | * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92 |
84 | | * |
85 | | * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992 |
86 | | * |
87 | | * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91 |
88 | | * |
89 | | * 1.3 eay - Fixed a few lint problems 19/3/1991 |
90 | | * |
91 | | * 1.2 eay - Fixed lh_doall problem 13/3/1991 |
92 | | * |
93 | | * 1.1 eay - Added lh_doall |
94 | | * |
95 | | * 1.0 eay - First version |
96 | | */ |
97 | | #include <stdio.h> |
98 | | #include <string.h> |
99 | | #include <stdlib.h> |
100 | | |
101 | | #include <openssl/opensslconf.h> |
102 | | |
103 | | #include <openssl/crypto.h> |
104 | | #include <openssl/lhash.h> |
105 | | |
106 | | #undef MIN_NODES |
107 | 27.0k | #define MIN_NODES 16 |
108 | 6.77k | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ |
109 | 6.77k | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ |
110 | | |
111 | | static void expand(_LHASH *lh); |
112 | | static void contract(_LHASH *lh); |
113 | | static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash); |
114 | | |
115 | | _LHASH * |
116 | | lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c) |
117 | 6.77k | { |
118 | 6.77k | _LHASH *ret; |
119 | | |
120 | 6.77k | if ((ret = calloc(1, sizeof(_LHASH))) == NULL) |
121 | 0 | return NULL; |
122 | 6.77k | if ((ret->b = calloc(MIN_NODES, sizeof(LHASH_NODE *))) == NULL) { |
123 | 0 | free(ret); |
124 | 0 | return NULL; |
125 | 0 | } |
126 | 6.77k | ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c); |
127 | 6.77k | ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h); |
128 | 6.77k | ret->num_nodes = MIN_NODES / 2; |
129 | 6.77k | ret->num_alloc_nodes = MIN_NODES; |
130 | 6.77k | ret->pmax = MIN_NODES / 2; |
131 | 6.77k | ret->up_load = UP_LOAD; |
132 | 6.77k | ret->down_load = DOWN_LOAD; |
133 | | |
134 | 6.77k | return (ret); |
135 | 6.77k | } |
136 | | |
137 | | void |
138 | | lh_free(_LHASH *lh) |
139 | 6.76k | { |
140 | 6.76k | unsigned int i; |
141 | 6.76k | LHASH_NODE *n, *nn; |
142 | | |
143 | 6.76k | if (lh == NULL) |
144 | 0 | return; |
145 | | |
146 | 60.8k | for (i = 0; i < lh->num_nodes; i++) { |
147 | 54.1k | n = lh->b[i]; |
148 | 54.1k | while (n != NULL) { |
149 | 0 | nn = n->next; |
150 | 0 | free(n); |
151 | 0 | n = nn; |
152 | 0 | } |
153 | 54.1k | } |
154 | 6.76k | free(lh->b); |
155 | 6.76k | free(lh); |
156 | 6.76k | } |
157 | | |
158 | | void * |
159 | | lh_insert(_LHASH *lh, void *data) |
160 | 3.78k | { |
161 | 3.78k | unsigned long hash; |
162 | 3.78k | LHASH_NODE *nn, **rn; |
163 | 3.78k | void *ret; |
164 | | |
165 | 3.78k | lh->error = 0; |
166 | 3.78k | if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) |
167 | 1.77k | expand(lh); |
168 | | |
169 | 3.78k | rn = getrn(lh, data, &hash); |
170 | | |
171 | 3.78k | if (*rn == NULL) { |
172 | 3.62k | if ((nn = malloc(sizeof(LHASH_NODE))) == NULL) { |
173 | 0 | lh->error++; |
174 | 0 | return (NULL); |
175 | 0 | } |
176 | 3.62k | nn->data = data; |
177 | 3.62k | nn->next = NULL; |
178 | 3.62k | #ifndef OPENSSL_NO_HASH_COMP |
179 | 3.62k | nn->hash = hash; |
180 | 3.62k | #endif |
181 | 3.62k | *rn = nn; |
182 | 3.62k | ret = NULL; |
183 | 3.62k | lh->num_insert++; |
184 | 3.62k | lh->num_items++; |
185 | 3.62k | } |
186 | 166 | else /* replace same key */ |
187 | 166 | { |
188 | 166 | ret = (*rn)->data; |
189 | 166 | (*rn)->data = data; |
190 | 166 | lh->num_replace++; |
191 | 166 | } |
192 | 3.78k | return (ret); |
193 | 3.78k | } |
194 | | |
195 | | void * |
196 | | lh_delete(_LHASH *lh, const void *data) |
197 | 0 | { |
198 | 0 | unsigned long hash; |
199 | 0 | LHASH_NODE *nn, **rn; |
200 | 0 | void *ret; |
201 | |
|
202 | 0 | lh->error = 0; |
203 | 0 | rn = getrn(lh, data, &hash); |
204 | |
|
205 | 0 | if (*rn == NULL) { |
206 | 0 | lh->num_no_delete++; |
207 | 0 | return (NULL); |
208 | 0 | } else { |
209 | 0 | nn= *rn; |
210 | 0 | *rn = nn->next; |
211 | 0 | ret = nn->data; |
212 | 0 | free(nn); |
213 | 0 | lh->num_delete++; |
214 | 0 | } |
215 | | |
216 | 0 | lh->num_items--; |
217 | 0 | if ((lh->num_nodes > MIN_NODES) && |
218 | 0 | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
219 | 0 | contract(lh); |
220 | |
|
221 | 0 | return (ret); |
222 | 0 | } |
223 | | |
224 | | void * |
225 | | lh_retrieve(_LHASH *lh, const void *data) |
226 | 158k | { |
227 | 158k | unsigned long hash; |
228 | 158k | LHASH_NODE **rn; |
229 | 158k | void *ret; |
230 | | |
231 | 158k | lh->error = 0; |
232 | 158k | rn = getrn(lh, data, &hash); |
233 | | |
234 | 158k | if (*rn == NULL) { |
235 | 552 | lh->num_retrieve_miss++; |
236 | 552 | return (NULL); |
237 | 158k | } else { |
238 | 158k | ret = (*rn)->data; |
239 | 158k | lh->num_retrieve++; |
240 | 158k | } |
241 | 158k | return (ret); |
242 | 158k | } |
243 | | |
244 | | static void |
245 | | doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, |
246 | | LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) |
247 | 6.76k | { |
248 | 6.76k | int i; |
249 | 6.76k | LHASH_NODE *a, *n; |
250 | | |
251 | 6.76k | if (lh == NULL) |
252 | 0 | return; |
253 | | |
254 | | /* reverse the order so we search from 'top to bottom' |
255 | | * We were having memory leaks otherwise */ |
256 | 60.8k | for (i = lh->num_nodes - 1; i >= 0; i--) { |
257 | 54.1k | a = lh->b[i]; |
258 | 54.1k | while (a != NULL) { |
259 | | /* 28/05/91 - eay - n added so items can be deleted |
260 | | * via lh_doall */ |
261 | | /* 22/05/08 - ben - eh? since a is not passed, |
262 | | * this should not be needed */ |
263 | 0 | n = a->next; |
264 | 0 | if (use_arg) |
265 | 0 | func_arg(a->data, arg); |
266 | 0 | else |
267 | 0 | func(a->data); |
268 | 0 | a = n; |
269 | 0 | } |
270 | 54.1k | } |
271 | 6.76k | } |
272 | | |
273 | | void |
274 | | lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func) |
275 | 0 | { |
276 | 0 | doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL); |
277 | 0 | } |
278 | | |
279 | | void |
280 | | lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg) |
281 | 6.76k | { |
282 | 6.76k | doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg); |
283 | 6.76k | } |
284 | | |
285 | | static void |
286 | | expand(_LHASH *lh) |
287 | 1.77k | { |
288 | 1.77k | LHASH_NODE **n, **n1, **n2, *np; |
289 | 1.77k | unsigned int p, i, j; |
290 | 1.77k | unsigned long hash, nni; |
291 | | |
292 | 1.77k | lh->num_nodes++; |
293 | 1.77k | lh->num_expands++; |
294 | 1.77k | p = (int)lh->p++; |
295 | 1.77k | n1 = &(lh->b[p]); |
296 | 1.77k | n2 = &(lh->b[p + (int)lh->pmax]); |
297 | 1.77k | *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */ |
298 | 1.77k | nni = lh->num_alloc_nodes; |
299 | | |
300 | 7.95k | for (np = *n1; np != NULL; ) { |
301 | 6.18k | #ifndef OPENSSL_NO_HASH_COMP |
302 | 6.18k | hash = np->hash; |
303 | | #else |
304 | | hash = lh->hash(np->data); |
305 | | lh->num_hash_calls++; |
306 | | #endif |
307 | 6.18k | if ((hash % nni) != p) { /* move it */ |
308 | 1.12k | *n1 = (*n1)->next; |
309 | 1.12k | np->next= *n2; |
310 | 1.12k | *n2 = np; |
311 | 1.12k | } else |
312 | 5.06k | n1 = &((*n1)->next); |
313 | 6.18k | np= *n1; |
314 | 6.18k | } |
315 | | |
316 | 1.77k | if ((lh->p) >= lh->pmax) { |
317 | 20 | j = (int)lh->num_alloc_nodes * 2; |
318 | 20 | n = reallocarray(lh->b, j, sizeof(LHASH_NODE *)); |
319 | 20 | if (n == NULL) { |
320 | | /* fputs("realloc error in lhash", stderr); */ |
321 | 0 | lh->error++; |
322 | 0 | lh->p = 0; |
323 | 0 | return; |
324 | 0 | } |
325 | | /* else */ |
326 | 2.51k | for (i = (int)lh->num_alloc_nodes; i < j; i++)/* 26/02/92 eay */ |
327 | 2.49k | n[i] = NULL; /* 02/03/92 eay */ |
328 | 20 | lh->pmax = lh->num_alloc_nodes; |
329 | 20 | lh->num_alloc_nodes = j; |
330 | 20 | lh->num_expand_reallocs++; |
331 | 20 | lh->p = 0; |
332 | 20 | lh->b = n; |
333 | 20 | } |
334 | 1.77k | } |
335 | | |
336 | | static void |
337 | | contract(_LHASH *lh) |
338 | 0 | { |
339 | 0 | LHASH_NODE **n, *n1, *np; |
340 | |
|
341 | 0 | np = lh->b[lh->p + lh->pmax - 1]; |
342 | 0 | lh->b[lh->p+lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
343 | 0 | if (lh->p == 0) { |
344 | 0 | n = reallocarray(lh->b, lh->pmax, sizeof(LHASH_NODE *)); |
345 | 0 | if (n == NULL) { |
346 | | /* fputs("realloc error in lhash", stderr); */ |
347 | 0 | lh->error++; |
348 | 0 | return; |
349 | 0 | } |
350 | 0 | lh->num_contract_reallocs++; |
351 | 0 | lh->num_alloc_nodes /= 2; |
352 | 0 | lh->pmax /= 2; |
353 | 0 | lh->p = lh->pmax - 1; |
354 | 0 | lh->b = n; |
355 | 0 | } else |
356 | 0 | lh->p--; |
357 | | |
358 | 0 | lh->num_nodes--; |
359 | 0 | lh->num_contracts++; |
360 | |
|
361 | 0 | n1 = lh->b[(int)lh->p]; |
362 | 0 | if (n1 == NULL) |
363 | 0 | lh->b[(int)lh->p] = np; |
364 | 0 | else { |
365 | 0 | while (n1->next != NULL) |
366 | 0 | n1 = n1->next; |
367 | 0 | n1->next = np; |
368 | 0 | } |
369 | 0 | } |
370 | | |
371 | | static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash) |
372 | 162k | { |
373 | 162k | LHASH_NODE **ret, *n1; |
374 | 162k | unsigned long hash, nn; |
375 | 162k | LHASH_COMP_FN_TYPE cf; |
376 | | |
377 | 162k | hash = (*(lh->hash))(data); |
378 | 162k | lh->num_hash_calls++; |
379 | 162k | *rhash = hash; |
380 | | |
381 | 162k | nn = hash % lh->pmax; |
382 | 162k | if (nn < lh->p) |
383 | 1.57k | nn = hash % lh->num_alloc_nodes; |
384 | | |
385 | 162k | cf = lh->comp; |
386 | 162k | ret = &(lh->b[(int)nn]); |
387 | 193k | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
388 | 189k | #ifndef OPENSSL_NO_HASH_COMP |
389 | 189k | lh->num_hash_comps++; |
390 | 189k | if (n1->hash != hash) { |
391 | 30.6k | ret = &(n1->next); |
392 | 30.6k | continue; |
393 | 30.6k | } |
394 | 158k | #endif |
395 | 158k | lh->num_comp_calls++; |
396 | 158k | if (cf(n1->data, data) == 0) |
397 | 158k | break; |
398 | 188 | ret = &(n1->next); |
399 | 188 | } |
400 | 162k | return (ret); |
401 | 162k | } |
402 | | |
403 | | /* The following hash seems to work very well on normal text strings |
404 | | * no collisions on /usr/dict/words and it distributes on %2^n quite |
405 | | * well, not as good as MD5, but still good. |
406 | | */ |
407 | | unsigned long |
408 | | lh_strhash(const char *c) |
409 | 716 | { |
410 | 716 | unsigned long ret = 0; |
411 | 716 | unsigned long n, v; |
412 | 716 | unsigned int r; |
413 | | |
414 | 716 | if (c == NULL || *c == '\0') |
415 | 0 | return ret; |
416 | | |
417 | 716 | n = 0x100; |
418 | 8.31k | while (*c) { |
419 | 7.60k | v = n | *c; |
420 | 7.60k | n += 0x100; |
421 | 7.60k | if ((r = ((v >> 2) ^ v) & 0x0f) != 0) |
422 | 7.60k | ret = (ret << r) | (ret >> (32 - r)); |
423 | 7.60k | ret &= 0xFFFFFFFFUL; |
424 | 7.60k | ret ^= v * v; |
425 | 7.60k | c++; |
426 | 7.60k | } |
427 | 716 | return (ret >> 16) ^ ret; |
428 | 716 | } |
429 | | |
430 | | unsigned long |
431 | | lh_num_items(const _LHASH *lh) |
432 | 0 | { |
433 | 0 | return lh ? lh->num_items : 0; |
434 | 0 | } |