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 p[extent]='\0';
1414 }
1415 else
1416 {
1417 char
1418 *extent_xml;
1419
1420 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1421 sizeof(*extent_xml));
1422 if (extent_xml != (char *) NULL)
1423 {
1424 memset(extent_xml,0,extent*sizeof(*extent_xml));
1425 (void) CopyMagickString(extent_xml,p,extent*
1426 sizeof(*extent_xml));
1427 }
1428 p=extent_xml;
1429 }
1430 if (p == (char *) NULL)
1431 ThrowFatalException(ResourceLimitFatalError,
1432 "MemoryAllocationFailed");
1433 xml=p+offset;
1434 entity=strchr(xml,';');
1435 }
1436 if (entity != (char *) NULL)
1437 (void) memmove(xml+length,entity+1,strlen(entity));
1438 (void) memcpy(xml,entities[i],length);
1439 }
1440 }
1441 else
1442 if (((state == ' ') || (state == '*')) &&
1443 (isspace((int) ((unsigned char) *xml)) != 0))
1444 *(xml++)=' ';
1445 else
1446 xml++;
1447 }
1448 if (state == '*')
1449 {
1450 /*
1451 Normalize spaces for non-CDATA attributes.
1452 */
1453 for (xml=p; *xml != '\0'; xml++)
1454 {
1455 char
1456 accept[] = " ";
1457
1458 i=(ssize_t) strspn(xml,accept);
1459 if (i != 0)
1460 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1461 while ((*xml != '\0') && (*xml != ' '))
1462 xml++;
1463 if (*xml == '\0')
1464 break;
1465 }
1466 xml--;
1467 if ((xml >= p) && (*xml == ' '))
1468 *xml='\0';
1469 }
1470 return(p == q ? ConstantString(p) : p);
1471}
1472
1473static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1474 const size_t length,const char state)
1475{
1477 *xml_info;
1478
1479 xml_info=root->node;
1480 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1481 (length == 0))
1482 return;
1483 xml[length]='\0';
1484 xml=ParseEntities(xml,root->entities,state);
1485 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1486 {
1487 (void) ConcatenateString(&xml_info->content,xml);
1488 xml=DestroyString(xml);
1489 }
1490 else
1491 {
1492 if (xml_info->content != (char *) NULL)
1493 xml_info->content=DestroyString(xml_info->content);
1494 xml_info->content=xml;
1495 }
1496}
1497
1498static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1499 ExceptionInfo *exception)
1500{
1501 if ((root->node == (XMLTreeInfo *) NULL) ||
1502 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1503 {
1504 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1505 "ParseError","unexpected closing tag </%s>",tag);
1506 return(&root->root);
1507 }
1508 root->node=root->node->parent;
1509 return((XMLTreeInfo *) NULL);
1510}
1511
1512static MagickBooleanType ValidateEntities(char *tag,char *xml,
1513 const size_t depth,char **entities)
1514{
1515 ssize_t
1516 i;
1517
1518 /*
1519 Check for circular entity references.
1520 */
1521 if (depth > MagickMaxRecursionDepth)
1522 return(MagickFalse);
1523 for ( ; ; xml++)
1524 {
1525 while ((*xml != '\0') && (*xml != '&'))
1526 xml++;
1527 if (*xml == '\0')
1528 return(MagickTrue);
1529 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1530 return(MagickFalse);
1531 i=0;
1532 while ((entities[i] != (char *) NULL) &&
1533 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1534 i+=2;
1535 if ((entities[i] != (char *) NULL) &&
1536 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1537 return(MagickFalse);
1538 }
1539}
1540
1541static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1542 size_t length)
1543{
1544 char
1545 *target;
1546
1547 ssize_t
1548 i,
1549 j;
1550
1551 target=xml;
1552 xml[length]='\0';
1553 xml+=strcspn(xml,XMLWhitespace);
1554 if (*xml != '\0')
1555 {
1556 *xml='\0';
1557 xml+=strspn(xml+1,XMLWhitespace)+1;
1558 }
1559 if (strcmp(target,"xml") == 0)
1560 {
1561 xml=strstr(xml,"standalone");
1562 if ((xml != (char *) NULL) &&
1563 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1564 root->standalone=MagickTrue;
1565 return;
1566 }
1567 if (root->processing_instructions[0] == (char **) NULL)
1568 {
1569 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1570 *root->processing_instructions));
1571 *root->processing_instructions=(char **) NULL;
1572 }
1573 i=0;
1574 while ((root->processing_instructions[i] != (char **) NULL) &&
1575 (strcmp(target,root->processing_instructions[i][0]) != 0))
1576 i++;
1577 if (root->processing_instructions[i] == (char **) NULL)
1578 {
1579 root->processing_instructions=(char ***) ResizeQuantumMemory(
1580 root->processing_instructions,(size_t) (i+2),
1581 sizeof(*root->processing_instructions));
1582 if (root->processing_instructions == (char ***) NULL)
1583 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1584 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1585 sizeof(**root->processing_instructions));
1586 if (root->processing_instructions[i] == (char **) NULL)
1587 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1588 root->processing_instructions[i+1]=(char **) NULL;
1589 root->processing_instructions[i][0]=ConstantString(target);
1590 root->processing_instructions[i][1]=(char *)
1591 root->processing_instructions[i+1];
1592 root->processing_instructions[i+1]=(char **) NULL;
1593 root->processing_instructions[i][2]=ConstantString("");
1594 }
1595 j=1;
1596 while (root->processing_instructions[i][j] != (char *) NULL)
1597 j++;
1598 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1599 root->processing_instructions[i],(size_t) (j+3),
1600 sizeof(**root->processing_instructions));
1601 if (root->processing_instructions[i] == (char **) NULL)
1602 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1603 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1604 root->processing_instructions[i][j+1],(size_t) (j+1),
1605 sizeof(***root->processing_instructions));
1606 if (root->processing_instructions[i][j+2] == (char *) NULL)
1607 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1608 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1609 root->root.tag != (char *) NULL ? ">" : "<",2);
1610 root->processing_instructions[i][j]=ConstantString(xml);
1611 root->processing_instructions[i][j+1]=(char *) NULL;
1612}
1613
1614static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1615 size_t length,ExceptionInfo *exception)
1616{
1617 char
1618 *c,
1619 **entities,
1620 *n,
1621 **predefined_entities,
1622 q,
1623 *t,
1624 *v;
1625
1626 ssize_t
1627 i,
1628 j;
1629
1630 n=(char *) NULL;
1631 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1632 if (predefined_entities == (char **) NULL)
1633 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1634 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1635 for (xml[length]='\0'; xml != (char *) NULL; )
1636 {
1637 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1638 xml++;
1639 if (*xml == '\0')
1640 break;
1641 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1642 {
1643 /*
1644 Parse entity definitions.
1645 */
1646 if (strspn(xml+8,XMLWhitespace) == 0)
1647 break;
1648 xml+=strspn(xml+8,XMLWhitespace)+8;
1649 c=xml;
1650 n=xml+strspn(xml,XMLWhitespace "%");
1651 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1652 break;
1653 xml=n+strcspn(n,XMLWhitespace);
1654 if (*xml == '\0')
1655 break;
1656 *xml=';';
1657 v=xml+strspn(xml+1,XMLWhitespace)+1;
1658 q=(*v);
1659 v++;
1660 if ((q != '"') && (q != '\''))
1661 {
1662 /*
1663 Skip externals.
1664 */
1665 xml=strchr(xml,'>');
1666 continue;
1667 }
1668 entities=(*c == '%') ? predefined_entities : root->entities;
1669 for (i=0; entities[i] != (char *) NULL; i++) ;
1670 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1671 sizeof(*entities));
1672 if (entities == (char **) NULL)
1673 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1674 if (*c == '%')
1675 predefined_entities=entities;
1676 else
1677 root->entities=entities;
1678 xml++;
1679 *xml='\0';
1680 xml=strchr(v,q);
1681 if (xml != (char *) NULL)
1682 {
1683 *xml='\0';
1684 xml++;
1685 }
1686 entities[i+1]=ParseEntities(v,predefined_entities,'%');
1687 entities[i+2]=(char *) NULL;
1688 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1689 entities[i]=n;
1690 else
1691 {
1692 if (entities[i+1] != v)
1693 entities[i+1]=DestroyString(entities[i+1]);
1694 (void) ThrowMagickException(exception,GetMagickModule(),
1695 OptionWarning,"ParseError","circular entity declaration &%s",n);
1696 predefined_entities=(char **) RelinquishMagickMemory(
1697 predefined_entities);
1698 return(MagickFalse);
1699 }
1700 }
1701 else
1702 if (strncmp(xml,"<!ATTLIST",9) == 0)
1703 {
1704 /*
1705 Parse default attributes.
1706 */
1707 t=xml+strspn(xml+9,XMLWhitespace)+9;
1708 if (*t == '\0')
1709 {
1710 (void) ThrowMagickException(exception,GetMagickModule(),
1711 OptionWarning,"ParseError","unclosed <!ATTLIST");
1712 predefined_entities=(char **) RelinquishMagickMemory(
1713 predefined_entities);
1714 return(MagickFalse);
1715 }
1716 xml=t+strcspn(t,XMLWhitespace ">");
1717 if (*xml == '>')
1718 continue;
1719 *xml='\0';
1720 i=0;
1721 while ((root->attributes[i] != (char **) NULL) &&
1722 (n != (char *) NULL) &&
1723 (strcmp(n,root->attributes[i][0]) != 0))
1724 i++;
1725 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1726 (*n != '>'))
1727 {
1728 xml=n+strcspn(n,XMLWhitespace);
1729 if (*xml != '\0')
1730 *xml='\0';
1731 else
1732 {
1733 (void) ThrowMagickException(exception,GetMagickModule(),
1734 OptionWarning,"ParseError","malformed <!ATTLIST");
1735 predefined_entities=(char **) RelinquishMagickMemory(
1736 predefined_entities);
1737 return(MagickFalse);
1738 }
1739 xml+=strspn(xml+1,XMLWhitespace)+1;
1740 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1741 if (strncmp(xml,"NOTATION",8) == 0)
1742 xml+=strspn(xml+8,XMLWhitespace)+8;
1743 xml=(*xml == '(') ? strchr(xml,')') : xml+
1744 strcspn(xml,XMLWhitespace);
1745 if (xml == (char *) NULL)
1746 {
1747 (void) ThrowMagickException(exception,GetMagickModule(),
1748 OptionWarning,"ParseError","malformed <!ATTLIST");
1749 predefined_entities=(char **) RelinquishMagickMemory(
1750 predefined_entities);
1751 return(MagickFalse);
1752 }
1753 xml+=strspn(xml,XMLWhitespace ")");
1754 if (strncmp(xml,"#FIXED",6) == 0)
1755 xml+=strspn(xml+6,XMLWhitespace)+6;
1756 if (*xml == '#')
1757 {
1758 xml+=strcspn(xml,XMLWhitespace ">")-1;
1759 if (*c == ' ')
1760 continue;
1761 v=(char *) NULL;
1762 }
1763 else
1764 if (((*xml == '"') || (*xml == '\'')) &&
1765 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1766 *xml='\0';
1767 else
1768 {
1769 (void) ThrowMagickException(exception,GetMagickModule(),
1770 OptionWarning,"ParseError","malformed <!ATTLIST");
1771 predefined_entities=(char **) RelinquishMagickMemory(
1772 predefined_entities);
1773 return(MagickFalse);
1774 }
1775 if (root->attributes[i] == (char **) NULL)
1776 {
1777 /*
1778 New attribute tag.
1779 */
1780 if (i == 0)
1781 root->attributes=(char ***) AcquireQuantumMemory(2,
1782 sizeof(*root->attributes));
1783 else
1784 root->attributes=(char ***) ResizeQuantumMemory(
1785 root->attributes,(size_t) (i+2),
1786 sizeof(*root->attributes));
1787 if (root->attributes == (char ***) NULL)
1788 ThrowFatalException(ResourceLimitFatalError,
1789 "MemoryAllocationFailed");
1790 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1791 sizeof(**root->attributes));
1792 if (root->attributes[i] == (char **) NULL)
1793 ThrowFatalException(ResourceLimitFatalError,
1794 "MemoryAllocationFailed");
1795 root->attributes[i][0]=ConstantString(t);
1796 root->attributes[i][1]=(char *) NULL;
1797 root->attributes[i+1]=(char **) NULL;
1798 }
1799 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1800 root->attributes[i]=(char **) ResizeQuantumMemory(
1801 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1802 if (root->attributes[i] == (char **) NULL)
1803 ThrowFatalException(ResourceLimitFatalError,
1804 "MemoryAllocationFailed");
1805 root->attributes[i][j+3]=(char *) NULL;
1806 root->attributes[i][j+2]=ConstantString(c);
1807 root->attributes[i][j+1]=(char *) NULL;
1808 if (v != (char *) NULL)
1809 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1810 root->attributes[i][j]=ConstantString(n);
1811 }
1812 }
1813 else
1814 if (strncmp(xml, "<!--", 4) == 0)
1815 xml=strstr(xml+4,"-->");
1816 else
1817 if (strncmp(xml,"<?", 2) == 0)
1818 {
1819 c=xml+2;
1820 xml=strstr(c,"?>");
1821 if (xml != (char *) NULL)
1822 {
1823 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1824 xml++;
1825 }
1826 }
1827 else
1828 if (*xml == '<')
1829 xml=strchr(xml,'>');
1830 else
1831 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1832 break;
1833 }
1834 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities);
1835 return(MagickTrue);
1836}
1837
1838static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1839{
1841 *xml_info;
1842
1843 xml_info=root->node;
1844 if (xml_info->tag == (char *) NULL)
1845 xml_info->tag=ConstantString(tag);
1846 else
1847 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1848 if (xml_info != (XMLTreeInfo *) NULL)
1849 xml_info->attributes=attributes;
1850 root->node=xml_info;
1851}
1852
1853static const char
1854 *ignore_tags[3] =
1855 {
1856 "rdf:Bag",
1857 "rdf:Seq",
1858 (const char *) NULL
1859 };
1860
1861static inline MagickBooleanType IsSkipTag(const char *tag)
1862{
1863 ssize_t
1864 i;
1865
1866 i=0;
1867 while (ignore_tags[i] != (const char *) NULL)
1868 {
1869 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1870 return(MagickTrue);
1871 i++;
1872 }
1873 return(MagickFalse);
1874}
1875
1876MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1877{
1878 char
1879 **attribute,
1880 **attributes,
1881 *p,
1882 *tag,
1883 *utf8;
1884
1885 int
1886 c,
1887 terminal;
1888
1889 MagickBooleanType
1890 status;
1891
1892 size_t
1893 ignore_depth,
1894 length;
1895
1896 ssize_t
1897 i,
1898 j,
1899 l;
1900
1902 *root;
1903
1904 /*
1905 Convert xml-string to UTF8.
1906 */
1907 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1908 {
1909 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1910 "ParseError","root tag missing");
1911 return((XMLTreeInfo *) NULL);
1912 }
1913 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1914 length=strlen(xml);
1915 utf8=ConvertUTF16ToUTF8(xml,&length);
1916 if (utf8 == (char *) NULL)
1917 {
1918 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1919 "ParseError","UTF16 to UTF8 failed");
1920 return((XMLTreeInfo *) NULL);
1921 }
1922 terminal=utf8[MagickMax(length-1,0)];
1923 utf8[MagickMax(length-1,0)]='\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=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=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
2288 *root;
2289
2290 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2291 if (root == (XMLTreeRoot *) NULL)
2292 return((XMLTreeInfo *) NULL);
2293 (void) memset(root,0,sizeof(*root));
2294 root->root.tag=(char *) NULL;
2295 if (tag != (char *) NULL)
2296 root->root.tag=ConstantString(tag);
2297 root->node=(&root->root);
2298 root->root.content=ConstantString("");
2299 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2300 if (root->entities == (char **) NULL)
2301 return((XMLTreeInfo *) NULL);
2302 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2303 root->root.attributes=sentinel;
2304 root->attributes=(char ***) root->root.attributes;
2305 root->processing_instructions=(char ***) root->root.attributes;
2306 root->debug=IsEventLogging();
2307 root->signature=MagickCoreSignature;
2308 return(&root->root);
2309}
2310
2311/*
2312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2313% %
2314% %
2315% %
2316% P r u n e T a g F r o m X M L T r e e %
2317% %
2318% %
2319% %
2320%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2321%
2322% PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2323% subtags.
2324%
2325% The format of the PruneTagFromXMLTree method is:
2326%
2327% XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2328%
2329% A description of each parameter follows:
2330%
2331% o xml_info: the xml info.
2332%
2333*/
2334MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2335{
2337 *node;
2338
2339 assert(xml_info != (XMLTreeInfo *) NULL);
2340 assert((xml_info->signature == MagickCoreSignature) ||
2341 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2342 if (IsEventLogging() != MagickFalse)
2343 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2344 if (xml_info->next != (XMLTreeInfo *) NULL)
2345 xml_info->next->sibling=xml_info->sibling;
2346 if (xml_info->parent != (XMLTreeInfo *) NULL)
2347 {
2348 node=xml_info->parent->child;
2349 if (node == xml_info)
2350 xml_info->parent->child=xml_info->ordered;
2351 else
2352 {
2353 while (node->ordered != xml_info)
2354 node=node->ordered;
2355 node->ordered=node->ordered->ordered;
2356 node=xml_info->parent->child;
2357 if (strcmp(node->tag,xml_info->tag) != 0)
2358 {
2359 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2360 node=node->sibling;
2361 if (node->sibling != xml_info)
2362 node=node->sibling;
2363 else
2364 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2365 xml_info->next : node->sibling->sibling;
2366 }
2367 while ((node->next != (XMLTreeInfo *) NULL) &&
2368 (node->next != xml_info))
2369 node=node->next;
2370 if (node->next != (XMLTreeInfo *) NULL)
2371 node->next=node->next->next;
2372 }
2373 }
2374 xml_info->ordered=(XMLTreeInfo *) NULL;
2375 xml_info->sibling=(XMLTreeInfo *) NULL;
2376 xml_info->next=(XMLTreeInfo *) NULL;
2377 return(xml_info);
2378}
2379
2380/*
2381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2382% %
2383% %
2384% %
2385% S e t X M L T r e e A t t r i b u t e %
2386% %
2387% %
2388% %
2389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2390%
2391% SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2392% found. A value of NULL removes the specified attribute.
2393%
2394% The format of the SetXMLTreeAttribute method is:
2395%
2396% XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2397% const char *value)
2398%
2399% A description of each parameter follows:
2400%
2401% o xml_info: the xml info.
2402%
2403% o tag: The attribute tag.
2404%
2405% o value: The attribute value.
2406%
2407*/
2408MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2409 const char *tag,const char *value)
2410{
2411 ssize_t
2412 i,
2413 j;
2414
2415 assert(xml_info != (XMLTreeInfo *) NULL);
2416 assert((xml_info->signature == MagickCoreSignature) ||
2417 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2418 if (IsEventLogging() != MagickFalse)
2419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2420 i=0;
2421 while ((xml_info->attributes[i] != (char *) NULL) &&
2422 (strcmp(xml_info->attributes[i],tag) != 0))
2423 i+=2;
2424 if (xml_info->attributes[i] == (char *) NULL)
2425 {
2426 /*
2427 Add new attribute tag.
2428 */
2429 if (value == (const char *) NULL)
2430 return(xml_info);
2431 if (xml_info->attributes != sentinel)
2432 xml_info->attributes=(char **) ResizeQuantumMemory(
2433 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2434 else
2435 {
2436 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2437 sizeof(*xml_info->attributes));
2438 if (xml_info->attributes != (char **) NULL)
2439 xml_info->attributes[1]=ConstantString("");
2440 }
2441 if (xml_info->attributes == (char **) NULL)
2442 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2443 xml_info->attributes[i]=ConstantString(tag);
2444 xml_info->attributes[i+2]=(char *) NULL;
2445 (void) strlen(xml_info->attributes[i+1]);
2446 }
2447 /*
2448 Add new value to an existing attribute.
2449 */
2450 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2451 if (xml_info->attributes[i+1] != (char *) NULL)
2452 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2453 if (value != (const char *) NULL)
2454 {
2455 xml_info->attributes[i+1]=ConstantString(value);
2456 return(xml_info);
2457 }
2458 if (xml_info->attributes[i] != (char *) NULL)
2459 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2460 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(size_t)
2461 (j-i)*sizeof(*xml_info->attributes));
2462 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2463 (size_t) (j+2),sizeof(*xml_info->attributes));
2464 if (xml_info->attributes == (char **) NULL)
2465 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2466 j-=2;
2467 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2468 (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
2469 return(xml_info);
2470}
2471
2472/*
2473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2474% %
2475% %
2476% %
2477% S e t X M L T r e e C o n t e n t %
2478% %
2479% %
2480% %
2481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2482%
2483% SetXMLTreeContent() sets the character content for the given tag and
2484% returns the tag.
2485%
2486% The format of the SetXMLTreeContent method is:
2487%
2488% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2489% const char *content)
2490%
2491% A description of each parameter follows:
2492%
2493% o xml_info: the xml info.
2494%
2495% o content: The content.
2496%
2497*/
2498MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2499 const char *content)
2500{
2501 assert(xml_info != (XMLTreeInfo *) NULL);
2502 assert((xml_info->signature == MagickCoreSignature) ||
2503 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2504 if (IsEventLogging() != MagickFalse)
2505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2506 if (xml_info->content != (char *) NULL)
2507 xml_info->content=DestroyString(xml_info->content);
2508 xml_info->content=(char *) ConstantString(content);
2509 return(xml_info);
2510}
2511
2512/*
2513%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2514% %
2515% %
2516% %
2517% X M L T r e e I n f o T o X M L %
2518% %
2519% %
2520% %
2521%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2522%
2523% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2524%
2525% The format of the XMLTreeInfoToXML method is:
2526%
2527% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2528%
2529% A description of each parameter follows:
2530%
2531% o xml_info: the xml info.
2532%
2533*/
2534
2535static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2536 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2537{
2538 char
2539 *canonical_content;
2540
2541 if (offset < 0)
2542 canonical_content=CanonicalXMLContent(source,pedantic);
2543 else
2544 {
2545 char
2546 *content;
2547
2548 content=AcquireString(source);
2549 content[offset]='\0';
2550 canonical_content=CanonicalXMLContent(content,pedantic);
2551 content=DestroyString(content);
2552 }
2553 if (canonical_content == (char *) NULL)
2554 return(*destination);
2555 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2556 {
2557 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2558 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2559 sizeof(**destination));
2560 if (*destination == (char *) NULL)
2561 return(*destination);
2562 }
2563 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2564 canonical_content);
2565 canonical_content=DestroyString(canonical_content);
2566 return(*destination);
2567}
2568
2569static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2570 size_t *extent,size_t start,char ***attributes)
2571{
2572 char
2573 *content;
2574
2575 const char
2576 *attribute;
2577
2578 size_t
2579 offset;
2580
2581 ssize_t
2582 i,
2583 j;
2584
2585 content=(char *) "";
2586 if (xml_info->parent != (XMLTreeInfo *) NULL)
2587 content=xml_info->parent->content;
2588 offset=0;
2589 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2590 start),source,length,extent,MagickFalse);
2591 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2592 {
2593 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2594 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2595 if (*source == (char *) NULL)
2596 return(*source);
2597 }
2598 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2599 "<%s",xml_info->tag);
2600 for (i=0; xml_info->attributes[i]; i+=2)
2601 {
2602 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2603 if (attribute != xml_info->attributes[i+1])
2604 continue;
2605 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2606 {
2607 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2608 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2609 if (*source == (char *) NULL)
2610 return((char *) NULL);
2611 }
2612 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2613 xml_info->attributes[i]);
2614 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2615 extent,MagickTrue);
2616 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2617 }
2618 i=0;
2619 while ((attributes[i] != (char **) NULL) &&
2620 (strcmp(attributes[i][0],xml_info->tag) != 0))
2621 i++;
2622 j=1;
2623 while ((attributes[i] != (char **) NULL) &&
2624 (attributes[i][j] != (char *) NULL))
2625 {
2626 if ((attributes[i][j+1] == (char *) NULL) ||
2627 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2628 {
2629 j+=3;
2630 continue;
2631 }
2632 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2633 {
2634 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2635 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2636 if (*source == (char *) NULL)
2637 return((char *) NULL);
2638 }
2639 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2640 attributes[i][j]);
2641 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2642 MagickTrue);
2643 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2644 j+=3;
2645 }
2646 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2647 *xml_info->content ? ">" : "/>");
2648 if (xml_info->child != (XMLTreeInfo *) NULL)
2649 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2650 else
2651 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2652 MagickFalse);
2653 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2654 {
2655 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2656 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2657 if (*source == (char *) NULL)
2658 return((char *) NULL);
2659 }
2660 if (*xml_info->content != '\0')
2661 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2662 xml_info->tag);
2663 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2664 offset++;
2665 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2666 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2667 attributes);
2668 else
2669 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2670 MagickFalse);
2671 return(content);
2672}
2673
2674MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2675{
2676 char
2677 *p,
2678 *q,
2679 *xml;
2680
2681 size_t
2682 extent,
2683 length;
2684
2685 ssize_t
2686 i,
2687 j,
2688 k;
2689
2691 *ordered,
2692 *parent;
2693
2695 *root;
2696
2697 assert(xml_info != (XMLTreeInfo *) NULL);
2698 assert((xml_info->signature == MagickCoreSignature) ||
2699 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2700 if (IsEventLogging() != MagickFalse)
2701 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2702 if (xml_info->tag == (char *) NULL)
2703 return((char *) NULL);
2704 xml=AcquireString((char *) NULL);
2705 length=0;
2706 extent=MagickPathExtent;
2707 root=(XMLTreeRoot *) xml_info;
2708 while (root->root.parent != (XMLTreeInfo *) NULL)
2709 root=(XMLTreeRoot *) root->root.parent;
2710 parent=xml_info->parent;
2711 if (parent == (XMLTreeInfo *) NULL)
2712 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2713 {
2714 /*
2715 Pre-root processing instructions.
2716 */
2717 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2718 p=root->processing_instructions[i][1];
2719 for (j=1; p != (char *) NULL; j++)
2720 {
2721 if (root->processing_instructions[i][k][j-1] == '>')
2722 {
2723 p=root->processing_instructions[i][j];
2724 continue;
2725 }
2726 q=root->processing_instructions[i][0];
2727 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2728 {
2729 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2730 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2731 if (xml == (char *) NULL)
2732 return(xml);
2733 }
2734 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2735 *p != '\0' ? " " : "",p);
2736 p=root->processing_instructions[i][j];
2737 }
2738 }
2739 ordered=xml_info->ordered;
2740 xml_info->parent=(XMLTreeInfo *) NULL;
2741 xml_info->ordered=(XMLTreeInfo *) NULL;
2742 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2743 xml_info->parent=parent;
2744 xml_info->ordered=ordered;
2745 if (parent == (XMLTreeInfo *) NULL)
2746 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2747 {
2748 /*
2749 Post-root processing instructions.
2750 */
2751 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2752 p=root->processing_instructions[i][1];
2753 for (j=1; p != (char *) NULL; j++)
2754 {
2755 if (root->processing_instructions[i][k][j-1] == '<')
2756 {
2757 p=root->processing_instructions[i][j];
2758 continue;
2759 }
2760 q=root->processing_instructions[i][0];
2761 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2762 {
2763 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2764 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2765 if (xml == (char *) NULL)
2766 return(xml);
2767 }
2768 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2769 *p != '\0' ? " " : "",p);
2770 p=root->processing_instructions[i][j];
2771 }
2772 }
2773 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2774}