Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/lhash/lhash.c
Line
Count
Source
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
9.67M
#define MIN_NODES 16
40
805k
#define UP_LOAD (2 * LH_LOAD_MULT) /* load times 256 (default 2) */
41
805k
#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
805k
{
49
805k
    OPENSSL_LHASH *ret;
50
51
805k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
52
        /*
53
         * Do not set the error code, because the ERR code uses LHASH
54
         * and we want to avoid possible endless error loop.
55
         * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
56
         */
57
0
        return NULL;
58
0
    }
59
805k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
60
0
        goto err;
61
805k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62
805k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63
805k
    ret->num_nodes = MIN_NODES / 2;
64
805k
    ret->num_alloc_nodes = MIN_NODES;
65
805k
    ret->pmax = MIN_NODES / 2;
66
805k
    ret->up_load = UP_LOAD;
67
805k
    ret->down_load = DOWN_LOAD;
68
805k
    return ret;
69
70
0
err:
71
0
    OPENSSL_free(ret->b);
72
0
    OPENSSL_free(ret);
73
0
    return NULL;
74
805k
}
75
76
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
77
797k
{
78
797k
    if (lh == NULL)
79
220
        return;
80
81
797k
    OPENSSL_LH_flush(lh);
82
797k
    OPENSSL_free(lh->b);
83
797k
    OPENSSL_free(lh);
84
797k
}
85
86
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
87
802k
{
88
802k
    unsigned int i;
89
802k
    OPENSSL_LH_NODE *n, *nn;
90
91
802k
    if (lh == NULL)
92
0
        return;
93
94
9.52M
    for (i = 0; i < lh->num_nodes; i++) {
95
8.72M
        n = lh->b[i];
96
11.0M
        while (n != NULL) {
97
2.33M
            nn = n->next;
98
2.33M
            OPENSSL_free(n);
99
2.33M
            n = nn;
100
2.33M
        }
101
8.72M
        lh->b[i] = NULL;
102
8.72M
    }
103
104
802k
    lh->num_items = 0;
105
802k
}
106
107
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
108
10.7M
{
109
10.7M
    unsigned long hash;
110
10.7M
    OPENSSL_LH_NODE *nn, **rn;
111
10.7M
    void *ret;
112
113
10.7M
    lh->error = 0;
114
10.7M
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
115
0
        return NULL; /* 'lh->error++' already done in 'expand' */
116
117
10.7M
    rn = getrn(lh, data, &hash);
118
119
10.7M
    if (*rn == NULL) {
120
9.70M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
121
0
            lh->error++;
122
0
            return NULL;
123
0
        }
124
9.70M
        nn->data = data;
125
9.70M
        nn->next = NULL;
126
9.70M
        nn->hash = hash;
127
9.70M
        *rn = nn;
128
9.70M
        ret = NULL;
129
9.70M
        lh->num_items++;
130
9.70M
    } else { /* replace same key */
131
1.03M
        ret = (*rn)->data;
132
1.03M
        (*rn)->data = data;
133
1.03M
    }
134
10.7M
    return ret;
135
10.7M
}
136
137
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
138
7.26M
{
139
7.26M
    unsigned long hash;
140
7.26M
    OPENSSL_LH_NODE *nn, **rn;
141
7.26M
    void *ret;
142
143
7.26M
    lh->error = 0;
144
7.26M
    rn = getrn(lh, data, &hash);
145
146
7.26M
    if (*rn == NULL) {
147
0
        return NULL;
148
7.26M
    } else {
149
7.26M
        nn = *rn;
150
7.26M
        *rn = nn->next;
151
7.26M
        ret = nn->data;
152
7.26M
        OPENSSL_free(nn);
153
7.26M
    }
154
155
7.26M
    lh->num_items--;
156
7.26M
    if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
157
1.68M
        contract(lh);
158
159
7.26M
    return ret;
160
7.26M
}
161
162
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
163
330M
{
164
330M
    unsigned long hash;
165
330M
    OPENSSL_LH_NODE **rn;
166
167
330M
    if (lh->error != 0)
168
0
        lh->error = 0;
169
170
330M
    rn = getrn(lh, data, &hash);
171
172
330M
    return *rn == NULL ? NULL : (*rn)->data;
173
330M
}
174
175
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
176
    OPENSSL_LH_DOALL_FUNC func,
177
    OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
178
21.4M
{
179
21.4M
    int i;
180
21.4M
    OPENSSL_LH_NODE *a, *n;
181
182
21.4M
    if (lh == NULL)
183
0
        return;
184
185
    /*
186
     * reverse the order so we search from 'top to bottom' We were having
187
     * memory leaks otherwise
188
     */
189
2.83G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
190
2.81G
        a = lh->b[i];
191
8.36G
        while (a != NULL) {
192
5.54G
            n = a->next;
193
5.54G
            if (use_arg)
194
5.54G
                func_arg(a->data, arg);
195
665k
            else
196
665k
                func(a->data);
197
5.54G
            a = n;
198
5.54G
        }
199
2.81G
    }
