dict.c 29.7 KB
Newer Older
1 2 3 4
/*
 * dict.c: dictionary of reusable strings, just used to avoid allocation
 *         and freeing operations.
 *
5
 * Copyright (C) 2003-2012 Daniel Veillard.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
 *
 * Author: daniel@veillard.com
 */

#define IN_LIBXML
#include "libxml.h"

22
#include <limits.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif

/*
 * Following http://www.ocert.org/advisories/ocert-2011-003.html
 * it seems that having hash randomization might be a good idea
 * when using XML with untrusted data
 * Note1: that it works correctly only if compiled with WITH_BIG_KEY
 *  which is the default.
 * Note2: the fast function used for a small dict won't protect very
 *  well but since the attack is based on growing a very big hash
 *  list we will use the BigKey algo as soon as the hash size grows
 *  over MIN_DICT_SIZE so this actually works
 */
#if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME)
#define DICT_RANDOMIZATION
#endif

45
#include <string.h>
46 47
#ifdef HAVE_STDINT_H
#include <stdint.h>
48 49 50
#else
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
51 52 53
#elif defined(WIN32)
typedef unsigned __int32 uint32_t;
#endif
54
#endif
55 56 57 58 59 60
#include <libxml/tree.h>
#include <libxml/dict.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlerror.h>
#include <libxml/globals.h>

61 62 63 64
/* #define DEBUG_GROW */
/* #define DICT_DEBUG_PATTERNS */

#define MAX_HASH_LEN 3
65 66
#define MIN_DICT_SIZE 128
#define MAX_DICT_HASH 8 * 2048
67 68 69
#define WITH_BIG_KEY

#ifdef WITH_BIG_KEY
70 71 72 73 74 75 76 77 78 79 80
#define xmlDictComputeKey(dict, name, len)                              \
    (((dict)->size == MIN_DICT_SIZE) ?                                  \
     xmlDictComputeFastKey(name, len, (dict)->seed) :                   \
     xmlDictComputeBigKey(name, len, (dict)->seed))

#define xmlDictComputeQKey(dict, prefix, plen, name, len)               \
    (((prefix) == NULL) ?                                               \
      (xmlDictComputeKey(dict, name, len)) :                             \
      (((dict)->size == MIN_DICT_SIZE) ?                                \
       xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed) :	\
       xmlDictComputeBigQKey(prefix, plen, name, len, (dict)->seed)))
81 82

#else /* !WITH_BIG_KEY */
83 84 85 86
#define xmlDictComputeKey(dict, name, len)                              \
        xmlDictComputeFastKey(name, len, (dict)->seed)
#define xmlDictComputeQKey(dict, prefix, plen, name, len)               \
        xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed)
87
#endif /* WITH_BIG_KEY */
88 89

/*
90
 * An entry in the dictionary
91 92 93 94 95 96
 */
typedef struct _xmlDictEntry xmlDictEntry;
typedef xmlDictEntry *xmlDictEntryPtr;
struct _xmlDictEntry {
    struct _xmlDictEntry *next;
    const xmlChar *name;
97
    unsigned int len;
98
    int valid;
99
    unsigned long okey;
100 101 102 103 104 105 106 107
};

typedef struct _xmlDictStrings xmlDictStrings;
typedef xmlDictStrings *xmlDictStringsPtr;
struct _xmlDictStrings {
    xmlDictStringsPtr next;
    xmlChar *free;
    xmlChar *end;
108 109
    size_t size;
    size_t nbStrings;
110 111 112
    xmlChar array[1];
};
/*
113
 * The entire dictionary
114 115 116 117 118
 */
struct _xmlDict {
    int ref_counter;

    struct _xmlDictEntry *dict;
119 120
    size_t size;
    unsigned int nbElems;
121 122 123
    xmlDictStringsPtr strings;

    struct _xmlDict *subdict;
124 125
    /* used for randomization */
    int seed;
126 127
    /* used to impose a limit on size */
    size_t limit;
128 129
};

130 131 132 133 134 135 136 137 138 139 140
/*
 * A mutex for modifying the reference counter for shared
 * dictionaries.
 */
static xmlRMutexPtr xmlDictMutex = NULL;

/*
 * Whether the dictionary mutex was initialized.
 */
static int xmlDictInitialized = 0;

141 142 143 144 145
#ifdef DICT_RANDOMIZATION
#ifdef HAVE_RAND_R
/*
 * Internal data for random function, protected by xmlDictMutex
 */
146
static unsigned int rand_seed = 0;
147 148 149
#endif
#endif

150 151 152 153
/**
 * xmlInitializeDict:
 *
 * Do the dictionary mutex initialization.
154
 * this function is deprecated
155 156 157 158
 *
 * Returns 0 if initialization was already done, and 1 if that
 * call led to the initialization
 */
159
int xmlInitializeDict(void) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    return(0);
}

