Coverage Report

Created: 2026-11-11 06:09

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
 * Author: Daniel Veillard
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
#include "private/io.h"
32
#include "private/tree.h"
33
34
#ifndef SIZE_MAX
35
  #define SIZE_MAX ((size_t) -1)
36
#endif
37
38
/*
39
 * The XML predefined entities.
40
 */
41
42
static xmlEntity xmlEntityLt = {
43
    NULL, XML_ENTITY_DECL, BAD_CAST "lt",
44
    NULL, NULL, NULL, NULL, NULL, NULL,
45
    BAD_CAST "<", BAD_CAST "<", 1,
46
    XML_INTERNAL_PREDEFINED_ENTITY,
47
    NULL, NULL, NULL, NULL, 0, 0, 0
48
};
49
static xmlEntity xmlEntityGt = {
50
    NULL, XML_ENTITY_DECL, BAD_CAST "gt",
51
    NULL, NULL, NULL, NULL, NULL, NULL,
52
    BAD_CAST ">", BAD_CAST ">", 1,
53
    XML_INTERNAL_PREDEFINED_ENTITY,
54
    NULL, NULL, NULL, NULL, 0, 0, 0
55
};
56
static xmlEntity xmlEntityAmp = {
57
    NULL, XML_ENTITY_DECL, BAD_CAST "amp",
58
    NULL, NULL, NULL, NULL, NULL, NULL,
59
    BAD_CAST "&", BAD_CAST "&", 1,
60
    XML_INTERNAL_PREDEFINED_ENTITY,
61
    NULL, NULL, NULL, NULL, 0, 0, 0
62
};
63
static xmlEntity xmlEntityQuot = {
64
    NULL, XML_ENTITY_DECL, BAD_CAST "quot",
65
    NULL, NULL, NULL, NULL, NULL, NULL,
66
    BAD_CAST "\"", BAD_CAST "\"", 1,
67
    XML_INTERNAL_PREDEFINED_ENTITY,
68
    NULL, NULL, NULL, NULL, 0, 0, 0
69
};
70
static xmlEntity xmlEntityApos = {
71
    NULL, XML_ENTITY_DECL, BAD_CAST "apos",
72
    NULL, NULL, NULL, NULL, NULL, NULL,
73
    BAD_CAST "'", BAD_CAST "'", 1,
74
    XML_INTERNAL_PREDEFINED_ENTITY,
75
    NULL, NULL, NULL, NULL, 0, 0, 0
76
};
77
78
/**
79
 * Frees the entity.
80
 *
81
 * @param entity  an entity
82
 */
