Coverage Report

Created: 2026-12-01 06:25

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
188k
{
85
188k
    xmlDictPtr dict = NULL;
86
87
188k
    if (entity == NULL)
88
0
        return;
89
90
188k
    if (entity->doc != NULL)
91
188k
        dict = entity->doc->dict;
92
93
94
188k
    if ((entity->children) &&
95
1.56k
        (entity == (xmlEntityPtr) entity->children->parent))
96
1.56k
        xmlFreeNodeList(entity->children);
97
188k
    if ((entity->name != NULL) &&
98
188k
        ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
99
123k
        xmlFree((char *) entity->name);
100
188k
    if (entity->ExternalID != NULL)
101
8.97k
        xmlFree((char *) entity->ExternalID);
102
188k
    if (entity->SystemID != NULL)
103
66.5k
        xmlFree((char *) entity->SystemID);
104
188k
    if (entity->URI != NULL)
105
64.5k
        xmlFree((char *) entity->URI);
106
188k
    if (entity->content != NULL)
107
121k
        xmlFree((char *) entity->content);
108
188k
    if (entity->orig != NULL)
109
117k
        xmlFree((char *) entity->orig);
110
188k
    xmlFree(entity);
111
188k
}
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
79.0k
          const xmlChar *content) {
122
79.0k
    xmlEntityPtr ret;
123
124
79.0k
    ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
125
79.0k
    if (ret == NULL)
126
13
  return(NULL);
127
79.0k
    memset(ret, 0, sizeof(xmlEntity));
128
79.0k
    ret->doc = doc;
129
79.0k
    ret->type = XML_ENTITY_DECL;
130
131
    /*
132
     * fill the structure.
133
     */
134
79.0k
    ret->etype = (xmlEntityType) type;
135
79.0k
    if ((doc == NULL) || (doc->dict == NULL))
136
14.5k
  ret->name = xmlStrdup(name);
137
64.4k
    else
138
64.4k
        ret->name = xmlDictLookup(doc->dict, name, -1);
139
79.0k
    if (ret->name == NULL)
140
1
        goto error;
141
79.0k
    if (ExternalID != NULL) {
142
8.97k
        ret->ExternalID = xmlStrdup(ExternalID);
143
8.97k
        if (ret->ExternalID == NULL)
144
2
            goto error;
145
8.97k
    }
146
79.0k
    if (SystemID != NULL) {
147
35.4k
        ret->SystemID = xmlStrdup(SystemID);
148
35.4k
        if (ret->SystemID == NULL)
149
8
            goto error;
150
35.4k
    }
151
79.0k
    if (content != NULL) {
152
41.4k
        ret->length = xmlStrlen(content);
153
41.4k
  ret->content = xmlStrndup(content, ret->length);
154
41.4k
        if (ret->content == NULL)
155
7
            goto error;
156
41.4k
     } else {
157
37.5k
        ret->length = 0;
158
37.5k
        ret->content = NULL;
159
37.5k
    }
160
79.0k
    ret->URI = NULL; /* to be computed by the layer knowing
161
      the defining entity */
162
79.0k
    ret->orig = NULL;
163
164
79.0k
    return(ret);
165
166
18
error:
167
18
    xmlFreeEntity(ret);
168
18
    return(NULL);
169
79.0k
}
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
81.2k
    const xmlChar *content, xmlEntityPtr *out) {
192
81.2k
    xmlDtdPtr dtd;
193
81.2k
    xmlDictPtr dict = NULL;
194
81.2k
    xmlEntitiesTablePtr table = NULL;
195
81.2k
    xmlEntityPtr ret, predef;
196
81.2k
    int res;
197
198
81.2k
    if (out != NULL)
199
81.2k
        *out = NULL;
200
81.2k
    if ((doc == NULL) || (name == NULL))
201
0
  return(XML_ERR_ARGUMENT);
202
81.2k
    dict = doc->dict;
203
204
81.2k
    if (extSubset)
205
10.2k
        dtd = doc->extSubset;
206
71.0k
    else
207
71.0k
        dtd = doc->intSubset;
208
81.2k
    if (dtd == NULL)
209
0
        return(XML_DTD_NO_DTD);
210
211
81.2k
    switch (type) {
212
28.2k
        case XML_INTERNAL_GENERAL_ENTITY:
213
38.6k
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
214
38.9k
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
215
38.9k
            predef = xmlGetPredefinedEntity(name);
216
38.9k
            if (predef != NULL) {
217
3.33k
                int valid = 0;
218
219
                /* 4.6 Predefined Entities */
220
3.33k
                if ((type == XML_INTERNAL_GENERAL_ENTITY) &&
221
3.02k
                    (content != NULL)) {
222
2.80k
                    int c = predef->content[0];
223
224
2.80k
                    if (((content[0] == c) && (content[1] == 0)) &&
225
1.09k
                        ((c == '>') || (c == '\'') || (c == '"'))) {
226
866
                        valid = 1;
227
1.93k
                    } else if ((content[0] == '&') && (content[1] == '#')) {
228
878
                        if (content[2] == 'x') {
229
303
                            xmlChar *hex = BAD_CAST "0123456789ABCDEF";
230
303
                            xmlChar ref[] = "00;";
231
232
303
                            ref[0] = hex[c / 16 % 16];
233
303
                            ref[1] = hex[c % 16];
234
303
                            if (xmlStrcasecmp(&content[3], ref) == 0)
235
0
                                valid = 1;
236
575
                        } else {
237
575
                            xmlChar ref[] = "00;";
238
239
575
                            ref[0] = '0' + c / 10 % 10;
240
575
                            ref[1] = '0' + c % 10;
241
575
                            if (xmlStrEqual(&content[2], ref))
242
263
                                valid = 1;
243
575
                        }
244
878
                    }
245
2.80k
                }
246
3.33k
                if (!valid)
247
2.20k
                    return(XML_ERR_REDECL_PREDEF_ENTITY);
248
3.33k
            }
249
36.7k
      if (dtd->entities == NULL) {
250
25.3k
    dtd->entities = xmlHashCreateDict(0, dict);
251
25.3k
                if (dtd->entities == NULL)
252
5
                    return(XML_ERR_NO_MEMORY);
253
25.3k
            }
254
36.7k
      table = dtd->entities;
255
36.7k
      break;
256
15.9k
        case XML_INTERNAL_PARAMETER_ENTITY:
257
42.3k
        case XML_EXTERNAL_PARAMETER_ENTITY:
258
42.3k
      if (dtd->pentities == NULL) {
259
21.9k
    dtd->pentities = xmlHashCreateDict(0, dict);
260
21.9k
                if (dtd->pentities == NULL)
261
2
                    return(XML_ERR_NO_MEMORY);
262
21.9k
            }
263
42.3k
      table = dtd->pentities;
264
42.3k
      break;
265
0
        default:
266
0
      return(XML_ERR_ARGUMENT);
267
81.2k
    }
268
79.0k
    ret = xmlCreateEntity(dtd->doc, name, type, ExternalID, SystemID, content);
269
79.0k
    if (ret == NULL)
270
31
        return(XML_ERR_NO_MEMORY);
271
272
79.0k
    res = xmlHashAdd(table, name, ret);
273
79.0k
    if (res < 0) {
274
6
        xmlFreeEntity(ret);
275
6
        return(XML_ERR_NO_MEMORY);
276
79.0k
    } else if (res == 0) {
277
  /*
278
   * entity was already defined at another level.
279
   */
280
849
        xmlFreeEntity(ret);
281
849
  return(XML_WAR_ENTITY_REDEFINED);
282
849
    }
283
284
    /*
285
     * Link it to the DTD
286
     */
287
78.1k
    ret->parent = dtd;
288
78.1k
    ret->doc = dtd->doc;
289
78.1k
    if (dtd->last == NULL) {
290
30.8k
  dtd->children = dtd->last = (xmlNodePtr) ret;
291
47.3k
    } else {
292
47.3k
  dtd->last->next = (xmlNodePtr) ret;
293
47.3k
  ret->prev = dtd->last;
294
47.3k
  dtd->last = (xmlNodePtr) ret;
295
47.3k
    }
296
297
78.1k
    if (out != NULL)
298
78.1k
        *out = ret;
299
78.1k
    return(0);
300
79.0k
}
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.48M
xmlGetPredefinedEntity(const xmlChar *name) {
312
1.48M
    if (name == NULL) return(NULL);
313
1.48M
    switch (name[0]) {
314
34.6k
        case 'l':
315
34.6k
      if (xmlStrEqual(name, BAD_CAST "lt"))
316
31.3k
          return(&xmlEntityLt);
317
3.29k
      break;
318
64.7k
        case 'g':
319
64.7k
      if (xmlStrEqual(name, BAD_CAST "gt"))
320
61.9k
          return(&xmlEntityGt);
321
2.85k
      break;
322
459k
        case 'a':
323
459k
      if (xmlStrEqual(name, BAD_CAST "amp"))
324
66.3k
          return(&xmlEntityAmp);
325
393k
      if (xmlStrEqual(name, BAD_CAST "apos"))
326
1.24k
          return(&xmlEntityApos);
327
391k
      break;
328
565k
        case 'q':
329
565k
      if (xmlStrEqual(name, BAD_CAST "quot"))
330
1.24k
          return(&xmlEntityQuot);
331
564k
      break;
332
564k
  default:
333
364k
      break;
334
1.48M
    }
335
1.32M
    return(NULL);
336
1.48M
}
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
755k
xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
426
755k
    return((xmlEntityPtr) xmlHashLookup(table, name));
