dict.c 31 KB

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