83
void
84
xmlFreeEntity(xmlEntity *entity)
85
398k
{
86
398k
    xmlDictPtr dict = NULL;
87
88
398k
    if (entity == NULL)
89
682
        return;
90
91
397k
    if (entity->doc != NULL)
92
392k
        dict = entity->doc->dict;
93
94
95
397k
    if ((entity->children) &&
96
23.9k
        (entity == (xmlEntityPtr) entity->children->parent))
97
23.9k
        xmlFreeNodeList(entity->children);
98
397k
    if ((entity->name != NULL) &&
99
397k
        ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
100
201k
        xmlFree((char *) entity->name);
101
397k
    if (entity->ExternalID != NULL)
102
31.1k
        xmlFree((char *) entity->ExternalID);
103
397k
    if (entity->SystemID != NULL)
104
80.1k
        xmlFree((char *) entity->SystemID);
105
397k
    if (entity->URI != NULL)
106
57.2k
        xmlFree((char *) entity->URI);
107
397k
    if (entity->content != NULL)
108
301k
        xmlFree((char *) entity->content);
109
397k
    if (entity->orig != NULL)
110
119k
        xmlFree((char *) entity->orig);
111
397k
    xmlFree(entity);
112
397k
}
113
114
/*
115
 * internal routine doing the entity node structures allocations
116
 */
117
static xmlEntityPtr
118
xmlCreateEntity(xmlDocPtr doc, const xmlChar *name, int type,
119
          const xmlChar *publicId, const xmlChar *systemId,
120
381k
          const xmlChar *content) {
121
381k
    xmlEntityPtr ret;
122
123
381k
    ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
124
381k
    if (ret == NULL)
125
87
  return(NULL);
126
381k
    memset(ret, 0, sizeof(xmlEntity));
127
381k
    ret->doc = doc;
128
381k
    ret->type = XML_ENTITY_DECL;
129
130
    /*
131
     * fill the structure.
132
     */
133
381k
    ret->etype = (xmlEntityType) type;
134
381k
    if ((doc == NULL) || (doc->dict == NULL))
135
185k
  ret->name = xmlStrdup(name);
136
196k
    else
137
196k
        ret->name = xmlDictLookup(doc->dict, name, -1);
138
381k
    if (ret->name == NULL)
139
53
        goto error;
140
381k
    if (publicId != NULL) {
141
27.1k
        ret->ExternalID = xmlStrdup(publicId);
142
27.1k
        if (ret->ExternalID == NULL)
143
15
            goto error;
144
27.1k
    }
145
381k
    if (systemId != NULL) {
146
71.0k
        ret->SystemID = xmlStrdup(systemId);
147
71.0k
        if (ret->SystemID == NULL)
148
23
            goto error;
149
71.0k
    }
150
381k
    if (content != NULL) {
151
292k
        ret->length = xmlStrlen(content);
152
292k
  ret->content = xmlStrndup(content, ret->length);
153
292k
        if (ret->content == NULL)
154
62
            goto error;
155
292k
     } else {
156
89.0k
        ret->length = 0;
157
89.0k
        ret->content = NULL;
158
89.0k
    }
159
381k
    ret->URI = NULL; /* to be computed by the layer knowing
160
      the defining entity */
161
381k
    ret->orig = NULL;
162
163
381k
    return(ret);
164
165
153
error:
166
153
    xmlFreeEntity(ret);
167
153
    return(NULL);
168
381k
}
169
170
/**
171
 * Register a new entity for this document.
172
 *
173
 * @since 2.13.0
174
 *
175
 * @param doc  the document
176
 * @param extSubset  add to the external or internal subset
177
 * @param name  the entity name
178
 * @param type  an xmlEntityType value
179
 * @param publicId  the publid identifier (optional)
180
 * @param systemId  the system identifier (URL) (optional)
181
 * @param content  the entity content
182
 * @param out  pointer to resulting entity (optional)
183
 * @returns an xmlParserErrors error code.
184
 */
185
int
186
xmlAddEntity(xmlDoc *doc, int extSubset, const xmlChar *name, int type,
187
    const xmlChar *publicId, const xmlChar *systemId,
188
407k
    const xmlChar *content, xmlEntity **out) {
189
407k
    xmlDtdPtr dtd;
190
407k
    xmlDictPtr dict = NULL;
191
407k
    xmlEntitiesTablePtr table = NULL;
192
407k
    xmlEntityPtr ret, predef;
193
407k
    int res;
194
195
407k
    if (out != NULL)
196
407k
        *out = NULL;
197
407k
    if ((doc == NULL) || (name == NULL))
198
2.48k
  return(XML_ERR_ARGUMENT);
199
404k
    dict = doc->dict;
200
201
404k
    if (extSubset)
202
39.0k
        dtd = doc->extSubset;
203
365k
    else
204
365k
        dtd = doc->intSubset;
205
404k
    if (dtd == NULL)
206
413
        return(XML_DTD_NO_DTD);
207
208
404k
    switch (type) {
209
199k
        case XML_INTERNAL_GENERAL_ENTITY:
210
246k
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
211
253k
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
212
253k
            predef = xmlGetPredefinedEntity(name);
213
253k
            if (predef != NULL) {
214
28.5k
                int valid = 0;
215
216
                /* 4.6 Predefined Entities */
217
28.5k
                if ((type == XML_INTERNAL_GENERAL_ENTITY) &&
218
25.8k
                    (content != NULL)) {
219
22.1k
                    int c = predef->content[0];
220
221
22.1k
                    if (((content[0] == c) && (content[1] == 0)) &&
222
4.65k
                        ((c == '>') || (c == '\'') || (c == '"'))) {
223
3.42k
                        valid = 1;
224
18.6k
                    } else if ((content[0] == '&') && (content[1] == '#')) {
225
4.45k
                        if (content[2] == 'x') {
226
2.22k
                            xmlChar *hex = BAD_CAST "0123456789ABCDEF";
227
2.22k
                            xmlChar ref[] = "00;";
228
229
2.22k
                            ref[0] = hex[c / 16 % 16];
230
2.22k
                            ref[1] = hex[c % 16];
231
2.22k
                            if (xmlStrcasecmp(&content[3], ref) == 0)
232
984
                                valid = 1;
233
2.23k
                        } else {
234
2.23k
                            xmlChar ref[] = "00;";
235
236
2.23k
                            ref[0] = '0' + c / 10 % 10;
237
2.23k
                            ref[1] = '0' + c % 10;
238
2.23k
                            if (xmlStrEqual(&content[2], ref))
239
1.03k
                                valid = 1;
240
2.23k
                        }
241
4.45k
                    }
242
22.1k
                }
243
28.5k
                if (!valid)
244
23.0k
                    return(XML_ERR_REDECL_PREDEF_ENTITY);
245
28.5k
            }
246
230k
      if (dtd->entities == NULL) {
247
62.3k
    dtd->entities = xmlHashCreateDict(0, dict);
248
62.3k
                if (dtd->entities == NULL)
249
79
                    return(XML_ERR_NO_MEMORY);
250
62.3k
            }
251
230k
      table = dtd->entities;
252
230k
      break;
253
112k
        case XML_INTERNAL_PARAMETER_ENTITY:
254
150k
        case XML_EXTERNAL_PARAMETER_ENTITY:
255
150k
      if (dtd->pentities == NULL) {
256
35.1k
    dtd->pentities = xmlHashCreateDict(0, dict);
257
35.1k
                if (dtd->pentities == NULL)
258
20
                    return(XML_ERR_NO_MEMORY);
259
35.1k
            }
260
150k
      table = dtd->pentities;
261
150k
      break;
262
394
        default:
263
394
      return(XML_ERR_ARGUMENT);
264
404k
    }
265
380k
    ret = xmlCreateEntity(dtd->doc, name, type, publicId, systemId, content);
266
380k
    if (ret == NULL)
267
238
        return(XML_ERR_NO_MEMORY);
268
269
380k
    res = xmlHashAdd(table, name, ret);
270
380k
    if (res < 0) {
271
109
        xmlFreeEntity(ret);
272
109
        return(XML_ERR_NO_MEMORY);
273
380k
    } else if (res == 0) {
274
  /*
275
   * entity was already defined at another level.
276
   */
277
203k
        xmlFreeEntity(ret);
278
203k
  return(XML_WAR_ENTITY_REDEFINED);
279
203k
    }
280
281
    /*
282
     * Link it to the DTD
283
     */
284
176k
    ret->parent = dtd;
285
176k
    ret->doc = dtd->doc;
286
176k
    if (dtd->last == NULL) {
287
81.9k
  dtd->children = dtd->last = (xmlNodePtr) ret;
288
94.8k
    } else {
289
94.8k
  dtd->last->next = (xmlNodePtr) ret;
290
94.8k
  ret->prev = dtd->last;
291
94.8k
  dtd->last = (xmlNodePtr) ret;
292
94.8k
    }
293
294
176k
    if (out != NULL)
295
176k
        *out = ret;
296
176k
    return(0);
297
380k
}
298
299
/**
300
 * Look up a predefined entity.
301
 *
302
 * @param name  the entity name
303
 * @returns the entity, or NULL if not found.
304
 */
305
xmlEntity *
306
17.8M
xmlGetPredefinedEntity(const xmlChar *name) {
307
17.8M
    if (name == NULL) return(NULL);
308
17.8M
    switch (name[0]) {
309
1.46M
        case 'l':
310
1.46M
      if (xmlStrEqual(name, BAD_CAST "lt"))
311
605k
          return(&xmlEntityLt);
312
859k
      break;
313
2.13M
        case 'g':
314
2.13M
      if (xmlStrEqual(name, BAD_CAST "gt"))
315
1.97M
          return(&xmlEntityGt);
316
162k
      break;
317
1.70M
        case 'a':
318
1.70M
      if (xmlStrEqual(name, BAD_CAST "amp"))
319
220k
          return(&xmlEntityAmp);
320
1.48M
      if (xmlStrEqual(name, BAD_CAST "apos"))
321
381k
          return(&xmlEntityApos);
322
1.10M
      break;
323
1.10M
        case 'q':
324
432k
      if (xmlStrEqual(name, BAD_CAST "quot"))
325
421k
          return(&xmlEntityQuot);
326
10.6k
      break;
327
12.1M
  default:
328
12.1M
      break;
329
17.8M
    }
330
14.2M
    return(NULL);
331
17.8M
}
332
333
/**
334
 * Add a new entity to the document's external subset.
335
 *
336
 * #xmlAddEntity offers better error handling.
337
 *
338
 * @param doc  the document
339
 * @param name  the entity name
340
 * @param type  an xmlEntityType value
341
 * @param publicId  the publid identifier (optional)
342
 * @param systemId  the system identifier (URL) (optional)
343
 * @param content  the entity content
344
 * @returns a pointer to the entity or NULL in case of error
345
 */
346
xmlEntity *
347
xmlAddDtdEntity(xmlDoc *doc, const xmlChar *name, int type,
348
          const xmlChar *publicId, const xmlChar *systemId,
349
1.44k
    const xmlChar *content) {
350
1.44k
    xmlEntityPtr ret;
351
352
1.44k
    xmlAddEntity(doc, 1, name, type, publicId, systemId, content, &ret);
353
1.44k
    return(ret);
354
1.44k
}
355
356
/**
357
 * Add a new entity to the document's internal subset.
358
 *
359
 * #xmlAddEntity offers better error handling.
360
 *
361
 * @param doc  the document
362
 * @param name  the entity name
363
 * @param type  an xmlEntityType value
364
 * @param publicId  the publid identifier (optional)
365
 * @param systemId  the system identifier (URL) (optional)
366
 * @param content  the entity content
367
 * @returns a pointer to the entity or NULL in case of error
368
 */
369
xmlEntity *
370
xmlAddDocEntity(xmlDoc *doc, const xmlChar *name, int type,
371
          const xmlChar *publicId, const xmlChar *systemId,
372
6.36k
          const xmlChar *content) {
373
6.36k
    xmlEntityPtr ret;
374
375
6.36k
    xmlAddEntity(doc, 0, name, type, publicId, systemId, content, &ret);
376
6.36k
    return(ret);
377
6.36k
}
378
379
/**
380
 * Create a new entity.
381
 *
382
 * Like #xmlAddDocEntity, but if `doc` is NULL or has no internal
383
 * subset defined, an unlinked entity will be returned. It is then
384
 * the responsibility of the caller to link it to the document later
385
 * or free it when not needed anymore.
386
 *
387
 * @param doc  the document (optional)
388
 * @param name  the entity name
389
 * @param type  an xmlEntityType value
390
 * @param publicId  the publid identifier (optional)
391
 * @param systemId  the system identifier (URL) (optional)
392
 * @param content  the entity content
393
 * @returns a pointer to the entity or NULL in case of error
394
 */
395
xmlEntity *
396
xmlNewEntity(xmlDoc *doc, const xmlChar *name, int type,
397
       const xmlChar *publicId, const xmlChar *systemId,
398
5.23k
       const xmlChar *content) {
399
5.23k
    if ((doc != NULL) && (doc->intSubset != NULL)) {
400
3.92k
  return(xmlAddDocEntity(doc, name, type, publicId, systemId, content));
401
3.92k
    }
402
1.30k
    if (name == NULL)
403
680
        return(NULL);
404
625
    return(xmlCreateEntity(doc, name, type, publicId, systemId, content));
405
1.30k
}
406
407
/**
408
 * Look up an entity in a table.
409
 *
410
 * @param table  an entity table
411
 * @param name  the entity name
412
 * @returns a pointer to the entity or NULL if not found.
413
 */
414
static xmlEntityPtr
415
8.51M
xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
416
8.51M
    return((xmlEntityPtr) xmlHashLookup(table, name));
417
8.51M
}
418
419
/**
420
 * Look up a paramater entity in the internal and external subset
421
 * of `doc`.
422
 *
423
 * @param doc  the document
424
 * @param name  the entity name
425
 * @returns a pointer to the entity or NULL if not found.
426
 */
