Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/lhash/lhash.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
10.4M
#define MIN_NODES 16
40
810k
#define UP_LOAD (2 * LH_LOAD_MULT) /* load times 256 (default 2) */
41
810k
#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_set_thunks(OPENSSL_LHASH *lh,
48
    OPENSSL_LH_HASHFUNCTHUNK hw,
49
    OPENSSL_LH_COMPFUNCTHUNK cw,
50
    OPENSSL_LH_DOALL_FUNC_THUNK daw,
51
    OPENSSL_LH_DOALL_FUNCARG_THUNK daaw)
52
792k
{
53
54
792k
    if (lh == NULL)
55
0
        return NULL;
56
792k
    lh->compw = cw;
57
792k
    lh->hashw = hw;
58
792k
    lh->daw = daw;
59
792k
    lh->daaw = daaw;
60
792k
    return lh;
61
792k
}
62
63
OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
64
810k
{
65
810k
    OPENSSL_LHASH *ret;
66
67
810k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
68
0
        return NULL;
69
810k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
70
0
        goto err;
71
810k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
72
810k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
73
810k
    ret->num_nodes = MIN_NODES / 2;
74
810k
    ret->num_alloc_nodes = MIN_NODES;
75
810k
    ret->pmax = MIN_NODES / 2;
76
810k
    ret->up_load = UP_LOAD;
77
810k
    ret->down_load = DOWN_LOAD;
78
810k
    return ret;
79
80
0
err:
81
0
    OPENSSL_free(ret->b);
82
0
    OPENSSL_free(ret);
83
0
    return NULL;
84
810k
}
85
86
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
87
842k
{
88
842k
    if (lh == NULL)
89
34.5k
        return;
90
91
808k
    OPENSSL_LH_flush(lh);
92
808k
    OPENSSL_free(lh->b);
93
808k
    OPENSSL_free(lh);
94
808k
}
95
96
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
97
812k
{
98
812k
    unsigned int i;
99
812k
    OPENSSL_LH_NODE *n, *nn;
100
101
812k
    if (lh == NULL)
102
0
        return;
103
104
10.0M
    for (i = 0; i < lh->num_nodes; i++) {
105
9.21M
        n = lh->b[i];
106
12.0M
        while (n != NULL) {
107
2.80M
            nn = n->next;
108
2.80M
            OPENSSL_free(n);
109
2.80M
            n = nn;
110
2.80M
        }
111
9.21M
        lh->b[i] = NULL;
112
9.21M
    }
113
114
812k
    lh->num_items = 0;
115
812k
}
116
117
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
118
12.0M
{
119
12.0M
    unsigned long hash;
120
12.0M
    OPENSSL_LH_NODE *nn, **rn;
121
12.0M
    void *ret;
122
123
12.0M
    lh->error = 0;
124
12.0M
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
125
0
        return NULL; /* 'lh->error++' already done in 'expand' */
126
127
12.0M
    rn = getrn(lh, data, &hash);
128
129
12.0M
    if (*rn == NULL) {
130
10.9M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
131
0
            lh->error++;
132
0
            return NULL;
133
0
        }
134
10.9M
        nn->data = data;
135
10.9M
        nn->next = NULL;
136
10.9M
        nn->hash = hash;
137
10.9M
        *rn = nn;
138
10.9M
        ret = NULL;
139
10.9M
        lh->num_items++;
140
10.9M
    } else { /* replace same key */
141
1.05M
        ret = (*rn)->data;
142
1.05M
        (*rn)->data = data;
143
1.05M
    }
144
12.0M
    return ret;
145
12.0M
}
146
147
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
148
8.03M
{
149
8.03M
    unsigned long hash;
150
8.03M
    OPENSSL_LH_NODE *nn, **rn;
151
8.03M
    void *ret;
152
153
8.03M
    lh->error = 0;
154
8.03M
    rn = getrn(lh, data, &hash);
155
156
8.03M
    if (*rn == NULL) {
157
0
        return NULL;
158
8.03M
    } else {
159
8.03M
        nn = *rn;
160
8.03M
        *rn = nn->next;
161
8.03M
        ret = nn->data;
162
8.03M
        OPENSSL_free(nn);
163
8.03M
    }
164
165
8.03M
    lh->num_items--;
166
8.03M
    if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
167
1.82M
        contract(lh);
168
169
8.03M
    return ret;
170
8.03M
}
171
172
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
173
315M
{
174
315M
    unsigned long hash;
175
315M
    OPENSSL_LH_NODE **rn;
176
177
315M
    if (lh->error != 0)
178
0
        lh->error = 0;
179
180
315M
    rn = getrn(lh, data, &hash);
181
182
315M
    return *rn == NULL ? NULL : (*rn)->data;
183
315M
}
184
185
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
186
    OPENSSL_LH_DOALL_FUNC_THUNK wfunc,
