Coverage Report

Created: 2025-08-29 06:10

/src/curl/lib/hash.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#include <curl/curl.h>
28
29
#include "hash.h"
30
#include "llist.h"
31
#include "curl_memory.h"
32
33
/* The last #include file should be: */
34
#include "memdebug.h"
35
36
/* random patterns for API verification */
37
#ifdef DEBUGBUILD
38
1.23M
#define HASHINIT 0x7017e781
39
313k
#define ITERINIT 0x5FEDCBA9
40
#endif
41
42
43
#if 0 /* useful function for debugging hashes and their contents */
44
void Curl_hash_print(struct Curl_hash *h,
45
                     void (*func)(void *))
46
{
47
  struct Curl_hash_iterator iter;
48
  struct Curl_hash_element *he;
49
  size_t last_index = UINT_MAX;
50
51
  if(!h)
52
    return;
53
54
  fprintf(stderr, "=Hash dump=\n");
55
56
  Curl_hash_start_iterate(h, &iter);
57
58
  he = Curl_hash_next_element(&iter);
59
  while(he) {
60
    if(iter.slot_index != last_index) {
61
      fprintf(stderr, "index %d:", (int)iter.slot_index);
62
      if(last_index != UINT_MAX) {
63
        fprintf(stderr, "\n");
64
      }
65
      last_index = iter.slot_index;
66
    }
67
68
    if(func)
69
      func(he->ptr);
70
    else
71
      fprintf(stderr, " [key=%.*s, he=%p, ptr=%p]",
72
              (int)he->key_len, (char *)he->key,
73
              (void *)he, (void *)he->ptr);
74
75
    he = Curl_hash_next_element(&iter);
76
  }
77
  fprintf(stderr, "\n");
78
}
79
#endif
80
81
/* Initializes a hash structure.
82
 * Return 1 on error, 0 is fine.
83
 *
84
 * @unittest: 1602
85
 * @unittest: 1603
86
 */
87
void
88
Curl_hash_init(struct Curl_hash *h,
89
               size_t slots,
90
               hash_function hfunc,
91
               comp_function comparator,
92
               Curl_hash_dtor dtor)
93
1.23M
{
94
1.23M
  DEBUGASSERT(h);
95
1.23M
  DEBUGASSERT(slots);
96
1.23M
  DEBUGASSERT(hfunc);
97
1.23M
  DEBUGASSERT(comparator);
98
1.23M
  DEBUGASSERT(dtor);
99
100
1.23M
  h->table = NULL;
101
1.23M
  h->hash_func = hfunc;
102
1.23M
  h->comp_func = comparator;
103
1.23M
  h->dtor = dtor;
104
1.23M
  h->size = 0;
105
1.23M
  h->slots = slots;
106
1.23M
#ifdef DEBUGBUILD
107
1.23M
  h->init = HASHINIT;
108
1.23M
#endif
109
1.23M
}
110
111
static struct Curl_hash_element *
112
hash_elem_create(const void *key, size_t key_len, const void *p,
113
                 Curl_hash_elem_dtor dtor)
114
369k
{
115
369k
  struct Curl_hash_element *he;
116
117
  /* allocate the struct plus memory after it to store the key */
118
369k
  he = malloc(sizeof(struct Curl_hash_element) + key_len);
119
369k
  if(he) {
120
369k
    he->next = NULL;
121
    /* copy the key */
122
369k
    memcpy(he->key, key, key_len);
123
369k
    he->key_len = key_len;
124
369k
    he->ptr = CURL_UNCONST(p);
125
369k
    he->dtor = dtor;
126
369k
  }
127
369k
  return he;
128
369k
}
129
130
static void hash_elem_clear_ptr(struct Curl_hash *h,
131
                                struct Curl_hash_element *he)
132
379k
{
133
379k
  DEBUGASSERT(h);
134
379k
  DEBUGASSERT(he);
135
379k
  if(he->ptr) {
136
379k
    if(he->dtor)
137
133k
      he->dtor(he->key, he->key_len, he->ptr);
138
245k
    else
139
245k
      h->dtor(he->ptr);
140
379k
    he->ptr = NULL;
141
379k
  }
142
379k
}
143
144
static void hash_elem_destroy(struct Curl_hash *h,
145
                              struct Curl_hash_element *he)
146
369k
{
147
369k
  hash_elem_clear_ptr(h, he);
148
369k
  free(he);
149
369k
}
150
151
static void hash_elem_unlink(struct Curl_hash *h,
152
                             struct Curl_hash_element **he_anchor,
153
                             struct Curl_hash_element *he)
