Coverage Report

Created: 2026-08-11 07:29

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 "zipint.h"
35
#include <stdlib.h>
36
#include <string.h>
37
38
/* parameter for the string hash function */
39
1.13M
#define HASH_MULTIPLIER 33
40
57.4k
#define HASH_START 5381
41
42
/* hash table's fill ratio is kept between these by doubling/halfing its size as necessary */
43
54.6k
#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
5.44k
#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_uint32_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
};
64
65
66
/* free list of entries */
67
20.1k
static void free_list(zip_hash_entry_t *entry) {
68
45.4k
    while (entry != NULL) {
69
25.2k
        zip_hash_entry_t *next = entry->next;
70
25.2k
        free(entry);
71
25.2k
        entry = next;
72
25.2k
    }
73
20.1k
}
74
75
76
/* compute hash of string, full 32 bit value */
77
57.4k
static zip_uint32_t hash_string(const zip_uint8_t *name) {
78
57.4k
    zip_uint64_t value = HASH_START;
79
80
57.4k
    if (name == NULL) {
81
0
        return 0;
82
0
    }
83
84
1.19M
    while (*name != 0) {
85
1.13M
        value = (zip_uint64_t)(((value * HASH_MULTIPLIER) + (zip_uint8_t)*name) % 0x100000000ul);
86
1.13M
        name++;
87
1.13M
    }
88
89
57.4k
    return (zip_uint32_t)value;
90
57.4k
}
91
92
93
/* resize hash table; new_size must be a power of 2, can be larger or smaller than current size */
94
5.44k
static bool hash_resize(zip_hash_t *hash, zip_uint32_t new_size, zip_error_t *error) {
95
5.44k
    zip_hash_entry_t **new_table;
96
97
5.44k
    if (new_size == hash->table_size) {
98
0
        return true;
99
0
    }
100
101
5.44k
    if ((new_table = (zip_hash_entry_t **)calloc(new_size, sizeof(zip_hash_entry_t *))) == NULL) {
102
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
103
0
        return false;
104
0
    }
105
106
5.44k
    if (hash->nentries > 0) {
107
1.39k
        zip_uint32_t i;
108
109
2.78k
        for (i = 0; i < hash->table_size; i++) {
110
1.39k
            zip_hash_entry_t *entry = hash->table[i];
111
2.78k
            while (entry) {
112
1.39k
                zip_hash_entry_t *next = entry->next;
113
114
1.39k
                zip_uint32_t new_index = entry->hash_value % new_size;
115
116
1.39k
                entry->next = new_table[new_index];
117
1.39k
                new_table[new_index] = entry;
118
119
1.39k
                entry = next;
120
1.39k
            }
121
1.39k
        }
122
1.39k
    }
123
124
5.44k
    free(hash->table);
125
5.44k
    hash->table = new_table;
126
5.44k
    hash->table_size = new_size;
127
128
5.44k
    return true;
129
5.44k
}
130
131
132
4.04k
static zip_uint32_t size_for_capacity(zip_uint64_t capacity) {
133
4.04k
    double needed_size = capacity / HASH_MAX_FILL;
134
4.04k
    zip_uint32_t v;
135
136
4.04k
    if (needed_size > ZIP_UINT32_MAX) {
137
0
        v = ZIP_UINT32_MAX;
138
0
    }
139
4.04k
    else {
140
4.04k
        v = (zip_uint32_t)needed_size;
141
4.04k
    }
142
143
4.04k
    if (v > HASH_MAX_SIZE) {
144
0
        return HASH_MAX_SIZE;
145
0
    }
146
147
    /* From Bit Twiddling Hacks by Sean Eron Anderson <seander@cs.stanford.edu>
148
     (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2). */
149
150
4.04k
    v--;
151
4.04k
    v |= v >> 1;
152
4.04k
    v |= v >> 2;
153
4.04k
    v |= v >> 4;
154
4.04k
    v |= v >> 8;
155
4.04k
    v |= v >> 16;
156
4.04k
    v++;
157
158
4.04k
    return v;
159
4.04k
}
160
161
162
6.61k
zip_hash_t *_zip_hash_new(zip_error_t *error) {
163
6.61k
    zip_hash_t *hash;
164
165
6.61k
    if ((hash = (zip_hash_t *)malloc(sizeof(zip_hash_t))) == NULL) {
166
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
167
0
        return NULL;
168
0
    }
169
170
6.61k
    hash->table_size = 0;
171
6.61k
    hash->nentries = 0;
172
6.61k
    hash->table = NULL;
173
174
6.61k
    return hash;
175
6.61k
}
176
177
178
6.61k
void _zip_hash_free(zip_hash_t *hash) {
179
6.61k
    zip_uint32_t i;
180
181
6.61k
    if (hash == NULL) {
182
0
        return;
183
0
    }
184
185
6.61k
    if (hash->table != NULL) {
186
49.3k
        for (i = 0; i < hash->table_size; i++) {
187
45.2k
            if (hash->table[i] != NULL) {
188
20.1k
                free_list(hash->table[i]);
189
20.1k
            }
190
45.2k
        }
191
4.04k
        free(hash->table);
192
4.04k
    }
193
6.61k
    free(hash);
194
6.61k
}
195
196
197
/* insert into hash, return error on existence or memory issues */
198
25.3k
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) {
199
25.3k
    zip_uint32_t hash_value, table_index;
200
25.3k
    zip_hash_entry_t *entry;
201
202
25.3k
    if (hash == NULL || name == NULL || index > ZIP_INT64_MAX) {
203
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
204
0
        return false;
205
0
    }
206
207
25.3k
    if (hash->table_size == 0) {
208
0
        if (!hash_resize(hash, HASH_MIN_SIZE, error)) {
209
0
            return false;
210
0
        }
211
0
    }
212
213
25.3k
    hash_value = hash_string(name);
214
25.3k
    table_index = hash_value % hash->table_size;
215
216
30.4k
    for (entry = hash->table[table_index]; entry != NULL; entry = entry->next) {
217
5.20k
        if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) {
218
14
            if (((flags & ZIP_FL_UNCHANGED) && entry->orig_index != -1) || entry->current_index != -1) {
219
14
                zip_error_set(error, ZIP_ER_EXISTS, 0);
220
14
                return false;
221
14
            }
222
0
            else {
223
0
                break;
224
0
            }
225
14
        }
226
5.20k
    }
