Coverage Report

Created: 2026-10-15 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/hash.c
Line
Count
Source
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
#include "curl_setup.h"
25
26
#include <stddef.h> /* for offsetof() */
27
28
#include "hash.h"
29
#include "rand.h"
30
31
/* random patterns for API verification */
32
#ifdef DEBUGBUILD
33
0
#define HASHINIT 0x7017e781
34
0
#define ITERINIT 0x5FEDCBA9
35
#endif
36
37
38
typedef size_t (*hash_function)(const void *key,
39
                                size_t key_length,
40
                                size_t slots_num);
41
42
typedef size_t (*comp_function)(const void *key1,
43
                                size_t key1_len,
44
                                const void *key2,
45
                                size_t key2_len);
46
47
/* Curl_hash stores its type in a uint8_t. */
48
typedef char hash_types_fit_uint8[
49
  ((CURL_HASH_TYPE_LAST - 1) <= UINT8_MAX) ? 1 : -1];
50
51
struct hash_functions {
52
  hash_function hash;
53
  comp_function compare;
54
};
55
56
/* set at global init, read-only afterwards */
57
static size_t hash_seed;
58
59
CURLcode Curl_hash_global_init(void)
60
0
{
61
0
  static bool seeded;
62
0
  unsigned char buf[sizeof(size_t)];
63
0
  size_t seed = 0;
64
0
  size_t i;
65
0
  CURLcode result;
66
67
0
  if(seeded)
68
0
    return CURLE_OK;
69
70
0
  result = Curl_rand_bytes(NULL,
71
0
#ifdef DEBUGBUILD
72
0
                           FALSE,
73
0
#endif
74
0
                           buf, sizeof(buf));
75
0
  if(result)
76
0
    return result;
77
78
0
  for(i = 0; i < sizeof(buf); i++)
79
0
    seed = (seed << 8) | buf[i];
80
81
0
  hash_seed = seed ? seed : 1;
82
0
  seeded = TRUE;
83
0
  return CURLE_OK;
84
0
}
85
86
/* @unittest 1603
87
 */
88
UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
89
                                size_t slots_num, size_t seed);
90
UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
91
                                size_t slots_num, size_t seed)
92
0
{
93
0
  const char *key_str = (const char *)key;
94
0
  const char *end = key_str + key_length;
95
0
  size_t h = 5381 ^ seed;
96
97
0
  while(key_str < end) {
98
0
    size_t j = (size_t)*key_str++;
99
0
    h += h << 5;
100
0
    h ^= j;
101
0
  }
102
103
0
  return (h % slots_num);
104
0
}
105
106
/* @unittest 1603
107
 */
108
UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num);
109
UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num)
110
0
{
111
0
  return hash_str_seeded(key, key_length, slots_num, hash_seed);
112
0
}
113
114
/* Avoid treating the variable-length key[1] as a one-byte object. */
115
static char *hash_elem_key(struct Curl_hash_element *he)
116
0
{
117
0
  return (char *)he + offsetof(struct Curl_hash_element, key);
118
0
}
119
120
static size_t hash_socket(const void *key, size_t key_length,
121
                          size_t slots_num)
122
0
{
123
0
  curl_socket_t socket;
124
125
0
  DEBUGASSERT(key_length == sizeof(socket));
126
0
  if(key_length != sizeof(socket))
127
0
    return 0;
128
0
  memcpy(&socket, key, sizeof(socket));
129
0
  return (size_t)(socket % (curl_socket_t)slots_num);
130
0
}
131
132
static size_t compare_socket(const void *key1, size_t key1_len,
133
                             const void *key2, size_t key2_len)
134
0
{
135
0
  curl_socket_t socket1;
136
0
  curl_socket_t socket2;
137
138
0
  if((key1_len != sizeof(socket1)) || (key2_len != sizeof(socket2)))
139
0
    return 0;
140
0
  memcpy(&socket1, key1, sizeof(socket1));
141
0
  memcpy(&socket2, key2, sizeof(socket2));
142
0
  return socket1 == socket2;
143
0
}
144
145
static size_t compare_bytes(const void *key1, size_t key1_len,
146
                            const void *key2, size_t key2_len)
