Coverage Report

Created: 2026-07-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/entities.c
Line
Count
Source
1
/*
2
 * entities.c : implementation for the XML entities handling
3
 *
4
 * See Copyright for the status of this software.
5
 *
6
 * daniel@veillard.com
7
 */
8
9
/* To avoid EBCDIC trouble when parsing on zOS */
10
#if defined(__MVS__)
11
#pragma convert("ISO8859-1")
12
#endif
13
14
#define IN_LIBXML
15
#include "libxml.h"
16
17
#include <string.h>
18
#include <stdlib.h>
19
20
#include <libxml/xmlmemory.h>
21
#include <libxml/hash.h>
22
#include <libxml/entities.h>
23
#include <libxml/parser.h>
24
#include <libxml/parserInternals.h>
25
#include <libxml/xmlerror.h>
26
#include <libxml/dict.h>
27
#include <libxml/xmlsave.h>
28
29
#include "private/entities.h"
30
#include "private/error.h"
31
32
#ifndef SIZE_MAX
33
  #define SIZE_MAX ((size_t) -1)
34
#endif
35
36
/*
37
 * The XML predefined entities.
38
 */
39
40
static xmlEntity xmlEntityLt = {
41
    NULL, XML_ENTITY_DECL, BAD_CAST "lt",
42
    NULL, NULL, NULL, NULL, NULL, NULL,
43
    BAD_CAST "<", BAD_CAST "<", 1,
44
    XML_INTERNAL_PREDEFINED_ENTITY,
45
    NULL, NULL, NULL, NULL, 0, 0, 0
46
};
47
static xmlEntity xmlEntityGt = {
48
    NULL, XML_ENTITY_DECL, BAD_CAST "gt",
49
    NULL, NULL, NULL, NULL, NULL, NULL,
50
    BAD_CAST ">", BAD_CAST ">", 1,
51
    XML_INTERNAL_PREDEFINED_ENTITY,
52
    NULL, NULL, NULL, NULL, 0, 0, 0
53
};
54
static xmlEntity xmlEntityAmp = {
55
    NULL, XML_ENTITY_DECL, BAD_CAST "amp",
56
    NULL, NULL, NULL, NULL, NULL, NULL,
57
    BAD_CAST "&", BAD_CAST "&", 1,
58
    XML_INTERNAL_PREDEFINED_ENTITY,
59
    NULL, NULL, NULL, NULL, 0, 0, 0
60
};
61
static xmlEntity xmlEntityQuot = {
62
    NULL, XML_ENTITY_DECL, BAD_CAST "quot",
63
    NULL, NULL, NULL, NULL, NULL, NULL,
64
    BAD_CAST "\"", BAD_CAST "\"", 1,
65
    XML_INTERNAL_PREDEFINED_ENTITY,
66
    NULL, NULL, NULL, NULL, 0, 0, 0
67
};
68
static xmlEntity xmlEntityApos = {
69
    NULL, XML_ENTITY_DECL, BAD_CAST "apos",
70
    NULL, NULL, NULL, NULL, NULL, NULL,
71
    BAD_CAST "'", BAD_CAST "'", 1,
72
    XML_INTERNAL_PREDEFINED_ENTITY,
73
    NULL, NULL, NULL, NULL, 0, 0, 0
74
};
75
76
/*
77
 * xmlFreeEntity:
78
 * @entity:  an entity
79
 *
80
 * Frees the entity.
81
 */
