ReactOS 0.4.16-dev-2232-gc2aaa52
entities.c
Go to the documentation of this file.
1/*
2 * entities.c : implementation for the XML entities handling
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9/* To avoid EBCDIC trouble when parsing on zOS */
10#if defined(__MVS__)
11#pragma convert("ISO8859-1")
12#endif
13
14#define IN_LIBXML
15#include "libxml.h"
16
17#include <string.h>
18#include <stdlib.h>
19
20#include <libxml/xmlmemory.h>
21#include <libxml/hash.h>
22#include <libxml/entities.h>
23#include <libxml/parser.h>
25#include <libxml/xmlerror.h>
26#include <libxml/dict.h>
27
28#include "private/entities.h"
29#include "private/error.h"
30
31/*
32 * The XML predefined entities.
33 */
34
35static xmlEntity xmlEntityLt = {
36 NULL, XML_ENTITY_DECL, BAD_CAST "lt",
38 BAD_CAST "<", BAD_CAST "<", 1,
40 NULL, NULL, NULL, NULL, 0, 0, 0
41};
42static xmlEntity xmlEntityGt = {
43 NULL, XML_ENTITY_DECL, BAD_CAST "gt",
45 BAD_CAST ">", BAD_CAST ">", 1,
47 NULL, NULL, NULL, NULL, 0, 0, 0
48};
49static xmlEntity xmlEntityAmp = {
50 NULL, XML_ENTITY_DECL, BAD_CAST "amp",
52 BAD_CAST "&", BAD_CAST "&", 1,
54 NULL, NULL, NULL, NULL, 0, 0, 0
55};
56static xmlEntity xmlEntityQuot = {
57 NULL, XML_ENTITY_DECL, BAD_CAST "quot",
59 BAD_CAST "\"", BAD_CAST "\"", 1,
61 NULL, NULL, NULL, NULL, 0, 0, 0
62};
63static xmlEntity xmlEntityApos = {
64 NULL, XML_ENTITY_DECL, BAD_CAST "apos",
66 BAD_CAST "'", BAD_CAST "'", 1,
68 NULL, NULL, NULL, NULL, 0, 0, 0
69};
70
77static void
79{
81}
82
90static void LIBXML_ATTR_FORMAT(2,0)
91xmlEntitiesErr(xmlParserErrors code, const char *msg)
92{
94}
95
103static void LIBXML_ATTR_FORMAT(2,0)
104xmlEntitiesWarn(xmlParserErrors code, const char *msg, const xmlChar *str1)
105{
109 (const char *)str1, NULL, NULL, 0, 0,
110 msg, (const char *)str1, NULL);
111}
112
113/*
114 * xmlFreeEntity : clean-up an entity record.
115 */
116void
117xmlFreeEntity(xmlEntityPtr entity)
118{
119 xmlDictPtr dict = NULL;
120
121 if (entity == NULL)
122 return;
123
124 if (entity->doc != NULL)
125 dict = entity->doc->dict;
126
127
128 if ((entity->children) && (entity->owner == 1) &&
129 (entity == (xmlEntityPtr) entity->children->parent))
130 xmlFreeNodeList(entity->children);
131 if ((entity->name != NULL) &&
132 ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
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);
145}
146
147/*
148 * xmlCreateEntity:
149 *
150 * internal routine doing the entity node structures allocations
151 */
152static xmlEntityPtr
153xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
154 const xmlChar *ExternalID, const xmlChar *SystemID,
155 const xmlChar *content) {
156 xmlEntityPtr ret;
157
158 ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
159 if (ret == NULL) {
160 xmlEntitiesErrMemory("xmlCreateEntity: malloc failed");
161 return(NULL);
162 }
163 memset(ret, 0, sizeof(xmlEntity));
164 ret->type = XML_ENTITY_DECL;
165
166 /*
167 * fill the structure.
168 */
169 ret->etype = (xmlEntityType) type;
170 if (dict == NULL) {
171 ret->name = xmlStrdup(name);
172 if (ExternalID != NULL)
173 ret->ExternalID = xmlStrdup(ExternalID);
174 if (SystemID != NULL)
175 ret->SystemID = xmlStrdup(SystemID);
176 } else {
177 ret->name = xmlDictLookup(dict, name, -1);
178 ret->ExternalID = xmlStrdup(ExternalID);
179 ret->SystemID = xmlStrdup(SystemID);
180 }
181 if (content != NULL) {
182 ret->length = xmlStrlen(content);
183 ret->content = xmlStrndup(content, ret->length);
184 } else {
185 ret->length = 0;
186 ret->content = NULL;
187 }
188 ret->URI = NULL; /* to be computed by the layer knowing
189 the defining entity */
190 ret->orig = NULL;
191 ret->owner = 0;
192
193 return(ret);
194}
195
196/*
197 * xmlAddEntity : register a new entity for an entities table.
198 */
199static xmlEntityPtr
200xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
201 const xmlChar *ExternalID, const xmlChar *SystemID,
202 const xmlChar *content) {
203 xmlDictPtr dict = NULL;
205 xmlEntityPtr ret, predef;
206
207 if (name == NULL)
208 return(NULL);
209 if (dtd == NULL)
210 return(NULL);
211 if (dtd->doc != NULL)
212 dict = dtd->doc->dict;
213
214 switch (type) {
219 if (predef != NULL) {
220 int valid = 0;
221
222 /* 4.6 Predefined Entities */
224 (content != NULL)) {
225 int c = predef->content[0];
226
227 if (((content[0] == c) && (content[1] == 0)) &&
228 ((c == '>') || (c == '\'') || (c == '"'))) {
229 valid = 1;
230 } else if ((content[0] == '&') && (content[1] == '#')) {
231 if (content[2] == 'x') {
232 xmlChar *hex = BAD_CAST "0123456789ABCDEF";
233 xmlChar ref[] = "00;";
234
235 ref[0] = hex[c / 16 % 16];
236 ref[1] = hex[c % 16];
237 if (xmlStrcasecmp(&content[3], ref) == 0)
238 valid = 1;
239 } else {
240 xmlChar ref[] = "00;";
241
242 ref[0] = '0' + c / 10 % 10;
243 ref[1] = '0' + c % 10;
244 if (xmlStrEqual(&content[2], ref))
245 valid = 1;
246 }
247 }
248 }
249 if (!valid) {
250 xmlEntitiesWarn(XML_ERR_ENTITY_PROCESSING,
251 "xmlAddEntity: invalid redeclaration of predefined"
252 " entity '%s'", name);
253 return(NULL);
254 }
255 }
256 if (dtd->entities == NULL)
257 dtd->entities = xmlHashCreateDict(0, dict);
258 table = dtd->entities;
259 break;
262 if (dtd->pentities == NULL)
263 dtd->pentities = xmlHashCreateDict(0, dict);
264 table = dtd->pentities;
265 break;
267 return(NULL);
268 }
269 if (table == NULL)
270 return(NULL);
271 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
272 if (ret == NULL)
273 return(NULL);
274 ret->doc = dtd->doc;
275
276 if (xmlHashAddEntry(table, name, ret)) {
277 /*
278 * entity was already defined at another level.
279 */
281 return(NULL);
282 }
283 return(ret);
284}
285
294xmlEntityPtr
296 if (name == NULL) return(NULL);
297 switch (name[0]) {
298 case 'l':
299 if (xmlStrEqual(name, BAD_CAST "lt"))
300 return(&xmlEntityLt);
301 break;
302 case 'g':
303 if (xmlStrEqual(name, BAD_CAST "gt"))
304 return(&xmlEntityGt);
305 break;
306 case 'a':
307 if (xmlStrEqual(name, BAD_CAST "amp"))
308 return(&xmlEntityAmp);
309 if (xmlStrEqual(name, BAD_CAST "apos"))
310 return(&xmlEntityApos);
311 break;
312 case 'q':
313 if (xmlStrEqual(name, BAD_CAST "quot"))
314 return(&xmlEntityQuot);
315 break;
316 default:
317 break;
318 }
319 return(NULL);
320}
321
335xmlEntityPtr
336xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
337 const xmlChar *ExternalID, const xmlChar *SystemID,
338 const xmlChar *content) {
339 xmlEntityPtr ret;
340 xmlDtdPtr dtd;
341
342 if (doc == NULL) {
343 xmlEntitiesErr(XML_DTD_NO_DOC,
344 "xmlAddDtdEntity: document is NULL");
345 return(NULL);
346 }
347 if (doc->extSubset == NULL) {
348 xmlEntitiesErr(XML_DTD_NO_DTD,
349 "xmlAddDtdEntity: document without external subset");
350 return(NULL);
351 }
352 dtd = doc->extSubset;
353 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
354 if (ret == NULL) return(NULL);
355
356 /*
357 * Link it to the DTD
358 */
359 ret->parent = dtd;
360 ret->doc = dtd->doc;
361 if (dtd->last == NULL) {
362 dtd->children = dtd->last = (xmlNodePtr) ret;
363 } else {
364 dtd->last->next = (xmlNodePtr) ret;
365 ret->prev = dtd->last;
366 dtd->last = (xmlNodePtr) ret;
367 }
368 return(ret);
369}
370
384xmlEntityPtr
385xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
386 const xmlChar *ExternalID, const xmlChar *SystemID,
387 const xmlChar *content) {
388 xmlEntityPtr ret;
389 xmlDtdPtr dtd;
390
391 if (doc == NULL) {
392 xmlEntitiesErr(XML_DTD_NO_DOC,
393 "xmlAddDocEntity: document is NULL");
394 return(NULL);
395 }
396 if (doc->intSubset == NULL) {
397 xmlEntitiesErr(XML_DTD_NO_DTD,
398 "xmlAddDocEntity: document without internal subset");
399 return(NULL);
400 }
401 dtd = doc->intSubset;
402 ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
403 if (ret == NULL) return(NULL);
404
405 /*
406 * Link it to the DTD
407 */
408 ret->parent = dtd;
409 ret->doc = dtd->doc;
410 if (dtd->last == NULL) {
411 dtd->children = dtd->last = (xmlNodePtr) ret;
412 } else {
413 dtd->last->next = (xmlNodePtr) ret;
414 ret->prev = dtd->last;
415 dtd->last = (xmlNodePtr) ret;
416 }
417 return(ret);
418}
419
437xmlEntityPtr
438xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
439 const xmlChar *ExternalID, const xmlChar *SystemID,
440 const xmlChar *content) {
441 xmlEntityPtr ret;
442 xmlDictPtr dict;
443
444 if ((doc != NULL) && (doc->intSubset != NULL)) {
445 return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
446 }
447 if (doc != NULL)
448 dict = doc->dict;
449 else
450 dict = NULL;
451 ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
452 if (ret == NULL)
453 return(NULL);
454 ret->doc = doc;
455 return(ret);
456}
457
469static xmlEntityPtr
470xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
471 return((xmlEntityPtr) xmlHashLookup(table, name));
472}
473
484xmlEntityPtr
485xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
487 xmlEntityPtr ret;
488
489 if (doc == NULL)
490 return(NULL);
491 if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
492 table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
493 ret = xmlGetEntityFromTable(table, name);
494 if (ret != NULL)
495 return(ret);
496 }
497 if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
498 table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
499 return(xmlGetEntityFromTable(table, name));
500 }
501 return(NULL);
502}
503
515xmlEntityPtr
516xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
518
519 if (doc == NULL)
520 return(NULL);
521 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
522 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
523 return(xmlGetEntityFromTable(table, name));
524 }
525 return(NULL);
526}
527
539xmlEntityPtr
540xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
541 xmlEntityPtr cur;
543
544 if (doc != NULL) {
545 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
546 table = (xmlEntitiesTablePtr) doc->intSubset->entities;
547 cur = xmlGetEntityFromTable(table, name);
548 if (cur != NULL)
549 return(cur);
550 }
551 if (doc->standalone != 1) {
552 if ((doc->extSubset != NULL) &&
553 (doc->extSubset->entities != NULL)) {
554 table = (xmlEntitiesTablePtr) doc->extSubset->entities;
555 cur = xmlGetEntityFromTable(table, name);
556 if (cur != NULL)
557 return(cur);
558 }
559 }
560 }
562}
563
564/*
565 * Macro used to grow the current buffer.
566 */
567#define growBufferReentrant() { \
568 xmlChar *tmp; \
569 size_t new_size = buffer_size * 2; \
570 if (new_size < buffer_size) goto mem_error; \
571 tmp = (xmlChar *) xmlRealloc(buffer, new_size); \
572 if (tmp == NULL) goto mem_error; \
573 buffer = tmp; \
574 buffer_size = new_size; \
575}
576
590static xmlChar *
591xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
592 const xmlChar *cur = input;
594 xmlChar *out = NULL;
595 size_t buffer_size = 0;
596 int html = 0;
597
598 if (input == NULL) return(NULL);
599 if (doc != NULL)
600 html = (doc->type == XML_HTML_DOCUMENT_NODE);
601
602 /*
603 * allocate an translation buffer.
604 */
605 buffer_size = 1000;
607 if (buffer == NULL) {
608 xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
609 return(NULL);
610 }
611 out = buffer;
612
613 while (*cur != '\0') {
614 size_t indx = out - buffer;
615 if (indx + 100 > buffer_size) {
616
618 out = &buffer[indx];
619 }
620
621 /*
622 * By default one have to encode at least '<', '>', '"' and '&' !
623 */
624 if (*cur == '<') {
625 const xmlChar *end;
626
627 /*
628 * Special handling of server side include in HTML attributes
629 */
630 if (html && attr &&
631 (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
632 ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
633 while (cur != end) {
634 *out++ = *cur++;
635 indx = out - buffer;
636 if (indx + 100 > buffer_size) {
638 out = &buffer[indx];
639 }
640 }
641 *out++ = *cur++;
642 *out++ = *cur++;
643 *out++ = *cur++;
644 continue;
645 }
646 *out++ = '&';
647 *out++ = 'l';
648 *out++ = 't';
649 *out++ = ';';
650 } else if (*cur == '>') {
651 *out++ = '&';
652 *out++ = 'g';
653 *out++ = 't';
654 *out++ = ';';
655 } else if (*cur == '&') {
656 /*
657 * Special handling of &{...} construct from HTML 4, see
658 * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
659 */
660 if (html && attr && (cur[1] == '{') &&
661 (strchr((const char *) cur, '}'))) {
662 while (*cur != '}') {
663 *out++ = *cur++;
664 indx = out - buffer;
665 if (indx + 100 > buffer_size) {
667 out = &buffer[indx];
668 }
669 }
670 *out++ = *cur++;
671 continue;
672 }
673 *out++ = '&';
674 *out++ = 'a';
675 *out++ = 'm';
676 *out++ = 'p';
677 *out++ = ';';
678 } else if (((*cur >= 0x20) && (*cur < 0x80)) ||
679 (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
680 /*
681 * default case, just copy !
682 */
683 *out++ = *cur;
684 } else if (*cur >= 0x80) {
685 if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
686 /*
687 * Bjørn Reese <br@sseusa.com> provided the patch
688 xmlChar xc;
689 xc = (*cur & 0x3F) << 6;
690 if (cur[1] != 0) {
691 xc += *(++cur) & 0x3F;
692 *out++ = xc;
693 } else
694 */
695 *out++ = *cur;
696 } else {
697 /*
698 * We assume we have UTF-8 input.
699 * It must match either:
700 * 110xxxxx 10xxxxxx
701 * 1110xxxx 10xxxxxx 10xxxxxx
702 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
703 * That is:
704 * cur[0] is 11xxxxxx
705 * cur[1] is 10xxxxxx
706 * cur[2] is 10xxxxxx if cur[0] is 111xxxxx
707 * cur[3] is 10xxxxxx if cur[0] is 1111xxxx
708 * cur[0] is not 11111xxx
709 */
710 char buf[11], *ptr;
711 int val = 0, l = 1;
712
713 if (((cur[0] & 0xC0) != 0xC0) ||
714 ((cur[1] & 0xC0) != 0x80) ||
715 (((cur[0] & 0xE0) == 0xE0) && ((cur[2] & 0xC0) != 0x80)) ||
716 (((cur[0] & 0xF0) == 0xF0) && ((cur[3] & 0xC0) != 0x80)) ||
717 (((cur[0] & 0xF8) == 0xF8))) {
718 xmlEntitiesErr(XML_CHECK_NOT_UTF8,
719 "xmlEncodeEntities: input not UTF-8");
720 snprintf(buf, sizeof(buf), "&#%d;", *cur);
721 buf[sizeof(buf) - 1] = 0;
722 ptr = buf;
723 while (*ptr != 0) *out++ = *ptr++;
724 cur++;
725 continue;
726 } else if (*cur < 0xE0) {
727 val = (cur[0]) & 0x1F;
728 val <<= 6;
729 val |= (cur[1]) & 0x3F;
730 l = 2;
731 } else if (*cur < 0xF0) {
732 val = (cur[0]) & 0x0F;
733 val <<= 6;
734 val |= (cur[1]) & 0x3F;
735 val <<= 6;
736 val |= (cur[2]) & 0x3F;
737 l = 3;
738 } else if (*cur < 0xF8) {
739 val = (cur[0]) & 0x07;
740 val <<= 6;
741 val |= (cur[1]) & 0x3F;
742 val <<= 6;
743 val |= (cur[2]) & 0x3F;
744 val <<= 6;
745 val |= (cur[3]) & 0x3F;
746 l = 4;
747 }
748 if ((l == 1) || (!IS_CHAR(val))) {
749 xmlEntitiesErr(XML_ERR_INVALID_CHAR,
750 "xmlEncodeEntities: char out of range\n");
751 snprintf(buf, sizeof(buf), "&#%d;", *cur);
752 buf[sizeof(buf) - 1] = 0;
753 ptr = buf;
754 while (*ptr != 0) *out++ = *ptr++;
755 cur++;
756 continue;
757 }
758 /*
759 * We could do multiple things here. Just save as a char ref
760 */
761 snprintf(buf, sizeof(buf), "&#x%X;", val);
762 buf[sizeof(buf) - 1] = 0;
763 ptr = buf;
764 while (*ptr != 0) *out++ = *ptr++;
765 cur += l;
766 continue;
767 }
768 } else if (IS_BYTE_CHAR(*cur)) {
769 char buf[11], *ptr;
770
771 snprintf(buf, sizeof(buf), "&#%d;", *cur);
772 buf[sizeof(buf) - 1] = 0;
773 ptr = buf;
774 while (*ptr != 0) *out++ = *ptr++;
775 }
776 cur++;
777 }
778 *out = 0;
779 return(buffer);
780
781mem_error:
782 xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
784 return(NULL);
785}
786
798xmlChar *
799xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
800 return xmlEncodeEntitiesInternal(doc, input, 1);
801}
802
815xmlChar *
816xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
817 return xmlEncodeEntitiesInternal(doc, input, 0);
818}
819
830xmlChar *
831xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlChar *input) {
832 const xmlChar *cur = input;
834 xmlChar *out = NULL;
835 size_t buffer_size = 0;
836 if (input == NULL) return(NULL);
837
838 /*
839 * allocate an translation buffer.
840 */
841 buffer_size = 1000;
843 if (buffer == NULL) {
844 xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
845 return(NULL);
846 }
847 out = buffer;
848
849 while (*cur != '\0') {
850 size_t indx = out - buffer;
851 if (indx + 10 > buffer_size) {
852
854 out = &buffer[indx];
855 }
856
857 /*
858 * By default one have to encode at least '<', '>', '"' and '&' !
859 */
860 if (*cur == '<') {
861 *out++ = '&';
862 *out++ = 'l';
863 *out++ = 't';
864 *out++ = ';';
865 } else if (*cur == '>') {
866 *out++ = '&';
867 *out++ = 'g';
868 *out++ = 't';
869 *out++ = ';';
870 } else if (*cur == '&') {
871 *out++ = '&';
872 *out++ = 'a';
873 *out++ = 'm';
874 *out++ = 'p';
875 *out++ = ';';
876 } else if (*cur == '"') {
877 *out++ = '&';
878 *out++ = 'q';
879 *out++ = 'u';
880 *out++ = 'o';
881 *out++ = 't';
882 *out++ = ';';
883 } else if (*cur == '\r') {
884 *out++ = '&';
885 *out++ = '#';
886 *out++ = '1';
887 *out++ = '3';
888 *out++ = ';';
889 } else {
890 /*
891 * Works because on UTF-8, all extended sequences cannot
892 * result in bytes in the ASCII range.
893 */
894 *out++ = *cur;
895 }
896 cur++;
897 }
898 *out = 0;
899 return(buffer);
900
901mem_error:
902 xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
904 return(NULL);
905}
906
918}
919
927static void
928xmlFreeEntityWrapper(void *entity, const xmlChar *name ATTRIBUTE_UNUSED) {
929 if (entity != NULL)
930 xmlFreeEntity((xmlEntityPtr) entity);
931}
932
939void
941 xmlHashFree(table, xmlFreeEntityWrapper);
942}
943
944#ifdef LIBXML_TREE_ENABLED
953static void *
954xmlCopyEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
955 xmlEntityPtr ent = (xmlEntityPtr) payload;
956 xmlEntityPtr cur;
957
958 cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
959 if (cur == NULL) {
960 xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
961 return(NULL);
962 }
963 memset(cur, 0, sizeof(xmlEntity));
964 cur->type = XML_ENTITY_DECL;
965
966 cur->etype = ent->etype;
967 if (ent->name != NULL)
968 cur->name = xmlStrdup(ent->name);
969 if (ent->ExternalID != NULL)
970 cur->ExternalID = xmlStrdup(ent->ExternalID);
971 if (ent->SystemID != NULL)
972 cur->SystemID = xmlStrdup(ent->SystemID);
973 if (ent->content != NULL)
974 cur->content = xmlStrdup(ent->content);
975 if (ent->orig != NULL)
976 cur->orig = xmlStrdup(ent->orig);
977 if (ent->URI != NULL)
978 cur->URI = xmlStrdup(ent->URI);
979 return(cur);
980}
981
991xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
992 return(xmlHashCopy(table, xmlCopyEntity));
993}
994#endif /* LIBXML_TREE_ENABLED */
995
996#ifdef LIBXML_OUTPUT_ENABLED
997
1006static void
1007xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
1008 if (xmlStrchr(content, '%')) {
1009 const xmlChar * base, *cur;
1010
1011 xmlBufferCCat(buf, "\"");
1012 base = cur = content;
1013 while (*cur != 0) {
1014 if (*cur == '"') {
1015 if (base != cur)
1016 xmlBufferAdd(buf, base, cur - base);
1017 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1018 cur++;
1019 base = cur;
1020 } else if (*cur == '%') {
1021 if (base != cur)
1022 xmlBufferAdd(buf, base, cur - base);
1023 xmlBufferAdd(buf, BAD_CAST "&#x25;", 6);
1024 cur++;
1025 base = cur;
1026 } else {
1027 cur++;
1028 }
1029 }
1030 if (base != cur)
1031 xmlBufferAdd(buf, base, cur - base);
1032 xmlBufferCCat(buf, "\"");
1033 } else {
1034 xmlBufferWriteQuotedString(buf, content);
1035 }
1036}
1037
1045void
1046xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
1047 if ((buf == NULL) || (ent == NULL)) return;
1048 switch (ent->etype) {
1050 xmlBufferWriteChar(buf, "<!ENTITY ");
1051 xmlBufferWriteCHAR(buf, ent->name);
1052 xmlBufferWriteChar(buf, " ");
1053 if (ent->orig != NULL)
1054 xmlBufferWriteQuotedString(buf, ent->orig);
1055 else
1056 xmlDumpEntityContent(buf, ent->content);
1057 xmlBufferWriteChar(buf, ">\n");
1058 break;
1060 xmlBufferWriteChar(buf, "<!ENTITY ");
1061 xmlBufferWriteCHAR(buf, ent->name);
1062 if (ent->ExternalID != NULL) {
1063 xmlBufferWriteChar(buf, " PUBLIC ");
1064 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1065 xmlBufferWriteChar(buf, " ");
1066 xmlBufferWriteQuotedString(buf, ent->SystemID);
1067 } else {
1068 xmlBufferWriteChar(buf, " SYSTEM ");
1069 xmlBufferWriteQuotedString(buf, ent->SystemID);
1070 }
1071 xmlBufferWriteChar(buf, ">\n");
1072 break;
1074 xmlBufferWriteChar(buf, "<!ENTITY ");
1075 xmlBufferWriteCHAR(buf, ent->name);
1076 if (ent->ExternalID != NULL) {
1077 xmlBufferWriteChar(buf, " PUBLIC ");
1078 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1079 xmlBufferWriteChar(buf, " ");
1080 xmlBufferWriteQuotedString(buf, ent->SystemID);
1081 } else {
1082 xmlBufferWriteChar(buf, " SYSTEM ");
1083 xmlBufferWriteQuotedString(buf, ent->SystemID);
1084 }
1085 if (ent->content != NULL) { /* Should be true ! */
1086 xmlBufferWriteChar(buf, " NDATA ");
1087 if (ent->orig != NULL)
1088 xmlBufferWriteCHAR(buf, ent->orig);
1089 else
1090 xmlBufferWriteCHAR(buf, ent->content);
1091 }
1092 xmlBufferWriteChar(buf, ">\n");
1093 break;
1095 xmlBufferWriteChar(buf, "<!ENTITY % ");
1096 xmlBufferWriteCHAR(buf, ent->name);
1097 xmlBufferWriteChar(buf, " ");
1098 if (ent->orig == NULL)
1099 xmlDumpEntityContent(buf, ent->content);
1100 else
1101 xmlBufferWriteQuotedString(buf, ent->orig);
1102 xmlBufferWriteChar(buf, ">\n");
1103 break;
1105 xmlBufferWriteChar(buf, "<!ENTITY % ");
1106 xmlBufferWriteCHAR(buf, ent->name);
1107 if (ent->ExternalID != NULL) {
1108 xmlBufferWriteChar(buf, " PUBLIC ");
1109 xmlBufferWriteQuotedString(buf, ent->ExternalID);
1110 xmlBufferWriteChar(buf, " ");
1111 xmlBufferWriteQuotedString(buf, ent->SystemID);
1112 } else {
1113 xmlBufferWriteChar(buf, " SYSTEM ");
1114 xmlBufferWriteQuotedString(buf, ent->SystemID);
1115 }
1116 xmlBufferWriteChar(buf, ">\n");
1117 break;
1118 default:
1119 xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
1120 "xmlDumpEntitiesDecl: internal: unknown type entity type");
1121 }
1122}
1123
1131static void
1132xmlDumpEntityDeclScan(void *ent, void *buf,
1133 const xmlChar *name ATTRIBUTE_UNUSED) {
1134 xmlDumpEntityDecl((xmlBufferPtr) buf, (xmlEntityPtr) ent);
1135}
1136
1144void
1145xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
1146 xmlHashScan(table, xmlDumpEntityDeclScan, buf);
1147}
1148#endif /* LIBXML_OUTPUT_ENABLED */
#define msg(x)
Definition: auth_time.c:54
r l[0]
Definition: byte_order.h:168
#define NULL
Definition: types.h:112
content
Definition: atl_ax.c:994
_ACRTIMP char *__cdecl strchr(const char *, int)
Definition: string.c:3286
return ret
Definition: mutex.c:146
static xmlEntity xmlEntityLt
Definition: entities.c:35
static xmlEntity xmlEntityApos
Definition: entities.c:63
#define growBufferReentrant()
static xmlEntity xmlEntityGt
Definition: entities.c:42
static xmlEntity xmlEntityQuot
Definition: entities.c:56
static xmlEntity xmlEntityAmp
Definition: entities.c:49
static void xmlEntitiesErrMemory(const char *extra)
Definition: entities.c:78
BOOLEAN valid
FxCollectionEntry * cur
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint GLuint end
Definition: gl.h:1545
GLuint buffer
Definition: glext.h:5915
const GLubyte * c
Definition: glext.h:8905
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint GLfloat * val
Definition: glext.h:7180
GLenum GLenum GLenum input
Definition: glext.h:9031
#define ATTRIBUTE_UNUSED
Definition: i386-dis.c:36
int hex(char ch)
@ extra
Definition: id3.c:95
XMLPUBFUN xmlEntityPtr xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type, const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content)
XMLPUBFUN xmlEntityPtr xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name)
XMLPUBFUN xmlEntitiesTablePtr xmlCreateEntitiesTable(void)
XMLPUBFUN xmlEntityPtr xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type, const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content)
XMLPUBFUN xmlChar * xmlEncodeSpecialChars(const xmlDoc *doc, const xmlChar *input)
XMLPUBFUN void xmlFreeEntity(xmlEntityPtr entity)
XMLPUBFUN xmlEntityPtr xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name)
XMLPUBFUN xmlEntityPtr xmlGetPredefinedEntity(const xmlChar *name)
xmlEntityType
Definition: entities.h:26
@ XML_EXTERNAL_GENERAL_PARSED_ENTITY
Definition: entities.h:28
@ XML_INTERNAL_PREDEFINED_ENTITY
Definition: entities.h:32
@ XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
Definition: entities.h:29
@ XML_INTERNAL_GENERAL_ENTITY
Definition: entities.h:27
@ XML_INTERNAL_PARAMETER_ENTITY
Definition: entities.h:30
@ XML_EXTERNAL_PARAMETER_ENTITY
Definition: entities.h:31
XMLPUBFUN void xmlFreeEntitiesTable(xmlEntitiesTablePtr table)
xmlEntitiesTable * xmlEntitiesTablePtr
Definition: entities.h:71
XMLPUBFUN xmlEntityPtr xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type, const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content)
XMLPUBFUN xmlEntityPtr xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name)
XMLPUBFUN xmlChar * xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input)
static PVOID ptr
Definition: dispmode.c:27
#define IS_CHAR(c)
#define IS_BYTE_CHAR(c)
XML_HIDDEN xmlChar * xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input)
int xmlDictOwns(xmlDictPtr dict, const xmlChar *str)
Definition: dict.c:376
const xmlChar * xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len)
Definition: dict.c:824
xmlFreeFunc xmlFree
Definition: globals.c:184
xmlMallocFunc xmlMalloc
Definition: globals.c:193
void xmlHashScan(xmlHashTablePtr hash, xmlHashScanner scan, void *data)
Definition: hash.c:898
void xmlHashFree(xmlHashTablePtr hash, xmlHashDeallocator dealloc)
Definition: hash.c:229
void * xmlHashLookup(xmlHashTablePtr hash, const xmlChar *key)
Definition: hash.c:739
int xmlHashAddEntry(xmlHashTablePtr hash, const xmlChar *key, void *payload)
Definition: hash.c:621
xmlHashTablePtr xmlHashCreate(int size)
Definition: hash.c:160
xmlHashTablePtr xmlHashCopy(xmlHashTablePtr hash, xmlHashCopier copy)
Definition: hash.c:1050
xmlHashTablePtr xmlHashCreateDict(int size, xmlDictPtr dict)
Definition: hash.c:209
XML_HIDDEN void XML_HIDDEN void __xmlSimpleError(int domain, int code, struct _xmlNode *node, const char *msg, const char *extra) LIBXML_ATTR_FORMAT(4
XML_HIDDEN void __xmlRaiseError(xmlStructuredErrorFunc schannel, xmlGenericErrorFunc channel, void *data, void *ctx, void *nod, int domain, int code, xmlErrorLevel level, const char *file, int line, const char *str1, const char *str2, const char *str3, int int1, int col, const char *msg,...) LIBXML_ATTR_FORMAT(16
XML_HIDDEN void xmlParserErrors const char const xmlChar * str1
Definition: parser.h:35
#define memset(x, y, z)
Definition: compat.h:39
wchar_t const *const size_t const buffer_size
Definition: stat.cpp:95
Definition: dict.c:59
Definition: cookie.c:202
Definition: inflate.c:139
Definition: actctx.c:446
WCHAR * name
Definition: actctx.c:463
Definition: name.c:39
Definition: send.c:48
wchar_t tm const _CrtWcstime_Writes_and_advances_ptr_ count wchar_t ** out
Definition: wcsftime.cpp:383
#define snprintf
Definition: wintirpc.h:48
@ XML_ERR_WARNING
Definition: xmlerror.h:26
@ XML_FROM_TREE
Definition: xmlerror.h:39
xmlParserErrors
Definition: xmlerror.h:99
@ XML_DTD_NO_DTD
Definition: xmlerror.h:242
@ XML_DTD_UNKNOWN_ENTITY
Definition: xmlerror.h:255
@ XML_DTD_NO_DOC
Definition: xmlerror.h:241
@ XML_ERR_INVALID_CHAR
Definition: xmlerror.h:109
@ XML_CHECK_NOT_UTF8
Definition: xmlerror.h:825
@ XML_ERR_ENTITY_PROCESSING
Definition: xmlerror.h:204
@ XML_ERR_NO_MEMORY
Definition: xmlerror.h:102
XMLPUBFUN xmlChar * xmlStrndup(const xmlChar *cur, int len)
Definition: xmlstring.c:45
XMLPUBFUN int xmlStrlen(const xmlChar *str)
Definition: xmlstring.c:428
XMLPUBFUN int xmlStrcasecmp(const xmlChar *str1, const xmlChar *str2)
Definition: xmlstring.c:277
XMLPUBFUN const xmlChar * xmlStrstr(const xmlChar *str, const xmlChar *val)
Definition: xmlstring.c:347
#define BAD_CAST
Definition: xmlstring.h:35
XMLPUBFUN int xmlStrEqual(const xmlChar *str1, const xmlChar *str2)
Definition: xmlstring.c:162
XMLPUBFUN const xmlChar * xmlStrchr(const xmlChar *str, xmlChar val)
Definition: xmlstring.c:327
unsigned char xmlChar
Definition: xmlstring.h:28
XMLPUBFUN xmlChar * xmlStrdup(const xmlChar *cur)
Definition: xmlstring.c:69
#define LIBXML_ATTR_FORMAT(fmt, args)
Definition: xmlversion.h:472
#define const
Definition: zconf.h:233