1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             X   X  M   M  L                                 %
7 %                              X X   MM MM  L                                 %
8 %                               X    M M M  L                                 %
9 %                              X X   M   M  L                                 %
10 %                             X   X  M   M  LLLLL                             %
11 %                                                                             %
12 %                         TTTTT  RRRR   EEEEE  EEEEE                          %
13 %                           T    R   R  E      E                              %
14 %                           T    RRRR   EEE    EEE                            %
15 %                           T    R R    E      E                              %
16 %                           T    R  R   EEEEE  EEEEE                          %
17 %                                                                             %
18 %                                                                             %
19 %                              XML Tree Methods                               %
20 %                                                                             %
21 %                              Software Design                                %
22 %                                   Cristy                                    %
23 %                               December 2004                                 %
24 %                                                                             %
25 %                                                                             %
26 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
27 %  dedicated to making software imaging solutions freely available.           %
28 %                                                                             %
29 %  You may not use this file except in compliance with the License.  You may  %
30 %  obtain a copy of the License at                                            %
31 %                                                                             %
32 %    https://imagemagick.org/script/license.php                               %
33 %                                                                             %
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.                                             %
39 %                                                                             %
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %
42 %  This module implements the standard handy xml-tree methods for storing and
43 %  retrieving nodes and attributes from an XML string.
44 %
45 */
46 
47 /*
48   Include declarations.
49 */
50 #include "magick/studio.h"
51 #include "magick/blob.h"
52 #include "magick/blob-private.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/image-private.h"
56 #include "magick/log.h"
57 #include "magick/memory_.h"
58 #include "magick/semaphore.h"
59 #include "magick/string_.h"
60 #include "magick/string-private.h"
61 #include "magick/token-private.h"
62 #include "magick/utility.h"
63 #include "magick/utility-private.h"
64 #include "magick/xml-tree.h"
65 #include "magick/xml-tree-private.h"
66 
67 /*
68   Define declarations.
69 */
70 #define NumberPredefinedEntities  10
71 #define XMLWhitespace "\t\r\n "
72 
73 /*
74   Typedef declarations.
75 */
76 struct _XMLTreeInfo
77 {
78   char
79     *tag,
80     **attributes,
81     *content;
82 
83   size_t
84     offset;
85 
86   XMLTreeInfo
87     *parent,
88     *next,
89     *sibling,
90     *ordered,
91     *child;
92 
93   MagickBooleanType
94     debug;
95 
96   SemaphoreInfo
97     *semaphore;
98 
99   size_t
100     signature;
101 };
102 
103 typedef struct _XMLTreeRoot
104   XMLTreeRoot;
105 
106 struct _XMLTreeRoot
107 {
108   struct _XMLTreeInfo
109     root;
110 
111   XMLTreeInfo
112     *node;
113 
114   MagickBooleanType
115     standalone;
116 
117   char
118     ***processing_instructions,
119     **entities,
120     ***attributes;
121 
122   MagickBooleanType
123     debug;
124 
125   SemaphoreInfo
126     *semaphore;
127 
128   size_t
129     signature;
130 };
131 
132 /*
133   Global declarations.
134 */
135 static char
136   *sentinel[] = { (char *) NULL };
137 
138 /*
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %                                                                             %
141 %                                                                             %
142 %                                                                             %
143 %   A d d C h i l d T o X M L T r e e                                         %
144 %                                                                             %
145 %                                                                             %
146 %                                                                             %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %
149 %  AddChildToXMLTree() adds a child tag at an offset relative to the start of
150 %  the parent tag's character content.  Return the child tag.
151 %
152 %  The format of the AddChildToXMLTree method is:
153 %
154 %      XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
155 %        const size_t offset)
156 %
157 %  A description of each parameter follows:
158 %
159 %    o xml_info: the xml info.
160 %
161 %    o tag: the tag.
162 %
163 %    o offset: the tag offset.
164 %
165 */
AddChildToXMLTree(XMLTreeInfo * xml_info,const char * tag,const size_t offset)166 MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
167   const char *tag,const size_t offset)
168 {
169   XMLTreeInfo
170     *child;
171 
172   if (xml_info == (XMLTreeInfo *) NULL)
173     return((XMLTreeInfo *) NULL);
174   child=(XMLTreeInfo *) AcquireMagickMemory(sizeof(*child));
175   if (child == (XMLTreeInfo *) NULL)
176     return((XMLTreeInfo *) NULL);
177   (void) memset(child,0,sizeof(*child));
178   child->tag=ConstantString(tag);
179   child->attributes=sentinel;
180   child->content=ConstantString("");
181   child->debug=IsEventLogging();
182   child->signature=MagickCoreSignature;
183   return(InsertTagIntoXMLTree(xml_info,child,offset));
184 }
185 
186 /*
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 %                                                                             %
189 %                                                                             %
190 %                                                                             %
191 %   A d d P a t h T o X M L T r e e                                           %
192 %                                                                             %
193 %                                                                             %
194 %                                                                             %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 %
197 %  AddPathToXMLTree() adds a child tag at an offset relative to the start of
198 %  the parent tag's character content.  This method returns the child tag.
199 %
200 %  The format of the AddPathToXMLTree method is:
201 %
202 %      XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
203 %        const size_t offset)
204 %
205 %  A description of each parameter follows:
206 %
207 %    o xml_info: the xml info.
208 %
209 %    o path: the path.
210 %
211 %    o offset: the tag offset.
212 %
213 */
AddPathToXMLTree(XMLTreeInfo * xml_info,const char * path,const size_t offset)214 MagickExport XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
215   const char *path,const size_t offset)
216 {
217   char
218     **components,
219     subnode[MaxTextExtent],
220     tag[MaxTextExtent];
221 
222   ssize_t
223     i;
224 
225   size_t
226     number_components;
227 
228   ssize_t
229     j;
230 
231   XMLTreeInfo
232     *child,
233     *node;
234 
235   assert(xml_info != (XMLTreeInfo *) NULL);
236   assert((xml_info->signature == MagickCoreSignature) ||
237          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
238   if (xml_info->debug != MagickFalse)
239     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
240   node=xml_info;
241   components=GetPathComponents(path,&number_components);
242   if (components == (char **) NULL)
243     return((XMLTreeInfo *) NULL);
244   for (i=0; i < (ssize_t) number_components; i++)
245   {
246     GetPathComponent(components[i],SubimagePath,subnode);
247     GetPathComponent(components[i],CanonicalPath,tag);
248     child=GetXMLTreeChild(node,tag);
249     if (child == (XMLTreeInfo *) NULL)
250       child=AddChildToXMLTree(node,tag,offset);
251     node=child;
252     if (node == (XMLTreeInfo *) NULL)
253       break;
254     for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
255     {
256       node=GetXMLTreeOrdered(node);
257       if (node == (XMLTreeInfo *) NULL)
258         break;
259     }
260     if (node == (XMLTreeInfo *) NULL)
261       break;
262     components[i]=DestroyString(components[i]);
263   }
264   for ( ; i < (ssize_t) number_components; i++)
265     components[i]=DestroyString(components[i]);
266   components=(char **) RelinquishMagickMemory(components);
267   return(node);
268 }
269 
270 /*
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %                                                                             %
273 %                                                                             %
274 %                                                                             %
275 %   C a n o n i c a l X M L C o n t e n t                                     %
276 %                                                                             %
277 %                                                                             %
278 %                                                                             %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %
281 %  CanonicalXMLContent() converts text to canonical XML content by converting
282 %  to UTF-8, substituting predefined entities, wrapping as CDATA, or encoding
283 %  as base-64 as required.
284 %
285 %  The format of the CanonicalXMLContent method is:
286 %
287 %      char *CanonicalXMLContent(const char *content,
288 %        const MagickBooleanType pedantic)
289 %
290 %  A description of each parameter follows:
291 %
292 %    o content: the content.
293 %
294 %    o pedantic: if true, replace newlines and tabs with their respective
295 %      entities.
296 %
297 */
CanonicalXMLContent(const char * content,const MagickBooleanType pedantic)298 MagickExport char *CanonicalXMLContent(const char *content,
299   const MagickBooleanType pedantic)
300 {
301   char
302     *base64,
303     *canonical_content;
304 
305   const unsigned char
306     *p;
307 
308   ssize_t
309     i;
310 
311   size_t
312     extent,
313     length;
314 
315   unsigned char
316     *utf8;
317 
318   utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
319   if (utf8 == (unsigned char *) NULL)
320     return((char *) NULL);
321   for (p=utf8; *p != '\0'; p++)
322     if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
323       break;
324   if (*p != '\0')
325     {
326       /*
327         String is binary, base64-encode it.
328       */
329       base64=Base64Encode(utf8,strlen((char *) utf8),&length);
330       utf8=(unsigned char *) RelinquishMagickMemory(utf8);
331       if (base64 == (char *) NULL)
332         return((char *) NULL);
333       canonical_content=AcquireString("<base64>");
334       (void) ConcatenateString(&canonical_content,base64);
335       base64=DestroyString(base64);
336       (void) ConcatenateString(&canonical_content,"</base64>");
337       return(canonical_content);
338     }
339   /*
340     Substitute predefined entities.
341   */
342   i=0;
343   canonical_content=AcquireString((char *) NULL);
344   extent=MaxTextExtent;
345   for (p=utf8; *p != '\0'; p++)
346   {
347     if ((i+MaxTextExtent) > (ssize_t) extent)
348       {
349         extent+=MaxTextExtent;
350         canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
351           sizeof(*canonical_content));
352         if (canonical_content == (char *) NULL)
353           return(canonical_content);
354       }
355     switch (*p)
356     {
357       case '&':
358       {
359         i+=FormatLocaleString(canonical_content+i,extent,"&amp;");
360         break;
361       }
362       case '<':
363       {
364         i+=FormatLocaleString(canonical_content+i,extent,"&lt;");
365         break;
366       }
367       case '>':
368       {
369         i+=FormatLocaleString(canonical_content+i,extent,"&gt;");
370         break;
371       }
372       case '"':
373       {
374         i+=FormatLocaleString(canonical_content+i,extent,"&quot;");
375         break;
376       }
377       case '\n':
378       {
379         if (pedantic == MagickFalse)
380           {
381             canonical_content[i++]=(char) (*p);
382             break;
383           }
384         i+=FormatLocaleString(canonical_content+i,extent,"&#xA;");
385         break;
386       }
387       case '\t':
388       {
389         if (pedantic == MagickFalse)
390           {
391             canonical_content[i++]=(char) (*p);
392             break;
393           }
394         i+=FormatLocaleString(canonical_content+i,extent,"&#x9;");
395         break;
396       }
397       case '\r':
398       {
399         i+=FormatLocaleString(canonical_content+i,extent,"&#xD;");
400         break;
401       }
402       default:
403       {
404         canonical_content[i++]=(char) (*p);
405         break;
406       }
407     }
408   }
409   canonical_content[i]='\0';
410   utf8=(unsigned char *) RelinquishMagickMemory(utf8);
411   return(canonical_content);
412 }
413 
414 /*
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 %                                                                             %
417 %                                                                             %
418 %                                                                             %
419 %   D e s t r o y X M L T r e e                                               %
420 %                                                                             %
421 %                                                                             %
422 %                                                                             %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 %
425 %  DestroyXMLTree() destroys the xml-tree.
426 %
427 %  The format of the DestroyXMLTree method is:
428 %
429 %      XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
430 %
431 %  A description of each parameter follows:
432 %
433 %    o xml_info: the xml info.
434 %
435 */
436 
DestroyXMLTreeAttributes(char ** attributes)437 static char **DestroyXMLTreeAttributes(char **attributes)
438 {
439   ssize_t
440     i;
441 
442   /*
443     Destroy a tag attribute list.
444   */
445   if ((attributes == (char **) NULL) || (attributes == sentinel))
446     return((char **) NULL);
447   for (i=0; attributes[i] != (char *) NULL; i+=2)
448   {
449     /*
450       Destroy attribute tag and value.
451     */
452     if (attributes[i] != (char *) NULL)
453       attributes[i]=DestroyString(attributes[i]);
454     if (attributes[i+1] != (char *) NULL)
455       attributes[i+1]=DestroyString(attributes[i+1]);
456   }
457   attributes=(char **) RelinquishMagickMemory(attributes);
458   return((char **) NULL);
459 }
460 
DestroyXMLTreeChild(XMLTreeInfo * xml_info)461 static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
462 {
463   XMLTreeInfo
464     *child,
465     *node;
466 
467   child=xml_info->child;
468   while(child != (XMLTreeInfo *) NULL)
469   {
470     node=child;
471     child=node->child;
472     node->child=(XMLTreeInfo *) NULL;
473     (void) DestroyXMLTree(node);
474   }
475 }
476 
DestroyXMLTreeOrdered(XMLTreeInfo * xml_info)477 static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
478 {
479   XMLTreeInfo
480     *node,
481     *ordered;
482 
483   ordered=xml_info->ordered;
484   while(ordered != (XMLTreeInfo *) NULL)
485   {
486     node=ordered;
487     ordered=node->ordered;
488     node->ordered=(XMLTreeInfo *) NULL;
489     (void) DestroyXMLTree(node);
490   }
491 }
492 
DestroyXMLTreeRoot(XMLTreeInfo * xml_info)493 static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
494 {
495   char
496     **attributes;
497 
498   ssize_t
499     i;
500 
501   ssize_t
502     j;
503 
504   XMLTreeRoot
505     *root;
506 
507   assert(xml_info != (XMLTreeInfo *) NULL);
508   assert((xml_info->signature == MagickCoreSignature) ||
509          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
510   if (xml_info->debug != MagickFalse)
511     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
512   if (xml_info->parent != (XMLTreeInfo *) NULL)
513     return;
514   /*
515     Free root tag allocations.
516   */
517   root=(XMLTreeRoot *) xml_info;
518   for (i=NumberPredefinedEntities; root->entities[i] != (char *) NULL; i+=2)
519     root->entities[i+1]=DestroyString(root->entities[i+1]);
520   root->entities=(char **) RelinquishMagickMemory(root->entities);
521   for (i=0; root->attributes[i] != (char **) NULL; i++)
522   {
523     attributes=root->attributes[i];
524     if (attributes[0] != (char *) NULL)
525       attributes[0]=DestroyString(attributes[0]);
526     for (j=1; attributes[j] != (char *) NULL; j+=3)
527     {
528       if (attributes[j] != (char *) NULL)
529         attributes[j]=DestroyString(attributes[j]);
530       if (attributes[j+1] != (char *) NULL)
531         attributes[j+1]=DestroyString(attributes[j+1]);
532       if (attributes[j+2] != (char *) NULL)
533         attributes[j+2]=DestroyString(attributes[j+2]);
534     }
535     attributes=(char **) RelinquishMagickMemory(attributes);
536   }
537   if (root->attributes[0] != (char **) NULL)
538     root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
539   if (root->processing_instructions[0] != (char **) NULL)
540     {
541       for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
542       {
543         for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
544           root->processing_instructions[i][j]=DestroyString(
545             root->processing_instructions[i][j]);
546         root->processing_instructions[i][j+1]=DestroyString(
547           root->processing_instructions[i][j+1]);
548         root->processing_instructions[i]=(char **) RelinquishMagickMemory(
549           root->processing_instructions[i]);
550       }
551       root->processing_instructions=(char ***) RelinquishMagickMemory(
552         root->processing_instructions);
553     }
554 }
555 
DestroyXMLTree(XMLTreeInfo * xml_info)556 MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
557 {
558   assert(xml_info != (XMLTreeInfo *) NULL);
559   assert((xml_info->signature == MagickCoreSignature) ||
560          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
561   if (xml_info->debug != MagickFalse)
562     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
563   DestroyXMLTreeChild(xml_info);
564   DestroyXMLTreeOrdered(xml_info);
565   DestroyXMLTreeRoot(xml_info);
566   xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
567   xml_info->content=DestroyString(xml_info->content);
568   xml_info->tag=DestroyString(xml_info->tag);
569   xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
570   return((XMLTreeInfo *) NULL);
571 }
572 
573 /*
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 %                                                                             %
576 %                                                                             %
577 %                                                                             %
578 %   F i l e T o X M L                                                         %
579 %                                                                             %
580 %                                                                             %
581 %                                                                             %
582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 %
584 %  FileToXML() returns the contents of a file as a XML string.
585 %
586 %  The format of the FileToXML method is:
587 %
588 %      char *FileToXML(const char *filename,const size_t extent)
589 %
590 %  A description of each parameter follows:
591 %
592 %    o filename: the filename.
593 %
594 %    o extent: Maximum length of the string.
595 %
596 */
FileToXML(const char * filename,const size_t extent)597 MagickPrivate char *FileToXML(const char *filename,const size_t extent)
598 {
599   char
600     *xml;
601 
602   int
603     file;
604 
605   MagickOffsetType
606     offset;
607 
608   size_t
609     i;
610 
611   size_t
612     length;
613 
614   ssize_t
615     count;
616 
617   void
618     *map;
619 
620   assert(filename != (const char *) NULL);
621   length=0;
622   file=fileno(stdin);
623   if (LocaleCompare(filename,"-") != 0)
624     file=open_utf8(filename,O_RDONLY | O_BINARY,0);
625   if (file == -1)
626     return((char *) NULL);
627   offset=(MagickOffsetType) lseek(file,0,SEEK_END);
628   count=0;
629   if ((file == fileno(stdin)) || (offset < 0) ||
630       (offset != (MagickOffsetType) ((ssize_t) offset)))
631     {
632       size_t
633         quantum;
634 
635       struct stat
636         file_stats;
637 
638       /*
639         Stream is not seekable.
640       */
641       offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
642       quantum=(size_t) MagickMaxBufferExtent;
643       if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
644         quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
645       xml=(char *) AcquireQuantumMemory(quantum,sizeof(*xml));
646       for (i=0; xml != (char *) NULL; i+=count)
647       {
648         count=read(file,xml+i,quantum);
649         if (count <= 0)
650           {
651             count=0;
652             if (errno != EINTR)
653               break;
654           }
655         if (~((size_t) i) < (quantum+1))
656           {
657             xml=(char *) RelinquishMagickMemory(xml);
658             break;
659           }
660         xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
661         if ((size_t) (i+count) >= extent)
662           break;
663       }
664       if (LocaleCompare(filename,"-") != 0)
665         file=close(file);
666       if (xml == (char *) NULL)
667         return((char *) NULL);
668       if (file == -1)
669         {
670           xml=(char *) RelinquishMagickMemory(xml);
671           return((char *) NULL);
672         }
673       length=(size_t) MagickMin(i+count,extent);
674       xml[length]='\0';
675       return(xml);
676     }
677   length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
678   xml=(char *) NULL;
679   if (~length >= (MaxTextExtent-1))
680     xml=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*xml));
681   if (xml == (char *) NULL)
682     {
683       file=close(file);
684       return((char *) NULL);
685     }
686   map=MapBlob(file,ReadMode,0,length);
687   if (map != (char *) NULL)
688     {
689       (void) memcpy(xml,map,length);
690       (void) UnmapBlob(map,length);
691     }
692   else
693     {
694       (void) lseek(file,0,SEEK_SET);
695       for (i=0; i < length; i+=count)
696       {
697         count=read(file,xml+i,(size_t) MagickMin(length-i,(size_t) MAGICK_SSIZE_MAX));
698         if (count <= 0)
699           {
700             count=0;
701             if (errno != EINTR)
702               break;
703           }
704       }
705       if (i < length)
706         {
707           file=close(file)-1;
708           xml=(char *) RelinquishMagickMemory(xml);
709           return((char *) NULL);
710         }
711     }
712   xml[length]='\0';
713   if (LocaleCompare(filename,"-") != 0)
714     file=close(file);
715   if (file == -1)
716     xml=(char *) RelinquishMagickMemory(xml);
717   return(xml);
718 }
719 
720 /*
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 %                                                                             %
723 %                                                                             %
724 %                                                                             %
725 %   G e t N e x t X M L T r e e T a g                                         %
726 %                                                                             %
727 %                                                                             %
728 %                                                                             %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 %
731 %  GetNextXMLTreeTag() returns the next tag or NULL if not found.
732 %
733 %  The format of the GetNextXMLTreeTag method is:
734 %
735 %      XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
736 %
737 %  A description of each parameter follows:
738 %
739 %    o xml_info: the xml info.
740 %
741 */
GetNextXMLTreeTag(XMLTreeInfo * xml_info)742 MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
743 {
744   assert(xml_info != (XMLTreeInfo *) NULL);
745   assert((xml_info->signature == MagickCoreSignature) ||
746          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
747   if (xml_info->debug != MagickFalse)
748     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
749   return(xml_info->next);
750 }
751 
752 /*
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 %                                                                             %
755 %                                                                             %
756 %                                                                             %
757 %   G e t X M L T r e e A t t r i b u t e                                     %
758 %                                                                             %
759 %                                                                             %
760 %                                                                             %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 %
763 %  GetXMLTreeAttribute() returns the value of the attribute tag with the
764 %  specified tag if found, otherwise NULL.
765 %
766 %  The format of the GetXMLTreeAttribute method is:
767 %
768 %      const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
769 %
770 %  A description of each parameter follows:
771 %
772 %    o xml_info: the xml info.
773 %
774 %    o tag: the attribute tag.
775 %
776 */
GetXMLTreeAttribute(XMLTreeInfo * xml_info,const char * tag)777 MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
778   const char *tag)
779 {
780   ssize_t
781     i;
782 
783   ssize_t
784     j;
785 
786   XMLTreeRoot
787     *root;
788 
789   assert(xml_info != (XMLTreeInfo *) NULL);
790   assert((xml_info->signature == MagickCoreSignature) ||
791          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
792   if (xml_info->debug != MagickFalse)
793     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
794   if (xml_info->attributes == (char **) NULL)
795     return((const char *) NULL);
796   i=0;
797   while ((xml_info->attributes[i] != (char *) NULL) &&
798          (strcmp(xml_info->attributes[i],tag) != 0))
799     i+=2;
800   if (xml_info->attributes[i] != (char *) NULL)
801     return(xml_info->attributes[i+1]);
802   root=(XMLTreeRoot*) xml_info;
803   while (root->root.parent != (XMLTreeInfo *) NULL)
804     root=(XMLTreeRoot *) root->root.parent;
805   i=0;
806   while ((root->attributes[i] != (char **) NULL) &&
807          (strcmp(root->attributes[i][0],xml_info->tag) != 0))
808     i++;
809   if (root->attributes[i] == (char **) NULL)
810     return((const char *) NULL);
811   j=1;
812   while ((root->attributes[i][j] != (char *) NULL) &&
813          (strcmp(root->attributes[i][j],tag) != 0))
814     j+=3;
815   if (root->attributes[i][j] == (char *) NULL)
816     return((const char *) NULL);
817   return(root->attributes[i][j+1]);
818 }
819 
820 /*
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 %                                                                             %
823 %                                                                             %
824 %                                                                             %
825 %   G e t X M L T r e e A t t r i b u t e s                                   %
826 %                                                                             %
827 %                                                                             %
828 %                                                                             %
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 %
831 %  GetXMLTreeAttributes() injects all attributes associated with the current
832 %  tag in the specified splay-tree.
833 %
834 %  The format of the GetXMLTreeAttributes method is:
835 %
836 %      MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
837 %        SplayTreeInfo *attributes)
838 %
839 %  A description of each parameter follows:
840 %
841 %    o xml_info: the xml info.
842 %
843 %    o attributes: the attribute splay-tree.
844 %
845 */
GetXMLTreeAttributes(const XMLTreeInfo * xml_info,SplayTreeInfo * attributes)846 MagickExport MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
847   SplayTreeInfo *attributes)
848 {
849   ssize_t
850     i;
851 
852   assert(xml_info != (XMLTreeInfo *) NULL);
853   assert((xml_info->signature == MagickCoreSignature) ||
854          (((const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
855   if (xml_info->debug != MagickFalse)
856     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
857   assert(attributes != (SplayTreeInfo *) NULL);
858   if (xml_info->attributes == (char **) NULL)
859     return(MagickTrue);
860   i=0;
861   while (xml_info->attributes[i] != (char *) NULL)
862   {
863      (void) AddValueToSplayTree(attributes,
864        ConstantString(xml_info->attributes[i]),
865        ConstantString(xml_info->attributes[i+1]));
866     i+=2;
867   }
868   return(MagickTrue);
869 }
870 
871 /*
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 %                                                                             %
874 %                                                                             %
875 %                                                                             %
876 %   G e t X M L T r e e C h i l d                                             %
877 %                                                                             %
878 %                                                                             %
879 %                                                                             %
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 %
882 %  GetXMLTreeChild() returns the first child tag with the specified tag if
883 %  found, otherwise NULL.
884 %
885 %  The format of the GetXMLTreeChild method is:
886 %
887 %      XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
888 %
889 %  A description of each parameter follows:
890 %
891 %    o xml_info: the xml info.
892 %
893 */
GetXMLTreeChild(XMLTreeInfo * xml_info,const char * tag)894 MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
895 {
896   XMLTreeInfo
897     *child;
898 
899   assert(xml_info != (XMLTreeInfo *) NULL);
900   assert((xml_info->signature == MagickCoreSignature) ||
901          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
902   if (xml_info->debug != MagickFalse)
903     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
904   child=xml_info->child;
905   if (tag != (const char *) NULL)
906     while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
907       child=child->sibling;
908   return(child);
909 }
910 
911 /*
912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913 %                                                                             %
914 %                                                                             %
915 %                                                                             %
916 %   G e t X M L T r e e C o n t e n t                                         %
917 %                                                                             %
918 %                                                                             %
919 %                                                                             %
920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 %
922 %  GetXMLTreeContent() returns any content associated with specified
923 %  xml-tree node.
924 %
925 %  The format of the GetXMLTreeContent method is:
926 %
927 %      const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
928 %
929 %  A description of each parameter follows:
930 %
931 %    o xml_info: the xml info.
932 %
933 */
GetXMLTreeContent(XMLTreeInfo * xml_info)934 MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
935 {
936   assert(xml_info != (XMLTreeInfo *) NULL);
937   assert((xml_info->signature == MagickCoreSignature) ||
938          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
939   if (xml_info->debug != MagickFalse)
940     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
941   return(xml_info->content);
942 }
943 
944 /*
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 %                                                                             %
947 %                                                                             %
948 %                                                                             %
949 %   G e t X M L T r e e O r d e r e d                                         %
950 %                                                                             %
951 %                                                                             %
952 %                                                                             %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 %
955 %  GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
956 %
957 %  The format of the GetXMLTreeOrdered method is:
958 %
959 %      XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
960 %
961 %  A description of each parameter follows:
962 %
963 %    o xml_info: the xml info.
964 %
965 */
GetXMLTreeOrdered(XMLTreeInfo * xml_info)966 MagickExport XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
967 {
968   assert(xml_info != (XMLTreeInfo *) NULL);
969   assert((xml_info->signature == MagickCoreSignature) ||
970          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
971   if (xml_info->debug != MagickFalse)
972     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
973   return(xml_info->ordered);
974 }
975 
976 /*
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 %                                                                             %
979 %                                                                             %
980 %                                                                             %
981 %   G e t X M L T r e e P a t h                                               %
982 %                                                                             %
983 %                                                                             %
984 %                                                                             %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 %
987 %  GetXMLTreePath() traverses the XML-tree as defined by the specified path
988 %  and returns the node if found, otherwise NULL.
989 %
990 %  The format of the GetXMLTreePath method is:
991 %
992 %      XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
993 %
994 %  A description of each parameter follows:
995 %
996 %    o xml_info: the xml info.
997 %
998 %    o path: the path (e.g. property/elapsed-time).
999 %
1000 */
GetXMLTreePath(XMLTreeInfo * xml_info,const char * path)1001 MagickExport XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
1002 {
1003   char
1004     **components,
1005     subnode[MaxTextExtent],
1006     tag[MaxTextExtent];
1007 
1008   ssize_t
1009     i;
1010 
1011   size_t
1012     number_components;
1013 
1014   ssize_t
1015     j;
1016 
1017   XMLTreeInfo
1018     *node;
1019 
1020   assert(xml_info != (XMLTreeInfo *) NULL);
1021   assert((xml_info->signature == MagickCoreSignature) ||
1022          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1023   if (xml_info->debug != MagickFalse)
1024     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1025   node=xml_info;
1026   components=GetPathComponents(path,&number_components);
1027   if (components == (char **) NULL)
1028     return((XMLTreeInfo *) NULL);
1029   for (i=0; i < (ssize_t) number_components; i++)
1030   {
1031     GetPathComponent(components[i],SubimagePath,subnode);
1032     GetPathComponent(components[i],CanonicalPath,tag);
1033     node=GetXMLTreeChild(node,tag);
1034     if (node == (XMLTreeInfo *) NULL)
1035       break;
1036     for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1037     {
1038       node=GetXMLTreeOrdered(node);
1039       if (node == (XMLTreeInfo *) NULL)
1040         break;
1041     }
1042     if (node == (XMLTreeInfo *) NULL)
1043       break;
1044     components[i]=DestroyString(components[i]);
1045   }
1046   for ( ; i < (ssize_t) number_components; i++)
1047     components[i]=DestroyString(components[i]);
1048   components=(char **) RelinquishMagickMemory(components);
1049   return(node);
1050 }
1051 
1052 /*
1053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054 %                                                                             %
1055 %                                                                             %
1056 %                                                                             %
1057 %   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           %
1058 %                                                                             %
1059 %                                                                             %
1060 %                                                                             %
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1062 %
1063 %  GetXMLTreeProcessingInstructions() returns a null terminated array of
1064 %  processing instructions for the given target.
1065 %
1066 %  The format of the GetXMLTreeProcessingInstructions method is:
1067 %
1068 %      const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
1069 %        const char *target)
1070 %
1071 %  A description of each parameter follows:
1072 %
1073 %    o xml_info: the xml info.
1074 %
1075 */
GetXMLTreeProcessingInstructions(XMLTreeInfo * xml_info,const char * target)1076 MagickExport const char **GetXMLTreeProcessingInstructions(
1077   XMLTreeInfo *xml_info,const char *target)
1078 {
1079   ssize_t
1080     i;
1081 
1082   XMLTreeRoot
1083     *root;
1084 
1085   assert(xml_info != (XMLTreeInfo *) NULL);
1086   assert((xml_info->signature == MagickCoreSignature) ||
1087          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1088   if (xml_info->debug != MagickFalse)
1089     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1090   root=(XMLTreeRoot *) xml_info;
1091   while (root->root.parent != (XMLTreeInfo *) NULL)
1092     root=(XMLTreeRoot *) root->root.parent;
1093   i=0;
1094   while ((root->processing_instructions[i] != (char **) NULL) &&
1095          (strcmp(root->processing_instructions[i][0],target) != 0))
1096     i++;
1097   if (root->processing_instructions[i] == (char **) NULL)
1098     return((const char **) sentinel);
1099   return((const char **) (root->processing_instructions[i]+1));
1100 }
1101 
1102 /*
1103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1104 %                                                                             %
1105 %                                                                             %
1106 %                                                                             %
1107 %   G e t X M L T r e e S i b l i n g                                         %
1108 %                                                                             %
1109 %                                                                             %
1110 %                                                                             %
1111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1112 %
1113 %  GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
1114 %
1115 %  The format of the GetXMLTreeSibling method is:
1116 %
1117 %      XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1118 %
1119 %  A description of each parameter follows:
1120 %
1121 %    o xml_info: the xml info.
1122 %
1123 */
GetXMLTreeSibling(XMLTreeInfo * xml_info)1124 MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1125 {
1126   assert(xml_info != (XMLTreeInfo *) NULL);
1127   assert((xml_info->signature == MagickCoreSignature) ||
1128          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1129   if (xml_info->debug != MagickFalse)
1130     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1131   return(xml_info->sibling);
1132 }
1133 
1134 /*
1135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136 %                                                                             %
1137 %                                                                             %
1138 %                                                                             %
1139 %   G e t X M L T r e e T a g                                                 %
1140 %                                                                             %
1141 %                                                                             %
1142 %                                                                             %
1143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 %
1145 %  GetXMLTreeTag() returns the tag associated with specified xml-tree node.
1146 %
1147 %  The format of the GetXMLTreeTag method is:
1148 %
1149 %      const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1150 %
1151 %  A description of each parameter follows:
1152 %
1153 %    o xml_info: the xml info.
1154 %
1155 */
GetXMLTreeTag(XMLTreeInfo * xml_info)1156 MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1157 {
1158   assert(xml_info != (XMLTreeInfo *) NULL);
1159   assert((xml_info->signature == MagickCoreSignature) ||
1160          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1161   if (xml_info->debug != MagickFalse)
1162     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1163   return(xml_info->tag);
1164 }
1165 
1166 /*
1167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1168 %                                                                             %
1169 %                                                                             %
1170 %                                                                             %
1171 %   I n s e r t I n t o T a g X M L T r e e                                   %
1172 %                                                                             %
1173 %                                                                             %
1174 %                                                                             %
1175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176 %
1177 %  InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
1178 %  the parent tag's character content.  This method returns the child tag.
1179 %
1180 %  The format of the InsertTagIntoXMLTree method is:
1181 %
1182 %      XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1183 %        XMLTreeInfo *child,const size_t offset)
1184 %
1185 %  A description of each parameter follows:
1186 %
1187 %    o xml_info: the xml info.
1188 %
1189 %    o child: the child tag.
1190 %
1191 %    o offset: the tag offset.
1192 %
1193 */
InsertTagIntoXMLTree(XMLTreeInfo * xml_info,XMLTreeInfo * child,const size_t offset)1194 MagickExport XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1195   XMLTreeInfo *child,const size_t offset)
1196 {
1197   XMLTreeInfo
1198     *head,
1199     *node,
1200     *previous;
1201 
1202   child->ordered=(XMLTreeInfo *) NULL;
1203   child->sibling=(XMLTreeInfo *) NULL;
1204   child->next=(XMLTreeInfo *) NULL;
1205   child->offset=offset;
1206   child->parent=xml_info;
1207   if (xml_info->child == (XMLTreeInfo *) NULL)
1208     {
1209       xml_info->child=child;
1210       return(child);
1211     }
1212   head=xml_info->child;
1213   if (head->offset > offset)
1214     {
1215       child->ordered=head;
1216       xml_info->child=child;
1217     }
1218   else
1219     {
1220       node=head;
1221       while ((node->ordered != (XMLTreeInfo *) NULL) &&
1222              (node->ordered->offset <= offset))
1223         node=node->ordered;
1224       child->ordered=node->ordered;
1225       node->ordered=child;
1226     }
1227   previous=(XMLTreeInfo *) NULL;
1228   node=head;
1229   while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1230   {
1231     previous=node;
1232     node=node->sibling;
1233   }
1234   if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1235     {
1236       while ((node->next != (XMLTreeInfo *) NULL) &&
1237              (node->next->offset <= offset))
1238         node=node->next;
1239       child->next=node->next;
1240       node->next=child;
1241     }
1242   else
1243     {
1244       if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1245         previous->sibling=node->sibling;
1246       child->next=node;
1247       previous=(XMLTreeInfo *) NULL;
1248       node=head;
1249       while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1250       {
1251         previous=node;
1252         node=node->sibling;
1253       }
1254       child->sibling=node;
1255       if (previous != (XMLTreeInfo *) NULL)
1256         previous->sibling=child;
1257     }
1258   return(child);
1259 }
1260 
1261 /*
1262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263 %                                                                             %
1264 %                                                                             %
1265 %                                                                             %
1266 %   N e w X M L T r e e                                                       %
1267 %                                                                             %
1268 %                                                                             %
1269 %                                                                             %
1270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271 %
1272 %  NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
1273 %  XML string.
1274 %
1275 %  The format of the NewXMLTree method is:
1276 %
1277 %      XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1278 %
1279 %  A description of each parameter follows:
1280 %
1281 %    o xml:  A null-terminated XML string.
1282 %
1283 %    o exception: return any errors or warnings in this structure.
1284 %
1285 */
1286 
ConvertUTF16ToUTF8(const char * content,size_t * length)1287 static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
1288 {
1289   char
1290     *utf8;
1291 
1292   int
1293     bits,
1294     byte,
1295     c,
1296     encoding;
1297 
1298   ssize_t
1299     i;
1300 
1301   size_t
1302     extent;
1303 
1304   ssize_t
1305     j;
1306 
1307   utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
1308   if (utf8 == (char *) NULL)
1309     return((char *) NULL);
1310   encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
1311   if (encoding == -1)
1312     {
1313       /*
1314         Already UTF-8.
1315       */
1316       (void) memcpy(utf8,content,*length*sizeof(*utf8));
1317       utf8[*length]='\0';
1318       return(utf8);
1319     }
1320   j=0;
1321   extent=(*length);
1322   for (i=2; i < (ssize_t) (*length-1); i+=2)
1323   {
1324     c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1325       ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1326     if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1327       {
1328         byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1329           (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1330           (content[i] & 0xff);
1331         c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1332       }
1333     if ((size_t) (j+MaxTextExtent) > extent)
1334       {
1335         extent=(size_t) j+MaxTextExtent;
1336         utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1337         if (utf8 == (char *) NULL)
1338           return(utf8);
1339       }
1340     if (c < 0x80)
1341       {
1342         utf8[j]=c;
1343         j++;
1344         continue;
1345       }
1346     /*
1347       Multi-byte UTF-8 sequence.
1348     */
1349     byte=c;
1350     for (bits=0; byte != 0; byte/=2)
1351       bits++;
1352     bits=(bits-2)/5;
1353     utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1354     while (bits != 0)
1355     {
1356       bits--;
1357       utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1358       j++;
1359     }
1360   }
1361   *length=(size_t) j;
1362   utf8=(char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8));
1363   if (utf8 != (char *) NULL)
1364     utf8[*length]='\0';
1365   return(utf8);
1366 }
1367 
ParseEntities(char * xml,char ** entities,int state)1368 static char *ParseEntities(char *xml,char **entities,int state)
1369 {
1370   char
1371     *entity;
1372 
1373   int
1374     byte,
1375     c;
1376 
1377   char
1378     *p,
1379     *q;
1380 
1381   ssize_t
1382     i;
1383 
1384   size_t
1385     extent,
1386     length;
1387 
1388   ssize_t
1389     offset;
1390 
1391   /*
1392     Normalize line endings.
1393   */
1394   p=xml;
1395   q=xml;
1396   for ( ; *xml != '\0'; xml++)
1397     while (*xml == '\r')
1398     {
1399       *(xml++)='\n';
1400       if (*xml == '\n')
1401         (void) memmove(xml,xml+1,strlen(xml));
1402     }
1403   for (xml=p; ; )
1404   {
1405     while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1406            (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
1407       xml++;
1408     if (*xml == '\0')
1409       break;
1410     /*
1411       States include:
1412         '&' for general entity decoding
1413         '%' for parameter entity decoding
1414         'c' for CDATA sections
1415         ' ' for attributes normalization
1416         '*' for non-CDATA attributes normalization
1417     */
1418     if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1419       {
1420         /*
1421           Character reference.
1422         */
1423         if (xml[2] != 'x')
1424           c=strtol(xml+2,&entity,10);  /* base 10 */
1425         else
1426           c=strtol(xml+3,&entity,16);  /* base 16 */
1427         if ((c == 0) || (*entity != ';'))
1428           {
1429             /*
1430               Not a character reference.
1431             */
1432             xml++;
1433             continue;
1434           }
1435         if (c < 0x80)
1436           *(xml++)=c;
1437         else
1438           {
1439             /*
1440               Multi-byte UTF-8 sequence.
1441             */
1442             byte=c;
1443             for (i=0; byte != 0; byte/=2)
1444               i++;
1445             i=(i-2)/5;
1446             *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1447             xml++;
1448             while (i != 0)
1449             {
1450               i--;
1451               *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1452               xml++;
1453             }
1454           }
1455         (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1456       }
1457     else
1458       if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1459           (state == '*'))) || ((state == '%') && (*xml == '%')))
1460         {
1461           /*
1462             Find entity in the list.
1463           */
1464           i=0;
1465           while ((entities[i] != (char *) NULL) &&
1466                  (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1467             i+=2;
1468           if (entities[i++] == (char *) NULL)
1469             xml++;
1470           else
1471             if (entities[i] != (char *) NULL)
1472               {
1473                 /*
1474                   Found a match.
1475                 */
1476                 length=strlen(entities[i]);
1477                 entity=strchr(xml,';');
1478                 if ((entity != (char *) NULL) &&
1479                     ((length-1L) >= (size_t) (entity-xml)))
1480                   {
1481                     offset=(ssize_t) (xml-p);
1482                     extent=(size_t) (offset+length+strlen(entity));
1483                     if (p != q)
1484                       {
1485                         p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1486                         p[extent]='\0';
1487                       }
1488                     else
1489                       {
1490                         char
1491                           *extent_xml;
1492 
1493                         extent_xml=(char *) AcquireQuantumMemory(extent+1,
1494                           sizeof(*extent_xml));
1495                         if (extent_xml != (char *) NULL)
1496                           {
1497                             memset(extent_xml,0,extent*sizeof(*extent_xml));
1498                             (void) CopyMagickString(extent_xml,p,extent*
1499                               sizeof(*extent_xml));
1500                           }
1501                         p=extent_xml;
1502                       }
1503                     if (p == (char *) NULL)
1504                       ThrowFatalException(ResourceLimitFatalError,
1505                         "MemoryAllocationFailed");
1506                     xml=p+offset;
1507                     entity=strchr(xml,';');
1508                   }
1509                 if (entity != (char *) NULL)
1510                   (void) memmove(xml+length,entity+1,strlen(entity));
1511                 (void) memcpy(xml,entities[i],length);
1512               }
1513         }
1514       else
1515         if (((state == ' ') || (state == '*')) &&
1516             (isspace((int) ((unsigned char) *xml) != 0)))
1517           *(xml++)=' ';
1518         else
1519           xml++;
1520   }
1521   if (state == '*')
1522     {
1523 
1524       /*
1525         Normalize spaces for non-CDATA attributes.
1526       */
1527       for (xml=p; *xml != '\0'; xml++)
1528       {
1529         char
1530           accept[] = " ";
1531 
1532         i=(ssize_t) strspn(xml,accept);
1533         if (i != 0)
1534           (void) memmove(xml,xml+i,strlen(xml+i)+1);
1535         while ((*xml != '\0') && (*xml != ' '))
1536           xml++;
1537         if (*xml == '\0')
1538           break;
1539       }
1540       xml--;
1541       if ((xml >= p) && (*xml == ' '))
1542         *xml='\0';
1543     }
1544   return(p == q ? ConstantString(p) : p);
1545 }
1546 
ParseCharacterContent(XMLTreeRoot * root,char * xml,const size_t length,const char state)1547 static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1548   const size_t length,const char state)
1549 {
1550   XMLTreeInfo
1551     *xml_info;
1552 
1553   xml_info=root->node;
1554   if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1555       (length == 0))
1556     return;
1557   xml[length]='\0';
1558   xml=ParseEntities(xml,root->entities,state);
1559   if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1560     {
1561       (void) ConcatenateString(&xml_info->content,xml);
1562       xml=DestroyString(xml);
1563     }
1564   else
1565     {
1566       if (xml_info->content != (char *) NULL)
1567         xml_info->content=DestroyString(xml_info->content);
1568       xml_info->content=xml;
1569     }
1570 }
1571 
ParseCloseTag(XMLTreeRoot * root,char * tag,ExceptionInfo * exception)1572 static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1573   ExceptionInfo *exception)
1574 {
1575   if ((root->node == (XMLTreeInfo *) NULL) ||
1576       (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1577     {
1578       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1579         "ParseError","unexpected closing tag </%s>",tag);
1580       return(&root->root);
1581     }
1582   root->node=root->node->parent;
1583   return((XMLTreeInfo *) NULL);
1584 }
1585 
ValidateEntities(char * tag,char * xml,const size_t depth,char ** entities)1586 static MagickBooleanType ValidateEntities(char *tag,char *xml,
1587   const size_t depth,char **entities)
1588 {
1589   ssize_t
1590     i;
1591 
1592   /*
1593     Check for circular entity references.
1594   */
1595   if (depth > MagickMaxRecursionDepth)
1596     return(MagickFalse);
1597   for ( ; ; xml++)
1598   {
1599     while ((*xml != '\0') && (*xml != '&'))
1600       xml++;
1601     if (*xml == '\0')
1602       return(MagickTrue);
1603     if (strncmp(xml+1,tag,strlen(tag)) == 0)
1604       return(MagickFalse);
1605     i=0;
1606     while ((entities[i] != (char *) NULL) &&
1607            (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1608       i+=2;
1609     if ((entities[i] != (char *) NULL) &&
1610         (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1611       return(MagickFalse);
1612   }
1613 }
1614 
ParseProcessingInstructions(XMLTreeRoot * root,char * xml,size_t length)1615 static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1616   size_t length)
1617 {
1618   char
1619     *target;
1620 
1621   ssize_t
1622     i;
1623 
1624   ssize_t
1625     j;
1626 
1627   target=xml;
1628   xml[length]='\0';
1629   xml+=strcspn(xml,XMLWhitespace);
1630   if (*xml != '\0')
1631     {
1632       *xml='\0';
1633       xml+=strspn(xml+1,XMLWhitespace)+1;
1634     }
1635   if (strcmp(target,"xml") == 0)
1636     {
1637       xml=strstr(xml,"standalone");
1638       if ((xml != (char *) NULL) &&
1639           (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1640         root->standalone=MagickTrue;
1641       return;
1642     }
1643   if (root->processing_instructions[0] == (char **) NULL)
1644     {
1645       root->processing_instructions=(char ***) AcquireMagickMemory(sizeof(
1646         *root->processing_instructions));
1647       if (root->processing_instructions ==(char ***) NULL)
1648         ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1649       *root->processing_instructions=(char **) NULL;
1650     }
1651   i=0;
1652   while ((root->processing_instructions[i] != (char **) NULL) &&
1653          (strcmp(target,root->processing_instructions[i][0]) != 0))
1654     i++;
1655   if (root->processing_instructions[i] == (char **) NULL)
1656     {
1657       root->processing_instructions=(char ***) ResizeQuantumMemory(
1658         root->processing_instructions,(size_t) (i+2),
1659         sizeof(*root->processing_instructions));
1660       if (root->processing_instructions == (char ***) NULL)
1661         ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1662       root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1663         sizeof(**root->processing_instructions));
1664       if (root->processing_instructions[i] == (char **) NULL)
1665         ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1666       root->processing_instructions[i+1]=(char **) NULL;
1667       root->processing_instructions[i][0]=ConstantString(target);
1668       root->processing_instructions[i][1]=(char *)
1669         root->processing_instructions[i+1];
1670       root->processing_instructions[i+1]=(char **) NULL;
1671       root->processing_instructions[i][2]=ConstantString("");
1672     }
1673   j=1;
1674   while (root->processing_instructions[i][j] != (char *) NULL)
1675     j++;
1676   root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1677     root->processing_instructions[i],(size_t) (j+3),
1678     sizeof(**root->processing_instructions));
1679   if (root->processing_instructions[i] == (char **) NULL)
1680     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1681   root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1682     root->processing_instructions[i][j+1],(size_t) (j+1),
1683     sizeof(***root->processing_instructions));
1684   if (root->processing_instructions[i][j+2] == (char *) NULL)
1685     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1686   (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1687     root->root.tag != (char *) NULL ? ">" : "<",2);
1688   root->processing_instructions[i][j]=ConstantString(xml);
1689   root->processing_instructions[i][j+1]=(char *) NULL;
1690 }
1691 
ParseInternalDoctype(XMLTreeRoot * root,char * xml,size_t length,ExceptionInfo * exception)1692 static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1693   size_t length,ExceptionInfo *exception)
1694 {
1695   char
1696     *c,
1697     **entities,
1698     *n,
1699     **predefined_entitites,
1700     q,
1701     *t,
1702     *v;
1703 
1704   ssize_t
1705     i;
1706 
1707   ssize_t
1708     j;
1709 
1710   n=(char *) NULL;
1711   predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
1712   if (predefined_entitites == (char **) NULL)
1713     ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1714   (void) memcpy(predefined_entitites,sentinel,sizeof(sentinel));
1715   for (xml[length]='\0'; xml != (char *) NULL; )
1716   {
1717     while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1718       xml++;
1719     if (*xml == '\0')
1720       break;
1721     if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1722       {
1723         /*
1724           Parse entity definitions.
1725         */
1726         if (strspn(xml+8,XMLWhitespace) == 0)
1727           break;
1728         xml+=strspn(xml+8,XMLWhitespace)+8;
1729         c=xml;
1730         n=xml+strspn(xml,XMLWhitespace "%");
1731         if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1732           break;
1733         xml=n+strcspn(n,XMLWhitespace);
1734         if (*xml == '\0')
1735           break;
1736         *xml=';';
1737         v=xml+strspn(xml+1,XMLWhitespace)+1;
1738         q=(*v);
1739         v++;
1740         if ((q != '"') && (q != '\''))
1741           {
1742             /*
1743               Skip externals.
1744             */
1745             xml=strchr(xml,'>');
1746             continue;
1747           }
1748         entities=(*c == '%') ? predefined_entitites : root->entities;
1749         for (i=0; entities[i] != (char *) NULL; i++) ;
1750         entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1751           sizeof(*entities));
1752         if (entities == (char **) NULL)
1753           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1754         if (*c == '%')
1755           predefined_entitites=entities;
1756         else
1757           root->entities=entities;
1758         xml++;
1759         *xml='\0';
1760         xml=strchr(v,q);
1761         if (xml != (char *) NULL)
1762           {
1763             *xml='\0';
1764             xml++;
1765           }
1766         entities[i+1]=ParseEntities(v,predefined_entitites,'%');
1767         entities[i+2]=(char *) NULL;
1768         if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1769           entities[i]=n;
1770         else
1771           {
1772             if (entities[i+1] != v)
1773               entities[i+1]=DestroyString(entities[i+1]);
1774             (void) ThrowMagickException(exception,GetMagickModule(),
1775               OptionWarning,"ParseError","circular entity declaration &%s",n);
1776             predefined_entitites=(char **) RelinquishMagickMemory(
1777               predefined_entitites);
1778             return(MagickFalse);
1779           }
1780         }
1781       else
1782        if (strncmp(xml,"<!ATTLIST",9) == 0)
1783          {
1784             /*
1785               Parse default attributes.
1786             */
1787             t=xml+strspn(xml+9,XMLWhitespace)+9;
1788             if (*t == '\0')
1789               {
1790                 (void) ThrowMagickException(exception,GetMagickModule(),
1791                   OptionWarning,"ParseError","unclosed <!ATTLIST");
1792                 predefined_entitites=(char **) RelinquishMagickMemory(
1793                   predefined_entitites);
1794                 return(MagickFalse);
1795               }
1796             xml=t+strcspn(t,XMLWhitespace ">");
1797             if (*xml == '>')
1798               continue;
1799             *xml='\0';
1800             i=0;
1801             while ((root->attributes[i] != (char **) NULL) &&
1802                    (n != (char *) NULL) &&
1803                    (strcmp(n,root->attributes[i][0]) != 0))
1804               i++;
1805             while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1806                    (*n != '>'))
1807             {
1808               xml=n+strcspn(n,XMLWhitespace);
1809               if (*xml != '\0')
1810                 *xml='\0';
1811               else
1812                 {
1813                   (void) ThrowMagickException(exception,GetMagickModule(),
1814                     OptionWarning,"ParseError","malformed <!ATTLIST");
1815                   predefined_entitites=(char **) RelinquishMagickMemory(
1816                     predefined_entitites);
1817                   return(MagickFalse);
1818                 }
1819               xml+=strspn(xml+1,XMLWhitespace)+1;
1820               c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1821               if (strncmp(xml,"NOTATION",8) == 0)
1822                 xml+=strspn(xml+8,XMLWhitespace)+8;
1823               xml=(*xml == '(') ? strchr(xml,')') : xml+
1824                 strcspn(xml,XMLWhitespace);
1825               if (xml == (char *) NULL)
1826                 {
1827                   (void) ThrowMagickException(exception,GetMagickModule(),
1828                     OptionWarning,"ParseError","malformed <!ATTLIST");
1829                   predefined_entitites=(char **) RelinquishMagickMemory(
1830                     predefined_entitites);
1831                   return(MagickFalse);
1832                 }
1833               xml+=strspn(xml,XMLWhitespace ")");
1834               if (strncmp(xml,"#FIXED",6) == 0)
1835                 xml+=strspn(xml+6,XMLWhitespace)+6;
1836               if (*xml == '#')
1837                 {
1838                   xml+=strcspn(xml,XMLWhitespace ">")-1;
1839                   if (*c == ' ')
1840                     continue;
1841                   v=(char *) NULL;
1842                 }
1843               else
1844                 if (((*xml == '"') || (*xml == '\''))  &&
1845                     ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1846                   *xml='\0';
1847                 else
1848                   {
1849                     (void) ThrowMagickException(exception,GetMagickModule(),
1850                       OptionWarning,"ParseError","malformed <!ATTLIST");
1851                     predefined_entitites=(char **) RelinquishMagickMemory(
1852                       predefined_entitites);
1853                     return(MagickFalse);
1854                   }
1855               if (root->attributes[i] == (char **) NULL)
1856                 {
1857                   /*
1858                     New attribute tag.
1859                   */
1860                   if (i == 0)
1861                     root->attributes=(char ***) AcquireQuantumMemory(2,
1862                       sizeof(*root->attributes));
1863                   else
1864                     root->attributes=(char ***) ResizeQuantumMemory(
1865                       root->attributes,(size_t) (i+2),
1866                       sizeof(*root->attributes));
1867                   if (root->attributes == (char ***) NULL)
1868                     ThrowFatalException(ResourceLimitFatalError,
1869                       "MemoryAllocationFailed");
1870                   root->attributes[i]=(char **) AcquireQuantumMemory(2,
1871                     sizeof(**root->attributes));
1872                   if (root->attributes[i] == (char **) NULL)
1873                     ThrowFatalException(ResourceLimitFatalError,
1874                       "MemoryAllocationFailed");
1875                   root->attributes[i][0]=ConstantString(t);
1876                   root->attributes[i][1]=(char *) NULL;
1877                   root->attributes[i+1]=(char **) NULL;
1878                 }
1879               for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1880               root->attributes[i]=(char **) ResizeQuantumMemory(
1881                 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1882               if (root->attributes[i] == (char **) NULL)
1883                 ThrowFatalException(ResourceLimitFatalError,
1884                   "MemoryAllocationFailed");
1885               root->attributes[i][j+3]=(char *) NULL;
1886               root->attributes[i][j+2]=ConstantString(c);
1887               root->attributes[i][j+1]=(char *) NULL;
1888               if (v != (char *) NULL)
1889                 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1890               root->attributes[i][j]=ConstantString(n);
1891             }
1892         }
1893       else
1894         if (strncmp(xml, "<!--", 4) == 0)
1895           xml=strstr(xml+4,"-->");
1896         else
1897           if (strncmp(xml,"<?", 2) == 0)
1898             {
1899               c=xml+2;
1900               xml=strstr(c,"?>");
1901               if (xml != (char *) NULL)
1902                 {
1903                   ParseProcessingInstructions(root,c,(size_t) (xml-c));
1904                   xml++;
1905                 }
1906             }
1907            else
1908              if (*xml == '<')
1909                xml=strchr(xml,'>');
1910              else
1911                if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1912                  break;
1913     }
1914   predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
1915   return(MagickTrue);
1916 }
1917 
ParseOpenTag(XMLTreeRoot * root,char * tag,char ** attributes)1918 static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1919 {
1920   XMLTreeInfo
1921     *xml_info;
1922 
1923   xml_info=root->node;
1924   if (xml_info->tag == (char *) NULL)
1925     xml_info->tag=ConstantString(tag);
1926   else
1927     xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1928   if (xml_info != (XMLTreeInfo *) NULL)
1929     xml_info->attributes=attributes;
1930   root->node=xml_info;
1931 }
1932 
1933 static const char
1934   *skip_tags[3] =
1935   {
1936     "rdf:Bag",
1937     "rdf:Seq",
1938     (const char *) NULL
1939   };
1940 
IsSkipTag(const char * tag)1941 static inline MagickBooleanType IsSkipTag(const char *tag)
1942 {
1943   ssize_t
1944     i;
1945 
1946   i=0;
1947   while (skip_tags[i] != (const char *) NULL)
1948   {
1949     if (LocaleCompare(tag,skip_tags[i]) == 0)
1950       return(MagickTrue);
1951     i++;
1952   }
1953   return(MagickFalse);
1954 }
1955 
NewXMLTree(const char * xml,ExceptionInfo * exception)1956 MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1957 {
1958   char
1959     **attribute,
1960     **attributes,
1961     *tag,
1962     *utf8;
1963 
1964   int
1965     c,
1966     terminal;
1967 
1968   MagickBooleanType
1969     status;
1970 
1971   char
1972     *p;
1973 
1974   ssize_t
1975     i;
1976 
1977   size_t
1978     ignore_depth,
1979     length;
1980 
1981   ssize_t
1982     j,
1983     l;
1984 
1985   XMLTreeRoot
1986     *root;
1987 
1988   /*
1989     Convert xml-string to UTF8.
1990   */
1991   if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1992     {
1993       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1994         "ParseError","root tag missing");
1995       return((XMLTreeInfo *) NULL);
1996     }
1997   root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1998   length=strlen(xml);
1999   utf8=ConvertUTF16ToUTF8(xml,&length);
2000   if (utf8 == (char *) NULL)
2001     {
2002       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2003         "ParseError","UTF16 to UTF8 failed");
2004       return((XMLTreeInfo *) NULL);
2005     }
2006   terminal=utf8[length-1];
2007   utf8[length-1]='\0';
2008   p=utf8;
2009   while ((*p != '\0') && (*p != '<'))
2010     p++;
2011   if (*p == '\0')
2012     {
2013       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2014         "ParseError","root tag missing");
2015       utf8=DestroyString(utf8);
2016       return((XMLTreeInfo *) NULL);
2017     }
2018   attribute=(char **) NULL;
2019   l=0;
2020   ignore_depth=0;
2021   for (p++; ; p++)
2022   {
2023     attributes=(char **) sentinel;
2024     tag=p;
2025     c=(*p);
2026     if ((isalpha((int) ((unsigned char) *p)) !=0) || (*p == '_') ||
2027         (*p == ':') || (c < '\0'))
2028       {
2029         /*
2030           Tag.
2031         */
2032         if (root->node == (XMLTreeInfo *) NULL)
2033           {
2034             (void) ThrowMagickException(exception,GetMagickModule(),
2035               OptionWarning,"ParseError","root tag missing");
2036             utf8=DestroyString(utf8);
2037             return(&root->root);
2038           }
2039         p+=strcspn(p,XMLWhitespace "/>");
2040         while (isspace((int) ((unsigned char) *p)) != 0)
2041           *p++='\0';
2042         if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
2043             (ignore_depth == 0))
2044           {
2045             if ((*p != '\0') && (*p != '/') && (*p != '>'))
2046               {
2047                 /*
2048                   Find tag in default attributes list.
2049                 */
2050                 i=0;
2051                 while ((root->attributes[i] != (char **) NULL) &&
2052                        (strcmp(root->attributes[i][0],tag) != 0))
2053                   i++;
2054                 attribute=root->attributes[i];
2055               }
2056             for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
2057             {
2058               /*
2059                 Attribute.
2060               */
2061               if (l == 0)
2062                 attributes=(char **) AcquireQuantumMemory(4,
2063                   sizeof(*attributes));
2064               else
2065                 attributes=(char **) ResizeQuantumMemory(attributes,
2066                   (size_t) (l+4),sizeof(*attributes));
2067               if (attributes == (char **) NULL)
2068                 {
2069                   (void) ThrowMagickException(exception,GetMagickModule(),
2070                     ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2071                   utf8=DestroyString(utf8);
2072                   return(&root->root);
2073                 }
2074               attributes[l+2]=(char *) NULL;
2075               attributes[l+1]=(char *) NULL;
2076               attributes[l]=p;
2077               p+=strcspn(p,XMLWhitespace "=/>");
2078               if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
2079                 attributes[l]=ConstantString("");
2080               else
2081                 {
2082                   *p++='\0';
2083                   p+=strspn(p,XMLWhitespace "=");
2084                   c=(*p);
2085                   if ((c == '"') || (c == '\''))
2086                     {
2087                       /*
2088                         Attributes value.
2089                       */
2090                       p++;
2091                       attributes[l+1]=p;
2092                       while ((*p != '\0') && (*p != c))
2093                         p++;
2094                       if (*p != '\0')
2095                         *p++='\0';
2096                       else
2097                         {
2098                           attributes[l]=ConstantString("");
2099                           attributes[l+1]=ConstantString("");
2100                           (void) DestroyXMLTreeAttributes(attributes);
2101                           (void) ThrowMagickException(exception,
2102                             GetMagickModule(),OptionWarning,"ParseError",
2103                             "missing %c",c);
2104                           utf8=DestroyString(utf8);
2105                           return(&root->root);
2106                         }
2107                       j=1;
2108                       while ((attribute != (char **) NULL) &&
2109                              (attribute[j] != (char *) NULL) &&
2110                              (strcmp(attribute[j],attributes[l]) != 0))
2111                         j+=3;
2112                       attributes[l+1]=ParseEntities(attributes[l+1],
2113                         root->entities,(attribute != (char **) NULL) &&
2114                         (attribute[j] != (char *) NULL) ? *attribute[j+2] :
2115                         ' ');
2116                     }
2117                   attributes[l]=ConstantString(attributes[l]);
2118                 }
2119               while (isspace((int) ((unsigned char) *p)) != 0)
2120                 p++;
2121             }
2122           }
2123         else
2124           {
2125             while((*p != '\0') && (*p != '/') && (*p != '>'))
2126               p++;
2127           }
2128         if (*p == '/')
2129           {
2130             /*
2131               Self closing tag.
2132             */
2133             *p++='\0';
2134             if (((*p != '\0') && (*p != '>')) ||
2135                 ((*p == '\0') && (terminal != '>')))
2136               {
2137                 if (l != 0)
2138                   (void) DestroyXMLTreeAttributes(attributes);
2139                 (void) ThrowMagickException(exception,GetMagickModule(),
2140                   OptionWarning,"ParseError","missing >");
2141                 utf8=DestroyString(utf8);
2142                 return(&root->root);
2143               }
2144             if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2145               (void) DestroyXMLTreeAttributes(attributes);
2146             else
2147               {
2148                 ParseOpenTag(root,tag,attributes);
2149                 (void) ParseCloseTag(root,tag,exception);
2150               }
2151           }
2152         else
2153           {
2154             c=(*p);
2155             if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
2156               {
2157                 *p='\0';
2158                 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2159                   ParseOpenTag(root,tag,attributes);
2160                 else
2161                   {
2162                     ignore_depth++;
2163                     (void) DestroyXMLTreeAttributes(attributes);
2164                   }
2165                 *p=c;
2166               }
2167             else
2168               {
2169                 if (l != 0)
2170                   (void) DestroyXMLTreeAttributes(attributes);
2171                 (void) ThrowMagickException(exception,GetMagickModule(),
2172                   OptionWarning,"ParseError","missing >");
2173                 utf8=DestroyString(utf8);
2174                 return(&root->root);
2175               }
2176           }
2177       }
2178     else
2179       if (*p == '/')
2180         {
2181           /*
2182             Close tag.
2183           */
2184           tag=p+1;
2185           p+=strcspn(tag,XMLWhitespace ">")+1;
2186           c=(*p);
2187           if ((c == '\0') && (terminal != '>'))
2188             {
2189               (void) ThrowMagickException(exception,GetMagickModule(),
2190                 OptionWarning,"ParseError","missing >");
2191               utf8=DestroyString(utf8);
2192               return(&root->root);
2193             }
2194           *p='\0';
2195           if ((ignore_depth == 0) &&
2196               (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2197             {
2198               utf8=DestroyString(utf8);
2199               return(&root->root);
2200             }
2201           if (ignore_depth > 0)
2202             ignore_depth--;
2203           *p=c;
2204           if (isspace((int) ((unsigned char) *p)) != 0)
2205             p+=strspn(p,XMLWhitespace);
2206         }
2207       else
2208         if (strncmp(p,"!--",3) == 0)
2209           {
2210             /*
2211               Comment.
2212             */
2213             p=strstr(p+3,"--");
2214             if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
2215                 ((*p == '\0') && (terminal != '>')))
2216               {
2217                 (void) ThrowMagickException(exception,GetMagickModule(),
2218                   OptionWarning,"ParseError","unclosed <!--");
2219                 utf8=DestroyString(utf8);
2220                 return(&root->root);
2221               }
2222           }
2223         else
2224           if (strncmp(p,"![CDATA[",8) == 0)
2225             {
2226               /*
2227                 Cdata.
2228               */
2229               p=strstr(p,"]]>");
2230               if (p != (char *) NULL)
2231                 {
2232                   p+=2;
2233                   if (ignore_depth == 0)
2234                     ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
2235                 }
2236               else
2237                 {
2238                   (void) ThrowMagickException(exception,GetMagickModule(),
2239                     OptionWarning,"ParseError","unclosed <![CDATA[");
2240                   utf8=DestroyString(utf8);
2241                   return(&root->root);
2242                 }
2243             }
2244           else
2245             if (strncmp(p,"!DOCTYPE",8) == 0)
2246               {
2247                 /*
2248                   DTD.
2249                 */
2250                 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
2251                      ((l != 0) && ((*p != ']') ||
2252                      (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
2253                   l=(ssize_t) ((*p == '[') ? 1 : l))
2254                 p+=strcspn(p+1,"[]>")+1;
2255                 if ((*p == '\0') && (terminal != '>'))
2256                   {
2257                     (void) ThrowMagickException(exception,GetMagickModule(),
2258                       OptionWarning,"ParseError","unclosed <!DOCTYPE");
2259                     utf8=DestroyString(utf8);
2260                     return(&root->root);
2261                   }
2262                 if (l != 0)
2263                   tag=strchr(tag,'[')+1;
2264                 if (l != 0)
2265                   {
2266                     status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
2267                       exception);
2268                     if (status == MagickFalse)
2269                       {
2270                         utf8=DestroyString(utf8);
2271                         return(&root->root);
2272                       }
2273                     p++;
2274                   }
2275               }
2276             else
2277               if (*p == '?')
2278                 {
2279                   /*
2280                     Processing instructions.
2281                   */
2282                   do
2283                   {
2284                     p=strchr(p,'?');
2285                     if (p == (char *) NULL)
2286                       break;
2287                     p++;
2288                   } while ((*p != '\0') && (*p != '>'));
2289                   if ((p == (char *) NULL) || ((*p == '\0') &&
2290                       (terminal != '>')))
2291                     {
2292                       (void) ThrowMagickException(exception,GetMagickModule(),
2293                         OptionWarning,"ParseError","unclosed <?");
2294                       utf8=DestroyString(utf8);
2295                       return(&root->root);
2296                     }
2297                   ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2298                 }
2299               else
2300                 {
2301                   (void) ThrowMagickException(exception,GetMagickModule(),
2302                     OptionWarning,"ParseError","unexpected <");
2303                   utf8=DestroyString(utf8);
2304                   return(&root->root);
2305                 }
2306      if ((p == (char *) NULL) || (*p == '\0'))
2307        break;
2308      *p++='\0';
2309      tag=p;
2310      if ((*p != '\0') && (*p != '<'))
2311        {
2312         /*
2313           Tag character content.
2314         */
2315         while ((*p != '\0') && (*p != '<'))
2316           p++;
2317         if (*p == '\0')
2318           break;
2319         if (ignore_depth == 0)
2320           ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2321       }
2322     else
2323       if (*p == '\0')
2324         break;
2325   }
2326   utf8=DestroyString(utf8);
2327   if (root->node == (XMLTreeInfo *) NULL)
2328     return(&root->root);
2329   if (root->node->tag == (char *) NULL)
2330     {
2331       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2332         "ParseError","root tag missing");
2333       return(&root->root);
2334     }
2335   (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2336     "ParseError","unclosed tag: `%s'",root->node->tag);
2337   return(&root->root);
2338 }
2339 
2340 /*
2341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2342 %                                                                             %
2343 %                                                                             %
2344 %                                                                             %
2345 %   N e w X M L T r e e T a g                                                 %
2346 %                                                                             %
2347 %                                                                             %
2348 %                                                                             %
2349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2350 %
2351 %  NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2352 %
2353 %  The format of the NewXMLTreeTag method is:
2354 %
2355 %      XMLTreeInfo *NewXMLTreeTag(const char *tag)
2356 %
2357 %  A description of each parameter follows:
2358 %
2359 %    o tag: the tag.
2360 %
2361 */
NewXMLTreeTag(const char * tag)2362 MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2363 {
2364   static const char
2365     *predefined_entities[NumberPredefinedEntities+1] =
2366     {
2367       "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2368       "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2369     };
2370 
2371   XMLTreeRoot
2372     *root;
2373 
2374   root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2375   if (root == (XMLTreeRoot *) NULL)
2376     return((XMLTreeInfo *) NULL);
2377   (void) memset(root,0,sizeof(*root));
2378   root->root.tag=(char *) NULL;
2379   if (tag != (char *) NULL)
2380     root->root.tag=ConstantString(tag);
2381   root->node=(&root->root);
2382   root->root.content=ConstantString("");
2383   root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2384   if (root->entities == (char **) NULL)
2385     return((XMLTreeInfo *) NULL);
2386   (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2387   root->root.attributes=sentinel;
2388   root->attributes=(char ***) root->root.attributes;
2389   root->processing_instructions=(char ***) root->root.attributes;
2390   root->debug=IsEventLogging();
2391   root->signature=MagickCoreSignature;
2392   return(&root->root);
2393 }
2394 
2395 /*
2396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2397 %                                                                             %
2398 %                                                                             %
2399 %                                                                             %
2400 %   P r u n e T a g F r o m X M L T r e e                                     %
2401 %                                                                             %
2402 %                                                                             %
2403 %                                                                             %
2404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2405 %
2406 %  PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2407 %  subtags.
2408 %
2409 %  The format of the PruneTagFromXMLTree method is:
2410 %
2411 %      XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2412 %
2413 %  A description of each parameter follows:
2414 %
2415 %    o xml_info: the xml info.
2416 %
2417 */
PruneTagFromXMLTree(XMLTreeInfo * xml_info)2418 MagickExport XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2419 {
2420   XMLTreeInfo
2421     *node;
2422 
2423   assert(xml_info != (XMLTreeInfo *) NULL);
2424   assert((xml_info->signature == MagickCoreSignature) ||
2425          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2426   if (xml_info->debug != MagickFalse)
2427     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2428   if (xml_info->next != (XMLTreeInfo *) NULL)
2429     xml_info->next->sibling=xml_info->sibling;
2430   if (xml_info->parent != (XMLTreeInfo *) NULL)
2431     {
2432       node=xml_info->parent->child;
2433       if (node == xml_info)
2434         xml_info->parent->child=xml_info->ordered;
2435       else
2436         {
2437           while (node->ordered != xml_info)
2438             node=node->ordered;
2439           node->ordered=node->ordered->ordered;
2440           node=xml_info->parent->child;
2441           if (strcmp(node->tag,xml_info->tag) != 0)
2442             {
2443               while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2444                 node=node->sibling;
2445               if (node->sibling != xml_info)
2446                 node=node->sibling;
2447               else
2448                 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2449                   xml_info->next : node->sibling->sibling;
2450             }
2451           while ((node->next != (XMLTreeInfo *) NULL) &&
2452                  (node->next != xml_info))
2453             node=node->next;
2454           if (node->next != (XMLTreeInfo *) NULL)
2455             node->next=node->next->next;
2456         }
2457     }
2458   xml_info->ordered=(XMLTreeInfo *) NULL;
2459   xml_info->sibling=(XMLTreeInfo *) NULL;
2460   xml_info->next=(XMLTreeInfo *) NULL;
2461   return(xml_info);
2462 }
2463 
2464 /*
2465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2466 %                                                                             %
2467 %                                                                             %
2468 %                                                                             %
2469 %   S e t X M L T r e e A t t r i b u t e                                     %
2470 %                                                                             %
2471 %                                                                             %
2472 %                                                                             %
2473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2474 %
2475 %  SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2476 %  found.  A value of NULL removes the specified attribute.
2477 %
2478 %  The format of the SetXMLTreeAttribute method is:
2479 %
2480 %      XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2481 %        const char *value)
2482 %
2483 %  A description of each parameter follows:
2484 %
2485 %    o xml_info: the xml info.
2486 %
2487 %    o tag:  The attribute tag.
2488 %
2489 %    o value:  The attribute value.
2490 %
2491 */
SetXMLTreeAttribute(XMLTreeInfo * xml_info,const char * tag,const char * value)2492 MagickExport XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2493   const char *tag,const char *value)
2494 {
2495   ssize_t
2496     i;
2497 
2498   ssize_t
2499     j;
2500 
2501   assert(xml_info != (XMLTreeInfo *) NULL);
2502   assert((xml_info->signature == MagickCoreSignature) ||
2503          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2504   if (xml_info->debug != MagickFalse)
2505     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2506   i=0;
2507   while ((xml_info->attributes[i] != (char *) NULL) &&
2508          (strcmp(xml_info->attributes[i],tag) != 0))
2509     i+=2;
2510   if (xml_info->attributes[i] == (char *) NULL)
2511     {
2512       /*
2513         Add new attribute tag.
2514       */
2515       if (value == (const char *) NULL)
2516         return(xml_info);
2517       if (xml_info->attributes != sentinel)
2518         xml_info->attributes=(char **) ResizeQuantumMemory(
2519           xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2520       else
2521         {
2522           xml_info->attributes=(char **) AcquireQuantumMemory(4,
2523             sizeof(*xml_info->attributes));
2524           if (xml_info->attributes != (char **) NULL)
2525             xml_info->attributes[1]=ConstantString("");
2526         }
2527       if (xml_info->attributes == (char **) NULL)
2528         ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2529       xml_info->attributes[i]=ConstantString(tag);
2530       xml_info->attributes[i+2]=(char *) NULL;
2531       (void) strlen(xml_info->attributes[i+1]);
2532     }
2533   /*
2534     Add new value to an existing attribute.
2535   */
2536   for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2537   if (xml_info->attributes[i+1] != (char *) NULL)
2538     xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2539   if (value != (const char *) NULL)
2540     {
2541       xml_info->attributes[i+1]=ConstantString(value);
2542       return(xml_info);
2543     }
2544   if (xml_info->attributes[i] != (char *) NULL)
2545     xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2546   (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,
2547     (size_t) (j-i)*sizeof(*xml_info->attributes));
2548   xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2549     (size_t) (j+2),sizeof(*xml_info->attributes));
2550   if (xml_info->attributes == (char **) NULL)
2551     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2552   j-=2;
2553   (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2554     (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
2555   return(xml_info);
2556 }
2557 
2558 /*
2559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2560 %                                                                             %
2561 %                                                                             %
2562 %                                                                             %
2563 %   S e t X M L T r e e C o n t e n t                                         %
2564 %                                                                             %
2565 %                                                                             %
2566 %                                                                             %
2567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2568 %
2569 %  SetXMLTreeContent() sets the character content for the given tag and
2570 %  returns the tag.
2571 %
2572 %  The format of the SetXMLTreeContent method is:
2573 %
2574 %      XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2575 %        const char *content)
2576 %
2577 %  A description of each parameter follows:
2578 %
2579 %    o xml_info: the xml info.
2580 %
2581 %    o content:  The content.
2582 %
2583 */
SetXMLTreeContent(XMLTreeInfo * xml_info,const char * content)2584 MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2585   const char *content)
2586 {
2587   assert(xml_info != (XMLTreeInfo *) NULL);
2588   assert((xml_info->signature == MagickCoreSignature) ||
2589          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2590   if (xml_info->debug != MagickFalse)
2591     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2592   if (xml_info->content != (char *) NULL)
2593     xml_info->content=DestroyString(xml_info->content);
2594   xml_info->content=(char *) ConstantString(content);
2595   return(xml_info);
2596 }
2597 
2598 /*
2599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2600 %                                                                             %
2601 %                                                                             %
2602 %                                                                             %
2603 %   X M L T r e e I n f o T o X M L                                           %
2604 %                                                                             %
2605 %                                                                             %
2606 %                                                                             %
2607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2608 %
2609 %  XMLTreeInfoToXML() converts an xml-tree to an XML string.
2610 %
2611 %  The format of the XMLTreeInfoToXML method is:
2612 %
2613 %      char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2614 %
2615 %  A description of each parameter follows:
2616 %
2617 %    o xml_info: the xml info.
2618 %
2619 */
2620 
EncodePredefinedEntities(const char * source,ssize_t offset,char ** destination,size_t * length,size_t * extent,MagickBooleanType pedantic)2621 static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2622   char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2623 {
2624   char
2625     *canonical_content;
2626 
2627   if (offset < 0)
2628     canonical_content=CanonicalXMLContent(source,pedantic);
2629   else
2630     {
2631       char
2632         *content;
2633 
2634       content=AcquireString(source);
2635       content[offset]='\0';
2636       canonical_content=CanonicalXMLContent(content,pedantic);
2637       content=DestroyString(content);
2638     }
2639   if (canonical_content == (char *) NULL)
2640     return(*destination);
2641   if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2642     {
2643       *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2644       *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2645         sizeof(**destination));
2646       if (*destination == (char *) NULL)
2647         return(*destination);
2648     }
2649   *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
2650     canonical_content);
2651   canonical_content=DestroyString(canonical_content);
2652   return(*destination);
2653 }
2654 
XMLTreeTagToXML(XMLTreeInfo * xml_info,char ** source,size_t * length,size_t * extent,size_t start,char *** attributes)2655 static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2656   size_t *extent,size_t start,char ***attributes)
2657 {
2658   char
2659     *content;
2660 
2661   const char
2662     *attribute;
2663 
2664   ssize_t
2665     i;
2666 
2667   size_t
2668     offset;
2669 
2670   ssize_t
2671     j;
2672 
2673   content=(char *) "";
2674   if (xml_info->parent != (XMLTreeInfo *) NULL)
2675     content=xml_info->parent->content;
2676   offset=0;
2677   *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2678     start),source,length,extent,MagickFalse);
2679   if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2680     {
2681       *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2682       *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2683       if (*source == (char *) NULL)
2684         return(*source);
2685     }
2686   *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
2687   for (i=0; xml_info->attributes[i]; i+=2)
2688   {
2689     attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2690     if (attribute != xml_info->attributes[i+1])
2691       continue;
2692     if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2693       {
2694         *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2695         *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2696         if (*source == (char *) NULL)
2697           return((char *) NULL);
2698       }
2699     *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2700       xml_info->attributes[i]);
2701     (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2702       extent,MagickTrue);
2703     *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2704   }
2705   i=0;
2706   while ((attributes[i] != (char **) NULL) &&
2707          (strcmp(attributes[i][0],xml_info->tag) != 0))
2708     i++;
2709   j=1;
2710   while ((attributes[i] != (char **) NULL) &&
2711          (attributes[i][j] != (char *) NULL))
2712   {
2713     if ((attributes[i][j+1] == (char *) NULL) ||
2714         (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2715       {
2716         j+=3;
2717         continue;
2718       }
2719     if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2720       {
2721         *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2722         *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2723         if (*source == (char *) NULL)
2724           return((char *) NULL);
2725       }
2726     *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
2727       attributes[i][j]);
2728     (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2729       MagickTrue);
2730     *length+=FormatLocaleString(*source+(*length),*extent,"\"");
2731     j+=3;
2732   }
2733   *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2734     ">" : "/>");
2735   if (xml_info->child != (XMLTreeInfo *) NULL)
2736     *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2737   else
2738     *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2739       MagickFalse);
2740   if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2741     {
2742       *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2743       *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2744       if (*source == (char *) NULL)
2745         return((char *) NULL);
2746     }
2747   if (*xml_info->content != '\0')
2748     *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
2749       xml_info->tag);
2750   while ((offset < xml_info->offset) && (content[offset] != '\0'))
2751     offset++;
2752   if (xml_info->ordered != (XMLTreeInfo *) NULL)
2753     content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2754       attributes);
2755   else
2756     content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2757       MagickFalse);
2758   return(content);
2759 }
2760 
XMLTreeInfoToXML(XMLTreeInfo * xml_info)2761 MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2762 {
2763   char
2764     *xml;
2765 
2766   char
2767     *p,
2768     *q;
2769 
2770   ssize_t
2771     i;
2772 
2773   size_t
2774     extent,
2775     length;
2776 
2777   ssize_t
2778     j,
2779     k;
2780 
2781   XMLTreeInfo
2782     *ordered,
2783     *parent;
2784 
2785   XMLTreeRoot
2786     *root;
2787 
2788   assert(xml_info != (XMLTreeInfo *) NULL);
2789   assert((xml_info->signature == MagickCoreSignature) ||
2790          (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2791   if (xml_info->debug != MagickFalse)
2792     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2793   if (xml_info->tag == (char *) NULL)
2794     return((char *) NULL);
2795   xml=AcquireString((char *) NULL);
2796   length=0;
2797   extent=MaxTextExtent;
2798   root=(XMLTreeRoot *) xml_info;
2799   while (root->root.parent != (XMLTreeInfo *) NULL)
2800     root=(XMLTreeRoot *) root->root.parent;
2801   parent=xml_info->parent;
2802   if (parent == (XMLTreeInfo *) NULL)
2803     for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2804     {
2805       /*
2806         Pre-root processing instructions.
2807       */
2808       for (k=2; root->processing_instructions[i][k-1]; k++) ;
2809       p=root->processing_instructions[i][1];
2810       for (j=1; p != (char *) NULL; j++)
2811       {
2812         if (root->processing_instructions[i][k][j-1] == '>')
2813           {
2814             p=root->processing_instructions[i][j];
2815             continue;
2816           }
2817         q=root->processing_instructions[i][0];
2818         if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2819           {
2820             extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2821             xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2822             if (xml == (char *) NULL)
2823               return(xml);
2824           }
2825         length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2826           *p != '\0' ? " " : "",p);
2827         p=root->processing_instructions[i][j];
2828       }
2829     }
2830   ordered=xml_info->ordered;
2831   xml_info->parent=(XMLTreeInfo *) NULL;
2832   xml_info->ordered=(XMLTreeInfo *) NULL;
2833   xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2834   xml_info->parent=parent;
2835   xml_info->ordered=ordered;
2836   if (parent == (XMLTreeInfo *) NULL)
2837     for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2838     {
2839       /*
2840         Post-root processing instructions.
2841       */
2842       for (k=2; root->processing_instructions[i][k-1]; k++) ;
2843       p=root->processing_instructions[i][1];
2844       for (j=1; p != (char *) NULL; j++)
2845       {
2846         if (root->processing_instructions[i][k][j-1] == '<')
2847           {
2848             p=root->processing_instructions[i][j];
2849             continue;
2850           }
2851         q=root->processing_instructions[i][0];
2852         if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2853           {
2854             extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2855             xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2856             if (xml == (char *) NULL)
2857               return(xml);
2858           }
2859         length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2860           *p != '\0' ? " " : "",p);
2861         p=root->processing_instructions[i][j];
2862       }
2863     }
2864   return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2865 }
2866