Coverage Report

Created: 2024-08-20 14:11

/src/openssl/crypto/lhash/lhash.c
Line
Count
Source (jump to first uncovered line)
1
/* crypto/lhash/lhash.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
/*-
60
 * Code for dynamic hash table routines
61
 * Author - Eric Young v 2.0
62
 *
63
 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
64
 *           present. eay 18-Jun-98
65
 *
66
 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
67
 *
68
 * 2.0 eay - Fixed a bug that occurred when using lh_delete
69
 *           from inside lh_doall().  As entries were deleted,
70
 *           the 'table' was 'contract()ed', making some entries
71
 *           jump from the end of the table to the start, there by
72
 *           skipping the lh_doall() processing. eay - 4/12/95
73
 *
74
 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
75
 *           were not being free()ed. 21/11/95
76
 *
77
 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
78
 *           19/09/95
79
 *
80
 * 1.7 eay - Removed the fputs() for realloc failures - the code
81
 *           should silently tolerate them.  I have also fixed things
82
 *           lint complained about 04/05/95
83
 *
84
 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
85
 *
86
 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
87
 *
88
 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
89
 *
90
 * 1.3 eay - Fixed a few lint problems 19/3/1991
91
 *
92
 * 1.2 eay - Fixed lh_doall problem 13/3/1991
93
 *
94
 * 1.1 eay - Added lh_doall
95
 *
96
 * 1.0 eay - First version
97
 */
98
#include <stdio.h>
99
#include <string.h>
100
#include <stdlib.h>
101
#include <openssl/crypto.h>
102
#include <openssl/lhash.h>
103
104
/*
105
 * A hashing implementation that appears to be based on the linear hashing
106
 * alogrithm:
107
 * https://en.wikipedia.org/wiki/Linear_hashing
108
 *
109
 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
110
 * addressing", Proc. 6th Conference on Very Large Databases: 212–223
111
 * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
112
 *
113
 * From the wikipedia article "Linear hashing is used in the BDB Berkeley
114
 * database system, which in turn is used by many software systems such as
115
 * OpenLDAP, using a C implementation derived from the CACM article and first
116
 * published on the Usenet in 1988 by Esmond Pitt."
117
 *
118
 * The CACM paper is available here:
119
 * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
120
 */
121
122
const char lh_version[] = "lhash" OPENSSL_VERSION_PTEXT;
123
124
#undef MIN_NODES
125
622k
#define MIN_NODES       16
126
30.4k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
127
30.4k
#define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
128
129
static int expand(_LHASH *lh);
130
static void contract(_LHASH *lh);
131
static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
132
133
_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
134
30.4k
{
135
30.4k
    _LHASH *ret;
136
30.4k
    int i;
137
138
30.4k
    if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)
139
0
        goto err0;
140
30.4k
    if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)
141
0
        goto err1;
142
517k
    for (i = 0; i < MIN_NODES; i++)
143
486k
        ret->b[i] = NULL;
144
30.4k
    ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
145
30.4k
    ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
146
30.4k
    ret->num_nodes = MIN_NODES / 2;
147
30.4k
    ret->num_alloc_nodes = MIN_NODES;
148
30.4k
    ret->p = 0;
149
30.4k
    ret->pmax = MIN_NODES / 2;
150
30.4k
    ret->up_load = UP_LOAD;
151
30.4k
    ret->down_load = DOWN_LOAD;
152
30.4k
    ret->num_items = 0;
153
154
30.4k
    ret->num_expands = 0;
155
30.4k
    ret->num_expand_reallocs = 0;
156
30.4k
    ret->num_contracts = 0;
157
30.4k
    ret->num_contract_reallocs = 0;
158
30.4k
    ret->num_hash_calls = 0;
159
30.4k
    ret->num_comp_calls = 0;
160
30.4k
    ret->num_insert = 0;
161
30.4k
    ret->num_replace = 0;
162
30.4k
    ret->num_delete = 0;
163
30.4k
    ret->num_no_delete = 0;
164
30.4k
    ret->num_retrieve = 0;
165
30.4k
    ret->num_retrieve_miss = 0;
166
30.4k
    ret->num_hash_comps = 0;
167
168
30.4k
    ret->error = 0;
169
30.4k
    return (ret);
170
0
 err1:
171
0
    OPENSSL_free(ret);
172
0
 err0:
173
0
    return (NULL);
174
0
}
175
176
void lh_free(_LHASH *lh)
177
13.8k
{
178
13.8k
    unsigned int i;
179
13.8k
    LHASH_NODE *n, *nn;
180
181
13.8k
    if (lh == NULL)
182
0
        return;
183
184
124k
    for (i = 0; i < lh->num_nodes; i++) {
185
110k
        n = lh->b[i];
186
110k
        while (n != NULL) {
187
0
            nn = n->next;
188
0
            OPENSSL_free(n);
189
0
            n = nn;
190
0
        }
191
110k
    }
192
13.8k
    OPENSSL_free(lh->b);
193
13.8k
    OPENSSL_free(lh);
194
13.8k
}
195
196
void *lh_insert(_LHASH *lh, void *data)
197
32.2M
{
198
32.2M
    unsigned long hash;
199
32.2M
    LHASH_NODE *nn, **rn;
200
32.2M
    void *ret;
201
202
32.2M
    lh->error = 0;
203
32.2M
    if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)
