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