427
xmlEntity *
428
1.54M
xmlGetParameterEntity(xmlDoc *doc, const xmlChar *name) {
429
1.54M
    xmlEntitiesTablePtr table;
430
1.54M
    xmlEntityPtr ret;
431
432
1.54M
    if (doc == NULL)
433
1.35k
  return(NULL);
434
1.54M
    if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
435
1.41M
  table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
436
1.41M
  ret = xmlGetEntityFromTable(table, name);
437
1.41M
  if (ret != NULL)
438
1.15M
      return(ret);
439
1.41M
    }
440
386k
    if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
441
124k
  table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
442
124k
  return(xmlGetEntityFromTable(table, name));
443
124k
    }
444
261k
    return(NULL);
445
386k
}
446
447
/**
448
 * Look up a general entity in the external subset of `doc`.
449
 *
450
 * @param doc  the document
451
 * @param name  the entity name
452
 * @returns a pointer to the entity or NULL if not found.
453
 */
454
xmlEntity *
455
673
xmlGetDtdEntity(xmlDoc *doc, const xmlChar *name) {
456
673
    xmlEntitiesTablePtr table;
457
458
673
    if (doc == NULL)
459
385
  return(NULL);
460
288
    if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
461
114
  table = (xmlEntitiesTablePtr) doc->extSubset->entities;
462
114
  return(xmlGetEntityFromTable(table, name));
463
114
    }
