blob: 6a0e38d7ef0d9bbc7932f669ce336d1d33cff2d0 [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,
34 NULL, NULL, NULL, NULL, 0
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,
41 NULL, NULL, NULL, NULL, 0
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,
48 NULL, NULL, NULL, NULL, 0
49};
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,
55 NULL, NULL, NULL, NULL, 0
56};
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,
62 NULL, NULL, NULL, NULL, 0
63};
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/*
142 * xmlAddEntity : register a new entity for an entities table.
143 */
144static xmlEntityPtr
145xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
146 const xmlChar *ExternalID, const xmlChar *SystemID,
147 const xmlChar *content) {
Daniel Veillard7da92702005-01-23 20:15:53 +0000148 xmlDictPtr dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000149 xmlEntitiesTablePtr table = NULL;
150 xmlEntityPtr ret;
151
152 if (name == NULL)
153 return(NULL);
Daniel Veillard7da92702005-01-23 20:15:53 +0000154 if (dtd == NULL)
155 return(NULL);
156 if (dtd->doc != NULL)
157 dict = dtd->doc->dict;
158
Owen Taylor3473f882001-02-23 17:55:21 +0000159 switch (type) {
160 case XML_INTERNAL_GENERAL_ENTITY:
161 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
162 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
163 if (dtd->entities == NULL)
Daniel Veillard316a5c32005-01-23 22:56:39 +0000164 dtd->entities = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +0000165 table = dtd->entities;
166 break;
167 case XML_INTERNAL_PARAMETER_ENTITY:
168 case XML_EXTERNAL_PARAMETER_ENTITY:
169 if (dtd->pentities == NULL)
Daniel Veillard316a5c32005-01-23 22:56:39 +0000170 dtd->pentities = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +0000171 table = dtd->pentities;
172 break;
173 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000174 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000175 }
176 if (table == NULL)
177 return(NULL);
178 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
179 if (ret == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000180 xmlEntitiesErrMemory("xmlAddEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000181 return(NULL);
182 }
183 memset(ret, 0, sizeof(xmlEntity));
184 ret->type = XML_ENTITY_DECL;
185
186 /*
187 * fill the structure.
188 */
Owen Taylor3473f882001-02-23 17:55:21 +0000189 ret->etype = (xmlEntityType) type;
Daniel Veillard7da92702005-01-23 20:15:53 +0000190 if (dict == NULL) {
191 ret->name = xmlStrdup(name);
192 if (ExternalID != NULL)
193 ret->ExternalID = xmlStrdup(ExternalID);
194 if (SystemID != NULL)
195 ret->SystemID = xmlStrdup(SystemID);
196 } else {
197 ret->name = xmlDictLookup(dict, name, -1);
198 if (ExternalID != NULL)
199 ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
200 if (SystemID != NULL)
201 ret->SystemID = xmlDictLookup(dict, SystemID, -1);
202 }
Owen Taylor3473f882001-02-23 17:55:21 +0000203 if (content != NULL) {
204 ret->length = xmlStrlen(content);
Daniel Veillard7da92702005-01-23 20:15:53 +0000205 if ((dict != NULL) && (ret->length < 5))
206 ret->content = (xmlChar *)
207 xmlDictLookup(dict, content, ret->length);
208 else
209 ret->content = xmlStrndup(content, ret->length);
Owen Taylor3473f882001-02-23 17:55:21 +0000210 } else {
211 ret->length = 0;
212 ret->content = NULL;
213 }
214 ret->URI = NULL; /* to be computed by the layer knowing
215 the defining entity */
216 ret->orig = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +0000217 ret->owner = 0;
Daniel Veillard7da92702005-01-23 20:15:53 +0000218 ret->doc = dtd->doc;
Owen Taylor3473f882001-02-23 17:55:21 +0000219
220 if (xmlHashAddEntry(table, name, ret)) {
221 /*
222 * entity was already defined at another level.
223 */
224 xmlFreeEntity(ret);
225 return(NULL);
226 }
227 return(ret);
228}
229
230/**
Owen Taylor3473f882001-02-23 17:55:21 +0000231 * xmlGetPredefinedEntity:
232 * @name: the entity name
233 *
234 * Check whether this name is an predefined entity.
235 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000236 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000237 */
238xmlEntityPtr
239xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000240 if (name == NULL) return(NULL);
241 switch (name[0]) {
242 case 'l':
243 if (xmlStrEqual(name, BAD_CAST "lt"))
244 return(&xmlEntityLt);
245 break;
246 case 'g':
247 if (xmlStrEqual(name, BAD_CAST "gt"))
248 return(&xmlEntityGt);
249 break;
250 case 'a':
251 if (xmlStrEqual(name, BAD_CAST "amp"))
252 return(&xmlEntityAmp);
253 if (xmlStrEqual(name, BAD_CAST "apos"))
254 return(&xmlEntityApos);
255 break;
256 case 'q':
257 if (xmlStrEqual(name, BAD_CAST "quot"))
258 return(&xmlEntityQuot);
259 break;
260 default:
261 break;
262 }
263 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000264}
265
266/**
267 * xmlAddDtdEntity:
268 * @doc: the document
269 * @name: the entity name
270 * @type: the entity type XML_xxx_yyy_ENTITY
271 * @ExternalID: the entity external ID if available
272 * @SystemID: the entity system ID if available
273 * @content: the entity content
274 *
275 * Register a new entity for this document DTD external subset.
276 *
277 * Returns a pointer to the entity or NULL in case of error
278 */
279xmlEntityPtr
280xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
281 const xmlChar *ExternalID, const xmlChar *SystemID,
282 const xmlChar *content) {
283 xmlEntityPtr ret;
284 xmlDtdPtr dtd;
285
286 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000287 xmlEntitiesErr(XML_DTD_NO_DOC,
288 "xmlAddDtdEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000289 return(NULL);
290 }
291 if (doc->extSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000292 xmlEntitiesErr(XML_DTD_NO_DTD,
293 "xmlAddDtdEntity: document without external subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000294 return(NULL);
295 }
296 dtd = doc->extSubset;
297 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
298 if (ret == NULL) return(NULL);
299
300 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000301 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000302 */
303 ret->parent = dtd;
304 ret->doc = dtd->doc;
305 if (dtd->last == NULL) {
306 dtd->children = dtd->last = (xmlNodePtr) ret;
307 } else {
308 dtd->last->next = (xmlNodePtr) ret;
309 ret->prev = dtd->last;
310 dtd->last = (xmlNodePtr) ret;
311 }
312 return(ret);
313}
314
315/**
316 * xmlAddDocEntity:
317 * @doc: the document
318 * @name: the entity name
319 * @type: the entity type XML_xxx_yyy_ENTITY
320 * @ExternalID: the entity external ID if available
321 * @SystemID: the entity system ID if available
322 * @content: the entity content
323 *
324 * Register a new entity for this document.
325 *
326 * Returns a pointer to the entity or NULL in case of error
327 */
328xmlEntityPtr
329xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
330 const xmlChar *ExternalID, const xmlChar *SystemID,
331 const xmlChar *content) {
332 xmlEntityPtr ret;
333 xmlDtdPtr dtd;
334
335 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000336 xmlEntitiesErr(XML_DTD_NO_DOC,
337 "xmlAddDocEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000338 return(NULL);
339 }
340 if (doc->intSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000341 xmlEntitiesErr(XML_DTD_NO_DTD,
342 "xmlAddDocEntity: document without internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000343 return(NULL);
344 }
345 dtd = doc->intSubset;
346 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
347 if (ret == NULL) return(NULL);
348
349 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000350 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000351 */
352 ret->parent = dtd;
353 ret->doc = dtd->doc;
354 if (dtd->last == NULL) {
355 dtd->children = dtd->last = (xmlNodePtr) ret;
356 } else {
357 dtd->last->next = (xmlNodePtr) ret;
358 ret->prev = dtd->last;
359 dtd->last = (xmlNodePtr) ret;
360 }
361 return(ret);
362}
363
364/**
365 * xmlGetEntityFromTable:
366 * @table: an entity table
367 * @name: the entity name
368 * @parameter: look for parameter entities
369 *
370 * Do an entity lookup in the table.
371 * returns the corresponding parameter entity, if found.
372 *
373 * Returns A pointer to the entity structure or NULL if not found.
374 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000375static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000376xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
377 return((xmlEntityPtr) xmlHashLookup(table, name));
378}
379
380/**
381 * xmlGetParameterEntity:
382 * @doc: the document referencing the entity
383 * @name: the entity name
384 *
385 * Do an entity lookup in the internal and external subsets and
386 * returns the corresponding parameter entity, if found.
387 *
388 * Returns A pointer to the entity structure or NULL if not found.
389 */
390xmlEntityPtr
391xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
392 xmlEntitiesTablePtr table;
393 xmlEntityPtr ret;
394
Daniel Veillard36065812002-01-24 15:02:46 +0000395 if (doc == NULL)
396 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000397 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
398 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
399 ret = xmlGetEntityFromTable(table, name);
400 if (ret != NULL)
401 return(ret);
402 }
403 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
404 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
405 return(xmlGetEntityFromTable(table, name));
406 }
407 return(NULL);
408}
409
410/**
411 * xmlGetDtdEntity:
412 * @doc: the document referencing the entity
413 * @name: the entity name
414 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000415 * Do an entity lookup in the DTD entity hash table and
Owen Taylor3473f882001-02-23 17:55:21 +0000416 * returns the corresponding entity, if found.
Daniel Veillard36065812002-01-24 15:02:46 +0000417 * Note: the first argument is the document node, not the DTD node.
Owen Taylor3473f882001-02-23 17:55:21 +0000418 *
419 * Returns A pointer to the entity structure or NULL if not found.
420 */
421xmlEntityPtr
422xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
423 xmlEntitiesTablePtr table;
424
Daniel Veillard36065812002-01-24 15:02:46 +0000425 if (doc == NULL)
426 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000427 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
428 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
429 return(xmlGetEntityFromTable(table, name));
430 }
431 return(NULL);
432}
433
434/**
435 * xmlGetDocEntity:
436 * @doc: the document referencing the entity
437 * @name: the entity name
438 *
439 * Do an entity lookup in the document entity hash table and
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000440 * returns the corresponding entity, otherwise a lookup is done
Owen Taylor3473f882001-02-23 17:55:21 +0000441 * in the predefined entities too.
442 *
443 * Returns A pointer to the entity structure or NULL if not found.
444 */
445xmlEntityPtr
446xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
447 xmlEntityPtr cur;
448 xmlEntitiesTablePtr table;
449
450 if (doc != NULL) {
451 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
452 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
453 cur = xmlGetEntityFromTable(table, name);
454 if (cur != NULL)
455 return(cur);
456 }
Daniel Veillard28757702002-02-18 11:19:30 +0000457 if (doc->standalone != 1) {
458 if ((doc->extSubset != NULL) &&
459 (doc->extSubset->entities != NULL)) {
460 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
461 cur = xmlGetEntityFromTable(table, name);
462 if (cur != NULL)
463 return(cur);
464 }
Owen Taylor3473f882001-02-23 17:55:21 +0000465 }
466 }
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000467 return(xmlGetPredefinedEntity(name));
Owen Taylor3473f882001-02-23 17:55:21 +0000468}
469
470/*
Owen Taylor3473f882001-02-23 17:55:21 +0000471 * Macro used to grow the current buffer.
472 */
473#define growBufferReentrant() { \
474 buffer_size *= 2; \
475 buffer = (xmlChar *) \
476 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
477 if (buffer == NULL) { \
Daniel Veillardce244ad2004-11-05 10:03:46 +0000478 xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\
Owen Taylor3473f882001-02-23 17:55:21 +0000479 return(NULL); \
480 } \
481}
482
483
484/**
485 * xmlEncodeEntitiesReentrant:
486 * @doc: the document containing the string
487 * @input: A string to convert to XML.
488 *
489 * Do a global encoding of a string, replacing the predefined entities
490 * and non ASCII values with their entities and CharRef counterparts.
491 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
492 * must be deallocated.
493 *
494 * Returns A newly allocated string with the substitution done.
495 */
496xmlChar *
497xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
498 const xmlChar *cur = input;
499 xmlChar *buffer = NULL;
500 xmlChar *out = NULL;
501 int buffer_size = 0;
502 int html = 0;
503
504 if (input == NULL) return(NULL);
505 if (doc != NULL)
506 html = (doc->type == XML_HTML_DOCUMENT_NODE);
507
508 /*
509 * allocate an translation buffer.
510 */
511 buffer_size = 1000;
512 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
513 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000514 xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000515 return(NULL);
516 }
517 out = buffer;
518
519 while (*cur != '\0') {
520 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000521 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000522
523 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000524 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000525 }
526
527 /*
528 * By default one have to encode at least '<', '>', '"' and '&' !
529 */
530 if (*cur == '<') {
531 *out++ = '&';
532 *out++ = 'l';
533 *out++ = 't';
534 *out++ = ';';
535 } else if (*cur == '>') {
536 *out++ = '&';
537 *out++ = 'g';
538 *out++ = 't';
539 *out++ = ';';
540 } else if (*cur == '&') {
541 *out++ = '&';
542 *out++ = 'a';
543 *out++ = 'm';
544 *out++ = 'p';
545 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000546 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Daniel Veillard0046c0f2003-02-23 13:52:30 +0000547 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000548 /*
549 * default case, just copy !
550 */
551 *out++ = *cur;
552 } else if (*cur >= 0x80) {
Daniel Veillard122376b2001-04-24 12:12:30 +0000553 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000554 /*
555 * Bjørn Reese <br@sseusa.com> provided the patch
556 xmlChar xc;
557 xc = (*cur & 0x3F) << 6;
558 if (cur[1] != 0) {
559 xc += *(++cur) & 0x3F;
560 *out++ = xc;
561 } else
562 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000563 *out++ = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +0000564 } else {
565 /*
566 * We assume we have UTF-8 input.
567 */
Daniel Veillardb2517d82003-10-01 19:13:56 +0000568 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000569 int val = 0, l = 1;
570
571 if (*cur < 0xC0) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000572 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
573 "xmlEncodeEntitiesReentrant : input not UTF-8");
Daniel Veillard122376b2001-04-24 12:12:30 +0000574 if (doc != NULL)
575 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000576 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000577 buf[sizeof(buf) - 1] = 0;
578 ptr = buf;
579 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard05c13a22001-09-09 08:38:09 +0000580 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000581 continue;
582 } else if (*cur < 0xE0) {
583 val = (cur[0]) & 0x1F;
584 val <<= 6;
585 val |= (cur[1]) & 0x3F;
586 l = 2;
587 } else if (*cur < 0xF0) {
588 val = (cur[0]) & 0x0F;
589 val <<= 6;
590 val |= (cur[1]) & 0x3F;
591 val <<= 6;
592 val |= (cur[2]) & 0x3F;
593 l = 3;
594 } else if (*cur < 0xF8) {
595 val = (cur[0]) & 0x07;
596 val <<= 6;
597 val |= (cur[1]) & 0x3F;
598 val <<= 6;
599 val |= (cur[2]) & 0x3F;
600 val <<= 6;
601 val |= (cur[3]) & 0x3F;
602 l = 4;
603 }
604 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000605 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
Owen Taylor3473f882001-02-23 17:55:21 +0000606 "xmlEncodeEntitiesReentrant : char out of range\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000607 if (doc != NULL)
608 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000609 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000610 buf[sizeof(buf) - 1] = 0;
611 ptr = buf;
612 while (*ptr != 0) *out++ = *ptr++;
613 cur++;
614 continue;
615 }
616 /*
617 * We could do multiple things here. Just save as a char ref
618 */
Daniel Veillard2728f842006-03-09 16:49:24 +0000619 snprintf(buf, sizeof(buf), "&#x%X;", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000620 buf[sizeof(buf) - 1] = 0;
621 ptr = buf;
622 while (*ptr != 0) *out++ = *ptr++;
623 cur += l;
624 continue;
625 }
William M. Brack76e95df2003-10-18 16:20:14 +0000626 } else if (IS_BYTE_CHAR(*cur)) {
Daniel Veillardb2517d82003-10-01 19:13:56 +0000627 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000628
Owen Taylor3473f882001-02-23 17:55:21 +0000629 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000630 buf[sizeof(buf) - 1] = 0;
631 ptr = buf;
632 while (*ptr != 0) *out++ = *ptr++;
633 }
Owen Taylor3473f882001-02-23 17:55:21 +0000634 cur++;
635 }
636 *out++ = 0;
637 return(buffer);
638}
639
640/**
641 * xmlEncodeSpecialChars:
642 * @doc: the document containing the string
643 * @input: A string to convert to XML.
644 *
645 * Do a global encoding of a string, replacing the predefined entities
646 * this routine is reentrant, and result must be deallocated.
647 *
648 * Returns A newly allocated string with the substitution done.
649 */
650xmlChar *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000651xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Owen Taylor3473f882001-02-23 17:55:21 +0000652 const xmlChar *cur = input;
653 xmlChar *buffer = NULL;
654 xmlChar *out = NULL;
655 int buffer_size = 0;
William M. Brack899e64a2003-09-26 18:03:42 +0000656 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000657
658 /*
659 * allocate an translation buffer.
660 */
661 buffer_size = 1000;
662 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
663 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000664 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000665 return(NULL);
666 }
667 out = buffer;
668
669 while (*cur != '\0') {
670 if (out - buffer > buffer_size - 10) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000671 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000672
673 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000674 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000675 }
676
677 /*
678 * By default one have to encode at least '<', '>', '"' and '&' !
679 */
680 if (*cur == '<') {
681 *out++ = '&';
682 *out++ = 'l';
683 *out++ = 't';
684 *out++ = ';';
685 } else if (*cur == '>') {
686 *out++ = '&';
687 *out++ = 'g';
688 *out++ = 't';
689 *out++ = ';';
690 } else if (*cur == '&') {
691 *out++ = '&';
692 *out++ = 'a';
693 *out++ = 'm';
694 *out++ = 'p';
695 *out++ = ';';
696 } else if (*cur == '"') {
697 *out++ = '&';
698 *out++ = 'q';
699 *out++ = 'u';
700 *out++ = 'o';
701 *out++ = 't';
702 *out++ = ';';
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000703 } else if (*cur == '\r') {
704 *out++ = '&';
705 *out++ = '#';
706 *out++ = '1';
707 *out++ = '3';
708 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000709 } else {
710 /*
711 * Works because on UTF-8, all extended sequences cannot
712 * result in bytes in the ASCII range.
713 */
714 *out++ = *cur;
715 }
716 cur++;
717 }
718 *out++ = 0;
719 return(buffer);
720}
721
722/**
723 * xmlCreateEntitiesTable:
724 *
725 * create and initialize an empty entities hash table.
Daniel Veillard316a5c32005-01-23 22:56:39 +0000726 * This really doesn't make sense and should be deprecated
Owen Taylor3473f882001-02-23 17:55:21 +0000727 *
728 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
729 */
730xmlEntitiesTablePtr
731xmlCreateEntitiesTable(void) {
732 return((xmlEntitiesTablePtr) xmlHashCreate(0));
733}
734
735/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000736 * xmlFreeEntityWrapper:
737 * @entity: An entity
738 * @name: its name
739 *
740 * Deallocate the memory used by an entities in the hash table.
741 */
742static void
743xmlFreeEntityWrapper(xmlEntityPtr entity,
744 const xmlChar *name ATTRIBUTE_UNUSED) {
745 if (entity != NULL)
746 xmlFreeEntity(entity);
747}
748
749/**
Owen Taylor3473f882001-02-23 17:55:21 +0000750 * xmlFreeEntitiesTable:
751 * @table: An entity table
752 *
753 * Deallocate the memory used by an entities hash table.
754 */
755void
756xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000757 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000758}
759
Daniel Veillard652327a2003-09-29 18:02:38 +0000760#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000761/**
762 * xmlCopyEntity:
763 * @ent: An entity
764 *
765 * Build a copy of an entity
766 *
767 * Returns the new xmlEntitiesPtr or NULL in case of error.
768 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000769static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000770xmlCopyEntity(xmlEntityPtr ent) {
771 xmlEntityPtr cur;
772
773 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
774 if (cur == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000775 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000776 return(NULL);
777 }
778 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000779 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000780
781 cur->etype = ent->etype;
782 if (ent->name != NULL)
783 cur->name = xmlStrdup(ent->name);
784 if (ent->ExternalID != NULL)
785 cur->ExternalID = xmlStrdup(ent->ExternalID);
786 if (ent->SystemID != NULL)
787 cur->SystemID = xmlStrdup(ent->SystemID);
788 if (ent->content != NULL)
789 cur->content = xmlStrdup(ent->content);
790 if (ent->orig != NULL)
791 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000792 if (ent->URI != NULL)
793 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000794 return(cur);
795}
796
797/**
798 * xmlCopyEntitiesTable:
799 * @table: An entity table
800 *
801 * Build a copy of an entity table.
802 *
803 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
804 */
805xmlEntitiesTablePtr
806xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
807 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
808}
Daniel Veillard652327a2003-09-29 18:02:38 +0000809#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000810
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000811#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard18ab8722003-12-09 22:51:37 +0000812
813/**
814 * xmlDumpEntityContent:
815 * @buf: An XML buffer.
816 * @content: The entity content.
817 *
818 * This will dump the quoted string value, taking care of the special
819 * treatment required by %
820 */
821static void
822xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
823 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
824 if (xmlStrchr(content, '%')) {
825 const xmlChar * base, *cur;
826
827 xmlBufferCCat(buf, "\"");
828 base = cur = content;
829 while (*cur != 0) {
830 if (*cur == '"') {
831 if (base != cur)
832 xmlBufferAdd(buf, base, cur - base);
833 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
834 cur++;
835 base = cur;
836 } else if (*cur == '%') {
837 if (base != cur)
838 xmlBufferAdd(buf, base, cur - base);
839 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
840 cur++;
841 base = cur;
842 } else {
843 cur++;
844 }
845 }
846 if (base != cur)
847 xmlBufferAdd(buf, base, cur - base);
848 xmlBufferCCat(buf, "\"");
849 } else {
850 xmlBufferWriteQuotedString(buf, content);
851 }
852}
853
Owen Taylor3473f882001-02-23 17:55:21 +0000854/**
855 * xmlDumpEntityDecl:
856 * @buf: An XML buffer.
857 * @ent: An entity table
858 *
859 * This will dump the content of the entity table as an XML DTD definition
860 */
861void
862xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
Daniel Veillardce682bc2004-11-05 17:22:25 +0000863 if ((buf == NULL) || (ent == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000864 switch (ent->etype) {
865 case XML_INTERNAL_GENERAL_ENTITY:
866 xmlBufferWriteChar(buf, "<!ENTITY ");
867 xmlBufferWriteCHAR(buf, ent->name);
868 xmlBufferWriteChar(buf, " ");
869 if (ent->orig != NULL)
870 xmlBufferWriteQuotedString(buf, ent->orig);
871 else
Daniel Veillard18ab8722003-12-09 22:51:37 +0000872 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000873 xmlBufferWriteChar(buf, ">\n");
874 break;
875 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
876 xmlBufferWriteChar(buf, "<!ENTITY ");
877 xmlBufferWriteCHAR(buf, ent->name);
878 if (ent->ExternalID != NULL) {
879 xmlBufferWriteChar(buf, " PUBLIC ");
880 xmlBufferWriteQuotedString(buf, ent->ExternalID);
881 xmlBufferWriteChar(buf, " ");
882 xmlBufferWriteQuotedString(buf, ent->SystemID);
883 } else {
884 xmlBufferWriteChar(buf, " SYSTEM ");
885 xmlBufferWriteQuotedString(buf, ent->SystemID);
886 }
887 xmlBufferWriteChar(buf, ">\n");
888 break;
889 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
890 xmlBufferWriteChar(buf, "<!ENTITY ");
891 xmlBufferWriteCHAR(buf, ent->name);
892 if (ent->ExternalID != NULL) {
893 xmlBufferWriteChar(buf, " PUBLIC ");
894 xmlBufferWriteQuotedString(buf, ent->ExternalID);
895 xmlBufferWriteChar(buf, " ");
896 xmlBufferWriteQuotedString(buf, ent->SystemID);
897 } else {
898 xmlBufferWriteChar(buf, " SYSTEM ");
899 xmlBufferWriteQuotedString(buf, ent->SystemID);
900 }
901 if (ent->content != NULL) { /* Should be true ! */
902 xmlBufferWriteChar(buf, " NDATA ");
903 if (ent->orig != NULL)
904 xmlBufferWriteCHAR(buf, ent->orig);
905 else
906 xmlBufferWriteCHAR(buf, ent->content);
907 }
908 xmlBufferWriteChar(buf, ">\n");
909 break;
910 case XML_INTERNAL_PARAMETER_ENTITY:
911 xmlBufferWriteChar(buf, "<!ENTITY % ");
912 xmlBufferWriteCHAR(buf, ent->name);
913 xmlBufferWriteChar(buf, " ");
914 if (ent->orig == NULL)
Daniel Veillard18ab8722003-12-09 22:51:37 +0000915 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000916 else
917 xmlBufferWriteQuotedString(buf, ent->orig);
918 xmlBufferWriteChar(buf, ">\n");
919 break;
920 case XML_EXTERNAL_PARAMETER_ENTITY:
921 xmlBufferWriteChar(buf, "<!ENTITY % ");
922 xmlBufferWriteCHAR(buf, ent->name);
923 if (ent->ExternalID != NULL) {
924 xmlBufferWriteChar(buf, " PUBLIC ");
925 xmlBufferWriteQuotedString(buf, ent->ExternalID);
926 xmlBufferWriteChar(buf, " ");
927 xmlBufferWriteQuotedString(buf, ent->SystemID);
928 } else {
929 xmlBufferWriteChar(buf, " SYSTEM ");
930 xmlBufferWriteQuotedString(buf, ent->SystemID);
931 }
932 xmlBufferWriteChar(buf, ">\n");
933 break;
934 default:
Daniel Veillardce244ad2004-11-05 10:03:46 +0000935 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
936 "xmlDumpEntitiesDecl: internal: unknown type entity type");
Owen Taylor3473f882001-02-23 17:55:21 +0000937 }
938}
939
940/**
William M. Brack9e660592003-10-20 14:56:06 +0000941 * xmlDumpEntityDeclScan:
942 * @ent: An entity table
943 * @buf: An XML buffer.
944 *
945 * When using the hash table scan function, arguments need to be reversed
946 */
947static void
948xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
949 xmlDumpEntityDecl(buf, ent);
950}
951
952/**
Owen Taylor3473f882001-02-23 17:55:21 +0000953 * xmlDumpEntitiesTable:
954 * @buf: An XML buffer.
955 * @table: An entity table
956 *
957 * This will dump the content of the entity table as an XML DTD definition
958 */
959void
960xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +0000961 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000962}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000963#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +0000964#define bottom_entities
965#include "elfgcchack.h"