Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/lhash/lhash.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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 "crypto/ctype.h"
17
#include "crypto/lhash.h"
18
#include "lhash_local.h"
19
20
/*
21
 * A hashing implementation that appears to be based on the linear hashing
22
 * algorithm:
23
 * https://en.wikipedia.org/wiki/Linear_hashing
24
 *
25
 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
26
 * addressing", Proc. 6th Conference on Very Large Databases: 212-223
27
 * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
28
 *
29
 * From the Wikipedia article "Linear hashing is used in the BDB Berkeley
30
 * database system, which in turn is used by many software systems such as
31
 * OpenLDAP, using a C implementation derived from the CACM article and first
32
 * published on the Usenet in 1988 by Esmond Pitt."
33
 *
34
 * The CACM paper is available here:
35
 * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
36
 */
37
38
#undef MIN_NODES
39
2.66M
#define MIN_NODES       16
40
305k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
305k
#define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
42
43
static int expand(OPENSSL_LHASH *lh);
44
static void contract(OPENSSL_LHASH *lh);
45
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
46
47
OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
48
305k
{
49
305k
    OPENSSL_LHASH *ret;
50
51
305k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
305k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
305k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
305k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
305k
    ret->num_nodes = MIN_NODES / 2;
58
305k
    ret->num_alloc_nodes = MIN_NODES;
59
305k
    ret->pmax = MIN_NODES / 2;
60
305k
    ret->up_load = UP_LOAD;
61
305k
    ret->down_load = DOWN_LOAD;
62
305k
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
305k
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
305k
{
72
305k
    if (lh == NULL)
73
133
        return;
74
75
305k
    OPENSSL_LH_flush(lh);
76
305k
    OPENSSL_free(lh->b);
77
305k
    OPENSSL_free(lh);
78
305k
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
307k
{
82
307k
    unsigned int i;
83
307k
    OPENSSL_LH_NODE *n, *nn;
84
85
307k
    if (lh == NULL)
86
0
        return;
87
88
3.39M
    for (i = 0; i < lh->num_nodes; i++) {
89
3.08M
        n = lh->b[i];
90
3.94M
        while (n != NULL) {
91
864k
            nn = n->next;
92
864k
            OPENSSL_free(n);
93
864k
            n = nn;
94
864k
        }
95
3.08M
        lh->b[i] = NULL;
96
3.08M
    }
97
98
307k
    lh->num_items = 0;
99
307k
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
3.17M
{
103
3.17M
    unsigned long hash;
104
3.17M
    OPENSSL_LH_NODE *nn, **rn;
105
3.17M
    void *ret;
106
107
3.17M
    lh->error = 0;
108
3.17M
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
109
0
        return NULL;        /* 'lh->error++' already done in 'expand' */
110
111
3.17M
    rn = getrn(lh, data, &hash);
112
113
3.17M
    if (*rn == NULL) {
114
2.61M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
2.61M
        nn->data = data;
119
2.61M
        nn->next = NULL;
120
2.61M
        nn->hash = hash;
121
2.61M
        *rn = nn;
122
2.61M
        ret = NULL;
123
2.61M
        lh->num_items++;
124
2.61M
    } else {                    /* replace same key */
125
563k
        ret = (*rn)->data;
126
563k
        (*rn)->data = data;
127
563k
    }
128
3.17M
    return ret;
129
3.17M
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
1.74M
{
133
1.74M
    unsigned long hash;
134
1.74M
    OPENSSL_LH_NODE *nn, **rn;
135
1.74M
    void *ret;
136
137
1.74M
    lh->error = 0;
138
1.74M
    rn = getrn(lh, data, &hash);
139
140
1.74M
    if (*rn == NULL) {
141
144
        return NULL;
142
1.74M
    } else {
143
1.74M
        nn = *rn;
144
1.74M
        *rn = nn->next;
145
1.74M
        ret = nn->data;
146
1.74M
        OPENSSL_free(nn);
147
1.74M
    }
148
149
1.74M
    lh->num_items--;
150
1.74M
    if ((lh->num_nodes > MIN_NODES) &&
151
1.74M
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
378k
        contract(lh);
153
154
1.74M
    return ret;
155
1.74M
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
311M
{
159
311M
    unsigned long hash;
160
311M
    OPENSSL_LH_NODE **rn;
161
162
311M
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
311M
    rn = getrn(lh, data, &hash);
166
167
311M
    return *rn == NULL ? NULL : (*rn)->data;
168
311M
}
169
170
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
171
                          OPENSSL_LH_DOALL_FUNC func,
172
                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
173
10.7M
{
174
10.7M
    int i;
175
10.7M
    OPENSSL_LH_NODE *a, *n;
176
177
10.7M
    if (lh == NULL)
178
0
        return;
179
180
    /*
181
     * reverse the order so we search from 'top to bottom' We were having
182
     * memory leaks otherwise
183
     */
184
1.07G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
1.06G
        a = lh->b[i];
186
3.16G
        while (a != NULL) {
187
2.10G
            n = a->next;
188
2.10G
            if (use_arg)
189
2.10G
                func_arg(a->data, arg);
190
226k
            else
191
226k
                func(a->data);
192
2.10G
            a = n;
193
2.10G
        }
194
1.06G
    }
195
10.7M
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
14.0k
{
199
14.0k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
14.0k
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
8.13M
{
204
8.13M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
8.13M
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
1.00M
{
209
1.00M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
1.00M
    unsigned int p, pmax, nni, j;
211
1.00M
    unsigned long hash;
212
213
1.00M
    nni = lh->num_alloc_nodes;
214
1.00M
    p = lh->p;
215
1.00M
    pmax = lh->pmax;
216
1.00M
    if (p + 1 >= pmax) {
217
17.3k
        j = nni * 2;
218
17.3k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
17.3k
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
17.3k
        lh->b = n;
224
17.3k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
17.3k
        lh->pmax = nni;
226
17.3k
        lh->num_alloc_nodes = j;
227
17.3k
        lh->p = 0;
228
986k
    } else {
229
986k
        lh->p++;
230
986k
    }
231
232
1.00M
    lh->num_nodes++;
233
1.00M
    n1 = &(lh->b[p]);
234
1.00M
    n2 = &(lh->b[p + pmax]);
235
1.00M
    *n2 = NULL;
236
237
3.98M
    for (np = *n1; np != NULL;) {
238
2.98M
        hash = np->hash;
239
2.98M
        if ((hash % nni) != p) { /* move it */
240
1.30M
            *n1 = (*n1)->next;
241
1.30M
            np->next = *n2;
242
1.30M
            *n2 = np;
243
1.30M
        } else
244
1.67M
            n1 = &((*n1)->next);
245
2.98M
        np = *n1;
246
2.98M
    }
247
248
1.00M
    return 1;
249
1.00M
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
378k
{
253
378k
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
378k
    np = lh->b[lh->p + lh->pmax - 1];
256
378k
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
378k
    if (lh->p == 0) {
258
6.31k
        n = OPENSSL_realloc(lh->b,
259
6.31k
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
260
6.31k
        if (n == NULL) {
261
            /* fputs("realloc error in lhash", stderr); */
262
0
            lh->error++;
263
6.31k
        } else {
264
6.31k
            lh->b = n;
265
6.31k
        }
266
6.31k
        lh->num_alloc_nodes /= 2;
267
6.31k
        lh->pmax /= 2;
268
6.31k
        lh->p = lh->pmax - 1;
269
6.31k
    } else
270
372k
        lh->p--;
271
272
378k
    lh->num_nodes--;
273
274
378k
    n1 = lh->b[(int)lh->p];
275
378k
    if (n1 == NULL)
276
105k
        lh->b[(int)lh->p] = np;
277
273k
    else {
278
458k
        while (n1->next != NULL)
279
185k
            n1 = n1->next;
280
273k
        n1->next = np;
281
273k
    }
282
378k
}
283
284
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285
                               const void *data, unsigned long *rhash)
286
298M
{
287
298M
    OPENSSL_LH_NODE **ret, *n1;
288
298M
    unsigned long hash, nn;
289
298M
    OPENSSL_LH_COMPFUNC cf;
290
291
298M
    hash = (*(lh->hash)) (data);
292
298M
    *rhash = hash;
293
294
298M
    nn = hash % lh->pmax;
295
298M
    if (nn < lh->p)
296
192M
        nn = hash % lh->num_alloc_nodes;
297
298
298M
    cf = lh->comp;
299
298M
    ret = &(lh->b[(int)nn]);
300
1.03G
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
301
964M
        if (n1->hash != hash) {
302
739M
            ret = &(n1->next);
303
739M
            continue;
304
739M
        }
305
224M
        if (cf(n1->data, data) == 0)
306
224M
            break;
307
127k
        ret = &(n1->next);
308
127k
    }
309
298M
    return ret;
310
298M
}
311
312
/*
313
 * The following hash seems to work very well on normal text strings no
314
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
315
 * as good as MD5, but still good.
316
 */
317
unsigned long OPENSSL_LH_strhash(const char *c)
318
28.3M
{
319
28.3M
    unsigned long ret = 0;
320
28.3M
    long n;
321
28.3M
    unsigned long v;
322
28.3M
    int r;
323
324
28.3M
    if ((c == NULL) || (*c == '\0'))
325
11.9M
        return ret;
326
327
16.3M
    n = 0x100;
328
614M
    while (*c) {
329
598M
        v = n | (*c);
330
598M
        n += 0x100;
331
598M
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
598M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
598M
        ret &= 0xFFFFFFFFL;
335
598M
        ret ^= v * v;
336
598M
        c++;
337
598M
    }
338
16.3M
    return (ret >> 16) ^ ret;
339
28.3M
}
340
341
/*
342
 * Case insensitive string hashing.
343
 *
344
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
345
 * The major side effect on non-alpha characters is mapping the symbols and
346
 * digits into the control character range (which should be harmless).
347
 * The duplication (with respect to the hash value) of printable characters
348
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
349
 * respectively (which seems tolerable).
350
 *
351
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
352
 * characters.  The only duplication is '0' mapping to '^', which is better
353
 * than for ASCII.
354
 */
355
unsigned long ossl_lh_strcasehash(const char *c)
356
273M
{
357
273M
    unsigned long ret = 0;
358
273M
    long n;
359
273M
    unsigned long v;
360
273M
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
273M
    const long int case_adjust = ~0x20;
365
273M
#endif
366
367
273M
    if (c == NULL || *c == '\0')
368
495
        return ret;
369
370
1.67G
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
1.40G
        v = n | (case_adjust & *c);
372
1.40G
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
1.40G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
1.40G
        ret &= 0xFFFFFFFFL;
376
1.40G
        ret ^= v * v;
377
1.40G
        c++;
378
1.40G
    }
379
273M
    return (ret >> 16) ^ ret;
380
273M
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
2.92M
{
384
2.92M
    return lh ? lh->num_items : 0;
385
2.92M
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
81.4k
{
389
81.4k
    return lh->down_load;
390
81.4k
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
222k
{
394
222k
    lh->down_load = down_load;
395
222k
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
1.34M
{
399
1.34M
    return lh->error;
400
1.34M
}