82
void
83
xmlFreeEntity(xmlEntityPtr entity)
84
122k
{
85
122k
    xmlDictPtr dict = NULL;
86
87
122k
    if (entity == NULL)
88
0
        return;
89
90
122k
    if (entity->doc != NULL)
91
122k
        dict = entity->doc->dict;
92
93
94
122k
    if ((entity->children) &&
95
1.16k
        (entity == (xmlEntityPtr) entity->children->parent))
96
1.16k
        xmlFreeNodeList(entity->children);
97
122k
    if ((entity->name != NULL) &&
98
122k
        ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
99
31.2k
        xmlFree((char *) entity->name);
100
122k
    if (entity->ExternalID != NULL)
101
8.48k
        xmlFree((char *) entity->ExternalID);
102
122k
    if (entity->SystemID != NULL)
103
37.4k
        xmlFree((char *) entity->SystemID);
104
122k
    if (entity->URI != NULL)
105
35.0k
        xmlFree((char *) entity->URI);
106
122k
    if (entity->content != NULL)
107
81.8k
        xmlFree((char *) entity->content);
108
122k
    if (entity->orig != NULL)
109
66.8k
        xmlFree((char *) entity->orig);
110
122k
    xmlFree(entity);
111
122k
}
112
113
/*
114
 * xmlCreateEntity:
115
 *
116
 * internal routine doing the entity node structures allocations
117
 */
118
static xmlEntityPtr
119
xmlCreateEntity(xmlDocPtr doc, const xmlChar *name, int type,
120
          const xmlChar *ExternalID, const xmlChar *SystemID,
121
100k
          const xmlChar *content) {
122
100k
    xmlEntityPtr ret;
123
124
100k
    ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
125
100k
    if (ret == NULL)
126
14
  return(NULL);
127
100k
    memset(ret, 0, sizeof(xmlEntity));
128
100k
    ret->doc = doc;
129
100k
    ret->type = XML_ENTITY_DECL;
130
131
    /*
132
     * fill the structure.
133
     */
134
100k
    ret->etype = (xmlEntityType) type;
135
100k
    if ((doc == NULL) || (doc->dict == NULL))
136
8.96k
  ret->name = xmlStrdup(name);
137
91.0k
    else
138
91.0k
        ret->name = xmlDictLookup(doc->dict, name, -1);
139
100k
    if (ret->name == NULL)
140
3
        goto error;
141
99.9k
    if (ExternalID != NULL) {
142
8.48k
        ret->ExternalID = xmlStrdup(ExternalID);
143
8.48k
        if (ret->ExternalID == NULL)
144
2
            goto error;
145
8.48k
    }
146
99.9k
    if (SystemID != NULL) {
147
34.6k
        ret->SystemID = xmlStrdup(SystemID);
148
34.6k
        if (ret->SystemID == NULL)
149
5
            goto error;
150
34.6k
    }
151
99.9k
    if (content != NULL) {
152
60.8k
        ret->length = xmlStrlen(content);
153
60.8k
  ret->content = xmlStrndup(content, ret->length);
154
60.8k
        if (ret->content == NULL)
155
5
            goto error;
156
60.8k
     } else {
157
39.1k
        ret->length = 0;
158
39.1k
        ret->content = NULL;
159
39.1k
    }
160
99.9k
    ret->URI = NULL; /* to be computed by the layer knowing
161
      the defining entity */
162
99.9k
    ret->orig = NULL;
163
164
99.9k
    return(ret);
165
166
15
error:
167
15
    xmlFreeEntity(ret);
168
15
    return(NULL);
169
99.9k
}
170
171
/**
172
 * xmlAddEntity:
173
 * @doc:  the document
174
 * @extSubset:  add to the external or internal subset
175
 * @name:  the entity name
176
 * @type:  the entity type XML_xxx_yyy_ENTITY
177
 * @ExternalID:  the entity external ID if available
178
 * @SystemID:  the entity system ID if available
179
 * @content:  the entity content
180
 * @out:  pointer to resulting entity (optional)
181
 *
182
 * Register a new entity for this document.
183
 *
184
 * Available since 2.13.0.
185
 *
186
 * Returns an xmlParserErrors error code.
187
 */
188
int
189
xmlAddEntity(xmlDocPtr doc, int extSubset, const xmlChar *name, int type,
190
    const xmlChar *ExternalID, const xmlChar *SystemID,
191
109k
    const xmlChar *content, xmlEntityPtr *out) {
192
109k
    xmlDtdPtr dtd;
193
109k
    xmlDictPtr dict = NULL;
194
109k
    xmlEntitiesTablePtr table = NULL;
195
109k
    xmlEntityPtr ret, predef;
196
109k
    int res;
197
198
109k
    if (out != NULL)
199
109k
        *out = NULL;
200
109k
    if ((doc == NULL) || (name == NULL))
201
0
  return(XML_ERR_ARGUMENT);
202
109k
    dict = doc->dict;
203
204
109k
    if (extSubset)
205
6.74k
        dtd = doc->extSubset;
206
102k
    else
207
102k
        dtd = doc->intSubset;
208
109k
    if (dtd == NULL)
209
0
        return(XML_DTD_NO_DTD);
210
211
109k
    switch (type) {
212
34.2k
        case XML_INTERNAL_GENERAL_ENTITY:
213
47.4k
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
214
47.7k
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
215
47.7k
            predef = xmlGetPredefinedEntity(name);
216
47.7k
            if (predef != NULL) {
217
12.8k
                int valid = 0;
218
219
                /* 4.6 Predefined Entities */
220
12.8k
                if ((type == XML_INTERNAL_GENERAL_ENTITY) &&
221
11.8k
                    (content != NULL)) {
222
11.6k
                    int c = predef->content[0];
223
224
11.6k
                    if (((content[0] == c) && (content[1] == 0)) &&
225
1.27k
                        ((c == '>') || (c == '\'') || (c == '"'))) {
226
1.04k
                        valid = 1;
227
10.6k
                    } else if ((content[0] == '&') && (content[1] == '#')) {
228
5.70k
                        if (content[2] == 'x') {
229
4.44k
                            xmlChar *hex = BAD_CAST "0123456789ABCDEF";
230
4.44k
                            xmlChar ref[] = "00;";
231
232
4.44k
                            ref[0] = hex[c / 16 % 16];
233
4.44k
                            ref[1] = hex[c % 16];
234
4.44k
                            if (xmlStrcasecmp(&content[3], ref) == 0)
235
1.75k
                                valid = 1;
236
4.44k
                        } else {
237
1.25k
                            xmlChar ref[] = "00;";
238
239
1.25k
                            ref[0] = '0' + c / 10 % 10;
240
1.25k
                            ref[1] = '0' + c % 10;
241
1.25k
                            if (xmlStrEqual(&content[2], ref))
242
840
                                valid = 1;
243
1.25k
                        }
244
5.70k
                    }
245
11.6k
                }
246
12.8k
                if (!valid)
247
9.21k
                    return(XML_ERR_REDECL_PREDEF_ENTITY);
248
12.8k
            }
249
38.5k
      if (dtd->entities == NULL) {
250
20.4k
    dtd->entities = xmlHashCreateDict(0, dict);
251
20.4k
                if (dtd->entities == NULL)
252
4
                    return(XML_ERR_NO_MEMORY);
253
20.4k
            }
254
38.5k
      table = dtd->entities;
255
38.5k
      break;
256
35.0k
        case XML_INTERNAL_PARAMETER_ENTITY:
257
61.4k
        case XML_EXTERNAL_PARAMETER_ENTITY:
258
61.4k
      if (dtd->pentities == NULL) {
259
21.9k
    dtd->pentities = xmlHashCreateDict(0, dict);
260
21.9k
                if (dtd->pentities == NULL)
261
3
                    return(XML_ERR_NO_MEMORY);
262
21.9k
            }
263
61.4k
      table = dtd->pentities;
264
61.4k
      break;
265
0
        default:
266
0
      return(XML_ERR_ARGUMENT);
267
109k
    }
268
100k
    ret = xmlCreateEntity(dtd->doc, name, type, ExternalID, SystemID, content);
269
100k
    if (ret == NULL)
270
29
        return(XML_ERR_NO_MEMORY);
271
272
99.9k
    res = xmlHashAdd(table, name, ret);
273
99.9k
    if (res < 0) {
274
4
        xmlFreeEntity(ret);
275
4
        return(XML_ERR_NO_MEMORY);
276
99.9k
    } else if (res == 0) {
277
  /*
278
   * entity was already defined at another level.
279
   */
280
17.0k
        xmlFreeEntity(ret);
281
17.0k
  return(XML_WAR_ENTITY_REDEFINED);
282
17.0k
    }
283
284
    /*
285
     * Link it to the DTD
286
     */
287
82.9k
    ret->parent = dtd;
288
82.9k
    ret->doc = dtd->doc;
289
82.9k
    if (dtd->last == NULL) {
290
31.0k
  dtd->children = dtd->last = (xmlNodePtr) ret;
291
51.8k
    } else {
292
51.8k
  dtd->last->next = (xmlNodePtr) ret;
293
51.8k
  ret->prev = dtd->last;
294
51.8k
  dtd->last = (xmlNodePtr) ret;
295
51.8k
    }
296
297
82.9k
    if (out != NULL)
298
82.9k
        *out = ret;
299
82.9k
    return(0);
300
99.9k
}
301
302
/**
303
 * xmlGetPredefinedEntity:
304
 * @name:  the entity name
305
 *
306
 * Check whether this name is an predefined entity.
307
 *
308
 * Returns NULL if not, otherwise the entity
309
 */
310
xmlEntityPtr
311
1.43M
xmlGetPredefinedEntity(const xmlChar *name) {
312
1.43M
    if (name == NULL) return(NULL);
313
1.43M
    switch (name[0]) {
314
17.1k
        case 'l':
315
17.1k
      if (xmlStrEqual(name, BAD_CAST "lt"))
316
11.0k
          return(&xmlEntityLt);
317
6.03k
      break;
318
48.4k
        case 'g':
319
48.4k
      if (xmlStrEqual(name, BAD_CAST "gt"))
320
36.8k
          return(&xmlEntityGt);
321
11.6k
      break;
322
441k
        case 'a':
323
441k
      if (xmlStrEqual(name, BAD_CAST "amp"))
324
68.4k
          return(&xmlEntityAmp);
325
373k
      if (xmlStrEqual(name, BAD_CAST "apos"))
326
3.04k
          return(&xmlEntityApos);
327
370k
      break;
328
408k
        case 'q':
329
408k
      if (xmlStrEqual(name, BAD_CAST "quot"))
330
5.47k
          return(&xmlEntityQuot);
331
403k
      break;
332
514k
  default:
333
514k
      break;
334
1.43M
    }
335
1.30M
    return(NULL);
336
1.43M
}
337
338
/**
339
 * xmlAddDtdEntity:
340
 * @doc:  the document
341
 * @name:  the entity name
342
 * @type:  the entity type XML_xxx_yyy_ENTITY
343
 * @ExternalID:  the entity external ID if available
344
 * @SystemID:  the entity system ID if available
345
 * @content:  the entity content
346
 *
347
 * Register a new entity for this document DTD external subset.
348
 *
349
 * Returns a pointer to the entity or NULL in case of error
350
 */
351
xmlEntityPtr
352
xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
353
          const xmlChar *ExternalID, const xmlChar *SystemID,
354
0
    const xmlChar *content) {
355
0
    xmlEntityPtr ret;
356
357
0
    xmlAddEntity(doc, 1, name, type, ExternalID, SystemID, content, &ret);
358
0
    return(ret);
359
0
}
360
361
/**
362
 * xmlAddDocEntity:
363
 * @doc:  the document
364
 * @name:  the entity name
365
 * @type:  the entity type XML_xxx_yyy_ENTITY
366
 * @ExternalID:  the entity external ID if available
367
 * @SystemID:  the entity system ID if available
368
 * @content:  the entity content
369
 *
370
 * Register a new entity for this document.
371
 *
372
 * Returns a pointer to the entity or NULL in case of error
373
 */
