Coverage Report

Created: 2025-08-11 07:04

/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
7.01M
#define MIN_NODES       16
40
671k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
671k
#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
671k
{
49
671k
    OPENSSL_LHASH *ret;
50
51
671k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
671k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
671k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
671k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
671k
    ret->num_nodes = MIN_NODES / 2;
58
671k
    ret->num_alloc_nodes = MIN_NODES;
59
671k
    ret->pmax = MIN_NODES / 2;
60
671k
    ret->up_load = UP_LOAD;
61
671k
    ret->down_load = DOWN_LOAD;
62
671k
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
671k
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
669k
{
72
669k
    if (lh == NULL)
73
252
        return;
74
75
668k
    OPENSSL_LH_flush(lh);
76
668k
    OPENSSL_free(lh->b);
77
668k
    OPENSSL_free(lh);
78
668k
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
673k
{
82
673k
    unsigned int i;
83
673k
    OPENSSL_LH_NODE *n, *nn;
84
85
673k
    if (lh == NULL)
86
0
        return;
87
88
7.86M
    for (i = 0; i < lh->num_nodes; i++) {
89
7.18M
        n = lh->b[i];
90
9.29M
        while (n != NULL) {
91
2.10M
            nn = n->next;
92
2.10M
            OPENSSL_free(n);
93
2.10M
            n = nn;
94
2.10M
        }
95
7.18M
        lh->b[i] = NULL;
96
7.18M
    }
97
98
673k
    lh->num_items = 0;
99
673k
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
8.15M
{
103
8.15M
    unsigned long hash;
104
8.15M
    OPENSSL_LH_NODE *nn, **rn;
105
8.15M
    void *ret;
106
107
8.15M
    lh->error = 0;
108
8.15M
    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
8.15M
    rn = getrn(lh, data, &hash);
112
113
8.15M
    if (*rn == NULL) {
114
7.11M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
7.11M
        nn->data = data;
119
7.11M
        nn->next = NULL;
120
7.11M
        nn->hash = hash;
121
7.11M
        *rn = nn;
122
7.11M
        ret = NULL;
123
7.11M
        lh->num_items++;
124
7.11M
    } else {                    /* replace same key */
125
1.04M
        ret = (*rn)->data;
126
1.04M
        (*rn)->data = data;
127
1.04M
    }
128
8.15M
    return ret;
129
8.15M
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
5.00M
{
133
5.00M
    unsigned long hash;
134
5.00M
    OPENSSL_LH_NODE *nn, **rn;
135
5.00M
    void *ret;
136
137
5.00M
    lh->error = 0;
138
5.00M
    rn = getrn(lh, data, &hash);
139
140
5.00M
    if (*rn == NULL) {
141
166
        return NULL;
142
5.00M
    } else {
143
5.00M
        nn = *rn;
144
5.00M
        *rn = nn->next;
145
5.00M
        ret = nn->data;
146
5.00M
        OPENSSL_free(nn);
147
5.00M
    }
148
149
5.00M
    lh->num_items--;
150
5.00M
    if ((lh->num_nodes > MIN_NODES) &&
151
5.00M
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
1.06M
        contract(lh);
153
154
5.00M
    return ret;
155
5.00M
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
317M
{
159
317M
    unsigned long hash;
160
317M
    OPENSSL_LH_NODE **rn;
161
162
317M
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
317M
    rn = getrn(lh, data, &hash);
166
167
317M
    return *rn == NULL ? NULL : (*rn)->data;
168
317M
}
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
19.2M
{
174
19.2M
    int i;
175
19.2M
    OPENSSL_LH_NODE *a, *n;
176
177
19.2M
    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
2.93G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
2.91G
        a = lh->b[i];
186
8.69G
        while (a != NULL) {
187
5.77G
            n = a->next;
188
5.77G
            if (use_arg)
189
5.77G
                func_arg(a->data, arg);
190
600k
            else
191
600k
                func(a->data);
192
5.77G
            a = n;
193
5.77G
        }
194
2.91G
    }
195
19.2M
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
7.63k
{
199
7.63k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
7.63k
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
7.32M
{
204
7.32M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
7.32M
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
2.86M
{
209
2.86M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
2.86M
    unsigned int p, pmax, nni, j;
211
2.86M
    unsigned long hash;
212
213
2.86M
    nni = lh->num_alloc_nodes;
214
2.86M
    p = lh->p;
215
2.86M
    pmax = lh->pmax;
216
2.86M
    if (p + 1 >= pmax) {
217
37.9k
        j = nni * 2;
218
37.9k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
37.9k
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
37.9k
        lh->b = n;
224
37.9k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
37.9k
        lh->pmax = nni;
226
37.9k
        lh->num_alloc_nodes = j;
227
37.9k
        lh->p = 0;
228
2.82M
    } else {
229
2.82M
        lh->p++;
230
2.82M
    }
231
232
2.86M
    lh->num_nodes++;
233
2.86M
    n1 = &(lh->b[p]);
234
2.86M
    n2 = &(lh->b[p + pmax]);
235
2.86M
    *n2 = NULL;
236
237
11.2M
    for (np = *n1; np != NULL;) {
238
8.37M
        hash = np->hash;
239
8.37M
        if ((hash % nni) != p) { /* move it */
240
3.24M
            *n1 = (*n1)->next;
241
3.24M
            np->next = *n2;
242
3.24M
            *n2 = np;
243
3.24M
        } else
244
5.12M
            n1 = &((*n1)->next);
245
8.37M
        np = *n1;
246
8.37M
    }
247
248
2.86M
    return 1;
249
2.86M
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
1.06M
{
253
1.06M
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
1.06M
    np = lh->b[lh->p + lh->pmax - 1];
256
1.06M
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
1.06M
    if (lh->p == 0) {
258
11.7k
        n = OPENSSL_realloc(lh->b,
259
11.7k
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
260
11.7k
        if (n == NULL) {
261
            /* fputs("realloc error in lhash", stderr); */
262
0
            lh->error++;
263
11.7k
        } else {
264
11.7k
            lh->b = n;
265
11.7k
        }
266
11.7k
        lh->num_alloc_nodes /= 2;
267
11.7k
        lh->pmax /= 2;
268
11.7k
        lh->p = lh->pmax - 1;
269
11.7k
    } else
270
1.05M
        lh->p--;
271
272
1.06M
    lh->num_nodes--;
273
274
1.06M
    n1 = lh->b[(int)lh->p];
275
1.06M
    if (n1 == NULL)
276
601k
        lh->b[(int)lh->p] = np;
277
464k
    else {
278
1.50M
        while (n1->next != NULL)
279
1.03M
            n1 = n1->next;
280
464k
        n1->next = np;
281
464k
    }
282
1.06M
}
283
284
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285
                               const void *data, unsigned long *rhash)
286
253M
{
287
253M
    OPENSSL_LH_NODE **ret, *n1;
288
253M
    unsigned long hash, nn;
289
253M
    OPENSSL_LH_COMPFUNC cf;
290
291
253M
    hash = (*(lh->hash)) (data);
292
253M
    *rhash = hash;
293
294
253M
    nn = hash % lh->pmax;
295
253M
    if (nn < lh->p)
296
158M
        nn = hash % lh->num_alloc_nodes;
297
298
253M
    cf = lh->comp;
299
253M
    ret = &(lh->b[(int)nn]);
300
911M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
301
863M
        if (n1->hash != hash) {
302
657M
            ret = &(n1->next);
303
657M
            continue;
304
657M
        }
305
206M
        if (cf(n1->data, data) == 0)
306
206M
            break;
307
91.1k
        ret = &(n1->next);
308
91.1k
    }
309
253M
    return ret;
310
253M
}
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
31.1M
{
319
31.1M
    unsigned long ret = 0;
320
31.1M
    long n;
321
31.1M
    unsigned long v;
322
31.1M
    int r;
323
324
31.1M
    if ((c == NULL) || (*c == '\0'))
325
21.0M
        return ret;
326
327
10.0M
    n = 0x100;
328
790M
    while (*c) {
329
780M
        v = n | (*c);
330
780M
        n += 0x100;
331
780M
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
780M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
780M
        ret &= 0xFFFFFFFFL;
335
780M
        ret ^= v * v;
336
780M
        c++;
337
780M
    }
338
10.0M
    return (ret >> 16) ^ ret;
339
31.1M
}
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
238M
{
357
238M
    unsigned long ret = 0;
358
238M
    long n;
359
238M
    unsigned long v;
360
238M
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
238M
    const long int case_adjust = ~0x20;
365
238M
#endif
366
367
238M
    if (c == NULL || *c == '\0')
368
709
        return ret;
369
370
1.51G
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
1.27G
        v = n | (case_adjust & *c);
372
1.27G
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
1.27G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
1.27G
        ret &= 0xFFFFFFFFL;
376
1.27G
        ret ^= v * v;
377
1.27G
        c++;
378
1.27G
    }
379
238M
    return (ret >> 16) ^ ret;
380
238M
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
2.44M
{
384
2.44M
    return lh ? lh->num_items : 0;
385
2.44M
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
143k
{
389
143k
    return lh->down_load;
390
143k
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
455k
{
394
455k
    lh->down_load = down_load;
395
455k
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
5.11M
{
399
5.11M
    return lh->error;
400
5.11M
}