Coverage Report

Created: 2025-08-28 07:07

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