/**
 * __xmlInitializeDict:
 *
 * This function is not public
 * Do the dictionary mutex initialization.
 * this function is not thread safe, initialization should
 * normally be done once at setup when called from xmlOnceInit()
 * we may also land in this code if thread support is not compiled in
 *
 * Returns 0 if initialization was already done, and 1 if that
 * call led to the initialization
 */
int __xmlInitializeDict(void) {
176 177 178 179 180
    if (xmlDictInitialized)
        return(1);

    if ((xmlDictMutex = xmlNewRMutex()) == NULL)
        return(0);
181
    xmlRMutexLock(xmlDictMutex);
182

183 184 185 186 187 188 189 190
#ifdef DICT_RANDOMIZATION
#ifdef HAVE_RAND_R
    rand_seed = time(NULL);
    rand_r(& rand_seed);
#else
    srand(time(NULL));
#endif
#endif
191
    xmlDictInitialized = 1;
192
    xmlRMutexUnlock(xmlDictMutex);
193 194 195
    return(1);
}

196 197 198 199 200
#ifdef DICT_RANDOMIZATION
int __xmlRandom(void) {
    int ret;

    if (xmlDictInitialized == 0)
201
        __xmlInitializeDict();
202 203 204 205 206 207 208 209 210 211 212 213

    xmlRMutexLock(xmlDictMutex);
#ifdef HAVE_RAND_R
    ret = rand_r(& rand_seed);
#else
    ret = rand();
#endif
    xmlRMutexUnlock(xmlDictMutex);
    return(ret);
}
#endif

214 215 216
/**
 * xmlDictCleanup:
 *
217 218
 * Free the dictionary mutex. Do not call unless sure the library
 * is not in use anymore !
219 220 221 222 223 224 225 226 227 228 229
 */
void
xmlDictCleanup(void) {
    if (!xmlDictInitialized)
        return;

    xmlFreeRMutex(xmlDictMutex);

    xmlDictInitialized = 0;
}

230 231
/*
 * xmlDictAddString:
232
 * @dict: the dictionary
233
 * @name: the name of the userdata
234
 * @len: the length of the name
235 236 237 238 239 240
 *
 * Add the string to the array[s]
 *
 * Returns the pointer of the local string, or NULL in case of error.
 */
static const xmlChar *
241
xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) {
242 243
    xmlDictStringsPtr pool;
    const xmlChar *ret;
244 245
    size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
    size_t limit = 0;
246

247 248 249
#ifdef DICT_DEBUG_PATTERNS
    fprintf(stderr, "-");
#endif
250 251 252 253 254
    pool = dict->strings;
    while (pool != NULL) {
	if (pool->end - pool->free > namelen)
	    goto found_pool;
	if (pool->size > size) size = pool->size;
255
        limit += pool->size;
256 257 258 259 260 261
	pool = pool->next;
    }
    /*
     * Not found, need to allocate
     */
    if (pool == NULL) {
262 263 264 265
        if ((dict->limit > 0) && (limit > dict->limit)) {
            return(NULL);
        }

266 267
        if (size == 0) size = 1000;
	else size *= 4; /* exponential growth */
268
        if (size < 4 * namelen)
269 270 271 272 273 274 275 276 277 278
	    size = 4 * namelen; /* just in case ! */
	pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size);
	if (pool == NULL)
	    return(NULL);
	pool->size = size;
	pool->nbStrings = 0;
	pool->free = &pool->array[0];
	pool->end = &pool->array[size];
	pool->next = dict->strings;
	dict->strings = pool;
279 280 281
#ifdef DICT_DEBUG_PATTERNS
        fprintf(stderr, "+");
#endif
282 283 284 285 286 287
    }
found_pool:
    ret = pool->free;
    memcpy(pool->free, name, namelen);
    pool->free += namelen;
    *(pool->free++) = 0;
288
    pool->nbStrings++;
289 290 291 292 293
    return(ret);
}

/*
 * xmlDictAddQString:
294
 * @dict: the dictionary
295
 * @prefix: the prefix of the userdata
296
 * @plen: the prefix length
297
 * @name: the name of the userdata
298
 * @len: the length of the name
299 300 301 302 303 304
 *
 * Add the QName to the array[s]
 *
 * Returns the pointer of the local string, or NULL in case of error.
 */
static const xmlChar *
305 306
xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, unsigned int plen,
                 const xmlChar *name, unsigned int namelen)
