Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_hash.c
Line
Count
Source
1
/*
2
  zip_hash.c -- hash table string -> uint64
3
  Copyright (C) 2015-2024 Dieter Baron and Thomas Klausner
4
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
  The authors can be contacted at <info@libzip.org>
7
8
  Redistribution and use in source and binary forms, with or without
9
  modification, are permitted provided that the following conditions
10
  are met:
11
  1. Redistributions of source code must retain the above copyright
12
     notice, this list of conditions and the following disclaimer.
13
  2. Redistributions in binary form must reproduce the above copyright
14
     notice, this list of conditions and the following disclaimer in
15
     the documentation and/or other materials provided with the
16
     distribution.
17
  3. The names of the authors may not be used to endorse or promote
18
     products derived from this software without specific prior
19
     written permission.
20
21
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
#include "zip.h"
35
#include "zipint.h"
36
#include <stdlib.h>
37
#include <string.h>
38
39
#include "siphash.h"
40
41
42
/* hash table's fill ratio is kept between these by doubling/halfing its size as necessary */
43
61.5k
#define HASH_MAX_FILL .75
44
0
#define HASH_MIN_FILL .01
45
46
/* but hash table size is kept between these */
47
0
#define HASH_MIN_SIZE 256
48
7.20k
#define HASH_MAX_SIZE 0x80000000ul
49
50
struct zip_hash_entry {
51
    const zip_uint8_t *name;
52
    zip_int64_t orig_index;
53
    zip_int64_t current_index;
54
    struct zip_hash_entry *next;
55
    zip_uint64_t hash_value;
56
};
57
typedef struct zip_hash_entry zip_hash_entry_t;
58
59
struct zip_hash {
60
    zip_uint32_t table_size;
61
    zip_uint64_t nentries;
62
    zip_hash_entry_t **table;
63
    zip_uint8_t hash_key[16];
64
};
65
66
67
/* free list of entries */
68
22.6k
static void free_list(zip_hash_entry_t *entry) {
69
50.8k
    while (entry != NULL) {
70
28.2k
        zip_hash_entry_t *next = entry->next;
71
28.2k
        free(entry);
72
28.2k
        entry = next;
73
28.2k
    }
74
22.6k
}
75
76
77
/* resize hash table; new_size must be a power of 2, can be larger or smaller than current size */
78
7.20k
static bool hash_resize(zip_hash_t *hash, zip_uint32_t new_size, zip_error_t *error) {
79
7.20k
    zip_hash_entry_t **new_table;
80
81
7.20k
    if (new_size == hash->table_size) {
82
0
        return true;
83
0
    }
84
85
7.20k
    if ((new_table = (zip_hash_entry_t **)calloc(new_size, sizeof(zip_hash_entry_t *))) == NULL) {
86
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
87
0
        return false;
88
0
    }
89
90
7.20k
    if (hash->nentries > 0) {
91
2.15k
        zip_uint32_t i;
92
93
4.30k
        for (i = 0; i < hash->table_size; i++) {
94
2.15k
            zip_hash_entry_t *entry = hash->table[i];
95
4.30k
            while (entry) {
96
2.15k
                zip_hash_entry_t *next = entry->next;
97
98
2.15k
                zip_uint32_t new_index = entry->hash_value % new_size;
99
100
2.15k
                entry->next = new_table[new_index];
101
2.15k
                new_table[new_index] = entry;
102
103
2.15k
                entry = next;
104
2.15k
            }
105
2.15k
        }
106
2.15k
    }
107
108
7.20k
    free(hash->table);
109
7.20k
    hash->table = new_table;
110
7.20k
    hash->table_size = new_size;
111
112
7.20k
    return true;
113
7.20k
}
114
115
116
5.05k
static zip_uint32_t size_for_capacity(zip_uint64_t capacity) {
117
5.05k
    double needed_size = capacity / HASH_MAX_FILL;
118
5.05k
    zip_uint32_t v;
119
120
5.05k
    if (needed_size > ZIP_UINT32_MAX) {
121
0
        v = ZIP_UINT32_MAX;
122
0
    }
123
5.05k
    else {
124
5.05k
        v = (zip_uint32_t)needed_size;
125
5.05k
    }
126
127
5.05k
    if (v > HASH_MAX_SIZE) {
128
0
        return HASH_MAX_SIZE;
129
0
    }
130
131
    /* From Bit Twiddling Hacks by Sean Eron Anderson <seander@cs.stanford.edu>
132
     (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2). */
133
134
5.05k
    v--;
135
5.05k
    v |= v >> 1;
136
5.05k
    v |= v >> 2;
137
5.05k
    v |= v >> 4;
138
5.05k
    v |= v >> 8;
139
5.05k
    v |= v >> 16;
140
5.05k
    v++;
141
142
5.05k
    return v;
143
5.05k
}
144
145
146
9.04k
zip_hash_t *_zip_hash_new(zip_error_t *error) {
147
9.04k
    zip_hash_t *hash;
148
149
9.04k
    if ((hash = (zip_hash_t *)malloc(sizeof(zip_hash_t))) == NULL) {
150
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
151
0
        return NULL;
152
0
    }
153
154
9.04k
    hash->table_size = 0;
155
9.04k
    hash->nentries = 0;
156
9.04k
    hash->table = NULL;
157
9.04k
    if (!zip_secure_random(hash->hash_key, sizeof(hash->hash_key))) {
158
0
        free(hash);
159
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
160
0
        return NULL;
161
0
    }
162
163
9.04k
    return hash;
164
9.04k
}
165
166
167
9.04k
void _zip_hash_free(zip_hash_t *hash) {
168
9.04k
    zip_uint32_t i;
169
170
9.04k
    if (hash == NULL) {
171
0
        return;
172
0
    }
173
174
9.04k
    if (hash->table != NULL) {
175
55.7k
        for (i = 0; i < hash->table_size; i++) {
176
50.7k
            if (hash->table[i] != NULL) {
177
22.6k
                free_list(hash->table[i]);
178
22.6k
            }
179
50.7k
        }
180
5.05k
        free(hash->table);
181
5.05k
    }
182
    /* The hash key does not need to be securely erased. */
183
9.04k
    free(hash);
184
9.04k
}
185
186
187
/* insert into hash, return error on existence or memory issues */
188
28.2k
bool _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error) {
189
28.2k
    zip_uint64_t hash_value;
190
28.2k
    zip_uint32_t table_index;
191
28.2k
    zip_hash_entry_t *entry;
192
193
28.2k
    if (hash == NULL || name == NULL || index > ZIP_INT64_MAX) {
194
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
195
0
        return false;
196
0
    }
197
198
28.2k
    if (hash->table_size == 0) {
199
0
        if (!hash_resize(hash, HASH_MIN_SIZE, error)) {
200
0
            return false;
201
0
        }
202
0
    }
203
204
28.2k
    hash_value = siphash(name, hash->hash_key);
205
28.2k
    table_index = hash_value % hash->table_size;
206
207
34.6k
    for (entry = hash->table[table_index]; entry != NULL; entry = entry->next) {
208
6.43k
        if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) {
209
14
            if (((flags & ZIP_FL_UNCHANGED) && entry->orig_index != -1) || entry->current_index != -1) {
210
14
                zip_error_set(error, ZIP_ER_EXISTS, 0);
211
14
                return false;
212
14
            }
213
0
            else {
214
0
                break;
215
0
            }
216
14
        }
217
6.43k
    }
