PR: 2124
[openssl.git] / crypto / lhash / lhash.c
index cd56515df79f00bb63daf640a13e8e254bb846c8..528f9c7ceff877d84e53829b606ba97f1de7d24e 100644 (file)
  *
  * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
  *
- * 2.0 eay - Fixed a bug that occured when using lh_delete
+ * 2.0 eay - Fixed a bug that occurred when using lh_delete
  *          from inside lh_doall().  As entries were deleted,
  *          the 'table' was 'contract()ed', making some entries
  *          jump from the end of the table to the start, there by
- *          skiping the lh_doall() processing. eay - 4/12/95
+ *          skipping the lh_doall() processing. eay - 4/12/95
  *
  * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
  *          were not being free()ed. 21/11/95
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include "crypto.h"
-#include "lhash.h"
+#include <openssl/crypto.h>
+#include <openssl/lhash.h>
 
-const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
+const char lh_version[]="lhash" OPENSSL_VERSION_PTEXT;
 
 #undef MIN_NODES 
 #define MIN_NODES      16
 #define UP_LOAD                (2*LH_LOAD_MULT) /* load times 256  (default 2) */
 #define DOWN_LOAD      (LH_LOAD_MULT)   /* load times 256  (default 1) */
 
-#ifndef NOPROTO
+static void expand(_LHASH *lh);
+static void contract(_LHASH *lh);
+static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
 
-#define P_CP   char *
-#define P_CPP  char *,char *
-static void expand(LHASH *lh);
-static void contract(LHASH *lh);
-static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash);
-
-#else
-
-#define        P_CP
-#define P_CPP
-static void expand();
-static void contract();
-static LHASH_NODE **getrn();
-
-#endif
-
-LHASH *lh_new(h, c)
-unsigned long (*h)();
-int (*c)();
+_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
        {
-       LHASH *ret;
+       _LHASH *ret;
        int i;
 
-       if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)
+       if ((ret=OPENSSL_malloc(sizeof(_LHASH))) == NULL)
                goto err0;
-       if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
+       if ((ret->b=OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
                goto err1;
        for (i=0; i<MIN_NODES; i++)
                ret->b[i]=NULL;
-       ret->comp=((c == NULL)?(int (*)())strcmp:c);
-       ret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);
+       ret->comp=((c == NULL)?(LHASH_COMP_FN_TYPE)strcmp:c);
+       ret->hash=((h == NULL)?(LHASH_HASH_FN_TYPE)lh_strhash:h);
        ret->num_nodes=MIN_NODES/2;
        ret->num_alloc_nodes=MIN_NODES;
        ret->p=0;
@@ -165,18 +149,17 @@ int (*c)();
        ret->error=0;
        return(ret);
 err1:
-       Free((char *)ret);
+       OPENSSL_free(ret);
 err0:
        return(NULL);
        }
 
-void lh_free(lh)
-LHASH *lh;
+void lh_free(_LHASH *lh)
        {
        unsigned int i;
        LHASH_NODE *n,*nn;
 
-       if(lh == NULL)
+       if (lh == NULL)
            return;
 
        for (i=0; i<lh->num_nodes; i++)
@@ -185,21 +168,19 @@ LHASH *lh;
                while (n != NULL)
                        {
                        nn=n->next;
-                       Free(n);
+                       OPENSSL_free(n);
                        n=nn;
                        }
                }
-       Free((char *)lh->b);
-       Free((char *)lh);
+       OPENSSL_free(lh->b);
+       OPENSSL_free(lh);
        }
 
-char *lh_insert(lh, data)
-LHASH *lh;
-char *data;
+void *lh_insert(_LHASH *lh, void *data)
        {
        unsigned long hash;
        LHASH_NODE *nn,**rn;
-       char *ret;
+       void *ret;
 
        lh->error=0;
        if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))