464
174
    return(NULL);
465
288
}
466
467
/**
468
 * Look up a general entity in the internal and external subset
469
 * of `doc`. Also checks for predefined entities.
470
 *
471
 * @param doc  the document referencing the entity
472
 * @param name  the entity name
473
 * @returns a pointer to the entity or NULL if not found.
474
 */
475
xmlEntity *
476
9.15M
xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
477
9.15M
    xmlEntityPtr cur;
478
9.15M
    xmlEntitiesTablePtr table;
479
480
9.15M
    if (doc != NULL) {
481
9.11M
  if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
482
6.91M
      table = (xmlEntitiesTablePtr) doc->intSubset->entities;
483
6.91M
      cur = xmlGetEntityFromTable(table, name);
484
6.91M
      if (cur != NULL)
485
6.02M
    return(cur);
486
6.91M
  }
487
3.08M
  if (doc->standalone != 1) {
488
3.01M
      if ((doc->extSubset != NULL) &&
489
1.87M
    (doc->extSubset->entities != NULL)) {
490
62.8k
    table = (xmlEntitiesTablePtr) doc->extSubset->entities;
491
62.8k
    cur = xmlGetEntityFromTable(table, name);
492
62.8k
    if (cur != NULL)
493
24.4k
        return(cur);
494
62.8k
      }
495
3.01M
  }
