Coverage Report

Created: 2026-10-15 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/u8_strset.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 "u8_strset.h"
27
#include "curlx/strdup.h"
28
29
/* random patterns for API verification */
30
#ifdef DEBUGBUILD
31
63.0k
#define CURL_U8_STRSET_MAGIC 0x7117e783
32
#endif
33
34
#define CURL_U8_STRSET_DEBUG      0
35
36
29.6k
#define CURL_SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
37
38
783k
#define CURL_U8_STRSET_SLOT_IDX(s, i)    (uint8_t)((i) & (s)->slotmask)
39
249k
#define CURL_U8_SLOT_CNT(i)              (uint16_t)((i) ? (1U << (i)) : 0)
40
63.1k
#define CURL_U8_SLOT_MASK(i)             (uint8_t)((1U << (i)) - 1)
41
247k
#define CURL_U8_STRSET_SLOT_CNT(s)       CURL_U8_SLOT_CNT((s)->slotbits)
42
43
/* A hashset for tuples (id, string) using Robin Hood Hashing.
44
 * <https://www.cs.cornell.edu/courses/JavaAndDS/files/hashing_RobinHood.pdf>
45
 * The basic idea here to handle collisions by robbing "rich" entries and
46
 * giving to the "poor":
47
 * - We have an array: (id, string) are ideally placed at index "id % size".
48
 * - If slot at index is already occupied, we have a collision.
49
 * - A simple collision strategy would look at the next index, and the
50
 *   next until finding an empty slot.
51
 * - The drawback is that this may lead to many checks on lookups, as it
52
 *   will need to also look at subsequent slots until it finds the match.
53
 *   The amount of lookups is the "probe sequence length" (psl) and this
54
 *   may vary greatly between entries.
55
 * - Robin Hood Hashing balances the 'psl's of all entries more evenly:
56
 *   - psl == 0 means an entry is in exactly the right slot
57
 *   - psl == 1 means it is in the slot right after. psl == 2 is the slot
58
 *     after that, etc.
59
 *   - when inserting a new entry, track its psl. Finding a slot where
60
 *     the existing entry has a lower psl makes a swap. Put the new entry
61
 *     and its psl there, take the previous entry and its psl and find
62
 *     the next best slot for the previous entry. */
