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