496
3.08M
    }
497
3.10M
    return(xmlGetPredefinedEntity(name));
498
9.15M
}
499
500
/**
501
 * Replace special characters with predefined entities or numeric
502
 * character references.
503
 *
504
 * If `doc` is NULL or an XML document, replaces `<`, `>` and `&`
505
 * with predefined entities. Carriage return is replaced with
506
 * `&#13;`. If `doc` or its encoding are NULL, non-ASCII
507
 * characters are replaced with a hexadecimal character reference.
508
 *
509
 * If `doc` is an HTML document, follows the HTML serialization
510
 * rules.
511
 *
512
 * Silently removes some invalid characters like ASCII control
513
 * codes.
514
 *
515
 * See #xmlEncodeSpecialChars for an alternative.
516
 *
517
 * @param doc  the document containing the string (optional)
518
 * @param input  A string to convert to XML.
519
 * @returns a newly allocated string with substitutions.
520
 */
521
xmlChar *
522
3.12k
xmlEncodeEntitiesReentrant(xmlDoc *doc, const xmlChar *input) {
523
3.12k
    int flags = 0;
524
525
3.12k
    if (input == NULL)
526
630
        return(NULL);
527
528
2.49k
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
529
1.38k
        flags |= XML_ESCAPE_HTML;
530
1.10k
    else if ((doc == NULL) || (doc->encoding == NULL))
531
858
        flags |= XML_ESCAPE_NON_ASCII;
532
533
2.49k
    return(xmlEscapeText(input, flags));
534
3.12k
}
535
536
/**
537
 * Replace special characters with predefined entities or numeric
538
 * character references.
539
 *
540
 * Replaces `<`, `>`, `&` and `"` with predefined entities. Carriage
541
 * return is replaced with `&#13;`.
542
 *
543
 * @param doc  unused
544
 * @param input  A string to convert to XML.
545
 * @returns a newly allocated string with substitutions.
546
 */
