MagickCore 7.1.2-19
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=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
720 XMLTreeRoot
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{
830 XMLTreeInfo
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
950 XMLTreeInfo
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
1015 XMLTreeRoot
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{
1130 XMLTreeInfo
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]=(char) 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++]=(char) ((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++)=(char) 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{
1476 XMLTreeInfo
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{
1840 XMLTreeInfo
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
1901 XMLTreeRoot
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 if (length == 0)
1923 {
1924 utf8=DestroyString(utf8);
1925 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1926 "ParseError","root tag missing");
1927 return((XMLTreeInfo *) NULL);
1928 }
1929 terminal=utf8[length-1];
1930 utf8[length-1]='\0';
1931 p=utf8;
1932 while ((*p != '\0') && (*p != '<'))
1933 p++;
1934 if (*p == '\0')
1935 {
1936 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1937 "ParseError","root tag missing");
1938 utf8=DestroyString(utf8);
1939 return((XMLTreeInfo *) NULL);
1940 }
1941 attribute=(char **) NULL;
1942 l=0;
1943 ignore_depth=0;
1944 for (p++; ; p++)
1945 {
1946 attributes=(char **) sentinel;
1947 tag=p;
1948 c=(*p);
1949 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1950 (*p == ':') || (c < '\0'))
1951 {
1952 /*
1953 Tag.
1954 */
1955 if (root->node == (XMLTreeInfo *) NULL)
1956 {
1957 (void) ThrowMagickException(exception,GetMagickModule(),
1958 OptionWarning,"ParseError","root tag missing");
1959 utf8=DestroyString(utf8);
1960 return(&root->root);
1961 }
1962 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1963 while (isspace((int) ((unsigned char) *p)) != 0)
1964 *p++='\0';
1965 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1966 (ignore_depth == 0))
1967 {
1968 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1969 {
1970 /*
1971 Find tag in default attributes list.
1972 */
1973 i=0;
1974 while ((root->attributes[i] != (char **) NULL) &&
1975 (strcmp(root->attributes[i][0],tag) != 0))
1976 i++;
1977 attribute=root->attributes[i];
1978 }
1979 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1980 {
1981 /*
1982 Attribute.
1983 */
1984 if (l == 0)
1985 attributes=(char **) AcquireQuantumMemory(4,
1986 sizeof(*attributes));
1987 else
1988 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1989 (l+4),sizeof(*attributes));
1990 if (attributes == (char **) NULL)
1991 {
1992 (void) ThrowMagickException(exception,GetMagickModule(),
1993 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1994 utf8=DestroyString(utf8);
1995 return(&root->root);
1996 }
1997 attributes[l+2]=(char *) NULL;
1998 attributes[l+1]=(char *) NULL;
1999 attributes[l]=p;
2000 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
2001 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
2002 attributes[l]=ConstantString("");
2003 else
2004 {
2005 *p++='\0';
2006 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
2007 c=(*p);
2008 if ((c == '"') || (c == '\''))
2009 {
2010 /*
2011 Attributes value.
2012 */
2013 p++;
2014 attributes[l+1]=p;
2015 while ((*p != '\0') && (*p != c))
2016 p++;
2017 if (*p != '\0')
2018 *p++='\0';
2019 else
2020 {
2021 attributes[l]=ConstantString("");
2022 attributes[l+1]=ConstantString("");
2023 (void) DestroyXMLTreeAttributes(attributes);
2024 (void) ThrowMagickException(exception,
2025 GetMagickModule(),OptionWarning,"ParseError",
2026 "missing %c",c);
2027 utf8=DestroyString(utf8);
2028 return(&root->root);
2029 }
2030 j=1;
2031 while ((attribute != (char **) NULL) &&
2032 (attribute[j] != (char *) NULL) &&
2033 (strcmp(attribute[j],attributes[l]) != 0))
2034 j+=3;
2035 attributes[l+1]=ParseEntities(attributes[l+1],
2036 root->entities,(attribute != (char **) NULL) &&
2037 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
2038 ' ');
2039 }
2040 attributes[l]=ConstantString(attributes[l]);
2041 }
2042 while (isspace((int) ((unsigned char) *p)) != 0)
2043 p++;
2044 }
2045 }
2046 else
2047 {
2048 while ((*p != '\0') && (*p != '/') && (*p != '>'))
2049 p++;
2050 }
2051 if (*p == '/')
2052 {
2053 /*
2054 Self closing tag.
2055 */
2056 *p++='\0';
2057 if (((*p != '\0') && (*p != '>')) ||
2058 ((*p == '\0') && (terminal != '>')))
2059 {
2060 if (l != 0)
2061 (void) DestroyXMLTreeAttributes(attributes);
2062 (void) ThrowMagickException(exception,GetMagickModule(),
2063 OptionWarning,"ParseError","missing >");
2064 utf8=DestroyString(utf8);
2065 return(&root->root);
2066 }
2067 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2068 (void) DestroyXMLTreeAttributes(attributes);
2069 else
2070 {
2071 ParseOpenTag(root,tag,attributes);
2072 (void) ParseCloseTag(root,tag,exception);
2073 }
2074 }
2075 else
2076 {
2077 c=(*p);
2078 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
2079 {
2080 *p='\0';
2081 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2082 ParseOpenTag(root,tag,attributes);
2083 else
2084 {
2085 ignore_depth++;
2086 (void) DestroyXMLTreeAttributes(attributes);
2087 }
2088 *p=(char) c;
2089 }
2090 else
2091 {
2092 if (l != 0)
2093 (void) DestroyXMLTreeAttributes(attributes);
2094 (void) ThrowMagickException(exception,GetMagickModule(),
2095 OptionWarning,"ParseError","missing >");
2096 utf8=DestroyString(utf8);
2097 return(&root->root);
2098 }
2099 }
2100 }
2101 else
2102 if (*p == '/')
2103 {
2104 /*
2105 Close tag.
2106 */
2107 tag=p+1;
2108 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
2109 c=(*p);
2110 if ((c == '\0') && (terminal != '>'))
2111 {
2112 (void) ThrowMagickException(exception,GetMagickModule(),
2113 OptionWarning,"ParseError","missing >");
2114 utf8=DestroyString(utf8);
2115 return(&root->root);
2116 }
2117 *p='\0';
2118 if ((ignore_depth == 0) &&
2119 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2120 {
2121 utf8=DestroyString(utf8);
2122 return(&root->root);
2123 }
2124 if (ignore_depth > 0)
2125 ignore_depth--;
2126 *p=(char) c;
2127 if (isspace((int) ((unsigned char) *p)) != 0)
2128 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2129 }
2130 else
2131 if (strncmp(p,"!--",3) == 0)
2132 {
2133 /*
2134 Comment.
2135 */
2136 p=strstr(p+3,"--");
2137 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
2138 ((*p == '\0') && (terminal != '>')))
2139 {
2140 (void) ThrowMagickException(exception,GetMagickModule(),
2141 OptionWarning,"ParseError","unclosed <!--");
2142 utf8=DestroyString(utf8);
2143 return(&root->root);
2144 }
2145 }
2146 else
2147 if (strncmp(p,"![CDATA[",8) == 0)
2148 {
2149 /*
2150 Cdata.
2151 */
2152 p=strstr(p,"]]>");
2153 if (p != (char *) NULL)
2154 {
2155 p+=(ptrdiff_t) 2;
2156 if (ignore_depth == 0)
2157 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
2158 }
2159 else
2160 {
2161 (void) ThrowMagickException(exception,GetMagickModule(),
2162 OptionWarning,"ParseError","unclosed <![CDATA[");
2163 utf8=DestroyString(utf8);
2164 return(&root->root);
2165 }
2166 }
2167 else
2168 if (strncmp(p,"!DOCTYPE",8) == 0)
2169 {
2170 /*
2171 DTD.
2172 */
2173 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
2174 ((l != 0) && ((*p != ']') ||
2175 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
2176 l=(ssize_t) ((*p == '[') ? 1 : l))
2177 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
2178 if ((*p == '\0') && (terminal != '>'))
2179 {
2180 (void) ThrowMagickException(exception,GetMagickModule(),
2181 OptionWarning,"ParseError","unclosed <!DOCTYPE");
2182 utf8=DestroyString(utf8);
2183 return(&root->root);
2184 }
2185 if (l != 0)
2186 tag=strchr(tag,'[')+1;
2187 if (l != 0)
2188 {
2189 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
2190 exception);
2191 if (status == MagickFalse)
2192 {
2193 utf8=DestroyString(utf8);
2194 return(&root->root);
2195 }
2196 p++;
2197 }
2198 }
2199 else
2200 if (*p == '?')
2201 {
2202 /*
2203 Processing instructions.
2204 */
2205 do
2206 {
2207 p=strchr(p,'?');
2208 if (p == (char *) NULL)
2209 break;
2210 p++;
2211 } while ((*p != '\0') && (*p != '>'));
2212 if ((p == (char *) NULL) || ((*p == '\0') &&
2213 (terminal != '>')))
2214 {
2215 (void) ThrowMagickException(exception,GetMagickModule(),
2216 OptionWarning,"ParseError","unclosed <?");
2217 utf8=DestroyString(utf8);
2218 return(&root->root);
2219 }
2220 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2221 }
2222 else
2223 {
2224 (void) ThrowMagickException(exception,GetMagickModule(),
2225 OptionWarning,"ParseError","unexpected <");
2226 utf8=DestroyString(utf8);
2227 return(&root->root);
2228 }
2229 if ((p == (char *) NULL) || (*p == '\0'))
2230 break;
2231 *p++='\0';
2232 tag=p;
2233 if ((*p != '\0') && (*p != '<'))
2234 {
2235 /*
2236 Tag character content.
2237 */
2238 while ((*p != '\0') && (*p != '<'))
2239 p++;
2240 if (*p == '\0')
2241 break;
2242 if (ignore_depth == 0)
2243 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2244 }
2245 else
2246 if (*p == '\0')
2247 break;
2248 }
2249 utf8=DestroyString(utf8);
2250 if (root->node == (XMLTreeInfo *) NULL)
2251 return(&root->root);
2252 if (root->node->tag == (char *) NULL)
2253 {
2254 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2255 "ParseError","root tag missing");
2256 return(&root->root);
2257 }
2258 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2259 "ParseError","unclosed tag: '%s'",root->node->tag);
2260 return(&root->root);
2261}
2262
2263/*
2264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2265% %
2266% %
2267% %
2268% N e w X M L T r e e T a g %
2269% %
2270% %
2271% %
2272%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2273%
2274% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2275%
2276% The format of the NewXMLTreeTag method is:
2277%
2278% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2279%
2280% A description of each parameter follows:
2281%
2282% o tag: the tag.
2283%
2284*/
2285MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2286{
2287 static const char
2288 *predefined_entities[NumberPredefinedEntities+1] =
2289 {
2290 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2291 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2292 };
2293
2294 XMLTreeRoot
2295 *root;
2296
2297 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2298 (void) memset(root,0,sizeof(*root));
2299 root->root.tag=(char *) NULL;
2300 if (tag != (char *) NULL)
2301 root->root.tag=ConstantString(tag);
2302 root->node=(&root->root);
2303 root->root.content=ConstantString("");
2304 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2305 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2306 root->root.attributes=sentinel;
2307 root->attributes=(char ***) root->root.attributes;
2308 root->processing_instructions=(char ***) root->root.attributes;
2309 root->debug=IsEventLogging();
2310 root->signature=MagickCoreSignature;
2311 return(&root->root);
2312}
2313
2314/*
2315%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2316% %
2317% %
2318% %
2319% P r u n e T a g F r o m X M L T r e e %
2320% %
2321% %
2322% %
2323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2324%
2325% PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2326% subtags.
2327%
2328% The format of the PruneTagFromXMLTree method is:
2329%
2330% XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2331%
2332% A description of each parameter follows:
2333%
2334% o xml_info: the xml info.
2335%
2336*/
2337MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2338{
2339 XMLTreeInfo
2340 *node;
2341
2342 assert(xml_info != (XMLTreeInfo *) NULL);
2343 assert((xml_info->signature == MagickCoreSignature) ||
2344 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2345 if (IsEventLogging() != MagickFalse)
2346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2347 if (xml_info->next != (XMLTreeInfo *) NULL)
2348 xml_info->next->sibling=xml_info->sibling;
2349 if (xml_info->parent != (XMLTreeInfo *) NULL)
2350 {
2351 node=xml_info->parent->child;
2352 if (node == xml_info)
2353 xml_info->parent->child=xml_info->ordered;
2354 else
2355 {
2356 while (node->ordered != xml_info)
2357 node=node->ordered;
2358 node->ordered=node->ordered->ordered;
2359 node=xml_info->parent->child;
2360 if (strcmp(node->tag,xml_info->tag) != 0)
2361 {
2362 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2363 node=node->sibling;
2364 if (node->sibling != xml_info)
2365 node=node->sibling;
2366 else
2367 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2368 xml_info->next : node->sibling->sibling;
2369 }
2370 while ((node->next != (XMLTreeInfo *) NULL) &&
2371 (node->next != xml_info))
2372 node=node->next;
2373 if (node->next != (XMLTreeInfo *) NULL)
2374 node->next=node->next->next;
2375 }
2376 }
2377 xml_info->ordered=(XMLTreeInfo *) NULL;
2378 xml_info->sibling=(XMLTreeInfo *) NULL;
2379 xml_info->next=(XMLTreeInfo *) NULL;
2380 return(xml_info);
2381}
2382
2383/*
2384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2385% %
2386% %
2387% %
2388% S e t X M L T r e e A t t r i b u t e %
2389% %
2390% %
2391% %
2392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2393%
2394% SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2395% found. A value of NULL removes the specified attribute.
2396%
2397% The format of the SetXMLTreeAttribute method is:
2398%
2399% XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2400% const char *value)
2401%
2402% A description of each parameter follows:
2403%
2404% o xml_info: the xml info.
2405%
2406% o tag: The attribute tag.
2407%
2408% o value: The attribute value.
2409%
2410*/
2411MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2412 const char *tag,const char *value)
2413{
2414 ssize_t
2415 i,
2416 j;
2417
2418 assert(xml_info != (XMLTreeInfo *) NULL);
2419 assert((xml_info->signature == MagickCoreSignature) ||
2420 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2421 if (IsEventLogging() != MagickFalse)
2422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2423 i=0;
2424 while ((xml_info->attributes[i] != (char *) NULL) &&
2425 (strcmp(xml_info->attributes[i],tag) != 0))
2426 i+=2;
2427 if (xml_info->attributes[i] == (char *) NULL)
2428 {
2429 /*
2430 Add new attribute tag.
2431 */
2432 if (value == (const char *) NULL)
2433 return(xml_info);
2434 if (xml_info->attributes != sentinel)
2435 xml_info->attributes=(char **) ResizeQuantumMemory(
2436 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2437 else
2438 {
2439 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2440 sizeof(*xml_info->attributes));
2441 if (xml_info->attributes != (char **) NULL)
2442 xml_info->attributes[1]=ConstantString("");
2443 }
2444 if (xml_info->attributes == (char **) NULL)
2445 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2446 xml_info->attributes[i]=ConstantString(tag);
2447 xml_info->attributes[i+2]=(char *) NULL;
2448 (void) strlen(xml_info->attributes[i+1]);
2449 }
2450 /*
2451 Add new value to an existing attribute.
2452 */
2453 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2454 if (xml_info->attributes[i+1] != (char *) NULL)
2455 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2456 if (value != (const char *) NULL)
2457 {
2458 xml_info->attributes[i+1]=ConstantString(value);
2459 return(xml_info);
2460 }
2461 if (xml_info->attributes[i] != (char *) NULL)
2462 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2463 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(size_t)
2464 (j-i)*sizeof(*xml_info->attributes));
2465 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2466 (size_t) (j+2),sizeof(*xml_info->attributes));
2467 if (xml_info->attributes == (char **) NULL)
2468 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2469 j-=2;
2470 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2471 (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
2472 return(xml_info);
2473}
2474
2475/*
2476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2477% %
2478% %
2479% %
2480% S e t X M L T r e e C o n t e n t %
2481% %
2482% %
2483% %
2484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2485%
2486% SetXMLTreeContent() sets the character content for the given tag and
2487% returns the tag.
2488%
2489% The format of the SetXMLTreeContent method is:
2490%
2491% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2492% const char *content)
2493%
2494% A description of each parameter follows:
2495%
2496% o xml_info: the xml info.
2497%
2498% o content: The content.
2499%
2500*/
2501MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2502 const char *content)
2503{
2504 assert(xml_info != (XMLTreeInfo *) NULL);
2505 assert((xml_info->signature == MagickCoreSignature) ||
2506 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2507 if (IsEventLogging() != MagickFalse)
2508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2509 if (xml_info->content != (char *) NULL)
2510 xml_info->content=DestroyString(xml_info->content);
2511 xml_info->content=(char *) ConstantString(content);
2512 return(xml_info);
2513}
2514
2515/*
2516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2517% %
2518% %
2519% %
2520% X M L T r e e I n f o T o X M L %
2521% %
2522% %
2523% %
2524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2525%
2526% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2527%
2528% The format of the XMLTreeInfoToXML method is:
2529%
2530% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2531%
2532% A description of each parameter follows:
2533%
2534% o xml_info: the xml info.
2535%
2536*/
2537
2538static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2539 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2540{
2541 char
2542 *canonical_content;
2543
2544 if (offset < 0)
2545 canonical_content=CanonicalXMLContent(source,pedantic);
2546 else
2547 {
2548 char
2549 *content;
2550
2551 content=AcquireString(source);
2552 content[offset]='\0';
2553 canonical_content=CanonicalXMLContent(content,pedantic);
2554 content=DestroyString(content);
2555 }
2556 if (canonical_content == (char *) NULL)
2557 return(*destination);
2558 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2559 {
2560 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2561 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2562 sizeof(**destination));
2563 if (*destination == (char *) NULL)
2564 return(*destination);
2565 }
2566 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2567 canonical_content);
2568 canonical_content=DestroyString(canonical_content);
2569 return(*destination);
2570}
2571
2572static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2573 size_t *extent,size_t start,char ***attributes)
2574{
2575 char
2576 *content;
2577
2578 const char
2579 *attribute;
2580
2581 size_t
2582 offset;
2583
2584 ssize_t
2585 i,
2586 j;
2587
2588 content=(char *) "";
2589 if (xml_info->parent != (XMLTreeInfo *) NULL)
2590 content=xml_info->parent->content;
2591 offset=0;
2592 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2593 start),source,length,extent,MagickFalse);
2594 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2595 {
2596 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2597 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2598 if (*source == (char *) NULL)
2599 return(*source);
2600 }
2601 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2602 "<%s",xml_info->tag);
2603 for (i=0; xml_info->attributes[i]; i+=2)
2604 {
2605 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2606 if (attribute != xml_info->attributes[i+1])
2607 continue;
2608 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2609 {
2610 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2611 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2612 if (*source == (char *) NULL)
2613 return((char *) NULL);
2614 }
2615 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2616 xml_info->attributes[i]);
2617 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2618 extent,MagickTrue);
2619 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2620 }
2621 i=0;
2622 while ((attributes[i] != (char **) NULL) &&
2623 (strcmp(attributes[i][0],xml_info->tag) != 0))
2624 i++;
2625 j=1;
2626 while ((attributes[i] != (char **) NULL) &&
2627 (attributes[i][j] != (char *) NULL))
2628 {
2629 if ((attributes[i][j+1] == (char *) NULL) ||
2630 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2631 {
2632 j+=3;
2633 continue;
2634 }
2635 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2636 {
2637 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2638 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2639 if (*source == (char *) NULL)
2640 return((char *) NULL);
2641 }
2642 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2643 attributes[i][j]);
2644 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2645 MagickTrue);
2646 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2647 j+=3;
2648 }
2649 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2650 *xml_info->content ? ">" : "/>");
2651 if (xml_info->child != (XMLTreeInfo *) NULL)
2652 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2653 else
2654 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2655 MagickFalse);
2656 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2657 {
2658 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2659 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2660 if (*source == (char *) NULL)
2661 return((char *) NULL);
2662 }
2663 if (*xml_info->content != '\0')
2664 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2665 xml_info->tag);
2666 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2667 offset++;
2668 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2669 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2670 attributes);
2671 else
2672 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2673 MagickFalse);
2674 return(content);
2675}
2676
2677MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2678{
2679 char
2680 *p,
2681 *q,
2682 *xml;
2683
2684 size_t
2685 extent,
2686 length;
2687
2688 ssize_t
2689 i,
2690 j,
2691 k;
2692
2693 XMLTreeInfo
2694 *ordered,
2695 *parent;
2696
2697 XMLTreeRoot
2698 *root;
2699
2700 assert(xml_info != (XMLTreeInfo *) NULL);
2701 assert((xml_info->signature == MagickCoreSignature) ||
2702 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2703 if (IsEventLogging() != MagickFalse)
2704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2705 if (xml_info->tag == (char *) NULL)
2706 return((char *) NULL);
2707 xml=AcquireString((char *) NULL);
2708 length=0;
2709 extent=MagickPathExtent;
2710 root=(XMLTreeRoot *) xml_info;
2711 while (root->root.parent != (XMLTreeInfo *) NULL)
2712 root=(XMLTreeRoot *) root->root.parent;
2713 parent=xml_info->parent;
2714 if (parent == (XMLTreeInfo *) NULL)
2715 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2716 {
2717 /*
2718 Pre-root processing instructions.
2719 */
2720 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2721 p=root->processing_instructions[i][1];
2722 for (j=1; p != (char *) NULL; j++)
2723 {
2724 if (root->processing_instructions[i][k][j-1] == '>')
2725 {
2726 p=root->processing_instructions[i][j];
2727 continue;
2728 }
2729 q=root->processing_instructions[i][0];
2730 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2731 {
2732 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2733 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2734 if (xml == (char *) NULL)
2735 return(xml);
2736 }
2737 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2738 *p != '\0' ? " " : "",p);
2739 p=root->processing_instructions[i][j];
2740 }
2741 }
2742 ordered=xml_info->ordered;
2743 xml_info->parent=(XMLTreeInfo *) NULL;
2744 xml_info->ordered=(XMLTreeInfo *) NULL;
2745 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2746 xml_info->parent=parent;
2747 xml_info->ordered=ordered;
2748 if (parent == (XMLTreeInfo *) NULL)
2749 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2750 {
2751 /*
2752 Post-root processing instructions.
2753 */
2754 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2755 p=root->processing_instructions[i][1];
2756 for (j=1; p != (char *) NULL; j++)
2757 {
2758 if (root->processing_instructions[i][k][j-1] == '<')
2759 {
2760 p=root->processing_instructions[i][j];
2761 continue;
2762 }
2763 q=root->processing_instructions[i][0];
2764 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2765 {
2766 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2767 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2768 if (xml == (char *) NULL)
2769 return(xml);
2770 }
2771 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2772 *p != '\0' ? " " : "",p);
2773 p=root->processing_instructions[i][j];
2774 }
2775 }
2776 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2777}