dict.c 30 KB

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