147
0
{
148
0
  return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
149
0
}
150
151
static const struct hash_functions hash_functions[] = {
152
  { hash_str, compare_bytes },
153
  { hash_socket, compare_socket }
154
};
155
156
static const struct hash_functions *hash_get_functions(uint8_t type)
157
0
{
158
0
  DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159
0
  if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160
0
    type = CURL_HASH_TYPE_BYTES;
161
0
  return &hash_functions[type];
162
0
}
163
164
static bool hash_key_is_valid(uint8_t type, size_t key_len)
165
0
{
166
0
  return (type == CURL_HASH_TYPE_BYTES) ||
167
0
    ((type == CURL_HASH_TYPE_SOCKET) &&
168
0
     (key_len == sizeof(curl_socket_t)));
169
0
}
170
171
#if 0 /* useful function for debugging hashes and their contents */
172
void Curl_hash_print(struct Curl_hash *h, void (*func)(void *))
173
{
174
  struct Curl_hash_iterator iter;
175
  struct Curl_hash_element *he;
176
  size_t last_index = UINT_MAX;
177
178
  if(!h)
179
    return;
180
181
  curl_mfprintf(stderr, "=Hash dump=\n");
182
183
  Curl_hash_start_iterate(h, &iter);
184
185
  he = Curl_hash_next_element(&iter);
186
  while(he) {
187
    if(iter.slot_index != last_index) {
188
      curl_mfprintf(stderr, "index %d:", (int)iter.slot_index);
189
      if(last_index != UINT_MAX) {
190
        curl_mfprintf(stderr, "\n");
191
      }
192
      last_index = iter.slot_index;
193
    }
194
195
    if(func)
196
      func(he->ptr);
197
    else
198
      curl_mfprintf(stderr, " [key=%.*s, he=%p, ptr=%p]",
199
                    (int)he->key_len, hash_elem_key(he),
200
                    (void *)he, (void *)he->ptr);
201
202
    he = Curl_hash_next_element(&iter);
203
  }
204
  curl_mfprintf(stderr, "\n");
205
}
206
#endif
207
208
/* Initializes a hash structure.
209
 *
210
 * @unittest: 1602
211
 * @unittest: 1603
212
 */
213
void Curl_hash_init(struct Curl_hash *h,
214
                    size_t slots,
215
                    Curl_hash_type type,
216
                    Curl_hash_dtor dtor)
217
0
{
218
0
  DEBUGASSERT(h);
219
0
  DEBUGASSERT(slots);
220
0
  DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
221
0
  DEBUGASSERT(dtor);
222
223
0
  h->table = NULL;
224
0
  h->dtor = dtor;
225
0
  h->size = 0;
226
0
  h->slots = slots;
227
0
  h->type = (uint8_t)type;
228
0
#ifdef DEBUGBUILD
229
0
  h->init = HASHINIT;
230
0
#endif
231
0
}
232
233
static struct Curl_hash_element *hash_elem_create(const void *key,
234
                                                  size_t key_len,
235
                                                  void *p,
236
                                                  Curl_hash_elem_dtor dtor)
237
0
{
238
0
  struct Curl_hash_element *he;
239
240
  /* allocate the struct plus memory after it to store the key */
241
0
  he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
242
0
  if(he) {
243
0
    he->next = NULL;
244
    /* copy the key */
245
0
    memcpy(hash_elem_key(he), key, key_len);
246
0
    he->key_len = key_len;
247
0
    he->ptr = p;
248
0
    he->dtor = dtor;
249
0
  }
250
0
  return he;
251
0
}
252
253
static void hash_elem_clear_ptr(struct Curl_hash *h,
254
                                struct Curl_hash_element *he)
255
0
{
256
0
  DEBUGASSERT(h);
257
0
  DEBUGASSERT(he);
258
0
  if(he->ptr) {
259
0
    if(he->dtor)
260
0
      he->dtor(hash_elem_key(he), he->key_len, he->ptr);
261
0
    else
262
0
      h->dtor(he->ptr);
263
0
    he->ptr = NULL;
264
0
  }
265
0
}
266
267
static void hash_elem_destroy(struct Curl_hash *h,
268
                              struct Curl_hash_element *he)
269
0
{
270
0
  hash_elem_clear_ptr(h, he);
271
0
  curlx_free(he);
272
0
}
273
274
static void hash_elem_unlink(struct Curl_hash *h,
275
                             struct Curl_hash_element **he_anchor,
276
                             struct Curl_hash_element *he)
