Constification of LHASH. Contributed by "Paul D. Smith" <psmith@gnu.org>
[openssl.git] / crypto / lhash / lhash.c
index 29e091525c384e49ad4aa8897ba9f9abc65f3852..cdcc3b6e4d04aafd153db72bf63fd67312830d2d 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
@@ -97,8 +97,8 @@
 #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;
 
@@ -107,32 +107,18 @@ const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
 #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
-
-#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
+static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash);
 
 LHASH *lh_new(unsigned long (*h)(), int (*c)())
        {
        LHASH *ret;
        int i;
 
-       if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)
+       if ((ret=(LHASH *)OPENSSL_malloc(sizeof(LHASH))) == NULL)
                goto err0;
-       if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
+       if ((ret->b=(LHASH_NODE **)OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
                goto err1;
        for (i=0; i<MIN_NODES; i++)
                ret->b[i]=NULL;
@@ -163,7 +149,7 @@ LHASH *lh_new(unsigned long (*h)(), int (*c)())
        ret->error=0;
        return(ret);
 err1:
-       Free((char *)ret);
+       OPENSSL_free(ret);
 err0:
        return(NULL);
        }
@@ -173,7 +159,7 @@ 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++)
@@ -182,19 +168,19 @@ void lh_free(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(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))
@@ -204,7 +190,7 @@ char *lh_insert(LHASH *lh, 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);
@@ -228,11 +214,11 @@ char *lh_insert(LHASH *lh, char *data)
        return(ret);
        }
 
-char *lh_delete(LHASH *lh, char *data)
+void *lh_delete(LHASH *lh, void *data)
        {
        unsigned long hash;
        LHASH_NODE *nn,**rn;
-       char *ret;
+       void *ret;
 
        lh->error=0;
        rn=getrn(lh,data,&hash);
@@ -247,7 +233,7 @@ char *lh_delete(LHASH *lh, char *data)
                nn= *rn;
                *rn=nn->next;
                ret=nn->data;
-               Free((char *)nn);
+               OPENSSL_free(nn);
                lh->num_delete++;
                }
 
@@ -259,11 +245,11 @@ char *lh_delete(LHASH *lh, char *data)
        return(ret);
        }
 
-char *lh_retrieve(LHASH *lh, char *data)
+void *lh_retrieve(LHASH *lh, void *data)
        {
        unsigned long hash;
        LHASH_NODE **rn;
-       char *ret;
+       void *ret;
 
        lh->error=0;
        rn=getrn(lh,data,&hash);
@@ -286,7 +272,7 @@ void lh_doall(LHASH *lh, void (*func)())
        lh_doall_arg(lh,func,NULL);
        }
 
-void lh_doall_arg(LHASH *lh, void (*func)(), char *arg)
+void lh_doall_arg(LHASH *lh, void (*func)(), void *arg)
        {
        int i;
        LHASH_NODE *a,*n;
@@ -343,7 +329,7 @@ static void expand(LHASH *lh)
        if ((lh->p) >= lh->pmax)
                {
                j=(int)lh->num_alloc_nodes*2;
-               n=(LHASH_NODE **)Realloc((char *)lh->b,
+               n=(LHASH_NODE **)OPENSSL_realloc(lh->b,
                        (unsigned int)sizeof(LHASH_NODE *)*j);
                if (n == NULL)
                        {
@@ -371,7 +357,7 @@ static void contract(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)
                        {
@@ -402,7 +388,7 @@ static void contract(LHASH *lh)
                }
        }
 
-static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash)
+static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash)
        {
        LHASH_NODE **ret,*n1;
        unsigned long hash,nn;
@@ -436,22 +422,6 @@ static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash)
        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.
@@ -485,3 +455,7 @@ unsigned long lh_strhash(const char *c)
        return((ret>>16)^ret);
        }
 
+unsigned long lh_num_items(const LHASH *lh)
+       {
+       return lh ? lh->num_items : 0;
+       }