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