blob: a72afb3459d2aad828aa9d4e2bbf80950eac3095 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002 * entities.c : implementation for the XML entities handling
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
Daniel Veillard34ce8be2002-03-18 19:37:11 +00009#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011
Owen Taylor3473f882001-02-23 17:55:21 +000012#include <string.h>
13#ifdef HAVE_STDLIB_H
14#include <stdlib.h>
15#endif
16#include <libxml/xmlmemory.h>
17#include <libxml/hash.h>
18#include <libxml/entities.h>
19#include <libxml/parser.h>
William M. Brack76e95df2003-10-18 16:20:14 +000020#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000021#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000022#include <libxml/globals.h>
Daniel Veillard7da92702005-01-23 20:15:53 +000023#include <libxml/dict.h>
Owen Taylor3473f882001-02-23 17:55:21 +000024
Daniel Veillard76516062012-09-11 14:02:08 +080025#include "save.h"
26
Owen Taylor3473f882001-02-23 17:55:21 +000027/*
28 * The XML predefined entities.
29 */
30
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000031static xmlEntity xmlEntityLt = {
32 NULL, XML_ENTITY_DECL, BAD_CAST "lt",
Daniel Veillardf8e3db02012-09-11 13:26:36 +080033 NULL, NULL, NULL, NULL, NULL, NULL,
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000034 BAD_CAST "<", BAD_CAST "<", 1,
35 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000036 NULL, NULL, NULL, NULL, 0, 1
Owen Taylor3473f882001-02-23 17:55:21 +000037};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000038static xmlEntity xmlEntityGt = {
39 NULL, XML_ENTITY_DECL, BAD_CAST "gt",
Daniel Veillardf8e3db02012-09-11 13:26:36 +080040 NULL, NULL, NULL, NULL, NULL, NULL,
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000041 BAD_CAST ">", BAD_CAST ">", 1,
42 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000043 NULL, NULL, NULL, NULL, 0, 1
Owen Taylor3473f882001-02-23 17:55:21 +000044};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000045static xmlEntity xmlEntityAmp = {
46 NULL, XML_ENTITY_DECL, BAD_CAST "amp",
Daniel Veillardf8e3db02012-09-11 13:26:36 +080047 NULL, NULL, NULL, NULL, NULL, NULL,
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000048 BAD_CAST "&", BAD_CAST "&", 1,
49 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000050 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000051};
52static xmlEntity xmlEntityQuot = {
53 NULL, XML_ENTITY_DECL, BAD_CAST "quot",
Daniel Veillardf8e3db02012-09-11 13:26:36 +080054 NULL, NULL, NULL, NULL, NULL, NULL,
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000055 BAD_CAST "\"", BAD_CAST "\"", 1,
56 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000057 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000058};
59static xmlEntity xmlEntityApos = {
60 NULL, XML_ENTITY_DECL, BAD_CAST "apos",
Daniel Veillardf8e3db02012-09-11 13:26:36 +080061 NULL, NULL, NULL, NULL, NULL, NULL,
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000062 BAD_CAST "'", BAD_CAST "'", 1,
63 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000064 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000065};
Owen Taylor3473f882001-02-23 17:55:21 +000066
Daniel Veillardce244ad2004-11-05 10:03:46 +000067/**
68 * xmlEntitiesErrMemory:
69 * @extra: extra informations
70 *
71 * Handle an out of memory condition
72 */
73static void
74xmlEntitiesErrMemory(const char *extra)
75{
76 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
77}
78
79/**
80 * xmlEntitiesErr:
81 * @code: the error code
82 * @msg: the message
83 *
84 * Handle an out of memory condition
85 */
86static void
87xmlEntitiesErr(xmlParserErrors code, const char *msg)
88{
Daniel Veillardce244ad2004-11-05 10:03:46 +000089 __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);
90}
91
Owen Taylor3473f882001-02-23 17:55:21 +000092/*
93 * xmlFreeEntity : clean-up an entity record.
94 */
Daniel Veillard7da92702005-01-23 20:15:53 +000095static void
96xmlFreeEntity(xmlEntityPtr entity)
97{
98 xmlDictPtr dict = NULL;
99
100 if (entity == NULL)
101 return;
102
103 if (entity->doc != NULL)
104 dict = entity->doc->dict;
105
Owen Taylor3473f882001-02-23 17:55:21 +0000106
Daniel Veillard2d84a892002-12-30 00:01:08 +0000107 if ((entity->children) && (entity->owner == 1) &&
Daniel Veillard7da92702005-01-23 20:15:53 +0000108 (entity == (xmlEntityPtr) entity->children->parent))
109 xmlFreeNodeList(entity->children);
110 if (dict != NULL) {
111 if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
112 xmlFree((char *) entity->name);
113 if ((entity->ExternalID != NULL) &&
114 (!xmlDictOwns(dict, entity->ExternalID)))
115 xmlFree((char *) entity->ExternalID);
116 if ((entity->SystemID != NULL) &&
117 (!xmlDictOwns(dict, entity->SystemID)))
118 xmlFree((char *) entity->SystemID);
119 if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
120 xmlFree((char *) entity->URI);
121 if ((entity->content != NULL)
122 && (!xmlDictOwns(dict, entity->content)))
123 xmlFree((char *) entity->content);
124 if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
125 xmlFree((char *) entity->orig);
126 } else {
127 if (entity->name != NULL)
128 xmlFree((char *) entity->name);
129 if (entity->ExternalID != NULL)
130 xmlFree((char *) entity->ExternalID);
131 if (entity->SystemID != NULL)
132 xmlFree((char *) entity->SystemID);
133 if (entity->URI != NULL)
134 xmlFree((char *) entity->URI);
135 if (entity->content != NULL)
136 xmlFree((char *) entity->content);
137 if (entity->orig != NULL)
138 xmlFree((char *) entity->orig);
139 }
Owen Taylor3473f882001-02-23 17:55:21 +0000140 xmlFree(entity);
141}
142
143/*
Daniel Veillardaa6de472008-08-25 14:53:31 +0000144 * xmlCreateEntity:
145 *
146 * internal routine doing the entity node strutures allocations
Owen Taylor3473f882001-02-23 17:55:21 +0000147 */
148static xmlEntityPtr
Daniel Veillardaa6de472008-08-25 14:53:31 +0000149xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
150 const xmlChar *ExternalID, const xmlChar *SystemID,
151 const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +0000152 xmlEntityPtr ret;
153
Owen Taylor3473f882001-02-23 17:55:21 +0000154 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
155 if (ret == NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000156 xmlEntitiesErrMemory("xmlCreateEntity: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000157 return(NULL);
158 }
159 memset(ret, 0, sizeof(xmlEntity));
160 ret->type = XML_ENTITY_DECL;
Daniel Veillarda37a6ad2006-10-10 20:05:45 +0000161 ret->checked = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000162
163 /*
164 * fill the structure.
165 */
Owen Taylor3473f882001-02-23 17:55:21 +0000166 ret->etype = (xmlEntityType) type;
Daniel Veillard7da92702005-01-23 20:15:53 +0000167 if (dict == NULL) {
168 ret->name = xmlStrdup(name);
169 if (ExternalID != NULL)
170 ret->ExternalID = xmlStrdup(ExternalID);
171 if (SystemID != NULL)
172 ret->SystemID = xmlStrdup(SystemID);
173 } else {
174 ret->name = xmlDictLookup(dict, name, -1);
175 if (ExternalID != NULL)
176 ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
177 if (SystemID != NULL)
178 ret->SystemID = xmlDictLookup(dict, SystemID, -1);
179 }
Owen Taylor3473f882001-02-23 17:55:21 +0000180 if (content != NULL) {
181 ret->length = xmlStrlen(content);
Daniel Veillard7da92702005-01-23 20:15:53 +0000182 if ((dict != NULL) && (ret->length < 5))
183 ret->content = (xmlChar *)
184 xmlDictLookup(dict, content, ret->length);
185 else
186 ret->content = xmlStrndup(content, ret->length);
Owen Taylor3473f882001-02-23 17:55:21 +0000187 } else {
188 ret->length = 0;
189 ret->content = NULL;
190 }
191 ret->URI = NULL; /* to be computed by the layer knowing
192 the defining entity */
193 ret->orig = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +0000194 ret->owner = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000195
196 return(ret);
197}
198
199/*
200 * xmlAddEntity : register a new entity for an entities table.
201 */
202static xmlEntityPtr
203xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
204 const xmlChar *ExternalID, const xmlChar *SystemID,
205 const xmlChar *content) {
206 xmlDictPtr dict = NULL;
207 xmlEntitiesTablePtr table = NULL;
208 xmlEntityPtr ret;
209
210 if (name == NULL)
211 return(NULL);
212 if (dtd == NULL)
213 return(NULL);
214 if (dtd->doc != NULL)
215 dict = dtd->doc->dict;
216
217 switch (type) {
218 case XML_INTERNAL_GENERAL_ENTITY:
219 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
220 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
221 if (dtd->entities == NULL)
222 dtd->entities = xmlHashCreateDict(0, dict);
223 table = dtd->entities;
224 break;
225 case XML_INTERNAL_PARAMETER_ENTITY:
226 case XML_EXTERNAL_PARAMETER_ENTITY:
227 if (dtd->pentities == NULL)
228 dtd->pentities = xmlHashCreateDict(0, dict);
229 table = dtd->pentities;
230 break;
231 case XML_INTERNAL_PREDEFINED_ENTITY:
232 return(NULL);
233 }
234 if (table == NULL)
235 return(NULL);
236 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
237 if (ret == NULL)
238 return(NULL);
Daniel Veillard7da92702005-01-23 20:15:53 +0000239 ret->doc = dtd->doc;
Owen Taylor3473f882001-02-23 17:55:21 +0000240
241 if (xmlHashAddEntry(table, name, ret)) {
242 /*
243 * entity was already defined at another level.
244 */
245 xmlFreeEntity(ret);
246 return(NULL);
247 }
248 return(ret);
249}
250
251/**
Owen Taylor3473f882001-02-23 17:55:21 +0000252 * xmlGetPredefinedEntity:
253 * @name: the entity name
254 *
255 * Check whether this name is an predefined entity.
256 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000257 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000258 */
259xmlEntityPtr
260xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000261 if (name == NULL) return(NULL);
262 switch (name[0]) {
263 case 'l':
264 if (xmlStrEqual(name, BAD_CAST "lt"))
265 return(&xmlEntityLt);
266 break;
267 case 'g':
268 if (xmlStrEqual(name, BAD_CAST "gt"))
269 return(&xmlEntityGt);
270 break;
271 case 'a':
272 if (xmlStrEqual(name, BAD_CAST "amp"))
273 return(&xmlEntityAmp);
274 if (xmlStrEqual(name, BAD_CAST "apos"))
275 return(&xmlEntityApos);
276 break;
277 case 'q':
278 if (xmlStrEqual(name, BAD_CAST "quot"))
279 return(&xmlEntityQuot);
280 break;
281 default:
282 break;
283 }
284 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000285}
286
287/**
288 * xmlAddDtdEntity:
289 * @doc: the document
290 * @name: the entity name
291 * @type: the entity type XML_xxx_yyy_ENTITY
292 * @ExternalID: the entity external ID if available
293 * @SystemID: the entity system ID if available
294 * @content: the entity content
295 *
296 * Register a new entity for this document DTD external subset.
297 *
298 * Returns a pointer to the entity or NULL in case of error
299 */
300xmlEntityPtr
301xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
302 const xmlChar *ExternalID, const xmlChar *SystemID,
303 const xmlChar *content) {
304 xmlEntityPtr ret;
305 xmlDtdPtr dtd;
306
307 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000308 xmlEntitiesErr(XML_DTD_NO_DOC,
309 "xmlAddDtdEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000310 return(NULL);
311 }
312 if (doc->extSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000313 xmlEntitiesErr(XML_DTD_NO_DTD,
314 "xmlAddDtdEntity: document without external subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000315 return(NULL);
316 }
317 dtd = doc->extSubset;
318 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
319 if (ret == NULL) return(NULL);
320
321 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000322 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000323 */
324 ret->parent = dtd;
325 ret->doc = dtd->doc;
326 if (dtd->last == NULL) {
327 dtd->children = dtd->last = (xmlNodePtr) ret;
328 } else {
329 dtd->last->next = (xmlNodePtr) ret;
330 ret->prev = dtd->last;
331 dtd->last = (xmlNodePtr) ret;
332 }
333 return(ret);
334}
335
336/**
337 * xmlAddDocEntity:
338 * @doc: the document
339 * @name: the entity name
340 * @type: the entity type XML_xxx_yyy_ENTITY
341 * @ExternalID: the entity external ID if available
342 * @SystemID: the entity system ID if available
343 * @content: the entity content
344 *
345 * Register a new entity for this document.
346 *
347 * Returns a pointer to the entity or NULL in case of error
348 */
349xmlEntityPtr
350xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
351 const xmlChar *ExternalID, const xmlChar *SystemID,
352 const xmlChar *content) {
353 xmlEntityPtr ret;
354 xmlDtdPtr dtd;
355
356 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000357 xmlEntitiesErr(XML_DTD_NO_DOC,
358 "xmlAddDocEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000359 return(NULL);
360 }
361 if (doc->intSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000362 xmlEntitiesErr(XML_DTD_NO_DTD,
363 "xmlAddDocEntity: document without internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000364 return(NULL);
365 }
366 dtd = doc->intSubset;
367 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
368 if (ret == NULL) return(NULL);
369
370 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000371 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000372 */
373 ret->parent = dtd;
374 ret->doc = dtd->doc;
375 if (dtd->last == NULL) {
376 dtd->children = dtd->last = (xmlNodePtr) ret;
377 } else {
378 dtd->last->next = (xmlNodePtr) ret;
379 ret->prev = dtd->last;
380 dtd->last = (xmlNodePtr) ret;
381 }
382 return(ret);
383}
384
385/**
Daniel Veillardaa6de472008-08-25 14:53:31 +0000386 * xmlNewEntity:
387 * @doc: the document
388 * @name: the entity name
389 * @type: the entity type XML_xxx_yyy_ENTITY
390 * @ExternalID: the entity external ID if available
391 * @SystemID: the entity system ID if available
392 * @content: the entity content
393 *
394 * Create a new entity, this differs from xmlAddDocEntity() that if
395 * the document is NULL or has no internal subset defined, then an
396 * unlinked entity structure will be returned, it is then the responsability
397 * of the caller to link it to the document later or free it when not needed
398 * anymore.
399 *
400 * Returns a pointer to the entity or NULL in case of error
401 */
402xmlEntityPtr
403xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
404 const xmlChar *ExternalID, const xmlChar *SystemID,
405 const xmlChar *content) {
406 xmlEntityPtr ret;
407 xmlDictPtr dict;
408
409 if ((doc != NULL) && (doc->intSubset != NULL)) {
410 return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
411 }
412 if (doc != NULL)
413 dict = doc->dict;
414 else
415 dict = NULL;
416 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
417 if (ret == NULL)
418 return(NULL);
419 ret->doc = doc;
420 return(ret);
421}
422
423/**
Owen Taylor3473f882001-02-23 17:55:21 +0000424 * xmlGetEntityFromTable:
425 * @table: an entity table
426 * @name: the entity name
427 * @parameter: look for parameter entities
428 *
429 * Do an entity lookup in the table.
430 * returns the corresponding parameter entity, if found.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800431 *
Owen Taylor3473f882001-02-23 17:55:21 +0000432 * Returns A pointer to the entity structure or NULL if not found.
433 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000434static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000435xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
436 return((xmlEntityPtr) xmlHashLookup(table, name));
437}
438
439/**
440 * xmlGetParameterEntity:
441 * @doc: the document referencing the entity
442 * @name: the entity name
443 *
444 * Do an entity lookup in the internal and external subsets and
445 * returns the corresponding parameter entity, if found.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800446 *
Owen Taylor3473f882001-02-23 17:55:21 +0000447 * Returns A pointer to the entity structure or NULL if not found.
448 */
449xmlEntityPtr
450xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
451 xmlEntitiesTablePtr table;
452 xmlEntityPtr ret;
453
Daniel Veillard36065812002-01-24 15:02:46 +0000454 if (doc == NULL)
455 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000456 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
457 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
458 ret = xmlGetEntityFromTable(table, name);
459 if (ret != NULL)
460 return(ret);
461 }
462 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
463 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
464 return(xmlGetEntityFromTable(table, name));
465 }
466 return(NULL);
467}
468
469/**
470 * xmlGetDtdEntity:
471 * @doc: the document referencing the entity
472 * @name: the entity name
473 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000474 * Do an entity lookup in the DTD entity hash table and
Owen Taylor3473f882001-02-23 17:55:21 +0000475 * returns the corresponding entity, if found.
Daniel Veillard36065812002-01-24 15:02:46 +0000476 * Note: the first argument is the document node, not the DTD node.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800477 *
Owen Taylor3473f882001-02-23 17:55:21 +0000478 * Returns A pointer to the entity structure or NULL if not found.
479 */
480xmlEntityPtr
481xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
482 xmlEntitiesTablePtr table;
483
Daniel Veillard36065812002-01-24 15:02:46 +0000484 if (doc == NULL)
485 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000486 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
487 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
488 return(xmlGetEntityFromTable(table, name));
489 }
490 return(NULL);
491}
492
493/**
494 * xmlGetDocEntity:
495 * @doc: the document referencing the entity
496 * @name: the entity name
497 *
498 * Do an entity lookup in the document entity hash table and
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000499 * returns the corresponding entity, otherwise a lookup is done
Owen Taylor3473f882001-02-23 17:55:21 +0000500 * in the predefined entities too.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800501 *
Owen Taylor3473f882001-02-23 17:55:21 +0000502 * Returns A pointer to the entity structure or NULL if not found.
503 */
504xmlEntityPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +0800505xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +0000506 xmlEntityPtr cur;
507 xmlEntitiesTablePtr table;
508
509 if (doc != NULL) {
510 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
511 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
512 cur = xmlGetEntityFromTable(table, name);
513 if (cur != NULL)
514 return(cur);
515 }
Daniel Veillard28757702002-02-18 11:19:30 +0000516 if (doc->standalone != 1) {
517 if ((doc->extSubset != NULL) &&
518 (doc->extSubset->entities != NULL)) {
519 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
520 cur = xmlGetEntityFromTable(table, name);
521 if (cur != NULL)
522 return(cur);
523 }
Owen Taylor3473f882001-02-23 17:55:21 +0000524 }
525 }
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000526 return(xmlGetPredefinedEntity(name));
Owen Taylor3473f882001-02-23 17:55:21 +0000527}
528
529/*
Owen Taylor3473f882001-02-23 17:55:21 +0000530 * Macro used to grow the current buffer.
531 */
532#define growBufferReentrant() { \
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800533 xmlChar *tmp; \
Aron Xubaaf03f2012-07-20 15:41:34 +0800534 size_t new_size = buffer_size * 2; \
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800535 if (new_size < buffer_size) goto mem_error; \
536 tmp = (xmlChar *) xmlRealloc(buffer, new_size); \
537 if (tmp == NULL) goto mem_error; \
538 buffer = tmp; \
539 buffer_size = new_size; \
Owen Taylor3473f882001-02-23 17:55:21 +0000540}
541
Owen Taylor3473f882001-02-23 17:55:21 +0000542/**
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800543 * xmlEncodeEntitiesInternal:
Owen Taylor3473f882001-02-23 17:55:21 +0000544 * @doc: the document containing the string
545 * @input: A string to convert to XML.
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800546 * @attr: are we handling an atrbute value
Owen Taylor3473f882001-02-23 17:55:21 +0000547 *
548 * Do a global encoding of a string, replacing the predefined entities
549 * and non ASCII values with their entities and CharRef counterparts.
550 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
551 * must be deallocated.
552 *
553 * Returns A newly allocated string with the substitution done.
554 */
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800555static xmlChar *
556xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
Owen Taylor3473f882001-02-23 17:55:21 +0000557 const xmlChar *cur = input;
558 xmlChar *buffer = NULL;
559 xmlChar *out = NULL;
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800560 size_t buffer_size = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000561 int html = 0;
562
563 if (input == NULL) return(NULL);
564 if (doc != NULL)
565 html = (doc->type == XML_HTML_DOCUMENT_NODE);
566
567 /*
568 * allocate an translation buffer.
569 */
570 buffer_size = 1000;
571 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
572 if (buffer == NULL) {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800573 xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000574 return(NULL);
575 }
576 out = buffer;
577
578 while (*cur != '\0') {
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800579 size_t indx = out - buffer;
580 if (indx + 100 > buffer_size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000581
582 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000583 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000584 }
585
586 /*
587 * By default one have to encode at least '<', '>', '"' and '&' !
588 */
589 if (*cur == '<') {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800590 const xmlChar *end;
591
592 /*
593 * Special handling of server side include in HTML attributes
594 */
595 if (html && attr &&
596 (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
597 ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
598 while (cur != end) {
599 *out++ = *cur++;
600 indx = out - buffer;
601 if (indx + 100 > buffer_size) {
602 growBufferReentrant();
603 out = &buffer[indx];
604 }
605 }
606 *out++ = *cur++;
607 *out++ = *cur++;
608 *out++ = *cur++;
609 continue;
610 }
Owen Taylor3473f882001-02-23 17:55:21 +0000611 *out++ = '&';
612 *out++ = 'l';
613 *out++ = 't';
614 *out++ = ';';
615 } else if (*cur == '>') {
616 *out++ = '&';
617 *out++ = 'g';
618 *out++ = 't';
619 *out++ = ';';
620 } else if (*cur == '&') {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800621 /*
622 * Special handling of &{...} construct from HTML 4, see
623 * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
624 */
Daniel Veillard76516062012-09-11 14:02:08 +0800625 if (html && attr && (cur[1] == '{') &&
626 (strchr((const char *) cur, '}'))) {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800627 while (*cur != '}') {
628 *out++ = *cur++;
629 indx = out - buffer;
630 if (indx + 100 > buffer_size) {
631 growBufferReentrant();
632 out = &buffer[indx];
633 }
634 }
635 *out++ = *cur++;
636 continue;
637 }
Owen Taylor3473f882001-02-23 17:55:21 +0000638 *out++ = '&';
639 *out++ = 'a';
640 *out++ = 'm';
641 *out++ = 'p';
642 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000643 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Daniel Veillard0046c0f2003-02-23 13:52:30 +0000644 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000645 /*
646 * default case, just copy !
647 */
648 *out++ = *cur;
649 } else if (*cur >= 0x80) {
Daniel Veillard122376b2001-04-24 12:12:30 +0000650 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000651 /*
Daniel Veillard0ab8ce52013-03-28 08:47:42 +0800652 * Bjørn Reese <br@sseusa.com> provided the patch
Owen Taylor3473f882001-02-23 17:55:21 +0000653 xmlChar xc;
654 xc = (*cur & 0x3F) << 6;
655 if (cur[1] != 0) {
656 xc += *(++cur) & 0x3F;
657 *out++ = xc;
658 } else
659 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000660 *out++ = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +0000661 } else {
662 /*
663 * We assume we have UTF-8 input.
664 */
Daniel Veillardb2517d82003-10-01 19:13:56 +0000665 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000666 int val = 0, l = 1;
667
668 if (*cur < 0xC0) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000669 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800670 "xmlEncodeEntities: input not UTF-8");
Daniel Veillard122376b2001-04-24 12:12:30 +0000671 if (doc != NULL)
672 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000673 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000674 buf[sizeof(buf) - 1] = 0;
675 ptr = buf;
676 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard05c13a22001-09-09 08:38:09 +0000677 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000678 continue;
679 } else if (*cur < 0xE0) {
680 val = (cur[0]) & 0x1F;
681 val <<= 6;
682 val |= (cur[1]) & 0x3F;
683 l = 2;
684 } else if (*cur < 0xF0) {
685 val = (cur[0]) & 0x0F;
686 val <<= 6;
687 val |= (cur[1]) & 0x3F;
688 val <<= 6;
689 val |= (cur[2]) & 0x3F;
690 l = 3;
691 } else if (*cur < 0xF8) {
692 val = (cur[0]) & 0x07;
693 val <<= 6;
694 val |= (cur[1]) & 0x3F;
695 val <<= 6;
696 val |= (cur[2]) & 0x3F;
697 val <<= 6;
698 val |= (cur[3]) & 0x3F;
699 l = 4;
700 }
701 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000702 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800703 "xmlEncodeEntities: char out of range\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000704 if (doc != NULL)
705 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000706 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000707 buf[sizeof(buf) - 1] = 0;
708 ptr = buf;
709 while (*ptr != 0) *out++ = *ptr++;
710 cur++;
711 continue;
712 }
713 /*
714 * We could do multiple things here. Just save as a char ref
715 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000716 snprintf(buf, sizeof(buf), "&#x%X;", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000717 buf[sizeof(buf) - 1] = 0;
718 ptr = buf;
719 while (*ptr != 0) *out++ = *ptr++;
720 cur += l;
721 continue;
722 }
William M. Brack76e95df2003-10-18 16:20:14 +0000723 } else if (IS_BYTE_CHAR(*cur)) {
Daniel Veillardb2517d82003-10-01 19:13:56 +0000724 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000725
Owen Taylor3473f882001-02-23 17:55:21 +0000726 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000727 buf[sizeof(buf) - 1] = 0;
728 ptr = buf;
729 while (*ptr != 0) *out++ = *ptr++;
730 }
Owen Taylor3473f882001-02-23 17:55:21 +0000731 cur++;
732 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200733 *out = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000734 return(buffer);
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800735
736mem_error:
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800737 xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800738 xmlFree(buffer);
739 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000740}
741
742/**
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800743 * xmlEncodeAttributeEntities:
744 * @doc: the document containing the string
745 * @input: A string to convert to XML.
746 *
747 * Do a global encoding of a string, replacing the predefined entities
748 * and non ASCII values with their entities and CharRef counterparts for
749 * attribute values.
750 *
751 * Returns A newly allocated string with the substitution done.
752 */
753xmlChar *
754xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
755 return xmlEncodeEntitiesInternal(doc, input, 1);
756}
757
758/**
759 * xmlEncodeEntitiesReentrant:
760 * @doc: the document containing the string
761 * @input: A string to convert to XML.
762 *
763 * Do a global encoding of a string, replacing the predefined entities
764 * and non ASCII values with their entities and CharRef counterparts.
765 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
766 * must be deallocated.
767 *
768 * Returns A newly allocated string with the substitution done.
769 */
770xmlChar *
771xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
772 return xmlEncodeEntitiesInternal(doc, input, 0);
773}
774
775/**
Owen Taylor3473f882001-02-23 17:55:21 +0000776 * xmlEncodeSpecialChars:
777 * @doc: the document containing the string
778 * @input: A string to convert to XML.
779 *
780 * Do a global encoding of a string, replacing the predefined entities
781 * this routine is reentrant, and result must be deallocated.
782 *
783 * Returns A newly allocated string with the substitution done.
784 */
785xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +0800786xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Owen Taylor3473f882001-02-23 17:55:21 +0000787 const xmlChar *cur = input;
788 xmlChar *buffer = NULL;
789 xmlChar *out = NULL;
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800790 size_t buffer_size = 0;
William M. Brack899e64a2003-09-26 18:03:42 +0000791 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000792
793 /*
794 * allocate an translation buffer.
795 */
796 buffer_size = 1000;
797 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
798 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000799 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000800 return(NULL);
801 }
802 out = buffer;
803
804 while (*cur != '\0') {
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800805 size_t indx = out - buffer;
806 if (indx + 10 > buffer_size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000807
808 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000809 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000810 }
811
812 /*
813 * By default one have to encode at least '<', '>', '"' and '&' !
814 */
815 if (*cur == '<') {
816 *out++ = '&';
817 *out++ = 'l';
818 *out++ = 't';
819 *out++ = ';';
820 } else if (*cur == '>') {
821 *out++ = '&';
822 *out++ = 'g';
823 *out++ = 't';
824 *out++ = ';';
825 } else if (*cur == '&') {
826 *out++ = '&';
827 *out++ = 'a';
828 *out++ = 'm';
829 *out++ = 'p';
830 *out++ = ';';
831 } else if (*cur == '"') {
832 *out++ = '&';
833 *out++ = 'q';
834 *out++ = 'u';
835 *out++ = 'o';
836 *out++ = 't';
837 *out++ = ';';
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000838 } else if (*cur == '\r') {
839 *out++ = '&';
840 *out++ = '#';
841 *out++ = '1';
842 *out++ = '3';
843 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000844 } else {
845 /*
846 * Works because on UTF-8, all extended sequences cannot
847 * result in bytes in the ASCII range.
848 */
849 *out++ = *cur;
850 }
851 cur++;
852 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200853 *out = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000854 return(buffer);
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800855
856mem_error:
857 xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
858 xmlFree(buffer);
859 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000860}
861
862/**
863 * xmlCreateEntitiesTable:
864 *
865 * create and initialize an empty entities hash table.
Daniel Veillard316a5c32005-01-23 22:56:39 +0000866 * This really doesn't make sense and should be deprecated
Owen Taylor3473f882001-02-23 17:55:21 +0000867 *
868 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
869 */
870xmlEntitiesTablePtr
871xmlCreateEntitiesTable(void) {
872 return((xmlEntitiesTablePtr) xmlHashCreate(0));
873}
874
875/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000876 * xmlFreeEntityWrapper:
877 * @entity: An entity
878 * @name: its name
879 *
880 * Deallocate the memory used by an entities in the hash table.
881 */
882static void
883xmlFreeEntityWrapper(xmlEntityPtr entity,
884 const xmlChar *name ATTRIBUTE_UNUSED) {
885 if (entity != NULL)
886 xmlFreeEntity(entity);
887}
888
889/**
Owen Taylor3473f882001-02-23 17:55:21 +0000890 * xmlFreeEntitiesTable:
891 * @table: An entity table
892 *
893 * Deallocate the memory used by an entities hash table.
894 */
895void
896xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000897 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000898}
899
Daniel Veillard652327a2003-09-29 18:02:38 +0000900#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000901/**
902 * xmlCopyEntity:
903 * @ent: An entity
904 *
905 * Build a copy of an entity
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800906 *
Owen Taylor3473f882001-02-23 17:55:21 +0000907 * Returns the new xmlEntitiesPtr or NULL in case of error.
908 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000909static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000910xmlCopyEntity(xmlEntityPtr ent) {
911 xmlEntityPtr cur;
912
913 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
914 if (cur == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000915 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000916 return(NULL);
917 }
918 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000919 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000920
921 cur->etype = ent->etype;
922 if (ent->name != NULL)
923 cur->name = xmlStrdup(ent->name);
924 if (ent->ExternalID != NULL)
925 cur->ExternalID = xmlStrdup(ent->ExternalID);
926 if (ent->SystemID != NULL)
927 cur->SystemID = xmlStrdup(ent->SystemID);
928 if (ent->content != NULL)
929 cur->content = xmlStrdup(ent->content);
930 if (ent->orig != NULL)
931 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000932 if (ent->URI != NULL)
933 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000934 return(cur);
935}
936
937/**
938 * xmlCopyEntitiesTable:
939 * @table: An entity table
940 *
941 * Build a copy of an entity table.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800942 *
Owen Taylor3473f882001-02-23 17:55:21 +0000943 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
944 */
945xmlEntitiesTablePtr
946xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
947 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
948}
Daniel Veillard652327a2003-09-29 18:02:38 +0000949#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000950
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000951#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard18ab8722003-12-09 22:51:37 +0000952
953/**
954 * xmlDumpEntityContent:
955 * @buf: An XML buffer.
956 * @content: The entity content.
957 *
958 * This will dump the quoted string value, taking care of the special
959 * treatment required by %
960 */
961static void
962xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
963 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
964 if (xmlStrchr(content, '%')) {
965 const xmlChar * base, *cur;
966
967 xmlBufferCCat(buf, "\"");
968 base = cur = content;
969 while (*cur != 0) {
970 if (*cur == '"') {
971 if (base != cur)
972 xmlBufferAdd(buf, base, cur - base);
973 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
974 cur++;
975 base = cur;
976 } else if (*cur == '%') {
977 if (base != cur)
978 xmlBufferAdd(buf, base, cur - base);
979 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
980 cur++;
981 base = cur;
982 } else {
983 cur++;
984 }
985 }
986 if (base != cur)
987 xmlBufferAdd(buf, base, cur - base);
988 xmlBufferCCat(buf, "\"");
989 } else {
990 xmlBufferWriteQuotedString(buf, content);
991 }
992}
993
Owen Taylor3473f882001-02-23 17:55:21 +0000994/**
995 * xmlDumpEntityDecl:
996 * @buf: An XML buffer.
997 * @ent: An entity table
998 *
999 * This will dump the content of the entity table as an XML DTD definition
1000 */
1001void
1002xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001003 if ((buf == NULL) || (ent == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001004 switch (ent->etype) {
1005 case XML_INTERNAL_GENERAL_ENTITY:
1006 xmlBufferWriteChar(buf, "<!ENTITY ");
1007 xmlBufferWriteCHAR(buf, ent->name);
1008 xmlBufferWriteChar(buf, " ");
1009 if (ent->orig != NULL)
1010 xmlBufferWriteQuotedString(buf, ent->orig);
1011 else
Daniel Veillard18ab8722003-12-09 22:51:37 +00001012 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001013 xmlBufferWriteChar(buf, ">\n");
1014 break;
1015 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1016 xmlBufferWriteChar(buf, "<!ENTITY ");
1017 xmlBufferWriteCHAR(buf, ent->name);
1018 if (ent->ExternalID != NULL) {
1019 xmlBufferWriteChar(buf, " PUBLIC ");
1020 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1021 xmlBufferWriteChar(buf, " ");
1022 xmlBufferWriteQuotedString(buf, ent->SystemID);
1023 } else {
1024 xmlBufferWriteChar(buf, " SYSTEM ");
1025 xmlBufferWriteQuotedString(buf, ent->SystemID);
1026 }
1027 xmlBufferWriteChar(buf, ">\n");
1028 break;
1029 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1030 xmlBufferWriteChar(buf, "<!ENTITY ");
1031 xmlBufferWriteCHAR(buf, ent->name);
1032 if (ent->ExternalID != NULL) {
1033 xmlBufferWriteChar(buf, " PUBLIC ");
1034 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1035 xmlBufferWriteChar(buf, " ");
1036 xmlBufferWriteQuotedString(buf, ent->SystemID);
1037 } else {
1038 xmlBufferWriteChar(buf, " SYSTEM ");
1039 xmlBufferWriteQuotedString(buf, ent->SystemID);
1040 }
1041 if (ent->content != NULL) { /* Should be true ! */
1042 xmlBufferWriteChar(buf, " NDATA ");
1043 if (ent->orig != NULL)
1044 xmlBufferWriteCHAR(buf, ent->orig);
1045 else
1046 xmlBufferWriteCHAR(buf, ent->content);
1047 }
1048 xmlBufferWriteChar(buf, ">\n");
1049 break;
1050 case XML_INTERNAL_PARAMETER_ENTITY:
1051 xmlBufferWriteChar(buf, "<!ENTITY % ");
1052 xmlBufferWriteCHAR(buf, ent->name);
1053 xmlBufferWriteChar(buf, " ");
1054 if (ent->orig == NULL)
Daniel Veillard18ab8722003-12-09 22:51:37 +00001055 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001056 else
1057 xmlBufferWriteQuotedString(buf, ent->orig);
1058 xmlBufferWriteChar(buf, ">\n");
1059 break;
1060 case XML_EXTERNAL_PARAMETER_ENTITY:
1061 xmlBufferWriteChar(buf, "<!ENTITY % ");
1062 xmlBufferWriteCHAR(buf, ent->name);
1063 if (ent->ExternalID != NULL) {
1064 xmlBufferWriteChar(buf, " PUBLIC ");
1065 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1066 xmlBufferWriteChar(buf, " ");
1067 xmlBufferWriteQuotedString(buf, ent->SystemID);
1068 } else {
1069 xmlBufferWriteChar(buf, " SYSTEM ");
1070 xmlBufferWriteQuotedString(buf, ent->SystemID);
1071 }
1072 xmlBufferWriteChar(buf, ">\n");
1073 break;
1074 default:
Daniel Veillardce244ad2004-11-05 10:03:46 +00001075 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
1076 "xmlDumpEntitiesDecl: internal: unknown type entity type");
Owen Taylor3473f882001-02-23 17:55:21 +00001077 }
1078}
1079
1080/**
William M. Brack9e660592003-10-20 14:56:06 +00001081 * xmlDumpEntityDeclScan:
1082 * @ent: An entity table
1083 * @buf: An XML buffer.
1084 *
1085 * When using the hash table scan function, arguments need to be reversed
1086 */
1087static void
1088xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
1089 xmlDumpEntityDecl(buf, ent);
1090}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001091
William M. Brack9e660592003-10-20 14:56:06 +00001092/**
Owen Taylor3473f882001-02-23 17:55:21 +00001093 * xmlDumpEntitiesTable:
1094 * @buf: An XML buffer.
1095 * @table: An entity table
1096 *
1097 * This will dump the content of the entity table as an XML DTD definition
1098 */
1099void
1100xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00001101 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001102}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001103#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001104#define bottom_entities
1105#include "elfgcchack.h"