218
219
28.2k
    if (entry == NULL) {
220
28.2k
        if ((entry = (zip_hash_entry_t *)malloc(sizeof(zip_hash_entry_t))) == NULL) {
221
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
222
0
            return false;
223
0
        }
224
28.2k
        entry->name = name;
225
28.2k
        entry->next = hash->table[table_index];
226
28.2k
        hash->table[table_index] = entry;
227
28.2k
        entry->hash_value = hash_value;
228
28.2k
        entry->orig_index = -1;
229
28.2k
        hash->nentries++;
230
28.2k
        if (hash->nentries > hash->table_size * HASH_MAX_FILL && hash->table_size < HASH_MAX_SIZE) {
231
2.15k
            if (!hash_resize(hash, hash->table_size * 2, error)) {
232
0
                return false;
233
0
            }
234
2.15k
        }
235
28.2k
    }
236
237
28.2k
    if (flags & ZIP_FL_UNCHANGED) {
238
28.2k
        entry->orig_index = (zip_int64_t)index;
239
28.2k
    }
240
28.2k
    entry->current_index = (zip_int64_t)index;
241
242
28.2k
    return true;
243
28.2k
}
244
245
246
/* remove entry from hash, error if not found */
247
0
bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) {
248
0
    zip_uint64_t hash_value;
249
0
    zip_uint32_t index;
250
0
    zip_hash_entry_t *entry, *previous;
251
252
0
    if (hash == NULL || name == NULL) {
253
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
254
0
        return false;
255
0
    }
256
257
0
    if (hash->nentries > 0) {
258
0
        hash_value = siphash(name, hash->hash_key);
259
0
        index = hash_value % hash->table_size;
260
0
        previous = NULL;
261
0
        entry = hash->table[index];
262
0
        while (entry) {
263
0
            if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) {
264
0
                if (entry->orig_index == -1) {
265
0
                    if (previous) {
266
0
                        previous->next = entry->next;
267
0
                    }
268
0
                    else {
269
0
                        hash->table[index] = entry->next;
270
0
                    }
271
0
                    free(entry);
272
0
                    hash->nentries--;
273
0
                    if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) {
274
0
                        if (!hash_resize(hash, hash->table_size / 2, error)) {
275
0
                            return false;
276
0
                        }
277
0
                    }
278
0
                }
279
0
                else {
280
0
                    entry->current_index = -1;
281
0
                }
282
0
                return true;
283
0
            }
284
0
            previous = entry;
285
0
            entry = entry->next;
286
0
        }
