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