427
755k
}
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
119k
xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
441
119k
    xmlEntitiesTablePtr table;
442
119k
    xmlEntityPtr ret;
443
444
119k
    if (doc == NULL)
445
448
  return(NULL);
446
119k
    if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
447
112k
  table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
448
112k
  ret = xmlGetEntityFromTable(table, name);
449
112k
  if (ret != NULL)
450
90.2k
      return(ret);
451
112k
    }
452
29.0k
    if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
453
9.99k
  table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
454
9.99k
  return(xmlGetEntityFromTable(table, name));
455
9.99k
    }
456
19.0k
    return(NULL);
457
29.0k
}
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
616k
  if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
501
560k
      table = (xmlEntitiesTablePtr) doc->intSubset->entities;
502
560k
      cur = xmlGetEntityFromTable(table, name);
503
560k
      if (cur != NULL)
504
466k
    return(cur);
505
560k
  }
506
149k
  if (doc->standalone != 1) {
507
148k
      if ((doc->extSubset != NULL) &&
508
75.7k
    (doc->extSubset->entities != NULL)) {
509
73.1k
    table = (xmlEntitiesTablePtr) doc->extSubset->entities;
510
73.1k
    cur = xmlGetEntityFromTable(table, name);
511
73.1k
    if (cur != NULL)
512
1.06k
        return(cur);
513
73.1k
      }
514
148k
  }