227
228
25.2k
    if (entry == NULL) {
229
25.2k
        if ((entry = (zip_hash_entry_t *)malloc(sizeof(zip_hash_entry_t))) == NULL) {
230
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
231
0
            return false;
232
0
        }
233
25.2k
        entry->name = name;
234
25.2k
        entry->next = hash->table[table_index];
235
25.2k
        hash->table[table_index] = entry;
236
25.2k
        entry->hash_value = hash_value;
237
25.2k
        entry->orig_index = -1;
238
25.2k
        hash->nentries++;
239
25.2k
        if (hash->nentries > hash->table_size * HASH_MAX_FILL && hash->table_size < HASH_MAX_SIZE) {
240
1.39k
            if (!hash_resize(hash, hash->table_size * 2, error)) {
241
0
                return false;
242
0
            }
243
1.39k
        }
244
25.2k
    }
245
246
25.2k
    if (flags & ZIP_FL_UNCHANGED) {
247
25.2k
        entry->orig_index = (zip_int64_t)index;
248
25.2k
    }
249
25.2k
    entry->current_index = (zip_int64_t)index;
250
251
25.2k
    return true;
252
25.2k
}
253
254
255
/* remove entry from hash, error if not found */
256
0
bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) {
257
0
    zip_uint32_t hash_value, index;
258
0
    zip_hash_entry_t *entry, *previous;
259
260
0
    if (hash == NULL || name == NULL) {
261
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
262
0
        return false;
263
0
    }
264
265
0
    if (hash->nentries > 0) {
266
0
        hash_value = hash_string(name);
267
0
        index = hash_value % hash->table_size;
268
0
        previous = NULL;
269
0
        entry = hash->table[index];
270
0
        while (entry) {
271
0
            if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) {
272
0
                if (entry->orig_index == -1) {
273
0
                    if (previous) {
274
0
                        previous->next = entry->next;
275
0
                    }
276
0
                    else {
277
0
                        hash->table[index] = entry->next;
278
0
                    }
279
0
                    free(entry);
280
0
                    hash->nentries--;
281
0
                    if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) {
282
0
                        if (!hash_resize(hash, hash->table_size / 2, error)) {
283
0
                            return false;
284
0
                        }
285
0
                    }
286
0
                }
287
0
                else {
288
0
                    entry->current_index = -1;
289
0
                }
290
0
                return true;
291
0
            }
292
0
            previous = entry;
293
0
            entry = entry->next;
294
0
        }
