]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/lhash/lhash.c
Add UEFI flag for rand build
[thirdparty/openssl.git] / crypto / lhash / lhash.c
CommitLineData
d02b48c6 1/* crypto/lhash/lhash.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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.
0f113f3e 8 *
d02b48c6
RE
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).
0f113f3e 15 *
d02b48c6
RE
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.
0f113f3e 22 *
d02b48c6
RE
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 :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
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.
0f113f3e 52 *
d02b48c6
RE
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
1d97c843
TH
59/*-
60 * Code for dynamic hash table routines
d02b48c6
RE
61 * Author - Eric Young v 2.0
62 *
dfeab068 63 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
0f113f3e 64 * present. eay 18-Jun-98
dfeab068
RE
65 *
66 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
67 *
657e60fa 68 * 2.0 eay - Fixed a bug that occurred when using lh_delete
0f113f3e
MC
69 * from inside lh_doall(). As entries were deleted,
70 * the 'table' was 'contract()ed', making some entries
71 * jump from the end of the table to the start, there by
72 * skipping the lh_doall() processing. eay - 4/12/95
d02b48c6
RE
73 *
74 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
0f113f3e 75 * were not being free()ed. 21/11/95
d02b48c6
RE
76 *
77 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
0f113f3e 78 * 19/09/95
d02b48c6
RE
79 *
80 * 1.7 eay - Removed the fputs() for realloc failures - the code
81 * should silently tolerate them. I have also fixed things
82 * lint complained about 04/05/95
83 *
84 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
85 *
86 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
87 *
88 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
89 *
90 * 1.3 eay - Fixed a few lint problems 19/3/1991
91 *
92 * 1.2 eay - Fixed lh_doall problem 13/3/1991
93 *
94 * 1.1 eay - Added lh_doall
95 *
96 * 1.0 eay - First version
97 */
98#include <stdio.h>
99#include <string.h>
100#include <stdlib.h>
ec577822
BM
101#include <openssl/crypto.h>
102#include <openssl/lhash.h>
d02b48c6 103
0f113f3e
MC
104#undef MIN_NODES
105#define MIN_NODES 16
106#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
107#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
d02b48c6 108
3c1d6bbc
BL
109static void expand(_LHASH *lh);
110static void contract(_LHASH *lh);
111static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
d02b48c6 112
3c1d6bbc 113_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
0f113f3e
MC
114{
115 _LHASH *ret;
116 int i;
117
b4faea50 118 if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
0f113f3e 119 goto err0;
b4faea50 120 if ((ret->b = OPENSSL_malloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
0f113f3e
MC
121 goto err1;
122 for (i = 0; i < MIN_NODES; i++)
123 ret->b[i] = NULL;
124 ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
125 ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
126 ret->num_nodes = MIN_NODES / 2;
127 ret->num_alloc_nodes = MIN_NODES;
128 ret->p = 0;
129 ret->pmax = MIN_NODES / 2;
130 ret->up_load = UP_LOAD;
131 ret->down_load = DOWN_LOAD;
132 ret->num_items = 0;
133
134 ret->num_expands = 0;
135 ret->num_expand_reallocs = 0;
136 ret->num_contracts = 0;
137 ret->num_contract_reallocs = 0;
138 ret->num_hash_calls = 0;
139 ret->num_comp_calls = 0;
140 ret->num_insert = 0;
141 ret->num_replace = 0;
142 ret->num_delete = 0;
143 ret->num_no_delete = 0;
144 ret->num_retrieve = 0;
145 ret->num_retrieve_miss = 0;
146 ret->num_hash_comps = 0;
147
148 ret->error = 0;
149 return (ret);
150 err1:
151 OPENSSL_free(ret);
152 err0:
153 return (NULL);
154}
d02b48c6 155
3c1d6bbc 156void lh_free(_LHASH *lh)
0f113f3e
MC
157{
158 unsigned int i;
159 LHASH_NODE *n, *nn;
160
161 if (lh == NULL)
162 return;
163
164 for (i = 0; i < lh->num_nodes; i++) {
165 n = lh->b[i];
166 while (n != NULL) {
167 nn = n->next;
168 OPENSSL_free(n);
169 n = nn;
170 }
171 }
172 OPENSSL_free(lh->b);
173 OPENSSL_free(lh);
174}
d02b48c6 175
3c1d6bbc 176void *lh_insert(_LHASH *lh, void *data)
0f113f3e
MC
177{
178 unsigned long hash;
179 LHASH_NODE *nn, **rn;
180 void *ret;
181
182 lh->error = 0;
183 if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
184 expand(lh);
185
186 rn = getrn(lh, data, &hash);
187
188 if (*rn == NULL) {
b4faea50 189 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
0f113f3e
MC
190 lh->error++;
191 return (NULL);
192 }
193 nn->data = data;
194 nn->next = NULL;
0f113f3e 195 nn->hash = hash;
0f113f3e
MC
196 *rn = nn;
197 ret = NULL;
198 lh->num_insert++;
199 lh->num_items++;
200 } else { /* replace same key */
201
202 ret = (*rn)->data;
203 (*rn)->data = data;
204 lh->num_replace++;
205 }
206 return (ret);
207}
d02b48c6 208
3c1d6bbc 209void *lh_delete(_LHASH *lh, const void *data)
0f113f3e
MC
210{
211 unsigned long hash;
212 LHASH_NODE *nn, **rn;
213 void *ret;
214
215 lh->error = 0;
216 rn = getrn(lh, data, &hash);
217
218 if (*rn == NULL) {
219 lh->num_no_delete++;
220 return (NULL);
221 } else {
222 nn = *rn;
223 *rn = nn->next;
224 ret = nn->data;
225 OPENSSL_free(nn);
226 lh->num_delete++;
227 }
228
229 lh->num_items--;
230 if ((lh->num_nodes > MIN_NODES) &&
231 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
232 contract(lh);
233
234 return (ret);
235}
d02b48c6 236
3c1d6bbc 237void *lh_retrieve(_LHASH *lh, const void *data)
0f113f3e
MC
238{
239 unsigned long hash;
240 LHASH_NODE **rn;
241 void *ret;
242
243 lh->error = 0;
244 rn = getrn(lh, data, &hash);
245
246 if (*rn == NULL) {
247 lh->num_retrieve_miss++;
248 return (NULL);
249 } else {
250 ret = (*rn)->data;
251 lh->num_retrieve++;
252 }
253 return (ret);
254}
d02b48c6 255
3c1d6bbc 256static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
0f113f3e
MC
257 LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
258{
259 int i;
260 LHASH_NODE *a, *n;
261
262 if (lh == NULL)
263 return;
264
265 /*
266 * reverse the order so we search from 'top to bottom' We were having
267 * memory leaks otherwise
268 */
269 for (i = lh->num_nodes - 1; i >= 0; i--) {
270 a = lh->b[i];
271 while (a != NULL) {
272 /*
273 * 28/05/91 - eay - n added so items can be deleted via lh_doall
274 */
275 /*
276 * 22/05/08 - ben - eh? since a is not passed, this should not be
277 * needed
278 */
279 n = a->next;
280 if (use_arg)
281 func_arg(a->data, arg);
282 else
283 func(a->data);
284 a = n;
285 }
286 }
287}
d02b48c6 288
3c1d6bbc 289void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
0f113f3e
MC
290{
291 doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
292}
18602745 293
3c1d6bbc 294void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
0f113f3e
MC
295{
296 doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
297}
18602745 298
3c1d6bbc 299static void expand(_LHASH *lh)
0f113f3e
MC
300{
301 LHASH_NODE **n, **n1, **n2, *np;
302 unsigned int p, i, j;
303 unsigned long hash, nni;
304
305 lh->num_nodes++;
306 lh->num_expands++;
307 p = (int)lh->p++;
308 n1 = &(lh->b[p]);
309 n2 = &(lh->b[p + (int)lh->pmax]);
310 *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */
311 nni = lh->num_alloc_nodes;
312
313 for (np = *n1; np != NULL;) {
0f113f3e 314 hash = np->hash;
0f113f3e
MC
315 if ((hash % nni) != p) { /* move it */
316 *n1 = (*n1)->next;
317 np->next = *n2;
318 *n2 = np;
319 } else
320 n1 = &((*n1)->next);
321 np = *n1;
322 }
323
324 if ((lh->p) >= lh->pmax) {
325 j = (int)lh->num_alloc_nodes * 2;
b196e7d9 326 n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
0f113f3e 327 if (n == NULL) {
b196e7d9 328 /* fputs("realloc error in lhash",stderr); */
0f113f3e
MC
329 lh->error++;
330 lh->p = 0;
331 return;
332 }
0f113f3e
MC
333 for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
334 n[i] = NULL; /* 02/03/92 eay */
335 lh->pmax = lh->num_alloc_nodes;
336 lh->num_alloc_nodes = j;
337 lh->num_expand_reallocs++;
338 lh->p = 0;
339 lh->b = n;
340 }
341}
d02b48c6 342
3c1d6bbc 343static void contract(_LHASH *lh)
0f113f3e
MC
344{
345 LHASH_NODE **n, *n1, *np;
346
347 np = lh->b[lh->p + lh->pmax - 1];
348 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
349 if (lh->p == 0) {
b196e7d9
RS
350 n = OPENSSL_realloc(lh->b,
351 (unsigned int)(sizeof(LHASH_NODE *) * lh->pmax));
0f113f3e 352 if (n == NULL) {
b196e7d9 353 /* fputs("realloc error in lhash",stderr); */
0f113f3e
MC
354 lh->error++;
355 return;
356 }
357 lh->num_contract_reallocs++;
358 lh->num_alloc_nodes /= 2;
359 lh->pmax /= 2;
360 lh->p = lh->pmax - 1;
361 lh->b = n;
362 } else
363 lh->p--;
364
365 lh->num_nodes--;
366 lh->num_contracts++;
367
368 n1 = lh->b[(int)lh->p];
369 if (n1 == NULL)
370 lh->b[(int)lh->p] = np;
371 else {
372 while (n1->next != NULL)
373 n1 = n1->next;
374 n1->next = np;
375 }
376}
d02b48c6 377
3c1d6bbc 378static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
0f113f3e
MC
379{
380 LHASH_NODE **ret, *n1;
381 unsigned long hash, nn;
382 LHASH_COMP_FN_TYPE cf;
383
384 hash = (*(lh->hash)) (data);
385 lh->num_hash_calls++;
386 *rhash = hash;
387
388 nn = hash % lh->pmax;
389 if (nn < lh->p)
390 nn = hash % lh->num_alloc_nodes;
391
392 cf = lh->comp;
393 ret = &(lh->b[(int)nn]);
394 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
0f113f3e
MC
395 lh->num_hash_comps++;
396 if (n1->hash != hash) {
397 ret = &(n1->next);
398 continue;
399 }
0f113f3e
MC
400 lh->num_comp_calls++;
401 if (cf(n1->data, data) == 0)
402 break;
403 ret = &(n1->next);
404 }
405 return (ret);
406}
407
408/*
409 * The following hash seems to work very well on normal text strings no
410 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
411 * as good as MD5, but still good.
d02b48c6 412 */
6b691a5c 413unsigned long lh_strhash(const char *c)
0f113f3e
MC
414{
415 unsigned long ret = 0;
416 long n;
417 unsigned long v;
418 int r;
419
420 if ((c == NULL) || (*c == '\0'))
421 return (ret);
c80fd6b2 422/*-
35a1cc90
MC
423 unsigned char b[16];
424 MD5(c,strlen(c),b);
425 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
d02b48c6
RE
426*/
427
0f113f3e
MC
428 n = 0x100;
429 while (*c) {
430 v = n | (*c);
431 n += 0x100;
432 r = (int)((v >> 2) ^ v) & 0x0f;
433 ret = (ret << r) | (ret >> (32 - r));
434 ret &= 0xFFFFFFFFL;
435 ret ^= v * v;
436 c++;
437 }
438 return ((ret >> 16) ^ ret);
439}
d02b48c6 440
3c1d6bbc 441unsigned long lh_num_items(const _LHASH *lh)
0f113f3e
MC
442{
443 return lh ? lh->num_items : 0;
444}