blob: 0b0df65c695b7257ac4cb28bcbbda056698e87ee [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++ = ';';
Daniel Veillardd4532552003-11-25 18:29:55 +0000628#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000629 } else if (*cur == '"') {
630 *out++ = '&';
631 *out++ = 'q';
632 *out++ = 'u';
633 *out++ = 'o';
634 *out++ = 't';
635 *out++ = ';';
Daniel Veillardd4532552003-11-25 18:29:55 +0000636#endif
Daniel Veillard19ab45b2003-02-26 15:49:03 +0000637 } else if (*cur == '\r') {
638 *out++ = '&';
639 *out++ = '#';
640 *out++ = '1';
641 *out++ = '3';
642 *out++ = ';';
Owen Taylor3473f882001-02-23 17:55:21 +0000643 } else {
644 /*
645 * Works because on UTF-8, all extended sequences cannot
646 * result in bytes in the ASCII range.
647 */
648 *out++ = *cur;
649 }
650 cur++;
651 }
652 *out++ = 0;
653 return(buffer);
654}
655
656/**
657 * xmlCreateEntitiesTable:
658 *
659 * create and initialize an empty entities hash table.
660 *
661 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
662 */
663xmlEntitiesTablePtr
664xmlCreateEntitiesTable(void) {
665 return((xmlEntitiesTablePtr) xmlHashCreate(0));
666}
667
668/**
Daniel Veillard2d84a892002-12-30 00:01:08 +0000669 * xmlFreeEntityWrapper:
670 * @entity: An entity
671 * @name: its name
672 *
673 * Deallocate the memory used by an entities in the hash table.
674 */
675static void
676xmlFreeEntityWrapper(xmlEntityPtr entity,
677 const xmlChar *name ATTRIBUTE_UNUSED) {
678 if (entity != NULL)
679 xmlFreeEntity(entity);
680}
681
682/**
Owen Taylor3473f882001-02-23 17:55:21 +0000683 * xmlFreeEntitiesTable:
684 * @table: An entity table
685 *
686 * Deallocate the memory used by an entities hash table.
687 */
688void
689xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard2d84a892002-12-30 00:01:08 +0000690 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Owen Taylor3473f882001-02-23 17:55:21 +0000691}
692
Daniel Veillard652327a2003-09-29 18:02:38 +0000693#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000694/**
695 * xmlCopyEntity:
696 * @ent: An entity
697 *
698 * Build a copy of an entity
699 *
700 * Returns the new xmlEntitiesPtr or NULL in case of error.
701 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000702static xmlEntityPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000703xmlCopyEntity(xmlEntityPtr ent) {
704 xmlEntityPtr cur;
705
706 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
707 if (cur == NULL) {
708 xmlGenericError(xmlGenericErrorContext,
709 "xmlCopyEntity: out of memory !\n");
710 return(NULL);
711 }
712 memset(cur, 0, sizeof(xmlEntity));
Daniel Veillard845cce42002-01-09 11:51:37 +0000713 cur->type = XML_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +0000714
715 cur->etype = ent->etype;
716 if (ent->name != NULL)
717 cur->name = xmlStrdup(ent->name);
718 if (ent->ExternalID != NULL)
719 cur->ExternalID = xmlStrdup(ent->ExternalID);
720 if (ent->SystemID != NULL)
721 cur->SystemID = xmlStrdup(ent->SystemID);
722 if (ent->content != NULL)
723 cur->content = xmlStrdup(ent->content);
724 if (ent->orig != NULL)
725 cur->orig = xmlStrdup(ent->orig);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000726 if (ent->URI != NULL)
727 cur->URI = xmlStrdup(ent->URI);
Owen Taylor3473f882001-02-23 17:55:21 +0000728 return(cur);
729}
730
731/**
732 * xmlCopyEntitiesTable:
733 * @table: An entity table
734 *
735 * Build a copy of an entity table.
736 *
737 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
738 */
739xmlEntitiesTablePtr
740xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
741 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
742}
Daniel Veillard652327a2003-09-29 18:02:38 +0000743#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000744
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000745#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000746/**
747 * xmlDumpEntityDecl:
748 * @buf: An XML buffer.
749 * @ent: An entity table
750 *
751 * This will dump the content of the entity table as an XML DTD definition
752 */
753void
754xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
755 switch (ent->etype) {
756 case XML_INTERNAL_GENERAL_ENTITY:
757 xmlBufferWriteChar(buf, "<!ENTITY ");
758 xmlBufferWriteCHAR(buf, ent->name);
759 xmlBufferWriteChar(buf, " ");
760 if (ent->orig != NULL)
761 xmlBufferWriteQuotedString(buf, ent->orig);
762 else
763 xmlBufferWriteQuotedString(buf, ent->content);
764 xmlBufferWriteChar(buf, ">\n");
765 break;
766 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
767 xmlBufferWriteChar(buf, "<!ENTITY ");
768 xmlBufferWriteCHAR(buf, ent->name);
769 if (ent->ExternalID != NULL) {
770 xmlBufferWriteChar(buf, " PUBLIC ");
771 xmlBufferWriteQuotedString(buf, ent->ExternalID);
772 xmlBufferWriteChar(buf, " ");
773 xmlBufferWriteQuotedString(buf, ent->SystemID);
774 } else {
775 xmlBufferWriteChar(buf, " SYSTEM ");
776 xmlBufferWriteQuotedString(buf, ent->SystemID);
777 }
778 xmlBufferWriteChar(buf, ">\n");
779 break;
780 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
781 xmlBufferWriteChar(buf, "<!ENTITY ");
782 xmlBufferWriteCHAR(buf, ent->name);
783 if (ent->ExternalID != NULL) {
784 xmlBufferWriteChar(buf, " PUBLIC ");
785 xmlBufferWriteQuotedString(buf, ent->ExternalID);
786 xmlBufferWriteChar(buf, " ");
787 xmlBufferWriteQuotedString(buf, ent->SystemID);
788 } else {
789 xmlBufferWriteChar(buf, " SYSTEM ");
790 xmlBufferWriteQuotedString(buf, ent->SystemID);
791 }
792 if (ent->content != NULL) { /* Should be true ! */
793 xmlBufferWriteChar(buf, " NDATA ");
794 if (ent->orig != NULL)
795 xmlBufferWriteCHAR(buf, ent->orig);
796 else
797 xmlBufferWriteCHAR(buf, ent->content);
798 }
799 xmlBufferWriteChar(buf, ">\n");
800 break;
801 case XML_INTERNAL_PARAMETER_ENTITY:
802 xmlBufferWriteChar(buf, "<!ENTITY % ");
803 xmlBufferWriteCHAR(buf, ent->name);
804 xmlBufferWriteChar(buf, " ");
805 if (ent->orig == NULL)
806 xmlBufferWriteQuotedString(buf, ent->content);
807 else
808 xmlBufferWriteQuotedString(buf, ent->orig);
809 xmlBufferWriteChar(buf, ">\n");
810 break;
811 case XML_EXTERNAL_PARAMETER_ENTITY:
812 xmlBufferWriteChar(buf, "<!ENTITY % ");
813 xmlBufferWriteCHAR(buf, ent->name);
814 if (ent->ExternalID != NULL) {
815 xmlBufferWriteChar(buf, " PUBLIC ");
816 xmlBufferWriteQuotedString(buf, ent->ExternalID);
817 xmlBufferWriteChar(buf, " ");
818 xmlBufferWriteQuotedString(buf, ent->SystemID);
819 } else {
820 xmlBufferWriteChar(buf, " SYSTEM ");
821 xmlBufferWriteQuotedString(buf, ent->SystemID);
822 }
823 xmlBufferWriteChar(buf, ">\n");
824 break;
825 default:
826 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000827 "xmlDumpEntitiesDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +0000828 ent->etype);
829 }
830}
831
832/**
William M. Brack9e660592003-10-20 14:56:06 +0000833 * xmlDumpEntityDeclScan:
834 * @ent: An entity table
835 * @buf: An XML buffer.
836 *
837 * When using the hash table scan function, arguments need to be reversed
838 */
839static void
840xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
841 xmlDumpEntityDecl(buf, ent);
842}
843
844/**
Owen Taylor3473f882001-02-23 17:55:21 +0000845 * xmlDumpEntitiesTable:
846 * @buf: An XML buffer.
847 * @table: An entity table
848 *
849 * This will dump the content of the entity table as an XML DTD definition
850 */
851void
852xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +0000853 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000854}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000855#endif /* LIBXML_OUTPUT_ENABLED */