200
21.4M
}
201
202
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
203
4.34k
{
204
4.34k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
205
4.34k
}
206
207
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
208
6.61M
{
209
6.61M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
210
6.61M
}
211
212
static int expand(OPENSSL_LHASH *lh)
213
4.03M
{
214
4.03M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
215
4.03M
    unsigned int p, pmax, nni, j;
216
4.03M
    unsigned long hash;
217
218
4.03M
    nni = lh->num_alloc_nodes;
219
4.03M
    p = lh->p;
220
4.03M
    pmax = lh->pmax;
221
4.03M
    if (p + 1 >= pmax) {
222
48.5k
        j = nni * 2;
223
48.5k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
224
48.5k
        if (n == NULL) {
225
0
            lh->error++;
226
0
            return 0;
227
0
        }
228
48.5k
        lh->b = n;
229
48.5k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
230
48.5k
        lh->pmax = nni;
231
48.5k
        lh->num_alloc_nodes = j;
232
48.5k
        lh->p = 0;
233
3.98M
    } else {
234
3.98M
        lh->p++;
235
3.98M
    }
236
237
4.03M
    lh->num_nodes++;
238
4.03M
    n1 = &(lh->b[p]);
239
4.03M
    n2 = &(lh->b[p + pmax]);
240
4.03M
    *n2 = NULL;
241
242
15.7M
    for (np = *n1; np != NULL;) {
243
11.7M
        hash = np->hash;
244
11.7M
        if ((hash % nni) != p) { /* move it */
245
4.74M
            *n1 = (*n1)->next;
246
4.74M
            np->next = *n2;
247
4.74M
            *n2 = np;
248
4.74M
        } else
249
6.97M
            n1 = &((*n1)->next);
250
11.7M
        np = *n1;
251
11.7M
    }
252
253
4.03M
    return 1;
254
4.03M
}
255
256
static void contract(OPENSSL_LHASH *lh)
257
1.68M
{
258
1.68M
    OPENSSL_LH_NODE **n, *n1, *np;
259
260
1.68M
    np = lh->b[lh->p + lh->pmax - 1];
261
1.68M
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
262
1.68M
    if (lh->p == 0) {
263
18.8k
        n = OPENSSL_realloc(lh->b,
264
18.8k
            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
265
18.8k
        if (n == NULL) {
266
            /* fputs("realloc error in lhash",stderr); */
267
0
            lh->error++;
268
18.8k
        } else {
269
18.8k
            lh->b = n;
270
18.8k
        }
271
18.8k
        lh->num_alloc_nodes /= 2;
272
18.8k
        lh->pmax /= 2;
273
18.8k
        lh->p = lh->pmax - 1;
274
18.8k
    } else
275
1.66M
        lh->p--;
276
277
1.68M
    lh->num_nodes--;
278
279
1.68M
    n1 = lh->b[(int)lh->p];
280
1.68M
    if (n1 == NULL)
281
786k
        lh->b[(int)lh->p] = np;
282
895k
    else {
283
2.43M
        while (n1->next != NULL)
284
1.54M
            n1 = n1->next;
285
895k
        n1->next = np;
286
895k
    }
287
1.68M
}
288
289
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
290
    const void *data, unsigned long *rhash)
291
240M
{
292
240M
    OPENSSL_LH_NODE **ret, *n1;
293
240M
    unsigned long hash, nn;
294
240M
    OPENSSL_LH_COMPFUNC cf;
295
296
240M
    hash = (*(lh->hash))(data);
297
240M
    *rhash = hash;
298
299
240M
    nn = hash % lh->pmax;
300
240M
    if (nn < lh->p)
301
157M
        nn = hash % lh->num_alloc_nodes;
302
303
240M
    cf = lh->comp;
304
240M
    ret = &(lh->b[(int)nn]);
305
906M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
306
866M
        if (n1->hash != hash) {
307
665M
            ret = &(n1->next);
308
665M
            continue;
309
665M
        }
310
201M
        if (cf(n1->data, data) == 0)
311
201M
            break;
312
40.5k
        ret = &(n1->next);
313
40.5k
    }
314
240M
    return ret;
315
240M
}
316
317
/*
318
 * The following hash seems to work very well on normal text strings no
319
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
320
 * as good as MD5, but still good.
321
 */
322
unsigned long OPENSSL_LH_strhash(const char *c)
323
38.1M
{
324
38.1M
    unsigned long ret = 0;
325
38.1M
    long n;
326
38.1M
    unsigned long v;
327
38.1M
    int r;
328
329
38.1M
    if ((c == NULL) || (*c == '\0'))
330
24.4M
        return ret;
331
332
13.6M
    n = 0x100;
333
699M
    while (*c) {
334
685M
        v = n | (*c);
335
685M
        n += 0x100;
336
685M
        r = (int)((v >> 2) ^ v) & 0x0f;
337
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
338
685M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
339
685M
        ret &= 0xFFFFFFFFL;
340
685M
        ret ^= v * v;
341
685M
        c++;
342
685M
    }
343
13.6M
    return (ret >> 16) ^ ret;
344
38.1M
}
345
346
unsigned long ossl_lh_strcasehash(const char *c)
347
243M
{
348
243M
    unsigned long ret = 0;
349
243M
    long n;
350
243M
    unsigned long v;
351
243M
    int r;
352
353
243M
    if (c == NULL || *c == '\0')
354
1.14k
        return ret;
355
356
1.56G
    for (n = 0x100; *c != '\0'; n += 0x100) {
357
1.32G
        v = n | ossl_tolower(*c);
358
1.32G
        r = (int)((v >> 2) ^ v) & 0x0f;
359
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
360
1.32G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
361
1.32G
        ret &= 0xFFFFFFFFL;
362
1.32G
        ret ^= v * v;
363
1.32G
        c++;
364
1.32G
    }
365
243M
    return (ret >> 16) ^ ret;
366
243M
}
367
368
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
369
1.70M
{
370
1.70M
    return lh ? lh->num_items : 0;
371
1.70M
}
372
373
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
374
169k
{
375
169k
    return lh->down_load;
376
169k
}
377
378
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
379
548k
{
380
548k
    lh->down_load = down_load;
381
548k
}
382
383
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
384
6.94M
{
385
6.94M
    return lh->error;
386
6.94M
}