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_CLOEXEC | 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=(int) strtol(xml+2,&entity,10);
1082 c=(int) 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 int delimiter,
const size_t depth,
char **entities)
1251 if (depth > MagickMaxRecursionDepth)
1252 return(MagickFalse);
1255 while ((*xml !=
'\0') && (*xml != delimiter))
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],delimiter,depth+1,entities) == MagickFalse))
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) &&
1419 (ValidateEntities(n,entities[i+1],
'&',0,entities) != MagickFalse))
1423 if (entities[i+1] != v)
1424 entities[i+1]=DestroyString(entities[i+1]);
1425 (void) ThrowMagickException(exception,GetMagickModule(),
1426 OptionWarning,
"ParseError",
"circular entity declaration %s",n);
1427 predefined_entities=(
char **) RelinquishMagickMemory(
1428 predefined_entities);
1429 return(MagickFalse);
1433 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1438 t=xml+strspn(xml+9,XMLWhitespace)+9;
1441 (void) ThrowMagickException(exception,GetMagickModule(),
1442 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1443 predefined_entities=(
char **) RelinquishMagickMemory(
1444 predefined_entities);
1445 return(MagickFalse);
1447 xml=t+strcspn(t,XMLWhitespace
">");
1452 while ((root->attributes[i] != (
char **) NULL) &&
1453 (n != (
char *) NULL) &&
1454 (strcmp(n,root->attributes[i][0]) != 0))
1456 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1459 xml=n+strcspn(n,XMLWhitespace);
1464 (void) ThrowMagickException(exception,GetMagickModule(),
1465 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1466 predefined_entities=(
char **) RelinquishMagickMemory(
1467 predefined_entities);
1468 return(MagickFalse);
1470 xml+=strspn(xml+1,XMLWhitespace)+1;
1471 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1472 if (strncmp(xml,
"NOTATION",8) == 0)
1473 xml+=strspn(xml+8,XMLWhitespace)+8;
1474 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1475 strcspn(xml,XMLWhitespace);
1476 if (xml == (
char *) NULL)
1478 (void) ThrowMagickException(exception,GetMagickModule(),
1479 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1480 predefined_entities=(
char **) RelinquishMagickMemory(
1481 predefined_entities);
1482 return(MagickFalse);
1484 xml+=strspn(xml,XMLWhitespace
")");
1485 if (strncmp(xml,
"#FIXED",6) == 0)
1486 xml+=strspn(xml+6,XMLWhitespace)+6;
1489 xml+=strcspn(xml,XMLWhitespace
">")-1;
1495 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1496 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1500 (void) ThrowMagickException(exception,GetMagickModule(),
1501 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1502 predefined_entities=(
char **) RelinquishMagickMemory(
1503 predefined_entities);
1504 return(MagickFalse);
1506 if (root->attributes[i] == (
char **) NULL)
1512 root->attributes=(
char ***) AcquireQuantumMemory(2,
1513 sizeof(*root->attributes));
1515 root->attributes=(
char ***) ResizeQuantumMemory(
1516 root->attributes,(
size_t) (i+2),
1517 sizeof(*root->attributes));
1518 if (root->attributes == (
char ***) NULL)
1519 ThrowFatalException(ResourceLimitFatalError,
1520 "MemoryAllocationFailed");
1521 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1522 sizeof(**root->attributes));
1523 if (root->attributes[i] == (
char **) NULL)
1524 ThrowFatalException(ResourceLimitFatalError,
1525 "MemoryAllocationFailed");
1526 root->attributes[i][0]=ConstantString(t);
1527 root->attributes[i][1]=(
char *) NULL;
1528 root->attributes[i+1]=(
char **) NULL;
1530 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1531 root->attributes[i]=(
char **) ResizeQuantumMemory(
1532 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1533 if (root->attributes[i] == (
char **) NULL)
1534 ThrowFatalException(ResourceLimitFatalError,
1535 "MemoryAllocationFailed");
1536 root->attributes[i][j+3]=(
char *) NULL;
1537 root->attributes[i][j+2]=ConstantString(c);
1538 root->attributes[i][j+1]=(
char *) NULL;
1539 if (v != (
char *) NULL)
1540 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1541 root->attributes[i][j]=ConstantString(n);
1545 if (strncmp(xml,
"<!--", 4) == 0)
1546 xml=strstr(xml+4,
"-->");
1548 if (strncmp(xml,
"<?", 2) == 0)
1552 if (xml != (
char *) NULL)
1554 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1560 xml=strchr(xml,
'>');
1562 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1565 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1569static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
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;
1592static inline MagickBooleanType IsSkipTag(
const char *tag)
1598 while (ignore_tags[i] != (
const char *) NULL)
1600 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1604 return(MagickFalse);
1607MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1638 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1640 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1641 "ParseError",
"root tag missing");
1642 return((XMLTreeInfo *) NULL);
1644 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1646 utf8=ConvertUTF16ToUTF8(xml,&length);
1647 if (utf8 == (
char *) NULL)
1649 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1650 "ParseError",
"UTF16 to UTF8 failed");
1651 return((XMLTreeInfo *) NULL);
1655 utf8=DestroyString(utf8);
1656 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1657 "ParseError",
"root tag missing");
1658 return((XMLTreeInfo *) NULL);
1660 terminal=utf8[length-1];
1661 utf8[length-1]=
'\0';
1663 while ((*p !=
'\0') && (*p !=
'<'))
1667 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1668 "ParseError",
"root tag missing");
1669 utf8=DestroyString(utf8);
1670 return((XMLTreeInfo *) NULL);
1672 attribute=(
char **) NULL;
1677 attributes=(
char **) sentinel;
1680 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1681 (*p ==
':') || (c <
'\0'))
1686 if (root->node == (XMLTreeInfo *) NULL)
1688 (void) ThrowMagickException(exception,GetMagickModule(),
1689 OptionWarning,
"ParseError",
"root tag missing");
1690 utf8=DestroyString(utf8);
1691 return(&root->root);
1693 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1694 while (isspace((
int) ((
unsigned char) *p)) != 0)
1696 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1697 (ignore_depth == 0))
1699 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1705 while ((root->attributes[i] != (
char **) NULL) &&
1706 (strcmp(root->attributes[i][0],tag) != 0))
1708 attribute=root->attributes[i];
1710 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1716 attributes=(
char **) AcquireQuantumMemory(4,
1717 sizeof(*attributes));
1719 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1720 (l+4),
sizeof(*attributes));
1721 if (attributes == (
char **) NULL)
1723 (void) ThrowMagickException(exception,GetMagickModule(),
1724 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1725 utf8=DestroyString(utf8);
1726 return(&root->root);
1728 attributes[l+2]=(
char *) NULL;
1729 attributes[l+1]=(
char *) NULL;
1731 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1732 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1733 attributes[l]=ConstantString(
"");
1737 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1739 if ((c ==
'"') || (c ==
'\''))
1746 while ((*p !=
'\0') && (*p != c))
1752 attributes[l]=ConstantString(
"");
1753 attributes[l+1]=ConstantString(
"");
1754 (void) DestroyXMLTreeAttributes(attributes);
1755 (void) ThrowMagickException(exception,
1756 GetMagickModule(),OptionWarning,
"ParseError",
1758 utf8=DestroyString(utf8);
1759 return(&root->root);
1762 while ((attribute != (
char **) NULL) &&
1763 (attribute[j] != (
char *) NULL) &&
1764 (strcmp(attribute[j],attributes[l]) != 0))
1766 attributes[l+1]=ParseEntities(attributes[l+1],
1767 root->entities,(attribute != (
char **) NULL) &&
1768 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
1771 attributes[l]=ConstantString(attributes[l]);
1773 while (isspace((
int) ((
unsigned char) *p)) != 0)
1779 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1788 if (((*p !=
'\0') && (*p !=
'>')) ||
1789 ((*p ==
'\0') && (terminal !=
'>')))
1792 (void) DestroyXMLTreeAttributes(attributes);
1793 (void) ThrowMagickException(exception,GetMagickModule(),
1794 OptionWarning,
"ParseError",
"missing >");
1795 utf8=DestroyString(utf8);
1796 return(&root->root);
1798 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
1799 (void) DestroyXMLTreeAttributes(attributes);
1802 ParseOpenTag(root,tag,attributes);
1803 (void) ParseCloseTag(root,tag,exception);
1809 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
1812 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
1813 ParseOpenTag(root,tag,attributes);
1817 (void) DestroyXMLTreeAttributes(attributes);
1824 (void) DestroyXMLTreeAttributes(attributes);
1825 (void) ThrowMagickException(exception,GetMagickModule(),
1826 OptionWarning,
"ParseError",
"missing >");
1827 utf8=DestroyString(utf8);
1828 return(&root->root);
1839 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
1841 if ((c ==
'\0') && (terminal !=
'>'))
1843 (void) ThrowMagickException(exception,GetMagickModule(),
1844 OptionWarning,
"ParseError",
"missing >");
1845 utf8=DestroyString(utf8);
1846 return(&root->root);
1849 if ((ignore_depth == 0) &&
1850 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
1852 utf8=DestroyString(utf8);
1853 return(&root->root);
1855 if (ignore_depth > 0)
1858 if (isspace((
int) ((
unsigned char) *p)) != 0)
1859 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
1862 if (strncmp(p,
"!--",3) == 0)
1868 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
1869 ((*p ==
'\0') && (terminal !=
'>')))
1871 (void) ThrowMagickException(exception,GetMagickModule(),
1872 OptionWarning,
"ParseError",
"unclosed <!--");
1873 utf8=DestroyString(utf8);
1874 return(&root->root);
1878 if (strncmp(p,
"![CDATA[",8) == 0)
1884 if (p != (
char *) NULL)
1887 if (ignore_depth == 0)
1888 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
1892 (void) ThrowMagickException(exception,GetMagickModule(),
1893 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
1894 utf8=DestroyString(utf8);
1895 return(&root->root);
1899 if (strncmp(p,
"!DOCTYPE",8) == 0)
1904 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
1905 ((l != 0) && ((*p !=
']') ||
1906 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
1907 l=(ssize_t) ((*p ==
'[') ? 1 : l))
1908 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
1909 if ((*p ==
'\0') && (terminal !=
'>'))
1911 (void) ThrowMagickException(exception,GetMagickModule(),
1912 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
1913 utf8=DestroyString(utf8);
1914 return(&root->root);
1917 tag=strchr(tag,
'[')+1;
1920 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
1922 if (status == MagickFalse)
1924 utf8=DestroyString(utf8);
1925 return(&root->root);
1939 if (p == (
char *) NULL)
1942 }
while ((*p !=
'\0') && (*p !=
'>'));
1943 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
1946 (void) ThrowMagickException(exception,GetMagickModule(),
1947 OptionWarning,
"ParseError",
"unclosed <?");
1948 utf8=DestroyString(utf8);
1949 return(&root->root);
1951 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
1955 (void) ThrowMagickException(exception,GetMagickModule(),
1956 OptionWarning,
"ParseError",
"unexpected <");
1957 utf8=DestroyString(utf8);
1958 return(&root->root);
1960 if ((p == (
char *) NULL) || (*p ==
'\0'))
1964 if ((*p !=
'\0') && (*p !=
'<'))
1969 while ((*p !=
'\0') && (*p !=
'<'))
1973 if (ignore_depth == 0)
1974 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
1980 utf8=DestroyString(utf8);
1981 if (root->node == (XMLTreeInfo *) NULL)
1982 return(&root->root);
1983 if (root->node->tag == (
char *) NULL)
1985 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1986 "ParseError",
"root tag missing");
1987 return(&root->root);
1989 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1990 "ParseError",
"unclosed tag: '%s'",root->node->tag);
1991 return(&root->root);
2016MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2019 *predefined_entities[NumberPredefinedEntities+1] =
2021 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2022 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2028 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2029 (void) memset(root,0,
sizeof(*root));
2030 root->root.tag=(
char *) NULL;
2031 if (tag != (
char *) NULL)
2032 root->root.tag=ConstantString(tag);
2033 root->node=(&root->root);
2034 root->root.content=ConstantString(
"");
2035 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2036 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2037 root->root.attributes=sentinel;
2038 root->attributes=(
char ***) root->root.attributes;
2039 root->processing_instructions=(
char ***) root->root.attributes;
2040 root->debug=IsEventLogging();
2041 root->signature=MagickCoreSignature;
2042 return(&root->root);
2071MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2072 const char *content)
2074 assert(xml_info != (XMLTreeInfo *) NULL);
2075 assert((xml_info->signature == MagickCoreSignature) ||
2076 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2077 if (IsEventLogging() != MagickFalse)
2078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2079 if (xml_info->content != (
char *) NULL)
2080 xml_info->content=DestroyString(xml_info->content);
2081 xml_info->content=(
char *) ConstantString(content);
2108static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2109 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2115 canonical_content=CanonicalXMLContent(source,pedantic);
2121 content=AcquireString(source);
2122 content[offset]=
'\0';
2123 canonical_content=CanonicalXMLContent(content,pedantic);
2124 content=DestroyString(content);
2126 if (canonical_content == (
char *) NULL)
2127 return(*destination);
2128 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2130 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2131 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2132 sizeof(**destination));
2133 if (*destination == (
char *) NULL)
2134 return(*destination);
2136 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2138 canonical_content=DestroyString(canonical_content);
2139 return(*destination);
2142static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2143 size_t *extent,
size_t start,
char ***attributes)
2158 content=(
char *)
"";
2159 if (xml_info->parent != (XMLTreeInfo *) NULL)
2160 content=xml_info->parent->content;
2162 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2163 start),source,length,extent,MagickFalse);
2164 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2166 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2167 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2168 if (*source == (
char *) NULL)
2171 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2172 "<%s",xml_info->tag);
2173 for (i=0; xml_info->attributes[i]; i+=2)
2175 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2176 if (attribute != xml_info->attributes[i+1])
2178 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2180 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2181 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2182 if (*source == (
char *) NULL)
2183 return((
char *) NULL);
2185 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2186 xml_info->attributes[i]);
2187 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2189 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2192 while ((attributes[i] != (
char **) NULL) &&
2193 (strcmp(attributes[i][0],xml_info->tag) != 0))
2196 while ((attributes[i] != (
char **) NULL) &&
2197 (attributes[i][j] != (
char *) NULL))
2199 if ((attributes[i][j+1] == (
char *) NULL) ||
2200 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2205 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2207 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2208 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2209 if (*source == (
char *) NULL)
2210 return((
char *) NULL);
2212 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2214 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2216 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2219 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2220 *xml_info->content ?
">" :
"/>");
2221 if (xml_info->child != (XMLTreeInfo *) NULL)
2222 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2224 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2226 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2228 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2229 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2230 if (*source == (
char *) NULL)
2231 return((
char *) NULL);
2233 if (*xml_info->content !=
'\0')
2234 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2236 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2238 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2239 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2242 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2247MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2270 assert(xml_info != (XMLTreeInfo *) NULL);
2271 assert((xml_info->signature == MagickCoreSignature) ||
2272 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2273 if (IsEventLogging() != MagickFalse)
2274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2275 if (xml_info->tag == (
char *) NULL)
2276 return((
char *) NULL);
2277 xml=AcquireString((
char *) NULL);
2279 extent=MagickPathExtent;
2280 root=(XMLTreeRoot *) xml_info;
2281 while (root->root.parent != (XMLTreeInfo *) NULL)
2282 root=(XMLTreeRoot *) root->root.parent;
2283 parent=xml_info->parent;
2284 if (parent == (XMLTreeInfo *) NULL)
2285 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2290 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2291 p=root->processing_instructions[i][1];
2292 for (j=1; p != (
char *) NULL; j++)
2294 if (root->processing_instructions[i][k][j-1] ==
'>')
2296 p=root->processing_instructions[i][j];
2299 q=root->processing_instructions[i][0];
2300 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2302 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2303 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2304 if (xml == (
char *) NULL)
2307 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2308 *p !=
'\0' ?
" " :
"",p);
2309 p=root->processing_instructions[i][j];
2312 ordered=xml_info->ordered;
2313 xml_info->parent=(XMLTreeInfo *) NULL;
2314 xml_info->ordered=(XMLTreeInfo *) NULL;
2315 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2316 xml_info->parent=parent;
2317 xml_info->ordered=ordered;
2318 if (parent == (XMLTreeInfo *) NULL)
2319 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2324 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2325 p=root->processing_instructions[i][1];
2326 for (j=1; p != (
char *) NULL; j++)
2328 if (root->processing_instructions[i][k][j-1] ==
'<')
2330 p=root->processing_instructions[i][j];
2333 q=root->processing_instructions[i][0];
2334 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2336 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2337 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2338 if (xml == (
char *) NULL)
2341 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2342 *p !=
'\0' ?
" " :
"",p);
2343 p=root->processing_instructions[i][j];
2346 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));