547
xmlChar *
548
xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED,
549
2.76k
                      const xmlChar *input) {
550
2.76k
    if (input == NULL)
551
802
        return(NULL);
552
553
1.96k
    return(xmlEscapeText(input, XML_ESCAPE_QUOT));
554
2.76k
}
555
556
/**
557
 * Create and initialize an empty entities hash table.
558
 *
559
 * @deprecated Internal function, don't use.
560
 *
561
 * @returns the xmlEntitiesTable just created or NULL in case of error.
562
 */
563
xmlEntitiesTable *
564
0
xmlCreateEntitiesTable(void) {
565
0
    return((xmlEntitiesTablePtr) xmlHashCreate(0));
566
0
}
567
568
/**
569
 * Deallocate the memory used by an entities in the hash table.
570
 *
571
 * @param entity  An entity
572
 * @param name  its name
573
 */
574
static void
575
193k
xmlFreeEntityWrapper(void *entity, const xmlChar *name ATTRIBUTE_UNUSED) {
576
193k
    if (entity != NULL)
577
193k
  xmlFreeEntity((xmlEntityPtr) entity);
578
193k
}
579
580
/**
581
 * Deallocate the memory used by an entities hash table.
582
 *
583
 * @deprecated Internal function, don't use.
584
 *
585
 * @param table  An entity table
586
 */
587
void
588
107k
xmlFreeEntitiesTable(xmlEntitiesTable *table) {
589
107k
    xmlHashFree(table, xmlFreeEntityWrapper);
590
107k
}
591
592
/**
593
 * Build a copy of an entity
594
 *
595
 * @param payload  An entity
596
 * @param name  unused
597
 * @returns the new xmlEntities or NULL in case of error.
598
 */
599
static void *
600
16.5k
xmlCopyEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
601
16.5k
    xmlEntityPtr ent = (xmlEntityPtr) payload;
602
16.5k
    xmlEntityPtr cur;
603
604
16.5k
    cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
605
16.5k
    if (cur == NULL)
606
25
  return(NULL);
607
16.5k
    memset(cur, 0, sizeof(xmlEntity));
608
16.5k
    cur->type = XML_ENTITY_DECL;
609
610
16.5k
    cur->etype = ent->etype;
611
16.5k
    if (ent->name != NULL) {
612
16.5k
  cur->name = xmlStrdup(ent->name);
613
16.5k
        if (cur->name == NULL)
614
23
            goto error;
615
16.5k
    }
616
16.4k
    if (ent->ExternalID != NULL) {
617
4.00k
  cur->ExternalID = xmlStrdup(ent->ExternalID);
618
4.00k
        if (cur->ExternalID == NULL)
619
9
            goto error;
620
4.00k
    }
621
16.4k
    if (ent->SystemID != NULL) {
622
9.12k
  cur->SystemID = xmlStrdup(ent->SystemID);
623
9.12k
        if (cur->SystemID == NULL)
624
15
            goto error;
625
9.12k
    }
