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