Coverage Report

Created: 2023-03-26 06:13

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