2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % TTTTT RRRR EEEEE EEEEE %
26 % Copyright 1999-2017 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://www.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/semaphore.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/string-private.h"
61 #include "MagickCore/token-private.h"
62 #include "MagickCore/xml-tree.h"
63 #include "MagickCore/xml-tree-private.h"
64 #include "MagickCore/utility.h"
65 #include "MagickCore/utility-private.h"
70 #define NumberPredefinedEntities 10
71 #define XMLWhitespace "\t\r\n "
103 typedef struct _XMLTreeRoot
118 ***processing_instructions,
136 *sentinel[] = { (char *) NULL };
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % A d d C h i l d T o X M L T r e e %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % AddChildToXMLTree() adds a child tag at an offset relative to the start of
150 % the parent tag's character content. Return the child tag.
152 % The format of the AddChildToXMLTree method is:
154 % XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
155 % const size_t offset)
157 % A description of each parameter follows:
159 % o xml_info: the xml info.
163 % o offset: the tag offset.
166 MagickExport 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) ResetMagickMemory(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));
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % A d d P a t h T o X M L T r e e %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % AddPathToXMLTree() adds a child tag at an offset relative to the start of
198 % the parent tag's character content. This method returns the child tag.
200 % The format of the AddPathToXMLTree method is:
202 % XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
203 % const size_t offset)
205 % A description of each parameter follows:
207 % o xml_info: the xml info.
211 % o offset: the tag offset.
214 MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
215 const char *path,const size_t offset)
219 subnode[MagickPathExtent],
220 tag[MagickPathExtent];
235 assert(xml_info != (XMLTreeInfo *) NULL);
236 assert((xml_info->signature == MagickCoreSignature) ||
237 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
238 if (xml_info->debug != MagickFalse)
239 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
241 components=GetPathComponents(path,&number_components);
242 if (components == (char **) NULL)
243 return((XMLTreeInfo *) NULL);
244 for (i=0; i < (ssize_t) number_components; i++)
246 GetPathComponent(components[i],SubimagePath,subnode);
247 GetPathComponent(components[i],CanonicalPath,tag);
248 child=GetXMLTreeChild(node,tag);
249 if (child == (XMLTreeInfo *) NULL)
250 child=AddChildToXMLTree(node,tag,offset);
252 if (node == (XMLTreeInfo *) NULL)
254 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
256 node=GetXMLTreeOrdered(node);
257 if (node == (XMLTreeInfo *) NULL)
260 if (node == (XMLTreeInfo *) NULL)
262 components[i]=DestroyString(components[i]);
264 for ( ; i < (ssize_t) number_components; i++)
265 components[i]=DestroyString(components[i]);
266 components=(char **) RelinquishMagickMemory(components);
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % C a n o n i c a l X M L C o n t e n t %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % CanonicalXMLContent() converts text to canonical XML content by converting
282 % to UTF-8, substituting predefined entities, wrapping as CDATA, or encoding
283 % as base-64 as required.
285 % The format of the CanonicalXMLContent method is:
288 % char *CanonicalXMLContent(const char *content,
289 % const MagickBooleanType pedantic)
291 % A description of each parameter follows:
293 % o content: the content.
295 % o pedantic: if true, replace newlines and tabs with their respective
299 MagickPrivate char *CanonicalXMLContent(const char *content,
300 const MagickBooleanType pedantic)
306 register const unsigned char
319 utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
320 if (utf8 == (unsigned char *) NULL)
321 return((char *) NULL);
322 for (p=utf8; *p != '\0'; p++)
323 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
328 String is binary, base64-encode it.
330 base64=Base64Encode(utf8,strlen((char *) utf8),&length);
331 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
332 if (base64 == (char *) NULL)
333 return((char *) NULL);
334 canonical_content=AcquireString("<base64>");
335 (void) ConcatenateString(&canonical_content,base64);
336 base64=DestroyString(base64);
337 (void) ConcatenateString(&canonical_content,"</base64>");
338 return(canonical_content);
341 Substitute predefined entities.
344 canonical_content=AcquireString((char *) NULL);
345 extent=MagickPathExtent;
346 for (p=utf8; *p != '\0'; p++)
348 if ((i+MagickPathExtent) > (ssize_t) extent)
350 extent+=MagickPathExtent;
351 canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
352 sizeof(*canonical_content));
353 if (canonical_content == (char *) NULL)
354 return(canonical_content);
360 i+=FormatLocaleString(canonical_content+i,extent,"&");
365 i+=FormatLocaleString(canonical_content+i,extent,"<");
370 i+=FormatLocaleString(canonical_content+i,extent,">");
375 i+=FormatLocaleString(canonical_content+i,extent,""");
380 if (pedantic == MagickFalse)
382 canonical_content[i++]=(char) (*p);
385 i+=FormatLocaleString(canonical_content+i,extent,"
");
390 if (pedantic == MagickFalse)
392 canonical_content[i++]=(char) (*p);
395 i+=FormatLocaleString(canonical_content+i,extent,"	");
400 i+=FormatLocaleString(canonical_content+i,extent,"
");
405 canonical_content[i++]=(char) (*p);
410 canonical_content[i]='\0';
411 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
412 return(canonical_content);
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 % D e s t r o y X M L T r e e %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % DestroyXMLTree() destroys the xml-tree.
428 % The format of the DestroyXMLTree method is:
430 % XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
432 % A description of each parameter follows:
434 % o xml_info: the xml info.
438 static char **DestroyXMLTreeAttributes(char **attributes)
444 Destroy a tag attribute list.
446 if ((attributes == (char **) NULL) || (attributes == sentinel))
447 return((char **) NULL);
448 for (i=0; attributes[i] != (char *) NULL; i+=2)
451 Destroy attribute tag and value.
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);
462 static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
468 child=xml_info->child;
469 while(child != (XMLTreeInfo *) NULL)
473 node->child=(XMLTreeInfo *) NULL;
474 (void) DestroyXMLTree(node);
478 static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
484 ordered=xml_info->ordered;
485 while(ordered != (XMLTreeInfo *) NULL)
488 ordered=node->ordered;
489 node->ordered=(XMLTreeInfo *) NULL;
490 (void) DestroyXMLTree(node);
494 static 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 (xml_info->debug != MagickFalse)
512 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
513 if (xml_info->parent != (XMLTreeInfo *) NULL)
516 Free root tag allocations.
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);
557 MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
559 assert(xml_info != (XMLTreeInfo *) NULL);
560 assert((xml_info->signature == MagickCoreSignature) ||
561 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
562 if (xml_info->debug != MagickFalse)
563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
564 DestroyXMLTreeChild(xml_info);
565 DestroyXMLTreeOrdered(xml_info);
566 DestroyXMLTreeRoot(xml_info);
567 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
568 xml_info->content=DestroyString(xml_info->content);
569 xml_info->tag=DestroyString(xml_info->tag);
570 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
571 return((XMLTreeInfo *) NULL);
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % F i l e T o X M L %
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % FileToXML() returns the contents of a file as a XML string.
587 % The format of the FileToXML method is:
589 % char *FileToXML(const char *filename,const size_t extent)
591 % A description of each parameter follows:
593 % o filename: the filename.
595 % 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_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(const XMLTreeInfo *xml_info,
849 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,const char *path)
1007 subnode[MagickPathExtent],
1008 tag[MagickPathExtent];
1022 assert(xml_info != (XMLTreeInfo *) NULL);
1023 assert((xml_info->signature == MagickCoreSignature) ||
1024 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1025 if (xml_info->debug != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1028 components=GetPathComponents(path,&number_components);
1029 if (components == (char **) NULL)
1030 return((XMLTreeInfo *) NULL);
1031 for (i=0; i < (ssize_t) number_components; i++)
1033 GetPathComponent(components[i],SubimagePath,subnode);
1034 GetPathComponent(components[i],CanonicalPath,tag);
1035 node=GetXMLTreeChild(node,tag);
1036 if (node == (XMLTreeInfo *) NULL)
1038 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1040 node=GetXMLTreeOrdered(node);
1041 if (node == (XMLTreeInfo *) NULL)
1044 if (node == (XMLTreeInfo *) NULL)
1046 components[i]=DestroyString(components[i]);
1048 for ( ; i < (ssize_t) number_components; i++)
1049 components[i]=DestroyString(components[i]);
1050 components=(char **) RelinquishMagickMemory(components);
1055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059 % 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 %
1063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 % GetXMLTreeProcessingInstructions() returns a null terminated array of
1066 % processing instructions for the given target.
1068 % The format of the GetXMLTreeProcessingInstructions method is:
1070 % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
1071 % const char *target)
1073 % A description of each parameter follows:
1075 % o xml_info: the xml info.
1078 MagickPrivate const char **GetXMLTreeProcessingInstructions(
1079 XMLTreeInfo *xml_info,const char *target)
1087 assert(xml_info != (XMLTreeInfo *) NULL);
1088 assert((xml_info->signature == MagickCoreSignature) ||
1089 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1090 if (xml_info->debug != MagickFalse)
1091 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1092 root=(XMLTreeRoot *) xml_info;
1093 while (root->root.parent != (XMLTreeInfo *) NULL)
1094 root=(XMLTreeRoot *) root->root.parent;
1096 while ((root->processing_instructions[i] != (char **) NULL) &&
1097 (strcmp(root->processing_instructions[i][0],target) != 0))
1099 if (root->processing_instructions[i] == (char **) NULL)
1100 return((const char **) sentinel);
1101 return((const char **) (root->processing_instructions[i]+1));
1105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1109 % G e t X M L T r e e S i b l i n g %
1113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1115 % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
1117 % The format of the GetXMLTreeSibling method is:
1119 % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1121 % A description of each parameter follows:
1123 % o xml_info: the xml info.
1126 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1128 assert(xml_info != (XMLTreeInfo *) NULL);
1129 assert((xml_info->signature == MagickCoreSignature) ||
1130 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1131 if (xml_info->debug != MagickFalse)
1132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1133 return(xml_info->sibling);
1137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1141 % G e t X M L T r e e T a g %
1145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1147 % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
1149 % The format of the GetXMLTreeTag method is:
1151 % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1153 % A description of each parameter follows:
1155 % o xml_info: the xml info.
1158 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1160 assert(xml_info != (XMLTreeInfo *) NULL);
1161 assert((xml_info->signature == MagickCoreSignature) ||
1162 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1163 if (xml_info->debug != MagickFalse)
1164 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1165 return(xml_info->tag);
1169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1173 % I n s e r t I n t o T a g X M L T r e e %
1177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179 % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
1180 % the parent tag's character content. This method returns the child tag.
1182 % The format of the InsertTagIntoXMLTree method is:
1184 % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1185 % XMLTreeInfo *child,const size_t offset)
1187 % A description of each parameter follows:
1189 % o xml_info: the xml info.
1191 % o child: the child tag.
1193 % o offset: the tag offset.
1196 MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1197 XMLTreeInfo *child,const size_t offset)
1204 child->ordered=(XMLTreeInfo *) NULL;
1205 child->sibling=(XMLTreeInfo *) NULL;
1206 child->next=(XMLTreeInfo *) NULL;
1207 child->offset=offset;
1208 child->parent=xml_info;
1209 if (xml_info->child == (XMLTreeInfo *) NULL)
1211 xml_info->child=child;
1214 head=xml_info->child;
1215 if (head->offset > offset)
1217 child->ordered=head;
1218 xml_info->child=child;
1223 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1224 (node->ordered->offset <= offset))
1226 child->ordered=node->ordered;
1227 node->ordered=child;
1229 previous=(XMLTreeInfo *) NULL;
1231 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1236 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1238 while ((node->next != (XMLTreeInfo *) NULL) &&
1239 (node->next->offset <= offset))
1241 child->next=node->next;
1246 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1247 previous->sibling=node->sibling;
1249 previous=(XMLTreeInfo *) NULL;
1251 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1256 child->sibling=node;
1257 if (previous != (XMLTreeInfo *) NULL)
1258 previous->sibling=child;
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % N e w X M L T r e e %
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1274 % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1277 % The format of the NewXMLTree method is:
1279 % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1281 % A description of each parameter follows:
1283 % o xml: The XML string.
1285 % o exception: return any errors or warnings in this structure.
1289 static 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) CopyMagickMemory(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+MagickPathExtent) > extent)
1337 extent=(size_t) j+MagickPathExtent;
1338 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1339 if (utf8 == (char *) NULL)
1349 Multi-byte UTF-8 sequence.
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,sizeof(*utf8));
1365 if (utf8 != (char *) NULL)
1370 static char *ParseEntities(char *xml,char **entities,int state)
1394 Normalize line endings.
1398 for ( ; *xml != '\0'; xml++)
1399 while (*xml == '\r')
1403 (void) CopyMagickMemory(xml,xml+1,strlen(xml));
1407 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1408 (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1414 '&' for general entity decoding
1415 '%' for parameter entity decoding
1416 'c' for CDATA sections
1417 ' ' for attributes normalization
1418 '*' for non-CDATA attributes normalization
1420 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1423 Character reference.
1426 c=strtol(xml+2,&entity,10); /* base 10 */
1428 c=strtol(xml+3,&entity,16); /* base 16 */
1429 if ((c == 0) || (*entity != ';'))
1432 Not a character reference.
1442 Multi-byte UTF-8 sequence.
1445 for (i=0; byte != 0; byte/=2)
1448 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1453 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1457 (void) CopyMagickMemory(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1460 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1461 (state == '*'))) || ((state == '%') && (*xml == '%')))
1464 Find entity in the list.
1467 while ((entities[i] != (char *) NULL) &&
1468 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1470 if (entities[i++] == (char *) NULL)
1473 if (entities[i] != (char *) NULL)
1478 length=strlen(entities[i]);
1479 entity=strchr(xml,';');
1480 if ((entity != (char *) NULL) &&
1481 ((length-1L) >= (size_t) (entity-xml)))
1483 offset=(ssize_t) (xml-p);
1484 extent=(size_t) (offset+length+strlen(entity));
1486 p=(char *) ResizeQuantumMemory(p,extent,sizeof(*p));
1492 extent_xml=(char *) AcquireQuantumMemory(extent,
1493 sizeof(*extent_xml));
1494 if (extent_xml != (char *) NULL)
1496 (void) CopyMagickString(extent_xml,p,extent*
1497 sizeof(*extent_xml));
1501 if (p == (char *) NULL)
1502 ThrowFatalException(ResourceLimitFatalError,
1503 "MemoryAllocationFailed");
1505 entity=strchr(xml,';');
1507 if (entity != (char *) NULL)
1508 (void) CopyMagickMemory(xml+length,entity+1,strlen(entity));
1509 (void) strncpy(xml,entities[i],length);
1513 if (((state == ' ') || (state == '*')) &&
1514 (isspace((int) ((unsigned char) *xml) != 0)))
1522 Normalize spaces for non-CDATA attributes.
1524 for (xml=p; *xml != '\0'; xml++)
1529 i=(ssize_t) strspn(xml,accept);
1531 (void) CopyMagickMemory(xml,xml+i,strlen(xml+i)+1);
1532 while ((*xml != '\0') && (*xml != ' '))
1536 if ((xml >= p) && (*xml == ' '))
1539 return(p == q ? ConstantString(p) : p);
1542 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1543 const size_t length,const char state)
1548 xml_info=root->node;
1549 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1553 xml=ParseEntities(xml,root->entities,state);
1554 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1556 (void) ConcatenateString(&xml_info->content,xml);
1557 xml=DestroyString(xml);
1561 if (xml_info->content != (char *) NULL)
1562 xml_info->content=DestroyString(xml_info->content);
1563 xml_info->content=xml;
1567 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1568 ExceptionInfo *exception)
1570 if ((root->node == (XMLTreeInfo *) NULL) ||
1571 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1573 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1574 "ParseError","unexpected closing tag </%s>",tag);
1575 return(&root->root);
1577 root->node=root->node->parent;
1578 return((XMLTreeInfo *) NULL);
1581 static MagickBooleanType ValidateEntities(char *tag,char *xml,char **entities)
1587 Check for circular entity references.
1591 while ((*xml != '\0') && (*xml != '&'))
1595 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1596 return(MagickFalse);
1598 while ((entities[i] != (char *) NULL) &&
1599 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1601 if ((entities[i] != (char *) NULL) &&
1602 (ValidateEntities(tag,entities[i+1],entities) == 0))
1603 return(MagickFalse);
1607 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1621 xml+=strcspn(xml,XMLWhitespace);
1625 xml+=strspn(xml+1,XMLWhitespace)+1;
1627 if (strcmp(target,"xml") == 0)
1629 xml=strstr(xml,"standalone");
1630 if ((xml != (char *) NULL) &&
1631 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1632 root->standalone=MagickTrue;
1635 if (root->processing_instructions[0] == (char **) NULL)
1637 root->processing_instructions=(char ***) AcquireMagickMemory(sizeof(
1638 *root->processing_instructions));
1639 if (root->processing_instructions ==(char ***) NULL)
1640 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1641 *root->processing_instructions=(char **) NULL;
1644 while ((root->processing_instructions[i] != (char **) NULL) &&
1645 (strcmp(target,root->processing_instructions[i][0]) != 0))
1647 if (root->processing_instructions[i] == (char **) NULL)
1649 root->processing_instructions=(char ***) ResizeQuantumMemory(
1650 root->processing_instructions,(size_t) (i+2),
1651 sizeof(*root->processing_instructions));
1652 if (root->processing_instructions == (char ***) NULL)
1653 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1654 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1655 sizeof(**root->processing_instructions));
1656 if (root->processing_instructions[i] == (char **) NULL)
1657 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1658 root->processing_instructions[i+1]=(char **) NULL;
1659 root->processing_instructions[i][0]=ConstantString(target);
1660 root->processing_instructions[i][1]=(char *)
1661 root->processing_instructions[i+1];
1662 root->processing_instructions[i+1]=(char **) NULL;
1663 root->processing_instructions[i][2]=ConstantString("");
1666 while (root->processing_instructions[i][j] != (char *) NULL)
1668 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1669 root->processing_instructions[i],(size_t) (j+3),
1670 sizeof(**root->processing_instructions));
1671 if (root->processing_instructions[i] == (char **) NULL)
1672 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1673 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1674 root->processing_instructions[i][j+1],(size_t) (j+1),
1675 sizeof(***root->processing_instructions));
1676 if (root->processing_instructions[i][j+2] == (char *) NULL)
1677 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1678 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1679 root->root.tag != (char *) NULL ? ">" : "<",2);
1680 root->processing_instructions[i][j]=ConstantString(xml);
1681 root->processing_instructions[i][j+1]=(char *) NULL;
1684 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1685 size_t length,ExceptionInfo *exception)
1691 **predefined_entitites,
1703 predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1704 if (predefined_entitites == (char **) NULL)
1705 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1706 (void) CopyMagickMemory(predefined_entitites,sentinel,sizeof(sentinel));
1707 for (xml[length]='\0'; xml != (char *) NULL; )
1709 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1713 if (strncmp(xml,"<!ENTITY",8) == 0)
1716 Parse entity definitions.
1718 xml+=strspn(xml+8,XMLWhitespace)+8;
1720 n=xml+strspn(xml,XMLWhitespace "%");
1721 xml=n+strcspn(n,XMLWhitespace);
1723 v=xml+strspn(xml+1,XMLWhitespace)+1;
1726 if ((q != '"') && (q != '\''))
1731 xml=strchr(xml,'>');
1734 entities=(*c == '%') ? predefined_entitites : root->entities;
1735 for (i=0; entities[i] != (char *) NULL; i++) ;
1736 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1738 if (entities == (char **) NULL)
1739 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1741 predefined_entitites=entities;
1743 root->entities=entities;
1747 if (xml != (char *) NULL)
1752 entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1753 entities[i+2]=(char *) NULL;
1754 if (ValidateEntities(n,entities[i+1],entities) != MagickFalse)
1758 if (entities[i+1] != v)
1759 entities[i+1]=DestroyString(entities[i+1]);
1760 (void) ThrowMagickException(exception,GetMagickModule(),
1761 OptionWarning,"ParseError","circular entity declaration &%s",n);
1762 predefined_entitites=(char **) RelinquishMagickMemory(
1763 predefined_entitites);
1764 return(MagickFalse);
1768 if (strncmp(xml,"<!ATTLIST",9) == 0)
1771 Parse default attributes.
1773 t=xml+strspn(xml+9,XMLWhitespace)+9;
1776 (void) ThrowMagickException(exception,GetMagickModule(),
1777 OptionWarning,"ParseError","unclosed <!ATTLIST");
1778 predefined_entitites=(char **) RelinquishMagickMemory(
1779 predefined_entitites);
1780 return(MagickFalse);
1782 xml=t+strcspn(t,XMLWhitespace ">");
1787 while ((root->attributes[i] != (char **) NULL) &&
1788 (n != (char *) NULL) &&
1789 (strcmp(n,root->attributes[i][0]) != 0))
1791 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1794 xml=n+strcspn(n,XMLWhitespace);
1799 (void) ThrowMagickException(exception,GetMagickModule(),
1800 OptionWarning,"ParseError","malformed <!ATTLIST");
1801 predefined_entitites=(char **) RelinquishMagickMemory(
1802 predefined_entitites);
1803 return(MagickFalse);
1805 xml+=strspn(xml+1,XMLWhitespace)+1;
1806 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1807 if (strncmp(xml,"NOTATION",8) == 0)
1808 xml+=strspn(xml+8,XMLWhitespace)+8;
1809 xml=(*xml == '(') ? strchr(xml,')') : xml+
1810 strcspn(xml,XMLWhitespace);
1811 if (xml == (char *) NULL)
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,XMLWhitespace ")");
1820 if (strncmp(xml,"#FIXED",6) == 0)
1821 xml+=strspn(xml+6,XMLWhitespace)+6;
1824 xml+=strcspn(xml,XMLWhitespace ">")-1;
1830 if (((*xml == '"') || (*xml == '\'')) &&
1831 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1835 (void) ThrowMagickException(exception,GetMagickModule(),
1836 OptionWarning,"ParseError","malformed <!ATTLIST");
1837 predefined_entitites=(char **) RelinquishMagickMemory(
1838 predefined_entitites);
1839 return(MagickFalse);
1841 if (root->attributes[i] == (char **) NULL)
1847 root->attributes=(char ***) AcquireQuantumMemory(2,
1848 sizeof(*root->attributes));
1850 root->attributes=(char ***) ResizeQuantumMemory(
1851 root->attributes,(size_t) (i+2),
1852 sizeof(*root->attributes));
1853 if (root->attributes == (char ***) NULL)
1854 ThrowFatalException(ResourceLimitFatalError,
1855 "MemoryAllocationFailed");
1856 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1857 sizeof(**root->attributes));
1858 if (root->attributes[i] == (char **) NULL)
1859 ThrowFatalException(ResourceLimitFatalError,
1860 "MemoryAllocationFailed");
1861 root->attributes[i][0]=ConstantString(t);
1862 root->attributes[i][1]=(char *) NULL;
1863 root->attributes[i+1]=(char **) NULL;
1865 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1866 root->attributes[i]=(char **) ResizeQuantumMemory(
1867 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1868 if (root->attributes[i] == (char **) NULL)
1869 ThrowFatalException(ResourceLimitFatalError,
1870 "MemoryAllocationFailed");
1871 root->attributes[i][j+3]=(char *) NULL;
1872 root->attributes[i][j+2]=ConstantString(c);
1873 root->attributes[i][j+1]=(char *) NULL;
1874 if (v != (char *) NULL)
1875 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1876 root->attributes[i][j]=ConstantString(n);
1880 if (strncmp(xml, "<!--", 4) == 0)
1881 xml=strstr(xml+4,"-->");
1883 if (strncmp(xml,"<?", 2) == 0)
1887 if (xml != (char *) NULL)
1889 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1895 xml=strchr(xml,'>');
1897 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1900 predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1904 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1909 xml_info=root->node;
1910 if (xml_info->tag == (char *) NULL)
1911 xml_info->tag=ConstantString(tag);
1913 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1914 if (xml_info != (XMLTreeInfo *) NULL)
1915 xml_info->attributes=attributes;
1916 root->node=xml_info;
1927 static inline MagickBooleanType IsSkipTag(const char *tag)
1933 while (ignore_tags[i] != (const char *) NULL)
1935 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1939 return(MagickFalse);
1942 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1975 Convert xml-string to UTF8.
1977 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1979 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1980 "ParseError","root tag missing");
1981 return((XMLTreeInfo *) NULL);
1983 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1985 utf8=ConvertUTF16ToUTF8(xml,&length);
1986 if (utf8 == (char *) NULL)
1988 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1989 "ParseError","UTF16 to UTF8 failed");
1990 return((XMLTreeInfo *) NULL);
1992 terminal=utf8[length-1];
1993 utf8[length-1]='\0';
1995 while ((*p != '\0') && (*p != '<'))
1999 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2000 "ParseError","root tag missing");
2001 utf8=DestroyString(utf8);
2002 return((XMLTreeInfo *) NULL);
2004 attribute=(char **) NULL;
2009 attributes=(char **) sentinel;
2012 if ((isalpha((int) ((unsigned char) *p)) !=0) || (*p == '_') ||
2013 (*p == ':') || (c < '\0'))
2018 if (root->node == (XMLTreeInfo *) NULL)
2020 (void) ThrowMagickException(exception,GetMagickModule(),
2021 OptionWarning,"ParseError","root tag missing");
2022 utf8=DestroyString(utf8);
2023 return(&root->root);
2025 p+=strcspn(p,XMLWhitespace "/>");
2026 while (isspace((int) ((unsigned char) *p)) != 0)
2028 if (ignore_depth == 0)
2030 if ((*p != '\0') && (*p != '/') && (*p != '>'))
2033 Find tag in default attributes list.
2036 while ((root->attributes[i] != (char **) NULL) &&
2037 (strcmp(root->attributes[i][0],tag) != 0))
2039 attribute=root->attributes[i];
2041 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
2047 attributes=(char **) AcquireQuantumMemory(4,
2048 sizeof(*attributes));
2050 attributes=(char **) ResizeQuantumMemory(attributes,
2051 (size_t) (l+4),sizeof(*attributes));
2052 if (attributes == (char **) NULL)
2054 (void) ThrowMagickException(exception,GetMagickModule(),
2055 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2056 utf8=DestroyString(utf8);
2057 return(&root->root);
2059 attributes[l+2]=(char *) NULL;
2060 attributes[l+1]=(char *) NULL;
2062 p+=strcspn(p,XMLWhitespace "=/>");
2063 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
2064 attributes[l]=ConstantString("");
2068 p+=strspn(p,XMLWhitespace "=");
2070 if ((c == '"') || (c == '\''))
2077 while ((*p != '\0') && (*p != c))
2083 attributes[l]=ConstantString("");
2084 attributes[l+1]=ConstantString("");
2085 (void) DestroyXMLTreeAttributes(attributes);
2086 (void) ThrowMagickException(exception,
2087 GetMagickModule(),OptionWarning,"ParseError",
2089 utf8=DestroyString(utf8);
2090 return(&root->root);
2093 while ((attribute != (char **) NULL) &&
2094 (attribute[j] != (char *) NULL) &&
2095 (strcmp(attribute[j],attributes[l]) != 0))
2097 attributes[l+1]=ParseEntities(attributes[l+1],
2098 root->entities,(attribute != (char **) NULL) &&
2099 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
2102 attributes[l]=ConstantString(attributes[l]);
2104 while (isspace((int) ((unsigned char) *p)) != 0)
2110 while((*p != '\0') && (*p != '/') && (*p != '>'))
2119 if (((*p != '\0') && (*p != '>')) ||
2120 ((*p == '\0') && (terminal != '>')))
2123 (void) DestroyXMLTreeAttributes(attributes);
2124 (void) ThrowMagickException(exception,GetMagickModule(),
2125 OptionWarning,"ParseError","missing >");
2126 utf8=DestroyString(utf8);
2127 return(&root->root);
2129 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2131 ParseOpenTag(root,tag,attributes);
2132 (void) ParseCloseTag(root,tag,exception);
2138 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
2141 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2142 ParseOpenTag(root,tag,attributes);
2146 (void) DestroyXMLTreeAttributes(attributes);
2153 (void) DestroyXMLTreeAttributes(attributes);
2154 (void) ThrowMagickException(exception,GetMagickModule(),
2155 OptionWarning,"ParseError","missing >");
2156 utf8=DestroyString(utf8);
2157 return(&root->root);
2168 p+=strcspn(tag,XMLWhitespace ">")+1;
2170 if ((c == '\0') && (terminal != '>'))
2172 (void) ThrowMagickException(exception,GetMagickModule(),
2173 OptionWarning,"ParseError","missing >");
2174 utf8=DestroyString(utf8);
2175 return(&root->root);
2178 if (ignore_depth == 0 && ParseCloseTag(root,tag,exception) !=
2179 (XMLTreeInfo *) NULL)
2181 utf8=DestroyString(utf8);
2182 return(&root->root);
2184 if (ignore_depth > 0)
2187 if (isspace((int) ((unsigned char) *p)) != 0)
2188 p+=strspn(p,XMLWhitespace);
2191 if (strncmp(p,"!--",3) == 0)
2197 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
2198 ((*p == '\0') && (terminal != '>')))
2200 (void) ThrowMagickException(exception,GetMagickModule(),
2201 OptionWarning,"ParseError","unclosed <!--");
2202 utf8=DestroyString(utf8);
2203 return(&root->root);
2207 if (strncmp(p,"![CDATA[",8) == 0)
2213 if (p != (char *) NULL)
2216 if (ignore_depth == 0)
2217 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
2221 (void) ThrowMagickException(exception,GetMagickModule(),
2222 OptionWarning,"ParseError","unclosed <![CDATA[");
2223 utf8=DestroyString(utf8);
2224 return(&root->root);
2228 if (strncmp(p,"!DOCTYPE",8) == 0)
2233 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
2234 ((l != 0) && ((*p != ']') ||
2235 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
2236 l=(ssize_t) ((*p == '[') ? 1 : l))
2237 p+=strcspn(p+1,"[]>")+1;
2238 if ((*p == '\0') && (terminal != '>'))
2240 (void) ThrowMagickException(exception,GetMagickModule(),
2241 OptionWarning,"ParseError","unclosed <!DOCTYPE");
2242 utf8=DestroyString(utf8);
2243 return(&root->root);
2246 tag=strchr(tag,'[')+1;
2249 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
2251 if (status == MagickFalse)
2253 utf8=DestroyString(utf8);
2254 return(&root->root);
2263 Processing instructions.
2268 if (p == (char *) NULL)
2271 } while ((*p != '\0') && (*p != '>'));
2272 if ((p == (char *) NULL) || ((*p == '\0') &&
2275 (void) ThrowMagickException(exception,GetMagickModule(),
2276 OptionWarning,"ParseError","unclosed <?");
2277 utf8=DestroyString(utf8);
2278 return(&root->root);
2280 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2284 (void) ThrowMagickException(exception,GetMagickModule(),
2285 OptionWarning,"ParseError","unexpected <");
2286 utf8=DestroyString(utf8);
2287 return(&root->root);
2289 if ((p == (char *) NULL) || (*p == '\0'))
2293 if ((*p != '\0') && (*p != '<'))
2296 Tag character content.
2298 while ((*p != '\0') && (*p != '<'))
2302 if (ignore_depth == 0)
2303 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2309 utf8=DestroyString(utf8);
2310 if (root->node == (XMLTreeInfo *) NULL)
2311 return(&root->root);
2312 if (root->node->tag == (char *) NULL)
2314 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2315 "ParseError","root tag missing");
2316 return(&root->root);
2318 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2319 "ParseError","unclosed tag: '%s'",root->node->tag);
2320 return(&root->root);
2324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2328 % N e w X M L T r e e T a g %
2332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2334 % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2336 % The format of the NewXMLTreeTag method is:
2338 % XMLTreeInfo *NewXMLTreeTag(const char *tag)
2340 % A description of each parameter follows:
2345 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2348 *predefined_entities[NumberPredefinedEntities+1] =
2350 "lt;", "<", "gt;", ">", "quot;", """,
2351 "apos;", "'", "amp;", "&", (char *) NULL
2357 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2358 if (root == (XMLTreeRoot *) NULL)
2359 return((XMLTreeInfo *) NULL);
2360 (void) ResetMagickMemory(root,0,sizeof(*root));
2361 root->root.tag=(char *) NULL;
2362 if (tag != (char *) NULL)
2363 root->root.tag=ConstantString(tag);
2364 root->node=(&root->root);
2365 root->root.content=ConstantString("");
2366 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2367 if (root->entities == (char **) NULL)
2368 return((XMLTreeInfo *) NULL);
2369 (void) CopyMagickMemory(root->entities,predefined_entities,
2370 sizeof(predefined_entities));
2371 root->root.attributes=sentinel;
2372 root->attributes=(char ***) root->root.attributes;
2373 root->processing_instructions=(char ***) root->root.attributes;
2374 root->debug=IsEventLogging();
2375 root->signature=MagickCoreSignature;
2376 return(&root->root);
2380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2384 % P r u n e T a g F r o m X M L T r e e %
2388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2390 % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2393 % The format of the PruneTagFromXMLTree method is:
2395 % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2397 % A description of each parameter follows:
2399 % o xml_info: the xml info.
2402 MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2407 assert(xml_info != (XMLTreeInfo *) NULL);
2408 assert((xml_info->signature == MagickCoreSignature) ||
2409 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2410 if (xml_info->debug != MagickFalse)
2411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2412 if (xml_info->next != (XMLTreeInfo *) NULL)
2413 xml_info->next->sibling=xml_info->sibling;
2414 if (xml_info->parent != (XMLTreeInfo *) NULL)
2416 node=xml_info->parent->child;
2417 if (node == xml_info)
2418 xml_info->parent->child=xml_info->ordered;
2421 while (node->ordered != xml_info)
2423 node->ordered=node->ordered->ordered;
2424 node=xml_info->parent->child;
2425 if (strcmp(node->tag,xml_info->tag) != 0)
2427 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2429 if (node->sibling != xml_info)
2432 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2433 xml_info->next : node->sibling->sibling;
2435 while ((node->next != (XMLTreeInfo *) NULL) &&
2436 (node->next != xml_info))
2438 if (node->next != (XMLTreeInfo *) NULL)
2439 node->next=node->next->next;
2442 xml_info->ordered=(XMLTreeInfo *) NULL;
2443 xml_info->sibling=(XMLTreeInfo *) NULL;
2444 xml_info->next=(XMLTreeInfo *) NULL;
2449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2453 % S e t X M L T r e e A t t r i b u t e %
2457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2459 % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2460 % found. A value of NULL removes the specified attribute.
2462 % The format of the SetXMLTreeAttribute method is:
2464 % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2465 % const char *value)
2467 % A description of each parameter follows:
2469 % o xml_info: the xml info.
2471 % o tag: The attribute tag.
2473 % o value: The attribute value.
2476 MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2477 const char *tag,const char *value)
2485 assert(xml_info != (XMLTreeInfo *) NULL);
2486 assert((xml_info->signature == MagickCoreSignature) ||
2487 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2488 if (xml_info->debug != MagickFalse)
2489 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2491 while ((xml_info->attributes[i] != (char *) NULL) &&
2492 (strcmp(xml_info->attributes[i],tag) != 0))
2494 if (xml_info->attributes[i] == (char *) NULL)
2497 Add new attribute tag.
2499 if (value == (const char *) NULL)
2501 if (xml_info->attributes != sentinel)
2502 xml_info->attributes=(char **) ResizeQuantumMemory(
2503 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2506 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2507 sizeof(*xml_info->attributes));
2508 if (xml_info->attributes != (char **) NULL)
2509 xml_info->attributes[1]=ConstantString("");
2511 if (xml_info->attributes == (char **) NULL)
2512 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2513 xml_info->attributes[i]=ConstantString(tag);
2514 xml_info->attributes[i+2]=(char *) NULL;
2515 (void) strlen(xml_info->attributes[i+1]);
2518 Add new value to an existing attribute.
2520 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2521 if (xml_info->attributes[i+1] != (char *) NULL)
2522 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2523 if (value != (const char *) NULL)
2525 xml_info->attributes[i+1]=ConstantString(value);
2528 if (xml_info->attributes[i] != (char *) NULL)
2529 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2530 (void) CopyMagickMemory(xml_info->attributes+i,xml_info->attributes+i+2,
2531 (size_t) (j-i)*sizeof(*xml_info->attributes));
2532 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2533 (size_t) (j+2),sizeof(*xml_info->attributes));
2534 if (xml_info->attributes == (char **) NULL)
2535 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2537 (void) CopyMagickMemory(xml_info->attributes[j+1]+(i/2),
2538 xml_info->attributes[j+1]+(i/2)+1,(size_t) (((j+2)/2)-(i/2))*
2539 sizeof(**xml_info->attributes));
2544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2548 % S e t X M L T r e e C o n t e n t %
2552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2554 % SetXMLTreeContent() sets the character content for the given tag and
2557 % The format of the SetXMLTreeContent method is:
2559 % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2560 % const char *content)
2562 % A description of each parameter follows:
2564 % o xml_info: the xml info.
2566 % o content: The content.
2569 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2570 const char *content)
2572 assert(xml_info != (XMLTreeInfo *) NULL);
2573 assert((xml_info->signature == MagickCoreSignature) ||
2574 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2575 if (xml_info->debug != MagickFalse)
2576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2577 if (xml_info->content != (char *) NULL)
2578 xml_info->content=DestroyString(xml_info->content);
2579 xml_info->content=(char *) ConstantString(content);
2584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2588 % X M L T r e e I n f o T o X M L %
2592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2594 % XMLTreeInfoToXML() converts an xml-tree to an XML string.
2596 % The format of the XMLTreeInfoToXML method is:
2598 % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2600 % A description of each parameter follows:
2602 % o xml_info: the xml info.
2606 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2607 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2613 canonical_content=CanonicalXMLContent(source,pedantic);
2619 content=AcquireString(source);
2620 content[offset]='\0';
2621 canonical_content=CanonicalXMLContent(content,pedantic);
2622 content=DestroyString(content);
2624 if (canonical_content == (char *) NULL)
2625 return(*destination);
2626 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2628 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2629 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2630 sizeof(**destination));
2631 if (*destination == (char *) NULL)
2632 return(*destination);
2634 *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2636 canonical_content=DestroyString(canonical_content);
2637 return(*destination);
2640 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2641 size_t *extent,size_t start,char ***attributes)
2658 content=(char *) "";
2659 if (xml_info->parent != (XMLTreeInfo *) NULL)
2660 content=xml_info->parent->content;
2662 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2663 start),source,length,extent,MagickFalse);
2664 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2666 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2667 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2668 if (*source == (char *) NULL)
2671 *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2672 for (i=0; xml_info->attributes[i]; i+=2)
2674 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2675 if (attribute != xml_info->attributes[i+1])
2677 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2679 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2680 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2681 if (*source == (char *) NULL)
2682 return((char *) NULL);
2684 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2685 xml_info->attributes[i]);
2686 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2688 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2691 while ((attributes[i] != (char **) NULL) &&
2692 (strcmp(attributes[i][0],xml_info->tag) != 0))
2695 while ((attributes[i] != (char **) NULL) &&
2696 (attributes[i][j] != (char *) NULL))
2698 if ((attributes[i][j+1] == (char *) NULL) ||
2699 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2704 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2706 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2707 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2708 if (*source == (char *) NULL)
2709 return((char *) NULL);
2711 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2713 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2715 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2718 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2720 if (xml_info->child != (XMLTreeInfo *) NULL)
2721 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2723 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2725 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2727 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2728 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2729 if (*source == (char *) NULL)
2730 return((char *) NULL);
2732 if (*xml_info->content != '\0')
2733 *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2735 while ((content[offset] != '\0') && (offset < xml_info->offset))
2737 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2738 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2741 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2746 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2773 assert(xml_info != (XMLTreeInfo *) NULL);
2774 assert((xml_info->signature == MagickCoreSignature) ||
2775 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2776 if (xml_info->debug != MagickFalse)
2777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2778 if (xml_info->tag == (char *) NULL)
2779 return((char *) NULL);
2780 xml=AcquireString((char *) NULL);
2782 extent=MagickPathExtent;
2783 root=(XMLTreeRoot *) xml_info;
2784 while (root->root.parent != (XMLTreeInfo *) NULL)
2785 root=(XMLTreeRoot *) root->root.parent;
2786 parent=xml_info->parent;
2787 if (parent == (XMLTreeInfo *) NULL)
2788 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2791 Pre-root processing instructions.
2793 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2794 p=root->processing_instructions[i][1];
2795 for (j=1; p != (char *) NULL; j++)
2797 if (root->processing_instructions[i][k][j-1] == '>')
2799 p=root->processing_instructions[i][j];
2802 q=root->processing_instructions[i][0];
2803 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2805 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2806 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2807 if (xml == (char *) NULL)
2810 length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2811 *p != '\0' ? " " : "",p);
2812 p=root->processing_instructions[i][j];
2815 ordered=xml_info->ordered;
2816 xml_info->parent=(XMLTreeInfo *) NULL;
2817 xml_info->ordered=(XMLTreeInfo *) NULL;
2818 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2819 xml_info->parent=parent;
2820 xml_info->ordered=ordered;
2821 if (parent == (XMLTreeInfo *) NULL)
2822 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2825 Post-root processing instructions.
2827 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2828 p=root->processing_instructions[i][1];
2829 for (j=1; p != (char *) NULL; j++)
2831 if (root->processing_instructions[i][k][j-1] == '<')
2833 p=root->processing_instructions[i][j];
2836 q=root->processing_instructions[i][0];
2837 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2839 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2840 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2841 if (xml == (char *) NULL)
2844 length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2845 *p != '\0' ? " " : "",p);
2846 p=root->processing_instructions[i][j];
2849 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));