63
void Curl_u8_strset_init(struct u8_strset *set)
64
63.0k
{
65
#if defined(__GNUC__) && __GNUC__ >= 13
66
#pragma GCC diagnostic push
67
#pragma GCC diagnostic ignored "-Warray-bounds"
68
#endif
69
63.0k
  memset(set, 0, sizeof(*set));
70
#if defined(__GNUC__) && __GNUC__ >= 13
71
#pragma GCC diagnostic pop
72
#endif
73
63.0k
  set->data = set->sdata;
74
63.0k
  set->ids = set->sids;
75
63.0k
  set->psl = set->spsl;
76
63.0k
  set->slotbits = CURL_U8_STRSET_START_BITS;
77
63.0k
  set->slotmask = CURL_U8_SLOT_MASK(set->slotbits);
78
63.0k
  set->count = 0;
79
63.0k
#ifdef DEBUGBUILD
80
63.0k
  set->init = CURL_U8_STRSET_MAGIC;
81
63.0k
#endif
82
63.0k
}
83
84
void Curl_u8_strset_clear(struct u8_strset *set)
85
21.0k
{
86
21.0k
  uint16_t i;
87
21.0k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
88
190k
  for(i = 0; i < CURL_U8_STRSET_SLOT_CNT(set); ++i)
89
169k
    curlx_safefree(set->data[i]);
90
91
21.0k
  if(set->data != set->sdata)
92
97
    curlx_safefree(set->data);
93
21.0k
  Curl_u8_strset_init(set);
94
21.0k
}
95
96
static void u8_strset_addn(struct u8_strset *set, uint8_t id, char *val)
97
58.5k
{
98
58.5k
  uint8_t i = CURL_U8_STRSET_SLOT_IDX(set, id);
99
58.5k
  uint8_t psl = 0;
100
134k
  while(set->data[i]) {
101
75.6k
    if(psl > set->psl[i]) { /* SWAP */
102
14.8k
      char *tmpdata;
103
14.8k
      tmpdata = set->data[i];
104
14.8k
      set->data[i] = val;
105
14.8k
      val = tmpdata;
106
14.8k
      CURL_SWAP(set->psl[i], psl);
107
14.8k
      CURL_SWAP(set->ids[i], id);
108
14.8k
    }
109
75.6k
    i = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
110
75.6k
    ++psl;
111
75.6k
  }
112
58.5k
  set->ids[i] = id;
113
58.5k
  set->data[i] = val;
114
58.5k
  set->psl[i] = psl;
115
58.5k
  ++set->count;
116
58.5k
}
117
118
static bool u8_strset_grow(struct u8_strset *set)
119
131
{
120
131
  uint8_t *prev_ids, nslotbits;
121
131
  uint16_t prev_slots;
122
131
  char **prev_data;
123
131
  size_t nslots;
124
131
  void *d;
125
126
131
  if(set->slotbits >= 8)
127
0
    return FALSE;
128
131
  nslotbits = (uint8_t)(set->slotbits + 1);
129
#if CURL_U8_STRSET_DEBUG
130
  curl_mfprintf(stderr, "u8_strset_grow from %d to %d\n",
131
                set->slotbits, nslotbits);
132
#endif
133
131
  nslots = CURL_U8_SLOT_CNT(nslotbits);
134
131
  d = curlx_calloc(1, (nslots * sizeof(char *)) + (2 * nslots));
135
131
  if(!d)
136
0
    return FALSE;
137
138
131
  prev_data = set->data;
139
131
  prev_ids = set->ids;
140
131
  prev_slots = set->slotbits;
141
#if defined(__GNUC__) && __GNUC__ >= 13
142
#pragma GCC diagnostic push
143
#pragma GCC diagnostic ignored "-Wanalyzer-allocation-size"
144
#endif
145
131
  set->data = (char **)d;
146
#if defined(__GNUC__) && __GNUC__ >= 13
147
#pragma GCC diagnostic pop
148
#endif
149
131
  set->ids = (uint8_t *)d + (nslots * sizeof(char *));
150
131
  set->psl = set->ids + nslots;
151
131
  set->slotbits = nslotbits;
152
131
  set->slotmask = CURL_U8_SLOT_MASK(set->slotbits);
153
131
  set->count = 0;
154
  /* re-add previous entries */
155
131
  {
156
131
    uint16_t i;
157
1.53k
    for(i = 0; i < CURL_U8_SLOT_CNT(prev_slots); ++i) {
158
1.40k
      if(prev_data[i])
159
1.40k
        u8_strset_addn(set, prev_ids[i], prev_data[i]);
160
1.40k
    }
161
131
  }
162
131
  if(prev_data != set->sdata)
163
34
    curlx_free(prev_data);
164
131
  return TRUE;
165
131
}
166
167
static bool u8_strset_get_index(struct u8_strset *set,
168
                                uint8_t id, uint8_t *pindex)
169
320k
{
170
320k
  uint8_t i = CURL_U8_STRSET_SLOT_IDX(set, id);
171
320k
  uint8_t psl = 0;
172
648k
  while(set->data[i] && (psl <= set->psl[i])) {
173
379k
    if(set->ids[i] == id) {
174
51.9k
      *pindex = i;
175
#if CURL_U8_STRSET_DEBUG
176
      curl_mfprintf(stderr, "u8_strset_index %d=%s\n", id, set->data[i]);
177
#endif
178
51.9k
      return TRUE;
179
51.9k
    }
180
327k
    i = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
181
327k
    ++psl;
182
327k
  }
183
#if CURL_U8_STRSET_DEBUG
184
  curl_mfprintf(stderr, "u8_strset_index %d not found\n", id);
185
#endif
186
268k
  *pindex = 0;
187
268k
  return FALSE;
188
320k
}
189
190
#ifdef UNITTESTS
191
/* @unittest 3211 */
192
uint16_t Curl_u8_strset_count(struct u8_strset *set)
193
0
{
194
0
  return set->count;
195
0
}
196
#endif
197
198
const char *Curl_u8_strset_get(struct u8_strset *set, uint8_t id)
199
157k
{
200
157k
  uint8_t i;
201
157k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
202
157k
  if(u8_strset_get_index(set, id, &i))
203
51.4k
    return set->data[i];
204
106k
  return NULL;
205
157k
}
206
207
CURLcode Curl_u8_strset_setn(struct u8_strset *set,
208
                             uint8_t id, char *str)
