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