2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % TTTTT RRRR EEEEE EEEEE %
26 % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
27 % dedicated to making software imaging solutions freely available. %
29 % You may not use this file except in compliance with the License. You may %
30 % obtain a copy of the License at %
32 % https://imagemagick.org/script/license.php %
34 % Unless required by applicable law or agreed to in writing, software %
35 % distributed under the License is distributed on an "AS IS" BASIS, %
36 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
37 % See the License for the specific language governing permissions and %
38 % limitations under the License. %
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 % This module implements the standard handy xml-tree methods for storing and
43 % retrieving nodes and attributes from an XML string.
50 #include "MagickCore/studio.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/blob-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/log.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/memory-private.h"
59 #include "MagickCore/semaphore.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/string-private.h"
62 #include "MagickCore/token-private.h"
63 #include "MagickCore/xml-tree.h"
64 #include "MagickCore/xml-tree-private.h"
65 #include "MagickCore/utility.h"
66 #include "MagickCore/utility-private.h"
71 #define NumberPredefinedEntities 10
72 #define XMLWhitespace "\t\r\n "
104 typedef struct _XMLTreeRoot
119 ***processing_instructions,
137 *sentinel[] = { (char *) NULL };
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % A d d C h i l d T o X M L T r e e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % AddChildToXMLTree() adds a child tag at an offset relative to the start of
151 % the parent tag's character content. Return the child tag.
153 % The format of the AddChildToXMLTree method is:
155 % XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
156 % const size_t offset)
158 % A description of each parameter follows:
160 % o xml_info: the xml info.
164 % o offset: the tag offset.
167 MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
168 const char *tag,const size_t offset)
173 if (xml_info == (XMLTreeInfo *) NULL)
174 return((XMLTreeInfo *) NULL);
175 child=(XMLTreeInfo *) AcquireMagickMemory(sizeof(*child));
176 if (child == (XMLTreeInfo *) NULL)
177 return((XMLTreeInfo *) NULL);
178 (void) memset(child,0,sizeof(*child));
179 child->tag=ConstantString(tag);
180 child->attributes=sentinel;
181 child->content=ConstantString("");
182 child->debug=IsEventLogging();
183 child->signature=MagickCoreSignature;
184 return(InsertTagIntoXMLTree(xml_info,child,offset));
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % A d d P a t h T o X M L T r e e %
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 % AddPathToXMLTree() adds a child tag at an offset relative to the start of
199 % the parent tag's character content. This method returns the child tag.
201 % The format of the AddPathToXMLTree method is:
203 % XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
204 % const size_t offset)
206 % A description of each parameter follows:
208 % o xml_info: the xml info.
212 % o offset: the tag offset.
215 MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
216 const char *path,const size_t offset)
220 subnode[MagickPathExtent],
221 tag[MagickPathExtent];
236 assert(xml_info != (XMLTreeInfo *) NULL);
237 assert((xml_info->signature == MagickCoreSignature) ||
238 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
239 if (xml_info->debug != MagickFalse)
240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
242 components=GetPathComponents(path,&number_components);
243 if (components == (char **) NULL)
244 return((XMLTreeInfo *) NULL);
245 for (i=0; i < (ssize_t) number_components; i++)
247 GetPathComponent(components[i],SubimagePath,subnode);
248 GetPathComponent(components[i],CanonicalPath,tag);
249 child=GetXMLTreeChild(node,tag);
250 if (child == (XMLTreeInfo *) NULL)
251 child=AddChildToXMLTree(node,tag,offset);
253 if (node == (XMLTreeInfo *) NULL)
255 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
257 node=GetXMLTreeOrdered(node);
258 if (node == (XMLTreeInfo *) NULL)
261 if (node == (XMLTreeInfo *) NULL)
263 components[i]=DestroyString(components[i]);
265 for ( ; i < (ssize_t) number_components; i++)
266 components[i]=DestroyString(components[i]);
267 components=(char **) RelinquishMagickMemory(components);
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % C a n o n i c a l X M L C o n t e n t %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % CanonicalXMLContent() converts text to canonical XML content by converting
283 % to UTF-8, substituting predefined entities, wrapping as CDATA, or encoding
284 % as base-64 as required.
286 % The format of the CanonicalXMLContent method is:
289 % char *CanonicalXMLContent(const char *content,
290 % const MagickBooleanType pedantic)
292 % A description of each parameter follows:
294 % o content: the content.
296 % o pedantic: if true, replace newlines and tabs with their respective
300 MagickPrivate char *CanonicalXMLContent(const char *content,
301 const MagickBooleanType pedantic)
307 register const unsigned char
320 utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
321 if (utf8 == (unsigned char *) NULL)
322 return((char *) NULL);
323 for (p=utf8; *p != '\0'; p++)
324 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
329 String is binary, base64-encode it.
331 base64=Base64Encode(utf8,strlen((char *) utf8),&length);
332 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
333 if (base64 == (char *) NULL)
334 return((char *) NULL);
335 canonical_content=AcquireString("<base64>");
336 (void) ConcatenateString(&canonical_content,base64);
337 base64=DestroyString(base64);
338 (void) ConcatenateString(&canonical_content,"</base64>");
339 return(canonical_content);
342 Substitute predefined entities.
345 canonical_content=AcquireString((char *) NULL);
346 extent=MagickPathExtent;
347 for (p=utf8; *p != '\0'; p++)
349 if ((i+MagickPathExtent) > (ssize_t) extent)
351 extent+=MagickPathExtent;
352 canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
353 sizeof(*canonical_content));
354 if (canonical_content == (char *) NULL)
355 return(canonical_content);
361 i+=FormatLocaleString(canonical_content+i,extent,"&");
366 i+=FormatLocaleString(canonical_content+i,extent,"<");
371 i+=FormatLocaleString(canonical_content+i,extent,">");
376 i+=FormatLocaleString(canonical_content+i,extent,""");
381 if (pedantic == MagickFalse)
383 canonical_content[i++]=(char) (*p);
386 i+=FormatLocaleString(canonical_content+i,extent,"
");
391 if (pedantic == MagickFalse)
393 canonical_content[i++]=(char) (*p);
396 i+=FormatLocaleString(canonical_content+i,extent,"	");
401 i+=FormatLocaleString(canonical_content+i,extent,"
");
406 canonical_content[i++]=(char) (*p);
411 canonical_content[i]='\0';
412 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
413 return(canonical_content);
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % D e s t r o y X M L T r e e %
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % DestroyXMLTree() destroys the xml-tree.
429 % The format of the DestroyXMLTree method is:
431 % XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
433 % A description of each parameter follows:
435 % o xml_info: the xml info.
439 static char **DestroyXMLTreeAttributes(char **attributes)
445 Destroy a tag attribute list.
447 if ((attributes == (char **) NULL) || (attributes == sentinel))
448 return((char **) NULL);
449 for (i=0; attributes[i] != (char *) NULL; i+=2)
452 Destroy attribute tag and value.
454 if (attributes[i] != (char *) NULL)
455 attributes[i]=DestroyString(attributes[i]);
456 if (attributes[i+1] != (char *) NULL)
457 attributes[i+1]=DestroyString(attributes[i+1]);
459 attributes=(char **) RelinquishMagickMemory(attributes);
460 return((char **) NULL);
463 static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
469 child=xml_info->child;
470 while(child != (XMLTreeInfo *) NULL)
474 node->child=(XMLTreeInfo *) NULL;
475 (void) DestroyXMLTree(node);
479 static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
485 ordered=xml_info->ordered;
486 while(ordered != (XMLTreeInfo *) NULL)
489 ordered=node->ordered;
490 node->ordered=(XMLTreeInfo *) NULL;
491 (void) DestroyXMLTree(node);
495 static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
509 assert(xml_info != (XMLTreeInfo *) NULL);
510 assert((xml_info->signature == MagickCoreSignature) ||
511 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
512 if (xml_info->debug != MagickFalse)
513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
514 if (xml_info->parent != (XMLTreeInfo *) NULL)
517 Free root tag allocations.
519 root=(XMLTreeRoot *) xml_info;
520 for (i=NumberPredefinedEntities; root->entities[i] != (char *) NULL; i+=2)
521 root->entities[i+1]=DestroyString(root->entities[i+1]);
522 root->entities=(char **) RelinquishMagickMemory(root->entities);
523 for (i=0; root->attributes[i] != (char **) NULL; i++)
525 attributes=root->attributes[i];
526 if (attributes[0] != (char *) NULL)
527 attributes[0]=DestroyString(attributes[0]);
528 for (j=1; attributes[j] != (char *) NULL; j+=3)
530 if (attributes[j] != (char *) NULL)
531 attributes[j]=DestroyString(attributes[j]);
532 if (attributes[j+1] != (char *) NULL)
533 attributes[j+1]=DestroyString(attributes[j+1]);
534 if (attributes[j+2] != (char *) NULL)
535 attributes[j+2]=DestroyString(attributes[j+2]);
537 attributes=(char **) RelinquishMagickMemory(attributes);
539 if (root->attributes[0] != (char **) NULL)
540 root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
541 if (root->processing_instructions[0] != (char **) NULL)
543 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
545 for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
546 root->processing_instructions[i][j]=DestroyString(
547 root->processing_instructions[i][j]);
548 root->processing_instructions[i][j+1]=DestroyString(
549 root->processing_instructions[i][j+1]);
550 root->processing_instructions[i]=(char **) RelinquishMagickMemory(
551 root->processing_instructions[i]);
553 root->processing_instructions=(char ***) RelinquishMagickMemory(
554 root->processing_instructions);
558 MagickExport 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 (xml_info->debug != MagickFalse)
564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
565 DestroyXMLTreeChild(xml_info);
566 DestroyXMLTreeOrdered(xml_info);
567 DestroyXMLTreeRoot(xml_info);
568 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
569 xml_info->content=DestroyString(xml_info->content);
570 xml_info->tag=DestroyString(xml_info->tag);
571 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
572 return((XMLTreeInfo *) NULL);
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 % F i l e T o X M L %
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
586 % FileToXML() returns the contents of a file as a XML string.
588 % The format of the FileToXML method is:
590 % char *FileToXML(const char *filename,const size_t extent)
592 % A description of each parameter follows:
594 % o filename: the filename.
596 % o extent: Maximum length of the string.
599 MagickPrivate char *FileToXML(const char *filename,const size_t extent)
622 assert(filename != (const char *) NULL);
625 if (LocaleCompare(filename,"-") != 0)
626 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
628 return((char *) NULL);
629 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
631 if ((file == fileno(stdin)) || (offset < 0) ||
632 (offset != (MagickOffsetType) ((ssize_t) offset)))
641 Stream is not seekable.
643 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
644 quantum=(size_t) MagickMaxBufferExtent;
645 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
646 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
647 xml=(char *) AcquireQuantumMemory(quantum,sizeof(*xml));
648 for (i=0; xml != (char *) NULL; i+=count)
650 count=read(file,xml+i,quantum);
657 if (~((size_t) i) < (quantum+1))
659 xml=(char *) RelinquishMagickMemory(xml);
662 xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
663 if ((size_t) (i+count) >= extent)
666 if (LocaleCompare(filename,"-") != 0)
668 if (xml == (char *) NULL)
669 return((char *) NULL);
672 xml=(char *) RelinquishMagickMemory(xml);
673 return((char *) NULL);
675 length=(size_t) MagickMin(i+count,extent);
679 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
681 if (~length >= (MagickPathExtent-1))
682 xml=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*xml));
683 if (xml == (char *) NULL)
686 return((char *) NULL);
688 map=MapBlob(file,ReadMode,0,length);
689 if (map != (char *) NULL)
691 (void) memcpy(xml,map,length);
692 (void) UnmapBlob(map,length);
696 (void) lseek(file,0,SEEK_SET);
697 for (i=0; i < length; i+=count)
699 count=read(file,xml+i,(size_t) MagickMin(length-i,(ssize_t) SSIZE_MAX));
710 xml=(char *) RelinquishMagickMemory(xml);
711 return((char *) NULL);
715 if (LocaleCompare(filename,"-") != 0)
718 xml=(char *) RelinquishMagickMemory(xml);
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % G e t N e x t X M L T r e e T a g %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % GetNextXMLTreeTag() returns the next tag or NULL if not found.
735 % The format of the GetNextXMLTreeTag method is:
737 % XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
739 % A description of each parameter follows:
741 % o xml_info: the xml info.
744 MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
746 assert(xml_info != (XMLTreeInfo *) NULL);
747 assert((xml_info->signature == MagickCoreSignature) ||
748 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
749 if (xml_info->debug != MagickFalse)
750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
751 return(xml_info->next);
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % G e t X M L T r e e A t t r i b u t e %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % GetXMLTreeAttribute() returns the value of the attribute tag with the
766 % specified tag if found, otherwise NULL.
768 % The format of the GetXMLTreeAttribute method is:
770 % const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
772 % A description of each parameter follows:
774 % o xml_info: the xml info.
776 % o tag: the attribute tag.
779 MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
791 assert(xml_info != (XMLTreeInfo *) NULL);
792 assert((xml_info->signature == MagickCoreSignature) ||
793 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
794 if (xml_info->debug != MagickFalse)
795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
796 if (xml_info->attributes == (char **) NULL)
797 return((const char *) NULL);
799 while ((xml_info->attributes[i] != (char *) NULL) &&
800 (strcmp(xml_info->attributes[i],tag) != 0))
802 if (xml_info->attributes[i] != (char *) NULL)
803 return(xml_info->attributes[i+1]);
804 root=(XMLTreeRoot*) xml_info;
805 while (root->root.parent != (XMLTreeInfo *) NULL)
806 root=(XMLTreeRoot *) root->root.parent;
808 while ((root->attributes[i] != (char **) NULL) &&
809 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
811 if (root->attributes[i] == (char **) NULL)
812 return((const char *) NULL);
814 while ((root->attributes[i][j] != (char *) NULL) &&
815 (strcmp(root->attributes[i][j],tag) != 0))
817 if (root->attributes[i][j] == (char *) NULL)
818 return((const char *) NULL);
819 return(root->attributes[i][j+1]);
823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % G e t X M L T r e e A t t r i b u t e s %
831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % GetXMLTreeAttributes() injects all attributes associated with the current
834 % tag in the specified splay-tree.
836 % The format of the GetXMLTreeAttributes method is:
838 % MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
839 % SplayTreeInfo *attributes)
841 % A description of each parameter follows:
843 % o xml_info: the xml info.
845 % o attributes: the attribute splay-tree.
848 MagickPrivate MagickBooleanType GetXMLTreeAttributes(
849 const XMLTreeInfo *xml_info,SplayTreeInfo *attributes)
854 assert(xml_info != (XMLTreeInfo *) NULL);
855 assert((xml_info->signature == MagickCoreSignature) ||
856 (((const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
857 if (xml_info->debug != MagickFalse)
858 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
859 assert(attributes != (SplayTreeInfo *) NULL);
860 if (xml_info->attributes == (char **) NULL)
863 while (xml_info->attributes[i] != (char *) NULL)
865 (void) AddValueToSplayTree(attributes,
866 ConstantString(xml_info->attributes[i]),
867 ConstantString(xml_info->attributes[i+1]));
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % G e t X M L T r e e C h i l d %
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % GetXMLTreeChild() returns the first child tag with the specified tag if
885 % found, otherwise NULL.
887 % The format of the GetXMLTreeChild method is:
889 % XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
891 % A description of each parameter follows:
893 % o xml_info: the xml info.
896 MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
901 assert(xml_info != (XMLTreeInfo *) NULL);
902 assert((xml_info->signature == MagickCoreSignature) ||
903 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
904 if (xml_info->debug != MagickFalse)
905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
906 child=xml_info->child;
907 if (tag != (const char *) NULL)
908 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
909 child=child->sibling;
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % G e t X M L T r e e C o n t e n t %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % GetXMLTreeContent() returns any content associated with specified
927 % The format of the GetXMLTreeContent method is:
929 % const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
931 % A description of each parameter follows:
933 % o xml_info: the xml info.
936 MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
938 assert(xml_info != (XMLTreeInfo *) NULL);
939 assert((xml_info->signature == MagickCoreSignature) ||
940 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
941 if (xml_info->debug != MagickFalse)
942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
943 return(xml_info->content);
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951 % G e t X M L T r e e O r d e r e d %
955 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 % GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
959 % The format of the GetXMLTreeOrdered method is:
961 % XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
963 % A description of each parameter follows:
965 % o xml_info: the xml info.
968 MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
970 assert(xml_info != (XMLTreeInfo *) NULL);
971 assert((xml_info->signature == MagickCoreSignature) ||
972 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
973 if (xml_info->debug != MagickFalse)
974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
975 return(xml_info->ordered);
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 % G e t X M L T r e e P a t h %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % GetXMLTreePath() traverses the XML-tree as defined by the specified path
990 % and returns the node if found, otherwise NULL.
992 % The format of the GetXMLTreePath method is:
994 % XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
996 % A description of each parameter follows:
998 % o xml_info: the xml info.
1000 % o path: the path (e.g. property/elapsed-time).
1003 MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
1008 subnode[MagickPathExtent],
1009 tag[MagickPathExtent];
1023 assert(xml_info != (XMLTreeInfo *) NULL);
1024 assert((xml_info->signature == MagickCoreSignature) ||
1025 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1026 if (xml_info->debug != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1029 components=GetPathComponents(path,&number_components);
1030 if (components == (char **) NULL)
1031 return((XMLTreeInfo *) NULL);
1032 for (i=0; i < (ssize_t) number_components; i++)
1034 GetPathComponent(components[i],SubimagePath,subnode);
1035 GetPathComponent(components[i],CanonicalPath,tag);
1036 node=GetXMLTreeChild(node,tag);
1037 if (node == (XMLTreeInfo *) NULL)
1039 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1041 node=GetXMLTreeOrdered(node);
1042 if (node == (XMLTreeInfo *) NULL)
1045 if (node == (XMLTreeInfo *) NULL)
1047 components[i]=DestroyString(components[i]);
1049 for ( ; i < (ssize_t) number_components; i++)
1050 components[i]=DestroyString(components[i]);
1051 components=(char **) RelinquishMagickMemory(components);
1056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 % G e t X M L T r e e P r o c e s s i n g I n s t r u c t i o n s %
1064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % GetXMLTreeProcessingInstructions() returns a null terminated array of
1067 % processing instructions for the given target.
1069 % The format of the GetXMLTreeProcessingInstructions method is:
1071 % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
1072 % const char *target)
1074 % A description of each parameter follows:
1076 % o xml_info: the xml info.
1079 MagickPrivate const char **GetXMLTreeProcessingInstructions(
1080 XMLTreeInfo *xml_info,const char *target)
1088 assert(xml_info != (XMLTreeInfo *) NULL);
1089 assert((xml_info->signature == MagickCoreSignature) ||
1090 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1091 if (xml_info->debug != MagickFalse)
1092 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1093 root=(XMLTreeRoot *) xml_info;
1094 while (root->root.parent != (XMLTreeInfo *) NULL)
1095 root=(XMLTreeRoot *) root->root.parent;
1097 while ((root->processing_instructions[i] != (char **) NULL) &&
1098 (strcmp(root->processing_instructions[i][0],target) != 0))
1100 if (root->processing_instructions[i] == (char **) NULL)
1101 return((const char **) sentinel);
1102 return((const char **) (root->processing_instructions[i]+1));
1106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1110 % G e t X M L T r e e S i b l i n g %
1114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1116 % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
1118 % The format of the GetXMLTreeSibling method is:
1120 % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1122 % A description of each parameter follows:
1124 % o xml_info: the xml info.
1127 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1129 assert(xml_info != (XMLTreeInfo *) NULL);
1130 assert((xml_info->signature == MagickCoreSignature) ||
1131 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1132 if (xml_info->debug != MagickFalse)
1133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1134 return(xml_info->sibling);
1138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1142 % G e t X M L T r e e T a g %
1146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148 % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
1150 % The format of the GetXMLTreeTag method is:
1152 % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1154 % A description of each parameter follows:
1156 % o xml_info: the xml info.
1159 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1161 assert(xml_info != (XMLTreeInfo *) NULL);
1162 assert((xml_info->signature == MagickCoreSignature) ||
1163 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1164 if (xml_info->debug != MagickFalse)
1165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1166 return(xml_info->tag);
1170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1174 % I n s e r t I n t o T a g X M L T r e e %
1178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
1181 % the parent tag's character content. This method returns the child tag.
1183 % The format of the InsertTagIntoXMLTree method is:
1185 % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1186 % XMLTreeInfo *child,const size_t offset)
1188 % A description of each parameter follows:
1190 % o xml_info: the xml info.
1192 % o child: the child tag.
1194 % o offset: the tag offset.
1197 MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1198 XMLTreeInfo *child,const size_t offset)
1205 child->ordered=(XMLTreeInfo *) NULL;
1206 child->sibling=(XMLTreeInfo *) NULL;
1207 child->next=(XMLTreeInfo *) NULL;
1208 child->offset=offset;
1209 child->parent=xml_info;
1210 if (xml_info->child == (XMLTreeInfo *) NULL)
1212 xml_info->child=child;
1215 head=xml_info->child;
1216 if (head->offset > offset)
1218 child->ordered=head;
1219 xml_info->child=child;
1224 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1225 (node->ordered->offset <= offset))
1227 child->ordered=node->ordered;
1228 node->ordered=child;
1230 previous=(XMLTreeInfo *) NULL;
1232 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1237 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1239 while ((node->next != (XMLTreeInfo *) NULL) &&
1240 (node->next->offset <= offset))
1242 child->next=node->next;
1247 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1248 previous->sibling=node->sibling;
1250 previous=(XMLTreeInfo *) NULL;
1252 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1257 child->sibling=node;
1258 if (previous != (XMLTreeInfo *) NULL)
1259 previous->sibling=child;
1265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269 % N e w X M L T r e e %
1273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1278 % The format of the NewXMLTree method is:
1280 % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1282 % A description of each parameter follows:
1284 % o xml: A null-terminated XML string.
1286 % o exception: return any errors or warnings in this structure.
1290 static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
1310 utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
1311 if (utf8 == (char *) NULL)
1312 return((char *) NULL);
1313 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
1319 (void) memcpy(utf8,content,*length*sizeof(*utf8));
1325 for (i=2; i < (ssize_t) (*length-1); i+=2)
1327 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1328 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1329 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1331 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1332 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1333 (content[i] & 0xff);
1334 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1336 if ((size_t) (j+MagickPathExtent) > extent)
1338 extent=(size_t) j+MagickPathExtent;
1339 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1340 if (utf8 == (char *) NULL)
1350 Multi-byte UTF-8 sequence.
1353 for (bits=0; byte != 0; byte/=2)
1356 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1360 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1365 utf8=(char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8));
1366 if (utf8 != (char *) NULL)
1371 static char *ParseEntities(char *xml,char **entities,int state)
1395 Normalize line endings.
1399 for ( ; *xml != '\0'; xml++)
1400 while (*xml == '\r')
1404 (void) memmove(xml,xml+1,strlen(xml));
1408 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1409 (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1415 '&' for general entity decoding
1416 '%' for parameter entity decoding
1417 'c' for CDATA sections
1418 ' ' for attributes normalization
1419 '*' for non-CDATA attributes normalization
1421 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1424 Character reference.
1427 c=strtol(xml+2,&entity,10); /* base 10 */
1429 c=strtol(xml+3,&entity,16); /* base 16 */
1430 if ((c == 0) || (*entity != ';'))
1433 Not a character reference.
1443 Multi-byte UTF-8 sequence.
1446 for (i=0; byte != 0; byte/=2)
1449 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1454 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1458 (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1461 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1462 (state == '*'))) || ((state == '%') && (*xml == '%')))
1465 Find entity in the list.
1468 while ((entities[i] != (char *) NULL) &&
1469 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1471 if (entities[i++] == (char *) NULL)
1474 if (entities[i] != (char *) NULL)
1479 length=strlen(entities[i]);
1480 entity=strchr(xml,';');
1481 if ((entity != (char *) NULL) &&
1482 ((length-1L) >= (size_t) (entity-xml)))
1484 offset=(ssize_t) (xml-p);
1485 extent=(size_t) (offset+length+strlen(entity));
1488 p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1496 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1497 sizeof(*extent_xml));
1498 if (extent_xml != (char *) NULL)
1500 memset(extent_xml,0,extent*sizeof(*extent_xml));
1501 (void) CopyMagickString(extent_xml,p,extent*
1502 sizeof(*extent_xml));
1506 if (p == (char *) NULL)
1507 ThrowFatalException(ResourceLimitFatalError,
1508 "MemoryAllocationFailed");
1510 entity=strchr(xml,';');
1512 if (entity != (char *) NULL)
1513 (void) memmove(xml+length,entity+1,strlen(entity));
1514 (void) strncpy(xml,entities[i],length);
1518 if (((state == ' ') || (state == '*')) &&
1519 (isspace((int) ((unsigned char) *xml) != 0)))
1527 Normalize spaces for non-CDATA attributes.
1529 for (xml=p; *xml != '\0'; xml++)
1534 i=(ssize_t) strspn(xml,accept);
1536 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1537 while ((*xml != '\0') && (*xml != ' '))
1543 if ((xml >= p) && (*xml == ' '))
1546 return(p == q ? ConstantString(p) : p);
1549 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1550 const size_t length,const char state)
1555 xml_info=root->node;
1556 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1560 xml=ParseEntities(xml,root->entities,state);
1561 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1563 (void) ConcatenateString(&xml_info->content,xml);
1564 xml=DestroyString(xml);
1568 if (xml_info->content != (char *) NULL)
1569 xml_info->content=DestroyString(xml_info->content);
1570 xml_info->content=xml;
1574 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1575 ExceptionInfo *exception)
1577 if ((root->node == (XMLTreeInfo *) NULL) ||
1578 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1580 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1581 "ParseError","unexpected closing tag </%s>",tag);
1582 return(&root->root);
1584 root->node=root->node->parent;
1585 return((XMLTreeInfo *) NULL);
1588 static MagickBooleanType ValidateEntities(char *tag,char *xml,
1589 const size_t depth,char **entities)
1595 Check for circular entity references.
1597 if (depth > MagickMaxRecursionDepth)
1598 return(MagickFalse);
1601 while ((*xml != '\0') && (*xml != '&'))
1605 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1606 return(MagickFalse);
1608 while ((entities[i] != (char *) NULL) &&
1609 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1611 if ((entities[i] != (char *) NULL) &&
1612 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1613 return(MagickFalse);
1617 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1631 xml+=strcspn(xml,XMLWhitespace);
1635 xml+=strspn(xml+1,XMLWhitespace)+1;
1637 if (strcmp(target,"xml") == 0)
1639 xml=strstr(xml,"standalone");
1640 if ((xml != (char *) NULL) &&
1641 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1642 root->standalone=MagickTrue;
1645 if (root->processing_instructions[0] == (char **) NULL)
1647 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1648 *root->processing_instructions));
1649 *root->processing_instructions=(char **) NULL;
1652 while ((root->processing_instructions[i] != (char **) NULL) &&
1653 (strcmp(target,root->processing_instructions[i][0]) != 0))
1655 if (root->processing_instructions[i] == (char **) NULL)
1657 root->processing_instructions=(char ***) ResizeQuantumMemory(
1658 root->processing_instructions,(size_t) (i+2),
1659 sizeof(*root->processing_instructions));
1660 if (root->processing_instructions == (char ***) NULL)
1661 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1662 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1663 sizeof(**root->processing_instructions));
1664 if (root->processing_instructions[i] == (char **) NULL)
1665 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1666 root->processing_instructions[i+1]=(char **) NULL;
1667 root->processing_instructions[i][0]=ConstantString(target);
1668 root->processing_instructions[i][1]=(char *)
1669 root->processing_instructions[i+1];
1670 root->processing_instructions[i+1]=(char **) NULL;
1671 root->processing_instructions[i][2]=ConstantString("");
1674 while (root->processing_instructions[i][j] != (char *) NULL)
1676 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1677 root->processing_instructions[i],(size_t) (j+3),
1678 sizeof(**root->processing_instructions));
1679 if (root->processing_instructions[i] == (char **) NULL)
1680 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1681 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1682 root->processing_instructions[i][j+1],(size_t) (j+1),
1683 sizeof(***root->processing_instructions));
1684 if (root->processing_instructions[i][j+2] == (char *) NULL)
1685 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1686 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1687 root->root.tag != (char *) NULL ? ">" : "<",2);
1688 root->processing_instructions[i][j]=ConstantString(xml);
1689 root->processing_instructions[i][j+1]=(char *) NULL;
1692 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1693 size_t length,ExceptionInfo *exception)
1699 **predefined_entitites,
1711 predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1712 if (predefined_entitites == (char **) NULL)
1713 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1714 (void) memcpy(predefined_entitites,sentinel,sizeof(sentinel));
1715 for (xml[length]='\0'; xml != (char *) NULL; )
1717 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1721 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1724 Parse entity definitions.
1726 if (strspn(xml+8,XMLWhitespace) == 0)
1728 xml+=strspn(xml+8,XMLWhitespace)+8;
1730 n=xml+strspn(xml,XMLWhitespace "%");
1731 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1733 xml=n+strcspn(n,XMLWhitespace);
1737 v=xml+strspn(xml+1,XMLWhitespace)+1;
1740 if ((q != '"') && (q != '\''))
1745 xml=strchr(xml,'>');
1748 entities=(*c == '%') ? predefined_entitites : root->entities;
1749 for (i=0; entities[i] != (char *) NULL; i++) ;
1750 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1752 if (entities == (char **) NULL)
1753 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1755 predefined_entitites=entities;
1757 root->entities=entities;
1761 if (xml != (char *) NULL)
1766 entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1767 entities[i+2]=(char *) NULL;
1768 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1772 if (entities[i+1] != v)
1773 entities[i+1]=DestroyString(entities[i+1]);
1774 (void) ThrowMagickException(exception,GetMagickModule(),
1775 OptionWarning,"ParseError","circular entity declaration &%s",n);
1776 predefined_entitites=(char **) RelinquishMagickMemory(
1777 predefined_entitites);
1778 return(MagickFalse);
1782 if (strncmp(xml,"<!ATTLIST",9) == 0)
1785 Parse default attributes.
1787 t=xml+strspn(xml+9,XMLWhitespace)+9;
1790 (void) ThrowMagickException(exception,GetMagickModule(),
1791 OptionWarning,"ParseError","unclosed <!ATTLIST");
1792 predefined_entitites=(char **) RelinquishMagickMemory(
1793 predefined_entitites);
1794 return(MagickFalse);
1796 xml=t+strcspn(t,XMLWhitespace ">");
1801 while ((root->attributes[i] != (char **) NULL) &&
1802 (n != (char *) NULL) &&
1803 (strcmp(n,root->attributes[i][0]) != 0))
1805 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1808 xml=n+strcspn(n,XMLWhitespace);
1813 (void) ThrowMagickException(exception,GetMagickModule(),
1814 OptionWarning,"ParseError","malformed <!ATTLIST");
1815 predefined_entitites=(char **) RelinquishMagickMemory(
1816 predefined_entitites);
1817 return(MagickFalse);
1819 xml+=strspn(xml+1,XMLWhitespace)+1;
1820 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1821 if (strncmp(xml,"NOTATION",8) == 0)
1822 xml+=strspn(xml+8,XMLWhitespace)+8;
1823 xml=(*xml == '(') ? strchr(xml,')') : xml+
1824 strcspn(xml,XMLWhitespace);
1825 if (xml == (char *) NULL)
1827 (void) ThrowMagickException(exception,GetMagickModule(),
1828 OptionWarning,"ParseError","malformed <!ATTLIST");
1829 predefined_entitites=(char **) RelinquishMagickMemory(
1830 predefined_entitites);
1831 return(MagickFalse);
1833 xml+=strspn(xml,XMLWhitespace ")");
1834 if (strncmp(xml,"#FIXED",6) == 0)
1835 xml+=strspn(xml+6,XMLWhitespace)+6;
1838 xml+=strcspn(xml,XMLWhitespace ">")-1;
1844 if (((*xml == '"') || (*xml == '\'')) &&
1845 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1849 (void) ThrowMagickException(exception,GetMagickModule(),
1850 OptionWarning,"ParseError","malformed <!ATTLIST");
1851 predefined_entitites=(char **) RelinquishMagickMemory(
1852 predefined_entitites);
1853 return(MagickFalse);
1855 if (root->attributes[i] == (char **) NULL)
1861 root->attributes=(char ***) AcquireQuantumMemory(2,
1862 sizeof(*root->attributes));
1864 root->attributes=(char ***) ResizeQuantumMemory(
1865 root->attributes,(size_t) (i+2),
1866 sizeof(*root->attributes));
1867 if (root->attributes == (char ***) NULL)
1868 ThrowFatalException(ResourceLimitFatalError,
1869 "MemoryAllocationFailed");
1870 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1871 sizeof(**root->attributes));
1872 if (root->attributes[i] == (char **) NULL)
1873 ThrowFatalException(ResourceLimitFatalError,
1874 "MemoryAllocationFailed");
1875 root->attributes[i][0]=ConstantString(t);
1876 root->attributes[i][1]=(char *) NULL;
1877 root->attributes[i+1]=(char **) NULL;
1879 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1880 root->attributes[i]=(char **) ResizeQuantumMemory(
1881 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1882 if (root->attributes[i] == (char **) NULL)
1883 ThrowFatalException(ResourceLimitFatalError,
1884 "MemoryAllocationFailed");
1885 root->attributes[i][j+3]=(char *) NULL;
1886 root->attributes[i][j+2]=ConstantString(c);
1887 root->attributes[i][j+1]=(char *) NULL;
1888 if (v != (char *) NULL)
1889 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1890 root->attributes[i][j]=ConstantString(n);
1894 if (strncmp(xml, "<!--", 4) == 0)
1895 xml=strstr(xml+4,"-->");
1897 if (strncmp(xml,"<?", 2) == 0)
1901 if (xml != (char *) NULL)
1903 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1909 xml=strchr(xml,'>');
1911 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1914 predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1918 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1923 xml_info=root->node;
1924 if (xml_info->tag == (char *) NULL)
1925 xml_info->tag=ConstantString(tag);
1927 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1928 if (xml_info != (XMLTreeInfo *) NULL)
1929 xml_info->attributes=attributes;
1930 root->node=xml_info;
1941 static inline MagickBooleanType IsSkipTag(const char *tag)
1947 while (ignore_tags[i] != (const char *) NULL)
1949 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1953 return(MagickFalse);
1956 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1989 Convert xml-string to UTF8.
1991 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1993 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1994 "ParseError","root tag missing");
1995 return((XMLTreeInfo *) NULL);
1997 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1999 utf8=ConvertUTF16ToUTF8(xml,&length);
2000 if (utf8 == (char *) NULL)
2002 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2003 "ParseError","UTF16 to UTF8 failed");
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+=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 != '>'))
2048 Find tag in default attributes list.
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,(size_t)
2066 (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+=strcspn(p,XMLWhitespace "=/>");
2078 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
2079 attributes[l]=ConstantString("");
2083 p+=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+=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+=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+=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);
2280 Processing instructions.
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 != '<'))
2313 Tag character content.
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);
2341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2345 % N e w X M L T r e e T a g %
2349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2351 % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2353 % The format of the NewXMLTreeTag method is:
2355 % XMLTreeInfo *NewXMLTreeTag(const char *tag)
2357 % A description of each parameter follows:
2362 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2365 *predefined_entities[NumberPredefinedEntities+1] =
2367 "lt;", "<", "gt;", ">", "quot;", """,
2368 "apos;", "'", "amp;", "&", (char *) NULL
2374 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2375 if (root == (XMLTreeRoot *) NULL)
2376 return((XMLTreeInfo *) NULL);
2377 (void) memset(root,0,sizeof(*root));
2378 root->root.tag=(char *) NULL;
2379 if (tag != (char *) NULL)
2380 root->root.tag=ConstantString(tag);
2381 root->node=(&root->root);
2382 root->root.content=ConstantString("");
2383 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2384 if (root->entities == (char **) NULL)
2385 return((XMLTreeInfo *) NULL);
2386 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2387 root->root.attributes=sentinel;
2388 root->attributes=(char ***) root->root.attributes;
2389 root->processing_instructions=(char ***) root->root.attributes;
2390 root->debug=IsEventLogging();
2391 root->signature=MagickCoreSignature;
2392 return(&root->root);
2396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2400 % P r u n e T a g F r o m X M L T r e e %
2404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2406 % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2409 % The format of the PruneTagFromXMLTree method is:
2411 % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2413 % A description of each parameter follows:
2415 % o xml_info: the xml info.
2418 MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2423 assert(xml_info != (XMLTreeInfo *) NULL);
2424 assert((xml_info->signature == MagickCoreSignature) ||
2425 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2426 if (xml_info->debug != MagickFalse)
2427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2428 if (xml_info->next != (XMLTreeInfo *) NULL)
2429 xml_info->next->sibling=xml_info->sibling;
2430 if (xml_info->parent != (XMLTreeInfo *) NULL)
2432 node=xml_info->parent->child;
2433 if (node == xml_info)
2434 xml_info->parent->child=xml_info->ordered;
2437 while (node->ordered != xml_info)
2439 node->ordered=node->ordered->ordered;
2440 node=xml_info->parent->child;
2441 if (strcmp(node->tag,xml_info->tag) != 0)
2443 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2445 if (node->sibling != xml_info)
2448 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2449 xml_info->next : node->sibling->sibling;
2451 while ((node->next != (XMLTreeInfo *) NULL) &&
2452 (node->next != xml_info))
2454 if (node->next != (XMLTreeInfo *) NULL)
2455 node->next=node->next->next;
2458 xml_info->ordered=(XMLTreeInfo *) NULL;
2459 xml_info->sibling=(XMLTreeInfo *) NULL;
2460 xml_info->next=(XMLTreeInfo *) NULL;
2465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2469 % S e t X M L T r e e A t t r i b u t e %
2473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2475 % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2476 % found. A value of NULL removes the specified attribute.
2478 % The format of the SetXMLTreeAttribute method is:
2480 % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2481 % const char *value)
2483 % A description of each parameter follows:
2485 % o xml_info: the xml info.
2487 % o tag: The attribute tag.
2489 % o value: The attribute value.
2492 MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2493 const char *tag,const char *value)
2501 assert(xml_info != (XMLTreeInfo *) NULL);
2502 assert((xml_info->signature == MagickCoreSignature) ||
2503 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2504 if (xml_info->debug != MagickFalse)
2505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2507 while ((xml_info->attributes[i] != (char *) NULL) &&
2508 (strcmp(xml_info->attributes[i],tag) != 0))
2510 if (xml_info->attributes[i] == (char *) NULL)
2513 Add new attribute tag.
2515 if (value == (const char *) NULL)
2517 if (xml_info->attributes != sentinel)
2518 xml_info->attributes=(char **) ResizeQuantumMemory(
2519 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2522 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2523 sizeof(*xml_info->attributes));
2524 if (xml_info->attributes != (char **) NULL)
2525 xml_info->attributes[1]=ConstantString("");
2527 if (xml_info->attributes == (char **) NULL)
2528 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2529 xml_info->attributes[i]=ConstantString(tag);
2530 xml_info->attributes[i+2]=(char *) NULL;
2531 (void) strlen(xml_info->attributes[i+1]);
2534 Add new value to an existing attribute.
2536 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2537 if (xml_info->attributes[i+1] != (char *) NULL)
2538 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2539 if (value != (const char *) NULL)
2541 xml_info->attributes[i+1]=ConstantString(value);
2544 if (xml_info->attributes[i] != (char *) NULL)
2545 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2546 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(size_t)
2547 (j-i)*sizeof(*xml_info->attributes));
2548 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2549 (size_t) (j+2),sizeof(*xml_info->attributes));
2550 if (xml_info->attributes == (char **) NULL)
2551 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2553 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2554 (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
2559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2563 % S e t X M L T r e e C o n t e n t %
2567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2569 % SetXMLTreeContent() sets the character content for the given tag and
2572 % The format of the SetXMLTreeContent method is:
2574 % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2575 % const char *content)
2577 % A description of each parameter follows:
2579 % o xml_info: the xml info.
2581 % o content: The content.
2584 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2585 const char *content)
2587 assert(xml_info != (XMLTreeInfo *) NULL);
2588 assert((xml_info->signature == MagickCoreSignature) ||
2589 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2590 if (xml_info->debug != MagickFalse)
2591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2592 if (xml_info->content != (char *) NULL)
2593 xml_info->content=DestroyString(xml_info->content);
2594 xml_info->content=(char *) ConstantString(content);
2599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2603 % X M L T r e e I n f o T o X M L %
2607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2609 % XMLTreeInfoToXML() converts an xml-tree to an XML string.
2611 % The format of the XMLTreeInfoToXML method is:
2613 % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2615 % A description of each parameter follows:
2617 % o xml_info: the xml info.
2621 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2622 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2628 canonical_content=CanonicalXMLContent(source,pedantic);
2634 content=AcquireString(source);
2635 content[offset]='\0';
2636 canonical_content=CanonicalXMLContent(content,pedantic);
2637 content=DestroyString(content);
2639 if (canonical_content == (char *) NULL)
2640 return(*destination);
2641 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2643 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2644 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2645 sizeof(**destination));
2646 if (*destination == (char *) NULL)
2647 return(*destination);
2649 *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2651 canonical_content=DestroyString(canonical_content);
2652 return(*destination);
2655 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2656 size_t *extent,size_t start,char ***attributes)
2673 content=(char *) "";
2674 if (xml_info->parent != (XMLTreeInfo *) NULL)
2675 content=xml_info->parent->content;
2677 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2678 start),source,length,extent,MagickFalse);
2679 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2681 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2682 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2683 if (*source == (char *) NULL)
2686 *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2687 for (i=0; xml_info->attributes[i]; i+=2)
2689 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2690 if (attribute != xml_info->attributes[i+1])
2692 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2694 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2695 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2696 if (*source == (char *) NULL)
2697 return((char *) NULL);
2699 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2700 xml_info->attributes[i]);
2701 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2703 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2706 while ((attributes[i] != (char **) NULL) &&
2707 (strcmp(attributes[i][0],xml_info->tag) != 0))
2710 while ((attributes[i] != (char **) NULL) &&
2711 (attributes[i][j] != (char *) NULL))
2713 if ((attributes[i][j+1] == (char *) NULL) ||
2714 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2719 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2721 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2722 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2723 if (*source == (char *) NULL)
2724 return((char *) NULL);
2726 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2728 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2730 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2733 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2735 if (xml_info->child != (XMLTreeInfo *) NULL)
2736 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2738 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2740 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2742 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2743 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2744 if (*source == (char *) NULL)
2745 return((char *) NULL);
2747 if (*xml_info->content != '\0')
2748 *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2750 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2752 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2753 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2756 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2761 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2788 assert(xml_info != (XMLTreeInfo *) NULL);
2789 assert((xml_info->signature == MagickCoreSignature) ||
2790 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2791 if (xml_info->debug != MagickFalse)
2792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2793 if (xml_info->tag == (char *) NULL)
2794 return((char *) NULL);
2795 xml=AcquireString((char *) NULL);
2797 extent=MagickPathExtent;
2798 root=(XMLTreeRoot *) xml_info;
2799 while (root->root.parent != (XMLTreeInfo *) NULL)
2800 root=(XMLTreeRoot *) root->root.parent;
2801 parent=xml_info->parent;
2802 if (parent == (XMLTreeInfo *) NULL)
2803 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2806 Pre-root processing instructions.
2808 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2809 p=root->processing_instructions[i][1];
2810 for (j=1; p != (char *) NULL; j++)
2812 if (root->processing_instructions[i][k][j-1] == '>')
2814 p=root->processing_instructions[i][j];
2817 q=root->processing_instructions[i][0];
2818 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2820 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2821 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2822 if (xml == (char *) NULL)
2825 length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2826 *p != '\0' ? " " : "",p);
2827 p=root->processing_instructions[i][j];
2830 ordered=xml_info->ordered;
2831 xml_info->parent=(XMLTreeInfo *) NULL;
2832 xml_info->ordered=(XMLTreeInfo *) NULL;
2833 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2834 xml_info->parent=parent;
2835 xml_info->ordered=ordered;
2836 if (parent == (XMLTreeInfo *) NULL)
2837 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2840 Post-root processing instructions.
2842 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2843 p=root->processing_instructions[i][1];
2844 for (j=1; p != (char *) NULL; j++)
2846 if (root->processing_instructions[i][k][j-1] == '<')
2848 p=root->processing_instructions[i][j];
2851 q=root->processing_instructions[i][0];
2852 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2854 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2855 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2856 if (xml == (char *) NULL)
2859 length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2860 *p != '\0' ? " " : "",p);
2861 p=root->processing_instructions[i][j];
2864 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));