154
369k
{
155
369k
  *he_anchor = he->next;
156
369k
  --h->size;
157
369k
}
158
159
static void hash_elem_link(struct Curl_hash *h,
160
                           struct Curl_hash_element **he_anchor,
161
                           struct Curl_hash_element *he)
162
369k
{
163
369k
  he->next = *he_anchor;
164
369k
  *he_anchor = he;
165
369k
  ++h->size;
166
369k
}
167
168
103M
#define CURL_HASH_SLOT(x,y,z)      x->table[x->hash_func(y, z, x->slots)]
169
630k
#define CURL_HASH_SLOT_ADDR(x,y,z) &CURL_HASH_SLOT(x,y,z)
170
171
void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
172
                     Curl_hash_elem_dtor dtor)
173
379k
{
174
379k
  struct Curl_hash_element *he, **slot;
175
176
379k
  DEBUGASSERT(h);
177
379k
  DEBUGASSERT(h->slots);
178
379k
  DEBUGASSERT(h->init == HASHINIT);
179
379k
  if(!h->table) {
180
350k
    h->table = calloc(h->slots, sizeof(struct Curl_hash_element *));
181
350k
    if(!h->table)
182
0
      return NULL; /* OOM */
183
350k
  }
184
185
379k
  slot = CURL_HASH_SLOT_ADDR(h, key, key_len);
186
379k
  for(he = *slot; he; he = he->next) {
187
10.0k
    if(h->comp_func(he->key, he->key_len, key, key_len)) {
188
      /* existing key entry, overwrite by clearing old pointer */
189
9.57k
      hash_elem_clear_ptr(h, he);
190
9.57k
      he->ptr = (void *)p;
191
9.57k
      he->dtor = dtor;
192
9.57k
      return p;
193
9.57k
    }
194
10.0k
  }
195
196
369k
  he = hash_elem_create(key, key_len, p, dtor);
197
369k
  if(!he)
198
0
    return NULL; /* OOM */
199
200
369k
  hash_elem_link(h, slot, he);
201
369k
  return p; /* return the new entry */
202
369k
}
203
204
/* Insert the data in the hash. If there already was a match in the hash, that
205
 * data is replaced. This function also "lazily" allocates the table if
206
 * needed, as it is not done in the _init function (anymore).
207
 *
208
 * @unittest: 1305
209
 * @unittest: 1602
210
 * @unittest: 1603
211
 */
212
void *
213
Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
214
245k
{
215
245k
  return Curl_hash_add2(h, key, key_len, p, NULL);
216
245k
}
217
218
/* Remove the identified hash entry.
219
 * Returns non-zero on failure.
220
 *
221
 * @unittest: 1603
222
 */
223
int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
224
464k
{
225
464k
  DEBUGASSERT(h);
226
464k
  DEBUGASSERT(h->slots);
227
464k
  DEBUGASSERT(h->init == HASHINIT);
228
464k
  if(h->table) {
229
251k
    struct Curl_hash_element *he, **he_anchor;
230
231
251k
    he_anchor = CURL_HASH_SLOT_ADDR(h, key, key_len);
232
251k
    while(*he_anchor) {
233
137k
      he = *he_anchor;
234
137k
      if(h->comp_func(he->key, he->key_len, key, key_len)) {
235
137k
        hash_elem_unlink(h, he_anchor, he);
236
137k
        hash_elem_destroy(h, he);
237
137k
        return 0;
238
137k
      }
239
2
      he_anchor = &he->next;
240
2
    }
241
251k
  }
242
327k
  return 1;
243
464k
}
244
245
/* Retrieves a hash element.
246
 *
247
 * @unittest: 1603
248
 */
249
void *
250
Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
251
103M
{
252
103M
  DEBUGASSERT(h);
253
103M
  DEBUGASSERT(h->init == HASHINIT);
254
103M
  if(h->table) {
255
102M
    struct Curl_hash_element *he;
256
102M
    DEBUGASSERT(h->slots);
257
102M
    he = CURL_HASH_SLOT(h, key, key_len);
258
102M
    while(he) {
259
101M
      if(h->comp_func(he->key, he->key_len, key, key_len)) {
260
101M
        return he->ptr;
261
101M
      }
262
158k
      he = he->next;
263
158k
    }
264
102M
  }
265
1.85M
  return NULL;
266
103M
}
267
268
/* Destroys all the entries in the given hash and resets its attributes,
269
 * prepping the given hash for [static|dynamic] deallocation.
270
 *
271
 * @unittest: 1305
272
 * @unittest: 1602
273
 * @unittest: 1603
274
 */
