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,
137 *sentinel[] = { (
char *) NULL };
168static XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
169 XMLTreeInfo *child,
const size_t offset)
176 child->ordered=(XMLTreeInfo *) NULL;
177 child->sibling=(XMLTreeInfo *) NULL;
178 child->next=(XMLTreeInfo *) NULL;
179 child->offset=offset;
180 child->parent=xml_info;
181 if (xml_info->child == (XMLTreeInfo *) NULL)
183 xml_info->child=child;
186 head=xml_info->child;
187 if (head->offset > offset)
190 xml_info->child=child;
195 while ((node->ordered != (XMLTreeInfo *) NULL) &&
196 (node->ordered->offset <= offset))
198 child->ordered=node->ordered;
201 previous=(XMLTreeInfo *) NULL;
203 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
208 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
210 while ((node->next != (XMLTreeInfo *) NULL) &&
211 (node->next->offset <= offset))
213 child->next=node->next;
218 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
219 previous->sibling=node->sibling;
221 previous=(XMLTreeInfo *) NULL;
223 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
229 if (previous != (XMLTreeInfo *) NULL)
230 previous->sibling=child;
235MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
236 const char *tag,
const size_t offset)
241 if (xml_info == (XMLTreeInfo *) NULL)
242 return((XMLTreeInfo *) NULL);
243 child=(XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
244 if (child == (XMLTreeInfo *) NULL)
245 return((XMLTreeInfo *) NULL);
246 (void) memset(child,0,
sizeof(*child));
247 child->tag=ConstantString(tag);
248 child->attributes=sentinel;
249 child->content=ConstantString(
"");
250 child->debug=IsEventLogging();
251 child->signature=MagickCoreSignature;
252 return(InsertTagIntoXMLTree(xml_info,child,offset));
284static char *CanonicalXMLContent(
const char *content,
285 const MagickBooleanType pedantic)
300 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
301 if (utf8 == (
unsigned char *) NULL)
302 return((
char *) NULL);
303 for (p=utf8; *p !=
'\0'; p++)
304 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
311 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
312 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
313 if (base64 == (
char *) NULL)
314 return((
char *) NULL);
315 canonical_content=AcquireString(
"<base64>");
316 (void) ConcatenateString(&canonical_content,base64);
317 base64=DestroyString(base64);
318 (void) ConcatenateString(&canonical_content,
"</base64>");
319 return(canonical_content);
321 canonical_content=SubstituteXMLEntities((
const char *) utf8,pedantic);
322 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
323 return(canonical_content);
326MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
327 const char *path,
const size_t offset)
331 subnode[MagickPathExtent],
332 tag[MagickPathExtent];
345 assert(xml_info != (XMLTreeInfo *) NULL);
346 assert((xml_info->signature == MagickCoreSignature) ||
347 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
348 if (IsEventLogging() != MagickFalse)
349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
351 components=GetPathComponents(path,&number_components);
352 if (components == (
char **) NULL)
353 return((XMLTreeInfo *) NULL);
354 for (i=0; i < (ssize_t) number_components; i++)
356 GetPathComponent(components[i],SubimagePath,subnode);
357 GetPathComponent(components[i],CanonicalPath,tag);
358 child=GetXMLTreeChild(node,tag);
359 if (child == (XMLTreeInfo *) NULL)
360 child=AddChildToXMLTree(node,tag,offset);
362 if (node == (XMLTreeInfo *) NULL)
364 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
367 if (node == (XMLTreeInfo *) NULL)
370 if (node == (XMLTreeInfo *) NULL)
372 components[i]=DestroyString(components[i]);
374 for ( ; i < (ssize_t) number_components; i++)
375 components[i]=DestroyString(components[i]);
376 components=(
char **) RelinquishMagickMemory(components);
404 *DestroyXMLTree_(XMLTreeInfo *,
const size_t);
406static char **DestroyXMLTreeAttributes(
char **attributes)
414 if ((attributes == (
char **) NULL) || (attributes == sentinel))
415 return((
char **) NULL);
416 for (i=0; attributes[i] != (
char *) NULL; i+=2)
421 if (attributes[i] != (
char *) NULL)
422 attributes[i]=DestroyString(attributes[i]);
423 if (attributes[i+1] != (
char *) NULL)
424 attributes[i+1]=DestroyString(attributes[i+1]);
426 attributes=(
char **) RelinquishMagickMemory(attributes);
427 return((
char **) NULL);
430static void DestroyXMLTreeChild(XMLTreeInfo *xml_info,
437 child=xml_info->child;
438 while (child != (XMLTreeInfo *) NULL)
442 node->child=(XMLTreeInfo *) NULL;
443 (void) DestroyXMLTree_(node,depth+1);
447static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info,
454 ordered=xml_info->ordered;
455 while (ordered != (XMLTreeInfo *) NULL)
458 ordered=node->ordered;
459 node->ordered=(XMLTreeInfo *) NULL;
460 (void) DestroyXMLTree_(node,depth+1);
464static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
476 assert(xml_info != (XMLTreeInfo *) NULL);
477 assert((xml_info->signature == MagickCoreSignature) ||
478 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
479 if (IsEventLogging() != MagickFalse)
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
481 if (xml_info->parent != (XMLTreeInfo *) NULL)
486 root=(XMLTreeRoot *) xml_info;
487 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
488 root->entities[i+1]=DestroyString(root->entities[i+1]);
489 root->entities=(
char **) RelinquishMagickMemory(root->entities);
490 for (i=0; root->attributes[i] != (
char **) NULL; i++)
492 attributes=root->attributes[i];
493 if (attributes[0] != (
char *) NULL)
494 attributes[0]=DestroyString(attributes[0]);
495 for (j=1; attributes[j] != (
char *) NULL; j+=3)
497 if (attributes[j] != (
char *) NULL)
498 attributes[j]=DestroyString(attributes[j]);
499 if (attributes[j+1] != (
char *) NULL)
500 attributes[j+1]=DestroyString(attributes[j+1]);
501 if (attributes[j+2] != (
char *) NULL)
502 attributes[j+2]=DestroyString(attributes[j+2]);
504 attributes=(
char **) RelinquishMagickMemory(attributes);
506 if (root->attributes[0] != (
char **) NULL)
507 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
508 if (root->processing_instructions[0] != (
char **) NULL)
510 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
512 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
513 root->processing_instructions[i][j]=DestroyString(
514 root->processing_instructions[i][j]);
515 root->processing_instructions[i][j+1]=DestroyString(
516 root->processing_instructions[i][j+1]);
517 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
518 root->processing_instructions[i]);
520 root->processing_instructions=(
char ***) RelinquishMagickMemory(
521 root->processing_instructions);
525static XMLTreeInfo *DestroyXMLTree_(XMLTreeInfo *xml_info,
528 assert(xml_info != (XMLTreeInfo *) NULL);
529 assert((xml_info->signature == MagickCoreSignature) ||
530 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
531 if (IsEventLogging() != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
533 if (depth > MagickMaxRecursionDepth)
534 return((XMLTreeInfo *) NULL);
535 DestroyXMLTreeChild(xml_info,depth+1);
536 DestroyXMLTreeOrdered(xml_info,depth+1);
537 DestroyXMLTreeRoot(xml_info);
538 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
539 xml_info->content=DestroyString(xml_info->content);
540 xml_info->tag=DestroyString(xml_info->tag);
541 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
542 return((XMLTreeInfo *) NULL);
545MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
547 return(DestroyXMLTree_(xml_info,0));
574MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
595 assert(filename != (
const char *) NULL);
598 if (LocaleCompare(filename,
"-") != 0)
599 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
601 return((
char *) NULL);
602 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
604 if ((file == fileno(stdin)) || (offset < 0) ||
605 (offset != (MagickOffsetType) ((ssize_t) offset)))
616 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
617 quantum=(size_t) MagickMaxBufferExtent;
618 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
619 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
620 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
621 for (i=0; xml != (
char *) NULL; i+=(size_t) count)
623 count=MagickRead(file,xml+i,quantum);
626 if (~((
size_t) i) < (quantum+1))
628 xml=(
char *) RelinquishMagickMemory(xml);
631 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
632 if ((i+(
size_t) count) >= extent)
635 if (LocaleCompare(filename,
"-") != 0)
636 file=close_utf8(file);
637 if (xml == (
char *) NULL)
638 return((
char *) NULL);
641 xml=(
char *) RelinquishMagickMemory(xml);
642 return((
char *) NULL);
644 length=MagickMin(i+(
size_t) count,extent);
648 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
650 if (~length >= (MagickPathExtent-1))
651 xml=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*xml));
652 if (xml == (
char *) NULL)
654 file=close_utf8(file);
655 return((
char *) NULL);
657 map=MapBlob(file,ReadMode,0,length);
658 if (map != (
char *) NULL)
660 (void) memcpy(xml,map,length);
661 (void) UnmapBlob(map,length);
665 (void) lseek(file,0,SEEK_SET);
666 for (i=0; i < length; i+=(size_t) count)
668 count=MagickRead(file,xml+i,(
size_t) MagickMin(length-i,(
size_t)
669 MagickMaxBufferExtent));
675 file=close_utf8(file)-1;
676 xml=(
char *) RelinquishMagickMemory(xml);
677 return((
char *) NULL);
681 if (LocaleCompare(filename,
"-") != 0)
682 file=close_utf8(file);
684 xml=(
char *) RelinquishMagickMemory(xml);
710MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
712 assert(xml_info != (XMLTreeInfo *) NULL);
713 assert((xml_info->signature == MagickCoreSignature) ||
714 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
715 if (IsEventLogging() != MagickFalse)
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
717 return(xml_info->next);
745MagickExport
const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
755 assert(xml_info != (XMLTreeInfo *) NULL);
756 assert((xml_info->signature == MagickCoreSignature) ||
757 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
758 if (IsEventLogging() != MagickFalse)
759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
760 if (xml_info->attributes == (
char **) NULL)
761 return((
const char *) NULL);
763 while ((xml_info->attributes[i] != (
char *) NULL) &&
764 (strcmp(xml_info->attributes[i],tag) != 0))
766 if (xml_info->attributes[i] != (
char *) NULL)
767 return(xml_info->attributes[i+1]);
768 root=(XMLTreeRoot*) xml_info;
769 while (root->root.parent != (XMLTreeInfo *) NULL)
770 root=(XMLTreeRoot *) root->root.parent;
772 while ((root->attributes[i] != (
char **) NULL) &&
773 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
775 if (root->attributes[i] == (
char **) NULL)
776 return((
const char *) NULL);
778 while ((root->attributes[i][j] != (
char *) NULL) &&
779 (strcmp(root->attributes[i][j],tag) != 0))
781 if (root->attributes[i][j] == (
char *) NULL)
782 return((
const char *) NULL);
783 return(root->attributes[i][j+1]);
809MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
814 assert(xml_info != (XMLTreeInfo *) NULL);
815 assert((xml_info->signature == MagickCoreSignature) ||
816 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
817 if (IsEventLogging() != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
819 child=xml_info->child;
820 if (tag != (
const char *) NULL)
821 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
822 child=child->sibling;
849MagickExport
const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
851 assert(xml_info != (XMLTreeInfo *) NULL);
852 assert((xml_info->signature == MagickCoreSignature) ||
853 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
854 if (IsEventLogging() != MagickFalse)
855 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
856 return(xml_info->content);
881MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
883 assert(xml_info != (XMLTreeInfo *) NULL);
884 assert((xml_info->signature == MagickCoreSignature) ||
885 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
886 if (IsEventLogging() != MagickFalse)
887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
888 return(xml_info->sibling);
913MagickExport
const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
915 assert(xml_info != (XMLTreeInfo *) NULL);
916 assert((xml_info->signature == MagickCoreSignature) ||
917 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
918 if (IsEventLogging() != MagickFalse)
919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
920 return(xml_info->tag);
949static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
967 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
968 if (utf8 == (
char *) NULL)
969 return((
char *) NULL);
970 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
976 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
982 for (i=2; i < (ssize_t) (*length-1); i+=2)
984 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
985 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
986 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
988 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
989 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
991 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
993 if ((
size_t) (j+MagickPathExtent) > extent)
995 extent=(size_t) j+MagickPathExtent;
996 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
997 if (utf8 == (
char *) NULL)
1010 for (bits=0;
byte != 0;
byte/=2)
1013 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1017 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1022 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1023 if (utf8 != (
char *) NULL)
1028static char *ParseEntities(
char *xml,
char **entities,
int state)
1052 for ( ; *xml !=
'\0'; xml++)
1053 while (*xml ==
'\r')
1057 (void) memmove(xml,xml+1,strlen(xml));
1061 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1062 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1074 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1080 c=strtol(xml+2,&entity,10);
1082 c=strtol(xml+3,&entity,16);
1083 if ((c == 0) || (*entity !=
';'))
1099 for (i=0;
byte != 0;
byte/=2)
1102 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1107 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1111 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1114 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1115 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1121 while ((entities[i] != (
char *) NULL) &&
1122 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1124 if (entities[i++] == (
char *) NULL)
1127 if (entities[i] != (
char *) NULL)
1132 length=strlen(entities[i]);
1133 entity=strchr(xml,
';');
1134 if ((entity != (
char *) NULL) &&
1135 ((length-1L) >= (
size_t) (entity-xml)))
1137 offset=(ssize_t) (xml-p);
1138 extent=((size_t) offset+length+strlen(entity));
1141 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1142 if (p != (
char *) NULL)
1150 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1151 sizeof(*extent_xml));
1152 if (extent_xml != (
char *) NULL)
1154 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1155 (void) CopyMagickString(extent_xml,p,extent*
1156 sizeof(*extent_xml));
1160 if (p == (
char *) NULL)
1161 ThrowFatalException(ResourceLimitFatalError,
1162 "MemoryAllocationFailed");
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)
1209 xml_info=root->node;
1210 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1214 xml=ParseEntities(xml,root->entities,state);
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;
1239 return((XMLTreeInfo *) NULL);
1242static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1243 const size_t depth,
char **entities)
1251 if (depth > MagickMaxRecursionDepth)
1252 return(MagickFalse);
1255 while ((*xml !=
'\0') && (*xml !=
'&'))
1259 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1260 return(MagickFalse);
1262 while ((entities[i] != (
char *) NULL) &&
1263 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1265 if ((entities[i] != (
char *) NULL) &&
1266 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1267 return(MagickFalse);
1271static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1283 xml+=strcspn(xml,XMLWhitespace);
1287 xml+=strspn(xml+1,XMLWhitespace)+1;
1289 if (strcmp(target,
"xml") == 0)
1291 xml=strstr(xml,
"standalone");
1292 if ((xml != (
char *) NULL) &&
1293 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1294 root->standalone=MagickTrue;
1297 if (root->processing_instructions[0] == (
char **) NULL)
1299 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1300 *root->processing_instructions));
1301 *root->processing_instructions=(
char **) NULL;
1304 while ((root->processing_instructions[i] != (
char **) NULL) &&
1305 (strcmp(target,root->processing_instructions[i][0]) != 0))
1307 if (root->processing_instructions[i] == (
char **) NULL)
1309 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1310 root->processing_instructions,(
size_t) (i+2),
1311 sizeof(*root->processing_instructions));
1312 if (root->processing_instructions == (
char ***) NULL)
1313 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1314 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1315 sizeof(**root->processing_instructions));
1316 if (root->processing_instructions[i] == (
char **) NULL)
1317 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1318 root->processing_instructions[i+1]=(
char **) NULL;
1319 root->processing_instructions[i][0]=ConstantString(target);
1320 root->processing_instructions[i][1]=(
char *)
1321 root->processing_instructions[i+1];
1322 root->processing_instructions[i+1]=(
char **) NULL;
1323 root->processing_instructions[i][2]=ConstantString(
"");
1326 while (root->processing_instructions[i][j] != (
char *) NULL)
1328 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1329 root->processing_instructions[i],(
size_t) (j+3),
1330 sizeof(**root->processing_instructions));
1331 if (root->processing_instructions[i] == (
char **) NULL)
1332 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1333 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1334 root->processing_instructions[i][j+1],(
size_t) (j+1),
1335 sizeof(***root->processing_instructions));
1336 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1337 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1338 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1339 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1340 root->processing_instructions[i][j]=ConstantString(xml);
1341 root->processing_instructions[i][j+1]=(
char *) NULL;
1344static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1345 size_t length,ExceptionInfo *exception)
1351 **predefined_entities,
1361 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1362 if (predefined_entities == (
char **) NULL)
1363 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1364 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1365 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1367 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1371 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1376 if (strspn(xml+8,XMLWhitespace) == 0)
1378 xml+=strspn(xml+8,XMLWhitespace)+8;
1380 n=xml+strspn(xml,XMLWhitespace
"%");
1381 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1383 xml=n+strcspn(n,XMLWhitespace);
1387 v=xml+strspn(xml+1,XMLWhitespace)+1;
1390 if ((q !=
'"') && (q !=
'\''))
1395 xml=strchr(xml,
'>');
1398 entities=(*c ==
'%') ? predefined_entities : root->entities;
1399 for (i=0; entities[i] != (
char *) NULL; i++) ;
1400 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1402 if (entities == (
char **) NULL)
1403 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1405 predefined_entities=entities;
1407 root->entities=entities;
1411 if (xml != (
char *) NULL)
1416 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1417 entities[i+2]=(
char *) NULL;
1418 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1422 if (entities[i+1] != v)
1423 entities[i+1]=DestroyString(entities[i+1]);
1424 (void) ThrowMagickException(exception,GetMagickModule(),
1425 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1426 predefined_entities=(
char **) RelinquishMagickMemory(
1427 predefined_entities);
1428 return(MagickFalse);
1432 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1437 t=xml+strspn(xml+9,XMLWhitespace)+9;
1440 (void) ThrowMagickException(exception,GetMagickModule(),
1441 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1442 predefined_entities=(
char **) RelinquishMagickMemory(
1443 predefined_entities);
1444 return(MagickFalse);
1446 xml=t+strcspn(t,XMLWhitespace
">");
1451 while ((root->attributes[i] != (
char **) NULL) &&
1452 (n != (
char *) NULL) &&
1453 (strcmp(n,root->attributes[i][0]) != 0))
1455 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1458 xml=n+strcspn(n,XMLWhitespace);
1463 (void) ThrowMagickException(exception,GetMagickModule(),
1464 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1465 predefined_entities=(
char **) RelinquishMagickMemory(
1466 predefined_entities);
1467 return(MagickFalse);
1469 xml+=strspn(xml+1,XMLWhitespace)+1;
1470 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1471 if (strncmp(xml,
"NOTATION",8) == 0)
1472 xml+=strspn(xml+8,XMLWhitespace)+8;
1473 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1474 strcspn(xml,XMLWhitespace);
1475 if (xml == (
char *) NULL)
1477 (void) ThrowMagickException(exception,GetMagickModule(),
1478 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1479 predefined_entities=(
char **) RelinquishMagickMemory(
1480 predefined_entities);
1481 return(MagickFalse);
1483 xml+=strspn(xml,XMLWhitespace
")");
1484 if (strncmp(xml,
"#FIXED",6) == 0)
1485 xml+=strspn(xml+6,XMLWhitespace)+6;
1488 xml+=strcspn(xml,XMLWhitespace
">")-1;
1494 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1495 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1499 (void) ThrowMagickException(exception,GetMagickModule(),
1500 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1501 predefined_entities=(
char **) RelinquishMagickMemory(
1502 predefined_entities);
1503 return(MagickFalse);
1505 if (root->attributes[i] == (
char **) NULL)
1511 root->attributes=(
char ***) AcquireQuantumMemory(2,
1512 sizeof(*root->attributes));
1514 root->attributes=(
char ***) ResizeQuantumMemory(
1515 root->attributes,(
size_t) (i+2),
1516 sizeof(*root->attributes));
1517 if (root->attributes == (
char ***) NULL)
1518 ThrowFatalException(ResourceLimitFatalError,
1519 "MemoryAllocationFailed");
1520 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1521 sizeof(**root->attributes));
1522 if (root->attributes[i] == (
char **) NULL)
1523 ThrowFatalException(ResourceLimitFatalError,
1524 "MemoryAllocationFailed");
1525 root->attributes[i][0]=ConstantString(t);
1526 root->attributes[i][1]=(
char *) NULL;
1527 root->attributes[i+1]=(
char **) NULL;
1529 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1530 root->attributes[i]=(
char **) ResizeQuantumMemory(
1531 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1532 if (root->attributes[i] == (
char **) NULL)
1533 ThrowFatalException(ResourceLimitFatalError,
1534 "MemoryAllocationFailed");
1535 root->attributes[i][j+3]=(
char *) NULL;
1536 root->attributes[i][j+2]=ConstantString(c);
1537 root->attributes[i][j+1]=(
char *) NULL;
1538 if (v != (
char *) NULL)
1539 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1540 root->attributes[i][j]=ConstantString(n);
1544 if (strncmp(xml,
"<!--", 4) == 0)
1545 xml=strstr(xml+4,
"-->");
1547 if (strncmp(xml,
"<?", 2) == 0)
1551 if (xml != (
char *) NULL)
1553 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1559 xml=strchr(xml,
'>');
1561 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1564 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1568static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
1573 xml_info=root->node;
1574 if (xml_info->tag == (
char *) NULL)
1575 xml_info->tag=ConstantString(tag);
1577 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1578 if (xml_info != (XMLTreeInfo *) NULL)
1579 xml_info->attributes=attributes;
1580 root->node=xml_info;
1591static inline MagickBooleanType IsSkipTag(
const char *tag)
1597 while (ignore_tags[i] != (
const char *) NULL)
1599 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1603 return(MagickFalse);
1606MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1637 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1639 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1640 "ParseError",
"root tag missing");
1641 return((XMLTreeInfo *) NULL);
1643 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1645 utf8=ConvertUTF16ToUTF8(xml,&length);
1646 if (utf8 == (
char *) NULL)
1648 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1649 "ParseError",
"UTF16 to UTF8 failed");
1650 return((XMLTreeInfo *) NULL);
1654 utf8=DestroyString(utf8);
1655 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1656 "ParseError",
"root tag missing");
1657 return((XMLTreeInfo *) NULL);
1659 terminal=utf8[length-1];
1660 utf8[length-1]=
'\0';
1662 while ((*p !=
'\0') && (*p !=
'<'))
1666 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1667 "ParseError",
"root tag missing");
1668 utf8=DestroyString(utf8);
1669 return((XMLTreeInfo *) NULL);
1671 attribute=(
char **) NULL;
1676 attributes=(
char **) sentinel;
1679 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1680 (*p ==
':') || (c <
'\0'))
1685 if (root->node == (XMLTreeInfo *) NULL)
1687 (void) ThrowMagickException(exception,GetMagickModule(),
1688 OptionWarning,
"ParseError",
"root tag missing");
1689 utf8=DestroyString(utf8);
1690 return(&root->root);
1692 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1693 while (isspace((
int) ((
unsigned char) *p)) != 0)
1695 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1696 (ignore_depth == 0))
1698 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1704 while ((root->attributes[i] != (
char **) NULL) &&
1705 (strcmp(root->attributes[i][0],tag) != 0))
1707 attribute=root->attributes[i];
1709 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1715 attributes=(
char **) AcquireQuantumMemory(4,
1716 sizeof(*attributes));
1718 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1719 (l+4),
sizeof(*attributes));
1720 if (attributes == (
char **) NULL)
1722 (void) ThrowMagickException(exception,GetMagickModule(),
1723 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1724 utf8=DestroyString(utf8);
1725 return(&root->root);
1727 attributes[l+2]=(
char *) NULL;
1728 attributes[l+1]=(
char *) NULL;
1730 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1731 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1732 attributes[l]=ConstantString(
"");
1736 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1738 if ((c ==
'"') || (c ==
'\''))
1745 while ((*p !=
'\0') && (*p != c))
1751 attributes[l]=ConstantString(
"");
1752 attributes[l+1]=ConstantString(
"");
1753 (void) DestroyXMLTreeAttributes(attributes);
1754 (void) ThrowMagickException(exception,
1755 GetMagickModule(),OptionWarning,
"ParseError",
1757 utf8=DestroyString(utf8);
1758 return(&root->root);
1761 while ((attribute != (
char **) NULL) &&
1762 (attribute[j] != (
char *) NULL) &&
1763 (strcmp(attribute[j],attributes[l]) != 0))
1765 attributes[l+1]=ParseEntities(attributes[l+1],
1766 root->entities,(attribute != (
char **) NULL) &&
1767 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
1770 attributes[l]=ConstantString(attributes[l]);
1772 while (isspace((
int) ((
unsigned char) *p)) != 0)
1778 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1787 if (((*p !=
'\0') && (*p !=
'>')) ||
1788 ((*p ==
'\0') && (terminal !=
'>')))
1791 (void) DestroyXMLTreeAttributes(attributes);
1792 (void) ThrowMagickException(exception,GetMagickModule(),
1793 OptionWarning,
"ParseError",
"missing >");
1794 utf8=DestroyString(utf8);
1795 return(&root->root);
1797 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1798 (void) DestroyXMLTreeAttributes(attributes);
1801 ParseOpenTag(root,tag,attributes);
1802 (void) ParseCloseTag(root,tag,exception);
1808 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
1811 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1812 ParseOpenTag(root,tag,attributes);
1816 (void) DestroyXMLTreeAttributes(attributes);
1823 (void) DestroyXMLTreeAttributes(attributes);
1824 (void) ThrowMagickException(exception,GetMagickModule(),
1825 OptionWarning,
"ParseError",
"missing >");
1826 utf8=DestroyString(utf8);
1827 return(&root->root);
1838 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
1840 if ((c ==
'\0') && (terminal !=
'>'))
1842 (void) ThrowMagickException(exception,GetMagickModule(),
1843 OptionWarning,
"ParseError",
"missing >");
1844 utf8=DestroyString(utf8);
1845 return(&root->root);
1848 if ((ignore_depth == 0) &&
1849 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1851 utf8=DestroyString(utf8);
1852 return(&root->root);
1854 if (ignore_depth > 0)
1857 if (isspace((
int) ((
unsigned char) *p)) != 0)
1858 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1861 if (strncmp(p,
"!--",3) == 0)
1867 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
1868 ((*p ==
'\0') && (terminal !=
'>')))
1870 (void) ThrowMagickException(exception,GetMagickModule(),
1871 OptionWarning,
"ParseError",
"unclosed <!--");
1872 utf8=DestroyString(utf8);
1873 return(&root->root);
1877 if (strncmp(p,
"![CDATA[",8) == 0)
1883 if (p != (
char *) NULL)
1886 if (ignore_depth == 0)
1887 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
1891 (void) ThrowMagickException(exception,GetMagickModule(),
1892 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
1893 utf8=DestroyString(utf8);
1894 return(&root->root);
1898 if (strncmp(p,
"!DOCTYPE",8) == 0)
1903 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
1904 ((l != 0) && ((*p !=
']') ||
1905 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
1906 l=(ssize_t) ((*p ==
'[') ? 1 : l))
1907 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
1908 if ((*p ==
'\0') && (terminal !=
'>'))
1910 (void) ThrowMagickException(exception,GetMagickModule(),
1911 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
1912 utf8=DestroyString(utf8);
1913 return(&root->root);
1916 tag=strchr(tag,
'[')+1;
1919 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
1921 if (status == MagickFalse)
1923 utf8=DestroyString(utf8);
1924 return(&root->root);
1938 if (p == (
char *) NULL)
1941 }
while ((*p !=
'\0') && (*p !=
'>'));
1942 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
1945 (void) ThrowMagickException(exception,GetMagickModule(),
1946 OptionWarning,
"ParseError",
"unclosed <?");
1947 utf8=DestroyString(utf8);
1948 return(&root->root);
1950 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
1954 (void) ThrowMagickException(exception,GetMagickModule(),
1955 OptionWarning,
"ParseError",
"unexpected <");
1956 utf8=DestroyString(utf8);
1957 return(&root->root);
1959 if ((p == (
char *) NULL) || (*p ==
'\0'))
1963 if ((*p !=
'\0') && (*p !=
'<'))
1968 while ((*p !=
'\0') && (*p !=
'<'))
1972 if (ignore_depth == 0)
1973 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
1979 utf8=DestroyString(utf8);
1980 if (root->node == (XMLTreeInfo *) NULL)
1981 return(&root->root);
1982 if (root->node->tag == (
char *) NULL)
1984 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1985 "ParseError",
"root tag missing");
1986 return(&root->root);
1988 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1989 "ParseError",
"unclosed tag: '%s'",root->node->tag);
1990 return(&root->root);
2015MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2018 *predefined_entities[NumberPredefinedEntities+1] =
2020 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2021 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2027 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2028 (void) memset(root,0,
sizeof(*root));
2029 root->root.tag=(
char *) NULL;
2030 if (tag != (
char *) NULL)
2031 root->root.tag=ConstantString(tag);
2032 root->node=(&root->root);
2033 root->root.content=ConstantString(
"");
2034 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2035 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2036 root->root.attributes=sentinel;
2037 root->attributes=(
char ***) root->root.attributes;
2038 root->processing_instructions=(
char ***) root->root.attributes;
2039 root->debug=IsEventLogging();
2040 root->signature=MagickCoreSignature;
2041 return(&root->root);
2070MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2071 const char *content)
2073 assert(xml_info != (XMLTreeInfo *) NULL);
2074 assert((xml_info->signature == MagickCoreSignature) ||
2075 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2076 if (IsEventLogging() != MagickFalse)
2077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2078 if (xml_info->content != (
char *) NULL)
2079 xml_info->content=DestroyString(xml_info->content);
2080 xml_info->content=(
char *) ConstantString(content);
2107static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2108 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2114 canonical_content=CanonicalXMLContent(source,pedantic);
2120 content=AcquireString(source);
2121 content[offset]=
'\0';
2122 canonical_content=CanonicalXMLContent(content,pedantic);
2123 content=DestroyString(content);
2125 if (canonical_content == (
char *) NULL)
2126 return(*destination);
2127 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2129 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2130 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2131 sizeof(**destination));
2132 if (*destination == (
char *) NULL)
2133 return(*destination);
2135 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2137 canonical_content=DestroyString(canonical_content);
2138 return(*destination);
2141static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2142 size_t *extent,
size_t start,
char ***attributes)
2157 content=(
char *)
"";
2158 if (xml_info->parent != (XMLTreeInfo *) NULL)
2159 content=xml_info->parent->content;
2161 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2162 start),source,length,extent,MagickFalse);
2163 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2165 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2166 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2167 if (*source == (
char *) NULL)
2170 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2171 "<%s",xml_info->tag);
2172 for (i=0; xml_info->attributes[i]; i+=2)
2174 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2175 if (attribute != xml_info->attributes[i+1])
2177 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2179 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2180 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2181 if (*source == (
char *) NULL)
2182 return((
char *) NULL);
2184 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2185 xml_info->attributes[i]);
2186 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2188 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2191 while ((attributes[i] != (
char **) NULL) &&
2192 (strcmp(attributes[i][0],xml_info->tag) != 0))
2195 while ((attributes[i] != (
char **) NULL) &&
2196 (attributes[i][j] != (
char *) NULL))
2198 if ((attributes[i][j+1] == (
char *) NULL) ||
2199 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2204 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2206 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2207 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2208 if (*source == (
char *) NULL)
2209 return((
char *) NULL);
2211 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2213 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2215 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2218 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2219 *xml_info->content ?
">" :
"/>");
2220 if (xml_info->child != (XMLTreeInfo *) NULL)
2221 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2223 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2225 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2227 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2228 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2229 if (*source == (
char *) NULL)
2230 return((
char *) NULL);
2232 if (*xml_info->content !=
'\0')
2233 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2235 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2237 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2238 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2241 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2246MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2269 assert(xml_info != (XMLTreeInfo *) NULL);
2270 assert((xml_info->signature == MagickCoreSignature) ||
2271 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2272 if (IsEventLogging() != MagickFalse)
2273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2274 if (xml_info->tag == (
char *) NULL)
2275 return((
char *) NULL);
2276 xml=AcquireString((
char *) NULL);
2278 extent=MagickPathExtent;
2279 root=(XMLTreeRoot *) xml_info;
2280 while (root->root.parent != (XMLTreeInfo *) NULL)
2281 root=(XMLTreeRoot *) root->root.parent;
2282 parent=xml_info->parent;
2283 if (parent == (XMLTreeInfo *) NULL)
2284 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2289 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2290 p=root->processing_instructions[i][1];
2291 for (j=1; p != (
char *) NULL; j++)
2293 if (root->processing_instructions[i][k][j-1] ==
'>')
2295 p=root->processing_instructions[i][j];
2298 q=root->processing_instructions[i][0];
2299 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2301 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2302 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2303 if (xml == (
char *) NULL)
2306 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2307 *p !=
'\0' ?
" " :
"",p);
2308 p=root->processing_instructions[i][j];
2311 ordered=xml_info->ordered;
2312 xml_info->parent=(XMLTreeInfo *) NULL;
2313 xml_info->ordered=(XMLTreeInfo *) NULL;
2314 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2315 xml_info->parent=parent;
2316 xml_info->ordered=ordered;
2317 if (parent == (XMLTreeInfo *) NULL)
2318 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2323 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2324 p=root->processing_instructions[i][1];
2325 for (j=1; p != (
char *) NULL; j++)
2327 if (root->processing_instructions[i][k][j-1] ==
'<')
2329 p=root->processing_instructions[i][j];
2332 q=root->processing_instructions[i][0];
2333 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2335 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2336 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2337 if (xml == (
char *) NULL)
2340 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2341 *p !=
'\0' ?
" " :
"",p);
2342 p=root->processing_instructions[i][j];
2345 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));