50#include "magick/studio.h"
51#include "magick/blob.h"
52#include "magick/blob-private.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/image-private.h"
56#include "magick/log.h"
57#include "magick/memory_.h"
58#include "magick/semaphore.h"
59#include "magick/string_.h"
60#include "magick/string-private.h"
61#include "magick/token-private.h"
62#include "magick/utility.h"
63#include "magick/utility-private.h"
64#include "magick/xml-tree.h"
65#include "magick/xml-tree-private.h"
70#define NumberPredefinedEntities 10
71#define XMLWhitespace "\t\r\n "
118 ***processing_instructions,
136 *sentinel[] = { (
char *) NULL };
166MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
167 const char *tag,
const size_t offset)
172 if (xml_info == (XMLTreeInfo *) NULL)
173 return((XMLTreeInfo *) NULL);
174 child=(XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
175 if (child == (XMLTreeInfo *) NULL)
176 return((XMLTreeInfo *) NULL);
177 (void) memset(child,0,
sizeof(*child));
178 child->tag=ConstantString(tag);
179 child->attributes=sentinel;
180 child->content=ConstantString(
"");
181 child->debug=IsEventLogging();
182 child->signature=MagickCoreSignature;
183 return(InsertTagIntoXMLTree(xml_info,child,offset));
214MagickExport XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
215 const char *path,
const size_t offset)
219 subnode[MaxTextExtent],
233 assert(xml_info != (XMLTreeInfo *) NULL);
234 assert((xml_info->signature == MagickCoreSignature) ||
235 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
236 if (IsEventLogging() != MagickFalse)
237 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
239 components=GetPathComponents(path,&number_components);
240 if (components == (
char **) NULL)
241 return((XMLTreeInfo *) NULL);
242 for (i=0; i < (ssize_t) number_components; i++)
244 GetPathComponent(components[i],SubimagePath,subnode);
245 GetPathComponent(components[i],CanonicalPath,tag);
246 child=GetXMLTreeChild(node,tag);
247 if (child == (XMLTreeInfo *) NULL)
248 child=AddChildToXMLTree(node,tag,offset);
250 if (node == (XMLTreeInfo *) NULL)
252 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
254 node=GetXMLTreeOrdered(node);
255 if (node == (XMLTreeInfo *) NULL)
258 if (node == (XMLTreeInfo *) NULL)
260 components[i]=DestroyString(components[i]);
262 for ( ; i < (ssize_t) number_components; i++)
263 components[i]=DestroyString(components[i]);
264 components=(
char **) RelinquishMagickMemory(components);
296MagickExport
char *CanonicalXMLContent(
const char *content,
297 const MagickBooleanType pedantic)
316 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
317 if (utf8 == (
unsigned char *) NULL)
318 return((
char *) NULL);
319 for (p=utf8; *p !=
'\0'; p++)
320 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
327 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
328 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
329 if (base64 == (
char *) NULL)
330 return((
char *) NULL);
331 canonical_content=AcquireString(
"<base64>");
332 (void) ConcatenateString(&canonical_content,base64);
333 base64=DestroyString(base64);
334 (void) ConcatenateString(&canonical_content,
"</base64>");
335 return(canonical_content);
341 canonical_content=AcquireString((
char *) NULL);
342 extent=MaxTextExtent;
343 for (p=utf8; *p !=
'\0'; p++)
345 if ((i+MaxTextExtent) > (ssize_t) extent)
347 extent+=MaxTextExtent;
348 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
349 sizeof(*canonical_content));
350 if (canonical_content == (
char *) NULL)
351 return(canonical_content);
357 i+=FormatLocaleString(canonical_content+i,extent,
"&");
362 i+=FormatLocaleString(canonical_content+i,extent,
"<");
367 i+=FormatLocaleString(canonical_content+i,extent,
">");
372 i+=FormatLocaleString(canonical_content+i,extent,
""");
377 if (pedantic == MagickFalse)
379 canonical_content[i++]=(char) (*p);
382 i+=FormatLocaleString(canonical_content+i,extent,
"
");
387 if (pedantic == MagickFalse)
389 canonical_content[i++]=(char) (*p);
392 i+=FormatLocaleString(canonical_content+i,extent,
"	");
397 i+=FormatLocaleString(canonical_content+i,extent,
"
");
402 canonical_content[i++]=(char) (*p);
407 canonical_content[i]=
'\0';
408 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
409 return(canonical_content);
436 *DestroyXMLTree_(XMLTreeInfo *,
const size_t);
438static char **DestroyXMLTreeAttributes(
char **attributes)
446 if ((attributes == (
char **) NULL) || (attributes == sentinel))
447 return((
char **) NULL);
448 for (i=0; attributes[i] != (
char *) NULL; i+=2)
453 if (attributes[i] != (
char *) NULL)
454 attributes[i]=DestroyString(attributes[i]);
455 if (attributes[i+1] != (
char *) NULL)
456 attributes[i+1]=DestroyString(attributes[i+1]);
458 attributes=(
char **) RelinquishMagickMemory(attributes);
459 return((
char **) NULL);
462static void DestroyXMLTreeChild(XMLTreeInfo *xml_info,
469 child=xml_info->child;
470 while (child != (XMLTreeInfo *) NULL)
474 node->child=(XMLTreeInfo *) NULL;
475 (void) DestroyXMLTree_(node,depth+1);
479static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info,
486 ordered=xml_info->ordered;
487 while (ordered != (XMLTreeInfo *) NULL)
490 ordered=node->ordered;
491 node->ordered=(XMLTreeInfo *) NULL;
492 (void) DestroyXMLTree_(node,depth+1);
496static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
508 assert(xml_info != (XMLTreeInfo *) NULL);
509 assert((xml_info->signature == MagickCoreSignature) ||
510 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
511 if (IsEventLogging() != MagickFalse)
512 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
513 if (xml_info->parent != (XMLTreeInfo *) NULL)
518 root=(XMLTreeRoot *) xml_info;
519 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
520 root->entities[i+1]=DestroyString(root->entities[i+1]);
521 root->entities=(
char **) RelinquishMagickMemory(root->entities);
522 for (i=0; root->attributes[i] != (
char **) NULL; i++)
524 attributes=root->attributes[i];
525 if (attributes[0] != (
char *) NULL)
526 attributes[0]=DestroyString(attributes[0]);
527 for (j=1; attributes[j] != (
char *) NULL; j+=3)
529 if (attributes[j] != (
char *) NULL)
530 attributes[j]=DestroyString(attributes[j]);
531 if (attributes[j+1] != (
char *) NULL)
532 attributes[j+1]=DestroyString(attributes[j+1]);
533 if (attributes[j+2] != (
char *) NULL)
534 attributes[j+2]=DestroyString(attributes[j+2]);
536 attributes=(
char **) RelinquishMagickMemory(attributes);
538 if (root->attributes[0] != (
char **) NULL)
539 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
540 if (root->processing_instructions[0] != (
char **) NULL)
542 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
544 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
545 root->processing_instructions[i][j]=DestroyString(
546 root->processing_instructions[i][j]);
547 root->processing_instructions[i][j+1]=DestroyString(
548 root->processing_instructions[i][j+1]);
549 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
550 root->processing_instructions[i]);
552 root->processing_instructions=(
char ***) RelinquishMagickMemory(
553 root->processing_instructions);
557static XMLTreeInfo *DestroyXMLTree_(XMLTreeInfo *xml_info,
560 assert(xml_info != (XMLTreeInfo *) NULL);
561 assert((xml_info->signature == MagickCoreSignature) ||
562 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
563 if (IsEventLogging() != MagickFalse)
564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
565 if (depth > MagickMaxRecursionDepth)
566 ThrowFatalException(ResourceLimitFatalError,
567 "MemoryAllocationFailed");
568 DestroyXMLTreeChild(xml_info,depth+1);
569 DestroyXMLTreeOrdered(xml_info,depth+1);
570 DestroyXMLTreeRoot(xml_info);
571 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
572 xml_info->content=DestroyString(xml_info->content);
573 xml_info->tag=DestroyString(xml_info->tag);
574 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
575 return((XMLTreeInfo *) NULL);
578MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
580 return(DestroyXMLTree_(xml_info,0));
607MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
628 assert(filename != (
const char *) NULL);
631 if (LocaleCompare(filename,
"-") != 0)
632 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
634 return((
char *) NULL);
635 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
637 if ((file == fileno(stdin)) || (offset < 0) ||
638 (offset != (MagickOffsetType) ((ssize_t) offset)))
649 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
650 quantum=(size_t) MagickMaxBufferExtent;
651 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
652 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
653 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
654 for (i=0; xml != (
char *) NULL; i+=count)
656 count=read(file,xml+i,quantum);
663 if (~((
size_t) i) < (quantum+1))
665 xml=(
char *) RelinquishMagickMemory(xml);
668 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
669 if ((
size_t) (i+count) >= extent)
672 if (LocaleCompare(filename,
"-") != 0)
673 file=close_utf8(file);
674 if (xml == (
char *) NULL)
675 return((
char *) NULL);
678 xml=(
char *) RelinquishMagickMemory(xml);
679 return((
char *) NULL);
681 length=(size_t) MagickMin(i+count,extent);
685 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
687 if (~length >= (MaxTextExtent-1))
688 xml=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*xml));
689 if (xml == (
char *) NULL)
691 file=close_utf8(file);
692 return((
char *) NULL);
694 map=MapBlob(file,ReadMode,0,length);
695 if (map != (
char *) NULL)
697 (void) memcpy(xml,map,length);
698 (void) UnmapBlob(map,length);
702 (void) lseek(file,0,SEEK_SET);
703 for (i=0; i < length; i+=count)
705 count=read(file,xml+i,(
size_t) MagickMin(length-i,(
size_t) MagickMaxBufferExtent));
715 file=close_utf8(file)-1;
716 xml=(
char *) RelinquishMagickMemory(xml);
717 return((
char *) NULL);
721 if (LocaleCompare(filename,
"-") != 0)
722 file=close_utf8(file);
724 xml=(
char *) RelinquishMagickMemory(xml);
750MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
752 assert(xml_info != (XMLTreeInfo *) NULL);
753 assert((xml_info->signature == MagickCoreSignature) ||
754 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
755 if (IsEventLogging() != MagickFalse)
756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
757 return(xml_info->next);
785MagickExport
const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
795 assert(xml_info != (XMLTreeInfo *) NULL);
796 assert((xml_info->signature == MagickCoreSignature) ||
797 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
798 if (IsEventLogging() != MagickFalse)
799 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
800 if (xml_info->attributes == (
char **) NULL)
801 return((
const char *) NULL);
803 while ((xml_info->attributes[i] != (
char *) NULL) &&
804 (strcmp(xml_info->attributes[i],tag) != 0))
806 if (xml_info->attributes[i] != (
char *) NULL)
807 return(xml_info->attributes[i+1]);
808 root=(XMLTreeRoot*) xml_info;
809 while (root->root.parent != (XMLTreeInfo *) NULL)
810 root=(XMLTreeRoot *) root->root.parent;
812 while ((root->attributes[i] != (
char **) NULL) &&
813 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
815 if (root->attributes[i] == (
char **) NULL)
816 return((
const char *) NULL);
818 while ((root->attributes[i][j] != (
char *) NULL) &&
819 (strcmp(root->attributes[i][j],tag) != 0))
821 if (root->attributes[i][j] == (
char *) NULL)
822 return((
const char *) NULL);
823 return(root->attributes[i][j+1]);
852MagickExport MagickBooleanType GetXMLTreeAttributes(
const XMLTreeInfo *xml_info,
853 SplayTreeInfo *attributes)
858 assert(xml_info != (XMLTreeInfo *) NULL);
859 assert((xml_info->signature == MagickCoreSignature) ||
860 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
861 assert(attributes != (SplayTreeInfo *) NULL);
862 if (IsEventLogging() != MagickFalse)
863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
864 if (xml_info->attributes == (
char **) NULL)
867 while (xml_info->attributes[i] != (
char *) NULL)
869 (void) AddValueToSplayTree(attributes,
870 ConstantString(xml_info->attributes[i]),
871 ConstantString(xml_info->attributes[i+1]));
900MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
905 assert(xml_info != (XMLTreeInfo *) NULL);
906 assert((xml_info->signature == MagickCoreSignature) ||
907 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
908 if (IsEventLogging() != MagickFalse)
909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
910 child=xml_info->child;
911 if (tag != (
const char *) NULL)
912 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
913 child=child->sibling;
940MagickExport
const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
942 assert(xml_info != (XMLTreeInfo *) NULL);
943 assert((xml_info->signature == MagickCoreSignature) ||
944 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
945 if (IsEventLogging() != MagickFalse)
946 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
947 return(xml_info->content);
972MagickExport XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
974 assert(xml_info != (XMLTreeInfo *) NULL);
975 assert((xml_info->signature == MagickCoreSignature) ||
976 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
977 if (IsEventLogging() != MagickFalse)
978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
979 return(xml_info->ordered);
1007MagickExport XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
const char *path)
1011 subnode[MaxTextExtent],
1024 assert(xml_info != (XMLTreeInfo *) NULL);
1025 assert((xml_info->signature == MagickCoreSignature) ||
1026 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1027 if (IsEventLogging() != MagickFalse)
1028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1030 components=GetPathComponents(path,&number_components);
1031 if (components == (
char **) NULL)
1032 return((XMLTreeInfo *) NULL);
1033 for (i=0; i < (ssize_t) number_components; i++)
1035 GetPathComponent(components[i],SubimagePath,subnode);
1036 GetPathComponent(components[i],CanonicalPath,tag);
1037 node=GetXMLTreeChild(node,tag);
1038 if (node == (XMLTreeInfo *) NULL)
1040 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1042 node=GetXMLTreeOrdered(node);
1043 if (node == (XMLTreeInfo *) NULL)
1046 if (node == (XMLTreeInfo *) NULL)
1048 components[i]=DestroyString(components[i]);
1050 for ( ; i < (ssize_t) number_components; i++)
1051 components[i]=DestroyString(components[i]);
1052 components=(
char **) RelinquishMagickMemory(components);
1080MagickExport
const char **GetXMLTreeProcessingInstructions(
1081 XMLTreeInfo *xml_info,
const char *target)
1089 assert(xml_info != (XMLTreeInfo *) NULL);
1090 assert((xml_info->signature == MagickCoreSignature) ||
1091 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1092 if (IsEventLogging() != MagickFalse)
1093 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1094 root=(XMLTreeRoot *) xml_info;
1095 while (root->root.parent != (XMLTreeInfo *) NULL)
1096 root=(XMLTreeRoot *) root->root.parent;
1098 while ((root->processing_instructions[i] != (
char **) NULL) &&
1099 (strcmp(root->processing_instructions[i][0],target) != 0))
1101 if (root->processing_instructions[i] == (
char **) NULL)
1102 return((
const char **) sentinel);
1103 return((
const char **) (root->processing_instructions[i]+1));
1128MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1130 assert(xml_info != (XMLTreeInfo *) NULL);
1131 assert((xml_info->signature == MagickCoreSignature) ||
1132 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1133 if (IsEventLogging() != MagickFalse)
1134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1135 return(xml_info->sibling);
1160MagickExport
const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1162 assert(xml_info != (XMLTreeInfo *) NULL);
1163 assert((xml_info->signature == MagickCoreSignature) ||
1164 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1165 if (IsEventLogging() != MagickFalse)
1166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1167 return(xml_info->tag);
1198MagickExport XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1199 XMLTreeInfo *child,
const size_t offset)
1206 child->ordered=(XMLTreeInfo *) NULL;
1207 child->sibling=(XMLTreeInfo *) NULL;
1208 child->next=(XMLTreeInfo *) NULL;
1209 child->offset=offset;
1210 child->parent=xml_info;
1211 if (xml_info->child == (XMLTreeInfo *) NULL)
1213 xml_info->child=child;
1216 head=xml_info->child;
1217 if (head->offset > offset)
1219 child->ordered=head;
1220 xml_info->child=child;
1225 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1226 (node->ordered->offset <= offset))
1228 child->ordered=node->ordered;
1229 node->ordered=child;
1231 previous=(XMLTreeInfo *) NULL;
1233 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1238 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1240 while ((node->next != (XMLTreeInfo *) NULL) &&
1241 (node->next->offset <= offset))
1243 child->next=node->next;
1248 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1249 previous->sibling=node->sibling;
1251 previous=(XMLTreeInfo *) NULL;
1253 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1258 child->sibling=node;
1259 if (previous != (XMLTreeInfo *) NULL)
1260 previous->sibling=child;
1291static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1309 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1310 if (utf8 == (
char *) NULL)
1311 return((
char *) NULL);
1312 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1318 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1324 for (i=2; i < (ssize_t) (*length-1); i+=2)
1326 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1327 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1328 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1330 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1331 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1332 (content[i] & 0xff);
1333 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
1335 if ((
size_t) (j+MaxTextExtent) > extent)
1337 extent=(size_t) j+MaxTextExtent;
1338 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1339 if (utf8 == (
char *) NULL)
1352 for (bits=0;
byte != 0;
byte/=2)
1355 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1359 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1364 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1365 if (utf8 != (
char *) NULL)
1370static char *ParseEntities(
char *xml,
char **entities,
int state)
1394 for ( ; *xml !=
'\0'; xml++)
1395 while (*xml ==
'\r')
1399 (void) memmove(xml,xml+1,strlen(xml));
1403 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1404 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1416 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1422 c=strtol(xml+2,&entity,10);
1424 c=strtol(xml+3,&entity,16);
1425 if ((c == 0) || (*entity !=
';'))
1441 for (i=0;
byte != 0;
byte/=2)
1444 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1449 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1453 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1456 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1457 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1463 while ((entities[i] != (
char *) NULL) &&
1464 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1466 if (entities[i++] == (
char *) NULL)
1469 if (entities[i] != (
char *) NULL)
1474 length=strlen(entities[i]);
1475 entity=strchr(xml,
';');
1476 if ((entity != (
char *) NULL) &&
1477 ((length-1L) >= (
size_t) (entity-xml)))
1479 offset=(ssize_t) (xml-p);
1480 extent=(size_t) (offset+length+strlen(entity));
1483 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1484 if (p == (
char *) NULL)
1485 ThrowFatalException(ResourceLimitFatalError,
1486 "MemoryAllocationFailed");
1494 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1495 sizeof(*extent_xml));
1496 if (extent_xml != (
char *) NULL)
1498 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1499 (void) CopyMagickString(extent_xml,p,extent*
1500 sizeof(*extent_xml));
1504 if (p == (
char *) NULL)
1505 ThrowFatalException(ResourceLimitFatalError,
1506 "MemoryAllocationFailed");
1508 entity=strchr(xml,
';');
1510 if (entity != (
char *) NULL)
1511 (void) memmove(xml+length,entity+1,strlen(entity));
1512 (void) memcpy(xml,entities[i],length);
1516 if (((state ==
' ') || (state ==
'*')) &&
1517 (isspace((
int) ((
unsigned char) *xml) != 0)))
1528 for (xml=p; *xml !=
'\0'; xml++)
1533 i=(ssize_t) strspn(xml,accept);
1535 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1536 while ((*xml !=
'\0') && (*xml !=
' '))
1542 if ((xml >= p) && (*xml ==
' '))
1545 return(p == q ? ConstantString(p) : p);
1548static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1549 const size_t length,
const char state)
1554 xml_info=root->node;
1555 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1559 xml=ParseEntities(xml,root->entities,state);
1560 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1562 (void) ConcatenateString(&xml_info->content,xml);
1563 xml=DestroyString(xml);
1567 if (xml_info->content != (
char *) NULL)
1568 xml_info->content=DestroyString(xml_info->content);
1569 xml_info->content=xml;
1573static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1574 ExceptionInfo *exception)
1576 if ((root->node == (XMLTreeInfo *) NULL) ||
1577 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1579 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1580 "ParseError",
"unexpected closing tag </%s>",tag);
1581 return(&root->root);
1583 root->node=root->node->parent;
1584 return((XMLTreeInfo *) NULL);
1587static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1588 const size_t depth,
char **entities)
1596 if (depth > MagickMaxRecursionDepth)
1597 return(MagickFalse);
1600 while ((*xml !=
'\0') && (*xml !=
'&'))
1604 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1605 return(MagickFalse);
1607 while ((entities[i] != (
char *) NULL) &&
1608 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1610 if ((entities[i] != (
char *) NULL) &&
1611 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1612 return(MagickFalse);
1616static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1628 xml+=strcspn(xml,XMLWhitespace);
1632 xml+=strspn(xml+1,XMLWhitespace)+1;
1634 if (strcmp(target,
"xml") == 0)
1636 xml=strstr(xml,
"standalone");
1637 if ((xml != (
char *) NULL) &&
1638 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1639 root->standalone=MagickTrue;
1642 if (root->processing_instructions[0] == (
char **) NULL)
1644 root->processing_instructions=(
char ***) AcquireMagickMemory(
sizeof(
1645 *root->processing_instructions));
1646 if (root->processing_instructions ==(
char ***) NULL)
1647 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1648 *root->processing_instructions=(
char **) NULL;
1651 while ((root->processing_instructions[i] != (
char **) NULL) &&
1652 (strcmp(target,root->processing_instructions[i][0]) != 0))
1654 if (root->processing_instructions[i] == (
char **) NULL)
1656 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1657 root->processing_instructions,(
size_t) (i+2),
1658 sizeof(*root->processing_instructions));
1659 if (root->processing_instructions == (
char ***) NULL)
1660 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1661 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1662 sizeof(**root->processing_instructions));
1663 if (root->processing_instructions[i] == (
char **) NULL)
1664 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1665 root->processing_instructions[i+1]=(
char **) NULL;
1666 root->processing_instructions[i][0]=ConstantString(target);
1667 root->processing_instructions[i][1]=(
char *)
1668 root->processing_instructions[i+1];
1669 root->processing_instructions[i+1]=(
char **) NULL;
1670 root->processing_instructions[i][2]=ConstantString(
"");
1673 while (root->processing_instructions[i][j] != (
char *) NULL)
1675 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1676 root->processing_instructions[i],(
size_t) (j+3),
1677 sizeof(**root->processing_instructions));
1678 if (root->processing_instructions[i] == (
char **) NULL)
1679 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1680 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1681 root->processing_instructions[i][j+1],(
size_t) (j+1),
1682 sizeof(***root->processing_instructions));
1683 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1684 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1685 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1686 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1687 root->processing_instructions[i][j]=ConstantString(xml);
1688 root->processing_instructions[i][j+1]=(
char *) NULL;
1691static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1692 size_t length,ExceptionInfo *exception)
1698 **predefined_entities,
1708 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1709 if (predefined_entities == (
char **) NULL)
1710 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1711 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1712 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1714 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1718 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1723 if (strspn(xml+8,XMLWhitespace) == 0)
1725 xml+=strspn(xml+8,XMLWhitespace)+8;
1727 n=xml+strspn(xml,XMLWhitespace
"%");
1728 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1730 xml=n+strcspn(n,XMLWhitespace);
1734 v=xml+strspn(xml+1,XMLWhitespace)+1;
1737 if ((q !=
'"') && (q !=
'\''))
1742 xml=strchr(xml,
'>');
1745 entities=(*c ==
'%') ? predefined_entities : root->entities;
1746 for (i=0; entities[i] != (
char *) NULL; i++) ;
1747 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1749 if (entities == (
char **) NULL)
1750 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1752 predefined_entities=entities;
1754 root->entities=entities;
1758 if (xml != (
char *) NULL)
1763 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1764 entities[i+2]=(
char *) NULL;
1765 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1769 if (entities[i+1] != v)
1770 entities[i+1]=DestroyString(entities[i+1]);
1771 (void) ThrowMagickException(exception,GetMagickModule(),
1772 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1773 predefined_entities=(
char **) RelinquishMagickMemory(
1774 predefined_entities);
1775 return(MagickFalse);
1779 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1784 t=xml+strspn(xml+9,XMLWhitespace)+9;
1787 (void) ThrowMagickException(exception,GetMagickModule(),
1788 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1789 predefined_entities=(
char **) RelinquishMagickMemory(
1790 predefined_entities);
1791 return(MagickFalse);
1793 xml=t+strcspn(t,XMLWhitespace
">");
1798 while ((root->attributes[i] != (
char **) NULL) &&
1799 (n != (
char *) NULL) &&
1800 (strcmp(n,root->attributes[i][0]) != 0))
1802 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1805 xml=n+strcspn(n,XMLWhitespace);
1810 (void) ThrowMagickException(exception,GetMagickModule(),
1811 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1812 predefined_entities=(
char **) RelinquishMagickMemory(
1813 predefined_entities);
1814 return(MagickFalse);
1816 xml+=strspn(xml+1,XMLWhitespace)+1;
1817 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1818 if (strncmp(xml,
"NOTATION",8) == 0)
1819 xml+=strspn(xml+8,XMLWhitespace)+8;
1820 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1821 strcspn(xml,XMLWhitespace);
1822 if (xml == (
char *) NULL)
1824 (void) ThrowMagickException(exception,GetMagickModule(),
1825 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1826 predefined_entities=(
char **) RelinquishMagickMemory(
1827 predefined_entities);
1828 return(MagickFalse);
1830 xml+=strspn(xml,XMLWhitespace
")");
1831 if (strncmp(xml,
"#FIXED",6) == 0)
1832 xml+=strspn(xml+6,XMLWhitespace)+6;
1835 xml+=strcspn(xml,XMLWhitespace
">")-1;
1841 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1842 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1846 (void) ThrowMagickException(exception,GetMagickModule(),
1847 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1848 predefined_entities=(
char **) RelinquishMagickMemory(
1849 predefined_entities);
1850 return(MagickFalse);
1852 if (root->attributes[i] == (
char **) NULL)
1858 root->attributes=(
char ***) AcquireQuantumMemory(2,
1859 sizeof(*root->attributes));
1861 root->attributes=(
char ***) ResizeQuantumMemory(
1862 root->attributes,(
size_t) (i+2),
1863 sizeof(*root->attributes));
1864 if (root->attributes == (
char ***) NULL)
1865 ThrowFatalException(ResourceLimitFatalError,
1866 "MemoryAllocationFailed");
1867 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1868 sizeof(**root->attributes));
1869 if (root->attributes[i] == (
char **) NULL)
1870 ThrowFatalException(ResourceLimitFatalError,
1871 "MemoryAllocationFailed");
1872 root->attributes[i][0]=ConstantString(t);
1873 root->attributes[i][1]=(
char *) NULL;
1874 root->attributes[i+1]=(
char **) NULL;
1876 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1877 root->attributes[i]=(
char **) ResizeQuantumMemory(
1878 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1879 if (root->attributes[i] == (
char **) NULL)
1880 ThrowFatalException(ResourceLimitFatalError,
1881 "MemoryAllocationFailed");
1882 root->attributes[i][j+3]=(
char *) NULL;
1883 root->attributes[i][j+2]=ConstantString(c);
1884 root->attributes[i][j+1]=(
char *) NULL;
1885 if (v != (
char *) NULL)
1886 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1887 root->attributes[i][j]=ConstantString(n);
1891 if (strncmp(xml,
"<!--", 4) == 0)
1892 xml=strstr(xml+4,
"-->");
1894 if (strncmp(xml,
"<?", 2) == 0)
1898 if (xml != (
char *) NULL)
1900 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1906 xml=strchr(xml,
'>');
1908 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1911 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1915static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
1920 xml_info=root->node;
1921 if (xml_info->tag == (
char *) NULL)
1922 xml_info->tag=ConstantString(tag);
1924 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1925 if (xml_info != (XMLTreeInfo *) NULL)
1926 xml_info->attributes=attributes;
1927 root->node=xml_info;
1938static inline MagickBooleanType IsSkipTag(
const char *tag)
1944 while (skip_tags[i] != (
const char *) NULL)
1946 if (LocaleCompare(tag,skip_tags[i]) == 0)
1950 return(MagickFalse);
1953MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1984 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1986 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1987 "ParseError",
"root tag missing");
1988 return((XMLTreeInfo *) NULL);
1990 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1992 utf8=ConvertUTF16ToUTF8(xml,&length);
1993 if (utf8 == (
char *) NULL)
1995 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1996 "ParseError",
"UTF16 to UTF8 failed");
1997 return((XMLTreeInfo *) NULL);
2001 utf8=DestroyString(utf8);
2002 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2003 "ParseError",
"root tag missing");
2004 return((XMLTreeInfo *) NULL);
2006 terminal=utf8[length-1];
2007 utf8[length-1]=
'\0';
2009 while ((*p !=
'\0') && (*p !=
'<'))
2013 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2014 "ParseError",
"root tag missing");
2015 utf8=DestroyString(utf8);
2016 return((XMLTreeInfo *) NULL);
2018 attribute=(
char **) NULL;
2023 attributes=(
char **) sentinel;
2026 if ((isalpha((
int) ((
unsigned char) *p)) !=0) || (*p ==
'_') ||
2027 (*p ==
':') || (c <
'\0'))
2032 if (root->node == (XMLTreeInfo *) NULL)
2034 (void) ThrowMagickException(exception,GetMagickModule(),
2035 OptionWarning,
"ParseError",
"root tag missing");
2036 utf8=DestroyString(utf8);
2037 return(&root->root);
2039 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
2040 while (isspace((
int) ((
unsigned char) *p)) != 0)
2042 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
2043 (ignore_depth == 0))
2045 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2051 while ((root->attributes[i] != (
char **) NULL) &&
2052 (strcmp(root->attributes[i][0],tag) != 0))
2054 attribute=root->attributes[i];
2056 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
2062 attributes=(
char **) AcquireQuantumMemory(4,
2063 sizeof(*attributes));
2065 attributes=(
char **) ResizeQuantumMemory(attributes,
2066 (
size_t) (l+4),
sizeof(*attributes));
2067 if (attributes == (
char **) NULL)
2069 (void) ThrowMagickException(exception,GetMagickModule(),
2070 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2071 utf8=DestroyString(utf8);
2072 return(&root->root);
2074 attributes[l+2]=(
char *) NULL;
2075 attributes[l+1]=(
char *) NULL;
2077 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
2078 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2079 attributes[l]=ConstantString(
"");
2083 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2085 if ((c ==
'"') || (c ==
'\''))
2092 while ((*p !=
'\0') && (*p != c))
2098 attributes[l]=ConstantString(
"");
2099 attributes[l+1]=ConstantString(
"");
2100 (void) DestroyXMLTreeAttributes(attributes);
2101 (void) ThrowMagickException(exception,
2102 GetMagickModule(),OptionWarning,
"ParseError",
2104 utf8=DestroyString(utf8);
2105 return(&root->root);
2108 while ((attribute != (
char **) NULL) &&
2109 (attribute[j] != (
char *) NULL) &&
2110 (strcmp(attribute[j],attributes[l]) != 0))
2112 attributes[l+1]=ParseEntities(attributes[l+1],
2113 root->entities,(attribute != (
char **) NULL) &&
2114 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2117 attributes[l]=ConstantString(attributes[l]);
2119 while (isspace((
int) ((
unsigned char) *p)) != 0)
2125 while((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2134 if (((*p !=
'\0') && (*p !=
'>')) ||
2135 ((*p ==
'\0') && (terminal !=
'>')))
2138 (void) DestroyXMLTreeAttributes(attributes);
2139 (void) ThrowMagickException(exception,GetMagickModule(),
2140 OptionWarning,
"ParseError",
"missing >");
2141 utf8=DestroyString(utf8);
2142 return(&root->root);
2144 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2145 (void) DestroyXMLTreeAttributes(attributes);
2148 ParseOpenTag(root,tag,attributes);
2149 (void) ParseCloseTag(root,tag,exception);
2155 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2158 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2159 ParseOpenTag(root,tag,attributes);
2163 (void) DestroyXMLTreeAttributes(attributes);
2170 (void) DestroyXMLTreeAttributes(attributes);
2171 (void) ThrowMagickException(exception,GetMagickModule(),
2172 OptionWarning,
"ParseError",
"missing >");
2173 utf8=DestroyString(utf8);
2174 return(&root->root);
2185 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2187 if ((c ==
'\0') && (terminal !=
'>'))
2189 (void) ThrowMagickException(exception,GetMagickModule(),
2190 OptionWarning,
"ParseError",
"missing >");
2191 utf8=DestroyString(utf8);
2192 return(&root->root);
2195 if ((ignore_depth == 0) &&
2196 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2198 utf8=DestroyString(utf8);
2199 return(&root->root);
2201 if (ignore_depth > 0)
2204 if (isspace((
int) ((
unsigned char) *p)) != 0)
2205 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2208 if (strncmp(p,
"!--",3) == 0)
2214 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2215 ((*p ==
'\0') && (terminal !=
'>')))
2217 (void) ThrowMagickException(exception,GetMagickModule(),
2218 OptionWarning,
"ParseError",
"unclosed <!--");
2219 utf8=DestroyString(utf8);
2220 return(&root->root);
2224 if (strncmp(p,
"![CDATA[",8) == 0)
2230 if (p != (
char *) NULL)
2233 if (ignore_depth == 0)
2234 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2238 (void) ThrowMagickException(exception,GetMagickModule(),
2239 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2240 utf8=DestroyString(utf8);
2241 return(&root->root);
2245 if (strncmp(p,
"!DOCTYPE",8) == 0)
2250 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2251 ((l != 0) && ((*p !=
']') ||
2252 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2253 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2254 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2255 if ((*p ==
'\0') && (terminal !=
'>'))
2257 (void) ThrowMagickException(exception,GetMagickModule(),
2258 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2259 utf8=DestroyString(utf8);
2260 return(&root->root);
2263 tag=strchr(tag,
'[')+1;
2266 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2268 if (status == MagickFalse)
2270 utf8=DestroyString(utf8);
2271 return(&root->root);
2285 if (p == (
char *) NULL)
2288 }
while ((*p !=
'\0') && (*p !=
'>'));
2289 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2292 (void) ThrowMagickException(exception,GetMagickModule(),
2293 OptionWarning,
"ParseError",
"unclosed <?");
2294 utf8=DestroyString(utf8);
2295 return(&root->root);
2297 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2301 (void) ThrowMagickException(exception,GetMagickModule(),
2302 OptionWarning,
"ParseError",
"unexpected <");
2303 utf8=DestroyString(utf8);
2304 return(&root->root);
2306 if ((p == (
char *) NULL) || (*p ==
'\0'))
2310 if ((*p !=
'\0') && (*p !=
'<'))
2315 while ((*p !=
'\0') && (*p !=
'<'))
2319 if (ignore_depth == 0)
2320 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2326 utf8=DestroyString(utf8);
2327 if (root->node == (XMLTreeInfo *) NULL)
2328 return(&root->root);
2329 if (root->node->tag == (
char *) NULL)
2331 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2332 "ParseError",
"root tag missing");
2333 return(&root->root);
2335 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2336 "ParseError",
"unclosed tag: `%s'",root->node->tag);
2337 return(&root->root);
2362MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2365 *predefined_entities[NumberPredefinedEntities+1] =
2367 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2368 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2374 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2375 (void) memset(root,0,
sizeof(*root));
2376 root->root.tag=(
char *) NULL;
2377 if (tag != (
char *) NULL)
2378 root->root.tag=ConstantString(tag);
2379 root->node=(&root->root);
2380 root->root.content=ConstantString(
"");
2381 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2382 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2383 root->root.attributes=sentinel;
2384 root->attributes=(
char ***) root->root.attributes;
2385 root->processing_instructions=(
char ***) root->root.attributes;
2386 root->debug=IsEventLogging();
2387 root->signature=MagickCoreSignature;
2388 return(&root->root);
2414MagickExport XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2419 assert(xml_info != (XMLTreeInfo *) NULL);
2420 assert((xml_info->signature == MagickCoreSignature) ||
2421 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2422 if (IsEventLogging() != MagickFalse)
2423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2424 if (xml_info->next != (XMLTreeInfo *) NULL)
2425 xml_info->next->sibling=xml_info->sibling;
2426 if (xml_info->parent != (XMLTreeInfo *) NULL)
2428 node=xml_info->parent->child;
2429 if (node == xml_info)
2430 xml_info->parent->child=xml_info->ordered;
2433 while (node->ordered != xml_info)
2435 node->ordered=node->ordered->ordered;
2436 node=xml_info->parent->child;
2437 if (strcmp(node->tag,xml_info->tag) != 0)
2439 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2441 if (node->sibling != xml_info)
2444 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2445 xml_info->next : node->sibling->sibling;
2447 while ((node->next != (XMLTreeInfo *) NULL) &&
2448 (node->next != xml_info))
2450 if (node->next != (XMLTreeInfo *) NULL)
2451 node->next=node->next->next;
2454 xml_info->ordered=(XMLTreeInfo *) NULL;
2455 xml_info->sibling=(XMLTreeInfo *) NULL;
2456 xml_info->next=(XMLTreeInfo *) NULL;
2488MagickExport XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2489 const char *tag,
const char *value)
2495 assert(xml_info != (XMLTreeInfo *) NULL);
2496 assert((xml_info->signature == MagickCoreSignature) ||
2497 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2498 if (IsEventLogging() != MagickFalse)
2499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2501 while ((xml_info->attributes[i] != (
char *) NULL) &&
2502 (strcmp(xml_info->attributes[i],tag) != 0))
2504 if (xml_info->attributes[i] == (
char *) NULL)
2509 if (value == (
const char *) NULL)
2511 if (xml_info->attributes != sentinel)
2512 xml_info->attributes=(
char **) ResizeQuantumMemory(
2513 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2516 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2517 sizeof(*xml_info->attributes));
2518 if (xml_info->attributes != (
char **) NULL)
2519 xml_info->attributes[1]=ConstantString(
"");
2521 if (xml_info->attributes == (
char **) NULL)
2522 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2523 xml_info->attributes[i]=ConstantString(tag);
2524 xml_info->attributes[i+2]=(
char *) NULL;
2525 (void) strlen(xml_info->attributes[i+1]);
2530 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2531 if (xml_info->attributes[i+1] != (
char *) NULL)
2532 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2533 if (value != (
const char *) NULL)
2535 xml_info->attributes[i+1]=ConstantString(value);
2538 if (xml_info->attributes[i] != (
char *) NULL)
2539 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2540 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,
2541 (
size_t) (j-i)*
sizeof(*xml_info->attributes));
2542 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2543 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2544 if (xml_info->attributes == (
char **) NULL)
2545 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2547 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2548 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2578MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2579 const char *content)
2581 assert(xml_info != (XMLTreeInfo *) NULL);
2582 assert((xml_info->signature == MagickCoreSignature) ||
2583 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2584 if (IsEventLogging() != MagickFalse)
2585 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2586 if (xml_info->content != (
char *) NULL)
2587 xml_info->content=DestroyString(xml_info->content);
2588 xml_info->content=(
char *) ConstantString(content);
2615static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2616 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2622 canonical_content=CanonicalXMLContent(source,pedantic);
2628 content=AcquireString(source);
2629 content[offset]=
'\0';
2630 canonical_content=CanonicalXMLContent(content,pedantic);
2631 content=DestroyString(content);
2633 if (canonical_content == (
char *) NULL)
2634 return(*destination);
2635 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2637 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2638 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2639 sizeof(**destination));
2640 if (*destination == (
char *) NULL)
2641 return(*destination);
2643 *length+=FormatLocaleString(*destination+(*length),*extent,
"%s",
2645 canonical_content=DestroyString(canonical_content);
2646 return(*destination);
2649static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2650 size_t *extent,
size_t start,
char ***attributes)
2665 content=(
char *)
"";
2666 if (xml_info->parent != (XMLTreeInfo *) NULL)
2667 content=xml_info->parent->content;
2669 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2670 start),source,length,extent,MagickFalse);
2671 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2673 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2674 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2675 if (*source == (
char *) NULL)
2678 *length+=FormatLocaleString(*source+(*length),*extent,
"<%s",xml_info->tag);
2679 for (i=0; xml_info->attributes[i]; i+=2)
2681 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2682 if (attribute != xml_info->attributes[i+1])
2684 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2686 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2687 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2688 if (*source == (
char *) NULL)
2689 return((
char *) NULL);
2691 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2692 xml_info->attributes[i]);
2693 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2695 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2698 while ((attributes[i] != (
char **) NULL) &&
2699 (strcmp(attributes[i][0],xml_info->tag) != 0))
2702 while ((attributes[i] != (
char **) NULL) &&
2703 (attributes[i][j] != (
char *) NULL))
2705 if ((attributes[i][j+1] == (
char *) NULL) ||
2706 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2711 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2713 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2714 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2715 if (*source == (
char *) NULL)
2716 return((
char *) NULL);
2718 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2720 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2722 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2725 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2727 if (xml_info->child != (XMLTreeInfo *) NULL)
2728 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2730 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2732 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2734 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2735 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2736 if (*source == (
char *) NULL)
2737 return((
char *) NULL);
2739 if (*xml_info->content !=
'\0')
2740 *length+=FormatLocaleString(*source+(*length),*extent,
"</%s>",
2742 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2744 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2745 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2748 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2753MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2776 assert(xml_info != (XMLTreeInfo *) NULL);
2777 assert((xml_info->signature == MagickCoreSignature) ||
2778 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2779 if (IsEventLogging() != MagickFalse)
2780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2781 if (xml_info->tag == (
char *) NULL)
2782 return((
char *) NULL);
2783 xml=AcquireString((
char *) NULL);
2785 extent=MaxTextExtent;
2786 root=(XMLTreeRoot *) xml_info;
2787 while (root->root.parent != (XMLTreeInfo *) NULL)
2788 root=(XMLTreeRoot *) root->root.parent;
2789 parent=xml_info->parent;
2790 if (parent == (XMLTreeInfo *) NULL)
2791 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2796 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2797 p=root->processing_instructions[i][1];
2798 for (j=1; p != (
char *) NULL; j++)
2800 if (root->processing_instructions[i][k][j-1] ==
'>')
2802 p=root->processing_instructions[i][j];
2805 q=root->processing_instructions[i][0];
2806 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2808 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2809 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2810 if (xml == (
char *) NULL)
2813 length+=FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2814 *p !=
'\0' ?
" " :
"",p);
2815 p=root->processing_instructions[i][j];
2818 ordered=xml_info->ordered;
2819 xml_info->parent=(XMLTreeInfo *) NULL;
2820 xml_info->ordered=(XMLTreeInfo *) NULL;
2821 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2822 xml_info->parent=parent;
2823 xml_info->ordered=ordered;
2824 if (parent == (XMLTreeInfo *) NULL)
2825 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2830 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2831 p=root->processing_instructions[i][1];
2832 for (j=1; p != (
char *) NULL; j++)
2834 if (root->processing_instructions[i][k][j-1] ==
'<')
2836 p=root->processing_instructions[i][j];
2839 q=root->processing_instructions[i][0];
2840 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2842 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2843 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2844 if (xml == (
char *) NULL)
2847 length+=FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2848 *p !=
'\0' ?
" " :
"",p);
2849 p=root->processing_instructions[i][j];
2852 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));