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