307 308 309
{
    xmlDictStringsPtr pool;
    const xmlChar *ret;
310 311
    size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
    size_t limit = 0;
312 313 314

    if (prefix == NULL) return(xmlDictAddString(dict, name, namelen));

315 316 317
#ifdef DICT_DEBUG_PATTERNS
    fprintf(stderr, "=");
#endif
318 319
    pool = dict->strings;
    while (pool != NULL) {
320
	if (pool->end - pool->free > namelen + plen + 1)
321 322
	    goto found_pool;
	if (pool->size > size) size = pool->size;
323
        limit += pool->size;
324 325 326 327 328 329
	pool = pool->next;
    }
    /*
     * Not found, need to allocate
     */
    if (pool == NULL) {
330 331 332 333
        if ((dict->limit > 0) && (limit > dict->limit)) {
            return(NULL);
        }

334 335
        if (size == 0) size = 1000;
	else size *= 4; /* exponential growth */
336 337
        if (size < 4 * (namelen + plen + 1))
	    size = 4 * (namelen + plen + 1); /* just in case ! */
338 339 340 341 342 343 344 345 346
	pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size);
	if (pool == NULL)
	    return(NULL);
	pool->size = size;
	pool->nbStrings = 0;
	pool->free = &pool->array[0];
	pool->end = &pool->array[size];
	pool->next = dict->strings;
	dict->strings = pool;
347 348 349
#ifdef DICT_DEBUG_PATTERNS
        fprintf(stderr, "+");
#endif
350 351 352 353 354 355 356 357 358
    }
found_pool:
    ret = pool->free;
    memcpy(pool->free, prefix, plen);
    pool->free += plen;
    *(pool->free++) = ':';
    memcpy(pool->free, name, namelen);
    pool->free += namelen;
    *(pool->free++) = 0;
359
    pool->nbStrings++;
360 361 362
    return(ret);
}

363 364 365 366 367 368 369 370 371 372 373 374
#ifdef WITH_BIG_KEY
/*
 * xmlDictComputeBigKey:
 *
 * Calculate a hash key using a good hash function that works well for
 * larger hash table sizes.
 *
 * Hash function by "One-at-a-Time Hash" see
 * http://burtleburtle.net/bob/hash/doobs.html
 */

static uint32_t
375
xmlDictComputeBigKey(const xmlChar* data, int namelen, int seed) {
376 377 378 379 380
    uint32_t hash;
    int i;

    if (namelen <= 0 || data == NULL) return(0);

381
    hash = seed;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    for (i = 0;i < namelen; i++) {
        hash += data[i];
	hash += (hash << 10);
	hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);

    return hash;
}

/*
 * xmlDictComputeBigQKey:
 *
 * Calculate a hash key for two strings using a good hash function
 * that works well for larger hash table sizes.
 *
 * Hash function by "One-at-a-Time Hash" see
 * http://burtleburtle.net/bob/hash/doobs.html
 *
 * Neither of the two strings must be NULL.
 */
static unsigned long
xmlDictComputeBigQKey(const xmlChar *prefix, int plen,
408
                      const xmlChar *name, int len, int seed)
409 410 411 412
{
    uint32_t hash;
    int i;

413
    hash = seed;
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

    for (i = 0;i < plen; i++) {
        hash += prefix[i];
	hash += (hash << 10);
	hash ^= (hash >> 6);
    }
    hash += ':';
    hash += (hash << 10);
    hash ^= (hash >> 6);

    for (i = 0;i < len; i++) {
        hash += name[i];
	hash += (hash << 10);
	hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);

    return hash;
}
#endif /* WITH_BIG_KEY */

437
/*
438 439 440 441
 * xmlDictComputeFastKey:
 *
 * Calculate a hash key using a fast hash function that works well
 * for low hash table fill.
442 443
 */
static unsigned long
444 445
xmlDictComputeFastKey(const xmlChar *name, int namelen, int seed) {
    unsigned long value = seed;
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    if (name == NULL) return(0);
    value = *name;
    value <<= 5;
    if (namelen > 10) {
        value += name[namelen - 1];
        namelen = 10;
    }
    switch (namelen) {
        case 10: value += name[9];
        case 9: value += name[8];
        case 8: value += name[7];
        case 7: value += name[6];
        case 6: value += name[5];
        case 5: value += name[4];
        case 4: value += name[3];
        case 3: value += name[2];
        case 2: value += name[1];
        default: break;
    }
    return(value);
}

/*
470 471 472 473 474 475
 * xmlDictComputeFastQKey:
 *
 * Calculate a hash key for two strings using a fast hash function
 * that works well for low hash table fill.
 *
 * Neither of the two strings must be NULL.
476 477
 */
static unsigned long
478
xmlDictComputeFastQKey(const xmlChar *prefix, int plen,
479
                       const xmlChar *name, int len, int seed)
