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