lhash.c revision 65c4736d9c0ebc6d9b1d991593b55566909da9cd
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
/* Code for dynamic hash table routines
* Author - Eric Young v 2.0
*
* 2.2 eay - added #include "crypto.h" so the memory leak checking code is
* present. eay 18-Jun-98
*
* 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
* 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
*
* 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
* were not being free()ed. 21/11/95
*
* 1.8 eay - Put the stats routines into a separate file, lh_stats.c
* 19/09/95
*
* 1.7 eay - Removed the fputs() for realloc failures - the code
* should silently tolerate them. I have also fixed things
* lint complained about 04/05/95
*
*
* 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
*
* 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
*
* 1.3 eay - Fixed a few lint problems 19/3/1991
*
* 1.2 eay - Fixed lh_doall problem 13/3/1991
*
* 1.1 eay - Added lh_doall
*
* 1.0 eay - First version
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MIN_NODES 16
#define P_CP char *
#define P_CPP char *,char *
{
int i;
goto err0;
goto err1;
for (i=0; i<MIN_NODES; i++)
ret->p=0;
ret->num_expands=0;
ret->num_contracts=0;
ret->num_hash_calls=0;
ret->num_comp_calls=0;
ret->num_insert=0;
ret->num_replace=0;
ret->num_delete=0;
ret->num_no_delete=0;
ret->num_retrieve=0;
ret->num_retrieve_miss=0;
ret->num_hash_comps=0;
return(ret);
err1:
err0:
return(NULL);
}
{
unsigned int i;
LHASH_NODE *n,*nn;
return;
{
n=lh->b[i];
while (n != NULL)
{
Free(n);
n=nn;
}
}
}
{
unsigned long hash;
char *ret;
{
{
return(NULL);
}
#ifndef NO_HASH_COMP
#endif
lh->num_insert++;
}
else /* replace same key */
{
lh->num_replace++;
}
return(ret);
}
{
unsigned long hash;
char *ret;
{
lh->num_no_delete++;
return(NULL);
}
else
{
lh->num_delete++;
}
return(ret);
}
{
unsigned long hash;
LHASH_NODE **rn;
char *ret;
{
lh->num_retrieve_miss++;
return(NULL);
}
else
{
lh->num_retrieve++;
}
return(ret);
}
{
}
{
int i;
LHASH_NODE *a,*n;
/* reverse the order so we search from 'top to bottom'
* We were having memory leaks otherwise */
{
a=lh->b[i];
while (a != NULL)
{
/* 28/05/91 - eay - n added so items can be deleted
* via lh_doall */
n=a->next;
a=n;
}
}
}
{
unsigned int p,i,j;
lh->num_expands++;
p=(int)lh->p++;
{
#ifndef NO_HASH_COMP
#else
lh->num_hash_calls++;
#endif
{ /* move it */
}
else
}
{
(unsigned int)sizeof(LHASH_NODE *)*j);
if (n == NULL)
{
/* fputs("realloc error in lhash",stderr); */
lh->p=0;
return;
}
/* else */
n[i]=NULL; /* 02/03/92 eay */
lh->num_alloc_nodes=j;
lh->p=0;
lh->b=n;
}
}
{
if (lh->p == 0)
{
if (n == NULL)
{
/* fputs("realloc error in lhash",stderr); */
return;
}
lh->b=n;
}
else
lh->p--;
lh->num_contracts++;
else
{
}
}
{
int (*cf)();
lh->num_hash_calls++;
{
#ifndef NO_HASH_COMP
lh->num_hash_comps++;
{
continue;
}
#endif
lh->num_comp_calls++;
break;
}
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
* well, not as good as MD5, but still good.
*/
unsigned long lh_strhash(const char *c)
{
unsigned long ret=0;
long n;
unsigned long v;
int r;
if ((c == NULL) || (*c == '\0'))
return(ret);
/*
unsigned char b[16];
MD5(c,strlen(c),b);
return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
*/
n=0x100;
while (*c)
{
v=n|(*c);
n+=0x100;
r= (int)((v>>2)^v)&0x0f;
ret&=0xFFFFFFFFL;
ret^=v*v;
c++;
}
}