blob: 30506cb46b67b12c1adc31a2278ae4e39a8176fc [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 *
6 * $Id$
7 */
8
9#include <stdio.h>
Seth Alvese7f12e61998-10-01 20:51:15 +000010#include <stdlib.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000011#include <string.h>
12#include "entities.h"
13
14/*
15 * The XML predefined entities.
16 */
17
18struct xmlPredefinedEntityValue {
19 const char *name;
20 const char *value;
21};
22struct xmlPredefinedEntityValue xmlPredefinedEntityValues[] = {
23 { "lt", "<" },
24 { "gt", ">" },
25 { "apos", "'" },
26 { "quot", "\"" },
27 { "amp", "&" }
28};
29
30xmlEntitiesTablePtr xmlPredefinedEntities = NULL;
31
32/*
33 * A buffer used for converting entities to their equivalent and back.
Daniel Veillardbe36afe1998-11-27 06:39:50 +000034 *
35 * TODO: remove this, this helps performances but forbid reentrancy in a
36 * stupid way.
Daniel Veillard260a68f1998-08-13 03:39:55 +000037 */
38static int buffer_size = 0;
39static CHAR *buffer = NULL;
40
41void growBuffer(void) {
42 buffer_size *= 2;
43 buffer = (CHAR *) realloc(buffer, buffer_size * sizeof(CHAR));
44 if (buffer == NULL) {
45 perror("realloc failed");
46 exit(1);
47 }
48}
49
50/*
51 * xmlFreeEntity : clean-up an entity record.
52 */
Daniel Veillard260a68f1998-08-13 03:39:55 +000053void xmlFreeEntity(xmlEntityPtr entity) {
54 if (entity == NULL) return;
55
56 if (entity->name != NULL)
57 free((char *) entity->name);
58 if (entity->ExternalID != NULL)
59 free((char *) entity->ExternalID);
60 if (entity->SystemID != NULL)
61 free((char *) entity->SystemID);
62 if (entity->content != NULL)
63 free((char *) entity->content);
64 memset(entity, -1, sizeof(xmlEntity));
65}
66
67/*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000068 * xmlAddEntity : register a new entity for an entities table.
Daniel Veillard260a68f1998-08-13 03:39:55 +000069 *
70 * TODO !!! We should check here that the combination of type
71 * ExternalID and SystemID is valid.
72 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +000073static void
74xmlAddEntity(xmlEntitiesTablePtr table, const CHAR *name, int type,
Daniel Veillard260a68f1998-08-13 03:39:55 +000075 const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) {
76 int i;
77 xmlEntityPtr cur;
78 int len;
79
80 for (i = 0;i < table->nb_entities;i++) {
81 cur = &table->table[i];
82 if (!xmlStrcmp(cur->name, name)) {
83 /*
84 * The entity is already defined in this Dtd, the spec says to NOT
85 * override it ... Is it worth a Warning ??? !!!
86 */
87 return;
88 }
89 }
90 if (table->nb_entities >= table->max_entities) {
91 /*
92 * need more elements.
93 */
94 table->max_entities *= 2;
95 table->table = (xmlEntityPtr)
96 realloc(table->table, table->max_entities * sizeof(xmlEntity));
97 if (table->table) {
98 perror("realloc failed");
99 exit(1);
100 }
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;
119 table->nb_entities++;
120}
121
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000122/**
123 * xmlInitializePredefinedEntities:
124 *
125 * Set up the predefined entities.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000126 */
127void xmlInitializePredefinedEntities(void) {
128 int i;
129 CHAR name[50];
130 CHAR value[50];
131 const char *in;
132 CHAR *out;
133
134 if (xmlPredefinedEntities != NULL) return;
135
136 xmlPredefinedEntities = xmlCreateEntitiesTable();
137 for (i = 0;i < sizeof(xmlPredefinedEntityValues) /
138 sizeof(xmlPredefinedEntityValues[0]);i++) {
139 in = xmlPredefinedEntityValues[i].name;
140 out = &name[0];
141 for (;(*out++ = (CHAR) *in);)in++;
142 in = xmlPredefinedEntityValues[i].value;
143 out = &value[0];
144 for (;(*out++ = (CHAR) *in);)in++;
145 xmlAddEntity(xmlPredefinedEntities, (const CHAR *) &name[0],
Daniel Veillard25940b71998-10-29 05:51:30 +0000146 XML_INTERNAL_PREDEFINED_ENTITY, NULL, NULL,
Daniel Veillard260a68f1998-08-13 03:39:55 +0000147 &value[0]);
148 }
149}
150
Daniel Veillardccb09631998-10-27 06:21:04 +0000151/**
152 * xmlGetPredefinedEntity:
153 * @name: the entity name
154 *
155 * Check whether this name is an predefined entity.
156 *
157 * return values: NULL if not, othervise the entity
158 */
159xmlEntityPtr
160xmlGetPredefinedEntity(const CHAR *name) {
161 int i;
162 xmlEntityPtr cur;
163
164 if (xmlPredefinedEntities == NULL)
165 xmlInitializePredefinedEntities();
166 for (i = 0;i < xmlPredefinedEntities->nb_entities;i++) {
167 cur = &xmlPredefinedEntities->table[i];
168 if (!xmlStrcmp(cur->name, name)) return(cur);
169 }
170 return(NULL);
171}
172
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000173/**
174 * xmlAddDtdEntity:
175 * @doc: the document
176 * @name: the entity name
177 * @type: the entity type XML_xxx_yyy_ENTITY
178 * @ExternalID: the entity external ID if available
179 * @SystemID: the entity system ID if available
180 * @content: the entity content
181 *
182 * Register a new entity for this document DTD.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000183 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000184void
185xmlAddDtdEntity(xmlDocPtr doc, const CHAR *name, int type,
Daniel Veillard260a68f1998-08-13 03:39:55 +0000186 const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) {
187 xmlEntitiesTablePtr table;
188
189 if (doc->dtd == NULL) {
190 fprintf(stderr, "xmlAddDtdEntity: document without Dtd !\n");
191 return;
192 }
193 table = (xmlEntitiesTablePtr) doc->dtd->entities;
194 if (table == NULL) {
195 table = xmlCreateEntitiesTable();
196 doc->dtd->entities = table;
197 }
198 xmlAddEntity(table, name, type, ExternalID, SystemID, content);
199}
200
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000201/**
202 * xmlAddDocEntity:
203 * @doc: the document
204 * @name: the entity name
205 * @type: the entity type XML_xxx_yyy_ENTITY
206 * @ExternalID: the entity external ID if available
207 * @SystemID: the entity system ID if available
208 * @content: the entity content
209 *
210 * Register a new entity for this document.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000211 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000212void
213xmlAddDocEntity(xmlDocPtr doc, const CHAR *name, int type,
Daniel Veillard260a68f1998-08-13 03:39:55 +0000214 const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) {
215 xmlEntitiesTablePtr table;
216
217 table = (xmlEntitiesTablePtr) doc->entities;
218 if (table == NULL) {
219 table = xmlCreateEntitiesTable();
220 doc->entities = table;
221 }
222 xmlAddEntity(doc->entities, name, type, ExternalID, SystemID, content);
223}
224
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000225/**
226 * xmlGetDtdEntity:
227 * @doc: the document referencing the entity
228 * @name: the entity name
229 *
230 * Do an entity lookup in the Dtd entity hash table and
231 * returns the corresponding entity, if found.
232 *
233 * return values: A pointer to the entity structure or NULL if not found.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000234 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000235xmlEntityPtr
236xmlGetDtdEntity(xmlDocPtr doc, const CHAR *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000237 int i;
238 xmlEntityPtr cur;
239 xmlEntitiesTablePtr table;
240
241 if ((doc->dtd != NULL) && (doc->dtd->entities != NULL)) {
242 table = (xmlEntitiesTablePtr) doc->dtd->entities;
243 for (i = 0;i < table->nb_entities;i++) {
244 cur = &table->table[i];
245 if (!xmlStrcmp(cur->name, name)) return(cur);
246 }
247 }
248 return(NULL);
249}
250
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000251/**
252 * xmlGetDocEntity:
253 * @doc: the document referencing the entity
254 * @name: the entity name
255 *
256 * Do an entity lookup in the document entity hash table and
257 * returns the corrsponding entity, otherwise a lookup is done
258 * in the predefined entities too.
259 *
260 * return values: A pointer to the entity structure or NULL if not found.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000261 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000262xmlEntityPtr
263xmlGetDocEntity(xmlDocPtr doc, const CHAR *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000264 int i;
265 xmlEntityPtr cur;
266 xmlEntitiesTablePtr table;
267
268 if (doc->entities != NULL) {
269 table = (xmlEntitiesTablePtr) doc->entities;
270 for (i = 0;i < table->nb_entities;i++) {
271 cur = &table->table[i];
272 if (!xmlStrcmp(cur->name, name)) return(cur);
273 }
274 }
275 if (xmlPredefinedEntities == NULL)
276 xmlInitializePredefinedEntities();
277 table = xmlPredefinedEntities;
278 for (i = 0;i < table->nb_entities;i++) {
279 cur = &table->table[i];
280 if (!xmlStrcmp(cur->name, name)) return(cur);
281 }
282
283 return(NULL);
284}
285
286/*
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000287 * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
288 * | [#x10000-#x10FFFF]
289 * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
290 */
291#define IS_CHAR(c) \
292 (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) || \
293 (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF)))
294
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000295/**
296 * xmlEncodeEntities:
297 * @doc: the document containing the string
298 * @input: A string to convert to XML.
299 *
300 * Do a global encoding of a string, replacing the predefined entities
301 * and non ASCII values with their entities and CharRef counterparts.
302 *
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000303 * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii
304 * get erroneous.
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000305 *
306 * TODO This routine is not reentrant and this will be changed, the interface
307 * should not be modified though.
308 *
309 * return values: A newly allocated string with the substitution done.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000310 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000311CHAR *
312xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000313 const CHAR *cur = input;
314 CHAR *out = buffer;
315
Daniel Veillard242590e1998-11-13 18:04:35 +0000316 if (input == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000317 if (buffer == NULL) {
318 buffer_size = 1000;
319 buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
320 if (buffer == NULL) {
321 perror("malloc failed");
322 exit(1);
323 }
324 out = buffer;
325 }
326 while (*cur != '\0') {
327 if (out - buffer > buffer_size - 100) {
328 int index = out - buffer;
329
330 growBuffer();
331 out = &buffer[index];
332 }
333
334 /*
335 * By default one have to encode at least '<', '>', '"' and '&' !
Daniel Veillard260a68f1998-08-13 03:39:55 +0000336 */
337 if (*cur == '<') {
338 *out++ = '&';
339 *out++ = 'l';
340 *out++ = 't';
341 *out++ = ';';
342 } else if (*cur == '>') {
343 *out++ = '&';
344 *out++ = 'g';
345 *out++ = 't';
346 *out++ = ';';
347 } else if (*cur == '&') {
348 *out++ = '&';
349 *out++ = 'a';
350 *out++ = 'm';
351 *out++ = 'p';
352 *out++ = ';';
353 } else if (*cur == '"') {
354 *out++ = '&';
355 *out++ = 'q';
356 *out++ = 'u';
357 *out++ = 'o';
358 *out++ = 't';
359 *out++ = ';';
360 } else if (*cur == '\'') {
361 *out++ = '&';
362 *out++ = 'a';
363 *out++ = 'p';
364 *out++ = 'o';
365 *out++ = 's';
366 *out++ = ';';
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000367 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
368 (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) {
369 /*
370 * default case, just copy !
371 */
372 *out++ = *cur;
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000373#ifndef USE_UTF_8
374 } else if ((sizeof(CHAR) == 1) && (*cur >= 0x80)) {
375 char buf[10], *ptr;
Daniel Veillardda4d3c41998-11-04 20:07:05 +0000376#ifdef HAVE_SNPRINTF
377 snprintf(buf, 9, "&#%d;", *cur);
378#else
379 sprintf(buf, "&#%d;", *cur);
380#endif
Daniel Veillard0ba4d531998-11-01 19:34:31 +0000381 ptr = buf;
382 while (*ptr != 0) *out++ = *ptr++;
383#endif
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000384 } else if (IS_CHAR(*cur)) {
385 char buf[10], *ptr;
386
387#ifdef HAVE_SNPRINTF
388 snprintf(buf, 9, "&#%d;", *cur);
389#else
390 sprintf(buf, "&#%d;", *cur);
391#endif
392 ptr = buf;
393 while (*ptr != 0) *out++ = *ptr++;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000394 }
Daniel Veillard8cc0d1f1998-11-16 01:04:26 +0000395#if 0
396 else {
397 /*
398 * default case, this is not a valid char !
399 * Skip it...
400 */
401 fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);
402 }
403#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000404 cur++;
405 }
406 *out++ = 0;
407 return(buffer);
408}
409
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000410/**
411 * xmlCreateEntitiesTable:
412 *
413 * create and initialize an empty entities hash table.
414 *
415 * return values: the xmlEntitiesTablePtr just created or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000416 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000417xmlEntitiesTablePtr
418xmlCreateEntitiesTable(void) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000419 xmlEntitiesTablePtr ret;
420
421 ret = (xmlEntitiesTablePtr)
422 malloc(sizeof(xmlEntitiesTable));
423 if (ret == NULL) {
424 fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
425 sizeof(xmlEntitiesTable));
426 return(NULL);
427 }
428 ret->max_entities = XML_MIN_ENTITIES_TABLE;
429 ret->nb_entities = 0;
430 ret->table = (xmlEntityPtr )
431 malloc(ret->max_entities * sizeof(xmlEntity));
432 if (ret == NULL) {
433 fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
434 ret->max_entities * sizeof(xmlEntity));
435 free(ret);
436 return(NULL);
437 }
438 return(ret);
439}
440
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000441/**
442 * xmlFreeEntitiesTable:
443 * @table: An entity table
444 *
445 * Deallocate the memory used by an entities hash table.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000446 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000447void
448xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000449 int i;
450
451 if (table == NULL) return;
452
453 for (i = 0;i < table->nb_entities;i++) {
454 xmlFreeEntity(&table->table[i]);
455 }
456 free(table->table);
457 free(table);
458}
459
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000460/**
461 * xmlCopyEntitiesTable:
462 * @table: An entity table
463 *
464 * Build a copy of an entity table.
465 *
466 * return values: the new xmlEntitiesTablePtr or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000467 */
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000468xmlEntitiesTablePtr
469xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
470 xmlEntitiesTablePtr ret;
471 xmlEntityPtr cur, ent;
472 int i;
473
474 ret = (xmlEntitiesTablePtr) malloc(sizeof(xmlEntitiesTable));
475 if (ret == NULL) {
476 fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");
477 return(NULL);
478 }
479 ret->table = (xmlEntityPtr) malloc(table->max_entities *
480 sizeof(xmlEntity));
481 if (ret->table == NULL) {
482 fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");
483 free(ret);
484 return(NULL);
485 }
486 ret->max_entities = table->max_entities;
487 ret->nb_entities = table->nb_entities;
488 for (i = 0;i < ret->nb_entities;i++) {
489 cur = &ret->table[i];
490 ent = &table->table[i];
491 cur->len = ent->len;
492 cur->type = ent->type;
493 if (ent->name != NULL)
494 cur->name = xmlStrdup(ent->name);
495 else
496 cur->name = NULL;
497 if (ent->ExternalID != NULL)
498 cur->ExternalID = xmlStrdup(ent->ExternalID);
499 else
500 cur->ExternalID = NULL;
501 if (ent->SystemID != NULL)
502 cur->SystemID = xmlStrdup(ent->SystemID);
503 else
504 cur->SystemID = NULL;
505 if (ent->content != NULL)
506 cur->content = xmlStrdup(ent->content);
507 else
508 cur->content = NULL;
509 }
510 return(ret);
511}
512
513/**
514 * xmlDumpEntitiesTable:
515 * @table: An entity table
516 *
517 * This will dump the content of the entity table as an XML DTD definition
518 *
519 * NOTE: TODO an extra parameter allowing a reentant implementation will
520 * be added.
521 */
522void
523xmlDumpEntitiesTable(xmlEntitiesTablePtr table) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000524 int i;
525 xmlEntityPtr cur;
526
527 if (table == NULL) return;
528
529 for (i = 0;i < table->nb_entities;i++) {
530 cur = &table->table[i];
531 switch (cur->type) {
532 case XML_INTERNAL_GENERAL_ENTITY:
533 xmlBufferWriteChar("<!ENTITY ");
534 xmlBufferWriteCHAR(cur->name);
535 xmlBufferWriteChar(" \"");
536 xmlBufferWriteCHAR(cur->content);
537 xmlBufferWriteChar("\">\n");
538 break;
539 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
540 xmlBufferWriteChar("<!ENTITY ");
541 xmlBufferWriteCHAR(cur->name);
542 if (cur->ExternalID != NULL) {
543 xmlBufferWriteChar(" PUBLIC \"");
544 xmlBufferWriteCHAR(cur->ExternalID);
545 xmlBufferWriteChar("\" \"");
546 xmlBufferWriteCHAR(cur->SystemID);
547 xmlBufferWriteChar("\"");
548 } else {
549 xmlBufferWriteChar(" SYSTEM \"");
550 xmlBufferWriteCHAR(cur->SystemID);
551 xmlBufferWriteChar("\"");
552 }
553 xmlBufferWriteChar(">\n");
554 break;
555 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
556 xmlBufferWriteChar("<!ENTITY ");
557 xmlBufferWriteCHAR(cur->name);
558 if (cur->ExternalID != NULL) {
559 xmlBufferWriteChar(" PUBLIC \"");
560 xmlBufferWriteCHAR(cur->ExternalID);
561 xmlBufferWriteChar("\" \"");
562 xmlBufferWriteCHAR(cur->SystemID);
563 xmlBufferWriteChar("\"");
564 } else {
565 xmlBufferWriteChar(" SYSTEM \"");
566 xmlBufferWriteCHAR(cur->SystemID);
567 xmlBufferWriteChar("\"");
568 }
569 if (cur->content != NULL) { /* Should be true ! */
570 xmlBufferWriteChar(" NDATA ");
571 xmlBufferWriteCHAR(cur->content);
572 }
573 xmlBufferWriteChar(">\n");
574 break;
575 case XML_INTERNAL_PARAMETER_ENTITY:
576 xmlBufferWriteChar("<!ENTITY % ");
577 xmlBufferWriteCHAR(cur->name);
578 xmlBufferWriteChar(" \"");
579 xmlBufferWriteCHAR(cur->content);
580 xmlBufferWriteChar("\">\n");
581 break;
582 case XML_EXTERNAL_PARAMETER_ENTITY:
583 xmlBufferWriteChar("<!ENTITY % ");
584 xmlBufferWriteCHAR(cur->name);
585 if (cur->ExternalID != NULL) {
586 xmlBufferWriteChar(" PUBLIC \"");
587 xmlBufferWriteCHAR(cur->ExternalID);
588 xmlBufferWriteChar("\" \"");
589 xmlBufferWriteCHAR(cur->SystemID);
590 xmlBufferWriteChar("\"");
591 } else {
592 xmlBufferWriteChar(" SYSTEM \"");
593 xmlBufferWriteCHAR(cur->SystemID);
594 xmlBufferWriteChar("\"");
595 }
596 xmlBufferWriteChar(">\n");
597 break;
598 default:
599 fprintf(stderr,
600 "xmlDumpEntitiesTable: internal: unknown type %d\n",
601 cur->type);
602 }
603 }
604}