Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * hash.c: hash tables |
3 | | * |
4 | | * Hash table with open addressing, linear probing and |
5 | | * Robin Hood reordering. |
6 | | * |
7 | | * See Copyright for the status of this software. |
8 | | */ |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | |
13 | | #include <string.h> |
14 | | #include <limits.h> |
15 | | |
16 | | #include <libxml/parser.h> |
17 | | #include <libxml/hash.h> |
18 | | #include <libxml/dict.h> |
19 | | #include <libxml/xmlmemory.h> |
20 | | #include <libxml/xmlstring.h> |
21 | | |
22 | | #include "private/dict.h" |
23 | | |
24 | | #ifndef SIZE_MAX |
25 | 3.67k | #define SIZE_MAX ((size_t) -1) |
26 | | #endif |
27 | | |
28 | 17.8k | #define MAX_FILL_NUM 7 |
29 | 17.8k | #define MAX_FILL_DENOM 8 |
30 | 17.7k | #define MIN_HASH_SIZE 8 |
31 | 55.7k | #define MAX_HASH_SIZE (1u << 31) |
32 | | |
33 | | /* |
34 | | * A single entry in the hash table |
35 | | */ |
36 | | typedef struct { |
37 | | unsigned hashValue; /* 0 means unoccupied, occupied entries have the |
38 | | * MAX_HASH_SIZE bit set to 1 */ |
39 | | xmlChar *key; |
40 | | xmlChar *key2; /* TODO: Don't allocate possibly empty keys */ |
41 | | xmlChar *key3; |
42 | | void *payload; |
43 | | } xmlHashEntry; |
44 | | |
45 | | /* |
46 | | * The entire hash table |
47 | | */ |
48 | | struct _xmlHashTable { |
49 | | xmlHashEntry *table; |
50 | | unsigned size; /* power of two */ |
51 | | unsigned nbElems; |
52 | | xmlDictPtr dict; |
53 | | unsigned randomSeed; |
54 | | }; |
55 | | |
56 | | static int |
57 | | xmlHashGrow(xmlHashTablePtr hash, unsigned size); |
58 | | |
59 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
60 | | static unsigned |
61 | | xmlHashValue(unsigned seed, const xmlChar *key, const xmlChar *key2, |
62 | 50.7k | const xmlChar *key3, size_t *lengths) { |
63 | 50.7k | unsigned h1, h2; |
64 | 50.7k | size_t i; |
65 | | |
66 | 50.7k | HASH_INIT(h1, h2, seed); |
67 | | |
68 | 4.38M | for (i = 0; key[i] != 0; i++) { |
69 | 4.33M | HASH_UPDATE(h1, h2, key[i]); |
70 | 4.33M | } |
71 | 50.7k | if (lengths) |
72 | 17.8k | lengths[0] = i; |
73 | | |
74 | 50.7k | HASH_UPDATE(h1, h2, 0); |
75 | | |
76 | 50.7k | if (key2 != NULL) { |
77 | 0 | for (i = 0; key2[i] != 0; i++) { |
78 | 0 | HASH_UPDATE(h1, h2, key2[i]); |
79 | 0 | } |
80 | 0 | if (lengths) |
81 | 0 | lengths[1] = i; |
82 | 0 | } |
83 | | |
84 | 50.7k | HASH_UPDATE(h1, h2, 0); |
85 | | |
86 | 50.7k | if (key3 != NULL) { |
87 | 0 | for (i = 0; key3[i] != 0; i++) { |
88 | 0 | HASH_UPDATE(h1, h2, key3[i]); |
89 | 0 | } |
90 | 0 | if (lengths) |
91 | 0 | lengths[2] = i; |
92 | 0 | } |
93 | | |
94 | 50.7k | HASH_FINISH(h1, h2); |
95 | | |
96 | 50.7k | return(h2); |
97 | 50.7k | } |
98 | | |
99 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
100 | | static unsigned |
101 | | xmlHashQNameValue(unsigned seed, |
102 | | const xmlChar *prefix, const xmlChar *name, |
103 | | const xmlChar *prefix2, const xmlChar *name2, |
104 | 0 | const xmlChar *prefix3, const xmlChar *name3) { |
105 | 0 | unsigned h1, h2, ch; |
106 | |
|
107 | 0 | HASH_INIT(h1, h2, seed); |
108 | |
|
109 | 0 | if (prefix != NULL) { |
110 | 0 | while ((ch = *prefix++) != 0) { |
111 | 0 | HASH_UPDATE(h1, h2, ch); |
112 | 0 | } |
113 | 0 | HASH_UPDATE(h1, h2, ':'); |
114 | 0 | } |
115 | 0 | if (name != NULL) { |
116 | 0 | while ((ch = *name++) != 0) { |
117 | 0 | HASH_UPDATE(h1, h2, ch); |
118 | 0 | } |
119 | 0 | } |
120 | 0 | HASH_UPDATE(h1, h2, 0); |
121 | 0 | if (prefix2 != NULL) { |
122 | 0 | while ((ch = *prefix2++) != 0) { |
123 | 0 | HASH_UPDATE(h1, h2, ch); |
124 | 0 | } |
125 | 0 | HASH_UPDATE(h1, h2, ':'); |
126 | 0 | } |
127 | 0 | if (name2 != NULL) { |
128 | 0 | while ((ch = *name2++) != 0) { |
129 | 0 | HASH_UPDATE(h1, h2, ch); |
130 | 0 | } |
131 | 0 | } |
132 | 0 | HASH_UPDATE(h1, h2, 0); |
133 | 0 | if (prefix3 != NULL) { |
134 | 0 | while ((ch = *prefix3++) != 0) { |
135 | 0 | HASH_UPDATE(h1, h2, ch); |
136 | 0 | } |
137 | 0 | HASH_UPDATE(h1, h2, ':'); |
138 | 0 | } |
139 | 0 | if (name3 != NULL) { |
140 | 0 | while ((ch = *name3++) != 0) { |
141 | 0 | HASH_UPDATE(h1, h2, ch); |
142 | 0 | } |
143 | 0 | } |
144 | |
|
145 | 0 | HASH_FINISH(h1, h2); |
146 | |
|
147 | 0 | return(h2); |
148 | 0 | } |
149 | | |
150 | | /** |
151 | | * xmlHashCreate: |
152 | | * @size: initial size of the hash table |
153 | | * |
154 | | * Create a new hash table. Set size to zero if the number of entries |
155 | | * can't be estimated. |
156 | | * |
157 | | * Returns the newly created object, or NULL if a memory allocation failed. |
158 | | */ |
159 | | xmlHashTablePtr |
160 | 15.0k | xmlHashCreate(int size) { |
161 | 15.0k | xmlHashTablePtr hash; |
162 | | |
163 | 15.0k | xmlInitParser(); |
164 | | |
165 | 15.0k | hash = xmlMalloc(sizeof(*hash)); |
166 | 15.0k | if (hash == NULL) |
167 | 14 | return(NULL); |
168 | 15.0k | hash->dict = NULL; |
169 | 15.0k | hash->size = 0; |
170 | 15.0k | hash->table = NULL; |
171 | 15.0k | hash->nbElems = 0; |
172 | 15.0k | hash->randomSeed = xmlRandom(); |
173 | 15.0k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
174 | 15.0k | hash->randomSeed = 0; |
175 | 15.0k | #endif |
176 | | |
177 | | /* |
178 | | * Unless a larger size is passed, the backing table is created |
179 | | * lazily with MIN_HASH_SIZE capacity. In practice, there are many |
180 | | * hash tables which are never filled. |
181 | | */ |
182 | 15.0k | if (size > MIN_HASH_SIZE) { |
183 | 0 | unsigned newSize = MIN_HASH_SIZE * 2; |
184 | |
|
185 | 0 | while ((newSize < (unsigned) size) && (newSize < MAX_HASH_SIZE)) |
186 | 0 | newSize *= 2; |
187 | |
|
188 | 0 | if (xmlHashGrow(hash, newSize) != 0) { |
189 | 0 | xmlFree(hash); |
190 | 0 | return(NULL); |
191 | 0 | } |
192 | 0 | } |
193 | | |
194 | 15.0k | return(hash); |
195 | 15.0k | } |
196 | | |
197 | | /** |
198 | | * xmlHashCreateDict: |
199 | | * @size: the size of the hash table |
200 | | * @dict: a dictionary to use for the hash |
201 | | * |
202 | | * Create a new hash table backed by a dictionary. This can reduce |
203 | | * resource usage considerably if most keys passed to API functions |
204 | | * originate from this dictionary. |
205 | | * |
206 | | * Returns the newly created object, or NULL if a memory allocation failed. |
207 | | */ |
208 | | xmlHashTablePtr |
209 | 2.80k | xmlHashCreateDict(int size, xmlDictPtr dict) { |
210 | 2.80k | xmlHashTablePtr hash; |
211 | | |
212 | 2.80k | hash = xmlHashCreate(size); |
213 | 2.80k | if (hash != NULL) { |
214 | 2.78k | hash->dict = dict; |
215 | 2.78k | xmlDictReference(dict); |
216 | 2.78k | } |
217 | 2.80k | return(hash); |
218 | 2.80k | } |
219 | | |
220 | | /** |
221 | | * xmlHashFree: |
222 | | * @hash: hash table |
223 | | * @dealloc: deallocator function or NULL |
224 | | * |
225 | | * Free the hash and its contents. The payload is deallocated with |
226 | | * @dealloc if provided. |
227 | | */ |
228 | | void |
229 | 15.0k | xmlHashFree(xmlHashTablePtr hash, xmlHashDeallocator dealloc) { |
230 | 15.0k | if (hash == NULL) |
231 | 0 | return; |
232 | | |
233 | 15.0k | if (hash->table) { |
234 | 2.75k | const xmlHashEntry *end = &hash->table[hash->size]; |
235 | 2.75k | const xmlHashEntry *entry; |
236 | | |
237 | 45.3k | for (entry = hash->table; entry < end; entry++) { |
238 | 42.6k | if (entry->hashValue == 0) |
239 | 25.7k | continue; |
240 | 16.8k | if ((dealloc != NULL) && (entry->payload != NULL)) |
241 | 16.8k | dealloc(entry->payload, entry->key); |
242 | 16.8k | if (hash->dict == NULL) { |
243 | 16.8k | if (entry->key) |
244 | 16.8k | xmlFree(entry->key); |
245 | 16.8k | if (entry->key2) |
246 | 0 | xmlFree(entry->key2); |
247 | 16.8k | if (entry->key3) |
248 | 0 | xmlFree(entry->key3); |
249 | 16.8k | } |
250 | 16.8k | } |
251 | | |
252 | 2.75k | xmlFree(hash->table); |
253 | 2.75k | } |
254 | | |
255 | 15.0k | if (hash->dict) |
256 | 0 | xmlDictFree(hash->dict); |
257 | | |
258 | 15.0k | xmlFree(hash); |
259 | 15.0k | } |
260 | | |
261 | | /** |
262 | | * xmlFastStrEqual: |
263 | | * @s1: string |
264 | | * @s2: string |
265 | | * |
266 | | * Compare two strings for equality, allowing NULL values. |
267 | | */ |
268 | | static int |
269 | 35.7k | xmlFastStrEqual(const xmlChar *s1, const xmlChar *s2) { |
270 | 35.7k | if (s1 == NULL) |
271 | 35.7k | return(s2 == NULL); |
272 | 0 | else |
273 | 0 | return((s2 != NULL) && |
274 | 0 | (strcmp((const char *) s1, (const char *) s2) == 0)); |
275 | 35.7k | } |
276 | | |
277 | | /** |
278 | | * xmlHashFindEntry: |
279 | | * @hash: hash table, non-NULL, size > 0 |
280 | | * @key: first string key, non-NULL |
281 | | * @key2: second string key |
282 | | * @key3: third string key |
283 | | * @hashValue: valid hash value of keys |
284 | | * @pfound: result of search |
285 | | * |
286 | | * Try to find a matching hash table entry. If an entry was found, set |
287 | | * @found to 1 and return the entry. Otherwise, set @found to 0 and return |
288 | | * the location where a new entry should be inserted. |
289 | | */ |
290 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
291 | | static xmlHashEntry * |
292 | | xmlHashFindEntry(const xmlHashTable *hash, const xmlChar *key, |
293 | | const xmlChar *key2, const xmlChar *key3, |
294 | 47.9k | unsigned hashValue, int *pfound) { |
295 | 47.9k | xmlHashEntry *entry; |
296 | 47.9k | unsigned mask, pos, displ; |
297 | 47.9k | int found = 0; |
298 | | |
299 | 47.9k | mask = hash->size - 1; |
300 | 47.9k | pos = hashValue & mask; |
301 | 47.9k | entry = &hash->table[pos]; |
302 | | |
303 | 47.9k | if (entry->hashValue != 0) { |
304 | | /* |
305 | | * Robin hood hashing: abort if the displacement of the entry |
306 | | * is smaller than the displacement of the key we look for. |
307 | | * This also stops at the correct position when inserting. |
308 | | */ |
309 | 37.0k | displ = 0; |
310 | 37.0k | hashValue |= MAX_HASH_SIZE; |
311 | | |
312 | 77.5k | do { |
313 | 77.5k | if (entry->hashValue == hashValue) { |
314 | 18.9k | if (hash->dict) { |
315 | 0 | if ((entry->key == key) && |
316 | 0 | (entry->key2 == key2) && |
317 | 0 | (entry->key3 == key3)) { |
318 | 0 | found = 1; |
319 | 0 | break; |
320 | 0 | } |
321 | 0 | } |
322 | 18.9k | if ((strcmp((const char *) entry->key, |
323 | 18.9k | (const char *) key) == 0) && |
324 | 18.9k | (xmlFastStrEqual(entry->key2, key2)) && |
325 | 18.9k | (xmlFastStrEqual(entry->key3, key3))) { |
326 | 17.8k | found = 1; |
327 | 17.8k | break; |
328 | 17.8k | } |
329 | 18.9k | } |
330 | | |
331 | 59.6k | displ++; |
332 | 59.6k | pos++; |
333 | 59.6k | entry++; |
334 | 59.6k | if ((pos & mask) == 0) |
335 | 2.27k | entry = hash->table; |
336 | 59.6k | } while ((entry->hashValue != 0) && |
337 | 59.6k | (((pos - entry->hashValue) & mask) >= displ)); |
338 | 37.0k | } |
339 | | |
340 | 0 | *pfound = found; |
341 | 47.9k | return(entry); |
342 | 47.9k | } |
343 | | |
344 | | /** |
345 | | * xmlHashGrow: |
346 | | * @hash: hash table |
347 | | * @size: new size of the hash table |
348 | | * |
349 | | * Resize the hash table. |
350 | | * |
351 | | * Returns 0 in case of success, -1 if a memory allocation failed. |
352 | | */ |
353 | | static int |
354 | 3.67k | xmlHashGrow(xmlHashTablePtr hash, unsigned size) { |
355 | 3.67k | const xmlHashEntry *oldentry, *oldend, *end; |
356 | 3.67k | xmlHashEntry *table; |
357 | 3.67k | unsigned oldsize, i; |
358 | | |
359 | | /* Add 0 to avoid spurious -Wtype-limits warning on 64-bit GCC */ |
360 | 3.67k | if ((size_t) size + 0 > SIZE_MAX / sizeof(table[0])) |
361 | 0 | return(-1); |
362 | 3.67k | table = xmlMalloc(size * sizeof(table[0])); |
363 | 3.67k | if (table == NULL) |
364 | 12 | return(-1); |
365 | 3.65k | memset(table, 0, size * sizeof(table[0])); |
366 | | |
367 | 3.65k | oldsize = hash->size; |
368 | 3.65k | if (oldsize == 0) |
369 | 2.75k | goto done; |
370 | | |
371 | 904 | oldend = &hash->table[oldsize]; |
372 | 904 | end = &table[size]; |
373 | | |
374 | | /* |
375 | | * Robin Hood sorting order is maintained if we |
376 | | * |
377 | | * - compute hash indices with modulo |
378 | | * - resize by an integer factor |
379 | | * - start to copy from the beginning of a probe sequence |
380 | | */ |
381 | 904 | oldentry = hash->table; |
382 | 6.40k | while (oldentry->hashValue != 0) { |
383 | 5.49k | if (++oldentry >= oldend) |
384 | 0 | oldentry = hash->table; |
385 | 5.49k | } |
386 | | |
387 | 21.4k | for (i = 0; i < oldsize; i++) { |
388 | 20.5k | if (oldentry->hashValue != 0) { |
389 | 18.0k | xmlHashEntry *entry = &table[oldentry->hashValue & (size - 1)]; |
390 | | |
391 | 29.6k | while (entry->hashValue != 0) { |
392 | 11.6k | if (++entry >= end) |
393 | 265 | entry = table; |
394 | 11.6k | } |
395 | 18.0k | *entry = *oldentry; |
396 | 18.0k | } |
397 | | |
398 | 20.5k | if (++oldentry >= oldend) |
399 | 904 | oldentry = hash->table; |
400 | 20.5k | } |
401 | | |
402 | 904 | xmlFree(hash->table); |
403 | | |
404 | 3.65k | done: |
405 | 3.65k | hash->table = table; |
406 | 3.65k | hash->size = size; |
407 | | |
408 | 3.65k | return(0); |
409 | 904 | } |
410 | | |
411 | | /** |
412 | | * xmlHashUpdateInternal: |
413 | | * @hash: hash table |
414 | | * @key: first string key |
415 | | * @key2: second string key |
416 | | * @key3: third string key |
417 | | * @payload: pointer to the payload |
418 | | * @dealloc: deallocator function for replaced item or NULL |
419 | | * @update: whether existing entries should be updated |
420 | | * |
421 | | * Internal function to add or update hash entries. |
422 | | */ |
423 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
424 | | static int |
425 | | xmlHashUpdateInternal(xmlHashTablePtr hash, const xmlChar *key, |
426 | | const xmlChar *key2, const xmlChar *key3, |
427 | 17.8k | void *payload, xmlHashDeallocator dealloc, int update) { |
428 | 17.8k | xmlChar *copy, *copy2, *copy3; |
429 | 17.8k | xmlHashEntry *entry = NULL; |
430 | 17.8k | size_t lengths[3]; |
431 | 17.8k | unsigned hashValue; |
432 | 17.8k | int found = 0; |
433 | | |
434 | 17.8k | if ((hash == NULL) || (key == NULL)) |
435 | 0 | return(-1); |
436 | | |
437 | | /* |
438 | | * Check for an existing entry |
439 | | */ |
440 | 17.8k | hashValue = xmlHashValue(hash->randomSeed, key, key2, key3, lengths); |
441 | 17.8k | if (hash->size > 0) |
442 | 15.0k | entry = xmlHashFindEntry(hash, key, key2, key3, hashValue, &found); |
443 | 17.8k | if (found) { |
444 | 0 | if (update) { |
445 | 0 | if (dealloc) |
446 | 0 | dealloc(entry->payload, entry->key); |
447 | 0 | entry->payload = payload; |
448 | 0 | } |
449 | |
|
450 | 0 | return(0); |
451 | 0 | } |
452 | | |
453 | | /* |
454 | | * Grow the hash table if needed |
455 | | */ |
456 | 17.8k | if (hash->nbElems + 1 > hash->size / MAX_FILL_DENOM * MAX_FILL_NUM) { |
457 | 3.67k | unsigned newSize, mask, displ, pos; |
458 | | |
459 | 3.67k | if (hash->size == 0) { |
460 | 2.76k | newSize = MIN_HASH_SIZE; |
461 | 2.76k | } else { |
462 | | /* This guarantees that nbElems < INT_MAX */ |
463 | 904 | if (hash->size >= MAX_HASH_SIZE) |
464 | 0 | return(-1); |
465 | 904 | newSize = hash->size * 2; |
466 | 904 | } |
467 | 3.67k | if (xmlHashGrow(hash, newSize) != 0) |
468 | 12 | return(-1); |
469 | | |
470 | | /* |
471 | | * Find new entry |
472 | | */ |
473 | 3.65k | mask = hash->size - 1; |
474 | 3.65k | displ = 0; |
475 | 3.65k | pos = hashValue & mask; |
476 | 3.65k | entry = &hash->table[pos]; |
477 | | |
478 | 3.65k | if (entry->hashValue != 0) { |
479 | 1.20k | do { |
480 | 1.20k | displ++; |
481 | 1.20k | pos++; |
482 | 1.20k | entry++; |
483 | 1.20k | if ((pos & mask) == 0) |
484 | 63 | entry = hash->table; |
485 | 1.20k | } while ((entry->hashValue != 0) && |
486 | 1.20k | ((pos - entry->hashValue) & mask) >= displ); |
487 | 594 | } |
488 | 3.65k | } |
489 | | |
490 | | /* |
491 | | * Copy keys |
492 | | */ |
493 | 17.8k | if (hash->dict != NULL) { |
494 | 0 | if (xmlDictOwns(hash->dict, key)) { |
495 | 0 | copy = (xmlChar *) key; |
496 | 0 | } else { |
497 | 0 | copy = (xmlChar *) xmlDictLookup(hash->dict, key, -1); |
498 | 0 | if (copy == NULL) |
499 | 0 | return(-1); |
500 | 0 | } |
501 | | |
502 | 0 | if ((key2 == NULL) || (xmlDictOwns(hash->dict, key2))) { |
503 | 0 | copy2 = (xmlChar *) key2; |
504 | 0 | } else { |
505 | 0 | copy2 = (xmlChar *) xmlDictLookup(hash->dict, key2, -1); |
506 | 0 | if (copy2 == NULL) |
507 | 0 | return(-1); |
508 | 0 | } |
509 | 0 | if ((key3 == NULL) || (xmlDictOwns(hash->dict, key3))) { |
510 | 0 | copy3 = (xmlChar *) key3; |
511 | 0 | } else { |
512 | 0 | copy3 = (xmlChar *) xmlDictLookup(hash->dict, key3, -1); |
513 | 0 | if (copy3 == NULL) |
514 | 0 | return(-1); |
515 | 0 | } |
516 | 17.8k | } else { |
517 | 17.8k | copy = xmlMalloc(lengths[0] + 1); |
518 | 17.8k | if (copy == NULL) |
519 | 18 | return(-1); |
520 | 17.8k | memcpy(copy, key, lengths[0] + 1); |
521 | | |
522 | 17.8k | if (key2 != NULL) { |
523 | 0 | copy2 = xmlMalloc(lengths[1] + 1); |
524 | 0 | if (copy2 == NULL) { |
525 | 0 | xmlFree(copy); |
526 | 0 | return(-1); |
527 | 0 | } |
528 | 0 | memcpy(copy2, key2, lengths[1] + 1); |
529 | 17.8k | } else { |
530 | 17.8k | copy2 = NULL; |
531 | 17.8k | } |
532 | | |
533 | 17.8k | if (key3 != NULL) { |
534 | 0 | copy3 = xmlMalloc(lengths[2] + 1); |
535 | 0 | if (copy3 == NULL) { |
536 | 0 | xmlFree(copy); |
537 | 0 | xmlFree(copy2); |
538 | 0 | return(-1); |
539 | 0 | } |
540 | 0 | memcpy(copy3, key3, lengths[2] + 1); |
541 | 17.8k | } else { |
542 | 17.8k | copy3 = NULL; |
543 | 17.8k | } |
544 | 17.8k | } |
545 | | |
546 | | /* |
547 | | * Shift the remainder of the probe sequence to the right |
548 | | */ |
549 | 17.8k | if (entry->hashValue != 0) { |
550 | 5.45k | const xmlHashEntry *end = &hash->table[hash->size]; |
551 | 5.45k | const xmlHashEntry *cur = entry; |
552 | | |
553 | 26.4k | do { |
554 | 26.4k | cur++; |
555 | 26.4k | if (cur >= end) |
556 | 407 | cur = hash->table; |
557 | 26.4k | } while (cur->hashValue != 0); |
558 | | |
559 | 5.45k | if (cur < entry) { |
560 | | /* |
561 | | * If we traversed the end of the buffer, handle the part |
562 | | * at the start of the buffer. |
563 | | */ |
564 | 407 | memmove(&hash->table[1], hash->table, |
565 | 407 | (char *) cur - (char *) hash->table); |
566 | 407 | cur = end - 1; |
567 | 407 | hash->table[0] = *cur; |
568 | 407 | } |
569 | | |
570 | 5.45k | memmove(&entry[1], entry, (char *) cur - (char *) entry); |
571 | 5.45k | } |
572 | | |
573 | | /* |
574 | | * Populate entry |
575 | | */ |
576 | 17.8k | entry->key = copy; |
577 | 17.8k | entry->key2 = copy2; |
578 | 17.8k | entry->key3 = copy3; |
579 | 17.8k | entry->payload = payload; |
580 | | /* OR with MAX_HASH_SIZE to make sure that the value is non-zero */ |
581 | 17.8k | entry->hashValue = hashValue | MAX_HASH_SIZE; |
582 | | |
583 | 17.8k | hash->nbElems++; |
584 | | |
585 | 17.8k | return(1); |
586 | 17.8k | } |
587 | | |
588 | | /** |
589 | | * xmlHashDefaultDeallocator: |
590 | | * @entry: hash table entry |
591 | | * @key: the entry's string key |
592 | | * |
593 | | * Free a hash table entry with xmlFree. |
594 | | */ |
595 | | void |
596 | 0 | xmlHashDefaultDeallocator(void *entry, const xmlChar *key ATTRIBUTE_UNUSED) { |
597 | 0 | xmlFree(entry); |
598 | 0 | } |
599 | | |
600 | | /** |
601 | | * xmlHashAdd: |
602 | | * @hash: hash table |
603 | | * @key: string key |
604 | | * @payload: pointer to the payload |
605 | | * |
606 | | * Add a hash table entry. If an entry with this key already exists, |
607 | | * payload will not be updated and 0 is returned. This return value |
608 | | * can't be distinguished from out-of-memory errors, so this function |
609 | | * should be used with care. |
610 | | * |
611 | | * Returns 1 on success, 0 if an entry exists and -1 in case of error. |
612 | | */ |
613 | | int |
614 | 0 | xmlHashAdd(xmlHashTablePtr hash, const xmlChar *key, void *payload) { |
615 | 0 | return(xmlHashUpdateInternal(hash, key, NULL, NULL, payload, NULL, 0)); |
616 | 0 | } |
617 | | |
618 | | /** |
619 | | * xmlHashAdd2: |
620 | | * @hash: hash table |
621 | | * @key: first string key |
622 | | * @key2: second string key |
623 | | * @payload: pointer to the payload |
624 | | * |
625 | | * Add a hash table entry with two strings as key. |
626 | | * |
627 | | * See xmlHashAdd. |
628 | | */ |
629 | | int |
630 | | xmlHashAdd2(xmlHashTablePtr hash, const xmlChar *key, |
631 | 0 | const xmlChar *key2, void *payload) { |
632 | 0 | return(xmlHashUpdateInternal(hash, key, key2, NULL, payload, NULL, 0)); |
633 | 0 | } |
634 | | |
635 | | /** |
636 | | * xmlHashAdd3: |
637 | | * @hash: hash table |
638 | | * @key: first string key |
639 | | * @key2: second string key |
640 | | * @key3: third string key |
641 | | * @payload: pointer to the payload |
642 | | * |
643 | | * Add a hash table entry with three strings as key. |
644 | | * |
645 | | * See xmlHashAdd. |
646 | | */ |
647 | | int |
648 | | xmlHashAdd3(xmlHashTablePtr hash, const xmlChar *key, |
649 | | const xmlChar *key2, const xmlChar *key3, |
650 | 0 | void *payload) { |
651 | 0 | return(xmlHashUpdateInternal(hash, key, key2, key3, payload, NULL, 0)); |
652 | 0 | } |
653 | | |
654 | | /** |
655 | | * xmlHashAddEntry: |
656 | | * @hash: hash table |
657 | | * @key: string key |
658 | | * @payload: pointer to the payload |
659 | | * |
660 | | * Add a hash table entry. If an entry with this key already exists, |
661 | | * payload will not be updated and -1 is returned. This return value |
662 | | * can't be distinguished from out-of-memory errors, so this function |
663 | | * should be used with care. |
664 | | * |
665 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
666 | | * existing entries. Use xmlHashAdd instead. |
667 | | * |
668 | | * Returns 0 on success and -1 in case of error. |
669 | | */ |
670 | | int |
671 | 17.8k | xmlHashAddEntry(xmlHashTablePtr hash, const xmlChar *key, void *payload) { |
672 | 17.8k | int res = xmlHashUpdateInternal(hash, key, NULL, NULL, payload, NULL, 0); |
673 | | |
674 | 17.8k | if (res == 0) |
675 | 0 | res = -1; |
676 | 17.8k | else if (res == 1) |
677 | 17.8k | res = 0; |
678 | | |
679 | 17.8k | return(res); |
680 | 17.8k | } |
681 | | |
682 | | /** |
683 | | * xmlHashAddEntry2: |
684 | | * @hash: hash table |
685 | | * @key: first string key |
686 | | * @key2: second string key |
687 | | * @payload: pointer to the payload |
688 | | * |
689 | | * Add a hash table entry with two strings as key. |
690 | | * |
691 | | * See xmlHashAddEntry. |
692 | | * |
693 | | * Returns 0 on success and -1 in case of error. |
694 | | */ |
695 | | int |
696 | | xmlHashAddEntry2(xmlHashTablePtr hash, const xmlChar *key, |
697 | 0 | const xmlChar *key2, void *payload) { |
698 | 0 | int res = xmlHashUpdateInternal(hash, key, key2, NULL, payload, NULL, 0); |
699 | |
|
700 | 0 | if (res == 0) |
701 | 0 | res = -1; |
702 | 0 | else if (res == 1) |
703 | 0 | res = 0; |
704 | |
|
705 | 0 | return(res); |
706 | 0 | } |
707 | | |
708 | | /** |
709 | | * xmlHashAddEntry3: |
710 | | * @hash: hash table |
711 | | * @key: first string key |
712 | | * @key2: second string key |
713 | | * @key3: third string key |
714 | | * @payload: pointer to the payload |
715 | | * |
716 | | * Add a hash table entry with three strings as key. |
717 | | * |
718 | | * See xmlHashAddEntry. |
719 | | * |
720 | | * Returns 0 on success and -1 in case of error. |
721 | | */ |
722 | | int |
723 | | xmlHashAddEntry3(xmlHashTablePtr hash, const xmlChar *key, |
724 | | const xmlChar *key2, const xmlChar *key3, |
725 | 0 | void *payload) { |
726 | 0 | int res = xmlHashUpdateInternal(hash, key, key2, key3, payload, NULL, 0); |
727 | |
|
728 | 0 | if (res == 0) |
729 | 0 | res = -1; |
730 | 0 | else if (res == 1) |
731 | 0 | res = 0; |
732 | |
|
733 | 0 | return(res); |
734 | 0 | } |
735 | | |
736 | | /** |
737 | | * xmlHashUpdateEntry: |
738 | | * @hash: hash table |
739 | | * @key: string key |
740 | | * @payload: pointer to the payload |
741 | | * @dealloc: deallocator function for replaced item or NULL |
742 | | * |
743 | | * Add a hash table entry. If an entry with this key already exists, |
744 | | * the old payload will be freed and updated with the new value. |
745 | | * |
746 | | * Returns 0 in case of success, -1 if a memory allocation failed. |
747 | | */ |
748 | | int |
749 | | xmlHashUpdateEntry(xmlHashTablePtr hash, const xmlChar *key, |
750 | 0 | void *payload, xmlHashDeallocator dealloc) { |
751 | 0 | int res = xmlHashUpdateInternal(hash, key, NULL, NULL, payload, |
752 | 0 | dealloc, 1); |
753 | |
|
754 | 0 | if (res == 1) |
755 | 0 | res = 0; |
756 | |
|
757 | 0 | return(res); |
758 | 0 | } |
759 | | |
760 | | /** |
761 | | * xmlHashUpdateEntry2: |
762 | | * @hash: hash table |
763 | | * @key: first string key |
764 | | * @key2: second string key |
765 | | * @payload: pointer to the payload |
766 | | * @dealloc: deallocator function for replaced item or NULL |
767 | | * |
768 | | * Add a hash table entry with two strings as key. |
769 | | * |
770 | | * See xmlHashUpdateEntry. |
771 | | * |
772 | | * Returns 0 on success and -1 in case of error. |
773 | | */ |
774 | | int |
775 | | xmlHashUpdateEntry2(xmlHashTablePtr hash, const xmlChar *key, |
776 | | const xmlChar *key2, void *payload, |
777 | 0 | xmlHashDeallocator dealloc) { |
778 | 0 | int res = xmlHashUpdateInternal(hash, key, key2, NULL, payload, |
779 | 0 | dealloc, 1); |
780 | |
|
781 | 0 | if (res == 1) |
782 | 0 | res = 0; |
783 | |
|
784 | 0 | return(res); |
785 | 0 | } |
786 | | |
787 | | /** |
788 | | * xmlHashUpdateEntry3: |
789 | | * @hash: hash table |
790 | | * @key: first string key |
791 | | * @key2: second string key |
792 | | * @key3: third string key |
793 | | * @payload: pointer to the payload |
794 | | * @dealloc: deallocator function for replaced item or NULL |
795 | | * |
796 | | * Add a hash table entry with three strings as key. |
797 | | * |
798 | | * See xmlHashUpdateEntry. |
799 | | * |
800 | | * Returns 0 on success and -1 in case of error. |
801 | | */ |
802 | | int |
803 | | xmlHashUpdateEntry3(xmlHashTablePtr hash, const xmlChar *key, |
804 | | const xmlChar *key2, const xmlChar *key3, |
805 | 0 | void *payload, xmlHashDeallocator dealloc) { |
806 | 0 | int res = xmlHashUpdateInternal(hash, key, key2, key3, payload, |
807 | 0 | dealloc, 1); |
808 | |
|
809 | 0 | if (res == 1) |
810 | 0 | res = 0; |
811 | |
|
812 | 0 | return(res); |
813 | 0 | } |
814 | | |
815 | | /** |
816 | | * xmlHashLookup: |
817 | | * @hash: hash table |
818 | | * @key: string key |
819 | | * |
820 | | * Find the entry specified by @key. |
821 | | * |
822 | | * Returns a pointer to the payload or NULL if no entry was found. |
823 | | */ |
824 | | void * |
825 | 31.9k | xmlHashLookup(xmlHashTablePtr hash, const xmlChar *key) { |
826 | 31.9k | return(xmlHashLookup3(hash, key, NULL, NULL)); |
827 | 31.9k | } |
828 | | |
829 | | /** |
830 | | * xmlHashLookup2: |
831 | | * @hash: hash table |
832 | | * @key: first string key |
833 | | * @key2: second string key |
834 | | * |
835 | | * Find the payload specified by the (@key, @key2) tuple. |
836 | | * |
837 | | * Returns a pointer to the payload or NULL if no entry was found. |
838 | | */ |
839 | | void * |
840 | | xmlHashLookup2(xmlHashTablePtr hash, const xmlChar *key, |
841 | 0 | const xmlChar *key2) { |
842 | 0 | return(xmlHashLookup3(hash, key, key2, NULL)); |
843 | 0 | } |
844 | | |
845 | | /** |
846 | | * xmlHashQLookup: |
847 | | * @hash: hash table |
848 | | * @prefix: prefix of the string key |
849 | | * @name: local name of the string key |
850 | | * |
851 | | * Find the payload specified by the QName @prefix:@name or @name. |
852 | | * |
853 | | * Returns a pointer to the payload or NULL if no entry was found. |
854 | | */ |
855 | | void * |
856 | | xmlHashQLookup(xmlHashTablePtr hash, const xmlChar *prefix, |
857 | 0 | const xmlChar *name) { |
858 | 0 | return(xmlHashQLookup3(hash, prefix, name, NULL, NULL, NULL, NULL)); |
859 | 0 | } |
860 | | |
861 | | /** |
862 | | * xmlHashQLookup2: |
863 | | * @hash: hash table |
864 | | * @prefix: first prefix |
865 | | * @name: first local name |
866 | | * @prefix2: second prefix |
867 | | * @name2: second local name |
868 | | * |
869 | | * Find the payload specified by the QNames tuple. |
870 | | * |
871 | | * Returns a pointer to the payload or NULL if no entry was found. |
872 | | */ |
873 | | void * |
874 | | xmlHashQLookup2(xmlHashTablePtr hash, const xmlChar *prefix, |
875 | | const xmlChar *name, const xmlChar *prefix2, |
876 | 0 | const xmlChar *name2) { |
877 | 0 | return(xmlHashQLookup3(hash, prefix, name, prefix2, name2, NULL, NULL)); |
878 | 0 | } |
879 | | |
880 | | /** |
881 | | * xmlHashLookup3: |
882 | | * @hash: hash table |
883 | | * @key: first string key |
884 | | * @key2: second string key |
885 | | * @key3: third string key |
886 | | * |
887 | | * Find the payload specified by the (@key, @key2, @key3) tuple. |
888 | | * |
889 | | * Returns a pointer to the payload or NULL if no entry was found. |
890 | | */ |
891 | | void * |
892 | | xmlHashLookup3(xmlHashTablePtr hash, const xmlChar *key, |
893 | 73.0k | const xmlChar *key2, const xmlChar *key3) { |
894 | 73.0k | const xmlHashEntry *entry; |
895 | 73.0k | unsigned hashValue; |
896 | 73.0k | int found; |
897 | | |
898 | 73.0k | if ((hash == NULL) || (hash->size == 0) || (key == NULL)) |
899 | 41.1k | return(NULL); |
900 | 31.9k | hashValue = xmlHashValue(hash->randomSeed, key, key2, key3, NULL); |
901 | 31.9k | entry = xmlHashFindEntry(hash, key, key2, key3, hashValue, &found); |
902 | 31.9k | if (found) |
903 | 16.8k | return(entry->payload); |
904 | 15.0k | return(NULL); |
905 | 31.9k | } |
906 | | |
907 | | /** |
908 | | * xmlHashQLookup3: |
909 | | * @hash: hash table |
910 | | * @prefix: first prefix |
911 | | * @name: first local name |
912 | | * @prefix2: second prefix |
913 | | * @name2: second local name |
914 | | * @prefix3: third prefix |
915 | | * @name3: third local name |
916 | | * |
917 | | * Find the payload specified by the QNames tuple. |
918 | | * |
919 | | * Returns a pointer to the payload or NULL if no entry was found. |
920 | | */ |
921 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
922 | | void * |
923 | | xmlHashQLookup3(xmlHashTablePtr hash, |
924 | | const xmlChar *prefix, const xmlChar *name, |
925 | | const xmlChar *prefix2, const xmlChar *name2, |
926 | 0 | const xmlChar *prefix3, const xmlChar *name3) { |
927 | 0 | const xmlHashEntry *entry; |
928 | 0 | unsigned hashValue, mask, pos, displ; |
929 | |
|
930 | 0 | if ((hash == NULL) || (hash->size == 0) || (name == NULL)) |
931 | 0 | return(NULL); |
932 | | |
933 | 0 | hashValue = xmlHashQNameValue(hash->randomSeed, prefix, name, prefix2, |
934 | 0 | name2, prefix3, name3); |
935 | 0 | mask = hash->size - 1; |
936 | 0 | pos = hashValue & mask; |
937 | 0 | entry = &hash->table[pos]; |
938 | |
|
939 | 0 | if (entry->hashValue != 0) { |
940 | 0 | displ = 0; |
941 | 0 | hashValue |= MAX_HASH_SIZE; |
942 | |
|
943 | 0 | do { |
944 | 0 | if ((hashValue == entry->hashValue) && |
945 | 0 | (xmlStrQEqual(prefix, name, entry->key)) && |
946 | 0 | (xmlStrQEqual(prefix2, name2, entry->key2)) && |
947 | 0 | (xmlStrQEqual(prefix3, name3, entry->key3))) |
948 | 0 | return(entry->payload); |
949 | | |
950 | 0 | displ++; |
951 | 0 | pos++; |
952 | 0 | entry++; |
953 | 0 | if ((pos & mask) == 0) |
954 | 0 | entry = hash->table; |
955 | 0 | } while ((entry->hashValue != 0) && |
956 | 0 | (((pos - entry->hashValue) & mask) >= displ)); |
957 | 0 | } |
958 | | |
959 | 0 | return(NULL); |
960 | 0 | } |
961 | | |
962 | | typedef struct { |
963 | | xmlHashScanner scan; |
964 | | void *data; |
965 | | } stubData; |
966 | | |
967 | | static void |
968 | | stubHashScannerFull(void *payload, void *data, const xmlChar *key, |
969 | | const xmlChar *key2 ATTRIBUTE_UNUSED, |
970 | 0 | const xmlChar *key3 ATTRIBUTE_UNUSED) { |
971 | 0 | stubData *sdata = (stubData *) data; |
972 | 0 | sdata->scan(payload, sdata->data, key); |
973 | 0 | } |
974 | | |
975 | | /** |
976 | | * xmlHashScan: |
977 | | * @hash: hash table |
978 | | * @scan: scanner function for items in the hash |
979 | | * @data: extra data passed to @scan |
980 | | * |
981 | | * Scan the hash @table and apply @scan to each value. |
982 | | */ |
983 | | void |
984 | 0 | xmlHashScan(xmlHashTablePtr hash, xmlHashScanner scan, void *data) { |
985 | 0 | stubData sdata; |
986 | 0 | sdata.data = data; |
987 | 0 | sdata.scan = scan; |
988 | 0 | xmlHashScanFull(hash, stubHashScannerFull, &sdata); |
989 | 0 | } |
990 | | |
991 | | /** |
992 | | * xmlHashScanFull: |
993 | | * @hash: hash table |
994 | | * @scan: scanner function for items in the hash |
995 | | * @data: extra data passed to @scan |
996 | | * |
997 | | * Scan the hash @table and apply @scan to each value. |
998 | | */ |
999 | | void |
1000 | 0 | xmlHashScanFull(xmlHashTablePtr hash, xmlHashScannerFull scan, void *data) { |
1001 | 0 | const xmlHashEntry *entry, *end; |
1002 | 0 | xmlHashEntry old; |
1003 | 0 | unsigned i; |
1004 | |
|
1005 | 0 | if ((hash == NULL) || (hash->size == 0) || (scan == NULL)) |
1006 | 0 | return; |
1007 | | |
1008 | | /* |
1009 | | * We must handle the case that a scanned entry is removed when executing |
1010 | | * the callback (xmlCleanSpecialAttr and possibly other places). |
1011 | | * |
1012 | | * Find the start of a probe sequence to avoid scanning entries twice if |
1013 | | * a deletion happens. |
1014 | | */ |
1015 | 0 | entry = hash->table; |
1016 | 0 | end = &hash->table[hash->size]; |
1017 | 0 | while (entry->hashValue != 0) { |
1018 | 0 | if (++entry >= end) |
1019 | 0 | entry = hash->table; |
1020 | 0 | } |
1021 | |
|
1022 | 0 | for (i = 0; i < hash->size; i++) { |
1023 | 0 | if ((entry->hashValue != 0) && (entry->payload != NULL)) { |
1024 | | /* |
1025 | | * Make sure to rescan after a possible deletion. |
1026 | | */ |
1027 | 0 | do { |
1028 | 0 | old = *entry; |
1029 | 0 | scan(entry->payload, data, entry->key, entry->key2, entry->key3); |
1030 | 0 | } while ((entry->hashValue != 0) && |
1031 | 0 | (entry->payload != NULL) && |
1032 | 0 | ((entry->key != old.key) || |
1033 | 0 | (entry->key2 != old.key2) || |
1034 | 0 | (entry->key3 != old.key3))); |
1035 | 0 | } |
1036 | 0 | if (++entry >= end) |
1037 | 0 | entry = hash->table; |
1038 | 0 | } |
1039 | 0 | } |
1040 | | |
1041 | | /** |
1042 | | * xmlHashScan3: |
1043 | | * @hash: hash table |
1044 | | * @key: first string key or NULL |
1045 | | * @key2: second string key or NULL |
1046 | | * @key3: third string key or NULL |
1047 | | * @scan: scanner function for items in the hash |
1048 | | * @data: extra data passed to @scan |
1049 | | * |
1050 | | * Scan the hash @table and apply @scan to each value matching |
1051 | | * (@key, @key2, @key3) tuple. If one of the keys is null, |
1052 | | * the comparison is considered to match. |
1053 | | */ |
1054 | | void |
1055 | | xmlHashScan3(xmlHashTablePtr hash, const xmlChar *key, |
1056 | | const xmlChar *key2, const xmlChar *key3, |
1057 | 0 | xmlHashScanner scan, void *data) { |
1058 | 0 | stubData sdata; |
1059 | 0 | sdata.data = data; |
1060 | 0 | sdata.scan = scan; |
1061 | 0 | xmlHashScanFull3(hash, key, key2, key3, stubHashScannerFull, &sdata); |
1062 | 0 | } |
1063 | | |
1064 | | /** |
1065 | | * xmlHashScanFull3: |
1066 | | * @hash: hash table |
1067 | | * @key: first string key or NULL |
1068 | | * @key2: second string key or NULL |
1069 | | * @key3: third string key or NULL |
1070 | | * @scan: scanner function for items in the hash |
1071 | | * @data: extra data passed to @scan |
1072 | | * |
1073 | | * Scan the hash @table and apply @scan to each value matching |
1074 | | * (@key, @key2, @key3) tuple. If one of the keys is null, |
1075 | | * the comparison is considered to match. |
1076 | | */ |
1077 | | void |
1078 | | xmlHashScanFull3(xmlHashTablePtr hash, const xmlChar *key, |
1079 | | const xmlChar *key2, const xmlChar *key3, |
1080 | 0 | xmlHashScannerFull scan, void *data) { |
1081 | 0 | const xmlHashEntry *entry, *end; |
1082 | 0 | xmlHashEntry old; |
1083 | 0 | unsigned i; |
1084 | |
|
1085 | 0 | if ((hash == NULL) || (hash->size == 0) || (scan == NULL)) |
1086 | 0 | return; |
1087 | | |
1088 | | /* |
1089 | | * We must handle the case that a scanned entry is removed when executing |
1090 | | * the callback (xmlCleanSpecialAttr and possibly other places). |
1091 | | * |
1092 | | * Find the start of a probe sequence to avoid scanning entries twice if |
1093 | | * a deletion happens. |
1094 | | */ |
1095 | 0 | entry = hash->table; |
1096 | 0 | end = &hash->table[hash->size]; |
1097 | 0 | while (entry->hashValue != 0) { |
1098 | 0 | if (++entry >= end) |
1099 | 0 | entry = hash->table; |
1100 | 0 | } |
1101 | |
|
1102 | 0 | for (i = 0; i < hash->size; i++) { |
1103 | 0 | if ((entry->hashValue != 0) && (entry->payload != NULL)) { |
1104 | | /* |
1105 | | * Make sure to rescan after a possible deletion. |
1106 | | */ |
1107 | 0 | do { |
1108 | 0 | if (((key != NULL) && (strcmp((const char *) key, |
1109 | 0 | (const char *) entry->key) != 0)) || |
1110 | 0 | ((key2 != NULL) && (!xmlFastStrEqual(key2, entry->key2))) || |
1111 | 0 | ((key3 != NULL) && (!xmlFastStrEqual(key3, entry->key3)))) |
1112 | 0 | break; |
1113 | 0 | old = *entry; |
1114 | 0 | scan(entry->payload, data, entry->key, entry->key2, entry->key3); |
1115 | 0 | } while ((entry->hashValue != 0) && |
1116 | 0 | (entry->payload != NULL) && |
1117 | 0 | ((entry->key != old.key) || |
1118 | 0 | (entry->key2 != old.key2) || |
1119 | 0 | (entry->key3 != old.key3))); |
1120 | 0 | } |
1121 | 0 | if (++entry >= end) |
1122 | 0 | entry = hash->table; |
1123 | 0 | } |
1124 | 0 | } |
1125 | | |
1126 | | /* |
1127 | | * xmlHashCopySafe: |
1128 | | * @hash: hash table |
1129 | | * @copyFunc: copier function for items in the hash |
1130 | | * @deallocFunc: deallocation function in case of errors |
1131 | | * |
1132 | | * Copy the hash table using @copyFunc to copy payloads. |
1133 | | * |
1134 | | * Returns the new table or NULL if a memory allocation failed. |
1135 | | */ |
1136 | | xmlHashTablePtr |
1137 | | xmlHashCopySafe(xmlHashTablePtr hash, xmlHashCopier copyFunc, |
1138 | 0 | xmlHashDeallocator deallocFunc) { |
1139 | 0 | const xmlHashEntry *entry, *end; |
1140 | 0 | xmlHashTablePtr ret; |
1141 | |
|
1142 | 0 | if ((hash == NULL) || (copyFunc == NULL)) |
1143 | 0 | return(NULL); |
1144 | | |
1145 | 0 | ret = xmlHashCreate(hash->size); |
1146 | 0 | if (ret == NULL) |
1147 | 0 | return(NULL); |
1148 | | |
1149 | 0 | if (hash->size == 0) |
1150 | 0 | return(ret); |
1151 | | |
1152 | 0 | end = &hash->table[hash->size]; |
1153 | |
|
1154 | 0 | for (entry = hash->table; entry < end; entry++) { |
1155 | 0 | if (entry->hashValue != 0) { |
1156 | 0 | void *copy; |
1157 | |
|
1158 | 0 | copy = copyFunc(entry->payload, entry->key); |
1159 | 0 | if (copy == NULL) |
1160 | 0 | goto error; |
1161 | 0 | if (xmlHashAdd3(ret, entry->key, entry->key2, entry->key3, |
1162 | 0 | copy) <= 0) { |
1163 | 0 | if (deallocFunc != NULL) |
1164 | 0 | deallocFunc(copy, entry->key); |
1165 | 0 | goto error; |
1166 | 0 | } |
1167 | 0 | } |
1168 | 0 | } |
1169 | | |
1170 | 0 | return(ret); |
1171 | | |
1172 | 0 | error: |
1173 | 0 | xmlHashFree(ret, deallocFunc); |
1174 | 0 | return(NULL); |
1175 | 0 | } |
1176 | | |
1177 | | /* |
1178 | | * xmlHashCopy: |
1179 | | * @hash: hash table |
1180 | | * @copy: copier function for items in the hash |
1181 | | * |
1182 | | * DEPRECATED: Leaks memory in error case. |
1183 | | * |
1184 | | * Copy the hash table using @copy to copy payloads. |
1185 | | * |
1186 | | * Returns the new table or NULL if a memory allocation failed. |
1187 | | */ |
1188 | | xmlHashTablePtr |
1189 | 0 | xmlHashCopy(xmlHashTablePtr hash, xmlHashCopier copy) { |
1190 | 0 | return(xmlHashCopySafe(hash, copy, NULL)); |
1191 | 0 | } |
1192 | | |
1193 | | /** |
1194 | | * xmlHashSize: |
1195 | | * @hash: hash table |
1196 | | * |
1197 | | * Query the number of elements in the hash table. |
1198 | | * |
1199 | | * Returns the number of elements in the hash table or |
1200 | | * -1 in case of error. |
1201 | | */ |
1202 | | int |
1203 | 0 | xmlHashSize(xmlHashTablePtr hash) { |
1204 | 0 | if (hash == NULL) |
1205 | 0 | return(-1); |
1206 | 0 | return(hash->nbElems); |
1207 | 0 | } |
1208 | | |
1209 | | /** |
1210 | | * xmlHashRemoveEntry: |
1211 | | * @hash: hash table |
1212 | | * @key: string key |
1213 | | * @dealloc: deallocator function for removed item or NULL |
1214 | | * |
1215 | | * Find the entry specified by the @key and remove it from the hash table. |
1216 | | * Payload will be freed with @dealloc. |
1217 | | * |
1218 | | * Returns 0 on success and -1 if no entry was found. |
1219 | | */ |
1220 | | int xmlHashRemoveEntry(xmlHashTablePtr hash, const xmlChar *key, |
1221 | 963 | xmlHashDeallocator dealloc) { |
1222 | 963 | return(xmlHashRemoveEntry3(hash, key, NULL, NULL, dealloc)); |
1223 | 963 | } |
1224 | | |
1225 | | /** |
1226 | | * xmlHashRemoveEntry2: |
1227 | | * @hash: hash table |
1228 | | * @key: first string key |
1229 | | * @key2: second string key |
1230 | | * @dealloc: deallocator function for removed item or NULL |
1231 | | * |
1232 | | * Remove an entry with two strings as key. |
1233 | | * |
1234 | | * See xmlHashRemoveEntry. |
1235 | | * |
1236 | | * Returns 0 on success and -1 in case of error. |
1237 | | */ |
1238 | | int |
1239 | | xmlHashRemoveEntry2(xmlHashTablePtr hash, const xmlChar *key, |
1240 | 0 | const xmlChar *key2, xmlHashDeallocator dealloc) { |
1241 | 0 | return(xmlHashRemoveEntry3(hash, key, key2, NULL, dealloc)); |
1242 | 0 | } |
1243 | | |
1244 | | /** |
1245 | | * xmlHashRemoveEntry3: |
1246 | | * @hash: hash table |
1247 | | * @key: first string key |
1248 | | * @key2: second string key |
1249 | | * @key3: third string key |
1250 | | * @dealloc: deallocator function for removed item or NULL |
1251 | | * |
1252 | | * Remove an entry with three strings as key. |
1253 | | * |
1254 | | * See xmlHashRemoveEntry. |
1255 | | * |
1256 | | * Returns 0 on success and -1 in case of error. |
1257 | | */ |
1258 | | ATTRIBUTE_NO_SANITIZE_INTEGER |
1259 | | int |
1260 | | xmlHashRemoveEntry3(xmlHashTablePtr hash, const xmlChar *key, |
1261 | | const xmlChar *key2, const xmlChar *key3, |
1262 | 963 | xmlHashDeallocator dealloc) { |
1263 | 963 | xmlHashEntry *entry, *cur, *next; |
1264 | 963 | unsigned hashValue, mask, pos, nextpos; |
1265 | 963 | int found; |
1266 | | |
1267 | 963 | if ((hash == NULL) || (hash->size == 0) || (key == NULL)) |
1268 | 0 | return(-1); |
1269 | | |
1270 | 963 | hashValue = xmlHashValue(hash->randomSeed, key, key2, key3, NULL); |
1271 | 963 | entry = xmlHashFindEntry(hash, key, key2, key3, hashValue, &found); |
1272 | 963 | if (!found) |
1273 | 0 | return(-1); |
1274 | | |
1275 | 963 | if ((dealloc != NULL) && (entry->payload != NULL)) |
1276 | 963 | dealloc(entry->payload, entry->key); |
1277 | 963 | if (hash->dict == NULL) { |
1278 | 963 | if (entry->key) |
1279 | 963 | xmlFree(entry->key); |
1280 | 963 | if (entry->key2) |
1281 | 0 | xmlFree(entry->key2); |
1282 | 963 | if (entry->key3) |
1283 | 0 | xmlFree(entry->key3); |
1284 | 963 | } |
1285 | | |
1286 | | /* |
1287 | | * Find end of probe sequence. Entries at their initial probe |
1288 | | * position start a new sequence. |
1289 | | */ |
1290 | 963 | mask = hash->size - 1; |
1291 | 963 | pos = entry - hash->table; |
1292 | 963 | cur = entry; |
1293 | | |
1294 | 1.89k | while (1) { |
1295 | 1.89k | nextpos = pos + 1; |
1296 | 1.89k | next = cur + 1; |
1297 | 1.89k | if ((nextpos & mask) == 0) |
1298 | 72 | next = hash->table; |
1299 | | |
1300 | 1.89k | if ((next->hashValue == 0) || |
1301 | 1.89k | (((next->hashValue - nextpos) & mask) == 0)) |
1302 | 963 | break; |
1303 | | |
1304 | 930 | cur = next; |
1305 | 930 | pos = nextpos; |
1306 | 930 | } |
1307 | | |
1308 | | /* |
1309 | | * Backward shift |
1310 | | */ |
1311 | 963 | next = entry + 1; |
1312 | | |
1313 | 963 | if (cur < entry) { |
1314 | 34 | xmlHashEntry *end = &hash->table[hash->size]; |
1315 | | |
1316 | 34 | memmove(entry, next, (char *) end - (char *) next); |
1317 | 34 | entry = hash->table; |
1318 | 34 | end[-1] = *entry; |
1319 | 34 | next = entry + 1; |
1320 | 34 | } |
1321 | | |
1322 | 963 | memmove(entry, next, (char *) cur - (char *) entry); |
1323 | | |
1324 | | /* |
1325 | | * Update entry |
1326 | | */ |
1327 | 963 | cur->hashValue = 0; |
1328 | | |
1329 | 963 | hash->nbElems--; |
1330 | | |
1331 | 963 | return(0); |
1332 | 963 | } |
1333 | | |