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))
1072 codepoint=strtol(xml+2,&entity,10);
1074 codepoint=strtol(xml+3,&entity,16);
1075 if ((codepoint <= 0) || (codepoint > 0x10ffff) ||
1076 ((codepoint >= 0xd800) && (codepoint <= 0xdfff)) ||
1094 for (i=0;
byte != 0;
byte/=2)
1097 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1102 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1106 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1109 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1110 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1116 while ((entities[i] != (
char *) NULL) &&
1117 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1119 if (entities[i++] == (
char *) NULL)
1122 if (entities[i] != (
char *) NULL)
1127 length=strlen(entities[i]);
1128 entity=strchr(xml,
';');
1129 if ((entity != (
char *) NULL) &&
1130 ((length-1L) >= (
size_t) (entity-xml)))
1132 offset=(ssize_t) (xml-p);
1133 extent=((size_t) offset+length+strlen(entity));
1136 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1137 if (p != (
char *) NULL)
1145 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1146 sizeof(*extent_xml));
1147 if (extent_xml != (
char *) NULL)
1149 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1150 (void) CopyMagickString(extent_xml,p,extent*
1151 sizeof(*extent_xml));
1155 if (p == (
char *) NULL)
1157 (void) ThrowMagickException(exception,GetMagickModule(),
1158 OptionWarning,
"ParseError",
1159 "memory allocation failed");
1162 if (strlen(p) > GetMaxProfileSize())
1164 (void) ThrowMagickException(exception,GetMagickModule(),
1165 ResourceLimitError,
"ProfileSizeExceedsLimit",
1166 "`%.17g'",(
double) strlen(p));
1167 return(p == q ? ConstantString(p) : p);
1170 entity=strchr(xml,
';');
1172 if (entity != (
char *) NULL)
1173 (void) memmove(xml+length,entity+1,strlen(entity));
1174 (void) memcpy(xml,entities[i],length);
1178 if (((state ==
' ') || (state ==
'*')) &&
1179 (isspace((
int) ((
unsigned char) *xml)) != 0))
1189 for (xml=p; *xml !=
'\0'; xml++)
1194 i=(ssize_t) strspn(xml,accept);
1196 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1197 while ((*xml !=
'\0') && (*xml !=
' '))
1203 if ((xml >= p) && (*xml ==
' '))
1206 return(p == q ? ConstantString(p) : p);
1209static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1210 const size_t length,
const char state,ExceptionInfo *exception)
1215 xml_info=root->node;
1216 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1220 xml=ParseEntities(xml,root->entities,state,exception);
1221 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1223 (void) ConcatenateString(&xml_info->content,xml);
1224 xml=DestroyString(xml);
1228 if (xml_info->content != (
char *) NULL)
1229 xml_info->content=DestroyString(xml_info->content);
1230 xml_info->content=xml;
1234static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1235 ExceptionInfo *exception)
1237 if ((root->node == (XMLTreeInfo *) NULL) ||
1238 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1240 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1241 "ParseError",
"unexpected closing tag </%s>",tag);
1242 return(&root->root);
1244 root->node=root->node->parent;
1246 return((XMLTreeInfo *) NULL);
1249static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1250 const int delimiter,
const size_t depth,
char **entities)
1258 if (depth >= MagickMaxRecursionDepth)
1259 return(MagickFalse);
1262 while ((*xml !=
'\0') && (*xml != delimiter))
1266 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1267 return(MagickFalse);
1269 while ((entities[i] != (
char *) NULL) &&
1270 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1272 if ((entities[i] != (
char *) NULL) &&
1273 (ValidateEntities(tag,entities[i+1],delimiter,depth+1,entities) == MagickFalse))
1274 return(MagickFalse);
1278static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1290 xml+=strcspn(xml,XMLWhitespace);
1294 xml+=strspn(xml+1,XMLWhitespace)+1;
1296 if (strcmp(target,
"xml") == 0)
1298 xml=strstr(xml,
"standalone");
1299 if ((xml != (
char *) NULL) &&
1300 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1301 root->standalone=MagickTrue;
1304 if (root->processing_instructions[0] == (
char **) NULL)
1306 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1307 *root->processing_instructions));
1308 *root->processing_instructions=(
char **) NULL;
1311 while ((root->processing_instructions[i] != (
char **) NULL) &&
1312 (strcmp(target,root->processing_instructions[i][0]) != 0))
1314 if (root->processing_instructions[i] == (
char **) NULL)
1316 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1317 root->processing_instructions,(
size_t) (i+2),
1318 sizeof(*root->processing_instructions));
1319 if (root->processing_instructions == (
char ***) NULL)
1320 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1321 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1322 sizeof(**root->processing_instructions));
1323 if (root->processing_instructions[i] == (
char **) NULL)
1324 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1325 root->processing_instructions[i+1]=(
char **) NULL;
1326 root->processing_instructions[i][0]=ConstantString(target);
1327 root->processing_instructions[i][1]=(
char *)
1328 root->processing_instructions[i+1];
1329 root->processing_instructions[i+1]=(
char **) NULL;
1330 root->processing_instructions[i][2]=ConstantString(
"");
1333 while (root->processing_instructions[i][j] != (
char *) NULL)
1335 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1336 root->processing_instructions[i],(
size_t) (j+3),
1337 sizeof(**root->processing_instructions));
1338 if (root->processing_instructions[i] == (
char **) NULL)
1339 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1340 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1341 root->processing_instructions[i][j+1],(
size_t) (j+1),
1342 sizeof(***root->processing_instructions));
1343 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1344 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1345 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1346 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1347 root->processing_instructions[i][j]=ConstantString(xml);
1348 root->processing_instructions[i][j+1]=(
char *) NULL;
1351static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1352 size_t length,ExceptionInfo *exception)
1354#define DestroyXMLEntities(predefined_entities) \
1357 for (k=0; predefined_entities[k] != (char *) NULL; k++) \
1358 if ((k & 0x01) != 0) \
1359 predefined_entities[k]=DestroyString(predefined_entities[k]); \
1360 predefined_entities=(char **) RelinquishMagickMemory(predefined_entities); \
1367 **predefined_entities,
1377 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1378 if (predefined_entities == (
char **) NULL)
1379 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1380 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1381 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1383 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1387 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1392 if (strspn(xml+8,XMLWhitespace) == 0)
1394 xml+=strspn(xml+8,XMLWhitespace)+8;
1396 n=xml+strspn(xml,XMLWhitespace
"%");
1397 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1399 xml=n+strcspn(n,XMLWhitespace);
1403 v=xml+strspn(xml+1,XMLWhitespace)+1;
1406 if ((q !=
'"') && (q !=
'\''))
1411 xml=strchr(xml,
'>');
1414 entities=(*c ==
'%') ? predefined_entities : root->entities;
1415 for (i=0; entities[i] != (
char *) NULL; i++) ;
1416 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1418 if (entities == (
char **) NULL)
1419 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1421 predefined_entities=entities;
1423 root->entities=entities;
1427 if (xml != (
char *) NULL)
1432 entities[i+1]=ParseEntities(v,predefined_entities,
'%',exception);
1433 entities[i+2]=(
char *) NULL;
1434 if ((ValidateEntities(n,entities[i+1],
'%',0,entities) != MagickFalse) &&
1435 (ValidateEntities(n,entities[i+1],
'&',0,entities) != MagickFalse))
1439 if (entities[i+1] != v)
1440 entities[i+1]=DestroyString(entities[i+1]);
1441 (void) ThrowMagickException(exception,GetMagickModule(),
1442 OptionWarning,
"ParseError",
"circular entity declaration %s",n);
1443 DestroyXMLEntities(predefined_entities);
1444 return(MagickFalse);
1448 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1453 t=xml+strspn(xml+9,XMLWhitespace)+9;
1456 (void) ThrowMagickException(exception,GetMagickModule(),
1457 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1458 DestroyXMLEntities(predefined_entities);
1459 return(MagickFalse);
1461 xml=t+strcspn(t,XMLWhitespace
">");
1466 while ((root->attributes[i] != (
char **) NULL) &&
1467 (n != (
char *) NULL) &&
1468 (strcmp(n,root->attributes[i][0]) != 0))
1470 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1473 xml=n+strcspn(n,XMLWhitespace);
1478 (void) ThrowMagickException(exception,GetMagickModule(),
1479 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1480 DestroyXMLEntities(predefined_entities);
1481 return(MagickFalse);
1483 xml+=strspn(xml+1,XMLWhitespace)+1;
1484 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1485 if (strncmp(xml,
"NOTATION",8) == 0)
1486 xml+=strspn(xml+8,XMLWhitespace)+8;
1487 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1488 strcspn(xml,XMLWhitespace);
1489 if (xml == (
char *) NULL)
1491 (void) ThrowMagickException(exception,GetMagickModule(),
1492 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1493 DestroyXMLEntities(predefined_entities);
1494 return(MagickFalse);
1496 xml+=strspn(xml,XMLWhitespace
")");
1497 if (strncmp(xml,
"#FIXED",6) == 0)
1498 xml+=strspn(xml+6,XMLWhitespace)+6;
1501 xml+=strcspn(xml,XMLWhitespace
">")-1;
1507 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1508 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1512 (void) ThrowMagickException(exception,GetMagickModule(),
1513 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1514 DestroyXMLEntities(predefined_entities);
1515 return(MagickFalse);
1517 if (root->attributes[i] == (
char **) NULL)
1523 root->attributes=(
char ***) AcquireQuantumMemory(2,
1524 sizeof(*root->attributes));
1526 root->attributes=(
char ***) ResizeQuantumMemory(
1527 root->attributes,(
size_t) (i+2),
1528 sizeof(*root->attributes));
1529 if (root->attributes == (
char ***) NULL)
1530 ThrowFatalException(ResourceLimitFatalError,
1531 "MemoryAllocationFailed");
1532 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1533 sizeof(**root->attributes));
1534 if (root->attributes[i] == (
char **) NULL)
1535 ThrowFatalException(ResourceLimitFatalError,
1536 "MemoryAllocationFailed");
1537 root->attributes[i][0]=ConstantString(t);
1538 root->attributes[i][1]=(
char *) NULL;
1539 root->attributes[i+1]=(
char **) NULL;
1541 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1542 root->attributes[i]=(
char **) ResizeQuantumMemory(
1543 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1544 if (root->attributes[i] == (
char **) NULL)
1545 ThrowFatalException(ResourceLimitFatalError,
1546 "MemoryAllocationFailed");
1547 root->attributes[i][j+3]=(
char *) NULL;
1548 root->attributes[i][j+2]=ConstantString(c);
1549 root->attributes[i][j+1]=(
char *) NULL;
1550 if (v != (
char *) NULL)
1551 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c,
1553 root->attributes[i][j]=ConstantString(n);
1557 if (strncmp(xml,
"<!--", 4) == 0)
1558 xml=strstr(xml+4,
"-->");
1560 if (strncmp(xml,
"<?", 2) == 0)
1564 if (xml != (
char *) NULL)
1566 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1572 xml=strchr(xml,
'>');
1574 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1577 DestroyXMLEntities(predefined_entities);
1581static MagickBooleanType ParseOpenTag(XMLTreeRoot *root,
char *tag,
1582 char **attributes,ExceptionInfo *exception)
1587 if (root->depth >= MagickMaxRecursionDepth)
1589 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1590 "ParseError",
"unexpected open tag </%s>",tag);
1591 return(MagickFalse);
1593 xml_info=root->node;
1594 if (xml_info->tag == (
char *) NULL)
1595 xml_info->tag=ConstantString(tag);
1597 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1598 if (xml_info != (XMLTreeInfo *) NULL)
1599 xml_info->attributes=attributes;
1600 root->node=xml_info;
1613static inline MagickBooleanType IsSkipTag(
const char *tag)
1619 while (ignore_tags[i] != (
const char *) NULL)
1621 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1625 return(MagickFalse);
1628MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1659 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1661 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1662 "ParseError",
"root tag missing");
1663 return((XMLTreeInfo *) NULL);
1665 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1667 utf8=ConvertUTF16ToUTF8(xml,&length);
1668 if (utf8 == (
char *) NULL)
1670 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1671 "ParseError",
"UTF16 to UTF8 failed");
1672 return((XMLTreeInfo *) NULL);
1676 utf8=DestroyString(utf8);
1677 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1678 "ParseError",
"root tag missing");
1679 return((XMLTreeInfo *) NULL);
1681 terminal=utf8[length-1];
1682 utf8[length-1]=
'\0';
1684 while ((*p !=
'\0') && (*p !=
'<'))
1688 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1689 "ParseError",
"root tag missing");
1690 utf8=DestroyString(utf8);
1691 return((XMLTreeInfo *) NULL);
1693 attribute=(
char **) NULL;
1698 attributes=(
char **) sentinel;
1701 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1702 (*p ==
':') || (c <
'\0'))
1707 if (root->node == (XMLTreeInfo *) NULL)
1709 (void) ThrowMagickException(exception,GetMagickModule(),
1710 OptionWarning,
"ParseError",
"root tag missing");
1711 utf8=DestroyString(utf8);
1712 return(&root->root);
1714 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1715 while (isspace((
int) ((
unsigned char) *p)) != 0)
1717 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1718 (ignore_depth == 0))
1720 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1726 while ((root->attributes[i] != (
char **) NULL) &&
1727 (strcmp(root->attributes[i][0],tag) != 0))
1729 attribute=root->attributes[i];
1731 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1737 attributes=(
char **) AcquireQuantumMemory(4,
1738 sizeof(*attributes));
1740 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1741 (l+4),
sizeof(*attributes));
1742 if (attributes == (
char **) NULL)
1744 (void) ThrowMagickException(exception,GetMagickModule(),
1745 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1746 utf8=DestroyString(utf8);
1747 return(&root->root);
1749 attributes[l+2]=(
char *) NULL;
1750 attributes[l+1]=(
char *) NULL;
1752 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1753 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1754 attributes[l]=ConstantString(
"");
1758 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1760 if ((c ==
'"') || (c ==
'\''))
1767 while ((*p !=
'\0') && (*p != c))
1773 attributes[l]=ConstantString(
"");
1774 attributes[l+1]=ConstantString(
"");
1775 (void) DestroyXMLTreeAttributes(attributes);
1776 (void) ThrowMagickException(exception,
1777 GetMagickModule(),OptionWarning,
"ParseError",
1779 utf8=DestroyString(utf8);
1780 return(&root->root);
1783 while ((attribute != (
char **) NULL) &&
1784 (attribute[j] != (
char *) NULL) &&
1785 (strcmp(attribute[j],attributes[l]) != 0))
1787 attributes[l+1]=ParseEntities(attributes[l+1],
1788 root->entities,(attribute != (
char **) NULL) &&
1789 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
1792 attributes[l]=ConstantString(attributes[l]);
1794 while (isspace((
int) ((
unsigned char) *p)) != 0)
1800 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1809 if (((*p !=
'\0') && (*p !=
'>')) ||
1810 ((*p ==
'\0') && (terminal !=
'>')))
1812 if ((LocaleNCompare(tag,
"rdf:",4) != 0) &&
1813 (LocaleNCompare(tag,
"x:xmpmeta",9) != 0))
1816 (void) DestroyXMLTreeAttributes(attributes);
1817 (void) ThrowMagickException(exception,GetMagickModule(),
1818 OptionWarning,
"ParseError",
"missing >");
1819 utf8=DestroyString(utf8);
1820 return(&root->root);
1824 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1825 (void) DestroyXMLTreeAttributes(attributes);
1828 status=ParseOpenTag(root,tag,attributes,exception);
1829 if (status == MagickFalse)
1832 (void) DestroyXMLTreeAttributes(attributes);
1833 utf8=DestroyString(utf8);
1834 return(&root->root);
1836 (void) ParseCloseTag(root,tag,exception);
1842 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
1845 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1847 status=ParseOpenTag(root,tag,attributes,exception);
1848 if (status == MagickFalse)
1851 (void) DestroyXMLTreeAttributes(attributes);
1852 utf8=DestroyString(utf8);
1853 return(&root->root);
1859 (void) DestroyXMLTreeAttributes(attributes);
1866 (void) DestroyXMLTreeAttributes(attributes);
1867 (void) ThrowMagickException(exception,GetMagickModule(),
1868 OptionWarning,
"ParseError",
"missing >");
1869 utf8=DestroyString(utf8);
1870 return(&root->root);
1881 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
1883 if ((c ==
'\0') && (terminal !=
'>'))
1885 (void) ThrowMagickException(exception,GetMagickModule(),
1886 OptionWarning,
"ParseError",
"missing >");
1887 utf8=DestroyString(utf8);
1888 return(&root->root);
1891 if ((ignore_depth == 0) &&
1892 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1894 utf8=DestroyString(utf8);
1895 return(&root->root);
1897 if (ignore_depth > 0)
1900 if (isspace((
int) ((
unsigned char) *p)) != 0)
1901 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1904 if (strncmp(p,
"!--",3) == 0)
1910 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
1911 ((*p ==
'\0') && (terminal !=
'>')))
1913 (void) ThrowMagickException(exception,GetMagickModule(),
1914 OptionWarning,
"ParseError",
"unclosed <!--");
1915 utf8=DestroyString(utf8);
1916 return(&root->root);
1920 if (strncmp(p,
"![CDATA[",8) == 0)
1926 if (p != (
char *) NULL)
1929 if (ignore_depth == 0)
1930 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c',
1935 (void) ThrowMagickException(exception,GetMagickModule(),
1936 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
1937 utf8=DestroyString(utf8);
1938 return(&root->root);
1942 if (strncmp(p,
"!DOCTYPE",8) == 0)
1947 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
1948 ((l != 0) && ((*p !=
']') ||
1949 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
1950 l=(ssize_t) ((*p ==
'[') ? 1 : l))
1951 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
1952 if ((*p ==
'\0') && (terminal !=
'>'))
1954 (void) ThrowMagickException(exception,GetMagickModule(),
1955 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
1956 utf8=DestroyString(utf8);
1957 return(&root->root);
1960 tag=strchr(tag,
'[')+1;
1963 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
1965 if (status == MagickFalse)
1967 utf8=DestroyString(utf8);
1968 return(&root->root);
1982 if (p == (
char *) NULL)
1985 }
while ((*p !=
'\0') && (*p !=
'>'));
1986 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
1989 (void) ThrowMagickException(exception,GetMagickModule(),
1990 OptionWarning,
"ParseError",
"unclosed <?");
1991 utf8=DestroyString(utf8);
1992 return(&root->root);
1994 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
1998 (void) ThrowMagickException(exception,GetMagickModule(),
1999 OptionWarning,
"ParseError",
"unexpected <");
2000 utf8=DestroyString(utf8);
2001 return(&root->root);
2003 if ((p == (
char *) NULL) || (*p ==
'\0'))
2007 if ((*p !=
'\0') && (*p !=
'<'))
2012 while ((*p !=
'\0') && (*p !=
'<'))
2016 if (ignore_depth == 0)
2017 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&',exception);
2023 utf8=DestroyString(utf8);
2024 if (root->node == (XMLTreeInfo *) NULL)
2025 return(&root->root);
2026 if (root->node->tag == (
char *) NULL)
2028 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2029 "ParseError",
"root tag missing");
2030 return(&root->root);
2032 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2033 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2034 return(&root->root);
2059MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2062 *predefined_entities[NumberPredefinedEntities+1] =
2064 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2065 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2071 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2072 (void) memset(root,0,
sizeof(*root));
2073 root->root.tag=(
char *) NULL;
2074 if (tag != (
char *) NULL)
2075 root->root.tag=ConstantString(tag);
2076 root->node=(&root->root);
2077 root->root.content=ConstantString(
"");
2078 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2079 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2080 root->root.attributes=sentinel;
2081 root->attributes=(
char ***) root->root.attributes;
2082 root->processing_instructions=(
char ***) root->root.attributes;
2083 root->debug=IsEventLogging();
2084 root->signature=MagickCoreSignature;
2085 return(&root->root);
2114MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2115 const char *content)
2117 assert(xml_info != (XMLTreeInfo *) NULL);
2118 assert((xml_info->signature == MagickCoreSignature) ||
2119 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2120 if (IsEventLogging() != MagickFalse)
2121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2122 if (xml_info->content != (
char *) NULL)
2123 xml_info->content=DestroyString(xml_info->content);
2124 xml_info->content=(
char *) ConstantString(content);
2143MagickPrivate MagickBooleanType SkipXMLComment(
const char **xml)
2150 while (isspace((
int) ((
unsigned char) *p)) != 0)
2152 if (strncmp(p,
"<!--",4) != 0)
2155 p=strstr(p+4,
"-->");
2156 if (p == (
const char *) NULL)
2157 return(MagickFalse);
2178MagickPrivate MagickBooleanType SkipXMLDocType(
const char **xml)
2189 for (p=*xml; *p !=
'\0'; )
2191 if ((quote == 0) && (strncmp(p,
"<!--",4) == 0))
2193 if (SkipXMLComment(&p) == MagickFalse)
2204 if ((*p ==
'"') || (*p ==
'\''))
2212 if (bracket_depth > 0)
2216 if ((*p ==
'>') && (bracket_depth == 0))
2225 return(MagickFalse);
2242MagickExport
char *SubstituteXMLEntities(
const char *content,
2243 const MagickBooleanType pedantic)
2261 canonical_content=AcquireString((
char *) NULL);
2262 extent=MagickPathExtent;
2263 for (p=content; *p !=
'\0'; p++)
2265 if ((i+MagickPathExtent) > (ssize_t) extent)
2267 extent+=MagickPathExtent;
2268 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
2269 sizeof(*canonical_content));
2270 if (canonical_content == (
char *) NULL)
2271 return(canonical_content);
2277 i+=FormatLocaleString(canonical_content+i,extent,
"&");
2282 i+=FormatLocaleString(canonical_content+i,extent,
"<");
2287 i+=FormatLocaleString(canonical_content+i,extent,
">");
2292 i+=FormatLocaleString(canonical_content+i,extent,
""");
2297 if (pedantic == MagickFalse)
2299 canonical_content[i++]=(char) (*p);
2302 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2307 if (pedantic == MagickFalse)
2309 canonical_content[i++]=(char) (*p);
2312 i+=FormatLocaleString(canonical_content+i,extent,
"	");
2317 i+=FormatLocaleString(canonical_content+i,extent,
"
");
2322 canonical_content[i++]=(char) (*p);
2327 canonical_content[i]=
'\0';
2328 return(canonical_content);
2354static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2355 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2361 canonical_content=CanonicalXMLContent(source,pedantic);
2367 content=AcquireString(source);
2368 content[offset]=
'\0';
2369 canonical_content=CanonicalXMLContent(content,pedantic);
2370 content=DestroyString(content);
2372 if (canonical_content == (
char *) NULL)
2373 return(*destination);
2374 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2376 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2377 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2378 sizeof(**destination));
2379 if (*destination == (
char *) NULL)
2380 return(*destination);
2382 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2384 canonical_content=DestroyString(canonical_content);
2385 return(*destination);
2388static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2389 size_t *extent,
size_t start,
char ***attributes)
2404 content=(
char *)
"";
2405 if (xml_info->parent != (XMLTreeInfo *) NULL)
2406 content=xml_info->parent->content;
2408 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2409 start),source,length,extent,MagickFalse);
2410 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2412 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2413 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2414 if (*source == (
char *) NULL)
2417 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2418 "<%s",xml_info->tag);
2419 for (i=0; xml_info->attributes[i]; i+=2)
2421 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2422 if (attribute != xml_info->attributes[i+1])
2424 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2426 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2427 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2428 if (*source == (
char *) NULL)
2429 return((
char *) NULL);
2431 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2432 xml_info->attributes[i]);
2433 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2435 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2438 while ((attributes[i] != (
char **) NULL) &&
2439 (strcmp(attributes[i][0],xml_info->tag) != 0))
2442 while ((attributes[i] != (
char **) NULL) &&
2443 (attributes[i][j] != (
char *) NULL))
2445 if ((attributes[i][j+1] == (
char *) NULL) ||
2446 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2451 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2453 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2454 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2455 if (*source == (
char *) NULL)
2456 return((
char *) NULL);
2458 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2460 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2462 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2465 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2466 (*xml_info->content || (xml_info->child != (XMLTreeInfo *) NULL)) ?
2468 if (xml_info->child != (XMLTreeInfo *) NULL)
2469 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2471 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2473 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2475 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2476 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2477 if (*source == (
char *) NULL)
2478 return((
char *) NULL);
2480 if ((*xml_info->content !=
'\0') ||
2481 (xml_info->child != (XMLTreeInfo *) NULL))
2482 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2484 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2486 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2487 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2490 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2495MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2518 assert(xml_info != (XMLTreeInfo *) NULL);
2519 assert((xml_info->signature == MagickCoreSignature) ||
2520 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2521 if (IsEventLogging() != MagickFalse)
2522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2523 if (xml_info->tag == (
char *) NULL)
2524 return((
char *) NULL);
2525 xml=AcquireString((
char *) NULL);
2527 extent=MagickPathExtent;
2528 root=(XMLTreeRoot *) xml_info;
2529 while (root->root.parent != (XMLTreeInfo *) NULL)
2530 root=(XMLTreeRoot *) root->root.parent;
2531 parent=xml_info->parent;
2532 if (parent == (XMLTreeInfo *) NULL)
2533 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2538 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2539 p=root->processing_instructions[i][1];
2540 for (j=1; p != (
char *) NULL; j++)
2542 if (root->processing_instructions[i][k][j-1] ==
'>')
2544 p=root->processing_instructions[i][j];
2547 q=root->processing_instructions[i][0];
2548 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2550 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2551 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2552 if (xml == (
char *) NULL)
2555 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2556 *p !=
'\0' ?
" " :
"",p);
2557 p=root->processing_instructions[i][j];
2560 ordered=xml_info->ordered;
2561 xml_info->parent=(XMLTreeInfo *) NULL;
2562 xml_info->ordered=(XMLTreeInfo *) NULL;
2563 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2564 xml_info->parent=parent;
2565 xml_info->ordered=ordered;
2566 if (parent == (XMLTreeInfo *) NULL)
2567 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2572 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2573 p=root->processing_instructions[i][1];
2574 for (j=1; p != (
char *) NULL; j++)
2576 if (root->processing_instructions[i][k][j-1] ==
'<')
2578 p=root->processing_instructions[i][j];
2581 q=root->processing_instructions[i][0];
2582 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2584 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2585 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2586 if (xml == (
char *) NULL)
2589 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2590 *p !=
'\0' ?
" " :
"",p);
2591 p=root->processing_instructions[i][j];
2594 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));