480
{
481
    unsigned long value = (unsigned long) seed;
482 483 484 485 486

    if (plen == 0)
	value += 30 * (unsigned long) ':';
    else
	value += 30 * (*prefix);
487

488
    if (len > 10) {
489 490 491 492
        int offset = len - (plen + 1 + 1);
	if (offset < 0)
	    offset = len - (10 + 1);
	value += name[offset];
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        len = 10;
	if (plen > 10)
	    plen = 10;
    }
    switch (plen) {
        case 10: value += prefix[9];
        case 9: value += prefix[8];
        case 8: value += prefix[7];
        case 7: value += prefix[6];
        case 6: value += prefix[5];
        case 5: value += prefix[4];
        case 4: value += prefix[3];
        case 3: value += prefix[2];
        case 2: value += prefix[1];
        case 1: value += prefix[0];
        default: break;
    }
    len -= plen;
    if (len > 0) {
        value += (unsigned long) ':';
	len--;
    }
    switch (len) {
        case 10: value += name[9];
        case 9: value += name[8];
        case 8: value += name[7];
        case 7: value += name[6];
        case 6: value += name[5];
        case 5: value += name[4];
        case 4: value += name[3];
        case 3: value += name[2];
        case 2: value += name[1];
        case 1: value += name[0];
        default: break;
    }
    return(value);
}

/**
 * xmlDictCreate:
 *
 * Create a new dictionary
 *
536
 * Returns the newly created dictionary, or NULL if an error occured.
537 538 539 540
 */
xmlDictPtr
xmlDictCreate(void) {
    xmlDictPtr dict;
541 542

    if (!xmlDictInitialized)
543
        if (!__xmlInitializeDict())
544
            return(NULL);
545 546 547 548 549

#ifdef DICT_DEBUG_PATTERNS
    fprintf(stderr, "C");
#endif

550 551 552
    dict = xmlMalloc(sizeof(xmlDict));
    if (dict) {
        dict->ref_counter = 1;
553
        dict->limit = 0;
554 555 556 557 558 559 560

        dict->size = MIN_DICT_SIZE;
	dict->nbElems = 0;
        dict->dict = xmlMalloc(MIN_DICT_SIZE * sizeof(xmlDictEntry));
	dict->strings = NULL;
	dict->subdict = NULL;
        if (dict->dict) {
561
	    memset(dict->dict, 0, MIN_DICT_SIZE * sizeof(xmlDictEntry));
562 563 564 565 566
#ifdef DICT_RANDOMIZATION
            dict->seed = __xmlRandom();
#else
            dict->seed = 0;
#endif
567
	    return(dict);
568 569 570 571 572 573 574 575
        }
        xmlFree(dict);
    }
    return(NULL);
}

/**
 * xmlDictCreateSub:
576
 * @sub: an existing dictionary
577 578
 *
 * Create a new dictionary, inheriting strings from the read-only
579 580 581
 * dictionary @sub. On lookup, strings are first searched in the
 * new dictionary, then in @sub, and if not found are created in the
 * new dictionary.
582
 *
583
 * Returns the newly created dictionary, or NULL if an error occured.
584 585 586 587
 */
xmlDictPtr
xmlDictCreateSub(xmlDictPtr sub) {
    xmlDictPtr dict = xmlDictCreate();
588

589
    if ((dict != NULL) && (sub != NULL)) {
590 591 592
#ifdef DICT_DEBUG_PATTERNS
        fprintf(stderr, "R");
#endif
593
        dict->seed = sub->seed;
594 595 596 597 598 599 600 601
        dict->subdict = sub;
	xmlDictReference(dict->subdict);
    }
    return(dict);
}

/**
 * xmlDictReference:
602
 * @dict: the dictionary
603 604 605 606 607 608 609
 *
 * Increment the reference counter of a dictionary
 *
 * Returns 0 in case of success and -1 in case of error
 */
int
xmlDictReference(xmlDictPtr dict) {
610
    if (!xmlDictInitialized)
611
        if (!__xmlInitializeDict())
612 613
            return(-1);

614
    if (dict == NULL) return -1;
615
    xmlRMutexLock(xmlDictMutex);
616
    dict->ref_counter++;
617
    xmlRMutexUnlock(xmlDictMutex);
618 619 620 621 622
    return(0);
}

