blob: 913a4e684f4a38ac3f8929e62c4b129ab5ca81ac [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>
Owen Taylor3473f882001-02-23 17:55:21 +000023
Owen Taylor3473f882001-02-23 17:55:21 +000024/*
25 * The XML predefined entities.
26 */
27
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000028static xmlEntity xmlEntityLt = {
29 NULL, XML_ENTITY_DECL, BAD_CAST "lt",
30 NULL, NULL, NULL, NULL, NULL, NULL,
31 BAD_CAST "<", BAD_CAST "<", 1,
32 XML_INTERNAL_PREDEFINED_ENTITY,
33 NULL, NULL, NULL, NULL, 0
Owen Taylor3473f882001-02-23 17:55:21 +000034};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000035static xmlEntity xmlEntityGt = {
36 NULL, XML_ENTITY_DECL, BAD_CAST "gt",
37 NULL, NULL, NULL, NULL, NULL, NULL,
38 BAD_CAST ">", BAD_CAST ">", 1,
39 XML_INTERNAL_PREDEFINED_ENTITY,
40 NULL, NULL, NULL, NULL, 0
Owen Taylor3473f882001-02-23 17:55:21 +000041};
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +000042static xmlEntity xmlEntityAmp = {
43 NULL, XML_ENTITY_DECL, BAD_CAST "amp",
44 NULL, NULL, NULL, NULL, NULL, NULL,
45 BAD_CAST "&", BAD_CAST "&", 1,
46 XML_INTERNAL_PREDEFINED_ENTITY,
47 NULL, NULL, NULL, NULL, 0
48};
49static xmlEntity xmlEntityQuot = {
50 NULL, XML_ENTITY_DECL, BAD_CAST "quot",
51 NULL, NULL, NULL, NULL, NULL, NULL,
52 BAD_CAST "\"", BAD_CAST "\"", 1,
53 XML_INTERNAL_PREDEFINED_ENTITY,
54 NULL, NULL, NULL, NULL, 0
55};
56static xmlEntity xmlEntityApos = {
57 NULL, XML_ENTITY_DECL, BAD_CAST "apos",
58 NULL, NULL, NULL, NULL, NULL, NULL,
59 BAD_CAST "'", BAD_CAST "'", 1,
60 XML_INTERNAL_PREDEFINED_ENTITY,
61 NULL, NULL, NULL, NULL, 0
62};
Owen Taylor3473f882001-02-23 17:55:21 +000063
Daniel Veillardce244ad2004-11-05 10:03:46 +000064/**
65 * xmlEntitiesErrMemory:
66 * @extra: extra informations
67 *
68 * Handle an out of memory condition
69 */
70static void
71xmlEntitiesErrMemory(const char *extra)
72{
73 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
74}
75
76/**
77 * xmlEntitiesErr:
78 * @code: the error code
79 * @msg: the message
80 *
81 * Handle an out of memory condition
82 */
83static void
84xmlEntitiesErr(xmlParserErrors code, const char *msg)
85{
Daniel Veillardce244ad2004-11-05 10:03:46 +000086 __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);
87}
88
Owen Taylor3473f882001-02-23 17:55:21 +000089/*
90 * xmlFreeEntity : clean-up an entity record.
91 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000092static void xmlFreeEntity(xmlEntityPtr entity) {
Owen Taylor3473f882001-02-23 17:55:21 +000093 if (entity == NULL) return;
94
Daniel Veillard2d84a892002-12-30 00:01:08 +000095 if ((entity->children) && (entity->owner == 1) &&
Daniel Veillard22090732001-07-16 00:06:07 +000096 (entity == (xmlEntityPtr) entity->children->parent))
Owen Taylor3473f882001-02-23 17:55:21 +000097 xmlFreeNodeList(entity->children);
98 if (entity->name != NULL)
99 xmlFree((char *) entity->name);
100 if (entity->ExternalID != NULL)
101 xmlFree((char *) entity->ExternalID);
102 if (entity->SystemID != NULL)
103 xmlFree((char *) entity->SystemID);
104 if (entity->URI != NULL)
105 xmlFree((char *) entity->URI);
106 if (entity->content != NULL)
107 xmlFree((char *) entity->content);
108 if (entity->orig != NULL)
109 xmlFree((char *) entity->orig);
Owen Taylor3473f882001-02-23 17:55:21 +0000110 xmlFree(entity);
111}
112
113/*
114 * xmlAddEntity : register a new entity for an entities table.
115 */
116static xmlEntityPtr
117xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
118 const xmlChar *ExternalID, const xmlChar *SystemID,
119 const xmlChar *content) {
120 xmlEntitiesTablePtr table = NULL;
121 xmlEntityPtr ret;
122
123 if (name == NULL)
124 return(NULL);
125 switch (type) {
126 case XML_INTERNAL_GENERAL_ENTITY:
127 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
128 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
129 if (dtd->entities == NULL)
130 dtd->entities = xmlHashCreate(0);
131 table = dtd->entities;
132 break;
133 case XML_INTERNAL_PARAMETER_ENTITY:
134 case XML_EXTERNAL_PARAMETER_ENTITY:
135 if (dtd->pentities == NULL)
136 dtd->pentities = xmlHashCreate(0);
137 table = dtd->pentities;
138 break;
139 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000140 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000141 }
142 if (table == NULL)
143 return(NULL);
144 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
145 if (ret == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000146 xmlEntitiesErrMemory("xmlAddEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000147 return(NULL);
148 }
149 memset(ret, 0, sizeof(xmlEntity));
150 ret->type = XML_ENTITY_DECL;
151
152 /*
153 * fill the structure.
154 */
155 ret->name = xmlStrdup(name);
156 ret->etype = (xmlEntityType) type;
157 if (ExternalID != NULL)
158 ret->ExternalID = xmlStrdup(ExternalID);
159 if (SystemID != NULL)
160 ret->SystemID = xmlStrdup(SystemID);
161 if (content != NULL) {
162 ret->length = xmlStrlen(content);
163 ret->content = xmlStrndup(content, ret->length);
164 } else {
165 ret->length = 0;
166 ret->content = NULL;
167 }
168 ret->URI = NULL; /* to be computed by the layer knowing
169 the defining entity */
170 ret->orig = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +0000171 ret->owner = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000172
173 if (xmlHashAddEntry(table, name, ret)) {
174 /*
175 * entity was already defined at another level.
176 */
177 xmlFreeEntity(ret);
178 return(NULL);
179 }
180 return(ret);
181}
182
183/**
Owen Taylor3473f882001-02-23 17:55:21 +0000184 * xmlGetPredefinedEntity:
185 * @name: the entity name
186 *
187 * Check whether this name is an predefined entity.
188 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000189 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000190 */
191xmlEntityPtr
192xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000193 if (name == NULL) return(NULL);
194 switch (name[0]) {
195 case 'l':
196 if (xmlStrEqual(name, BAD_CAST "lt"))
197 return(&xmlEntityLt);
198 break;
199 case 'g':
200 if (xmlStrEqual(name, BAD_CAST "gt"))
201 return(&xmlEntityGt);
202 break;
203 case 'a':
204 if (xmlStrEqual(name, BAD_CAST "amp"))
205 return(&xmlEntityAmp);
206 if (xmlStrEqual(name, BAD_CAST "apos"))
207 return(&xmlEntityApos);
208 break;
209 case 'q':
210 if (xmlStrEqual(name, BAD_CAST "quot"))
211 return(&xmlEntityQuot);
212 break;
213 default:
214 break;
215 }
216 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000217}
218
219/**
220 * xmlAddDtdEntity:
221 * @doc: the document
222 * @name: the entity name
223 * @type: the entity type XML_xxx_yyy_ENTITY
224 * @ExternalID: the entity external ID if available
225 * @SystemID: the entity system ID if available
226 * @content: the entity content
227 *
228 * Register a new entity for this document DTD external subset.
229 *
230 * Returns a pointer to the entity or NULL in case of error
231 */
232xmlEntityPtr
233xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
234 const xmlChar *ExternalID, const xmlChar *SystemID,
235 const xmlChar *content) {
236 xmlEntityPtr ret;
237 xmlDtdPtr dtd;
238
239 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000240 xmlEntitiesErr(XML_DTD_NO_DOC,
241 "xmlAddDtdEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000242 return(NULL);
243 }
244 if (doc->extSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000245 xmlEntitiesErr(XML_DTD_NO_DTD,
246 "xmlAddDtdEntity: document without external subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000247 return(NULL);
248 }
249 dtd = doc->extSubset;
250 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
251 if (ret == NULL) return(NULL);
252
253 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000254 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000255 */
256 ret->parent = dtd;
257 ret->doc = dtd->doc;
258 if (dtd->last == NULL) {
259 dtd->children = dtd->last = (xmlNodePtr) ret;
260 } else {
261 dtd->last->next = (xmlNodePtr) ret;
262 ret->prev = dtd->last;
263 dtd->last = (xmlNodePtr) ret;
264 }
265 return(ret);
266}
267
268/**
269 * xmlAddDocEntity:
270 * @doc: the document
271 * @name: the entity name
272 * @type: the entity type XML_xxx_yyy_ENTITY
273 * @ExternalID: the entity external ID if available
274 * @SystemID: the entity system ID if available
275 * @content: the entity content
276 *
277 * Register a new entity for this document.
278 *
279 * Returns a pointer to the entity or NULL in case of error
280 */
281xmlEntityPtr
282xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
283 const xmlChar *ExternalID, const xmlChar *SystemID,
284 const xmlChar *content) {
285 xmlEntityPtr ret;
286 xmlDtdPtr dtd;
287
288 if (doc == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000289 xmlEntitiesErr(XML_DTD_NO_DOC,
290 "xmlAddDocEntity: document is NULL");
Owen Taylor3473f882001-02-23 17:55:21 +0000291 return(NULL);
292 }
293 if (doc->intSubset == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000294 xmlEntitiesErr(XML_DTD_NO_DTD,
295 "xmlAddDocEntity: document without internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000296 return(NULL);
297 }
298 dtd = doc->intSubset;
299 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
300 if (ret == NULL) return(NULL);
301
302 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000303 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000304 */
305 ret->parent = dtd;
306 ret->doc = dtd->doc;
307 if (dtd->last == NULL) {
308 dtd->children = dtd->last = (xmlNodePtr) ret;
309 } else {
310 dtd->last->next = (xmlNodePtr) ret;
311 ret->prev = dtd->last;
312 dtd->last = (xmlNodePtr) ret;
313 }
314 return(ret);
315}
316
317/**
318 * xmlGetEntityFromTable:
319 * @table: an entity table
320 * @name: the entity name
321 * @parameter: look for parameter entities
322 *
323 * Do an entity lookup in the table.
324 * returns the corresponding parameter entity, if found.
325 *
326 * Returns A pointer to the entity structure or NULL if not found.
327 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000328static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000329xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
330 return((xmlEntityPtr) xmlHashLookup(table, name));
331}
332
333/**
334 * xmlGetParameterEntity:
335 * @doc: the document referencing the entity
336 * @name: the entity name
337 *
338 * Do an entity lookup in the internal and external subsets and
339 * returns the corresponding parameter entity, if found.
340 *
341 * Returns A pointer to the entity structure or NULL if not found.
342 */
343xmlEntityPtr
344xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
345 xmlEntitiesTablePtr table;
346 xmlEntityPtr ret;
347
Daniel Veillard36065812002-01-24 15:02:46 +0000348 if (doc == NULL)
349 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000350 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
351 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
352 ret = xmlGetEntityFromTable(table, name);
353 if (ret != NULL)
354 return(ret);
355 }
356 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
357 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
358 return(xmlGetEntityFromTable(table, name));
359 }
360 return(NULL);
361}
362
363/**
364 * xmlGetDtdEntity:
365 * @doc: the document referencing the entity
366 * @name: the entity name
367 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000368 * Do an entity lookup in the DTD entity hash table and
Owen Taylor3473f882001-02-23 17:55:21 +0000369 * returns the corresponding entity, if found.
Daniel Veillard36065812002-01-24 15:02:46 +0000370 * Note: the first argument is the document node, not the DTD node.
Owen Taylor3473f882001-02-23 17:55:21 +0000371 *
372 * Returns A pointer to the entity structure or NULL if not found.
373 */
374xmlEntityPtr
375xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
376 xmlEntitiesTablePtr table;
377
Daniel Veillard36065812002-01-24 15:02:46 +0000378 if (doc == NULL)
379 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000380 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
381 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
382 return(xmlGetEntityFromTable(table, name));
383 }
384 return(NULL);
385}
386
387/**
388 * xmlGetDocEntity:
389 * @doc: the document referencing the entity
390 * @name: the entity name
391 *
392 * Do an entity lookup in the document entity hash table and
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000393 * returns the corresponding entity, otherwise a lookup is done
Owen Taylor3473f882001-02-23 17:55:21 +0000394 * in the predefined entities too.
395 *
396 * Returns A pointer to the entity structure or NULL if not found.
397 */
398xmlEntityPtr
399xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
400 xmlEntityPtr cur;
401 xmlEntitiesTablePtr table;
402
403 if (doc != NULL) {
404 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
405 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
406 cur = xmlGetEntityFromTable(table, name);
407 if (cur != NULL)
408 return(cur);
409 }
Daniel Veillard28757702002-02-18 11:19:30 +0000410 if (doc->standalone != 1) {
411 if ((doc->extSubset != NULL) &&
412 (doc->extSubset->entities != NULL)) {
413 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
414 cur = xmlGetEntityFromTable(table, name);
415 if (cur != NULL)
416 return(cur);
417 }
Owen Taylor3473f882001-02-23 17:55:21 +0000418 }
419 }
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000420 return(xmlGetPredefinedEntity(name));
Owen Taylor3473f882001-02-23 17:55:21 +0000421}
422
423/*
Owen Taylor3473f882001-02-23 17:55:21 +0000424 * Macro used to grow the current buffer.
425 */
426#define growBufferReentrant() { \
427 buffer_size *= 2; \
428 buffer = (xmlChar *) \
429 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
430 if (buffer == NULL) { \
Daniel Veillardce244ad2004-11-05 10:03:46 +0000431 xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\
Owen Taylor3473f882001-02-23 17:55:21 +0000432 return(NULL); \
433 } \
434}
435
436
437/**
438 * xmlEncodeEntitiesReentrant:
439 * @doc: the document containing the string
440 * @input: A string to convert to XML.
441 *
442 * Do a global encoding of a string, replacing the predefined entities
443 * and non ASCII values with their entities and CharRef counterparts.
444 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
445 * must be deallocated.
446 *
447 * Returns A newly allocated string with the substitution done.
448 */
449xmlChar *
450xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
451 const xmlChar *cur = input;
452 xmlChar *buffer = NULL;
453 xmlChar *out = NULL;
454 int buffer_size = 0;
455 int html = 0;
456
457 if (input == NULL) return(NULL);
458 if (doc != NULL)
459 html = (doc->type == XML_HTML_DOCUMENT_NODE);
460
461 /*
462 * allocate an translation buffer.
463 */
464 buffer_size = 1000;
465 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
466 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000467 xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000468 return(NULL);
469 }
470 out = buffer;
471
472 while (*cur != '\0') {
473 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000474 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000475
476 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000477 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000478 }
479
480 /*
481 * By default one have to encode at least '<', '>', '"' and '&' !
482 */
483 if (*cur == '<') {
484 *out++ = '&';
485 *out++ = 'l';
486 *out++ = 't';
487 *out++ = ';';
488 } else if (*cur == '>') {
489 *out++ = '&';
490 *out++ = 'g';
491 *out++ = 't';
492 *out++ = ';';
493 } else if (*cur == '&') {
494 *out++ = '&';
495 *out++ = 'a';
496 *out++ = 'm';
497 *out++ = 'p';
498 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000499 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Daniel Veillard0046c0f2003-02-23 13:52:30 +0000500 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000501 /*
502 * default case, just copy !
503 */
504 *out++ = *cur;
505 } else if (*cur >= 0x80) {
Daniel Veillard122376b2001-04-24 12:12:30 +0000506 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000507 /*
508 * Bjørn Reese <br@sseusa.com> provided the patch
509 xmlChar xc;
510 xc = (*cur & 0x3F) << 6;
511 if (cur[1] != 0) {
512 xc += *(++cur) & 0x3F;
513 *out++ = xc;
514 } else
515 */
516 *out++ = *cur;
517 } else {
518 /*
519 * We assume we have UTF-8 input.
520 */
Daniel Veillardb2517d82003-10-01 19:13:56 +0000521 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000522 int val = 0, l = 1;
523
524 if (*cur < 0xC0) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000525 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
526 "xmlEncodeEntitiesReentrant : input not UTF-8");
Daniel Veillard122376b2001-04-24 12:12:30 +0000527 if (doc != NULL)
528 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000529 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000530 buf[sizeof(buf) - 1] = 0;
531 ptr = buf;
532 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard05c13a22001-09-09 08:38:09 +0000533 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000534 continue;
535 } else if (*cur < 0xE0) {
536 val = (cur[0]) & 0x1F;
537 val <<= 6;
538 val |= (cur[1]) & 0x3F;
539 l = 2;
540 } else if (*cur < 0xF0) {
541 val = (cur[0]) & 0x0F;
542 val <<= 6;
543 val |= (cur[1]) & 0x3F;
544 val <<= 6;
545 val |= (cur[2]) & 0x3F;
546 l = 3;
547 } else if (*cur < 0xF8) {
548 val = (cur[0]) & 0x07;
549 val <<= 6;
550 val |= (cur[1]) & 0x3F;
551 val <<= 6;
552 val |= (cur[2]) & 0x3F;
553 val <<= 6;
554 val |= (cur[3]) & 0x3F;
555 l = 4;
556 }
557 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000558 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
Owen Taylor3473f882001-02-23 17:55:21 +0000559 "xmlEncodeEntitiesReentrant : char out of range\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000560 if (doc != NULL)
561 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000562 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000563 buf[sizeof(buf) - 1] = 0;
564 ptr = buf;
565 while (*ptr != 0) *out++ = *ptr++;
566 cur++;
567 continue;
568 }
569 /*
570 * We could do multiple things here. Just save as a char ref
571 */
Daniel Veillard16698282001-09-14 10:29:27 +0000572 if (html)
573 snprintf(buf, sizeof(buf), "&#%d;", val);
574 else
575 snprintf(buf, sizeof(buf), "&#x%X;", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000576 buf[sizeof(buf) - 1] = 0;
577 ptr = buf;
578 while (*ptr != 0) *out++ = *ptr++;
579 cur += l;
580 continue;
581 }
William M. Brack76e95df2003-10-18 16:20:14 +0000582 } else if (IS_BYTE_CHAR(*cur)) {
Daniel Veillardb2517d82003-10-01 19:13:56 +0000583 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000584
Owen Taylor3473f882001-02-23 17:55:21 +0000585 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000586 buf[sizeof(buf) - 1] = 0;
587 ptr = buf;
588 while (*ptr != 0) *out++ = *ptr++;
589 }
Owen Taylor3473f882001-02-23 17:55:21 +0000590 cur++;
591 }
592 *out++ = 0;
593 return(buffer);
594}
595
596/**
597 * xmlEncodeSpecialChars:
598 * @doc: the document containing the string
599 * @input: A string to convert to XML.
600 *
601 * Do a global encoding of a string, replacing the predefined entities
602 * this routine is reentrant, and result must be deallocated.
603 *
604 * Returns A newly allocated string with the substitution done.
605 */
606xmlChar *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000607xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Owen Taylor3473f882001-02-23 17:55:21 +0000608 const xmlChar *cur = input;
609 xmlChar *buffer = NULL;
610 xmlChar *out = NULL;
611 int buffer_size = 0;
William M. Brack899e64a2003-09-26 18:03:42 +0000612 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000613
614 /*
615 * allocate an translation buffer.
616 */
617 buffer_size = 1000;
618 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
619 if (buffer == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000620 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000621 return(NULL);
622 }
623 out = buffer;
624
625 while (*cur != '\0') {
626 if (out - buffer > buffer_size - 10) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000627 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000628
629 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000630 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000631 }
632
633 /*
634 * By default one have to encode at least '<', '>', '"' and '&' !
635 */
636 if (*cur == '<') {
637 *out++ = '&';
638 *out++ = 'l';
639 *out++ = 't';
640 *out++ = ';';
641 } else if (*cur == '>') {
642 *out++ = '&';
643 *out++ = 'g';
644 *out++ = 't';
645 *out++ = ';';
646 } else if (*cur == '&') {
647 *out++ = '&';
648 *out++ = 'a';
649 *out++ = 'm';
650 *out++ = 'p';
651 *out++ = ';';
652 } else if (*cur == '"') {
653 *out++ = '&';
654 *out++ = 'q';
655 *out++ = 'u';
656 *out++ = 'o';
657 *out++ = 't';
658 *out++ = ';';
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000659 } else if (*cur == '\r') {
660 *out++ = '&';
661 *out++ = '#';
662 *out++ = '1';
663 *out++ = '3';
664 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000665 } else {
666 /*
667 * Works because on UTF-8, all extended sequences cannot
668 * result in bytes in the ASCII range.
669 */
670 *out++ = *cur;
671 }
672 cur++;
673 }
674 *out++ = 0;
675 return(buffer);
676}
677
678/**
679 * xmlCreateEntitiesTable:
680 *
681 * create and initialize an empty entities hash table.
682 *
683 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
684 */
685xmlEntitiesTablePtr
686xmlCreateEntitiesTable(void) {
687 return((xmlEntitiesTablePtr) xmlHashCreate(0));
688}
689
690/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000691 * xmlFreeEntityWrapper:
692 * @entity: An entity
693 * @name: its name
694 *
695 * Deallocate the memory used by an entities in the hash table.
696 */
697static void
698xmlFreeEntityWrapper(xmlEntityPtr entity,
699 const xmlChar *name ATTRIBUTE_UNUSED) {
700 if (entity != NULL)
701 xmlFreeEntity(entity);
702}
703
704/**
Owen Taylor3473f882001-02-23 17:55:21 +0000705 * xmlFreeEntitiesTable:
706 * @table: An entity table
707 *
708 * Deallocate the memory used by an entities hash table.
709 */
710void
711xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000712 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000713}
714
Daniel Veillard652327a2003-09-29 18:02:38 +0000715#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000716/**
717 * xmlCopyEntity:
718 * @ent: An entity
719 *
720 * Build a copy of an entity
721 *
722 * Returns the new xmlEntitiesPtr or NULL in case of error.
723 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000724static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000725xmlCopyEntity(xmlEntityPtr ent) {
726 xmlEntityPtr cur;
727
728 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
729 if (cur == NULL) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000730 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000731 return(NULL);
732 }
733 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000734 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000735
736 cur->etype = ent->etype;
737 if (ent->name != NULL)
738 cur->name = xmlStrdup(ent->name);
739 if (ent->ExternalID != NULL)
740 cur->ExternalID = xmlStrdup(ent->ExternalID);
741 if (ent->SystemID != NULL)
742 cur->SystemID = xmlStrdup(ent->SystemID);
743 if (ent->content != NULL)
744 cur->content = xmlStrdup(ent->content);
745 if (ent->orig != NULL)
746 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000747 if (ent->URI != NULL)
748 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000749 return(cur);
750}
751
752/**
753 * xmlCopyEntitiesTable:
754 * @table: An entity table
755 *
756 * Build a copy of an entity table.
757 *
758 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
759 */
760xmlEntitiesTablePtr
761xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
762 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
763}
Daniel Veillard652327a2003-09-29 18:02:38 +0000764#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000765
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000766#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard18ab8722003-12-09 22:51:37 +0000767
768/**
769 * xmlDumpEntityContent:
770 * @buf: An XML buffer.
771 * @content: The entity content.
772 *
773 * This will dump the quoted string value, taking care of the special
774 * treatment required by %
775 */
776static void
777xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
778 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
779 if (xmlStrchr(content, '%')) {
780 const xmlChar * base, *cur;
781
782 xmlBufferCCat(buf, "\"");
783 base = cur = content;
784 while (*cur != 0) {
785 if (*cur == '"') {
786 if (base != cur)
787 xmlBufferAdd(buf, base, cur - base);
788 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
789 cur++;
790 base = cur;
791 } else if (*cur == '%') {
792 if (base != cur)
793 xmlBufferAdd(buf, base, cur - base);
794 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
795 cur++;
796 base = cur;
797 } else {
798 cur++;
799 }
800 }
801 if (base != cur)
802 xmlBufferAdd(buf, base, cur - base);
803 xmlBufferCCat(buf, "\"");
804 } else {
805 xmlBufferWriteQuotedString(buf, content);
806 }
807}
808
Owen Taylor3473f882001-02-23 17:55:21 +0000809/**
810 * xmlDumpEntityDecl:
811 * @buf: An XML buffer.
812 * @ent: An entity table
813 *
814 * This will dump the content of the entity table as an XML DTD definition
815 */
816void
817xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
Daniel Veillardce682bc2004-11-05 17:22:25 +0000818 if ((buf == NULL) || (ent == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000819 switch (ent->etype) {
820 case XML_INTERNAL_GENERAL_ENTITY:
821 xmlBufferWriteChar(buf, "<!ENTITY ");
822 xmlBufferWriteCHAR(buf, ent->name);
823 xmlBufferWriteChar(buf, " ");
824 if (ent->orig != NULL)
825 xmlBufferWriteQuotedString(buf, ent->orig);
826 else
Daniel Veillard18ab8722003-12-09 22:51:37 +0000827 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000828 xmlBufferWriteChar(buf, ">\n");
829 break;
830 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
831 xmlBufferWriteChar(buf, "<!ENTITY ");
832 xmlBufferWriteCHAR(buf, ent->name);
833 if (ent->ExternalID != NULL) {
834 xmlBufferWriteChar(buf, " PUBLIC ");
835 xmlBufferWriteQuotedString(buf, ent->ExternalID);
836 xmlBufferWriteChar(buf, " ");
837 xmlBufferWriteQuotedString(buf, ent->SystemID);
838 } else {
839 xmlBufferWriteChar(buf, " SYSTEM ");
840 xmlBufferWriteQuotedString(buf, ent->SystemID);
841 }
842 xmlBufferWriteChar(buf, ">\n");
843 break;
844 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
845 xmlBufferWriteChar(buf, "<!ENTITY ");
846 xmlBufferWriteCHAR(buf, ent->name);
847 if (ent->ExternalID != NULL) {
848 xmlBufferWriteChar(buf, " PUBLIC ");
849 xmlBufferWriteQuotedString(buf, ent->ExternalID);
850 xmlBufferWriteChar(buf, " ");
851 xmlBufferWriteQuotedString(buf, ent->SystemID);
852 } else {
853 xmlBufferWriteChar(buf, " SYSTEM ");
854 xmlBufferWriteQuotedString(buf, ent->SystemID);
855 }
856 if (ent->content != NULL) { /* Should be true ! */
857 xmlBufferWriteChar(buf, " NDATA ");
858 if (ent->orig != NULL)
859 xmlBufferWriteCHAR(buf, ent->orig);
860 else
861 xmlBufferWriteCHAR(buf, ent->content);
862 }
863 xmlBufferWriteChar(buf, ">\n");
864 break;
865 case XML_INTERNAL_PARAMETER_ENTITY:
866 xmlBufferWriteChar(buf, "<!ENTITY % ");
867 xmlBufferWriteCHAR(buf, ent->name);
868 xmlBufferWriteChar(buf, " ");
869 if (ent->orig == NULL)
Daniel Veillard18ab8722003-12-09 22:51:37 +0000870 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000871 else
872 xmlBufferWriteQuotedString(buf, ent->orig);
873 xmlBufferWriteChar(buf, ">\n");
874 break;
875 case XML_EXTERNAL_PARAMETER_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 default:
Daniel Veillardce244ad2004-11-05 10:03:46 +0000890 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
891 "xmlDumpEntitiesDecl: internal: unknown type entity type");
Owen Taylor3473f882001-02-23 17:55:21 +0000892 }
893}
894
895/**
William M. Brack9e660592003-10-20 14:56:06 +0000896 * xmlDumpEntityDeclScan:
897 * @ent: An entity table
898 * @buf: An XML buffer.
899 *
900 * When using the hash table scan function, arguments need to be reversed
901 */
902static void
903xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
904 xmlDumpEntityDecl(buf, ent);
905}
906
907/**
Owen Taylor3473f882001-02-23 17:55:21 +0000908 * xmlDumpEntitiesTable:
909 * @buf: An XML buffer.
910 * @table: An entity table
911 *
912 * This will dump the content of the entity table as an XML DTD definition
913 */
914void
915xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +0000916 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000917}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000918#endif /* LIBXML_OUTPUT_ENABLED */