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)
523 assert(xml_info != (XMLTreeInfo *) NULL);
524 assert((xml_info->signature == MagickSignature) ||
525 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
526 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
527 if (xml_info->child != (XMLTreeInfo *) NULL)
528 xml_info->child=DestroyXMLTree(xml_info->child);
529 if (xml_info->ordered != (XMLTreeInfo *) NULL)
530 xml_info->ordered=DestroyXMLTree(xml_info->ordered);
531 DestroyXMLTreeRoot(xml_info);
532 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
533 xml_info->content=DestroyString(xml_info->content);
534 xml_info->tag=DestroyString(xml_info->tag);
535 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
536 return((XMLTreeInfo *) NULL);
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 % G e t N e x t X M L T r e e T a g %
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % GetNextXMLTreeTag() returns the next tag or NULL if not found.
552 % The format of the GetNextXMLTreeTag method is:
554 % XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
556 % A description of each parameter follows:
558 % o xml_info: the xml info.
561 MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
563 assert(xml_info != (XMLTreeInfo *) NULL);
564 assert((xml_info->signature == MagickSignature) ||
565 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
567 return(xml_info->next);
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 % G e t X M L T r e e A t t r i b u t e %
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581 % GetXMLTreeAttribute() returns the value of the attribute tag with the
582 % specified tag if found, otherwise NULL.
584 % The format of the GetXMLTreeAttribute method is:
586 % const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
588 % A description of each parameter follows:
590 % o xml_info: the xml info.
592 % o tag: the attribute tag.
595 MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
607 assert(xml_info != (XMLTreeInfo *) NULL);
608 assert((xml_info->signature == MagickSignature) ||
609 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
611 if (xml_info->attributes == (char **) NULL)
612 return((const char *) NULL);
614 while ((xml_info->attributes[i] != (char *) NULL) &&
615 (strcmp(xml_info->attributes[i],tag) != 0))
617 if (xml_info->attributes[i] != (char *) NULL)
618 return(xml_info->attributes[i+1]);
619 root=(XMLTreeRoot*) xml_info;
620 while (root->root.parent != (XMLTreeInfo *) NULL)
621 root=(XMLTreeRoot *) root->root.parent;
623 while ((root->attributes[i] != (char **) NULL) &&
624 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
626 if (root->attributes[i] == (char **) NULL)
627 return((const char *) NULL);
629 while ((root->attributes[i][j] != (char *) NULL) &&
630 (strcmp(root->attributes[i][j],tag) != 0))
632 if (root->attributes[i][j] == (char *) NULL)
633 return((const char *) NULL);
634 return(root->attributes[i][j+1]);
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % G e t X M L T r e e A t t r i b u t e s %
646 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 % GetXMLTreeAttributes() injects all attributes associated with the current
649 % tag in the specified splay-tree.
651 % The format of the GetXMLTreeAttributes method is:
653 % MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
654 % SplayTreeInfo *attributes)
656 % A description of each parameter follows:
658 % o xml_info: the xml info.
660 % o attributes: the attribute splay-tree.
663 MagickPrivate MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
664 SplayTreeInfo *attributes)
669 assert(xml_info != (XMLTreeInfo *) NULL);
670 assert((xml_info->signature == MagickSignature) ||
671 (((const XMLTreeRoot *) xml_info)->signature == MagickSignature));
672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
673 assert(attributes != (SplayTreeInfo *) NULL);
674 if (xml_info->attributes == (char **) NULL)
677 while (xml_info->attributes[i] != (char *) NULL)
679 (void) AddValueToSplayTree(attributes,
680 ConstantString(xml_info->attributes[i]),
681 ConstantString(xml_info->attributes[i+1]));
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 % G e t X M L T r e e C h i l d %
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 % GetXMLTreeChild() returns the first child tag with the specified tag if
699 % found, otherwise NULL.
701 % The format of the GetXMLTreeChild method is:
703 % XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
705 % A description of each parameter follows:
707 % o xml_info: the xml info.
710 MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
715 assert(xml_info != (XMLTreeInfo *) NULL);
716 assert((xml_info->signature == MagickSignature) ||
717 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
718 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
719 child=xml_info->child;
720 if (tag != (const char *) NULL)
721 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
722 child=child->sibling;
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % G e t X M L T r e e C o n t e n t %
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % GetXMLTreeContent() returns any content associated with specified
740 % The format of the GetXMLTreeContent method is:
742 % const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
744 % A description of each parameter follows:
746 % o xml_info: the xml info.
749 MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
751 assert(xml_info != (XMLTreeInfo *) NULL);
752 assert((xml_info->signature == MagickSignature) ||
753 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
755 return(xml_info->content);
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % G e t X M L T r e e O r d e r e d %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
771 % The format of the GetXMLTreeOrdered method is:
773 % XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
775 % A description of each parameter follows:
777 % o xml_info: the xml info.
780 MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
782 assert(xml_info != (XMLTreeInfo *) NULL);
783 assert((xml_info->signature == MagickSignature) ||
784 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
786 return(xml_info->ordered);
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % G e t X M L T r e e P a t h %
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % GetXMLTreePath() traverses the XML-tree as defined by the specified path
801 % and returns the node if found, otherwise NULL.
803 % The format of the GetXMLTreePath method is:
805 % XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
807 % A description of each parameter follows:
809 % o xml_info: the xml info.
811 % o path: the path (e.g. property/elapsed-time).
814 MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
818 subnode[MaxTextExtent],
833 assert(xml_info != (XMLTreeInfo *) NULL);
834 assert((xml_info->signature == MagickSignature) ||
835 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
838 components=GetPathComponents(path,&number_components);
839 if (components == (char **) NULL)
840 return((XMLTreeInfo *) NULL);
841 for (i=0; i < (ssize_t) number_components; i++)
843 GetPathComponent(components[i],SubimagePath,subnode);
844 GetPathComponent(components[i],CanonicalPath,tag);
845 node=GetXMLTreeChild(node,tag);
846 if (node == (XMLTreeInfo *) NULL)
848 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
850 node=GetXMLTreeOrdered(node);
851 if (node == (XMLTreeInfo *) NULL)
854 if (node == (XMLTreeInfo *) NULL)
856 components[i]=DestroyString(components[i]);
858 for ( ; i < (ssize_t) number_components; i++)
859 components[i]=DestroyString(components[i]);
860 components=(char **) RelinquishMagickMemory(components);
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 % 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 %
873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 % GetXMLTreeProcessingInstructions() returns a null terminated array of
876 % processing instructions for the given target.
878 % The format of the GetXMLTreeProcessingInstructions method is:
880 % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
881 % const char *target)
883 % A description of each parameter follows:
885 % o xml_info: the xml info.
888 MagickPrivate const char **GetXMLTreeProcessingInstructions(
889 XMLTreeInfo *xml_info,const char *target)
897 assert(xml_info != (XMLTreeInfo *) NULL);
898 assert((xml_info->signature == MagickSignature) ||
899 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
901 root=(XMLTreeRoot *) xml_info;
902 while (root->root.parent != (XMLTreeInfo *) NULL)
903 root=(XMLTreeRoot *) root->root.parent;
905 while ((root->processing_instructions[i] != (char **) NULL) &&
906 (strcmp(root->processing_instructions[i][0],target) != 0))
908 if (root->processing_instructions[i] == (char **) NULL)
909 return((const char **) sentinel);
910 return((const char **) (root->processing_instructions[i]+1));
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % G e t X M L T r e e S i b l i n g %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
926 % The format of the GetXMLTreeSibling method is:
928 % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
930 % A description of each parameter follows:
932 % o xml_info: the xml info.
935 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
937 assert(xml_info != (XMLTreeInfo *) NULL);
938 assert((xml_info->signature == MagickSignature) ||
939 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
941 return(xml_info->sibling);
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % G e t X M L T r e e T a g %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
957 % The format of the GetXMLTreeTag method is:
959 % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
961 % A description of each parameter follows:
963 % o xml_info: the xml info.
966 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
968 assert(xml_info != (XMLTreeInfo *) NULL);
969 assert((xml_info->signature == MagickSignature) ||
970 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
972 return(xml_info->tag);
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 % I n s e r t I n t o T a g X M L T r e e %
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
987 % the parent tag's character content. This method returns the child tag.
989 % The format of the InsertTagIntoXMLTree method is:
991 % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
992 % XMLTreeInfo *child,const size_t offset)
994 % A description of each parameter follows:
996 % o xml_info: the xml info.
998 % o child: the child tag.
1000 % o offset: the tag offset.
1003 MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1004 XMLTreeInfo *child,const size_t offset)
1011 child->ordered=(XMLTreeInfo *) NULL;
1012 child->sibling=(XMLTreeInfo *) NULL;
1013 child->next=(XMLTreeInfo *) NULL;
1014 child->offset=offset;
1015 child->parent=xml_info;
1016 if (xml_info->child == (XMLTreeInfo *) NULL)
1018 xml_info->child=child;
1021 head=xml_info->child;
1022 if (head->offset > offset)
1024 child->ordered=head;
1025 xml_info->child=child;
1030 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1031 (node->ordered->offset <= offset))
1033 child->ordered=node->ordered;
1034 node->ordered=child;
1036 previous=(XMLTreeInfo *) NULL;
1038 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1043 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1045 while ((node->next != (XMLTreeInfo *) NULL) &&
1046 (node->next->offset <= offset))
1048 child->next=node->next;
1053 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1054 previous->sibling=node->sibling;
1056 previous=(XMLTreeInfo *) NULL;
1058 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1063 child->sibling=node;
1064 if (previous != (XMLTreeInfo *) NULL)
1065 previous->sibling=child;
1071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1075 % N e w X M L T r e e %
1079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1084 % The format of the NewXMLTree method is:
1086 % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1088 % A description of each parameter follows:
1090 % o xml: The XML string.
1092 % o exception: return any errors or warnings in this structure.
1096 static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
1116 utf8=(char *) AcquireQuantumMemory(*length,sizeof(*utf8));
1117 if (utf8 == (char *) NULL)
1118 return((char *) NULL);
1119 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
1125 (void) CopyMagickMemory(utf8,content,*length*sizeof(*utf8));
1130 for (i=2; i < (ssize_t) (*length-1); i+=2)
1132 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1133 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1134 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1136 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1137 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1138 (content[i] & 0xff);
1139 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1141 if ((size_t) (j+MaxTextExtent) > extent)
1143 extent=(size_t) j+MaxTextExtent;
1144 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1145 if (utf8 == (char *) NULL)
1155 Multi-byte UTF-8 sequence.
1158 for (bits=0; byte != 0; byte/=2)
1161 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1165 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1170 return((char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8)));
1173 static char *ParseEntities(char *xml,char **entities,int state)
1197 Normalize line endings.
1201 for ( ; *xml != '\0'; xml++)
1202 while (*xml == '\r')
1206 (void) CopyMagickMemory(xml,xml+1,strlen(xml));
1210 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1211 (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1217 '&' for general entity decoding
1218 '%' for parameter entity decoding
1219 'c' for CDATA sections
1220 ' ' for attributes normalization
1221 '*' for non-CDATA attributes normalization
1223 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1226 Character reference.
1229 c=strtol(xml+2,&entity,10); /* base 10 */
1231 c=strtol(xml+3,&entity,16); /* base 16 */
1232 if ((c == 0) || (*entity != ';'))
1235 Not a character reference.
1245 Multi-byte UTF-8 sequence.
1248 for (i=0; byte != 0; byte/=2)
1251 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1256 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1260 (void) CopyMagickMemory(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1263 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1264 (state == '*'))) || ((state == '%') && (*xml == '%')))
1267 Find entity in the list.
1270 while ((entities[i] != (char *) NULL) &&
1271 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1273 if (entities[i++] == (char *) NULL)
1280 length=strlen(entities[i]);
1281 entity=strchr(xml,';');
1282 if ((length-1L) >= (size_t) (entity-xml))
1284 offset=(ssize_t) (xml-p);
1285 extent=(size_t) (offset+length+strlen(entity));
1287 p=(char *) ResizeQuantumMemory(p,extent,sizeof(*p));
1293 xml=(char *) AcquireQuantumMemory(extent,sizeof(*xml));
1294 if (xml != (char *) NULL)
1296 (void) CopyMagickString(xml,p,extent*sizeof(*xml));
1300 if (p == (char *) NULL)
1301 ThrowFatalException(ResourceLimitFatalError,
1302 "MemoryAllocationFailed");
1304 entity=strchr(xml,';');
1306 (void) CopyMagickMemory(xml+length,entity+1,strlen(entity));
1307 (void) strncpy(xml,entities[i],length);
1311 if (((state == ' ') || (state == '*')) &&
1312 (isspace((int) ((unsigned char) *xml) != 0)))
1320 Normalize spaces for non-CDATA attributes.
1322 for (xml=p; *xml != '\0'; xml++)
1324 i=(ssize_t) strspn(xml," ");
1326 (void) CopyMagickMemory(xml,xml+i,strlen(xml+i)+1);
1327 while ((*xml != '\0') && (*xml != ' '))
1331 if ((xml >= p) && (*xml == ' '))
1334 return(p == q ? ConstantString(p) : p);
1337 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1338 const size_t length,const char state)
1343 xml_info=root->node;
1344 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1348 xml=ParseEntities(xml,root->entities,state);
1349 if (*xml_info->content != '\0')
1351 (void) ConcatenateString(&xml_info->content,xml);
1352 xml=DestroyString(xml);
1356 if (xml_info->content != (char *) NULL)
1357 xml_info->content=DestroyString(xml_info->content);
1358 xml_info->content=xml;
1362 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1363 char *magick_unused(xml),ExceptionInfo *exception)
1365 if ((root->node == (XMLTreeInfo *) NULL) ||
1366 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1368 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1369 "ParseError","unexpected closing tag </%s>",tag);
1370 return(&root->root);
1372 root->node=root->node->parent;
1373 return((XMLTreeInfo *) NULL);
1376 static MagickBooleanType ValidateEntities(char *tag,char *xml,char **entities)
1382 Check for circular entity references.
1386 while ((*xml != '\0') && (*xml != '&'))
1390 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1391 return(MagickFalse);
1393 while ((entities[i] != (char *) NULL) &&
1394 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1396 if ((entities[i] != (char *) NULL) &&
1397 (ValidateEntities(tag,entities[i+1],entities) == 0))
1398 return(MagickFalse);
1402 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1416 xml+=strcspn(xml,XMLWhitespace);
1420 xml+=strspn(xml+1,XMLWhitespace)+1;
1422 if (strcmp(target,"xml") == 0)
1424 xml=strstr(xml,"standalone");
1425 if ((xml != (char *) NULL) &&
1426 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1427 root->standalone=MagickTrue;
1430 if (root->processing_instructions[0] == (char **) NULL)
1432 root->processing_instructions=(char ***) AcquireMagickMemory(sizeof(
1433 *root->processing_instructions));
1434 if (root->processing_instructions ==(char ***) NULL)
1435 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1436 *root->processing_instructions=(char **) NULL;
1439 while ((root->processing_instructions[i] != (char **) NULL) &&
1440 (strcmp(target,root->processing_instructions[i][0]) != 0))
1442 if (root->processing_instructions[i] == (char **) NULL)
1444 root->processing_instructions=(char ***) ResizeQuantumMemory(
1445 root->processing_instructions,(size_t) (i+2),
1446 sizeof(*root->processing_instructions));
1447 if (root->processing_instructions == (char ***) NULL)
1448 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1449 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1450 sizeof(**root->processing_instructions));
1451 if (root->processing_instructions[i] == (char **) NULL)
1452 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1453 root->processing_instructions[i+1]=(char **) NULL;
1454 root->processing_instructions[i][0]=ConstantString(target);
1455 root->processing_instructions[i][1]=(char *)
1456 root->processing_instructions[i+1];
1457 root->processing_instructions[i+1]=(char **) NULL;
1458 root->processing_instructions[i][2]=ConstantString("");
1461 while (root->processing_instructions[i][j] != (char *) NULL)
1463 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1464 root->processing_instructions[i],(size_t) (j+3),
1465 sizeof(**root->processing_instructions));
1466 if (root->processing_instructions[i] == (char **) NULL)
1467 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1468 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1469 root->processing_instructions[i][j+1],(size_t) (j+1),
1470 sizeof(**root->processing_instructions));
1471 if (root->processing_instructions[i][j+2] == (char *) NULL)
1472 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1473 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1474 root->root.tag != (char *) NULL ? ">" : "<",2);
1475 root->processing_instructions[i][j]=ConstantString(xml);
1476 root->processing_instructions[i][j+1]=(char *) NULL;
1479 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1480 size_t length,ExceptionInfo *exception)
1486 **predefined_entitites,
1498 predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1499 if (predefined_entitites == (char **) NULL)
1500 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1501 (void) CopyMagickMemory(predefined_entitites,sentinel,sizeof(sentinel));
1502 for (xml[length]='\0'; xml != (char *) NULL; )
1504 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1508 if (strncmp(xml,"<!ENTITY",8) == 0)
1511 Parse entity definitions.
1513 xml+=strspn(xml+8,XMLWhitespace)+8;
1515 n=xml+strspn(xml,XMLWhitespace "%");
1516 xml=n+strcspn(n,XMLWhitespace);
1518 v=xml+strspn(xml+1,XMLWhitespace)+1;
1521 if ((q != '"') && (q != '\''))
1526 xml=strchr(xml,'>');
1529 entities=(*c == '%') ? predefined_entitites : root->entities;
1530 for (i=0; entities[i] != (char *) NULL; i++) ;
1531 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1533 if (entities == (char **) NULL)
1534 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1536 predefined_entitites=entities;
1538 root->entities=entities;
1542 if (xml != (char *) NULL)
1547 entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1548 entities[i+2]=(char *) NULL;
1549 if (ValidateEntities(n,entities[i+1],entities) != MagickFalse)
1553 if (entities[i+1] != v)
1554 entities[i+1]=DestroyString(entities[i+1]);
1555 (void) ThrowMagickException(exception,GetMagickModule(),
1556 OptionWarning,"ParseError","circular entity declaration &%s",n);
1557 predefined_entitites=(char **) RelinquishMagickMemory(
1558 predefined_entitites);
1559 return(MagickFalse);
1563 if (strncmp(xml,"<!ATTLIST",9) == 0)
1566 Parse default attributes.
1568 t=xml+strspn(xml+9,XMLWhitespace)+9;
1571 (void) ThrowMagickException(exception,GetMagickModule(),
1572 OptionWarning,"ParseError","unclosed <!ATTLIST");
1573 predefined_entitites=(char **) RelinquishMagickMemory(
1574 predefined_entitites);
1575 return(MagickFalse);
1577 xml=t+strcspn(t,XMLWhitespace ">");
1582 while ((root->attributes[i] != (char **) NULL) &&
1583 (strcmp(n,root->attributes[i][0]) != 0))
1585 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1588 xml=n+strcspn(n,XMLWhitespace);
1593 (void) ThrowMagickException(exception,GetMagickModule(),
1594 OptionWarning,"ParseError","malformed <!ATTLIST");
1595 predefined_entitites=(char **) RelinquishMagickMemory(
1596 predefined_entitites);
1597 return(MagickFalse);
1599 xml+=strspn(xml+1,XMLWhitespace)+1;
1600 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1601 if (strncmp(xml,"NOTATION",8) == 0)
1602 xml+=strspn(xml+8,XMLWhitespace)+8;
1603 xml=(*xml == '(') ? strchr(xml,')') : xml+
1604 strcspn(xml,XMLWhitespace);
1605 if (xml == (char *) NULL)
1607 (void) ThrowMagickException(exception,GetMagickModule(),
1608 OptionWarning,"ParseError","malformed <!ATTLIST");
1609 predefined_entitites=(char **) RelinquishMagickMemory(
1610 predefined_entitites);
1611 return(MagickFalse);
1613 xml+=strspn(xml,XMLWhitespace ")");
1614 if (strncmp(xml,"#FIXED",6) == 0)
1615 xml+=strspn(xml+6,XMLWhitespace)+6;
1618 xml+=strcspn(xml,XMLWhitespace ">")-1;
1624 if (((*xml == '"') || (*xml == '\'')) &&
1625 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1629 (void) ThrowMagickException(exception,GetMagickModule(),
1630 OptionWarning,"ParseError","malformed <!ATTLIST");
1631 predefined_entitites=(char **) RelinquishMagickMemory(
1632 predefined_entitites);
1633 return(MagickFalse);
1635 if (root->attributes[i] == (char **) NULL)
1641 root->attributes=(char ***) AcquireQuantumMemory(2,
1642 sizeof(*root->attributes));
1644 root->attributes=(char ***) ResizeQuantumMemory(
1645 root->attributes,(size_t) (i+2),
1646 sizeof(*root->attributes));
1647 if (root->attributes == (char ***) NULL)
1648 ThrowFatalException(ResourceLimitFatalError,
1649 "MemoryAllocationFailed");
1650 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1651 sizeof(*root->attributes));
1652 if (root->attributes[i] == (char **) NULL)
1653 ThrowFatalException(ResourceLimitFatalError,
1654 "MemoryAllocationFailed");
1655 root->attributes[i][0]=ConstantString(t);
1656 root->attributes[i][1]=(char *) NULL;
1657 root->attributes[i+1]=(char **) NULL;
1659 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1660 root->attributes[i]=(char **) ResizeQuantumMemory(
1661 root->attributes[i],(size_t) (j+4),sizeof(*root->attributes));
1662 if (root->attributes[i] == (char **) NULL)
1663 ThrowFatalException(ResourceLimitFatalError,
1664 "MemoryAllocationFailed");
1665 root->attributes[i][j+3]=(char *) NULL;
1666 root->attributes[i][j+2]=ConstantString(c);
1667 root->attributes[i][j+1]=(char *) NULL;
1668 if (v != (char *) NULL)
1669 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1670 root->attributes[i][j]=ConstantString(n);
1674 if (strncmp(xml, "<!--", 4) == 0)
1675 xml=strstr(xml+4,"-->");
1677 if (strncmp(xml,"<?", 2) == 0)
1681 if (xml != (char *) NULL)
1683 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1689 xml=strchr(xml,'>');
1691 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1694 predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1698 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1703 xml_info=root->node;
1704 if (xml_info->tag == (char *) NULL)
1705 xml_info->tag=ConstantString(tag);
1707 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1708 xml_info->attributes=attributes;
1709 root->node=xml_info;
1712 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1744 Convert xml-string to UTF8.
1746 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1748 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1749 "ParseError","root tag missing");
1750 return((XMLTreeInfo *) NULL);
1752 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1754 utf8=ConvertUTF16ToUTF8(xml,&length);
1755 if (utf8 == (char *) NULL)
1757 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1758 "ParseError","UTF16 to UTF8 failed");
1759 return((XMLTreeInfo *) NULL);
1761 terminal=utf8[length-1];
1762 utf8[length-1]='\0';
1764 while ((*p != '\0') && (*p != '<'))
1768 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1769 "ParseError","root tag missing");
1770 utf8=DestroyString(utf8);
1771 return((XMLTreeInfo *) NULL);
1773 attribute=(char **) NULL;
1776 attributes=(char **) sentinel;
1779 if ((isalpha((int) ((unsigned char) *p)) !=0) || (*p == '_') ||
1780 (*p == ':') || (c < '\0'))
1785 if (root->node == (XMLTreeInfo *) NULL)
1787 (void) ThrowMagickException(exception,GetMagickModule(),
1788 OptionWarning,"ParseError","root tag missing");
1789 utf8=DestroyString(utf8);
1790 return(&root->root);
1792 p+=strcspn(p,XMLWhitespace "/>");
1793 while (isspace((int) ((unsigned char) *p)) != 0)
1795 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1798 Find tag in default attributes list.
1801 while ((root->attributes[i] != (char **) NULL) &&
1802 (strcmp(root->attributes[i][0],tag) != 0))
1804 attribute=root->attributes[i];
1806 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1812 attributes=(char **) AcquireQuantumMemory(4,sizeof(*attributes));
1814 attributes=(char **) ResizeQuantumMemory(attributes,(size_t) (l+4),
1815 sizeof(*attributes));
1816 if (attributes == (char **) NULL)
1818 (void) ThrowMagickException(exception,GetMagickModule(),
1819 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1820 utf8=DestroyString(utf8);
1821 return(&root->root);
1823 attributes[l+2]=(char *) NULL;
1824 attributes[l+1]=(char *) NULL;
1826 p+=strcspn(p,XMLWhitespace "=/>");
1827 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1828 attributes[l]=ConstantString("");
1832 p+=strspn(p,XMLWhitespace "=");
1834 if ((c == '"') || (c == '\''))
1841 while ((*p != '\0') && (*p != c))
1847 attributes[l]=ConstantString("");
1848 attributes[l+1]=ConstantString("");
1849 (void) DestroyXMLTreeAttributes(attributes);
1850 (void) ThrowMagickException(exception,GetMagickModule(),
1851 OptionWarning,"ParseError","missing %c",c);
1852 utf8=DestroyString(utf8);
1853 return(&root->root);
1856 while ((attribute != (char **) NULL) &&
1857 (attribute[j] != (char *) NULL) &&
1858 (strcmp(attribute[j],attributes[l]) != 0))
1860 attributes[l+1]=ParseEntities(attributes[l+1],root->entities,
1861 (attribute != (char **) NULL) && (attribute[j] !=
1862 (char *) NULL) ? *attribute[j+2] : ' ');
1864 attributes[l]=ConstantString(attributes[l]);
1866 while (isspace((int) ((unsigned char) *p)) != 0)
1875 if (((*p != '\0') && (*p != '>')) ||
1876 ((*p == '\0') && (terminal != '>')))
1879 (void) DestroyXMLTreeAttributes(attributes);
1880 (void) ThrowMagickException(exception,GetMagickModule(),
1881 OptionWarning,"ParseError","missing >");
1882 utf8=DestroyString(utf8);
1883 return(&root->root);
1885 ParseOpenTag(root,tag,attributes);
1886 (void) ParseCloseTag(root,tag,p,exception);
1891 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1894 ParseOpenTag(root,tag,attributes);
1900 (void) DestroyXMLTreeAttributes(attributes);
1901 (void) ThrowMagickException(exception,GetMagickModule(),
1902 OptionWarning,"ParseError","missing >");
1903 utf8=DestroyString(utf8);
1904 return(&root->root);
1915 p+=strcspn(tag,XMLWhitespace ">")+1;
1917 if ((c == '\0') && (terminal != '>'))
1919 (void) ThrowMagickException(exception,GetMagickModule(),
1920 OptionWarning,"ParseError","missing >");
1921 utf8=DestroyString(utf8);
1922 return(&root->root);
1925 if (ParseCloseTag(root,tag,p,exception) != (XMLTreeInfo *) NULL)
1927 utf8=DestroyString(utf8);
1928 return(&root->root);
1931 if (isspace((int) ((unsigned char) *p)) != 0)
1932 p+=strspn(p,XMLWhitespace);
1935 if (strncmp(p,"!--",3) == 0)
1941 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1942 ((*p == '\0') && (terminal != '>')))
1944 (void) ThrowMagickException(exception,GetMagickModule(),
1945 OptionWarning,"ParseError","unclosed <!--");
1946 utf8=DestroyString(utf8);
1947 return(&root->root);
1951 if (strncmp(p,"![CDATA[",8) == 0)
1957 if (p != (char *) NULL)
1960 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1964 (void) ThrowMagickException(exception,GetMagickModule(),
1965 OptionWarning,"ParseError","unclosed <![CDATA[");
1966 utf8=DestroyString(utf8);
1967 return(&root->root);
1971 if (strncmp(p,"!DOCTYPE",8) == 0)
1976 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1977 ((l != 0) && ((*p != ']') ||
1978 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1979 l=(ssize_t) ((*p == '[') ? 1 : l))
1980 p+=strcspn(p+1,"[]>")+1;
1981 if ((*p == '\0') && (terminal != '>'))
1983 (void) ThrowMagickException(exception,GetMagickModule(),
1984 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1985 utf8=DestroyString(utf8);
1986 return(&root->root);
1989 tag=strchr(tag,'[')+1;
1992 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1994 if (status == MagickFalse)
1996 utf8=DestroyString(utf8);
1997 return(&root->root);
2006 Processing instructions.
2011 if (p == (char *) NULL)
2014 } while ((*p != '\0') && (*p != '>'));
2015 if ((p == (char *) NULL) || ((*p == '\0') &&
2018 (void) ThrowMagickException(exception,GetMagickModule(),
2019 OptionWarning,"ParseError","unclosed <?");
2020 utf8=DestroyString(utf8);
2021 return(&root->root);
2023 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2027 (void) ThrowMagickException(exception,GetMagickModule(),
2028 OptionWarning,"ParseError","unexpected <");
2029 utf8=DestroyString(utf8);
2030 return(&root->root);
2032 if ((p == (char *) NULL) || (*p == '\0'))
2036 if ((*p != '\0') && (*p != '<'))
2039 Tag character content.
2041 while ((*p != '\0') && (*p != '<'))
2045 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2051 utf8=DestroyString(utf8);
2052 if (root->node == (XMLTreeInfo *) NULL)
2053 return(&root->root);
2054 if (root->node->tag == (char *) NULL)
2056 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2057 "ParseError","root tag missing");
2058 return(&root->root);
2060 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2061 "ParseError","unclosed tag: '%s'",root->node->tag);
2062 return(&root->root);
2066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070 % N e w X M L T r e e T a g %
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2078 % The format of the NewXMLTreeTag method is:
2080 % XMLTreeInfo *NewXMLTreeTag(const char *tag)
2082 % A description of each parameter follows:
2087 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2090 *predefined_entities[NumberPredefinedEntities+1] =
2092 "lt;", "<", "gt;", ">", "quot;", """,
2093 "apos;", "'", "amp;", "&", (char *) NULL
2099 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2100 if (root == (XMLTreeRoot *) NULL)
2101 return((XMLTreeInfo *) NULL);
2102 (void) ResetMagickMemory(root,0,sizeof(*root));
2103 root->root.tag=(char *) NULL;
2104 if (tag != (char *) NULL)
2105 root->root.tag=ConstantString(tag);
2106 root->node=(&root->root);
2107 root->root.content=ConstantString("");
2108 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2109 if (root->entities == (char **) NULL)
2110 return((XMLTreeInfo *) NULL);
2111 (void) CopyMagickMemory(root->entities,predefined_entities,
2112 sizeof(predefined_entities));
2113 root->root.attributes=sentinel;
2114 root->attributes=(char ***) root->root.attributes;
2115 root->processing_instructions=(char ***) root->root.attributes;
2116 root->debug=IsEventLogging();
2117 root->signature=MagickSignature;
2118 return(&root->root);
2122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2126 % P r u n e T a g F r o m X M L T r e e %
2130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2132 % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2135 % The format of the PruneTagFromXMLTree method is:
2137 % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2139 % A description of each parameter follows:
2141 % o xml_info: the xml info.
2144 MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2149 assert(xml_info != (XMLTreeInfo *) NULL);
2150 assert((xml_info->signature == MagickSignature) ||
2151 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2153 if (xml_info->next != (XMLTreeInfo *) NULL)
2154 xml_info->next->sibling=xml_info->sibling;
2155 if (xml_info->parent != (XMLTreeInfo *) NULL)
2157 node=xml_info->parent->child;
2158 if (node == xml_info)
2159 xml_info->parent->child=xml_info->ordered;
2162 while (node->ordered != xml_info)
2164 node->ordered=node->ordered->ordered;
2165 node=xml_info->parent->child;
2166 if (strcmp(node->tag,xml_info->tag) != 0)
2168 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2170 if (node->sibling != xml_info)
2173 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2174 xml_info->next : node->sibling->sibling;
2176 while ((node->next != (XMLTreeInfo *) NULL) &&
2177 (node->next != xml_info))
2179 if (node->next != (XMLTreeInfo *) NULL)
2180 node->next=node->next->next;
2183 xml_info->ordered=(XMLTreeInfo *) NULL;
2184 xml_info->sibling=(XMLTreeInfo *) NULL;
2185 xml_info->next=(XMLTreeInfo *) NULL;
2190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2194 % S e t X M L T r e e A t t r i b u t e %
2198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2200 % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2201 % found. A value of NULL removes the specified attribute.
2203 % The format of the SetXMLTreeAttribute method is:
2205 % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2206 % const char *value)
2208 % A description of each parameter follows:
2210 % o xml_info: the xml info.
2212 % o tag: The attribute tag.
2214 % o value: The attribute value.
2217 MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2218 const char *tag,const char *value)
2226 assert(xml_info != (XMLTreeInfo *) NULL);
2227 assert((xml_info->signature == MagickSignature) ||
2228 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2231 while ((xml_info->attributes[i] != (char *) NULL) &&
2232 (strcmp(xml_info->attributes[i],tag) != 0))
2234 if (xml_info->attributes[i] == (char *) NULL)
2237 Add new attribute tag.
2239 if (value == (const char *) NULL)
2241 if (xml_info->attributes != sentinel)
2242 xml_info->attributes=(char **) ResizeQuantumMemory(
2243 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2246 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2247 sizeof(*xml_info->attributes));
2248 if (xml_info->attributes != (char **) NULL)
2249 xml_info->attributes[1]=ConstantString("");
2251 if (xml_info->attributes == (char **) NULL)
2252 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2253 xml_info->attributes[i]=ConstantString(tag);
2254 xml_info->attributes[i+2]=(char *) NULL;
2255 (void) strlen(xml_info->attributes[i+1]);
2258 Add new value to an existing attribute.
2260 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2261 if (xml_info->attributes[i+1] != (char *) NULL)
2262 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2263 if (value != (const char *) NULL)
2265 xml_info->attributes[i+1]=ConstantString(value);
2268 if (xml_info->attributes[i] != (char *) NULL)
2269 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2270 (void) CopyMagickMemory(xml_info->attributes+i,xml_info->attributes+i+2,
2271 (size_t) (j-i)*sizeof(*xml_info->attributes));
2272 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2273 (size_t) (j+2),sizeof(*xml_info->attributes));
2274 if (xml_info->attributes == (char **) NULL)
2275 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2277 (void) CopyMagickMemory(xml_info->attributes[j+1]+(i/2),
2278 xml_info->attributes[j+1]+(i/2)+1,(size_t) (((j+2)/2)-(i/2))*
2279 sizeof(*xml_info->attributes));
2284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288 % S e t X M L T r e e C o n t e n t %
2292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2294 % SetXMLTreeContent() sets the character content for the given tag and
2297 % The format of the SetXMLTreeContent method is:
2299 % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2300 % const char *content)
2302 % A description of each parameter follows:
2304 % o xml_info: the xml info.
2306 % o content: The content.
2309 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2310 const char *content)
2312 assert(xml_info != (XMLTreeInfo *) NULL);
2313 assert((xml_info->signature == MagickSignature) ||
2314 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2316 if (xml_info->content != (char *) NULL)
2317 xml_info->content=DestroyString(xml_info->content);
2318 xml_info->content=(char *) ConstantString(content);
2323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327 % X M L T r e e I n f o T o X M L %
2331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2333 % XMLTreeInfoToXML() converts an xml-tree to an XML string.
2335 % The format of the XMLTreeInfoToXML method is:
2337 % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2339 % A description of each parameter follows:
2341 % o xml_info: the xml info.
2345 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2346 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2352 canonical_content=CanonicalXMLContent(source,pedantic);
2358 content=AcquireString(source);
2359 content[offset]='\0';
2360 canonical_content=CanonicalXMLContent(content,pedantic);
2361 content=DestroyString(content);
2363 if (canonical_content == (char *) NULL)
2364 return(*destination);
2365 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2367 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2368 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2369 sizeof(**destination));
2370 if (*destination == (char *) NULL)
2371 return(*destination);
2373 *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2375 canonical_content=DestroyString(canonical_content);
2376 return(*destination);
2379 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2380 size_t *extent,size_t start,char ***attributes)
2397 content=(char *) "";
2398 if (xml_info->parent != (XMLTreeInfo *) NULL)
2399 content=xml_info->parent->content;
2401 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2402 start),source,length,extent,MagickFalse);
2403 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2405 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2406 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(*source));
2407 if (*source == (char *) NULL)
2410 *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2411 for (i=0; xml_info->attributes[i]; i+=2)
2413 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2414 if (attribute != xml_info->attributes[i+1])
2416 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2418 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2419 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2420 if (*source == (char *) NULL)
2421 return((char *) NULL);
2423 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2424 xml_info->attributes[i]);
2425 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2427 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2430 while ((attributes[i] != (char **) NULL) &&
2431 (strcmp(attributes[i][0],xml_info->tag) != 0))
2434 while ((attributes[i] != (char **) NULL) &&
2435 (attributes[i][j] != (char *) NULL))
2437 if ((attributes[i][j+1] == (char *) NULL) ||
2438 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2443 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2445 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2446 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2447 if (*source == (char *) NULL)
2448 return((char *) NULL);
2450 *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2452 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2454 *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2457 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2459 if (xml_info->child != (XMLTreeInfo *) NULL)
2460 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2462 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2464 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2466 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2467 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2468 if (*source == (char *) NULL)
2469 return((char *) NULL);
2471 if (*xml_info->content != '\0')
2472 *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2474 while ((content[offset] != '\0') && (offset < xml_info->offset))
2476 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2477 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2480 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2485 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2512 assert(xml_info != (XMLTreeInfo *) NULL);
2513 assert((xml_info->signature == MagickSignature) ||
2514 (((XMLTreeRoot *) xml_info)->signature == MagickSignature));
2515 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2516 if (xml_info->tag == (char *) NULL)
2517 return((char *) NULL);
2518 xml=AcquireString((char *) NULL);
2520 extent=MaxTextExtent;
2521 root=(XMLTreeRoot *) xml_info;
2522 while (root->root.parent != (XMLTreeInfo *) NULL)
2523 root=(XMLTreeRoot *) root->root.parent;
2524 parent=(XMLTreeInfo *) NULL;
2525 if (xml_info != (XMLTreeInfo *) NULL)
2526 parent=xml_info->parent;
2527 if (parent == (XMLTreeInfo *) NULL)
2528 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2531 Pre-root processing instructions.
2533 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2534 p=root->processing_instructions[i][1];
2535 for (j=1; p != (char *) NULL; j++)
2537 if (root->processing_instructions[i][k][j-1] == '>')
2539 p=root->processing_instructions[i][j];
2542 q=root->processing_instructions[i][0];
2543 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2545 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2546 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2547 if (xml == (char *) NULL)
2550 length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2551 *p != '\0' ? " " : "",p);
2552 p=root->processing_instructions[i][j];
2555 ordered=(XMLTreeInfo *) NULL;
2556 if (xml_info != (XMLTreeInfo *) NULL)
2557 ordered=xml_info->ordered;
2558 xml_info->parent=(XMLTreeInfo *) NULL;
2559 xml_info->ordered=(XMLTreeInfo *) NULL;
2560 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2561 xml_info->parent=parent;
2562 xml_info->ordered=ordered;
2563 if (parent == (XMLTreeInfo *) NULL)
2564 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2567 Post-root processing instructions.
2569 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2570 p=root->processing_instructions[i][1];
2571 for (j=1; p != (char *) NULL; j++)
2573 if (root->processing_instructions[i][k][j-1] == '<')
2575 p=root->processing_instructions[i][j];
2578 q=root->processing_instructions[i][0];
2579 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2581 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2582 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2583 if (xml == (char *) NULL)
2586 length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2587 *p != '\0' ? " " : "",p);
2588 p=root->processing_instructions[i][j];
2591 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));