2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % TTTTT RRRR EEEEE EEEEE %
26 % Copyright 1999-2011 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 % http://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/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/log.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/token-private.h"
60 #include "MagickCore/xml-tree.h"
61 #include "MagickCore/xml-tree-private.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/utility-private.h"
68 #define NumberPredefinedEntities 10
69 #define XMLWhitespace "\t\r\n "
101 typedef struct _XMLTreeRoot
116 ***processing_instructions,
134 *sentinel[] = { (char *) NULL };
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % A d d C h i l d T o X M L T r e e %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % AddChildToXMLTree() adds a child tag at an offset relative to the start of
148 % the parent tag's character content. Return the child tag.
150 % The format of the AddChildToXMLTree method is:
152 % XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
153 % const size_t offset)
155 % A description of each parameter follows:
157 % o xml_info: the xml info.
161 % o offset: the tag offset.
164 MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
165 const char *tag,const size_t offset)
170 if (xml_info == (XMLTreeInfo *) NULL)
171 return((XMLTreeInfo *) NULL);
172 child=(XMLTreeInfo *) AcquireMagickMemory(sizeof(*child));
173 if (child == (XMLTreeInfo *) NULL)
174 return((XMLTreeInfo *) NULL);
175 (void) ResetMagickMemory(child,0,sizeof(*child));
176 child->tag=ConstantString(tag);
177 child->attributes=sentinel;
178 child->content=ConstantString("");
179 child->debug=IsEventLogging();
180 child->signature=MagickSignature;
181 return(InsertTagIntoXMLTree(xml_info,child,offset));
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % A d d P a t h T o X M L T r e e %
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 % AddPathToXMLTree() adds a child tag at an offset relative to the start of
196 % the parent tag's character content. This method returns the child tag.
198 % The format of the AddPathToXMLTree method is:
200 % XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
201 % const size_t offset)
203 % A description of each parameter follows:
205 % o xml_info: the xml info.
209 % o offset: the tag offset.
212 MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
213 const char *path,const size_t offset)
217 subnode[MaxTextExtent],
233 assert(xml_info != (XMLTreeInfo *) NULL);
234 assert((xml_info->signature == MagickSignature) ||
235 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
238 components=GetPathComponents(path,&number_components);
239 if (components == (char **) NULL)
240 return((XMLTreeInfo *) NULL);
241 for (i=0; i < (ssize_t) number_components; i++)
243 GetPathComponent(components[i],SubimagePath,subnode);
244 GetPathComponent(components[i],CanonicalPath,tag);
245 child=GetXMLTreeChild(node,tag);
246 if (child == (XMLTreeInfo *) NULL)
247 child=AddChildToXMLTree(node,tag,offset);
249 if (node == (XMLTreeInfo *) NULL)
251 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
253 node=GetXMLTreeOrdered(node);
254 if (node == (XMLTreeInfo *) NULL)
257 if (node == (XMLTreeInfo *) NULL)
259 components[i]=DestroyString(components[i]);
261 for ( ; i < (ssize_t) number_components; i++)
262 components[i]=DestroyString(components[i]);
263 components=(char **) RelinquishMagickMemory(components);
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % C a n o n i c a l X M L C o n t e n t %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % CanonicalXMLContent() converts text to canonical XML content by converting
279 % to UTF-8, substituting predefined entities, wrapping as CDATA, or encoding
280 % as base-64 as required.
282 % The format of the CanonicalXMLContent method is:
285 % char *CanonicalXMLContent(const char *content,
286 % const MagickBooleanType pedantic)
288 % A description of each parameter follows:
290 % o content: the content.
292 % o pedantic: if true, replace newlines and tabs with their respective
296 MagickPrivate char *CanonicalXMLContent(const char *content,
297 const MagickBooleanType pedantic)
303 register const unsigned char
316 utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
317 if (utf8 == (unsigned char *) NULL)
318 return((char *) NULL);
319 for (p=utf8; *p != '\0'; p++)
320 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
325 String is binary, base64-encode it.
327 base64=Base64Encode(utf8,strlen((char *) utf8),&length);
328 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
329 if (base64 == (char *) NULL)
330 return((char *) NULL);
331 canonical_content=AcquireString("<base64>");
332 (void) ConcatenateString(&canonical_content,base64);
333 base64=DestroyString(base64);
334 (void) ConcatenateString(&canonical_content,"</base64>");
335 return(canonical_content);
338 Substitute predefined entities.
341 canonical_content=AcquireString((char *) NULL);
342 extent=MaxTextExtent;
343 for (p=utf8; *p != '\0'; p++)
345 if ((i+MaxTextExtent) > (ssize_t) extent)
347 extent+=MaxTextExtent;
348 canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
349 sizeof(*canonical_content));
350 if (canonical_content == (char *) NULL)
351 return(canonical_content);
357 i+=FormatLocaleString(canonical_content+i,extent,"&");
362 i+=FormatLocaleString(canonical_content+i,extent,"<");
367 i+=FormatLocaleString(canonical_content+i,extent,">");
372 i+=FormatLocaleString(canonical_content+i,extent,""");
377 if (pedantic == MagickFalse)
379 canonical_content[i++]=(char) (*p);
382 i+=FormatLocaleString(canonical_content+i,extent,"
");
387 if (pedantic == MagickFalse)
389 canonical_content[i++]=(char) (*p);
392 i+=FormatLocaleString(canonical_content+i,extent,"	");
397 i+=FormatLocaleString(canonical_content+i,extent,"
");
402 canonical_content[i++]=(char) (*p);
407 canonical_content[i]='\0';
408 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
409 return(canonical_content);
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % D e s t r o y X M L T r e e %
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 % DestroyXMLTree() destroys the xml-tree.
425 % The format of the DestroyXMLTree method is:
427 % XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
429 % A description of each parameter follows:
431 % o xml_info: the xml info.
435 static char **DestroyXMLTreeAttributes(char **attributes)
441 Destroy a tag attribute list.
443 if ((attributes == (char **) NULL) || (attributes == sentinel))
444 return((char **) NULL);
445 for (i=0; attributes[i] != (char *) NULL; i+=2)
448 Destroy attribute tag and value.
450 if (attributes[i] != (char *) NULL)
451 attributes[i]=DestroyString(attributes[i]);
452 if (attributes[i+1] != (char *) NULL)
453 attributes[i+1]=DestroyString(attributes[i+1]);
455 attributes=(char **) RelinquishMagickMemory(attributes);
456 return((char **) NULL);
459 MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
473 assert(xml_info != (XMLTreeInfo *) NULL);
474 assert((xml_info->signature == MagickSignature) ||
475 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
477 if (xml_info->child != (XMLTreeInfo *) NULL)
478 xml_info->child=DestroyXMLTree(xml_info->child);
479 if (xml_info->ordered != (XMLTreeInfo *) NULL)
480 xml_info->ordered=DestroyXMLTree(xml_info->ordered);
481 if (xml_info->parent == (XMLTreeInfo *) NULL)
484 Free root tag allocations.
486 root=(XMLTreeRoot *) xml_info;
487 for (i=NumberPredefinedEntities; root->entities[i]; i+=2)
488 root->entities[i+1]=DestroyString(root->entities[i+1]);
489 root->entities=(char **) RelinquishMagickMemory(root->entities);
490 for (i=0; root->attributes[i] != (char **) NULL; i++)
492 attributes=root->attributes[i];
493 if (attributes[0] != (char *) NULL)
494 attributes[0]=DestroyString(attributes[0]);
495 for (j=1; attributes[j] != (char *) NULL; j+=3)
497 if (attributes[j] != (char *) NULL)
498 attributes[j]=DestroyString(attributes[j]);
499 if (attributes[j+1] != (char *) NULL)
500 attributes[j+1]=DestroyString(attributes[j+1]);
501 if (attributes[j+2] != (char *) NULL)
502 attributes[j+2]=DestroyString(attributes[j+2]);
504 attributes=(char **) RelinquishMagickMemory(attributes);
506 if (root->attributes[0] != (char **) NULL)
507 root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
508 if (root->processing_instructions[0] != (char **) NULL)
510 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
512 for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
513 root->processing_instructions[i][j]=DestroyString(
514 root->processing_instructions[i][j]);
515 root->processing_instructions[i][j+1]=DestroyString(
516 root->processing_instructions[i][j+1]);
517 root->processing_instructions[i]=(char **) RelinquishMagickMemory(
518 root->processing_instructions[i]);
520 root->processing_instructions=(char ***) RelinquishMagickMemory(
521 root->processing_instructions);
524 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
525 xml_info->content=DestroyString(xml_info->content);
526 xml_info->tag=DestroyString(xml_info->tag);
527 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
528 return((XMLTreeInfo *) NULL);
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 % G e t N e x t X M L T r e e T a g %
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 % GetNextXMLTreeTag() returns the next tag or NULL if not found.
544 % The format of the GetNextXMLTreeTag method is:
546 % XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
548 % A description of each parameter follows:
550 % o xml_info: the xml info.
553 MagickPrivate XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
555 assert(xml_info != (XMLTreeInfo *) NULL);
556 assert((xml_info->signature == MagickSignature) ||
557 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
558 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
559 return(xml_info->next);
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % G e t X M L T r e e A t t r i b u t e %
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % GetXMLTreeAttribute() returns the value of the attribute tag with the
574 % specified tag if found, otherwise NULL.
576 % The format of the GetXMLTreeAttribute method is:
578 % const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
580 % A description of each parameter follows:
582 % o xml_info: the xml info.
584 % o tag: the attribute tag.
587 MagickPrivate const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
599 assert(xml_info != (XMLTreeInfo *) NULL);
600 assert((xml_info->signature == MagickSignature) ||
601 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
603 if (xml_info->attributes == (char **) NULL)
604 return((const char *) NULL);
606 while ((xml_info->attributes[i] != (char *) NULL) &&
607 (strcmp(xml_info->attributes[i],tag) != 0))
609 if (xml_info->attributes[i] != (char *) NULL)
610 return(xml_info->attributes[i+1]);
611 root=(XMLTreeRoot*) xml_info;
612 while (root->root.parent != (XMLTreeInfo *) NULL)
613 root=(XMLTreeRoot *) root->root.parent;
615 while ((root->attributes[i] != (char **) NULL) &&
616 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
618 if (root->attributes[i] == (char **) NULL)
619 return((const char *) NULL);
621 while ((root->attributes[i][j] != (char *) NULL) &&
622 (strcmp(root->attributes[i][j],tag) != 0))
624 if (root->attributes[i][j] == (char *) NULL)
625 return((const char *) NULL);
626 return(root->attributes[i][j+1]);
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % G e t X M L T r e e A t t r i b u t e s %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % GetXMLTreeAttributes() injects all attributes associated with the current
641 % tag in the specified splay-tree.
643 % The format of the GetXMLTreeAttributes method is:
645 % MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
646 % SplayTreeInfo *attributes)
648 % A description of each parameter follows:
650 % o xml_info: the xml info.
652 % o attributes: the attribute splay-tree.
655 MagickPrivate MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
656 SplayTreeInfo *attributes)
661 assert(xml_info != (XMLTreeInfo *) NULL);
662 assert((xml_info->signature == MagickSignature) ||
663 (((const XMLTreeRoot *) xml_info)->signature == MagickSignature));
664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
665 assert(attributes != (SplayTreeInfo *) NULL);
666 if (xml_info->attributes == (char **) NULL)
669 while (xml_info->attributes[i] != (char *) NULL)
671 (void) AddValueToSplayTree(attributes,
672 ConstantString(xml_info->attributes[i]),
673 ConstantString(xml_info->attributes[i+1]));
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % G e t X M L T r e e C h i l d %
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 % GetXMLTreeChild() returns the first child tag with the specified tag if
691 % found, otherwise NULL.
693 % The format of the GetXMLTreeChild method is:
695 % XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
697 % A description of each parameter follows:
699 % o xml_info: the xml info.
702 MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
707 assert(xml_info != (XMLTreeInfo *) NULL);
708 assert((xml_info->signature == MagickSignature) ||
709 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
711 child=xml_info->child;
712 if (tag != (const char *) NULL)
713 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
714 child=child->sibling;
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 % G e t X M L T r e e C o n t e n t %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % GetXMLTreeContent() returns any content associated with specified
732 % The format of the GetXMLTreeContent method is:
734 % const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
736 % A description of each parameter follows:
738 % o xml_info: the xml info.
741 MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
743 assert(xml_info != (XMLTreeInfo *) NULL);
744 assert((xml_info->signature == MagickSignature) ||
745 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
747 return(xml_info->content);
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % G e t X M L T r e e O r d e r e d %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
763 % The format of the GetXMLTreeOrdered method is:
765 % XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
767 % A description of each parameter follows:
769 % o xml_info: the xml info.
772 MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
774 assert(xml_info != (XMLTreeInfo *) NULL);
775 assert((xml_info->signature == MagickSignature) ||
776 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
778 return(xml_info->ordered);
782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 % G e t X M L T r e e P a t h %
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 % GetXMLTreePath() traverses the XML-tree as defined by the specified path
793 % and returns the node if found, otherwise NULL.
795 % The format of the GetXMLTreePath method is:
797 % XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
799 % A description of each parameter follows:
801 % o xml_info: the xml info.
803 % o path: the path (e.g. property/elapsed-time).
806 MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
810 subnode[MaxTextExtent],
825 assert(xml_info != (XMLTreeInfo *) NULL);
826 assert((xml_info->signature == MagickSignature) ||
827 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
830 components=GetPathComponents(path,&number_components);
831 if (components == (char **) NULL)
832 return((XMLTreeInfo *) NULL);
833 for (i=0; i < (ssize_t) number_components; i++)
835 GetPathComponent(components[i],SubimagePath,subnode);
836 GetPathComponent(components[i],CanonicalPath,tag);
837 node=GetXMLTreeChild(node,tag);
838 if (node == (XMLTreeInfo *) NULL)
840 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
842 node=GetXMLTreeOrdered(node);
843 if (node == (XMLTreeInfo *) NULL)
846 if (node == (XMLTreeInfo *) NULL)
848 components[i]=DestroyString(components[i]);
850 for ( ; i < (ssize_t) number_components; i++)
851 components[i]=DestroyString(components[i]);
852 components=(char **) RelinquishMagickMemory(components);
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % 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 %
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % GetXMLTreeProcessingInstructions() returns a null terminated array of
868 % processing instructions for the given target.
870 % The format of the GetXMLTreeProcessingInstructions method is:
872 % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
873 % const char *target)
875 % A description of each parameter follows:
877 % o xml_info: the xml info.
880 MagickPrivate const char **GetXMLTreeProcessingInstructions(
881 XMLTreeInfo *xml_info,const char *target)
889 assert(xml_info != (XMLTreeInfo *) NULL);
890 assert((xml_info->signature == MagickSignature) ||
891 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
893 root=(XMLTreeRoot *) xml_info;
894 while (root->root.parent != (XMLTreeInfo *) NULL)
895 root=(XMLTreeRoot *) root->root.parent;
897 while ((root->processing_instructions[i] != (char **) NULL) &&
898 (strcmp(root->processing_instructions[i][0],target) != 0))
900 if (root->processing_instructions[i] == (char **) NULL)
901 return((const char **) sentinel);
902 return((const char **) (root->processing_instructions[i]+1));
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 % G e t X M L T r e e S i b l i n g %
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
918 % The format of the GetXMLTreeSibling method is:
920 % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
922 % A description of each parameter follows:
924 % o xml_info: the xml info.
927 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
929 assert(xml_info != (XMLTreeInfo *) NULL);
930 assert((xml_info->signature == MagickSignature) ||
931 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
933 return(xml_info->sibling);
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 % G e t X M L T r e e T a g %
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947 % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
949 % The format of the GetXMLTreeTag method is:
951 % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
953 % A description of each parameter follows:
955 % o xml_info: the xml info.
958 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
960 assert(xml_info != (XMLTreeInfo *) NULL);
961 assert((xml_info->signature == MagickSignature) ||
962 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
964 return(xml_info->tag);
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % I n s e r t I n t o T a g X M L T r e e %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
979 % the parent tag's character content. This method returns the child tag.
981 % The format of the InsertTagIntoXMLTree method is:
983 % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
984 % XMLTreeInfo *child,const size_t offset)
986 % A description of each parameter follows:
988 % o xml_info: the xml info.
990 % o child: the child tag.
992 % o offset: the tag offset.
995 MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
996 XMLTreeInfo *child,const size_t offset)
1003 child->ordered=(XMLTreeInfo *) NULL;
1004 child->sibling=(XMLTreeInfo *) NULL;
1005 child->next=(XMLTreeInfo *) NULL;
1006 child->offset=offset;
1007 child->parent=xml_info;
1008 if (xml_info->child == (XMLTreeInfo *) NULL)
1010 xml_info->child=child;
1013 head=xml_info->child;
1014 if (head->offset > offset)
1016 child->ordered=head;
1017 xml_info->child=child;
1022 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1023 (node->ordered->offset <= offset))
1025 child->ordered=node->ordered;
1026 node->ordered=child;
1028 previous=(XMLTreeInfo *) NULL;
1030 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1035 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1037 while ((node->next != (XMLTreeInfo *) NULL) &&
1038 (node->next->offset <= offset))
1040 child->next=node->next;
1045 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1046 previous->sibling=node->sibling;
1048 previous=(XMLTreeInfo *) NULL;
1050 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1055 child->sibling=node;
1056 if (previous != (XMLTreeInfo *) NULL)
1057 previous->sibling=child;
1063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1067 % N e w X M L T r e e %
1071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1076 % The format of the NewXMLTree method is:
1078 % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1080 % A description of each parameter follows:
1082 % o xml: The XML string.
1084 % o exception: return any errors or warnings in this structure.
1088 static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
1108 utf8=(char *) AcquireQuantumMemory(*length,sizeof(*utf8));
1109 if (utf8 == (char *) NULL)
1110 return((char *) NULL);
1111 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
1117 (void) CopyMagickMemory(utf8,content,*length*sizeof(*utf8));
1122 for (i=2; i < (ssize_t) (*length-1); i+=2)
1124 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1125 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1126 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1128 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1129 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1130 (content[i] & 0xff);
1131 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1133 if ((size_t) (j+MaxTextExtent) > extent)
1135 extent=(size_t) j+MaxTextExtent;
1136 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1137 if (utf8 == (char *) NULL)
1147 Multi-byte UTF-8 sequence.
1150 for (bits=0; byte != 0; byte/=2)
1153 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1157 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1162 return((char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8)));
1165 static char *ParseEntities(char *xml,char **entities,int state)
1189 Normalize line endings.
1193 for ( ; *xml != '\0'; xml++)
1194 while (*xml == '\r')
1198 (void) CopyMagickMemory(xml,xml+1,strlen(xml));
1202 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1203 (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1209 '&' for general entity decoding
1210 '%' for parameter entity decoding
1211 'c' for CDATA sections
1212 ' ' for attributes normalization
1213 '*' for non-CDATA attributes normalization
1215 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1218 Character reference.
1221 c=strtol(xml+2,&entity,10); /* base 10 */
1223 c=strtol(xml+3,&entity,16); /* base 16 */
1224 if ((c == 0) || (*entity != ';'))
1227 Not a character reference.
1237 Multi-byte UTF-8 sequence.
1240 for (i=0; byte != 0; byte/=2)
1243 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1248 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1252 (void) CopyMagickMemory(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1255 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1256 (state == '*'))) || ((state == '%') && (*xml == '%')))
1259 Find entity in the list.
1262 while ((entities[i] != (char *) NULL) &&
1263 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1265 if (entities[i++] == (char *) NULL)
1272 length=strlen(entities[i]);
1273 entity=strchr(xml,';');
1274 if ((length-1L) >= (size_t) (entity-xml))
1276 offset=(ssize_t) (xml-p);
1277 extent=(size_t) (offset+length+strlen(entity));
1279 p=(char *) ResizeQuantumMemory(p,extent,sizeof(*p));
1285 xml=(char *) AcquireQuantumMemory(extent,sizeof(*xml));
1286 if (xml != (char *) NULL)
1288 (void) CopyMagickString(xml,p,extent*sizeof(*xml));
1292 if (p == (char *) NULL)
1293 ThrowFatalException(ResourceLimitFatalError,
1294 "MemoryAllocationFailed");
1296 entity=strchr(xml,';');
1298 (void) CopyMagickMemory(xml+length,entity+1,strlen(entity));
1299 (void) strncpy(xml,entities[i],length);
1303 if (((state == ' ') || (state == '*')) &&
1304 (isspace((int) ((unsigned char) *xml) != 0)))
1312 Normalize spaces for non-CDATA attributes.
1314 for (xml=p; *xml != '\0'; xml++)
1316 i=(ssize_t) strspn(xml," ");
1318 (void) CopyMagickMemory(xml,xml+i,strlen(xml+i)+1);
1319 while ((*xml != '\0') && (*xml != ' '))
1323 if ((xml >= p) && (*xml == ' '))
1326 return(p == q ? ConstantString(p) : p);
1329 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1330 const size_t length,const char state)
1335 xml_info=root->node;
1336 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1340 xml=ParseEntities(xml,root->entities,state);
1341 if (*xml_info->content != '\0')
1343 (void) ConcatenateString(&xml_info->content,xml);
1344 xml=DestroyString(xml);
1348 if (xml_info->content != (char *) NULL)
1349 xml_info->content=DestroyString(xml_info->content);
1350 xml_info->content=xml;
1354 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1355 char *magick_unused(xml),ExceptionInfo *exception)
1357 if ((root->node == (XMLTreeInfo *) NULL) ||
1358 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1360 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1361 "ParseError","unexpected closing tag </%s>",tag);
1362 return(&root->root);
1364 root->node=root->node->parent;
1365 return((XMLTreeInfo *) NULL);
1368 static MagickBooleanType ValidateEntities(char *tag,char *xml,char **entities)
1374 Check for circular entity references.
1378 while ((*xml != '\0') && (*xml != '&'))
1382 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1383 return(MagickFalse);
1385 while ((entities[i] != (char *) NULL) &&
1386 (strncmp(entities[i],xml+1,strlen(entities[i]) == 0)))
1388 if ((entities[i] != (char *) NULL) &&
1389 (ValidateEntities(tag,entities[i+1],entities) == 0))
1390 return(MagickFalse);
1395 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1409 xml+=strcspn(xml,XMLWhitespace);
1413 xml+=strspn(xml+1,XMLWhitespace)+1;
1415 if (strcmp(target,"xml") == 0)
1417 xml=strstr(xml,"standalone");
1418 if ((xml != (char *) NULL) &&
1419 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1420 root->standalone=MagickTrue;
1423 if (root->processing_instructions[0] == (char **) NULL)
1425 root->processing_instructions=(char ***) AcquireMagickMemory(sizeof(
1426 *root->processing_instructions));
1427 if (root->processing_instructions ==(char ***) NULL)
1428 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1429 *root->processing_instructions=(char **) NULL;
1432 while ((root->processing_instructions[i] != (char **) NULL) &&
1433 (strcmp(target,root->processing_instructions[i][0]) != 0))
1435 if (root->processing_instructions[i] == (char **) NULL)
1437 root->processing_instructions=(char ***) ResizeQuantumMemory(
1438 root->processing_instructions,(size_t) (i+2),
1439 sizeof(*root->processing_instructions));
1440 if (root->processing_instructions == (char ***) NULL)
1441 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1442 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1443 sizeof(**root->processing_instructions));
1444 if (root->processing_instructions[i] == (char **) NULL)
1445 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1446 root->processing_instructions[i+1]=(char **) NULL;
1447 root->processing_instructions[i][0]=ConstantString(target);
1448 root->processing_instructions[i][1]=(char *)
1449 root->processing_instructions[i+1];
1450 root->processing_instructions[i+1]=(char **) NULL;
1451 root->processing_instructions[i][2]=ConstantString("");
1454 while (root->processing_instructions[i][j] != (char *) NULL)
1456 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1457 root->processing_instructions[i],(size_t) (j+3),
1458 sizeof(**root->processing_instructions));
1459 if (root->processing_instructions[i] == (char **) NULL)
1460 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1461 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1462 root->processing_instructions[i][j+1],(size_t) (j+1),
1463 sizeof(**root->processing_instructions));
1464 if (root->processing_instructions[i][j+2] == (char *) NULL)
1465 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1466 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1467 root->root.tag != (char *) NULL ? ">" : "<",2);
1468 root->processing_instructions[i][j]=ConstantString(xml);
1469 root->processing_instructions[i][j+1]=(char *) NULL;
1472 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1473 size_t length,ExceptionInfo *exception)
1479 **predefined_entitites,
1491 predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1492 if (predefined_entitites == (char **) NULL)
1493 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1494 (void) CopyMagickMemory(predefined_entitites,sentinel,sizeof(sentinel));
1495 for (xml[length]='\0'; xml != (char *) NULL; )
1497 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1501 if (strncmp(xml,"<!ENTITY",8) == 0)
1504 Parse entity definitions.
1506 xml+=strspn(xml+8,XMLWhitespace)+8;
1508 n=xml+strspn(xml,XMLWhitespace "%");
1509 xml=n+strcspn(n,XMLWhitespace);
1511 v=xml+strspn(xml+1,XMLWhitespace)+1;
1514 if ((q != '"') && (q != '\''))
1519 xml=strchr(xml,'>');
1522 entities=(*c == '%') ? predefined_entitites : root->entities;
1523 for (i=0; entities[i] != (char *) NULL; i++) ;
1524 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1526 if (entities == (char **) NULL)
1527 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1529 predefined_entitites=entities;
1531 root->entities=entities;
1535 if (xml != (char *) NULL)
1540 entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1541 entities[i+2]=(char *) NULL;
1542 if (ValidateEntities(n,entities[i+1],entities) != MagickFalse)
1546 if (entities[i+1] != v)
1547 entities[i+1]=DestroyString(entities[i+1]);
1548 (void) ThrowMagickException(exception,GetMagickModule(),
1549 OptionWarning,"ParseError","circular entity declaration &%s",n);
1550 predefined_entitites=(char **) RelinquishMagickMemory(
1551 predefined_entitites);
1552 return(MagickFalse);
1556 if (strncmp(xml,"<!ATTLIST",9) == 0)
1559 Parse default attributes.
1561 t=xml+strspn(xml+9,XMLWhitespace)+9;
1564 (void) ThrowMagickException(exception,GetMagickModule(),
1565 OptionWarning,"ParseError","unclosed <!ATTLIST");
1566 predefined_entitites=(char **) RelinquishMagickMemory(
1567 predefined_entitites);
1568 return(MagickFalse);
1570 xml=t+strcspn(t,XMLWhitespace ">");
1575 while ((root->attributes[i] != (char **) NULL) &&
1576 (strcmp(n,root->attributes[i][0]) != 0))
1578 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1581 xml=n+strcspn(n,XMLWhitespace);
1586 (void) ThrowMagickException(exception,GetMagickModule(),
1587 OptionWarning,"ParseError","malformed <!ATTLIST");
1588 predefined_entitites=(char **) RelinquishMagickMemory(
1589 predefined_entitites);
1590 return(MagickFalse);
1592 xml+=strspn(xml+1,XMLWhitespace)+1;
1593 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1594 if (strncmp(xml,"NOTATION",8) == 0)
1595 xml+=strspn(xml+8,XMLWhitespace)+8;
1596 xml=(*xml == '(') ? strchr(xml,')') : xml+
1597 strcspn(xml,XMLWhitespace);
1598 if (xml == (char *) NULL)
1600 (void) ThrowMagickException(exception,GetMagickModule(),
1601 OptionWarning,"ParseError","malformed <!ATTLIST");
1602 predefined_entitites=(char **) RelinquishMagickMemory(
1603 predefined_entitites);
1604 return(MagickFalse);
1606 xml+=strspn(xml,XMLWhitespace ")");
1607 if (strncmp(xml,"#FIXED",6) == 0)
1608 xml+=strspn(xml+6,XMLWhitespace)+6;
1611 xml+=strcspn(xml,XMLWhitespace ">")-1;
1617 if (((*xml == '"') || (*xml == '\'')) &&
1618 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1622 (void) ThrowMagickException(exception,GetMagickModule(),
1623 OptionWarning,"ParseError","malformed <!ATTLIST");
1624 predefined_entitites=(char **) RelinquishMagickMemory(
1625 predefined_entitites);
1626 return(MagickFalse);
1628 if (root->attributes[i] == (char **) NULL)
1634 root->attributes=(char ***) AcquireQuantumMemory(2,
1635 sizeof(*root->attributes));
1637 root->attributes=(char ***) ResizeQuantumMemory(
1638 root->attributes,(size_t) (i+2),
1639 sizeof(*root->attributes));
1640 if (root->attributes == (char ***) NULL)
1641 ThrowFatalException(ResourceLimitFatalError,
1642 "MemoryAllocationFailed");
1643 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1644 sizeof(*root->attributes));
1645 if (root->attributes[i] == (char **) NULL)
1646 ThrowFatalException(ResourceLimitFatalError,
1647 "MemoryAllocationFailed");
1648 root->attributes[i][0]=ConstantString(t);
1649 root->attributes[i][1]=(char *) NULL;
1650 root->attributes[i+1]=(char **) NULL;
1652 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1653 root->attributes[i]=(char **) ResizeQuantumMemory(
1654 root->attributes[i],(size_t) (j+4),sizeof(*root->attributes));
1655 if (root->attributes[i] == (char **) NULL)
1656 ThrowFatalException(ResourceLimitFatalError,
1657 "MemoryAllocationFailed");
1658 root->attributes[i][j+3]=(char *) NULL;
1659 root->attributes[i][j+2]=ConstantString(c);
1660 root->attributes[i][j+1]=(char *) NULL;
1661 if (v != (char *) NULL)
1662 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1663 root->attributes[i][j]=ConstantString(n);
1667 if (strncmp(xml, "<!--", 4) == 0)
1668 xml=strstr(xml+4,"-->");
1670 if (strncmp(xml,"<?", 2) == 0)
1674 if (xml != (char *) NULL)
1676 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1682 xml=strchr(xml,'>');
1684 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1687 predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1691 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1696 xml_info=root->node;
1697 if (xml_info->tag == (char *) NULL)
1698 xml_info->tag=ConstantString(tag);
1700 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1701 xml_info->attributes=attributes;
1702 root->node=xml_info;
1705 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1737 Convert xml-string to UTF8.
1739 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1741 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1742 "ParseError","root tag missing");
1743 return((XMLTreeInfo *) NULL);
1745 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1747 utf8=ConvertUTF16ToUTF8(xml,&length);
1748 if (utf8 == (char *) NULL)
1750 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1751 "ParseError","UTF16 to UTF8 failed");
1752 return((XMLTreeInfo *) NULL);
1754 terminal=utf8[length-1];
1755 utf8[length-1]='\0';
1757 while ((*p != '\0') && (*p != '<'))
1761 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1762 "ParseError","root tag missing");
1763 utf8=DestroyString(utf8);
1764 return((XMLTreeInfo *) NULL);
1766 attribute=(char **) NULL;
1769 attributes=(char **) sentinel;
1772 if ((isalpha((int) ((unsigned char) *p)) !=0) || (*p == '_') ||
1773 (*p == ':') || (c < '\0'))
1778 if (root->node == (XMLTreeInfo *) NULL)
1780 (void) ThrowMagickException(exception,GetMagickModule(),
1781 OptionWarning,"ParseError","root tag missing");
1782 utf8=DestroyString(utf8);
1783 return(&root->root);
1785 p+=strcspn(p,XMLWhitespace "/>");
1786 while (isspace((int) ((unsigned char) *p)) != 0)
1788 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1791 Find tag in default attributes list.
1794 while ((root->attributes[i] != (char **) NULL) &&
1795 (strcmp(root->attributes[i][0],tag) != 0))
1797 attribute=root->attributes[i];
1799 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1805 attributes=(char **) AcquireQuantumMemory(4,sizeof(*attributes));
1807 attributes=(char **) ResizeQuantumMemory(attributes,(size_t) (l+4),
1808 sizeof(*attributes));
1809 if (attributes == (char **) NULL)
1811 (void) ThrowMagickException(exception,GetMagickModule(),
1812 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1813 utf8=DestroyString(utf8);
1814 return(&root->root);
1816 attributes[l+2]=(char *) NULL;
1817 attributes[l+1]=(char *) NULL;
1819 p+=strcspn(p,XMLWhitespace "=/>");
1820 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1821 attributes[l]=ConstantString("");
1825 p+=strspn(p,XMLWhitespace "=");
1827 if ((c == '"') || (c == '\''))
1834 while ((*p != '\0') && (*p != c))
1840 attributes[l]=ConstantString("");
1841 attributes[l+1]=ConstantString("");
1842 (void) DestroyXMLTreeAttributes(attributes);
1843 (void) ThrowMagickException(exception,GetMagickModule(),
1844 OptionWarning,"ParseError","missing %c",c);
1845 utf8=DestroyString(utf8);
1846 return(&root->root);
1849 while ((attribute != (char **) NULL) &&
1850 (attribute[j] != (char *) NULL) &&
1851 (strcmp(attribute[j],attributes[l]) != 0))
1853 attributes[l+1]=ParseEntities(attributes[l+1],root->entities,
1854 (attribute != (char **) NULL) && (attribute[j] !=
1855 (char *) NULL) ? *attribute[j+2] : ' ');
1857 attributes[l]=ConstantString(attributes[l]);
1859 while (isspace((int) ((unsigned char) *p)) != 0)
1868 if (((*p != '\0') && (*p != '>')) ||
1869 ((*p == '\0') && (terminal != '>')))
1872 (void) DestroyXMLTreeAttributes(attributes);
1873 (void) ThrowMagickException(exception,GetMagickModule(),
1874 OptionWarning,"ParseError","missing >");
1875 utf8=DestroyString(utf8);
1876 return(&root->root);
1878 ParseOpenTag(root,tag,attributes);
1879 (void) ParseCloseTag(root,tag,p,exception);
1884 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1887 ParseOpenTag(root,tag,attributes);
1893 (void) DestroyXMLTreeAttributes(attributes);
1894 (void) ThrowMagickException(exception,GetMagickModule(),
1895 OptionWarning,"ParseError","missing >");
1896 utf8=DestroyString(utf8);
1897 return(&root->root);
1908 p+=strcspn(tag,XMLWhitespace ">")+1;
1910 if ((c == '\0') && (terminal != '>'))
1912 (void) ThrowMagickException(exception,GetMagickModule(),
1913 OptionWarning,"ParseError","missing >");
1914 utf8=DestroyString(utf8);
1915 return(&root->root);
1918 if (ParseCloseTag(root,tag,p,exception) != (XMLTreeInfo *) NULL)
1920 utf8=DestroyString(utf8);
1921 return(&root->root);
1924 if (isspace((int) ((unsigned char) *p)) != 0)
1925 p+=strspn(p,XMLWhitespace);
1928 if (strncmp(p,"!--",3) == 0)
1934 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1935 ((*p == '\0') && (terminal != '>')))
1937 (void) ThrowMagickException(exception,GetMagickModule(),
1938 OptionWarning,"ParseError","unclosed <!--");
1939 utf8=DestroyString(utf8);
1940 return(&root->root);
1944 if (strncmp(p,"![CDATA[",8) == 0)
1950 if (p != (char *) NULL)
1953 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1957 (void) ThrowMagickException(exception,GetMagickModule(),
1958 OptionWarning,"ParseError","unclosed <![CDATA[");
1959 utf8=DestroyString(utf8);
1960 return(&root->root);
1964 if (strncmp(p,"!DOCTYPE",8) == 0)
1969 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1970 ((l != 0) && ((*p != ']') ||
1971 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1972 l=(ssize_t) ((*p == '[') ? 1 : l))
1973 p+=strcspn(p+1,"[]>")+1;
1974 if ((*p == '\0') && (terminal != '>'))
1976 (void) ThrowMagickException(exception,GetMagickModule(),
1977 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1978 utf8=DestroyString(utf8);
1979 return(&root->root);
1982 tag=strchr(tag,'[')+1;
1985 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1987 if (status == MagickFalse)
1989 utf8=DestroyString(utf8);
1990 return(&root->root);
1999 Processing instructions.
2004 if (p == (char *) NULL)
2007 } while ((*p != '\0') && (*p != '>'));
2008 if ((p == (char *) NULL) || ((*p == '\0') &&
2011 (void) ThrowMagickException(exception,GetMagickModule(),
2012 OptionWarning,"ParseError","unclosed <?");
2013 utf8=DestroyString(utf8);
2014 return(&root->root);
2016 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2020 (void) ThrowMagickException(exception,GetMagickModule(),
2021 OptionWarning,"ParseError","unexpected <");
2022 utf8=DestroyString(utf8);
2023 return(&root->root);
2025 if ((p == (char *) NULL) || (*p == '\0'))
2029 if ((*p != '\0') && (*p != '<'))
2032 Tag character content.
2034 while ((*p != '\0') && (*p != '<'))
2038 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2044 utf8=DestroyString(utf8);
2045 if (root->node == (XMLTreeInfo *) NULL)
2046 return(&root->root);
2047 if (root->node->tag == (char *) NULL)
2049 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2050 "ParseError","root tag missing");
2051 return(&root->root);
2053 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2054 "ParseError","unclosed tag: `%s'",root->node->tag);
2055 return(&root->root);
2059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2063 % N e w X M L T r e e T a g %
2067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069 % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2071 % The format of the NewXMLTreeTag method is:
2073 % XMLTreeInfo *NewXMLTreeTag(const char *tag)
2075 % A description of each parameter follows:
2080 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2083 *predefined_entities[NumberPredefinedEntities+1] =
2085 "lt;", "<", "gt;", ">", "quot;", """,
2086 "apos;", "'", "amp;", "&", (char *) NULL
2092 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2093 if (root == (XMLTreeRoot *) NULL)
2094 return((XMLTreeInfo *) NULL);
2095 (void) ResetMagickMemory(root,0,sizeof(*root));
2096 root->root.tag=(char *) NULL;
2097 if (tag != (char *) NULL)
2098 root->root.tag=ConstantString(tag);
2099 root->node=(&root->root);
2100 root->root.content=ConstantString("");
2101 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2102 if (root->entities == (char **) NULL)
2103 return((XMLTreeInfo *) NULL);
2104 (void) CopyMagickMemory(root->entities,predefined_entities,
2105 sizeof(predefined_entities));
2106 root->root.attributes=sentinel;
2107 root->attributes=(char ***) root->root.attributes;
2108 root->processing_instructions=(char ***) root->root.attributes;
2109 root->debug=IsEventLogging();
2110 root->signature=MagickSignature;
2111 return(&root->root);
2115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119 % P r u n e T a g F r o m X M L T r e e %
2123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2125 % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2128 % The format of the PruneTagFromXMLTree method is:
2130 % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2132 % A description of each parameter follows:
2134 % o xml_info: the xml info.
2137 MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2142 assert(xml_info != (XMLTreeInfo *) NULL);
2143 assert((xml_info->signature == MagickSignature) ||
2144 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2145 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2146 if (xml_info->next != (XMLTreeInfo *) NULL)
2147 xml_info->next->sibling=xml_info->sibling;
2148 if (xml_info->parent != (XMLTreeInfo *) NULL)
2150 node=xml_info->parent->child;
2151 if (node == xml_info)
2152 xml_info->parent->child=xml_info->ordered;
2155 while (node->ordered != xml_info)
2157 node->ordered=node->ordered->ordered;
2158 node=xml_info->parent->child;
2159 if (strcmp(node->tag,xml_info->tag) != 0)
2161 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2163 if (node->sibling != xml_info)
2166 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2167 xml_info->next : node->sibling->sibling;
2169 while ((node->next != (XMLTreeInfo *) NULL) &&
2170 (node->next != xml_info))
2172 if (node->next != (XMLTreeInfo *) NULL)
2173 node->next=node->next->next;
2176 xml_info->ordered=(XMLTreeInfo *) NULL;
2177 xml_info->sibling=(XMLTreeInfo *) NULL;
2178 xml_info->next=(XMLTreeInfo *) NULL;
2183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2187 % S e t X M L T r e e A t t r i b u t e %
2191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2193 % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2194 % found. A value of NULL removes the specified attribute.
2196 % The format of the SetXMLTreeAttribute method is:
2198 % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2199 % const char *value)
2201 % A description of each parameter follows:
2203 % o xml_info: the xml info.
2205 % o tag: The attribute tag.
2207 % o value: The attribute value.
2210 MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2211 const char *tag,const char *value)
2219 assert(xml_info != (XMLTreeInfo *) NULL);
2220 assert((xml_info->signature == MagickSignature) ||
2221 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2222 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2224 while ((xml_info->attributes[i] != (char *) NULL) &&
2225 (strcmp(xml_info->attributes[i],tag) != 0))
2227 if (xml_info->attributes[i] == (char *) NULL)
2230 Add new attribute tag.
2232 if (value == (const char *) NULL)
2234 if (xml_info->attributes != sentinel)
2235 xml_info->attributes=(char **) ResizeQuantumMemory(
2236 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2239 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2240 sizeof(*xml_info->attributes));
2241 if (xml_info->attributes != (char **) NULL)
2242 xml_info->attributes[1]=ConstantString("");
2244 if (xml_info->attributes == (char **) NULL)
2245 ThrowFatalException(ResourceLimitFatalError,
2246 "UnableToAcquireString");
2247 xml_info->attributes[i]=ConstantString(tag);
2248 xml_info->attributes[i+2]=(char *) NULL;
2249 (void) strlen(xml_info->attributes[i+1]);
2252 Add new value to an existing attribute.
2254 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2255 if (xml_info->attributes[i+1] != (char *) NULL)
2256 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2257 if (value != (const char *) NULL)
2259 xml_info->attributes[i+1]=ConstantString(value);
2262 if (xml_info->attributes[i] != (char *) NULL)
2263 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2264 (void) CopyMagickMemory(xml_info->attributes+i,xml_info->attributes+i+2,
2265 (size_t) (j-i)*sizeof(*xml_info->attributes));
2267 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2268 (size_t) (j+2),sizeof(*xml_info->attributes));
2269 if (xml_info->attributes == (char **) NULL)
2270 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2271 (void) CopyMagickMemory(xml_info->attributes[j+1]+(i/2),
2272 xml_info->attributes[j+1]+(i/2)+1,(size_t) ((j/2)-(i/2))*
2273 sizeof(*xml_info->attributes));
2278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2282 % S e t X M L T r e e C o n t e n t %
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288 % SetXMLTreeContent() sets the character content for the given tag and
2291 % The format of the SetXMLTreeContent method is:
2293 % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2294 % const char *content)
2296 % A description of each parameter follows:
2298 % o xml_info: the xml info.
2300 % o content: The content.
2303 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2304 const char *content)
2306 assert(xml_info != (XMLTreeInfo *) NULL);
2307 assert((xml_info->signature == MagickSignature) ||
2308 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2310 if (xml_info->content != (char *) NULL)
2311 xml_info->content=DestroyString(xml_info->content);
2312 xml_info->content=(char *) ConstantString(content);
2317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2321 % X M L T r e e I n f o T o X M L %
2325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327 % XMLTreeInfoToXML() converts an xml-tree to an XML string.
2329 % The format of the XMLTreeInfoToXML method is:
2331 % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2333 % A description of each parameter follows:
2335 % o xml_info: the xml info.
2339 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2340 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2346 canonical_content=CanonicalXMLContent(source,pedantic);
2352 content=AcquireString(source);
2353 content[offset]='\0';
2354 canonical_content=CanonicalXMLContent(content,pedantic);
2355 content=DestroyString(content);
2357 if (canonical_content == (char *) NULL)
2358 return(*destination);
2359 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2361 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2362 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2363 sizeof(**destination));
2364 if (*destination == (char *) NULL)
2365 return(*destination);
2367 *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2369 canonical_content=DestroyString(canonical_content);
2370 return(*destination);
2373 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2374 size_t *extent,size_t start,char ***attributes)
2391 content=(char *) "";
2392 if (xml_info->parent != (XMLTreeInfo *) NULL)
2393 content=xml_info->parent->content;
2395 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2396 start),source,length,extent,MagickFalse);
2397 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2399 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2400 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(*source));
2401 if (*source == (char *) NULL)
2404 *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2405 for (i=0; xml_info->attributes[i]; i+=2)
2407 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2408 if (attribute != xml_info->attributes[i+1])
2410 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2412 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2413 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2414 if (*source == (char *) NULL)
2415 return((char *) NULL);
2417 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2418 xml_info->attributes[i]);
2419 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2421 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2424 while ((attributes[i] != (char **) NULL) &&
2425 (strcmp(attributes[i][0],xml_info->tag) != 0))
2428 while ((attributes[i] != (char **) NULL) &&
2429 (attributes[i][j] != (char *) NULL))
2431 if ((attributes[i][j+1] == (char *) NULL) ||
2432 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2437 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2439 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2440 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2441 if (*source == (char *) NULL)
2442 return((char *) NULL);
2444 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2446 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2448 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2451 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2453 if (xml_info->child != (XMLTreeInfo *) NULL)
2454 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2456 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2458 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2460 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2461 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2462 if (*source == (char *) NULL)
2463 return((char *) NULL);
2465 if (*xml_info->content != '\0')
2466 *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2468 while ((content[offset] != '\0') && (offset < xml_info->offset))
2470 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2471 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2474 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2479 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2506 assert(xml_info != (XMLTreeInfo *) NULL);
2507 assert((xml_info->signature == MagickSignature) ||
2508 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2510 if (xml_info->tag == (char *) NULL)
2511 return((char *) NULL);
2512 xml=AcquireString((char *) NULL);
2514 extent=MaxTextExtent;
2515 root=(XMLTreeRoot *) xml_info;
2516 while (root->root.parent != (XMLTreeInfo *) NULL)
2517 root=(XMLTreeRoot *) root->root.parent;
2518 parent=(XMLTreeInfo *) NULL;
2519 if (xml_info != (XMLTreeInfo *) NULL)
2520 parent=xml_info->parent;
2521 if (parent == (XMLTreeInfo *) NULL)
2522 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2525 Pre-root processing instructions.
2527 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2528 p=root->processing_instructions[i][1];
2529 for (j=1; p != (char *) NULL; j++)
2531 if (root->processing_instructions[i][k][j-1] == '>')
2533 p=root->processing_instructions[i][j];
2536 q=root->processing_instructions[i][0];
2537 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2539 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2540 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2541 if (xml == (char *) NULL)
2544 length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2545 *p != '\0' ? " " : "",p);
2546 p=root->processing_instructions[i][j];
2549 ordered=(XMLTreeInfo *) NULL;
2550 if (xml_info != (XMLTreeInfo *) NULL)
2551 ordered=xml_info->ordered;
2552 xml_info->parent=(XMLTreeInfo *) NULL;
2553 xml_info->ordered=(XMLTreeInfo *) NULL;
2554 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2555 xml_info->parent=parent;
2556 xml_info->ordered=ordered;
2557 if (parent == (XMLTreeInfo *) NULL)
2558 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2561 Post-root processing instructions.
2563 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2564 p=root->processing_instructions[i][1];
2565 for (j=1; p != (char *) NULL; j++)
2567 if (root->processing_instructions[i][k][j-1] == '<')
2569 p=root->processing_instructions[i][j];
2572 q=root->processing_instructions[i][0];
2573 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2575 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2576 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2577 if (xml == (char *) NULL)
2580 length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2581 *p != '\0' ? " " : "",p);
2582 p=root->processing_instructions[i][j];
2585 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));