MagickCore 7.1.2-31
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*/
167
168static XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
169 XMLTreeInfo *child,const size_t offset)
170{
171 XMLTreeInfo
172 *head,
173 *node,
174 *previous;
175
176 child->ordered=(XMLTreeInfo *) NULL;
177 child->sibling=(XMLTreeInfo *) NULL;
178 child->next=(XMLTreeInfo *) NULL;
179 child->offset=offset;
180 child->parent=xml_info;
181 if (xml_info->child == (XMLTreeInfo *) NULL)
182 {
183 xml_info->child=child;
184 return(child);
185 }
186 head=xml_info->child;
187 if (head->offset > offset)
188 {
189 child->ordered=head;
190 xml_info->child=child;
191 }
192 else
193 {
194 node=head;
195 while ((node->ordered != (XMLTreeInfo *) NULL) &&
196 (node->ordered->offset <= offset))
197 node=node->ordered;
198 child->ordered=node->ordered;
199 node->ordered=child;
200 }
201 previous=(XMLTreeInfo *) NULL;
202 node=head;
203 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
204 {
205 previous=node;
206 node=node->sibling;
207 }
208 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
209 {
210 while ((node->next != (XMLTreeInfo *) NULL) &&
211 (node->next->offset <= offset))
212 node=node->next;
213 child->next=node->next;
214 node->next=child;
215 }
216 else
217 {
218 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
219 previous->sibling=node->sibling;
220 child->next=node;
221 previous=(XMLTreeInfo *) NULL;
222 node=head;
223 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
224 {
225 previous=node;
226 node=node->sibling;
227 }
228 child->sibling=node;
229 if (previous != (XMLTreeInfo *) NULL)
230 previous->sibling=child;
231 }
232 return(child);
233}
234
235MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
236 const char *tag,const size_t offset)
237{
238 XMLTreeInfo
239 *child;
240
241 if (xml_info == (XMLTreeInfo *) NULL)
242 return((XMLTreeInfo *) NULL);
243 child=(XMLTreeInfo *) AcquireMagickMemory(sizeof(*child));
244 if (child == (XMLTreeInfo *) NULL)
245 return((XMLTreeInfo *) NULL);
246 (void) memset(child,0,sizeof(*child));
247 child->tag=ConstantString(tag);
248 child->attributes=sentinel;
249 child->content=ConstantString("");
250 child->debug=IsEventLogging();
251 child->signature=MagickCoreSignature;
252 return(InsertTagIntoXMLTree(xml_info,child,offset));
253}
254
255/*
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257% %
258% %
259% %
260% A d d P a t h T o X M L T r e e %
261% %
262% %
263% %
264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265%
266% AddPathToXMLTree() adds a child tag at an offset relative to the start of
267% the parent tag's character content. This method returns the child tag.
268%
269% The format of the AddPathToXMLTree method is:
270%
271% XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
272% const size_t offset)
273%
274% A description of each parameter follows:
275%
276% o xml_info: the xml info.
277%
278% o path: the path.
279%
280% o offset: the tag offset.
281%
282*/
283
284static char *CanonicalXMLContent(const char *content,
285 const MagickBooleanType pedantic)
286{
287 char
288 *base64,
289 *canonical_content;
290
291 const unsigned char
292 *p;
293
294 size_t
295 length;
296
297 unsigned char
298 *utf8;
299
300 utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
301 if (utf8 == (unsigned char *) NULL)
302 return((char *) NULL);
303 for (p=utf8; *p != '\0'; p++)
304 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
305 break;
306 if (*p != '\0')
307 {
308 /*
309 String is binary, base64-encode it.
310 */
311 base64=Base64Encode(utf8,strlen((char *) utf8),&length);
312 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
313 if (base64 == (char *) NULL)
314 return((char *) NULL);
315 canonical_content=AcquireString("<base64>");
316 (void) ConcatenateString(&canonical_content,base64);
317 base64=DestroyString(base64);
318 (void) ConcatenateString(&canonical_content,"</base64>");
319 return(canonical_content);
320 }
321 canonical_content=SubstituteXMLEntities((const char *) utf8,pedantic);
322 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
323 return(canonical_content);
324}
325
326MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
327 const char *path,const size_t offset)
328{
329 char
330 **components,
331 subnode[MagickPathExtent],
332 tag[MagickPathExtent];
333
334 size_t
335 number_components;
336
337 ssize_t
338 i,
339 j;
340
341 XMLTreeInfo
342 *child,
343 *node;
344
345 assert(xml_info != (XMLTreeInfo *) NULL);
346 assert((xml_info->signature == MagickCoreSignature) ||
347 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
348 if (IsEventLogging() != MagickFalse)
349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
350 node=xml_info;
351 components=GetPathComponents(path,&number_components);
352 if (components == (char **) NULL)
353 return((XMLTreeInfo *) NULL);
354 for (i=0; i < (ssize_t) number_components; i++)
355 {
356 GetPathComponent(components[i],SubimagePath,subnode);
357 GetPathComponent(components[i],CanonicalPath,tag);
358 child=GetXMLTreeChild(node,tag);
359 if (child == (XMLTreeInfo *) NULL)
360 child=AddChildToXMLTree(node,tag,offset);
361 node=child;
362 if (node == (XMLTreeInfo *) NULL)
363 break;
364 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
365 {
366 node=node->ordered;
367 if (node == (XMLTreeInfo *) NULL)
368 break;
369 }
370 if (node == (XMLTreeInfo *) NULL)
371 break;
372 components[i]=DestroyString(components[i]);
373 }
374 for ( ; i < (ssize_t) number_components; i++)
375 components[i]=DestroyString(components[i]);
376 components=(char **) RelinquishMagickMemory(components);
377 return(node);
378}
379
380/*
381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382% %
383% %
384% %
385% D e s t r o y X M L T r e e %
386% %
387% %
388% %
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390%
391% DestroyXMLTree() destroys the xml-tree.
392%
393% The format of the DestroyXMLTree method is:
394%
395% XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
396%
397% A description of each parameter follows:
398%
399% o xml_info: the xml info.
400%
401*/
402
403static XMLTreeInfo
404 *DestroyXMLTree_(XMLTreeInfo *,const size_t);
405
406static char **DestroyXMLTreeAttributes(char **attributes)
407{
408 ssize_t
409 i;
410
411 /*
412 Destroy a tag attribute list.
413 */
414 if ((attributes == (char **) NULL) || (attributes == sentinel))
415 return((char **) NULL);
416 for (i=0; attributes[i] != (char *) NULL; i+=2)
417 {
418 /*
419 Destroy attribute tag and value.
420 */
421 if (attributes[i] != (char *) NULL)
422 attributes[i]=DestroyString(attributes[i]);
423 if (attributes[i+1] != (char *) NULL)
424 attributes[i+1]=DestroyString(attributes[i+1]);
425 }
426 attributes=(char **) RelinquishMagickMemory(attributes);
427 return((char **) NULL);
428}
429
430static void DestroyXMLTreeChild(XMLTreeInfo *xml_info,
431 const size_t depth)
432{
433 XMLTreeInfo
434 *child,
435 *node;
436
437 child=xml_info->child;
438 while (child != (XMLTreeInfo *) NULL)
439 {
440 node=child;
441 child=node->child;
442 node->child=(XMLTreeInfo *) NULL;
443 (void) DestroyXMLTree_(node,depth+1);
444 }
445}
446
447static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info,
448 const size_t depth)
449{
450 XMLTreeInfo
451 *node,
452 *ordered;
453
454 ordered=xml_info->ordered;
455 while (ordered != (XMLTreeInfo *) NULL)
456 {
457 node=ordered;
458 ordered=node->ordered;
459 node->ordered=(XMLTreeInfo *) NULL;
460 (void) DestroyXMLTree_(node,depth+1);
461 }
462}
463
464static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
465{
466 char
467 **attributes;
468
469 ssize_t
470 i,
471 j;
472
473 XMLTreeRoot
474 *root;
475
476 assert(xml_info != (XMLTreeInfo *) NULL);
477 assert((xml_info->signature == MagickCoreSignature) ||
478 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
479 if (IsEventLogging() != MagickFalse)
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
481 if (xml_info->parent != (XMLTreeInfo *) NULL)
482 return;
483 /*
484 Free root tag allocations.
485 */
486 root=(XMLTreeRoot *) xml_info;
487 for (i=NumberPredefinedEntities; root->entities[i] != (char *) NULL; i+=2)
488 root->entities[i+1]=DestroyString(root->entities[i+1]);
489 root->entities=(char **) RelinquishMagickMemory(root->entities);
490 for (i=0; root->attributes[i] != (char **) NULL; i++)
491 {
492 attributes=root->attributes[i];
493 if (attributes[0] != (char *) NULL)
494 attributes[0]=DestroyString(attributes[0]);
495 for (j=1; attributes[j] != (char *) NULL; j+=3)
496 {
497 if (attributes[j] != (char *) NULL)
498 attributes[j]=DestroyString(attributes[j]);
499 if (attributes[j+1] != (char *) NULL)
500 attributes[j+1]=DestroyString(attributes[j+1]);
501 if (attributes[j+2] != (char *) NULL)
502 attributes[j+2]=DestroyString(attributes[j+2]);
503 }
504 attributes=(char **) RelinquishMagickMemory(attributes);
505 }
506 if (root->attributes[0] != (char **) NULL)
507 root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
508 if (root->processing_instructions[0] != (char **) NULL)
509 {
510 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
511 {
512 for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
513 root->processing_instructions[i][j]=DestroyString(
514 root->processing_instructions[i][j]);
515 root->processing_instructions[i][j+1]=DestroyString(
516 root->processing_instructions[i][j+1]);
517 root->processing_instructions[i]=(char **) RelinquishMagickMemory(
518 root->processing_instructions[i]);
519 }
520 root->processing_instructions=(char ***) RelinquishMagickMemory(
521 root->processing_instructions);
522 }
523}
524
525static XMLTreeInfo *DestroyXMLTree_(XMLTreeInfo *xml_info,
526 const size_t depth)
527{
528 assert(xml_info != (XMLTreeInfo *) NULL);
529 assert((xml_info->signature == MagickCoreSignature) ||
530 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
531 if (IsEventLogging() != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
533 if (depth > MagickMaxRecursionDepth)
534 return((XMLTreeInfo *) NULL);
535 DestroyXMLTreeChild(xml_info,depth+1);
536 DestroyXMLTreeOrdered(xml_info,depth+1);
537 DestroyXMLTreeRoot(xml_info);
538 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
539 xml_info->content=DestroyString(xml_info->content);
540 xml_info->tag=DestroyString(xml_info->tag);
541 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
542 return((XMLTreeInfo *) NULL);
543}
544
545MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
546{
547 return(DestroyXMLTree_(xml_info,0));
548}
549
550/*
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552% %
553% %
554% %
555% F i l e T o X M L %
556% %
557% %
558% %
559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560%
561% FileToXML() returns the contents of a file as a XML string.
562%
563% The format of the FileToXML method is:
564%
565% char *FileToXML(const char *filename,const size_t extent)
566%
567% A description of each parameter follows:
568%
569% o filename: the filename.
570%
571% o extent: Maximum length of the string.
572%
573*/
574MagickPrivate char *FileToXML(const char *filename,const size_t extent)
575{
576 char
577 *xml;
578
579 int
580 file;
581
582 MagickOffsetType
583 offset;
584
585 size_t
586 i,
587 length;
588
589 ssize_t
590 count;
591
592 void
593 *map;
594
595 assert(filename != (const char *) NULL);
596 length=0;
597 file=fileno(stdin);
598 if (LocaleCompare(filename,"-") != 0)
599 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
600 if (file == -1)
601 return((char *) NULL);
602 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
603 count=0;
604 if ((file == fileno(stdin)) || (offset < 0) ||
605 (offset != (MagickOffsetType) ((ssize_t) offset)))
606 {
607 size_t
608 quantum;
609
610 struct stat
611 file_stats;
612
613 /*
614 Stream is not seekable.
615 */
616 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
617 quantum=(size_t) MagickMaxBufferExtent;
618 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
619 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
620 xml=(char *) AcquireQuantumMemory(quantum,sizeof(*xml));
621 for (i=0; xml != (char *) NULL; i+=(size_t) count)
622 {
623 count=MagickRead(file,xml+i,quantum);
624 if (count <= 0)
625 break;
626 if (~((size_t) i) < (quantum+1))
627 {
628 xml=(char *) RelinquishMagickMemory(xml);
629 break;
630 }
631 xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
632 if ((i+(size_t) count) >= extent)
633 break;
634 }
635 if (LocaleCompare(filename,"-") != 0)
636 file=close_utf8(file);
637 if (xml == (char *) NULL)
638 return((char *) NULL);
639 if (file == -1)
640 {
641 xml=(char *) RelinquishMagickMemory(xml);
642 return((char *) NULL);
643 }
644 length=MagickMin(i+(size_t) count,extent);
645 xml[length]='\0';
646 return(xml);
647 }
648 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
649 xml=(char *) NULL;
650 if (~length >= (MagickPathExtent-1))
651 xml=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*xml));
652 if (xml == (char *) NULL)
653 {
654 file=close_utf8(file);
655 return((char *) NULL);
656 }
657 map=MapBlob(file,ReadMode,0,length);
658 if (map != (char *) NULL)
659 {
660 (void) memcpy(xml,map,length);
661 (void) UnmapBlob(map,length);
662 }
663 else
664 {
665 (void) lseek(file,0,SEEK_SET);
666 for (i=0; i < length; i+=(size_t) count)
667 {
668 count=MagickRead(file,xml+i,(size_t) MagickMin(length-i,(size_t)
669 MagickMaxBufferExtent));
670 if (count <= 0)
671 break;
672 }
673 if (i < length)
674 {
675 file=close_utf8(file)-1;
676 xml=(char *) RelinquishMagickMemory(xml);
677 return((char *) NULL);
678 }
679 }
680 xml[length]='\0';
681 if (LocaleCompare(filename,"-") != 0)
682 file=close_utf8(file);
683 if (file == -1)
684 xml=(char *) RelinquishMagickMemory(xml);
685 return(xml);
686}
687
688/*
689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690% %
691% %
692% %
693% G e t N e x t X M L T r e e T a g %
694% %
695% %
696% %
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698%
699% GetNextXMLTreeTag() returns the next tag or NULL if not found.
700%
701% The format of the GetNextXMLTreeTag method is:
702%
703% XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
704%
705% A description of each parameter follows:
706%
707% o xml_info: the xml info.
708%
709*/
710MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
711{
712 assert(xml_info != (XMLTreeInfo *) NULL);
713 assert((xml_info->signature == MagickCoreSignature) ||
714 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
715 if (IsEventLogging() != MagickFalse)
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
717 return(xml_info->next);
718}
719
720/*
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722% %
723% %
724% %
725% G e t X M L T r e e A t t r i b u t e %
726% %
727% %
728% %
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730%
731% GetXMLTreeAttribute() returns the value of the attribute tag with the
732% specified tag if found, otherwise NULL.
733%
734% The format of the GetXMLTreeAttribute method is:
735%
736% const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
737%
738% A description of each parameter follows:
739%
740% o xml_info: the xml info.
741%
742% o tag: the attribute tag.
743%
744*/
745MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
746 const char *tag)
747{
748 ssize_t
749 i,
750 j;
751
752 XMLTreeRoot
753 *root;
754
755 assert(xml_info != (XMLTreeInfo *) NULL);
756 assert((xml_info->signature == MagickCoreSignature) ||
757 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
758 if (IsEventLogging() != MagickFalse)
759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
760 if (xml_info->attributes == (char **) NULL)
761 return((const char *) NULL);
762 i=0;
763 while ((xml_info->attributes[i] != (char *) NULL) &&
764 (strcmp(xml_info->attributes[i],tag) != 0))
765 i+=2;
766 if (xml_info->attributes[i] != (char *) NULL)
767 return(xml_info->attributes[i+1]);
768 root=(XMLTreeRoot*) xml_info;
769 while (root->root.parent != (XMLTreeInfo *) NULL)
770 root=(XMLTreeRoot *) root->root.parent;
771 i=0;
772 while ((root->attributes[i] != (char **) NULL) &&
773 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
774 i++;
775 if (root->attributes[i] == (char **) NULL)
776 return((const char *) NULL);
777 j=1;
778 while ((root->attributes[i][j] != (char *) NULL) &&
779 (strcmp(root->attributes[i][j],tag) != 0))
780 j+=3;
781 if (root->attributes[i][j] == (char *) NULL)
782 return((const char *) NULL);
783 return(root->attributes[i][j+1]);
784}
785
786/*
787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788% %
789% %
790% %
791% G e t X M L T r e e C h i l d %
792% %
793% %
794% %
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796%
797% GetXMLTreeChild() returns the first child tag with the specified tag if
798% found, otherwise NULL.
799%
800% The format of the GetXMLTreeChild method is:
801%
802% XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
803%
804% A description of each parameter follows:
805%
806% o xml_info: the xml info.
807%
808*/
809MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
810{
811 XMLTreeInfo
812 *child;
813
814 assert(xml_info != (XMLTreeInfo *) NULL);
815 assert((xml_info->signature == MagickCoreSignature) ||
816 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
817 if (IsEventLogging() != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
819 child=xml_info->child;
820 if (tag != (const char *) NULL)
821 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
822 child=child->sibling;
823 return(child);
824}
825
826/*
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828% %
829% %
830% %
831% G e t X M L T r e e C o n t e n t %
832% %
833% %
834% %
835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836%
837% GetXMLTreeContent() returns any content associated with specified
838% xml-tree node.
839%
840% The format of the GetXMLTreeContent method is:
841%
842% const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
843%
844% A description of each parameter follows:
845%
846% o xml_info: the xml info.
847%
848*/
849MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
850{
851 assert(xml_info != (XMLTreeInfo *) NULL);
852 assert((xml_info->signature == MagickCoreSignature) ||
853 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
854 if (IsEventLogging() != MagickFalse)
855 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
856 return(xml_info->content);
857}
858
859/*
860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861% %
862% %
863% %
864% G e t X M L T r e e S i b l i n g %
865% %
866% %
867% %
868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869%
870% GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
871%
872% The format of the GetXMLTreeSibling method is:
873%
874% XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
875%
876% A description of each parameter follows:
877%
878% o xml_info: the xml info.
879%
880*/
881MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
882{
883 assert(xml_info != (XMLTreeInfo *) NULL);
884 assert((xml_info->signature == MagickCoreSignature) ||
885 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
886 if (IsEventLogging() != MagickFalse)
887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
888 return(xml_info->sibling);
889}
890
891/*
892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893% %
894% %
895% %
896% G e t X M L T r e e T a g %
897% %
898% %
899% %
900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901%
902% GetXMLTreeTag() returns the tag associated with specified xml-tree node.
903%
904% The format of the GetXMLTreeTag method is:
905%
906% const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
907%
908% A description of each parameter follows:
909%
910% o xml_info: the xml info.
911%
912*/
913MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
914{
915 assert(xml_info != (XMLTreeInfo *) NULL);
916 assert((xml_info->signature == MagickCoreSignature) ||
917 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
918 if (IsEventLogging() != MagickFalse)
919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
920 return(xml_info->tag);
921}
922
923/*
924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925% %
926% %
927% %
928% N e w X M L T r e e %
929% %
930% %
931% %
932%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933%
934% NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
935% XML string.
936%
937% The format of the NewXMLTree method is:
938%
939% XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
940%
941% A description of each parameter follows:
942%
943% o xml: A null-terminated XML string.
944%
945% o exception: return any errors or warnings in this structure.
946%
947*/
948
949static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
950{
951 char
952 *utf8;
953
954 int
955 bits,
956 byte,
957 c,
958 encoding;
959
960 size_t
961 extent;
962
963 ssize_t
964 i,
965 j;
966
967 utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
968 if (utf8 == (char *) NULL)
969 return((char *) NULL);
970 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
971 if (encoding == -1)
972 {
973 /*
974 Already UTF-8.
975 */
976 (void) memcpy(utf8,content,*length*sizeof(*utf8));
977 utf8[*length]='\0';
978 return(utf8);
979 }
980 j=0;
981 extent=(*length);
982 for (i=2; i < (ssize_t) (*length-1); i+=2)
983 {
984 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
985 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
986 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
987 {
988 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
989 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
990 (content[i] & 0xff);
991 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
992 }
993 if ((size_t) (j+MagickPathExtent) > extent)
994 {
995 extent=(size_t) j+MagickPathExtent;
996 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
997 if (utf8 == (char *) NULL)
998 return(utf8);
999 }
1000 if (c < 0x80)
1001 {
1002 utf8[j]=(char) c;
1003 j++;
1004 continue;
1005 }
1006 /*
1007 Multi-byte UTF-8 sequence.
1008 */
1009 byte=c;
1010 for (bits=0; byte != 0; byte/=2)
1011 bits++;
1012 bits=(bits-2)/5;
1013 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1014 while (bits != 0)
1015 {
1016 bits--;
1017 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1018 j++;
1019 }
1020 }
1021 *length=(size_t) j;
1022 utf8=(char *) ResizeQuantumMemory(utf8,(*length+1),sizeof(*utf8));
1023 if (utf8 != (char *) NULL)
1024 utf8[*length]='\0';
1025 return(utf8);
1026}
1027
1028static char *ParseEntities(char *xml,char **entities,int state)
1029{
1030 char
1031 *entity,
1032 *p,
1033 *q;
1034
1035 int
1036 byte,
1037 c;
1038
1039 size_t
1040 extent,
1041 length;
1042
1043 ssize_t
1044 i,
1045 offset;
1046
1047 /*
1048 Normalize line endings.
1049 */
1050 p=xml;
1051 q=xml;
1052 for ( ; *xml != '\0'; xml++)
1053 while (*xml == '\r')
1054 {
1055 *(xml++)='\n';
1056 if (*xml == '\n')
1057 (void) memmove(xml,xml+1,strlen(xml));
1058 }
1059 for (xml=p; ; )
1060 {
1061 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1062 (state != '%')) && (isspace((int) ((unsigned char) *xml)) == 0))
1063 xml++;
1064 if (*xml == '\0')
1065 break;
1066 /*
1067 States include:
1068 '&' for general entity decoding
1069 '%' for parameter entity decoding
1070 'c' for CDATA sections
1071 ' ' for attributes normalization
1072 '*' for non-CDATA attributes normalization
1073 */
1074 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1075 {
1076 /*
1077 Character reference.
1078 */
1079 if (xml[2] != 'x')
1080 c=strtol(xml+2,&entity,10); /* base 10 */
1081 else
1082 c=strtol(xml+3,&entity,16); /* base 16 */
1083 if ((c == 0) || (*entity != ';'))
1084 {
1085 /*
1086 Not a character reference.
1087 */
1088 xml++;
1089 continue;
1090 }
1091 if (c < 0x80)
1092 *(xml++)=(char) c;
1093 else
1094 {
1095 /*
1096 Multi-byte UTF-8 sequence.
1097 */
1098 byte=c;
1099 for (i=0; byte != 0; byte/=2)
1100 i++;
1101 i=(i-2)/5;
1102 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1103 xml++;
1104 while (i != 0)
1105 {
1106 i--;
1107 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1108 xml++;
1109 }
1110 }
1111 (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1112 }
1113 else
1114 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1115 (state == '*'))) || ((state == '%') && (*xml == '%')))
1116 {
1117 /*
1118 Find entity in the list.
1119 */
1120 i=0;
1121 while ((entities[i] != (char *) NULL) &&
1122 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1123 i+=2;
1124 if (entities[i++] == (char *) NULL)
1125 xml++;
1126 else
1127 if (entities[i] != (char *) NULL)
1128 {
1129 /*
1130 Found a match.
1131 */
1132 length=strlen(entities[i]);
1133 entity=strchr(xml,';');
1134 if ((entity != (char *) NULL) &&
1135 ((length-1L) >= (size_t) (entity-xml)))
1136 {
1137 offset=(ssize_t) (xml-p);
1138 extent=((size_t) offset+length+strlen(entity));
1139 if (p != q)
1140 {
1141 p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1142 if (p != (char *) NULL)
1143 p[extent]='\0';
1144 }
1145 else
1146 {
1147 char
1148 *extent_xml;
1149
1150 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1151 sizeof(*extent_xml));
1152 if (extent_xml != (char *) NULL)
1153 {
1154 memset(extent_xml,0,extent*sizeof(*extent_xml));
1155 (void) CopyMagickString(extent_xml,p,extent*
1156 sizeof(*extent_xml));
1157 }
1158 p=extent_xml;
1159 }
1160 if (p == (char *) NULL)
1161 ThrowFatalException(ResourceLimitFatalError,
1162 "MemoryAllocationFailed");
1163 xml=p+offset;
1164 entity=strchr(xml,';');
1165 }
1166 if (entity != (char *) NULL)
1167 (void) memmove(xml+length,entity+1,strlen(entity));
1168 (void) memcpy(xml,entities[i],length);
1169 }
1170 }
1171 else
1172 if (((state == ' ') || (state == '*')) &&
1173 (isspace((int) ((unsigned char) *xml)) != 0))
1174 *(xml++)=' ';
1175 else
1176 xml++;
1177 }
1178 if (state == '*')
1179 {
1180 /*
1181 Normalize spaces for non-CDATA attributes.
1182 */
1183 for (xml=p; *xml != '\0'; xml++)
1184 {
1185 char
1186 accept[] = " ";
1187
1188 i=(ssize_t) strspn(xml,accept);
1189 if (i != 0)
1190 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1191 while ((*xml != '\0') && (*xml != ' '))
1192 xml++;
1193 if (*xml == '\0')
1194 break;
1195 }
1196 xml--;
1197 if ((xml >= p) && (*xml == ' '))
1198 *xml='\0';
1199 }
1200 return(p == q ? ConstantString(p) : p);
1201}
1202
1203static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1204 const size_t length,const char state)
1205{
1206 XMLTreeInfo
1207 *xml_info;
1208
1209 xml_info=root->node;
1210 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1211 (length == 0))
1212 return;
1213 xml[length]='\0';
1214 xml=ParseEntities(xml,root->entities,state);
1215 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1216 {
1217 (void) ConcatenateString(&xml_info->content,xml);
1218 xml=DestroyString(xml);
1219 }
1220 else
1221 {
1222 if (xml_info->content != (char *) NULL)
1223 xml_info->content=DestroyString(xml_info->content);
1224 xml_info->content=xml;
1225 }
1226}
1227
1228static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1229 ExceptionInfo *exception)
1230{
1231 if ((root->node == (XMLTreeInfo *) NULL) ||
1232 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1233 {
1234 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1235 "ParseError","unexpected closing tag </%s>",tag);
1236 return(&root->root);
1237 }
1238 root->node=root->node->parent;
1239 return((XMLTreeInfo *) NULL);
1240}
1241
1242static MagickBooleanType ValidateEntities(char *tag,char *xml,
1243 const size_t depth,char **entities)
1244{
1245 ssize_t
1246 i;
1247
1248 /*
1249 Check for circular entity references.
1250 */
1251 if (depth > MagickMaxRecursionDepth)
1252 return(MagickFalse);
1253 for ( ; ; xml++)
1254 {
1255 while ((*xml != '\0') && (*xml != '&'))
1256 xml++;
1257 if (*xml == '\0')
1258 return(MagickTrue);
1259 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1260 return(MagickFalse);
1261 i=0;
1262 while ((entities[i] != (char *) NULL) &&
1263 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1264 i+=2;
1265 if ((entities[i] != (char *) NULL) &&
1266 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1267 return(MagickFalse);
1268 }
1269}
1270
1271static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1272 size_t length)
1273{
1274 char
1275 *target;
1276
1277 ssize_t
1278 i,
1279 j;
1280
1281 target=xml;
1282 xml[length]='\0';
1283 xml+=strcspn(xml,XMLWhitespace);
1284 if (*xml != '\0')
1285 {
1286 *xml='\0';
1287 xml+=strspn(xml+1,XMLWhitespace)+1;
1288 }
1289 if (strcmp(target,"xml") == 0)
1290 {
1291 xml=strstr(xml,"standalone");
1292 if ((xml != (char *) NULL) &&
1293 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1294 root->standalone=MagickTrue;
1295 return;
1296 }
1297 if (root->processing_instructions[0] == (char **) NULL)
1298 {
1299 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1300 *root->processing_instructions));
1301 *root->processing_instructions=(char **) NULL;
1302 }
1303 i=0;
1304 while ((root->processing_instructions[i] != (char **) NULL) &&
1305 (strcmp(target,root->processing_instructions[i][0]) != 0))
1306 i++;
1307 if (root->processing_instructions[i] == (char **) NULL)
1308 {
1309 root->processing_instructions=(char ***) ResizeQuantumMemory(
1310 root->processing_instructions,(size_t) (i+2),
1311 sizeof(*root->processing_instructions));
1312 if (root->processing_instructions == (char ***) NULL)
1313 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1314 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1315 sizeof(**root->processing_instructions));
1316 if (root->processing_instructions[i] == (char **) NULL)
1317 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1318 root->processing_instructions[i+1]=(char **) NULL;
1319 root->processing_instructions[i][0]=ConstantString(target);
1320 root->processing_instructions[i][1]=(char *)
1321 root->processing_instructions[i+1];
1322 root->processing_instructions[i+1]=(char **) NULL;
1323 root->processing_instructions[i][2]=ConstantString("");
1324 }
1325 j=1;
1326 while (root->processing_instructions[i][j] != (char *) NULL)
1327 j++;
1328 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1329 root->processing_instructions[i],(size_t) (j+3),
1330 sizeof(**root->processing_instructions));
1331 if (root->processing_instructions[i] == (char **) NULL)
1332 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1333 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1334 root->processing_instructions[i][j+1],(size_t) (j+1),
1335 sizeof(***root->processing_instructions));
1336 if (root->processing_instructions[i][j+2] == (char *) NULL)
1337 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1338 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1339 root->root.tag != (char *) NULL ? ">" : "<",2);
1340 root->processing_instructions[i][j]=ConstantString(xml);
1341 root->processing_instructions[i][j+1]=(char *) NULL;
1342}
1343
1344static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1345 size_t length,ExceptionInfo *exception)
1346{
1347 char
1348 *c,
1349 **entities,
1350 *n,
1351 **predefined_entities,
1352 q,
1353 *t,
1354 *v;
1355
1356 ssize_t
1357 i,
1358 j;
1359
1360 n=(char *) NULL;
1361 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1362 if (predefined_entities == (char **) NULL)
1363 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1364 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1365 for (xml[length]='\0'; xml != (char *) NULL; )
1366 {
1367 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1368 xml++;
1369 if (*xml == '\0')
1370 break;
1371 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1372 {
1373 /*
1374 Parse entity definitions.
1375 */
1376 if (strspn(xml+8,XMLWhitespace) == 0)
1377 break;
1378 xml+=strspn(xml+8,XMLWhitespace)+8;
1379 c=xml;
1380 n=xml+strspn(xml,XMLWhitespace "%");
1381 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1382 break;
1383 xml=n+strcspn(n,XMLWhitespace);
1384 if (*xml == '\0')
1385 break;
1386 *xml=';';
1387 v=xml+strspn(xml+1,XMLWhitespace)+1;
1388 q=(*v);
1389 v++;
1390 if ((q != '"') && (q != '\''))
1391 {
1392 /*
1393 Skip externals.
1394 */
1395 xml=strchr(xml,'>');
1396 continue;
1397 }
1398 entities=(*c == '%') ? predefined_entities : root->entities;
1399 for (i=0; entities[i] != (char *) NULL; i++) ;
1400 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1401 sizeof(*entities));
1402 if (entities == (char **) NULL)
1403 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1404 if (*c == '%')
1405 predefined_entities=entities;
1406 else
1407 root->entities=entities;
1408 xml++;
1409 *xml='\0';
1410 xml=strchr(v,q);
1411 if (xml != (char *) NULL)
1412 {
1413 *xml='\0';
1414 xml++;
1415 }
1416 entities[i+1]=ParseEntities(v,predefined_entities,'%');
1417 entities[i+2]=(char *) NULL;
1418 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1419 entities[i]=n;
1420 else
1421 {
1422 if (entities[i+1] != v)
1423 entities[i+1]=DestroyString(entities[i+1]);
1424 (void) ThrowMagickException(exception,GetMagickModule(),
1425 OptionWarning,"ParseError","circular entity declaration &%s",n);
1426 predefined_entities=(char **) RelinquishMagickMemory(
1427 predefined_entities);
1428 return(MagickFalse);
1429 }
1430 }
1431 else
1432 if (strncmp(xml,"<!ATTLIST",9) == 0)
1433 {
1434 /*
1435 Parse default attributes.
1436 */
1437 t=xml+strspn(xml+9,XMLWhitespace)+9;
1438 if (*t == '\0')
1439 {
1440 (void) ThrowMagickException(exception,GetMagickModule(),
1441 OptionWarning,"ParseError","unclosed <!ATTLIST");
1442 predefined_entities=(char **) RelinquishMagickMemory(
1443 predefined_entities);
1444 return(MagickFalse);
1445 }
1446 xml=t+strcspn(t,XMLWhitespace ">");
1447 if (*xml == '>')
1448 continue;
1449 *xml='\0';
1450 i=0;
1451 while ((root->attributes[i] != (char **) NULL) &&
1452 (n != (char *) NULL) &&
1453 (strcmp(n,root->attributes[i][0]) != 0))
1454 i++;
1455 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1456 (*n != '>'))
1457 {
1458 xml=n+strcspn(n,XMLWhitespace);
1459 if (*xml != '\0')
1460 *xml='\0';
1461 else
1462 {
1463 (void) ThrowMagickException(exception,GetMagickModule(),
1464 OptionWarning,"ParseError","malformed <!ATTLIST");
1465 predefined_entities=(char **) RelinquishMagickMemory(
1466 predefined_entities);
1467 return(MagickFalse);
1468 }
1469 xml+=strspn(xml+1,XMLWhitespace)+1;
1470 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1471 if (strncmp(xml,"NOTATION",8) == 0)
1472 xml+=strspn(xml+8,XMLWhitespace)+8;
1473 xml=(*xml == '(') ? strchr(xml,')') : xml+
1474 strcspn(xml,XMLWhitespace);
1475 if (xml == (char *) NULL)
1476 {
1477 (void) ThrowMagickException(exception,GetMagickModule(),
1478 OptionWarning,"ParseError","malformed <!ATTLIST");
1479 predefined_entities=(char **) RelinquishMagickMemory(
1480 predefined_entities);
1481 return(MagickFalse);
1482 }
1483 xml+=strspn(xml,XMLWhitespace ")");
1484 if (strncmp(xml,"#FIXED",6) == 0)
1485 xml+=strspn(xml+6,XMLWhitespace)+6;
1486 if (*xml == '#')
1487 {
1488 xml+=strcspn(xml,XMLWhitespace ">")-1;
1489 if (*c == ' ')
1490 continue;
1491 v=(char *) NULL;
1492 }
1493 else
1494 if (((*xml == '"') || (*xml == '\'')) &&
1495 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1496 *xml='\0';
1497 else
1498 {
1499 (void) ThrowMagickException(exception,GetMagickModule(),
1500 OptionWarning,"ParseError","malformed <!ATTLIST");
1501 predefined_entities=(char **) RelinquishMagickMemory(
1502 predefined_entities);
1503 return(MagickFalse);
1504 }
1505 if (root->attributes[i] == (char **) NULL)
1506 {
1507 /*
1508 New attribute tag.
1509 */
1510 if (i == 0)
1511 root->attributes=(char ***) AcquireQuantumMemory(2,
1512 sizeof(*root->attributes));
1513 else
1514 root->attributes=(char ***) ResizeQuantumMemory(
1515 root->attributes,(size_t) (i+2),
1516 sizeof(*root->attributes));
1517 if (root->attributes == (char ***) NULL)
1518 ThrowFatalException(ResourceLimitFatalError,
1519 "MemoryAllocationFailed");
1520 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1521 sizeof(**root->attributes));
1522 if (root->attributes[i] == (char **) NULL)
1523 ThrowFatalException(ResourceLimitFatalError,
1524 "MemoryAllocationFailed");
1525 root->attributes[i][0]=ConstantString(t);
1526 root->attributes[i][1]=(char *) NULL;
1527 root->attributes[i+1]=(char **) NULL;
1528 }
1529 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1530 root->attributes[i]=(char **) ResizeQuantumMemory(
1531 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1532 if (root->attributes[i] == (char **) NULL)
1533 ThrowFatalException(ResourceLimitFatalError,
1534 "MemoryAllocationFailed");
1535 root->attributes[i][j+3]=(char *) NULL;
1536 root->attributes[i][j+2]=ConstantString(c);
1537 root->attributes[i][j+1]=(char *) NULL;
1538 if (v != (char *) NULL)
1539 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1540 root->attributes[i][j]=ConstantString(n);
1541 }
1542 }
1543 else
1544 if (strncmp(xml, "<!--", 4) == 0)
1545 xml=strstr(xml+4,"-->");
1546 else
1547 if (strncmp(xml,"<?", 2) == 0)
1548 {
1549 c=xml+2;
1550 xml=strstr(c,"?>");
1551 if (xml != (char *) NULL)
1552 {
1553 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1554 xml++;
1555 }
1556 }
1557 else
1558 if (*xml == '<')
1559 xml=strchr(xml,'>');
1560 else
1561 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1562 break;
1563 }
1564 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities);
1565 return(MagickTrue);
1566}
1567
1568static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1569{
1570 XMLTreeInfo
1571 *xml_info;
1572
1573 xml_info=root->node;
1574 if (xml_info->tag == (char *) NULL)
1575 xml_info->tag=ConstantString(tag);
1576 else
1577 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1578 if (xml_info != (XMLTreeInfo *) NULL)
1579 xml_info->attributes=attributes;
1580 root->node=xml_info;
1581}
1582
1583static const char
1584 *ignore_tags[3] =
1585 {
1586 "rdf:Bag",
1587 "rdf:Seq",
1588 (const char *) NULL
1589 };
1590
1591static inline MagickBooleanType IsSkipTag(const char *tag)
1592{
1593 ssize_t
1594 i;
1595
1596 i=0;
1597 while (ignore_tags[i] != (const char *) NULL)
1598 {
1599 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1600 return(MagickTrue);
1601 i++;
1602 }
1603 return(MagickFalse);
1604}
1605
1606MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1607{
1608 char
1609 **attribute,
1610 **attributes,
1611 *p,
1612 *tag,
1613 *utf8;
1614
1615 int
1616 c,
1617 terminal;
1618
1619 MagickBooleanType
1620 status;
1621
1622 size_t
1623 ignore_depth,
1624 length;
1625
1626 ssize_t
1627 i,
1628 j,
1629 l;
1630
1631 XMLTreeRoot
1632 *root;
1633
1634 /*
1635 Convert xml-string to UTF8.
1636 */
1637 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1638 {
1639 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1640 "ParseError","root tag missing");
1641 return((XMLTreeInfo *) NULL);
1642 }
1643 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1644 length=strlen(xml);
1645 utf8=ConvertUTF16ToUTF8(xml,&length);
1646 if (utf8 == (char *) NULL)
1647 {
1648 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1649 "ParseError","UTF16 to UTF8 failed");
1650 return((XMLTreeInfo *) NULL);
1651 }
1652 if (length == 0)
1653 {
1654 utf8=DestroyString(utf8);
1655 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1656 "ParseError","root tag missing");
1657 return((XMLTreeInfo *) NULL);
1658 }
1659 terminal=utf8[length-1];
1660 utf8[length-1]='\0';
1661 p=utf8;
1662 while ((*p != '\0') && (*p != '<'))
1663 p++;
1664 if (*p == '\0')
1665 {
1666 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1667 "ParseError","root tag missing");
1668 utf8=DestroyString(utf8);
1669 return((XMLTreeInfo *) NULL);
1670 }
1671 attribute=(char **) NULL;
1672 l=0;
1673 ignore_depth=0;
1674 for (p++; ; p++)
1675 {
1676 attributes=(char **) sentinel;
1677 tag=p;
1678 c=(*p);
1679 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1680 (*p == ':') || (c < '\0'))
1681 {
1682 /*
1683 Tag.
1684 */
1685 if (root->node == (XMLTreeInfo *) NULL)
1686 {
1687 (void) ThrowMagickException(exception,GetMagickModule(),
1688 OptionWarning,"ParseError","root tag missing");
1689 utf8=DestroyString(utf8);
1690 return(&root->root);
1691 }
1692 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1693 while (isspace((int) ((unsigned char) *p)) != 0)
1694 *p++='\0';
1695 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1696 (ignore_depth == 0))
1697 {
1698 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1699 {
1700 /*
1701 Find tag in default attributes list.
1702 */
1703 i=0;
1704 while ((root->attributes[i] != (char **) NULL) &&
1705 (strcmp(root->attributes[i][0],tag) != 0))
1706 i++;
1707 attribute=root->attributes[i];
1708 }
1709 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1710 {
1711 /*
1712 Attribute.
1713 */
1714 if (l == 0)
1715 attributes=(char **) AcquireQuantumMemory(4,
1716 sizeof(*attributes));
1717 else
1718 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1719 (l+4),sizeof(*attributes));
1720 if (attributes == (char **) NULL)
1721 {
1722 (void) ThrowMagickException(exception,GetMagickModule(),
1723 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1724 utf8=DestroyString(utf8);
1725 return(&root->root);
1726 }
1727 attributes[l+2]=(char *) NULL;
1728 attributes[l+1]=(char *) NULL;
1729 attributes[l]=p;
1730 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
1731 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1732 attributes[l]=ConstantString("");
1733 else
1734 {
1735 *p++='\0';
1736 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
1737 c=(*p);
1738 if ((c == '"') || (c == '\''))
1739 {
1740 /*
1741 Attributes value.
1742 */
1743 p++;
1744 attributes[l+1]=p;
1745 while ((*p != '\0') && (*p != c))
1746 p++;
1747 if (*p != '\0')
1748 *p++='\0';
1749 else
1750 {
1751 attributes[l]=ConstantString("");
1752 attributes[l+1]=ConstantString("");
1753 (void) DestroyXMLTreeAttributes(attributes);
1754 (void) ThrowMagickException(exception,
1755 GetMagickModule(),OptionWarning,"ParseError",
1756 "missing %c",c);
1757 utf8=DestroyString(utf8);
1758 return(&root->root);
1759 }
1760 j=1;
1761 while ((attribute != (char **) NULL) &&
1762 (attribute[j] != (char *) NULL) &&
1763 (strcmp(attribute[j],attributes[l]) != 0))
1764 j+=3;
1765 attributes[l+1]=ParseEntities(attributes[l+1],
1766 root->entities,(attribute != (char **) NULL) &&
1767 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
1768 ' ');
1769 }
1770 attributes[l]=ConstantString(attributes[l]);
1771 }
1772 while (isspace((int) ((unsigned char) *p)) != 0)
1773 p++;
1774 }
1775 }
1776 else
1777 {
1778 while ((*p != '\0') && (*p != '/') && (*p != '>'))
1779 p++;
1780 }
1781 if (*p == '/')
1782 {
1783 /*
1784 Self closing tag.
1785 */
1786 *p++='\0';
1787 if (((*p != '\0') && (*p != '>')) ||
1788 ((*p == '\0') && (terminal != '>')))
1789 {
1790 if (l != 0)
1791 (void) DestroyXMLTreeAttributes(attributes);
1792 (void) ThrowMagickException(exception,GetMagickModule(),
1793 OptionWarning,"ParseError","missing >");
1794 utf8=DestroyString(utf8);
1795 return(&root->root);
1796 }
1797 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1798 (void) DestroyXMLTreeAttributes(attributes);
1799 else
1800 {
1801 ParseOpenTag(root,tag,attributes);
1802 (void) ParseCloseTag(root,tag,exception);
1803 }
1804 }
1805 else
1806 {
1807 c=(*p);
1808 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1809 {
1810 *p='\0';
1811 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1812 ParseOpenTag(root,tag,attributes);
1813 else
1814 {
1815 ignore_depth++;
1816 (void) DestroyXMLTreeAttributes(attributes);
1817 }
1818 *p=(char) c;
1819 }
1820 else
1821 {
1822 if (l != 0)
1823 (void) DestroyXMLTreeAttributes(attributes);
1824 (void) ThrowMagickException(exception,GetMagickModule(),
1825 OptionWarning,"ParseError","missing >");
1826 utf8=DestroyString(utf8);
1827 return(&root->root);
1828 }
1829 }
1830 }
1831 else
1832 if (*p == '/')
1833 {
1834 /*
1835 Close tag.
1836 */
1837 tag=p+1;
1838 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
1839 c=(*p);
1840 if ((c == '\0') && (terminal != '>'))
1841 {
1842 (void) ThrowMagickException(exception,GetMagickModule(),
1843 OptionWarning,"ParseError","missing >");
1844 utf8=DestroyString(utf8);
1845 return(&root->root);
1846 }
1847 *p='\0';
1848 if ((ignore_depth == 0) &&
1849 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1850 {
1851 utf8=DestroyString(utf8);
1852 return(&root->root);
1853 }
1854 if (ignore_depth > 0)
1855 ignore_depth--;
1856 *p=(char) c;
1857 if (isspace((int) ((unsigned char) *p)) != 0)
1858 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1859 }
1860 else
1861 if (strncmp(p,"!--",3) == 0)
1862 {
1863 /*
1864 Comment.
1865 */
1866 p=strstr(p+3,"--");
1867 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1868 ((*p == '\0') && (terminal != '>')))
1869 {
1870 (void) ThrowMagickException(exception,GetMagickModule(),
1871 OptionWarning,"ParseError","unclosed <!--");
1872 utf8=DestroyString(utf8);
1873 return(&root->root);
1874 }
1875 }
1876 else
1877 if (strncmp(p,"![CDATA[",8) == 0)
1878 {
1879 /*
1880 Cdata.
1881 */
1882 p=strstr(p,"]]>");
1883 if (p != (char *) NULL)
1884 {
1885 p+=(ptrdiff_t) 2;
1886 if (ignore_depth == 0)
1887 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1888 }
1889 else
1890 {
1891 (void) ThrowMagickException(exception,GetMagickModule(),
1892 OptionWarning,"ParseError","unclosed <![CDATA[");
1893 utf8=DestroyString(utf8);
1894 return(&root->root);
1895 }
1896 }
1897 else
1898 if (strncmp(p,"!DOCTYPE",8) == 0)
1899 {
1900 /*
1901 DTD.
1902 */
1903 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1904 ((l != 0) && ((*p != ']') ||
1905 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1906 l=(ssize_t) ((*p == '[') ? 1 : l))
1907 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
1908 if ((*p == '\0') && (terminal != '>'))
1909 {
1910 (void) ThrowMagickException(exception,GetMagickModule(),
1911 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1912 utf8=DestroyString(utf8);
1913 return(&root->root);
1914 }
1915 if (l != 0)
1916 tag=strchr(tag,'[')+1;
1917 if (l != 0)
1918 {
1919 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1920 exception);
1921 if (status == MagickFalse)
1922 {
1923 utf8=DestroyString(utf8);
1924 return(&root->root);
1925 }
1926 p++;
1927 }
1928 }
1929 else
1930 if (*p == '?')
1931 {
1932 /*
1933 Processing instructions.
1934 */
1935 do
1936 {
1937 p=strchr(p,'?');
1938 if (p == (char *) NULL)
1939 break;
1940 p++;
1941 } while ((*p != '\0') && (*p != '>'));
1942 if ((p == (char *) NULL) || ((*p == '\0') &&
1943 (terminal != '>')))
1944 {
1945 (void) ThrowMagickException(exception,GetMagickModule(),
1946 OptionWarning,"ParseError","unclosed <?");
1947 utf8=DestroyString(utf8);
1948 return(&root->root);
1949 }
1950 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
1951 }
1952 else
1953 {
1954 (void) ThrowMagickException(exception,GetMagickModule(),
1955 OptionWarning,"ParseError","unexpected <");
1956 utf8=DestroyString(utf8);
1957 return(&root->root);
1958 }
1959 if ((p == (char *) NULL) || (*p == '\0'))
1960 break;
1961 *p++='\0';
1962 tag=p;
1963 if ((*p != '\0') && (*p != '<'))
1964 {
1965 /*
1966 Tag character content.
1967 */
1968 while ((*p != '\0') && (*p != '<'))
1969 p++;
1970 if (*p == '\0')
1971 break;
1972 if (ignore_depth == 0)
1973 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
1974 }
1975 else
1976 if (*p == '\0')
1977 break;
1978 }
1979 utf8=DestroyString(utf8);
1980 if (root->node == (XMLTreeInfo *) NULL)
1981 return(&root->root);
1982 if (root->node->tag == (char *) NULL)
1983 {
1984 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1985 "ParseError","root tag missing");
1986 return(&root->root);
1987 }
1988 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1989 "ParseError","unclosed tag: '%s'",root->node->tag);
1990 return(&root->root);
1991}
1992
1993/*
1994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995% %
1996% %
1997% %
1998% N e w X M L T r e e T a g %
1999% %
2000% %
2001% %
2002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2003%
2004% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2005%
2006% The format of the NewXMLTreeTag method is:
2007%
2008% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2009%
2010% A description of each parameter follows:
2011%
2012% o tag: the tag.
2013%
2014*/
2015MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2016{
2017 static const char
2018 *predefined_entities[NumberPredefinedEntities+1] =
2019 {
2020 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2021 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2022 };
2023
2024 XMLTreeRoot
2025 *root;
2026
2027 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2028 (void) memset(root,0,sizeof(*root));
2029 root->root.tag=(char *) NULL;
2030 if (tag != (char *) NULL)
2031 root->root.tag=ConstantString(tag);
2032 root->node=(&root->root);
2033 root->root.content=ConstantString("");
2034 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2035 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2036 root->root.attributes=sentinel;
2037 root->attributes=(char ***) root->root.attributes;
2038 root->processing_instructions=(char ***) root->root.attributes;
2039 root->debug=IsEventLogging();
2040 root->signature=MagickCoreSignature;
2041 return(&root->root);
2042}
2043
2044/*
2045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2046% %
2047% %
2048% %
2049% S e t X M L T r e e C o n t e n t %
2050% %
2051% %
2052% %
2053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2054%
2055% SetXMLTreeContent() sets the character content for the given tag and
2056% returns the tag.
2057%
2058% The format of the SetXMLTreeContent method is:
2059%
2060% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2061% const char *content)
2062%
2063% A description of each parameter follows:
2064%
2065% o xml_info: the xml info.
2066%
2067% o content: The content.
2068%
2069*/
2070MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2071 const char *content)
2072{
2073 assert(xml_info != (XMLTreeInfo *) NULL);
2074 assert((xml_info->signature == MagickCoreSignature) ||
2075 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2076 if (IsEventLogging() != MagickFalse)
2077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2078 if (xml_info->content != (char *) NULL)
2079 xml_info->content=DestroyString(xml_info->content);
2080 xml_info->content=(char *) ConstantString(content);
2081 return(xml_info);
2082}
2083
2084/*
2085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2086% %
2087% %
2088% %
2089% X M L T r e e I n f o T o X M L %
2090% %
2091% %
2092% %
2093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2094%
2095% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2096%
2097% The format of the XMLTreeInfoToXML method is:
2098%
2099% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2100%
2101% A description of each parameter follows:
2102%
2103% o xml_info: the xml info.
2104%
2105*/
2106
2107static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2108 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2109{
2110 char
2111 *canonical_content;
2112
2113 if (offset < 0)
2114 canonical_content=CanonicalXMLContent(source,pedantic);
2115 else
2116 {
2117 char
2118 *content;
2119
2120 content=AcquireString(source);
2121 content[offset]='\0';
2122 canonical_content=CanonicalXMLContent(content,pedantic);
2123 content=DestroyString(content);
2124 }
2125 if (canonical_content == (char *) NULL)
2126 return(*destination);
2127 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2128 {
2129 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2130 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2131 sizeof(**destination));
2132 if (*destination == (char *) NULL)
2133 return(*destination);
2134 }
2135 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2136 canonical_content);
2137 canonical_content=DestroyString(canonical_content);
2138 return(*destination);
2139}
2140
2141static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2142 size_t *extent,size_t start,char ***attributes)
2143{
2144 char
2145 *content;
2146
2147 const char
2148 *attribute;
2149
2150 size_t
2151 offset;
2152
2153 ssize_t
2154 i,
2155 j;
2156
2157 content=(char *) "";
2158 if (xml_info->parent != (XMLTreeInfo *) NULL)
2159 content=xml_info->parent->content;
2160 offset=0;
2161 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2162 start),source,length,extent,MagickFalse);
2163 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2164 {
2165 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2166 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2167 if (*source == (char *) NULL)
2168 return(*source);
2169 }
2170 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2171 "<%s",xml_info->tag);
2172 for (i=0; xml_info->attributes[i]; i+=2)
2173 {
2174 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2175 if (attribute != xml_info->attributes[i+1])
2176 continue;
2177 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2178 {
2179 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2180 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2181 if (*source == (char *) NULL)
2182 return((char *) NULL);
2183 }
2184 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2185 xml_info->attributes[i]);
2186 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2187 extent,MagickTrue);
2188 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2189 }
2190 i=0;
2191 while ((attributes[i] != (char **) NULL) &&
2192 (strcmp(attributes[i][0],xml_info->tag) != 0))
2193 i++;
2194 j=1;
2195 while ((attributes[i] != (char **) NULL) &&
2196 (attributes[i][j] != (char *) NULL))
2197 {
2198 if ((attributes[i][j+1] == (char *) NULL) ||
2199 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2200 {
2201 j+=3;
2202 continue;
2203 }
2204 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2205 {
2206 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2207 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2208 if (*source == (char *) NULL)
2209 return((char *) NULL);
2210 }
2211 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2212 attributes[i][j]);
2213 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2214 MagickTrue);
2215 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2216 j+=3;
2217 }
2218 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2219 *xml_info->content ? ">" : "/>");
2220 if (xml_info->child != (XMLTreeInfo *) NULL)
2221 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2222 else
2223 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2224 MagickFalse);
2225 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2226 {
2227 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2228 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2229 if (*source == (char *) NULL)
2230 return((char *) NULL);
2231 }
2232 if (*xml_info->content != '\0')
2233 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2234 xml_info->tag);
2235 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2236 offset++;
2237 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2238 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2239 attributes);
2240 else
2241 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2242 MagickFalse);
2243 return(content);
2244}
2245
2246MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2247{
2248 char
2249 *p,
2250 *q,
2251 *xml;
2252
2253 size_t
2254 extent,
2255 length;
2256
2257 ssize_t
2258 i,
2259 j,
2260 k;
2261
2262 XMLTreeInfo
2263 *ordered,
2264 *parent;
2265
2266 XMLTreeRoot
2267 *root;
2268
2269 assert(xml_info != (XMLTreeInfo *) NULL);
2270 assert((xml_info->signature == MagickCoreSignature) ||
2271 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2272 if (IsEventLogging() != MagickFalse)
2273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2274 if (xml_info->tag == (char *) NULL)
2275 return((char *) NULL);
2276 xml=AcquireString((char *) NULL);
2277 length=0;
2278 extent=MagickPathExtent;
2279 root=(XMLTreeRoot *) xml_info;
2280 while (root->root.parent != (XMLTreeInfo *) NULL)
2281 root=(XMLTreeRoot *) root->root.parent;
2282 parent=xml_info->parent;
2283 if (parent == (XMLTreeInfo *) NULL)
2284 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2285 {
2286 /*
2287 Pre-root processing instructions.
2288 */
2289 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2290 p=root->processing_instructions[i][1];
2291 for (j=1; p != (char *) NULL; j++)
2292 {
2293 if (root->processing_instructions[i][k][j-1] == '>')
2294 {
2295 p=root->processing_instructions[i][j];
2296 continue;
2297 }
2298 q=root->processing_instructions[i][0];
2299 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2300 {
2301 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2302 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2303 if (xml == (char *) NULL)
2304 return(xml);
2305 }
2306 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2307 *p != '\0' ? " " : "",p);
2308 p=root->processing_instructions[i][j];
2309 }
2310 }
2311 ordered=xml_info->ordered;
2312 xml_info->parent=(XMLTreeInfo *) NULL;
2313 xml_info->ordered=(XMLTreeInfo *) NULL;
2314 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2315 xml_info->parent=parent;
2316 xml_info->ordered=ordered;
2317 if (parent == (XMLTreeInfo *) NULL)
2318 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2319 {
2320 /*
2321 Post-root processing instructions.
2322 */
2323 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2324 p=root->processing_instructions[i][1];
2325 for (j=1; p != (char *) NULL; j++)
2326 {
2327 if (root->processing_instructions[i][k][j-1] == '<')
2328 {
2329 p=root->processing_instructions[i][j];
2330 continue;
2331 }
2332 q=root->processing_instructions[i][0];
2333 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2334 {
2335 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2336 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2337 if (xml == (char *) NULL)
2338 return(xml);
2339 }
2340 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2341 *p != '\0' ? " " : "",p);
2342 p=root->processing_instructions[i][j];
2343 }
2344 }
2345 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2346}