277
0
{
278
0
  *he_anchor = he->next;
279
0
  --h->size;
280
0
}
281
282
static void hash_elem_link(struct Curl_hash *h,
283
                           struct Curl_hash_element **he_anchor,
284
                           struct Curl_hash_element *he)
285
0
{
286
0
  he->next = *he_anchor;
287
0
  *he_anchor = he;
288
0
  ++h->size;
289
0
}
290
291
void *Curl_hash_add2(struct Curl_hash *h,
292
                     const void *key, size_t key_len, void *p,
293
                     Curl_hash_elem_dtor dtor)
294
0
{
295
0
  const struct hash_functions *functions;
296
0
  struct Curl_hash_element *he, **slot;
297
298
0
  DEBUGASSERT(h);
299
0
  DEBUGASSERT(h->slots);
300
0
  DEBUGASSERT(h->init == HASHINIT);
301
0
  if(!hash_key_is_valid(h->type, key_len))
302
0
    return NULL;
303
0
  if(!h->table) {
304
0
    h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
305
0
    if(!h->table)
306
0
      return NULL; /* OOM */
307
0
  }
308
309
0
  functions = hash_get_functions(h->type);
310
0
  slot = &h->table[functions->hash(key, key_len, h->slots)];
311
0
  for(he = *slot; he; he = he->next) {
312
0
    if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
313
      /* existing key entry, overwrite by clearing old pointer */
314
0
      hash_elem_clear_ptr(h, he);
315
0
      he->ptr = p;
316
0
      he->dtor = dtor;
317
0
      return p;
318
0
    }
319
0
  }
320
321
0
  he = hash_elem_create(key, key_len, p, dtor);
322
0
  if(!he)
323
0
    return NULL; /* OOM */
324
325
0
  hash_elem_link(h, slot, he);
326
0
  return p; /* return the new entry */
327
0
}
328
329
/* Insert the data in the hash. If there already was a match in the hash, that
330
 * data is replaced. This function also "lazily" allocates the table if
331
 * needed, as it is not done in the _init function (anymore).
332
 *
333
 * @unittest: 1305
334
 * @unittest: 1602
335
 * @unittest: 1603
336
 */
337
void *Curl_hash_add(struct Curl_hash *h,
338
                    const void *key, size_t key_len, void *p)
339
0
{
340
0
  return Curl_hash_add2(h, key, key_len, p, NULL);
341
0
}
342
343
/* Remove the identified hash entry.
344
 * Returns non-zero on failure.
345
 *
346
 * @unittest: 1603
347
 */
348
int Curl_hash_delete(struct Curl_hash *h, const void *key, size_t key_len)
349
0
{
350
0
  const struct hash_functions *functions;
351
352
0
  DEBUGASSERT(h);
353
0
  DEBUGASSERT(h->slots);
354
0
  DEBUGASSERT(h->init == HASHINIT);
355
0
  if(!hash_key_is_valid(h->type, key_len))
356
0
    return 1;
357
0
  if(h->table) {
358
0
    struct Curl_hash_element *he, **he_anchor;
359
360
0
    functions = hash_get_functions(h->type);
361
0
    he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
362
0
    while(*he_anchor) {
363
0
      he = *he_anchor;
364
0
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
365
0
        hash_elem_unlink(h, he_anchor, he);
366
0
        hash_elem_destroy(h, he);
367
0
        return 0;
368
0
      }
369
0
      he_anchor = &he->next;
370
0
    }
371
0
  }
372
0
  return 1;
373
0
}
374
375
/* Retrieves a hash element.
376
 *
377
 * @unittest: 1603
378
 */
379
void *Curl_hash_pick(struct Curl_hash *h, const void *key, size_t key_len)
380
0
{
381
0
  const struct hash_functions *functions;
382
383
0
  DEBUGASSERT(h);
384
0
  DEBUGASSERT(h->init == HASHINIT);
385
0
  if(!hash_key_is_valid(h->type, key_len))
386
0
    return NULL;
387
0
  if(h->table) {
388
0
    struct Curl_hash_element *he;
389
0
    DEBUGASSERT(h->slots);
390
0
    functions = hash_get_functions(h->type);
391
0
    he = h->table[functions->hash(key, key_len, h->slots)];
392
0
    while(he) {
393
0
      if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
394
0
        return he->ptr;
395
0
      }
396
0
      he = he->next;
397
0
    }
398
0
  }