374
xmlEntityPtr
375
xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
376
          const xmlChar *ExternalID, const xmlChar *SystemID,
377
0
          const xmlChar *content) {
378
0
    xmlEntityPtr ret;
379
380
0
    xmlAddEntity(doc, 0, name, type, ExternalID, SystemID, content, &ret);
381
0
    return(ret);
382
0
}
383
384
/**
385
 * xmlNewEntity:
386
 * @doc:  the document
387
 * @name:  the entity name
388
 * @type:  the entity type XML_xxx_yyy_ENTITY
389
 * @ExternalID:  the entity external ID if available
390
 * @SystemID:  the entity system ID if available
391
 * @content:  the entity content
392
 *
393
 * Create a new entity, this differs from xmlAddDocEntity() that if
394
 * the document is NULL or has no internal subset defined, then an
395
 * unlinked entity structure will be returned, it is then the responsibility
396
 * of the caller to link it to the document later or free it when not needed
397
 * anymore.
398
 *
399
 * Returns a pointer to the entity or NULL in case of error
400
 */
401
xmlEntityPtr
402
xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
403
       const xmlChar *ExternalID, const xmlChar *SystemID,
404
0
       const xmlChar *content) {
405
0
    if ((doc != NULL) && (doc->intSubset != NULL)) {
406
0
  return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
407
0
    }
408
0
    if (name == NULL)
409
0
        return(NULL);
410
0
    return(xmlCreateEntity(doc, name, type, ExternalID, SystemID, content));
411
0
}
412
413
/**
414
 * xmlGetEntityFromTable:
415
 * @table:  an entity table
416
 * @name:  the entity name
417
 * @parameter:  look for parameter entities
418
 *
419
 * Do an entity lookup in the table.
420
 * returns the corresponding parameter entity, if found.
421
 *
422
 * Returns A pointer to the entity structure or NULL if not found.
423
 */