275
void
276
Curl_hash_destroy(struct Curl_hash *h)
277
1.23M
{
278
1.23M
  DEBUGASSERT(h->init == HASHINIT);
279
1.23M
  if(h->table) {
280
350k
    Curl_hash_clean(h);
281
350k
    Curl_safefree(h->table);
282
350k
  }
283
1.23M
  DEBUGASSERT(h->size == 0);
284
1.23M
  h->slots = 0;
285
1.23M
}
286
287
/* Removes all the entries in the given hash.
288
 *
289
 * @unittest: 1602
290
 */
291
void Curl_hash_clean(struct Curl_hash *h)
292
350k
{
293
350k
  if(h && h->table) {
294
350k
    struct Curl_hash_element *he, **he_anchor;
295
350k
    size_t i;
296
350k
    DEBUGASSERT(h->init == HASHINIT);
297
22.3M
    for(i = 0; i < h->slots; ++i) {
298
21.9M
      he_anchor = &h->table[i];
299
22.2M
      while(*he_anchor) {
300
232k
        he = *he_anchor;
301
232k
        hash_elem_unlink(h, he_anchor, he);
302
232k
        hash_elem_destroy(h, he);
303
232k
      }
304
21.9M
    }
305
350k
  }
306
350k
}
307
308
size_t Curl_hash_count(struct Curl_hash *h)
309
128k
{
310
128k
  DEBUGASSERT(h->init == HASHINIT);
311
128k
  return h->size;
312
128k
}
313
314
/* Cleans all entries that pass the comp function criteria. */
315
void
316
Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
317
                               int (*comp)(void *, void *))
318
128k
{
319
128k
  size_t i;
320
321
128k
  if(!h || !h->table)
322
8.49k
    return;
323
324
119k
  DEBUGASSERT(h->init == HASHINIT);
325
8.62M
  for(i = 0; i < h->slots; ++i) {
326
8.50M
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
327
8.63M
    while(*he_anchor) {
328
      /* ask the callback function if we shall remove this entry or not */
329
122k
      if(!comp || comp(user, (*he_anchor)->ptr)) {
330
216
        he = *he_anchor;
331
216
        hash_elem_unlink(h, he_anchor, he);
332
216
        hash_elem_destroy(h, he);
333
216
      }
334
122k
      else
335
122k
        he_anchor = &(*he_anchor)->next;
336
122k
    }
337
8.50M
  }
338
119k
}
339
340
size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
341
103M
{
342
103M
  const char *key_str = (const char *) key;
343
103M
  const char *end = key_str + key_length;
344
103M
  size_t h = 5381;
345
346
2.18G
  while(key_str < end) {
347
2.08G
    size_t j = (size_t)*key_str++;
348
2.08G
    h += h << 5;
349
2.08G
    h ^= j;
350
2.08G
  }
351
352
103M
  return (h % slots_num);
353
103M
}
354
355
size_t curlx_str_key_compare(void *k1, size_t key1_len,
356
                             void *k2, size_t key2_len)
357
101M
{
358
101M
  if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
359
101M
    return 1;
360
361
158k
  return 0;
362
101M
}
363
364
void Curl_hash_start_iterate(struct Curl_hash *hash,
365
                             struct Curl_hash_iterator *iter)
366
313k
{
367
313k
  DEBUGASSERT(hash->init == HASHINIT);
368
313k
  iter->hash = hash;
369
313k
  iter->slot_index = 0;
370
313k
  iter->current = NULL;
371
313k
#ifdef DEBUGBUILD
372
313k
  iter->init = ITERINIT;
373
313k
#endif
374
313k
}
375
376
struct Curl_hash_element *
377
Curl_hash_next_element(struct Curl_hash_iterator *iter)
378
326k
{
379
326k
  struct Curl_hash *h;
380
326k
  DEBUGASSERT(iter->init == ITERINIT);
381
326k
  h = iter->hash;
382
326k
  if(!h->table)
383
170k
    return NULL; /* empty hash, nothing to return */
384
385
  /* Get the next element in the current list, if any */
386
155k
  if(iter->current)
387
12.6k
    iter->current = iter->current->next;
388
389
  /* If we have reached the end of the list, find the next one */
390
155k
  if(!iter->current) {
391
155k
    size_t i;
392
13.7M
    for(i = iter->slot_index; i < h->slots; i++) {
393
13.6M
      if(h->table[i]) {
394
20.8k
        iter->current = h->table[i];
395
20.8k
        iter->slot_index = i + 1;
396
20.8k
        break;
397
20.8k
      }
398
13.6M
    }
399
155k
  }
400
401
155k
  return iter->current;
402
326k
}