@@ -209,14 +190,14 @@ char *data;
 
        if (*rn == NULL)
                {
-               if ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL)
+               if ((nn=(LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL)
                        {
                        lh->error++;
                        return(NULL);
                        }
                nn->data=data;
                nn->next=NULL;
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                nn->hash=hash;
 #endif
                *rn=nn;
@@ -233,13 +214,11 @@ char *data;
        return(ret);
        }
 
-char *lh_delete(lh, data)
-LHASH *lh;
-char *data;
+void *lh_delete(_LHASH *lh, const void *data)
        {
        unsigned long hash;
        LHASH_NODE *nn,**rn;
-       char *ret;
+       void *ret;
 
        lh->error=0;
        rn=getrn(lh,data,&hash);
@@ -254,7 +233,7 @@ char *data;
                nn= *rn;
                *rn=nn->next;
                ret=nn->data;
-               Free((char *)nn);
+               OPENSSL_free(nn);
                lh->num_delete++;
                }
 
@@ -266,13 +245,11 @@ char *data;
        return(ret);
        }
 
-char *lh_retrieve(lh, data)
-LHASH *lh;
-char *data;
+void *lh_retrieve(_LHASH *lh, const void *data)
        {
        unsigned long hash;
        LHASH_NODE **rn;
-       char *ret;
+       void *ret;
 
        lh->error=0;
        rn=getrn(lh,data,&hash);
@@ -290,21 +267,15 @@ char *data;
        return(ret);
        }
 
-void lh_doall(lh, func)
-LHASH *lh;
-void (*func)();
-       {
-       lh_doall_arg(lh,func,NULL);
-       }
-
-void lh_doall_arg(lh, func, arg)
-LHASH *lh;
-void (*func)();
-char *arg;
+static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
+                         LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
        {
        int i;
        LHASH_NODE *a,*n;
 
+       if (lh == NULL)
+               return;
+
        /* reverse the order so we search from 'top to bottom'
         * We were having memory leaks otherwise */
        for (i=lh->num_nodes-1; i>=0; i--)
@@ -314,34 +285,72 @@ char *arg;
                        {
                        /* 28/05/91 - eay - n added so items can be deleted
                         * via lh_doall */
+                       /* 22/05/08 - ben - eh? since a is not passed,
+                        * this should not be needed */
                        n=a->next;
-                       func(a->data,arg);
+                       if(use_arg)
+                               func_arg(a->data,arg);
+                       else
+                               func(a->data);
                        a=n;
                        }
                }
        }
 
