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)
1040 for ( ; *xml !=
'\0'; xml++)
1041 while (*xml ==
'\r')
1045 (void) memmove(xml,xml+1,strlen(xml));
1049 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1050 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1062 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1068 c=(int) strtol(xml+2,&entity,10);
1070 c=(int) strtol(xml+3,&entity,16);
1071 if ((c == 0) || (*entity !=
';'))
1087 for (i=0;
byte != 0;
byte/=2)
1090 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1095 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1099 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1102 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1103 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1109 while ((entities[i] != (
char *) NULL) &&
1110 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1112 if (entities[i++] == (
char *) NULL)
1115 if (entities[i] != (
char *) NULL)
1120 length=strlen(entities[i]);
1121 entity=strchr(xml,
';');
1122 if ((entity != (
char *) NULL) &&
1123 ((length-1L) >= (
size_t) (entity-xml)))
1125 offset=(ssize_t) (xml-p);
1126 extent=((size_t) offset+length+strlen(entity));
1129 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1130 if (p != (
char *) NULL)
1138 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1139 sizeof(*extent_xml));
1140 if (extent_xml != (
char *) NULL)
1142 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1143 (void) CopyMagickString(extent_xml,p,extent*
1144 sizeof(*extent_xml));
1148 if (p == (
char *) NULL)
1149 ThrowFatalException(ResourceLimitFatalError,
1150 "MemoryAllocationFailed");
1152 entity=strchr(xml,
';');
1154 if (entity != (
char *) NULL)
1155 (void) memmove(xml+length,entity+1,strlen(entity));
1156 (void) memcpy(xml,entities[i],length);
1160 if (((state ==
' ') || (state ==
'*')) &&
1161 (isspace((
int) ((
unsigned char) *xml)) != 0))
1171 for (xml=p; *xml !=
'\0'; xml++)
1176 i=(ssize_t) strspn(xml,accept);
1178 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1179 while ((*xml !=
'\0') && (*xml !=
' '))
1185 if ((xml >= p) && (*xml ==
' '))
1188 return(p == q ? ConstantString(p) : p);
1191static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1192 const size_t length,
const char state)
1197 xml_info=root->node;
1198 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1202 xml=ParseEntities(xml,root->entities,state);
1203 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1205 (void) ConcatenateString(&xml_info->content,xml);
1206 xml=DestroyString(xml);
1210 if (xml_info->content != (
char *) NULL)
1211 xml_info->content=DestroyString(xml_info->content);
1212 xml_info->content=xml;
1216static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1217 ExceptionInfo *exception)
1219 if ((root->node == (XMLTreeInfo *) NULL) ||
1220 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1222 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1223 "ParseError",
"unexpected closing tag </%s>",tag);
1224 return(&root->root);
1226 root->node=root->node->parent;
1228 return((XMLTreeInfo *) NULL);
1231static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1232 const int delimiter,
const size_t depth,
char **entities)
1240 if (depth >= MagickMaxRecursionDepth)
1241 return(MagickFalse);
1244 while ((*xml !=
'\0') && (*xml != delimiter))
1248 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1249 return(MagickFalse);
1251 while ((entities[i] != (
char *) NULL) &&
1252 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1254 if ((entities[i] != (
char *) NULL) &&
1255 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1256 return(MagickFalse);
1260static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1272 xml+=strcspn(xml,XMLWhitespace);
1276 xml+=strspn(xml+1,XMLWhitespace)+1;
1278 if (strcmp(target,
"xml") == 0)
1280 xml=strstr(xml,
"standalone");
1281 if ((xml != (
char *) NULL) &&
1282 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1283 root->standalone=MagickTrue;
1286 if (root->processing_instructions[0] == (
char **) NULL)
1288 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1289 *root->processing_instructions));
1290 *root->processing_instructions=(
char **) NULL;
1293 while ((root->processing_instructions[i] != (
char **) NULL) &&
1294 (strcmp(target,root->processing_instructions[i][0]) != 0))
1296 if (root->processing_instructions[i] == (
char **) NULL)
1298 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1299 root->processing_instructions,(
size_t) (i+2),
1300 sizeof(*root->processing_instructions));
1301 if (root->processing_instructions == (
char ***) NULL)
1302 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1303 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1304 sizeof(**root->processing_instructions));
1305 if (root->processing_instructions[i] == (
char **) NULL)
1306 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1307 root->processing_instructions[i+1]=(
char **) NULL;
1308 root->processing_instructions[i][0]=ConstantString(target);
1309 root->processing_instructions[i][1]=(
char *)
1310 root->processing_instructions[i+1];
1311 root->processing_instructions[i+1]=(
char **) NULL;
1312 root->processing_instructions[i][2]=ConstantString(
"");
1315 while (root->processing_instructions[i][j] != (
char *) NULL)
1317 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1318 root->processing_instructions[i],(
size_t) (j+3),
1319 sizeof(**root->processing_instructions));
1320 if (root->processing_instructions[i] == (
char **) NULL)
1321 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1322 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1323 root->processing_instructions[i][j+1],(
size_t) (j+1),
1324 sizeof(***root->processing_instructions));
1325 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1326 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1327 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1328 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1329 root->processing_instructions[i][j]=ConstantString(xml);
1330 root->processing_instructions[i][j+1]=(
char *) NULL;
1333static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1334 size_t length,ExceptionInfo *exception)
1336#define DestroyXMLEntities(predefined_entities) \
1339 for (k=0; predefined_entities[k] != (char *) NULL; k++) \
1340 if ((k & 0x01) != 0) \
1341 predefined_entities[k]=DestroyString(predefined_entities[k]); \
1342 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities); \
1349 **predefined_entities,
1359 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1360 if (predefined_entities == (
char **) NULL)
1361 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1362 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1363 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1365 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1369 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1374 if (strspn(xml+8,XMLWhitespace) == 0)
1376 xml+=strspn(xml+8,XMLWhitespace)+8;
1378 n=xml+strspn(xml,XMLWhitespace
"%");
1379 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1381 xml=n+strcspn(n,XMLWhitespace);
1385 v=xml+strspn(xml+1,XMLWhitespace)+1;
1388 if ((q !=
'"') && (q !=
'\''))
1393 xml=strchr(xml,
'>');
1396 entities=(*c ==
'%') ? predefined_entities : root->entities;
1397 for (i=0; entities[i] != (
char *) NULL; i++) ;
1398 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1400 if (entities == (
char **) NULL)
1401 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1403 predefined_entities=entities;
1405 root->entities=entities;
1409 if (xml != (
char *) NULL)
1414 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1415 entities[i+2]=(
char *) NULL;
1416 if ((ValidateEntities(n,entities[i+1],
'%',0,entities) != MagickFalse) &&
1417 (ValidateEntities(n,entities[i+1],
'&',0,entities) != MagickFalse))
1421 if (entities[i+1] != v)
1422 entities[i+1]=DestroyString(entities[i+1]);
1423 (void) ThrowMagickException(exception,GetMagickModule(),
1424 OptionWarning,
"ParseError",
"circular entity declaration %s",n);
1425 DestroyXMLEntities(predefined_entities);
1426 return(MagickFalse);
1430 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1435 t=xml+strspn(xml+9,XMLWhitespace)+9;
1438 (void) ThrowMagickException(exception,GetMagickModule(),
1439 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1440 DestroyXMLEntities(predefined_entities);
1441 return(MagickFalse);
1443 xml=t+strcspn(t,XMLWhitespace
">");
1448 while ((root->attributes[i] != (
char **) NULL) &&
1449 (n != (
char *) NULL) &&
1450 (strcmp(n,root->attributes[i][0]) != 0))
1452 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1455 xml=n+strcspn(n,XMLWhitespace);
1460 (void) ThrowMagickException(exception,GetMagickModule(),
1461 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1462 DestroyXMLEntities(predefined_entities);
1463 return(MagickFalse);
1465 xml+=strspn(xml+1,XMLWhitespace)+1;
1466 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1467 if (strncmp(xml,
"NOTATION",8) == 0)
1468 xml+=strspn(xml+8,XMLWhitespace)+8;
1469 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1470 strcspn(xml,XMLWhitespace);
1471 if (xml == (
char *) NULL)
1473 (void) ThrowMagickException(exception,GetMagickModule(),
1474 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1475 DestroyXMLEntities(predefined_entities);
1476 return(MagickFalse);
1478 xml+=strspn(xml,XMLWhitespace
")");
1479 if (strncmp(xml,
"#FIXED",6) == 0)
1480 xml+=strspn(xml+6,XMLWhitespace)+6;
1483 xml+=strcspn(xml,XMLWhitespace
">")-1;
1489 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1490 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1494 (void) ThrowMagickException(exception,GetMagickModule(),
1495 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1496 DestroyXMLEntities(predefined_entities);
1497 return(MagickFalse);
1499 if (root->attributes[i] == (
char **) NULL)
1505 root->attributes=(
char ***) AcquireQuantumMemory(2,
1506 sizeof(*root->attributes));
1508 root->attributes=(
char ***) ResizeQuantumMemory(
1509 root->attributes,(
size_t) (i+2),
1510 sizeof(*root->attributes));
1511 if (root->attributes == (
char ***) NULL)
1512 ThrowFatalException(ResourceLimitFatalError,
1513 "MemoryAllocationFailed");
1514 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1515 sizeof(**root->attributes));
1516 if (root->attributes[i] == (
char **) NULL)
1517 ThrowFatalException(ResourceLimitFatalError,
1518 "MemoryAllocationFailed");
1519 root->attributes[i][0]=ConstantString(t);
1520 root->attributes[i][1]=(
char *) NULL;
1521 root->attributes[i+1]=(
char **) NULL;
1523 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1524 root->attributes[i]=(
char **) ResizeQuantumMemory(
1525 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1526 if (root->attributes[i] == (
char **) NULL)
1527 ThrowFatalException(ResourceLimitFatalError,
1528 "MemoryAllocationFailed");
1529 root->attributes[i][j+3]=(
char *) NULL;
1530 root->attributes[i][j+2]=ConstantString(c);
1531 root->attributes[i][j+1]=(
char *) NULL;
1532 if (v != (
char *) NULL)
1533 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1534 root->attributes[i][j]=ConstantString(n);
1538 if (strncmp(xml,
"<!--", 4) == 0)
1539 xml=strstr(xml+4,
"-->");
1541 if (strncmp(xml,
"<?", 2) == 0)
1545 if (xml != (
char *) NULL)
1547 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1553 xml=strchr(xml,
'>');
1555 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1558 DestroyXMLEntities(predefined_entities);
1562static MagickBooleanType ParseOpenTag(XMLTreeRoot *root,
char *tag,
1563 char **attributes,ExceptionInfo *exception)
1568 if (root->depth >= MagickMaxRecursionDepth)
1570 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1571 "ParseError",
"unexpected open tag </%s>",tag);
1572 return(MagickFalse);
1574 xml_info=root->node;
1575 if (xml_info->tag == (
char *) NULL)
1576 xml_info->tag=ConstantString(tag);
1578 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1579 if (xml_info != (XMLTreeInfo *) NULL)
1580 xml_info->attributes=attributes;
1581 root->node=xml_info;
1594static inline MagickBooleanType IsSkipTag(
const char *tag)
1600 while (ignore_tags[i] != (
const char *) NULL)
1602 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1606 return(MagickFalse);
1609MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1640 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1642 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1643 "ParseError",
"root tag missing");
1644 return((XMLTreeInfo *) NULL);
1646 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1648 utf8=ConvertUTF16ToUTF8(xml,&length);
1649 if (utf8 == (
char *) NULL)
1651 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1652 "ParseError",
"UTF16 to UTF8 failed");
1653 return((XMLTreeInfo *) NULL);
1657 utf8=DestroyString(utf8);
1658 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1659 "ParseError",
"root tag missing");
1660 return((XMLTreeInfo *) NULL);
1662 terminal=utf8[length-1];
1663 utf8[length-1]=
'\0';
1665 while ((*p !=
'\0') && (*p !=
'<'))
1669 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1670 "ParseError",
"root tag missing");
1671 utf8=DestroyString(utf8);
1672 return((XMLTreeInfo *) NULL);
1674 attribute=(
char **) NULL;
1679 attributes=(
char **) sentinel;
1682 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1683 (*p ==
':') || (c <
'\0'))
1688 if (root->node == (XMLTreeInfo *) NULL)
1690 (void) ThrowMagickException(exception,GetMagickModule(),
1691 OptionWarning,
"ParseError",
"root tag missing");
1692 utf8=DestroyString(utf8);
1693 return(&root->root);
1695 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1696 while (isspace((
int) ((
unsigned char) *p)) != 0)
1698 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1699 (ignore_depth == 0))
1701 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1707 while ((root->attributes[i] != (
char **) NULL) &&
1708 (strcmp(root->attributes[i][0],tag) != 0))
1710 attribute=root->attributes[i];
1712 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1718 attributes=(
char **) AcquireQuantumMemory(4,
1719 sizeof(*attributes));
1721 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1722 (l+4),
sizeof(*attributes));
1723 if (attributes == (
char **) NULL)
1725 (void) ThrowMagickException(exception,GetMagickModule(),
1726 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1727 utf8=DestroyString(utf8);
1728 return(&root->root);
1730 attributes[l+2]=(
char *) NULL;
1731 attributes[l+1]=(
char *) NULL;
1733 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1734 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1735 attributes[l]=ConstantString(
"");
1739 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1741 if ((c ==
'"') || (c ==
'\''))
1748 while ((*p !=
'\0') && (*p != c))
1754 attributes[l]=ConstantString(
"");
1755 attributes[l+1]=ConstantString(
"");
1756 (void) DestroyXMLTreeAttributes(attributes);
1757 (void) ThrowMagickException(exception,
1758 GetMagickModule(),OptionWarning,
"ParseError",
1760 utf8=DestroyString(utf8);
1761 return(&root->root);
1764 while ((attribute != (
char **) NULL) &&
1765 (attribute[j] != (
char *) NULL) &&
1766 (strcmp(attribute[j],attributes[l]) != 0))
1768 attributes[l+1]=ParseEntities(attributes[l+1],
1769 root->entities,(attribute != (
char **) NULL) &&
1770 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
1773 attributes[l]=ConstantString(attributes[l]);
1775 while (isspace((
int) ((
unsigned char) *p)) != 0)
1781 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1790 if (((*p !=
'\0') && (*p !=
'>')) ||
1791 ((*p ==
'\0') && (terminal !=
'>')))
1794 (void) DestroyXMLTreeAttributes(attributes);
1795 (void) ThrowMagickException(exception,GetMagickModule(),
1796 OptionWarning,
"ParseError",
"missing >");
1797 utf8=DestroyString(utf8);
1798 return(&root->root);
1800 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1801 (void) DestroyXMLTreeAttributes(attributes);
1804 status=ParseOpenTag(root,tag,attributes,exception);
1805 if (status == MagickFalse)
1808 (void) DestroyXMLTreeAttributes(attributes);
1809 utf8=DestroyString(utf8);
1810 return(&root->root);
1812 (void) ParseCloseTag(root,tag,exception);
1818 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
1821 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1823 status=ParseOpenTag(root,tag,attributes,exception);
1824 if (status == MagickFalse)
1827 (void) DestroyXMLTreeAttributes(attributes);
1828 utf8=DestroyString(utf8);
1829 return(&root->root);
1835 (void) DestroyXMLTreeAttributes(attributes);
1842 (void) DestroyXMLTreeAttributes(attributes);
1843 (void) ThrowMagickException(exception,GetMagickModule(),
1844 OptionWarning,
"ParseError",
"missing >");
1845 utf8=DestroyString(utf8);
1846 return(&root->root);
1857 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
1859 if ((c ==
'\0') && (terminal !=
'>'))
1861 (void) ThrowMagickException(exception,GetMagickModule(),
1862 OptionWarning,
"ParseError",
"missing >");
1863 utf8=DestroyString(utf8);
1864 return(&root->root);
1867 if ((ignore_depth == 0) &&
1868 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1870 utf8=DestroyString(utf8);
1871 return(&root->root);
1873 if (ignore_depth > 0)
1876 if (isspace((
int) ((
unsigned char) *p)) != 0)
1877 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1880 if (strncmp(p,
"!--",3) == 0)
1886 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
1887 ((*p ==
'\0') && (terminal !=
'>')))
1889 (void) ThrowMagickException(exception,GetMagickModule(),
1890 OptionWarning,
"ParseError",
"unclosed <!--");
1891 utf8=DestroyString(utf8);
1892 return(&root->root);
1896 if (strncmp(p,
"![CDATA[",8) == 0)
1902 if (p != (
char *) NULL)
1905 if (ignore_depth == 0)
1906 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
1910 (void) ThrowMagickException(exception,GetMagickModule(),
1911 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
1912 utf8=DestroyString(utf8);
1913 return(&root->root);
1917 if (strncmp(p,
"!DOCTYPE",8) == 0)
1922 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
1923 ((l != 0) && ((*p !=
']') ||
1924 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
1925 l=(ssize_t) ((*p ==
'[') ? 1 : l))
1926 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
1927 if ((*p ==
'\0') && (terminal !=
'>'))
1929 (void) ThrowMagickException(exception,GetMagickModule(),
1930 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
1931 utf8=DestroyString(utf8);
1932 return(&root->root);
1935 tag=strchr(tag,
'[')+1;
1938 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
1940 if (status == MagickFalse)
1942 utf8=DestroyString(utf8);
1943 return(&root->root);
1957 if (p == (
char *) NULL)
1960 }
while ((*p !=
'\0') && (*p !=
'>'));
1961 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
1964 (void) ThrowMagickException(exception,GetMagickModule(),
1965 OptionWarning,
"ParseError",
"unclosed <?");
1966 utf8=DestroyString(utf8);
1967 return(&root->root);
1969 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
1973 (void) ThrowMagickException(exception,GetMagickModule(),
1974 OptionWarning,
"ParseError",
"unexpected <");
1975 utf8=DestroyString(utf8);
1976 return(&root->root);
1978 if ((p == (
char *) NULL) || (*p ==
'\0'))
1982 if ((*p !=
'\0') && (*p !=
'<'))
1987 while ((*p !=
'\0') && (*p !=
'<'))
1991 if (ignore_depth == 0)
1992 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
1998 utf8=DestroyString(utf8);
1999 if (root->node == (XMLTreeInfo *) NULL)
2000 return(&root->root);
2001 if (root->node->tag == (
char *) NULL)
2003 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2004 "ParseError",
"root tag missing");
2005 return(&root->root);
2007 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2008 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2009 return(&root->root);
2034MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2037 *predefined_entities[NumberPredefinedEntities+1] =
2039 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2040 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2046 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2047 (void) memset(root,0,
sizeof(*root));
2048 root->root.tag=(
char *) NULL;
2049 if (tag != (
char *) NULL)
2050 root->root.tag=ConstantString(tag);
2051 root->node=(&root->root);
2052 root->root.content=ConstantString(
"");
2053 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2054 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2055 root->root.attributes=sentinel;
2056 root->attributes=(
char ***) root->root.attributes;
2057 root->processing_instructions=(
char ***) root->root.attributes;
2058 root->debug=IsEventLogging();
2059 root->signature=MagickCoreSignature;
2060 return(&root->root);
2089MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2090 const char *content)
2092 assert(xml_info != (XMLTreeInfo *) NULL);
2093 assert((xml_info->signature == MagickCoreSignature) ||
2094 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2095 if (IsEventLogging() != MagickFalse)
2096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2097 if (xml_info->content != (
char *) NULL)
2098 xml_info->content=DestroyString(xml_info->content);
2099 xml_info->content=(
char *) ConstantString(content);
2118MagickPrivate MagickBooleanType SkipXMLComment(
const char **xml)
2125 while (isspace((
int) ((
unsigned char) *p)) != 0)
2127 if (strncmp(p,
"<!--",4) != 0)
2130 p=strstr(p+4,
"-->");
2131 if (p == (
const char *) NULL)
2132 return(MagickFalse);
2153MagickPrivate MagickBooleanType SkipXMLDocType(
const char **xml)
2164 for (p=*xml; *p !=
'\0'; )
2166 if ((quote == 0) && (strncmp(p,
"<!--",4) == 0))
2168 if (SkipXMLComment(&p) == MagickFalse)
2179 if ((*p ==
'"') || (*p ==
'\''))
2187 if (bracket_depth > 0)
2191 if ((*p ==
'>') && (bracket_depth == 0))
2200 return(MagickFalse);
2217MagickExport
char *SubstituteXMLEntities(
const char *content,
2218 const MagickBooleanType pedantic)
2236 canonical_content=AcquireString((
char *) NULL);
2237 extent=MagickPathExtent;
2238 for (p=content; *p !=
'\0'; p++)
2240 if ((i+MagickPathExtent) > (ssize_t) extent)
2242 extent+=MagickPathExtent;
2243 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
2244 sizeof(*canonical_content));
2245 if (canonical_content == (
char *) NULL)
2246 return(canonical_content);
2252 i+=FormatLocaleString(canonical_content+i,extent,
"&");
2257 i+=FormatLocaleString(canonical_content+i,extent,
"<");
2262 i+=FormatLocaleString(canonical_content+i,extent,
">");
2267 i+=FormatLocaleString(canonical_content+i,extent,
""");
2272 if (pedantic == MagickFalse)
2274 canonical_content[i++]=(char) (*p);
2277 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2282 if (pedantic == MagickFalse)
2284 canonical_content[i++]=(char) (*p);
2287 i+=FormatLocaleString(canonical_content+i,extent,
"	");
2292 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2297 canonical_content[i++]=(char) (*p);
2302 canonical_content[i]=
'\0';
2303 return(canonical_content);
2329static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2330 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2336 canonical_content=CanonicalXMLContent(source,pedantic);
2342 content=AcquireString(source);
2343 content[offset]=
'\0';
2344 canonical_content=CanonicalXMLContent(content,pedantic);
2345 content=DestroyString(content);
2347 if (canonical_content == (
char *) NULL)
2348 return(*destination);
2349 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2351 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2352 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2353 sizeof(**destination));
2354 if (*destination == (
char *) NULL)
2355 return(*destination);
2357 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2359 canonical_content=DestroyString(canonical_content);
2360 return(*destination);
2363static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2364 size_t *extent,
size_t start,
char ***attributes)
2379 content=(
char *)
"";
2380 if (xml_info->parent != (XMLTreeInfo *) NULL)
2381 content=xml_info->parent->content;
2383 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2384 start),source,length,extent,MagickFalse);
2385 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2387 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2388 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2389 if (*source == (
char *) NULL)
2392 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2393 "<%s",xml_info->tag);
2394 for (i=0; xml_info->attributes[i]; i+=2)
2396 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2397 if (attribute != xml_info->attributes[i+1])
2399 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2401 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2402 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2403 if (*source == (
char *) NULL)
2404 return((
char *) NULL);
2406 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2407 xml_info->attributes[i]);
2408 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2410 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2413 while ((attributes[i] != (
char **) NULL) &&
2414 (strcmp(attributes[i][0],xml_info->tag) != 0))
2417 while ((attributes[i] != (
char **) NULL) &&
2418 (attributes[i][j] != (
char *) NULL))
2420 if ((attributes[i][j+1] == (
char *) NULL) ||
2421 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2426 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2428 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2429 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2430 if (*source == (
char *) NULL)
2431 return((
char *) NULL);
2433 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2435 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2437 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2440 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2441 *xml_info->content ?
">" :
"/>");
2442 if (xml_info->child != (XMLTreeInfo *) NULL)
2443 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2445 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2447 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2449 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2450 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2451 if (*source == (
char *) NULL)
2452 return((
char *) NULL);
2454 if (*xml_info->content !=
'\0')
2455 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2457 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2459 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2460 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2463 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2468MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2491 assert(xml_info != (XMLTreeInfo *) NULL);
2492 assert((xml_info->signature == MagickCoreSignature) ||
2493 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2494 if (IsEventLogging() != MagickFalse)
2495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2496 if (xml_info->tag == (
char *) NULL)
2497 return((
char *) NULL);
2498 xml=AcquireString((
char *) NULL);
2500 extent=MagickPathExtent;
2501 root=(XMLTreeRoot *) xml_info;
2502 while (root->root.parent != (XMLTreeInfo *) NULL)
2503 root=(XMLTreeRoot *) root->root.parent;
2504 parent=xml_info->parent;
2505 if (parent == (XMLTreeInfo *) NULL)
2506 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2511 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2512 p=root->processing_instructions[i][1];
2513 for (j=1; p != (
char *) NULL; j++)
2515 if (root->processing_instructions[i][k][j-1] ==
'>')
2517 p=root->processing_instructions[i][j];
2520 q=root->processing_instructions[i][0];
2521 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2523 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2524 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2525 if (xml == (
char *) NULL)
2528 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2529 *p !=
'\0' ?
" " :
"",p);
2530 p=root->processing_instructions[i][j];
2533 ordered=xml_info->ordered;
2534 xml_info->parent=(XMLTreeInfo *) NULL;
2535 xml_info->ordered=(XMLTreeInfo *) NULL;
2536 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2537 xml_info->parent=parent;
2538 xml_info->ordered=ordered;
2539 if (parent == (XMLTreeInfo *) NULL)
2540 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2545 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2546 p=root->processing_instructions[i][1];
2547 for (j=1; p != (
char *) NULL; j++)
2549 if (root->processing_instructions[i][k][j-1] ==
'<')
2551 p=root->processing_instructions[i][j];
2554 q=root->processing_instructions[i][0];
2555 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2557 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2558 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2559 if (xml == (
char *) NULL)
2562 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2563 *p !=
'\0' ?
" " :
"",p);
2564 p=root->processing_instructions[i][j];
2567 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));