/**
 * xmlDictGrow:
623 624
 * @dict: the dictionary
 * @size: the new size of the dictionary
625
 *
626
 * resize the dictionary
627 628 629 630
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
631
xmlDictGrow(xmlDictPtr dict, size_t size) {
632
    unsigned long key, okey;
633
    size_t oldsize, i;
634 635 636 637 638
    xmlDictEntryPtr iter, next;
    struct _xmlDictEntry *olddict;
#ifdef DEBUG_GROW
    unsigned long nbElem = 0;
#endif
639 640 641
    int ret = 0;
    int keep_keys = 1;

642 643 644 645 646 647 648
    if (dict == NULL)
	return(-1);
    if (size < 8)
        return(-1);
    if (size > 8 * 2048)
	return(-1);

649 650 651 652
#ifdef DICT_DEBUG_PATTERNS
    fprintf(stderr, "*");
#endif

653 654 655 656
    oldsize = dict->size;
    olddict = dict->dict;
    if (olddict == NULL)
        return(-1);
657 658 659
    if (oldsize == MIN_DICT_SIZE)
        keep_keys = 0;

660 661 662 663 664 665 666 667 668
    dict->dict = xmlMalloc(size * sizeof(xmlDictEntry));
    if (dict->dict == NULL) {
	dict->dict = olddict;
	return(-1);
    }
    memset(dict->dict, 0, size * sizeof(xmlDictEntry));
    dict->size = size;

    /*	If the two loops are merged, there would be situations where
669 670 671 672
	a new entry needs to allocated and data copied into it from
	the main dict. It is nicer to run through the array twice, first
	copying all the elements in the main array (less probability of
	allocate) and then the rest, so we only free in the second loop.
673 674
    */
    for (i = 0; i < oldsize; i++) {
675
	if (olddict[i].valid == 0)
676
	    continue;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706

	if (keep_keys)
	    okey = olddict[i].okey;
	else
	    okey = xmlDictComputeKey(dict, olddict[i].name, olddict[i].len);
	key = okey % dict->size;

	if (dict->dict[key].valid == 0) {
	    memcpy(&(dict->dict[key]), &(olddict[i]), sizeof(xmlDictEntry));
	    dict->dict[key].next = NULL;
	    dict->dict[key].okey = okey;
	} else {
	    xmlDictEntryPtr entry;

	    entry = xmlMalloc(sizeof(xmlDictEntry));
	    if (entry != NULL) {
		entry->name = olddict[i].name;
		entry->len = olddict[i].len;
		entry->okey = okey;
		entry->next = dict->dict[key].next;
		entry->valid = 1;
		dict->dict[key].next = entry;
	    } else {
	        /*
		 * we don't have much ways to alert from herei
		 * result is loosing an entry and unicity garantee
		 */
	        ret = -1;
	    }
	}
707 708 709 710 711 712 713 714 715 716 717 718 719 720
#ifdef DEBUG_GROW
	nbElem++;
#endif
    }

    for (i = 0; i < oldsize; i++) {
	iter = olddict[i].next;
	while (iter) {
	    next = iter->next;

	    /*
	     * put back the entry in the new dict
	     */

721 722 723 724 725
	    if (keep_keys)
		okey = iter->okey;
	    else
		okey = xmlDictComputeKey(dict, iter->name, iter->len);
	    key = okey % dict->size;
726 727 728 729
	    if (dict->dict[key].valid == 0) {
		memcpy(&(dict->dict[key]), iter, sizeof(xmlDictEntry));
		dict->dict[key].next = NULL;
		dict->dict[key].valid = 1;
730
		dict->dict[key].okey = okey;
731 732
		xmlFree(iter);
	    } else {
733 734 735
		iter->next = dict->dict[key].next;
		iter->okey = okey;
		dict->dict[key].next = iter;
736 737 738 739 740 741 742 743 744 745 746 747 748 749
	    }

#ifdef DEBUG_GROW
	    nbElem++;
#endif

	    iter = next;
	}
    }

    xmlFree(olddict);

#ifdef DEBUG_GROW
    xmlGenericError(xmlGenericErrorContext,
750
	    "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize, size, nbElem);
751 752
#endif

753
    return(ret);
754 755 756 757
}

/**
 * xmlDictFree:
758
 * @dict: the dictionary
759 760 761 762 763 764
 *
 * Free the hash @dict and its contents. The userdata is
 * deallocated with @f if provided.
 */
void
xmlDictFree(xmlDictPtr dict) {
765
    size_t i;
766 767 768 769 770 771 772 773
    xmlDictEntryPtr iter;
    xmlDictEntryPtr next;
    int inside_dict = 0;
    xmlDictStringsPtr pool, nextp;

    if (dict == NULL)
	return;

774
    if (!xmlDictInitialized)
775
        if (!__xmlInitializeDict())
776 777
            return;

778
    /* decrement the counter, it may be shared by a parser and docs */
779
    xmlRMutexLock(xmlDictMutex);
780
    dict->ref_counter--;
781 782 783 784 785 786
    if (dict->ref_counter > 0) {
        xmlRMutexUnlock(xmlDictMutex);
        return;
    }

    xmlRMutexUnlock(xmlDictMutex);
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

    if (dict->subdict != NULL) {
        xmlDictFree(dict->subdict);
    }

    if (dict->dict) {
	for(i = 0; ((i < dict->size) && (dict->nbElems > 0)); i++) {
	    iter = &(dict->dict[i]);
	    if (iter->valid == 0)
		continue;
	    inside_dict = 1;
	    while (iter) {
		next = iter->next;
		if (!inside_dict)
		    xmlFree(iter);
		dict->nbElems--;
		inside_dict = 0;
		iter = next;
	    }
	}
	xmlFree(dict->dict);
    }
    pool = dict->strings;
    while (pool != NULL) {
        nextp = pool->next;
	xmlFree(pool);
	pool = nextp;
    }
    xmlFree(dict);
}