187
    OPENSSL_LH_DOALL_FUNC func,
188
    OPENSSL_LH_DOALL_FUNCARG func_arg,
189
    OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg,
190
    void *arg)
191
22.0M
{
192
22.0M
    int i;
193
22.0M
    OPENSSL_LH_NODE *a, *n;
194
195
22.0M
    if (lh == NULL)
196
0
        return;
197
198
    /*
199
     * reverse the order so we search from 'top to bottom' We were having
200
     * memory leaks otherwise
201
     */
202
4.65G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
203
4.62G
        a = lh->b[i];
204
13.7G
        while (a != NULL) {
205
9.17G
            n = a->next;
206
9.17G
            if (use_arg)
207
9.17G
                wfunc_arg(a->data, arg, func_arg);
208
881k
            else
209
881k
                wfunc(a->data, func);
210
9.17G
            a = n;
211
9.17G
        }
212
4.62G
    }
213
22.0M
}
214
215
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
216
120k
{
217
120k
    if (lh == NULL)
218
0
        return;
219
220
120k
    doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL,
221
120k
        (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL);
222
120k
}
223
224
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh,
225
    OPENSSL_LH_DOALL_FUNCARG func, void *arg)
226
15.5M
{
227
15.5M
    if (lh == NULL)
228
0
        return;
229
230
15.5M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
231
15.5M
        (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg);
232
15.5M
}
233
234
void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh,
235
    OPENSSL_LH_DOALL_FUNCARG_THUNK daaw,
236
    OPENSSL_LH_DOALL_FUNCARG fn, void *arg)
237
215k
{
238
215k
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
239
215k
        (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg);
240
215k
}
241
242
static int expand(OPENSSL_LHASH *lh)
243
4.58M
{
244
4.58M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
245
4.58M
    unsigned int p, pmax, nni, j;
246
4.58M
    unsigned long hash;
247
248
4.58M
    nni = lh->num_alloc_nodes;
249
4.58M
    p = lh->p;
250
4.58M
    pmax = lh->pmax;
251
4.58M
    if (p + 1 >= pmax) {
252
53.2k
        j = nni * 2;
253
53.2k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
254
53.2k
        if (n == NULL) {
255
0
            lh->error++;
256
0
            return 0;
257
0
        }
258
53.2k
        lh->b = n;
259
53.2k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
260
53.2k
        lh->pmax = nni;
261
53.2k
        lh->num_alloc_nodes = j;
262
53.2k
        lh->p = 0;
263
4.53M
    } else {
264
4.53M
        lh->p++;
265
4.53M
    }
266
267
4.58M
    lh->num_nodes++;
268
4.58M
    n1 = &(lh->b[p]);
269
4.58M
    n2 = &(lh->b[p + pmax]);
270
4.58M
    *n2 = NULL;
271
272
17.8M
    for (np = *n1; np != NULL;) {
273
13.3M
        hash = np->hash;
274
13.3M
        if ((hash % nni) != p) { /* move it */
275
5.46M
            *n1 = (*n1)->next;
276
5.46M
            np->next = *n2;
277
5.46M
            *n2 = np;
278
5.46M
        } else
279
7.83M
            n1 = &((*n1)->next);
280
13.3M
        np = *n1;
281
13.3M
    }
282
283
4.58M
    return 1;
284
4.58M
}
285
286
static void contract(OPENSSL_LHASH *lh)
287
1.82M
{
288
1.82M
    OPENSSL_LH_NODE **n, *n1, *np;
289
290
1.82M
    np = lh->b[lh->p + lh->pmax - 1];
291
1.82M
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
292
1.82M
    if (lh->p == 0) {
293
20.7k
        n = OPENSSL_realloc(lh->b,
294
20.7k
            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
295
20.7k
        if (n == NULL) {
296
            /* fputs("realloc error in lhash", stderr); */
297
0
            lh->error++;
298
20.7k
        } else {
299
20.7k
            lh->b = n;
300
20.7k
        }
301
20.7k
        lh->num_alloc_nodes /= 2;
302
20.7k
        lh->pmax /= 2;
303
20.7k
        lh->p = lh->pmax - 1;
304
20.7k
    } else
305
1.80M
        lh->p--;
306
307
1.82M
    lh->num_nodes--;
308
309
1.82M
    n1 = lh->b[(int)lh->p];
310
1.82M
    if (n1 == NULL)
311
837k
        lh->b[(int)lh->p] = np;
312
983k
    else {
313
2.64M
        while (n1->next != NULL)
314
1.66M
            n1 = n1->next;
315
983k
        n1->next = np;
316
983k
    }
317
1.82M
}
318
319
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
320
    const void *data, unsigned long *rhash)
