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