MagickCore 7.1.2-26
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 if (p != (char *) NULL)
1414 p[extent]='\0';
1415 }
1416 else
1417 {
1418 char
1419 *extent_xml;
1420
1421 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1422 sizeof(*extent_xml));
1423 if (extent_xml != (char *) NULL)
1424 {
1425 memset(extent_xml,0,extent*sizeof(*extent_xml));
1426 (void) CopyMagickString(extent_xml,p,extent*
1427 sizeof(*extent_xml));
1428 }
1429 p=extent_xml;
1430 }
1431 if (p == (char *) NULL)
1432 ThrowFatalException(ResourceLimitFatalError,
1433 "MemoryAllocationFailed");
1434 xml=p+offset;
1435 entity=strchr(xml,';');
1436 }
1437 if (entity != (char *) NULL)
1438 (void) memmove(xml+length,entity+1,strlen(entity));
1439 (void) memcpy(xml,entities[i],length);
1440 }
1441 }
1442 else
1443 if (((state == ' ') || (state == '*')) &&
1444 (isspace((int) ((unsigned char) *xml)) != 0))
1445 *(xml++)=' ';
1446 else
1447 xml++;
1448 }
1449 if (state == '*')
1450 {
1451 /*
1452 Normalize spaces for non-CDATA attributes.
1453 */
1454 for (xml=p; *xml != '\0'; xml++)
1455 {
1456 char
1457 accept[] = " ";
1458
1459 i=(ssize_t) strspn(xml,accept);
1460 if (i != 0)
1461 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1462 while ((*xml != '\0') && (*xml != ' '))
1463 xml++;
1464 if (*xml == '\0')
1465 break;
1466 }
1467 xml--;
1468 if ((xml >= p) && (*xml == ' '))
1469 *xml='\0';
1470 }
1471 return(p == q ? ConstantString(p) : p);
1472}
1473
1474static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1475 const size_t length,const char state)
1476{
1477 XMLTreeInfo
1478 *xml_info;
1479
1480 xml_info=root->node;
1481 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1482 (length == 0))
1483 return;
1484 xml[length]='\0';
1485 xml=ParseEntities(xml,root->entities,state);
1486 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1487 {
1488 (void) ConcatenateString(&xml_info->content,xml);
1489 xml=DestroyString(xml);
1490 }
1491 else
1492 {
1493 if (xml_info->content != (char *) NULL)
1494 xml_info->content=DestroyString(xml_info->content);
1495 xml_info->content=xml;
1496 }
1497}
1498
1499static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1500 ExceptionInfo *exception)
1501{
1502 if ((root->node == (XMLTreeInfo *) NULL) ||
1503 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1504 {
1505 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1506 "ParseError","unexpected closing tag </%s>",tag);
1507 return(&root->root);
1508 }
1509 root->node=root->node->parent;
1510 return((XMLTreeInfo *) NULL);
1511}
1512
1513static MagickBooleanType ValidateEntities(char *tag,char *xml,
1514 const size_t depth,char **entities)
1515{
1516 ssize_t
1517 i;
1518
1519 /*
1520 Check for circular entity references.
1521 */
1522 if (depth > MagickMaxRecursionDepth)
1523 return(MagickFalse);
1524 for ( ; ; xml++)
1525 {
1526 while ((*xml != '\0') && (*xml != '&'))
1527 xml++;
1528 if (*xml == '\0')
1529 return(MagickTrue);
1530 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1531 return(MagickFalse);
1532 i=0;
1533 while ((entities[i] != (char *) NULL) &&
1534 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1535 i+=2;
1536 if ((entities[i] != (char *) NULL) &&
1537 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1538 return(MagickFalse);
1539 }
1540}
1541
1542static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1543 size_t length)
1544{
1545 char
1546 *target;
1547
1548 ssize_t
1549 i,
1550 j;
1551
1552 target=xml;
1553 xml[length]='\0';
1554 xml+=strcspn(xml,XMLWhitespace);
1555 if (*xml != '\0')
1556 {
1557 *xml='\0';
1558 xml+=strspn(xml+1,XMLWhitespace)+1;
1559 }
1560 if (strcmp(target,"xml") == 0)
1561 {
1562 xml=strstr(xml,"standalone");
1563 if ((xml != (char *) NULL) &&
1564 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1565 root->standalone=MagickTrue;
1566 return;
1567 }
1568 if (root->processing_instructions[0] == (char **) NULL)
1569 {
1570 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1571 *root->processing_instructions));
1572 *root->processing_instructions=(char **) NULL;
1573 }
1574 i=0;
1575 while ((root->processing_instructions[i] != (char **) NULL) &&
1576 (strcmp(target,root->processing_instructions[i][0]) != 0))
1577 i++;
1578 if (root->processing_instructions[i] == (char **) NULL)
1579 {
1580 root->processing_instructions=(char ***) ResizeQuantumMemory(
1581 root->processing_instructions,(size_t) (i+2),
1582 sizeof(*root->processing_instructions));
1583 if (root->processing_instructions == (char ***) NULL)
1584 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1585 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1586 sizeof(**root->processing_instructions));
1587 if (root->processing_instructions[i] == (char **) NULL)
1588 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1589 root->processing_instructions[i+1]=(char **) NULL;
1590 root->processing_instructions[i][0]=ConstantString(target);
1591 root->processing_instructions[i][1]=(char *)
1592 root->processing_instructions[i+1];
1593 root->processing_instructions[i+1]=(char **) NULL;
1594 root->processing_instructions[i][2]=ConstantString("");
1595 }
1596 j=1;
1597 while (root->processing_instructions[i][j] != (char *) NULL)
1598 j++;
1599 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1600 root->processing_instructions[i],(size_t) (j+3),
1601 sizeof(**root->processing_instructions));
1602 if (root->processing_instructions[i] == (char **) NULL)
1603 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1604 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1605 root->processing_instructions[i][j+1],(size_t) (j+1),
1606 sizeof(***root->processing_instructions));
1607 if (root->processing_instructions[i][j+2] == (char *) NULL)
1608 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1609 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1610 root->root.tag != (char *) NULL ? ">" : "<",2);
1611 root->processing_instructions[i][j]=ConstantString(xml);
1612 root->processing_instructions[i][j+1]=(char *) NULL;
1613}
1614
1615static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1616 size_t length,ExceptionInfo *exception)
1617{
1618 char
1619 *c,
1620 **entities,
1621 *n,
1622 **predefined_entities,
1623 q,
1624 *t,
1625 *v;
1626
1627 ssize_t
1628 i,
1629 j;
1630
1631 n=(char *) NULL;
1632 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1633 if (predefined_entities == (char **) NULL)
1634 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1635 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1636 for (xml[length]='\0'; xml != (char *) NULL; )
1637 {
1638 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1639 xml++;
1640 if (*xml == '\0')
1641 break;
1642 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1643 {
1644 /*
1645 Parse entity definitions.
1646 */
1647 if (strspn(xml+8,XMLWhitespace) == 0)
1648 break;
1649 xml+=strspn(xml+8,XMLWhitespace)+8;
1650 c=xml;
1651 n=xml+strspn(xml,XMLWhitespace "%");
1652 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1653 break;
1654 xml=n+strcspn(n,XMLWhitespace);
1655 if (*xml == '\0')
1656 break;
1657 *xml=';';
1658 v=xml+strspn(xml+1,XMLWhitespace)+1;
1659 q=(*v);
1660 v++;
1661 if ((q != '"') && (q != '\''))
1662 {
1663 /*
1664 Skip externals.
1665 */
1666 xml=strchr(xml,'>');
1667 continue;
1668 }
1669 entities=(*c == '%') ? predefined_entities : root->entities;
1670 for (i=0; entities[i] != (char *) NULL; i++) ;
1671 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1672 sizeof(*entities));
1673 if (entities == (char **) NULL)
1674 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1675 if (*c == '%')
1676 predefined_entities=entities;
1677 else
1678 root->entities=entities;
1679 xml++;
1680 *xml='\0';
1681 xml=strchr(v,q);
1682 if (xml != (char *) NULL)
1683 {
1684 *xml='\0';
1685 xml++;
1686 }
1687 entities[i+1]=ParseEntities(v,predefined_entities,'%');
1688 entities[i+2]=(char *) NULL;
1689 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1690 entities[i]=n;
1691 else
1692 {
1693 if (entities[i+1] != v)
1694 entities[i+1]=DestroyString(entities[i+1]);
1695 (void) ThrowMagickException(exception,GetMagickModule(),
1696 OptionWarning,"ParseError","circular entity declaration &%s",n);
1697 predefined_entities=(char **) RelinquishMagickMemory(
1698 predefined_entities);
1699 return(MagickFalse);
1700 }
1701 }
1702 else
1703 if (strncmp(xml,"<!ATTLIST",9) == 0)
1704 {
1705 /*
1706 Parse default attributes.
1707 */
1708 t=xml+strspn(xml+9,XMLWhitespace)+9;
1709 if (*t == '\0')
1710 {
1711 (void) ThrowMagickException(exception,GetMagickModule(),
1712 OptionWarning,"ParseError","unclosed <!ATTLIST");
1713 predefined_entities=(char **) RelinquishMagickMemory(
1714 predefined_entities);
1715 return(MagickFalse);
1716 }
1717 xml=t+strcspn(t,XMLWhitespace ">");
1718 if (*xml == '>')
1719 continue;
1720 *xml='\0';
1721 i=0;
1722 while ((root->attributes[i] != (char **) NULL) &&
1723 (n != (char *) NULL) &&
1724 (strcmp(n,root->attributes[i][0]) != 0))
1725 i++;
1726 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1727 (*n != '>'))
1728 {
1729 xml=n+strcspn(n,XMLWhitespace);
1730 if (*xml != '\0')
1731 *xml='\0';
1732 else
1733 {
1734 (void) ThrowMagickException(exception,GetMagickModule(),
1735 OptionWarning,"ParseError","malformed <!ATTLIST");
1736 predefined_entities=(char **) RelinquishMagickMemory(
1737 predefined_entities);
1738 return(MagickFalse);
1739 }
1740 xml+=strspn(xml+1,XMLWhitespace)+1;
1741 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1742 if (strncmp(xml,"NOTATION",8) == 0)
1743 xml+=strspn(xml+8,XMLWhitespace)+8;
1744 xml=(*xml == '(') ? strchr(xml,')') : xml+
1745 strcspn(xml,XMLWhitespace);
1746 if (xml == (char *) NULL)
1747 {
1748 (void) ThrowMagickException(exception,GetMagickModule(),
1749 OptionWarning,"ParseError","malformed <!ATTLIST");
1750 predefined_entities=(char **) RelinquishMagickMemory(
1751 predefined_entities);
1752 return(MagickFalse);
1753 }
1754 xml+=strspn(xml,XMLWhitespace ")");
1755 if (strncmp(xml,"#FIXED",6) == 0)
1756 xml+=strspn(xml+6,XMLWhitespace)+6;
1757 if (*xml == '#')
1758 {
1759 xml+=strcspn(xml,XMLWhitespace ">")-1;
1760 if (*c == ' ')
1761 continue;
1762 v=(char *) NULL;
1763 }
1764 else
1765 if (((*xml == '"') || (*xml == '\'')) &&
1766 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1767 *xml='\0';
1768 else
1769 {
1770 (void) ThrowMagickException(exception,GetMagickModule(),
1771 OptionWarning,"ParseError","malformed <!ATTLIST");
1772 predefined_entities=(char **) RelinquishMagickMemory(
1773 predefined_entities);
1774 return(MagickFalse);
1775 }
1776 if (root->attributes[i] == (char **) NULL)
1777 {
1778 /*
1779 New attribute tag.
1780 */
1781 if (i == 0)
1782 root->attributes=(char ***) AcquireQuantumMemory(2,
1783 sizeof(*root->attributes));
1784 else
1785 root->attributes=(char ***) ResizeQuantumMemory(
1786 root->attributes,(size_t) (i+2),
1787 sizeof(*root->attributes));
1788 if (root->attributes == (char ***) NULL)
1789 ThrowFatalException(ResourceLimitFatalError,
1790 "MemoryAllocationFailed");
1791 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1792 sizeof(**root->attributes));
1793 if (root->attributes[i] == (char **) NULL)
1794 ThrowFatalException(ResourceLimitFatalError,
1795 "MemoryAllocationFailed");
1796 root->attributes[i][0]=ConstantString(t);
1797 root->attributes[i][1]=(char *) NULL;
1798 root->attributes[i+1]=(char **) NULL;
1799 }
1800 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1801 root->attributes[i]=(char **) ResizeQuantumMemory(
1802 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1803 if (root->attributes[i] == (char **) NULL)
1804 ThrowFatalException(ResourceLimitFatalError,
1805 "MemoryAllocationFailed");
1806 root->attributes[i][j+3]=(char *) NULL;
1807 root->attributes[i][j+2]=ConstantString(c);
1808 root->attributes[i][j+1]=(char *) NULL;
1809 if (v != (char *) NULL)
1810 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1811 root->attributes[i][j]=ConstantString(n);
1812 }
1813 }
1814 else
1815 if (strncmp(xml, "<!--", 4) == 0)
1816 xml=strstr(xml+4,"-->");
1817 else
1818 if (strncmp(xml,"<?", 2) == 0)
1819 {
1820 c=xml+2;
1821 xml=strstr(c,"?>");
1822 if (xml != (char *) NULL)
1823 {
1824 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1825 xml++;
1826 }
1827 }
1828 else
1829 if (*xml == '<')
1830 xml=strchr(xml,'>');
1831 else
1832 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1833 break;
1834 }
1835 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities);
1836 return(MagickTrue);
1837}
1838
1839static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1840{
1841 XMLTreeInfo
1842 *xml_info;
1843
1844 xml_info=root->node;
1845 if (xml_info->tag == (char *) NULL)
1846 xml_info->tag=ConstantString(tag);
1847 else
1848 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1849 if (xml_info != (XMLTreeInfo *) NULL)
1850 xml_info->attributes=attributes;
1851 root->node=xml_info;
1852}
1853
1854static const char
1855 *ignore_tags[3] =
1856 {
1857 "rdf:Bag",
1858 "rdf:Seq",
1859 (const char *) NULL
1860 };
1861
1862static inline MagickBooleanType IsSkipTag(const char *tag)
1863{
1864 ssize_t
1865 i;
1866
1867 i=0;
1868 while (ignore_tags[i] != (const char *) NULL)
1869 {
1870 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1871 return(MagickTrue);
1872 i++;
1873 }
1874 return(MagickFalse);
1875}
1876
1877MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1878{
1879 char
1880 **attribute,
1881 **attributes,
1882 *p,
1883 *tag,
1884 *utf8;
1885
1886 int
1887 c,
1888 terminal;
1889
1890 MagickBooleanType
1891 status;
1892
1893 size_t
1894 ignore_depth,
1895 length;
1896
1897 ssize_t
1898 i,
1899 j,
1900 l;
1901
1902 XMLTreeRoot
1903 *root;
1904
1905 /*
1906 Convert xml-string to UTF8.
1907 */
1908 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1909 {
1910 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1911 "ParseError","root tag missing");
1912 return((XMLTreeInfo *) NULL);
1913 }
1914 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1915 length=strlen(xml);
1916 utf8=ConvertUTF16ToUTF8(xml,&length);
1917 if (utf8 == (char *) NULL)
1918 {
1919 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1920 "ParseError","UTF16 to UTF8 failed");
1921 return((XMLTreeInfo *) NULL);
1922 }
1923 if (length == 0)
1924 {
1925 utf8=DestroyString(utf8);
1926 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1927 "ParseError","root tag missing");
1928 return((XMLTreeInfo *) NULL);
1929 }
1930 terminal=utf8[length-1];
1931 utf8[length-1]='\0';
1932 p=utf8;
1933 while ((*p != '\0') && (*p != '<'))
1934 p++;
1935 if (*p == '\0')
1936 {
1937 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1938 "ParseError","root tag missing");
1939 utf8=DestroyString(utf8);
1940 return((XMLTreeInfo *) NULL);
1941 }
1942 attribute=(char **) NULL;
1943 l=0;
1944 ignore_depth=0;
1945 for (p++; ; p++)
1946 {
1947 attributes=(char **) sentinel;
1948 tag=p;
1949 c=(*p);
1950 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1951 (*p == ':') || (c < '\0'))
1952 {
1953 /*
1954 Tag.
1955 */
1956 if (root->node == (XMLTreeInfo *) NULL)
1957 {
1958 (void) ThrowMagickException(exception,GetMagickModule(),
1959 OptionWarning,"ParseError","root tag missing");
1960 utf8=DestroyString(utf8);
1961 return(&root->root);
1962 }
1963 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1964 while (isspace((int) ((unsigned char) *p)) != 0)
1965 *p++='\0';
1966 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1967 (ignore_depth == 0))
1968 {
1969 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1970 {
1971 /*
1972 Find tag in default attributes list.
1973 */
1974 i=0;
1975 while ((root->attributes[i] != (char **) NULL) &&
1976 (strcmp(root->attributes[i][0],tag) != 0))
1977 i++;
1978 attribute=root->attributes[i];
1979 }
1980 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1981 {
1982 /*
1983 Attribute.
1984 */
1985 if (l == 0)
1986 attributes=(char **) AcquireQuantumMemory(4,
1987 sizeof(*attributes));
1988 else
1989 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1990 (l+4),sizeof(*attributes));
1991 if (attributes == (char **) NULL)
1992 {
1993 (void) ThrowMagickException(exception,GetMagickModule(),
1994 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1995 utf8=DestroyString(utf8);
1996 return(&root->root);
1997 }
1998 attributes[l+2]=(char *) NULL;
1999 attributes[l+1]=(char *) NULL;
2000 attributes[l]=p;
2001 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
2002 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
2003 attributes[l]=ConstantString("");
2004 else
2005 {
2006 *p++='\0';
2007 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
2008 c=(*p);
2009 if ((c == '"') || (c == '\''))
2010 {
2011 /*
2012 Attributes value.
2013 */
2014 p++;
2015 attributes[l+1]=p;
2016 while ((*p != '\0') && (*p != c))
2017 p++;
2018 if (*p != '\0')
2019 *p++='\0';
2020 else
2021 {
2022 attributes[l]=ConstantString("");
2023 attributes[l+1]=ConstantString("");
2024 (void) DestroyXMLTreeAttributes(attributes);
2025 (void) ThrowMagickException(exception,
2026 GetMagickModule(),OptionWarning,"ParseError",
2027 "missing %c",c);
2028 utf8=DestroyString(utf8);
2029 return(&root->root);
2030 }
2031 j=1;
2032 while ((attribute != (char **) NULL) &&
2033 (attribute[j] != (char *) NULL) &&
2034 (strcmp(attribute[j],attributes[l]) != 0))
2035 j+=3;
2036 attributes[l+1]=ParseEntities(attributes[l+1],
2037 root->entities,(attribute != (char **) NULL) &&
2038 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
2039 ' ');
2040 }
2041 attributes[l]=ConstantString(attributes[l]);
2042 }
2043 while (isspace((int) ((unsigned char) *p)) != 0)
2044 p++;
2045 }
2046 }
2047 else
2048 {
2049 while ((*p != '\0') && (*p != '/') && (*p != '>'))
2050 p++;
2051 }
2052 if (*p == '/')
2053 {
2054 /*
2055 Self closing tag.
2056 */
2057 *p++='\0';
2058 if (((*p != '\0') && (*p != '>')) ||
2059 ((*p == '\0') && (terminal != '>')))
2060 {
2061 if (l != 0)
2062 (void) DestroyXMLTreeAttributes(attributes);
2063 (void) ThrowMagickException(exception,GetMagickModule(),
2064 OptionWarning,"ParseError","missing >");
2065 utf8=DestroyString(utf8);
2066 return(&root->root);
2067 }
2068 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2069 (void) DestroyXMLTreeAttributes(attributes);
2070 else
2071 {
2072 ParseOpenTag(root,tag,attributes);
2073 (void) ParseCloseTag(root,tag,exception);
2074 }
2075 }
2076 else
2077 {
2078 c=(*p);
2079 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
2080 {
2081 *p='\0';
2082 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2083 ParseOpenTag(root,tag,attributes);
2084 else
2085 {
2086 ignore_depth++;
2087 (void) DestroyXMLTreeAttributes(attributes);
2088 }
2089 *p=(char) c;
2090 }
2091 else
2092 {
2093 if (l != 0)
2094 (void) DestroyXMLTreeAttributes(attributes);
2095 (void) ThrowMagickException(exception,GetMagickModule(),
2096 OptionWarning,"ParseError","missing >");
2097 utf8=DestroyString(utf8);
2098 return(&root->root);
2099 }
2100 }
2101 }
2102 else
2103 if (*p == '/')
2104 {
2105 /*
2106 Close tag.
2107 */
2108 tag=p+1;
2109 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
2110 c=(*p);
2111 if ((c == '\0') && (terminal != '>'))
2112 {
2113 (void) ThrowMagickException(exception,GetMagickModule(),
2114 OptionWarning,"ParseError","missing >");
2115 utf8=DestroyString(utf8);
2116 return(&root->root);
2117 }
2118 *p='\0';
2119 if ((ignore_depth == 0) &&
2120 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2121 {
2122 utf8=DestroyString(utf8);
2123 return(&root->root);
2124 }
2125 if (ignore_depth > 0)
2126 ignore_depth--;
2127 *p=(char) c;
2128 if (isspace((int) ((unsigned char) *p)) != 0)
2129 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2130 }
2131 else
2132 if (strncmp(p,"!--",3) == 0)
2133 {
2134 /*
2135 Comment.
2136 */
2137 p=strstr(p+3,"--");
2138 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
2139 ((*p == '\0') && (terminal != '>')))
2140 {
2141 (void) ThrowMagickException(exception,GetMagickModule(),
2142 OptionWarning,"ParseError","unclosed <!--");
2143 utf8=DestroyString(utf8);
2144 return(&root->root);
2145 }
2146 }
2147 else
2148 if (strncmp(p,"![CDATA[",8) == 0)
2149 {
2150 /*
2151 Cdata.
2152 */
2153 p=strstr(p,"]]>");
2154 if (p != (char *) NULL)
2155 {
2156 p+=(ptrdiff_t) 2;
2157 if (ignore_depth == 0)
2158 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
2159 }
2160 else
2161 {
2162 (void) ThrowMagickException(exception,GetMagickModule(),
2163 OptionWarning,"ParseError","unclosed <![CDATA[");
2164 utf8=DestroyString(utf8);
2165 return(&root->root);
2166 }
2167 }
2168 else
2169 if (strncmp(p,"!DOCTYPE",8) == 0)
2170 {
2171 /*
2172 DTD.
2173 */
2174 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
2175 ((l != 0) && ((*p != ']') ||
2176 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
2177 l=(ssize_t) ((*p == '[') ? 1 : l))
2178 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
2179 if ((*p == '\0') && (terminal != '>'))
2180 {
2181 (void) ThrowMagickException(exception,GetMagickModule(),
2182 OptionWarning,"ParseError","unclosed <!DOCTYPE");
2183 utf8=DestroyString(utf8);
2184 return(&root->root);
2185 }
2186 if (l != 0)
2187 tag=strchr(tag,'[')+1;
2188 if (l != 0)
2189 {
2190 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
2191 exception);
2192 if (status == MagickFalse)
2193 {
2194 utf8=DestroyString(utf8);
2195 return(&root->root);
2196 }
2197 p++;
2198 }
2199 }
2200 else
2201 if (*p == '?')
2202 {
2203 /*
2204 Processing instructions.
2205 */
2206 do
2207 {
2208 p=strchr(p,'?');
2209 if (p == (char *) NULL)
2210 break;
2211 p++;
2212 } while ((*p != '\0') && (*p != '>'));
2213 if ((p == (char *) NULL) || ((*p == '\0') &&
2214 (terminal != '>')))
2215 {
2216 (void) ThrowMagickException(exception,GetMagickModule(),
2217 OptionWarning,"ParseError","unclosed <?");
2218 utf8=DestroyString(utf8);
2219 return(&root->root);
2220 }
2221 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
2222 }
2223 else
2224 {
2225 (void) ThrowMagickException(exception,GetMagickModule(),
2226 OptionWarning,"ParseError","unexpected <");
2227 utf8=DestroyString(utf8);
2228 return(&root->root);
2229 }
2230 if ((p == (char *) NULL) || (*p == '\0'))
2231 break;
2232 *p++='\0';
2233 tag=p;
2234 if ((*p != '\0') && (*p != '<'))
2235 {
2236 /*
2237 Tag character content.
2238 */
2239 while ((*p != '\0') && (*p != '<'))
2240 p++;
2241 if (*p == '\0')
2242 break;
2243 if (ignore_depth == 0)
2244 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
2245 }
2246 else
2247 if (*p == '\0')
2248 break;
2249 }
2250 utf8=DestroyString(utf8);
2251 if (root->node == (XMLTreeInfo *) NULL)
2252 return(&root->root);
2253 if (root->node->tag == (char *) NULL)
2254 {
2255 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2256 "ParseError","root tag missing");
2257 return(&root->root);
2258 }
2259 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2260 "ParseError","unclosed tag: '%s'",root->node->tag);
2261 return(&root->root);
2262}
2263
2264/*
2265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2266% %
2267% %
2268% %
2269% N e w X M L T r e e T a g %
2270% %
2271% %
2272% %
2273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2274%
2275% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2276%
2277% The format of the NewXMLTreeTag method is:
2278%
2279% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2280%
2281% A description of each parameter follows:
2282%
2283% o tag: the tag.
2284%
2285*/
2286MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2287{
2288 static const char
2289 *predefined_entities[NumberPredefinedEntities+1] =
2290 {
2291 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2292 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2293 };
2294
2295 XMLTreeRoot
2296 *root;
2297
2298 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2299 (void) memset(root,0,sizeof(*root));
2300 root->root.tag=(char *) NULL;
2301 if (tag != (char *) NULL)
2302 root->root.tag=ConstantString(tag);
2303 root->node=(&root->root);
2304 root->root.content=ConstantString("");
2305 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2306 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2307 root->root.attributes=sentinel;
2308 root->attributes=(char ***) root->root.attributes;
2309 root->processing_instructions=(char ***) root->root.attributes;
2310 root->debug=IsEventLogging();
2311 root->signature=MagickCoreSignature;
2312 return(&root->root);
2313}
2314
2315/*
2316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2317% %
2318% %
2319% %
2320% P r u n e T a g F r o m X M L T r e e %
2321% %
2322% %
2323% %
2324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2325%
2326% PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
2327% subtags.
2328%
2329% The format of the PruneTagFromXMLTree method is:
2330%
2331% XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2332%
2333% A description of each parameter follows:
2334%
2335% o xml_info: the xml info.
2336%
2337*/
2338MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2339{
2340 XMLTreeInfo
2341 *node;
2342
2343 assert(xml_info != (XMLTreeInfo *) NULL);
2344 assert((xml_info->signature == MagickCoreSignature) ||
2345 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2346 if (IsEventLogging() != MagickFalse)
2347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2348 if (xml_info->next != (XMLTreeInfo *) NULL)
2349 xml_info->next->sibling=xml_info->sibling;
2350 if (xml_info->parent != (XMLTreeInfo *) NULL)
2351 {
2352 node=xml_info->parent->child;
2353 if (node == xml_info)
2354 xml_info->parent->child=xml_info->ordered;
2355 else
2356 {
2357 while (node->ordered != xml_info)
2358 node=node->ordered;
2359 node->ordered=node->ordered->ordered;
2360 node=xml_info->parent->child;
2361 if (strcmp(node->tag,xml_info->tag) != 0)
2362 {
2363 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2364 node=node->sibling;
2365 if (node->sibling != xml_info)
2366 node=node->sibling;
2367 else
2368 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2369 xml_info->next : node->sibling->sibling;
2370 }
2371 while ((node->next != (XMLTreeInfo *) NULL) &&
2372 (node->next != xml_info))
2373 node=node->next;
2374 if (node->next != (XMLTreeInfo *) NULL)
2375 node->next=node->next->next;
2376 }
2377 }
2378 xml_info->ordered=(XMLTreeInfo *) NULL;
2379 xml_info->sibling=(XMLTreeInfo *) NULL;
2380 xml_info->next=(XMLTreeInfo *) NULL;
2381 return(xml_info);
2382}
2383
2384/*
2385%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2386% %
2387% %
2388% %
2389% S e t X M L T r e e A t t r i b u t e %
2390% %
2391% %
2392% %
2393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2394%
2395% SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
2396% found. A value of NULL removes the specified attribute.
2397%
2398% The format of the SetXMLTreeAttribute method is:
2399%
2400% XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
2401% const char *value)
2402%
2403% A description of each parameter follows:
2404%
2405% o xml_info: the xml info.
2406%
2407% o tag: The attribute tag.
2408%
2409% o value: The attribute value.
2410%
2411*/
2412MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2413 const char *tag,const char *value)
2414{
2415 ssize_t
2416 i,
2417 j;
2418
2419 assert(xml_info != (XMLTreeInfo *) NULL);
2420 assert((xml_info->signature == MagickCoreSignature) ||
2421 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2422 if (IsEventLogging() != MagickFalse)
2423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2424 i=0;
2425 while ((xml_info->attributes[i] != (char *) NULL) &&
2426 (strcmp(xml_info->attributes[i],tag) != 0))
2427 i+=2;
2428 if (xml_info->attributes[i] == (char *) NULL)
2429 {
2430 /*
2431 Add new attribute tag.
2432 */
2433 if (value == (const char *) NULL)
2434 return(xml_info);
2435 if (xml_info->attributes != sentinel)
2436 xml_info->attributes=(char **) ResizeQuantumMemory(
2437 xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
2438 else
2439 {
2440 xml_info->attributes=(char **) AcquireQuantumMemory(4,
2441 sizeof(*xml_info->attributes));
2442 if (xml_info->attributes != (char **) NULL)
2443 xml_info->attributes[1]=ConstantString("");
2444 }
2445 if (xml_info->attributes == (char **) NULL)
2446 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2447 xml_info->attributes[i]=ConstantString(tag);
2448 xml_info->attributes[i+2]=(char *) NULL;
2449 (void) strlen(xml_info->attributes[i+1]);
2450 }
2451 /*
2452 Add new value to an existing attribute.
2453 */
2454 for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
2455 if (xml_info->attributes[i+1] != (char *) NULL)
2456 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2457 if (value != (const char *) NULL)
2458 {
2459 xml_info->attributes[i+1]=ConstantString(value);
2460 return(xml_info);
2461 }
2462 if (xml_info->attributes[i] != (char *) NULL)
2463 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2464 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(size_t)
2465 (j-i)*sizeof(*xml_info->attributes));
2466 xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
2467 (size_t) (j+2),sizeof(*xml_info->attributes));
2468 if (xml_info->attributes == (char **) NULL)
2469 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2470 j-=2;
2471 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2472 (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
2473 return(xml_info);
2474}
2475
2476/*
2477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2478% %
2479% %
2480% %
2481% S e t X M L T r e e C o n t e n t %
2482% %
2483% %
2484% %
2485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2486%
2487% SetXMLTreeContent() sets the character content for the given tag and
2488% returns the tag.
2489%
2490% The format of the SetXMLTreeContent method is:
2491%
2492% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2493% const char *content)
2494%
2495% A description of each parameter follows:
2496%
2497% o xml_info: the xml info.
2498%
2499% o content: The content.
2500%
2501*/
2502MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2503 const char *content)
2504{
2505 assert(xml_info != (XMLTreeInfo *) NULL);
2506 assert((xml_info->signature == MagickCoreSignature) ||
2507 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2508 if (IsEventLogging() != MagickFalse)
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2510 if (xml_info->content != (char *) NULL)
2511 xml_info->content=DestroyString(xml_info->content);
2512 xml_info->content=(char *) ConstantString(content);
2513 return(xml_info);
2514}
2515
2516/*
2517%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2518% %
2519% %
2520% %
2521% X M L T r e e I n f o T o X M L %
2522% %
2523% %
2524% %
2525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2526%
2527% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2528%
2529% The format of the XMLTreeInfoToXML method is:
2530%
2531% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2532%
2533% A description of each parameter follows:
2534%
2535% o xml_info: the xml info.
2536%
2537*/
2538
2539static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2540 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2541{
2542 char
2543 *canonical_content;
2544
2545 if (offset < 0)
2546 canonical_content=CanonicalXMLContent(source,pedantic);
2547 else
2548 {
2549 char
2550 *content;
2551
2552 content=AcquireString(source);
2553 content[offset]='\0';
2554 canonical_content=CanonicalXMLContent(content,pedantic);
2555 content=DestroyString(content);
2556 }
2557 if (canonical_content == (char *) NULL)
2558 return(*destination);
2559 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2560 {
2561 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2562 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2563 sizeof(**destination));
2564 if (*destination == (char *) NULL)
2565 return(*destination);
2566 }
2567 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2568 canonical_content);
2569 canonical_content=DestroyString(canonical_content);
2570 return(*destination);
2571}
2572
2573static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2574 size_t *extent,size_t start,char ***attributes)
2575{
2576 char
2577 *content;
2578
2579 const char
2580 *attribute;
2581
2582 size_t
2583 offset;
2584
2585 ssize_t
2586 i,
2587 j;
2588
2589 content=(char *) "";
2590 if (xml_info->parent != (XMLTreeInfo *) NULL)
2591 content=xml_info->parent->content;
2592 offset=0;
2593 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2594 start),source,length,extent,MagickFalse);
2595 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2596 {
2597 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2598 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2599 if (*source == (char *) NULL)
2600 return(*source);
2601 }
2602 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2603 "<%s",xml_info->tag);
2604 for (i=0; xml_info->attributes[i]; i+=2)
2605 {
2606 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2607 if (attribute != xml_info->attributes[i+1])
2608 continue;
2609 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2610 {
2611 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2612 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2613 if (*source == (char *) NULL)
2614 return((char *) NULL);
2615 }
2616 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2617 xml_info->attributes[i]);
2618 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2619 extent,MagickTrue);
2620 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2621 }
2622 i=0;
2623 while ((attributes[i] != (char **) NULL) &&
2624 (strcmp(attributes[i][0],xml_info->tag) != 0))
2625 i++;
2626 j=1;
2627 while ((attributes[i] != (char **) NULL) &&
2628 (attributes[i][j] != (char *) NULL))
2629 {
2630 if ((attributes[i][j+1] == (char *) NULL) ||
2631 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2632 {
2633 j+=3;
2634 continue;
2635 }
2636 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2637 {
2638 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2639 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2640 if (*source == (char *) NULL)
2641 return((char *) NULL);
2642 }
2643 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2644 attributes[i][j]);
2645 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2646 MagickTrue);
2647 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2648 j+=3;
2649 }
2650 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2651 *xml_info->content ? ">" : "/>");
2652 if (xml_info->child != (XMLTreeInfo *) NULL)
2653 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2654 else
2655 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2656 MagickFalse);
2657 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2658 {
2659 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2660 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2661 if (*source == (char *) NULL)
2662 return((char *) NULL);
2663 }
2664 if (*xml_info->content != '\0')
2665 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2666 xml_info->tag);
2667 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2668 offset++;
2669 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2670 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2671 attributes);
2672 else
2673 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2674 MagickFalse);
2675 return(content);
2676}
2677
2678MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2679{
2680 char
2681 *p,
2682 *q,
2683 *xml;
2684
2685 size_t
2686 extent,
2687 length;
2688
2689 ssize_t
2690 i,
2691 j,
2692 k;
2693
2694 XMLTreeInfo
2695 *ordered,
2696 *parent;
2697
2698 XMLTreeRoot
2699 *root;
2700
2701 assert(xml_info != (XMLTreeInfo *) NULL);
2702 assert((xml_info->signature == MagickCoreSignature) ||
2703 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2704 if (IsEventLogging() != MagickFalse)
2705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2706 if (xml_info->tag == (char *) NULL)
2707 return((char *) NULL);
2708 xml=AcquireString((char *) NULL);
2709 length=0;
2710 extent=MagickPathExtent;
2711 root=(XMLTreeRoot *) xml_info;
2712 while (root->root.parent != (XMLTreeInfo *) NULL)
2713 root=(XMLTreeRoot *) root->root.parent;
2714 parent=xml_info->parent;
2715 if (parent == (XMLTreeInfo *) NULL)
2716 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2717 {
2718 /*
2719 Pre-root processing instructions.
2720 */
2721 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2722 p=root->processing_instructions[i][1];
2723 for (j=1; p != (char *) NULL; j++)
2724 {
2725 if (root->processing_instructions[i][k][j-1] == '>')
2726 {
2727 p=root->processing_instructions[i][j];
2728 continue;
2729 }
2730 q=root->processing_instructions[i][0];
2731 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2732 {
2733 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2734 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2735 if (xml == (char *) NULL)
2736 return(xml);
2737 }
2738 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2739 *p != '\0' ? " " : "",p);
2740 p=root->processing_instructions[i][j];
2741 }
2742 }
2743 ordered=xml_info->ordered;
2744 xml_info->parent=(XMLTreeInfo *) NULL;
2745 xml_info->ordered=(XMLTreeInfo *) NULL;
2746 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2747 xml_info->parent=parent;
2748 xml_info->ordered=ordered;
2749 if (parent == (XMLTreeInfo *) NULL)
2750 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2751 {
2752 /*
2753 Post-root processing instructions.
2754 */
2755 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2756 p=root->processing_instructions[i][1];
2757 for (j=1; p != (char *) NULL; j++)
2758 {
2759 if (root->processing_instructions[i][k][j-1] == '<')
2760 {
2761 p=root->processing_instructions[i][j];
2762 continue;
2763 }
2764 q=root->processing_instructions[i][0];
2765 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2766 {
2767 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2768 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2769 if (xml == (char *) NULL)
2770 return(xml);
2771 }
2772 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2773 *p != '\0' ? " " : "",p);
2774 p=root->processing_instructions[i][j];
2775 }
2776 }
2777 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2778}