321
110M
{
322
110M
    OPENSSL_LH_NODE **ret, *n1;
323
110M
    unsigned long hash, nn;
324
325
110M
    if (lh->hashw != NULL)
326
110M
        hash = lh->hashw(data, lh->hash);
327
8.20k
    else
328
8.20k
        hash = lh->hash(data);
329
330
110M
    *rhash = hash;
331
332
110M
    nn = hash % lh->pmax;
333
110M
    if (nn < lh->p)
334
25.0M
        nn = hash % lh->num_alloc_nodes;
335
336
110M
    ret = &(lh->b[(int)nn]);
337
3.81G
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
338
3.75G
        if (n1->hash != hash) {
339
3.69G
            ret = &(n1->next);
340
3.69G
            continue;
341
3.69G
        }
342
343
51.4M
        if (lh->compw != NULL) {
344
51.4M
            if (lh->compw(n1->data, data, lh->comp) == 0)
345
51.1M
                break;
346
51.4M
        } else {
347
21
            if (lh->comp(n1->data, data) == 0)
348
21
                break;
349
21
        }
350
349k
        ret = &(n1->next);
351
349k
    }
352
110M
    return ret;
353
110M
}
354
355
/*
356
 * The following hash seems to work very well on normal text strings no
357
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
358
 * as good as MD5, but still good.
359
 */
360
unsigned long OPENSSL_LH_strhash(const char *c)
361
34.8M
{
362
34.8M
    unsigned long ret = 0;
363
34.8M
    long n;
364
34.8M
    unsigned long v;
365
34.8M
    int r;
366
367
34.8M
    if ((c == NULL) || (*c == '\0'))
368
21.0M
        return ret;
369
370
13.8M
    n = 0x100;
371
734M
    while (*c) {
372
720M
        v = n | (*c);
373
720M
        n += 0x100;
374
720M
        r = (int)((v >> 2) ^ v) & 0x0f;
375
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
376
720M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
377
720M
        ret &= 0xFFFFFFFFL;
378
720M
        ret ^= v * v;
379
720M
        c++;
380
720M
    }
381
13.8M
    return (ret >> 16) ^ ret;
382
34.8M
}
383
384
/*
385
 * Case insensitive string hashing.
386
 *
387
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
388
 * The major side effect on non-alpha characters is mapping the symbols and
389
 * digits into the control character range (which should be harmless).
390
 * The duplication (with respect to the hash value) of printable characters
391
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
392
 * respectively (which seems tolerable).
393
 *
394
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
395
 * characters.  The only duplication is '0' mapping to '^', which is better
396
 * than for ASCII.
397
 */
398
unsigned long ossl_lh_strcasehash(const char *c)
399
228M
{
400
228M
    unsigned long ret = 0;
401
228M
    long n;
402
228M
    unsigned long v;
403
228M
    int r;
404
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
405
    const long int case_adjust = ~0x40;
406
#else
407
228M
    const long int case_adjust = ~0x20;
408
228M
#endif
409
410
228M
    if (c == NULL || *c == '\0')
411
1.17k
        return ret;
412
413
1.47G
    for (n = 0x100; *c != '\0'; n += 0x100) {
414
1.24G
        v = n | (case_adjust & *c);
415
1.24G
        r = (int)((v >> 2) ^ v) & 0x0f;
416
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
417
1.24G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
418
1.24G
        ret &= 0xFFFFFFFFL;
419
1.24G
        ret ^= v * v;
420
1.24G
        c++;
421
1.24G
    }
422
228M
    return (ret >> 16) ^ ret;
423
228M
}
424
425
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
426
1.62M
{
427
1.62M
    return lh ? lh->num_items : 0;
428
1.62M
}
429
430
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
431
172k
{
432
172k
    return lh->down_load;
433
172k
}
434
435
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
436
610k
{
437
610k
    lh->down_load = down_load;
438
610k
}
439
440
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
441
7.97M
{
442
7.97M
    return lh->error;
443
7.97M
}