sl@0: /* crypto/lhash/lhash.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* Code for dynamic hash table routines sl@0: * Author - Eric Young v 2.0 sl@0: * sl@0: * 2.2 eay - added #include "crypto.h" so the memory leak checking code is sl@0: * present. eay 18-Jun-98 sl@0: * sl@0: * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98 sl@0: * sl@0: * 2.0 eay - Fixed a bug that occurred when using lh_delete sl@0: * from inside lh_doall(). As entries were deleted, sl@0: * the 'table' was 'contract()ed', making some entries sl@0: * jump from the end of the table to the start, there by sl@0: * skipping the lh_doall() processing. eay - 4/12/95 sl@0: * sl@0: * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs sl@0: * were not being free()ed. 21/11/95 sl@0: * sl@0: * 1.8 eay - Put the stats routines into a separate file, lh_stats.c sl@0: * 19/09/95 sl@0: * sl@0: * 1.7 eay - Removed the fputs() for realloc failures - the code sl@0: * should silently tolerate them. I have also fixed things sl@0: * lint complained about 04/05/95 sl@0: * sl@0: * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92 sl@0: * sl@0: * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992 sl@0: * sl@0: * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91 sl@0: * sl@0: * 1.3 eay - Fixed a few lint problems 19/3/1991 sl@0: * sl@0: * 1.2 eay - Fixed lh_doall problem 13/3/1991 sl@0: * sl@0: * 1.1 eay - Added lh_doall sl@0: * sl@0: * 1.0 eay - First version sl@0: */ sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: const char lh_version[]="lhash" OPENSSL_VERSION_PTEXT; sl@0: sl@0: #undef MIN_NODES sl@0: #define MIN_NODES 16 sl@0: #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ sl@0: #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ sl@0: sl@0: static void expand(LHASH *lh); sl@0: static void contract(LHASH *lh); sl@0: static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash); sl@0: sl@0: EXPORT_C LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c) sl@0: { sl@0: LHASH *ret; sl@0: int i; sl@0: sl@0: if ((ret=(LHASH *)OPENSSL_malloc(sizeof(LHASH))) == NULL) sl@0: goto err0; sl@0: if ((ret->b=(LHASH_NODE **)OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL) sl@0: goto err1; sl@0: for (i=0; ib[i]=NULL; sl@0: ret->comp=((c == NULL)?(LHASH_COMP_FN_TYPE)strcmp:c); sl@0: ret->hash=((h == NULL)?(LHASH_HASH_FN_TYPE)lh_strhash:h); sl@0: ret->num_nodes=MIN_NODES/2; sl@0: ret->num_alloc_nodes=MIN_NODES; sl@0: ret->p=0; sl@0: ret->pmax=MIN_NODES/2; sl@0: ret->up_load=UP_LOAD; sl@0: ret->down_load=DOWN_LOAD; sl@0: ret->num_items=0; sl@0: sl@0: ret->num_expands=0; sl@0: ret->num_expand_reallocs=0; sl@0: ret->num_contracts=0; sl@0: ret->num_contract_reallocs=0; sl@0: ret->num_hash_calls=0; sl@0: ret->num_comp_calls=0; sl@0: ret->num_insert=0; sl@0: ret->num_replace=0; sl@0: ret->num_delete=0; sl@0: ret->num_no_delete=0; sl@0: ret->num_retrieve=0; sl@0: ret->num_retrieve_miss=0; sl@0: ret->num_hash_comps=0; sl@0: sl@0: ret->error=0; sl@0: return(ret); sl@0: err1: sl@0: OPENSSL_free(ret); sl@0: err0: sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C void lh_free(LHASH *lh) sl@0: { sl@0: unsigned int i; sl@0: LHASH_NODE *n,*nn; sl@0: sl@0: if (lh == NULL) sl@0: return; sl@0: sl@0: for (i=0; inum_nodes; i++) sl@0: { sl@0: n=lh->b[i]; sl@0: while (n != NULL) sl@0: { sl@0: nn=n->next; sl@0: OPENSSL_free(n); sl@0: n=nn; sl@0: } sl@0: } sl@0: OPENSSL_free(lh->b); sl@0: OPENSSL_free(lh); sl@0: } sl@0: sl@0: EXPORT_C void *lh_insert(LHASH *lh, void *data) sl@0: { sl@0: unsigned long hash; sl@0: LHASH_NODE *nn,**rn; sl@0: void *ret; sl@0: sl@0: lh->error=0; sl@0: if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)) sl@0: expand(lh); sl@0: sl@0: rn=getrn(lh,data,&hash); sl@0: sl@0: if (*rn == NULL) sl@0: { sl@0: if ((nn=(LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) sl@0: { sl@0: lh->error++; sl@0: return(NULL); sl@0: } sl@0: nn->data=data; sl@0: nn->next=NULL; sl@0: #ifndef OPENSSL_NO_HASH_COMP sl@0: nn->hash=hash; sl@0: #endif sl@0: *rn=nn; sl@0: ret=NULL; sl@0: lh->num_insert++; sl@0: lh->num_items++; sl@0: } sl@0: else /* replace same key */ sl@0: { sl@0: ret= (*rn)->data; sl@0: (*rn)->data=data; sl@0: lh->num_replace++; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void *lh_delete(LHASH *lh, const void *data) sl@0: { sl@0: unsigned long hash; sl@0: LHASH_NODE *nn,**rn; sl@0: void *ret; sl@0: sl@0: lh->error=0; sl@0: rn=getrn(lh,data,&hash); sl@0: sl@0: if (*rn == NULL) sl@0: { sl@0: lh->num_no_delete++; sl@0: return(NULL); sl@0: } sl@0: else sl@0: { sl@0: nn= *rn; sl@0: *rn=nn->next; sl@0: ret=nn->data; sl@0: OPENSSL_free(nn); sl@0: lh->num_delete++; sl@0: } sl@0: sl@0: lh->num_items--; sl@0: if ((lh->num_nodes > MIN_NODES) && sl@0: (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) sl@0: contract(lh); sl@0: sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void *lh_retrieve(LHASH *lh, const void *data) sl@0: { sl@0: unsigned long hash; sl@0: LHASH_NODE **rn; sl@0: void *ret; sl@0: sl@0: lh->error=0; sl@0: rn=getrn(lh,data,&hash); sl@0: sl@0: if (*rn == NULL) sl@0: { sl@0: lh->num_retrieve_miss++; sl@0: return(NULL); sl@0: } sl@0: else sl@0: { sl@0: ret= (*rn)->data; sl@0: lh->num_retrieve++; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, sl@0: LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) sl@0: { sl@0: int i; sl@0: LHASH_NODE *a,*n; sl@0: sl@0: /* reverse the order so we search from 'top to bottom' sl@0: * We were having memory leaks otherwise */ sl@0: for (i=lh->num_nodes-1; i>=0; i--) sl@0: { sl@0: a=lh->b[i]; sl@0: while (a != NULL) sl@0: { sl@0: /* 28/05/91 - eay - n added so items can be deleted sl@0: * via lh_doall */ sl@0: n=a->next; sl@0: if(use_arg) sl@0: func_arg(a->data,arg); sl@0: else sl@0: func(a->data); sl@0: a=n; sl@0: } sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void lh_doall(LHASH *lh, LHASH_DOALL_FN_TYPE func) sl@0: { sl@0: doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL); sl@0: } sl@0: sl@0: EXPORT_C void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg) sl@0: { sl@0: doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg); sl@0: } sl@0: sl@0: static void expand(LHASH *lh) sl@0: { sl@0: LHASH_NODE **n,**n1,**n2,*np; sl@0: unsigned int p,i,j; sl@0: unsigned long hash,nni; sl@0: sl@0: lh->num_nodes++; sl@0: lh->num_expands++; sl@0: p=(int)lh->p++; sl@0: n1= &(lh->b[p]); sl@0: n2= &(lh->b[p+(int)lh->pmax]); sl@0: *n2=NULL; /* 27/07/92 - eay - undefined pointer bug */ sl@0: nni=lh->num_alloc_nodes; sl@0: sl@0: for (np= *n1; np != NULL; ) sl@0: { sl@0: #ifndef OPENSSL_NO_HASH_COMP sl@0: hash=np->hash; sl@0: #else sl@0: hash=lh->hash(np->data); sl@0: lh->num_hash_calls++; sl@0: #endif sl@0: if ((hash%nni) != p) sl@0: { /* move it */ sl@0: *n1= (*n1)->next; sl@0: np->next= *n2; sl@0: *n2=np; sl@0: } sl@0: else sl@0: n1= &((*n1)->next); sl@0: np= *n1; sl@0: } sl@0: sl@0: if ((lh->p) >= lh->pmax) sl@0: { sl@0: j=(int)lh->num_alloc_nodes*2; sl@0: n=(LHASH_NODE **)OPENSSL_realloc(lh->b, sl@0: (int)(sizeof(LHASH_NODE *)*j)); sl@0: if (n == NULL) sl@0: { sl@0: /* fputs("realloc error in lhash",stderr); */ sl@0: lh->error++; sl@0: lh->p=0; sl@0: return; sl@0: } sl@0: /* else */ sl@0: for (i=(int)lh->num_alloc_nodes; ipmax=lh->num_alloc_nodes; sl@0: lh->num_alloc_nodes=j; sl@0: lh->num_expand_reallocs++; sl@0: lh->p=0; sl@0: lh->b=n; sl@0: } sl@0: } sl@0: sl@0: static void contract(LHASH *lh) sl@0: { sl@0: LHASH_NODE **n,*n1,*np; sl@0: sl@0: np=lh->b[lh->p+lh->pmax-1]; sl@0: lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */ sl@0: if (lh->p == 0) sl@0: { sl@0: n=(LHASH_NODE **)OPENSSL_realloc(lh->b, sl@0: (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); sl@0: if (n == NULL) sl@0: { sl@0: /* fputs("realloc error in lhash",stderr); */ sl@0: lh->error++; sl@0: return; sl@0: } sl@0: lh->num_contract_reallocs++; sl@0: lh->num_alloc_nodes/=2; sl@0: lh->pmax/=2; sl@0: lh->p=lh->pmax-1; sl@0: lh->b=n; sl@0: } sl@0: else sl@0: lh->p--; sl@0: sl@0: lh->num_nodes--; sl@0: lh->num_contracts++; sl@0: sl@0: n1=lh->b[(int)lh->p]; sl@0: if (n1 == NULL) sl@0: lh->b[(int)lh->p]=np; sl@0: else sl@0: { sl@0: while (n1->next != NULL) sl@0: n1=n1->next; sl@0: n1->next=np; sl@0: } sl@0: } sl@0: sl@0: static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash) sl@0: { sl@0: LHASH_NODE **ret,*n1; sl@0: unsigned long hash,nn; sl@0: LHASH_COMP_FN_TYPE cf; sl@0: sl@0: hash=(*(lh->hash))(data); sl@0: lh->num_hash_calls++; sl@0: *rhash=hash; sl@0: sl@0: nn=hash%lh->pmax; sl@0: if (nn < lh->p) sl@0: nn=hash%lh->num_alloc_nodes; sl@0: sl@0: cf=lh->comp; sl@0: ret= &(lh->b[(int)nn]); sl@0: for (n1= *ret; n1 != NULL; n1=n1->next) sl@0: { sl@0: #ifndef OPENSSL_NO_HASH_COMP sl@0: lh->num_hash_comps++; sl@0: if (n1->hash != hash) sl@0: { sl@0: ret= &(n1->next); sl@0: continue; sl@0: } sl@0: #endif sl@0: lh->num_comp_calls++; sl@0: if(cf(n1->data,data) == 0) sl@0: break; sl@0: ret= &(n1->next); sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: /* The following hash seems to work very well on normal text strings sl@0: * no collisions on /usr/dict/words and it distributes on %2^n quite sl@0: * well, not as good as MD5, but still good. sl@0: */ sl@0: EXPORT_C unsigned long lh_strhash(const char *c) sl@0: { sl@0: unsigned long ret=0; sl@0: long n; sl@0: unsigned long v; sl@0: int r; sl@0: sl@0: if ((c == NULL) || (*c == '\0')) sl@0: return(ret); sl@0: /* sl@0: unsigned char b[16]; sl@0: MD5(c,strlen(c),b); sl@0: return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24)); sl@0: */ sl@0: sl@0: n=0x100; sl@0: while (*c) sl@0: { sl@0: v=n|(*c); sl@0: n+=0x100; sl@0: r= (int)((v>>2)^v)&0x0f; sl@0: ret=(ret<>(32-r)); sl@0: ret&=0xFFFFFFFFL; sl@0: ret^=v*v; sl@0: c++; sl@0: } sl@0: return((ret>>16)^ret); sl@0: } sl@0: sl@0: EXPORT_C unsigned long lh_num_items(const LHASH *lh) sl@0: { sl@0: return lh ? lh->num_items : 0; sl@0: }