MagickCore 7.1.2-33
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 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{
172 XMLTreeInfo
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{
239 XMLTreeInfo
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
342 XMLTreeInfo
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{
430 XMLTreeInfo
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{
446 XMLTreeInfo
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
469 XMLTreeRoot
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_CLOEXEC | 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=MagickRead(file,xml+i,quantum);
612 if (count <= 0)
613 break;
614 if (~((size_t) i) < (quantum+1))
615 {
616 xml=(char *) RelinquishMagickMemory(xml);
617 break;
618 }
619 xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
620 if ((i+(size_t) count) >= extent)
621 break;
622 }
623 if (LocaleCompare(filename,"-") != 0)
624 file=close_utf8(file);
625 if (xml == (char *) NULL)
626 return((char *) NULL);
627 if (file == -1)
628 {
629 xml=(char *) RelinquishMagickMemory(xml);
630 return((char *) NULL);
631 }
632 length=MagickMin(i+(size_t) count,extent);
633 xml[length]='\0';
634 return(xml);
635 }
636 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
637 xml=(char *) NULL;
638 if (~length >= (MagickPathExtent-1))
639 xml=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*xml));
640 if (xml == (char *) NULL)
641 {
642 file=close_utf8(file);
643 return((char *) NULL);
644 }
645 map=MapBlob(file,ReadMode,0,length);
646 if (map != (char *) NULL)
647 {
648 (void) memcpy(xml,map,length);
649 (void) UnmapBlob(map,length);
650 }
651 else
652 {
653 (void) lseek(file,0,SEEK_SET);
654 for (i=0; i < length; i+=(size_t) count)
655 {
656 count=MagickRead(file,xml+i,(size_t) MagickMin(length-i,(size_t)
657 MagickMaxBufferExtent));
658 if (count <= 0)
659 break;
660 }
661 if (i < length)
662 {
663 file=close_utf8(file)-1;
664 xml=(char *) RelinquishMagickMemory(xml);
665 return((char *) NULL);
666 }
667 }
668 xml[length]='\0';
669 if (LocaleCompare(filename,"-") != 0)
670 file=close_utf8(file);
671 if (file == -1)
672 xml=(char *) RelinquishMagickMemory(xml);
673 return(xml);
674}
675␌
676/*
677%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678% %
679% %
680% %
681% G e t N e x t X M L T r e e T a g %
682% %
683% %
684% %
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686%
687% GetNextXMLTreeTag() returns the next tag or NULL if not found.
688%
689% The format of the GetNextXMLTreeTag method is:
690%
691% XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
692%
693% A description of each parameter follows:
694%
695% o xml_info: the xml info.
696%
697*/
698MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
699{
700 assert(xml_info != (XMLTreeInfo *) NULL);
701 assert((xml_info->signature == MagickCoreSignature) ||
702 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
703 if (IsEventLogging() != MagickFalse)
704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
705 return(xml_info->next);
706}
707␌
708/*
709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710% %
711% %
712% %
713% G e t X M L T r e e A t t r i b u t e %
714% %
715% %
716% %
717%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718%
719% GetXMLTreeAttribute() returns the value of the attribute tag with the
720% specified tag if found, otherwise NULL.
721%
722% The format of the GetXMLTreeAttribute method is:
723%
724% const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
725%
726% A description of each parameter follows:
727%
728% o xml_info: the xml info.
729%
730% o tag: the attribute tag.
731%
732*/
733MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
734 const char *tag)
735{
736 ssize_t
737 i,
738 j;
739
740 XMLTreeRoot
741 *root;
742
743 assert(xml_info != (XMLTreeInfo *) NULL);
744 assert((xml_info->signature == MagickCoreSignature) ||
745 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
746 if (IsEventLogging() != MagickFalse)
747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
748 if (xml_info->attributes == (char **) NULL)
749 return((const char *) NULL);
750 i=0;
751 while ((xml_info->attributes[i] != (char *) NULL) &&
752 (strcmp(xml_info->attributes[i],tag) != 0))
753 i+=2;
754 if (xml_info->attributes[i] != (char *) NULL)
755 return(xml_info->attributes[i+1]);
756 root=(XMLTreeRoot*) xml_info;
757 while (root->root.parent != (XMLTreeInfo *) NULL)
758 root=(XMLTreeRoot *) root->root.parent;
759 i=0;
760 while ((root->attributes[i] != (char **) NULL) &&
761 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
762 i++;
763 if (root->attributes[i] == (char **) NULL)
764 return((const char *) NULL);
765 j=1;
766 while ((root->attributes[i][j] != (char *) NULL) &&
767 (strcmp(root->attributes[i][j],tag) != 0))
768 j+=3;
769 if (root->attributes[i][j] == (char *) NULL)
770 return((const char *) NULL);
771 return(root->attributes[i][j+1]);
772}
773␌
774/*
775%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776% %
777% %
778% %
779% G e t X M L T r e e C h i l d %
780% %
781% %
782% %
783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784%
785% GetXMLTreeChild() returns the first child tag with the specified tag if
786% found, otherwise NULL.
787%
788% The format of the GetXMLTreeChild method is:
789%
790% XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
791%
792% A description of each parameter follows:
793%
794% o xml_info: the xml info.
795%
796*/
797MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
798{
799 XMLTreeInfo
800 *child;
801
802 assert(xml_info != (XMLTreeInfo *) NULL);
803 assert((xml_info->signature == MagickCoreSignature) ||
804 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
805 if (IsEventLogging() != MagickFalse)
806 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
807 child=xml_info->child;
808 if (tag != (const char *) NULL)
809 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
810 child=child->sibling;
811 return(child);
812}
813␌
814/*
815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816% %
817% %
818% %
819% G e t X M L T r e e C o n t e n t %
820% %
821% %
822% %
823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824%
825% GetXMLTreeContent() returns any content associated with specified
826% xml-tree node.
827%
828% The format of the GetXMLTreeContent method is:
829%
830% const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
831%
832% A description of each parameter follows:
833%
834% o xml_info: the xml info.
835%
836*/
837MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
838{
839 assert(xml_info != (XMLTreeInfo *) NULL);
840 assert((xml_info->signature == MagickCoreSignature) ||
841 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
842 if (IsEventLogging() != MagickFalse)
843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
844 return(xml_info->content);
845}
846␌
847/*
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849% %
850% %
851% %
852% G e t X M L T r e e S i b l i n g %
853% %
854% %
855% %
856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857%
858% GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
859%
860% The format of the GetXMLTreeSibling method is:
861%
862% XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
863%
864% A description of each parameter follows:
865%
866% o xml_info: the xml info.
867%
868*/
869MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
870{
871 assert(xml_info != (XMLTreeInfo *) NULL);
872 assert((xml_info->signature == MagickCoreSignature) ||
873 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
874 if (IsEventLogging() != MagickFalse)
875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
876 return(xml_info->sibling);
877}
878␌
879/*
880%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881% %
882% %
883% %
884% G e t X M L T r e e T a g %
885% %
886% %
887% %
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889%
890% GetXMLTreeTag() returns the tag associated with specified xml-tree node.
891%
892% The format of the GetXMLTreeTag method is:
893%
894% const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
895%
896% A description of each parameter follows:
897%
898% o xml_info: the xml info.
899%
900*/
901MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
902{
903 assert(xml_info != (XMLTreeInfo *) NULL);
904 assert((xml_info->signature == MagickCoreSignature) ||
905 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
906 if (IsEventLogging() != MagickFalse)
907 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
908 return(xml_info->tag);
909}
910␌
911/*
912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913% %
914% %
915% %
916% N e w X M L T r e e %
917% %
918% %
919% %
920%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921%
922% NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
923% XML string.
924%
925% The format of the NewXMLTree method is:
926%
927% XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
928%
929% A description of each parameter follows:
930%
931% o xml: A null-terminated XML string.
932%
933% o exception: return any errors or warnings in this structure.
934%
935*/
936
937static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
938{
939 char
940 *utf8;
941
942 int
943 bits,
944 byte,
945 c,
946 encoding;
947
948 size_t
949 extent;
950
951 ssize_t
952 i,
953 j;
954
955 utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
956 if (utf8 == (char *) NULL)
957 return((char *) NULL);
958 encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
959 if (encoding == -1)
960 {
961 /*
962 Already UTF-8.
963 */
964 (void) memcpy(utf8,content,*length*sizeof(*utf8));
965 utf8[*length]='\0';
966 return(utf8);
967 }
968 j=0;
969 extent=(*length);
970 for (i=2; i < (ssize_t) (*length-1); i+=2)
971 {
972 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
973 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
974 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
975 {
976 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
977 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
978 (content[i] & 0xff);
979 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
980 }
981 if ((size_t) (j+MagickPathExtent) > extent)
982 {
983 extent=(size_t) j+MagickPathExtent;
984 utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
985 if (utf8 == (char *) NULL)
986 return(utf8);
987 }
988 if (c < 0x80)
989 {
990 utf8[j]=(char) c;
991 j++;
992 continue;
993 }
994 /*
995 Multi-byte UTF-8 sequence.
996 */
997 byte=c;
998 for (bits=0; byte != 0; byte/=2)
999 bits++;
1000 bits=(bits-2)/5;
1001 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1002 while (bits != 0)
1003 {
1004 bits--;
1005 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1006 j++;
1007 }
1008 }
1009 *length=(size_t) j;
1010 utf8=(char *) ResizeQuantumMemory(utf8,(*length+1),sizeof(*utf8));
1011 if (utf8 != (char *) NULL)
1012 utf8[*length]='\0';
1013 return(utf8);
1014}
1015
1016static char *ParseEntities(char *xml,char **entities,int state)
1017{
1018 char
1019 *entity,
1020 *p,
1021 *q;
1022
1023 int
1024 byte,
1025 c;
1026
1027 size_t
1028 extent,
1029 length;
1030
1031 ssize_t
1032 i,
1033 offset;
1034
1035 /*
1036 Normalize line endings.
1037 */
1038 p=xml;
1039 q=xml;
1040 for ( ; *xml != '\0'; xml++)
1041 while (*xml == '\r')
1042 {
1043 *(xml++)='\n';
1044 if (*xml == '\n')
1045 (void) memmove(xml,xml+1,strlen(xml));
1046 }
1047 for (xml=p; ; )
1048 {
1049 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1050 (state != '%')) && (isspace((int) ((unsigned char) *xml)) == 0))
1051 xml++;
1052 if (*xml == '\0')
1053 break;
1054 /*
1055 States include:
1056 '&' for general entity decoding
1057 '%' for parameter entity decoding
1058 'c' for CDATA sections
1059 ' ' for attributes normalization
1060 '*' for non-CDATA attributes normalization
1061 */
1062 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1063 {
1064 /*
1065 Character reference.
1066 */
1067 if (xml[2] != 'x')
1068 c=(int) strtol(xml+2,&entity,10); /* base 10 */
1069 else
1070 c=(int) strtol(xml+3,&entity,16); /* base 16 */
1071 if ((c == 0) || (*entity != ';'))
1072 {
1073 /*
1074 Not a character reference.
1075 */
1076 xml++;
1077 continue;
1078 }
1079 if (c < 0x80)
1080 *(xml++)=(char) c;
1081 else
1082 {
1083 /*
1084 Multi-byte UTF-8 sequence.
1085 */
1086 byte=c;
1087 for (i=0; byte != 0; byte/=2)
1088 i++;
1089 i=(i-2)/5;
1090 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1091 xml++;
1092 while (i != 0)
1093 {
1094 i--;
1095 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1096 xml++;
1097 }
1098 }
1099 (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1100 }
1101 else
1102 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1103 (state == '*'))) || ((state == '%') && (*xml == '%')))
1104 {
1105 /*
1106 Find entity in the list.
1107 */
1108 i=0;
1109 while ((entities[i] != (char *) NULL) &&
1110 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1111 i+=2;
1112 if (entities[i++] == (char *) NULL)
1113 xml++;
1114 else
1115 if (entities[i] != (char *) NULL)
1116 {
1117 /*
1118 Found a match.
1119 */
1120 length=strlen(entities[i]);
1121 entity=strchr(xml,';');
1122 if ((entity != (char *) NULL) &&
1123 ((length-1L) >= (size_t) (entity-xml)))
1124 {
1125 offset=(ssize_t) (xml-p);
1126 extent=((size_t) offset+length+strlen(entity));
1127 if (p != q)
1128 {
1129 p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1130 if (p != (char *) NULL)
1131 p[extent]='\0';
1132 }
1133 else
1134 {
1135 char
1136 *extent_xml;
1137
1138 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1139 sizeof(*extent_xml));
1140 if (extent_xml != (char *) NULL)
1141 {
1142 memset(extent_xml,0,extent*sizeof(*extent_xml));
1143 (void) CopyMagickString(extent_xml,p,extent*
1144 sizeof(*extent_xml));
1145 }
1146 p=extent_xml;
1147 }
1148 if (p == (char *) NULL)
1149 ThrowFatalException(ResourceLimitFatalError,
1150 "MemoryAllocationFailed");
1151 xml=p+offset;
1152 entity=strchr(xml,';');
1153 }
1154 if (entity != (char *) NULL)
1155 (void) memmove(xml+length,entity+1,strlen(entity));
1156 (void) memcpy(xml,entities[i],length);
1157 }
1158 }
1159 else
1160 if (((state == ' ') || (state == '*')) &&
1161 (isspace((int) ((unsigned char) *xml)) != 0))
1162 *(xml++)=' ';
1163 else
1164 xml++;
1165 }
1166 if (state == '*')
1167 {
1168 /*
1169 Normalize spaces for non-CDATA attributes.
1170 */
1171 for (xml=p; *xml != '\0'; xml++)
1172 {
1173 char
1174 accept[] = " ";
1175
1176 i=(ssize_t) strspn(xml,accept);
1177 if (i != 0)
1178 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1179 while ((*xml != '\0') && (*xml != ' '))
1180 xml++;
1181 if (*xml == '\0')
1182 break;
1183 }
1184 xml--;
1185 if ((xml >= p) && (*xml == ' '))
1186 *xml='\0';
1187 }
1188 return(p == q ? ConstantString(p) : p);
1189}
1190
1191static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1192 const size_t length,const char state)
1193{
1194 XMLTreeInfo
1195 *xml_info;
1196
1197 xml_info=root->node;
1198 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1199 (length == 0))
1200 return;
1201 xml[length]='\0';
1202 xml=ParseEntities(xml,root->entities,state);
1203 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1204 {
1205 (void) ConcatenateString(&xml_info->content,xml);
1206 xml=DestroyString(xml);
1207 }
1208 else
1209 {
1210 if (xml_info->content != (char *) NULL)
1211 xml_info->content=DestroyString(xml_info->content);
1212 xml_info->content=xml;
1213 }
1214}
1215
1216static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1217 ExceptionInfo *exception)
1218{
1219 if ((root->node == (XMLTreeInfo *) NULL) ||
1220 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1221 {
1222 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1223 "ParseError","unexpected closing tag </%s>",tag);
1224 return(&root->root);
1225 }
1226 root->node=root->node->parent;
1227 root->depth--;
1228 return((XMLTreeInfo *) NULL);
1229}
1230
1231static MagickBooleanType ValidateEntities(char *tag,char *xml,
1232 const int delimiter,const size_t depth,char **entities)
1233{
1234 ssize_t
1235 i;
1236
1237 /*
1238 Check for circular entity references.
1239 */
1240 if (depth >= MagickMaxRecursionDepth)
1241 return(MagickFalse);
1242 for ( ; ; xml++)
1243 {
1244 while ((*xml != '\0') && (*xml != delimiter))
1245 xml++;
1246 if (*xml == '\0')
1247 return(MagickTrue);
1248 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1249 return(MagickFalse);
1250 i=0;
1251 while ((entities[i] != (char *) NULL) &&
1252 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1253 i+=2;
1254 if ((entities[i] != (char *) NULL) &&
1255 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1256 return(MagickFalse);
1257 }
1258}
1259
1260static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1261 size_t length)
1262{
1263 char
1264 *target;
1265
1266 ssize_t
1267 i,
1268 j;
1269
1270 target=xml;
1271 xml[length]='\0';
1272 xml+=strcspn(xml,XMLWhitespace);
1273 if (*xml != '\0')
1274 {
1275 *xml='\0';
1276 xml+=strspn(xml+1,XMLWhitespace)+1;
1277 }
1278 if (strcmp(target,"xml") == 0)
1279 {
1280 xml=strstr(xml,"standalone");
1281 if ((xml != (char *) NULL) &&
1282 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1283 root->standalone=MagickTrue;
1284 return;
1285 }
1286 if (root->processing_instructions[0] == (char **) NULL)
1287 {
1288 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1289 *root->processing_instructions));
1290 *root->processing_instructions=(char **) NULL;
1291 }
1292 i=0;
1293 while ((root->processing_instructions[i] != (char **) NULL) &&
1294 (strcmp(target,root->processing_instructions[i][0]) != 0))
1295 i++;
1296 if (root->processing_instructions[i] == (char **) NULL)
1297 {
1298 root->processing_instructions=(char ***) ResizeQuantumMemory(
1299 root->processing_instructions,(size_t) (i+2),
1300 sizeof(*root->processing_instructions));
1301 if (root->processing_instructions == (char ***) NULL)
1302 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1303 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1304 sizeof(**root->processing_instructions));
1305 if (root->processing_instructions[i] == (char **) NULL)
1306 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1307 root->processing_instructions[i+1]=(char **) NULL;
1308 root->processing_instructions[i][0]=ConstantString(target);
1309 root->processing_instructions[i][1]=(char *)
1310 root->processing_instructions[i+1];
1311 root->processing_instructions[i+1]=(char **) NULL;
1312 root->processing_instructions[i][2]=ConstantString("");
1313 }
1314 j=1;
1315 while (root->processing_instructions[i][j] != (char *) NULL)
1316 j++;
1317 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1318 root->processing_instructions[i],(size_t) (j+3),
1319 sizeof(**root->processing_instructions));
1320 if (root->processing_instructions[i] == (char **) NULL)
1321 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1322 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1323 root->processing_instructions[i][j+1],(size_t) (j+1),
1324 sizeof(***root->processing_instructions));
1325 if (root->processing_instructions[i][j+2] == (char *) NULL)
1326 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1327 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1328 root->root.tag != (char *) NULL ? ">" : "<",2);
1329 root->processing_instructions[i][j]=ConstantString(xml);
1330 root->processing_instructions[i][j+1]=(char *) NULL;
1331}
1332
1333static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1334 size_t length,ExceptionInfo *exception)
1335{
1336#define DestroyXMLEntities(predefined_entities) \
1337{ \
1338 ssize_t k; \
1339 for (k=0; predefined_entities[k] != (char *) NULL; k++) \
1340 if ((k & 0x01) != 0) \
1341 predefined_entities[k]=DestroyString(predefined_entities[k]); \
1342 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities); \
1343}
1344
1345 char
1346 *c,
1347 **entities,
1348 *n,
1349 **predefined_entities,
1350 q,
1351 *t,
1352 *v;
1353
1354 ssize_t
1355 i,
1356 j;
1357
1358 n=(char *) NULL;
1359 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1360 if (predefined_entities == (char **) NULL)
1361 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1362 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1363 for (xml[length]='\0'; xml != (char *) NULL; )
1364 {
1365 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1366 xml++;
1367 if (*xml == '\0')
1368 break;
1369 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1370 {
1371 /*
1372 Parse entity definitions.
1373 */
1374 if (strspn(xml+8,XMLWhitespace) == 0)
1375 break;
1376 xml+=strspn(xml+8,XMLWhitespace)+8;
1377 c=xml;
1378 n=xml+strspn(xml,XMLWhitespace "%");
1379 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1380 break;
1381 xml=n+strcspn(n,XMLWhitespace);
1382 if (*xml == '\0')
1383 break;
1384 *xml=';';
1385 v=xml+strspn(xml+1,XMLWhitespace)+1;
1386 q=(*v);
1387 v++;
1388 if ((q != '"') && (q != '\''))
1389 {
1390 /*
1391 Skip externals.
1392 */
1393 xml=strchr(xml,'>');
1394 continue;
1395 }
1396 entities=(*c == '%') ? predefined_entities : root->entities;
1397 for (i=0; entities[i] != (char *) NULL; i++) ;
1398 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1399 sizeof(*entities));
1400 if (entities == (char **) NULL)
1401 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1402 if (*c == '%')
1403 predefined_entities=entities;
1404 else
1405 root->entities=entities;
1406 xml++;
1407 *xml='\0';
1408 xml=strchr(v,q);
1409 if (xml != (char *) NULL)
1410 {
1411 *xml='\0';
1412 xml++;
1413 }
1414 entities[i+1]=ParseEntities(v,predefined_entities,'%');
1415 entities[i+2]=(char *) NULL;
1416 if ((ValidateEntities(n,entities[i+1],'%',0,entities) != MagickFalse) &&
1417 (ValidateEntities(n,entities[i+1],'&',0,entities) != MagickFalse))
1418 entities[i]=n;
1419 else
1420 {
1421 if (entities[i+1] != v)
1422 entities[i+1]=DestroyString(entities[i+1]);
1423 (void) ThrowMagickException(exception,GetMagickModule(),
1424 OptionWarning,"ParseError","circular entity declaration %s",n);
1425 DestroyXMLEntities(predefined_entities);
1426 return(MagickFalse);
1427 }
1428 }
1429 else
1430 if (strncmp(xml,"<!ATTLIST",9) == 0)
1431 {
1432 /*
1433 Parse default attributes.
1434 */
1435 t=xml+strspn(xml+9,XMLWhitespace)+9;
1436 if (*t == '\0')
1437 {
1438 (void) ThrowMagickException(exception,GetMagickModule(),
1439 OptionWarning,"ParseError","unclosed <!ATTLIST");
1440 DestroyXMLEntities(predefined_entities);
1441 return(MagickFalse);
1442 }
1443 xml=t+strcspn(t,XMLWhitespace ">");
1444 if (*xml == '>')
1445 continue;
1446 *xml='\0';
1447 i=0;
1448 while ((root->attributes[i] != (char **) NULL) &&
1449 (n != (char *) NULL) &&
1450 (strcmp(n,root->attributes[i][0]) != 0))
1451 i++;
1452 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1453 (*n != '>'))
1454 {
1455 xml=n+strcspn(n,XMLWhitespace);
1456 if (*xml != '\0')
1457 *xml='\0';
1458 else
1459 {
1460 (void) ThrowMagickException(exception,GetMagickModule(),
1461 OptionWarning,"ParseError","malformed <!ATTLIST");
1462 DestroyXMLEntities(predefined_entities);
1463 return(MagickFalse);
1464 }
1465 xml+=strspn(xml+1,XMLWhitespace)+1;
1466 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1467 if (strncmp(xml,"NOTATION",8) == 0)
1468 xml+=strspn(xml+8,XMLWhitespace)+8;
1469 xml=(*xml == '(') ? strchr(xml,')') : xml+
1470 strcspn(xml,XMLWhitespace);
1471 if (xml == (char *) NULL)
1472 {
1473 (void) ThrowMagickException(exception,GetMagickModule(),
1474 OptionWarning,"ParseError","malformed <!ATTLIST");
1475 DestroyXMLEntities(predefined_entities);
1476 return(MagickFalse);
1477 }
1478 xml+=strspn(xml,XMLWhitespace ")");
1479 if (strncmp(xml,"#FIXED",6) == 0)
1480 xml+=strspn(xml+6,XMLWhitespace)+6;
1481 if (*xml == '#')
1482 {
1483 xml+=strcspn(xml,XMLWhitespace ">")-1;
1484 if (*c == ' ')
1485 continue;
1486 v=(char *) NULL;
1487 }
1488 else
1489 if (((*xml == '"') || (*xml == '\'')) &&
1490 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1491 *xml='\0';
1492 else
1493 {
1494 (void) ThrowMagickException(exception,GetMagickModule(),
1495 OptionWarning,"ParseError","malformed <!ATTLIST");
1496 DestroyXMLEntities(predefined_entities);
1497 return(MagickFalse);
1498 }
1499 if (root->attributes[i] == (char **) NULL)
1500 {
1501 /*
1502 New attribute tag.
1503 */
1504 if (i == 0)
1505 root->attributes=(char ***) AcquireQuantumMemory(2,
1506 sizeof(*root->attributes));
1507 else
1508 root->attributes=(char ***) ResizeQuantumMemory(
1509 root->attributes,(size_t) (i+2),
1510 sizeof(*root->attributes));
1511 if (root->attributes == (char ***) NULL)
1512 ThrowFatalException(ResourceLimitFatalError,
1513 "MemoryAllocationFailed");
1514 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1515 sizeof(**root->attributes));
1516 if (root->attributes[i] == (char **) NULL)
1517 ThrowFatalException(ResourceLimitFatalError,
1518 "MemoryAllocationFailed");
1519 root->attributes[i][0]=ConstantString(t);
1520 root->attributes[i][1]=(char *) NULL;
1521 root->attributes[i+1]=(char **) NULL;
1522 }
1523 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1524 root->attributes[i]=(char **) ResizeQuantumMemory(
1525 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1526 if (root->attributes[i] == (char **) NULL)
1527 ThrowFatalException(ResourceLimitFatalError,
1528 "MemoryAllocationFailed");
1529 root->attributes[i][j+3]=(char *) NULL;
1530 root->attributes[i][j+2]=ConstantString(c);
1531 root->attributes[i][j+1]=(char *) NULL;
1532 if (v != (char *) NULL)
1533 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1534 root->attributes[i][j]=ConstantString(n);
1535 }
1536 }
1537 else
1538 if (strncmp(xml, "<!--", 4) == 0)
1539 xml=strstr(xml+4,"-->");
1540 else
1541 if (strncmp(xml,"<?", 2) == 0)
1542 {
1543 c=xml+2;
1544 xml=strstr(c,"?>");
1545 if (xml != (char *) NULL)
1546 {
1547 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1548 xml++;
1549 }
1550 }
1551 else
1552 if (*xml == '<')
1553 xml=strchr(xml,'>');
1554 else
1555 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1556 break;
1557 }
1558 DestroyXMLEntities(predefined_entities);
1559 return(MagickTrue);
1560}
1561
1562static MagickBooleanType ParseOpenTag(XMLTreeRoot *root,char *tag,
1563 char **attributes,ExceptionInfo *exception)
1564{
1565 XMLTreeInfo
1566 *xml_info;
1567
1568 if (root->depth >= MagickMaxRecursionDepth)
1569 {
1570 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1571 "ParseError","unexpected open tag </%s>",tag);
1572 return(MagickFalse);
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 root->depth++;
1583 return(MagickTrue);
1584}
1585
1586static const char
1587 *ignore_tags[3] =
1588 {
1589 "rdf:Bag",
1590 "rdf:Seq",
1591 (const char *) NULL
1592 };
1593
1594static inline MagickBooleanType IsSkipTag(const char *tag)
1595{
1596 ssize_t
1597 i;
1598
1599 i=0;
1600 while (ignore_tags[i] != (const char *) NULL)
1601 {
1602 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1603 return(MagickTrue);
1604 i++;
1605 }
1606 return(MagickFalse);
1607}
1608
1609MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1610{
1611 char
1612 **attribute,
1613 **attributes,
1614 *p,
1615 *tag,
1616 *utf8;
1617
1618 int
1619 c,
1620 terminal;
1621
1622 MagickBooleanType
1623 status;
1624
1625 size_t
1626 ignore_depth,
1627 length;
1628
1629 ssize_t
1630 i,
1631 j,
1632 l;
1633
1634 XMLTreeRoot
1635 *root;
1636
1637 /*
1638 Convert xml-string to UTF8.
1639 */
1640 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1641 {
1642 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1643 "ParseError","root tag missing");
1644 return((XMLTreeInfo *) NULL);
1645 }
1646 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1647 length=strlen(xml);
1648 utf8=ConvertUTF16ToUTF8(xml,&length);
1649 if (utf8 == (char *) NULL)
1650 {
1651 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1652 "ParseError","UTF16 to UTF8 failed");
1653 return((XMLTreeInfo *) NULL);
1654 }
1655 if (length == 0)
1656 {
1657 utf8=DestroyString(utf8);
1658 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1659 "ParseError","root tag missing");
1660 return((XMLTreeInfo *) NULL);
1661 }
1662 terminal=utf8[length-1];
1663 utf8[length-1]='\0';
1664 p=utf8;
1665 while ((*p != '\0') && (*p != '<'))
1666 p++;
1667 if (*p == '\0')
1668 {
1669 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1670 "ParseError","root tag missing");
1671 utf8=DestroyString(utf8);
1672 return((XMLTreeInfo *) NULL);
1673 }
1674 attribute=(char **) NULL;
1675 l=0;
1676 ignore_depth=0;
1677 for (p++; ; p++)
1678 {
1679 attributes=(char **) sentinel;
1680 tag=p;
1681 c=(*p);
1682 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1683 (*p == ':') || (c < '\0'))
1684 {
1685 /*
1686 Tag.
1687 */
1688 if (root->node == (XMLTreeInfo *) NULL)
1689 {
1690 (void) ThrowMagickException(exception,GetMagickModule(),
1691 OptionWarning,"ParseError","root tag missing");
1692 utf8=DestroyString(utf8);
1693 return(&root->root);
1694 }
1695 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1696 while (isspace((int) ((unsigned char) *p)) != 0)
1697 *p++='\0';
1698 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1699 (ignore_depth == 0))
1700 {
1701 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1702 {
1703 /*
1704 Find tag in default attributes list.
1705 */
1706 i=0;
1707 while ((root->attributes[i] != (char **) NULL) &&
1708 (strcmp(root->attributes[i][0],tag) != 0))
1709 i++;
1710 attribute=root->attributes[i];
1711 }
1712 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1713 {
1714 /*
1715 Attribute.
1716 */
1717 if (l == 0)
1718 attributes=(char **) AcquireQuantumMemory(4,
1719 sizeof(*attributes));
1720 else
1721 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1722 (l+4),sizeof(*attributes));
1723 if (attributes == (char **) NULL)
1724 {
1725 (void) ThrowMagickException(exception,GetMagickModule(),
1726 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1727 utf8=DestroyString(utf8);
1728 return(&root->root);
1729 }
1730 attributes[l+2]=(char *) NULL;
1731 attributes[l+1]=(char *) NULL;
1732 attributes[l]=p;
1733 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
1734 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1735 attributes[l]=ConstantString("");
1736 else
1737 {
1738 *p++='\0';
1739 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
1740 c=(*p);
1741 if ((c == '"') || (c == '\''))
1742 {
1743 /*
1744 Attributes value.
1745 */
1746 p++;
1747 attributes[l+1]=p;
1748 while ((*p != '\0') && (*p != c))
1749 p++;
1750 if (*p != '\0')
1751 *p++='\0';
1752 else
1753 {
1754 attributes[l]=ConstantString("");
1755 attributes[l+1]=ConstantString("");
1756 (void) DestroyXMLTreeAttributes(attributes);
1757 (void) ThrowMagickException(exception,
1758 GetMagickModule(),OptionWarning,"ParseError",
1759 "missing %c",c);
1760 utf8=DestroyString(utf8);
1761 return(&root->root);
1762 }
1763 j=1;
1764 while ((attribute != (char **) NULL) &&
1765 (attribute[j] != (char *) NULL) &&
1766 (strcmp(attribute[j],attributes[l]) != 0))
1767 j+=3;
1768 attributes[l+1]=ParseEntities(attributes[l+1],
1769 root->entities,(attribute != (char **) NULL) &&
1770 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
1771 ' ');
1772 }
1773 attributes[l]=ConstantString(attributes[l]);
1774 }
1775 while (isspace((int) ((unsigned char) *p)) != 0)
1776 p++;
1777 }
1778 }
1779 else
1780 {
1781 while ((*p != '\0') && (*p != '/') && (*p != '>'))
1782 p++;
1783 }
1784 if (*p == '/')
1785 {
1786 /*
1787 Self closing tag.
1788 */
1789 *p++='\0';
1790 if (((*p != '\0') && (*p != '>')) ||
1791 ((*p == '\0') && (terminal != '>')))
1792 {
1793 if (l != 0)
1794 (void) DestroyXMLTreeAttributes(attributes);
1795 (void) ThrowMagickException(exception,GetMagickModule(),
1796 OptionWarning,"ParseError","missing >");
1797 utf8=DestroyString(utf8);
1798 return(&root->root);
1799 }
1800 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1801 (void) DestroyXMLTreeAttributes(attributes);
1802 else
1803 {
1804 status=ParseOpenTag(root,tag,attributes,exception);
1805 if (status == MagickFalse)
1806 {
1807 if (l != 0)
1808 (void) DestroyXMLTreeAttributes(attributes);
1809 utf8=DestroyString(utf8);
1810 return(&root->root);
1811 }
1812 (void) ParseCloseTag(root,tag,exception);
1813 }
1814 }
1815 else
1816 {
1817 c=(*p);
1818 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1819 {
1820 *p='\0';
1821 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1822 {
1823 status=ParseOpenTag(root,tag,attributes,exception);
1824 if (status == MagickFalse)
1825 {
1826 if (l != 0)
1827 (void) DestroyXMLTreeAttributes(attributes);
1828 utf8=DestroyString(utf8);
1829 return(&root->root);
1830 }
1831 }
1832 else
1833 {
1834 ignore_depth++;
1835 (void) DestroyXMLTreeAttributes(attributes);
1836 }
1837 *p=(char) c;
1838 }
1839 else
1840 {
1841 if (l != 0)
1842 (void) DestroyXMLTreeAttributes(attributes);
1843 (void) ThrowMagickException(exception,GetMagickModule(),
1844 OptionWarning,"ParseError","missing >");
1845 utf8=DestroyString(utf8);
1846 return(&root->root);
1847 }
1848 }
1849 }
1850 else
1851 if (*p == '/')
1852 {
1853 /*
1854 Close tag.
1855 */
1856 tag=p+1;
1857 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
1858 c=(*p);
1859 if ((c == '\0') && (terminal != '>'))
1860 {
1861 (void) ThrowMagickException(exception,GetMagickModule(),
1862 OptionWarning,"ParseError","missing >");
1863 utf8=DestroyString(utf8);
1864 return(&root->root);
1865 }
1866 *p='\0';
1867 if ((ignore_depth == 0) &&
1868 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1869 {
1870 utf8=DestroyString(utf8);
1871 return(&root->root);
1872 }
1873 if (ignore_depth > 0)
1874 ignore_depth--;
1875 *p=(char) c;
1876 if (isspace((int) ((unsigned char) *p)) != 0)
1877 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1878 }
1879 else
1880 if (strncmp(p,"!--",3) == 0)
1881 {
1882 /*
1883 Comment.
1884 */
1885 p=strstr(p+3,"--");
1886 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1887 ((*p == '\0') && (terminal != '>')))
1888 {
1889 (void) ThrowMagickException(exception,GetMagickModule(),
1890 OptionWarning,"ParseError","unclosed <!--");
1891 utf8=DestroyString(utf8);
1892 return(&root->root);
1893 }
1894 }
1895 else
1896 if (strncmp(p,"![CDATA[",8) == 0)
1897 {
1898 /*
1899 Cdata.
1900 */
1901 p=strstr(p,"]]>");
1902 if (p != (char *) NULL)
1903 {
1904 p+=(ptrdiff_t) 2;
1905 if (ignore_depth == 0)
1906 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
1907 }
1908 else
1909 {
1910 (void) ThrowMagickException(exception,GetMagickModule(),
1911 OptionWarning,"ParseError","unclosed <![CDATA[");
1912 utf8=DestroyString(utf8);
1913 return(&root->root);
1914 }
1915 }
1916 else
1917 if (strncmp(p,"!DOCTYPE",8) == 0)
1918 {
1919 /*
1920 DTD.
1921 */
1922 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1923 ((l != 0) && ((*p != ']') ||
1924 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1925 l=(ssize_t) ((*p == '[') ? 1 : l))
1926 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
1927 if ((*p == '\0') && (terminal != '>'))
1928 {
1929 (void) ThrowMagickException(exception,GetMagickModule(),
1930 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1931 utf8=DestroyString(utf8);
1932 return(&root->root);
1933 }
1934 if (l != 0)
1935 tag=strchr(tag,'[')+1;
1936 if (l != 0)
1937 {
1938 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1939 exception);
1940 if (status == MagickFalse)
1941 {
1942 utf8=DestroyString(utf8);
1943 return(&root->root);
1944 }
1945 p++;
1946 }
1947 }
1948 else
1949 if (*p == '?')
1950 {
1951 /*
1952 Processing instructions.
1953 */
1954 do
1955 {
1956 p=strchr(p,'?');
1957 if (p == (char *) NULL)
1958 break;
1959 p++;
1960 } while ((*p != '\0') && (*p != '>'));
1961 if ((p == (char *) NULL) || ((*p == '\0') &&
1962 (terminal != '>')))
1963 {
1964 (void) ThrowMagickException(exception,GetMagickModule(),
1965 OptionWarning,"ParseError","unclosed <?");
1966 utf8=DestroyString(utf8);
1967 return(&root->root);
1968 }
1969 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
1970 }
1971 else
1972 {
1973 (void) ThrowMagickException(exception,GetMagickModule(),
1974 OptionWarning,"ParseError","unexpected <");
1975 utf8=DestroyString(utf8);
1976 return(&root->root);
1977 }
1978 if ((p == (char *) NULL) || (*p == '\0'))
1979 break;
1980 *p++='\0';
1981 tag=p;
1982 if ((*p != '\0') && (*p != '<'))
1983 {
1984 /*
1985 Tag character content.
1986 */
1987 while ((*p != '\0') && (*p != '<'))
1988 p++;
1989 if (*p == '\0')
1990 break;
1991 if (ignore_depth == 0)
1992 ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
1993 }
1994 else
1995 if (*p == '\0')
1996 break;
1997 }
1998 utf8=DestroyString(utf8);
1999 if (root->node == (XMLTreeInfo *) NULL)
2000 return(&root->root);
2001 if (root->node->tag == (char *) NULL)
2002 {
2003 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2004 "ParseError","root tag missing");
2005 return(&root->root);
2006 }
2007 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2008 "ParseError","unclosed tag: '%s'",root->node->tag);
2009 return(&root->root);
2010}
2011␌
2012/*
2013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2014% %
2015% %
2016% %
2017% N e w X M L T r e e T a g %
2018% %
2019% %
2020% %
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022%
2023% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2024%
2025% The format of the NewXMLTreeTag method is:
2026%
2027% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2028%
2029% A description of each parameter follows:
2030%
2031% o tag: the tag.
2032%
2033*/
2034MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2035{
2036 static const char
2037 *predefined_entities[NumberPredefinedEntities+1] =
2038 {
2039 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2040 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2041 };
2042
2043 XMLTreeRoot
2044 *root;
2045
2046 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2047 (void) memset(root,0,sizeof(*root));
2048 root->root.tag=(char *) NULL;
2049 if (tag != (char *) NULL)
2050 root->root.tag=ConstantString(tag);
2051 root->node=(&root->root);
2052 root->root.content=ConstantString("");
2053 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2054 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2055 root->root.attributes=sentinel;
2056 root->attributes=(char ***) root->root.attributes;
2057 root->processing_instructions=(char ***) root->root.attributes;
2058 root->debug=IsEventLogging();
2059 root->signature=MagickCoreSignature;
2060 return(&root->root);
2061}
2062␌
2063/*
2064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065% %
2066% %
2067% %
2068% S e t X M L T r e e C o n t e n t %
2069% %
2070% %
2071% %
2072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2073%
2074% SetXMLTreeContent() sets the character content for the given tag and
2075% returns the tag.
2076%
2077% The format of the SetXMLTreeContent method is:
2078%
2079% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2080% const char *content)
2081%
2082% A description of each parameter follows:
2083%
2084% o xml_info: the xml info.
2085%
2086% o content: The content.
2087%
2088*/
2089MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2090 const char *content)
2091{
2092 assert(xml_info != (XMLTreeInfo *) NULL);
2093 assert((xml_info->signature == MagickCoreSignature) ||
2094 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2095 if (IsEventLogging() != MagickFalse)
2096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2097 if (xml_info->content != (char *) NULL)
2098 xml_info->content=DestroyString(xml_info->content);
2099 xml_info->content=(char *) ConstantString(content);
2100 return(xml_info);
2101}
2102␌
2103/*
2104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105% %
2106% %
2107% %
2108% S k i p X M L C o m m e n t %
2109% %
2110% %
2111% %
2112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113%
2114% SkipXMLComment() skips leading whitespace and XML comments, leaving the
2115% next token untouched. It returns MagickFalse for an unterminated comment.
2116%
2117*/
2118MagickPrivate MagickBooleanType SkipXMLComment(const char **xml)
2119{
2120 const char
2121 *p;
2122
2123 for (p=*xml; ; )
2124 {
2125 while (isspace((int) ((unsigned char) *p)) != 0)
2126 p++;
2127 if (strncmp(p,"<!--",4) != 0)
2128 break;
2129 *xml=p;
2130 p=strstr(p+4,"-->");
2131 if (p == (const char *) NULL)
2132 return(MagickFalse);
2133 p+=3;
2134 }
2135 *xml=p;
2136 return(MagickTrue);
2137}
2138␌
2139/*
2140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2141% %
2142% %
2143% %
2144% S k i p X M L D O C T Y P E %
2145% %
2146% %
2147% %
2148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2149%
2150% SkipXMLDocType() skips a DOCTYPE declaration, including its internal subset.
2151%
2152*/
2153MagickPrivate MagickBooleanType SkipXMLDocType(const char **xml)
2154{
2155 const char
2156 *p;
2157
2158 int
2159 bracket_depth,
2160 quote;
2161
2162 bracket_depth=0;
2163 quote=0;
2164 for (p=*xml; *p != '\0'; )
2165 {
2166 if ((quote == 0) && (strncmp(p,"<!--",4) == 0))
2167 {
2168 if (SkipXMLComment(&p) == MagickFalse)
2169 break;
2170 continue;
2171 }
2172 if (quote != 0)
2173 {
2174 if (*p == quote)
2175 quote=0;
2176 }
2177 else
2178 {
2179 if ((*p == '"') || (*p == '\''))
2180 quote=(*p);
2181 else
2182 if (*p == '[')
2183 bracket_depth++;
2184 else
2185 if (*p == ']')
2186 {
2187 if (bracket_depth > 0)
2188 bracket_depth--;
2189 }
2190 else
2191 if ((*p == '>') && (bracket_depth == 0))
2192 {
2193 *xml=p+1;
2194 return(MagickTrue);
2195 }
2196 }
2197 p++;
2198 }
2199 *xml=p;
2200 return(MagickFalse);
2201}
2202␌
2203/*
2204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2205% %
2206% %
2207% %
2208% S u b s t i t u t e X M L E n t i t i e s %
2209% %
2210% %
2211% %
2212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213%
2214% SubstituteXMLEntities() substitutes the predefined XML entities.
2215%
2216*/
2217MagickExport char *SubstituteXMLEntities(const char *content,
2218 const MagickBooleanType pedantic)
2219{
2220 char
2221 *canonical_content;
2222
2223 const char
2224 *p;
2225
2226 size_t
2227 extent;
2228
2229 ssize_t
2230 i;
2231
2232 /*
2233 Substitute predefined entities.
2234 */
2235 i=0;
2236 canonical_content=AcquireString((char *) NULL);
2237 extent=MagickPathExtent;
2238 for (p=content; *p != '\0'; p++)
2239 {
2240 if ((i+MagickPathExtent) > (ssize_t) extent)
2241 {
2242 extent+=MagickPathExtent;
2243 canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
2244 sizeof(*canonical_content));
2245 if (canonical_content == (char *) NULL)
2246 return(canonical_content);
2247 }
2248 switch (*p)
2249 {
2250 case '&':
2251 {
2252 i+=FormatLocaleString(canonical_content+i,extent,"&amp;");
2253 break;
2254 }
2255 case '<':
2256 {
2257 i+=FormatLocaleString(canonical_content+i,extent,"&lt;");
2258 break;
2259 }
2260 case '>':
2261 {
2262 i+=FormatLocaleString(canonical_content+i,extent,"&gt;");
2263 break;
2264 }
2265 case '"':
2266 {
2267 i+=FormatLocaleString(canonical_content+i,extent,"&quot;");
2268 break;
2269 }
2270 case '\n':
2271 {
2272 if (pedantic == MagickFalse)
2273 {
2274 canonical_content[i++]=(char) (*p);
2275 break;
2276 }
2277 i+=FormatLocaleString(canonical_content+i,extent,"&#xA;");
2278 break;
2279 }
2280 case '\t':
2281 {
2282 if (pedantic == MagickFalse)
2283 {
2284 canonical_content[i++]=(char) (*p);
2285 break;
2286 }
2287 i+=FormatLocaleString(canonical_content+i,extent,"&#x9;");
2288 break;
2289 }
2290 case '\r':
2291 {
2292 i+=FormatLocaleString(canonical_content+i,extent,"&#xD;");
2293 break;
2294 }
2295 default:
2296 {
2297 canonical_content[i++]=(char) (*p);
2298 break;
2299 }
2300 }
2301 }
2302 canonical_content[i]='\0';
2303 return(canonical_content);
2304}
2305␌
2306/*
2307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2308% %
2309% %
2310% %
2311% X M L T r e e I n f o T o X M L %
2312% %
2313% %
2314% %
2315%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2316%
2317% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2318%
2319% The format of the XMLTreeInfoToXML method is:
2320%
2321% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2322%
2323% A description of each parameter follows:
2324%
2325% o xml_info: the xml info.
2326%
2327*/
2328
2329static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2330 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2331{
2332 char
2333 *canonical_content;
2334
2335 if (offset < 0)
2336 canonical_content=CanonicalXMLContent(source,pedantic);
2337 else
2338 {
2339 char
2340 *content;
2341
2342 content=AcquireString(source);
2343 content[offset]='\0';
2344 canonical_content=CanonicalXMLContent(content,pedantic);
2345 content=DestroyString(content);
2346 }
2347 if (canonical_content == (char *) NULL)
2348 return(*destination);
2349 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2350 {
2351 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2352 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2353 sizeof(**destination));
2354 if (*destination == (char *) NULL)
2355 return(*destination);
2356 }
2357 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2358 canonical_content);
2359 canonical_content=DestroyString(canonical_content);
2360 return(*destination);
2361}
2362
2363static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2364 size_t *extent,size_t start,char ***attributes)
2365{
2366 char
2367 *content;
2368
2369 const char
2370 *attribute;
2371
2372 size_t
2373 offset;
2374
2375 ssize_t
2376 i,
2377 j;
2378
2379 content=(char *) "";
2380 if (xml_info->parent != (XMLTreeInfo *) NULL)
2381 content=xml_info->parent->content;
2382 offset=0;
2383 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2384 start),source,length,extent,MagickFalse);
2385 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2386 {
2387 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2388 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2389 if (*source == (char *) NULL)
2390 return(*source);
2391 }
2392 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2393 "<%s",xml_info->tag);
2394 for (i=0; xml_info->attributes[i]; i+=2)
2395 {
2396 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2397 if (attribute != xml_info->attributes[i+1])
2398 continue;
2399 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2400 {
2401 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2402 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2403 if (*source == (char *) NULL)
2404 return((char *) NULL);
2405 }
2406 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2407 xml_info->attributes[i]);
2408 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2409 extent,MagickTrue);
2410 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2411 }
2412 i=0;
2413 while ((attributes[i] != (char **) NULL) &&
2414 (strcmp(attributes[i][0],xml_info->tag) != 0))
2415 i++;
2416 j=1;
2417 while ((attributes[i] != (char **) NULL) &&
2418 (attributes[i][j] != (char *) NULL))
2419 {
2420 if ((attributes[i][j+1] == (char *) NULL) ||
2421 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2422 {
2423 j+=3;
2424 continue;
2425 }
2426 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2427 {
2428 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2429 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2430 if (*source == (char *) NULL)
2431 return((char *) NULL);
2432 }
2433 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2434 attributes[i][j]);
2435 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2436 MagickTrue);
2437 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2438 j+=3;
2439 }
2440 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2441 *xml_info->content ? ">" : "/>");
2442 if (xml_info->child != (XMLTreeInfo *) NULL)
2443 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2444 else
2445 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2446 MagickFalse);
2447 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2448 {
2449 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2450 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2451 if (*source == (char *) NULL)
2452 return((char *) NULL);
2453 }
2454 if (*xml_info->content != '\0')
2455 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2456 xml_info->tag);
2457 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2458 offset++;
2459 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2460 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2461 attributes);
2462 else
2463 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2464 MagickFalse);
2465 return(content);
2466}
2467
2468MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2469{
2470 char
2471 *p,
2472 *q,
2473 *xml;
2474
2475 size_t
2476 extent,
2477 length;
2478
2479 ssize_t
2480 i,
2481 j,
2482 k;
2483
2484 XMLTreeInfo
2485 *ordered,
2486 *parent;
2487
2488 XMLTreeRoot
2489 *root;
2490
2491 assert(xml_info != (XMLTreeInfo *) NULL);
2492 assert((xml_info->signature == MagickCoreSignature) ||
2493 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2494 if (IsEventLogging() != MagickFalse)
2495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2496 if (xml_info->tag == (char *) NULL)
2497 return((char *) NULL);
2498 xml=AcquireString((char *) NULL);
2499 length=0;
2500 extent=MagickPathExtent;
2501 root=(XMLTreeRoot *) xml_info;
2502 while (root->root.parent != (XMLTreeInfo *) NULL)
2503 root=(XMLTreeRoot *) root->root.parent;
2504 parent=xml_info->parent;
2505 if (parent == (XMLTreeInfo *) NULL)
2506 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2507 {
2508 /*
2509 Pre-root processing instructions.
2510 */
2511 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2512 p=root->processing_instructions[i][1];
2513 for (j=1; p != (char *) NULL; j++)
2514 {
2515 if (root->processing_instructions[i][k][j-1] == '>')
2516 {
2517 p=root->processing_instructions[i][j];
2518 continue;
2519 }
2520 q=root->processing_instructions[i][0];
2521 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2522 {
2523 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2524 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2525 if (xml == (char *) NULL)
2526 return(xml);
2527 }
2528 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2529 *p != '\0' ? " " : "",p);
2530 p=root->processing_instructions[i][j];
2531 }
2532 }
2533 ordered=xml_info->ordered;
2534 xml_info->parent=(XMLTreeInfo *) NULL;
2535 xml_info->ordered=(XMLTreeInfo *) NULL;
2536 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2537 xml_info->parent=parent;
2538 xml_info->ordered=ordered;
2539 if (parent == (XMLTreeInfo *) NULL)
2540 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2541 {
2542 /*
2543 Post-root processing instructions.
2544 */
2545 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2546 p=root->processing_instructions[i][1];
2547 for (j=1; p != (char *) NULL; j++)
2548 {
2549 if (root->processing_instructions[i][k][j-1] == '<')
2550 {
2551 p=root->processing_instructions[i][j];
2552 continue;
2553 }
2554 q=root->processing_instructions[i][0];
2555 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2556 {
2557 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2558 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2559 if (xml == (char *) NULL)
2560 return(xml);
2561 }
2562 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2563 *p != '\0' ? " " : "",p);
2564 p=root->processing_instructions[i][j];
2565 }
2566 }
2567 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2568}