MagickCore 7.1.2-35
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 ExceptionInfo *exception)
1018{
1019 char
1020 *entity,
1021 *p,
1022 *q;
1023
1024 int
1025 byte,
1026 c;
1027
1028 size_t
1029 extent,
1030 length;
1031
1032 ssize_t
1033 i,
1034 offset;
1035
1036 /*
1037 Normalize line endings.
1038 */
1039 p=xml;
1040 q=xml;
1041 for ( ; *xml != '\0'; xml++)
1042 while (*xml == '\r')
1043 {
1044 *(xml++)='\n';
1045 if (*xml == '\n')
1046 (void) memmove(xml,xml+1,strlen(xml));
1047 }
1048 for (xml=p; ; )
1049 {
1050 while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
1051 (state != '%')) && (isspace((int) ((unsigned char) *xml)) == 0))
1052 xml++;
1053 if (*xml == '\0')
1054 break;
1055 /*
1056 States include:
1057 '&' for general entity decoding
1058 '%' for parameter entity decoding
1059 'c' for CDATA sections
1060 ' ' for attributes normalization
1061 '*' for non-CDATA attributes normalization
1062 */
1063 if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
1064 {
1065 /*
1066 Character reference.
1067 */
1068 if (xml[2] != 'x')
1069 c=(int) strtol(xml+2,&entity,10); /* base 10 */
1070 else
1071 c=(int) strtol(xml+3,&entity,16); /* base 16 */
1072 if ((c == 0) || (*entity != ';'))
1073 {
1074 /*
1075 Not a character reference.
1076 */
1077 xml++;
1078 continue;
1079 }
1080 if (c < 0x80)
1081 *(xml++)=(char) c;
1082 else
1083 {
1084 /*
1085 Multi-byte UTF-8 sequence.
1086 */
1087 byte=c;
1088 for (i=0; byte != 0; byte/=2)
1089 i++;
1090 i=(i-2)/5;
1091 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1092 xml++;
1093 while (i != 0)
1094 {
1095 i--;
1096 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1097 xml++;
1098 }
1099 }
1100 (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
1101 }
1102 else
1103 if (((*xml == '&') && ((state == '&') || (state == ' ') ||
1104 (state == '*'))) || ((state == '%') && (*xml == '%')))
1105 {
1106 /*
1107 Find entity in the list.
1108 */
1109 i=0;
1110 while ((entities[i] != (char *) NULL) &&
1111 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1112 i+=2;
1113 if (entities[i++] == (char *) NULL)
1114 xml++;
1115 else
1116 if (entities[i] != (char *) NULL)
1117 {
1118 /*
1119 Found a match.
1120 */
1121 length=strlen(entities[i]);
1122 entity=strchr(xml,';');
1123 if ((entity != (char *) NULL) &&
1124 ((length-1L) >= (size_t) (entity-xml)))
1125 {
1126 offset=(ssize_t) (xml-p);
1127 extent=((size_t) offset+length+strlen(entity));
1128 if (p != q)
1129 {
1130 p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
1131 if (p != (char *) NULL)
1132 p[extent]='\0';
1133 }
1134 else
1135 {
1136 char
1137 *extent_xml;
1138
1139 extent_xml=(char *) AcquireQuantumMemory(extent+1,
1140 sizeof(*extent_xml));
1141 if (extent_xml != (char *) NULL)
1142 {
1143 memset(extent_xml,0,extent*sizeof(*extent_xml));
1144 (void) CopyMagickString(extent_xml,p,extent*
1145 sizeof(*extent_xml));
1146 }
1147 p=extent_xml;
1148 }
1149 if (p == (char *) NULL)
1150 {
1151 (void) ThrowMagickException(exception,GetMagickModule(),
1152 OptionWarning,"ParseError",
1153 "memory allocation failed");
1154 return(p);
1155 }
1156 if (strlen(p) > GetMaxProfileSize())
1157 {
1158 (void) ThrowMagickException(exception,GetMagickModule(),
1159 ResourceLimitError,"ProfileSizeExceedsLimit",
1160 "`%.17g'",(double) strlen(p));
1161 return(p == q ? ConstantString(p) : p);
1162 }
1163 xml=p+offset;
1164 entity=strchr(xml,';');
1165 }
1166 if (entity != (char *) NULL)
1167 (void) memmove(xml+length,entity+1,strlen(entity));
1168 (void) memcpy(xml,entities[i],length);
1169 }
1170 }
1171 else
1172 if (((state == ' ') || (state == '*')) &&
1173 (isspace((int) ((unsigned char) *xml)) != 0))
1174 *(xml++)=' ';
1175 else
1176 xml++;
1177 }
1178 if (state == '*')
1179 {
1180 /*
1181 Normalize spaces for non-CDATA attributes.
1182 */
1183 for (xml=p; *xml != '\0'; xml++)
1184 {
1185 char
1186 accept[] = " ";
1187
1188 i=(ssize_t) strspn(xml,accept);
1189 if (i != 0)
1190 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1191 while ((*xml != '\0') && (*xml != ' '))
1192 xml++;
1193 if (*xml == '\0')
1194 break;
1195 }
1196 xml--;
1197 if ((xml >= p) && (*xml == ' '))
1198 *xml='\0';
1199 }
1200 return(p == q ? ConstantString(p) : p);
1201}
1202
1203static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
1204 const size_t length,const char state,ExceptionInfo *exception)
1205{
1206 XMLTreeInfo
1207 *xml_info;
1208
1209 xml_info=root->node;
1210 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
1211 (length == 0))
1212 return;
1213 xml[length]='\0';
1214 xml=ParseEntities(xml,root->entities,state,exception);
1215 if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
1216 {
1217 (void) ConcatenateString(&xml_info->content,xml);
1218 xml=DestroyString(xml);
1219 }
1220 else
1221 {
1222 if (xml_info->content != (char *) NULL)
1223 xml_info->content=DestroyString(xml_info->content);
1224 xml_info->content=xml;
1225 }
1226}
1227
1228static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
1229 ExceptionInfo *exception)
1230{
1231 if ((root->node == (XMLTreeInfo *) NULL) ||
1232 (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1233 {
1234 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1235 "ParseError","unexpected closing tag </%s>",tag);
1236 return(&root->root);
1237 }
1238 root->node=root->node->parent;
1239 root->depth--;
1240 return((XMLTreeInfo *) NULL);
1241}
1242
1243static MagickBooleanType ValidateEntities(char *tag,char *xml,
1244 const int delimiter,const size_t depth,char **entities)
1245{
1246 ssize_t
1247 i;
1248
1249 /*
1250 Check for circular entity references.
1251 */
1252 if (depth >= MagickMaxRecursionDepth)
1253 return(MagickFalse);
1254 for ( ; ; xml++)
1255 {
1256 while ((*xml != '\0') && (*xml != delimiter))
1257 xml++;
1258 if (*xml == '\0')
1259 return(MagickTrue);
1260 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1261 return(MagickFalse);
1262 i=0;
1263 while ((entities[i] != (char *) NULL) &&
1264 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1265 i+=2;
1266 if ((entities[i] != (char *) NULL) &&
1267 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1268 return(MagickFalse);
1269 }
1270}
1271
1272static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
1273 size_t length)
1274{
1275 char
1276 *target;
1277
1278 ssize_t
1279 i,
1280 j;
1281
1282 target=xml;
1283 xml[length]='\0';
1284 xml+=strcspn(xml,XMLWhitespace);
1285 if (*xml != '\0')
1286 {
1287 *xml='\0';
1288 xml+=strspn(xml+1,XMLWhitespace)+1;
1289 }
1290 if (strcmp(target,"xml") == 0)
1291 {
1292 xml=strstr(xml,"standalone");
1293 if ((xml != (char *) NULL) &&
1294 (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
1295 root->standalone=MagickTrue;
1296 return;
1297 }
1298 if (root->processing_instructions[0] == (char **) NULL)
1299 {
1300 root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
1301 *root->processing_instructions));
1302 *root->processing_instructions=(char **) NULL;
1303 }
1304 i=0;
1305 while ((root->processing_instructions[i] != (char **) NULL) &&
1306 (strcmp(target,root->processing_instructions[i][0]) != 0))
1307 i++;
1308 if (root->processing_instructions[i] == (char **) NULL)
1309 {
1310 root->processing_instructions=(char ***) ResizeQuantumMemory(
1311 root->processing_instructions,(size_t) (i+2),
1312 sizeof(*root->processing_instructions));
1313 if (root->processing_instructions == (char ***) NULL)
1314 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1315 root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
1316 sizeof(**root->processing_instructions));
1317 if (root->processing_instructions[i] == (char **) NULL)
1318 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1319 root->processing_instructions[i+1]=(char **) NULL;
1320 root->processing_instructions[i][0]=ConstantString(target);
1321 root->processing_instructions[i][1]=(char *)
1322 root->processing_instructions[i+1];
1323 root->processing_instructions[i+1]=(char **) NULL;
1324 root->processing_instructions[i][2]=ConstantString("");
1325 }
1326 j=1;
1327 while (root->processing_instructions[i][j] != (char *) NULL)
1328 j++;
1329 root->processing_instructions[i]=(char **) ResizeQuantumMemory(
1330 root->processing_instructions[i],(size_t) (j+3),
1331 sizeof(**root->processing_instructions));
1332 if (root->processing_instructions[i] == (char **) NULL)
1333 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1334 root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
1335 root->processing_instructions[i][j+1],(size_t) (j+1),
1336 sizeof(***root->processing_instructions));
1337 if (root->processing_instructions[i][j+2] == (char *) NULL)
1338 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1339 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1340 root->root.tag != (char *) NULL ? ">" : "<",2);
1341 root->processing_instructions[i][j]=ConstantString(xml);
1342 root->processing_instructions[i][j+1]=(char *) NULL;
1343}
1344
1345static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
1346 size_t length,ExceptionInfo *exception)
1347{
1348#define DestroyXMLEntities(predefined_entities) \
1349{ \
1350 ssize_t k; \
1351 for (k=0; predefined_entities[k] != (char *) NULL; k++) \
1352 if ((k & 0x01) != 0) \
1353 predefined_entities[k]=DestroyString(predefined_entities[k]); \
1354 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities); \
1355}
1356
1357 char
1358 *c,
1359 **entities,
1360 *n,
1361 **predefined_entities,
1362 q,
1363 *t,
1364 *v;
1365
1366 ssize_t
1367 i,
1368 j;
1369
1370 n=(char *) NULL;
1371 predefined_entities=(char **) AcquireMagickMemory(sizeof(sentinel));
1372 if (predefined_entities == (char **) NULL)
1373 ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
1374 (void) memcpy(predefined_entities,sentinel,sizeof(sentinel));
1375 for (xml[length]='\0'; xml != (char *) NULL; )
1376 {
1377 while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
1378 xml++;
1379 if (*xml == '\0')
1380 break;
1381 if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
1382 {
1383 /*
1384 Parse entity definitions.
1385 */
1386 if (strspn(xml+8,XMLWhitespace) == 0)
1387 break;
1388 xml+=strspn(xml+8,XMLWhitespace)+8;
1389 c=xml;
1390 n=xml+strspn(xml,XMLWhitespace "%");
1391 if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
1392 break;
1393 xml=n+strcspn(n,XMLWhitespace);
1394 if (*xml == '\0')
1395 break;
1396 *xml=';';
1397 v=xml+strspn(xml+1,XMLWhitespace)+1;
1398 q=(*v);
1399 v++;
1400 if ((q != '"') && (q != '\''))
1401 {
1402 /*
1403 Skip externals.
1404 */
1405 xml=strchr(xml,'>');
1406 continue;
1407 }
1408 entities=(*c == '%') ? predefined_entities : root->entities;
1409 for (i=0; entities[i] != (char *) NULL; i++) ;
1410 entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
1411 sizeof(*entities));
1412 if (entities == (char **) NULL)
1413 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1414 if (*c == '%')
1415 predefined_entities=entities;
1416 else
1417 root->entities=entities;
1418 xml++;
1419 *xml='\0';
1420 xml=strchr(v,q);
1421 if (xml != (char *) NULL)
1422 {
1423 *xml='\0';
1424 xml++;
1425 }
1426 entities[i+1]=ParseEntities(v,predefined_entities,'%',exception);
1427 entities[i+2]=(char *) NULL;
1428 if ((ValidateEntities(n,entities[i+1],'%',0,entities) != MagickFalse) &&
1429 (ValidateEntities(n,entities[i+1],'&',0,entities) != MagickFalse))
1430 entities[i]=n;
1431 else
1432 {
1433 if (entities[i+1] != v)
1434 entities[i+1]=DestroyString(entities[i+1]);
1435 (void) ThrowMagickException(exception,GetMagickModule(),
1436 OptionWarning,"ParseError","circular entity declaration %s",n);
1437 DestroyXMLEntities(predefined_entities);
1438 return(MagickFalse);
1439 }
1440 }
1441 else
1442 if (strncmp(xml,"<!ATTLIST",9) == 0)
1443 {
1444 /*
1445 Parse default attributes.
1446 */
1447 t=xml+strspn(xml+9,XMLWhitespace)+9;
1448 if (*t == '\0')
1449 {
1450 (void) ThrowMagickException(exception,GetMagickModule(),
1451 OptionWarning,"ParseError","unclosed <!ATTLIST");
1452 DestroyXMLEntities(predefined_entities);
1453 return(MagickFalse);
1454 }
1455 xml=t+strcspn(t,XMLWhitespace ">");
1456 if (*xml == '>')
1457 continue;
1458 *xml='\0';
1459 i=0;
1460 while ((root->attributes[i] != (char **) NULL) &&
1461 (n != (char *) NULL) &&
1462 (strcmp(n,root->attributes[i][0]) != 0))
1463 i++;
1464 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
1465 (*n != '>'))
1466 {
1467 xml=n+strcspn(n,XMLWhitespace);
1468 if (*xml != '\0')
1469 *xml='\0';
1470 else
1471 {
1472 (void) ThrowMagickException(exception,GetMagickModule(),
1473 OptionWarning,"ParseError","malformed <!ATTLIST");
1474 DestroyXMLEntities(predefined_entities);
1475 return(MagickFalse);
1476 }
1477 xml+=strspn(xml+1,XMLWhitespace)+1;
1478 c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
1479 if (strncmp(xml,"NOTATION",8) == 0)
1480 xml+=strspn(xml+8,XMLWhitespace)+8;
1481 xml=(*xml == '(') ? strchr(xml,')') : xml+
1482 strcspn(xml,XMLWhitespace);
1483 if (xml == (char *) NULL)
1484 {
1485 (void) ThrowMagickException(exception,GetMagickModule(),
1486 OptionWarning,"ParseError","malformed <!ATTLIST");
1487 DestroyXMLEntities(predefined_entities);
1488 return(MagickFalse);
1489 }
1490 xml+=strspn(xml,XMLWhitespace ")");
1491 if (strncmp(xml,"#FIXED",6) == 0)
1492 xml+=strspn(xml+6,XMLWhitespace)+6;
1493 if (*xml == '#')
1494 {
1495 xml+=strcspn(xml,XMLWhitespace ">")-1;
1496 if (*c == ' ')
1497 continue;
1498 v=(char *) NULL;
1499 }
1500 else
1501 if (((*xml == '"') || (*xml == '\'')) &&
1502 ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
1503 *xml='\0';
1504 else
1505 {
1506 (void) ThrowMagickException(exception,GetMagickModule(),
1507 OptionWarning,"ParseError","malformed <!ATTLIST");
1508 DestroyXMLEntities(predefined_entities);
1509 return(MagickFalse);
1510 }
1511 if (root->attributes[i] == (char **) NULL)
1512 {
1513 /*
1514 New attribute tag.
1515 */
1516 if (i == 0)
1517 root->attributes=(char ***) AcquireQuantumMemory(2,
1518 sizeof(*root->attributes));
1519 else
1520 root->attributes=(char ***) ResizeQuantumMemory(
1521 root->attributes,(size_t) (i+2),
1522 sizeof(*root->attributes));
1523 if (root->attributes == (char ***) NULL)
1524 ThrowFatalException(ResourceLimitFatalError,
1525 "MemoryAllocationFailed");
1526 root->attributes[i]=(char **) AcquireQuantumMemory(2,
1527 sizeof(**root->attributes));
1528 if (root->attributes[i] == (char **) NULL)
1529 ThrowFatalException(ResourceLimitFatalError,
1530 "MemoryAllocationFailed");
1531 root->attributes[i][0]=ConstantString(t);
1532 root->attributes[i][1]=(char *) NULL;
1533 root->attributes[i+1]=(char **) NULL;
1534 }
1535 for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
1536 root->attributes[i]=(char **) ResizeQuantumMemory(
1537 root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
1538 if (root->attributes[i] == (char **) NULL)
1539 ThrowFatalException(ResourceLimitFatalError,
1540 "MemoryAllocationFailed");
1541 root->attributes[i][j+3]=(char *) NULL;
1542 root->attributes[i][j+2]=ConstantString(c);
1543 root->attributes[i][j+1]=(char *) NULL;
1544 if (v != (char *) NULL)
1545 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c,
1546 exception);
1547 root->attributes[i][j]=ConstantString(n);
1548 }
1549 }
1550 else
1551 if (strncmp(xml, "<!--", 4) == 0)
1552 xml=strstr(xml+4,"-->");
1553 else
1554 if (strncmp(xml,"<?", 2) == 0)
1555 {
1556 c=xml+2;
1557 xml=strstr(c,"?>");
1558 if (xml != (char *) NULL)
1559 {
1560 ParseProcessingInstructions(root,c,(size_t) (xml-c));
1561 xml++;
1562 }
1563 }
1564 else
1565 if (*xml == '<')
1566 xml=strchr(xml,'>');
1567 else
1568 if ((*(xml++) == '%') && (root->standalone == MagickFalse))
1569 break;
1570 }
1571 DestroyXMLEntities(predefined_entities);
1572 return(MagickTrue);
1573}
1574
1575static MagickBooleanType ParseOpenTag(XMLTreeRoot *root,char *tag,
1576 char **attributes,ExceptionInfo *exception)
1577{
1578 XMLTreeInfo
1579 *xml_info;
1580
1581 if (root->depth >= MagickMaxRecursionDepth)
1582 {
1583 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1584 "ParseError","unexpected open tag </%s>",tag);
1585 return(MagickFalse);
1586 }
1587 xml_info=root->node;
1588 if (xml_info->tag == (char *) NULL)
1589 xml_info->tag=ConstantString(tag);
1590 else
1591 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1592 if (xml_info != (XMLTreeInfo *) NULL)
1593 xml_info->attributes=attributes;
1594 root->node=xml_info;
1595 root->depth++;
1596 return(MagickTrue);
1597}
1598
1599static const char
1600 *ignore_tags[3] =
1601 {
1602 "rdf:Bag",
1603 "rdf:Seq",
1604 (const char *) NULL
1605 };
1606
1607static inline MagickBooleanType IsSkipTag(const char *tag)
1608{
1609 ssize_t
1610 i;
1611
1612 i=0;
1613 while (ignore_tags[i] != (const char *) NULL)
1614 {
1615 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1616 return(MagickTrue);
1617 i++;
1618 }
1619 return(MagickFalse);
1620}
1621
1622MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
1623{
1624 char
1625 **attribute,
1626 **attributes,
1627 *p,
1628 *tag,
1629 *utf8;
1630
1631 int
1632 c,
1633 terminal;
1634
1635 MagickBooleanType
1636 status;
1637
1638 size_t
1639 ignore_depth,
1640 length;
1641
1642 ssize_t
1643 i,
1644 j,
1645 l;
1646
1647 XMLTreeRoot
1648 *root;
1649
1650 /*
1651 Convert xml-string to UTF8.
1652 */
1653 if ((xml == (const char *) NULL) || (strlen(xml) == 0))
1654 {
1655 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1656 "ParseError","root tag missing");
1657 return((XMLTreeInfo *) NULL);
1658 }
1659 root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
1660 length=strlen(xml);
1661 utf8=ConvertUTF16ToUTF8(xml,&length);
1662 if (utf8 == (char *) NULL)
1663 {
1664 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1665 "ParseError","UTF16 to UTF8 failed");
1666 return((XMLTreeInfo *) NULL);
1667 }
1668 if (length == 0)
1669 {
1670 utf8=DestroyString(utf8);
1671 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1672 "ParseError","root tag missing");
1673 return((XMLTreeInfo *) NULL);
1674 }
1675 terminal=utf8[length-1];
1676 utf8[length-1]='\0';
1677 p=utf8;
1678 while ((*p != '\0') && (*p != '<'))
1679 p++;
1680 if (*p == '\0')
1681 {
1682 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1683 "ParseError","root tag missing");
1684 utf8=DestroyString(utf8);
1685 return((XMLTreeInfo *) NULL);
1686 }
1687 attribute=(char **) NULL;
1688 l=0;
1689 ignore_depth=0;
1690 for (p++; ; p++)
1691 {
1692 attributes=(char **) sentinel;
1693 tag=p;
1694 c=(*p);
1695 if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
1696 (*p == ':') || (c < '\0'))
1697 {
1698 /*
1699 Tag.
1700 */
1701 if (root->node == (XMLTreeInfo *) NULL)
1702 {
1703 (void) ThrowMagickException(exception,GetMagickModule(),
1704 OptionWarning,"ParseError","root tag missing");
1705 utf8=DestroyString(utf8);
1706 return(&root->root);
1707 }
1708 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "/>");
1709 while (isspace((int) ((unsigned char) *p)) != 0)
1710 *p++='\0';
1711 if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
1712 (ignore_depth == 0))
1713 {
1714 if ((*p != '\0') && (*p != '/') && (*p != '>'))
1715 {
1716 /*
1717 Find tag in default attributes list.
1718 */
1719 i=0;
1720 while ((root->attributes[i] != (char **) NULL) &&
1721 (strcmp(root->attributes[i][0],tag) != 0))
1722 i++;
1723 attribute=root->attributes[i];
1724 }
1725 for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
1726 {
1727 /*
1728 Attribute.
1729 */
1730 if (l == 0)
1731 attributes=(char **) AcquireQuantumMemory(4,
1732 sizeof(*attributes));
1733 else
1734 attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
1735 (l+4),sizeof(*attributes));
1736 if (attributes == (char **) NULL)
1737 {
1738 (void) ThrowMagickException(exception,GetMagickModule(),
1739 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1740 utf8=DestroyString(utf8);
1741 return(&root->root);
1742 }
1743 attributes[l+2]=(char *) NULL;
1744 attributes[l+1]=(char *) NULL;
1745 attributes[l]=p;
1746 p+=(ptrdiff_t) strcspn(p,XMLWhitespace "=/>");
1747 if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
1748 attributes[l]=ConstantString("");
1749 else
1750 {
1751 *p++='\0';
1752 p+=(ptrdiff_t) strspn(p,XMLWhitespace "=");
1753 c=(*p);
1754 if ((c == '"') || (c == '\''))
1755 {
1756 /*
1757 Attributes value.
1758 */
1759 p++;
1760 attributes[l+1]=p;
1761 while ((*p != '\0') && (*p != c))
1762 p++;
1763 if (*p != '\0')
1764 *p++='\0';
1765 else
1766 {
1767 attributes[l]=ConstantString("");
1768 attributes[l+1]=ConstantString("");
1769 (void) DestroyXMLTreeAttributes(attributes);
1770 (void) ThrowMagickException(exception,
1771 GetMagickModule(),OptionWarning,"ParseError",
1772 "missing %c",c);
1773 utf8=DestroyString(utf8);
1774 return(&root->root);
1775 }
1776 j=1;
1777 while ((attribute != (char **) NULL) &&
1778 (attribute[j] != (char *) NULL) &&
1779 (strcmp(attribute[j],attributes[l]) != 0))
1780 j+=3;
1781 attributes[l+1]=ParseEntities(attributes[l+1],
1782 root->entities,(attribute != (char **) NULL) &&
1783 (attribute[j] != (char *) NULL) ? *attribute[j+2] :
1784 ' ',exception);
1785 }
1786 attributes[l]=ConstantString(attributes[l]);
1787 }
1788 while (isspace((int) ((unsigned char) *p)) != 0)
1789 p++;
1790 }
1791 }
1792 else
1793 {
1794 while ((*p != '\0') && (*p != '/') && (*p != '>'))
1795 p++;
1796 }
1797 if (*p == '/')
1798 {
1799 /*
1800 Self closing tag.
1801 */
1802 *p++='\0';
1803 if (((*p != '\0') && (*p != '>')) ||
1804 ((*p == '\0') && (terminal != '>')))
1805 {
1806 if (l != 0)
1807 (void) DestroyXMLTreeAttributes(attributes);
1808 (void) ThrowMagickException(exception,GetMagickModule(),
1809 OptionWarning,"ParseError","missing >");
1810 utf8=DestroyString(utf8);
1811 return(&root->root);
1812 }
1813 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1814 (void) DestroyXMLTreeAttributes(attributes);
1815 else
1816 {
1817 status=ParseOpenTag(root,tag,attributes,exception);
1818 if (status == MagickFalse)
1819 {
1820 if (l != 0)
1821 (void) DestroyXMLTreeAttributes(attributes);
1822 utf8=DestroyString(utf8);
1823 return(&root->root);
1824 }
1825 (void) ParseCloseTag(root,tag,exception);
1826 }
1827 }
1828 else
1829 {
1830 c=(*p);
1831 if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
1832 {
1833 *p='\0';
1834 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1835 {
1836 status=ParseOpenTag(root,tag,attributes,exception);
1837 if (status == MagickFalse)
1838 {
1839 if (l != 0)
1840 (void) DestroyXMLTreeAttributes(attributes);
1841 utf8=DestroyString(utf8);
1842 return(&root->root);
1843 }
1844 }
1845 else
1846 {
1847 ignore_depth++;
1848 (void) DestroyXMLTreeAttributes(attributes);
1849 }
1850 *p=(char) c;
1851 }
1852 else
1853 {
1854 if (l != 0)
1855 (void) DestroyXMLTreeAttributes(attributes);
1856 (void) ThrowMagickException(exception,GetMagickModule(),
1857 OptionWarning,"ParseError","missing >");
1858 utf8=DestroyString(utf8);
1859 return(&root->root);
1860 }
1861 }
1862 }
1863 else
1864 if (*p == '/')
1865 {
1866 /*
1867 Close tag.
1868 */
1869 tag=p+1;
1870 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace ">")+1;
1871 c=(*p);
1872 if ((c == '\0') && (terminal != '>'))
1873 {
1874 (void) ThrowMagickException(exception,GetMagickModule(),
1875 OptionWarning,"ParseError","missing >");
1876 utf8=DestroyString(utf8);
1877 return(&root->root);
1878 }
1879 *p='\0';
1880 if ((ignore_depth == 0) &&
1881 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1882 {
1883 utf8=DestroyString(utf8);
1884 return(&root->root);
1885 }
1886 if (ignore_depth > 0)
1887 ignore_depth--;
1888 *p=(char) c;
1889 if (isspace((int) ((unsigned char) *p)) != 0)
1890 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1891 }
1892 else
1893 if (strncmp(p,"!--",3) == 0)
1894 {
1895 /*
1896 Comment.
1897 */
1898 p=strstr(p+3,"--");
1899 if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
1900 ((*p == '\0') && (terminal != '>')))
1901 {
1902 (void) ThrowMagickException(exception,GetMagickModule(),
1903 OptionWarning,"ParseError","unclosed <!--");
1904 utf8=DestroyString(utf8);
1905 return(&root->root);
1906 }
1907 }
1908 else
1909 if (strncmp(p,"![CDATA[",8) == 0)
1910 {
1911 /*
1912 Cdata.
1913 */
1914 p=strstr(p,"]]>");
1915 if (p != (char *) NULL)
1916 {
1917 p+=(ptrdiff_t) 2;
1918 if (ignore_depth == 0)
1919 ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c',
1920 exception);
1921 }
1922 else
1923 {
1924 (void) ThrowMagickException(exception,GetMagickModule(),
1925 OptionWarning,"ParseError","unclosed <![CDATA[");
1926 utf8=DestroyString(utf8);
1927 return(&root->root);
1928 }
1929 }
1930 else
1931 if (strncmp(p,"!DOCTYPE",8) == 0)
1932 {
1933 /*
1934 DTD.
1935 */
1936 for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
1937 ((l != 0) && ((*p != ']') ||
1938 (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
1939 l=(ssize_t) ((*p == '[') ? 1 : l))
1940 p+=(ptrdiff_t) strcspn(p+1,"[]>")+1;
1941 if ((*p == '\0') && (terminal != '>'))
1942 {
1943 (void) ThrowMagickException(exception,GetMagickModule(),
1944 OptionWarning,"ParseError","unclosed <!DOCTYPE");
1945 utf8=DestroyString(utf8);
1946 return(&root->root);
1947 }
1948 if (l != 0)
1949 tag=strchr(tag,'[')+1;
1950 if (l != 0)
1951 {
1952 status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
1953 exception);
1954 if (status == MagickFalse)
1955 {
1956 utf8=DestroyString(utf8);
1957 return(&root->root);
1958 }
1959 p++;
1960 }
1961 }
1962 else
1963 if (*p == '?')
1964 {
1965 /*
1966 Processing instructions.
1967 */
1968 do
1969 {
1970 p=strchr(p,'?');
1971 if (p == (char *) NULL)
1972 break;
1973 p++;
1974 } while ((*p != '\0') && (*p != '>'));
1975 if ((p == (char *) NULL) || ((*p == '\0') &&
1976 (terminal != '>')))
1977 {
1978 (void) ThrowMagickException(exception,GetMagickModule(),
1979 OptionWarning,"ParseError","unclosed <?");
1980 utf8=DestroyString(utf8);
1981 return(&root->root);
1982 }
1983 ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
1984 }
1985 else
1986 {
1987 (void) ThrowMagickException(exception,GetMagickModule(),
1988 OptionWarning,"ParseError","unexpected <");
1989 utf8=DestroyString(utf8);
1990 return(&root->root);
1991 }
1992 if ((p == (char *) NULL) || (*p == '\0'))
1993 break;
1994 *p++='\0';
1995 tag=p;
1996 if ((*p != '\0') && (*p != '<'))
1997 {
1998 /*
1999 Tag character content.
2000 */
2001 while ((*p != '\0') && (*p != '<'))
2002 p++;
2003 if (*p == '\0')
2004 break;
2005 if (ignore_depth == 0)
2006 ParseCharacterContent(root,tag,(size_t) (p-tag),'&',exception);
2007 }
2008 else
2009 if (*p == '\0')
2010 break;
2011 }
2012 utf8=DestroyString(utf8);
2013 if (root->node == (XMLTreeInfo *) NULL)
2014 return(&root->root);
2015 if (root->node->tag == (char *) NULL)
2016 {
2017 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2018 "ParseError","root tag missing");
2019 return(&root->root);
2020 }
2021 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2022 "ParseError","unclosed tag: '%s'",root->node->tag);
2023 return(&root->root);
2024}
2025␌
2026/*
2027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028% %
2029% %
2030% %
2031% N e w X M L T r e e T a g %
2032% %
2033% %
2034% %
2035%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2036%
2037% NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
2038%
2039% The format of the NewXMLTreeTag method is:
2040%
2041% XMLTreeInfo *NewXMLTreeTag(const char *tag)
2042%
2043% A description of each parameter follows:
2044%
2045% o tag: the tag.
2046%
2047*/
2048MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
2049{
2050 static const char
2051 *predefined_entities[NumberPredefinedEntities+1] =
2052 {
2053 "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
2054 "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
2055 };
2056
2057 XMLTreeRoot
2058 *root;
2059
2060 root=(XMLTreeRoot *) AcquireCriticalMemory(sizeof(*root));
2061 (void) memset(root,0,sizeof(*root));
2062 root->root.tag=(char *) NULL;
2063 if (tag != (char *) NULL)
2064 root->root.tag=ConstantString(tag);
2065 root->node=(&root->root);
2066 root->root.content=ConstantString("");
2067 root->entities=(char **) AcquireCriticalMemory(sizeof(predefined_entities));
2068 (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
2069 root->root.attributes=sentinel;
2070 root->attributes=(char ***) root->root.attributes;
2071 root->processing_instructions=(char ***) root->root.attributes;
2072 root->debug=IsEventLogging();
2073 root->signature=MagickCoreSignature;
2074 return(&root->root);
2075}
2076␌
2077/*
2078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2079% %
2080% %
2081% %
2082% S e t X M L T r e e C o n t e n t %
2083% %
2084% %
2085% %
2086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2087%
2088% SetXMLTreeContent() sets the character content for the given tag and
2089% returns the tag.
2090%
2091% The format of the SetXMLTreeContent method is:
2092%
2093% XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2094% const char *content)
2095%
2096% A description of each parameter follows:
2097%
2098% o xml_info: the xml info.
2099%
2100% o content: The content.
2101%
2102*/
2103MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2104 const char *content)
2105{
2106 assert(xml_info != (XMLTreeInfo *) NULL);
2107 assert((xml_info->signature == MagickCoreSignature) ||
2108 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2109 if (IsEventLogging() != MagickFalse)
2110 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2111 if (xml_info->content != (char *) NULL)
2112 xml_info->content=DestroyString(xml_info->content);
2113 xml_info->content=(char *) ConstantString(content);
2114 return(xml_info);
2115}
2116␌
2117/*
2118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119% %
2120% %
2121% %
2122% S k i p X M L C o m m e n t %
2123% %
2124% %
2125% %
2126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2127%
2128% SkipXMLComment() skips leading whitespace and XML comments, leaving the
2129% next token untouched. It returns MagickFalse for an unterminated comment.
2130%
2131*/
2132MagickPrivate MagickBooleanType SkipXMLComment(const char **xml)
2133{
2134 const char
2135 *p;
2136
2137 for (p=*xml; ; )
2138 {
2139 while (isspace((int) ((unsigned char) *p)) != 0)
2140 p++;
2141 if (strncmp(p,"<!--",4) != 0)
2142 break;
2143 *xml=p;
2144 p=strstr(p+4,"-->");
2145 if (p == (const char *) NULL)
2146 return(MagickFalse);
2147 p+=3;
2148 }
2149 *xml=p;
2150 return(MagickTrue);
2151}
2152␌
2153/*
2154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2155% %
2156% %
2157% %
2158% S k i p X M L D O C T Y P E %
2159% %
2160% %
2161% %
2162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2163%
2164% SkipXMLDocType() skips a DOCTYPE declaration, including its internal subset.
2165%
2166*/
2167MagickPrivate MagickBooleanType SkipXMLDocType(const char **xml)
2168{
2169 const char
2170 *p;
2171
2172 int
2173 bracket_depth,
2174 quote;
2175
2176 bracket_depth=0;
2177 quote=0;
2178 for (p=*xml; *p != '\0'; )
2179 {
2180 if ((quote == 0) && (strncmp(p,"<!--",4) == 0))
2181 {
2182 if (SkipXMLComment(&p) == MagickFalse)
2183 break;
2184 continue;
2185 }
2186 if (quote != 0)
2187 {
2188 if (*p == quote)
2189 quote=0;
2190 }
2191 else
2192 {
2193 if ((*p == '"') || (*p == '\''))
2194 quote=(*p);
2195 else
2196 if (*p == '[')
2197 bracket_depth++;
2198 else
2199 if (*p == ']')
2200 {
2201 if (bracket_depth > 0)
2202 bracket_depth--;
2203 }
2204 else
2205 if ((*p == '>') && (bracket_depth == 0))
2206 {
2207 *xml=p+1;
2208 return(MagickTrue);
2209 }
2210 }
2211 p++;
2212 }
2213 *xml=p;
2214 return(MagickFalse);
2215}
2216␌
2217/*
2218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2219% %
2220% %
2221% %
2222% S u b s t i t u t e X M L E n t i t i e s %
2223% %
2224% %
2225% %
2226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2227%
2228% SubstituteXMLEntities() substitutes the predefined XML entities.
2229%
2230*/
2231MagickExport char *SubstituteXMLEntities(const char *content,
2232 const MagickBooleanType pedantic)
2233{
2234 char
2235 *canonical_content;
2236
2237 const char
2238 *p;
2239
2240 size_t
2241 extent;
2242
2243 ssize_t
2244 i;
2245
2246 /*
2247 Substitute predefined entities.
2248 */
2249 i=0;
2250 canonical_content=AcquireString((char *) NULL);
2251 extent=MagickPathExtent;
2252 for (p=content; *p != '\0'; p++)
2253 {
2254 if ((i+MagickPathExtent) > (ssize_t) extent)
2255 {
2256 extent+=MagickPathExtent;
2257 canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
2258 sizeof(*canonical_content));
2259 if (canonical_content == (char *) NULL)
2260 return(canonical_content);
2261 }
2262 switch (*p)
2263 {
2264 case '&':
2265 {
2266 i+=FormatLocaleString(canonical_content+i,extent,"&amp;");
2267 break;
2268 }
2269 case '<':
2270 {
2271 i+=FormatLocaleString(canonical_content+i,extent,"&lt;");
2272 break;
2273 }
2274 case '>':
2275 {
2276 i+=FormatLocaleString(canonical_content+i,extent,"&gt;");
2277 break;
2278 }
2279 case '"':
2280 {
2281 i+=FormatLocaleString(canonical_content+i,extent,"&quot;");
2282 break;
2283 }
2284 case '\n':
2285 {
2286 if (pedantic == MagickFalse)
2287 {
2288 canonical_content[i++]=(char) (*p);
2289 break;
2290 }
2291 i+=FormatLocaleString(canonical_content+i,extent,"&#xA;");
2292 break;
2293 }
2294 case '\t':
2295 {
2296 if (pedantic == MagickFalse)
2297 {
2298 canonical_content[i++]=(char) (*p);
2299 break;
2300 }
2301 i+=FormatLocaleString(canonical_content+i,extent,"&#x9;");
2302 break;
2303 }
2304 case '\r':
2305 {
2306 i+=FormatLocaleString(canonical_content+i,extent,"&#xD;");
2307 break;
2308 }
2309 default:
2310 {
2311 canonical_content[i++]=(char) (*p);
2312 break;
2313 }
2314 }
2315 }
2316 canonical_content[i]='\0';
2317 return(canonical_content);
2318}
2319␌
2320/*
2321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2322% %
2323% %
2324% %
2325% X M L T r e e I n f o T o X M L %
2326% %
2327% %
2328% %
2329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2330%
2331% XMLTreeInfoToXML() converts an xml-tree to an XML string.
2332%
2333% The format of the XMLTreeInfoToXML method is:
2334%
2335% char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2336%
2337% A description of each parameter follows:
2338%
2339% o xml_info: the xml info.
2340%
2341*/
2342
2343static char *EncodePredefinedEntities(const char *source,ssize_t offset,
2344 char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
2345{
2346 char
2347 *canonical_content;
2348
2349 if (offset < 0)
2350 canonical_content=CanonicalXMLContent(source,pedantic);
2351 else
2352 {
2353 char
2354 *content;
2355
2356 content=AcquireString(source);
2357 content[offset]='\0';
2358 canonical_content=CanonicalXMLContent(content,pedantic);
2359 content=DestroyString(content);
2360 }
2361 if (canonical_content == (char *) NULL)
2362 return(*destination);
2363 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2364 {
2365 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2366 *destination=(char *) ResizeQuantumMemory(*destination,*extent,
2367 sizeof(**destination));
2368 if (*destination == (char *) NULL)
2369 return(*destination);
2370 }
2371 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,"%s",
2372 canonical_content);
2373 canonical_content=DestroyString(canonical_content);
2374 return(*destination);
2375}
2376
2377static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
2378 size_t *extent,size_t start,char ***attributes)
2379{
2380 char
2381 *content;
2382
2383 const char
2384 *attribute;
2385
2386 size_t
2387 offset;
2388
2389 ssize_t
2390 i,
2391 j;
2392
2393 content=(char *) "";
2394 if (xml_info->parent != (XMLTreeInfo *) NULL)
2395 content=xml_info->parent->content;
2396 offset=0;
2397 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2398 start),source,length,extent,MagickFalse);
2399 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2400 {
2401 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2402 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2403 if (*source == (char *) NULL)
2404 return(*source);
2405 }
2406 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2407 "<%s",xml_info->tag);
2408 for (i=0; xml_info->attributes[i]; i+=2)
2409 {
2410 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2411 if (attribute != xml_info->attributes[i+1])
2412 continue;
2413 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2414 {
2415 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2416 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2417 if (*source == (char *) NULL)
2418 return((char *) NULL);
2419 }
2420 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2421 xml_info->attributes[i]);
2422 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2423 extent,MagickTrue);
2424 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2425 }
2426 i=0;
2427 while ((attributes[i] != (char **) NULL) &&
2428 (strcmp(attributes[i][0],xml_info->tag) != 0))
2429 i++;
2430 j=1;
2431 while ((attributes[i] != (char **) NULL) &&
2432 (attributes[i][j] != (char *) NULL))
2433 {
2434 if ((attributes[i][j+1] == (char *) NULL) ||
2435 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2436 {
2437 j+=3;
2438 continue;
2439 }
2440 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2441 {
2442 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2443 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2444 if (*source == (char *) NULL)
2445 return((char *) NULL);
2446 }
2447 *length+=(size_t) FormatLocaleString(*source+(*length),*extent," %s=\"",
2448 attributes[i][j]);
2449 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2450 MagickTrue);
2451 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"\"");
2452 j+=3;
2453 }
2454 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2455 *xml_info->content ? ">" : "/>");
2456 if (xml_info->child != (XMLTreeInfo *) NULL)
2457 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2458 else
2459 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2460 MagickFalse);
2461 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2462 {
2463 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2464 *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
2465 if (*source == (char *) NULL)
2466 return((char *) NULL);
2467 }
2468 if (*xml_info->content != '\0')
2469 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,"</%s>",
2470 xml_info->tag);
2471 while ((offset < xml_info->offset) && (content[offset] != '\0'))
2472 offset++;
2473 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2474 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2475 attributes);
2476 else
2477 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2478 MagickFalse);
2479 return(content);
2480}
2481
2482MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2483{
2484 char
2485 *p,
2486 *q,
2487 *xml;
2488
2489 size_t
2490 extent,
2491 length;
2492
2493 ssize_t
2494 i,
2495 j,
2496 k;
2497
2498 XMLTreeInfo
2499 *ordered,
2500 *parent;
2501
2502 XMLTreeRoot
2503 *root;
2504
2505 assert(xml_info != (XMLTreeInfo *) NULL);
2506 assert((xml_info->signature == MagickCoreSignature) ||
2507 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2508 if (IsEventLogging() != MagickFalse)
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2510 if (xml_info->tag == (char *) NULL)
2511 return((char *) NULL);
2512 xml=AcquireString((char *) NULL);
2513 length=0;
2514 extent=MagickPathExtent;
2515 root=(XMLTreeRoot *) xml_info;
2516 while (root->root.parent != (XMLTreeInfo *) NULL)
2517 root=(XMLTreeRoot *) root->root.parent;
2518 parent=xml_info->parent;
2519 if (parent == (XMLTreeInfo *) NULL)
2520 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2521 {
2522 /*
2523 Pre-root processing instructions.
2524 */
2525 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2526 p=root->processing_instructions[i][1];
2527 for (j=1; p != (char *) NULL; j++)
2528 {
2529 if (root->processing_instructions[i][k][j-1] == '>')
2530 {
2531 p=root->processing_instructions[i][j];
2532 continue;
2533 }
2534 q=root->processing_instructions[i][0];
2535 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2536 {
2537 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2538 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2539 if (xml == (char *) NULL)
2540 return(xml);
2541 }
2542 length+=(size_t) FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
2543 *p != '\0' ? " " : "",p);
2544 p=root->processing_instructions[i][j];
2545 }
2546 }
2547 ordered=xml_info->ordered;
2548 xml_info->parent=(XMLTreeInfo *) NULL;
2549 xml_info->ordered=(XMLTreeInfo *) NULL;
2550 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2551 xml_info->parent=parent;
2552 xml_info->ordered=ordered;
2553 if (parent == (XMLTreeInfo *) NULL)
2554 for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
2555 {
2556 /*
2557 Post-root processing instructions.
2558 */
2559 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2560 p=root->processing_instructions[i][1];
2561 for (j=1; p != (char *) NULL; j++)
2562 {
2563 if (root->processing_instructions[i][k][j-1] == '<')
2564 {
2565 p=root->processing_instructions[i][j];
2566 continue;
2567 }
2568 q=root->processing_instructions[i][0];
2569 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2570 {
2571 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2572 xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
2573 if (xml == (char *) NULL)
2574 return(xml);
2575 }
2576 length+=(size_t) FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
2577 *p != '\0' ? " " : "",p);
2578 p=root->processing_instructions[i][j];
2579 }
2580 }
2581 return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
2582}