/**
 * xmlDictLookup:
820
 * @dict: the dictionary
821 822 823
 * @name: the name of the userdata
 * @len: the length of the name, if -1 it is recomputed
 *
824
 * Add the @name to the dictionary @dict if not present.
825 826 827 828 829 830 831 832 833
 *
 * Returns the internal copy of the name or NULL in case of internal error
 */
const xmlChar *
xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
    unsigned long key, okey, nbi = 0;
    xmlDictEntryPtr entry;
    xmlDictEntryPtr insert;
    const xmlChar *ret;
834
    unsigned int l;
835 836 837 838 839

    if ((dict == NULL) || (name == NULL))
	return(NULL);

    if (len < 0)
840 841 842 843 844 845 846
        l = strlen((const char *) name);
    else
        l = len;

    if (((dict->limit > 0) && (l >= dict->limit)) ||
        (l > INT_MAX / 2))
        return(NULL);
847 848 849 850

    /*
     * Check for duplicate and insertion location.
     */
851
    okey = xmlDictComputeKey(dict, name, l);
852 853 854 855 856 857 858
    key = okey % dict->size;
    if (dict->dict[key].valid == 0) {
	insert = NULL;
    } else {
	for (insert = &(dict->dict[key]); insert->next != NULL;
	     insert = insert->next) {
#ifdef __GNUC__
859 860
	    if ((insert->okey == okey) && (insert->len == l)) {
		if (!memcmp(insert->name, name, l))
861 862 863
		    return(insert->name);
	    }
#else
864 865
	    if ((insert->okey == okey) && (insert->len == l) &&
	        (!xmlStrncmp(insert->name, name, l)))
866 867 868 869 870
		return(insert->name);
#endif
	    nbi++;
	}
#ifdef __GNUC__
871 872
	if ((insert->okey == okey) && (insert->len == l)) {
	    if (!memcmp(insert->name, name, l))
873 874 875
		return(insert->name);
	}
#else
876 877
	if ((insert->okey == okey) && (insert->len == l) &&
	    (!xmlStrncmp(insert->name, name, l)))
878 879 880 881 882
	    return(insert->name);
#endif
    }

    if (dict->subdict) {
883 884 885 886 887 888 889
        unsigned long skey;

        /* we cannot always reuse the same okey for the subdict */
        if (((dict->size == MIN_DICT_SIZE) &&
	     (dict->subdict->size != MIN_DICT_SIZE)) ||
            ((dict->size != MIN_DICT_SIZE) &&
	     (dict->subdict->size == MIN_DICT_SIZE)))
890
	    skey = xmlDictComputeKey(dict->subdict, name, l);
891 892 893 894
	else
	    skey = okey;

	key = skey % dict->subdict->size;
895 896 897 898 899 900
	if (dict->subdict->dict[key].valid != 0) {
	    xmlDictEntryPtr tmp;

	    for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL;
		 tmp = tmp->next) {
#ifdef __GNUC__
901 902
		if ((tmp->okey == skey) && (tmp->len == l)) {
		    if (!memcmp(tmp->name, name, l))
903 904 905
			return(tmp->name);
		}
#else
906 907
		if ((tmp->okey == skey) && (tmp->len == l) &&
		    (!xmlStrncmp(tmp->name, name, l)))
908 909 910 911 912
		    return(tmp->name);
#endif
		nbi++;
	    }
#ifdef __GNUC__
913 914
	    if ((tmp->okey == skey) && (tmp->len == l)) {
		if (!memcmp(tmp->name, name, l))
915 916 917
		    return(tmp->name);
	    }
#else
918 919
	    if ((tmp->okey == skey) && (tmp->len == l) &&
		(!xmlStrncmp(tmp->name, name, l)))
920 921 922 923 924 925
		return(tmp->name);
#endif
	}
	key = okey % dict->size;
    }

926
    ret = xmlDictAddString(dict, name, l);
927 928 929 930 931 932 933 934 935 936
    if (ret == NULL)
        return(NULL);
    if (insert == NULL) {
	entry = &(dict->dict[key]);
    } else {
	entry = xmlMalloc(sizeof(xmlDictEntry));
	if (entry == NULL)
	     return(NULL);
    }
    entry->name = ret;
937
    entry->len = l;
938 939
    entry->next = NULL;
    entry->valid = 1;
940
    entry->okey = okey;
941 942


943
    if (insert != NULL)
944 945 946 947 948
	insert->next = entry;

    dict->nbElems++;

    if ((nbi > MAX_HASH_LEN) &&
949 950 951 952
        (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN))) {
	if (xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size) != 0)
	    return(NULL);
    }
953 954 955 956 957
    /* Note that entry may have been freed at this point by xmlDictGrow */

    return(ret);
}

958 959
/**
 * xmlDictExists:
960
 * @dict: the dictionary
961 962 963
 * @name: the name of the userdata
 * @len: the length of the name, if -1 it is recomputed
 *
964
 * Check if the @name exists in the dictionary @dict.
965 966 967 968 969 970 971
 *
 * Returns the internal copy of the name or NULL if not found.
 */
