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