blob: c7e96ccb8820537c387e21c443ba16a60d9fd49c [file] [log] [blame]
Daniel Veillard260a68f1998-08-13 03:39:55 +00001/*
2 * entities.c : implementation for the XML entities handking
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00006 * Daniel.Veillard@w3.org
Daniel Veillard260a68f1998-08-13 03:39:55 +00007 */
8
Daniel Veillard7f7d1111999-09-22 09:46:25 +00009#ifndef WIN32
10#include "config.h"
11#endif
12
Daniel Veillard260a68f1998-08-13 03:39:55 +000013#include <stdio.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000014#include <string.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000015#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +000018#include "xmlmemory.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000019#include "entities.h"
Daniel Veillarda0555cc1999-12-01 09:51:45 +000020#include "parser.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000021
22/*
23 * The XML predefined entities.
24 */
25
26struct xmlPredefinedEntityValue {
27 const char *name;
28 const char *value;
29};
30struct xmlPredefinedEntityValue xmlPredefinedEntityValues[] = {
31 { "lt", "<" },
32 { "gt", ">" },
33 { "apos", "'" },
34 { "quot", "\"" },
35 { "amp", "&" }
36};
37
38xmlEntitiesTablePtr xmlPredefinedEntities = NULL;
39
40/*
Daniel Veillard260a68f1998-08-13 03:39:55 +000041 * xmlFreeEntity : clean-up an entity record.
42 */
Daniel Veillard260a68f1998-08-13 03:39:55 +000043void xmlFreeEntity(xmlEntityPtr entity) {
44 if (entity == NULL) return;
45
46 if (entity->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +000047 xmlFree((char *) entity->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +000048 if (entity->ExternalID != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +000049 xmlFree((char *) entity->ExternalID);
Daniel Veillard260a68f1998-08-13 03:39:55 +000050 if (entity->SystemID != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +000051 xmlFree((char *) entity->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +000052 if (entity->content != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +000053 xmlFree((char *) entity->content);
Daniel Veillard011b63c1999-06-02 17:44:04 +000054 if (entity->orig != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +000055 xmlFree((char *) entity->orig);
Daniel Veillard260a68f1998-08-13 03:39:55 +000056 memset(entity, -1, sizeof(xmlEntity));
57}
58
59/*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000060 * xmlAddEntity : register a new entity for an entities table.
Daniel Veillard260a68f1998-08-13 03:39:55 +000061 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +000062static void
Daniel Veillarddd6b3671999-09-23 22:19:22 +000063xmlAddEntity(xmlEntitiesTablePtr table, const xmlChar *name, int type,
64 const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000065 int i;
66 xmlEntityPtr cur;
67 int len;
68
69 for (i = 0;i < table->nb_entities;i++) {
70 cur = &table->table[i];
71 if (!xmlStrcmp(cur->name, name)) {
72 /*
73 * The entity is already defined in this Dtd, the spec says to NOT
74 * override it ... Is it worth a Warning ??? !!!
Daniel Veillardb96e6431999-08-29 21:02:19 +000075 * Not having a cprinting context this seems hard ...
Daniel Veillard260a68f1998-08-13 03:39:55 +000076 */
Daniel Veillardb05deb71999-08-10 19:04:08 +000077 if (((type == XML_INTERNAL_PARAMETER_ENTITY) ||
78 (type == XML_EXTERNAL_PARAMETER_ENTITY)) &&
79 ((cur->type == XML_INTERNAL_PARAMETER_ENTITY) ||
80 (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)))
81 return;
82 else
83 if (((type != XML_INTERNAL_PARAMETER_ENTITY) &&
84 (type != XML_EXTERNAL_PARAMETER_ENTITY)) &&
85 ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) &&
86 (cur->type != XML_EXTERNAL_PARAMETER_ENTITY)))
87 return;
Daniel Veillard260a68f1998-08-13 03:39:55 +000088 }
89 }
90 if (table->nb_entities >= table->max_entities) {
91 /*
92 * need more elements.
93 */
94 table->max_entities *= 2;
95 table->table = (xmlEntityPtr)
Daniel Veillard6454aec1999-09-02 22:04:43 +000096 xmlRealloc(table->table, table->max_entities * sizeof(xmlEntity));
Daniel Veillardb05deb71999-08-10 19:04:08 +000097 if (table->table == NULL) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000098 perror("realloc failed");
Daniel Veillardb05deb71999-08-10 19:04:08 +000099 return;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000100 }
101 }
102 cur = &table->table[table->nb_entities];
103 cur->name = xmlStrdup(name);
104 for (len = 0;name[0] != 0;name++)len++;
105 cur->len = len;
106 cur->type = type;
107 if (ExternalID != NULL)
108 cur->ExternalID = xmlStrdup(ExternalID);
109 else
110 cur->ExternalID = NULL;
111 if (SystemID != NULL)
112 cur->SystemID = xmlStrdup(SystemID);
113 else
114 cur->SystemID = NULL;
115 if (content != NULL)
116 cur->content = xmlStrdup(content);
117 else
118 cur->content = NULL;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000119 cur->orig = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000120 table->nb_entities++;
121}
122
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000123/**
124 * xmlInitializePredefinedEntities:
125 *
126 * Set up the predefined entities.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000127 */
128void xmlInitializePredefinedEntities(void) {
129 int i;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000130 xmlChar name[50];
131 xmlChar value[50];
Daniel Veillard260a68f1998-08-13 03:39:55 +0000132 const char *in;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000133 xmlChar *out;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000134
135 if (xmlPredefinedEntities != NULL) return;
136
137 xmlPredefinedEntities = xmlCreateEntitiesTable();
138 for (i = 0;i < sizeof(xmlPredefinedEntityValues) /
139 sizeof(xmlPredefinedEntityValues[0]);i++) {
140 in = xmlPredefinedEntityValues[i].name;
141 out = &name[0];
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000142 for (;(*out++ = (xmlChar) *in);)in++;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000143 in = xmlPredefinedEntityValues[i].value;
144 out = &value[0];
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000145 for (;(*out++ = (xmlChar) *in);)in++;
146 xmlAddEntity(xmlPredefinedEntities, (const xmlChar *) &name[0],
Daniel Veillard25940b71998-10-29 05:51:30 +0000147 XML_INTERNAL_PREDEFINED_ENTITY, NULL, NULL,
Daniel Veillard260a68f1998-08-13 03:39:55 +0000148 &value[0]);
149 }
150}
151
Daniel Veillardccb09631998-10-27 06:21:04 +0000152/**
Daniel Veillarda594bf41999-12-01 09:51:45 +0000153 * xmlCleanupPredefinedEntities:
154 *
155 * Cleanup up the predefined entities table.
156 */
157void xmlCleanupPredefinedEntities(void) {
158 if (xmlPredefinedEntities == NULL) return;
159
160 xmlFreeEntitiesTable(xmlPredefinedEntities);
161 xmlPredefinedEntities = NULL;
162}
163
164/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000165 * xmlGetPredefinedEntity:
166 * @name: the entity name
167 *
168 * Check whether this name is an predefined entity.
169 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000170 * Returns NULL if not, othervise the entity
Daniel Veillardccb09631998-10-27 06:21:04 +0000171 */
172xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000173xmlGetPredefinedEntity(const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000174 int i;
175 xmlEntityPtr cur;
176
177 if (xmlPredefinedEntities == NULL)
178 xmlInitializePredefinedEntities();
179 for (i = 0;i < xmlPredefinedEntities->nb_entities;i++) {
180 cur = &xmlPredefinedEntities->table[i];
181 if (!xmlStrcmp(cur->name, name)) return(cur);
182 }
183 return(NULL);
184}
185
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000186/**
187 * xmlAddDtdEntity:
188 * @doc: the document
189 * @name: the entity name
190 * @type: the entity type XML_xxx_yyy_ENTITY
191 * @ExternalID: the entity external ID if available
192 * @SystemID: the entity system ID if available
193 * @content: the entity content
194 *
195 * Register a new entity for this document DTD.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000196 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000197void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000198xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
199 const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000200 xmlEntitiesTablePtr table;
201
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000202 if (doc->extSubset == NULL) {
203 fprintf(stderr,
204 "xmlAddDtdEntity: document without external subset !\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000205 return;
206 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000207 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000208 if (table == NULL) {
209 table = xmlCreateEntitiesTable();
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000210 doc->extSubset->entities = table;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000211 }
212 xmlAddEntity(table, name, type, ExternalID, SystemID, content);
213}
214
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000215/**
216 * xmlAddDocEntity:
217 * @doc: the document
218 * @name: the entity name
219 * @type: the entity type XML_xxx_yyy_ENTITY
220 * @ExternalID: the entity external ID if available
221 * @SystemID: the entity system ID if available
222 * @content: the entity content
223 *
224 * Register a new entity for this document.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000225 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000226void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000227xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
228 const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000229 xmlEntitiesTablePtr table;
230
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000231 if (doc == NULL) {
232 fprintf(stderr,
233 "xmlAddDocEntity: document is NULL !\n");
234 return;
235 }
236 if (doc->intSubset == NULL) {
237 fprintf(stderr,
238 "xmlAddDtdEntity: document without internal subset !\n");
239 return;
240 }
241 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000242 if (table == NULL) {
243 table = xmlCreateEntitiesTable();
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000244 doc->intSubset->entities = table;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000245 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000246 xmlAddEntity(table, name, type, ExternalID, SystemID, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000247}
248
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000249/**
Daniel Veillardb05deb71999-08-10 19:04:08 +0000250 * xmlGetParameterEntity:
251 * @doc: the document referencing the entity
252 * @name: the entity name
253 *
254 * Do an entity lookup in the internal and external subsets and
255 * returns the corresponding parameter entity, if found.
256 *
257 * Returns A pointer to the entity structure or NULL if not found.
258 */
259xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000260xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000261 int i;
262 xmlEntityPtr cur;
263 xmlEntitiesTablePtr table;
264
265 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
266 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
267 for (i = 0;i < table->nb_entities;i++) {
268 cur = &table->table[i];
269 if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) ||
270 (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) &&
271 (!xmlStrcmp(cur->name, name))) return(cur);
272 }
273 }
274 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
275 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
276 for (i = 0;i < table->nb_entities;i++) {
277 cur = &table->table[i];
278 if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) ||
279 (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) &&
280 (!xmlStrcmp(cur->name, name))) return(cur);
281 }
282 }
Daniel Veillard1ff7ae31999-09-01 12:19:13 +0000283 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
284 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
285 for (i = 0;i < table->nb_entities;i++) {
286 cur = &table->table[i];
287 if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) ||
288 (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) &&
289 (!xmlStrcmp(cur->name, name))) return(cur);
290 }
291 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000292 return(NULL);
293}
294
295/**
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000296 * xmlGetDtdEntity:
297 * @doc: the document referencing the entity
298 * @name: the entity name
299 *
300 * Do an entity lookup in the Dtd entity hash table and
301 * returns the corresponding entity, if found.
302 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000303 * Returns A pointer to the entity structure or NULL if not found.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000304 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000305xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000306xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000307 int i;
308 xmlEntityPtr cur;
309 xmlEntitiesTablePtr table;
310
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000311 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
312 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000313 for (i = 0;i < table->nb_entities;i++) {
314 cur = &table->table[i];
Daniel Veillardb05deb71999-08-10 19:04:08 +0000315 if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) &&
316 (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) &&
317 (!xmlStrcmp(cur->name, name))) return(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000318 }
319 }
320 return(NULL);
321}
322
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000323/**
324 * xmlGetDocEntity:
325 * @doc: the document referencing the entity
326 * @name: the entity name
327 *
328 * Do an entity lookup in the document entity hash table and
329 * returns the corrsponding entity, otherwise a lookup is done
330 * in the predefined entities too.
331 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000332 * Returns A pointer to the entity structure or NULL if not found.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000333 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000334xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000335xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000336 int i;
337 xmlEntityPtr cur;
338 xmlEntitiesTablePtr table;
339
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000340 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
341 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000342 for (i = 0;i < table->nb_entities;i++) {
343 cur = &table->table[i];
Daniel Veillardb05deb71999-08-10 19:04:08 +0000344 if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) &&
345 (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) &&
346 (!xmlStrcmp(cur->name, name))) return(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000347 }
348 }
Daniel Veillard1ff7ae31999-09-01 12:19:13 +0000349 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
350 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
351 for (i = 0;i < table->nb_entities;i++) {
352 cur = &table->table[i];
353 if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) &&
354 (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) &&
355 (!xmlStrcmp(cur->name, name))) return(cur);
356 }
357 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000358 if (xmlPredefinedEntities == NULL)
359 xmlInitializePredefinedEntities();
360 table = xmlPredefinedEntities;
361 for (i = 0;i < table->nb_entities;i++) {
362 cur = &table->table[i];
Daniel Veillardb05deb71999-08-10 19:04:08 +0000363 if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) &&
364 (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) &&
365 (!xmlStrcmp(cur->name, name))) return(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000366 }
367
368 return(NULL);
369}
370
371/*
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000372 * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
373 * | [#x10000-#x10FFFF]
374 * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
375 */
376#define IS_CHAR(c) \
377 (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) || \
378 (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF)))
379
Daniel Veillard14fff061999-06-22 21:49:07 +0000380/*
381 * A buffer used for converting entities to their equivalent and back.
Daniel Veillard14fff061999-06-22 21:49:07 +0000382 */
383static int buffer_size = 0;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000384static xmlChar *buffer = NULL;
Daniel Veillard14fff061999-06-22 21:49:07 +0000385
386void growBuffer(void) {
387 buffer_size *= 2;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000388 buffer = (xmlChar *) xmlRealloc(buffer, buffer_size * sizeof(xmlChar));
Daniel Veillard14fff061999-06-22 21:49:07 +0000389 if (buffer == NULL) {
390 perror("realloc failed");
391 exit(1);
392 }
393}
394
395
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000396/**
397 * xmlEncodeEntities:
398 * @doc: the document containing the string
399 * @input: A string to convert to XML.
400 *
401 * Do a global encoding of a string, replacing the predefined entities
402 * and non ASCII values with their entities and CharRef counterparts.
403 *
Daniel Veillardb96e6431999-08-29 21:02:19 +0000404 * TODO: remove xmlEncodeEntities, once we are not afraid of breaking binary
405 * compatibility
Daniel Veillard14fff061999-06-22 21:49:07 +0000406 *
407 * People must migrate their code to xmlEncodeEntitiesReentrant !
Daniel Veillardb05deb71999-08-10 19:04:08 +0000408 * This routine will issue a warning when encountered.
Daniel Veillard14fff061999-06-22 21:49:07 +0000409 *
410 * Returns A newly allocated string with the substitution done.
411 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000412const xmlChar *
413xmlEncodeEntities(xmlDocPtr doc, const xmlChar *input) {
414 const xmlChar *cur = input;
415 xmlChar *out = buffer;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000416 static int warning = 1;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000417 int html = 0;
418
Daniel Veillardb05deb71999-08-10 19:04:08 +0000419
420 if (warning) {
421 fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n");
422 fprintf(stderr, " change code to use xmlEncodeEntitiesReentrant()\n");
423 warning = 0;
424 }
Daniel Veillard14fff061999-06-22 21:49:07 +0000425
426 if (input == NULL) return(NULL);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000427 if (doc != NULL)
428 html = (doc->type == XML_HTML_DOCUMENT_NODE);
429
Daniel Veillard14fff061999-06-22 21:49:07 +0000430 if (buffer == NULL) {
431 buffer_size = 1000;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000432 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
Daniel Veillard14fff061999-06-22 21:49:07 +0000433 if (buffer == NULL) {
434 perror("malloc failed");
435 exit(1);
436 }
437 out = buffer;
438 }
439 while (*cur != '\0') {
440 if (out - buffer > buffer_size - 100) {
441 int index = out - buffer;
442
443 growBuffer();
444 out = &buffer[index];
445 }
446
447 /*
448 * By default one have to encode at least '<', '>', '"' and '&' !
449 */
450 if (*cur == '<') {
451 *out++ = '&';
452 *out++ = 'l';
453 *out++ = 't';
454 *out++ = ';';
455 } else if (*cur == '>') {
456 *out++ = '&';
457 *out++ = 'g';
458 *out++ = 't';
459 *out++ = ';';
460 } else if (*cur == '&') {
461 *out++ = '&';
462 *out++ = 'a';
463 *out++ = 'm';
464 *out++ = 'p';
465 *out++ = ';';
466 } else if (*cur == '"') {
467 *out++ = '&';
468 *out++ = 'q';
469 *out++ = 'u';
470 *out++ = 'o';
471 *out++ = 't';
472 *out++ = ';';
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000473 } else if ((*cur == '\'') && (!html)) {
Daniel Veillard14fff061999-06-22 21:49:07 +0000474 *out++ = '&';
475 *out++ = 'a';
476 *out++ = 'p';
477 *out++ = 'o';
478 *out++ = 's';
479 *out++ = ';';
480 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
481 (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) {
482 /*
483 * default case, just copy !
484 */
485 *out++ = *cur;
486#ifndef USE_UTF_8
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000487 } else if ((sizeof(xmlChar) == 1) && (*cur >= 0x80)) {
Daniel Veillard14fff061999-06-22 21:49:07 +0000488 char buf[10], *ptr;
489#ifdef HAVE_SNPRINTF
490 snprintf(buf, 9, "&#%d;", *cur);
491#else
492 sprintf(buf, "&#%d;", *cur);
493#endif
494 ptr = buf;
495 while (*ptr != 0) *out++ = *ptr++;
496#endif
497 } else if (IS_CHAR(*cur)) {
498 char buf[10], *ptr;
499
500#ifdef HAVE_SNPRINTF
501 snprintf(buf, 9, "&#%d;", *cur);
502#else
503 sprintf(buf, "&#%d;", *cur);
504#endif
505 ptr = buf;
506 while (*ptr != 0) *out++ = *ptr++;
507 }
508#if 0
509 else {
510 /*
511 * default case, this is not a valid char !
512 * Skip it...
513 */
514 fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);
515 }
516#endif
517 cur++;
518 }
519 *out++ = 0;
520 return(buffer);
521}
522
523/*
524 * Macro used to grow the current buffer.
525 */
526#define growBufferReentrant() { \
527 buffer_size *= 2; \
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000528 buffer = (xmlChar *) xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
Daniel Veillard14fff061999-06-22 21:49:07 +0000529 if (buffer == NULL) { \
530 perror("realloc failed"); \
531 exit(1); \
532 } \
533}
534
535
536/**
537 * xmlEncodeEntitiesReentrant:
538 * @doc: the document containing the string
539 * @input: A string to convert to XML.
540 *
541 * Do a global encoding of a string, replacing the predefined entities
542 * and non ASCII values with their entities and CharRef counterparts.
543 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
544 * must be deallocated.
545 *
546 * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii
547 * get erroneous.
548 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000549 * Returns A newly allocated string with the substitution done.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000550 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000551xmlChar *
552xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
553 const xmlChar *cur = input;
554 xmlChar *buffer = NULL;
555 xmlChar *out = NULL;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000556 int buffer_size = 0;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000557 int html = 0;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000558
Daniel Veillard242590e1998-11-13 18:04:35 +0000559 if (input == NULL) return(NULL);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000560 if (doc != NULL)
561 html = (doc->type == XML_HTML_DOCUMENT_NODE);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000562
563 /*
564 * allocate an translation buffer.
565 */
566 buffer_size = 1000;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000567 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000568 if (buffer == NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000569 perror("malloc failed");
570 exit(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000571 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000572 out = buffer;
573
Daniel Veillard260a68f1998-08-13 03:39:55 +0000574 while (*cur != '\0') {
575 if (out - buffer > buffer_size - 100) {
576 int index = out - buffer;
577
Daniel Veillard14fff061999-06-22 21:49:07 +0000578 growBufferReentrant();
Daniel Veillard260a68f1998-08-13 03:39:55 +0000579 out = &buffer[index];
580 }
581
582 /*
583 * By default one have to encode at least '<', '>', '"' and '&' !
Daniel Veillard260a68f1998-08-13 03:39:55 +0000584 */
585 if (*cur == '<') {
586 *out++ = '&';
587 *out++ = 'l';
588 *out++ = 't';
589 *out++ = ';';
590 } else if (*cur == '>') {
591 *out++ = '&';
592 *out++ = 'g';
593 *out++ = 't';
594 *out++ = ';';
595 } else if (*cur == '&') {
596 *out++ = '&';
597 *out++ = 'a';
598 *out++ = 'm';
599 *out++ = 'p';
600 *out++ = ';';
601 } else if (*cur == '"') {
602 *out++ = '&';
603 *out++ = 'q';
604 *out++ = 'u';
605 *out++ = 'o';
606 *out++ = 't';
607 *out++ = ';';
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000608 } else if ((*cur == '\'') && (!html)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000609 *out++ = '&';
610 *out++ = 'a';
611 *out++ = 'p';
612 *out++ = 'o';
613 *out++ = 's';
614 *out++ = ';';
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000615 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
616 (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) {
617 /*
618 * default case, just copy !
619 */
620 *out++ = *cur;
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000621#ifndef USE_UTF_8
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000622 } else if ((sizeof(xmlChar) == 1) && (*cur >= 0x80)) {
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000623 char buf[10], *ptr;
Daniel Veillardda4d3c41998-11-04 20:07:05 +0000624#ifdef HAVE_SNPRINTF
625 snprintf(buf, 9, "&#%d;", *cur);
626#else
627 sprintf(buf, "&#%d;", *cur);
628#endif
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000629 ptr = buf;
630 while (*ptr != 0) *out++ = *ptr++;
631#endif
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000632 } else if (IS_CHAR(*cur)) {
633 char buf[10], *ptr;
634
635#ifdef HAVE_SNPRINTF
636 snprintf(buf, 9, "&#%d;", *cur);
637#else
638 sprintf(buf, "&#%d;", *cur);
639#endif
640 ptr = buf;
641 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000642 }
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000643#if 0
644 else {
645 /*
646 * default case, this is not a valid char !
647 * Skip it...
648 */
649 fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);
650 }
651#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000652 cur++;
653 }
654 *out++ = 0;
655 return(buffer);
656}
657
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000658/**
659 * xmlCreateEntitiesTable:
660 *
661 * create and initialize an empty entities hash table.
662 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000663 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000664 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000665xmlEntitiesTablePtr
666xmlCreateEntitiesTable(void) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000667 xmlEntitiesTablePtr ret;
668
669 ret = (xmlEntitiesTablePtr)
Daniel Veillard6454aec1999-09-02 22:04:43 +0000670 xmlMalloc(sizeof(xmlEntitiesTable));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000671 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000672 fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n",
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000673 (long)sizeof(xmlEntitiesTable));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000674 return(NULL);
675 }
676 ret->max_entities = XML_MIN_ENTITIES_TABLE;
677 ret->nb_entities = 0;
678 ret->table = (xmlEntityPtr )
Daniel Veillard6454aec1999-09-02 22:04:43 +0000679 xmlMalloc(ret->max_entities * sizeof(xmlEntity));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000680 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000681 fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n",
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000682 ret->max_entities * (long)sizeof(xmlEntity));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000683 xmlFree(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000684 return(NULL);
685 }
686 return(ret);
687}
688
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000689/**
690 * xmlFreeEntitiesTable:
691 * @table: An entity table
692 *
693 * Deallocate the memory used by an entities hash table.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000694 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000695void
696xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000697 int i;
698
699 if (table == NULL) return;
700
701 for (i = 0;i < table->nb_entities;i++) {
702 xmlFreeEntity(&table->table[i]);
703 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000704 xmlFree(table->table);
705 xmlFree(table);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000706}
707
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000708/**
709 * xmlCopyEntitiesTable:
710 * @table: An entity table
711 *
712 * Build a copy of an entity table.
713 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000714 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000715 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000716xmlEntitiesTablePtr
717xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
718 xmlEntitiesTablePtr ret;
719 xmlEntityPtr cur, ent;
720 int i;
721
Daniel Veillard6454aec1999-09-02 22:04:43 +0000722 ret = (xmlEntitiesTablePtr) xmlMalloc(sizeof(xmlEntitiesTable));
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000723 if (ret == NULL) {
724 fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");
725 return(NULL);
726 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000727 ret->table = (xmlEntityPtr) xmlMalloc(table->max_entities *
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000728 sizeof(xmlEntity));
729 if (ret->table == NULL) {
730 fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +0000731 xmlFree(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000732 return(NULL);
733 }
734 ret->max_entities = table->max_entities;
735 ret->nb_entities = table->nb_entities;
736 for (i = 0;i < ret->nb_entities;i++) {
737 cur = &ret->table[i];
738 ent = &table->table[i];
739 cur->len = ent->len;
740 cur->type = ent->type;
741 if (ent->name != NULL)
742 cur->name = xmlStrdup(ent->name);
743 else
744 cur->name = NULL;
745 if (ent->ExternalID != NULL)
746 cur->ExternalID = xmlStrdup(ent->ExternalID);
747 else
748 cur->ExternalID = NULL;
749 if (ent->SystemID != NULL)
750 cur->SystemID = xmlStrdup(ent->SystemID);
751 else
752 cur->SystemID = NULL;
753 if (ent->content != NULL)
754 cur->content = xmlStrdup(ent->content);
755 else
756 cur->content = NULL;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000757 if (ent->orig != NULL)
758 cur->orig = xmlStrdup(ent->orig);
759 else
760 cur->orig = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000761 }
762 return(ret);
763}
764
765/**
766 * xmlDumpEntitiesTable:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000767 * @buf: An XML buffer.
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000768 * @table: An entity table
769 *
770 * This will dump the content of the entity table as an XML DTD definition
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000771 */
772void
Daniel Veillard5099ae81999-04-21 20:12:07 +0000773xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000774 int i;
775 xmlEntityPtr cur;
776
777 if (table == NULL) return;
778
779 for (i = 0;i < table->nb_entities;i++) {
780 cur = &table->table[i];
781 switch (cur->type) {
782 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000783 xmlBufferWriteChar(buf, "<!ENTITY ");
784 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000785 xmlBufferWriteChar(buf, " ");
786 if (cur->orig != NULL)
787 xmlBufferWriteQuotedString(buf, cur->orig);
788 else
789 xmlBufferWriteQuotedString(buf, cur->content);
790 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000791 break;
792 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000793 xmlBufferWriteChar(buf, "<!ENTITY ");
794 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000795 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000796 xmlBufferWriteChar(buf, " PUBLIC ");
797 xmlBufferWriteQuotedString(buf, cur->ExternalID);
798 xmlBufferWriteChar(buf, " ");
799 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000800 } else {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000801 xmlBufferWriteChar(buf, " SYSTEM ");
802 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000803 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000804 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000805 break;
806 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000807 xmlBufferWriteChar(buf, "<!ENTITY ");
808 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000809 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000810 xmlBufferWriteChar(buf, " PUBLIC ");
811 xmlBufferWriteQuotedString(buf, cur->ExternalID);
812 xmlBufferWriteChar(buf, " ");
813 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000814 } else {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000815 xmlBufferWriteChar(buf, " SYSTEM ");
816 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000817 }
818 if (cur->content != NULL) { /* Should be true ! */
Daniel Veillard5099ae81999-04-21 20:12:07 +0000819 xmlBufferWriteChar(buf, " NDATA ");
Daniel Veillard011b63c1999-06-02 17:44:04 +0000820 if (cur->orig != NULL)
821 xmlBufferWriteCHAR(buf, cur->orig);
822 else
823 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000824 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000825 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000826 break;
827 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000828 xmlBufferWriteChar(buf, "<!ENTITY % ");
829 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000830 xmlBufferWriteChar(buf, " ");
831 if (cur->orig == NULL)
832 xmlBufferWriteQuotedString(buf, cur->content);
833 else
834 xmlBufferWriteQuotedString(buf, cur->orig);
835 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000836 break;
837 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000838 xmlBufferWriteChar(buf, "<!ENTITY % ");
839 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000840 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000841 xmlBufferWriteChar(buf, " PUBLIC ");
842 xmlBufferWriteQuotedString(buf, cur->ExternalID);
843 xmlBufferWriteChar(buf, " ");
844 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000845 } else {
Daniel Veillard011b63c1999-06-02 17:44:04 +0000846 xmlBufferWriteChar(buf, " SYSTEM ");
847 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000848 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000849 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000850 break;
851 default:
852 fprintf(stderr,
853 "xmlDumpEntitiesTable: internal: unknown type %d\n",
854 cur->type);
855 }
856 }
857}