Document that lhash isn't thread safe under any circumstances and
[openssl.git] / crypto / lhash / lhash.c
1 /*
2  * Copyright 1995-2017 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         return NULL;
33     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
34         goto err;
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 err:
45     OPENSSL_free(ret->b);
46     OPENSSL_free(ret);
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
76     lh->error = 0;
77     if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
78         return NULL;        /* 'lh->error++' already done in 'expand' */
79
80     rn = getrn(lh, data, &hash);
81
82     if (*rn == NULL) {
83         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
84             lh->error++;
85             return NULL;
86         }
87         nn->data = data;
88         nn->next = NULL;
89         nn->hash = hash;
90         *rn = nn;
91         ret = NULL;
92         lh->num_insert++;
93         lh->num_items++;
94     } else {                    /* replace same key */
95         ret = (*rn)->data;
96         (*rn)->data = data;
97         lh->num_replace++;
98     }
99     return ret;
100 }
101
102 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
103 {
104     unsigned long hash;
105     OPENSSL_LH_NODE *nn, **rn;
106     void *ret;
107
108     lh->error = 0;
109     rn = getrn(lh, data, &hash);
110
111     if (*rn == NULL) {
112         lh->num_no_delete++;
113         return NULL;
114     } else {
115         nn = *rn;
116         *rn = nn->next;
117         ret = nn->data;
118         OPENSSL_free(nn);
119         lh->num_delete++;
120     }
121
122     lh->num_items--;
123     if ((lh->num_nodes > MIN_NODES) &&
124         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
125         contract(lh);
126
127     return ret;
128 }
129
130 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
131 {
132     unsigned long hash;
133     OPENSSL_LH_NODE **rn;
134     void *ret;
135
136     lh->error = 0;
137     rn = getrn(lh, data, &hash);
138
139     if (*rn == NULL) {
140         lh->num_retrieve_miss++;
141         return NULL;
142     } else {
143         ret = (*rn)->data;
144         lh->num_retrieve++;
145     }
146     return ret;
147 }
148
149 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
150                           OPENSSL_LH_DOALL_FUNC func,
151                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
152 {
153     int i;
154     OPENSSL_LH_NODE *a, *n;
155
156     if (lh == NULL)
157         return;
158
159     /*
160      * reverse the order so we search from 'top to bottom' We were having
161      * memory leaks otherwise
162      */
163     for (i = lh->num_nodes - 1; i >= 0; i--) {
164         a = lh->b[i];
165         while (a != NULL) {
166             n = a->next;
167             if (use_arg)
168                 func_arg(a->data, arg);
169             else
170                 func(a->data);
171             a = n;
172         }
173     }
174 }
175
176 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
177 {
178     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
179 }
180
181 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
182 {
183     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
184 }
185
186 static int expand(OPENSSL_LHASH *lh)
187 {
188     OPENSSL_LH_NODE **n, **n1, **n2, *np;
189     unsigned int p, i, j;
190     unsigned long hash, nni;
191
192     lh->num_nodes++;
193     lh->num_expands++;
194     p = (int)lh->p++;
195     n1 = &(lh->b[p]);
196     n2 = &(lh->b[p + (int)lh->pmax]);
197     *n2 = NULL;
198     nni = lh->num_alloc_nodes;
199
200     for (np = *n1; np != NULL;) {
201         hash = np->hash;
202         if ((hash % nni) != p) { /* move it */
203             *n1 = (*n1)->next;
204             np->next = *n2;
205             *n2 = np;
206         } else
207             n1 = &((*n1)->next);
208         np = *n1;
209     }
210
211     if ((lh->p) >= lh->pmax) {
212         j = (int)lh->num_alloc_nodes * 2;
213         n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
214         if (n == NULL) {
215             lh->error++;
216             lh->num_nodes--;
217             lh->p = 0;
218             return 0;
219         }
220         for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
221             n[i] = NULL;        /* 02/03/92 eay */
222         lh->pmax = lh->num_alloc_nodes;
223         lh->num_alloc_nodes = j;
224         lh->num_expand_reallocs++;
225         lh->p = 0;
226         lh->b = n;
227     }
228     return 1;
229 }
230
231 static void contract(OPENSSL_LHASH *lh)
232 {
233     OPENSSL_LH_NODE **n, *n1, *np;
234
235     np = lh->b[lh->p + lh->pmax - 1];
236     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
237     if (lh->p == 0) {
238         n = OPENSSL_realloc(lh->b,
239                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
240         if (n == NULL) {
241             /* fputs("realloc error in lhash",stderr); */
242             lh->error++;
243             return;
244         }
245         lh->num_contract_reallocs++;
246         lh->num_alloc_nodes /= 2;
247         lh->pmax /= 2;
248         lh->p = lh->pmax - 1;
249         lh->b = n;
250     } else
251         lh->p--;
252
253     lh->num_nodes--;
254     lh->num_contracts++;
255
256     n1 = lh->b[(int)lh->p];
257     if (n1 == NULL)
258         lh->b[(int)lh->p] = np;
259     else {
260         while (n1->next != NULL)
261             n1 = n1->next;
262         n1->next = np;
263     }
264 }
265
266 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
267                                const void *data, unsigned long *rhash)
268 {
269     OPENSSL_LH_NODE **ret, *n1;
270     unsigned long hash, nn;
271     OPENSSL_LH_COMPFUNC cf;
272
273     hash = (*(lh->hash)) (data);
274     lh->num_hash_calls++;
275     *rhash = hash;
276
277     nn = hash % lh->pmax;
278     if (nn < lh->p)
279         nn = hash % lh->num_alloc_nodes;
280
281     cf = lh->comp;
282     ret = &(lh->b[(int)nn]);
283     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
284         lh->num_hash_comps++;
285         if (n1->hash != hash) {
286             ret = &(n1->next);
287             continue;
288         }
289         lh->num_comp_calls++;
290         if (cf(n1->data, data) == 0)
291             break;
292         ret = &(n1->next);
293     }
294     return ret;
295 }
296
297 /*
298  * The following hash seems to work very well on normal text strings no
299  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
300  * as good as MD5, but still good.
301  */
302 unsigned long OPENSSL_LH_strhash(const char *c)
303 {
304     unsigned long ret = 0;
305     long n;
306     unsigned long v;
307     int r;
308
309     if ((c == NULL) || (*c == '\0'))
310         return ret;
311
312     n = 0x100;
313     while (*c) {
314         v = n | (*c);
315         n += 0x100;
316         r = (int)((v >> 2) ^ v) & 0x0f;
317         ret = (ret << r) | (ret >> (32 - r));
318         ret &= 0xFFFFFFFFL;
319         ret ^= v * v;
320         c++;
321     }
322     return (ret >> 16) ^ ret;
323 }
324
325 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
326 {
327     return lh ? lh->num_items : 0;
328 }
329
330 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
331 {
332     return lh->down_load;
333 }
334
335 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
336 {
337     lh->down_load = down_load;
338 }
339
340 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
341 {
342     return lh->error;
343 }