424
static xmlEntityPtr
425
688k
xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
426
688k
    return((xmlEntityPtr) xmlHashLookup(table, name));
427
688k
}
428
429
/**
430
 * xmlGetParameterEntity:
431
 * @doc:  the document referencing the entity
432
 * @name:  the entity name
433
 *
434
 * Do an entity lookup in the internal and external subsets and
435
 * returns the corresponding parameter entity, if found.
436
 *
437
 * Returns A pointer to the entity structure or NULL if not found.
438
 */
439
xmlEntityPtr
440
144k
xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
441
144k
    xmlEntitiesTablePtr table;
442
144k
    xmlEntityPtr ret;
443
444
144k
    if (doc == NULL)
445
471
  return(NULL);
446
144k
    if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
447
136k
  table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
448
136k
  ret = xmlGetEntityFromTable(table, name);
449
136k
  if (ret != NULL)
450
115k
      return(ret);
451
136k
    }
452
28.4k
    if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
453
8.14k
  table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
454
8.14k
  return(xmlGetEntityFromTable(table, name));
455
8.14k
    }
456
20.3k
    return(NULL);
457
28.4k
}
458
459
/**
460
 * xmlGetDtdEntity:
461
 * @doc:  the document referencing the entity
462
 * @name:  the entity name
463
 *
464
 * Do an entity lookup in the DTD entity hash table and
465
 * returns the corresponding entity, if found.
466
 * Note: the first argument is the document node, not the DTD node.
467
 *
468
 * Returns A pointer to the entity structure or NULL if not found.
469
 */
470
xmlEntityPtr
471
0
xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
472
0
    xmlEntitiesTablePtr table;
473
474
0
    if (doc == NULL)
475
0
  return(NULL);
476
0
    if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
477
0
  table = (xmlEntitiesTablePtr) doc->extSubset->entities;
478
0
  return(xmlGetEntityFromTable(table, name));
479
0
    }
480
0
    return(NULL);
481
0
}
482
483
/**
484
 * xmlGetDocEntity:
485
 * @doc:  the document referencing the entity
486
 * @name:  the entity name
487
 *
488
 * Do an entity lookup in the document entity hash table and
489
 * returns the corresponding entity, otherwise a lookup is done
490
 * in the predefined entities too.
491
 *
492
 * Returns A pointer to the entity structure or NULL if not found.
493
 */
