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"
71#define NumberPredefinedEntities 10
72#define XMLWhitespace "\t\r\n "
119 ***processing_instructions,
138 *sentinel[] = { (
char *) NULL };
169static XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
170 XMLTreeInfo *child,
const size_t offset)
177 child->ordered=(XMLTreeInfo *) NULL;
178 child->sibling=(XMLTreeInfo *) NULL;
179 child->next=(XMLTreeInfo *) NULL;
180 child->offset=offset;
181 child->parent=xml_info;
182 if (xml_info->child == (XMLTreeInfo *) NULL)
184 xml_info->child=child;
187 head=xml_info->child;
188 if (head->offset > offset)
191 xml_info->child=child;
196 while ((node->ordered != (XMLTreeInfo *) NULL) &&
197 (node->ordered->offset <= offset))
199 child->ordered=node->ordered;
202 previous=(XMLTreeInfo *) NULL;
204 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
209 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
211 while ((node->next != (XMLTreeInfo *) NULL) &&
212 (node->next->offset <= offset))
214 child->next=node->next;
219 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
220 previous->sibling=node->sibling;
222 previous=(XMLTreeInfo *) NULL;
224 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
230 if (previous != (XMLTreeInfo *) NULL)
231 previous->sibling=child;
236MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
237 const char *tag,
const size_t offset)
242 if (xml_info == (XMLTreeInfo *) NULL)
243 return((XMLTreeInfo *) NULL);
244 child=(XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
245 if (child == (XMLTreeInfo *) NULL)
246 return((XMLTreeInfo *) NULL);
247 (void) memset(child,0,
sizeof(*child));
248 child->tag=ConstantString(tag);
249 child->attributes=sentinel;
250 child->content=ConstantString(
"");
251 child->debug=IsEventLogging();
252 child->signature=MagickCoreSignature;
253 return(InsertTagIntoXMLTree(xml_info,child,offset));
285static char *CanonicalXMLContent(
const char *content,
286 const MagickBooleanType pedantic)
301 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
302 if (utf8 == (
unsigned char *) NULL)
303 return((
char *) NULL);
304 for (p=utf8; *p !=
'\0'; p++)
305 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
312 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
313 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
314 if (base64 == (
char *) NULL)
315 return((
char *) NULL);
316 canonical_content=AcquireString(
"<base64>");
317 (void) ConcatenateString(&canonical_content,base64);
318 base64=DestroyString(base64);
319 (void) ConcatenateString(&canonical_content,
"</base64>");
320 return(canonical_content);
322 canonical_content=SubstituteXMLEntities((
const char *) utf8,pedantic);
323 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
324 return(canonical_content);
327MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
328 const char *path,
const size_t offset)
332 subnode[MagickPathExtent],
333 tag[MagickPathExtent];
346 assert(xml_info != (XMLTreeInfo *) NULL);
347 assert((xml_info->signature == MagickCoreSignature) ||
348 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
349 if (IsEventLogging() != MagickFalse)
350 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
352 components=GetPathComponents(path,&number_components);
353 if (components == (
char **) NULL)
354 return((XMLTreeInfo *) NULL);
355 for (i=0; i < (ssize_t) number_components; i++)
357 GetPathComponent(components[i],SubimagePath,subnode);
358 GetPathComponent(components[i],CanonicalPath,tag);
359 child=GetXMLTreeChild(node,tag);
360 if (child == (XMLTreeInfo *) NULL)
361 child=AddChildToXMLTree(node,tag,offset);
363 if (node == (XMLTreeInfo *) NULL)
365 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
368 if (node == (XMLTreeInfo *) NULL)
371 if (node == (XMLTreeInfo *) NULL)
373 components[i]=DestroyString(components[i]);
375 for ( ; i < (ssize_t) number_components; i++)
376 components[i]=DestroyString(components[i]);
377 components=(
char **) RelinquishMagickMemory(components);
404static char **DestroyXMLTreeAttributes(
char **attributes)
412 if ((attributes == (
char **) NULL) || (attributes == sentinel))
413 return((
char **) NULL);
414 for (i=0; attributes[i] != (
char *) NULL; i+=2)
419 if (attributes[i] != (
char *) NULL)
420 attributes[i]=DestroyString(attributes[i]);
421 if (attributes[i+1] != (
char *) NULL)
422 attributes[i+1]=DestroyString(attributes[i+1]);
424 attributes=(
char **) RelinquishMagickMemory(attributes);
425 return((
char **) NULL);
428static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
434 child=xml_info->child;
435 while (child != (XMLTreeInfo *) NULL)
439 node->child=(XMLTreeInfo *) NULL;
440 (void) DestroyXMLTree(node);
444static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
450 ordered=xml_info->ordered;
451 while (ordered != (XMLTreeInfo *) NULL)
454 ordered=node->ordered;
455 node->ordered=(XMLTreeInfo *) NULL;
456 (void) DestroyXMLTree(node);
460static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
472 assert(xml_info != (XMLTreeInfo *) NULL);
473 assert((xml_info->signature == MagickCoreSignature) ||
474 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
475 if (IsEventLogging() != MagickFalse)
476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
477 if (xml_info->parent != (XMLTreeInfo *) NULL)
482 root=(XMLTreeRoot *) xml_info;
483 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
484 root->entities[i+1]=DestroyString(root->entities[i+1]);
485 root->entities=(
char **) RelinquishMagickMemory(root->entities);
486 for (i=0; root->attributes[i] != (
char **) NULL; i++)
488 attributes=root->attributes[i];
489 if (attributes[0] != (
char *) NULL)
490 attributes[0]=DestroyString(attributes[0]);
491 for (j=1; attributes[j] != (
char *) NULL; j+=3)
493 if (attributes[j] != (
char *) NULL)
494 attributes[j]=DestroyString(attributes[j]);
495 if (attributes[j+1] != (
char *) NULL)
496 attributes[j+1]=DestroyString(attributes[j+1]);
497 if (attributes[j+2] != (
char *) NULL)
498 attributes[j+2]=DestroyString(attributes[j+2]);
500 attributes=(
char **) RelinquishMagickMemory(attributes);
502 if (root->attributes[0] != (
char **) NULL)
503 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
504 if (root->processing_instructions[0] != (
char **) NULL)
506 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
508 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
509 root->processing_instructions[i][j]=DestroyString(
510 root->processing_instructions[i][j]);
511 root->processing_instructions[i][j+1]=DestroyString(
512 root->processing_instructions[i][j+1]);
513 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
514 root->processing_instructions[i]);
516 root->processing_instructions=(
char ***) RelinquishMagickMemory(
517 root->processing_instructions);
521MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
523 assert(xml_info != (XMLTreeInfo *) NULL);
524 assert((xml_info->signature == MagickCoreSignature) ||
525 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
526 if (IsEventLogging() != MagickFalse)
527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
528 DestroyXMLTreeChild(xml_info);
529 DestroyXMLTreeOrdered(xml_info);
530 DestroyXMLTreeRoot(xml_info);
531 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
532 xml_info->content=DestroyString(xml_info->content);
533 xml_info->tag=DestroyString(xml_info->tag);
534 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
535 return((XMLTreeInfo *) NULL);
562MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
583 assert(filename != (
const char *) NULL);
586 if (LocaleCompare(filename,
"-") != 0)
587 file=open_utf8(filename,O_RDONLY | O_CLOEXEC | O_BINARY,0);
589 return((
char *) NULL);
590 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
592 if ((file == fileno(stdin)) || (offset < 0) ||
593 (offset != (MagickOffsetType) ((ssize_t) offset)))
604 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
605 quantum=(size_t) MagickMaxBufferExtent;
606 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
607 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
608 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
609 for (i=0; xml != (
char *) NULL; i+=(size_t) count)
611 count=MagickRead(file,xml+i,quantum);
614 if (~((
size_t) i) < (quantum+1))
616 xml=(
char *) RelinquishMagickMemory(xml);
619 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
620 if ((i+(
size_t) count) >= extent)
623 if (LocaleCompare(filename,
"-") != 0)
624 file=close_utf8(file);
625 if (xml == (
char *) NULL)
626 return((
char *) NULL);
629 xml=(
char *) RelinquishMagickMemory(xml);
630 return((
char *) NULL);
632 length=MagickMin(i+(
size_t) count,extent);
636 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
638 if (~length >= (MagickPathExtent-1))
639 xml=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*xml));
640 if (xml == (
char *) NULL)
642 file=close_utf8(file);
643 return((
char *) NULL);
645 map=MapBlob(file,ReadMode,0,length);
646 if (map != (
char *) NULL)
648 (void) memcpy(xml,map,length);
649 (void) UnmapBlob(map,length);
653 (void) lseek(file,0,SEEK_SET);
654 for (i=0; i < length; i+=(size_t) count)
656 count=MagickRead(file,xml+i,(
size_t) MagickMin(length-i,(
size_t)
657 MagickMaxBufferExtent));
663 file=close_utf8(file)-1;
664 xml=(
char *) RelinquishMagickMemory(xml);
665 return((
char *) NULL);
669 if (LocaleCompare(filename,
"-") != 0)
670 file=close_utf8(file);
672 xml=(
char *) RelinquishMagickMemory(xml);
698MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
700 assert(xml_info != (XMLTreeInfo *) NULL);
701 assert((xml_info->signature == MagickCoreSignature) ||
702 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
703 if (IsEventLogging() != MagickFalse)
704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
705 return(xml_info->next);
733MagickExport
const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
743 assert(xml_info != (XMLTreeInfo *) NULL);
744 assert((xml_info->signature == MagickCoreSignature) ||
745 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
746 if (IsEventLogging() != MagickFalse)
747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
748 if (xml_info->attributes == (
char **) NULL)
749 return((
const char *) NULL);
751 while ((xml_info->attributes[i] != (
char *) NULL) &&
752 (strcmp(xml_info->attributes[i],tag) != 0))
754 if (xml_info->attributes[i] != (
char *) NULL)
755 return(xml_info->attributes[i+1]);
756 root=(XMLTreeRoot*) xml_info;
757 while (root->root.parent != (XMLTreeInfo *) NULL)
758 root=(XMLTreeRoot *) root->root.parent;
760 while ((root->attributes[i] != (
char **) NULL) &&
761 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
763 if (root->attributes[i] == (
char **) NULL)
764 return((
const char *) NULL);
766 while ((root->attributes[i][j] != (
char *) NULL) &&
767 (strcmp(root->attributes[i][j],tag) != 0))
769 if (root->attributes[i][j] == (
char *) NULL)
770 return((
const char *) NULL);
771 return(root->attributes[i][j+1]);
797MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
802 assert(xml_info != (XMLTreeInfo *) NULL);
803 assert((xml_info->signature == MagickCoreSignature) ||
804 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
805 if (IsEventLogging() != MagickFalse)
806 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
807 child=xml_info->child;
808 if (tag != (
const char *) NULL)
809 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
810 child=child->sibling;
837MagickExport
const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
839 assert(xml_info != (XMLTreeInfo *) NULL);
840 assert((xml_info->signature == MagickCoreSignature) ||
841 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
842 if (IsEventLogging() != MagickFalse)
843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
844 return(xml_info->content);
869MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
871 assert(xml_info != (XMLTreeInfo *) NULL);
872 assert((xml_info->signature == MagickCoreSignature) ||
873 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
874 if (IsEventLogging() != MagickFalse)
875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
876 return(xml_info->sibling);
901MagickExport
const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
903 assert(xml_info != (XMLTreeInfo *) NULL);
904 assert((xml_info->signature == MagickCoreSignature) ||
905 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
906 if (IsEventLogging() != MagickFalse)
907 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
908 return(xml_info->tag);
937static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
955 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
956 if (utf8 == (
char *) NULL)
957 return((
char *) NULL);
958 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
964 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
970 for (i=2; i < (ssize_t) (*length-1); i+=2)
972 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
973 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
974 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
976 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
977 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
979 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
981 if ((
size_t) (j+MagickPathExtent) > extent)
983 extent=(size_t) j+MagickPathExtent;
984 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
985 if (utf8 == (
char *) NULL)
998 for (bits=0;
byte != 0;
byte/=2)
1001 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1005 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1010 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1011 if (utf8 != (
char *) NULL)
1016static char *ParseEntities(
char *xml,
char **entities,
int state,
1017 ExceptionInfo *exception)
1041 for ( ; *xml !=
'\0'; xml++)
1042 while (*xml ==
'\r')
1046 (void) memmove(xml,xml+1,strlen(xml));
1050 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1051 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1063 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1069 c=(int) strtol(xml+2,&entity,10);
1071 c=(int) strtol(xml+3,&entity,16);
1072 if ((c == 0) || (*entity !=
';'))
1088 for (i=0;
byte != 0;
byte/=2)
1091 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1096 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1100 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1103 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1104 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1110 while ((entities[i] != (
char *) NULL) &&
1111 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1113 if (entities[i++] == (
char *) NULL)
1116 if (entities[i] != (
char *) NULL)
1121 length=strlen(entities[i]);
1122 entity=strchr(xml,
';');
1123 if ((entity != (
char *) NULL) &&
1124 ((length-1L) >= (
size_t) (entity-xml)))
1126 offset=(ssize_t) (xml-p);
1127 extent=((size_t) offset+length+strlen(entity));
1130 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1131 if (p != (
char *) NULL)
1139 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1140 sizeof(*extent_xml));
1141 if (extent_xml != (
char *) NULL)
1143 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1144 (void) CopyMagickString(extent_xml,p,extent*
1145 sizeof(*extent_xml));
1149 if (p == (
char *) NULL)
1151 (void) ThrowMagickException(exception,GetMagickModule(),
1152 OptionWarning,
"ParseError",
1153 "memory allocation failed");
1156 if (strlen(p) > GetMaxProfileSize())
1158 (void) ThrowMagickException(exception,GetMagickModule(),
1159 ResourceLimitError,
"ProfileSizeExceedsLimit",
1160 "`%.17g'",(
double) strlen(p));
1161 return(p == q ? ConstantString(p) : p);
1164 entity=strchr(xml,
';');
1166 if (entity != (
char *) NULL)
1167 (void) memmove(xml+length,entity+1,strlen(entity));
1168 (void) memcpy(xml,entities[i],length);
1172 if (((state ==
' ') || (state ==
'*')) &&
1173 (isspace((
int) ((
unsigned char) *xml)) != 0))
1183 for (xml=p; *xml !=
'\0'; xml++)
1188 i=(ssize_t) strspn(xml,accept);
1190 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1191 while ((*xml !=
'\0') && (*xml !=
' '))
1197 if ((xml >= p) && (*xml ==
' '))
1200 return(p == q ? ConstantString(p) : p);
1203static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1204 const size_t length,
const char state,ExceptionInfo *exception)
1209 xml_info=root->node;
1210 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1214 xml=ParseEntities(xml,root->entities,state,exception);
1215 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1217 (void) ConcatenateString(&xml_info->content,xml);
1218 xml=DestroyString(xml);
1222 if (xml_info->content != (
char *) NULL)
1223 xml_info->content=DestroyString(xml_info->content);
1224 xml_info->content=xml;
1228static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1229 ExceptionInfo *exception)
1231 if ((root->node == (XMLTreeInfo *) NULL) ||
1232 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1234 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1235 "ParseError",
"unexpected closing tag </%s>",tag);
1236 return(&root->root);
1238 root->node=root->node->parent;
1240 return((XMLTreeInfo *) NULL);
1243static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1244 const int delimiter,
const size_t depth,
char **entities)
1252 if (depth >= MagickMaxRecursionDepth)
1253 return(MagickFalse);
1256 while ((*xml !=
'\0') && (*xml != delimiter))
1260 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1261 return(MagickFalse);
1263 while ((entities[i] != (
char *) NULL) &&
1264 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1266 if ((entities[i] != (
char *) NULL) &&
1267 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1268 return(MagickFalse);
1272static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1284 xml+=strcspn(xml,XMLWhitespace);
1288 xml+=strspn(xml+1,XMLWhitespace)+1;
1290 if (strcmp(target,
"xml") == 0)
1292 xml=strstr(xml,
"standalone");
1293 if ((xml != (
char *) NULL) &&
1294 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1295 root->standalone=MagickTrue;
1298 if (root->processing_instructions[0] == (
char **) NULL)
1300 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1301 *root->processing_instructions));
1302 *root->processing_instructions=(
char **) NULL;
1305 while ((root->processing_instructions[i] != (
char **) NULL) &&
1306 (strcmp(target,root->processing_instructions[i][0]) != 0))
1308 if (root->processing_instructions[i] == (
char **) NULL)
1310 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1311 root->processing_instructions,(
size_t) (i+2),
1312 sizeof(*root->processing_instructions));
1313 if (root->processing_instructions == (
char ***) NULL)
1314 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1315 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1316 sizeof(**root->processing_instructions));
1317 if (root->processing_instructions[i] == (
char **) NULL)
1318 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1319 root->processing_instructions[i+1]=(
char **) NULL;
1320 root->processing_instructions[i][0]=ConstantString(target);
1321 root->processing_instructions[i][1]=(
char *)
1322 root->processing_instructions[i+1];
1323 root->processing_instructions[i+1]=(
char **) NULL;
1324 root->processing_instructions[i][2]=ConstantString(
"");
1327 while (root->processing_instructions[i][j] != (
char *) NULL)
1329 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1330 root->processing_instructions[i],(
size_t) (j+3),
1331 sizeof(**root->processing_instructions));
1332 if (root->processing_instructions[i] == (
char **) NULL)
1333 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1334 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1335 root->processing_instructions[i][j+1],(
size_t) (j+1),
1336 sizeof(***root->processing_instructions));
1337 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1338 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1339 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1340 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1341 root->processing_instructions[i][j]=ConstantString(xml);
1342 root->processing_instructions[i][j+1]=(
char *) NULL;
1345static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1346 size_t length,ExceptionInfo *exception)
1348#define DestroyXMLEntities(predefined_entities) \
1351 for (k=0; predefined_entities[k] != (char *) NULL; k++) \
1352 if ((k & 0x01) != 0) \
1353 predefined_entities[k]=DestroyString(predefined_entities[k]); \
1354 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities); \
1361 **predefined_entities,
1371 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1372 if (predefined_entities == (
char **) NULL)
1373 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1374 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1375 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1377 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1381 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1386 if (strspn(xml+8,XMLWhitespace) == 0)
1388 xml+=strspn(xml+8,XMLWhitespace)+8;
1390 n=xml+strspn(xml,XMLWhitespace
"%");
1391 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1393 xml=n+strcspn(n,XMLWhitespace);
1397 v=xml+strspn(xml+1,XMLWhitespace)+1;
1400 if ((q !=
'"') && (q !=
'\''))
1405 xml=strchr(xml,
'>');
1408 entities=(*c ==
'%') ? predefined_entities : root->entities;
1409 for (i=0; entities[i] != (
char *) NULL; i++) ;
1410 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1412 if (entities == (
char **) NULL)
1413 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1415 predefined_entities=entities;
1417 root->entities=entities;
1421 if (xml != (
char *) NULL)
1426 entities[i+1]=ParseEntities(v,predefined_entities,
'%',exception);
1427 entities[i+2]=(
char *) NULL;
1428 if ((ValidateEntities(n,entities[i+1],
'%',0,entities) != MagickFalse) &&
1429 (ValidateEntities(n,entities[i+1],
'&',0,entities) != MagickFalse))
1433 if (entities[i+1] != v)
1434 entities[i+1]=DestroyString(entities[i+1]);
1435 (void) ThrowMagickException(exception,GetMagickModule(),
1436 OptionWarning,
"ParseError",
"circular entity declaration %s",n);
1437 DestroyXMLEntities(predefined_entities);
1438 return(MagickFalse);
1442 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1447 t=xml+strspn(xml+9,XMLWhitespace)+9;
1450 (void) ThrowMagickException(exception,GetMagickModule(),
1451 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1452 DestroyXMLEntities(predefined_entities);
1453 return(MagickFalse);
1455 xml=t+strcspn(t,XMLWhitespace
">");
1460 while ((root->attributes[i] != (
char **) NULL) &&
1461 (n != (
char *) NULL) &&
1462 (strcmp(n,root->attributes[i][0]) != 0))
1464 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1467 xml=n+strcspn(n,XMLWhitespace);
1472 (void) ThrowMagickException(exception,GetMagickModule(),
1473 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1474 DestroyXMLEntities(predefined_entities);
1475 return(MagickFalse);
1477 xml+=strspn(xml+1,XMLWhitespace)+1;
1478 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1479 if (strncmp(xml,
"NOTATION",8) == 0)
1480 xml+=strspn(xml+8,XMLWhitespace)+8;
1481 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1482 strcspn(xml,XMLWhitespace);
1483 if (xml == (
char *) NULL)
1485 (void) ThrowMagickException(exception,GetMagickModule(),
1486 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1487 DestroyXMLEntities(predefined_entities);
1488 return(MagickFalse);
1490 xml+=strspn(xml,XMLWhitespace
")");
1491 if (strncmp(xml,
"#FIXED",6) == 0)
1492 xml+=strspn(xml+6,XMLWhitespace)+6;
1495 xml+=strcspn(xml,XMLWhitespace
">")-1;
1501 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1502 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1506 (void) ThrowMagickException(exception,GetMagickModule(),
1507 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1508 DestroyXMLEntities(predefined_entities);
1509 return(MagickFalse);
1511 if (root->attributes[i] == (
char **) NULL)
1517 root->attributes=(
char ***) AcquireQuantumMemory(2,
1518 sizeof(*root->attributes));
1520 root->attributes=(
char ***) ResizeQuantumMemory(
1521 root->attributes,(
size_t) (i+2),
1522 sizeof(*root->attributes));
1523 if (root->attributes == (
char ***) NULL)
1524 ThrowFatalException(ResourceLimitFatalError,
1525 "MemoryAllocationFailed");
1526 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1527 sizeof(**root->attributes));
1528 if (root->attributes[i] == (
char **) NULL)
1529 ThrowFatalException(ResourceLimitFatalError,
1530 "MemoryAllocationFailed");
1531 root->attributes[i][0]=ConstantString(t);
1532 root->attributes[i][1]=(
char *) NULL;
1533 root->attributes[i+1]=(
char **) NULL;
1535 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1536 root->attributes[i]=(
char **) ResizeQuantumMemory(
1537 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1538 if (root->attributes[i] == (
char **) NULL)
1539 ThrowFatalException(ResourceLimitFatalError,
1540 "MemoryAllocationFailed");
1541 root->attributes[i][j+3]=(
char *) NULL;
1542 root->attributes[i][j+2]=ConstantString(c);
1543 root->attributes[i][j+1]=(
char *) NULL;
1544 if (v != (
char *) NULL)
1545 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c,
1547 root->attributes[i][j]=ConstantString(n);
1551 if (strncmp(xml,
"<!--", 4) == 0)
1552 xml=strstr(xml+4,
"-->");
1554 if (strncmp(xml,
"<?", 2) == 0)
1558 if (xml != (
char *) NULL)
1560 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1566 xml=strchr(xml,
'>');
1568 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1571 DestroyXMLEntities(predefined_entities);
1575static MagickBooleanType ParseOpenTag(XMLTreeRoot *root,
char *tag,
1576 char **attributes,ExceptionInfo *exception)
1581 if (root->depth >= MagickMaxRecursionDepth)
1583 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1584 "ParseError",
"unexpected open tag </%s>",tag);
1585 return(MagickFalse);
1587 xml_info=root->node;
1588 if (xml_info->tag == (
char *) NULL)
1589 xml_info->tag=ConstantString(tag);
1591 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1592 if (xml_info != (XMLTreeInfo *) NULL)
1593 xml_info->attributes=attributes;
1594 root->node=xml_info;
1607static inline MagickBooleanType IsSkipTag(
const char *tag)
1613 while (ignore_tags[i] != (
const char *) NULL)
1615 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1619 return(MagickFalse);
1622MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1653 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1655 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1656 "ParseError",
"root tag missing");
1657 return((XMLTreeInfo *) NULL);
1659 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1661 utf8=ConvertUTF16ToUTF8(xml,&length);
1662 if (utf8 == (
char *) NULL)
1664 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1665 "ParseError",
"UTF16 to UTF8 failed");
1666 return((XMLTreeInfo *) NULL);
1670 utf8=DestroyString(utf8);
1671 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1672 "ParseError",
"root tag missing");
1673 return((XMLTreeInfo *) NULL);
1675 terminal=utf8[length-1];
1676 utf8[length-1]=
'\0';
1678 while ((*p !=
'\0') && (*p !=
'<'))
1682 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1683 "ParseError",
"root tag missing");
1684 utf8=DestroyString(utf8);
1685 return((XMLTreeInfo *) NULL);
1687 attribute=(
char **) NULL;
1692 attributes=(
char **) sentinel;
1695 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1696 (*p ==
':') || (c <
'\0'))
1701 if (root->node == (XMLTreeInfo *) NULL)
1703 (void) ThrowMagickException(exception,GetMagickModule(),
1704 OptionWarning,
"ParseError",
"root tag missing");
1705 utf8=DestroyString(utf8);
1706 return(&root->root);
1708 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1709 while (isspace((
int) ((
unsigned char) *p)) != 0)
1711 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1712 (ignore_depth == 0))
1714 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1720 while ((root->attributes[i] != (
char **) NULL) &&
1721 (strcmp(root->attributes[i][0],tag) != 0))
1723 attribute=root->attributes[i];
1725 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1731 attributes=(
char **) AcquireQuantumMemory(4,
1732 sizeof(*attributes));
1734 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1735 (l+4),
sizeof(*attributes));
1736 if (attributes == (
char **) NULL)
1738 (void) ThrowMagickException(exception,GetMagickModule(),
1739 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1740 utf8=DestroyString(utf8);
1741 return(&root->root);
1743 attributes[l+2]=(
char *) NULL;
1744 attributes[l+1]=(
char *) NULL;
1746 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1747 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1748 attributes[l]=ConstantString(
"");
1752 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1754 if ((c ==
'"') || (c ==
'\''))
1761 while ((*p !=
'\0') && (*p != c))
1767 attributes[l]=ConstantString(
"");
1768 attributes[l+1]=ConstantString(
"");
1769 (void) DestroyXMLTreeAttributes(attributes);
1770 (void) ThrowMagickException(exception,
1771 GetMagickModule(),OptionWarning,
"ParseError",
1773 utf8=DestroyString(utf8);
1774 return(&root->root);
1777 while ((attribute != (
char **) NULL) &&
1778 (attribute[j] != (
char *) NULL) &&
1779 (strcmp(attribute[j],attributes[l]) != 0))
1781 attributes[l+1]=ParseEntities(attributes[l+1],
1782 root->entities,(attribute != (
char **) NULL) &&
1783 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
1786 attributes[l]=ConstantString(attributes[l]);
1788 while (isspace((
int) ((
unsigned char) *p)) != 0)
1794 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1803 if (((*p !=
'\0') && (*p !=
'>')) ||
1804 ((*p ==
'\0') && (terminal !=
'>')))
1807 (void) DestroyXMLTreeAttributes(attributes);
1808 (void) ThrowMagickException(exception,GetMagickModule(),
1809 OptionWarning,
"ParseError",
"missing >");
1810 utf8=DestroyString(utf8);
1811 return(&root->root);
1813 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1814 (void) DestroyXMLTreeAttributes(attributes);
1817 status=ParseOpenTag(root,tag,attributes,exception);
1818 if (status == MagickFalse)
1821 (void) DestroyXMLTreeAttributes(attributes);
1822 utf8=DestroyString(utf8);
1823 return(&root->root);
1825 (void) ParseCloseTag(root,tag,exception);
1831 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
1834 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1836 status=ParseOpenTag(root,tag,attributes,exception);
1837 if (status == MagickFalse)
1840 (void) DestroyXMLTreeAttributes(attributes);
1841 utf8=DestroyString(utf8);
1842 return(&root->root);
1848 (void) DestroyXMLTreeAttributes(attributes);
1855 (void) DestroyXMLTreeAttributes(attributes);
1856 (void) ThrowMagickException(exception,GetMagickModule(),
1857 OptionWarning,
"ParseError",
"missing >");
1858 utf8=DestroyString(utf8);
1859 return(&root->root);
1870 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
1872 if ((c ==
'\0') && (terminal !=
'>'))
1874 (void) ThrowMagickException(exception,GetMagickModule(),
1875 OptionWarning,
"ParseError",
"missing >");
1876 utf8=DestroyString(utf8);
1877 return(&root->root);
1880 if ((ignore_depth == 0) &&
1881 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1883 utf8=DestroyString(utf8);
1884 return(&root->root);
1886 if (ignore_depth > 0)
1889 if (isspace((
int) ((
unsigned char) *p)) != 0)
1890 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1893 if (strncmp(p,
"!--",3) == 0)
1899 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
1900 ((*p ==
'\0') && (terminal !=
'>')))
1902 (void) ThrowMagickException(exception,GetMagickModule(),
1903 OptionWarning,
"ParseError",
"unclosed <!--");
1904 utf8=DestroyString(utf8);
1905 return(&root->root);
1909 if (strncmp(p,
"![CDATA[",8) == 0)
1915 if (p != (
char *) NULL)
1918 if (ignore_depth == 0)
1919 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c',
1924 (void) ThrowMagickException(exception,GetMagickModule(),
1925 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
1926 utf8=DestroyString(utf8);
1927 return(&root->root);
1931 if (strncmp(p,
"!DOCTYPE",8) == 0)
1936 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
1937 ((l != 0) && ((*p !=
']') ||
1938 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
1939 l=(ssize_t) ((*p ==
'[') ? 1 : l))
1940 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
1941 if ((*p ==
'\0') && (terminal !=
'>'))
1943 (void) ThrowMagickException(exception,GetMagickModule(),
1944 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
1945 utf8=DestroyString(utf8);
1946 return(&root->root);
1949 tag=strchr(tag,
'[')+1;
1952 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
1954 if (status == MagickFalse)
1956 utf8=DestroyString(utf8);
1957 return(&root->root);
1971 if (p == (
char *) NULL)
1974 }
while ((*p !=
'\0') && (*p !=
'>'));
1975 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
1978 (void) ThrowMagickException(exception,GetMagickModule(),
1979 OptionWarning,
"ParseError",
"unclosed <?");
1980 utf8=DestroyString(utf8);
1981 return(&root->root);
1983 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
1987 (void) ThrowMagickException(exception,GetMagickModule(),
1988 OptionWarning,
"ParseError",
"unexpected <");
1989 utf8=DestroyString(utf8);
1990 return(&root->root);
1992 if ((p == (
char *) NULL) || (*p ==
'\0'))
1996 if ((*p !=
'\0') && (*p !=
'<'))
2001 while ((*p !=
'\0') && (*p !=
'<'))
2005 if (ignore_depth == 0)
2006 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&',exception);
2012 utf8=DestroyString(utf8);
2013 if (root->node == (XMLTreeInfo *) NULL)
2014 return(&root->root);
2015 if (root->node->tag == (
char *) NULL)
2017 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2018 "ParseError",
"root tag missing");
2019 return(&root->root);
2021 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2022 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2023 return(&root->root);
2048MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2051 *predefined_entities[NumberPredefinedEntities+1] =
2053 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2054 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2060 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2061 (void) memset(root,0,
sizeof(*root));
2062 root->root.tag=(
char *) NULL;
2063 if (tag != (
char *) NULL)
2064 root->root.tag=ConstantString(tag);
2065 root->node=(&root->root);
2066 root->root.content=ConstantString(
"");
2067 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2068 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2069 root->root.attributes=sentinel;
2070 root->attributes=(
char ***) root->root.attributes;
2071 root->processing_instructions=(
char ***) root->root.attributes;
2072 root->debug=IsEventLogging();
2073 root->signature=MagickCoreSignature;
2074 return(&root->root);
2103MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2104 const char *content)
2106 assert(xml_info != (XMLTreeInfo *) NULL);
2107 assert((xml_info->signature == MagickCoreSignature) ||
2108 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2109 if (IsEventLogging() != MagickFalse)
2110 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2111 if (xml_info->content != (
char *) NULL)
2112 xml_info->content=DestroyString(xml_info->content);
2113 xml_info->content=(
char *) ConstantString(content);
2132MagickPrivate MagickBooleanType SkipXMLComment(
const char **xml)
2139 while (isspace((
int) ((
unsigned char) *p)) != 0)
2141 if (strncmp(p,
"<!--",4) != 0)
2144 p=strstr(p+4,
"-->");
2145 if (p == (
const char *) NULL)
2146 return(MagickFalse);
2167MagickPrivate MagickBooleanType SkipXMLDocType(
const char **xml)
2178 for (p=*xml; *p !=
'\0'; )
2180 if ((quote == 0) && (strncmp(p,
"<!--",4) == 0))
2182 if (SkipXMLComment(&p) == MagickFalse)
2193 if ((*p ==
'"') || (*p ==
'\''))
2201 if (bracket_depth > 0)
2205 if ((*p ==
'>') && (bracket_depth == 0))
2214 return(MagickFalse);
2231MagickExport
char *SubstituteXMLEntities(
const char *content,
2232 const MagickBooleanType pedantic)
2250 canonical_content=AcquireString((
char *) NULL);
2251 extent=MagickPathExtent;
2252 for (p=content; *p !=
'\0'; p++)
2254 if ((i+MagickPathExtent) > (ssize_t) extent)
2256 extent+=MagickPathExtent;
2257 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
2258 sizeof(*canonical_content));
2259 if (canonical_content == (
char *) NULL)
2260 return(canonical_content);
2266 i+=FormatLocaleString(canonical_content+i,extent,
"&");
2271 i+=FormatLocaleString(canonical_content+i,extent,
"<");
2276 i+=FormatLocaleString(canonical_content+i,extent,
">");
2281 i+=FormatLocaleString(canonical_content+i,extent,
""");
2286 if (pedantic == MagickFalse)
2288 canonical_content[i++]=(char) (*p);
2291 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2296 if (pedantic == MagickFalse)
2298 canonical_content[i++]=(char) (*p);
2301 i+=FormatLocaleString(canonical_content+i,extent,
"	");
2306 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2311 canonical_content[i++]=(char) (*p);
2316 canonical_content[i]=
'\0';
2317 return(canonical_content);
2343static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2344 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2350 canonical_content=CanonicalXMLContent(source,pedantic);
2356 content=AcquireString(source);
2357 content[offset]=
'\0';
2358 canonical_content=CanonicalXMLContent(content,pedantic);
2359 content=DestroyString(content);
2361 if (canonical_content == (
char *) NULL)
2362 return(*destination);
2363 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2365 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2366 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2367 sizeof(**destination));
2368 if (*destination == (
char *) NULL)
2369 return(*destination);
2371 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2373 canonical_content=DestroyString(canonical_content);
2374 return(*destination);
2377static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2378 size_t *extent,
size_t start,
char ***attributes)
2393 content=(
char *)
"";
2394 if (xml_info->parent != (XMLTreeInfo *) NULL)
2395 content=xml_info->parent->content;
2397 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2398 start),source,length,extent,MagickFalse);
2399 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2401 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2402 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2403 if (*source == (
char *) NULL)
2406 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2407 "<%s",xml_info->tag);
2408 for (i=0; xml_info->attributes[i]; i+=2)
2410 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2411 if (attribute != xml_info->attributes[i+1])
2413 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2415 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2416 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2417 if (*source == (
char *) NULL)
2418 return((
char *) NULL);
2420 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2421 xml_info->attributes[i]);
2422 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2424 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2427 while ((attributes[i] != (
char **) NULL) &&
2428 (strcmp(attributes[i][0],xml_info->tag) != 0))
2431 while ((attributes[i] != (
char **) NULL) &&
2432 (attributes[i][j] != (
char *) NULL))
2434 if ((attributes[i][j+1] == (
char *) NULL) ||
2435 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2440 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2442 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2443 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2444 if (*source == (
char *) NULL)
2445 return((
char *) NULL);
2447 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2449 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2451 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2454 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2455 *xml_info->content ?
">" :
"/>");
2456 if (xml_info->child != (XMLTreeInfo *) NULL)
2457 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2459 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2461 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2463 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2464 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2465 if (*source == (
char *) NULL)
2466 return((
char *) NULL);
2468 if (*xml_info->content !=
'\0')
2469 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2471 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2473 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2474 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2477 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2482MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2505 assert(xml_info != (XMLTreeInfo *) NULL);
2506 assert((xml_info->signature == MagickCoreSignature) ||
2507 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2508 if (IsEventLogging() != MagickFalse)
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2510 if (xml_info->tag == (
char *) NULL)
2511 return((
char *) NULL);
2512 xml=AcquireString((
char *) NULL);
2514 extent=MagickPathExtent;
2515 root=(XMLTreeRoot *) xml_info;
2516 while (root->root.parent != (XMLTreeInfo *) NULL)
2517 root=(XMLTreeRoot *) root->root.parent;
2518 parent=xml_info->parent;
2519 if (parent == (XMLTreeInfo *) NULL)
2520 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2525 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2526 p=root->processing_instructions[i][1];
2527 for (j=1; p != (
char *) NULL; j++)
2529 if (root->processing_instructions[i][k][j-1] ==
'>')
2531 p=root->processing_instructions[i][j];
2534 q=root->processing_instructions[i][0];
2535 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2537 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2538 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2539 if (xml == (
char *) NULL)
2542 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2543 *p !=
'\0' ?
" " :
"",p);
2544 p=root->processing_instructions[i][j];
2547 ordered=xml_info->ordered;
2548 xml_info->parent=(XMLTreeInfo *) NULL;
2549 xml_info->ordered=(XMLTreeInfo *) NULL;
2550 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2551 xml_info->parent=parent;
2552 xml_info->ordered=ordered;
2553 if (parent == (XMLTreeInfo *) NULL)
2554 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2559 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2560 p=root->processing_instructions[i][1];
2561 for (j=1; p != (
char *) NULL; j++)
2563 if (root->processing_instructions[i][k][j-1] ==
'<')
2565 p=root->processing_instructions[i][j];
2568 q=root->processing_instructions[i][0];
2569 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2571 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2572 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2573 if (xml == (
char *) NULL)
2576 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2577 *p !=
'\0' ?
" " :
"",p);
2578 p=root->processing_instructions[i][j];
2581 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));