295
0
    }
296
297
0
    zip_error_set(error, ZIP_ER_NOENT, 0);
298
0
    return false;
299
0
}
300
301
302
/* find value for entry in hash, -1 if not found */
303
33.0k
zip_int64_t _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error) {
304
33.0k
    zip_uint32_t hash_value, index;
305
33.0k
    zip_hash_entry_t *entry;
306
307
33.0k
    if (hash == NULL || name == NULL) {
308
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
309
0
        return -1;
310
0
    }
311
312
33.0k
    if (hash->nentries > 0) {
313
32.1k
        hash_value = hash_string(name);
314
32.1k
        index = hash_value % hash->table_size;
315
56.9k
        for (entry = hash->table[index]; entry != NULL; entry = entry->next) {
316
34.7k
            if (strcmp((const char *)name, (const char *)entry->name) == 0) {
317
10.0k
                if (flags & ZIP_FL_UNCHANGED) {
318
10.0k
                    if (entry->orig_index != -1) {
319
10.0k
                        return entry->orig_index;
320
10.0k
                    }
321
10.0k
                }
322
0
                else {
323
0
                    if (entry->current_index != -1) {
324
0
                        return entry->current_index;
325
0
                    }
326
0
                }
327
0
                break;
328
10.0k
            }
329
34.7k
        }
330
32.1k
    }
331
332
22.9k
    zip_error_set(error, ZIP_ER_NOENT, 0);
333
22.9k
    return -1;
334
33.0k
}
335
336
337
4.16k
bool _zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t *error) {
338
4.16k
    zip_uint32_t new_size;
339
340
4.16k
    if (capacity == 0) {
341
117
        return true;
342
117
    }
343
344
4.04k
    new_size = size_for_capacity(capacity);
345
346
4.04k
    if (new_size <= hash->table_size) {
347
0
        return true;
348
0
    }
349
350
4.04k
    if (!hash_resize(hash, new_size, error)) {
351
0
        return false;
352
0
    }
353
354
4.04k
    return true;
355
4.04k
}
356
357
358
0
bool _zip_hash_revert(zip_hash_t *hash, zip_error_t *error) {
359
0
    zip_uint32_t i;
360
0
    zip_hash_entry_t *entry, *previous;
361
362
0
    for (i = 0; i < hash->table_size; i++) {
363
0
        previous = NULL;
364
0
        entry = hash->table[i];
365
0
        while (entry) {
366
0
            if (entry->orig_index == -1) {
367
0
                zip_hash_entry_t *p;
368
0
                if (previous) {
369
0
                    previous->next = entry->next;
370
0
                }
371
0
                else {
372
0
                    hash->table[i] = entry->next;
373
0
                }
374
0
                p = entry;
375
0
                entry = entry->next;
376
                /* previous does not change */
377
0
                free(p);
378
0
                hash->nentries--;
379
0
            }
380
0
            else {
381
0
                entry->current_index = entry->orig_index;
382
0
                previous = entry;
383
0
                entry = entry->next;
384
0
            }
385
0
        }
386
0
    }
387
388
0
    if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) {
389
0
        zip_uint32_t new_size = hash->table_size / 2;
390
0
        while (hash->nentries < new_size * HASH_MIN_FILL && new_size > HASH_MIN_SIZE) {
391
0
            new_size /= 2;
392
0
        }
393
0
        if (!hash_resize(hash, new_size, error)) {
394
0
            return false;
395
0
        }
396
0
    }
397
398
0
    return true;
399
0
}