Coverage Report

Created: 2025-11-16 06:40

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