Coverage Report

Created: 2026-04-01 06:39

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
10.3M
#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_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
48
810k
{
49
810k
    OPENSSL_LHASH *ret;
50
51
810k
    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
810k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
60
0
        goto err;
61
810k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62
810k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63
810k
    ret->num_nodes = MIN_NODES / 2;
64
810k
    ret->num_alloc_nodes = MIN_NODES;
65
810k
    ret->pmax = MIN_NODES / 2;
66
810k
    ret->up_load = UP_LOAD;
67
810k
    ret->down_load = DOWN_LOAD;
68
810k
    return ret;
69
70
0
err:
71
0
    OPENSSL_free(ret->b);
72
0
    OPENSSL_free(ret);
73
0
    return NULL;
74
810k
}
75
76
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
77
841k
{
78
841k
    if (lh == NULL)
79
33.4k
        return;
80
81
808k
    OPENSSL_LH_flush(lh);
82
808k
    OPENSSL_free(lh->b);
83
808k
    OPENSSL_free(lh);
84
808k
}
85
86
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
87
812k
{
88
812k
    unsigned int i;
89
812k
    OPENSSL_LH_NODE *n, *nn;
90
91
812k
    if (lh == NULL)
92
0
        return;
93
94
9.91M
    for (i = 0; i < lh->num_nodes; i++) {
95
9.10M
        n = lh->b[i];
96
11.7M
        while (n != NULL) {
97
2.63M
            nn = n->next;
98
2.63M
            OPENSSL_free(n);
99
2.63M
            n = nn;
100
2.63M
        }
101
9.10M
        lh->b[i] = NULL;
102
9.10M
    }
103
104
812k
    lh->num_items = 0;
105
812k
}
106
107
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
108
11.6M
{
109
11.6M
    unsigned long hash;
110
11.6M
    OPENSSL_LH_NODE *nn, **rn;
111
11.6M
    void *ret;
112
113
11.6M
    lh->error = 0;
114
11.6M
    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
11.6M
    rn = getrn(lh, data, &hash);
118
119
11.6M
    if (*rn == NULL) {
120
10.6M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
121
0
            lh->error++;
122
0
            return NULL;
123
0
        }
124
10.6M
        nn->data = data;
125
10.6M
        nn->next = NULL;
126
10.6M
        nn->hash = hash;
127
10.6M
        *rn = nn;
128
10.6M
        ret = NULL;
129
10.6M
        lh->num_items++;
130
10.6M
    } else { /* replace same key */
131
1.02M
        ret = (*rn)->data;
132
1.02M
        (*rn)->data = data;
133
1.02M
    }
134
11.6M
    return ret;
135
11.6M
}
136
137
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
138
7.92M
{
139
7.92M
    unsigned long hash;
140
7.92M
    OPENSSL_LH_NODE *nn, **rn;
141
7.92M
    void *ret;
142
143
7.92M
    lh->error = 0;
144
7.92M
    rn = getrn(lh, data, &hash);
145
146
7.92M
    if (*rn == NULL) {
147
0
        return NULL;
148
7.92M
    } else {
149
7.92M
        nn = *rn;
150
7.92M
        *rn = nn->next;
151
7.92M
        ret = nn->data;
152
7.92M
        OPENSSL_free(nn);
153
7.92M
    }
154
155
7.92M
    lh->num_items--;
156
7.92M
    if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
157
1.79M
        contract(lh);
158
159
7.92M
    return ret;
160
7.92M
}
161
162
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
163
322M
{
164
322M
    unsigned long hash;
165
322M
    OPENSSL_LH_NODE **rn;
166
167
322M
    if (lh->error != 0)
168
0
        lh->error = 0;
169
170
322M
    rn = getrn(lh, data, &hash);
171
172
322M
    return *rn == NULL ? NULL : (*rn)->data;
173
322M
}
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.8M
{
179
21.8M
    int i;
180
21.8M
    OPENSSL_LH_NODE *a, *n;
181
182
21.8M
    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
3.87G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
190
3.85G
        a = lh->b[i];
191
11.4G
        while (a != NULL) {
192
7.61G
            n = a->next;
193
7.61G
            if (use_arg)
194
7.61G
                func_arg(a->data, arg);
195
801k
            else
196
801k
                func(a->data);
197
7.61G
            a = n;
198
7.61G
        }
199
3.85G
    }
