Coverage Report

Created: 2025-07-12 07:07

/src/irssi/subprojects/openssl-1.1.1l/crypto/lhash/lhash.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
203k
#define MIN_NODES       16
40
67.5k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
67.5k
#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
67.5k
{
49
67.5k
    OPENSSL_LHASH *ret;
50
51
67.5k
    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
         * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE);
56
         */
57
0
        return NULL;
58
0
    }
59
67.5k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
60
0
        goto err;
61
67.5k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62
67.5k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63
67.5k
    ret->num_nodes = MIN_NODES / 2;
64
67.5k
    ret->num_alloc_nodes = MIN_NODES;
65
67.5k
    ret->pmax = MIN_NODES / 2;
66
67.5k
    ret->up_load = UP_LOAD;
67
67.5k
    ret->down_load = DOWN_LOAD;
68
67.5k
    return ret;
69
70
0
err:
71
0
    OPENSSL_free(ret->b);
72
0
    OPENSSL_free(ret);
73
0
    return NULL;
74
67.5k
}
75
76
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
77
67.5k
{
78
67.5k
    unsigned int i;
79
67.5k
    OPENSSL_LH_NODE *n, *nn;
80
81
67.5k
    if (lh == NULL)
82
2
        return;
83
84
611k
    for (i = 0; i < lh->num_nodes; i++) {
85
543k
        n = lh->b[i];
86
549k
        while (n != NULL) {
87
6.37k
            nn = n->next;
88
6.37k
            OPENSSL_free(n);
89
6.37k
            n = nn;
90
6.37k
        }
91
543k
    }
92
67.5k
    OPENSSL_free(lh->b);
93
67.5k
    OPENSSL_free(lh);
94
67.5k
}
95
96
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
97
17.7k
{
98
17.7k
    unsigned long hash;
99
17.7k
    OPENSSL_LH_NODE *nn, **rn;
100
17.7k
    void *ret;
101
102
17.7k
    lh->error = 0;
103
17.7k
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
104
0
        return NULL;        /* 'lh->error++' already done in 'expand' */
105
106
17.7k
    rn = getrn(lh, data, &hash);
107
108
17.7k
    if (*rn == NULL) {
109
7.01k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
110
0
            lh->error++;
111
0
            return NULL;
112
0
        }
113
7.01k
        nn->data = data;
114
7.01k
        nn->next = NULL;
115
7.01k
        nn->hash = hash;
116
7.01k
        *rn = nn;
117
7.01k
        ret = NULL;
118
7.01k
        lh->num_insert++;
119
7.01k
        lh->num_items++;
120
10.6k
    } else {                    /* replace same key */
121
10.6k
        ret = (*rn)->data;
122
10.6k
        (*rn)->data = data;
123
10.6k
        lh->num_replace++;
124
10.6k
    }
125
17.7k
    return ret;
