Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * dict.c: dictionary of reusable strings, just used to avoid allocation  | 
3  |  |  *         and freeing operations.  | 
4  |  |  *  | 
5  |  |  * Copyright (C) 2003-2012 Daniel Veillard.  | 
6  |  |  *  | 
7  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
8  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
9  |  |  * copyright notice and this permission notice appear in all copies.  | 
10  |  |  *  | 
11  |  |  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED  | 
12  |  |  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF  | 
13  |  |  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND  | 
14  |  |  * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.  | 
15  |  |  *  | 
16  |  |  * Author: Daniel Veillard  | 
17  |  |  */  | 
18  |  |  | 
19  |  | #define IN_LIBXML  | 
20  |  | #include "libxml.h"  | 
21  |  |  | 
22  |  | #include <errno.h>  | 
23  |  | #include <limits.h>  | 
24  |  | #include <stdlib.h>  | 
25  |  | #include <string.h>  | 
26  |  |  | 
27  |  | #include "private/dict.h"  | 
28  |  | #include "private/error.h"  | 
29  |  | #include "private/globals.h"  | 
30  |  | #include "private/threads.h"  | 
31  |  |  | 
32  |  | #include <libxml/parser.h>  | 
33  |  | #include <libxml/dict.h>  | 
34  |  | #include <libxml/xmlmemory.h>  | 
35  |  | #include <libxml/xmlstring.h>  | 
36  |  |  | 
37  |  | #ifndef SIZE_MAX  | 
38  |  |   #define SIZE_MAX ((size_t) -1)  | 
39  |  | #endif  | 
40  |  |  | 
41  | 108k  | #define MAX_FILL_NUM 7  | 
42  | 108k  | #define MAX_FILL_DENOM 8  | 
43  | 12.5k  | #define MIN_HASH_SIZE 8  | 
44  | 1.83M  | #define MAX_HASH_SIZE (1u << 31)  | 
45  |  |  | 
46  |  | typedef struct _xmlDictStrings xmlDictStrings;  | 
47  |  | typedef xmlDictStrings *xmlDictStringsPtr;  | 
48  |  | struct _xmlDictStrings { | 
49  |  |     xmlDictStringsPtr next;  | 
50  |  |     xmlChar *free;  | 
51  |  |     xmlChar *end;  | 
52  |  |     size_t size;  | 
53  |  |     size_t nbStrings;  | 
54  |  |     xmlChar array[1];  | 
55  |  | };  | 
56  |  |  | 
57  |  | typedef xmlHashedString xmlDictEntry;  | 
58  |  |  | 
59  |  | /*  | 
60  |  |  * The entire dictionary  | 
61  |  |  */  | 
62  |  | struct _xmlDict { | 
63  |  |     int ref_counter;  | 
64  |  |  | 
65  |  |     xmlDictEntry *table;  | 
66  |  |     size_t size;  | 
67  |  |     unsigned int nbElems;  | 
68  |  |     xmlDictStringsPtr strings;  | 
69  |  |  | 
70  |  |     struct _xmlDict *subdict;  | 
71  |  |     /* used for randomization */  | 
72  |  |     unsigned seed;  | 
73  |  |     /* used to impose a limit on size */  | 
74  |  |     size_t limit;  | 
75  |  | };  | 
76  |  |  | 
77  |  | /*  | 
78  |  |  * A mutex for modifying the reference counter for shared  | 
79  |  |  * dictionaries.  | 
80  |  |  */  | 
81  |  | static xmlMutex xmlDictMutex;  | 
82  |  |  | 
83  |  | /**  | 
84  |  |  * @deprecated Alias for #xmlInitParser.  | 
85  |  |  *  | 
86  |  |  * @returns 0.  | 
87  |  |  */  | 
88  |  | int  | 
89  | 0  | xmlInitializeDict(void) { | 
90  | 0  |     xmlInitParser();  | 
91  | 0  |     return(0);  | 
92  | 0  | }  | 
93  |  |  | 
94  |  | /**  | 
95  |  |  * Initialize mutex.  | 
96  |  |  */  | 
97  |  | void  | 
98  | 1  | xmlInitDictInternal(void) { | 
99  | 1  |     xmlInitMutex(&xmlDictMutex);  | 
100  | 1  | }  | 
101  |  |  | 
102  |  | /**  | 
103  |  |  * @deprecated This function is a no-op. Call #xmlCleanupParser  | 
104  |  |  * to free global state but see the warnings there. #xmlCleanupParser  | 
105  |  |  * should be only called once at program exit. In most cases, you don't  | 
106  |  |  * have call cleanup functions at all.  | 
107  |  |  */  | 
108  |  | void  | 
109  | 0  | xmlDictCleanup(void) { | 
110  | 0  | }  | 
111  |  |  | 
112  |  | /**  | 
113  |  |  * Free the dictionary mutex.  | 
114  |  |  */  | 
115  |  | void  | 
116  | 0  | xmlCleanupDictInternal(void) { | 
117  | 0  |     xmlCleanupMutex(&xmlDictMutex);  | 
118  | 0  | }  | 
119  |  |  | 
120  |  | /*  | 
121  |  |  * @param dict  the dictionary  | 
122  |  |  * @param name  the name of the userdata  | 
123  |  |  * @param len  the length of the name  | 
124  |  |  *  | 
125  |  |  * Add the string to the array[s]  | 
126  |  |  *  | 
127  |  |  * @returns the pointer of the local string, or NULL in case of error.  | 
128  |  |  */  | 
129  |  | static const xmlChar *  | 
130  | 120k  | xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) { | 
131  | 120k  |     xmlDictStringsPtr pool;  | 
132  | 120k  |     const xmlChar *ret;  | 
133  | 120k  |     size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */  | 
134  | 120k  |     size_t limit = 0;  | 
135  |  |  | 
136  | 120k  |     pool = dict->strings;  | 
137  | 122k  |     while (pool != NULL) { | 
138  | 109k  |   if ((size_t)(pool->end - pool->free) > namelen)  | 
139  | 107k  |       goto found_pool;  | 
140  | 2.16k  |   if (pool->size > size) size = pool->size;  | 
141  | 2.16k  |         limit += pool->size;  | 
142  | 2.16k  |   pool = pool->next;  | 
143  | 2.16k  |     }  | 
144  |  |     /*  | 
145  |  |      * Not found, need to allocate  | 
146  |  |      */  | 
147  | 13.3k  |     if (pool == NULL) { | 
148  | 13.3k  |         if ((dict->limit > 0) && (limit > dict->limit)) { | 
149  | 0  |             return(NULL);  | 
150  | 0  |         }  | 
151  |  |  | 
152  | 13.3k  |         if (size == 0) { | 
153  | 12.5k  |             size = 1000;  | 
154  | 12.5k  |         } else { | 
155  | 824  |             if (size < (SIZE_MAX - sizeof(xmlDictStrings)) / 4)  | 
156  | 824  |                 size *= 4; /* exponential growth */  | 
157  | 0  |             else  | 
158  | 0  |                 size = SIZE_MAX - sizeof(xmlDictStrings);  | 
159  | 824  |         }  | 
160  | 13.3k  |         if (size / 4 < namelen) { | 
161  | 494  |             if ((size_t) namelen + 0 < (SIZE_MAX - sizeof(xmlDictStrings)) / 4)  | 
162  | 494  |                 size = 4 * (size_t) namelen; /* just in case ! */  | 
163  | 0  |             else  | 
164  | 0  |                 return(NULL);  | 
165  | 494  |         }  | 
166  | 13.3k  |   pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size);  | 
167  | 13.3k  |   if (pool == NULL)  | 
168  | 0  |       return(NULL);  | 
169  | 13.3k  |   pool->size = size;  | 
170  | 13.3k  |   pool->nbStrings = 0;  | 
171  | 13.3k  |   pool->free = &pool->array[0];  | 
172  | 13.3k  |   pool->end = &pool->array[size];  | 
173  | 13.3k  |   pool->next = dict->strings;  | 
174  | 13.3k  |   dict->strings = pool;  | 
175  | 13.3k  |     }  | 
176  | 120k  | found_pool:  | 
177  | 120k  |     ret = pool->free;  | 
178  | 120k  |     memcpy(pool->free, name, namelen);  | 
179  | 120k  |     pool->free += namelen;  | 
180  | 120k  |     *(pool->free++) = 0;  | 
181  | 120k  |     pool->nbStrings++;  | 
182  | 120k  |     return(ret);  | 
183  | 13.3k  | }  | 
184  |  |  | 
185  |  | /*  | 
186  |  |  * @param dict  the dictionary  | 
187  |  |  * @param prefix  the prefix of the userdata  | 
188  |  |  * @param plen  the prefix length  | 
189  |  |  * @param name  the name of the userdata  | 
190  |  |  * @param len  the length of the name  | 
191  |  |  *  | 
192  |  |  * Add the QName to the array[s]  | 
193  |  |  *  | 
194  |  |  * @returns the pointer of the local string, or NULL in case of error.  | 
195  |  |  */  | 
196  |  | static const xmlChar *  | 
197  |  | xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, unsigned int plen,  | 
198  |  |                  const xmlChar *name, unsigned int namelen)  | 
199  | 0  | { | 
200  | 0  |     xmlDictStringsPtr pool;  | 
201  | 0  |     const xmlChar *ret;  | 
202  | 0  |     size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */  | 
203  | 0  |     size_t limit = 0;  | 
204  |  | 
  | 
205  | 0  |     pool = dict->strings;  | 
206  | 0  |     while (pool != NULL) { | 
207  | 0  |   if ((size_t)(pool->end - pool->free) > namelen + plen + 1)  | 
208  | 0  |       goto found_pool;  | 
209  | 0  |   if (pool->size > size) size = pool->size;  | 
210  | 0  |         limit += pool->size;  | 
211  | 0  |   pool = pool->next;  | 
212  | 0  |     }  | 
213  |  |     /*  | 
214  |  |      * Not found, need to allocate  | 
215  |  |      */  | 
216  | 0  |     if (pool == NULL) { | 
217  | 0  |         if ((dict->limit > 0) && (limit > dict->limit)) { | 
218  | 0  |             return(NULL);  | 
219  | 0  |         }  | 
220  |  |  | 
221  | 0  |         if (size == 0) size = 1000;  | 
222  | 0  |   else size *= 4; /* exponential growth */  | 
223  | 0  |         if (size < 4 * (namelen + plen + 1))  | 
224  | 0  |       size = 4 * (namelen + plen + 1); /* just in case ! */  | 
225  | 0  |   pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size);  | 
226  | 0  |   if (pool == NULL)  | 
227  | 0  |       return(NULL);  | 
228  | 0  |   pool->size = size;  | 
229  | 0  |   pool->nbStrings = 0;  | 
230  | 0  |   pool->free = &pool->array[0];  | 
231  | 0  |   pool->end = &pool->array[size];  | 
232  | 0  |   pool->next = dict->strings;  | 
233  | 0  |   dict->strings = pool;  | 
234  | 0  |     }  | 
235  | 0  | found_pool:  | 
236  | 0  |     ret = pool->free;  | 
237  | 0  |     memcpy(pool->free, prefix, plen);  | 
238  | 0  |     pool->free += plen;  | 
239  | 0  |     *(pool->free++) = ':';  | 
240  | 0  |     memcpy(pool->free, name, namelen);  | 
241  | 0  |     pool->free += namelen;  | 
242  | 0  |     *(pool->free++) = 0;  | 
243  | 0  |     pool->nbStrings++;  | 
244  | 0  |     return(ret);  | 
245  | 0  | }  | 
246  |  |  | 
247  |  | /**  | 
248  |  |  * Create a new dictionary  | 
249  |  |  *  | 
250  |  |  * @returns the newly created dictionary, or NULL if an error occurred.  | 
251  |  |  */  | 
252  |  | xmlDict *  | 
253  | 12.5k  | xmlDictCreate(void) { | 
254  | 12.5k  |     xmlDictPtr dict;  | 
255  |  |  | 
256  | 12.5k  |     xmlInitParser();  | 
257  |  |  | 
258  | 12.5k  |     dict = xmlMalloc(sizeof(xmlDict));  | 
259  | 12.5k  |     if (dict == NULL)  | 
260  | 0  |         return(NULL);  | 
261  | 12.5k  |     dict->ref_counter = 1;  | 
262  | 12.5k  |     dict->limit = 0;  | 
263  |  |  | 
264  | 12.5k  |     dict->size = 0;  | 
265  | 12.5k  |     dict->nbElems = 0;  | 
266  | 12.5k  |     dict->table = NULL;  | 
267  | 12.5k  |     dict->strings = NULL;  | 
268  | 12.5k  |     dict->subdict = NULL;  | 
269  | 12.5k  |     dict->seed = xmlRandom();  | 
270  | 12.5k  | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
271  | 12.5k  |     dict->seed = 0;  | 
272  | 12.5k  | #endif  | 
273  | 12.5k  |     return(dict);  | 
274  | 12.5k  | }  | 
275  |  |  | 
276  |  | /**  | 
277  |  |  * Create a new dictionary, inheriting strings from the read-only  | 
278  |  |  * dictionary `sub`. On lookup, strings are first searched in the  | 
279  |  |  * new dictionary, then in `sub`, and if not found are created in the  | 
280  |  |  * new dictionary.  | 
281  |  |  *  | 
282  |  |  * @param sub  an existing dictionary  | 
283  |  |  * @returns the newly created dictionary, or NULL if an error occurred.  | 
284  |  |  */  | 
285  |  | xmlDict *  | 
286  | 0  | xmlDictCreateSub(xmlDict *sub) { | 
287  | 0  |     xmlDictPtr dict = xmlDictCreate();  | 
288  |  | 
  | 
289  | 0  |     if ((dict != NULL) && (sub != NULL)) { | 
290  | 0  |         dict->seed = sub->seed;  | 
291  | 0  |         dict->subdict = sub;  | 
292  | 0  |   xmlDictReference(dict->subdict);  | 
293  | 0  |     }  | 
294  | 0  |     return(dict);  | 
295  | 0  | }  | 
296  |  |  | 
297  |  | /**  | 
298  |  |  * Increment the reference counter of a dictionary  | 
299  |  |  *  | 
300  |  |  * @param dict  the dictionary  | 
301  |  |  * @returns 0 in case of success and -1 in case of error  | 
302  |  |  */  | 
303  |  | int  | 
304  | 6.28k  | xmlDictReference(xmlDict *dict) { | 
305  | 6.28k  |     if (dict == NULL) return -1;  | 
306  | 2.48k  |     xmlMutexLock(&xmlDictMutex);  | 
307  | 2.48k  |     dict->ref_counter++;  | 
308  | 2.48k  |     xmlMutexUnlock(&xmlDictMutex);  | 
309  | 2.48k  |     return(0);  | 
310  | 6.28k  | }  | 
311  |  |  | 
312  |  | /**  | 
313  |  |  * Free the hash `dict` and its contents. The userdata is  | 
314  |  |  * deallocated with `f` if provided.  | 
315  |  |  *  | 
316  |  |  * @param dict  the dictionary  | 
317  |  |  */  | 
318  |  | void  | 
319  | 15.0k  | xmlDictFree(xmlDict *dict) { | 
320  | 15.0k  |     xmlDictStringsPtr pool, nextp;  | 
321  |  |  | 
322  | 15.0k  |     if (dict == NULL)  | 
323  | 0  |   return;  | 
324  |  |  | 
325  |  |     /* decrement the counter, it may be shared by a parser and docs */  | 
326  | 15.0k  |     xmlMutexLock(&xmlDictMutex);  | 
327  | 15.0k  |     dict->ref_counter--;  | 
328  | 15.0k  |     if (dict->ref_counter > 0) { | 
329  | 2.48k  |         xmlMutexUnlock(&xmlDictMutex);  | 
330  | 2.48k  |         return;  | 
331  | 2.48k  |     }  | 
332  |  |  | 
333  | 12.5k  |     xmlMutexUnlock(&xmlDictMutex);  | 
334  |  |  | 
335  | 12.5k  |     if (dict->subdict != NULL) { | 
336  | 0  |         xmlDictFree(dict->subdict);  | 
337  | 0  |     }  | 
338  |  |  | 
339  | 12.5k  |     if (dict->table) { | 
340  | 12.5k  |   xmlFree(dict->table);  | 
341  | 12.5k  |     }  | 
342  | 12.5k  |     pool = dict->strings;  | 
343  | 25.9k  |     while (pool != NULL) { | 
344  | 13.3k  |         nextp = pool->next;  | 
345  | 13.3k  |   xmlFree(pool);  | 
346  | 13.3k  |   pool = nextp;  | 
347  | 13.3k  |     }  | 
348  | 12.5k  |     xmlFree(dict);  | 
349  | 12.5k  | }  | 
350  |  |  | 
351  |  | /**  | 
352  |  |  * check if a string is owned by the dictionary  | 
353  |  |  *  | 
354  |  |  * @param dict  the dictionary  | 
355  |  |  * @param str  the string  | 
356  |  |  * @returns 1 if true, 0 if false and -1 in case of error  | 
357  |  |  * -1 in case of error  | 
358  |  |  */  | 
359  |  | int  | 
360  | 23.3k  | xmlDictOwns(xmlDict *dict, const xmlChar *str) { | 
361  | 23.3k  |     xmlDictStringsPtr pool;  | 
362  |  |  | 
363  | 23.3k  |     if ((dict == NULL) || (str == NULL))  | 
364  | 0  |   return(-1);  | 
365  | 23.3k  |     pool = dict->strings;  | 
366  | 30.4k  |     while (pool != NULL) { | 
367  | 24.1k  |         if ((str >= &pool->array[0]) && (str <= pool->free))  | 
368  | 17.0k  |       return(1);  | 
369  | 7.11k  |   pool = pool->next;  | 
370  | 7.11k  |     }  | 
371  | 6.29k  |     if (dict->subdict)  | 
372  | 0  |         return(xmlDictOwns(dict->subdict, str));  | 
373  | 6.29k  |     return(0);  | 
374  | 6.29k  | }  | 
375  |  |  | 
376  |  | /**  | 
377  |  |  * Query the number of elements installed in the hash `dict`.  | 
378  |  |  *  | 
379  |  |  * @param dict  the dictionary  | 
380  |  |  * @returns the number of elements in the dictionary or  | 
381  |  |  * -1 in case of error  | 
382  |  |  */  | 
383  |  | int  | 
384  | 0  | xmlDictSize(xmlDict *dict) { | 
385  | 0  |     if (dict == NULL)  | 
386  | 0  |   return(-1);  | 
387  | 0  |     if (dict->subdict)  | 
388  | 0  |         return(dict->nbElems + dict->subdict->nbElems);  | 
389  | 0  |     return(dict->nbElems);  | 
390  | 0  | }  | 
391  |  |  | 
392  |  | /**  | 
393  |  |  * Set a size limit for the dictionary  | 
394  |  |  * Added in 2.9.0  | 
395  |  |  *  | 
396  |  |  * @param dict  the dictionary  | 
397  |  |  * @param limit  the limit in bytes  | 
398  |  |  * @returns the previous limit of the dictionary or 0  | 
399  |  |  */  | 
400  |  | size_t  | 
401  | 12.5k  | xmlDictSetLimit(xmlDict *dict, size_t limit) { | 
402  | 12.5k  |     size_t ret;  | 
403  |  |  | 
404  | 12.5k  |     if (dict == NULL)  | 
405  | 0  |   return(0);  | 
406  | 12.5k  |     ret = dict->limit;  | 
407  | 12.5k  |     dict->limit = limit;  | 
408  | 12.5k  |     return(ret);  | 
409  | 12.5k  | }  | 
410  |  |  | 
411  |  | /**  | 
412  |  |  * Get how much memory is used by a dictionary for strings  | 
413  |  |  * Added in 2.9.0  | 
414  |  |  *  | 
415  |  |  * @param dict  the dictionary  | 
416  |  |  * @returns the amount of strings allocated  | 
417  |  |  */  | 
418  |  | size_t  | 
419  | 0  | xmlDictGetUsage(xmlDict *dict) { | 
420  | 0  |     xmlDictStringsPtr pool;  | 
421  | 0  |     size_t limit = 0;  | 
422  |  | 
  | 
423  | 0  |     if (dict == NULL)  | 
424  | 0  |   return(0);  | 
425  | 0  |     pool = dict->strings;  | 
426  | 0  |     while (pool != NULL) { | 
427  | 0  |         limit += pool->size;  | 
428  | 0  |   pool = pool->next;  | 
429  | 0  |     }  | 
430  | 0  |     return(limit);  | 
431  | 0  | }  | 
432  |  |  | 
433  |  | /*****************************************************************  | 
434  |  |  *  | 
435  |  |  * The code below was rewritten and is additionally licensed under  | 
436  |  |  * the main license in file 'Copyright'.  | 
437  |  |  *  | 
438  |  |  *****************************************************************/  | 
439  |  |  | 
440  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
441  |  | static unsigned  | 
442  |  | xmlDictHashName(unsigned seed, const xmlChar* data, size_t maxLen,  | 
443  | 1.82M  |                 size_t *plen) { | 
444  | 1.82M  |     unsigned h1, h2;  | 
445  | 1.82M  |     size_t i;  | 
446  |  |  | 
447  | 1.82M  |     HASH_INIT(h1, h2, seed);  | 
448  |  |  | 
449  | 329M  |     for (i = 0; i < maxLen && data[i]; i++) { | 
450  | 327M  |         HASH_UPDATE(h1, h2, data[i]);  | 
451  | 327M  |     }  | 
452  |  |  | 
453  | 1.82M  |     HASH_FINISH(h1, h2);  | 
454  |  |  | 
455  | 1.82M  |     *plen = i;  | 
456  | 1.82M  |     return(h2 | MAX_HASH_SIZE);  | 
457  | 1.82M  | }  | 
458  |  |  | 
459  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
460  |  | static unsigned  | 
461  |  | xmlDictHashQName(unsigned seed, const xmlChar *prefix, const xmlChar *name,  | 
462  | 0  |                  size_t *pplen, size_t *plen) { | 
463  | 0  |     unsigned h1, h2;  | 
464  | 0  |     size_t i;  | 
465  |  | 
  | 
466  | 0  |     HASH_INIT(h1, h2, seed);  | 
467  |  | 
  | 
468  | 0  |     for (i = 0; prefix[i] != 0; i++) { | 
469  | 0  |         HASH_UPDATE(h1, h2, prefix[i]);  | 
470  | 0  |     }  | 
471  | 0  |     *pplen = i;  | 
472  |  | 
  | 
473  | 0  |     HASH_UPDATE(h1, h2, ':');  | 
474  |  | 
  | 
475  | 0  |     for (i = 0; name[i] != 0; i++) { | 
476  | 0  |         HASH_UPDATE(h1, h2, name[i]);  | 
477  | 0  |     }  | 
478  | 0  |     *plen = i;  | 
479  |  | 
  | 
480  | 0  |     HASH_FINISH(h1, h2);  | 
481  |  |  | 
482  |  |     /*  | 
483  |  |      * Always set the upper bit of hash values since 0 means an unoccupied  | 
484  |  |      * bucket.  | 
485  |  |      */  | 
486  | 0  |     return(h2 | MAX_HASH_SIZE);  | 
487  | 0  | }  | 
488  |  |  | 
489  |  | /**  | 
490  |  |  * Compute the hash value of a C string.  | 
491  |  |  *  | 
492  |  |  * @param dict  dictionary  | 
493  |  |  * @param string  C string  | 
494  |  |  * @returns the hash value.  | 
495  |  |  */  | 
496  |  | unsigned  | 
497  | 135k  | xmlDictComputeHash(const xmlDict *dict, const xmlChar *string) { | 
498  | 135k  |     size_t len;  | 
499  | 135k  |     return(xmlDictHashName(dict->seed, string, SIZE_MAX, &len));  | 
500  | 135k  | }  | 
501  |  |  | 
502  | 385k  | #define HASH_ROL31(x,n) ((x) << (n) | ((x) & 0x7FFFFFFF) >> (31 - (n)))  | 
503  |  |  | 
504  |  | /**  | 
505  |  |  * Combine two hash values.  | 
506  |  |  *  | 
507  |  |  * @param v1  first hash value  | 
508  |  |  * @param v2  second hash value  | 
509  |  |  * @returns the combined hash value.  | 
510  |  |  */  | 
511  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
512  |  | unsigned  | 
513  | 385k  | xmlDictCombineHash(unsigned v1, unsigned v2) { | 
514  |  |     /*  | 
515  |  |      * The upper bit of hash values is always set, so we have to operate on  | 
516  |  |      * 31-bit hashes here.  | 
517  |  |      */  | 
518  | 385k  |     v1 ^= v2;  | 
519  | 385k  |     v1 += HASH_ROL31(v2, 5);  | 
520  |  |  | 
521  | 385k  |     return((v1 & 0xFFFFFFFF) | 0x80000000);  | 
522  | 385k  | }  | 
523  |  |  | 
524  |  | /**  | 
525  |  |  * Try to find a matching hash table entry. If an entry was found, set  | 
526  |  |  * `found` to 1 and return the entry. Otherwise, set `found` to 0 and return  | 
527  |  |  * the location where a new entry should be inserted.  | 
528  |  |  *  | 
529  |  |  * @param dict  dict  | 
530  |  |  * @param prefix  optional QName prefix  | 
531  |  |  * @param name  string  | 
532  |  |  * @param len  length of string  | 
533  |  |  * @param hashValue  valid hash value of string  | 
534  |  |  * @param pfound  result of search  | 
535  |  |  */  | 
536  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
537  |  | static xmlDictEntry *  | 
538  |  | xmlDictFindEntry(const xmlDict *dict, const xmlChar *prefix,  | 
539  |  |                  const xmlChar *name, int len, unsigned hashValue,  | 
540  | 1.67M  |                  int *pfound) { | 
541  | 1.67M  |     xmlDictEntry *entry;  | 
542  | 1.67M  |     unsigned mask, pos, displ;  | 
543  | 1.67M  |     int found = 0;  | 
544  |  |  | 
545  | 1.67M  |     mask = dict->size - 1;  | 
546  | 1.67M  |     pos = hashValue & mask;  | 
547  | 1.67M  |     entry = &dict->table[pos];  | 
548  |  |  | 
549  | 1.67M  |     if (entry->hashValue != 0) { | 
550  |  |         /*  | 
551  |  |          * Robin hood hashing: abort if the displacement of the entry  | 
552  |  |          * is smaller than the displacement of the key we look for.  | 
553  |  |          * This also stops at the correct position when inserting.  | 
554  |  |          */  | 
555  | 1.62M  |         displ = 0;  | 
556  |  |  | 
557  | 2.93M  |         do { | 
558  | 2.93M  |             if (entry->hashValue == hashValue) { | 
559  | 1.57M  |                 if (prefix == NULL) { | 
560  |  |                     /*  | 
561  |  |                      * name is not necessarily null-terminated.  | 
562  |  |                      */  | 
563  | 1.57M  |                     if ((strncmp((const char *) entry->name,  | 
564  | 1.57M  |                                  (const char *) name, len) == 0) &&  | 
565  | 1.57M  |                         (entry->name[len] == 0)) { | 
566  | 1.57M  |                         found = 1;  | 
567  | 1.57M  |                         break;  | 
568  | 1.57M  |                     }  | 
569  | 1.57M  |                 } else { | 
570  | 0  |                     if (xmlStrQEqual(prefix, name, entry->name)) { | 
571  | 0  |                         found = 1;  | 
572  | 0  |                         break;  | 
573  | 0  |                     }  | 
574  | 0  |                 }  | 
575  | 1.57M  |             }  | 
576  |  |  | 
577  | 1.36M  |             displ++;  | 
578  | 1.36M  |             pos++;  | 
579  | 1.36M  |             entry++;  | 
580  | 1.36M  |             if ((pos & mask) == 0)  | 
581  | 138k  |                 entry = dict->table;  | 
582  | 1.36M  |         } while ((entry->hashValue != 0) &&  | 
583  | 1.33M  |                  (((pos - entry->hashValue) & mask) >= displ));  | 
584  | 1.62M  |     }  | 
585  |  |  | 
586  | 1.67M  |     *pfound = found;  | 
587  | 1.67M  |     return(entry);  | 
588  | 1.67M  | }  | 
589  |  |  | 
590  |  | /**  | 
591  |  |  * Resize the dictionary hash table.  | 
592  |  |  *  | 
593  |  |  * @param dict  dictionary  | 
594  |  |  * @param size  new size of the dictionary  | 
595  |  |  * @returns 0 in case of success, -1 if a memory allocation failed.  | 
596  |  |  */  | 
597  |  | static int  | 
598  | 17.4k  | xmlDictGrow(xmlDictPtr dict, unsigned size) { | 
599  | 17.4k  |     const xmlDictEntry *oldentry, *oldend, *end;  | 
600  | 17.4k  |     xmlDictEntry *table;  | 
601  | 17.4k  |     unsigned oldsize, i;  | 
602  |  |  | 
603  |  |     /* Add 0 to avoid spurious -Wtype-limits warning on 64-bit GCC */  | 
604  | 17.4k  |     if ((size_t) size + 0 > SIZE_MAX / sizeof(table[0]))  | 
605  | 0  |         return(-1);  | 
606  | 17.4k  |     table = xmlMalloc(size * sizeof(table[0]));  | 
607  | 17.4k  |     if (table == NULL)  | 
608  | 0  |         return(-1);  | 
609  | 17.4k  |     memset(table, 0, size * sizeof(table[0]));  | 
610  |  |  | 
611  | 17.4k  |     oldsize = dict->size;  | 
612  | 17.4k  |     if (oldsize == 0)  | 
613  | 12.5k  |         goto done;  | 
614  |  |  | 
615  | 4.92k  |     oldend = &dict->table[oldsize];  | 
616  | 4.92k  |     end = &table[size];  | 
617  |  |  | 
618  |  |     /*  | 
619  |  |      * Robin Hood sorting order is maintained if we  | 
620  |  |      *  | 
621  |  |      * - compute dict indices with modulo  | 
622  |  |      * - resize by an integer factor  | 
623  |  |      * - start to copy from the beginning of a probe sequence  | 
624  |  |      */  | 
625  | 4.92k  |     oldentry = dict->table;  | 
626  | 33.3k  |     while (oldentry->hashValue != 0) { | 
627  | 28.4k  |         if (++oldentry >= oldend)  | 
628  | 0  |             oldentry = dict->table;  | 
629  | 28.4k  |     }  | 
630  |  |  | 
631  | 108k  |     for (i = 0; i < oldsize; i++) { | 
632  | 103k  |         if (oldentry->hashValue != 0) { | 
633  | 90.5k  |             xmlDictEntry *entry = &table[oldentry->hashValue & (size - 1)];  | 
634  |  |  | 
635  | 122k  |             while (entry->hashValue != 0) { | 
636  | 32.1k  |                 if (++entry >= end)  | 
637  | 2.70k  |                     entry = table;  | 
638  | 32.1k  |             }  | 
639  | 90.5k  |             *entry = *oldentry;  | 
640  | 90.5k  |         }  | 
641  |  |  | 
642  | 103k  |         if (++oldentry >= oldend)  | 
643  | 4.92k  |             oldentry = dict->table;  | 
644  | 103k  |     }  | 
645  |  |  | 
646  | 4.92k  |     xmlFree(dict->table);  | 
647  |  |  | 
648  | 17.4k  | done:  | 
649  | 17.4k  |     dict->table = table;  | 
650  | 17.4k  |     dict->size = size;  | 
651  |  |  | 
652  | 17.4k  |     return(0);  | 
653  | 4.92k  | }  | 
654  |  |  | 
655  |  | /**  | 
656  |  |  * Internal lookup and update function.  | 
657  |  |  *  | 
658  |  |  * @param dict  dict  | 
659  |  |  * @param prefix  optional QName prefix  | 
660  |  |  * @param name  string  | 
661  |  |  * @param maybeLen  length of string or -1 if unknown  | 
662  |  |  * @param update  whether the string should be added  | 
663  |  |  */  | 
664  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
665  |  | static const xmlDictEntry *  | 
666  |  | xmlDictLookupInternal(xmlDict *dict, const xmlChar *prefix,  | 
667  | 1.69M  |                       const xmlChar *name, int maybeLen, int update) { | 
668  | 1.69M  |     xmlDictEntry *entry = NULL;  | 
669  | 1.69M  |     const xmlChar *ret;  | 
670  | 1.69M  |     unsigned hashValue, newSize;  | 
671  | 1.69M  |     size_t maxLen, len, plen, klen;  | 
672  | 1.69M  |     int found = 0;  | 
673  |  |  | 
674  | 1.69M  |     if ((dict == NULL) || (name == NULL))  | 
675  | 0  |   return(NULL);  | 
676  |  |  | 
677  | 1.69M  |     maxLen = (maybeLen < 0) ? SIZE_MAX : (size_t) maybeLen;  | 
678  |  |  | 
679  | 1.69M  |     if (prefix == NULL) { | 
680  | 1.69M  |         hashValue = xmlDictHashName(dict->seed, name, maxLen, &len);  | 
681  | 1.69M  |         if (len > INT_MAX / 2)  | 
682  | 0  |             return(NULL);  | 
683  | 1.69M  |         klen = len;  | 
684  | 1.69M  |     } else { | 
685  | 0  |         hashValue = xmlDictHashQName(dict->seed, prefix, name, &plen, &len);  | 
686  | 0  |         if ((len > INT_MAX / 2) || (plen >= INT_MAX / 2 - len))  | 
687  | 0  |             return(NULL);  | 
688  | 0  |         klen = plen + 1 + len;  | 
689  | 0  |     }  | 
690  |  |  | 
691  | 1.69M  |     if ((dict->limit > 0) && (klen >= dict->limit))  | 
692  | 0  |         return(NULL);  | 
693  |  |  | 
694  |  |     /*  | 
695  |  |      * Check for an existing entry  | 
696  |  |      */  | 
697  | 1.69M  |     if (dict->size == 0) { | 
698  | 12.5k  |         newSize = MIN_HASH_SIZE;  | 
699  | 1.67M  |     } else { | 
700  | 1.67M  |         entry = xmlDictFindEntry(dict, prefix, name, klen, hashValue, &found);  | 
701  | 1.67M  |         if (found)  | 
702  | 1.57M  |             return(entry);  | 
703  |  |  | 
704  | 108k  |         if (dict->nbElems + 1 > dict->size / MAX_FILL_DENOM * MAX_FILL_NUM) { | 
705  | 4.92k  |             if (dict->size >= MAX_HASH_SIZE)  | 
706  | 0  |                 return(NULL);  | 
707  | 4.92k  |             newSize = dict->size * 2;  | 
708  | 103k  |         } else { | 
709  | 103k  |             newSize = 0;  | 
710  | 103k  |         }  | 
711  | 108k  |     }  | 
712  |  |  | 
713  | 120k  |     if ((dict->subdict != NULL) && (dict->subdict->size > 0)) { | 
714  | 0  |         xmlDictEntry *subEntry;  | 
715  | 0  |         unsigned subHashValue;  | 
716  |  | 
  | 
717  | 0  |         if (prefix == NULL)  | 
718  | 0  |             subHashValue = xmlDictHashName(dict->subdict->seed, name, len,  | 
719  | 0  |                                            &len);  | 
720  | 0  |         else  | 
721  | 0  |             subHashValue = xmlDictHashQName(dict->subdict->seed, prefix, name,  | 
722  | 0  |                                             &plen, &len);  | 
723  | 0  |         subEntry = xmlDictFindEntry(dict->subdict, prefix, name, klen,  | 
724  | 0  |                                     subHashValue, &found);  | 
725  | 0  |         if (found)  | 
726  | 0  |             return(subEntry);  | 
727  | 0  |     }  | 
728  |  |  | 
729  | 120k  |     if (!update)  | 
730  | 0  |         return(NULL);  | 
731  |  |  | 
732  |  |     /*  | 
733  |  |      * Grow the hash table if needed  | 
734  |  |      */  | 
735  | 120k  |     if (newSize > 0) { | 
736  | 17.4k  |         unsigned mask, displ, pos;  | 
737  |  |  | 
738  | 17.4k  |         if (xmlDictGrow(dict, newSize) != 0)  | 
739  | 0  |             return(NULL);  | 
740  |  |  | 
741  |  |         /*  | 
742  |  |          * Find new entry  | 
743  |  |          */  | 
744  | 17.4k  |         mask = dict->size - 1;  | 
745  | 17.4k  |         displ = 0;  | 
746  | 17.4k  |         pos = hashValue & mask;  | 
747  | 17.4k  |         entry = &dict->table[pos];  | 
748  |  |  | 
749  | 20.8k  |         while ((entry->hashValue != 0) &&  | 
750  | 4.67k  |                ((pos - entry->hashValue) & mask) >= displ) { | 
751  | 3.40k  |             displ++;  | 
752  | 3.40k  |             pos++;  | 
753  | 3.40k  |             entry++;  | 
754  | 3.40k  |             if ((pos & mask) == 0)  | 
755  | 388  |                 entry = dict->table;  | 
756  | 3.40k  |         }  | 
757  | 17.4k  |     }  | 
758  |  |  | 
759  | 120k  |     if (prefix == NULL)  | 
760  | 120k  |         ret = xmlDictAddString(dict, name, len);  | 
761  | 0  |     else  | 
762  | 0  |         ret = xmlDictAddQString(dict, prefix, plen, name, len);  | 
763  | 120k  |     if (ret == NULL)  | 
764  | 0  |         return(NULL);  | 
765  |  |  | 
766  |  |     /*  | 
767  |  |      * Shift the remainder of the probe sequence to the right  | 
768  |  |      */  | 
769  | 120k  |     if (entry->hashValue != 0) { | 
770  | 26.2k  |         const xmlDictEntry *end = &dict->table[dict->size];  | 
771  | 26.2k  |         const xmlDictEntry *cur = entry;  | 
772  |  |  | 
773  | 127k  |         do { | 
774  | 127k  |             cur++;  | 
775  | 127k  |             if (cur >= end)  | 
776  | 5.33k  |                 cur = dict->table;  | 
777  | 127k  |         } while (cur->hashValue != 0);  | 
778  |  |  | 
779  | 26.2k  |         if (cur < entry) { | 
780  |  |             /*  | 
781  |  |              * If we traversed the end of the buffer, handle the part  | 
782  |  |              * at the start of the buffer.  | 
783  |  |              */  | 
784  | 5.33k  |             memmove(&dict->table[1], dict->table,  | 
785  | 5.33k  |                     (char *) cur - (char *) dict->table);  | 
786  | 5.33k  |             cur = end - 1;  | 
787  | 5.33k  |             dict->table[0] = *cur;  | 
788  | 5.33k  |         }  | 
789  |  |  | 
790  | 26.2k  |         memmove(&entry[1], entry, (char *) cur - (char *) entry);  | 
791  | 26.2k  |     }  | 
792  |  |  | 
793  |  |     /*  | 
794  |  |      * Populate entry  | 
795  |  |      */  | 
796  | 120k  |     entry->hashValue = hashValue;  | 
797  | 120k  |     entry->name = ret;  | 
798  |  |  | 
799  | 120k  |     dict->nbElems++;  | 
800  |  |  | 
801  | 120k  |     return(entry);  | 
802  | 120k  | }  | 
803  |  |  | 
804  |  | /**  | 
805  |  |  * Lookup a string and add it to the dictionary if it wasn't found.  | 
806  |  |  *  | 
807  |  |  * @param dict  dictionary  | 
808  |  |  * @param name  string key  | 
809  |  |  * @param len  length of the key, if -1 it is recomputed  | 
810  |  |  * @returns the interned copy of the string or NULL if a memory allocation  | 
811  |  |  * failed.  | 
812  |  |  */  | 
813  |  | const xmlChar *  | 
814  | 373k  | xmlDictLookup(xmlDict *dict, const xmlChar *name, int len) { | 
815  | 373k  |     const xmlDictEntry *entry;  | 
816  |  |  | 
817  | 373k  |     entry = xmlDictLookupInternal(dict, NULL, name, len, 1);  | 
818  | 373k  |     if (entry == NULL)  | 
819  | 0  |         return(NULL);  | 
820  | 373k  |     return(entry->name);  | 
821  | 373k  | }  | 
822  |  |  | 
823  |  | /**  | 
824  |  |  * Lookup a dictionary entry and add the string to the dictionary if  | 
825  |  |  * it wasn't found.  | 
826  |  |  *  | 
827  |  |  * @param dict  dictionary  | 
828  |  |  * @param name  string key  | 
829  |  |  * @param len  length of the key, if -1 it is recomputed  | 
830  |  |  * @returns the dictionary entry.  | 
831  |  |  */  | 
832  |  | xmlHashedString  | 
833  | 1.31M  | xmlDictLookupHashed(xmlDict *dict, const xmlChar *name, int len) { | 
834  | 1.31M  |     const xmlDictEntry *entry;  | 
835  | 1.31M  |     xmlHashedString ret;  | 
836  |  |  | 
837  | 1.31M  |     entry = xmlDictLookupInternal(dict, NULL, name, len, 1);  | 
838  |  |  | 
839  | 1.31M  |     if (entry == NULL) { | 
840  | 0  |         ret.name = NULL;  | 
841  | 0  |         ret.hashValue = 0;  | 
842  | 1.31M  |     } else { | 
843  | 1.31M  |         ret = *entry;  | 
844  | 1.31M  |     }  | 
845  |  |  | 
846  | 1.31M  |     return(ret);  | 
847  | 1.31M  | }  | 
848  |  |  | 
849  |  | /**  | 
850  |  |  * Check if a string exists in the dictionary.  | 
851  |  |  *  | 
852  |  |  * @param dict  the dictionary  | 
853  |  |  * @param name  the name of the userdata  | 
854  |  |  * @param len  the length of the name, if -1 it is recomputed  | 
855  |  |  * @returns the internal copy of the name or NULL if not found.  | 
856  |  |  */  | 
857  |  | const xmlChar *  | 
858  | 0  | xmlDictExists(xmlDict *dict, const xmlChar *name, int len) { | 
859  | 0  |     const xmlDictEntry *entry;  | 
860  |  | 
  | 
861  | 0  |     entry = xmlDictLookupInternal(dict, NULL, name, len, 0);  | 
862  | 0  |     if (entry == NULL)  | 
863  | 0  |         return(NULL);  | 
864  | 0  |     return(entry->name);  | 
865  | 0  | }  | 
866  |  |  | 
867  |  | /**  | 
868  |  |  * Lookup the QName `prefix:name` and add it to the dictionary if  | 
869  |  |  * it wasn't found.  | 
870  |  |  *  | 
871  |  |  * @param dict  the dictionary  | 
872  |  |  * @param prefix  the prefix  | 
873  |  |  * @param name  the name  | 
874  |  |  * @returns the interned copy of the string or NULL if a memory allocation  | 
875  |  |  * failed.  | 
876  |  |  */  | 
877  |  | const xmlChar *  | 
878  | 0  | xmlDictQLookup(xmlDict *dict, const xmlChar *prefix, const xmlChar *name) { | 
879  | 0  |     const xmlDictEntry *entry;  | 
880  |  | 
  | 
881  | 0  |     entry = xmlDictLookupInternal(dict, prefix, name, -1, 1);  | 
882  | 0  |     if (entry == NULL)  | 
883  | 0  |         return(NULL);  | 
884  | 0  |     return(entry->name);  | 
885  | 0  | }  | 
886  |  |  | 
887  |  | /*  | 
888  |  |  * Pseudo-random generator  | 
889  |  |  */  | 
890  |  |  | 
891  |  | #ifdef _WIN32  | 
892  |  |   #define WIN32_LEAN_AND_MEAN  | 
893  |  |   #include <windows.h>  | 
894  |  |   #include <bcrypt.h>  | 
895  |  | #else  | 
896  |  |   #if HAVE_DECL_GETENTROPY  | 
897  |  |     /* POSIX 2024 */  | 
898  |  |     #include <unistd.h>  | 
899  |  |     /* Older platforms */  | 
900  |  |     #include <sys/random.h>  | 
901  |  |   #endif  | 
902  |  |   #include <time.h>  | 
903  |  | #endif  | 
904  |  |  | 
905  |  | static xmlMutex xmlRngMutex;  | 
906  |  |  | 
907  |  | static unsigned globalRngState[2];  | 
908  |  |  | 
909  |  | /*  | 
910  |  |  *  | 
911  |  |  * Initialize the PRNG.  | 
912  |  |  */  | 
913  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
914  |  | void  | 
915  | 1  | xmlInitRandom(void) { | 
916  | 1  |     xmlInitMutex(&xmlRngMutex);  | 
917  |  |  | 
918  | 1  |     { | 
919  |  | #ifdef _WIN32  | 
920  |  |         NTSTATUS status;  | 
921  |  |  | 
922  |  |         /*  | 
923  |  |          * You can find many (recent as of 2025) discussions how  | 
924  |  |          * to get a pseudo-random seed on Windows in projects like  | 
925  |  |          * Golang, Rust, Chromium and Firefox.  | 
926  |  |          *  | 
927  |  |          * TODO: Support ProcessPrng available since Windows 10.  | 
928  |  |          */  | 
929  |  |         status = BCryptGenRandom(NULL, (unsigned char *) globalRngState,  | 
930  |  |                                  sizeof(globalRngState),  | 
931  |  |                                  BCRYPT_USE_SYSTEM_PREFERRED_RNG);  | 
932  |  |         if (!BCRYPT_SUCCESS(status))  | 
933  |  |             xmlAbort("libxml2: BCryptGenRandom failed with error code %lu\n", | 
934  |  |                      GetLastError());  | 
935  |  | #else  | 
936  | 1  |         int var;  | 
937  |  |  | 
938  | 1  | #if HAVE_DECL_GETENTROPY  | 
939  | 1  |         while (1) { | 
940  | 1  |             if (getentropy(globalRngState, sizeof(globalRngState)) == 0)  | 
941  | 1  |                 return;  | 
942  |  |  | 
943  |  |             /*  | 
944  |  |              * This most likely means that libxml2 was compiled on  | 
945  |  |              * a system supporting certain system calls and is running  | 
946  |  |              * on a system that doesn't support these calls, as can  | 
947  |  |              * be the case on Linux.  | 
948  |  |              */  | 
949  | 0  |             if (errno == ENOSYS)  | 
950  | 0  |                 break;  | 
951  |  |  | 
952  |  |             /*  | 
953  |  |              * We really don't want to fallback to the unsafe PRNG  | 
954  |  |              * for possibly accidental reasons, so we abort on any  | 
955  |  |              * unknown error.  | 
956  |  |              */  | 
957  | 0  |             if (errno != EINTR)  | 
958  | 0  |                 xmlAbort("libxml2: getentropy failed with error code %d\n", | 
959  | 0  |                          errno);  | 
960  | 0  |         }  | 
961  | 0  | #endif  | 
962  |  |  | 
963  |  |         /*  | 
964  |  |          * TODO: Fallback to /dev/urandom for older POSIX systems.  | 
965  |  |          */  | 
966  | 0  |         globalRngState[0] =  | 
967  | 0  |                 (unsigned) time(NULL) ^  | 
968  | 0  |                 HASH_ROL((unsigned) ((size_t) &xmlInitRandom & 0xFFFFFFFF), 8);  | 
969  | 0  |         globalRngState[1] =  | 
970  | 0  |                 HASH_ROL((unsigned) ((size_t) &xmlRngMutex & 0xFFFFFFFF), 16) ^  | 
971  | 0  |                 HASH_ROL((unsigned) ((size_t) &var & 0xFFFFFFFF), 24);  | 
972  | 0  | #endif  | 
973  | 0  |     }  | 
974  | 0  | }  | 
975  |  |  | 
976  |  | /*  | 
977  |  |  *  | 
978  |  |  * Clean up PRNG globals.  | 
979  |  |  */  | 
980  |  | void  | 
981  | 0  | xmlCleanupRandom(void) { | 
982  | 0  |     xmlCleanupMutex(&xmlRngMutex);  | 
983  | 0  | }  | 
984  |  |  | 
985  |  | ATTRIBUTE_NO_SANITIZE_INTEGER  | 
986  |  | static unsigned  | 
987  | 19.1k  | xoroshiro64ss(unsigned *s) { | 
988  | 19.1k  |     unsigned s0 = s[0];  | 
989  | 19.1k  |     unsigned s1 = s[1];  | 
990  | 19.1k  |     unsigned result = HASH_ROL(s0 * 0x9E3779BB, 5) * 5;  | 
991  |  |  | 
992  | 19.1k  |     s1 ^= s0;  | 
993  | 19.1k  |     s[0] = HASH_ROL(s0, 26) ^ s1 ^ (s1 << 9);  | 
994  | 19.1k  |     s[1] = HASH_ROL(s1, 13);  | 
995  |  |  | 
996  | 19.1k  |     return(result & 0xFFFFFFFF);  | 
997  | 19.1k  | }  | 
998  |  |  | 
999  |  | /*  | 
1000  |  |  *  | 
1001  |  |  * Generate a pseudo-random value using the global PRNG.  | 
1002  |  |  *  | 
1003  |  |  * @returns a random value.  | 
1004  |  |  */  | 
1005  |  | unsigned  | 
1006  | 2  | xmlGlobalRandom(void) { | 
1007  | 2  |     unsigned ret;  | 
1008  |  |  | 
1009  | 2  |     xmlMutexLock(&xmlRngMutex);  | 
1010  | 2  |     ret = xoroshiro64ss(globalRngState);  | 
1011  | 2  |     xmlMutexUnlock(&xmlRngMutex);  | 
1012  |  |  | 
1013  | 2  |     return(ret);  | 
1014  | 2  | }  | 
1015  |  |  | 
1016  |  | /*  | 
1017  |  |  *  | 
1018  |  |  * Generate a pseudo-random value using the thread-local PRNG.  | 
1019  |  |  *  | 
1020  |  |  * @returns a random value.  | 
1021  |  |  */  | 
1022  |  | unsigned  | 
1023  | 19.1k  | xmlRandom(void) { | 
1024  | 19.1k  |     return(xoroshiro64ss(xmlGetLocalRngState()));  | 
1025  | 19.1k  | }  | 
1026  |  |  |