Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl32/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
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_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
48
711k
{
49
711k
    OPENSSL_LHASH *ret;
50
51
711k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
711k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
711k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
711k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
711k
    ret->num_nodes = MIN_NODES / 2;
58
711k
    ret->num_alloc_nodes = MIN_NODES;
59
711k
    ret->pmax = MIN_NODES / 2;
60
711k
    ret->up_load = UP_LOAD;
61
711k
    ret->down_load = DOWN_LOAD;
62
711k
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
711k
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
709k
{
72
709k
    if (lh == NULL)
73
252
        return;
74
75
709k
    OPENSSL_LH_flush(lh);
76
709k
    OPENSSL_free(lh->b);
77
709k
    OPENSSL_free(lh);
78
709k
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
713k
{
82
713k
    unsigned int i;
83
713k
    OPENSSL_LH_NODE *n, *nn;
84
85
713k
    if (lh == NULL)
86
0
        return;
87
88
8.60M
    for (i = 0; i < lh->num_nodes; i++) {
89
7.89M
        n = lh->b[i];
90
10.1M
        while (n != NULL) {
91
2.27M
            nn = n->next;
92
2.27M
            OPENSSL_free(n);
93
2.27M
            n = nn;
94
2.27M
        }
95
7.89M
        lh->b[i] = NULL;
96
7.89M
    }
97
98
713k
    lh->num_items = 0;
99
713k
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
9.76M
{
103
9.76M
    unsigned long hash;
104
9.76M
    OPENSSL_LH_NODE *nn, **rn;
105
9.76M
    void *ret;
106
107
9.76M
    lh->error = 0;
108
9.76M
    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.76M
    rn = getrn(lh, data, &hash);
112
113
9.76M
    if (*rn == NULL) {
114
8.72M
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
8.72M
        nn->data = data;
119
8.72M
        nn->next = NULL;
120
8.72M
        nn->hash = hash;
121
8.72M
        *rn = nn;
122
8.72M
        ret = NULL;
123
8.72M
        lh->num_items++;
124
8.72M
    } else {                    /* replace same key */
125
1.03M
        ret = (*rn)->data;
126
1.03M
        (*rn)->data = data;
127
1.03M
    }
128
9.76M
    return ret;
129
9.76M
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
6.44M
{
133
6.44M
    unsigned long hash;
134
6.44M
    OPENSSL_LH_NODE *nn, **rn;
135
6.44M
    void *ret;
136
137
6.44M
    lh->error = 0;
138
6.44M
    rn = getrn(lh, data, &hash);
139
140
6.44M
    if (*rn == NULL) {
141
164
        return NULL;
142
6.44M
    } else {
143
6.44M
        nn = *rn;
144
6.44M
        *rn = nn->next;
145
6.44M
        ret = nn->data;
146
6.44M
        OPENSSL_free(nn);
147
6.44M
    }
148
149
6.44M
    lh->num_items--;
150
6.44M
    if ((lh->num_nodes > MIN_NODES) &&
151
5.76M
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
1.45M
        contract(lh);
153
154
6.44M
    return ret;
155
6.44M
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
327M
{
159
327M
    unsigned long hash;
160
327M
    OPENSSL_LH_NODE **rn;
161
162
327M
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
327M
    rn = getrn(lh, data, &hash);
166
167
327M
    return *rn == NULL ? NULL : (*rn)->data;
168
327M
}
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
19.4M
{
174
19.4M
    int i;
175
19.4M
    OPENSSL_LH_NODE *a, *n;
176
177
19.4M
    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
3.03G
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
3.01G
        a = lh->b[i];
186
8.99G
        while (a != NULL) {
187
5.97G
            n = a->next;
188
5.97G
            if (use_arg)
189
5.97G
                func_arg(a->data, arg);
190
636k
            else
191
636k
                func(a->data);
192
5.97G
            a = n;
193
5.97G
        }
194
3.01G
    }
195
19.4M
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
7.71k
{
199
7.71k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
7.71k
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
7.37M
{
204
7.37M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
7.37M
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
3.64M
{
209
3.64M
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
3.64M
    unsigned int p, pmax, nni, j;
211
3.64M
    unsigned long hash;
212
213
3.64M
    nni = lh->num_alloc_nodes;
214
3.64M
    p = lh->p;
215
3.64M
    pmax = lh->pmax;
216
3.64M
    if (p + 1 >= pmax) {
217
41.4k
        j = nni * 2;
218
41.4k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
41.4k
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
41.4k
        lh->b = n;
224
41.4k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
41.4k
        lh->pmax = nni;
226
41.4k
        lh->num_alloc_nodes = j;
227
41.4k
        lh->p = 0;
228
3.60M
    } else {
229
3.60M
        lh->p++;
230
3.60M
    }
231
232
3.64M
    lh->num_nodes++;
233
3.64M
    n1 = &(lh->b[p]);
234
3.64M
    n2 = &(lh->b[p + pmax]);
235
3.64M
    *n2 = NULL;
236
237
14.1M
    for (np = *n1; np != NULL;) {
238
10.5M
        hash = np->hash;
239
10.5M
        if ((hash % nni) != p) { /* move it */
240
4.25M
            *n1 = (*n1)->next;
241
4.25M
            np->next = *n2;
242
4.25M
            *n2 = np;
243
4.25M
        } else
244
6.29M
            n1 = &((*n1)->next);
245
10.5M
        np = *n1;
246
10.5M
    }
247
248
3.64M
    return 1;
249
3.64M
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
1.45M
{
253
1.45M
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
1.45M
    np = lh->b[lh->p + lh->pmax - 1];
256
1.45M
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
1.45M
    if (lh->p == 0) {
258
14.6k
        n = OPENSSL_realloc(lh->b,
259
14.6k
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
260
14.6k
        if (n == NULL) {
261
            /* fputs("realloc error in lhash", stderr); */
262
0
            lh->error++;
263
14.6k
        } else {
264
14.6k
            lh->b = n;
265
14.6k
        }
266
14.6k
        lh->num_alloc_nodes /= 2;
267
14.6k
        lh->pmax /= 2;
268
14.6k
        lh->p = lh->pmax - 1;
269
14.6k
    } else
270
1.43M
        lh->p--;
271
272
1.45M
    lh->num_nodes--;
273
274
1.45M
    n1 = lh->b[(int)lh->p];
275
1.45M
    if (n1 == NULL)
276
702k
        lh->b[(int)lh->p] = np;
277
751k
    else {
278
2.12M
        while (n1->next != NULL)
279
1.36M
            n1 = n1->next;
280
751k
        n1->next = np;
281
751k
    }
282
1.45M
}
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
876M
        if (n1->hash != hash) {
302
667M
            ret = &(n1->next);
303
667M
            continue;
304
667M
        }
305
208M
        if (cf(n1->data, data) == 0)
306
208M
            break;
307
89.1k
        ret = &(n1->next);
308
89.1k
    }
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
36.0M
{
319
36.0M
    unsigned long ret = 0;
320
36.0M
    long n;
321
36.0M
    unsigned long v;
322
36.0M
    int r;
323
324
36.0M
    if ((c == NULL) || (*c == '\0'))
325
23.1M
        return ret;
326
327
12.8M
    n = 0x100;
328
632M
    while (*c) {
329
619M
        v = n | (*c);
330
619M
        n += 0x100;
331
619M
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
619M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
619M
        ret &= 0xFFFFFFFFL;
335
619M
        ret ^= v * v;
336
619M
        c++;
337
619M
    }
338
12.8M
    return (ret >> 16) ^ ret;
339
36.0M
}
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
244M
{
357
244M
    unsigned long ret = 0;
358
244M
    long n;
359
244M
    unsigned long v;
360
244M
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
244M
    const long int case_adjust = ~0x20;
365
244M
#endif
366
367
244M
    if (c == NULL || *c == '\0')
368
998
        return ret;
369
370
1.56G
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
1.31G
        v = n | (case_adjust & *c);
372
1.31G
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
1.31G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
1.31G
        ret &= 0xFFFFFFFFL;
376
1.31G
        ret ^= v * v;
377
1.31G
        c++;
378
1.31G
    }
379
244M
    return (ret >> 16) ^ ret;
380
244M
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
2.48M
{
384
2.48M
    return lh ? lh->num_items : 0;
385
2.48M
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
156k
{
389
156k
    return lh->down_load;
390
156k
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
481k
{
394
481k
    lh->down_load = down_load;
395
481k
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
6.32M
{
399
6.32M
    return lh->error;
400
6.32M
}