626
16.4k
    if (ent->content != NULL) {
627
8.38k
  cur->content = xmlStrdup(ent->content);
628
8.38k
        if (cur->content == NULL)
629
21
            goto error;
630
8.38k
    }
631
16.4k
    if (ent->orig != NULL) {
632
6.27k
  cur->orig = xmlStrdup(ent->orig);
633
6.27k
        if (cur->orig == NULL)
634
17
            goto error;
635
6.27k
    }
636
16.4k
    if (ent->URI != NULL) {
637
6.69k
  cur->URI = xmlStrdup(ent->URI);
638
6.69k
        if (cur->URI == NULL)
639
17
            goto error;
640
6.69k
    }
641
    /* Handle XML_TEXT_NODE children for XML_ENTITY_DECL */
642
16.3k
    if (ent->children != NULL) {
643
3.22k
        cur->children = xmlStaticCopyNodeList(ent->children, cur->doc, (xmlNodePtr)cur);
644
        /* Update last pointers */
645
3.22k
        cur->last = cur->children;
646
11.0k
        while (cur->last && cur->last->next) cur->last = cur->last->next;
647
3.22k
    }
648
649
16.3k
    return(cur);
650
651
102
error:
652
102
    xmlFreeEntity(cur);
653
102
    return(NULL);
654
16.4k
}
655
656
/**
657
 * Build a copy of an entity table.
658
 *
659
 * @deprecated Internal function, don't use.
660
 *
661
 * @param table  An entity table
662
 * @returns the new xmlEntitiesTable or NULL in case of error.
663
 */
664
xmlEntitiesTable *
665
10.0k
xmlCopyEntitiesTable(xmlEntitiesTable *table) {
666
10.0k
    return(xmlHashCopySafe(table, xmlCopyEntity, xmlFreeEntityWrapper));
667
10.0k
}
668
669
#ifdef LIBXML_OUTPUT_ENABLED
670
671
/**
672
 * This will dump the content of the entity table as an XML DTD
673
 * definition.
674
 *
675
 * @deprecated Internal function, don't use.
676
 *
677
 * @param buf  An XML buffer.
678
 * @param ent  An entity table
679
 */
680
void
681
4.44k
xmlDumpEntityDecl(xmlBuffer *buf, xmlEntity *ent) {
682
4.44k
    xmlSaveCtxtPtr save;
683
684
4.44k
    if ((buf == NULL) || (ent == NULL))
685
4.16k
        return;
686
687
283
    save = xmlSaveToBuffer(buf, NULL, 0);
688
283
    xmlSaveTree(save, (xmlNodePtr) ent);
689
283
    if (xmlSaveFinish(save) != XML_ERR_OK)
690
2
        xmlFree(xmlBufferDetach(buf));
691
283
}
692
693
/**
694
 * When using the hash table scan function, arguments need to be
695
 * reversed.
696
 *
697
 * @param ent  an entity table
698
 * @param save  a save context
699
 * @param name  unused
700
 */
701
static void
702
xmlDumpEntityDeclScan(void *ent, void *save,
703
555
                      const xmlChar *name ATTRIBUTE_UNUSED) {
704
555
    xmlSaveTree(save, ent);
705
555
}
706
707
/**
708
 * This will dump the content of the entity table as an XML DTD
709
 * definition.
710
 *
711
 * @deprecated Internal function, don't use.
712
 *
713
 * @param buf  An XML buffer.
714
 * @param table  An entity table
715
 */
716
void
717
4.41k
xmlDumpEntitiesTable(xmlBuffer *buf, xmlEntitiesTable *table) {
718
4.41k
    xmlSaveCtxtPtr save;
719
720
4.41k
    if ((buf == NULL) || (table == NULL))
721
4.10k
        return;
722
723
313
    save = xmlSaveToBuffer(buf, NULL, 0);
724
313
    xmlHashScan(table, xmlDumpEntityDeclScan, save);
725
313
    if (xmlSaveFinish(save) != XML_ERR_OK)
726
1
        xmlFree(xmlBufferDetach(buf));
727
313
}
728
#endif /* LIBXML_OUTPUT_ENABLED */