1 /* crypto/lhash/lhash.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 /* Code for dynamic hash table routines
60 * Author - Eric Young v 2.0
62 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
63 * present. eay 18-Jun-98
65 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
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
73 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
74 * were not being free()ed. 21/11/95
76 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
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
83 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
85 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
87 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
89 * 1.3 eay - Fixed a few lint problems 19/3/1991
91 * 1.2 eay - Fixed lh_doall problem 13/3/1991
93 * 1.1 eay - Added lh_doall
95 * 1.0 eay - First version
100 #include <openssl/crypto.h>
101 #include <openssl/lhash.h>
103 const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
107 #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
108 #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
110 static void expand(LHASH *lh);
111 static void contract(LHASH *lh);
112 static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash);
114 LHASH *lh_new(unsigned long (*h)(), int (*c)())
119 if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)
121 if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
123 for (i=0; i<MIN_NODES; i++)
125 ret->comp=((c == NULL)?(int (*)())strcmp:c);
126 ret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);
127 ret->num_nodes=MIN_NODES/2;
128 ret->num_alloc_nodes=MIN_NODES;
130 ret->pmax=MIN_NODES/2;
131 ret->up_load=UP_LOAD;
132 ret->down_load=DOWN_LOAD;
136 ret->num_expand_reallocs=0;
137 ret->num_contracts=0;
138 ret->num_contract_reallocs=0;
139 ret->num_hash_calls=0;
140 ret->num_comp_calls=0;
144 ret->num_no_delete=0;
146 ret->num_retrieve_miss=0;
147 ret->num_hash_comps=0;
157 void lh_free(LHASH *lh)
165 for (i=0; i<lh->num_nodes; i++)
179 void *lh_insert(LHASH *lh, void *data)
186 if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))
189 rn=getrn(lh,data,&hash);
193 if ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL)
208 else /* replace same key */
217 void *lh_delete(LHASH *lh, void *data)
224 rn=getrn(lh,data,&hash);
241 if ((lh->num_nodes > MIN_NODES) &&
242 (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
248 void *lh_retrieve(LHASH *lh, void *data)
255 rn=getrn(lh,data,&hash);
259 lh->num_retrieve_miss++;
270 void lh_doall(LHASH *lh, void (*func)())
272 lh_doall_arg(lh,func,NULL);
275 void lh_doall_arg(LHASH *lh, void (*func)(), void *arg)
280 /* reverse the order so we search from 'top to bottom'
281 * We were having memory leaks otherwise */
282 for (i=lh->num_nodes-1; i>=0; i--)
287 /* 28/05/91 - eay - n added so items can be deleted
296 static void expand(LHASH *lh)
298 LHASH_NODE **n,**n1,**n2,*np;
300 unsigned long hash,nni;
306 n2= &(lh->b[p+(int)lh->pmax]);
307 *n2=NULL; /* 27/07/92 - eay - undefined pointer bug */
308 nni=lh->num_alloc_nodes;
310 for (np= *n1; np != NULL; )
315 hash=(*(lh->hash))(np->data);
316 lh->num_hash_calls++;
329 if ((lh->p) >= lh->pmax)
331 j=(int)lh->num_alloc_nodes*2;
332 n=(LHASH_NODE **)Realloc(lh->b,
333 (unsigned int)sizeof(LHASH_NODE *)*j);
336 /* fputs("realloc error in lhash",stderr); */
342 for (i=(int)lh->num_alloc_nodes; i<j; i++)/* 26/02/92 eay */
343 n[i]=NULL; /* 02/03/92 eay */
344 lh->pmax=lh->num_alloc_nodes;
345 lh->num_alloc_nodes=j;
346 lh->num_expand_reallocs++;
352 static void contract(LHASH *lh)
354 LHASH_NODE **n,*n1,*np;
356 np=lh->b[lh->p+lh->pmax-1];
357 lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */
360 n=(LHASH_NODE **)Realloc(lh->b,
361 (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
364 /* fputs("realloc error in lhash",stderr); */
368 lh->num_contract_reallocs++;
369 lh->num_alloc_nodes/=2;
380 n1=lh->b[(int)lh->p];
382 lh->b[(int)lh->p]=np;
385 while (n1->next != NULL)
391 static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash)
393 LHASH_NODE **ret,*n1;
394 unsigned long hash,nn;
397 hash=(*(lh->hash))(data);
398 lh->num_hash_calls++;
403 nn=hash%lh->num_alloc_nodes;
406 ret= &(lh->b[(int)nn]);
407 for (n1= *ret; n1 != NULL; n1=n1->next)
410 lh->num_hash_comps++;
411 if (n1->hash != hash)
417 lh->num_comp_calls++;
418 if ((*cf)(n1->data,data) == 0)
426 unsigned long lh_strhash(char *str)
432 if (str == NULL) return(0);
434 s=(unsigned short *)str;
436 ret^=(s[i]<<(i&0x0f));
440 /* The following hash seems to work very well on normal text strings
441 * no collisions on /usr/dict/words and it distributes on %2^n quite
442 * well, not as good as MD5, but still good.
444 unsigned long lh_strhash(const char *c)
451 if ((c == NULL) || (*c == '\0'))
456 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
464 r= (int)((v>>2)^v)&0x0f;
465 ret=(ret<<r)|(ret>>(32-r));
470 return((ret>>16)^ret);