MagickCore 7.1.1-43
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
xml-tree.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% X X M M L %
7% X X MM MM L %
8% X M M M L %
9% X X M M L %
10% X X M M LLLLL %
11% %
12% TTTTT RRRR EEEEE EEEEE %
13% T R R E E %
14% T RRRR EEE EEE %
15% T R R E E %
16% T R R EEEEE EEEEE %
17% %
18% %
19% XML Tree Methods %
20% %
21% Software Design %
22% Cristy %
23% December 2004 %
24% %
25% %
26% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
27% dedicated to making software imaging solutions freely available. %
28% %
29% You may not use this file except in compliance with the License. You may %
30% obtain a copy of the License at %
31% %
32% https://imagemagick.org/script/license.php %
33% %
34% Unless required by applicable law or agreed to in writing, software %
35% distributed under the License is distributed on an "AS IS" BASIS, %
36% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
37% See the License for the specific language governing permissions and %
38% limitations under the License. %
39% %
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41%
42% This module implements the standard handy xml-tree methods for storing and
43% retrieving nodes and attributes from an XML string.
44%
45*/
46␌
47/*
48 Include declarations.
49*/
50#include "MagickCore/studio.h"
51#include "MagickCore/blob.h"
52#include "MagickCore/blob-private.h"
53#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/image-private.h"
56#include "MagickCore/log.h"
57#include "MagickCore/memory_.h"
58#include "MagickCore/memory-private.h"
59#include "MagickCore/semaphore.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/string-private.h"
62#include "MagickCore/token-private.h"
63#include "MagickCore/xml-tree.h"
64#include "MagickCore/xml-tree-private.h"
65#include "MagickCore/utility.h"
66#include "MagickCore/utility-private.h"
67␌
68/*
69 Define declarations.
70*/
71#define NumberPredefinedEntities 10
72#define XMLWhitespace "\t\r\n "
73␌
74/*
75 Typedef declarations.
76*/
78{
79 char
80 *tag,
81 **attributes,
82 *content;
83
84 size_t
85 offset;
86
88 *parent,
89 *next,
90 *sibling,
91 *ordered,
92 *child;
93
94 MagickBooleanType
95 debug;
96
98 *semaphore;
99
100 size_t
101 signature;
102};
103
104typedef struct _XMLTreeRoot
106
108{
109 struct _XMLTreeInfo
110 root;
111
113 *node;
114
115 MagickBooleanType
116 standalone;
117
118 char
119 ***processing_instructions,
120 **entities,
121 ***attributes;
122
123 MagickBooleanType
124 debug;
125
127 *semaphore;
128
129 size_t
130 signature;
131};
132␌
133/*
134 Global declarations.
135*/
136static char
137 *sentinel[] = { (char *) NULL };
138␌
139/*
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141% %
142% %
143% %
144% A d d C h i l d T o X M L T r e e %
145% %
146% %
147% %
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149%
150% AddChildToXMLTree() adds a child tag at an offset relative to the start of
151% the parent tag's character content. Return the child tag.
152%
153% The format of the AddChildToXMLTree method is:
154%
155% XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
156% const size_t offset)
157%
158% A description of each parameter follows:
159%
160% o xml_info: the xml info.
161%
162% o tag: the tag.
163%
164% o offset: the tag offset.
165%
166*/
167
168static XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
169 XMLTreeInfo *child,const size_t offset)
170{
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{
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
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{
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{
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
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=read(file,xml+i,quantum);
624 if (count <= 0)
625 {
626 count=0;
627 if (errno != EINTR)
628 break;
629 }
630 if (~((size_t) i) < (quantum+1))
631 {
632 xml=(char *) RelinquishMagickMemory(xml);
633 break;
634 }
635 xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
636 if ((i+(size_t) count) >= extent)
637 break;
638 }
639 if (LocaleCompare(filename,"-") != 0)
640 file=close_utf8(file);
641 if (xml == (char *) NULL)
642 return((char *) NULL);
643 if (file == -1)
644 {
645 xml=(char *) RelinquishMagickMemory(xml);
646 return((char *) NULL);
647 }
648 length=MagickMin(i+(size_t) count,extent);
649 xml[length]='\0';
650 return(xml);
651 }
652 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
653 xml=(char *) NULL;
654 if (~length >= (MagickPathExtent-1))
655 xml=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*xml));
656 if (xml == (char *) NULL)
657 {
658 file=close_utf8(file);
659 return((char *) NULL);
660 }
661 map=MapBlob(file,ReadMode,0,length);
662 if (map != (char *) NULL)
663 {
664 (void) memcpy(xml,map,length);
665 (void) UnmapBlob(map,length);
666 }
667 else
668 {
669 (void) lseek(file,0,SEEK_SET);
670 for (i=0; i < length; i+=(size_t) count)
671 {
672 count=read(file,xml+i,(size_t) MagickMin(length-i,(size_t)
673 MagickMaxBufferExtent));
674 if (count <= 0)
675 {
676 count=0;
677 if (errno != EINTR)
678 break;
679 }
680 }
681 if (i < length)
682 {
683 file=close_utf8(file)-1;
684 xml=(char *) RelinquishMagickMemory(xml);
685 return((char *) NULL);
686 }
687 }
688 xml[length]='\0';
689 if (LocaleCompare(filename,"-") != 0)
690 file=close_utf8(file);
691 if (file == -1)
692 xml=(char *) RelinquishMagickMemory(xml);
693 return(xml);
694}
695␌
696/*
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698% %
699% %
700% %
701% G e t N e x t X M L T r e e T a g %
702% %
703% %
704% %
705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706%
707% GetNextXMLTreeTag() returns the next tag or NULL if not found.
708%
709% The format of the GetNextXMLTreeTag method is:
710%
711% XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
712%
713% A description of each parameter follows:
714%
715% o xml_info: the xml info.
716%
717*/
718MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
719{
720 assert(xml_info != (XMLTreeInfo *) NULL);
721 assert((xml_info->signature == MagickCoreSignature) ||
722 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
723 if (IsEventLogging() != MagickFalse)
724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
725 return(xml_info->next);
726}
727␌
728/*
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730% %
731% %
732% %
733% G e t X M L T r e e A t t r i b u t e %
734% %
735% %
736% %
737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738%
739% GetXMLTreeAttribute() returns the value of the attribute tag with the
740% specified tag if found, otherwise NULL.
741%
742% The format of the GetXMLTreeAttribute method is:
743%
744% const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
745%
746% A description of each parameter follows:
747%
748% o xml_info: the xml info.
749%
750% o tag: the attribute tag.
751%
752*/
753MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
754 const char *tag)
755{
756 ssize_t
757 i,
758 j;
759
761 *root;
762
763 assert(xml_info != (XMLTreeInfo *) NULL);
764 assert((xml_info->signature == MagickCoreSignature) ||
765 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
766 if (IsEventLogging() != MagickFalse)
767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
768 if (xml_info->attributes == (char **) NULL)
769 return((const char *) NULL);
770 i=0;
771 while ((xml_info->attributes[i] != (char *) NULL) &&
772 (strcmp(xml_info->attributes[i],tag) != 0))
773 i+=2;
774 if (xml_info->attributes[i] != (char *) NULL)
775 return(xml_info->attributes[i+1]);
776 root=(XMLTreeRoot*) xml_info;
777 while (root->root.parent != (XMLTreeInfo *) NULL)
778 root=(XMLTreeRoot *) root->root.parent;
779 i=0;
780 while ((root->attributes[i] != (char **) NULL) &&
781 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
782 i++;
783 if (root->attributes[i] == (char **) NULL)
784 return((const char *) NULL);
785 j=1;
786 while ((root->attributes[i][j] != (char *) NULL) &&
787 (strcmp(root->attributes[i][j],tag) != 0))
788 j+=3;
789 if (root->attributes[i][j] == (char *) NULL)
790 return((const char *) NULL);
791 return(root->attributes[i][j+1]);
792}
793␌
794/*
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796% %
797% %
798% %
799% G e t X M L T r e e C h i l d %
800% %
801% %
802% %
803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804%
805% GetXMLTreeChild() returns the first child tag with the specified tag if
806% found, otherwise NULL.
807%
808% The format of the GetXMLTreeChild method is:
809%
810% XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
811%
812% A description of each parameter follows:
813%
814% o xml_info: the xml info.
815%
816*/
817MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
818{
820 *child;
821
822 assert(xml_info != (XMLTreeInfo *) NULL);
823 assert((xml_info->signature == MagickCoreSignature) ||
824 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
825 if (IsEventLogging() != MagickFalse)
826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
827 child=xml_info->child;
828 if (tag != (const char *) NULL)
829 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
830 child=child->sibling;
831 return(child);
832}
833␌
834/*
835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836% %
837% %
838% %
839% G e t X M L T r e e C o n t e n t %
840% %
841% %
842% %
843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844%
845% GetXMLTreeContent() returns any content associated with specified
846% xml-tree node.
847%
848% The format of the GetXMLTreeContent method is:
849%
850% const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
851%
852% A description of each parameter follows:
853%
854% o xml_info: the xml info.
855%
856*/
857MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
858{
859 assert(xml_info != (XMLTreeInfo *) NULL);
860 assert((xml_info->signature == MagickCoreSignature) ||
861 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
862 if (IsEventLogging() != MagickFalse)
863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
864 return(xml_info->content);
865}
866␌
867/*
868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869% %
870% %
871% %
872% G e t X M L T r e e S i b l i n g %
873% %
874% %
875% %
876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877%
878% GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
879%
880% The format of the GetXMLTreeSibling method is:
881%
882% XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
883%
884% A description of each parameter follows:
885%
886% o xml_info: the xml info.
887%
888*/
889MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
890{
891 assert(xml_info != (XMLTreeInfo *) NULL);
892 assert((xml_info->signature == MagickCoreSignature) ||
893 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
894 if (IsEventLogging() != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
896 return(xml_info->sibling);
897}
898␌
899/*
900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901% %
902% %
903% %
904% G e t X M L T r e e T a g %
905% %
906% %
907% %
908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909%
910% GetXMLTreeTag() returns the tag associated with specified xml-tree node.
911%
912% The format of the GetXMLTreeTag method is:
913%
914% const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
915%
916% A description of each parameter follows:
917%
918% o xml_info: the xml info.
919%
920*/
921MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
922{
923 assert(xml_info != (XMLTreeInfo *) NULL);
924 assert((xml_info->signature == MagickCoreSignature) ||
925 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
926 if (IsEventLogging() != MagickFalse)
927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
928 return(xml_info->tag);
929}
930␌
931/*
932%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933% %
934% %
935% %
936% N e w X M L T r e e %
937% %
938% %
939% %
940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941%
942% NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
943% XML string.
944%
945% The format of the NewXMLTree method is:
946%
947% XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
948%
949% A description of each parameter follows:
950%
951% o xml: A null-terminated XML string.
952%
953% o exception: return any errors or warnings in this structure.
954%
955*/
956
957static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
958{
959 char
960 *utf8;
961
962 int
963 bits,
964 byte,
965 c,
966 encoding;
967
968 size_t
969 extent;
970
971 ssize_t
972 i,
973 j;
974
975 utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
976 if (utf8 == (char *) NULL)
977 return((char *) NULL);
978 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
979 if (encoding == -1)
980 {
981 /*
982 Already UTF-8.
983 */
984 (void) memcpy(utf8,content,*length*sizeof(*utf8));
985 utf8[*length]='\0';
986 return(utf8);
987 }
988 j=0;
989 extent=(*length);
990 for (i=2; i < (ssize_t) (*length-1); i+=2)
991 {
992 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
993 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
994 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
995 {
996 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
997 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
998 (content[i] & 0xff);
999 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1000 }
1001 if ((size_t) (j+MagickPathExtent) > extent)
1002 {
1003 extent=(size_t) j+MagickPathExtent;
1004 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
1005 if (utf8 == (char *) NULL)
1006 return(utf8);
1007 }
1008 if (c < 0x80)
1009 {
1010 utf8[j]=c;
1011 j++;
1012 continue;
1013 }
1014 /*
1015 Multi-byte UTF-8 sequence.
1016 */
1017 byte=c;
1018 for (bits=0; byte != 0; byte/=2)
1019 bits++;
1020 bits=(bits-2)/5;
1021 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1022 while (bits != 0)
1023 {
1024 bits--;
1025 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1026 j++;
1027 }
1028 }
1029 *length=(size_t) j;
1030 utf8=(char *) ResizeQuantumMemory(utf8,(*length+1),sizeof(*utf8));
1031 if (utf8 != (char *) NULL)
1032 utf8[*length]='\0';
1033 return(utf8);
1034}
1035
1036static char *ParseEntities(char *xml,char **entities,int state)
1037{
1038 char
1039 *entity,
1040 *p,
1041 *q;
1042
1043 int
1044 byte,
1045 c;
1046
1047 size_t
1048 extent,
1049 length;
1050
1051 ssize_t
1052 i,
1053 offset;
1054
1055 /*
1056 Normalize line endings.
1057 */
1058 p=xml;
1059 q=xml;
1060 for ( ; *xml != '\0'; xml++)
1061 while (*xml == '\r')
1062 {
1063 *(xml++)='\n';
1064 if (*xml == '\n')
1065 (void) memmove(xml,xml+1,strlen(xml));
1066 }
1067 for (xml=p; ; )
1068 {
1069 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1070 (state != '%')) && (isspace((int) ((unsigned char) *xml)) == 0))
1071 xml++;
1072 if (*xml == '\0')
1073 break;
1074 /*
1075 States include:
1076 '&' for general entity decoding
1077 '%' for parameter entity decoding
1078 'c' for CDATA sections
1079 ' ' for attributes normalization
1080 '*' for non-CDATA attributes normalization
1081 */
1082 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1083 {
1084 /*
1085 Character reference.
1086 */
1087 if (xml[2] != 'x')
1088 c=(int) strtol(xml+2,&entity,10); /* base 10 */
1089 else
1090 c=(int) strtol(xml+3,&entity,16); /* base 16 */
1091 if ((c == 0) || (*entity != ';'))
1092 {
1093 /*
1094 Not a character reference.
1095 */
1096 xml++;
1097 continue;
1098 }
1099 if (c < 0x80)
1100 *(xml++)=c;
1101 else
1102 {
1103 /*
1104 Multi-byte UTF-8 sequence.
1105 */
1106 byte=c;
1107 for (i=0; byte != 0; byte/=2)
1108 i++;
1109 i=(i-2)/5;
1110 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1111 xml++;
1112 while (i != 0)
1113 {
1114 i--;
1115 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1116 xml++;
1117 }
1118 }
1119 (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1120 }
1121 else
1122 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1123 (state == '*'))) || ((state == '%') && (*xml == '%')))
1124 {
1125 /*
1126 Find entity in the list.
1127 */
1128 i=0;
1129 while ((entities[i] != (char *) NULL) &&
1130 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1131 i+=2;
1132 if (entities[i++] == (char *) NULL)
1133 xml++;
1134 else
1135 if (entities[i] != (char *) NULL)
1136 {
1137 /*
1138 Found a match.
1139 */
1140 length=strlen(entities[i]);
1141 entity=strchr(xml,';');
1142 if ((entity != (char *) NULL) &&
1143 ((length-1L) >= (size_t) (entity-xml)))
1144 {
1145 offset=(ssize_t) (xml-p);
1146 extent=((size_t) offset+length+strlen(entity));
1147 if (p != q)
1148 {
1149 p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1150 if (p == (char *) NULL)
1151 ThrowFatalException(ResourceLimitFatalError,
1152 "MemoryAllocationFailed");
1153 p[extent]='\0';
1154 }
1155 else
1156 {
1157 char
1158 *extent_xml;
1159
1160 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1161 sizeof(*extent_xml));
1162 if (extent_xml != (char *) NULL)
1163 {
1164 memset(extent_xml,0,extent*sizeof(*extent_xml));
1165 (void) CopyMagickString(extent_xml,p,extent*
1166 sizeof(*extent_xml));
1167 }
1168 p=extent_xml;
1169 }
1170 if (p == (char *) NULL)
1171 ThrowFatalException(ResourceLimitFatalError,
1172 "MemoryAllocationFailed");
1173 xml=p+offset;
1174 entity=strchr(xml,';');
1175 }
1176 if (entity != (char *) NULL)
1177 (void) memmove(xml+length,entity+1,strlen(entity));
1178 (void) memcpy(xml,entities[i],length);
1179 }
1180 }
1181 else
1182 if (((state == ' ') || (state == '*')) &&
1183 (isspace((int) ((unsigned char) *xml)) != 0))
1184 *(xml++)=' ';
1185 else
1186 xml++;
1187 }
1188 if (state == '*')
1189 {
1190 /*
1191 Normalize spaces for non-CDATA attributes.
1192 */
1193 for (xml=p; *xml != '\0'; xml++)
1194 {
1195 char
1196 accept[] = " ";
1197
1198 i=(ssize_t) strspn(xml,accept);
1199 if (i != 0)
1200 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1201 while ((*xml != '\0') && (*xml != ' '))
1202 xml++;
1203 if (*xml == '\0')
1204 break;
1205 }
1206 xml--;
1207 if ((xml >= p) && (*xml == ' '))
1208 *xml='\0';
1209 }
1210 return(p == q ? ConstantString(p) : p);
1211}
1212
1213static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1214 const size_t length,const char state)
1215{
1217 *xml_info;
1218
1219 xml_info=root->node;
1220 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1221 (length == 0))
1222 return;
1223 xml[length]='\0';
1224 xml=ParseEntities(xml,root->entities,state);
1225 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1226 {
1227 (void) ConcatenateString(&xml_info->content,xml);
1228 xml=DestroyString(xml);
1229 }
1230 else
1231 {
1232 if (xml_info->content != (char *) NULL)
1233 xml_info->content=DestroyString(xml_info->content);
1234 xml_info->content=xml;
1235 }
1236}
1237
1238static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1239 ExceptionInfo *exception)
1240{
1241 if ((root->node == (XMLTreeInfo *) NULL) ||
1242 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1243 {
1244 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1245 "ParseError","unexpected closing tag </%s>",tag);
1246 return(&root->root);
1247 }
1248 root->node=root->node->parent;
1249 return((XMLTreeInfo *) NULL);
1250}
1251
1252static MagickBooleanType ValidateEntities(char *tag,char *xml,
1253 const int delimiter,const size_t depth,char **entities)
1254{
1255 ssize_t
1256 i;
1257
1258 /*
1259 Check for circular entity references.
1260 */
1261 if (depth > MagickMaxRecursionDepth)
1262 return(MagickFalse);
1263 for ( ; ; xml++)
1264 {
1265 while ((*xml != '\0') && (*xml != delimiter))
1266 xml++;
1267 if (*xml == '\0')
1268 return(MagickTrue);
1269 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1270 return(MagickFalse);
1271 i=0;
1272 while ((entities[i] != (char *) NULL) &&
1273 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1274 i+=2;
1275 if ((entities[i] != (char *) NULL) &&
1276 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1277 return(MagickFalse);
1278 }
1279}
1280
1281static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1282 size_t length)
1283{
1284 char
1285 *target;
1286
1287 ssize_t
1288 i,
1289 j;
1290
1291 target=xml;
1292 xml[length]='\0';
1293 xml+=strcspn(xml,XMLWhitespace);
1294 if (*xml != '\0')
1295 {
1296 *xml='\0';
1297 xml+=strspn(xml+1,XMLWhitespace)+1;
1298 }
1299 if (strcmp(target,"xml") == 0)
1300 {
1301 xml=strstr(xml,"standalone");
1302 if ((xml != (char *) NULL) &&
1303 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1304 root->standalone=MagickTrue;
1305 return;
1306 }
1307 if (root->processing_instructions[0] == (char **) NULL)
1308 {
1309 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1310 *root->processing_instructions));
1311 *root->processing_instructions=(char **) NULL;
1312 }
1313 i=0;
1314 while ((root->processing_instructions[i] != (char **) NULL) &&
1315 (strcmp(target,root->processing_instructions[i][0]) != 0))
1316 i++;
1317 if (root->processing_instructions[i] == (char **) NULL)
1318 {
1319 root->processing_instructions=(char ***) ResizeQuantumMemory(
1320 root->processing_instructions,(size_t) (i+2),
1321 sizeof(*root->processing_instructions));
1322 if (root->processing_instructions == (char ***) NULL)
1323 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1324 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1325 sizeof(**root->processing_instructions));
1326 if (root->processing_instructions[i] == (char **) NULL)
1327 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1328 root->processing_instructions[i+1]=(char **) NULL;
1329 root->processing_instructions[i][0]=ConstantString(target);
1330 root->processing_instructions[i][1]=(char *)
1331 root->processing_instructions[i+1];
1332 root->processing_instructions[i+1]=(char **) NULL;
1333 root->processing_instructions[i][2]=ConstantString("");
1334 }
1335 j=1;
1336 while (root->processing_instructions[i][j] != (char *) NULL)
1337 j++;
1338 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1339 root->processing_instructions[i],(size_t) (j+3),
1340 sizeof(**root->processing_instructions));
1341 if (root->processing_instructions[i] == (char **) NULL)
1342 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1343 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1344 root->processing_instructions[i][j+1],(size_t) (j+1),
1345 sizeof(***root->processing_instructions));
1346 if (root->processing_instructions[i][j+2] == (char *) NULL)
1347 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1348 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1349 root->root.tag != (char *) NULL ? ">" : "<",2);
1350 root->processing_instructions[i][j]=ConstantString(xml);
1351 root->processing_instructions[i][j+1]=(char *) NULL;
1352}
1353
1354static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1355 size_t length,ExceptionInfo *exception)
1356{
1357 char
1358 *c,
1359 **entities,
1360 *n,
1361 **predefined_entities,
1362 q,
1363 *t,
1364 *v;
1365
1366 ssize_t
1367 i,
1368 j;
1369
1370 n=(char *) NULL;
1371 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1372 if (predefined_entities == (char **) NULL)
1373 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1374 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1375 for (xml[length]='\0'; xml != (char *) NULL; )
1376 {
1377 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1378 xml++;
1379 if (*xml == '\0')
1380 break;
1381 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1382 {
1383 /*
1384 Parse entity definitions.
1385 */
1386 if (strspn(xml+8,XMLWhitespace) == 0)
1387 break;
1388 xml+=strspn(xml+8,XMLWhitespace)+8;
1389 c=xml;
1390 n=xml+strspn(xml,XMLWhitespace "%");
1391 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1392 break;
1393 xml=n+strcspn(n,XMLWhitespace);
1394 if (*xml == '\0')
1395 break;
1396 *xml=';';
1397 v=xml+strspn(xml+1,XMLWhitespace)+1;
1398 q=(*v);
1399 v++;
1400 if ((q != '"') && (q != '\''))
1401 {
1402 /*
1403 Skip externals.
1404 */
1405 xml=strchr(xml,'>');
1406 continue;
1407 }
1408 entities=(*c == '%') ? predefined_entities : root->entities;
1409 for (i=0; entities[i] != (char *) NULL; i++) ;
1410 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1411 sizeof(*entities));
1412 if (entities == (char **) NULL)
1413 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1414 if (*c == '%')
1415 predefined_entities=entities;
1416 else
1417 root->entities=entities;
1418 xml++;
1419 *xml='\0';
1420 xml=strchr(v,q);
1421 if (xml != (char *) NULL)
1422 {
1423 *xml='\0';
1424 xml++;
1425 }
1426 entities[i+1]=ParseEntities(v,predefined_entities,'%');
1427 entities[i+2]=(char *) NULL;
1428 if ((ValidateEntities(n,entities[i+1],'%',0,entities) != MagickFalse) &&
1429 (ValidateEntities(n,entities[i+1],'&',0,entities) != MagickFalse))
1430 entities[i]=n;
1431 else
1432 {
1433 if (entities[i+1] != v)
1434 entities[i+1]=DestroyString(entities[i+1]);
1435 (void) ThrowMagickException(exception,GetMagickModule(),
1436 OptionWarning,"ParseError","circular entity declaration %s",n);
1437 predefined_entities=(char **) RelinquishMagickMemory(
1438 predefined_entities);
1439 return(MagickFalse);
1440 }
1441 }
1442 else
1443 if (strncmp(xml,"<!ATTLIST",9) == 0)
1444 {
1445 /*
1446 Parse default attributes.
1447 */
1448 t=xml+strspn(xml+9,XMLWhitespace)+9;
1449 if (*t == '\0')
1450 {
1451 (void) ThrowMagickException(exception,GetMagickModule(),
1452 OptionWarning,"ParseError","unclosed <!ATTLIST");
1453 predefined_entities=(char **) RelinquishMagickMemory(
1454 predefined_entities);
1455 return(MagickFalse);
1456 }
1457 xml=t+strcspn(t,XMLWhitespace ">");
1458 if (*xml == '>')
1459 continue;
1460 *xml='\0';
1461 i=0;
1462 while ((root->attributes[i] != (char **) NULL) &&
1463 (n != (char *) NULL) &&
1464 (strcmp(n,root->attributes[i][0]) != 0))
1465 i++;
1466 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1467 (*n != '>'))
1468 {
1469 xml=n+strcspn(n,XMLWhitespace);
1470 if (*xml != '\0')
1471 *xml='\0';
1472 else
1473 {
1474 (void) ThrowMagickException(exception,GetMagickModule(),
1475 OptionWarning,"ParseError","malformed <!ATTLIST");
1476 predefined_entities=(char **) RelinquishMagickMemory(
1477 predefined_entities);
1478 return(MagickFalse);
1479 }
1480 xml+=strspn(xml+1,XMLWhitespace)+1;
1481 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1482 if (strncmp(xml,"NOTATION",8) == 0)
1483 xml+=strspn(xml+8,XMLWhitespace)+8;
1484 xml=(*xml == '(') ? strchr(xml,')') : xml+
1485 strcspn(xml,XMLWhitespace);
1486 if (xml == (char *) NULL)
1487 {
1488 (void) ThrowMagickException(exception,GetMagickModule(),
1489 OptionWarning,"ParseError","malformed <!ATTLIST");
1490 predefined_entities=(char **) RelinquishMagickMemory(
1491 predefined_entities);
1492 return(MagickFalse);
1493 }
1494 xml+=strspn(xml,XMLWhitespace ")");
1495 if (strncmp(xml,"#FIXED",6) == 0)
1496 xml+=strspn(xml+6,XMLWhitespace)+6;
1497 if (*xml == '#')
1498 {
1499 xml+=strcspn(xml,XMLWhitespace ">")-1;
1500 if (*c == ' ')
1501 continue;
1502 v=(char *) NULL;
1503 }
1504 else
1505 if (((*xml == '"') || (*xml == '\'')) &&
1506 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1507 *xml='\0';
1508 else
1509 {
1510 (void) ThrowMagickException(exception,GetMagickModule(),
1511 OptionWarning,"ParseError","malformed <!ATTLIST");
1512 predefined_entities=(char **) RelinquishMagickMemory(
1513 predefined_entities);
1514 return(MagickFalse);
1515 }
1516 if (root->attributes[i] == (char **) NULL)
1517 {
1518 /*
1519 New attribute tag.
1520 */
1521 if (i == 0)
1522 root->attributes=(char ***) AcquireQuantumMemory(2,
1523 sizeof(*root->attributes));
1524 else
1525 root->attributes=(char ***) ResizeQuantumMemory(
1526 root->attributes,(size_t) (i+2),
1527 sizeof(*root->attributes));
1528 if (root->attributes == (char ***) NULL)
1529 ThrowFatalException(ResourceLimitFatalError,
1530 "MemoryAllocationFailed");
1531 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1532 sizeof(**root->attributes));
1533 if (root->attributes[i] == (char **) NULL)
1534 ThrowFatalException(ResourceLimitFatalError,
1535 "MemoryAllocationFailed");
1536 root->attributes[i][0]=ConstantString(t);
1537 root->attributes[i][1]=(char *) NULL;
1538 root->attributes[i+1]=(char **) NULL;
1539 }
1540 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1541 root->attributes[i]=(char **) ResizeQuantumMemory(
1542 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1543 if (root->attributes[i] == (char **) NULL)
1544 ThrowFatalException(ResourceLimitFatalError,
1545 "MemoryAllocationFailed");
1546 root->attributes[i][j+3]=(char *) NULL;
1547 root->attributes[i][j+2]=ConstantString(c);
1548 root->attributes[i][j+1]=(char *) NULL;
1549 if (v != (char *) NULL)
1550 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1551 root->attributes[i][j]=ConstantString(n);
1552 }
1553 }
1554 else
1555 if (strncmp(xml, "<!--", 4) == 0)
1556 xml=strstr(xml+4,"-->");
1557 else
1558 if (strncmp(xml,"<?", 2) == 0)
1559 {
1560 c=xml+2;
1561 xml=strstr(c,"?>");
1562 if (xml != (char *) NULL)
1563 {
1564 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1565 xml++;
1566 }
1567 }
1568 else
1569 if (*xml == '<')
1570 xml=strchr(xml,'>');
1571 else
1572 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1573 break;
1574 }
1575 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities);
1576 return(MagickTrue);
1577}
1578
1579static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1580{
1582 *xml_info;
1583
1584 xml_info=root->node;
1585 if (xml_info->tag == (char *) NULL)
1586 xml_info->tag=ConstantString(tag);
1587 else
1588 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1589 if (xml_info != (XMLTreeInfo *) NULL)
1590 xml_info->attributes=attributes;
1591 root->node=xml_info;
1592}
1593
1594static const char
1595 *ignore_tags[3] =
1596 {
1597 "rdf:Bag",
1598 "rdf:Seq",
1599 (const char *) NULL
1600 };
1601
1602static inline MagickBooleanType IsSkipTag(const char *tag)
1603{
1604 ssize_t
1605 i;
1606
1607 i=0;
1608 while (ignore_tags[i] != (const char *) NULL)
1609 {
1610 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1611 return(MagickTrue);
1612 i++;
1613 }
1614 return(MagickFalse);
1615}
1616
1617MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1618{
1619 char
1620 **attribute,
1621 **attributes,
1622 *p,
1623 *tag,
1624 *utf8;
1625
1626 int
1627 c,
1628 terminal;
1629
1630 MagickBooleanType
1631 status;
1632
1633 size_t
1634 ignore_depth,
1635 length;
1636
1637 ssize_t
1638 i,
1639 j,
1640 l;
1641
1643 *root;
1644
1645 /*
1646 Convert xml-string to UTF8.
1647 */
1648 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1649 {
1650 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1651 "ParseError","root tag missing");
1652 return((XMLTreeInfo *) NULL);
1653 }
1654 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1655 length=strlen(xml);
1656 utf8=ConvertUTF16ToUTF8(xml,&length);
1657 if (utf8 == (char *) NULL)
1658 {
1659 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1660 "ParseError","UTF16 to UTF8 failed");
1661 return((XMLTreeInfo *) NULL);
1662 }
1663 terminal=utf8[MagickMax(length-1,0)];
1664 utf8[MagickMax(length-1,0)]='\0';
1665 p=utf8;
1666 while ((*p != '\0') && (*p != '<'))
1667 p++;
1668 if (*p == '\0')
1669 {
1670 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1671 "ParseError","root tag missing");
1672 utf8=DestroyString(utf8);
1673 return((XMLTreeInfo *) NULL);
1674 }
1675 attribute=(char **) NULL;
1676 l=0;
1677 ignore_depth=0;
1678 for (p++; ; p++)
1679 {
1680 attributes=(char **) sentinel;
1681 tag=p;
1682 c=(*p);
1683 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1684 (*p == ':') || (c < '\0'))
1685 {
1686 /*
1687 Tag.
1688 */
1689 if (root->node == (XMLTreeInfo *) NULL)
1690 {
1691 (void) ThrowMagickException(exception,GetMagickModule(),
1692 OptionWarning,"ParseError","root tag missing");
1693 utf8=DestroyString(utf8);
1694 return(&root->root);
1695 }
1696 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1697 while (isspace((int) ((unsigned char) *p)) != 0)
1698 *p++='\0';
1699 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1700 (ignore_depth == 0))
1701 {
1702 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1703 {
1704 /*
1705 Find tag in default attributes list.
1706 */
1707 i=0;
1708 while ((root->attributes[i] != (char **) NULL) &&
1709 (strcmp(root->attributes[i][0],tag) != 0))
1710 i++;
1711 attribute=root->attributes[i];
1712 }
1713 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1714 {
1715 /*
1716 Attribute.
1717 */
1718 if (l == 0)
1719 attributes=(char **) AcquireQuantumMemory(4,
1720 sizeof(*attributes));
1721 else
1722 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1723 (l+4),sizeof(*attributes));
1724 if (attributes == (char **) NULL)
1725 {
1726 (void) ThrowMagickException(exception,GetMagickModule(),
1727 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1728 utf8=DestroyString(utf8);
1729 return(&root->root);
1730 }
1731 attributes[l+2]=(char *) NULL;
1732 attributes[l+1]=(char *) NULL;
1733 attributes[l]=p;
1734 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
1735 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1736 attributes[l]=ConstantString("");
1737 else
1738 {
1739 *p++='\0';
1740 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
1741 c=(*p);
1742 if ((c == '"') || (c == '\''))
1743 {
1744 /*
1745 Attributes value.
1746 */
1747 p++;
1748 attributes[l+1]=p;
1749 while ((*p != '\0') && (*p != c))
1750 p++;
1751 if (*p != '\0')
1752 *p++='\0';
1753 else
1754 {
1755 attributes[l]=ConstantString("");
1756 attributes[l+1]=ConstantString("");
1757 (void) DestroyXMLTreeAttributes(attributes);
1758 (void) ThrowMagickException(exception,
1759 GetMagickModule(),OptionWarning,"ParseError",
1760 "missing %c",c);
1761 utf8=DestroyString(utf8);
1762 return(&root->root);
1763 }
1764 j=1;
1765 while ((attribute != (char **) NULL) &&
1766 (attribute[j] != (char *) NULL) &&
1767 (strcmp(attribute[j],attributes[l]) != 0))
1768 j+=3;
1769 attributes[l+1]=ParseEntities(attributes[l+1],
1770 root->entities,(attribute != (char **) NULL) &&
1771 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
1772 ' ');
1773 }
1774 attributes[l]=ConstantString(attributes[l]);
1775 }
1776 while (isspace((int) ((unsigned char) *p)) != 0)
1777 p++;
1778 }
1779 }
1780 else
1781 {
1782 while ((*p != '\0') && (*p != '/') && (*p != '>'))
1783 p++;
1784 }
1785 if (*p == '/')
1786 {
1787 /*
1788 Self closing tag.
1789 */
1790 *p++='\0';
1791 if (((*p != '\0') && (*p != '>')) ||
1792 ((*p == '\0') && (terminal != '>')))
1793 {
1794 if (l != 0)
1795 (void) DestroyXMLTreeAttributes(attributes);
1796 (void) ThrowMagickException(exception,GetMagickModule(),
1797 OptionWarning,"ParseError","missing >");
1798 utf8=DestroyString(utf8);
1799 return(&root->root);
1800 }
1801 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1802 (void) DestroyXMLTreeAttributes(attributes);
1803 else
1804 {
1805 ParseOpenTag(root,tag,attributes);
1806 (void) ParseCloseTag(root,tag,exception);
1807 }
1808 }
1809 else
1810 {
1811 c=(*p);
1812 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1813 {
1814 *p='\0';
1815 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1816 ParseOpenTag(root,tag,attributes);
1817 else
1818 {
1819 ignore_depth++;
1820 (void) DestroyXMLTreeAttributes(attributes);
1821 }
1822 *p=c;
1823 }
1824 else
1825 {
1826 if (l != 0)
1827 (void) DestroyXMLTreeAttributes(attributes);
1828 (void) ThrowMagickException(exception,GetMagickModule(),
1829 OptionWarning,"ParseError","missing >");
1830 utf8=DestroyString(utf8);
1831 return(&root->root);
1832 }
1833 }
1834 }
1835 else
1836 if (*p == '/')
1837 {
1838 /*
1839 Close tag.
1840 */
1841 tag=p+1;
1842 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
1843 c=(*p);
1844 if ((c == '\0') && (terminal != '>'))
1845 {
1846 (void) ThrowMagickException(exception,GetMagickModule(),
1847 OptionWarning,"ParseError","missing >");
1848 utf8=DestroyString(utf8);
1849 return(&root->root);
1850 }
1851 *p='\0';
1852 if ((ignore_depth == 0) &&
1853 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1854 {
1855 utf8=DestroyString(utf8);
1856 return(&root->root);
1857 }
1858 if (ignore_depth > 0)
1859 ignore_depth--;
1860 *p=c;
1861 if (isspace((int) ((unsigned char) *p)) != 0)
1862 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1863 }
1864 else
1865 if (strncmp(p,"!--",3) == 0)
1866 {
1867 /*
1868 Comment.
1869 */
1870 p=strstr(p+3,"--");
1871 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1872 ((*p == '\0') && (terminal != '>')))
1873 {
1874 (void) ThrowMagickException(exception,GetMagickModule(),
1875 OptionWarning,"ParseError","unclosed <!--");
1876 utf8=DestroyString(utf8);
1877 return(&root->root);
1878 }
1879 }
1880 else
1881 if (strncmp(p,"![CDATA[",8) == 0)
1882 {
1883 /*
1884 Cdata.
1885 */
1886 p=strstr(p,"]]>");
1887 if (p != (char *) NULL)
1888 {
1889 p+=(ptrdiff_t) 2;
1890 if (ignore_depth == 0)
1891 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1892 }
1893 else
1894 {
1895 (void) ThrowMagickException(exception,GetMagickModule(),
1896 OptionWarning,"ParseError","unclosed <![CDATA[");
1897 utf8=DestroyString(utf8);
1898 return(&root->root);
1899 }
1900 }
1901 else
1902 if (strncmp(p,"!DOCTYPE",8) == 0)
1903 {
1904 /*
1905 DTD.
1906 */
1907 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1908 ((l != 0) && ((*p != ']') ||
1909 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1910 l=(ssize_t) ((*p == '[') ? 1 : l))
1911 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
1912 if ((*p == '\0') && (terminal != '>'))
1913 {
1914 (void) ThrowMagickException(exception,GetMagickModule(),
1915 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1916 utf8=DestroyString(utf8);
1917 return(&root->root);
1918 }
1919 if (l != 0)
1920 tag=strchr(tag,'[')+1;
1921 if (l != 0)
1922 {
1923 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1924 exception);
1925 if (status == MagickFalse)
1926 {
1927 utf8=DestroyString(utf8);
1928 return(&root->root);
1929 }
1930 p++;
1931 }
1932 }
1933 else
1934 if (*p == '?')
1935 {
1936 /*
1937 Processing instructions.
1938 */
1939 do
1940 {
1941 p=strchr(p,'?');
1942 if (p == (char *) NULL)
1943 break;
1944 p++;
1945 } while ((*p != '\0') && (*p != '>'));
1946 if ((p == (char *) NULL) || ((*p == '\0') &&
1947 (terminal != '>')))
1948 {
1949 (void) ThrowMagickException(exception,GetMagickModule(),
1950 OptionWarning,"ParseError","unclosed <?");
1951 utf8=DestroyString(utf8);
1952 return(&root->root);
1953 }
1954 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
1955 }
1956 else
1957 {
1958 (void) ThrowMagickException(exception,GetMagickModule(),
1959 OptionWarning,"ParseError","unexpected <");
1960 utf8=DestroyString(utf8);
1961 return(&root->root);
1962 }
1963 if ((p == (char *) NULL) || (*p == '\0'))
1964 break;
1965 *p++='\0';
1966 tag=p;
1967 if ((*p != '\0') && (*p != '<'))
1968 {
1969 /*
1970 Tag character content.
1971 */
1972 while ((*p != '\0') && (*p != '<'))
1973 p++;
1974 if (*p == '\0')
1975 break;
1976 if (ignore_depth == 0)
1977 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
1978 }
1979 else
1980 if (*p == '\0')
1981 break;
1982 }
1983 utf8=DestroyString(utf8);
1984 if (root->node == (XMLTreeInfo *) NULL)
1985 return(&root->root);
1986 if (root->node->tag == (char *) NULL)
1987 {
1988 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1989 "ParseError","root tag missing");
1990 return(&root->root);
1991 }
1992 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1993 "ParseError","unclosed tag: '%s'",root->node->tag);
1994 return(&root->root);
1995}
1996␌
1997/*
1998%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1999% %
2000% %
2001% %
2002% N e w X M L T r e e T a g %
2003% %
2004% %
2005% %
2006%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2007%
2008% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2009%
2010% The format of the NewXMLTreeTag method is:
2011%
2012% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2013%
2014% A description of each parameter follows:
2015%
2016% o tag: the tag.
2017%
2018*/
2019MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2020{
2021 static const char
2022 *predefined_entities[NumberPredefinedEntities+1] =
2023 {
2024 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2025 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2026 };
2027
2029 *root;
2030
2031 root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
2032 if (root == (XMLTreeRoot *) NULL)
2033 return((XMLTreeInfo *) NULL);
2034 (void) memset(root,0,sizeof(*root));
2035 root->root.tag=(char *) NULL;
2036 if (tag != (char *) NULL)
2037 root->root.tag=ConstantString(tag);
2038 root->node=(&root->root);
2039 root->root.content=ConstantString("");
2040 root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
2041 if (root->entities == (char **) NULL)
2042 return((XMLTreeInfo *) NULL);
2043 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2044 root->root.attributes=sentinel;
2045 root->attributes=(char ***) root->root.attributes;
2046 root->processing_instructions=(char ***) root->root.attributes;
2047 root->debug=IsEventLogging();
2048 root->signature=MagickCoreSignature;
2049 return(&root->root);
2050}
2051␌
2052/*
2053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2054% %
2055% %
2056% %
2057% S e t X M L T r e e C o n t e n t %
2058% %
2059% %
2060% %
2061%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2062%
2063% SetXMLTreeContent() sets the character content for the given tag and
2064% returns the tag.
2065%
2066% The format of the SetXMLTreeContent method is:
2067%
2068% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2069% const char *content)
2070%
2071% A description of each parameter follows:
2072%
2073% o xml_info: the xml info.
2074%
2075% o content: The content.
2076%
2077*/
2078MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2079 const char *content)
2080{
2081 assert(xml_info != (XMLTreeInfo *) NULL);
2082 assert((xml_info->signature == MagickCoreSignature) ||
2083 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2084 if (IsEventLogging() != MagickFalse)
2085 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2086 if (xml_info->content != (char *) NULL)
2087 xml_info->content=DestroyString(xml_info->content);
2088 xml_info->content=(char *) ConstantString(content);
2089 return(xml_info);
2090}
2091␌
2092/*
2093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2094% %
2095% %
2096% %
2097% X M L T r e e I n f o T o X M L %
2098% %
2099% %
2100% %
2101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2102%
2103% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2104%
2105% The format of the XMLTreeInfoToXML method is:
2106%
2107% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2108%
2109% A description of each parameter follows:
2110%
2111% o xml_info: the xml info.
2112%
2113*/
2114
2115static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2116 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2117{
2118 char
2119 *canonical_content;
2120
2121 if (offset < 0)
2122 canonical_content=CanonicalXMLContent(source,pedantic);
2123 else
2124 {
2125 char
2126 *content;
2127
2128 content=AcquireString(source);
2129 content[offset]='\0';
2130 canonical_content=CanonicalXMLContent(content,pedantic);
2131 content=DestroyString(content);
2132 }
2133 if (canonical_content == (char *) NULL)
2134 return(*destination);
2135 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2136 {
2137 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2138 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2139 sizeof(**destination));
2140 if (*destination == (char *) NULL)
2141 return(*destination);
2142 }
2143 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2144 canonical_content);
2145 canonical_content=DestroyString(canonical_content);
2146 return(*destination);
2147}
2148
2149static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2150 size_t *extent,size_t start,char ***attributes)
2151{
2152 char
2153 *content;
2154
2155 const char
2156 *attribute;
2157
2158 size_t
2159 offset;
2160
2161 ssize_t
2162 i,
2163 j;
2164
2165 content=(char *) "";
2166 if (xml_info->parent != (XMLTreeInfo *) NULL)
2167 content=xml_info->parent->content;
2168 offset=0;
2169 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2170 start),source,length,extent,MagickFalse);
2171 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2172 {
2173 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2174 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2175 if (*source == (char *) NULL)
2176 return(*source);
2177 }
2178 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2179 "<%s",xml_info->tag);
2180 for (i=0; xml_info->attributes[i]; i+=2)
2181 {
2182 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2183 if (attribute != xml_info->attributes[i+1])
2184 continue;
2185 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2186 {
2187 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2188 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2189 if (*source == (char *) NULL)
2190 return((char *) NULL);
2191 }
2192 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2193 xml_info->attributes[i]);
2194 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2195 extent,MagickTrue);
2196 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2197 }
2198 i=0;
2199 while ((attributes[i] != (char **) NULL) &&
2200 (strcmp(attributes[i][0],xml_info->tag) != 0))
2201 i++;
2202 j=1;
2203 while ((attributes[i] != (char **) NULL) &&
2204 (attributes[i][j] != (char *) NULL))
2205 {
2206 if ((attributes[i][j+1] == (char *) NULL) ||
2207 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2208 {
2209 j+=3;
2210 continue;
2211 }
2212 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2213 {
2214 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2215 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2216 if (*source == (char *) NULL)
2217 return((char *) NULL);
2218 }
2219 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2220 attributes[i][j]);
2221 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2222 MagickTrue);
2223 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2224 j+=3;
2225 }
2226 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2227 *xml_info->content ? ">" : "/>");
2228 if (xml_info->child != (XMLTreeInfo *) NULL)
2229 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2230 else
2231 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2232 MagickFalse);
2233 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2234 {
2235 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2236 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2237 if (*source == (char *) NULL)
2238 return((char *) NULL);
2239 }
2240 if (*xml_info->content != '\0')
2241 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2242 xml_info->tag);
2243 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2244 offset++;
2245 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2246 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2247 attributes);
2248 else
2249 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2250 MagickFalse);
2251 return(content);
2252}
2253
2254MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2255{
2256 char
2257 *p,
2258 *q,
2259 *xml;
2260
2261 size_t
2262 extent,
2263 length;
2264
2265 ssize_t
2266 i,
2267 j,
2268 k;
2269
2271 *ordered,
2272 *parent;
2273
2275 *root;
2276
2277 assert(xml_info != (XMLTreeInfo *) NULL);
2278 assert((xml_info->signature == MagickCoreSignature) ||
2279 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2280 if (IsEventLogging() != MagickFalse)
2281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2282 if (xml_info->tag == (char *) NULL)
2283 return((char *) NULL);
2284 xml=AcquireString((char *) NULL);
2285 length=0;
2286 extent=MagickPathExtent;
2287 root=(XMLTreeRoot *) xml_info;
2288 while (root->root.parent != (XMLTreeInfo *) NULL)
2289 root=(XMLTreeRoot *) root->root.parent;
2290 parent=xml_info->parent;
2291 if (parent == (XMLTreeInfo *) NULL)
2292 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2293 {
2294 /*
2295 Pre-root processing instructions.
2296 */
2297 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2298 p=root->processing_instructions[i][1];
2299 for (j=1; p != (char *) NULL; j++)
2300 {
2301 if (root->processing_instructions[i][k][j-1] == '>')
2302 {
2303 p=root->processing_instructions[i][j];
2304 continue;
2305 }
2306 q=root->processing_instructions[i][0];
2307 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2308 {
2309 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2310 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2311 if (xml == (char *) NULL)
2312 return(xml);
2313 }
2314 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2315 *p != '\0' ? " " : "",p);
2316 p=root->processing_instructions[i][j];
2317 }
2318 }
2319 ordered=xml_info->ordered;
2320 xml_info->parent=(XMLTreeInfo *) NULL;
2321 xml_info->ordered=(XMLTreeInfo *) NULL;
2322 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2323 xml_info->parent=parent;
2324 xml_info->ordered=ordered;
2325 if (parent == (XMLTreeInfo *) NULL)
2326 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2327 {
2328 /*
2329 Post-root processing instructions.
2330 */
2331 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2332 p=root->processing_instructions[i][1];
2333 for (j=1; p != (char *) NULL; j++)
2334 {
2335 if (root->processing_instructions[i][k][j-1] == '<')
2336 {
2337 p=root->processing_instructions[i][j];
2338 continue;
2339 }
2340 q=root->processing_instructions[i][0];
2341 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2342 {
2343 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2344 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2345 if (xml == (char *) NULL)
2346 return(xml);
2347 }
2348 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2349 *p != '\0' ? " " : "",p);
2350 p=root->processing_instructions[i][j];
2351 }
2352 }
2353 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2354}