MagickCore 7.1.2-32
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_CLOEXEC | 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=(int) strtol(xml+2,&entity,10); /* base 10 */
1081 else
1082 c=(int) 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 int delimiter,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 != delimiter))
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],delimiter,depth+1,entities) == MagickFalse))
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 (ValidateEntities(n,entities[i+1],'&',0,entities) != MagickFalse))
1420 entities[i]=n;
1421 else
1422 {
1423 if (entities[i+1] != v)
1424 entities[i+1]=DestroyString(entities[i+1]);
1425 (void) ThrowMagickException(exception,GetMagickModule(),
1426 OptionWarning,"ParseError","circular entity declaration %s",n);
1427 predefined_entities=(char **) RelinquishMagickMemory(
1428 predefined_entities);
1429 return(MagickFalse);
1430 }
1431 }
1432 else
1433 if (strncmp(xml,"<!ATTLIST",9) == 0)
1434 {
1435 /*
1436 Parse default attributes.
1437 */
1438 t=xml+strspn(xml+9,XMLWhitespace)+9;
1439 if (*t == '\0')
1440 {
1441 (void) ThrowMagickException(exception,GetMagickModule(),
1442 OptionWarning,"ParseError","unclosed <!ATTLIST");
1443 predefined_entities=(char **) RelinquishMagickMemory(
1444 predefined_entities);
1445 return(MagickFalse);
1446 }
1447 xml=t+strcspn(t,XMLWhitespace ">");
1448 if (*xml == '>')
1449 continue;
1450 *xml='\0';
1451 i=0;
1452 while ((root->attributes[i] != (char **) NULL) &&
1453 (n != (char *) NULL) &&
1454 (strcmp(n,root->attributes[i][0]) != 0))
1455 i++;
1456 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1457 (*n != '>'))
1458 {
1459 xml=n+strcspn(n,XMLWhitespace);
1460 if (*xml != '\0')
1461 *xml='\0';
1462 else
1463 {
1464 (void) ThrowMagickException(exception,GetMagickModule(),
1465 OptionWarning,"ParseError","malformed <!ATTLIST");
1466 predefined_entities=(char **) RelinquishMagickMemory(
1467 predefined_entities);
1468 return(MagickFalse);
1469 }
1470 xml+=strspn(xml+1,XMLWhitespace)+1;
1471 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1472 if (strncmp(xml,"NOTATION",8) == 0)
1473 xml+=strspn(xml+8,XMLWhitespace)+8;
1474 xml=(*xml == '(') ? strchr(xml,')') : xml+
1475 strcspn(xml,XMLWhitespace);
1476 if (xml == (char *) NULL)
1477 {
1478 (void) ThrowMagickException(exception,GetMagickModule(),
1479 OptionWarning,"ParseError","malformed <!ATTLIST");
1480 predefined_entities=(char **) RelinquishMagickMemory(
1481 predefined_entities);
1482 return(MagickFalse);
1483 }
1484 xml+=strspn(xml,XMLWhitespace ")");
1485 if (strncmp(xml,"#FIXED",6) == 0)
1486 xml+=strspn(xml+6,XMLWhitespace)+6;
1487 if (*xml == '#')
1488 {
1489 xml+=strcspn(xml,XMLWhitespace ">")-1;
1490 if (*c == ' ')
1491 continue;
1492 v=(char *) NULL;
1493 }
1494 else
1495 if (((*xml == '"') || (*xml == '\'')) &&
1496 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1497 *xml='\0';
1498 else
1499 {
1500 (void) ThrowMagickException(exception,GetMagickModule(),
1501 OptionWarning,"ParseError","malformed <!ATTLIST");
1502 predefined_entities=(char **) RelinquishMagickMemory(
1503 predefined_entities);
1504 return(MagickFalse);
1505 }
1506 if (root->attributes[i] == (char **) NULL)
1507 {
1508 /*
1509 New attribute tag.
1510 */
1511 if (i == 0)
1512 root->attributes=(char ***) AcquireQuantumMemory(2,
1513 sizeof(*root->attributes));
1514 else
1515 root->attributes=(char ***) ResizeQuantumMemory(
1516 root->attributes,(size_t) (i+2),
1517 sizeof(*root->attributes));
1518 if (root->attributes == (char ***) NULL)
1519 ThrowFatalException(ResourceLimitFatalError,
1520 "MemoryAllocationFailed");
1521 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1522 sizeof(**root->attributes));
1523 if (root->attributes[i] == (char **) NULL)
1524 ThrowFatalException(ResourceLimitFatalError,
1525 "MemoryAllocationFailed");
1526 root->attributes[i][0]=ConstantString(t);
1527 root->attributes[i][1]=(char *) NULL;
1528 root->attributes[i+1]=(char **) NULL;
1529 }
1530 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1531 root->attributes[i]=(char **) ResizeQuantumMemory(
1532 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1533 if (root->attributes[i] == (char **) NULL)
1534 ThrowFatalException(ResourceLimitFatalError,
1535 "MemoryAllocationFailed");
1536 root->attributes[i][j+3]=(char *) NULL;
1537 root->attributes[i][j+2]=ConstantString(c);
1538 root->attributes[i][j+1]=(char *) NULL;
1539 if (v != (char *) NULL)
1540 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1541 root->attributes[i][j]=ConstantString(n);
1542 }
1543 }
1544 else
1545 if (strncmp(xml, "<!--", 4) == 0)
1546 xml=strstr(xml+4,"-->");
1547 else
1548 if (strncmp(xml,"<?", 2) == 0)
1549 {
1550 c=xml+2;
1551 xml=strstr(c,"?>");
1552 if (xml != (char *) NULL)
1553 {
1554 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1555 xml++;
1556 }
1557 }
1558 else
1559 if (*xml == '<')
1560 xml=strchr(xml,'>');
1561 else
1562 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1563 break;
1564 }
1565 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities);
1566 return(MagickTrue);
1567}
1568
1569static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
1570{
1571 XMLTreeInfo
1572 *xml_info;
1573
1574 xml_info=root->node;
1575 if (xml_info->tag == (char *) NULL)
1576 xml_info->tag=ConstantString(tag);
1577 else
1578 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1579 if (xml_info != (XMLTreeInfo *) NULL)
1580 xml_info->attributes=attributes;
1581 root->node=xml_info;
1582}
1583
1584static const char
1585 *ignore_tags[3] =
1586 {
1587 "rdf:Bag",
1588 "rdf:Seq",
1589 (const char *) NULL
1590 };
1591
1592static inline MagickBooleanType IsSkipTag(const char *tag)
1593{
1594 ssize_t
1595 i;
1596
1597 i=0;
1598 while (ignore_tags[i] != (const char *) NULL)
1599 {
1600 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1601 return(MagickTrue);
1602 i++;
1603 }
1604 return(MagickFalse);
1605}
1606
1607MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1608{
1609 char
1610 **attribute,
1611 **attributes,
1612 *p,
1613 *tag,
1614 *utf8;
1615
1616 int
1617 c,
1618 terminal;
1619
1620 MagickBooleanType
1621 status;
1622
1623 size_t
1624 ignore_depth,
1625 length;
1626
1627 ssize_t
1628 i,
1629 j,
1630 l;
1631
1632 XMLTreeRoot
1633 *root;
1634
1635 /*
1636 Convert xml-string to UTF8.
1637 */
1638 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1639 {
1640 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1641 "ParseError","root tag missing");
1642 return((XMLTreeInfo *) NULL);
1643 }
1644 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1645 length=strlen(xml);
1646 utf8=ConvertUTF16ToUTF8(xml,&length);
1647 if (utf8 == (char *) NULL)
1648 {
1649 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1650 "ParseError","UTF16 to UTF8 failed");
1651 return((XMLTreeInfo *) NULL);
1652 }
1653 if (length == 0)
1654 {
1655 utf8=DestroyString(utf8);
1656 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1657 "ParseError","root tag missing");
1658 return((XMLTreeInfo *) NULL);
1659 }
1660 terminal=utf8[length-1];
1661 utf8[length-1]='\0';
1662 p=utf8;
1663 while ((*p != '\0') && (*p != '<'))
1664 p++;
1665 if (*p == '\0')
1666 {
1667 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1668 "ParseError","root tag missing");
1669 utf8=DestroyString(utf8);
1670 return((XMLTreeInfo *) NULL);
1671 }
1672 attribute=(char **) NULL;
1673 l=0;
1674 ignore_depth=0;
1675 for (p++; ; p++)
1676 {
1677 attributes=(char **) sentinel;
1678 tag=p;
1679 c=(*p);
1680 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1681 (*p == ':') || (c < '\0'))
1682 {
1683 /*
1684 Tag.
1685 */
1686 if (root->node == (XMLTreeInfo *) NULL)
1687 {
1688 (void) ThrowMagickException(exception,GetMagickModule(),
1689 OptionWarning,"ParseError","root tag missing");
1690 utf8=DestroyString(utf8);
1691 return(&root->root);
1692 }
1693 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1694 while (isspace((int) ((unsigned char) *p)) != 0)
1695 *p++='\0';
1696 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1697 (ignore_depth == 0))
1698 {
1699 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1700 {
1701 /*
1702 Find tag in default attributes list.
1703 */
1704 i=0;
1705 while ((root->attributes[i] != (char **) NULL) &&
1706 (strcmp(root->attributes[i][0],tag) != 0))
1707 i++;
1708 attribute=root->attributes[i];
1709 }
1710 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1711 {
1712 /*
1713 Attribute.
1714 */
1715 if (l == 0)
1716 attributes=(char **) AcquireQuantumMemory(4,
1717 sizeof(*attributes));
1718 else
1719 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1720 (l+4),sizeof(*attributes));
1721 if (attributes == (char **) NULL)
1722 {
1723 (void) ThrowMagickException(exception,GetMagickModule(),
1724 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1725 utf8=DestroyString(utf8);
1726 return(&root->root);
1727 }
1728 attributes[l+2]=(char *) NULL;
1729 attributes[l+1]=(char *) NULL;
1730 attributes[l]=p;
1731 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
1732 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1733 attributes[l]=ConstantString("");
1734 else
1735 {
1736 *p++='\0';
1737 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
1738 c=(*p);
1739 if ((c == '"') || (c == '\''))
1740 {
1741 /*
1742 Attributes value.
1743 */
1744 p++;
1745 attributes[l+1]=p;
1746 while ((*p != '\0') && (*p != c))
1747 p++;
1748 if (*p != '\0')
1749 *p++='\0';
1750 else
1751 {
1752 attributes[l]=ConstantString("");
1753 attributes[l+1]=ConstantString("");
1754 (void) DestroyXMLTreeAttributes(attributes);
1755 (void) ThrowMagickException(exception,
1756 GetMagickModule(),OptionWarning,"ParseError",
1757 "missing %c",c);
1758 utf8=DestroyString(utf8);
1759 return(&root->root);
1760 }
1761 j=1;
1762 while ((attribute != (char **) NULL) &&
1763 (attribute[j] != (char *) NULL) &&
1764 (strcmp(attribute[j],attributes[l]) != 0))
1765 j+=3;
1766 attributes[l+1]=ParseEntities(attributes[l+1],
1767 root->entities,(attribute != (char **) NULL) &&
1768 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
1769 ' ');
1770 }
1771 attributes[l]=ConstantString(attributes[l]);
1772 }
1773 while (isspace((int) ((unsigned char) *p)) != 0)
1774 p++;
1775 }
1776 }
1777 else
1778 {
1779 while ((*p != '\0') && (*p != '/') && (*p != '>'))
1780 p++;
1781 }
1782 if (*p == '/')
1783 {
1784 /*
1785 Self closing tag.
1786 */
1787 *p++='\0';
1788 if (((*p != '\0') && (*p != '>')) ||
1789 ((*p == '\0') && (terminal != '>')))
1790 {
1791 if (l != 0)
1792 (void) DestroyXMLTreeAttributes(attributes);
1793 (void) ThrowMagickException(exception,GetMagickModule(),
1794 OptionWarning,"ParseError","missing >");
1795 utf8=DestroyString(utf8);
1796 return(&root->root);
1797 }
1798 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1799 (void) DestroyXMLTreeAttributes(attributes);
1800 else
1801 {
1802 ParseOpenTag(root,tag,attributes);
1803 (void) ParseCloseTag(root,tag,exception);
1804 }
1805 }
1806 else
1807 {
1808 c=(*p);
1809 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1810 {
1811 *p='\0';
1812 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1813 ParseOpenTag(root,tag,attributes);
1814 else
1815 {
1816 ignore_depth++;
1817 (void) DestroyXMLTreeAttributes(attributes);
1818 }
1819 *p=(char) c;
1820 }
1821 else
1822 {
1823 if (l != 0)
1824 (void) DestroyXMLTreeAttributes(attributes);
1825 (void) ThrowMagickException(exception,GetMagickModule(),
1826 OptionWarning,"ParseError","missing >");
1827 utf8=DestroyString(utf8);
1828 return(&root->root);
1829 }
1830 }
1831 }
1832 else
1833 if (*p == '/')
1834 {
1835 /*
1836 Close tag.
1837 */
1838 tag=p+1;
1839 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
1840 c=(*p);
1841 if ((c == '\0') && (terminal != '>'))
1842 {
1843 (void) ThrowMagickException(exception,GetMagickModule(),
1844 OptionWarning,"ParseError","missing >");
1845 utf8=DestroyString(utf8);
1846 return(&root->root);
1847 }
1848 *p='\0';
1849 if ((ignore_depth == 0) &&
1850 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1851 {
1852 utf8=DestroyString(utf8);
1853 return(&root->root);
1854 }
1855 if (ignore_depth > 0)
1856 ignore_depth--;
1857 *p=(char) c;
1858 if (isspace((int) ((unsigned char) *p)) != 0)
1859 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1860 }
1861 else
1862 if (strncmp(p,"!--",3) == 0)
1863 {
1864 /*
1865 Comment.
1866 */
1867 p=strstr(p+3,"--");
1868 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1869 ((*p == '\0') && (terminal != '>')))
1870 {
1871 (void) ThrowMagickException(exception,GetMagickModule(),
1872 OptionWarning,"ParseError","unclosed <!--");
1873 utf8=DestroyString(utf8);
1874 return(&root->root);
1875 }
1876 }
1877 else
1878 if (strncmp(p,"![CDATA[",8) == 0)
1879 {
1880 /*
1881 Cdata.
1882 */
1883 p=strstr(p,"]]>");
1884 if (p != (char *) NULL)
1885 {
1886 p+=(ptrdiff_t) 2;
1887 if (ignore_depth == 0)
1888 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1889 }
1890 else
1891 {
1892 (void) ThrowMagickException(exception,GetMagickModule(),
1893 OptionWarning,"ParseError","unclosed <![CDATA[");
1894 utf8=DestroyString(utf8);
1895 return(&root->root);
1896 }
1897 }
1898 else
1899 if (strncmp(p,"!DOCTYPE",8) == 0)
1900 {
1901 /*
1902 DTD.
1903 */
1904 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1905 ((l != 0) && ((*p != ']') ||
1906 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1907 l=(ssize_t) ((*p == '[') ? 1 : l))
1908 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
1909 if ((*p == '\0') && (terminal != '>'))
1910 {
1911 (void) ThrowMagickException(exception,GetMagickModule(),
1912 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1913 utf8=DestroyString(utf8);
1914 return(&root->root);
1915 }
1916 if (l != 0)
1917 tag=strchr(tag,'[')+1;
1918 if (l != 0)
1919 {
1920 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1921 exception);
1922 if (status == MagickFalse)
1923 {
1924 utf8=DestroyString(utf8);
1925 return(&root->root);
1926 }
1927 p++;
1928 }
1929 }
1930 else
1931 if (*p == '?')
1932 {
1933 /*
1934 Processing instructions.
1935 */
1936 do
1937 {
1938 p=strchr(p,'?');
1939 if (p == (char *) NULL)
1940 break;
1941 p++;
1942 } while ((*p != '\0') && (*p != '>'));
1943 if ((p == (char *) NULL) || ((*p == '\0') &&
1944 (terminal != '>')))
1945 {
1946 (void) ThrowMagickException(exception,GetMagickModule(),
1947 OptionWarning,"ParseError","unclosed <?");
1948 utf8=DestroyString(utf8);
1949 return(&root->root);
1950 }
1951 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
1952 }
1953 else
1954 {
1955 (void) ThrowMagickException(exception,GetMagickModule(),
1956 OptionWarning,"ParseError","unexpected <");
1957 utf8=DestroyString(utf8);
1958 return(&root->root);
1959 }
1960 if ((p == (char *) NULL) || (*p == '\0'))
1961 break;
1962 *p++='\0';
1963 tag=p;
1964 if ((*p != '\0') && (*p != '<'))
1965 {
1966 /*
1967 Tag character content.
1968 */
1969 while ((*p != '\0') && (*p != '<'))
1970 p++;
1971 if (*p == '\0')
1972 break;
1973 if (ignore_depth == 0)
1974 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
1975 }
1976 else
1977 if (*p == '\0')
1978 break;
1979 }
1980 utf8=DestroyString(utf8);
1981 if (root->node == (XMLTreeInfo *) NULL)
1982 return(&root->root);
1983 if (root->node->tag == (char *) NULL)
1984 {
1985 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1986 "ParseError","root tag missing");
1987 return(&root->root);
1988 }
1989 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1990 "ParseError","unclosed tag: '%s'",root->node->tag);
1991 return(&root->root);
1992}
1993
1994/*
1995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1996% %
1997% %
1998% %
1999% N e w X M L T r e e T a g %
2000% %
2001% %
2002% %
2003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004%
2005% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2006%
2007% The format of the NewXMLTreeTag method is:
2008%
2009% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2010%
2011% A description of each parameter follows:
2012%
2013% o tag: the tag.
2014%
2015*/
2016MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2017{
2018 static const char
2019 *predefined_entities[NumberPredefinedEntities+1] =
2020 {
2021 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2022 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2023 };
2024
2025 XMLTreeRoot
2026 *root;
2027
2028 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2029 (void) memset(root,0,sizeof(*root));
2030 root->root.tag=(char *) NULL;
2031 if (tag != (char *) NULL)
2032 root->root.tag=ConstantString(tag);
2033 root->node=(&root->root);
2034 root->root.content=ConstantString("");
2035 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2036 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2037 root->root.attributes=sentinel;
2038 root->attributes=(char ***) root->root.attributes;
2039 root->processing_instructions=(char ***) root->root.attributes;
2040 root->debug=IsEventLogging();
2041 root->signature=MagickCoreSignature;
2042 return(&root->root);
2043}
2044
2045/*
2046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047% %
2048% %
2049% %
2050% S e t X M L T r e e C o n t e n t %
2051% %
2052% %
2053% %
2054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2055%
2056% SetXMLTreeContent() sets the character content for the given tag and
2057% returns the tag.
2058%
2059% The format of the SetXMLTreeContent method is:
2060%
2061% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2062% const char *content)
2063%
2064% A description of each parameter follows:
2065%
2066% o xml_info: the xml info.
2067%
2068% o content: The content.
2069%
2070*/
2071MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2072 const char *content)
2073{
2074 assert(xml_info != (XMLTreeInfo *) NULL);
2075 assert((xml_info->signature == MagickCoreSignature) ||
2076 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2077 if (IsEventLogging() != MagickFalse)
2078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2079 if (xml_info->content != (char *) NULL)
2080 xml_info->content=DestroyString(xml_info->content);
2081 xml_info->content=(char *) ConstantString(content);
2082 return(xml_info);
2083}
2084
2085/*
2086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2087% %
2088% %
2089% %
2090% X M L T r e e I n f o T o X M L %
2091% %
2092% %
2093% %
2094%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2095%
2096% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2097%
2098% The format of the XMLTreeInfoToXML method is:
2099%
2100% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2101%
2102% A description of each parameter follows:
2103%
2104% o xml_info: the xml info.
2105%
2106*/
2107
2108static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2109 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2110{
2111 char
2112 *canonical_content;
2113
2114 if (offset < 0)
2115 canonical_content=CanonicalXMLContent(source,pedantic);
2116 else
2117 {
2118 char
2119 *content;
2120
2121 content=AcquireString(source);
2122 content[offset]='\0';
2123 canonical_content=CanonicalXMLContent(content,pedantic);
2124 content=DestroyString(content);
2125 }
2126 if (canonical_content == (char *) NULL)
2127 return(*destination);
2128 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2129 {
2130 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2131 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2132 sizeof(**destination));
2133 if (*destination == (char *) NULL)
2134 return(*destination);
2135 }
2136 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2137 canonical_content);
2138 canonical_content=DestroyString(canonical_content);
2139 return(*destination);
2140}
2141
2142static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2143 size_t *extent,size_t start,char ***attributes)
2144{
2145 char
2146 *content;
2147
2148 const char
2149 *attribute;
2150
2151 size_t
2152 offset;
2153
2154 ssize_t
2155 i,
2156 j;
2157
2158 content=(char *) "";
2159 if (xml_info->parent != (XMLTreeInfo *) NULL)
2160 content=xml_info->parent->content;
2161 offset=0;
2162 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2163 start),source,length,extent,MagickFalse);
2164 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2165 {
2166 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2167 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2168 if (*source == (char *) NULL)
2169 return(*source);
2170 }
2171 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2172 "<%s",xml_info->tag);
2173 for (i=0; xml_info->attributes[i]; i+=2)
2174 {
2175 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2176 if (attribute != xml_info->attributes[i+1])
2177 continue;
2178 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2179 {
2180 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2181 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2182 if (*source == (char *) NULL)
2183 return((char *) NULL);
2184 }
2185 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2186 xml_info->attributes[i]);
2187 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2188 extent,MagickTrue);
2189 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2190 }
2191 i=0;
2192 while ((attributes[i] != (char **) NULL) &&
2193 (strcmp(attributes[i][0],xml_info->tag) != 0))
2194 i++;
2195 j=1;
2196 while ((attributes[i] != (char **) NULL) &&
2197 (attributes[i][j] != (char *) NULL))
2198 {
2199 if ((attributes[i][j+1] == (char *) NULL) ||
2200 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2201 {
2202 j+=3;
2203 continue;
2204 }
2205 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2206 {
2207 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2208 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2209 if (*source == (char *) NULL)
2210 return((char *) NULL);
2211 }
2212 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2213 attributes[i][j]);
2214 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2215 MagickTrue);
2216 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2217 j+=3;
2218 }
2219 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2220 *xml_info->content ? ">" : "/>");
2221 if (xml_info->child != (XMLTreeInfo *) NULL)
2222 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2223 else
2224 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2225 MagickFalse);
2226 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2227 {
2228 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2229 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2230 if (*source == (char *) NULL)
2231 return((char *) NULL);
2232 }
2233 if (*xml_info->content != '\0')
2234 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2235 xml_info->tag);
2236 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2237 offset++;
2238 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2239 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2240 attributes);
2241 else
2242 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2243 MagickFalse);
2244 return(content);
2245}
2246
2247MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2248{
2249 char
2250 *p,
2251 *q,
2252 *xml;
2253
2254 size_t
2255 extent,
2256 length;
2257
2258 ssize_t
2259 i,
2260 j,
2261 k;
2262
2263 XMLTreeInfo
2264 *ordered,
2265 *parent;
2266
2267 XMLTreeRoot
2268 *root;
2269
2270 assert(xml_info != (XMLTreeInfo *) NULL);
2271 assert((xml_info->signature == MagickCoreSignature) ||
2272 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2273 if (IsEventLogging() != MagickFalse)
2274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2275 if (xml_info->tag == (char *) NULL)
2276 return((char *) NULL);
2277 xml=AcquireString((char *) NULL);
2278 length=0;
2279 extent=MagickPathExtent;
2280 root=(XMLTreeRoot *) xml_info;
2281 while (root->root.parent != (XMLTreeInfo *) NULL)
2282 root=(XMLTreeRoot *) root->root.parent;
2283 parent=xml_info->parent;
2284 if (parent == (XMLTreeInfo *) NULL)
2285 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2286 {
2287 /*
2288 Pre-root processing instructions.
2289 */
2290 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2291 p=root->processing_instructions[i][1];
2292 for (j=1; p != (char *) NULL; j++)
2293 {
2294 if (root->processing_instructions[i][k][j-1] == '>')
2295 {
2296 p=root->processing_instructions[i][j];
2297 continue;
2298 }
2299 q=root->processing_instructions[i][0];
2300 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2301 {
2302 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2303 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2304 if (xml == (char *) NULL)
2305 return(xml);
2306 }
2307 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2308 *p != '\0' ? " " : "",p);
2309 p=root->processing_instructions[i][j];
2310 }
2311 }
2312 ordered=xml_info->ordered;
2313 xml_info->parent=(XMLTreeInfo *) NULL;
2314 xml_info->ordered=(XMLTreeInfo *) NULL;
2315 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2316 xml_info->parent=parent;
2317 xml_info->ordered=ordered;
2318 if (parent == (XMLTreeInfo *) NULL)
2319 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2320 {
2321 /*
2322 Post-root processing instructions.
2323 */
2324 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2325 p=root->processing_instructions[i][1];
2326 for (j=1; p != (char *) NULL; j++)
2327 {
2328 if (root->processing_instructions[i][k][j-1] == '<')
2329 {
2330 p=root->processing_instructions[i][j];
2331 continue;
2332 }
2333 q=root->processing_instructions[i][0];
2334 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2335 {
2336 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2337 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2338 if (xml == (char *) NULL)
2339 return(xml);
2340 }
2341 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2342 *p != '\0' ? " " : "",p);
2343 p=root->processing_instructions[i][j];
2344 }
2345 }
2346 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2347}