crypto/*: address standard-compilance nits.
[openssl.git] / crypto / lhash / lhash.c
1 /*
2  * Copyright 1995-2018 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 <openssl/err.h>
16 #include "lhash_lcl.h"
17
18 /*
19  * A hashing implementation that appears to be based on the linear hashing
20  * alogrithm:
21  * https://en.wikipedia.org/wiki/Linear_hashing
22  *
23  * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
24  * addressing", Proc. 6th Conference on Very Large Databases: 212-223
25  * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
26  *
27  * From the wikipedia article "Linear hashing is used in the BDB Berkeley
28  * database system, which in turn is used by many software systems such as
29  * OpenLDAP, using a C implementation derived from the CACM article and first
30  * published on the Usenet in 1988 by Esmond Pitt."
31  *
32  * The CACM paper is available here:
33  * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
34  */
35
36 #undef MIN_NODES
37 #define MIN_NODES       16
38 #define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
39 #define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
40
41 static int expand(OPENSSL_LHASH *lh);
42 static void contract(OPENSSL_LHASH *lh);
43 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
44
45 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
46 {
47     OPENSSL_LHASH *ret;
48
49     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
50         /*
51          * Do not set the error code, because the ERR code uses LHASH
52          * and we want to avoid possible endless error loop.
53          * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE);
54          */
55         return NULL;
56     }
57     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
58         goto err;
59     ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
60     ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
61     ret->num_nodes = MIN_NODES / 2;
62     ret->num_alloc_nodes = MIN_NODES;
63     ret->pmax = MIN_NODES / 2;
64     ret->up_load = UP_LOAD;
65     ret->down_load = DOWN_LOAD;
66     return ret;
67
68 err:
69     OPENSSL_free(ret->b);
70     OPENSSL_free(ret);
71     return NULL;
72 }
73
74 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
75 {
76     unsigned int i;
77     OPENSSL_LH_NODE *n, *nn;
78
79     if (lh == NULL)
80         return;
81
82     for (i = 0; i < lh->num_nodes; i++) {
83         n = lh->b[i];
84         while (n != NULL) {
85             nn = n->next;
86             OPENSSL_free(n);
87             n = nn;
88         }
89     }
90     OPENSSL_free(lh->b);
91     OPENSSL_free(lh);
92 }
93
94 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
95 {
96     unsigned long hash;
97     OPENSSL_LH_NODE *nn, **rn;
98     void *ret;
99
100     lh->error = 0;
101     if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
102         return NULL;        /* 'lh->error++' already done in 'expand' */
103
104     rn = getrn(lh, data, &hash);
105
106     if (*rn == NULL) {
107         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
108             lh->error++;
109             return NULL;
110         }
111         nn->data = data;
112         nn->next = NULL;
113         nn->hash = hash;
114         *rn = nn;
115         ret = NULL;
116         lh->num_insert++;
117         lh->num_items++;
118     } else {                    /* replace same key */
119         ret = (*rn)->data;
120         (*rn)->data = data;
121         lh->num_replace++;
122     }
123     return ret;
124 }
125
126 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
127 {
128     unsigned long hash;
129     OPENSSL_LH_NODE *nn, **rn;
130     void *ret;
131
132     lh->error = 0;
133     rn = getrn(lh, data, &hash);
134
135     if (*rn == NULL) {
136         lh->num_no_delete++;
137         return NULL;
138     } else {
139         nn = *rn;
140         *rn = nn->next;
141         ret = nn->data;
142         OPENSSL_free(nn);
143         lh->num_delete++;
144     }
145
146     lh->num_items--;
147     if ((lh->num_nodes > MIN_NODES) &&
148         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
149         contract(lh);
150
151     return ret;
152 }
153
154 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
155 {
156     unsigned long hash;
157     OPENSSL_LH_NODE **rn;
158     void *ret;
159
160     lh->error = 0;
161     rn = getrn(lh, data, &hash);
162
163     if (*rn == NULL) {
164         lh->num_retrieve_miss++;
165         return NULL;
166     } else {
167         ret = (*rn)->data;
168         lh->num_retrieve++;
169     }
170     return ret;
171 }
172
173 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
174                           OPENSSL_LH_DOALL_FUNC func,
175                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
176 {
177     int i;
178     OPENSSL_LH_NODE *a, *n;
179
180     if (lh == NULL)
181         return;
182
183     /*
184      * reverse the order so we search from 'top to bottom' We were having
185      * memory leaks otherwise
186      */
187     for (i = lh->num_nodes - 1; i >= 0; i--) {
188         a = lh->b[i];
189         while (a != NULL) {
190             n = a->next;
191             if (use_arg)
192                 func_arg(a->data, arg);
193             else
194                 func(a->data);
195             a = n;
196         }
197     }
198 }
199
200 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
201 {
202     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
203 }
204
205 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
206 {
207     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
208 }
209
210 static int expand(OPENSSL_LHASH *lh)
211 {
212     OPENSSL_LH_NODE **n, **n1, **n2, *np;
213     unsigned int p, pmax, nni, j;
214     unsigned long hash;
215
216     nni = lh->num_alloc_nodes;
217     p = lh->p;
218     pmax = lh->pmax;
219     if (p + 1 >= pmax) {
220         j = nni * 2;
221         n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
222         if (n == NULL) {
223             lh->error++;
224             return 0;
225         }
226         lh->b = n;
227         memset(n + nni, 0, sizeof(*n) * (j - nni));
228         lh->pmax = nni;
229         lh->num_alloc_nodes = j;
230         lh->num_expand_reallocs++;
231         lh->p = 0;
232     } else {
233         lh->p++;
234     }
235
236     lh->num_nodes++;
237     lh->num_expands++;
238     n1 = &(lh->b[p]);
239     n2 = &(lh->b[p + pmax]);
240     *n2 = NULL;
241
242     for (np = *n1; np != NULL;) {
243         hash = np->hash;
244         if ((hash % nni) != p) { /* move it */
245             *n1 = (*n1)->next;
246             np->next = *n2;
247             *n2 = np;
248         } else
249             n1 = &((*n1)->next);
250         np = *n1;
251     }
252
253     return 1;
254 }
255
256 static void contract(OPENSSL_LHASH *lh)
257 {
258     OPENSSL_LH_NODE **n, *n1, *np;
259
260     np = lh->b[lh->p + lh->pmax - 1];
261     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
262     if (lh->p == 0) {
263         n = OPENSSL_realloc(lh->b,
264                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
265         if (n == NULL) {
266             /* fputs("realloc error in lhash",stderr); */
267             lh->error++;
268             return;
269         }
270         lh->num_contract_reallocs++;
271         lh->num_alloc_nodes /= 2;
272         lh->pmax /= 2;
273         lh->p = lh->pmax - 1;
274         lh->b = n;
275     } else
276         lh->p--;
277
278     lh->num_nodes--;
279     lh->num_contracts++;
280
281     n1 = lh->b[(int)lh->p];
282     if (n1 == NULL)
283         lh->b[(int)lh->p] = np;
284     else {
285         while (n1->next != NULL)
286             n1 = n1->next;
287         n1->next = np;
288     }
289 }
290
291 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
292                                const void *data, unsigned long *rhash)
293 {
294     OPENSSL_LH_NODE **ret, *n1;
295     unsigned long hash, nn;
296     OPENSSL_LH_COMPFUNC cf;
297
298     hash = (*(lh->hash)) (data);
299     lh->num_hash_calls++;
300     *rhash = hash;
301
302     nn = hash % lh->pmax;
303     if (nn < lh->p)
304         nn = hash % lh->num_alloc_nodes;
305
306     cf = lh->comp;
307     ret = &(lh->b[(int)nn]);
308     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
309         lh->num_hash_comps++;
310         if (n1->hash != hash) {
311             ret = &(n1->next);
312             continue;
313         }
314         lh->num_comp_calls++;
315         if (cf(n1->data, data) == 0)
316             break;
317         ret = &(n1->next);
318     }
319     return ret;
320 }
321
322 /*
323  * The following hash seems to work very well on normal text strings no
324  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
325  * as good as MD5, but still good.
326  */
327 unsigned long OPENSSL_LH_strhash(const char *c)
328 {
329     unsigned long ret = 0;
330     long n;
331     unsigned long v;
332     int r;
333
334     if ((c == NULL) || (*c == '\0'))
335         return ret;
336
337     n = 0x100;
338     while (*c) {
339         v = n | (*c);
340         n += 0x100;
341         r = (int)((v >> 2) ^ v) & 0x0f;
342         ret = (ret << r) | (ret >> (32 - r));
343         ret &= 0xFFFFFFFFL;
344         ret ^= v * v;
345         c++;
346     }
347     return (ret >> 16) ^ ret;
348 }
349
350 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
351 {
352     return lh ? lh->num_items : 0;
353 }
354
355 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
356 {
357     return lh->down_load;
358 }
359
360 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
361 {
362     lh->down_load = down_load;
363 }
364
365 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
366 {
367     return lh->error;
368 }