494
xmlEntityPtr
495
616k
xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
496
616k
    xmlEntityPtr cur;
497
616k
    xmlEntitiesTablePtr table;
498
499
616k
    if (doc != NULL) {
500
592k
  if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
501
488k
      table = (xmlEntitiesTablePtr) doc->intSubset->entities;
502
488k
      cur = xmlGetEntityFromTable(table, name);
503
488k
      if (cur != NULL)
504
417k
    return(cur);
505
488k
  }
506
175k
  if (doc->standalone != 1) {
507
174k
      if ((doc->extSubset != NULL) &&
508
56.1k
    (doc->extSubset->entities != NULL)) {
509
54.8k
    table = (xmlEntitiesTablePtr) doc->extSubset->entities;
510
54.8k
    cur = xmlGetEntityFromTable(table, name);
511
54.8k
    if (cur != NULL)
512
311
        return(cur);
513
54.8k
      }
514
174k
  }
515
175k
    }
516
198k
    return(xmlGetPredefinedEntity(name));
517
616k
}
518
519
/*
520
 * xmlSerializeHexCharRef:
521
 * @buf:  a char buffer
522
 * @val:  a codepoint
523
 *
524
 * Serializes a hex char ref like &#xA0;
525
 *
526
 * Writes at most 9 bytes. Does not include a terminating zero byte.
527
 *
528
 * Returns the number of bytes written.
529
 */
530
int
531
7.57k
xmlSerializeHexCharRef(char *buf, int val) {
532
7.57k
    char *out = buf;
533
7.57k
    int shift = 0, bits;
534
535
7.57k
    *out++ = '&';
536
7.57k
    *out++ = '#';
537
7.57k
    *out++ = 'x';
538
539
7.57k
    bits = val;
540
7.57k
    if (bits & 0xFF0000) {
541
150
        shift = 16;
542
150
        bits &= 0xFF0000;
543
7.42k
    } else if (bits & 0x00FF00) {
544
5.29k
        shift = 8;
545
5.29k
        bits &= 0x00FF00;
546
5.29k
    }
547
7.57k
    if (bits & 0xF0F0F0) {
548
7.07k
        shift += 4;
549
7.07k
    }
550
551
25.8k
    do {
552
25.8k
        int d = (val >> shift) & 0x0F;
553
554
25.8k
        if (d < 10)
555
3.23k
            *out++ = '0' + d;
556
22.5k
        else
557
22.5k
            *out++ = 'A' + (d - 10);
558
559
25.8k
  shift -= 4;
560
25.8k
    } while (shift >= 0);
561
562
7.57k
    *out++ = ';';
563
564
7.57k
    return(out - buf);
565
7.57k
}
566
567
/*
568
 * xmlSerializeDecCharRef:
569
 * @buf:  a char buffer
570
 * @val:  a codepoint
571
 *
572
 * Serializes a decimal char ref like &#38;
573
 *
574
 * Writes at most 10 bytes. Does not include a terminating zero byte.
575
 *
576
 * Returns the number of bytes written.
577
 */
578
int
579
82.0M
xmlSerializeDecCharRef(char *buf, int val) {
580
82.0M
    char *out = buf;
581
82.0M
    int len, i;
582
583
82.0M
    *out++ = '&';
584
82.0M
    *out++ = '#';
585
586
82.0M
    if (val < 100) {
587
370
        len = (val < 10) ? 1 : 2;
588
82.0M
    } else if (val < 10000) {
589
82.0M
        len = (val < 1000) ? 3 : 4;
590
82.0M
    } else if (val < 1000000) {
591
5.45k
        len = (val < 100000) ? 5 : 6;
592
5.45k
    } else {
593
274
        len = 7;
594
274
    }
595
596
340M
    for (i = len - 1; i >= 0; i--) {
597
258M
        out[i] = '0' + val % 10;
598
258M
        val /= 10;
599
258M
    }
600
601
82.0M
    out[len] = ';';
602
603
82.0M
    return(len + 3);
604
82.0M
}
605
606
static const char xmlEscapeSafe[128] = {
607
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
608
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
609
    1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
610
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
611
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
612
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
613
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
614
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
615
};
616
617
/*
618
 * xmlEscapeText:
619
 * @text:  input text
620
 * @flags:  XML_ESCAPE flags
621
 *
622
 * Escapes certain characters with char refs.
623
 *
624
 * XML_ESCAPE_ATTR: for attribute content.
625
 * XML_ESCAPE_NON_ASCII: escape non-ASCII chars.
626
 * XML_ESCAPE_HTML: for HTML content.
627
 * XML_ESCAPE_QUOT: escape double quotes.
628
 * XML_ESCAPE_ALLOW_INVALID: allow invalid characters.
629
 *
630
 * Returns an escaped string or NULL if a memory allocation failed.
631
 */
