2 * entities.c : implementation for the XML entities handling
4 * See Copyright for the status of this software.
9 /* To avoid EBCDIC trouble when parsing on zOS */
11 #pragma convert("ISO8859-1")
21 #include <libxml/xmlmemory.h>
22 #include <libxml/hash.h>
23 #include <libxml/entities.h>
24 #include <libxml/parser.h>
25 #include <libxml/parserInternals.h>
26 #include <libxml/xmlerror.h>
27 #include <libxml/globals.h>
28 #include <libxml/dict.h>
33 * The XML predefined entities.
36 static xmlEntity xmlEntityLt = {
37 NULL, XML_ENTITY_DECL, BAD_CAST "lt",
38 NULL, NULL, NULL, NULL, NULL, NULL,
39 BAD_CAST "<", BAD_CAST "<", 1,
40 XML_INTERNAL_PREDEFINED_ENTITY,
41 NULL, NULL, NULL, NULL, 0, 1
43 static xmlEntity xmlEntityGt = {
44 NULL, XML_ENTITY_DECL, BAD_CAST "gt",
45 NULL, NULL, NULL, NULL, NULL, NULL,
46 BAD_CAST ">", BAD_CAST ">", 1,
47 XML_INTERNAL_PREDEFINED_ENTITY,
48 NULL, NULL, NULL, NULL, 0, 1
50 static xmlEntity xmlEntityAmp = {
51 NULL, XML_ENTITY_DECL, BAD_CAST "amp",
52 NULL, NULL, NULL, NULL, NULL, NULL,
53 BAD_CAST "&", BAD_CAST "&", 1,
54 XML_INTERNAL_PREDEFINED_ENTITY,
55 NULL, NULL, NULL, NULL, 0, 1
57 static xmlEntity xmlEntityQuot = {
58 NULL, XML_ENTITY_DECL, BAD_CAST "quot",
59 NULL, NULL, NULL, NULL, NULL, NULL,
60 BAD_CAST "\"", BAD_CAST "\"", 1,
61 XML_INTERNAL_PREDEFINED_ENTITY,
62 NULL, NULL, NULL, NULL, 0, 1
64 static xmlEntity xmlEntityApos = {
65 NULL, XML_ENTITY_DECL, BAD_CAST "apos",
66 NULL, NULL, NULL, NULL, NULL, NULL,
67 BAD_CAST "'", BAD_CAST "'", 1,
68 XML_INTERNAL_PREDEFINED_ENTITY,
69 NULL, NULL, NULL, NULL, 0, 1
73 * xmlEntitiesErrMemory:
74 * @extra: extra informations
76 * Handle an out of memory condition
79 xmlEntitiesErrMemory(const char *extra)
81 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
86 * @code: the error code
89 * Handle an out of memory condition
91 static void LIBXML_ATTR_FORMAT(2,0)
92 xmlEntitiesErr(xmlParserErrors code, const char *msg)
94 __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);
98 * xmlFreeEntity : clean-up an entity record.
101 xmlFreeEntity(xmlEntityPtr entity)
103 xmlDictPtr dict = NULL;
108 if (entity->doc != NULL)
109 dict = entity->doc->dict;
112 if ((entity->children) && (entity->owner == 1) &&
113 (entity == (xmlEntityPtr) entity->children->parent))
114 xmlFreeNodeList(entity->children);
116 if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
117 xmlFree((char *) entity->name);
118 if ((entity->ExternalID != NULL) &&
119 (!xmlDictOwns(dict, entity->ExternalID)))
120 xmlFree((char *) entity->ExternalID);
121 if ((entity->SystemID != NULL) &&
122 (!xmlDictOwns(dict, entity->SystemID)))
123 xmlFree((char *) entity->SystemID);
124 if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
125 xmlFree((char *) entity->URI);
126 if ((entity->content != NULL)
127 && (!xmlDictOwns(dict, entity->content)))
128 xmlFree((char *) entity->content);
129 if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
130 xmlFree((char *) entity->orig);
132 if (entity->name != NULL)
133 xmlFree((char *) entity->name);
134 if (entity->ExternalID != NULL)
135 xmlFree((char *) entity->ExternalID);
136 if (entity->SystemID != NULL)
137 xmlFree((char *) entity->SystemID);
138 if (entity->URI != NULL)
139 xmlFree((char *) entity->URI);
140 if (entity->content != NULL)
141 xmlFree((char *) entity->content);
142 if (entity->orig != NULL)
143 xmlFree((char *) entity->orig);
151 * internal routine doing the entity node strutures allocations
154 xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
155 const xmlChar *ExternalID, const xmlChar *SystemID,
156 const xmlChar *content) {
159 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
161 xmlEntitiesErrMemory("xmlCreateEntity: malloc failed");
164 memset(ret, 0, sizeof(xmlEntity));
165 ret->type = XML_ENTITY_DECL;
169 * fill the structure.
171 ret->etype = (xmlEntityType) type;
173 ret->name = xmlStrdup(name);
174 if (ExternalID != NULL)
175 ret->ExternalID = xmlStrdup(ExternalID);
176 if (SystemID != NULL)
177 ret->SystemID = xmlStrdup(SystemID);
179 ret->name = xmlDictLookup(dict, name, -1);
180 if (ExternalID != NULL)
181 ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
182 if (SystemID != NULL)
183 ret->SystemID = xmlDictLookup(dict, SystemID, -1);
185 if (content != NULL) {
186 ret->length = xmlStrlen(content);
187 if ((dict != NULL) && (ret->length < 5))
188 ret->content = (xmlChar *)
189 xmlDictLookup(dict, content, ret->length);
191 ret->content = xmlStrndup(content, ret->length);
196 ret->URI = NULL; /* to be computed by the layer knowing
197 the defining entity */
205 * xmlAddEntity : register a new entity for an entities table.
208 xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
209 const xmlChar *ExternalID, const xmlChar *SystemID,
210 const xmlChar *content) {
211 xmlDictPtr dict = NULL;
212 xmlEntitiesTablePtr table = NULL;
219 if (dtd->doc != NULL)
220 dict = dtd->doc->dict;
223 case XML_INTERNAL_GENERAL_ENTITY:
224 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
225 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
226 if (dtd->entities == NULL)
227 dtd->entities = xmlHashCreateDict(0, dict);
228 table = dtd->entities;
230 case XML_INTERNAL_PARAMETER_ENTITY:
231 case XML_EXTERNAL_PARAMETER_ENTITY:
232 if (dtd->pentities == NULL)
233 dtd->pentities = xmlHashCreateDict(0, dict);
234 table = dtd->pentities;
236 case XML_INTERNAL_PREDEFINED_ENTITY:
241 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
246 if (xmlHashAddEntry(table, name, ret)) {
248 * entity was already defined at another level.
257 * xmlGetPredefinedEntity:
258 * @name: the entity name
260 * Check whether this name is an predefined entity.
262 * Returns NULL if not, otherwise the entity
265 xmlGetPredefinedEntity(const xmlChar *name) {
266 if (name == NULL) return(NULL);
269 if (xmlStrEqual(name, BAD_CAST "lt"))
270 return(&xmlEntityLt);
273 if (xmlStrEqual(name, BAD_CAST "gt"))
274 return(&xmlEntityGt);
277 if (xmlStrEqual(name, BAD_CAST "amp"))
278 return(&xmlEntityAmp);
279 if (xmlStrEqual(name, BAD_CAST "apos"))
280 return(&xmlEntityApos);
283 if (xmlStrEqual(name, BAD_CAST "quot"))
284 return(&xmlEntityQuot);
295 * @name: the entity name
296 * @type: the entity type XML_xxx_yyy_ENTITY
297 * @ExternalID: the entity external ID if available
298 * @SystemID: the entity system ID if available
299 * @content: the entity content
301 * Register a new entity for this document DTD external subset.
303 * Returns a pointer to the entity or NULL in case of error
306 xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
307 const xmlChar *ExternalID, const xmlChar *SystemID,
308 const xmlChar *content) {
313 xmlEntitiesErr(XML_DTD_NO_DOC,
314 "xmlAddDtdEntity: document is NULL");
317 if (doc->extSubset == NULL) {
318 xmlEntitiesErr(XML_DTD_NO_DTD,
319 "xmlAddDtdEntity: document without external subset");
322 dtd = doc->extSubset;
323 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
324 if (ret == NULL) return(NULL);
331 if (dtd->last == NULL) {
332 dtd->children = dtd->last = (xmlNodePtr) ret;
334 dtd->last->next = (xmlNodePtr) ret;
335 ret->prev = dtd->last;
336 dtd->last = (xmlNodePtr) ret;
344 * @name: the entity name
345 * @type: the entity type XML_xxx_yyy_ENTITY
346 * @ExternalID: the entity external ID if available
347 * @SystemID: the entity system ID if available
348 * @content: the entity content
350 * Register a new entity for this document.
352 * Returns a pointer to the entity or NULL in case of error
355 xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
356 const xmlChar *ExternalID, const xmlChar *SystemID,
357 const xmlChar *content) {
362 xmlEntitiesErr(XML_DTD_NO_DOC,
363 "xmlAddDocEntity: document is NULL");
366 if (doc->intSubset == NULL) {
367 xmlEntitiesErr(XML_DTD_NO_DTD,
368 "xmlAddDocEntity: document without internal subset");
371 dtd = doc->intSubset;
372 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
373 if (ret == NULL) return(NULL);
380 if (dtd->last == NULL) {
381 dtd->children = dtd->last = (xmlNodePtr) ret;
383 dtd->last->next = (xmlNodePtr) ret;
384 ret->prev = dtd->last;
385 dtd->last = (xmlNodePtr) ret;
393 * @name: the entity name
394 * @type: the entity type XML_xxx_yyy_ENTITY
395 * @ExternalID: the entity external ID if available
396 * @SystemID: the entity system ID if available
397 * @content: the entity content
399 * Create a new entity, this differs from xmlAddDocEntity() that if
400 * the document is NULL or has no internal subset defined, then an
401 * unlinked entity structure will be returned, it is then the responsability
402 * of the caller to link it to the document later or free it when not needed
405 * Returns a pointer to the entity or NULL in case of error
408 xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
409 const xmlChar *ExternalID, const xmlChar *SystemID,
410 const xmlChar *content) {
414 if ((doc != NULL) && (doc->intSubset != NULL)) {
415 return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
421 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
429 * xmlGetEntityFromTable:
430 * @table: an entity table
431 * @name: the entity name
432 * @parameter: look for parameter entities
434 * Do an entity lookup in the table.
435 * returns the corresponding parameter entity, if found.
437 * Returns A pointer to the entity structure or NULL if not found.
440 xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
441 return((xmlEntityPtr) xmlHashLookup(table, name));
445 * xmlGetParameterEntity:
446 * @doc: the document referencing the entity
447 * @name: the entity name
449 * Do an entity lookup in the internal and external subsets and
450 * returns the corresponding parameter entity, if found.
452 * Returns A pointer to the entity structure or NULL if not found.
455 xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
456 xmlEntitiesTablePtr table;
461 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
462 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
463 ret = xmlGetEntityFromTable(table, name);
467 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
468 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
469 return(xmlGetEntityFromTable(table, name));
476 * @doc: the document referencing the entity
477 * @name: the entity name
479 * Do an entity lookup in the DTD entity hash table and
480 * returns the corresponding entity, if found.
481 * Note: the first argument is the document node, not the DTD node.
483 * Returns A pointer to the entity structure or NULL if not found.
486 xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
487 xmlEntitiesTablePtr table;
491 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
492 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
493 return(xmlGetEntityFromTable(table, name));
500 * @doc: the document referencing the entity
501 * @name: the entity name
503 * Do an entity lookup in the document entity hash table and
504 * returns the corresponding entity, otherwise a lookup is done
505 * in the predefined entities too.
507 * Returns A pointer to the entity structure or NULL if not found.
510 xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
512 xmlEntitiesTablePtr table;
515 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
516 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
517 cur = xmlGetEntityFromTable(table, name);
521 if (doc->standalone != 1) {
522 if ((doc->extSubset != NULL) &&
523 (doc->extSubset->entities != NULL)) {
524 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
525 cur = xmlGetEntityFromTable(table, name);
531 return(xmlGetPredefinedEntity(name));
535 * Macro used to grow the current buffer.
537 #define growBufferReentrant() { \
539 size_t new_size = buffer_size * 2; \
540 if (new_size < buffer_size) goto mem_error; \
541 tmp = (xmlChar *) xmlRealloc(buffer, new_size); \
542 if (tmp == NULL) goto mem_error; \
544 buffer_size = new_size; \
548 * xmlEncodeEntitiesInternal:
549 * @doc: the document containing the string
550 * @input: A string to convert to XML.
551 * @attr: are we handling an atrbute value
553 * Do a global encoding of a string, replacing the predefined entities
554 * and non ASCII values with their entities and CharRef counterparts.
555 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
556 * must be deallocated.
558 * Returns A newly allocated string with the substitution done.
561 xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
562 const xmlChar *cur = input;
563 xmlChar *buffer = NULL;
565 size_t buffer_size = 0;
568 if (input == NULL) return(NULL);
570 html = (doc->type == XML_HTML_DOCUMENT_NODE);
573 * allocate an translation buffer.
576 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
577 if (buffer == NULL) {
578 xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
583 while (*cur != '\0') {
584 size_t indx = out - buffer;
585 if (indx + 100 > buffer_size) {
587 growBufferReentrant();
592 * By default one have to encode at least '<', '>', '"' and '&' !
598 * Special handling of server side include in HTML attributes
601 (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
602 ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
606 if (indx + 100 > buffer_size) {
607 growBufferReentrant();
620 } else if (*cur == '>') {
625 } else if (*cur == '&') {
627 * Special handling of &{...} construct from HTML 4, see
628 * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
630 if (html && attr && (cur[1] == '{') &&
631 (strchr((const char *) cur, '}'))) {
632 while (*cur != '}') {
635 if (indx + 100 > buffer_size) {
636 growBufferReentrant();
648 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
649 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
651 * default case, just copy !
654 } else if (*cur >= 0x80) {
655 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
657 * Bjørn Reese <br@sseusa.com> provided the patch
659 xc = (*cur & 0x3F) << 6;
661 xc += *(++cur) & 0x3F;
668 * We assume we have UTF-8 input.
674 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
675 "xmlEncodeEntities: input not UTF-8");
677 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
678 snprintf(buf, sizeof(buf), "&#%d;", *cur);
679 buf[sizeof(buf) - 1] = 0;
681 while (*ptr != 0) *out++ = *ptr++;
684 } else if (*cur < 0xE0) {
685 val = (cur[0]) & 0x1F;
687 val |= (cur[1]) & 0x3F;
689 } else if (*cur < 0xF0) {
690 val = (cur[0]) & 0x0F;
692 val |= (cur[1]) & 0x3F;
694 val |= (cur[2]) & 0x3F;
696 } else if (*cur < 0xF8) {
697 val = (cur[0]) & 0x07;
699 val |= (cur[1]) & 0x3F;
701 val |= (cur[2]) & 0x3F;
703 val |= (cur[3]) & 0x3F;
706 if ((l == 1) || (!IS_CHAR(val))) {
707 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
708 "xmlEncodeEntities: char out of range\n");
710 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
711 snprintf(buf, sizeof(buf), "&#%d;", *cur);
712 buf[sizeof(buf) - 1] = 0;
714 while (*ptr != 0) *out++ = *ptr++;
719 * We could do multiple things here. Just save as a char ref
721 snprintf(buf, sizeof(buf), "&#x%X;", val);
722 buf[sizeof(buf) - 1] = 0;
724 while (*ptr != 0) *out++ = *ptr++;
728 } else if (IS_BYTE_CHAR(*cur)) {
731 snprintf(buf, sizeof(buf), "&#%d;", *cur);
732 buf[sizeof(buf) - 1] = 0;
734 while (*ptr != 0) *out++ = *ptr++;
742 xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
748 * xmlEncodeAttributeEntities:
749 * @doc: the document containing the string
750 * @input: A string to convert to XML.
752 * Do a global encoding of a string, replacing the predefined entities
753 * and non ASCII values with their entities and CharRef counterparts for
756 * Returns A newly allocated string with the substitution done.
759 xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
760 return xmlEncodeEntitiesInternal(doc, input, 1);
764 * xmlEncodeEntitiesReentrant:
765 * @doc: the document containing the string
766 * @input: A string to convert to XML.
768 * Do a global encoding of a string, replacing the predefined entities
769 * and non ASCII values with their entities and CharRef counterparts.
770 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
771 * must be deallocated.
773 * Returns A newly allocated string with the substitution done.
776 xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
777 return xmlEncodeEntitiesInternal(doc, input, 0);
781 * xmlEncodeSpecialChars:
782 * @doc: the document containing the string
783 * @input: A string to convert to XML.
785 * Do a global encoding of a string, replacing the predefined entities
786 * this routine is reentrant, and result must be deallocated.
788 * Returns A newly allocated string with the substitution done.
791 xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlChar *input) {
792 const xmlChar *cur = input;
793 xmlChar *buffer = NULL;
795 size_t buffer_size = 0;
796 if (input == NULL) return(NULL);
799 * allocate an translation buffer.
802 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
803 if (buffer == NULL) {
804 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
809 while (*cur != '\0') {
810 size_t indx = out - buffer;
811 if (indx + 10 > buffer_size) {
813 growBufferReentrant();
818 * By default one have to encode at least '<', '>', '"' and '&' !
825 } else if (*cur == '>') {
830 } else if (*cur == '&') {
836 } else if (*cur == '"') {
843 } else if (*cur == '\r') {
851 * Works because on UTF-8, all extended sequences cannot
852 * result in bytes in the ASCII range.
862 xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
868 * xmlCreateEntitiesTable:
870 * create and initialize an empty entities hash table.
871 * This really doesn't make sense and should be deprecated
873 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
876 xmlCreateEntitiesTable(void) {
877 return((xmlEntitiesTablePtr) xmlHashCreate(0));
881 * xmlFreeEntityWrapper:
885 * Deallocate the memory used by an entities in the hash table.
888 xmlFreeEntityWrapper(xmlEntityPtr entity,
889 const xmlChar *name ATTRIBUTE_UNUSED) {
891 xmlFreeEntity(entity);
895 * xmlFreeEntitiesTable:
896 * @table: An entity table
898 * Deallocate the memory used by an entities hash table.
901 xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
902 xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
905 #ifdef LIBXML_TREE_ENABLED
910 * Build a copy of an entity
912 * Returns the new xmlEntitiesPtr or NULL in case of error.
915 xmlCopyEntity(xmlEntityPtr ent) {
918 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
920 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
923 memset(cur, 0, sizeof(xmlEntity));
924 cur->type = XML_ENTITY_DECL;
926 cur->etype = ent->etype;
927 if (ent->name != NULL)
928 cur->name = xmlStrdup(ent->name);
929 if (ent->ExternalID != NULL)
930 cur->ExternalID = xmlStrdup(ent->ExternalID);
931 if (ent->SystemID != NULL)
932 cur->SystemID = xmlStrdup(ent->SystemID);
933 if (ent->content != NULL)
934 cur->content = xmlStrdup(ent->content);
935 if (ent->orig != NULL)
936 cur->orig = xmlStrdup(ent->orig);
937 if (ent->URI != NULL)
938 cur->URI = xmlStrdup(ent->URI);
943 * xmlCopyEntitiesTable:
944 * @table: An entity table
946 * Build a copy of an entity table.
948 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
951 xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
952 return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
954 #endif /* LIBXML_TREE_ENABLED */
956 #ifdef LIBXML_OUTPUT_ENABLED
959 * xmlDumpEntityContent:
960 * @buf: An XML buffer.
961 * @content: The entity content.
963 * This will dump the quoted string value, taking care of the special
964 * treatment required by %
967 xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
968 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
969 if (xmlStrchr(content, '%')) {
970 const xmlChar * base, *cur;
972 xmlBufferCCat(buf, "\"");
973 base = cur = content;
977 xmlBufferAdd(buf, base, cur - base);
978 xmlBufferAdd(buf, BAD_CAST """, 6);
981 } else if (*cur == '%') {
983 xmlBufferAdd(buf, base, cur - base);
984 xmlBufferAdd(buf, BAD_CAST "%", 6);
992 xmlBufferAdd(buf, base, cur - base);
993 xmlBufferCCat(buf, "\"");
995 xmlBufferWriteQuotedString(buf, content);
1000 * xmlDumpEntityDecl:
1001 * @buf: An XML buffer.
1002 * @ent: An entity table
1004 * This will dump the content of the entity table as an XML DTD definition
1007 xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
1008 if ((buf == NULL) || (ent == NULL)) return;
1009 switch (ent->etype) {
1010 case XML_INTERNAL_GENERAL_ENTITY:
1011 xmlBufferWriteChar(buf, "<!ENTITY ");
1012 xmlBufferWriteCHAR(buf, ent->name);
1013 xmlBufferWriteChar(buf, " ");
1014 if (ent->orig != NULL)
1015 xmlBufferWriteQuotedString(buf, ent->orig);
1017 xmlDumpEntityContent(buf, ent->content);
1018 xmlBufferWriteChar(buf, ">\n");
1020 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1021 xmlBufferWriteChar(buf, "<!ENTITY ");
1022 xmlBufferWriteCHAR(buf, ent->name);
1023 if (ent->ExternalID != NULL) {
1024 xmlBufferWriteChar(buf, " PUBLIC ");
1025 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1026 xmlBufferWriteChar(buf, " ");
1027 xmlBufferWriteQuotedString(buf, ent->SystemID);
1029 xmlBufferWriteChar(buf, " SYSTEM ");
1030 xmlBufferWriteQuotedString(buf, ent->SystemID);
1032 xmlBufferWriteChar(buf, ">\n");
1034 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1035 xmlBufferWriteChar(buf, "<!ENTITY ");
1036 xmlBufferWriteCHAR(buf, ent->name);
1037 if (ent->ExternalID != NULL) {
1038 xmlBufferWriteChar(buf, " PUBLIC ");
1039 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1040 xmlBufferWriteChar(buf, " ");
1041 xmlBufferWriteQuotedString(buf, ent->SystemID);
1043 xmlBufferWriteChar(buf, " SYSTEM ");
1044 xmlBufferWriteQuotedString(buf, ent->SystemID);
1046 if (ent->content != NULL) { /* Should be true ! */
1047 xmlBufferWriteChar(buf, " NDATA ");
1048 if (ent->orig != NULL)
1049 xmlBufferWriteCHAR(buf, ent->orig);
1051 xmlBufferWriteCHAR(buf, ent->content);
1053 xmlBufferWriteChar(buf, ">\n");
1055 case XML_INTERNAL_PARAMETER_ENTITY:
1056 xmlBufferWriteChar(buf, "<!ENTITY % ");
1057 xmlBufferWriteCHAR(buf, ent->name);
1058 xmlBufferWriteChar(buf, " ");
1059 if (ent->orig == NULL)
1060 xmlDumpEntityContent(buf, ent->content);
1062 xmlBufferWriteQuotedString(buf, ent->orig);
1063 xmlBufferWriteChar(buf, ">\n");
1065 case XML_EXTERNAL_PARAMETER_ENTITY:
1066 xmlBufferWriteChar(buf, "<!ENTITY % ");
1067 xmlBufferWriteCHAR(buf, ent->name);
1068 if (ent->ExternalID != NULL) {
1069 xmlBufferWriteChar(buf, " PUBLIC ");
1070 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1071 xmlBufferWriteChar(buf, " ");
1072 xmlBufferWriteQuotedString(buf, ent->SystemID);
1074 xmlBufferWriteChar(buf, " SYSTEM ");
1075 xmlBufferWriteQuotedString(buf, ent->SystemID);
1077 xmlBufferWriteChar(buf, ">\n");
1080 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
1081 "xmlDumpEntitiesDecl: internal: unknown type entity type");
1086 * xmlDumpEntityDeclScan:
1087 * @ent: An entity table
1088 * @buf: An XML buffer.
1090 * When using the hash table scan function, arguments need to be reversed
1093 xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
1094 xmlDumpEntityDecl(buf, ent);
1098 * xmlDumpEntitiesTable:
1099 * @buf: An XML buffer.
1100 * @table: An entity table
1102 * This will dump the content of the entity table as an XML DTD definition
1105 xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
1106 xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
1108 #endif /* LIBXML_OUTPUT_ENABLED */
1109 #define bottom_entities
1110 #include "elfgcchack.h"