287
0
    }
288
289
0
    zip_error_set(error, ZIP_ER_NOENT, 0);
290
0
    return false;
291
0
}
292
293
294
/* find value for entry in hash, -1 if not found */
295
40.4k
zip_int64_t _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error) {
296
40.4k
    zip_uint64_t hash_value;
297
40.4k
    zip_uint32_t index;
298
40.4k
    zip_hash_entry_t *entry;
299
300
40.4k
    if (hash == NULL || name == NULL) {
301
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
302
0
        return -1;
303
0
    }
304
305
40.4k
    if (hash->nentries > 0) {
306
39.4k
        hash_value = siphash(name, hash->hash_key);
307
39.4k
        index = hash_value % hash->table_size;
308
58.0k
        for (entry = hash->table[index]; entry != NULL; entry = entry->next) {
309
30.0k
            if (strcmp((const char *)name, (const char *)entry->name) == 0) {
310
11.4k
                if (flags & ZIP_FL_UNCHANGED) {
311
11.4k
                    if (entry->orig_index != -1) {
312
11.4k
                        return entry->orig_index;
313
11.4k
                    }
314
11.4k
                }
315
18
                else {
316
18
                    if (entry->current_index != -1) {
317
18
                        return entry->current_index;
318
18
                    }
319
18
                }
320
0
                break;
321
11.4k
            }
322
30.0k
        }
323
39.4k
    }
324
325
29.0k
    zip_error_set(error, ZIP_ER_NOENT, 0);
326
29.0k
    return -1;
327
40.4k
}
328
329
330
5.20k
bool _zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t *error) {
331
5.20k
    zip_uint32_t new_size;
332
333
5.20k
    if (capacity == 0) {
334
148
        return true;
335
148
    }
336
337
5.05k
    new_size = size_for_capacity(capacity);
338
339
5.05k
    if (new_size <= hash->table_size) {
340
0
        return true;
341
0
    }
342
343
5.05k
    if (!hash_resize(hash, new_size, error)) {
344
0
        return false;
345
0
    }
346
347
5.05k
    return true;
348
5.05k
}
349
350
351
0
bool _zip_hash_revert(zip_hash_t *hash, zip_error_t *error) {
352
0
    zip_uint32_t i;
353
0
    zip_hash_entry_t *entry, *previous;
354
355
0
    for (i = 0; i < hash->table_size; i++) {
356
0
        previous = NULL;
357
0
        entry = hash->table[i];
358
0
        while (entry) {
359
0
            if (entry->orig_index == -1) {
360
0
                zip_hash_entry_t *p;
361
0
                if (previous) {
362
0
                    previous->next = entry->next;
363
0
                }
364
0
                else {
365
0
                    hash->table[i] = entry->next;
366
0
                }
367
0
                p = entry;
368
0
                entry = entry->next;
369
                /* previous does not change */
370
0
                free(p);
371
0
                hash->nentries--;
372
0
            }
373
0
            else {
374
0
                entry->current_index = entry->orig_index;
375
0
                previous = entry;
376
0
                entry = entry->next;
377
0
            }
378
0
        }
379
0
    }
380
381
0
    if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) {
382
0
        zip_uint32_t new_size = hash->table_size / 2;
383
0
        while (hash->nentries < new_size * HASH_MIN_FILL && new_size > HASH_MIN_SIZE) {
384
0
            new_size /= 2;
385
0
        }
386
0
        if (!hash_resize(hash, new_size, error)) {
387
0
            return false;
388
0
        }
389
0
    }
390
391
0
    return true;
392
0
}