632
xmlChar *
633
23.3k
xmlEscapeText(const xmlChar *text, int flags) {
634
23.3k
    const xmlChar *cur;
635
23.3k
    xmlChar *buffer;
636
23.3k
    xmlChar *out;
637
23.3k
    const xmlChar *unescaped;
638
23.3k
    size_t size = 50;
639
640
23.3k
    buffer = xmlMalloc(size + 1);
641
23.3k
    if (buffer == NULL)
642
65
        return(NULL);
643
23.2k
    out = buffer;
644
645
23.2k
    cur = text;
646
23.2k
    unescaped = cur;
647
648
269k
    while (*cur != '\0') {
649
245k
        char buf[12];
650
245k
  const xmlChar *end;
651
245k
        const xmlChar *repl;
652
245k
        size_t used;
653
245k
        size_t replSize;
654
245k
        size_t unescapedSize;
655
245k
        size_t totalSize;
656
245k
        int chunkSize = 1;
657
245k
        int c;
658
659
        /* accelerator */
660
176M
  while (1) {
661
176M
            c = *cur;
662
663
176M
            if (c < 0x80) {
664
90.0M
                if (!xmlEscapeSafe[*cur])
665
245k
                    break;
666
90.0M
            } else {
667
86.2M
               if (flags & XML_ESCAPE_NON_ASCII)
668
0
                   break;
669
86.2M
            }
670
176M
            cur += 1;
671
176M
        }
672
673
245k
        if (c == 0) {
674
20.8k
            chunkSize = 0;
675
20.8k
            repl = BAD_CAST "";
676
20.8k
            replSize = 0;
677
225k
        } else if (c == '<') {
678
      /*
679
       * Special handling of server side include in HTML attributes
680
       */
681
29.7k
      if ((flags & XML_ESCAPE_HTML) && (flags & XML_ESCAPE_ATTR) &&
682
27.0k
          (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
683
2.25k
          ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
684
1.95k
                chunkSize = (end - cur) + 3;
685
1.95k
                repl = cur;
686
1.95k
                replSize = chunkSize;
687
27.8k
      } else {
688
27.8k
                repl = BAD_CAST "&lt;";
689
27.8k
                replSize = 4;
690
27.8k
            }
691
195k
  } else if (c == '>') {
692
127k
            repl = BAD_CAST "&gt;";
693
127k
            replSize = 4;
694
127k
  } else if (c == '&') {
695
      /*
696
       * Special handling of &{...} construct from HTML 4, see
697
       * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
698
       */
699
12.3k
      if ((flags & XML_ESCAPE_HTML) && (flags & XML_ESCAPE_ATTR) &&
700
11.2k
                (cur[1] == '{') && (end = xmlStrchr(cur, '}'))) {
701
1.16k
                chunkSize = (end - cur) + 1;
702
1.16k
                repl = cur;
703
1.16k
                replSize = chunkSize;
704
11.1k
      } else {
705
11.1k
                repl = BAD_CAST "&amp;";
706
11.1k
                replSize = 5;
707
11.1k
            }
708
55.3k
  } else if ((flags & XML_ESCAPE_QUOT) && (c == '"')) {
709
0
            repl = BAD_CAST "&quot;";
710
0
            replSize = 6;
711
55.3k
  } else if (((flags & XML_ESCAPE_HTML) == 0) && (c == '\r')) {
712
0
      repl = BAD_CAST "&#13;";
713
0
            replSize = 5;
714
55.3k
  } else if ((flags & XML_ESCAPE_NON_ASCII) && (c >= 0x80)) {
715
0
            int val;
716
717
0
            chunkSize = 4;
718
0
            val = xmlGetUTF8Char(cur, &chunkSize);
719
0
            if (val < 0) {
720
0
                val = 0xFFFD;
721
0
                chunkSize = 1;
722
0
            } else if (((flags & XML_ESCAPE_ALLOW_INVALID) == 0) &&
723
0
                       (!IS_CHAR(val))) {
724
0
                val = 0xFFFD;
725
0
            }
726
727
0
            replSize = xmlSerializeHexCharRef(buf, val);
728
0
            repl = BAD_CAST buf;
729
55.3k
  } else if ((flags & (XML_ESCAPE_ALLOW_INVALID | XML_ESCAPE_HTML)) ||
730
0
                   (c >= 0x20) ||
731
55.3k
             (c == '\n') || (c == '\t') || (c == '\r')) {
732
      /* default case, just copy */
733
55.3k
            cur += 1;
734
55.3k
            if (*cur != 0)
735
54.2k
                continue;
736
737
1.03k
            chunkSize = 0;
738
1.03k
            repl = BAD_CAST "";
739
1.03k
            replSize = 0;
740
1.03k
  } else {
741
            /* ignore */
742
0
            repl = BAD_CAST "";
743
0
            replSize = 0;
744
0
        }
745
746
191k
        used = out - buffer;
747
191k
        unescapedSize = cur - unescaped;
748
191k
        totalSize = unescapedSize + replSize;
749
750
191k
  cur += chunkSize;
751
752
191k
        if (totalSize > size - used) {
753
7.20k
            xmlChar *tmp;
754
7.20k
            int newSize;
755
756
7.20k
            if ((size > (SIZE_MAX - 1) / 2) ||
757
7.20k
                (totalSize > (SIZE_MAX - 1) / 2 - size)) {
758
0
                xmlFree(buffer);
759
0
                return(NULL);
760
0
            }
761
7.20k
            newSize = size + totalSize;
762
7.20k
            if (*cur != 0)
763
4.68k
                newSize *= 2;
764
7.20k
            tmp = xmlRealloc(buffer, newSize + 1);
765
7.20k
            if (tmp == NULL) {
766
5
                xmlFree(buffer);
767
5
                return(NULL);
768
5
            }
769
7.20k
            buffer = tmp;
770
7.20k
            size = newSize;
771
7.20k
            out = buffer + used;
772
7.20k
        }
773
774
191k
        memcpy(out, unescaped, unescapedSize);
775
191k
        out += unescapedSize;
776
191k
        memcpy(out, repl, replSize);
777
191k
        out += replSize;
778
779
191k
        unescaped = cur;
780
191k
    }
781
782
23.2k
    *out = 0;
783
23.2k
    return(buffer);
784
23.2k
}
785
786
/**
787
 * xmlEncodeEntitiesInternal:
788
 * @doc:  the document containing the string
789
 * @input:  A string to convert to XML.
790
 * @flags:  XML_ESCAPE flags
791
 *
792
 * Do a global encoding of a string, replacing the predefined entities
793
 * and non ASCII values with their entities and CharRef counterparts.
794
 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
795
 * must be deallocated.
796
 *
797
 * Returns A newly allocated string with the substitution done.
798
 */
799
xmlChar *
800
xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input,
801
23.3k
                          unsigned flags) {
802
23.3k
    if (input == NULL)
803
0
        return(NULL);
804
805
23.3k
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
806
23.3k
        flags |= XML_ESCAPE_HTML;
807
0
    else if ((doc == NULL) || (doc->encoding == NULL))
808
0
        flags |= XML_ESCAPE_NON_ASCII;
809
810
23.3k
    return(xmlEscapeText(input, flags));
811
23.3k
}
812
813
/**
814
 * xmlEncodeEntitiesReentrant:
815
 * @doc:  the document containing the string
816
 * @input:  A string to convert to XML.
817
 *
818
 * Do a global encoding of a string, replacing the predefined entities
819
 * and non ASCII values with their entities and CharRef counterparts.
820
 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
821
 * must be deallocated.
822
 *
823
 * This escapes '<', '>', '&' and '\r'. If the document has no encoding,
824
 * non-ASCII codepoints are escaped. There is some special handling for
825
 * HTML documents.
826
 *
827
 * Returns A newly allocated string with the substitution done.
828
 */