515
149k
    }
516
148k
    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
1.67k
xmlSerializeHexCharRef(char *buf, int val) {
532
1.67k
    char *out = buf;
533
1.67k
    int shift = 0, bits;
534
535
1.67k
    *out++ = '&';
536
1.67k
    *out++ = '#';
537
1.67k
    *out++ = 'x';
538
539
1.67k
    bits = val;
540
1.67k
    if (bits & 0xFF0000) {
541
640
        shift = 16;
542
640
        bits &= 0xFF0000;
543
1.03k
    } else if (bits & 0x00FF00) {
544
515
        shift = 8;
545
515
        bits &= 0x00FF00;
546
515
    }
547
1.67k
    if (bits & 0xF0F0F0) {
548
731
        shift += 4;
549
731
    }
550
551
5.99k
    do {
552
5.99k
        int d = (val >> shift) & 0x0F;
553
554
5.99k
        if (d < 10)
555
4.22k
            *out++ = '0' + d;
556
1.77k
        else
557
1.77k
            *out++ = 'A' + (d - 10);
558
559
5.99k
  shift -= 4;
560
5.99k
    } while (shift >= 0);
561
562
1.67k
    *out++ = ';';
563
564
1.67k
    return(out - buf);
565
1.67k
}
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
87.7M
xmlSerializeDecCharRef(char *buf, int val) {
580
87.7M
    char *out = buf;
581
87.7M
    int len, i;
582
583
87.7M
    *out++ = '&';
584
87.7M
    *out++ = '#';
585
586
87.7M
    if (val < 100) {
587
238
        len = (val < 10) ? 1 : 2;
588
87.7M
    } else if (val < 10000) {
589
87.7M
        len = (val < 1000) ? 3 : 4;
590
87.7M
    } else if (val < 1000000) {
591
3.10k
        len = (val < 100000) ? 5 : 6;
592
3.10k
    } else {
593
285
        len = 7;
594
285
    }
595
596
365M
    for (i = len - 1; i >= 0; i--) {
597
277M
        out[i] = '0' + val % 10;
598
277M
        val /= 10;
599
277M
    }
600
601
87.7M
    out[len] = ';';
602
603
87.7M
    return(len + 3);
604
87.7M
}
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
50.4k
xmlEscapeText(const xmlChar *text, int flags) {
634
50.4k
    const xmlChar *cur;
635
50.4k
    xmlChar *buffer;
636
50.4k
    xmlChar *out;
637
50.4k
    const xmlChar *unescaped;
638
50.4k
    size_t size = 50;
639
640
50.4k
    buffer = xmlMalloc(size + 1);
641
50.4k
    if (buffer == NULL)
642
84
        return(NULL);
643
50.3k
    out = buffer;
644
645
50.3k
    cur = text;
646
50.3k
    unescaped = cur;
647
648
172k
    while (*cur != '\0') {
649
122k
        char buf[12];
650
122k
  const xmlChar *end;
651
122k
        const xmlChar *repl;
652
122k
        size_t used;
653
122k
        size_t replSize;
654
122k
        size_t unescapedSize;
655
122k
        size_t totalSize;
656
122k
        int chunkSize = 1;
657
122k
        int c;
658
659
        /* accelerator */
660
309M
  while (1) {
661
309M
            c = *cur;
662
663
309M
            if (c < 0x80) {
664
162M
                if (!xmlEscapeSafe[*cur])
665
122k
                    break;
666
162M
            } else {
667
146M
               if (flags & XML_ESCAPE_NON_ASCII)
668
0
                   break;
669
146M
            }
670
309M
            cur += 1;
671
309M
        }
672
673
122k
        if (c == 0) {
674
44.6k
            chunkSize = 0;
675
44.6k
            repl = BAD_CAST "";
676
44.6k
            replSize = 0;
677
77.8k
        } else if (c == '<') {
678
      /*
679
       * Special handling of server side include in HTML attributes
680
       */
681
9.58k
      if ((flags & XML_ESCAPE_HTML) && (flags & XML_ESCAPE_ATTR) &&
682
7.00k
          (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
683
753
          ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
684
617
                chunkSize = (end - cur) + 3;
685
617
                repl = cur;
686
617
                replSize = chunkSize;
687
8.96k
      } else {
688
8.96k
                repl = BAD_CAST "&lt;";
689
8.96k
                replSize = 4;
690
8.96k
            }
691
68.2k
  } else if (c == '>') {
692
27.2k
            repl = BAD_CAST "&gt;";
693
27.2k
            replSize = 4;
694
40.9k
  } 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
7.85k
      if ((flags & XML_ESCAPE_HTML) && (flags & XML_ESCAPE_ATTR) &&
700
5.28k
                (cur[1] == '{') && (end = xmlStrchr(cur, '}'))) {
701
2.49k
                chunkSize = (end - cur) + 1;
702
2.49k
                repl = cur;
703
2.49k
                replSize = chunkSize;
704
5.36k
      } else {
705
5.36k
                repl = BAD_CAST "&amp;";
706
5.36k
                replSize = 5;
707
5.36k
            }
708
33.1k
  } else if ((flags & XML_ESCAPE_QUOT) && (c == '"')) {
709
0
            repl = BAD_CAST "&quot;";
710
0
            replSize = 6;
711
33.1k
  } else if (((flags & XML_ESCAPE_HTML) == 0) && (c == '\r')) {
712
0
      repl = BAD_CAST "&#13;";
713
0
            replSize = 5;
714
33.1k
  } 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
33.1k
  } else if ((flags & (XML_ESCAPE_ALLOW_INVALID | XML_ESCAPE_HTML)) ||
730
0
                   (c >= 0x20) ||
731
33.1k
             (c == '\n') || (c == '\t') || (c == '\r')) {
732
      /* default case, just copy */
733
33.1k
            cur += 1;
734
33.1k
            if (*cur != 0)
735
32.4k
                continue;
736
737
694
            chunkSize = 0;
738
694
            repl = BAD_CAST "";
739
694
            replSize = 0;
740
694
  } else {
741
            /* ignore */
742
0
            repl = BAD_CAST "";
743
0
            replSize = 0;
744
0
        }
745
746
90.0k
        used = out - buffer;
747
90.0k
        unescapedSize = cur - unescaped;
748
90.0k
        totalSize = unescapedSize + replSize;
749
750
90.0k
  cur += chunkSize;
751
752
90.0k
        if (totalSize > size - used) {
753
9.46k
            xmlChar *tmp;
754
9.46k
            int newSize;
755
756
9.46k
            if ((size > (SIZE_MAX - 1) / 2) ||
757
9.46k
                (totalSize > (SIZE_MAX - 1) / 2 - size)) {
758
0
                xmlFree(buffer);
759
0
                return(NULL);
760
0
            }
761
9.46k
            newSize = size + totalSize;
762
9.46k
            if (*cur != 0)
763
6.11k
                newSize *= 2;
764
9.46k
            tmp = xmlRealloc(buffer, newSize + 1);
765
9.46k
            if (tmp == NULL) {
766
8
                xmlFree(buffer);
767
8
                return(NULL);
768
8
            }
769
9.46k
            buffer = tmp;
770
9.46k
            size = newSize;
771
9.46k
            out = buffer + used;
772
9.46k
        }
773
774
90.0k
        memcpy(out, unescaped, unescapedSize);
775
90.0k
        out += unescapedSize;
776
90.0k
        memcpy(out, repl, replSize);
777
90.0k
        out += replSize;
778
779
90.0k
        unescaped = cur;
780
90.0k
    }