const xmlChar *
xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
    unsigned long key, okey, nbi = 0;
    xmlDictEntryPtr insert;
972
    unsigned int l;
973 974 975 976 977

    if ((dict == NULL) || (name == NULL))
	return(NULL);

    if (len < 0)
978 979 980 981 982 983
        l = strlen((const char *) name);
    else
        l = len;
    if (((dict->limit > 0) && (l >= dict->limit)) ||
        (l > INT_MAX / 2))
        return(NULL);
984 985 986 987

    /*
     * Check for duplicate and insertion location.
     */
988
    okey = xmlDictComputeKey(dict, name, l);
989 990 991 992 993 994 995
    key = okey % dict->size;
    if (dict->dict[key].valid == 0) {
	insert = NULL;
    } else {
	for (insert = &(dict->dict[key]); insert->next != NULL;
	     insert = insert->next) {
#ifdef __GNUC__
996 997
	    if ((insert->okey == okey) && (insert->len == l)) {
		if (!memcmp(insert->name, name, l))
998 999 1000
		    return(insert->name);
	    }
#else
1001 1002
	    if ((insert->okey == okey) && (insert->len == l) &&
	        (!xmlStrncmp(insert->name, name, l)))
1003 1004 1005 1006 1007
		return(insert->name);
#endif
	    nbi++;
	}
#ifdef __GNUC__
1008 1009
	if ((insert->okey == okey) && (insert->len == l)) {
	    if (!memcmp(insert->name, name, l))
1010 1011 1012
		return(insert->name);
	}
#else
1013 1014
	if ((insert->okey == okey) && (insert->len == l) &&
	    (!xmlStrncmp(insert->name, name, l)))
1015 1016 1017 1018 1019
	    return(insert->name);
#endif
    }

    if (dict->subdict) {
1020 1021 1022 1023 1024 1025 1026
        unsigned long skey;

        /* we cannot always reuse the same okey for the subdict */
        if (((dict->size == MIN_DICT_SIZE) &&
	     (dict->subdict->size != MIN_DICT_SIZE)) ||
            ((dict->size != MIN_DICT_SIZE) &&
	     (dict->subdict->size == MIN_DICT_SIZE)))
1027
	    skey = xmlDictComputeKey(dict->subdict, name, l);
1028 1029 1030 1031
	else
	    skey = okey;

	key = skey % dict->subdict->size;
1032 1033 1034 1035 1036 1037
	if (dict->subdict->dict[key].valid != 0) {
	    xmlDictEntryPtr tmp;

	    for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL;
		 tmp = tmp->next) {
#ifdef __GNUC__
1038 1039
		if ((tmp->okey == skey) && (tmp->len == l)) {
		    if (!memcmp(tmp->name, name, l))
1040 1041 1042
			return(tmp->name);
		}
#else
1043 1044
		if ((tmp->okey == skey) && (tmp->len == l) &&
		    (!xmlStrncmp(tmp->name, name, l)))
1045 1046 1047 1048 1049
		    return(tmp->name);
#endif
		nbi++;
	    }
#ifdef __GNUC__
1050 1051
	    if ((tmp->okey == skey) && (tmp->len == l)) {
		if (!memcmp(tmp->name, name, l))
1052 1053 1054
		    return(tmp->name);
	    }
#else
1055 1056
	    if ((tmp->okey == skey) && (tmp->len == l) &&
		(!xmlStrncmp(tmp->name, name, l)))
1057 1058 1059 1060 1061 1062 1063 1064 1065
		return(tmp->name);
#endif
	}
    }

    /* not found */
    return(NULL);
}

1066 1067
/**
 * xmlDictQLookup:
1068
 * @dict: the dictionary
1069
 * @prefix: the prefix
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
 * @name: the name
 *
 * Add the QName @prefix:@name to the hash @dict if not present.
 *
 * Returns the internal copy of the QName or NULL in case of internal error
 */