204
32.2M
            && !expand(lh))
205
0
        return NULL;
206
207
32.2M
    rn = getrn(lh, data, &hash);
208
209
32.2M
    if (*rn == NULL) {
210
5.33M
        if ((nn = (LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) {
211
0
            lh->error++;
212
0
            return (NULL);
213
0
        }
214
5.33M
        nn->data = data;
215
5.33M
        nn->next = NULL;
216
5.33M
#ifndef OPENSSL_NO_HASH_COMP
217
5.33M
        nn->hash = hash;
218
5.33M
#endif
219
5.33M
        *rn = nn;
220
5.33M
        ret = NULL;
221
5.33M
        lh->num_insert++;
222
5.33M
        lh->num_items++;
223
26.9M
    } else {                    /* replace same key */
224
225
26.9M
        ret = (*rn)->data;
226
26.9M
        (*rn)->data = data;
227
26.9M
        lh->num_replace++;
228
26.9M
    }
229
32.2M
    return (ret);
230
32.2M
}
231
232
void *lh_delete(_LHASH *lh, const void *data)
233
13.8k
{
234
13.8k
    unsigned long hash;
235
13.8k
    LHASH_NODE *nn, **rn;
236
13.8k
    void *ret;
237
238
13.8k
    lh->error = 0;
239
13.8k
    rn = getrn(lh, data, &hash);
240
241
13.8k
    if (*rn == NULL) {
242
0
        lh->num_no_delete++;
243
0
        return (NULL);
244
13.8k
    } else {
245
13.8k
        nn = *rn;
246
13.8k
        *rn = nn->next;
247
13.8k
        ret = nn->data;
248
13.8k
        OPENSSL_free(nn);
249
13.8k
        lh->num_delete++;
250
13.8k
    }
251
252
13.8k
    lh->num_items--;
253
13.8k
    if ((lh->num_nodes > MIN_NODES) &&
254
13.8k
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
255
0
        contract(lh);
256
257
13.8k
    return (ret);
258
13.8k
}
259
260
void *lh_retrieve(_LHASH *lh, const void *data)
261
650k
{
262
650k
    unsigned long hash;
263
650k
    LHASH_NODE **rn;
264
650k
    void *ret;
265
266
650k
    lh->error = 0;
267
650k
    rn = getrn(lh, data, &hash);
268
269
650k
    if (*rn == NULL) {
270
211k
        lh->num_retrieve_miss++;
271
211k
        return (NULL);
272
438k
    } else {
273
438k
        ret = (*rn)->data;
274
438k
        lh->num_retrieve++;
275
438k
    }
276
438k
    return (ret);
277
650k
}
278
279
static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
280
                          LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
281
0
{
282
0
    int i;
283
0
    LHASH_NODE *a, *n;
284
285
0
    if (lh == NULL)
286
0
        return;
287
288
    /*
289
     * reverse the order so we search from 'top to bottom' We were having
290
     * memory leaks otherwise
291
     */
292
0
    for (i = lh->num_nodes - 1; i >= 0; i--) {
293
0
        a = lh->b[i];
294
0
        while (a != NULL) {
295
            /*
296
             * 28/05/91 - eay - n added so items can be deleted via lh_doall
297
             */
298
            /*
299
             * 22/05/08 - ben - eh? since a is not passed, this should not be
300
             * needed
301
             */
302
0
            n = a->next;
303
0
            if (use_arg)
304
0
                func_arg(a->data, arg);
305
0
            else
306
0
                func(a->data);
307
0
            a = n;
308
0
        }
309
0
    }
310
0
}
311
312
void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
313
0
{
314
0
    doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
315
0
}
316
317
void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
318
0
{
319
0
    doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
320
0
}
321
322
static int expand(_LHASH *lh)
323
2.62M
{
324
2.62M
    LHASH_NODE **n, **n1, **n2, *np;
325
2.62M
    unsigned int p, pmax, nni, j;
326
2.62M
    unsigned long hash;
327
328
2.62M
    nni = lh->num_alloc_nodes;
329
2.62M
    p = lh->p;
330
2.62M
    pmax = lh->pmax;
331
2.62M
    if (p + 1 >= pmax) {
332
18.2k
        j = nni * 2;
333
18.2k
        n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
334
18.2k
        if (n == NULL) {
335
0
            lh->error++;
336
0
            return 0;
337
0
        }
338
18.2k
        lh->b = n;
339
18.2k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
340
18.2k
        lh->pmax = nni;
341
18.2k
        lh->num_alloc_nodes = j;
342
18.2k
        lh->num_expand_reallocs++;
343
18.2k
        lh->p = 0;
344
2.60M
    } else {
345
2.60M
        lh->p++;
346
2.60M
    }
347
348
2.62M
    lh->num_nodes++;
349
2.62M
    lh->num_expands++;
350
2.62M
    n1 = &(lh->b[p]);
351
2.62M
    n2 = &(lh->b[p + pmax]);
352
2.62M
    *n2 = NULL;
353
354
11.9M
    for (np = *n1; np != NULL;) {
355
9.30M
#ifndef OPENSSL_NO_HASH_COMP
356
9.30M
        hash = np->hash;
357
#else
358
        hash = lh->hash(np->data);
359
        lh->num_hash_calls++;
360
#endif
361
9.30M
        if ((hash % nni) != p) { /* move it */
362
924k
            *n1 = (*n1)->next;
363
924k
            np->next = *n2;
364
924k
            *n2 = np;
365
924k
        } else
366
8.38M
            n1 = &((*n1)->next);
367
9.30M
        np = *n1;
368
9.30M
    }
369
370
2.62M
    return 1;
371
2.62M
}
372
373
static void contract(_LHASH *lh)
374
0
{
375
0
    LHASH_NODE **n, *n1, *np;
376
377
0
    np = lh->b[lh->p + lh->pmax - 1];
378
0
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
379
0
    if (lh->p == 0) {
380
0
        n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
381
0
                                           (unsigned int)(sizeof(LHASH_NODE *)
382
0
                                                          * lh->pmax));
383
0
        if (n == NULL) {
384
/*                      fputs("realloc error in lhash",stderr); */
385
0
            lh->error++;
386
0
            return;
387
0
        }
388
0
        lh->num_contract_reallocs++;
389
0
        lh->num_alloc_nodes /= 2;
390
0
        lh->pmax /= 2;
391
0
        lh->p = lh->pmax - 1;
392
0
        lh->b = n;
393
0
    } else
394
0
        lh->p--;
395
396
0
    lh->num_nodes--;
397
0
    lh->num_contracts++;
398
399
0
    n1 = lh->b[(int)lh->p];
400
0
    if (n1 == NULL)
401
0
        lh->b[(int)lh->p] = np;
402
0
    else {
403
0
        while (n1->next != NULL)
404
0
            n1 = n1->next;
405
0
        n1->next = np;
406
0
    }
407
0
}
408
409
static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
410
32.9M
{
411
32.9M
    LHASH_NODE **ret, *n1;
412
32.9M
    unsigned long hash, nn;
413
32.9M
    LHASH_COMP_FN_TYPE cf;
414
415
32.9M
    hash = (*(lh->hash)) (data);
416
32.9M
    lh->num_hash_calls++;
417
32.9M
    *rhash = hash;
418
419
32.9M
    nn = hash % lh->pmax;
420
32.9M
    if (nn < lh->p)
421
19.3M
        nn = hash % lh->num_alloc_nodes;
422
423
32.9M
    cf = lh->comp;
424
32.9M
    ret = &(lh->b[(int)nn]);
425
74.6M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
426
69.1M
#ifndef OPENSSL_NO_HASH_COMP
427
69.1M
        lh->num_hash_comps++;
428
69.1M
        if (n1->hash != hash) {
429
39.9M
            ret = &(n1->next);
430
39.9M
            continue;
431
39.9M
        }
432
29.1M
#endif
433
29.1M
        lh->num_comp_calls++;
434
29.1M
        if (cf(n1->data, data) == 0)
435
27.3M
            break;
436
1.75M
        ret = &(n1->next);
437
1.75M
    }
438
32.9M
    return (ret);
439
32.9M
}
440
441
/*
442
 * The following hash seems to work very well on normal text strings no
443
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
444
 * as good as MD5, but still good.
445
 */
446
unsigned long lh_strhash(const char *c)
447
642k
{
448
642k
    unsigned long ret = 0;
449
642k
    long n;
450
642k
    unsigned long v;
451
642k
    int r;
452
453
642k
    if ((c == NULL) || (*c == '\0'))
454
0
        return (ret);
455
/*-
456
    unsigned char b[16];
457
    MD5(c,strlen(c),b);
458
    return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
459
*/
460
461
642k
    n = 0x100;
462
7.27M
    while (*c) {
463
6.63M
        v = n | (*c);
464
6.63M
        n += 0x100;
465
6.63M
        r = (int)((v >> 2) ^ v) & 0x0f;
466
6.63M
        ret = (ret << r) | (ret >> (32 - r));
467
6.63M
        ret &= 0xFFFFFFFFL;
468
6.63M
        ret ^= v * v;
469
6.63M
        c++;
470
6.63M
    }
471
642k
    return ((ret >> 16) ^ ret);
472
642k
}
473
474
unsigned long lh_num_items(const _LHASH *lh)
475
13.8k
{
476
13.8k
    return lh ? lh->num_items : 0;
477
13.8k
}