209
57.2k
{
210
57.2k
  uint8_t i;
211
212
57.2k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
213
#if CURL_U8_STRSET_DEBUG
214
  curl_mfprintf(stderr, "u8_strset_setn %d=%s\n", id, str);
215
#endif
216
57.2k
  if(!str) {
217
22
    Curl_u8_strset_unset(set, id);
218
22
    return CURLE_OK;
219
22
  }
220
221
57.2k
  if(u8_strset_get_index(set, id, &i)) {
222
    /* `id` is in set, replace value */
223
103
    curlx_free(set->data[i]);
224
103
    set->data[i] = str;
225
103
    return CURLE_OK;
226
103
  }
227
  /* `id` not in set yet, grow if full */
228
57.1k
  if((set->count >= CURL_U8_STRSET_SLOT_CNT(set)) && !u8_strset_grow(set)) {
229
0
    curlx_free(str);
230
0
    return CURLE_OUT_OF_MEMORY;
231
0
  }
232
233
57.1k
  u8_strset_addn(set, id, str);
234
57.1k
  return CURLE_OK;
235
57.1k
}
236
237
CURLcode Curl_u8_strset_setx(struct u8_strset *set,
238
                             uint8_t id, const char *str, size_t slen)
239
56.9k
{
240
56.9k
  char *val;
241
242
56.9k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
243
56.9k
  if(!str) {
244
0
    Curl_u8_strset_unset(set, id);
245
0
    return CURLE_OK;
246
0
  }
247
248
56.9k
  val = curlx_memdup0(str, slen);
249
56.9k
  if(!val)
250
0
    return CURLE_OUT_OF_MEMORY;
251
56.9k
  return Curl_u8_strset_setn(set, id, val);
252
56.9k
}
253
254
/* @unittest 3211 */
255
UNITTEST CURLcode u8_strset_set(struct u8_strset *set,
256
                                uint8_t id, const char *str);
257
UNITTEST CURLcode u8_strset_set(struct u8_strset *set,
258
                                uint8_t id, const char *str)
259
0
{
260
0
  return Curl_u8_strset_setx(set, id, str, str ? strlen(str) : 0);
261
0
}
262
263
static void u8_strset_unset(struct u8_strset *set, uint8_t id, bool zero)
264
105k
{
265
105k
  uint8_t i, j;
266
267
105k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
268
105k
  if(u8_strset_get_index(set, id, &i)) {
269
    /* `id` is in set */
270
345
    if(zero)
271
304
      curlx_strzero(set->data[i]);
272
345
    curlx_safefree(set->data[i]);
273
345
    set->ids[i] = set->psl[i] = 0;
274
345
    --set->count;
275
345
    j = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
276
    /* shift all entries with positive psl "down" */
277
1.22k
    while(set->data[j] && set->psl[j]) {
278
880
      set->data[i] = set->data[j];
279
880
      set->ids[i] = set->ids[j];
280
880
      set->psl[i] = (uint8_t)(set->psl[j] - 1);
281
880
      set->data[j] = NULL;
282
880
      set->ids[j] = set->psl[j] = 0;
283
880
      i = j;
284
880
      j = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
285
880
    }
286
345
  }
287
105k
}
288
289
void Curl_u8_strset_unset(struct u8_strset *set, uint8_t id)
290
231
{
291
231
  u8_strset_unset(set, id, FALSE);
292
231
}
293
294
void Curl_u8_strset_unset0(struct u8_strset *set, uint8_t id)
295
105k
{
296
105k
  u8_strset_unset(set, id, TRUE);
297
105k
}
298
299
CURLcode Curl_u8_strset_copy(struct u8_strset *dest, struct u8_strset *src)
300
0
{
301
0
  CURLcode result = CURLE_OK;
302
0
  uint16_t i;
303
304
0
  DEBUGASSERT(src->init == CURL_U8_STRSET_MAGIC);
305
0
  Curl_u8_strset_clear(dest);
306
0
  for(i = 0; !result && (i < CURL_U8_STRSET_SLOT_CNT(src)); ++i) {
307
0
    if(src->data[i])
308
0
      result = u8_strset_set(dest, src->ids[i], src->data[i]);
309
0
  }
310
0
  return result;
311
0
}