829
xmlChar *
830
14.3k
xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
831
14.3k
    return xmlEncodeEntitiesInternal(doc, input, 0);
832
14.3k
}
833
834
/**
835
 * xmlEncodeSpecialChars:
836
 * @doc:  unused
837
 * @input:  A string to convert to XML.
838
 *
839
 * Do a global encoding of a string, replacing the predefined entities
840
 * this routine is reentrant, and result must be deallocated.
841
 *
842
 * This escapes '<', '>', '&', '"' and '\r' chars.
843
 *
844
 * Returns A newly allocated string with the substitution done.
845
 */
846
xmlChar *
847
xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED,
848
0
                      const xmlChar *input) {
849
0
    if (input == NULL)
850
0
        return(NULL);
851
852
0
    return(xmlEscapeText(input, XML_ESCAPE_QUOT | XML_ESCAPE_ALLOW_INVALID));
853
0
}
854
855
/**
856
 * xmlCreateEntitiesTable:
857
 *
858
 * create and initialize an empty entities hash table.
859
 * This really doesn't make sense and should be deprecated
860
 *
861
 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
862
 */
863
xmlEntitiesTablePtr
864
0
xmlCreateEntitiesTable(void) {
865
0
    return((xmlEntitiesTablePtr) xmlHashCreate(0));
866
0
}
867
868
/**
869
 * xmlFreeEntityWrapper:
870
 * @entity:  An entity
871
 * @name:  its name
872
 *
873
 * Deallocate the memory used by an entities in the hash table.
874
 */
875
static void
876
105k
xmlFreeEntityWrapper(void *entity, const xmlChar *name ATTRIBUTE_UNUSED) {
877
105k
    if (entity != NULL)
878
105k
  xmlFreeEntity((xmlEntityPtr) entity);
879
105k
}
880
881
/**
882
 * xmlFreeEntitiesTable:
883
 * @table:  An entity table
884
 *
885
 * Deallocate the memory used by an entities hash table.
886
 */