const xmlChar *
xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) {
    unsigned long okey, key, nbi = 0;
    xmlDictEntryPtr entry;
    xmlDictEntryPtr insert;
    const xmlChar *ret;
1082
    unsigned int len, plen, l;
1083 1084 1085

    if ((dict == NULL) || (name == NULL))
	return(NULL);
1086 1087
    if (prefix == NULL)
        return(xmlDictLookup(dict, name, -1));
1088

1089 1090 1091
    l = len = strlen((const char *) name);
    plen = strlen((const char *) prefix);
    len += 1 + plen;
1092 1093 1094 1095

    /*
     * Check for duplicate and insertion location.
     */
1096
    okey = xmlDictComputeQKey(dict, prefix, plen, name, l);
1097 1098 1099 1100 1101 1102
    key = okey % dict->size;
    if (dict->dict[key].valid == 0) {
	insert = NULL;
    } else {
	for (insert = &(dict->dict[key]); insert->next != NULL;
	     insert = insert->next) {
1103
	    if ((insert->okey == okey) && (insert->len == len) &&
1104 1105 1106 1107
	        (xmlStrQEqual(prefix, name, insert->name)))
		return(insert->name);
	    nbi++;
	}
1108
	if ((insert->okey == okey) && (insert->len == len) &&
1109 1110 1111 1112 1113
	    (xmlStrQEqual(prefix, name, insert->name)))
	    return(insert->name);
    }

    if (dict->subdict) {
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
        unsigned long skey;

        /* we cannot always reuse the same okey for the subdict */
        if (((dict->size == MIN_DICT_SIZE) &&
	     (dict->subdict->size != MIN_DICT_SIZE)) ||
            ((dict->size != MIN_DICT_SIZE) &&
	     (dict->subdict->size == MIN_DICT_SIZE)))
	    skey = xmlDictComputeQKey(dict->subdict, prefix, plen, name, l);
	else
	    skey = okey;

	key = skey % dict->subdict->size;
1126 1127 1128 1129
	if (dict->subdict->dict[key].valid != 0) {
	    xmlDictEntryPtr tmp;
	    for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL;
		 tmp = tmp->next) {
1130
		if ((tmp->okey == skey) && (tmp->len == len) &&
1131 1132 1133 1134
		    (xmlStrQEqual(prefix, name, tmp->name)))
		    return(tmp->name);
		nbi++;
	    }
1135
	    if ((tmp->okey == skey) && (tmp->len == len) &&
1136 1137 1138 1139 1140 1141
		(xmlStrQEqual(prefix, name, tmp->name)))
		return(tmp->name);
	}
	key = okey % dict->size;
    }

1142
    ret = xmlDictAddQString(dict, prefix, plen, name, l);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    if (ret == NULL)
        return(NULL);
    if (insert == NULL) {
	entry = &(dict->dict[key]);
    } else {
	entry = xmlMalloc(sizeof(xmlDictEntry));
	if (entry == NULL)
	     return(NULL);
    }
    entry->name = ret;
    entry->len = len;
    entry->next = NULL;
    entry->valid = 1;
1156
    entry->okey = okey;
1157

1158
    if (insert != NULL)
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	insert->next = entry;

    dict->nbElems++;

    if ((nbi > MAX_HASH_LEN) &&
        (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN)))
	xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size);
    /* Note that entry may have been freed at this point by xmlDictGrow */

    return(ret);
}

/**
 * xmlDictOwns:
1173
 * @dict: the dictionary
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
 * @str: the string
 *
 * check if a string is owned by the disctionary
 *
 * Returns 1 if true, 0 if false and -1 in case of error
 * -1 in case of error
 */
int
xmlDictOwns(xmlDictPtr dict, const xmlChar *str) {
    xmlDictStringsPtr pool;

    if ((dict == NULL) || (str == NULL))
	return(-1);
    pool = dict->strings;
    while (pool != NULL) {
1189
        if ((str >= &pool->array[0]) && (str <= pool->free))
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	    return(1);
	pool = pool->next;
    }
    if (dict->subdict)
        return(xmlDictOwns(dict->subdict, str));
    return(0);
}

/**
 * xmlDictSize:
1200
 * @dict: the dictionary
1201 1202 1203
 *
 * Query the number of elements installed in the hash @dict.
 *
1204
 * Returns the number of elements in the dictionary or
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
 * -1 in case of error
 */
int
xmlDictSize(xmlDictPtr dict) {
    if (dict == NULL)
	return(-1);
    if (dict->subdict)
        return(dict->nbElems + dict->subdict->nbElems);
    return(dict->nbElems);
}

1216 1217
/**
 * xmlDictSetLimit:
1218
 * @dict: the dictionary
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
 * @limit: the limit in bytes
 *
 * Set a size limit for the dictionary
 * Added in 2.9.0
 *
 * Returns the previous limit of the dictionary or 0
 */
size_t
xmlDictSetLimit(xmlDictPtr dict, size_t limit) {
    size_t ret;

    if (dict == NULL)
	return(0);
    ret = dict->limit;
    dict->limit = limit;
    return(ret);
}

/**
 * xmlDictGetUsage:
1239
 * @dict: the dictionary
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
 *
 * Get how much memory is used by a dictionary for strings
 * Added in 2.9.0
 *
 * Returns the amount of strings allocated
 */
size_t
xmlDictGetUsage(xmlDictPtr dict) {
    xmlDictStringsPtr pool;
    size_t limit = 0;

    if (dict == NULL)
	return(0);
    pool = dict->strings;
    while (pool != NULL) {
        limit += pool->size;
	pool = pool->next;
    }
    return(limit);
}
1260

1261 1262
#define bottom_dict
#include "elfgcchack.h"