add return value for expand
[openssl.git] / crypto / lhash / lhash.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/crypto.h>
14 #include <openssl/lhash.h>
15 #include "lhash_lcl.h"
16
17
18 #undef MIN_NODES
19 #define MIN_NODES       16
20 #define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
21 #define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
22
23 static int expand(OPENSSL_LHASH *lh);
24 static void contract(OPENSSL_LHASH *lh);
25 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
26
27 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
28 {
29     OPENSSL_LHASH *ret;
30
31     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
32         goto err0;
33     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
34         goto err1;
35     ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
36     ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
37     ret->num_nodes = MIN_NODES / 2;
38     ret->num_alloc_nodes = MIN_NODES;
39     ret->pmax = MIN_NODES / 2;
40     ret->up_load = UP_LOAD;
41     ret->down_load = DOWN_LOAD;
42     return (ret);
43
44  err1:
45     OPENSSL_free(ret);
46  err0:
47     return (NULL);
48 }
49
50 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
51 {
52     unsigned int i;
53     OPENSSL_LH_NODE *n, *nn;
54
55     if (lh == NULL)
56         return;
57
58     for (i = 0; i < lh->num_nodes; i++) {
59         n = lh->b[i];
60         while (n != NULL) {
61             nn = n->next;
62             OPENSSL_free(n);
63             n = nn;
64         }
65     }
66     OPENSSL_free(lh->b);
67     OPENSSL_free(lh);
68 }
69
70 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
71 {
72     unsigned long hash;
73     OPENSSL_LH_NODE *nn, **rn;
74     void *ret;
75     int val = 0;
76     
77     lh->error = 0;
78     if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
79         val = expand(lh);
80     
81     if(val){
82         lh->error++;
83         return (NULL);
84     }
85         
86     rn = getrn(lh, data, &hash);
87
88     if (*rn == NULL) {
89         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
90             lh->error++;
91             return (NULL);
92         }
93         nn->data = data;
94         nn->next = NULL;
95         nn->hash = hash;
96         *rn = nn;
97         ret = NULL;
98         lh->num_insert++;
99         lh->num_items++;
100     } else {                    /* replace same key */
101
102         ret = (*rn)->data;
103         (*rn)->data = data;
104         lh->num_replace++;
105     }
106     return (ret);
107 }
108
109 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
110 {
111     unsigned long hash;
112     OPENSSL_LH_NODE *nn, **rn;
113     void *ret;
114
115     lh->error = 0;
116     rn = getrn(lh, data, &hash);
117
118     if (*rn == NULL) {
119         lh->num_no_delete++;
120         return (NULL);
121     } else {
122         nn = *rn;
123         *rn = nn->next;
124         ret = nn->data;
125         OPENSSL_free(nn);
126         lh->num_delete++;
127     }
128
129     lh->num_items--;
130     if ((lh->num_nodes > MIN_NODES) &&
131         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
132         contract(lh);
133
134     return (ret);
135 }
136
137 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
138 {
139     unsigned long hash;
140     OPENSSL_LH_NODE **rn;
141     void *ret;
142
143     lh->error = 0;
144     rn = getrn(lh, data, &hash);
145
146     if (*rn == NULL) {
147         lh->num_retrieve_miss++;
148         return (NULL);
149     } else {
150         ret = (*rn)->data;
151         lh->num_retrieve++;
152     }
153     return (ret);
154 }
155
156 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
157                           OPENSSL_LH_DOALL_FUNC func,
158                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
159 {
160     int i;
161     OPENSSL_LH_NODE *a, *n;
162
163     if (lh == NULL)
164         return;
165
166     /*
167      * reverse the order so we search from 'top to bottom' We were having
168      * memory leaks otherwise
169      */
170     for (i = lh->num_nodes - 1; i >= 0; i--) {
171         a = lh->b[i];
172         while (a != NULL) {
173             n = a->next;
174             if (use_arg)
175                 func_arg(a->data, arg);
176             else
177                 func(a->data);
178             a = n;
179         }
180     }
181 }
182
183 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
184 {
185     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
186 }
187
188 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
189 {
190     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
191 }
192
193 static int expand(OPENSSL_LHASH *lh)
194 {
195     OPENSSL_LH_NODE **n, **n1, **n2, *np;
196     unsigned int p, i, j;
197     unsigned long hash, nni;
198
199     lh->num_nodes++;
200     lh->num_expands++;
201     p = (int)lh->p++;
202     n1 = &(lh->b[p]);
203     n2 = &(lh->b[p + (int)lh->pmax]);
204     *n2 = NULL;
205     nni = lh->num_alloc_nodes;
206
207     for (np = *n1; np != NULL;) {
208         hash = np->hash;
209         if ((hash % nni) != p) { /* move it */
210             *n1 = (*n1)->next;
211             np->next = *n2;
212             *n2 = np;
213         } else
214             n1 = &((*n1)->next);
215         np = *n1;
216     }
217
218     if ((lh->p) >= lh->pmax) {
219         j = (int)lh->num_alloc_nodes * 2;
220         n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
221         if (n == NULL) {
222             /* fputs("realloc error in lhash",stderr); */
223             lh->error++;
224             lh->p = 0;
225             return -1;
226         }
227         for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
228             n[i] = NULL;        /* 02/03/92 eay */
229         lh->pmax = lh->num_alloc_nodes;
230         lh->num_alloc_nodes = j;
231         lh->num_expand_reallocs++;
232         lh->p = 0;
233         lh->b = n;
234     }
235     return 0;
236 }
237
238 static void contract(OPENSSL_LHASH *lh)
239 {
240     OPENSSL_LH_NODE **n, *n1, *np;
241
242     np = lh->b[lh->p + lh->pmax - 1];
243     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
244     if (lh->p == 0) {
245         n = OPENSSL_realloc(lh->b,
246                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
247         if (n == NULL) {
248             /* fputs("realloc error in lhash",stderr); */
249             lh->error++;
250             return;
251         }
252         lh->num_contract_reallocs++;
253         lh->num_alloc_nodes /= 2;
254         lh->pmax /= 2;
255         lh->p = lh->pmax - 1;
256         lh->b = n;
257     } else
258         lh->p--;
259
260     lh->num_nodes--;
261     lh->num_contracts++;
262
263     n1 = lh->b[(int)lh->p];
264     if (n1 == NULL)
265         lh->b[(int)lh->p] = np;
266     else {
267         while (n1->next != NULL)
268             n1 = n1->next;
269         n1->next = np;
270     }
271 }
272
273 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
274                                const void *data, unsigned long *rhash)
275 {
276     OPENSSL_LH_NODE **ret, *n1;
277     unsigned long hash, nn;
278     OPENSSL_LH_COMPFUNC cf;
279
280     hash = (*(lh->hash)) (data);
281     lh->num_hash_calls++;
282     *rhash = hash;
283
284     nn = hash % lh->pmax;
285     if (nn < lh->p)
286         nn = hash % lh->num_alloc_nodes;
287
288     cf = lh->comp;
289     ret = &(lh->b[(int)nn]);
290     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
291         lh->num_hash_comps++;
292         if (n1->hash != hash) {
293             ret = &(n1->next);
294             continue;
295         }
296         lh->num_comp_calls++;
297         if (cf(n1->data, data) == 0)
298             break;
299         ret = &(n1->next);
300     }
301     return (ret);
302 }
303
304 /*
305  * The following hash seems to work very well on normal text strings no
306  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
307  * as good as MD5, but still good.
308  */
309 unsigned long OPENSSL_LH_strhash(const char *c)
310 {
311     unsigned long ret = 0;
312     long n;
313     unsigned long v;
314     int r;
315
316     if ((c == NULL) || (*c == '\0'))
317         return (ret);
318 /*-
319     unsigned char b[16];
320     MD5(c,strlen(c),b);
321     return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
322 */
323
324     n = 0x100;
325     while (*c) {
326         v = n | (*c);
327         n += 0x100;
328         r = (int)((v >> 2) ^ v) & 0x0f;
329         ret = (ret << r) | (ret >> (32 - r));
330         ret &= 0xFFFFFFFFL;
331         ret ^= v * v;
332         c++;
333     }
334     return ((ret >> 16) ^ ret);
335 }
336
337 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
338 {
339     return lh ? lh->num_items : 0;
340 }
341
342 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
343 {
344     return lh->down_load;
345 }
346
347 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
348 {
349     lh->down_load = down_load;
350 }
351
352 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
353 {
354     return lh->error;
355 }