781
782
50.3k
    *out = 0;
783
50.3k
    return(buffer);
784
50.3k
}
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
50.4k
                          unsigned flags) {
802
50.4k
    if (input == NULL)
803
0
        return(NULL);
804
805
50.4k
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
806
50.4k
        flags |= XML_ESCAPE_HTML;
807
0
    else if ((doc == NULL) || (doc->encoding == NULL))
808
0
        flags |= XML_ESCAPE_NON_ASCII;
809
810
50.4k
    return(xmlEscapeText(input, flags));
811
50.4k
}
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
37.1k
xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
831
37.1k
    return xmlEncodeEntitiesInternal(doc, input, 0);
832
37.1k
}
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
187k
xmlFreeEntityWrapper(void *entity, const xmlChar *name ATTRIBUTE_UNUSED) {
877
187k
    if (entity != NULL)
878
187k
  xmlFreeEntity((xmlEntityPtr) entity);
879
187k
}
880
881
/**
882
 * xmlFreeEntitiesTable:
883
 * @table:  An entity table
884
 *
885
 * Deallocate the memory used by an entities hash table.
886
 */
887
void
888
84.8k
xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
889
84.8k
    xmlHashFree(table, xmlFreeEntityWrapper);
890
84.8k
}
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
109k
xmlCopyEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
902
109k
    xmlEntityPtr ent = (xmlEntityPtr) payload;
