summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/lhash/lhash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/lhash/lhash.c')
-rw-r--r--src/lib/libcrypto/lhash/lhash.c97
1 files changed, 42 insertions, 55 deletions
diff --git a/src/lib/libcrypto/lhash/lhash.c b/src/lib/libcrypto/lhash/lhash.c
index 6dfb5c9ccc..801322beb6 100644
--- a/src/lib/libcrypto/lhash/lhash.c
+++ b/src/lib/libcrypto/lhash/lhash.c
@@ -56,11 +56,14 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59char *lh_version="lhash part of SSLeay 0.9.0b 29-Jun-1998";
60
61/* Code for dynamic hash table routines 59/* Code for dynamic hash table routines
62 * Author - Eric Young v 2.0 60 * Author - Eric Young v 2.0
63 * 61 *
62 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
63 * present. eay 18-Jun-98
64 *
65 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
66 *
64 * 2.0 eay - Fixed a bug that occured when using lh_delete 67 * 2.0 eay - Fixed a bug that occured when using lh_delete
65 * from inside lh_doall(). As entries were deleted, 68 * from inside lh_doall(). As entries were deleted,
66 * the 'table' was 'contract()ed', making some entries 69 * the 'table' was 'contract()ed', making some entries
@@ -94,14 +97,16 @@ char *lh_version="lhash part of SSLeay 0.9.0b 29-Jun-1998";
94#include <stdio.h> 97#include <stdio.h>
95#include <string.h> 98#include <string.h>
96#include <stdlib.h> 99#include <stdlib.h>
97#include "lhash.h" 100#include <openssl/crypto.h>
101#include <openssl/lhash.h>
102
103const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
98 104
99#undef MIN_NODES 105#undef MIN_NODES
100#define MIN_NODES 16 106#define MIN_NODES 16
101#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ 107#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
102#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ 108#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
103 109
104#ifndef NOPROTO
105 110
106#define P_CP char * 111#define P_CP char *
107#define P_CPP char *,char * 112#define P_CPP char *,char *
@@ -109,26 +114,14 @@ static void expand(LHASH *lh);
109static void contract(LHASH *lh); 114static void contract(LHASH *lh);
110static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash); 115static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash);
111 116
112#else 117LHASH *lh_new(unsigned long (*h)(), int (*c)())
113
114#define P_CP
115#define P_CPP
116static void expand();
117static void contract();
118static LHASH_NODE **getrn();
119
120#endif
121
122LHASH *lh_new(h, c)
123unsigned long (*h)();
124int (*c)();
125 { 118 {
126 LHASH *ret; 119 LHASH *ret;
127 int i; 120 int i;
128 121
129 if ((ret=(LHASH *)malloc(sizeof(LHASH))) == NULL) 122 if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)
130 goto err0; 123 goto err0;
131 if ((ret->b=(LHASH_NODE **)malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL) 124 if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
132 goto err1; 125 goto err1;
133 for (i=0; i<MIN_NODES; i++) 126 for (i=0; i<MIN_NODES; i++)
134 ret->b[i]=NULL; 127 ret->b[i]=NULL;
@@ -156,41 +149,43 @@ int (*c)();
156 ret->num_retrieve_miss=0; 149 ret->num_retrieve_miss=0;
157 ret->num_hash_comps=0; 150 ret->num_hash_comps=0;
158 151
152 ret->error=0;
159 return(ret); 153 return(ret);
160err1: 154err1:
161 free((char *)ret); 155 Free((char *)ret);
162err0: 156err0:
163 return(NULL); 157 return(NULL);
164 } 158 }
165 159
166void lh_free(lh) 160void lh_free(LHASH *lh)
167LHASH *lh;
168 { 161 {
169 unsigned int i; 162 unsigned int i;
170 LHASH_NODE *n,*nn; 163 LHASH_NODE *n,*nn;
171 164
165 if(lh == NULL)
166 return;
167
172 for (i=0; i<lh->num_nodes; i++) 168 for (i=0; i<lh->num_nodes; i++)
173 { 169 {
174 n=lh->b[i]; 170 n=lh->b[i];
175 while (n != NULL) 171 while (n != NULL)
176 { 172 {
177 nn=n->next; 173 nn=n->next;
178 free(n); 174 Free(n);
179 n=nn; 175 n=nn;
180 } 176 }
181 } 177 }
182 free((char *)lh->b); 178 Free((char *)lh->b);
183 free((char *)lh); 179 Free((char *)lh);
184 } 180 }
185 181
186char *lh_insert(lh, data) 182char *lh_insert(LHASH *lh, char *data)
187LHASH *lh;
188char *data;
189 { 183 {
190 unsigned long hash; 184 unsigned long hash;
191 LHASH_NODE *nn,**rn; 185 LHASH_NODE *nn,**rn;
192 char *ret; 186 char *ret;
193 187
188 lh->error=0;
194 if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)) 189 if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))
195 expand(lh); 190 expand(lh);
196 191
@@ -198,8 +193,11 @@ char *data;
198 193
199 if (*rn == NULL) 194 if (*rn == NULL)
200 { 195 {
201 if ((nn=(LHASH_NODE *)malloc(sizeof(LHASH_NODE))) == NULL) 196 if ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL)
197 {
198 lh->error++;
202 return(NULL); 199 return(NULL);
200 }
203 nn->data=data; 201 nn->data=data;
204 nn->next=NULL; 202 nn->next=NULL;
205#ifndef NO_HASH_COMP 203#ifndef NO_HASH_COMP
@@ -219,14 +217,13 @@ char *data;
219 return(ret); 217 return(ret);
220 } 218 }
221 219
222char *lh_delete(lh, data) 220char *lh_delete(LHASH *lh, char *data)
223LHASH *lh;
224char *data;
225 { 221 {
226 unsigned long hash; 222 unsigned long hash;
227 LHASH_NODE *nn,**rn; 223 LHASH_NODE *nn,**rn;
228 char *ret; 224 char *ret;
229 225
226 lh->error=0;
230 rn=getrn(lh,data,&hash); 227 rn=getrn(lh,data,&hash);
231 228
232 if (*rn == NULL) 229 if (*rn == NULL)
@@ -239,7 +236,7 @@ char *data;
239 nn= *rn; 236 nn= *rn;
240 *rn=nn->next; 237 *rn=nn->next;
241 ret=nn->data; 238 ret=nn->data;
242 free((char *)nn); 239 Free((char *)nn);
243 lh->num_delete++; 240 lh->num_delete++;
244 } 241 }
245 242
@@ -251,14 +248,13 @@ char *data;
251 return(ret); 248 return(ret);
252 } 249 }
253 250
254char *lh_retrieve(lh, data) 251char *lh_retrieve(LHASH *lh, char *data)
255LHASH *lh;
256char *data;
257 { 252 {
258 unsigned long hash; 253 unsigned long hash;
259 LHASH_NODE **rn; 254 LHASH_NODE **rn;
260 char *ret; 255 char *ret;
261 256
257 lh->error=0;
262 rn=getrn(lh,data,&hash); 258 rn=getrn(lh,data,&hash);
263 259
264 if (*rn == NULL) 260 if (*rn == NULL)
@@ -274,17 +270,12 @@ char *data;
274 return(ret); 270 return(ret);
275 } 271 }
276 272
277void lh_doall(lh, func) 273void lh_doall(LHASH *lh, void (*func)())
278LHASH *lh;
279void (*func)();
280 { 274 {
281 lh_doall_arg(lh,func,NULL); 275 lh_doall_arg(lh,func,NULL);
282 } 276 }
283 277
284void lh_doall_arg(lh, func, arg) 278void lh_doall_arg(LHASH *lh, void (*func)(), char *arg)
285LHASH *lh;
286void (*func)();
287char *arg;
288 { 279 {
289 int i; 280 int i;
290 LHASH_NODE *a,*n; 281 LHASH_NODE *a,*n;
@@ -305,8 +296,7 @@ char *arg;
305 } 296 }
306 } 297 }
307 298
308static void expand(lh) 299static void expand(LHASH *lh)
309LHASH *lh;
310 { 300 {
311 LHASH_NODE **n,**n1,**n2,*np; 301 LHASH_NODE **n,**n1,**n2,*np;
312 unsigned int p,i,j; 302 unsigned int p,i,j;
@@ -342,11 +332,12 @@ LHASH *lh;
342 if ((lh->p) >= lh->pmax) 332 if ((lh->p) >= lh->pmax)
343 { 333 {
344 j=(int)lh->num_alloc_nodes*2; 334 j=(int)lh->num_alloc_nodes*2;
345 n=(LHASH_NODE **)realloc((char *)lh->b, 335 n=(LHASH_NODE **)Realloc((char *)lh->b,
346 (unsigned int)sizeof(LHASH_NODE *)*j); 336 (unsigned int)sizeof(LHASH_NODE *)*j);
347 if (n == NULL) 337 if (n == NULL)
348 { 338 {
349/* fputs("realloc error in lhash",stderr); */ 339/* fputs("realloc error in lhash",stderr); */
340 lh->error++;
350 lh->p=0; 341 lh->p=0;
351 return; 342 return;
352 } 343 }
@@ -361,8 +352,7 @@ LHASH *lh;
361 } 352 }
362 } 353 }
363 354
364static void contract(lh) 355static void contract(LHASH *lh)
365LHASH *lh;
366 { 356 {
367 LHASH_NODE **n,*n1,*np; 357 LHASH_NODE **n,*n1,*np;
368 358
@@ -370,11 +360,12 @@ LHASH *lh;
370 lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */ 360 lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */
371 if (lh->p == 0) 361 if (lh->p == 0)
372 { 362 {
373 n=(LHASH_NODE **)realloc((char *)lh->b, 363 n=(LHASH_NODE **)Realloc((char *)lh->b,
374 (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); 364 (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
375 if (n == NULL) 365 if (n == NULL)
376 { 366 {
377/* fputs("realloc error in lhash",stderr); */ 367/* fputs("realloc error in lhash",stderr); */
368 lh->error++;
378 return; 369 return;
379 } 370 }
380 lh->num_contract_reallocs++; 371 lh->num_contract_reallocs++;
@@ -400,10 +391,7 @@ LHASH *lh;
400 } 391 }
401 } 392 }
402 393
403static LHASH_NODE **getrn(lh, data, rhash) 394static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash)
404LHASH *lh;
405char *data;
406unsigned long *rhash;
407 { 395 {
408 LHASH_NODE **ret,*n1; 396 LHASH_NODE **ret,*n1;
409 unsigned long hash,nn; 397 unsigned long hash,nn;
@@ -457,8 +445,7 @@ char *str;
457 * no collisions on /usr/dict/words and it distributes on %2^n quite 445 * no collisions on /usr/dict/words and it distributes on %2^n quite
458 * well, not as good as MD5, but still good. 446 * well, not as good as MD5, but still good.
459 */ 447 */
460unsigned long lh_strhash(c) 448unsigned long lh_strhash(const char *c)
461char *c;
462 { 449 {
463 unsigned long ret=0; 450 unsigned long ret=0;
464 long n; 451 long n;