126
17.7k
}
127
128
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
129
646
{
130
646
    unsigned long hash;
131
646
    OPENSSL_LH_NODE *nn, **rn;
132
646
    void *ret;
133
134
646
    lh->error = 0;
135
646
    rn = getrn(lh, data, &hash);
136
137
646
    if (*rn == NULL) {
138
0
        lh->num_no_delete++;
139
0
        return NULL;
140
646
    } else {
141
646
        nn = *rn;
142
646
        *rn = nn->next;
143
646
        ret = nn->data;
144
646
        OPENSSL_free(nn);
145
646
        lh->num_delete++;
146
646
    }
147
148
646
    lh->num_items--;
149
646
    if ((lh->num_nodes > MIN_NODES) &&
150
646
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
151
2
        contract(lh);
152
153
646
    return ret;
154
646
}
155
156
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
157
405k
{
158
405k
    unsigned long hash;
159
405k
    OPENSSL_LH_NODE **rn;
160
405k
    void *ret;
161
162
405k
    tsan_store((TSAN_QUALIFIER int *)&lh->error, 0);
163
164
405k
    rn = getrn(lh, data, &hash);
165
166
405k
    if (*rn == NULL) {
167
135k
        tsan_counter(&lh->num_retrieve_miss);
168
135k
        return NULL;
169
270k
    } else {
170
270k
        ret = (*rn)->data;
171
270k
        tsan_counter(&lh->num_retrieve);
172
270k
    }
173
174
270k
    return ret;
175
405k
}
176
177
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
178
                          OPENSSL_LH_DOALL_FUNC func,
179
                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
180
67.5k
{
181
67.5k
    int i;
182
67.5k
    OPENSSL_LH_NODE *a, *n;
183
184
67.5k
    if (lh == NULL)
185
0
        return;
186
187
    /*
188
     * reverse the order so we search from 'top to bottom' We were having
189
     * memory leaks otherwise
190
     */
191
608k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
192
540k
        a = lh->b[i];
193
541k
        while (a != NULL) {
194
828
            n = a->next;
195
828
            if (use_arg)
196
218
                func_arg(a->data, arg);
197
610
            else
198
610
                func(a->data);
199
828
            a = n;
200
828
        }
201
540k
    }
202
67.5k
}
203
204
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
205
8
{
206
8
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
207
8
}
208
209
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
210
67.5k
{
211
67.5k
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
212
67.5k
}
213
214
static int expand(OPENSSL_LHASH *lh)
215
3.46k
{
216
3.46k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
217
3.46k
    unsigned int p, pmax, nni, j;
218
3.46k
    unsigned long hash;
219
220
3.46k
    nni = lh->num_alloc_nodes;
221
3.46k
    p = lh->p;
222
3.46k
    pmax = lh->pmax;
223
3.46k
    if (p + 1 >= pmax) {
224
24
        j = nni * 2;
225
24
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
226
24
        if (n == NULL) {
227
0
            lh->error++;
228
0
            return 0;
229
0
        }
230
24
        lh->b = n;
231
24
        memset(n + nni, 0, sizeof(*n) * (j - nni));
232
24
        lh->pmax = nni;
233
24
        lh->num_alloc_nodes = j;
234
24
        lh->num_expand_reallocs++;
235
24
        lh->p = 0;
236
3.44k
    } else {
237
3.44k
        lh->p++;
238
3.44k
    }
239
240
3.46k
    lh->num_nodes++;
241
3.46k
    lh->num_expands++;
242
3.46k
    n1 = &(lh->b[p]);
243
3.46k
    n2 = &(lh->b[p + pmax]);
244
3.46k
    *n2 = NULL;
245
246
15.0k
    for (np = *n1; np != NULL;) {
247
11.5k
        hash = np->hash;
248
11.5k
        if ((hash % nni) != p) { /* move it */
249
1.14k
            *n1 = (*n1)->next;
250
1.14k
            np->next = *n2;
251
1.14k
            *n2 = np;
252
1.14k
        } else
253
10.3k
            n1 = &((*n1)->next);
254
11.5k
        np = *n1;
255
11.5k
    }
256
257
3.46k
    return 1;
258
3.46k
}
259
260
static void contract(OPENSSL_LHASH *lh)
261
2
{
262
2
    OPENSSL_LH_NODE **n, *n1, *np;
263
264
2
    np = lh->b[lh->p + lh->pmax - 1];
265
2
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
266
2
    if (lh->p == 0) {
267
0
        n = OPENSSL_realloc(lh->b,
268
0
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
269
0
        if (n == NULL) {
270
            /* fputs("realloc error in lhash",stderr); */
271
0
            lh->error++;
272
0
            return;
273
0
        }
274
0
        lh->num_contract_reallocs++;
275
0
        lh->num_alloc_nodes /= 2;
276
0
        lh->pmax /= 2;
277
0
        lh->p = lh->pmax - 1;
278
0
        lh->b = n;
279
0
    } else
280
2
        lh->p--;
281
282
2
    lh->num_nodes--;
283
2
    lh->num_contracts++;
284
285
2
    n1 = lh->b[(int)lh->p];
286
2
    if (n1 == NULL)
287
2
        lh->b[(int)lh->p] = np;
288
0
    else {
289
0
        while (n1->next != NULL)
290
0
            n1 = n1->next;
291
0
        n1->next = np;
292
0
    }
293
2
}
294
295
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
296
                               const void *data, unsigned long *rhash)