399
0
  return NULL;
400
0
}
401
402
/* Destroys all the entries in the given hash and resets its attributes,
403
 * prepping the given hash for [static|dynamic] deallocation.
404
 *
405
 * @unittest: 1305
406
 * @unittest: 1602
407
 * @unittest: 1603
408
 */
409
void Curl_hash_destroy(struct Curl_hash *h)
410
0
{
411
0
  DEBUGASSERT(h->init == HASHINIT);
412
0
  if(h->table) {
413
0
    Curl_hash_clean(h);
414
0
    curlx_safefree(h->table);
415
0
  }
416
0
  DEBUGASSERT(h->size == 0);
417
0
  h->slots = 0;
418
0
}
419
420
/* Removes all the entries in the given hash.
421
 *
422
 * @unittest: 1602
423
 */
424
void Curl_hash_clean(struct Curl_hash *h)
425
0
{
426
0
  if(h && h->table) {
427
0
    struct Curl_hash_element *he, **he_anchor;
428
0
    size_t i;
429
0
    DEBUGASSERT(h->init == HASHINIT);
430
0
    for(i = 0; i < h->slots; ++i) {
431
0
      he_anchor = &h->table[i];
432
0
      while(*he_anchor) {
433
0
        he = *he_anchor;
434
0
        hash_elem_unlink(h, he_anchor, he);
435
0
        hash_elem_destroy(h, he);
436
0
      }
437
0
    }
438
0
  }
439
0
}
440
441
size_t Curl_hash_count(struct Curl_hash *h)
442
0
{
443
0
  DEBUGASSERT(h->init == HASHINIT);
444
0
  return h->size;
445
0
}
446
447
/* Cleans all entries that pass the comp function criteria. */
448
void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
449
                                    int (*comp)(void *, void *))
450
0
{
451
0
  size_t i;
452
453
0
  if(!h || !h->table)
454
0
    return;
455
456
0
  DEBUGASSERT(h->init == HASHINIT);
457
0
  for(i = 0; i < h->slots; ++i) {
458
0
    struct Curl_hash_element *he, **he_anchor = &h->table[i];
459
0
    while(*he_anchor) {
460
      /* ask the callback function if we shall remove this entry or not */
461
0
      if(!comp || comp(user, (*he_anchor)->ptr)) {
462
0
        he = *he_anchor;
463
0
        hash_elem_unlink(h, he_anchor, he);
464
0
        hash_elem_destroy(h, he);
465
0
      }
466
0
      else
467
0
        he_anchor = &(*he_anchor)->next;
468
0
    }
469
0
  }
470
0
}
471
472
void Curl_hash_start_iterate(struct Curl_hash *hash,
473
                             struct Curl_hash_iterator *iter)
474
0
{
475
0
  DEBUGASSERT(hash->init == HASHINIT);
476
0
  iter->hash = hash;
477
0
  iter->slot_index = 0;
478
0
  iter->current = NULL;
479
0
#ifdef DEBUGBUILD
480
0
  iter->init = ITERINIT;
481
0
#endif
482
0
}
483
484
struct Curl_hash_element *Curl_hash_next_element(
485
  struct Curl_hash_iterator *iter)
486
0
{
487
0
  struct Curl_hash *h;
488
0
  DEBUGASSERT(iter->init == ITERINIT);
489
0
  h = iter->hash;
490
0
  if(!h->table)
491
0
    return NULL; /* empty hash, nothing to return */
492
493
  /* Get the next element in the current list, if any */
494
0
  if(iter->current)
495
0
    iter->current = iter->current->next;
496
497
  /* If we have reached the end of the list, find the next one */
498
0
  if(!iter->current) {
499
0
    size_t i;
500
0
    for(i = iter->slot_index; i < h->slots; i++) {
501
0
      if(h->table[i]) {
502
0
        iter->current = h->table[i];
503
0
        iter->slot_index = i + 1;
504
0
        break;
505
0
      }
506
0
    }
507
0
  }
508
509
0
  return iter->current;
510
0
}