blob: f9e9cd9d927cace4c799accb5300a680ca08205b [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
Owen Taylor3473f882001-02-23 17:55:21 +000025/*
26 * The XML predefined entities.
27 */
28
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000029static xmlEntity xmlEntityLt = {
30 NULL, XML_ENTITY_DECL, BAD_CAST "lt",
31 NULL, NULL, NULL, NULL, NULL, NULL,
32 BAD_CAST "<", BAD_CAST "<", 1,
33 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000034 NULL, NULL, NULL, NULL, 0, 1
Owen Taylor3473f882001-02-23 17:55:21 +000035};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000036static xmlEntity xmlEntityGt = {
37 NULL, XML_ENTITY_DECL, BAD_CAST "gt",
38 NULL, NULL, NULL, NULL, NULL, NULL,
39 BAD_CAST ">", BAD_CAST ">", 1,
40 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000041 NULL, NULL, NULL, NULL, 0, 1
Owen Taylor3473f882001-02-23 17:55:21 +000042};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000043static xmlEntity xmlEntityAmp = {
44 NULL, XML_ENTITY_DECL, BAD_CAST "amp",
45 NULL, NULL, NULL, NULL, NULL, NULL,
46 BAD_CAST "&", BAD_CAST "&", 1,
47 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000048 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000049};
50static xmlEntity xmlEntityQuot = {
51 NULL, XML_ENTITY_DECL, BAD_CAST "quot",
52 NULL, NULL, NULL, NULL, NULL, NULL,
53 BAD_CAST "\"", BAD_CAST "\"", 1,
54 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000055 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000056};
57static xmlEntity xmlEntityApos = {
58 NULL, XML_ENTITY_DECL, BAD_CAST "apos",
59 NULL, NULL, NULL, NULL, NULL, NULL,
60 BAD_CAST "'", BAD_CAST "'", 1,
61 XML_INTERNAL_PREDEFINED_ENTITY,
Daniel Veillardf4f4e482008-08-25 08:57:48 +000062 NULL, NULL, NULL, NULL, 0, 1
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000063};
Owen Taylor3473f882001-02-23 17:55:21 +000064
Daniel Veillardce244ad2004-11-05 10:03:46 +000065/**
66 * xmlEntitiesErrMemory:
67 * @extra: extra informations
68 *
69 * Handle an out of memory condition
70 */
71static void
72xmlEntitiesErrMemory(const char *extra)
73{
74 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
75}
76
77/**
78 * xmlEntitiesErr:
79 * @code: the error code
80 * @msg: the message
81 *
82 * Handle an out of memory condition
83 */
84static void
85xmlEntitiesErr(xmlParserErrors code, const char *msg)
86{
Daniel Veillardce244ad2004-11-05 10:03:46 +000087 __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);
88}
89
Owen Taylor3473f882001-02-23 17:55:21 +000090/*
91 * xmlFreeEntity : clean-up an entity record.
92 */
Daniel Veillard7da92702005-01-23 20:15:53 +000093static void
94xmlFreeEntity(xmlEntityPtr entity)
95{
96 xmlDictPtr dict = NULL;
97
98 if (entity == NULL)
99 return;
100
101 if (entity->doc != NULL)
102 dict = entity->doc->dict;
103
Owen Taylor3473f882001-02-23 17:55:21 +0000104
Daniel Veillard2d84a892002-12-30 00:01:08 +0000105 if ((entity->children) && (entity->owner == 1) &&
Daniel Veillard7da92702005-01-23 20:15:53 +0000106 (entity == (xmlEntityPtr) entity->children->parent))
107 xmlFreeNodeList(entity->children);
108 if (dict != NULL) {
109 if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
110 xmlFree((char *) entity->name);
111 if ((entity->ExternalID != NULL) &&
112 (!xmlDictOwns(dict, entity->ExternalID)))
113 xmlFree((char *) entity->ExternalID);
114 if ((entity->SystemID != NULL) &&
115 (!xmlDictOwns(dict, entity->SystemID)))
116 xmlFree((char *) entity->SystemID);
117 if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
118 xmlFree((char *) entity->URI);
119 if ((entity->content != NULL)
120 && (!xmlDictOwns(dict, entity->content)))
121 xmlFree((char *) entity->content);
122 if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
123 xmlFree((char *) entity->orig);
124 } else {
125 if (entity->name != NULL)
126 xmlFree((char *) entity->name);
127 if (entity->ExternalID != NULL)
128 xmlFree((char *) entity->ExternalID);
129 if (entity->SystemID != NULL)
130 xmlFree((char *) entity->SystemID);
131 if (entity->URI != NULL)
132 xmlFree((char *) entity->URI);
133 if (entity->content != NULL)
134 xmlFree((char *) entity->content);
135 if (entity->orig != NULL)
136 xmlFree((char *) entity->orig);
137 }
Owen Taylor3473f882001-02-23 17:55:21 +0000138 xmlFree(entity);
139}
140
141/*
Daniel Veillardaa6de472008-08-25 14:53:31 +0000142 * xmlCreateEntity:
143 *
144 * internal routine doing the entity node strutures allocations
Owen Taylor3473f882001-02-23 17:55:21 +0000145 */
146static xmlEntityPtr
Daniel Veillardaa6de472008-08-25 14:53:31 +0000147xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
148 const xmlChar *ExternalID, const xmlChar *SystemID,
149 const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +0000150 xmlEntityPtr ret;
151
Owen Taylor3473f882001-02-23 17:55:21 +0000152 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
153 if (ret == NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000154 xmlEntitiesErrMemory("xmlCreateEntity: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000155 return(NULL);
156 }
157 memset(ret, 0, sizeof(xmlEntity));
158 ret->type = XML_ENTITY_DECL;
Daniel Veillarda37a6ad2006-10-10 20:05:45 +0000159 ret->checked = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000160
161 /*
162 * fill the structure.
163 */
Owen Taylor3473f882001-02-23 17:55:21 +0000164 ret->etype = (xmlEntityType) type;
Daniel Veillard7da92702005-01-23 20:15:53 +0000165 if (dict == NULL) {
166 ret->name = xmlStrdup(name);
167 if (ExternalID != NULL)
168 ret->ExternalID = xmlStrdup(ExternalID);
169 if (SystemID != NULL)
170 ret->SystemID = xmlStrdup(SystemID);
171 } else {
172 ret->name = xmlDictLookup(dict, name, -1);
173 if (ExternalID != NULL)
174 ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
175 if (SystemID != NULL)
176 ret->SystemID = xmlDictLookup(dict, SystemID, -1);
177 }
Owen Taylor3473f882001-02-23 17:55:21 +0000178 if (content != NULL) {
179 ret->length = xmlStrlen(content);
Daniel Veillard7da92702005-01-23 20:15:53 +0000180 if ((dict != NULL) && (ret->length < 5))
181 ret->content = (xmlChar *)
182 xmlDictLookup(dict, content, ret->length);
183 else
184 ret->content = xmlStrndup(content, ret->length);
Owen Taylor3473f882001-02-23 17:55:21 +0000185 } else {
186 ret->length = 0;
187 ret->content = NULL;
188 }
189 ret->URI = NULL; /* to be computed by the layer knowing
190 the defining entity */
191 ret->orig = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +0000192 ret->owner = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000193
194 return(ret);
195}
196
197/*
198 * xmlAddEntity : register a new entity for an entities table.
199 */
200static xmlEntityPtr
201xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
202 const xmlChar *ExternalID, const xmlChar *SystemID,
203 const xmlChar *content) {
204 xmlDictPtr dict = NULL;
205 xmlEntitiesTablePtr table = NULL;
206 xmlEntityPtr ret;
207
208 if (name == NULL)
209 return(NULL);
210 if (dtd == NULL)
211 return(NULL);
212 if (dtd->doc != NULL)
213 dict = dtd->doc->dict;
214
215 switch (type) {
216 case XML_INTERNAL_GENERAL_ENTITY:
217 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
218 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
219 if (dtd->entities == NULL)
220 dtd->entities = xmlHashCreateDict(0, dict);
221 table = dtd->entities;
222 break;
223 case XML_INTERNAL_PARAMETER_ENTITY:
224 case XML_EXTERNAL_PARAMETER_ENTITY:
225 if (dtd->pentities == NULL)
226 dtd->pentities = xmlHashCreateDict(0, dict);
227 table = dtd->pentities;
228 break;
229 case XML_INTERNAL_PREDEFINED_ENTITY:
230 return(NULL);
231 }
232 if (table == NULL)
233 return(NULL);
234 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
235 if (ret == NULL)
236 return(NULL);
Daniel Veillard7da92702005-01-23 20:15:53 +0000237 ret->doc = dtd->doc;
Owen Taylor3473f882001-02-23 17:55:21 +0000238
239 if (xmlHashAddEntry(table, name, ret)) {
240 /*
241 * entity was already defined at another level.
242 */
243 xmlFreeEntity(ret);
244 return(NULL);
245 }
246 return(ret);
247}
248
249/**
Owen Taylor3473f882001-02-23 17:55:21 +0000250 * xmlGetPredefinedEntity:
251 * @name: the entity name
252 *
253 * Check whether this name is an predefined entity.
254 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000255 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000256 */
257xmlEntityPtr
258xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000259 if (name == NULL) return(NULL);
260 switch (name[0]) {
261 case 'l':
262 if (xmlStrEqual(name, BAD_CAST "lt"))
263 return(&xmlEntityLt);
264 break;
265 case 'g':
266 if (xmlStrEqual(name, BAD_CAST "gt"))
267 return(&xmlEntityGt);
268 break;
269 case 'a':
270 if (xmlStrEqual(name, BAD_CAST "amp"))
271 return(&xmlEntityAmp);
272 if (xmlStrEqual(name, BAD_CAST "apos"))
273 return(&xmlEntityApos);
274 break;
275 case 'q':
276 if (xmlStrEqual(name, BAD_CAST "quot"))
277 return(&xmlEntityQuot);
278 break;
279 default:
280 break;
281 }
282 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000283}
284
285/**
286 * xmlAddDtdEntity:
287 * @doc: the document
288 * @name: the entity name
289 * @type: the entity type XML_xxx_yyy_ENTITY
290 * @ExternalID: the entity external ID if available
291 * @SystemID: the entity system ID if available
292 * @content: the entity content
293 *
294 * Register a new entity for this document DTD external subset.
295 *
296 * Returns a pointer to the entity or NULL in case of error
297 */
298xmlEntityPtr
299xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
300 const xmlChar *ExternalID, const xmlChar *SystemID,
301 const xmlChar *content) {
302 xmlEntityPtr ret;
303 xmlDtdPtr dtd;
304
305 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000306 xmlEntitiesErr(XML_DTD_NO_DOC,
307 "xmlAddDtdEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000308 return(NULL);
309 }
310 if (doc->extSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000311 xmlEntitiesErr(XML_DTD_NO_DTD,
312 "xmlAddDtdEntity: document without external subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000313 return(NULL);
314 }
315 dtd = doc->extSubset;
316 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
317 if (ret == NULL) return(NULL);
318
319 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000320 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000321 */
322 ret->parent = dtd;
323 ret->doc = dtd->doc;
324 if (dtd->last == NULL) {
325 dtd->children = dtd->last = (xmlNodePtr) ret;
326 } else {
327 dtd->last->next = (xmlNodePtr) ret;
328 ret->prev = dtd->last;
329 dtd->last = (xmlNodePtr) ret;
330 }
331 return(ret);
332}
333
334/**
335 * xmlAddDocEntity:
336 * @doc: the document
337 * @name: the entity name
338 * @type: the entity type XML_xxx_yyy_ENTITY
339 * @ExternalID: the entity external ID if available
340 * @SystemID: the entity system ID if available
341 * @content: the entity content
342 *
343 * Register a new entity for this document.
344 *
345 * Returns a pointer to the entity or NULL in case of error
346 */
347xmlEntityPtr
348xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
349 const xmlChar *ExternalID, const xmlChar *SystemID,
350 const xmlChar *content) {
351 xmlEntityPtr ret;
352 xmlDtdPtr dtd;
353
354 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000355 xmlEntitiesErr(XML_DTD_NO_DOC,
356 "xmlAddDocEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000357 return(NULL);
358 }
359 if (doc->intSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000360 xmlEntitiesErr(XML_DTD_NO_DTD,
361 "xmlAddDocEntity: document without internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000362 return(NULL);
363 }
364 dtd = doc->intSubset;
365 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
366 if (ret == NULL) return(NULL);
367
368 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000369 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000370 */
371 ret->parent = dtd;
372 ret->doc = dtd->doc;
373 if (dtd->last == NULL) {
374 dtd->children = dtd->last = (xmlNodePtr) ret;
375 } else {
376 dtd->last->next = (xmlNodePtr) ret;
377 ret->prev = dtd->last;
378 dtd->last = (xmlNodePtr) ret;
379 }
380 return(ret);
381}
382
383/**
Daniel Veillardaa6de472008-08-25 14:53:31 +0000384 * xmlNewEntity:
385 * @doc: the document
386 * @name: the entity name
387 * @type: the entity type XML_xxx_yyy_ENTITY
388 * @ExternalID: the entity external ID if available
389 * @SystemID: the entity system ID if available
390 * @content: the entity content
391 *
392 * Create a new entity, this differs from xmlAddDocEntity() that if
393 * the document is NULL or has no internal subset defined, then an
394 * unlinked entity structure will be returned, it is then the responsability
395 * of the caller to link it to the document later or free it when not needed
396 * anymore.
397 *
398 * Returns a pointer to the entity or NULL in case of error
399 */
400xmlEntityPtr
401xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
402 const xmlChar *ExternalID, const xmlChar *SystemID,
403 const xmlChar *content) {
404 xmlEntityPtr ret;
405 xmlDictPtr dict;
406
407 if ((doc != NULL) && (doc->intSubset != NULL)) {
408 return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
409 }
410 if (doc != NULL)
411 dict = doc->dict;
412 else
413 dict = NULL;
414 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
415 if (ret == NULL)
416 return(NULL);
417 ret->doc = doc;
418 return(ret);
419}
420
421/**
Owen Taylor3473f882001-02-23 17:55:21 +0000422 * xmlGetEntityFromTable:
423 * @table: an entity table
424 * @name: the entity name
425 * @parameter: look for parameter entities
426 *
427 * Do an entity lookup in the table.
428 * returns the corresponding parameter entity, if found.
429 *
430 * Returns A pointer to the entity structure or NULL if not found.
431 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000432static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000433xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
434 return((xmlEntityPtr) xmlHashLookup(table, name));
435}
436
437/**
438 * xmlGetParameterEntity:
439 * @doc: the document referencing the entity
440 * @name: the entity name
441 *
442 * Do an entity lookup in the internal and external subsets and
443 * returns the corresponding parameter entity, if found.
444 *
445 * Returns A pointer to the entity structure or NULL if not found.
446 */
447xmlEntityPtr
448xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
449 xmlEntitiesTablePtr table;
450 xmlEntityPtr ret;
451
Daniel Veillard36065812002-01-24 15:02:46 +0000452 if (doc == NULL)
453 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000454 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
455 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
456 ret = xmlGetEntityFromTable(table, name);
457 if (ret != NULL)
458 return(ret);
459 }
460 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
461 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
462 return(xmlGetEntityFromTable(table, name));
463 }
464 return(NULL);
465}
466
467/**
468 * xmlGetDtdEntity:
469 * @doc: the document referencing the entity
470 * @name: the entity name
471 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000472 * Do an entity lookup in the DTD entity hash table and
Owen Taylor3473f882001-02-23 17:55:21 +0000473 * returns the corresponding entity, if found.
Daniel Veillard36065812002-01-24 15:02:46 +0000474 * Note: the first argument is the document node, not the DTD node.
Owen Taylor3473f882001-02-23 17:55:21 +0000475 *
476 * Returns A pointer to the entity structure or NULL if not found.
477 */
478xmlEntityPtr
479xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
480 xmlEntitiesTablePtr table;
481
Daniel Veillard36065812002-01-24 15:02:46 +0000482 if (doc == NULL)
483 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000484 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
485 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
486 return(xmlGetEntityFromTable(table, name));
487 }
488 return(NULL);
489}
490
491/**
492 * xmlGetDocEntity:
493 * @doc: the document referencing the entity
494 * @name: the entity name
495 *
496 * Do an entity lookup in the document entity hash table and
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000497 * returns the corresponding entity, otherwise a lookup is done
Owen Taylor3473f882001-02-23 17:55:21 +0000498 * in the predefined entities too.
499 *
500 * Returns A pointer to the entity structure or NULL if not found.
501 */
502xmlEntityPtr
503xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
504 xmlEntityPtr cur;
505 xmlEntitiesTablePtr table;
506
507 if (doc != NULL) {
508 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
509 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
510 cur = xmlGetEntityFromTable(table, name);
511 if (cur != NULL)
512 return(cur);
513 }
Daniel Veillard28757702002-02-18 11:19:30 +0000514 if (doc->standalone != 1) {
515 if ((doc->extSubset != NULL) &&
516 (doc->extSubset->entities != NULL)) {
517 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
518 cur = xmlGetEntityFromTable(table, name);
519 if (cur != NULL)
520 return(cur);
521 }
Owen Taylor3473f882001-02-23 17:55:21 +0000522 }
523 }
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000524 return(xmlGetPredefinedEntity(name));
Owen Taylor3473f882001-02-23 17:55:21 +0000525}
526
527/*
Owen Taylor3473f882001-02-23 17:55:21 +0000528 * Macro used to grow the current buffer.
529 */
530#define growBufferReentrant() { \
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800531 xmlChar *tmp; \
Aron Xubaaf03f2012-07-20 15:41:34 +0800532 size_t new_size = buffer_size * 2; \
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800533 if (new_size < buffer_size) goto mem_error; \
534 tmp = (xmlChar *) xmlRealloc(buffer, new_size); \
535 if (tmp == NULL) goto mem_error; \
536 buffer = tmp; \
537 buffer_size = new_size; \
Owen Taylor3473f882001-02-23 17:55:21 +0000538}
539
Owen Taylor3473f882001-02-23 17:55:21 +0000540/**
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800541 * xmlEncodeEntitiesInternal:
Owen Taylor3473f882001-02-23 17:55:21 +0000542 * @doc: the document containing the string
543 * @input: A string to convert to XML.
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800544 * @attr: are we handling an atrbute value
Owen Taylor3473f882001-02-23 17:55:21 +0000545 *
546 * Do a global encoding of a string, replacing the predefined entities
547 * and non ASCII values with their entities and CharRef counterparts.
548 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
549 * must be deallocated.
550 *
551 * Returns A newly allocated string with the substitution done.
552 */
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800553static xmlChar *
554xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
Owen Taylor3473f882001-02-23 17:55:21 +0000555 const xmlChar *cur = input;
556 xmlChar *buffer = NULL;
557 xmlChar *out = NULL;
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800558 size_t buffer_size = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000559 int html = 0;
560
561 if (input == NULL) return(NULL);
562 if (doc != NULL)
563 html = (doc->type == XML_HTML_DOCUMENT_NODE);
564
565 /*
566 * allocate an translation buffer.
567 */
568 buffer_size = 1000;
569 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
570 if (buffer == NULL) {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800571 xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000572 return(NULL);
573 }
574 out = buffer;
575
576 while (*cur != '\0') {
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800577 size_t indx = out - buffer;
578 if (indx + 100 > buffer_size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000579
580 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000581 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000582 }
583
584 /*
585 * By default one have to encode at least '<', '>', '"' and '&' !
586 */
587 if (*cur == '<') {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800588 const xmlChar *end;
589
590 /*
591 * Special handling of server side include in HTML attributes
592 */
593 if (html && attr &&
594 (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
595 ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
596 while (cur != end) {
597 *out++ = *cur++;
598 indx = out - buffer;
599 if (indx + 100 > buffer_size) {
600 growBufferReentrant();
601 out = &buffer[indx];
602 }
603 }
604 *out++ = *cur++;
605 *out++ = *cur++;
606 *out++ = *cur++;
607 continue;
608 }
Owen Taylor3473f882001-02-23 17:55:21 +0000609 *out++ = '&';
610 *out++ = 'l';
611 *out++ = 't';
612 *out++ = ';';
613 } else if (*cur == '>') {
614 *out++ = '&';
615 *out++ = 'g';
616 *out++ = 't';
617 *out++ = ';';
618 } else if (*cur == '&') {
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800619 /*
620 * Special handling of &{...} construct from HTML 4, see
621 * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
622 */
623 if (html && attr && (cur[1] == '{') && (strchr(cur, '}'))) {
624 while (*cur != '}') {
625 *out++ = *cur++;
626 indx = out - buffer;
627 if (indx + 100 > buffer_size) {
628 growBufferReentrant();
629 out = &buffer[indx];
630 }
631 }
632 *out++ = *cur++;
633 continue;
634 }
Owen Taylor3473f882001-02-23 17:55:21 +0000635 *out++ = '&';
636 *out++ = 'a';
637 *out++ = 'm';
638 *out++ = 'p';
639 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000640 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Daniel Veillard0046c0f2003-02-23 13:52:30 +0000641 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000642 /*
643 * default case, just copy !
644 */
645 *out++ = *cur;
646 } else if (*cur >= 0x80) {
Daniel Veillard122376b2001-04-24 12:12:30 +0000647 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000648 /*
649 * Bjørn Reese <br@sseusa.com> provided the patch
650 xmlChar xc;
651 xc = (*cur & 0x3F) << 6;
652 if (cur[1] != 0) {
653 xc += *(++cur) & 0x3F;
654 *out++ = xc;
655 } else
656 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000657 *out++ = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +0000658 } else {
659 /*
660 * We assume we have UTF-8 input.
661 */
Daniel Veillardb2517d82003-10-01 19:13:56 +0000662 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000663 int val = 0, l = 1;
664
665 if (*cur < 0xC0) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000666 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800667 "xmlEncodeEntities: input not UTF-8");
Daniel Veillard122376b2001-04-24 12:12:30 +0000668 if (doc != NULL)
669 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000670 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000671 buf[sizeof(buf) - 1] = 0;
672 ptr = buf;
673 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard05c13a22001-09-09 08:38:09 +0000674 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000675 continue;
676 } else if (*cur < 0xE0) {
677 val = (cur[0]) & 0x1F;
678 val <<= 6;
679 val |= (cur[1]) & 0x3F;
680 l = 2;
681 } else if (*cur < 0xF0) {
682 val = (cur[0]) & 0x0F;
683 val <<= 6;
684 val |= (cur[1]) & 0x3F;
685 val <<= 6;
686 val |= (cur[2]) & 0x3F;
687 l = 3;
688 } else if (*cur < 0xF8) {
689 val = (cur[0]) & 0x07;
690 val <<= 6;
691 val |= (cur[1]) & 0x3F;
692 val <<= 6;
693 val |= (cur[2]) & 0x3F;
694 val <<= 6;
695 val |= (cur[3]) & 0x3F;
696 l = 4;
697 }
698 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000699 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800700 "xmlEncodeEntities: char out of range\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000701 if (doc != NULL)
702 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000703 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000704 buf[sizeof(buf) - 1] = 0;
705 ptr = buf;
706 while (*ptr != 0) *out++ = *ptr++;
707 cur++;
708 continue;
709 }
710 /*
711 * We could do multiple things here. Just save as a char ref
712 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000713 snprintf(buf, sizeof(buf), "&#x%X;", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000714 buf[sizeof(buf) - 1] = 0;
715 ptr = buf;
716 while (*ptr != 0) *out++ = *ptr++;
717 cur += l;
718 continue;
719 }
William M. Brack76e95df2003-10-18 16:20:14 +0000720 } else if (IS_BYTE_CHAR(*cur)) {
Daniel Veillardb2517d82003-10-01 19:13:56 +0000721 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000722
Owen Taylor3473f882001-02-23 17:55:21 +0000723 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000724 buf[sizeof(buf) - 1] = 0;
725 ptr = buf;
726 while (*ptr != 0) *out++ = *ptr++;
727 }
Owen Taylor3473f882001-02-23 17:55:21 +0000728 cur++;
729 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200730 *out = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000731 return(buffer);
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800732
733mem_error:
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800734 xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800735 xmlFree(buffer);
736 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000737}
738
739/**
Daniel Veillard7d4c5292012-09-05 11:45:32 +0800740 * xmlEncodeAttributeEntities:
741 * @doc: the document containing the string
742 * @input: A string to convert to XML.
743 *
744 * Do a global encoding of a string, replacing the predefined entities
745 * and non ASCII values with their entities and CharRef counterparts for
746 * attribute values.
747 *
748 * Returns A newly allocated string with the substitution done.
749 */
750xmlChar *
751xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
752 return xmlEncodeEntitiesInternal(doc, input, 1);
753}
754
755/**
756 * xmlEncodeEntitiesReentrant:
757 * @doc: the document containing the string
758 * @input: A string to convert to XML.
759 *
760 * Do a global encoding of a string, replacing the predefined entities
761 * and non ASCII values with their entities and CharRef counterparts.
762 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
763 * must be deallocated.
764 *
765 * Returns A newly allocated string with the substitution done.
766 */
767xmlChar *
768xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
769 return xmlEncodeEntitiesInternal(doc, input, 0);
770}
771
772/**
Owen Taylor3473f882001-02-23 17:55:21 +0000773 * xmlEncodeSpecialChars:
774 * @doc: the document containing the string
775 * @input: A string to convert to XML.
776 *
777 * Do a global encoding of a string, replacing the predefined entities
778 * this routine is reentrant, and result must be deallocated.
779 *
780 * Returns A newly allocated string with the substitution done.
781 */
782xmlChar *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000783xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Owen Taylor3473f882001-02-23 17:55:21 +0000784 const xmlChar *cur = input;
785 xmlChar *buffer = NULL;
786 xmlChar *out = NULL;
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800787 size_t buffer_size = 0;
William M. Brack899e64a2003-09-26 18:03:42 +0000788 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000789
790 /*
791 * allocate an translation buffer.
792 */
793 buffer_size = 1000;
794 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
795 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000796 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000797 return(NULL);
798 }
799 out = buffer;
800
801 while (*cur != '\0') {
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800802 size_t indx = out - buffer;
803 if (indx + 10 > buffer_size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000804
805 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000806 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000807 }
808
809 /*
810 * By default one have to encode at least '<', '>', '"' and '&' !
811 */
812 if (*cur == '<') {
813 *out++ = '&';
814 *out++ = 'l';
815 *out++ = 't';
816 *out++ = ';';
817 } else if (*cur == '>') {
818 *out++ = '&';
819 *out++ = 'g';
820 *out++ = 't';
821 *out++ = ';';
822 } else if (*cur == '&') {
823 *out++ = '&';
824 *out++ = 'a';
825 *out++ = 'm';
826 *out++ = 'p';
827 *out++ = ';';
828 } else if (*cur == '"') {
829 *out++ = '&';
830 *out++ = 'q';
831 *out++ = 'u';
832 *out++ = 'o';
833 *out++ = 't';
834 *out++ = ';';
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000835 } else if (*cur == '\r') {
836 *out++ = '&';
837 *out++ = '#';
838 *out++ = '1';
839 *out++ = '3';
840 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000841 } else {
842 /*
843 * Works because on UTF-8, all extended sequences cannot
844 * result in bytes in the ASCII range.
845 */
846 *out++ = *cur;
847 }
848 cur++;
849 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200850 *out = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000851 return(buffer);
Daniel Veillard4f9fdc72012-07-18 11:38:17 +0800852
853mem_error:
854 xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
855 xmlFree(buffer);
856 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000857}
858
859/**
860 * xmlCreateEntitiesTable:
861 *
862 * create and initialize an empty entities hash table.
Daniel Veillard316a5c32005-01-23 22:56:39 +0000863 * This really doesn't make sense and should be deprecated
Owen Taylor3473f882001-02-23 17:55:21 +0000864 *
865 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
866 */
867xmlEntitiesTablePtr
868xmlCreateEntitiesTable(void) {
869 return((xmlEntitiesTablePtr) xmlHashCreate(0));
870}
871
872/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000873 * xmlFreeEntityWrapper:
874 * @entity: An entity
875 * @name: its name
876 *
877 * Deallocate the memory used by an entities in the hash table.
878 */
879static void
880xmlFreeEntityWrapper(xmlEntityPtr entity,
881 const xmlChar *name ATTRIBUTE_UNUSED) {
882 if (entity != NULL)
883 xmlFreeEntity(entity);
884}
885
886/**
Owen Taylor3473f882001-02-23 17:55:21 +0000887 * xmlFreeEntitiesTable:
888 * @table: An entity table
889 *
890 * Deallocate the memory used by an entities hash table.
891 */
892void
893xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000894 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000895}
896
Daniel Veillard652327a2003-09-29 18:02:38 +0000897#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000898/**
899 * xmlCopyEntity:
900 * @ent: An entity
901 *
902 * Build a copy of an entity
903 *
904 * Returns the new xmlEntitiesPtr or NULL in case of error.
905 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000906static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000907xmlCopyEntity(xmlEntityPtr ent) {
908 xmlEntityPtr cur;
909
910 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
911 if (cur == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000912 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000913 return(NULL);
914 }
915 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000916 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000917
918 cur->etype = ent->etype;
919 if (ent->name != NULL)
920 cur->name = xmlStrdup(ent->name);
921 if (ent->ExternalID != NULL)
922 cur->ExternalID = xmlStrdup(ent->ExternalID);
923 if (ent->SystemID != NULL)
924 cur->SystemID = xmlStrdup(ent->SystemID);
925 if (ent->content != NULL)
926 cur->content = xmlStrdup(ent->content);
927 if (ent->orig != NULL)
928 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000929 if (ent->URI != NULL)
930 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000931 return(cur);
932}
933
934/**
935 * xmlCopyEntitiesTable:
936 * @table: An entity table
937 *
938 * Build a copy of an entity table.
939 *
940 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
941 */
942xmlEntitiesTablePtr
943xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
944 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
945}
Daniel Veillard652327a2003-09-29 18:02:38 +0000946#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000947
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000948#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard18ab8722003-12-09 22:51:37 +0000949
950/**
951 * xmlDumpEntityContent:
952 * @buf: An XML buffer.
953 * @content: The entity content.
954 *
955 * This will dump the quoted string value, taking care of the special
956 * treatment required by %
957 */
958static void
959xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
960 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
961 if (xmlStrchr(content, '%')) {
962 const xmlChar * base, *cur;
963
964 xmlBufferCCat(buf, "\"");
965 base = cur = content;
966 while (*cur != 0) {
967 if (*cur == '"') {
968 if (base != cur)
969 xmlBufferAdd(buf, base, cur - base);
970 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
971 cur++;
972 base = cur;
973 } else if (*cur == '%') {
974 if (base != cur)
975 xmlBufferAdd(buf, base, cur - base);
976 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
977 cur++;
978 base = cur;
979 } else {
980 cur++;
981 }
982 }
983 if (base != cur)
984 xmlBufferAdd(buf, base, cur - base);
985 xmlBufferCCat(buf, "\"");
986 } else {
987 xmlBufferWriteQuotedString(buf, content);
988 }
989}
990
Owen Taylor3473f882001-02-23 17:55:21 +0000991/**
992 * xmlDumpEntityDecl:
993 * @buf: An XML buffer.
994 * @ent: An entity table
995 *
996 * This will dump the content of the entity table as an XML DTD definition
997 */
998void
999xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001000 if ((buf == NULL) || (ent == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001001 switch (ent->etype) {
1002 case XML_INTERNAL_GENERAL_ENTITY:
1003 xmlBufferWriteChar(buf, "<!ENTITY ");
1004 xmlBufferWriteCHAR(buf, ent->name);
1005 xmlBufferWriteChar(buf, " ");
1006 if (ent->orig != NULL)
1007 xmlBufferWriteQuotedString(buf, ent->orig);
1008 else
Daniel Veillard18ab8722003-12-09 22:51:37 +00001009 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001010 xmlBufferWriteChar(buf, ">\n");
1011 break;
1012 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1013 xmlBufferWriteChar(buf, "<!ENTITY ");
1014 xmlBufferWriteCHAR(buf, ent->name);
1015 if (ent->ExternalID != NULL) {
1016 xmlBufferWriteChar(buf, " PUBLIC ");
1017 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1018 xmlBufferWriteChar(buf, " ");
1019 xmlBufferWriteQuotedString(buf, ent->SystemID);
1020 } else {
1021 xmlBufferWriteChar(buf, " SYSTEM ");
1022 xmlBufferWriteQuotedString(buf, ent->SystemID);
1023 }
1024 xmlBufferWriteChar(buf, ">\n");
1025 break;
1026 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1027 xmlBufferWriteChar(buf, "<!ENTITY ");
1028 xmlBufferWriteCHAR(buf, ent->name);
1029 if (ent->ExternalID != NULL) {
1030 xmlBufferWriteChar(buf, " PUBLIC ");
1031 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1032 xmlBufferWriteChar(buf, " ");
1033 xmlBufferWriteQuotedString(buf, ent->SystemID);
1034 } else {
1035 xmlBufferWriteChar(buf, " SYSTEM ");
1036 xmlBufferWriteQuotedString(buf, ent->SystemID);
1037 }
1038 if (ent->content != NULL) { /* Should be true ! */
1039 xmlBufferWriteChar(buf, " NDATA ");
1040 if (ent->orig != NULL)
1041 xmlBufferWriteCHAR(buf, ent->orig);
1042 else
1043 xmlBufferWriteCHAR(buf, ent->content);
1044 }
1045 xmlBufferWriteChar(buf, ">\n");
1046 break;
1047 case XML_INTERNAL_PARAMETER_ENTITY:
1048 xmlBufferWriteChar(buf, "<!ENTITY % ");
1049 xmlBufferWriteCHAR(buf, ent->name);
1050 xmlBufferWriteChar(buf, " ");
1051 if (ent->orig == NULL)
Daniel Veillard18ab8722003-12-09 22:51:37 +00001052 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001053 else
1054 xmlBufferWriteQuotedString(buf, ent->orig);
1055 xmlBufferWriteChar(buf, ">\n");
1056 break;
1057 case XML_EXTERNAL_PARAMETER_ENTITY:
1058 xmlBufferWriteChar(buf, "<!ENTITY % ");
1059 xmlBufferWriteCHAR(buf, ent->name);
1060 if (ent->ExternalID != NULL) {
1061 xmlBufferWriteChar(buf, " PUBLIC ");
1062 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1063 xmlBufferWriteChar(buf, " ");
1064 xmlBufferWriteQuotedString(buf, ent->SystemID);
1065 } else {
1066 xmlBufferWriteChar(buf, " SYSTEM ");
1067 xmlBufferWriteQuotedString(buf, ent->SystemID);
1068 }
1069 xmlBufferWriteChar(buf, ">\n");
1070 break;
1071 default:
Daniel Veillardce244ad2004-11-05 10:03:46 +00001072 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
1073 "xmlDumpEntitiesDecl: internal: unknown type entity type");
Owen Taylor3473f882001-02-23 17:55:21 +00001074 }
1075}
1076
1077/**
William M. Brack9e660592003-10-20 14:56:06 +00001078 * xmlDumpEntityDeclScan:
1079 * @ent: An entity table
1080 * @buf: An XML buffer.
1081 *
1082 * When using the hash table scan function, arguments need to be reversed
1083 */
1084static void
1085xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
1086 xmlDumpEntityDecl(buf, ent);
1087}
1088
1089/**
Owen Taylor3473f882001-02-23 17:55:21 +00001090 * xmlDumpEntitiesTable:
1091 * @buf: An XML buffer.
1092 * @table: An entity table
1093 *
1094 * This will dump the content of the entity table as an XML DTD definition
1095 */
1096void
1097xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00001098 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001099}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001100#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001101#define bottom_entities
1102#include "elfgcchack.h"