903
109k
    xmlEntityPtr cur;
904
905
109k
    cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
906
109k
    if (cur == NULL)
907
3
  return(NULL);
908
109k
    memset(cur, 0, sizeof(xmlEntity));
909
109k
    cur->type = XML_ENTITY_DECL;
910
911
109k
    cur->etype = ent->etype;
912
109k
    if (ent->name != NULL) {
913
109k
  cur->name = xmlStrdup(ent->name);
914
109k
        if (cur->name == NULL)
915
5
            goto error;
916
109k
    }
917
109k
    if (ent->ExternalID != NULL) {
918
0
  cur->ExternalID = xmlStrdup(ent->ExternalID);
919
0
        if (cur->ExternalID == NULL)
920
0
            goto error;
921
0
    }
922
109k
    if (ent->SystemID != NULL) {
923
31.1k
  cur->SystemID = xmlStrdup(ent->SystemID);
924
31.1k
        if (cur->SystemID == NULL)
925
1
            goto error;
926
31.1k
    }
927
109k
    if (ent->content != NULL) {
928
78.1k
  cur->content = xmlStrdup(ent->content);
929
78.1k
        if (cur->content == NULL)
930
5
            goto error;
931
78.1k
    }
932
109k
    if (ent->orig != NULL) {
933
78.1k
  cur->orig = xmlStrdup(ent->orig);
934
78.1k
        if (cur->orig == NULL)
935
3
            goto error;
936
78.1k
    }
937
109k
    if (ent->URI != NULL) {
938
30.2k
  cur->URI = xmlStrdup(ent->URI);
939
30.2k
        if (cur->URI == NULL)
940
1
            goto error;
941
30.2k
    }
942
109k
    return(cur);
943
944
15
error:
945
15
    xmlFreeEntity(cur);
946
15
    return(NULL);
947
109k
}
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
37.6k
xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
959
37.6k
    return(xmlHashCopySafe(table, xmlCopyEntity, xmlFreeEntityWrapper));
960
37.6k
}
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 */