blob: 5b6f931e056ee645e510d056af42a1c7c5a2224b [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
64/*
65 * xmlFreeEntity : clean-up an entity record.
66 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000067static void xmlFreeEntity(xmlEntityPtr entity) {
Owen Taylor3473f882001-02-23 17:55:21 +000068 if (entity == NULL) return;
69
Daniel Veillard2d84a892002-12-30 00:01:08 +000070 if ((entity->children) && (entity->owner == 1) &&
Daniel Veillard22090732001-07-16 00:06:07 +000071 (entity == (xmlEntityPtr) entity->children->parent))
Owen Taylor3473f882001-02-23 17:55:21 +000072 xmlFreeNodeList(entity->children);
73 if (entity->name != NULL)
74 xmlFree((char *) entity->name);
75 if (entity->ExternalID != NULL)
76 xmlFree((char *) entity->ExternalID);
77 if (entity->SystemID != NULL)
78 xmlFree((char *) entity->SystemID);
79 if (entity->URI != NULL)
80 xmlFree((char *) entity->URI);
81 if (entity->content != NULL)
82 xmlFree((char *) entity->content);
83 if (entity->orig != NULL)
84 xmlFree((char *) entity->orig);
Owen Taylor3473f882001-02-23 17:55:21 +000085 xmlFree(entity);
86}
87
88/*
89 * xmlAddEntity : register a new entity for an entities table.
90 */
91static xmlEntityPtr
92xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
93 const xmlChar *ExternalID, const xmlChar *SystemID,
94 const xmlChar *content) {
95 xmlEntitiesTablePtr table = NULL;
96 xmlEntityPtr ret;
97
98 if (name == NULL)
99 return(NULL);
100 switch (type) {
101 case XML_INTERNAL_GENERAL_ENTITY:
102 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
103 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
104 if (dtd->entities == NULL)
105 dtd->entities = xmlHashCreate(0);
106 table = dtd->entities;
107 break;
108 case XML_INTERNAL_PARAMETER_ENTITY:
109 case XML_EXTERNAL_PARAMETER_ENTITY:
110 if (dtd->pentities == NULL)
111 dtd->pentities = xmlHashCreate(0);
112 table = dtd->pentities;
113 break;
114 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000115 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000116 }
117 if (table == NULL)
118 return(NULL);
119 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
120 if (ret == NULL) {
121 xmlGenericError(xmlGenericErrorContext,
122 "xmlAddEntity: out of memory\n");
123 return(NULL);
124 }
125 memset(ret, 0, sizeof(xmlEntity));
126 ret->type = XML_ENTITY_DECL;
127
128 /*
129 * fill the structure.
130 */
131 ret->name = xmlStrdup(name);
132 ret->etype = (xmlEntityType) type;
133 if (ExternalID != NULL)
134 ret->ExternalID = xmlStrdup(ExternalID);
135 if (SystemID != NULL)
136 ret->SystemID = xmlStrdup(SystemID);
137 if (content != NULL) {
138 ret->length = xmlStrlen(content);
139 ret->content = xmlStrndup(content, ret->length);
140 } else {
141 ret->length = 0;
142 ret->content = NULL;
143 }
144 ret->URI = NULL; /* to be computed by the layer knowing
145 the defining entity */
146 ret->orig = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +0000147 ret->owner = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000148
149 if (xmlHashAddEntry(table, name, ret)) {
150 /*
151 * entity was already defined at another level.
152 */
153 xmlFreeEntity(ret);
154 return(NULL);
155 }
156 return(ret);
157}
158
159/**
Owen Taylor3473f882001-02-23 17:55:21 +0000160 * xmlGetPredefinedEntity:
161 * @name: the entity name
162 *
163 * Check whether this name is an predefined entity.
164 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000165 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000166 */
167xmlEntityPtr
168xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000169 if (name == NULL) return(NULL);
170 switch (name[0]) {
171 case 'l':
172 if (xmlStrEqual(name, BAD_CAST "lt"))
173 return(&xmlEntityLt);
174 break;
175 case 'g':
176 if (xmlStrEqual(name, BAD_CAST "gt"))
177 return(&xmlEntityGt);
178 break;
179 case 'a':
180 if (xmlStrEqual(name, BAD_CAST "amp"))
181 return(&xmlEntityAmp);
182 if (xmlStrEqual(name, BAD_CAST "apos"))
183 return(&xmlEntityApos);
184 break;
185 case 'q':
186 if (xmlStrEqual(name, BAD_CAST "quot"))
187 return(&xmlEntityQuot);
188 break;
189 default:
190 break;
191 }
192 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000193}
194
195/**
196 * xmlAddDtdEntity:
197 * @doc: the document
198 * @name: the entity name
199 * @type: the entity type XML_xxx_yyy_ENTITY
200 * @ExternalID: the entity external ID if available
201 * @SystemID: the entity system ID if available
202 * @content: the entity content
203 *
204 * Register a new entity for this document DTD external subset.
205 *
206 * Returns a pointer to the entity or NULL in case of error
207 */
208xmlEntityPtr
209xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
210 const xmlChar *ExternalID, const xmlChar *SystemID,
211 const xmlChar *content) {
212 xmlEntityPtr ret;
213 xmlDtdPtr dtd;
214
215 if (doc == NULL) {
216 xmlGenericError(xmlGenericErrorContext,
217 "xmlAddDtdEntity: doc == NULL !\n");
218 return(NULL);
219 }
220 if (doc->extSubset == NULL) {
221 xmlGenericError(xmlGenericErrorContext,
222 "xmlAddDtdEntity: document without external subset !\n");
223 return(NULL);
224 }
225 dtd = doc->extSubset;
226 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
227 if (ret == NULL) return(NULL);
228
229 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000230 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000231 */
232 ret->parent = dtd;
233 ret->doc = dtd->doc;
234 if (dtd->last == NULL) {
235 dtd->children = dtd->last = (xmlNodePtr) ret;
236 } else {
237 dtd->last->next = (xmlNodePtr) ret;
238 ret->prev = dtd->last;
239 dtd->last = (xmlNodePtr) ret;
240 }
241 return(ret);
242}
243
244/**
245 * xmlAddDocEntity:
246 * @doc: the document
247 * @name: the entity name
248 * @type: the entity type XML_xxx_yyy_ENTITY
249 * @ExternalID: the entity external ID if available
250 * @SystemID: the entity system ID if available
251 * @content: the entity content
252 *
253 * Register a new entity for this document.
254 *
255 * Returns a pointer to the entity or NULL in case of error
256 */
257xmlEntityPtr
258xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
259 const xmlChar *ExternalID, const xmlChar *SystemID,
260 const xmlChar *content) {
261 xmlEntityPtr ret;
262 xmlDtdPtr dtd;
263
264 if (doc == NULL) {
265 xmlGenericError(xmlGenericErrorContext,
266 "xmlAddDocEntity: document is NULL !\n");
267 return(NULL);
268 }
269 if (doc->intSubset == NULL) {
270 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000271 "xmlAddDocEntity: document without internal subset !\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000272 return(NULL);
273 }
274 dtd = doc->intSubset;
275 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
276 if (ret == NULL) return(NULL);
277
278 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000279 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000280 */
281 ret->parent = dtd;
282 ret->doc = dtd->doc;
283 if (dtd->last == NULL) {
284 dtd->children = dtd->last = (xmlNodePtr) ret;
285 } else {
286 dtd->last->next = (xmlNodePtr) ret;
287 ret->prev = dtd->last;
288 dtd->last = (xmlNodePtr) ret;
289 }
290 return(ret);
291}
292
293/**
294 * xmlGetEntityFromTable:
295 * @table: an entity table
296 * @name: the entity name
297 * @parameter: look for parameter entities
298 *
299 * Do an entity lookup in the table.
300 * returns the corresponding parameter entity, if found.
301 *
302 * Returns A pointer to the entity structure or NULL if not found.
303 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000304static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000305xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
306 return((xmlEntityPtr) xmlHashLookup(table, name));
307}
308
309/**
310 * xmlGetParameterEntity:
311 * @doc: the document referencing the entity
312 * @name: the entity name
313 *
314 * Do an entity lookup in the internal and external subsets and
315 * returns the corresponding parameter entity, if found.
316 *
317 * Returns A pointer to the entity structure or NULL if not found.
318 */
319xmlEntityPtr
320xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
321 xmlEntitiesTablePtr table;
322 xmlEntityPtr ret;
323
Daniel Veillard36065812002-01-24 15:02:46 +0000324 if (doc == NULL)
325 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000326 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
327 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
328 ret = xmlGetEntityFromTable(table, name);
329 if (ret != NULL)
330 return(ret);
331 }
332 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
333 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
334 return(xmlGetEntityFromTable(table, name));
335 }
336 return(NULL);
337}
338
339/**
340 * xmlGetDtdEntity:
341 * @doc: the document referencing the entity
342 * @name: the entity name
343 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000344 * Do an entity lookup in the DTD entity hash table and
Owen Taylor3473f882001-02-23 17:55:21 +0000345 * returns the corresponding entity, if found.
Daniel Veillard36065812002-01-24 15:02:46 +0000346 * Note: the first argument is the document node, not the DTD node.
Owen Taylor3473f882001-02-23 17:55:21 +0000347 *
348 * Returns A pointer to the entity structure or NULL if not found.
349 */
350xmlEntityPtr
351xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
352 xmlEntitiesTablePtr table;
353
Daniel Veillard36065812002-01-24 15:02:46 +0000354 if (doc == NULL)
355 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000356 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
357 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
358 return(xmlGetEntityFromTable(table, name));
359 }
360 return(NULL);
361}
362
363/**
364 * xmlGetDocEntity:
365 * @doc: the document referencing the entity
366 * @name: the entity name
367 *
368 * Do an entity lookup in the document entity hash table and
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000369 * returns the corresponding entity, otherwise a lookup is done
Owen Taylor3473f882001-02-23 17:55:21 +0000370 * in the predefined entities too.
371 *
372 * Returns A pointer to the entity structure or NULL if not found.
373 */
374xmlEntityPtr
375xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
376 xmlEntityPtr cur;
377 xmlEntitiesTablePtr table;
378
379 if (doc != NULL) {
380 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
381 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
382 cur = xmlGetEntityFromTable(table, name);
383 if (cur != NULL)
384 return(cur);
385 }
Daniel Veillard28757702002-02-18 11:19:30 +0000386 if (doc->standalone != 1) {
387 if ((doc->extSubset != NULL) &&
388 (doc->extSubset->entities != NULL)) {
389 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
390 cur = xmlGetEntityFromTable(table, name);
391 if (cur != NULL)
392 return(cur);
393 }
Owen Taylor3473f882001-02-23 17:55:21 +0000394 }
395 }
Daniel Veillardd3a2e4c2003-09-30 13:38:04 +0000396 return(xmlGetPredefinedEntity(name));
Owen Taylor3473f882001-02-23 17:55:21 +0000397}
398
399/*
Owen Taylor3473f882001-02-23 17:55:21 +0000400 * Macro used to grow the current buffer.
401 */
402#define growBufferReentrant() { \
403 buffer_size *= 2; \
404 buffer = (xmlChar *) \
405 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
406 if (buffer == NULL) { \
Daniel Veillard3487c8d2002-09-05 11:33:25 +0000407 xmlGenericError(xmlGenericErrorContext, "realloc failed\n"); \
Owen Taylor3473f882001-02-23 17:55:21 +0000408 return(NULL); \
409 } \
410}
411
412
413/**
414 * xmlEncodeEntitiesReentrant:
415 * @doc: the document containing the string
416 * @input: A string to convert to XML.
417 *
418 * Do a global encoding of a string, replacing the predefined entities
419 * and non ASCII values with their entities and CharRef counterparts.
420 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
421 * must be deallocated.
422 *
423 * Returns A newly allocated string with the substitution done.
424 */
425xmlChar *
426xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
427 const xmlChar *cur = input;
428 xmlChar *buffer = NULL;
429 xmlChar *out = NULL;
430 int buffer_size = 0;
431 int html = 0;
432
433 if (input == NULL) return(NULL);
434 if (doc != NULL)
435 html = (doc->type == XML_HTML_DOCUMENT_NODE);
436
437 /*
438 * allocate an translation buffer.
439 */
440 buffer_size = 1000;
441 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
442 if (buffer == NULL) {
Daniel Veillard3487c8d2002-09-05 11:33:25 +0000443 xmlGenericError(xmlGenericErrorContext, "malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000444 return(NULL);
445 }
446 out = buffer;
447
448 while (*cur != '\0') {
449 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000450 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000451
452 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000453 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000454 }
455
456 /*
457 * By default one have to encode at least '<', '>', '"' and '&' !
458 */
459 if (*cur == '<') {
460 *out++ = '&';
461 *out++ = 'l';
462 *out++ = 't';
463 *out++ = ';';
464 } else if (*cur == '>') {
465 *out++ = '&';
466 *out++ = 'g';
467 *out++ = 't';
468 *out++ = ';';
469 } else if (*cur == '&') {
470 *out++ = '&';
471 *out++ = 'a';
472 *out++ = 'm';
473 *out++ = 'p';
474 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000475 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Daniel Veillard0046c0f2003-02-23 13:52:30 +0000476 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000477 /*
478 * default case, just copy !
479 */
480 *out++ = *cur;
481 } else if (*cur >= 0x80) {
Daniel Veillard122376b2001-04-24 12:12:30 +0000482 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000483 /*
484 * Bjørn Reese <br@sseusa.com> provided the patch
485 xmlChar xc;
486 xc = (*cur & 0x3F) << 6;
487 if (cur[1] != 0) {
488 xc += *(++cur) & 0x3F;
489 *out++ = xc;
490 } else
491 */
492 *out++ = *cur;
493 } else {
494 /*
495 * We assume we have UTF-8 input.
496 */
Daniel Veillardb2517d82003-10-01 19:13:56 +0000497 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000498 int val = 0, l = 1;
499
500 if (*cur < 0xC0) {
501 xmlGenericError(xmlGenericErrorContext,
502 "xmlEncodeEntitiesReentrant : input not UTF-8\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000503 if (doc != NULL)
504 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000505 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000506 buf[sizeof(buf) - 1] = 0;
507 ptr = buf;
508 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard05c13a22001-09-09 08:38:09 +0000509 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000510 continue;
511 } else if (*cur < 0xE0) {
512 val = (cur[0]) & 0x1F;
513 val <<= 6;
514 val |= (cur[1]) & 0x3F;
515 l = 2;
516 } else if (*cur < 0xF0) {
517 val = (cur[0]) & 0x0F;
518 val <<= 6;
519 val |= (cur[1]) & 0x3F;
520 val <<= 6;
521 val |= (cur[2]) & 0x3F;
522 l = 3;
523 } else if (*cur < 0xF8) {
524 val = (cur[0]) & 0x07;
525 val <<= 6;
526 val |= (cur[1]) & 0x3F;
527 val <<= 6;
528 val |= (cur[2]) & 0x3F;
529 val <<= 6;
530 val |= (cur[3]) & 0x3F;
531 l = 4;
532 }
533 if ((l == 1) || (!IS_CHAR(val))) {
534 xmlGenericError(xmlGenericErrorContext,
535 "xmlEncodeEntitiesReentrant : char out of range\n");
Daniel Veillard122376b2001-04-24 12:12:30 +0000536 if (doc != NULL)
537 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Owen Taylor3473f882001-02-23 17:55:21 +0000538 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000539 buf[sizeof(buf) - 1] = 0;
540 ptr = buf;
541 while (*ptr != 0) *out++ = *ptr++;
542 cur++;
543 continue;
544 }
545 /*
546 * We could do multiple things here. Just save as a char ref
547 */
Daniel Veillard16698282001-09-14 10:29:27 +0000548 if (html)
549 snprintf(buf, sizeof(buf), "&#%d;", val);
550 else
551 snprintf(buf, sizeof(buf), "&#x%X;", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000552 buf[sizeof(buf) - 1] = 0;
553 ptr = buf;
554 while (*ptr != 0) *out++ = *ptr++;
555 cur += l;
556 continue;
557 }
William M. Brack76e95df2003-10-18 16:20:14 +0000558 } else if (IS_BYTE_CHAR(*cur)) {
Daniel Veillardb2517d82003-10-01 19:13:56 +0000559 char buf[11], *ptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000560
Owen Taylor3473f882001-02-23 17:55:21 +0000561 snprintf(buf, sizeof(buf), "&#%d;", *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 buf[sizeof(buf) - 1] = 0;
563 ptr = buf;
564 while (*ptr != 0) *out++ = *ptr++;
565 }
Owen Taylor3473f882001-02-23 17:55:21 +0000566 cur++;
567 }
568 *out++ = 0;
569 return(buffer);
570}
571
572/**
573 * xmlEncodeSpecialChars:
574 * @doc: the document containing the string
575 * @input: A string to convert to XML.
576 *
577 * Do a global encoding of a string, replacing the predefined entities
578 * this routine is reentrant, and result must be deallocated.
579 *
580 * Returns A newly allocated string with the substitution done.
581 */
582xmlChar *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000583xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Owen Taylor3473f882001-02-23 17:55:21 +0000584 const xmlChar *cur = input;
585 xmlChar *buffer = NULL;
586 xmlChar *out = NULL;
587 int buffer_size = 0;
William M. Brack899e64a2003-09-26 18:03:42 +0000588 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000589
590 /*
591 * allocate an translation buffer.
592 */
593 buffer_size = 1000;
594 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
595 if (buffer == NULL) {
Daniel Veillard3487c8d2002-09-05 11:33:25 +0000596 xmlGenericError(xmlGenericErrorContext, "malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000597 return(NULL);
598 }
599 out = buffer;
600
601 while (*cur != '\0') {
602 if (out - buffer > buffer_size - 10) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000603 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000604
605 growBufferReentrant();
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000606 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000607 }
608
609 /*
610 * By default one have to encode at least '<', '>', '"' and '&' !
611 */
612 if (*cur == '<') {
613 *out++ = '&';
614 *out++ = 'l';
615 *out++ = 't';
616 *out++ = ';';
617 } else if (*cur == '>') {
618 *out++ = '&';
619 *out++ = 'g';
620 *out++ = 't';
621 *out++ = ';';
622 } else if (*cur == '&') {
623 *out++ = '&';
624 *out++ = 'a';
625 *out++ = 'm';
626 *out++ = 'p';
627 *out++ = ';';
628 } else if (*cur == '"') {
629 *out++ = '&';
630 *out++ = 'q';
631 *out++ = 'u';
632 *out++ = 'o';
633 *out++ = 't';
634 *out++ = ';';
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000635 } else if (*cur == '\r') {
636 *out++ = '&';
637 *out++ = '#';
638 *out++ = '1';
639 *out++ = '3';
640 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000641 } else {
642 /*
643 * Works because on UTF-8, all extended sequences cannot
644 * result in bytes in the ASCII range.
645 */
646 *out++ = *cur;
647 }
648 cur++;
649 }
650 *out++ = 0;
651 return(buffer);
652}
653
654/**
655 * xmlCreateEntitiesTable:
656 *
657 * create and initialize an empty entities hash table.
658 *
659 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
660 */
661xmlEntitiesTablePtr
662xmlCreateEntitiesTable(void) {
663 return((xmlEntitiesTablePtr) xmlHashCreate(0));
664}
665
666/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000667 * xmlFreeEntityWrapper:
668 * @entity: An entity
669 * @name: its name
670 *
671 * Deallocate the memory used by an entities in the hash table.
672 */
673static void
674xmlFreeEntityWrapper(xmlEntityPtr entity,
675 const xmlChar *name ATTRIBUTE_UNUSED) {
676 if (entity != NULL)
677 xmlFreeEntity(entity);
678}
679
680/**
Owen Taylor3473f882001-02-23 17:55:21 +0000681 * xmlFreeEntitiesTable:
682 * @table: An entity table
683 *
684 * Deallocate the memory used by an entities hash table.
685 */
686void
687xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000688 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000689}
690
Daniel Veillard652327a2003-09-29 18:02:38 +0000691#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000692/**
693 * xmlCopyEntity:
694 * @ent: An entity
695 *
696 * Build a copy of an entity
697 *
698 * Returns the new xmlEntitiesPtr or NULL in case of error.
699 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000700static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000701xmlCopyEntity(xmlEntityPtr ent) {
702 xmlEntityPtr cur;
703
704 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
705 if (cur == NULL) {
706 xmlGenericError(xmlGenericErrorContext,
707 "xmlCopyEntity: out of memory !\n");
708 return(NULL);
709 }
710 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000711 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000712
713 cur->etype = ent->etype;
714 if (ent->name != NULL)
715 cur->name = xmlStrdup(ent->name);
716 if (ent->ExternalID != NULL)
717 cur->ExternalID = xmlStrdup(ent->ExternalID);
718 if (ent->SystemID != NULL)
719 cur->SystemID = xmlStrdup(ent->SystemID);
720 if (ent->content != NULL)
721 cur->content = xmlStrdup(ent->content);
722 if (ent->orig != NULL)
723 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000724 if (ent->URI != NULL)
725 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000726 return(cur);
727}
728
729/**
730 * xmlCopyEntitiesTable:
731 * @table: An entity table
732 *
733 * Build a copy of an entity table.
734 *
735 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
736 */
737xmlEntitiesTablePtr
738xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
739 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
740}
Daniel Veillard652327a2003-09-29 18:02:38 +0000741#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000742
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000743#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard18ab8722003-12-09 22:51:37 +0000744
745/**
746 * xmlDumpEntityContent:
747 * @buf: An XML buffer.
748 * @content: The entity content.
749 *
750 * This will dump the quoted string value, taking care of the special
751 * treatment required by %
752 */
753static void
754xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
755 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
756 if (xmlStrchr(content, '%')) {
757 const xmlChar * base, *cur;
758
759 xmlBufferCCat(buf, "\"");
760 base = cur = content;
761 while (*cur != 0) {
762 if (*cur == '"') {
763 if (base != cur)
764 xmlBufferAdd(buf, base, cur - base);
765 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
766 cur++;
767 base = cur;
768 } else if (*cur == '%') {
769 if (base != cur)
770 xmlBufferAdd(buf, base, cur - base);
771 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
772 cur++;
773 base = cur;
774 } else {
775 cur++;
776 }
777 }
778 if (base != cur)
779 xmlBufferAdd(buf, base, cur - base);
780 xmlBufferCCat(buf, "\"");
781 } else {
782 xmlBufferWriteQuotedString(buf, content);
783 }
784}
785
Owen Taylor3473f882001-02-23 17:55:21 +0000786/**
787 * xmlDumpEntityDecl:
788 * @buf: An XML buffer.
789 * @ent: An entity table
790 *
791 * This will dump the content of the entity table as an XML DTD definition
792 */
793void
794xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
795 switch (ent->etype) {
796 case XML_INTERNAL_GENERAL_ENTITY:
797 xmlBufferWriteChar(buf, "<!ENTITY ");
798 xmlBufferWriteCHAR(buf, ent->name);
799 xmlBufferWriteChar(buf, " ");
800 if (ent->orig != NULL)
801 xmlBufferWriteQuotedString(buf, ent->orig);
802 else
Daniel Veillard18ab8722003-12-09 22:51:37 +0000803 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000804 xmlBufferWriteChar(buf, ">\n");
805 break;
806 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
807 xmlBufferWriteChar(buf, "<!ENTITY ");
808 xmlBufferWriteCHAR(buf, ent->name);
809 if (ent->ExternalID != NULL) {
810 xmlBufferWriteChar(buf, " PUBLIC ");
811 xmlBufferWriteQuotedString(buf, ent->ExternalID);
812 xmlBufferWriteChar(buf, " ");
813 xmlBufferWriteQuotedString(buf, ent->SystemID);
814 } else {
815 xmlBufferWriteChar(buf, " SYSTEM ");
816 xmlBufferWriteQuotedString(buf, ent->SystemID);
817 }
818 xmlBufferWriteChar(buf, ">\n");
819 break;
820 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
821 xmlBufferWriteChar(buf, "<!ENTITY ");
822 xmlBufferWriteCHAR(buf, ent->name);
823 if (ent->ExternalID != NULL) {
824 xmlBufferWriteChar(buf, " PUBLIC ");
825 xmlBufferWriteQuotedString(buf, ent->ExternalID);
826 xmlBufferWriteChar(buf, " ");
827 xmlBufferWriteQuotedString(buf, ent->SystemID);
828 } else {
829 xmlBufferWriteChar(buf, " SYSTEM ");
830 xmlBufferWriteQuotedString(buf, ent->SystemID);
831 }
832 if (ent->content != NULL) { /* Should be true ! */
833 xmlBufferWriteChar(buf, " NDATA ");
834 if (ent->orig != NULL)
835 xmlBufferWriteCHAR(buf, ent->orig);
836 else
837 xmlBufferWriteCHAR(buf, ent->content);
838 }
839 xmlBufferWriteChar(buf, ">\n");
840 break;
841 case XML_INTERNAL_PARAMETER_ENTITY:
842 xmlBufferWriteChar(buf, "<!ENTITY % ");
843 xmlBufferWriteCHAR(buf, ent->name);
844 xmlBufferWriteChar(buf, " ");
845 if (ent->orig == NULL)
Daniel Veillard18ab8722003-12-09 22:51:37 +0000846 xmlDumpEntityContent(buf, ent->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000847 else
848 xmlBufferWriteQuotedString(buf, ent->orig);
849 xmlBufferWriteChar(buf, ">\n");
850 break;
851 case XML_EXTERNAL_PARAMETER_ENTITY:
852 xmlBufferWriteChar(buf, "<!ENTITY % ");
853 xmlBufferWriteCHAR(buf, ent->name);
854 if (ent->ExternalID != NULL) {
855 xmlBufferWriteChar(buf, " PUBLIC ");
856 xmlBufferWriteQuotedString(buf, ent->ExternalID);
857 xmlBufferWriteChar(buf, " ");
858 xmlBufferWriteQuotedString(buf, ent->SystemID);
859 } else {
860 xmlBufferWriteChar(buf, " SYSTEM ");
861 xmlBufferWriteQuotedString(buf, ent->SystemID);
862 }
863 xmlBufferWriteChar(buf, ">\n");
864 break;
865 default:
866 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000867 "xmlDumpEntitiesDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +0000868 ent->etype);
869 }
870}
871
872/**
William M. Brack9e660592003-10-20 14:56:06 +0000873 * xmlDumpEntityDeclScan:
874 * @ent: An entity table
875 * @buf: An XML buffer.
876 *
877 * When using the hash table scan function, arguments need to be reversed
878 */
879static void
880xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
881 xmlDumpEntityDecl(buf, ent);
882}
883
884/**
Owen Taylor3473f882001-02-23 17:55:21 +0000885 * xmlDumpEntitiesTable:
886 * @buf: An XML buffer.
887 * @table: An entity table
888 *
889 * This will dump the content of the entity table as an XML DTD definition
890 */
891void
892xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +0000893 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000894}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000895#endif /* LIBXML_OUTPUT_ENABLED */