-static void expand(lh)
-LHASH *lh;
+void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
+       {
+       doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
+       }
+
+void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
+       {
+       doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
+       }
+
+static void expand(_LHASH *lh)
        {
        LHASH_NODE **n,**n1,**n2,*np;
-       unsigned int p,i,j;
+       unsigned int p,i,j,pmax;
        unsigned long hash,nni;
 
+       p=(int)lh->p++;
+       nni=lh->num_alloc_nodes;
+       pmax=lh->pmax;
+
+       if ((lh->p) >= lh->pmax)
+               {
+               j=(int)lh->num_alloc_nodes*2;
+               n=(LHASH_NODE **)OPENSSL_realloc(lh->b,
+                       (int)sizeof(LHASH_NODE *)*j);
+               if (n == NULL)
+                       {
+/*                     fputs("realloc error in lhash",stderr); */
+                       lh->error++;
+                       lh->p=0;
+                       return;
+                       }
+               /* else */
+               for (i=(int)lh->num_alloc_nodes; i<j; i++)/* 26/02/92 eay */
+                       n[i]=NULL;                        /* 02/03/92 eay */
+               lh->pmax=lh->num_alloc_nodes;
+               lh->num_alloc_nodes=j;
+               lh->num_expand_reallocs++;
+               lh->p=0;
+               lh->b=n;
+               }
+
        lh->num_nodes++;
        lh->num_expands++;
-       p=(int)lh->p++;
        n1= &(lh->b[p]);
-       n2= &(lh->b[p+(int)lh->pmax]);
+       n2= &(lh->b[p+pmax]);
        *n2=NULL;        /* 27/07/92 - eay - undefined pointer bug */
-       nni=lh->num_alloc_nodes;
        
        for (np= *n1; np != NULL; )
                {
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                hash=np->hash;
 #else
-               hash=(*(lh->hash))(np->data);
+               hash=lh->hash(np->data);
                lh->num_hash_calls++;
 #endif
                if ((hash%nni) != p)
@@ -358,8 +367,8 @@ LHASH *lh;
        if ((lh->p) >= lh->pmax)
                {
                j=(int)lh->num_alloc_nodes*2;
-               n=(LHASH_NODE **)Realloc((char *)lh->b,
-                       (unsigned int)sizeof(LHASH_NODE *)*j);
+               n=(LHASH_NODE **)OPENSSL_realloc(lh->b,
+                       (int)(sizeof(LHASH_NODE *)*j));
                if (n == NULL)
                        {
 /*                     fputs("realloc error in lhash",stderr); */
@@ -378,8 +387,7 @@ LHASH *lh;
                }
        }
 
-static void contract(lh)
-LHASH *lh;
+static void contract(_LHASH *lh)
        {
        LHASH_NODE **n,*n1,*np;
 
@@ -387,7 +395,7 @@ LHASH *lh;
        lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */
        if (lh->p == 0)
                {
-               n=(LHASH_NODE **)Realloc((char *)lh->b,
+               n=(LHASH_NODE **)OPENSSL_realloc(lh->b,
                        (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
                if (n == NULL)
                        {
@@ -404,6 +412,7 @@ LHASH *lh;
        else
                lh->p--;
 
+       lh->b[idx] = NULL;
        lh->num_nodes--;
        lh->num_contracts++;
 
@@ -418,14 +427,11 @@ LHASH *lh;
                }
        }
 
-static LHASH_NODE **getrn(lh, data, rhash)
-LHASH *lh;
-char *data;
-unsigned long *rhash;
+static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
        {
        LHASH_NODE **ret,*n1;
        unsigned long hash,nn;
-       int (*cf)();
+       LHASH_COMP_FN_TYPE cf;
 
        hash=(*(lh->hash))(data);
        lh->num_hash_calls++;
@@ -439,7 +445,7 @@ unsigned long *rhash;
        ret= &(lh->b[(int)nn]);
        for (n1= *ret; n1 != NULL; n1=n1->next)
                {
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                lh->num_hash_comps++;
                if (n1->hash != hash)
                        {
@@ -448,35 +454,18 @@ unsigned long *rhash;
                        }
 #endif
                lh->num_comp_calls++;
-               if ((*cf)(n1->data,data) == 0)
+               if(cf(n1->data,data) == 0)
                        break;
                ret= &(n1->next);
                }
        return(ret);
        }
 
-/*
-static unsigned long lh_strhash(str)
-char *str;
-       {
-       int i,l;
-       unsigned long ret=0;
-       unsigned short *s;
-
-       if (str == NULL) return(0);
-       l=(strlen(str)+1)/2;
-       s=(unsigned short *)str;
-       for (i=0; i<l; i++)
-               ret^=(s[i]<<(i&0x0f));
-       return(ret);
-       } */
-
 /* The following hash seems to work very well on normal text strings
  * no collisions on /usr/dict/words and it distributes on %2^n quite
  * well, not as good as MD5, but still good.
  */
-unsigned long lh_strhash(c)
-const char *c;
+unsigned long lh_strhash(const char *c)
        {
        unsigned long ret=0;
        long n;
@@ -505,3 +494,7 @@ const char *c;
        return((ret>>16)^ret);
        }
 
+unsigned long lh_num_items(const _LHASH *lh)
+       {
+       return lh ? lh->num_items : 0;
+       }