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