297
423k
{
298
423k
    OPENSSL_LH_NODE **ret, *n1;
299
423k
    unsigned long hash, nn;
300
423k
    OPENSSL_LH_COMPFUNC cf;
301
302
423k
    hash = (*(lh->hash)) (data);
303
423k
    tsan_counter(&lh->num_hash_calls);
304
423k
    *rhash = hash;
305
306
423k
    nn = hash % lh->pmax;
307
423k
    if (nn < lh->p)
308
348k
        nn = hash % lh->num_alloc_nodes;
309
310
423k
    cf = lh->comp;
311
423k
    ret = &(lh->b[(int)nn]);
312
2.55M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
313
2.41M
        tsan_counter(&lh->num_hash_comps);
314
2.41M
        if (n1->hash != hash) {
315
2.12M
            ret = &(n1->next);
316
2.12M
            continue;
317
2.12M
        }
318
2.41M
        tsan_counter(&lh->num_comp_calls);
319
281k
        if (cf(n1->data, data) == 0)
320
281k
            break;
321
320
        ret = &(n1->next);
322
320
    }
323
423k
    return ret;
324
423k
}
325
326
/*
327
 * The following hash seems to work very well on normal text strings no
328
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
329
 * as good as MD5, but still good.
330
 */
331
unsigned long OPENSSL_LH_strhash(const char *c)
332
924
{
333
924
    unsigned long ret = 0;
334
924
    long n;
335
924
    unsigned long v;
336
924
    int r;
337
338
924
    if ((c == NULL) || (*c == '\0'))
339
62
        return ret;
340
341
862
    n = 0x100;
342
11.1k
    while (*c) {
343
10.2k
        v = n | (*c);
344
10.2k
        n += 0x100;
345
10.2k
        r = (int)((v >> 2) ^ v) & 0x0f;
346
10.2k
        ret = (ret << r) | (ret >> (32 - r));
347
10.2k
        ret &= 0xFFFFFFFFL;
348
10.2k
        ret ^= v * v;
349
10.2k
        c++;
350
10.2k
    }
351
862
    return (ret >> 16) ^ ret;
352
924
}
353
354
unsigned long openssl_lh_strcasehash(const char *c)
355
271k
{
356
271k
    unsigned long ret = 0;
357
271k
    long n;
358
271k
    unsigned long v;
359
271k
    int r;
360
361
271k
    if (c == NULL || *c == '\0')
362
0
        return ret;
363
364
1.90M
    for (n = 0x100; *c != '\0'; n += 0x100) {
365
1.63M
        v = n | ossl_tolower(*c);
366
1.63M
        r = (int)((v >> 2) ^ v) & 0x0f;
367
1.63M
        ret = (ret << r) | (ret >> (32 - r));
368
1.63M
        ret &= 0xFFFFFFFFL;
369
1.63M
        ret ^= v * v;
370
1.63M
        c++;
371
1.63M
    }
372
271k
    return (ret >> 16) ^ ret;
373
271k
}
374
375
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
376
2
{
377
2
    return lh ? lh->num_items : 0;
378
2
}
379
380
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
381
67.5k
{
382
67.5k
    return lh->down_load;
383
67.5k
}
384
385
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
386
135k
{
387
135k
    lh->down_load = down_load;
388
135k
}
389
390
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
391
492
{
392
492
    return lh->error;
393
492
}