887
void
888
50.5k
xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
889
50.5k
    xmlHashFree(table, xmlFreeEntityWrapper);
890
50.5k
}
891
892
/**
893
 * xmlCopyEntity:
894
 * @ent:  An entity
895
 *
896
 * Build a copy of an entity
897
 *
898
 * Returns the new xmlEntitiesPtr or NULL in case of error.
899
 */
900
static void *
901
22.3k
xmlCopyEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
902
22.3k
    xmlEntityPtr ent = (xmlEntityPtr) payload;
903
22.3k
    xmlEntityPtr cur;
904
905
22.3k
    cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
906
22.3k
    if (cur == NULL)
907
6
  return(NULL);
908
22.2k
    memset(cur, 0, sizeof(xmlEntity));
909
22.2k
    cur->type = XML_ENTITY_DECL;
910
911
22.2k
    cur->etype = ent->etype;
912
22.2k
    if (ent->name != NULL) {
913
22.2k
  cur->name = xmlStrdup(ent->name);
914
22.2k
        if (cur->name == NULL)
915
3
            goto error;
916
22.2k
    }
917
22.2k
    if (ent->ExternalID != NULL) {
918
0
  cur->ExternalID = xmlStrdup(ent->ExternalID);
919
0
        if (cur->ExternalID == NULL)
920
0
            goto error;
921
0
    }
922
22.2k
    if (ent->SystemID != NULL) {
923
2.82k
  cur->SystemID = xmlStrdup(ent->SystemID);
924
2.82k
        if (cur->SystemID == NULL)
925
1
            goto error;
926
2.82k
    }
927
22.2k
    if (ent->content != NULL) {
928
19.4k
  cur->content = xmlStrdup(ent->content);
929
19.4k
        if (cur->content == NULL)
930
3
            goto error;
931
19.4k
    }
932
22.2k
    if (ent->orig != NULL) {
933
19.4k
  cur->orig = xmlStrdup(ent->orig);
934
19.4k
        if (cur->orig == NULL)
935
1
            goto error;
936
19.4k
    }
937
22.2k
    if (ent->URI != NULL) {
938
1.59k
  cur->URI = xmlStrdup(ent->URI);
939
1.59k
        if (cur->URI == NULL)
940
1
            goto error;
941
1.59k
    }
942
22.2k
    return(cur);
943
944
9
error:
945
9
    xmlFreeEntity(cur);
946
9
    return(NULL);
947
22.2k
}
948
949
/**
950
 * xmlCopyEntitiesTable:
951
 * @table:  An entity table
952
 *
953
 * Build a copy of an entity table.
954
 *
955
 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
956
 */
957
xmlEntitiesTablePtr
958
8.20k
xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
959
8.20k
    return(xmlHashCopySafe(table, xmlCopyEntity, xmlFreeEntityWrapper));
960
8.20k
}
961
962
#ifdef LIBXML_OUTPUT_ENABLED
963
964
/**
965
 * xmlDumpEntityDecl:
966
 * @buf:  An XML buffer.
967
 * @ent:  An entity table
968
 *
969
 * This will dump the content of the entity table as an XML DTD definition
970
 */
971
void
972
0
xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
973
0
    xmlSaveCtxtPtr save;
974
975
0
    if ((buf == NULL) || (ent == NULL))
976
0
        return;
977
978
0
    save = xmlSaveToBuffer(buf, NULL, 0);
979
0
    xmlSaveTree(save, (xmlNodePtr) ent);
980
0
    if (xmlSaveFinish(save) != XML_ERR_OK)
981
0
        xmlFree(xmlBufferDetach(buf));
982
0
}
983
984
/**
985
 * xmlDumpEntityDeclScan:
986
 * @ent:  An entity table
987
 * @buf:  An XML buffer.
988
 *
989
 * When using the hash table scan function, arguments need to be reversed
990
 */
991
static void
992
xmlDumpEntityDeclScan(void *ent, void *save,
993
0
                      const xmlChar *name ATTRIBUTE_UNUSED) {
994
0
    xmlSaveTree(save, ent);
995
0
}
996
997
/**
998
 * xmlDumpEntitiesTable:
999
 * @buf:  An XML buffer.
1000
 * @table:  An entity table
1001
 *
1002
 * This will dump the content of the entity table as an XML DTD definition
1003
 */
1004
void
1005
0
xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
1006
0
    xmlSaveCtxtPtr save;
1007
1008
0
    if ((buf == NULL) || (table == NULL))
1009
0
        return;
1010
1011
0
    save = xmlSaveToBuffer(buf, NULL, 0);
1012
0
    xmlHashScan(table, xmlDumpEntityDeclScan, save);
1013
0
    if (xmlSaveFinish(save) != XML_ERR_OK)
1014
0
        xmlFree(xmlBufferDetach(buf));
1015
0
}
1016
#endif /* LIBXML_OUTPUT_ENABLED */