200
21.8M
}
201
202
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
203
2.40k
{
204
2.40k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
205
2.40k
}
206
207
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
208
6.36M
{
209
6.36M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
210
6.36M
}
211
212
static int expand(OPENSSL_LHASH *lh)
213
4.45M
{
214
4.45M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
215
4.45M
    unsigned int p, pmax, nni, j;
216
4.45M
    unsigned long hash;
217
218
4.45M
    nni = lh->num_alloc_nodes;
219
4.45M
    p = lh->p;
220
4.45M
    pmax = lh->pmax;
221
4.45M
    if (p + 1 >= pmax) {
222
51.3k
        j = nni * 2;
223
51.3k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
224
51.3k
        if (n == NULL) {
225
0
            lh->error++;
226
0
            return 0;
227
0
        }
228
51.3k
        lh->b = n;
229
51.3k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
230
51.3k
        lh->pmax = nni;
231
51.3k
        lh->num_alloc_nodes = j;
232
51.3k
        lh->p = 0;
233
4.39M
    } else {
234
4.39M
        lh->p++;
235
4.39M
    }
236
237
4.45M
    lh->num_nodes++;
238
4.45M
    n1 = &(lh->b[p]);
239
4.45M
    n2 = &(lh->b[p + pmax]);
240
4.45M
    *n2 = NULL;
241
242
17.3M
    for (np = *n1; np != NULL;) {
243
12.9M
        hash = np->hash;
244
12.9M
        if ((hash % nni) != p) { /* move it */
245
5.29M
            *n1 = (*n1)->next;
246
5.29M
            np->next = *n2;
247
5.29M
            *n2 = np;
248
5.29M
        } else
249
7.63M
            n1 = &((*n1)->next);
250
12.9M
        np = *n1;
251
12.9M
    }
252
253
4.45M
    return 1;
254
4.45M
}
255
256
static void contract(OPENSSL_LHASH *lh)
257
1.79M
{
258
1.79M
    OPENSSL_LH_NODE **n, *n1, *np;
259
260
1.79M
    np = lh->b[lh->p + lh->pmax - 1];
261
1.79M
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
262
1.79M
    if (lh->p == 0) {
263
19.2k
        n = OPENSSL_realloc(lh->b,
264
19.2k
            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
265
19.2k
        if (n == NULL) {
266
            /* fputs("realloc error in lhash",stderr); */
267
0
            lh->error++;
268
19.2k
        } else {
269
19.2k
            lh->b = n;
270
19.2k
        }
271
19.2k
        lh->num_alloc_nodes /= 2;
272
19.2k
        lh->pmax /= 2;
273
19.2k
        lh->p = lh->pmax - 1;
274
19.2k
    } else
275
1.77M
        lh->p--;
276
277
1.79M
    lh->num_nodes--;
278
279
1.79M
    n1 = lh->b[(int)lh->p];
280
1.79M
    if (n1 == NULL)
281
826k
        lh->b[(int)lh->p] = np;
282
967k
    else {
283
2.61M
        while (n1->next != NULL)
284
1.64M
            n1 = n1->next;
285
967k
        n1->next = np;
286
967k
    }
287
1.79M
}
288
289
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
290
    const void *data, unsigned long *rhash)
291
231M
{
292
231M
    OPENSSL_LH_NODE **ret, *n1;
293
231M
    unsigned long hash, nn;
294
231M
    OPENSSL_LH_COMPFUNC cf;
295
296
231M
    hash = (*(lh->hash))(data);
297
231M
    *rhash = hash;
298
299
231M
    nn = hash % lh->pmax;
300
231M
    if (nn < lh->p)
301
152M
        nn = hash % lh->num_alloc_nodes;
302
303
231M
    cf = lh->comp;
304
231M
    ret = &(lh->b[(int)nn]);
305
875M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
306
837M
        if (n1->hash != hash) {
307
643M
            ret = &(n1->next);
308
643M
            continue;
309
643M
        }
310
193M
        if (cf(n1->data, data) == 0)
311
193M
            break;
312
32.8k
        ret = &(n1->next);
313
32.8k
    }
314
231M
    return ret;
315
231M
}
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
34.6M
{
324
34.6M
    unsigned long ret = 0;
325
34.6M
    long n;
326
34.6M
    unsigned long v;
327
34.6M
    int r;
328
329
34.6M
    if ((c == NULL) || (*c == '\0'))
330
21.0M
        return ret;
331
332
13.5M
    n = 0x100;
333
680M
    while (*c) {
334
666M
        v = n | (*c);
335
666M
        n += 0x100;
336
666M
        r = (int)((v >> 2) ^ v) & 0x0f;
337
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
338
666M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
339
666M
        ret &= 0xFFFFFFFFL;
340
666M
        ret ^= v * v;
341
666M
        c++;
342
666M
    }
343
13.5M
    return (ret >> 16) ^ ret;
344
34.6M
}
345
346
unsigned long ossl_lh_strcasehash(const char *c)
347
235M
{
348
235M
    unsigned long ret = 0;
349
235M
    long n;
350
235M
    unsigned long v;
351
235M
    int r;
352
353
235M
    if (c == NULL || *c == '\0')
354
1.16k
        return ret;
355
356
1.52G
    for (n = 0x100; *c != '\0'; n += 0x100) {
357
1.28G
        v = n | ossl_tolower(*c);
358
1.28G
        r = (int)((v >> 2) ^ v) & 0x0f;
359
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
360
1.28G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
361
1.28G
        ret &= 0xFFFFFFFFL;
362
1.28G
        ret ^= v * v;
363
1.28G
        c++;
364
1.28G
    }
365
235M
    return (ret >> 16) ^ ret;
366
235M
}
367
368
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
369
1.65M
{
370
1.65M
    return lh ? lh->num_items : 0;
371
1.65M
}
372
373
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
374
171k
{
375
171k
    return lh->down_load;
376
171k
}
377
378
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
379
608k
{
380
608k
    lh->down_load = down_load;
381
608k
}
382
383
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
384
7.71M
{
385
7.71M
    return lh->error;
386
7.71M
}