2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % TTTTT RRRR EEEEE EEEEE %
26 % Copyright 1999-2014 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 static void DestroyXMLTreeRoot(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->parent != (XMLTreeInfo *) NULL)
480 Free root tag allocations.
482 root=(XMLTreeRoot *) xml_info;
483 for (i=NumberPredefinedEntities; root->entities[i] != (char *) NULL; i+=2)
484 root->entities[i+1]=DestroyString(root->entities[i+1]);
485 root->entities=(char **) RelinquishMagickMemory(root->entities);
486 for (i=0; root->attributes[i] != (char **) NULL; i++)
488 attributes=root->attributes[i];
489 if (attributes[0] != (char *) NULL)
490 attributes[0]=DestroyString(attributes[0]);
491 for (j=1; attributes[j] != (char *) NULL; j+=3)
493 if (attributes[j] != (char *) NULL)
494 attributes[j]=DestroyString(attributes[j]);
495 if (attributes[j+1] != (char *) NULL)
496 attributes[j+1]=DestroyString(attributes[j+1]);
497 if (attributes[j+2] != (char *) NULL)
498 attributes[j+2]=DestroyString(attributes[j+2]);
500 attributes=(char **) RelinquishMagickMemory(attributes);
502 if (root->attributes[0] != (char **) NULL)
503 root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
504 if (root->processing_instructions[0] != (char **) NULL)
506 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
508 for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
509 root->processing_instructions[i][j]=DestroyString(
510 root->processing_instructions[i][j]);
511 root->processing_instructions[i][j+1]=DestroyString(
512 root->processing_instructions[i][j+1]);
513 root->processing_instructions[i]=(char **) RelinquishMagickMemory(
514 root->processing_instructions[i]);
516 root->processing_instructions=(char ***) RelinquishMagickMemory(
517 root->processing_instructions);
521 MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
535 assert(xml_info != (XMLTreeInfo *) NULL);
536 assert((xml_info->signature == MagickSignature) ||
537 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
538 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
539 if (xml_info->child != (XMLTreeInfo *) NULL)
540 xml_info->child=DestroyXMLTree(xml_info->child);
541 if (xml_info->ordered != (XMLTreeInfo *) NULL)
542 xml_info->ordered=DestroyXMLTree(xml_info->ordered);
543 DestroyXMLTreeRoot(xml_info);
544 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
545 xml_info->content=DestroyString(xml_info->content);
546 xml_info->tag=DestroyString(xml_info->tag);
547 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
548 return((XMLTreeInfo *) NULL);
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % G e t N e x t X M L T r e e T a g %
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % GetNextXMLTreeTag() returns the next tag or NULL if not found.
564 % The format of the GetNextXMLTreeTag method is:
566 % XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
568 % A description of each parameter follows:
570 % o xml_info: the xml info.
573 MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
575 assert(xml_info != (XMLTreeInfo *) NULL);
576 assert((xml_info->signature == MagickSignature) ||
577 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
579 return(xml_info->next);
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 % G e t X M L T r e e A t t r i b u t e %
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 % GetXMLTreeAttribute() returns the value of the attribute tag with the
594 % specified tag if found, otherwise NULL.
596 % The format of the GetXMLTreeAttribute method is:
598 % const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
600 % A description of each parameter follows:
602 % o xml_info: the xml info.
604 % o tag: the attribute tag.
607 MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
619 assert(xml_info != (XMLTreeInfo *) NULL);
620 assert((xml_info->signature == MagickSignature) ||
621 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
623 if (xml_info->attributes == (char **) NULL)
624 return((const char *) NULL);
626 while ((xml_info->attributes[i] != (char *) NULL) &&
627 (strcmp(xml_info->attributes[i],tag) != 0))
629 if (xml_info->attributes[i] != (char *) NULL)
630 return(xml_info->attributes[i+1]);
631 root=(XMLTreeRoot*) xml_info;
632 while (root->root.parent != (XMLTreeInfo *) NULL)
633 root=(XMLTreeRoot *) root->root.parent;
635 while ((root->attributes[i] != (char **) NULL) &&
636 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
638 if (root->attributes[i] == (char **) NULL)
639 return((const char *) NULL);
641 while ((root->attributes[i][j] != (char *) NULL) &&
642 (strcmp(root->attributes[i][j],tag) != 0))
644 if (root->attributes[i][j] == (char *) NULL)
645 return((const char *) NULL);
646 return(root->attributes[i][j+1]);
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % G e t X M L T r e e A t t r i b u t e s %
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 % GetXMLTreeAttributes() injects all attributes associated with the current
661 % tag in the specified splay-tree.
663 % The format of the GetXMLTreeAttributes method is:
665 % MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
666 % SplayTreeInfo *attributes)
668 % A description of each parameter follows:
670 % o xml_info: the xml info.
672 % o attributes: the attribute splay-tree.
675 MagickPrivate MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
676 SplayTreeInfo *attributes)
681 assert(xml_info != (XMLTreeInfo *) NULL);
682 assert((xml_info->signature == MagickSignature) ||
683 (((const XMLTreeRoot *) xml_info)->signature == MagickSignature));
684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
685 assert(attributes != (SplayTreeInfo *) NULL);
686 if (xml_info->attributes == (char **) NULL)
689 while (xml_info->attributes[i] != (char *) NULL)
691 (void) AddValueToSplayTree(attributes,
692 ConstantString(xml_info->attributes[i]),
693 ConstantString(xml_info->attributes[i+1]));
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704 % G e t X M L T r e e C h i l d %
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 % GetXMLTreeChild() returns the first child tag with the specified tag if
711 % found, otherwise NULL.
713 % The format of the GetXMLTreeChild method is:
715 % XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
717 % A description of each parameter follows:
719 % o xml_info: the xml info.
722 MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
727 assert(xml_info != (XMLTreeInfo *) NULL);
728 assert((xml_info->signature == MagickSignature) ||
729 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
731 child=xml_info->child;
732 if (tag != (const char *) NULL)
733 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
734 child=child->sibling;
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
743 % G e t X M L T r e e C o n t e n t %
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 % GetXMLTreeContent() returns any content associated with specified
752 % The format of the GetXMLTreeContent method is:
754 % const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
756 % A description of each parameter follows:
758 % o xml_info: the xml info.
761 MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
763 assert(xml_info != (XMLTreeInfo *) NULL);
764 assert((xml_info->signature == MagickSignature) ||
765 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
767 return(xml_info->content);
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 % G e t X M L T r e e O r d e r e d %
779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 % GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
783 % The format of the GetXMLTreeOrdered method is:
785 % XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
787 % A description of each parameter follows:
789 % o xml_info: the xml info.
792 MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
794 assert(xml_info != (XMLTreeInfo *) NULL);
795 assert((xml_info->signature == MagickSignature) ||
796 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
798 return(xml_info->ordered);
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % G e t X M L T r e e P a t h %
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % GetXMLTreePath() traverses the XML-tree as defined by the specified path
813 % and returns the node if found, otherwise NULL.
815 % The format of the GetXMLTreePath method is:
817 % XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
819 % A description of each parameter follows:
821 % o xml_info: the xml info.
823 % o path: the path (e.g. property/elapsed-time).
826 MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
830 subnode[MaxTextExtent],
845 assert(xml_info != (XMLTreeInfo *) NULL);
846 assert((xml_info->signature == MagickSignature) ||
847 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
850 components=GetPathComponents(path,&number_components);
851 if (components == (char **) NULL)
852 return((XMLTreeInfo *) NULL);
853 for (i=0; i < (ssize_t) number_components; i++)
855 GetPathComponent(components[i],SubimagePath,subnode);
856 GetPathComponent(components[i],CanonicalPath,tag);
857 node=GetXMLTreeChild(node,tag);
858 if (node == (XMLTreeInfo *) NULL)
860 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
862 node=GetXMLTreeOrdered(node);
863 if (node == (XMLTreeInfo *) NULL)
866 if (node == (XMLTreeInfo *) NULL)
868 components[i]=DestroyString(components[i]);
870 for ( ; i < (ssize_t) number_components; i++)
871 components[i]=DestroyString(components[i]);
872 components=(char **) RelinquishMagickMemory(components);
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 % 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 %
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887 % GetXMLTreeProcessingInstructions() returns a null terminated array of
888 % processing instructions for the given target.
890 % The format of the GetXMLTreeProcessingInstructions method is:
892 % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
893 % const char *target)
895 % A description of each parameter follows:
897 % o xml_info: the xml info.
900 MagickPrivate const char **GetXMLTreeProcessingInstructions(
901 XMLTreeInfo *xml_info,const char *target)
909 assert(xml_info != (XMLTreeInfo *) NULL);
910 assert((xml_info->signature == MagickSignature) ||
911 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
913 root=(XMLTreeRoot *) xml_info;
914 while (root->root.parent != (XMLTreeInfo *) NULL)
915 root=(XMLTreeRoot *) root->root.parent;
917 while ((root->processing_instructions[i] != (char **) NULL) &&
918 (strcmp(root->processing_instructions[i][0],target) != 0))
920 if (root->processing_instructions[i] == (char **) NULL)
921 return((const char **) sentinel);
922 return((const char **) (root->processing_instructions[i]+1));
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930 % G e t X M L T r e e S i b l i n g %
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
938 % The format of the GetXMLTreeSibling method is:
940 % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
942 % A description of each parameter follows:
944 % o xml_info: the xml info.
947 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
949 assert(xml_info != (XMLTreeInfo *) NULL);
950 assert((xml_info->signature == MagickSignature) ||
951 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
952 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
953 return(xml_info->sibling);
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961 % G e t X M L T r e e T a g %
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967 % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
969 % The format of the GetXMLTreeTag method is:
971 % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
973 % A description of each parameter follows:
975 % o xml_info: the xml info.
978 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
980 assert(xml_info != (XMLTreeInfo *) NULL);
981 assert((xml_info->signature == MagickSignature) ||
982 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
983 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
984 return(xml_info->tag);
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % I n s e r t I n t o T a g X M L T r e e %
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
999 % the parent tag's character content. This method returns the child tag.
1001 % The format of the InsertTagIntoXMLTree method is:
1003 % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1004 % XMLTreeInfo *child,const size_t offset)
1006 % A description of each parameter follows:
1008 % o xml_info: the xml info.
1010 % o child: the child tag.
1012 % o offset: the tag offset.
1015 MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1016 XMLTreeInfo *child,const size_t offset)
1023 child->ordered=(XMLTreeInfo *) NULL;
1024 child->sibling=(XMLTreeInfo *) NULL;
1025 child->next=(XMLTreeInfo *) NULL;
1026 child->offset=offset;
1027 child->parent=xml_info;
1028 if (xml_info->child == (XMLTreeInfo *) NULL)
1030 xml_info->child=child;
1033 head=xml_info->child;
1034 if (head->offset > offset)
1036 child->ordered=head;
1037 xml_info->child=child;
1042 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1043 (node->ordered->offset <= offset))
1045 child->ordered=node->ordered;
1046 node->ordered=child;
1048 previous=(XMLTreeInfo *) NULL;
1050 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1055 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1057 while ((node->next != (XMLTreeInfo *) NULL) &&
1058 (node->next->offset <= offset))
1060 child->next=node->next;
1065 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1066 previous->sibling=node->sibling;
1068 previous=(XMLTreeInfo *) NULL;
1070 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1075 child->sibling=node;
1076 if (previous != (XMLTreeInfo *) NULL)
1077 previous->sibling=child;
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % N e w X M L T r e e %
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093 % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1096 % The format of the NewXMLTree method is:
1098 % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1100 % A description of each parameter follows:
1102 % o xml: The XML string.
1104 % o exception: return any errors or warnings in this structure.
1108 static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
1128 utf8=(char *) AcquireQuantumMemory(*length,sizeof(*utf8));
1129 if (utf8 == (char *) NULL)
1130 return((char *) NULL);
1131 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
1137 (void) CopyMagickMemory(utf8,content,*length*sizeof(*utf8));
1142 for (i=2; i < (ssize_t) (*length-1); i+=2)
1144 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1145 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1146 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1148 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1149 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1150 (content[i] & 0xff);
1151 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1153 if ((size_t) (j+MaxTextExtent) > extent)
1155 extent=(size_t) j+MaxTextExtent;
1156 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1157 if (utf8 == (char *) NULL)
1167 Multi-byte UTF-8 sequence.
1170 for (bits=0; byte != 0; byte/=2)
1173 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1177 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1182 return((char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8)));
1185 static char *ParseEntities(char *xml,char **entities,int state)
1209 Normalize line endings.
1213 for ( ; *xml != '\0'; xml++)
1214 while (*xml == '\r')
1218 (void) CopyMagickMemory(xml,xml+1,strlen(xml));
1222 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1223 (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1229 '&' for general entity decoding
1230 '%' for parameter entity decoding
1231 'c' for CDATA sections
1232 ' ' for attributes normalization
1233 '*' for non-CDATA attributes normalization
1235 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1238 Character reference.
1241 c=strtol(xml+2,&entity,10); /* base 10 */
1243 c=strtol(xml+3,&entity,16); /* base 16 */
1244 if ((c == 0) || (*entity != ';'))
1247 Not a character reference.
1257 Multi-byte UTF-8 sequence.
1260 for (i=0; byte != 0; byte/=2)
1263 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1268 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1272 (void) CopyMagickMemory(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1275 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1276 (state == '*'))) || ((state == '%') && (*xml == '%')))
1279 Find entity in the list.
1282 while ((entities[i] != (char *) NULL) &&
1283 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1285 if (entities[i++] == (char *) NULL)
1292 length=strlen(entities[i]);
1293 entity=strchr(xml,';');
1294 if ((length-1L) >= (size_t) (entity-xml))
1296 offset=(ssize_t) (xml-p);
1297 extent=(size_t) (offset+length+strlen(entity));
1299 p=(char *) ResizeQuantumMemory(p,extent,sizeof(*p));
1305 xml=(char *) AcquireQuantumMemory(extent,sizeof(*xml));
1306 if (xml != (char *) NULL)
1308 (void) CopyMagickString(xml,p,extent*sizeof(*xml));
1312 if (p == (char *) NULL)
1313 ThrowFatalException(ResourceLimitFatalError,
1314 "MemoryAllocationFailed");
1316 entity=strchr(xml,';');
1318 (void) CopyMagickMemory(xml+length,entity+1,strlen(entity));
1319 (void) strncpy(xml,entities[i],length);
1323 if (((state == ' ') || (state == '*')) &&
1324 (isspace((int) ((unsigned char) *xml) != 0)))
1332 Normalize spaces for non-CDATA attributes.
1334 for (xml=p; *xml != '\0'; xml++)
1336 i=(ssize_t) strspn(xml," ");
1338 (void) CopyMagickMemory(xml,xml+i,strlen(xml+i)+1);
1339 while ((*xml != '\0') && (*xml != ' '))
1343 if ((xml >= p) && (*xml == ' '))
1346 return(p == q ? ConstantString(p) : p);
1349 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1350 const size_t length,const char state)
1355 xml_info=root->node;
1356 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1360 xml=ParseEntities(xml,root->entities,state);
1361 if (*xml_info->content != '\0')
1363 (void) ConcatenateString(&xml_info->content,xml);
1364 xml=DestroyString(xml);
1368 if (xml_info->content != (char *) NULL)
1369 xml_info->content=DestroyString(xml_info->content);
1370 xml_info->content=xml;
1374 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1375 char *magick_unused(xml),ExceptionInfo *exception)
1377 if ((root->node == (XMLTreeInfo *) NULL) ||
1378 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1380 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1381 "ParseError","unexpected closing tag </%s>",tag);
1382 return(&root->root);
1384 root->node=root->node->parent;
1385 return((XMLTreeInfo *) NULL);
1388 static MagickBooleanType ValidateEntities(char *tag,char *xml,char **entities)
1394 Check for circular entity references.
1398 while ((*xml != '\0') && (*xml != '&'))
1402 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1403 return(MagickFalse);
1405 while ((entities[i] != (char *) NULL) &&
1406 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1408 if ((entities[i] != (char *) NULL) &&
1409 (ValidateEntities(tag,entities[i+1],entities) == 0))
1410 return(MagickFalse);
1414 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1428 xml+=strcspn(xml,XMLWhitespace);
1432 xml+=strspn(xml+1,XMLWhitespace)+1;
1434 if (strcmp(target,"xml") == 0)
1436 xml=strstr(xml,"standalone");
1437 if ((xml != (char *) NULL) &&
1438 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1439 root->standalone=MagickTrue;
1442 if (root->processing_instructions[0] == (char **) NULL)
1444 root->processing_instructions=(char ***) AcquireMagickMemory(sizeof(
1445 *root->processing_instructions));
1446 if (root->processing_instructions ==(char ***) NULL)
1447 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1448 *root->processing_instructions=(char **) NULL;
1451 while ((root->processing_instructions[i] != (char **) NULL) &&
1452 (strcmp(target,root->processing_instructions[i][0]) != 0))
1454 if (root->processing_instructions[i] == (char **) NULL)
1456 root->processing_instructions=(char ***) ResizeQuantumMemory(
1457 root->processing_instructions,(size_t) (i+2),
1458 sizeof(*root->processing_instructions));
1459 if (root->processing_instructions == (char ***) NULL)
1460 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1461 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1462 sizeof(**root->processing_instructions));
1463 if (root->processing_instructions[i] == (char **) NULL)
1464 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1465 root->processing_instructions[i+1]=(char **) NULL;
1466 root->processing_instructions[i][0]=ConstantString(target);
1467 root->processing_instructions[i][1]=(char *)
1468 root->processing_instructions[i+1];
1469 root->processing_instructions[i+1]=(char **) NULL;
1470 root->processing_instructions[i][2]=ConstantString("");
1473 while (root->processing_instructions[i][j] != (char *) NULL)
1475 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1476 root->processing_instructions[i],(size_t) (j+3),
1477 sizeof(**root->processing_instructions));
1478 if (root->processing_instructions[i] == (char **) NULL)
1479 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1480 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1481 root->processing_instructions[i][j+1],(size_t) (j+1),
1482 sizeof(**root->processing_instructions));
1483 if (root->processing_instructions[i][j+2] == (char *) NULL)
1484 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1485 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1486 root->root.tag != (char *) NULL ? ">" : "<",2);
1487 root->processing_instructions[i][j]=ConstantString(xml);
1488 root->processing_instructions[i][j+1]=(char *) NULL;
1491 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1492 size_t length,ExceptionInfo *exception)
1498 **predefined_entitites,
1510 predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1511 if (predefined_entitites == (char **) NULL)
1512 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1513 (void) CopyMagickMemory(predefined_entitites,sentinel,sizeof(sentinel));
1514 for (xml[length]='\0'; xml != (char *) NULL; )
1516 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1520 if (strncmp(xml,"<!ENTITY",8) == 0)
1523 Parse entity definitions.
1525 xml+=strspn(xml+8,XMLWhitespace)+8;
1527 n=xml+strspn(xml,XMLWhitespace "%");
1528 xml=n+strcspn(n,XMLWhitespace);
1530 v=xml+strspn(xml+1,XMLWhitespace)+1;
1533 if ((q != '"') && (q != '\''))
1538 xml=strchr(xml,'>');
1541 entities=(*c == '%') ? predefined_entitites : root->entities;
1542 for (i=0; entities[i] != (char *) NULL; i++) ;
1543 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1545 if (entities == (char **) NULL)
1546 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1548 predefined_entitites=entities;
1550 root->entities=entities;
1554 if (xml != (char *) NULL)
1559 entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1560 entities[i+2]=(char *) NULL;
1561 if (ValidateEntities(n,entities[i+1],entities) != MagickFalse)
1565 if (entities[i+1] != v)
1566 entities[i+1]=DestroyString(entities[i+1]);
1567 (void) ThrowMagickException(exception,GetMagickModule(),
1568 OptionWarning,"ParseError","circular entity declaration &%s",n);
1569 predefined_entitites=(char **) RelinquishMagickMemory(
1570 predefined_entitites);
1571 return(MagickFalse);
1575 if (strncmp(xml,"<!ATTLIST",9) == 0)
1578 Parse default attributes.
1580 t=xml+strspn(xml+9,XMLWhitespace)+9;
1583 (void) ThrowMagickException(exception,GetMagickModule(),
1584 OptionWarning,"ParseError","unclosed <!ATTLIST");
1585 predefined_entitites=(char **) RelinquishMagickMemory(
1586 predefined_entitites);
1587 return(MagickFalse);
1589 xml=t+strcspn(t,XMLWhitespace ">");
1594 while ((root->attributes[i] != (char **) NULL) &&
1595 (strcmp(n,root->attributes[i][0]) != 0))
1597 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1600 xml=n+strcspn(n,XMLWhitespace);
1605 (void) ThrowMagickException(exception,GetMagickModule(),
1606 OptionWarning,"ParseError","malformed <!ATTLIST");
1607 predefined_entitites=(char **) RelinquishMagickMemory(
1608 predefined_entitites);
1609 return(MagickFalse);
1611 xml+=strspn(xml+1,XMLWhitespace)+1;
1612 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1613 if (strncmp(xml,"NOTATION",8) == 0)
1614 xml+=strspn(xml+8,XMLWhitespace)+8;
1615 xml=(*xml == '(') ? strchr(xml,')') : xml+
1616 strcspn(xml,XMLWhitespace);
1617 if (xml == (char *) NULL)
1619 (void) ThrowMagickException(exception,GetMagickModule(),
1620 OptionWarning,"ParseError","malformed <!ATTLIST");
1621 predefined_entitites=(char **) RelinquishMagickMemory(
1622 predefined_entitites);
1623 return(MagickFalse);
1625 xml+=strspn(xml,XMLWhitespace ")");
1626 if (strncmp(xml,"#FIXED",6) == 0)
1627 xml+=strspn(xml+6,XMLWhitespace)+6;
1630 xml+=strcspn(xml,XMLWhitespace ">")-1;
1636 if (((*xml == '"') || (*xml == '\'')) &&
1637 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1641 (void) ThrowMagickException(exception,GetMagickModule(),
1642 OptionWarning,"ParseError","malformed <!ATTLIST");
1643 predefined_entitites=(char **) RelinquishMagickMemory(
1644 predefined_entitites);
1645 return(MagickFalse);
1647 if (root->attributes[i] == (char **) NULL)
1653 root->attributes=(char ***) AcquireQuantumMemory(2,
1654 sizeof(*root->attributes));
1656 root->attributes=(char ***) ResizeQuantumMemory(
1657 root->attributes,(size_t) (i+2),
1658 sizeof(*root->attributes));
1659 if (root->attributes == (char ***) NULL)
1660 ThrowFatalException(ResourceLimitFatalError,
1661 "MemoryAllocationFailed");
1662 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1663 sizeof(*root->attributes));
1664 if (root->attributes[i] == (char **) NULL)
1665 ThrowFatalException(ResourceLimitFatalError,
1666 "MemoryAllocationFailed");
1667 root->attributes[i][0]=ConstantString(t);
1668 root->attributes[i][1]=(char *) NULL;
1669 root->attributes[i+1]=(char **) NULL;
1671 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1672 root->attributes[i]=(char **) ResizeQuantumMemory(
1673 root->attributes[i],(size_t) (j+4),sizeof(*root->attributes));
1674 if (root->attributes[i] == (char **) NULL)
1675 ThrowFatalException(ResourceLimitFatalError,
1676 "MemoryAllocationFailed");
1677 root->attributes[i][j+3]=(char *) NULL;
1678 root->attributes[i][j+2]=ConstantString(c);
1679 root->attributes[i][j+1]=(char *) NULL;
1680 if (v != (char *) NULL)
1681 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1682 root->attributes[i][j]=ConstantString(n);
1686 if (strncmp(xml, "<!--", 4) == 0)
1687 xml=strstr(xml+4,"-->");
1689 if (strncmp(xml,"<?", 2) == 0)
1693 if (xml != (char *) NULL)
1695 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1701 xml=strchr(xml,'>');
1703 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1706 predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1710 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1715 xml_info=root->node;
1716 if (xml_info->tag == (char *) NULL)
1717 xml_info->tag=ConstantString(tag);
1719 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1720 xml_info->attributes=attributes;
1721 root->node=xml_info;
1724 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1756 Convert xml-string to UTF8.
1758 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1760 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1761 "ParseError","root tag missing");
1762 return((XMLTreeInfo *) NULL);
1764 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1766 utf8=ConvertUTF16ToUTF8(xml,&length);
1767 if (utf8 == (char *) NULL)
1769 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1770 "ParseError","UTF16 to UTF8 failed");
1771 return((XMLTreeInfo *) NULL);
1773 terminal=utf8[length-1];
1774 utf8[length-1]='\0';
1776 while ((*p != '\0') && (*p != '<'))
1780 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1781 "ParseError","root tag missing");
1782 utf8=DestroyString(utf8);
1783 return((XMLTreeInfo *) NULL);
1785 attribute=(char **) NULL;
1788 attributes=(char **) sentinel;
1791 if ((isalpha((int) ((unsigned char) *p)) !=0) || (*p == '_') ||
1792 (*p == ':') || (c < '\0'))
1797 if (root->node == (XMLTreeInfo *) NULL)
1799 (void) ThrowMagickException(exception,GetMagickModule(),
1800 OptionWarning,"ParseError","root tag missing");
1801 utf8=DestroyString(utf8);
1802 return(&root->root);
1804 p+=strcspn(p,XMLWhitespace "/>");
1805 while (isspace((int) ((unsigned char) *p)) != 0)
1807 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1810 Find tag in default attributes list.
1813 while ((root->attributes[i] != (char **) NULL) &&
1814 (strcmp(root->attributes[i][0],tag) != 0))
1816 attribute=root->attributes[i];
1818 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1824 attributes=(char **) AcquireQuantumMemory(4,sizeof(*attributes));
1826 attributes=(char **) ResizeQuantumMemory(attributes,(size_t) (l+4),
1827 sizeof(*attributes));
1828 if (attributes == (char **) NULL)
1830 (void) ThrowMagickException(exception,GetMagickModule(),
1831 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1832 utf8=DestroyString(utf8);
1833 return(&root->root);
1835 attributes[l+2]=(char *) NULL;
1836 attributes[l+1]=(char *) NULL;
1838 p+=strcspn(p,XMLWhitespace "=/>");
1839 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1840 attributes[l]=ConstantString("");
1844 p+=strspn(p,XMLWhitespace "=");
1846 if ((c == '"') || (c == '\''))
1853 while ((*p != '\0') && (*p != c))
1859 attributes[l]=ConstantString("");
1860 attributes[l+1]=ConstantString("");
1861 (void) DestroyXMLTreeAttributes(attributes);
1862 (void) ThrowMagickException(exception,GetMagickModule(),
1863 OptionWarning,"ParseError","missing %c",c);
1864 utf8=DestroyString(utf8);
1865 return(&root->root);
1868 while ((attribute != (char **) NULL) &&
1869 (attribute[j] != (char *) NULL) &&
1870 (strcmp(attribute[j],attributes[l]) != 0))
1872 attributes[l+1]=ParseEntities(attributes[l+1],root->entities,
1873 (attribute != (char **) NULL) && (attribute[j] !=
1874 (char *) NULL) ? *attribute[j+2] : ' ');
1876 attributes[l]=ConstantString(attributes[l]);
1878 while (isspace((int) ((unsigned char) *p)) != 0)
1887 if (((*p != '\0') && (*p != '>')) ||
1888 ((*p == '\0') && (terminal != '>')))
1891 (void) DestroyXMLTreeAttributes(attributes);
1892 (void) ThrowMagickException(exception,GetMagickModule(),
1893 OptionWarning,"ParseError","missing >");
1894 utf8=DestroyString(utf8);
1895 return(&root->root);
1897 ParseOpenTag(root,tag,attributes);
1898 (void) ParseCloseTag(root,tag,p,exception);
1903 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1906 ParseOpenTag(root,tag,attributes);
1912 (void) DestroyXMLTreeAttributes(attributes);
1913 (void) ThrowMagickException(exception,GetMagickModule(),
1914 OptionWarning,"ParseError","missing >");
1915 utf8=DestroyString(utf8);
1916 return(&root->root);
1927 p+=strcspn(tag,XMLWhitespace ">")+1;
1929 if ((c == '\0') && (terminal != '>'))
1931 (void) ThrowMagickException(exception,GetMagickModule(),
1932 OptionWarning,"ParseError","missing >");
1933 utf8=DestroyString(utf8);
1934 return(&root->root);
1937 if (ParseCloseTag(root,tag,p,exception) != (XMLTreeInfo *) NULL)
1939 utf8=DestroyString(utf8);
1940 return(&root->root);
1943 if (isspace((int) ((unsigned char) *p)) != 0)
1944 p+=strspn(p,XMLWhitespace);
1947 if (strncmp(p,"!--",3) == 0)
1953 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1954 ((*p == '\0') && (terminal != '>')))
1956 (void) ThrowMagickException(exception,GetMagickModule(),
1957 OptionWarning,"ParseError","unclosed <!--");
1958 utf8=DestroyString(utf8);
1959 return(&root->root);
1963 if (strncmp(p,"![CDATA[",8) == 0)
1969 if (p != (char *) NULL)
1972 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1976 (void) ThrowMagickException(exception,GetMagickModule(),
1977 OptionWarning,"ParseError","unclosed <![CDATA[");
1978 utf8=DestroyString(utf8);
1979 return(&root->root);
1983 if (strncmp(p,"!DOCTYPE",8) == 0)
1988 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1989 ((l != 0) && ((*p != ']') ||
1990 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1991 l=(ssize_t) ((*p == '[') ? 1 : l))
1992 p+=strcspn(p+1,"[]>")+1;
1993 if ((*p == '\0') && (terminal != '>'))
1995 (void) ThrowMagickException(exception,GetMagickModule(),
1996 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1997 utf8=DestroyString(utf8);
1998 return(&root->root);
2001 tag=strchr(tag,'[')+1;
2004 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
2006 if (status == MagickFalse)
2008 utf8=DestroyString(utf8);
2009 return(&root->root);
2018 Processing instructions.
2023 if (p == (char *) NULL)
2026 } while ((*p != '\0') && (*p != '>'));
2027 if ((p == (char *) NULL) || ((*p == '\0') &&
2030 (void) ThrowMagickException(exception,GetMagickModule(),
2031 OptionWarning,"ParseError","unclosed <?");
2032 utf8=DestroyString(utf8);
2033 return(&root->root);
2035 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2039 (void) ThrowMagickException(exception,GetMagickModule(),
2040 OptionWarning,"ParseError","unexpected <");
2041 utf8=DestroyString(utf8);
2042 return(&root->root);
2044 if ((p == (char *) NULL) || (*p == '\0'))
2048 if ((*p != '\0') && (*p != '<'))
2051 Tag character content.
2053 while ((*p != '\0') && (*p != '<'))
2057 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2063 utf8=DestroyString(utf8);
2064 if (root->node == (XMLTreeInfo *) NULL)
2065 return(&root->root);
2066 if (root->node->tag == (char *) NULL)
2068 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2069 "ParseError","root tag missing");
2070 return(&root->root);
2072 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2073 "ParseError","unclosed tag: '%s'",root->node->tag);
2074 return(&root->root);
2078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082 % N e w X M L T r e e T a g %
2086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2088 % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2090 % The format of the NewXMLTreeTag method is:
2092 % XMLTreeInfo *NewXMLTreeTag(const char *tag)
2094 % A description of each parameter follows:
2099 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2102 *predefined_entities[NumberPredefinedEntities+1] =
2104 "lt;", "<", "gt;", ">", "quot;", """,
2105 "apos;", "'", "amp;", "&", (char *) NULL
2111 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2112 if (root == (XMLTreeRoot *) NULL)
2113 return((XMLTreeInfo *) NULL);
2114 (void) ResetMagickMemory(root,0,sizeof(*root));
2115 root->root.tag=(char *) NULL;
2116 if (tag != (char *) NULL)
2117 root->root.tag=ConstantString(tag);
2118 root->node=(&root->root);
2119 root->root.content=ConstantString("");
2120 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2121 if (root->entities == (char **) NULL)
2122 return((XMLTreeInfo *) NULL);
2123 (void) CopyMagickMemory(root->entities,predefined_entities,
2124 sizeof(predefined_entities));
2125 root->root.attributes=sentinel;
2126 root->attributes=(char ***) root->root.attributes;
2127 root->processing_instructions=(char ***) root->root.attributes;
2128 root->debug=IsEventLogging();
2129 root->signature=MagickSignature;
2130 return(&root->root);
2134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2138 % P r u n e T a g F r o m X M L T r e e %
2142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2144 % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2147 % The format of the PruneTagFromXMLTree method is:
2149 % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2151 % A description of each parameter follows:
2153 % o xml_info: the xml info.
2156 MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2161 assert(xml_info != (XMLTreeInfo *) NULL);
2162 assert((xml_info->signature == MagickSignature) ||
2163 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2164 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2165 if (xml_info->next != (XMLTreeInfo *) NULL)
2166 xml_info->next->sibling=xml_info->sibling;
2167 if (xml_info->parent != (XMLTreeInfo *) NULL)
2169 node=xml_info->parent->child;
2170 if (node == xml_info)
2171 xml_info->parent->child=xml_info->ordered;
2174 while (node->ordered != xml_info)
2176 node->ordered=node->ordered->ordered;
2177 node=xml_info->parent->child;
2178 if (strcmp(node->tag,xml_info->tag) != 0)
2180 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2182 if (node->sibling != xml_info)
2185 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2186 xml_info->next : node->sibling->sibling;
2188 while ((node->next != (XMLTreeInfo *) NULL) &&
2189 (node->next != xml_info))
2191 if (node->next != (XMLTreeInfo *) NULL)
2192 node->next=node->next->next;
2195 xml_info->ordered=(XMLTreeInfo *) NULL;
2196 xml_info->sibling=(XMLTreeInfo *) NULL;
2197 xml_info->next=(XMLTreeInfo *) NULL;
2202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2206 % S e t X M L T r e e A t t r i b u t e %
2210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212 % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2213 % found. A value of NULL removes the specified attribute.
2215 % The format of the SetXMLTreeAttribute method is:
2217 % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2218 % const char *value)
2220 % A description of each parameter follows:
2222 % o xml_info: the xml info.
2224 % o tag: The attribute tag.
2226 % o value: The attribute value.
2229 MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2230 const char *tag,const char *value)
2238 assert(xml_info != (XMLTreeInfo *) NULL);
2239 assert((xml_info->signature == MagickSignature) ||
2240 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2241 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2243 while ((xml_info->attributes[i] != (char *) NULL) &&
2244 (strcmp(xml_info->attributes[i],tag) != 0))
2246 if (xml_info->attributes[i] == (char *) NULL)
2249 Add new attribute tag.
2251 if (value == (const char *) NULL)
2253 if (xml_info->attributes != sentinel)
2254 xml_info->attributes=(char **) ResizeQuantumMemory(
2255 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2258 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2259 sizeof(*xml_info->attributes));
2260 if (xml_info->attributes != (char **) NULL)
2261 xml_info->attributes[1]=ConstantString("");
2263 if (xml_info->attributes == (char **) NULL)
2264 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2265 xml_info->attributes[i]=ConstantString(tag);
2266 xml_info->attributes[i+2]=(char *) NULL;
2267 (void) strlen(xml_info->attributes[i+1]);
2270 Add new value to an existing attribute.
2272 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2273 if (xml_info->attributes[i+1] != (char *) NULL)
2274 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2275 if (value != (const char *) NULL)
2277 xml_info->attributes[i+1]=ConstantString(value);
2280 if (xml_info->attributes[i] != (char *) NULL)
2281 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2282 (void) CopyMagickMemory(xml_info->attributes+i,xml_info->attributes+i+2,
2283 (size_t) (j-i)*sizeof(*xml_info->attributes));
2284 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2285 (size_t) (j+2),sizeof(*xml_info->attributes));
2286 if (xml_info->attributes == (char **) NULL)
2287 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2289 (void) CopyMagickMemory(xml_info->attributes[j+1]+(i/2),
2290 xml_info->attributes[j+1]+(i/2)+1,(size_t) (((j+2)/2)-(i/2))*
2291 sizeof(*xml_info->attributes));
2296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2300 % S e t X M L T r e e C o n t e n t %
2304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2306 % SetXMLTreeContent() sets the character content for the given tag and
2309 % The format of the SetXMLTreeContent method is:
2311 % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2312 % const char *content)
2314 % A description of each parameter follows:
2316 % o xml_info: the xml info.
2318 % o content: The content.
2321 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2322 const char *content)
2324 assert(xml_info != (XMLTreeInfo *) NULL);
2325 assert((xml_info->signature == MagickSignature) ||
2326 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2328 if (xml_info->content != (char *) NULL)
2329 xml_info->content=DestroyString(xml_info->content);
2330 xml_info->content=(char *) ConstantString(content);
2335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2339 % X M L T r e e I n f o T o X M L %
2343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2345 % XMLTreeInfoToXML() converts an xml-tree to an XML string.
2347 % The format of the XMLTreeInfoToXML method is:
2349 % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2351 % A description of each parameter follows:
2353 % o xml_info: the xml info.
2357 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2358 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2364 canonical_content=CanonicalXMLContent(source,pedantic);
2370 content=AcquireString(source);
2371 content[offset]='\0';
2372 canonical_content=CanonicalXMLContent(content,pedantic);
2373 content=DestroyString(content);
2375 if (canonical_content == (char *) NULL)
2376 return(*destination);
2377 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2379 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2380 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2381 sizeof(**destination));
2382 if (*destination == (char *) NULL)
2383 return(*destination);
2385 *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2387 canonical_content=DestroyString(canonical_content);
2388 return(*destination);
2391 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2392 size_t *extent,size_t start,char ***attributes)
2409 content=(char *) "";
2410 if (xml_info->parent != (XMLTreeInfo *) NULL)
2411 content=xml_info->parent->content;
2413 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2414 start),source,length,extent,MagickFalse);
2415 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2417 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2418 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(*source));
2419 if (*source == (char *) NULL)
2422 *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2423 for (i=0; xml_info->attributes[i]; i+=2)
2425 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2426 if (attribute != xml_info->attributes[i+1])
2428 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2430 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2431 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2432 if (*source == (char *) NULL)
2433 return((char *) NULL);
2435 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2436 xml_info->attributes[i]);
2437 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2439 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2442 while ((attributes[i] != (char **) NULL) &&
2443 (strcmp(attributes[i][0],xml_info->tag) != 0))
2446 while ((attributes[i] != (char **) NULL) &&
2447 (attributes[i][j] != (char *) NULL))
2449 if ((attributes[i][j+1] == (char *) NULL) ||
2450 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2455 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2457 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2458 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2459 if (*source == (char *) NULL)
2460 return((char *) NULL);
2462 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2464 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2466 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2469 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2471 if (xml_info->child != (XMLTreeInfo *) NULL)
2472 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2474 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2476 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2478 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2479 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2480 if (*source == (char *) NULL)
2481 return((char *) NULL);
2483 if (*xml_info->content != '\0')
2484 *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2486 while ((content[offset] != '\0') && (offset < xml_info->offset))
2488 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2489 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2492 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2497 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2524 assert(xml_info != (XMLTreeInfo *) NULL);
2525 assert((xml_info->signature == MagickSignature) ||
2526 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2528 if (xml_info->tag == (char *) NULL)
2529 return((char *) NULL);
2530 xml=AcquireString((char *) NULL);
2532 extent=MaxTextExtent;
2533 root=(XMLTreeRoot *) xml_info;
2534 while (root->root.parent != (XMLTreeInfo *) NULL)
2535 root=(XMLTreeRoot *) root->root.parent;
2536 parent=(XMLTreeInfo *) NULL;
2537 if (xml_info != (XMLTreeInfo *) NULL)
2538 parent=xml_info->parent;
2539 if (parent == (XMLTreeInfo *) NULL)
2540 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2543 Pre-root processing instructions.
2545 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2546 p=root->processing_instructions[i][1];
2547 for (j=1; p != (char *) NULL; j++)
2549 if (root->processing_instructions[i][k][j-1] == '>')
2551 p=root->processing_instructions[i][j];
2554 q=root->processing_instructions[i][0];
2555 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2557 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2558 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2559 if (xml == (char *) NULL)
2562 length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2563 *p != '\0' ? " " : "",p);
2564 p=root->processing_instructions[i][j];
2567 ordered=(XMLTreeInfo *) NULL;
2568 if (xml_info != (XMLTreeInfo *) NULL)
2569 ordered=xml_info->ordered;
2570 xml_info->parent=(XMLTreeInfo *) NULL;
2571 xml_info->ordered=(XMLTreeInfo *) NULL;
2572 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2573 xml_info->parent=parent;
2574 xml_info->ordered=ordered;
2575 if (parent == (XMLTreeInfo *) NULL)
2576 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2579 Post-root processing instructions.
2581 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2582 p=root->processing_instructions[i][1];
2583 for (j=1; p != (char *) NULL; j++)
2585 if (root->processing_instructions[i][k][j-1] == '<')
2587 p=root->processing_instructions[i][j];
2590 q=root->processing_instructions[i][0];
2591 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2593 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2594 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2595 if (xml == (char *) NULL)
2598 length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2599 *p != '\0' ? " " : "",p);
2600 p=root->processing_instructions[i][j];
2603 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));