2 * dict.c: dictionary of reusable strings, just used to avoid allocation
3 * and freeing operations.
5 * Copyright (C) 2003-2012 Daniel Veillard.
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.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
16 * Author: daniel@veillard.com
31 * Following http://www.ocert.org/advisories/ocert-2011-003.html
32 * it seems that having hash randomization might be a good idea
33 * when using XML with untrusted data
34 * Note1: that it works correctly only if compiled with WITH_BIG_KEY
35 * which is the default.
36 * Note2: the fast function used for a small dict won't protect very
37 * well but since the attack is based on growing a very big hash
38 * list we will use the BigKey algo as soon as the hash size grows
39 * over MIN_DICT_SIZE so this actually works
41 #if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME)
42 #define DICT_RANDOMIZATION
49 #ifdef HAVE_INTTYPES_H
52 typedef unsigned __int32
uint32_t;
55 #include <libxml/tree.h>
56 #include <libxml/dict.h>
57 #include <libxml/xmlmemory.h>
58 #include <libxml/xmlerror.h>
59 #include <libxml/globals.h>
61 /* #define DEBUG_GROW */
62 /* #define DICT_DEBUG_PATTERNS */
64 #define MAX_HASH_LEN 3
65 #define MIN_DICT_SIZE 128
66 #define MAX_DICT_HASH 8 * 2048
70 #define xmlDictComputeKey(dict, name, len) \
71 (((dict)->size == MIN_DICT_SIZE) ? \
72 xmlDictComputeFastKey(name, len, (dict)->seed) : \
73 xmlDictComputeBigKey(name, len, (dict)->seed))
75 #define xmlDictComputeQKey(dict, prefix, plen, name, len) \
76 (((prefix) == NULL) ? \
77 (xmlDictComputeKey(dict, name, len)) : \
78 (((dict)->size == MIN_DICT_SIZE) ? \
79 xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed) : \
80 xmlDictComputeBigQKey(prefix, plen, name, len, (dict)->seed)))
82 #else /* !WITH_BIG_KEY */
83 #define xmlDictComputeKey(dict, name, len) \
84 xmlDictComputeFastKey(name, len, (dict)->seed)
85 #define xmlDictComputeQKey(dict, prefix, plen, name, len) \
86 xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed)
87 #endif /* WITH_BIG_KEY */
90 * An entry in the dictionnary
92 typedef struct _xmlDictEntry xmlDictEntry
;
93 typedef xmlDictEntry
*xmlDictEntryPtr
;
94 struct _xmlDictEntry
{
95 struct _xmlDictEntry
*next
;
102 typedef struct _xmlDictStrings xmlDictStrings
;
103 typedef xmlDictStrings
*xmlDictStringsPtr
;
104 struct _xmlDictStrings
{
105 xmlDictStringsPtr next
;
113 * The entire dictionnary
118 struct _xmlDictEntry
*dict
;
120 unsigned int nbElems
;
121 xmlDictStringsPtr strings
;
123 struct _xmlDict
*subdict
;
124 /* used for randomization */
126 /* used to impose a limit on size */
131 * A mutex for modifying the reference counter for shared
134 static xmlRMutexPtr xmlDictMutex
= NULL
;
137 * Whether the dictionary mutex was initialized.
139 static int xmlDictInitialized
= 0;
141 #ifdef DICT_RANDOMIZATION
144 * Internal data for random function, protected by xmlDictMutex
146 static unsigned int rand_seed
= 0;
153 * Do the dictionary mutex initialization.
154 * this function is deprecated
156 * Returns 0 if initialization was already done, and 1 if that
157 * call led to the initialization
159 int xmlInitializeDict(void) {
164 * __xmlInitializeDict:
166 * This function is not public
167 * Do the dictionary mutex initialization.
168 * this function is not thread safe, initialization should
169 * normally be done once at setup when called from xmlOnceInit()
170 * we may also land in this code if thread support is not compiled in
172 * Returns 0 if initialization was already done, and 1 if that
173 * call led to the initialization
175 int __xmlInitializeDict(void) {
176 if (xmlDictInitialized
)
179 if ((xmlDictMutex
= xmlNewRMutex()) == NULL
)
181 xmlRMutexLock(xmlDictMutex
);
183 #ifdef DICT_RANDOMIZATION
185 rand_seed
= time(NULL
);
191 xmlDictInitialized
= 1;
192 xmlRMutexUnlock(xmlDictMutex
);
196 #ifdef DICT_RANDOMIZATION
197 int __xmlRandom(void) {
200 if (xmlDictInitialized
== 0)
201 __xmlInitializeDict();
203 xmlRMutexLock(xmlDictMutex
);
205 ret
= rand_r(& rand_seed
);
209 xmlRMutexUnlock(xmlDictMutex
);
217 * Free the dictionary mutex. Do not call unless sure the library
218 * is not in use anymore !
221 xmlDictCleanup(void) {
222 if (!xmlDictInitialized
)
225 xmlFreeRMutex(xmlDictMutex
);
227 xmlDictInitialized
= 0;
232 * @dict: the dictionnary
233 * @name: the name of the userdata
234 * @len: the length of the name
236 * Add the string to the array[s]
238 * Returns the pointer of the local string, or NULL in case of error.
240 static const xmlChar
*
241 xmlDictAddString(xmlDictPtr dict
, const xmlChar
*name
, unsigned int namelen
) {
242 xmlDictStringsPtr pool
;
244 size_t size
= 0; /* + sizeof(_xmlDictStrings) == 1024 */
247 #ifdef DICT_DEBUG_PATTERNS
248 fprintf(stderr
, "-");
250 pool
= dict
->strings
;
251 while (pool
!= NULL
) {
252 if (pool
->end
- pool
->free
> namelen
)
254 if (pool
->size
> size
) size
= pool
->size
;
259 * Not found, need to allocate
262 if ((dict
->limit
> 0) && (limit
> dict
->limit
)) {
266 if (size
== 0) size
= 1000;
267 else size
*= 4; /* exponential growth */
268 if (size
< 4 * namelen
)
269 size
= 4 * namelen
; /* just in case ! */
270 pool
= (xmlDictStringsPtr
) xmlMalloc(sizeof(xmlDictStrings
) + size
);
275 pool
->free
= &pool
->array
[0];
276 pool
->end
= &pool
->array
[size
];
277 pool
->next
= dict
->strings
;
278 dict
->strings
= pool
;
279 #ifdef DICT_DEBUG_PATTERNS
280 fprintf(stderr
, "+");
285 memcpy(pool
->free
, name
, namelen
);
286 pool
->free
+= namelen
;
294 * @dict: the dictionnary
295 * @prefix: the prefix of the userdata
296 * @plen: the prefix length
297 * @name: the name of the userdata
298 * @len: the length of the name
300 * Add the QName to the array[s]
302 * Returns the pointer of the local string, or NULL in case of error.
304 static const xmlChar
*
305 xmlDictAddQString(xmlDictPtr dict
, const xmlChar
*prefix
, unsigned int plen
,
306 const xmlChar
*name
, unsigned int namelen
)
308 xmlDictStringsPtr pool
;
310 size_t size
= 0; /* + sizeof(_xmlDictStrings) == 1024 */
313 if (prefix
== NULL
) return(xmlDictAddString(dict
, name
, namelen
));
315 #ifdef DICT_DEBUG_PATTERNS
316 fprintf(stderr
, "=");
318 pool
= dict
->strings
;
319 while (pool
!= NULL
) {
320 if (pool
->end
- pool
->free
> namelen
+ plen
+ 1)
322 if (pool
->size
> size
) size
= pool
->size
;
327 * Not found, need to allocate
330 if ((dict
->limit
> 0) && (limit
> dict
->limit
)) {
334 if (size
== 0) size
= 1000;
335 else size
*= 4; /* exponential growth */
336 if (size
< 4 * (namelen
+ plen
+ 1))
337 size
= 4 * (namelen
+ plen
+ 1); /* just in case ! */
338 pool
= (xmlDictStringsPtr
) xmlMalloc(sizeof(xmlDictStrings
) + size
);
343 pool
->free
= &pool
->array
[0];
344 pool
->end
= &pool
->array
[size
];
345 pool
->next
= dict
->strings
;
346 dict
->strings
= pool
;
347 #ifdef DICT_DEBUG_PATTERNS
348 fprintf(stderr
, "+");
353 memcpy(pool
->free
, prefix
, plen
);
355 *(pool
->free
++) = ':';
356 memcpy(pool
->free
, name
, namelen
);
357 pool
->free
+= namelen
;
365 * xmlDictComputeBigKey:
367 * Calculate a hash key using a good hash function that works well for
368 * larger hash table sizes.
370 * Hash function by "One-at-a-Time Hash" see
371 * http://burtleburtle.net/bob/hash/doobs.html
375 xmlDictComputeBigKey(const xmlChar
* data
, int namelen
, int seed
) {
379 if (namelen
<= 0 || data
== NULL
) return(0);
383 for (i
= 0;i
< namelen
; i
++) {
385 hash
+= (hash
<< 10);
389 hash
^= (hash
>> 11);
390 hash
+= (hash
<< 15);
396 * xmlDictComputeBigQKey:
398 * Calculate a hash key for two strings using a good hash function
399 * that works well for larger hash table sizes.
401 * Hash function by "One-at-a-Time Hash" see
402 * http://burtleburtle.net/bob/hash/doobs.html
404 * Neither of the two strings must be NULL.
407 xmlDictComputeBigQKey(const xmlChar
*prefix
, int plen
,
408 const xmlChar
*name
, int len
, int seed
)
415 for (i
= 0;i
< plen
; i
++) {
417 hash
+= (hash
<< 10);
421 hash
+= (hash
<< 10);
424 for (i
= 0;i
< len
; i
++) {
426 hash
+= (hash
<< 10);
430 hash
^= (hash
>> 11);
431 hash
+= (hash
<< 15);
435 #endif /* WITH_BIG_KEY */
438 * xmlDictComputeFastKey:
440 * Calculate a hash key using a fast hash function that works well
441 * for low hash table fill.
444 xmlDictComputeFastKey(const xmlChar
*name
, int namelen
, int seed
) {
445 unsigned long value
= seed
;
447 if (name
== NULL
) return(0);
451 value
+= name
[namelen
- 1];
455 case 10: value
+= name
[9];
456 case 9: value
+= name
[8];
457 case 8: value
+= name
[7];
458 case 7: value
+= name
[6];
459 case 6: value
+= name
[5];
460 case 5: value
+= name
[4];
461 case 4: value
+= name
[3];
462 case 3: value
+= name
[2];
463 case 2: value
+= name
[1];
470 * xmlDictComputeFastQKey:
472 * Calculate a hash key for two strings using a fast hash function
473 * that works well for low hash table fill.
475 * Neither of the two strings must be NULL.
478 xmlDictComputeFastQKey(const xmlChar
*prefix
, int plen
,
479 const xmlChar
*name
, int len
, int seed
)
481 unsigned long value
= (unsigned long) seed
;
484 value
+= 30 * (unsigned long) ':';
486 value
+= 30 * (*prefix
);
489 value
+= name
[len
- (plen
+ 1 + 1)];
495 case 10: value
+= prefix
[9];
496 case 9: value
+= prefix
[8];
497 case 8: value
+= prefix
[7];
498 case 7: value
+= prefix
[6];
499 case 6: value
+= prefix
[5];
500 case 5: value
+= prefix
[4];
501 case 4: value
+= prefix
[3];
502 case 3: value
+= prefix
[2];
503 case 2: value
+= prefix
[1];
504 case 1: value
+= prefix
[0];
509 value
+= (unsigned long) ':';
513 case 10: value
+= name
[9];
514 case 9: value
+= name
[8];
515 case 8: value
+= name
[7];
516 case 7: value
+= name
[6];
517 case 6: value
+= name
[5];
518 case 5: value
+= name
[4];
519 case 4: value
+= name
[3];
520 case 3: value
+= name
[2];
521 case 2: value
+= name
[1];
522 case 1: value
+= name
[0];
531 * Create a new dictionary
533 * Returns the newly created dictionnary, or NULL if an error occured.
536 xmlDictCreate(void) {
539 if (!xmlDictInitialized
)
540 if (!__xmlInitializeDict())
543 #ifdef DICT_DEBUG_PATTERNS
544 fprintf(stderr
, "C");
547 dict
= xmlMalloc(sizeof(xmlDict
));
549 dict
->ref_counter
= 1;
552 dict
->size
= MIN_DICT_SIZE
;
554 dict
->dict
= xmlMalloc(MIN_DICT_SIZE
* sizeof(xmlDictEntry
));
555 dict
->strings
= NULL
;
556 dict
->subdict
= NULL
;
558 memset(dict
->dict
, 0, MIN_DICT_SIZE
* sizeof(xmlDictEntry
));
559 #ifdef DICT_RANDOMIZATION
560 dict
->seed
= __xmlRandom();
573 * @sub: an existing dictionnary
575 * Create a new dictionary, inheriting strings from the read-only
576 * dictionnary @sub. On lookup, strings are first searched in the
577 * new dictionnary, then in @sub, and if not found are created in the
580 * Returns the newly created dictionnary, or NULL if an error occured.
583 xmlDictCreateSub(xmlDictPtr sub
) {
584 xmlDictPtr dict
= xmlDictCreate();
586 if ((dict
!= NULL
) && (sub
!= NULL
)) {
587 #ifdef DICT_DEBUG_PATTERNS
588 fprintf(stderr
, "R");
590 dict
->seed
= sub
->seed
;
592 xmlDictReference(dict
->subdict
);
599 * @dict: the dictionnary
601 * Increment the reference counter of a dictionary
603 * Returns 0 in case of success and -1 in case of error
606 xmlDictReference(xmlDictPtr dict
) {
607 if (!xmlDictInitialized
)
608 if (!__xmlInitializeDict())
611 if (dict
== NULL
) return -1;
612 xmlRMutexLock(xmlDictMutex
);
614 xmlRMutexUnlock(xmlDictMutex
);
620 * @dict: the dictionnary
621 * @size: the new size of the dictionnary
623 * resize the dictionnary
625 * Returns 0 in case of success, -1 in case of failure
628 xmlDictGrow(xmlDictPtr dict
, size_t size
) {
629 unsigned long key
, okey
;
631 xmlDictEntryPtr iter
, next
;
632 struct _xmlDictEntry
*olddict
;
634 unsigned long nbElem
= 0;
646 #ifdef DICT_DEBUG_PATTERNS
647 fprintf(stderr
, "*");
650 oldsize
= dict
->size
;
651 olddict
= dict
->dict
;
654 if (oldsize
== MIN_DICT_SIZE
)
657 dict
->dict
= xmlMalloc(size
* sizeof(xmlDictEntry
));
658 if (dict
->dict
== NULL
) {
659 dict
->dict
= olddict
;
662 memset(dict
->dict
, 0, size
* sizeof(xmlDictEntry
));
665 /* If the two loops are merged, there would be situations where
666 a new entry needs to allocated and data copied into it from
667 the main dict. It is nicer to run through the array twice, first
668 copying all the elements in the main array (less probability of
669 allocate) and then the rest, so we only free in the second loop.
671 for (i
= 0; i
< oldsize
; i
++) {
672 if (olddict
[i
].valid
== 0)
676 okey
= olddict
[i
].okey
;
678 okey
= xmlDictComputeKey(dict
, olddict
[i
].name
, olddict
[i
].len
);
679 key
= okey
% dict
->size
;
681 if (dict
->dict
[key
].valid
== 0) {
682 memcpy(&(dict
->dict
[key
]), &(olddict
[i
]), sizeof(xmlDictEntry
));
683 dict
->dict
[key
].next
= NULL
;
684 dict
->dict
[key
].okey
= okey
;
686 xmlDictEntryPtr entry
;
688 entry
= xmlMalloc(sizeof(xmlDictEntry
));
690 entry
->name
= olddict
[i
].name
;
691 entry
->len
= olddict
[i
].len
;
693 entry
->next
= dict
->dict
[key
].next
;
695 dict
->dict
[key
].next
= entry
;
698 * we don't have much ways to alert from herei
699 * result is loosing an entry and unicity garantee
709 for (i
= 0; i
< oldsize
; i
++) {
710 iter
= olddict
[i
].next
;
715 * put back the entry in the new dict
721 okey
= xmlDictComputeKey(dict
, iter
->name
, iter
->len
);
722 key
= okey
% dict
->size
;
723 if (dict
->dict
[key
].valid
== 0) {
724 memcpy(&(dict
->dict
[key
]), iter
, sizeof(xmlDictEntry
));
725 dict
->dict
[key
].next
= NULL
;
726 dict
->dict
[key
].valid
= 1;
727 dict
->dict
[key
].okey
= okey
;
730 iter
->next
= dict
->dict
[key
].next
;
732 dict
->dict
[key
].next
= iter
;
746 xmlGenericError(xmlGenericErrorContext
,
747 "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize
, size
, nbElem
);
755 * @dict: the dictionnary
757 * Free the hash @dict and its contents. The userdata is
758 * deallocated with @f if provided.
761 xmlDictFree(xmlDictPtr dict
) {
763 xmlDictEntryPtr iter
;
764 xmlDictEntryPtr next
;
766 xmlDictStringsPtr pool
, nextp
;
771 if (!xmlDictInitialized
)
772 if (!__xmlInitializeDict())
775 /* decrement the counter, it may be shared by a parser and docs */
776 xmlRMutexLock(xmlDictMutex
);
778 if (dict
->ref_counter
> 0) {
779 xmlRMutexUnlock(xmlDictMutex
);
783 xmlRMutexUnlock(xmlDictMutex
);
785 if (dict
->subdict
!= NULL
) {
786 xmlDictFree(dict
->subdict
);
790 for(i
= 0; ((i
< dict
->size
) && (dict
->nbElems
> 0)); i
++) {
791 iter
= &(dict
->dict
[i
]);
792 if (iter
->valid
== 0)
806 pool
= dict
->strings
;
807 while (pool
!= NULL
) {
817 * @dict: the dictionnary
818 * @name: the name of the userdata
819 * @len: the length of the name, if -1 it is recomputed
821 * Add the @name to the dictionnary @dict if not present.
823 * Returns the internal copy of the name or NULL in case of internal error
826 xmlDictLookup(xmlDictPtr dict
, const xmlChar
*name
, int len
) {
827 unsigned long key
, okey
, nbi
= 0;
828 xmlDictEntryPtr entry
;
829 xmlDictEntryPtr insert
;
833 if ((dict
== NULL
) || (name
== NULL
))
837 l
= strlen((const char *) name
);
841 if (((dict
->limit
> 0) && (l
>= dict
->limit
)) ||
846 * Check for duplicate and insertion location.
848 okey
= xmlDictComputeKey(dict
, name
, l
);
849 key
= okey
% dict
->size
;
850 if (dict
->dict
[key
].valid
== 0) {
853 for (insert
= &(dict
->dict
[key
]); insert
->next
!= NULL
;
854 insert
= insert
->next
) {
856 if ((insert
->okey
== okey
) && (insert
->len
== l
)) {
857 if (!memcmp(insert
->name
, name
, l
))
858 return(insert
->name
);
861 if ((insert
->okey
== okey
) && (insert
->len
== l
) &&
862 (!xmlStrncmp(insert
->name
, name
, l
)))
863 return(insert
->name
);
868 if ((insert
->okey
== okey
) && (insert
->len
== l
)) {
869 if (!memcmp(insert
->name
, name
, l
))
870 return(insert
->name
);
873 if ((insert
->okey
== okey
) && (insert
->len
== l
) &&
874 (!xmlStrncmp(insert
->name
, name
, l
)))
875 return(insert
->name
);
882 /* we cannot always reuse the same okey for the subdict */
883 if (((dict
->size
== MIN_DICT_SIZE
) &&
884 (dict
->subdict
->size
!= MIN_DICT_SIZE
)) ||
885 ((dict
->size
!= MIN_DICT_SIZE
) &&
886 (dict
->subdict
->size
== MIN_DICT_SIZE
)))
887 skey
= xmlDictComputeKey(dict
->subdict
, name
, l
);
891 key
= skey
% dict
->subdict
->size
;
892 if (dict
->subdict
->dict
[key
].valid
!= 0) {
895 for (tmp
= &(dict
->subdict
->dict
[key
]); tmp
->next
!= NULL
;
898 if ((tmp
->okey
== skey
) && (tmp
->len
== l
)) {
899 if (!memcmp(tmp
->name
, name
, l
))
903 if ((tmp
->okey
== skey
) && (tmp
->len
== l
) &&
904 (!xmlStrncmp(tmp
->name
, name
, l
)))
910 if ((tmp
->okey
== skey
) && (tmp
->len
== l
)) {
911 if (!memcmp(tmp
->name
, name
, l
))
915 if ((tmp
->okey
== skey
) && (tmp
->len
== l
) &&
916 (!xmlStrncmp(tmp
->name
, name
, l
)))
920 key
= okey
% dict
->size
;
923 ret
= xmlDictAddString(dict
, name
, l
);
926 if (insert
== NULL
) {
927 entry
= &(dict
->dict
[key
]);
929 entry
= xmlMalloc(sizeof(xmlDictEntry
));
941 insert
->next
= entry
;
945 if ((nbi
> MAX_HASH_LEN
) &&
946 (dict
->size
<= ((MAX_DICT_HASH
/ 2) / MAX_HASH_LEN
))) {
947 if (xmlDictGrow(dict
, MAX_HASH_LEN
* 2 * dict
->size
) != 0)
950 /* Note that entry may have been freed at this point by xmlDictGrow */
957 * @dict: the dictionnary
958 * @name: the name of the userdata
959 * @len: the length of the name, if -1 it is recomputed
961 * Check if the @name exists in the dictionnary @dict.
963 * Returns the internal copy of the name or NULL if not found.
966 xmlDictExists(xmlDictPtr dict
, const xmlChar
*name
, int len
) {
967 unsigned long key
, okey
, nbi
= 0;
968 xmlDictEntryPtr insert
;
971 if ((dict
== NULL
) || (name
== NULL
))
975 l
= strlen((const char *) name
);
978 if (((dict
->limit
> 0) && (l
>= dict
->limit
)) ||
983 * Check for duplicate and insertion location.
985 okey
= xmlDictComputeKey(dict
, name
, l
);
986 key
= okey
% dict
->size
;
987 if (dict
->dict
[key
].valid
== 0) {
990 for (insert
= &(dict
->dict
[key
]); insert
->next
!= NULL
;
991 insert
= insert
->next
) {
993 if ((insert
->okey
== okey
) && (insert
->len
== l
)) {
994 if (!memcmp(insert
->name
, name
, l
))
995 return(insert
->name
);
998 if ((insert
->okey
== okey
) && (insert
->len
== l
) &&
999 (!xmlStrncmp(insert
->name
, name
, l
)))
1000 return(insert
->name
);
1005 if ((insert
->okey
== okey
) && (insert
->len
== l
)) {
1006 if (!memcmp(insert
->name
, name
, l
))
1007 return(insert
->name
);
1010 if ((insert
->okey
== okey
) && (insert
->len
== l
) &&
1011 (!xmlStrncmp(insert
->name
, name
, l
)))
1012 return(insert
->name
);
1016 if (dict
->subdict
) {
1019 /* we cannot always reuse the same okey for the subdict */
1020 if (((dict
->size
== MIN_DICT_SIZE
) &&
1021 (dict
->subdict
->size
!= MIN_DICT_SIZE
)) ||
1022 ((dict
->size
!= MIN_DICT_SIZE
) &&
1023 (dict
->subdict
->size
== MIN_DICT_SIZE
)))
1024 skey
= xmlDictComputeKey(dict
->subdict
, name
, l
);
1028 key
= skey
% dict
->subdict
->size
;
1029 if (dict
->subdict
->dict
[key
].valid
!= 0) {
1030 xmlDictEntryPtr tmp
;
1032 for (tmp
= &(dict
->subdict
->dict
[key
]); tmp
->next
!= NULL
;
1035 if ((tmp
->okey
== skey
) && (tmp
->len
== l
)) {
1036 if (!memcmp(tmp
->name
, name
, l
))
1040 if ((tmp
->okey
== skey
) && (tmp
->len
== l
) &&
1041 (!xmlStrncmp(tmp
->name
, name
, l
)))
1047 if ((tmp
->okey
== skey
) && (tmp
->len
== l
)) {
1048 if (!memcmp(tmp
->name
, name
, l
))
1052 if ((tmp
->okey
== skey
) && (tmp
->len
== l
) &&
1053 (!xmlStrncmp(tmp
->name
, name
, l
)))
1065 * @dict: the dictionnary
1066 * @prefix: the prefix
1069 * Add the QName @prefix:@name to the hash @dict if not present.
1071 * Returns the internal copy of the QName or NULL in case of internal error
1074 xmlDictQLookup(xmlDictPtr dict
, const xmlChar
*prefix
, const xmlChar
*name
) {
1075 unsigned long okey
, key
, nbi
= 0;
1076 xmlDictEntryPtr entry
;
1077 xmlDictEntryPtr insert
;
1079 unsigned int len
, plen
, l
;
1081 if ((dict
== NULL
) || (name
== NULL
))
1084 return(xmlDictLookup(dict
, name
, -1));
1086 l
= len
= strlen((const char *) name
);
1087 plen
= strlen((const char *) prefix
);
1091 * Check for duplicate and insertion location.
1093 okey
= xmlDictComputeQKey(dict
, prefix
, plen
, name
, l
);
1094 key
= okey
% dict
->size
;
1095 if (dict
->dict
[key
].valid
== 0) {
1098 for (insert
= &(dict
->dict
[key
]); insert
->next
!= NULL
;
1099 insert
= insert
->next
) {
1100 if ((insert
->okey
== okey
) && (insert
->len
== len
) &&
1101 (xmlStrQEqual(prefix
, name
, insert
->name
)))
1102 return(insert
->name
);
1105 if ((insert
->okey
== okey
) && (insert
->len
== len
) &&
1106 (xmlStrQEqual(prefix
, name
, insert
->name
)))
1107 return(insert
->name
);
1110 if (dict
->subdict
) {
1113 /* we cannot always reuse the same okey for the subdict */
1114 if (((dict
->size
== MIN_DICT_SIZE
) &&
1115 (dict
->subdict
->size
!= MIN_DICT_SIZE
)) ||
1116 ((dict
->size
!= MIN_DICT_SIZE
) &&
1117 (dict
->subdict
->size
== MIN_DICT_SIZE
)))
1118 skey
= xmlDictComputeQKey(dict
->subdict
, prefix
, plen
, name
, l
);
1122 key
= skey
% dict
->subdict
->size
;
1123 if (dict
->subdict
->dict
[key
].valid
!= 0) {
1124 xmlDictEntryPtr tmp
;
1125 for (tmp
= &(dict
->subdict
->dict
[key
]); tmp
->next
!= NULL
;
1127 if ((tmp
->okey
== skey
) && (tmp
->len
== len
) &&
1128 (xmlStrQEqual(prefix
, name
, tmp
->name
)))
1132 if ((tmp
->okey
== skey
) && (tmp
->len
== len
) &&
1133 (xmlStrQEqual(prefix
, name
, tmp
->name
)))
1136 key
= okey
% dict
->size
;
1139 ret
= xmlDictAddQString(dict
, prefix
, plen
, name
, l
);
1142 if (insert
== NULL
) {
1143 entry
= &(dict
->dict
[key
]);
1145 entry
= xmlMalloc(sizeof(xmlDictEntry
));
1156 insert
->next
= entry
;
1160 if ((nbi
> MAX_HASH_LEN
) &&
1161 (dict
->size
<= ((MAX_DICT_HASH
/ 2) / MAX_HASH_LEN
)))
1162 xmlDictGrow(dict
, MAX_HASH_LEN
* 2 * dict
->size
);
1163 /* Note that entry may have been freed at this point by xmlDictGrow */
1170 * @dict: the dictionnary
1173 * check if a string is owned by the disctionary
1175 * Returns 1 if true, 0 if false and -1 in case of error
1176 * -1 in case of error
1179 xmlDictOwns(xmlDictPtr dict
, const xmlChar
*str
) {
1180 xmlDictStringsPtr pool
;
1182 if ((dict
== NULL
) || (str
== NULL
))
1184 pool
= dict
->strings
;
1185 while (pool
!= NULL
) {
1186 if ((str
>= &pool
->array
[0]) && (str
<= pool
->free
))
1191 return(xmlDictOwns(dict
->subdict
, str
));
1197 * @dict: the dictionnary
1199 * Query the number of elements installed in the hash @dict.
1201 * Returns the number of elements in the dictionnary or
1202 * -1 in case of error
1205 xmlDictSize(xmlDictPtr dict
) {
1209 return(dict
->nbElems
+ dict
->subdict
->nbElems
);
1210 return(dict
->nbElems
);
1215 * @dict: the dictionnary
1216 * @limit: the limit in bytes
1218 * Set a size limit for the dictionary
1221 * Returns the previous limit of the dictionary or 0
1224 xmlDictSetLimit(xmlDictPtr dict
, size_t limit
) {
1230 dict
->limit
= limit
;
1236 * @dict: the dictionnary
1238 * Get how much memory is used by a dictionary for strings
1241 * Returns the amount of strings allocated
1244 xmlDictGetUsage(xmlDictPtr dict
) {
1245 xmlDictStringsPtr pool
;
1250 pool
= dict
->strings
;
1251 while (pool
!= NULL
) {
1252 limit
+= pool
->size
;
1259 #include "elfgcchack.h"