Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * dict.c: dictionary of reusable strings, just used to avoid allocation |
| 3 | * and freeing operations. |
| 4 | * |
| 5 | * Copyright (C) 2003 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 | * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND |
| 14 | * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. |
| 15 | * |
| 16 | * Author: daniel@veillard.com |
| 17 | */ |
| 18 | |
| 19 | #define IN_LIBXML |
| 20 | #include "libxml.h" |
| 21 | |
| 22 | #include <string.h> |
Rob Richards | b6b2ee1 | 2008-05-03 12:34:25 +0000 | [diff] [blame] | 23 | #ifdef HAVE_STDINT_H |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 24 | #include <stdint.h> |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 25 | #else |
| 26 | #ifdef HAVE_INTTYPES_H |
| 27 | #include <inttypes.h> |
Rob Richards | b6b2ee1 | 2008-05-03 12:34:25 +0000 | [diff] [blame] | 28 | #elif defined(WIN32) |
| 29 | typedef unsigned __int32 uint32_t; |
Rob Richards | b6b2ee1 | 2008-05-03 12:34:25 +0000 | [diff] [blame] | 30 | #endif |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 31 | #endif |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 32 | #include <libxml/tree.h> |
| 33 | #include <libxml/dict.h> |
| 34 | #include <libxml/xmlmemory.h> |
| 35 | #include <libxml/xmlerror.h> |
| 36 | #include <libxml/globals.h> |
| 37 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 38 | /* #define DEBUG_GROW */ |
| 39 | /* #define DICT_DEBUG_PATTERNS */ |
| 40 | |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 41 | #define MAX_HASH_LEN 3 |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 42 | #define MIN_DICT_SIZE 128 |
| 43 | #define MAX_DICT_HASH 8 * 2048 |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 44 | #define WITH_BIG_KEY |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 45 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 46 | #ifdef WITH_BIG_KEY |
| 47 | #define xmlDictComputeKey(dict, name, len) \ |
| 48 | (((dict)->size == MIN_DICT_SIZE) ? \ |
| 49 | xmlDictComputeFastKey(name, len) : \ |
| 50 | xmlDictComputeBigKey(name, len)) |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 52 | #define xmlDictComputeQKey(dict, prefix, plen, name, len) \ |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 53 | (((prefix) == NULL) ? \ |
| 54 | (xmlDictComputeKey(dict, name, len)) : \ |
| 55 | (((dict)->size == MIN_DICT_SIZE) ? \ |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 56 | xmlDictComputeFastQKey(prefix, plen, name, len) : \ |
| 57 | xmlDictComputeBigQKey(prefix, plen, name, len))) |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 58 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 59 | #else /* !WITH_BIG_KEY */ |
| 60 | #define xmlDictComputeKey(dict, name, len) \ |
| 61 | xmlDictComputeFastKey(name, len) |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 62 | #define xmlDictComputeQKey(dict, prefix, plen, name, len) \ |
| 63 | xmlDictComputeFastQKey(prefix, plen, name, len) |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 64 | #endif /* WITH_BIG_KEY */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * An entry in the dictionnary |
| 68 | */ |
| 69 | typedef struct _xmlDictEntry xmlDictEntry; |
| 70 | typedef xmlDictEntry *xmlDictEntryPtr; |
| 71 | struct _xmlDictEntry { |
| 72 | struct _xmlDictEntry *next; |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 73 | const xmlChar *name; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 74 | int len; |
| 75 | int valid; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 76 | unsigned long okey; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 79 | typedef struct _xmlDictStrings xmlDictStrings; |
| 80 | typedef xmlDictStrings *xmlDictStringsPtr; |
| 81 | struct _xmlDictStrings { |
| 82 | xmlDictStringsPtr next; |
| 83 | xmlChar *free; |
| 84 | xmlChar *end; |
| 85 | int size; |
| 86 | int nbStrings; |
| 87 | xmlChar array[1]; |
| 88 | }; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 89 | /* |
| 90 | * The entire dictionnary |
| 91 | */ |
| 92 | struct _xmlDict { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 93 | int ref_counter; |
| 94 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 95 | struct _xmlDictEntry *dict; |
| 96 | int size; |
| 97 | int nbElems; |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 98 | xmlDictStringsPtr strings; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 99 | |
| 100 | struct _xmlDict *subdict; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /* |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 104 | * A mutex for modifying the reference counter for shared |
| 105 | * dictionaries. |
| 106 | */ |
| 107 | static xmlRMutexPtr xmlDictMutex = NULL; |
| 108 | |
| 109 | /* |
| 110 | * Whether the dictionary mutex was initialized. |
| 111 | */ |
| 112 | static int xmlDictInitialized = 0; |
| 113 | |
| 114 | /** |
William M. Brack | 4e1c2db | 2005-02-11 10:58:55 +0000 | [diff] [blame] | 115 | * xmlInitializeDict: |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 116 | * |
| 117 | * Do the dictionary mutex initialization. |
| 118 | * this function is not thread safe, initialization should |
| 119 | * preferably be done once at startup |
| 120 | */ |
William M. Brack | 4e1c2db | 2005-02-11 10:58:55 +0000 | [diff] [blame] | 121 | static int xmlInitializeDict(void) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 122 | if (xmlDictInitialized) |
| 123 | return(1); |
| 124 | |
| 125 | if ((xmlDictMutex = xmlNewRMutex()) == NULL) |
| 126 | return(0); |
| 127 | |
| 128 | xmlDictInitialized = 1; |
| 129 | return(1); |
| 130 | } |
| 131 | |
| 132 | /** |
Daniel Veillard | 2ae1338 | 2005-01-25 23:45:06 +0000 | [diff] [blame] | 133 | * xmlDictCleanup: |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 134 | * |
| 135 | * Free the dictionary mutex. |
| 136 | */ |
| 137 | void |
| 138 | xmlDictCleanup(void) { |
| 139 | if (!xmlDictInitialized) |
| 140 | return; |
| 141 | |
| 142 | xmlFreeRMutex(xmlDictMutex); |
| 143 | |
| 144 | xmlDictInitialized = 0; |
| 145 | } |
| 146 | |
| 147 | /* |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 148 | * xmlDictAddString: |
| 149 | * @dict: the dictionnary |
| 150 | * @name: the name of the userdata |
| 151 | * @len: the length of the name, if -1 it is recomputed |
| 152 | * |
| 153 | * Add the string to the array[s] |
| 154 | * |
| 155 | * Returns the pointer of the local string, or NULL in case of error. |
| 156 | */ |
| 157 | static const xmlChar * |
| 158 | xmlDictAddString(xmlDictPtr dict, const xmlChar *name, int namelen) { |
| 159 | xmlDictStringsPtr pool; |
| 160 | const xmlChar *ret; |
| 161 | int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ |
| 162 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 163 | #ifdef DICT_DEBUG_PATTERNS |
| 164 | fprintf(stderr, "-"); |
| 165 | #endif |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 166 | pool = dict->strings; |
| 167 | while (pool != NULL) { |
| 168 | if (pool->end - pool->free > namelen) |
| 169 | goto found_pool; |
| 170 | if (pool->size > size) size = pool->size; |
| 171 | pool = pool->next; |
| 172 | } |
| 173 | /* |
| 174 | * Not found, need to allocate |
| 175 | */ |
| 176 | if (pool == NULL) { |
| 177 | if (size == 0) size = 1000; |
| 178 | else size *= 4; /* exponential growth */ |
| 179 | if (size < 4 * namelen) |
| 180 | size = 4 * namelen; /* just in case ! */ |
| 181 | pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size); |
| 182 | if (pool == NULL) |
| 183 | return(NULL); |
| 184 | pool->size = size; |
| 185 | pool->nbStrings = 0; |
| 186 | pool->free = &pool->array[0]; |
| 187 | pool->end = &pool->array[size]; |
| 188 | pool->next = dict->strings; |
| 189 | dict->strings = pool; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 190 | #ifdef DICT_DEBUG_PATTERNS |
| 191 | fprintf(stderr, "+"); |
| 192 | #endif |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 193 | } |
| 194 | found_pool: |
| 195 | ret = pool->free; |
| 196 | memcpy(pool->free, name, namelen); |
| 197 | pool->free += namelen; |
| 198 | *(pool->free++) = 0; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 199 | pool->nbStrings++; |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 200 | return(ret); |
| 201 | } |
| 202 | |
| 203 | /* |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 204 | * xmlDictAddQString: |
| 205 | * @dict: the dictionnary |
| 206 | * @prefix: the prefix of the userdata |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 207 | * @plen: the prefix length |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 208 | * @name: the name of the userdata |
| 209 | * @len: the length of the name, if -1 it is recomputed |
| 210 | * |
| 211 | * Add the QName to the array[s] |
| 212 | * |
| 213 | * Returns the pointer of the local string, or NULL in case of error. |
| 214 | */ |
| 215 | static const xmlChar * |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 216 | xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, int plen, |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 217 | const xmlChar *name, int namelen) |
| 218 | { |
| 219 | xmlDictStringsPtr pool; |
| 220 | const xmlChar *ret; |
| 221 | int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 222 | |
| 223 | if (prefix == NULL) return(xmlDictAddString(dict, name, namelen)); |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 224 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 225 | #ifdef DICT_DEBUG_PATTERNS |
| 226 | fprintf(stderr, "="); |
| 227 | #endif |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 228 | pool = dict->strings; |
| 229 | while (pool != NULL) { |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 230 | if (pool->end - pool->free > namelen + plen + 1) |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 231 | goto found_pool; |
| 232 | if (pool->size > size) size = pool->size; |
| 233 | pool = pool->next; |
| 234 | } |
| 235 | /* |
| 236 | * Not found, need to allocate |
| 237 | */ |
| 238 | if (pool == NULL) { |
| 239 | if (size == 0) size = 1000; |
| 240 | else size *= 4; /* exponential growth */ |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 241 | if (size < 4 * (namelen + plen + 1)) |
| 242 | size = 4 * (namelen + plen + 1); /* just in case ! */ |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 243 | pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size); |
| 244 | if (pool == NULL) |
| 245 | return(NULL); |
| 246 | pool->size = size; |
| 247 | pool->nbStrings = 0; |
| 248 | pool->free = &pool->array[0]; |
| 249 | pool->end = &pool->array[size]; |
| 250 | pool->next = dict->strings; |
| 251 | dict->strings = pool; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 252 | #ifdef DICT_DEBUG_PATTERNS |
| 253 | fprintf(stderr, "+"); |
| 254 | #endif |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 255 | } |
| 256 | found_pool: |
| 257 | ret = pool->free; |
| 258 | memcpy(pool->free, prefix, plen); |
| 259 | pool->free += plen; |
| 260 | *(pool->free++) = ':'; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 261 | memcpy(pool->free, name, namelen); |
| 262 | pool->free += namelen; |
| 263 | *(pool->free++) = 0; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 264 | pool->nbStrings++; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 265 | return(ret); |
| 266 | } |
| 267 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 268 | #ifdef WITH_BIG_KEY |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 269 | /* |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 270 | * xmlDictComputeBigKey: |
| 271 | * |
| 272 | * Calculate a hash key using a good hash function that works well for |
| 273 | * larger hash table sizes. |
| 274 | * |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 275 | * Hash function by "One-at-a-Time Hash" see |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 276 | * http://burtleburtle.net/bob/hash/doobs.html |
| 277 | */ |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 278 | |
| 279 | static uint32_t |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 280 | xmlDictComputeBigKey(const xmlChar* data, int namelen) { |
| 281 | uint32_t hash; |
| 282 | int i; |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 283 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 284 | if (namelen <= 0 || data == NULL) return(0); |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 285 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 286 | hash = 0; |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 287 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 288 | for (i = 0;i < namelen; i++) { |
| 289 | hash += data[i]; |
| 290 | hash += (hash << 10); |
| 291 | hash ^= (hash >> 6); |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 292 | } |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 293 | hash += (hash << 3); |
| 294 | hash ^= (hash >> 11); |
| 295 | hash += (hash << 15); |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 296 | |
| 297 | return hash; |
| 298 | } |
| 299 | |
| 300 | /* |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 301 | * xmlDictComputeBigQKey: |
| 302 | * |
| 303 | * Calculate a hash key for two strings using a good hash function |
| 304 | * that works well for larger hash table sizes. |
| 305 | * |
| 306 | * Hash function by "One-at-a-Time Hash" see |
| 307 | * http://burtleburtle.net/bob/hash/doobs.html |
| 308 | * |
| 309 | * Neither of the two strings must be NULL. |
| 310 | */ |
| 311 | static unsigned long |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 312 | xmlDictComputeBigQKey(const xmlChar *prefix, int plen, |
| 313 | const xmlChar *name, int len) |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 314 | { |
| 315 | uint32_t hash; |
| 316 | int i; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 317 | |
| 318 | hash = 0; |
| 319 | |
| 320 | for (i = 0;i < plen; i++) { |
| 321 | hash += prefix[i]; |
| 322 | hash += (hash << 10); |
| 323 | hash ^= (hash >> 6); |
| 324 | } |
| 325 | hash += ':'; |
| 326 | hash += (hash << 10); |
| 327 | hash ^= (hash >> 6); |
| 328 | |
| 329 | for (i = 0;i < len; i++) { |
| 330 | hash += name[i]; |
| 331 | hash += (hash << 10); |
| 332 | hash ^= (hash >> 6); |
| 333 | } |
| 334 | hash += (hash << 3); |
| 335 | hash ^= (hash >> 11); |
| 336 | hash += (hash << 15); |
| 337 | |
| 338 | return hash; |
| 339 | } |
| 340 | #endif /* WITH_BIG_KEY */ |
| 341 | |
| 342 | /* |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 343 | * xmlDictComputeFastKey: |
| 344 | * |
| 345 | * Calculate a hash key using a fast hash function that works well |
| 346 | * for low hash table fill. |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 347 | */ |
| 348 | static unsigned long |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 349 | xmlDictComputeFastKey(const xmlChar *name, int namelen) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 350 | unsigned long value = 0L; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 351 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 352 | if (name == NULL) return(0); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 353 | value = *name; |
| 354 | value <<= 5; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 355 | if (namelen > 10) { |
| 356 | value += name[namelen - 1]; |
| 357 | namelen = 10; |
| 358 | } |
| 359 | switch (namelen) { |
| 360 | case 10: value += name[9]; |
| 361 | case 9: value += name[8]; |
| 362 | case 8: value += name[7]; |
| 363 | case 7: value += name[6]; |
| 364 | case 6: value += name[5]; |
| 365 | case 5: value += name[4]; |
| 366 | case 4: value += name[3]; |
| 367 | case 3: value += name[2]; |
| 368 | case 2: value += name[1]; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 369 | default: break; |
| 370 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 371 | return(value); |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 375 | * xmlDictComputeFastQKey: |
| 376 | * |
| 377 | * Calculate a hash key for two strings using a fast hash function |
| 378 | * that works well for low hash table fill. |
| 379 | * |
| 380 | * Neither of the two strings must be NULL. |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 381 | */ |
| 382 | static unsigned long |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 383 | xmlDictComputeFastQKey(const xmlChar *prefix, int plen, |
| 384 | const xmlChar *name, int len) |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 385 | { |
| 386 | unsigned long value = 0L; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 387 | |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 388 | if (plen == 0) |
| 389 | value += 30 * (unsigned long) ':'; |
| 390 | else |
| 391 | value += 30 * (*prefix); |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 392 | |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 393 | if (len > 10) { |
| 394 | value += name[len - (plen + 1 + 1)]; |
| 395 | len = 10; |
| 396 | if (plen > 10) |
| 397 | plen = 10; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 398 | } |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 399 | switch (plen) { |
| 400 | case 10: value += prefix[9]; |
| 401 | case 9: value += prefix[8]; |
| 402 | case 8: value += prefix[7]; |
| 403 | case 7: value += prefix[6]; |
| 404 | case 6: value += prefix[5]; |
| 405 | case 5: value += prefix[4]; |
| 406 | case 4: value += prefix[3]; |
| 407 | case 3: value += prefix[2]; |
| 408 | case 2: value += prefix[1]; |
| 409 | case 1: value += prefix[0]; |
| 410 | default: break; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 411 | } |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 412 | len -= plen; |
| 413 | if (len > 0) { |
| 414 | value += (unsigned long) ':'; |
| 415 | len--; |
| 416 | } |
| 417 | switch (len) { |
| 418 | case 10: value += name[9]; |
| 419 | case 9: value += name[8]; |
| 420 | case 8: value += name[7]; |
| 421 | case 7: value += name[6]; |
| 422 | case 6: value += name[5]; |
| 423 | case 5: value += name[4]; |
| 424 | case 4: value += name[3]; |
| 425 | case 3: value += name[2]; |
| 426 | case 2: value += name[1]; |
| 427 | case 1: value += name[0]; |
| 428 | default: break; |
| 429 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 430 | return(value); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * xmlDictCreate: |
| 435 | * |
| 436 | * Create a new dictionary |
| 437 | * |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 438 | * Returns the newly created dictionnary, or NULL if an error occured. |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 439 | */ |
| 440 | xmlDictPtr |
| 441 | xmlDictCreate(void) { |
| 442 | xmlDictPtr dict; |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 443 | |
| 444 | if (!xmlDictInitialized) |
| 445 | if (!xmlInitializeDict()) |
| 446 | return(NULL); |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 447 | |
| 448 | #ifdef DICT_DEBUG_PATTERNS |
| 449 | fprintf(stderr, "C"); |
| 450 | #endif |
| 451 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 452 | dict = xmlMalloc(sizeof(xmlDict)); |
| 453 | if (dict) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 454 | dict->ref_counter = 1; |
| 455 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 456 | dict->size = MIN_DICT_SIZE; |
| 457 | dict->nbElems = 0; |
| 458 | dict->dict = xmlMalloc(MIN_DICT_SIZE * sizeof(xmlDictEntry)); |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 459 | dict->strings = NULL; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 460 | dict->subdict = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 461 | if (dict->dict) { |
Daniel Veillard | b242b08 | 2008-02-08 09:56:31 +0000 | [diff] [blame] | 462 | memset(dict->dict, 0, MIN_DICT_SIZE * sizeof(xmlDictEntry)); |
| 463 | return(dict); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 464 | } |
| 465 | xmlFree(dict); |
| 466 | } |
| 467 | return(NULL); |
| 468 | } |
| 469 | |
| 470 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 471 | * xmlDictCreateSub: |
| 472 | * @sub: an existing dictionnary |
| 473 | * |
| 474 | * Create a new dictionary, inheriting strings from the read-only |
| 475 | * dictionnary @sub. On lookup, strings are first searched in the |
| 476 | * new dictionnary, then in @sub, and if not found are created in the |
| 477 | * new dictionnary. |
| 478 | * |
| 479 | * Returns the newly created dictionnary, or NULL if an error occured. |
| 480 | */ |
| 481 | xmlDictPtr |
| 482 | xmlDictCreateSub(xmlDictPtr sub) { |
| 483 | xmlDictPtr dict = xmlDictCreate(); |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 484 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 485 | if ((dict != NULL) && (sub != NULL)) { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 486 | #ifdef DICT_DEBUG_PATTERNS |
| 487 | fprintf(stderr, "R"); |
| 488 | #endif |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 489 | dict->subdict = sub; |
| 490 | xmlDictReference(dict->subdict); |
| 491 | } |
| 492 | return(dict); |
| 493 | } |
| 494 | |
| 495 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 496 | * xmlDictReference: |
| 497 | * @dict: the dictionnary |
| 498 | * |
| 499 | * Increment the reference counter of a dictionary |
| 500 | * |
| 501 | * Returns 0 in case of success and -1 in case of error |
| 502 | */ |
| 503 | int |
| 504 | xmlDictReference(xmlDictPtr dict) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 505 | if (!xmlDictInitialized) |
| 506 | if (!xmlInitializeDict()) |
| 507 | return(-1); |
| 508 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 509 | if (dict == NULL) return -1; |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 510 | xmlRMutexLock(xmlDictMutex); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 511 | dict->ref_counter++; |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 512 | xmlRMutexUnlock(xmlDictMutex); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 513 | return(0); |
| 514 | } |
| 515 | |
| 516 | /** |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 517 | * xmlDictGrow: |
| 518 | * @dict: the dictionnary |
| 519 | * @size: the new size of the dictionnary |
| 520 | * |
| 521 | * resize the dictionnary |
| 522 | * |
| 523 | * Returns 0 in case of success, -1 in case of failure |
| 524 | */ |
| 525 | static int |
| 526 | xmlDictGrow(xmlDictPtr dict, int size) { |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 527 | unsigned long key, okey; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 528 | int oldsize, i; |
| 529 | xmlDictEntryPtr iter, next; |
| 530 | struct _xmlDictEntry *olddict; |
| 531 | #ifdef DEBUG_GROW |
| 532 | unsigned long nbElem = 0; |
| 533 | #endif |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 534 | int ret = 0; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 535 | int keep_keys = 1; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 536 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 537 | if (dict == NULL) |
| 538 | return(-1); |
| 539 | if (size < 8) |
| 540 | return(-1); |
| 541 | if (size > 8 * 2048) |
| 542 | return(-1); |
| 543 | |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 544 | #ifdef DICT_DEBUG_PATTERNS |
| 545 | fprintf(stderr, "*"); |
| 546 | #endif |
| 547 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 548 | oldsize = dict->size; |
| 549 | olddict = dict->dict; |
| 550 | if (olddict == NULL) |
| 551 | return(-1); |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 552 | if (oldsize == MIN_DICT_SIZE) |
| 553 | keep_keys = 0; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 554 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 555 | dict->dict = xmlMalloc(size * sizeof(xmlDictEntry)); |
| 556 | if (dict->dict == NULL) { |
| 557 | dict->dict = olddict; |
| 558 | return(-1); |
| 559 | } |
| 560 | memset(dict->dict, 0, size * sizeof(xmlDictEntry)); |
| 561 | dict->size = size; |
| 562 | |
| 563 | /* If the two loops are merged, there would be situations where |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 564 | a new entry needs to allocated and data copied into it from |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 565 | the main dict. It is nicer to run through the array twice, first |
| 566 | copying all the elements in the main array (less probability of |
| 567 | allocate) and then the rest, so we only free in the second loop. |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 568 | */ |
| 569 | for (i = 0; i < oldsize; i++) { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 570 | if (olddict[i].valid == 0) |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 571 | continue; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 572 | |
| 573 | if (keep_keys) |
| 574 | okey = olddict[i].okey; |
| 575 | else |
| 576 | okey = xmlDictComputeKey(dict, olddict[i].name, olddict[i].len); |
| 577 | key = okey % dict->size; |
| 578 | |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 579 | if (dict->dict[key].valid == 0) { |
| 580 | memcpy(&(dict->dict[key]), &(olddict[i]), sizeof(xmlDictEntry)); |
| 581 | dict->dict[key].next = NULL; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 582 | dict->dict[key].okey = okey; |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 583 | } else { |
| 584 | xmlDictEntryPtr entry; |
| 585 | |
| 586 | entry = xmlMalloc(sizeof(xmlDictEntry)); |
| 587 | if (entry != NULL) { |
| 588 | entry->name = olddict[i].name; |
| 589 | entry->len = olddict[i].len; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 590 | entry->okey = okey; |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 591 | entry->next = dict->dict[key].next; |
| 592 | entry->valid = 1; |
| 593 | dict->dict[key].next = entry; |
| 594 | } else { |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 595 | /* |
| 596 | * we don't have much ways to alert from herei |
| 597 | * result is loosing an entry and unicity garantee |
| 598 | */ |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 599 | ret = -1; |
| 600 | } |
| 601 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 602 | #ifdef DEBUG_GROW |
| 603 | nbElem++; |
| 604 | #endif |
| 605 | } |
| 606 | |
| 607 | for (i = 0; i < oldsize; i++) { |
| 608 | iter = olddict[i].next; |
| 609 | while (iter) { |
| 610 | next = iter->next; |
| 611 | |
| 612 | /* |
| 613 | * put back the entry in the new dict |
| 614 | */ |
| 615 | |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 616 | if (keep_keys) |
| 617 | okey = iter->okey; |
| 618 | else |
| 619 | okey = xmlDictComputeKey(dict, iter->name, iter->len); |
| 620 | key = okey % dict->size; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 621 | if (dict->dict[key].valid == 0) { |
| 622 | memcpy(&(dict->dict[key]), iter, sizeof(xmlDictEntry)); |
| 623 | dict->dict[key].next = NULL; |
| 624 | dict->dict[key].valid = 1; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 625 | dict->dict[key].okey = okey; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 626 | xmlFree(iter); |
| 627 | } else { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 628 | iter->next = dict->dict[key].next; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 629 | iter->okey = okey; |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 630 | dict->dict[key].next = iter; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | #ifdef DEBUG_GROW |
| 634 | nbElem++; |
| 635 | #endif |
| 636 | |
| 637 | iter = next; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | xmlFree(olddict); |
| 642 | |
| 643 | #ifdef DEBUG_GROW |
| 644 | xmlGenericError(xmlGenericErrorContext, |
| 645 | "xmlDictGrow : from %d to %d, %d elems\n", oldsize, size, nbElem); |
| 646 | #endif |
| 647 | |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 648 | return(ret); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
| 652 | * xmlDictFree: |
| 653 | * @dict: the dictionnary |
| 654 | * |
| 655 | * Free the hash @dict and its contents. The userdata is |
| 656 | * deallocated with @f if provided. |
| 657 | */ |
| 658 | void |
| 659 | xmlDictFree(xmlDictPtr dict) { |
| 660 | int i; |
| 661 | xmlDictEntryPtr iter; |
| 662 | xmlDictEntryPtr next; |
| 663 | int inside_dict = 0; |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 664 | xmlDictStringsPtr pool, nextp; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 665 | |
| 666 | if (dict == NULL) |
| 667 | return; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 668 | |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 669 | if (!xmlDictInitialized) |
| 670 | if (!xmlInitializeDict()) |
| 671 | return; |
| 672 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 673 | /* decrement the counter, it may be shared by a parser and docs */ |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 674 | xmlRMutexLock(xmlDictMutex); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 675 | dict->ref_counter--; |
Daniel Veillard | 1bb16a1 | 2005-01-21 16:55:41 +0000 | [diff] [blame] | 676 | if (dict->ref_counter > 0) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 677 | xmlRMutexUnlock(xmlDictMutex); |
Daniel Veillard | 1bb16a1 | 2005-01-21 16:55:41 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 681 | xmlRMutexUnlock(xmlDictMutex); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 682 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 683 | if (dict->subdict != NULL) { |
| 684 | xmlDictFree(dict->subdict); |
| 685 | } |
| 686 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 687 | if (dict->dict) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 688 | for(i = 0; ((i < dict->size) && (dict->nbElems > 0)); i++) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 689 | iter = &(dict->dict[i]); |
| 690 | if (iter->valid == 0) |
| 691 | continue; |
| 692 | inside_dict = 1; |
| 693 | while (iter) { |
| 694 | next = iter->next; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 695 | if (!inside_dict) |
| 696 | xmlFree(iter); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 697 | dict->nbElems--; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 698 | inside_dict = 0; |
| 699 | iter = next; |
| 700 | } |
| 701 | inside_dict = 0; |
| 702 | } |
| 703 | xmlFree(dict->dict); |
| 704 | } |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 705 | pool = dict->strings; |
| 706 | while (pool != NULL) { |
| 707 | nextp = pool->next; |
| 708 | xmlFree(pool); |
| 709 | pool = nextp; |
| 710 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 711 | xmlFree(dict); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * xmlDictLookup: |
| 716 | * @dict: the dictionnary |
| 717 | * @name: the name of the userdata |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 718 | * @len: the length of the name, if -1 it is recomputed |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 719 | * |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 720 | * Add the @name to the dictionnary @dict if not present. |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 721 | * |
| 722 | * Returns the internal copy of the name or NULL in case of internal error |
| 723 | */ |
| 724 | const xmlChar * |
| 725 | xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 726 | unsigned long key, okey, nbi = 0; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 727 | xmlDictEntryPtr entry; |
| 728 | xmlDictEntryPtr insert; |
| 729 | const xmlChar *ret; |
| 730 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 731 | if ((dict == NULL) || (name == NULL)) |
| 732 | return(NULL); |
| 733 | |
| 734 | if (len < 0) |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 735 | len = strlen((const char *) name); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Check for duplicate and insertion location. |
| 739 | */ |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 740 | okey = xmlDictComputeKey(dict, name, len); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 741 | key = okey % dict->size; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 742 | if (dict->dict[key].valid == 0) { |
| 743 | insert = NULL; |
| 744 | } else { |
| 745 | for (insert = &(dict->dict[key]); insert->next != NULL; |
| 746 | insert = insert->next) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 747 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 748 | if ((insert->okey == okey) && (insert->len == len)) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 749 | if (!memcmp(insert->name, name, len)) |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 750 | return(insert->name); |
| 751 | } |
| 752 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 753 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 754 | (!xmlStrncmp(insert->name, name, len))) |
| 755 | return(insert->name); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 756 | #endif |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 757 | nbi++; |
| 758 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 759 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 760 | if ((insert->okey == okey) && (insert->len == len)) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 761 | if (!memcmp(insert->name, name, len)) |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 762 | return(insert->name); |
| 763 | } |
| 764 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 765 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 766 | (!xmlStrncmp(insert->name, name, len))) |
| 767 | return(insert->name); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 768 | #endif |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 771 | if (dict->subdict) { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 772 | unsigned long skey; |
| 773 | |
| 774 | /* we cannot always reuse the same okey for the subdict */ |
| 775 | if (((dict->size == MIN_DICT_SIZE) && |
| 776 | (dict->subdict->size != MIN_DICT_SIZE)) || |
| 777 | ((dict->size != MIN_DICT_SIZE) && |
| 778 | (dict->subdict->size == MIN_DICT_SIZE))) |
| 779 | skey = xmlDictComputeKey(dict->subdict, name, len); |
| 780 | else |
| 781 | skey = okey; |
| 782 | |
| 783 | key = skey % dict->subdict->size; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 784 | if (dict->subdict->dict[key].valid != 0) { |
| 785 | xmlDictEntryPtr tmp; |
| 786 | |
| 787 | for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL; |
| 788 | tmp = tmp->next) { |
| 789 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 790 | if ((tmp->okey == skey) && (tmp->len == len)) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 791 | if (!memcmp(tmp->name, name, len)) |
| 792 | return(tmp->name); |
| 793 | } |
| 794 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 795 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 796 | (!xmlStrncmp(tmp->name, name, len))) |
| 797 | return(tmp->name); |
| 798 | #endif |
| 799 | nbi++; |
| 800 | } |
| 801 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 802 | if ((tmp->okey == skey) && (tmp->len == len)) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 803 | if (!memcmp(tmp->name, name, len)) |
| 804 | return(tmp->name); |
| 805 | } |
| 806 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 807 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 808 | (!xmlStrncmp(tmp->name, name, len))) |
| 809 | return(tmp->name); |
| 810 | #endif |
| 811 | } |
| 812 | key = okey % dict->size; |
| 813 | } |
| 814 | |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 815 | ret = xmlDictAddString(dict, name, len); |
| 816 | if (ret == NULL) |
| 817 | return(NULL); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 818 | if (insert == NULL) { |
| 819 | entry = &(dict->dict[key]); |
| 820 | } else { |
| 821 | entry = xmlMalloc(sizeof(xmlDictEntry)); |
| 822 | if (entry == NULL) |
| 823 | return(NULL); |
| 824 | } |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 825 | entry->name = ret; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 826 | entry->len = len; |
| 827 | entry->next = NULL; |
| 828 | entry->valid = 1; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 829 | entry->okey = okey; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 830 | |
| 831 | |
| 832 | if (insert != NULL) |
| 833 | insert->next = entry; |
| 834 | |
| 835 | dict->nbElems++; |
| 836 | |
| 837 | if ((nbi > MAX_HASH_LEN) && |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 838 | (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN))) { |
| 839 | if (xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size) != 0) |
| 840 | return(NULL); |
| 841 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 842 | /* Note that entry may have been freed at this point by xmlDictGrow */ |
| 843 | |
| 844 | return(ret); |
| 845 | } |
| 846 | |
| 847 | /** |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 848 | * xmlDictExists: |
| 849 | * @dict: the dictionnary |
| 850 | * @name: the name of the userdata |
| 851 | * @len: the length of the name, if -1 it is recomputed |
| 852 | * |
| 853 | * Check if the @name exists in the dictionnary @dict. |
| 854 | * |
| 855 | * Returns the internal copy of the name or NULL if not found. |
| 856 | */ |
| 857 | const xmlChar * |
| 858 | xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) { |
| 859 | unsigned long key, okey, nbi = 0; |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 860 | xmlDictEntryPtr insert; |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 861 | |
| 862 | if ((dict == NULL) || (name == NULL)) |
| 863 | return(NULL); |
| 864 | |
| 865 | if (len < 0) |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 866 | len = strlen((const char *) name); |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 867 | |
| 868 | /* |
| 869 | * Check for duplicate and insertion location. |
| 870 | */ |
Daniel Veillard | e9100a5 | 2008-04-22 08:28:50 +0000 | [diff] [blame] | 871 | okey = xmlDictComputeKey(dict, name, len); |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 872 | key = okey % dict->size; |
| 873 | if (dict->dict[key].valid == 0) { |
| 874 | insert = NULL; |
| 875 | } else { |
| 876 | for (insert = &(dict->dict[key]); insert->next != NULL; |
| 877 | insert = insert->next) { |
| 878 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 879 | if ((insert->okey == okey) && (insert->len == len)) { |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 880 | if (!memcmp(insert->name, name, len)) |
| 881 | return(insert->name); |
| 882 | } |
| 883 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 884 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 885 | (!xmlStrncmp(insert->name, name, len))) |
| 886 | return(insert->name); |
| 887 | #endif |
| 888 | nbi++; |
| 889 | } |
| 890 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 891 | if ((insert->okey == okey) && (insert->len == len)) { |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 892 | if (!memcmp(insert->name, name, len)) |
| 893 | return(insert->name); |
| 894 | } |
| 895 | #else |
Rob Richards | 117baa0 | 2008-08-10 17:07:33 +0000 | [diff] [blame] | 896 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 897 | (!xmlStrncmp(insert->name, name, len))) |
| 898 | return(insert->name); |
| 899 | #endif |
| 900 | } |
| 901 | |
| 902 | if (dict->subdict) { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 903 | unsigned long skey; |
| 904 | |
| 905 | /* we cannot always reuse the same okey for the subdict */ |
| 906 | if (((dict->size == MIN_DICT_SIZE) && |
| 907 | (dict->subdict->size != MIN_DICT_SIZE)) || |
| 908 | ((dict->size != MIN_DICT_SIZE) && |
| 909 | (dict->subdict->size == MIN_DICT_SIZE))) |
| 910 | skey = xmlDictComputeKey(dict->subdict, name, len); |
| 911 | else |
| 912 | skey = okey; |
| 913 | |
| 914 | key = skey % dict->subdict->size; |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 915 | if (dict->subdict->dict[key].valid != 0) { |
| 916 | xmlDictEntryPtr tmp; |
| 917 | |
| 918 | for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL; |
| 919 | tmp = tmp->next) { |
| 920 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 921 | if ((tmp->okey == skey) && (tmp->len == len)) { |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 922 | if (!memcmp(tmp->name, name, len)) |
| 923 | return(tmp->name); |
| 924 | } |
| 925 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 926 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 927 | (!xmlStrncmp(tmp->name, name, len))) |
| 928 | return(tmp->name); |
| 929 | #endif |
| 930 | nbi++; |
| 931 | } |
| 932 | #ifdef __GNUC__ |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 933 | if ((tmp->okey == skey) && (tmp->len == len)) { |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 934 | if (!memcmp(tmp->name, name, len)) |
| 935 | return(tmp->name); |
| 936 | } |
| 937 | #else |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 938 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 939 | (!xmlStrncmp(tmp->name, name, len))) |
| 940 | return(tmp->name); |
| 941 | #endif |
| 942 | } |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* not found */ |
| 946 | return(NULL); |
| 947 | } |
| 948 | |
| 949 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 950 | * xmlDictQLookup: |
| 951 | * @dict: the dictionnary |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 952 | * @prefix: the prefix |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 953 | * @name: the name |
| 954 | * |
| 955 | * Add the QName @prefix:@name to the hash @dict if not present. |
| 956 | * |
| 957 | * Returns the internal copy of the QName or NULL in case of internal error |
| 958 | */ |
| 959 | const xmlChar * |
| 960 | xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 961 | unsigned long okey, key, nbi = 0; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 962 | xmlDictEntryPtr entry; |
| 963 | xmlDictEntryPtr insert; |
| 964 | const xmlChar *ret; |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 965 | int len, plen, l; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 966 | |
| 967 | if ((dict == NULL) || (name == NULL)) |
| 968 | return(NULL); |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 969 | if (prefix == NULL) |
| 970 | return(xmlDictLookup(dict, name, -1)); |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 971 | |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 972 | l = len = strlen((const char *) name); |
| 973 | plen = strlen((const char *) prefix); |
| 974 | len += 1 + plen; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 975 | |
| 976 | /* |
| 977 | * Check for duplicate and insertion location. |
| 978 | */ |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 979 | okey = xmlDictComputeQKey(dict, prefix, plen, name, l); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 980 | key = okey % dict->size; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 981 | if (dict->dict[key].valid == 0) { |
| 982 | insert = NULL; |
| 983 | } else { |
| 984 | for (insert = &(dict->dict[key]); insert->next != NULL; |
| 985 | insert = insert->next) { |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 986 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 987 | (xmlStrQEqual(prefix, name, insert->name))) |
| 988 | return(insert->name); |
| 989 | nbi++; |
| 990 | } |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 991 | if ((insert->okey == okey) && (insert->len == len) && |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 992 | (xmlStrQEqual(prefix, name, insert->name))) |
| 993 | return(insert->name); |
| 994 | } |
| 995 | |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 996 | if (dict->subdict) { |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 997 | unsigned long skey; |
| 998 | |
| 999 | /* we cannot always reuse the same okey for the subdict */ |
| 1000 | if (((dict->size == MIN_DICT_SIZE) && |
| 1001 | (dict->subdict->size != MIN_DICT_SIZE)) || |
| 1002 | ((dict->size != MIN_DICT_SIZE) && |
| 1003 | (dict->subdict->size == MIN_DICT_SIZE))) |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 1004 | skey = xmlDictComputeQKey(dict->subdict, prefix, plen, name, l); |
Daniel Veillard | 424785e | 2008-08-06 09:35:25 +0000 | [diff] [blame] | 1005 | else |
| 1006 | skey = okey; |
| 1007 | |
| 1008 | key = skey % dict->subdict->size; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1009 | if (dict->subdict->dict[key].valid != 0) { |
| 1010 | xmlDictEntryPtr tmp; |
| 1011 | for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL; |
| 1012 | tmp = tmp->next) { |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 1013 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1014 | (xmlStrQEqual(prefix, name, tmp->name))) |
| 1015 | return(tmp->name); |
| 1016 | nbi++; |
| 1017 | } |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 1018 | if ((tmp->okey == skey) && (tmp->len == len) && |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1019 | (xmlStrQEqual(prefix, name, tmp->name))) |
| 1020 | return(tmp->name); |
| 1021 | } |
| 1022 | key = okey % dict->size; |
| 1023 | } |
| 1024 | |
Daniel Veillard | ffda65f | 2008-08-07 16:33:49 +0000 | [diff] [blame] | 1025 | ret = xmlDictAddQString(dict, prefix, plen, name, l); |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 1026 | if (ret == NULL) |
| 1027 | return(NULL); |
| 1028 | if (insert == NULL) { |
| 1029 | entry = &(dict->dict[key]); |
| 1030 | } else { |
| 1031 | entry = xmlMalloc(sizeof(xmlDictEntry)); |
| 1032 | if (entry == NULL) |
| 1033 | return(NULL); |
| 1034 | } |
| 1035 | entry->name = ret; |
| 1036 | entry->len = len; |
| 1037 | entry->next = NULL; |
| 1038 | entry->valid = 1; |
Daniel Veillard | d68f891 | 2008-08-08 10:09:19 +0000 | [diff] [blame] | 1039 | entry->okey = okey; |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (insert != NULL) |
| 1042 | insert->next = entry; |
| 1043 | |
| 1044 | dict->nbElems++; |
| 1045 | |
| 1046 | if ((nbi > MAX_HASH_LEN) && |
| 1047 | (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN))) |
| 1048 | xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size); |
| 1049 | /* Note that entry may have been freed at this point by xmlDictGrow */ |
| 1050 | |
| 1051 | return(ret); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 1055 | * xmlDictOwns: |
| 1056 | * @dict: the dictionnary |
| 1057 | * @str: the string |
| 1058 | * |
| 1059 | * check if a string is owned by the disctionary |
| 1060 | * |
| 1061 | * Returns 1 if true, 0 if false and -1 in case of error |
| 1062 | * -1 in case of error |
| 1063 | */ |
| 1064 | int |
| 1065 | xmlDictOwns(xmlDictPtr dict, const xmlChar *str) { |
| 1066 | xmlDictStringsPtr pool; |
| 1067 | |
| 1068 | if ((dict == NULL) || (str == NULL)) |
| 1069 | return(-1); |
| 1070 | pool = dict->strings; |
| 1071 | while (pool != NULL) { |
William M. Brack | bf5cf21 | 2004-08-31 06:47:17 +0000 | [diff] [blame] | 1072 | if ((str >= &pool->array[0]) && (str <= pool->free)) |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 1073 | return(1); |
| 1074 | pool = pool->next; |
| 1075 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1076 | if (dict->subdict) |
| 1077 | return(xmlDictOwns(dict->subdict, str)); |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 1078 | return(0); |
| 1079 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1080 | |
Daniel Veillard | 81514ba | 2003-09-16 23:17:26 +0000 | [diff] [blame] | 1081 | /** |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1082 | * xmlDictSize: |
| 1083 | * @dict: the dictionnary |
| 1084 | * |
| 1085 | * Query the number of elements installed in the hash @dict. |
| 1086 | * |
| 1087 | * Returns the number of elements in the dictionnary or |
| 1088 | * -1 in case of error |
| 1089 | */ |
| 1090 | int |
| 1091 | xmlDictSize(xmlDictPtr dict) { |
| 1092 | if (dict == NULL) |
| 1093 | return(-1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 1094 | if (dict->subdict) |
| 1095 | return(dict->nbElems + dict->subdict->nbElems); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1096 | return(dict->nbElems); |
| 1097 | } |
| 1098 | |
| 1099 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1100 | #define bottom_dict |
| 1101 | #include "elfgcchack.h" |