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 };
168 const char *tag,
const size_t offset)
175 child=(
XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
178 (void) memset(child,0,
sizeof(*child));
179 child->tag=ConstantString(tag);
180 child->attributes=sentinel;
181 child->content=ConstantString(
"");
182 child->debug=IsEventLogging();
183 child->signature=MagickCoreSignature;
184 return(InsertTagIntoXMLTree(xml_info,child,offset));
216 const char *path,
const size_t offset)
220 subnode[MagickPathExtent],
221 tag[MagickPathExtent];
235 assert((xml_info->signature == MagickCoreSignature) ||
236 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
237 if (IsEventLogging() != MagickFalse)
238 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
240 components=GetPathComponents(path,&number_components);
241 if (components == (
char **) NULL)
243 for (i=0; i < (ssize_t) number_components; i++)
245 GetPathComponent(components[i],SubimagePath,subnode);
246 GetPathComponent(components[i],CanonicalPath,tag);
247 child=GetXMLTreeChild(node,tag);
249 child=AddChildToXMLTree(node,tag,offset);
253 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
255 node=GetXMLTreeOrdered(node);
261 components[i]=DestroyString(components[i]);
263 for ( ; i < (ssize_t) number_components; i++)
264 components[i]=DestroyString(components[i]);
265 components=(
char **) RelinquishMagickMemory(components);
297MagickPrivate
char *CanonicalXMLContent(
const char *content,
298 const MagickBooleanType pedantic)
313 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
314 if (utf8 == (
unsigned char *) NULL)
315 return((
char *) NULL);
316 for (p=utf8; *p !=
'\0'; p++)
317 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
324 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
325 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
326 if (base64 == (
char *) NULL)
327 return((
char *) NULL);
328 canonical_content=AcquireString(
"<base64>");
329 (void) ConcatenateString(&canonical_content,base64);
330 base64=DestroyString(base64);
331 (void) ConcatenateString(&canonical_content,
"</base64>");
332 return(canonical_content);
334 canonical_content=SubstituteXMLEntities((
const char *) utf8,pedantic);
335 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
336 return(canonical_content);
365static char **DestroyXMLTreeAttributes(
char **attributes)
373 if ((attributes == (
char **) NULL) || (attributes == sentinel))
374 return((
char **) NULL);
375 for (i=0; attributes[i] != (
char *) NULL; i+=2)
380 if (attributes[i] != (
char *) NULL)
381 attributes[i]=DestroyString(attributes[i]);
382 if (attributes[i+1] != (
char *) NULL)
383 attributes[i+1]=DestroyString(attributes[i+1]);
385 attributes=(
char **) RelinquishMagickMemory(attributes);
386 return((
char **) NULL);
389static void DestroyXMLTreeChild(
XMLTreeInfo *xml_info,
396 child=xml_info->child;
402 (void) DestroyXMLTree_(node,depth+1);
406static void DestroyXMLTreeOrdered(
XMLTreeInfo *xml_info,
413 ordered=xml_info->ordered;
417 ordered=node->ordered;
419 (void) DestroyXMLTree_(node,depth+1);
423static void DestroyXMLTreeRoot(
XMLTreeInfo *xml_info)
436 assert((xml_info->signature == MagickCoreSignature) ||
437 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
438 if (IsEventLogging() != MagickFalse)
439 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
446 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
447 root->entities[i+1]=DestroyString(root->entities[i+1]);
448 root->entities=(
char **) RelinquishMagickMemory(root->entities);
449 for (i=0; root->attributes[i] != (
char **) NULL; i++)
451 attributes=root->attributes[i];
452 if (attributes[0] != (
char *) NULL)
453 attributes[0]=DestroyString(attributes[0]);
454 for (j=1; attributes[j] != (
char *) NULL; j+=3)
456 if (attributes[j] != (
char *) NULL)
457 attributes[j]=DestroyString(attributes[j]);
458 if (attributes[j+1] != (
char *) NULL)
459 attributes[j+1]=DestroyString(attributes[j+1]);
460 if (attributes[j+2] != (
char *) NULL)
461 attributes[j+2]=DestroyString(attributes[j+2]);
463 attributes=(
char **) RelinquishMagickMemory(attributes);
465 if (root->attributes[0] != (
char **) NULL)
466 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
467 if (root->processing_instructions[0] != (
char **) NULL)
469 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
471 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
472 root->processing_instructions[i][j]=DestroyString(
473 root->processing_instructions[i][j]);
474 root->processing_instructions[i][j+1]=DestroyString(
475 root->processing_instructions[i][j+1]);
476 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
477 root->processing_instructions[i]);
479 root->processing_instructions=(
char ***) RelinquishMagickMemory(
480 root->processing_instructions);
488 assert((xml_info->signature == MagickCoreSignature) ||
489 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
490 if (IsEventLogging() != MagickFalse)
491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
492 if (depth > MagickMaxRecursionDepth)
493 ThrowFatalException(ResourceLimitFatalError,
494 "MemoryAllocationFailed");
495 DestroyXMLTreeChild(xml_info,depth+1);
496 DestroyXMLTreeOrdered(xml_info,depth+1);
497 DestroyXMLTreeRoot(xml_info);
498 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
499 xml_info->content=DestroyString(xml_info->content);
500 xml_info->tag=DestroyString(xml_info->tag);
501 xml_info=(
XMLTreeInfo *) RelinquishMagickMemory(xml_info);
507 return(DestroyXMLTree_(xml_info,0));
534MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
555 assert(filename != (
const char *) NULL);
558 if (LocaleCompare(filename,
"-") != 0)
559 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
561 return((
char *) NULL);
562 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
564 if ((file == fileno(stdin)) || (offset < 0) ||
565 (offset != (MagickOffsetType) ((ssize_t) offset)))
576 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
577 quantum=(size_t) MagickMaxBufferExtent;
578 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
579 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
580 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
581 for (i=0; xml != (
char *) NULL; i+=(size_t) count)
583 count=read(file,xml+i,quantum);
590 if (~((
size_t) i) < (quantum+1))
592 xml=(
char *) RelinquishMagickMemory(xml);
595 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
596 if ((i+(
size_t) count) >= extent)
599 if (LocaleCompare(filename,
"-") != 0)
600 file=close_utf8(file);
601 if (xml == (
char *) NULL)
602 return((
char *) NULL);
605 xml=(
char *) RelinquishMagickMemory(xml);
606 return((
char *) NULL);
608 length=MagickMin(i+(
size_t) count,extent);
612 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
614 if (~length >= (MagickPathExtent-1))
615 xml=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*xml));
616 if (xml == (
char *) NULL)
618 file=close_utf8(file);
619 return((
char *) NULL);
621 map=MapBlob(file,ReadMode,0,length);
622 if (map != (
char *) NULL)
624 (void) memcpy(xml,map,length);
625 (void) UnmapBlob(map,length);
629 (void) lseek(file,0,SEEK_SET);
630 for (i=0; i < length; i+=(size_t) count)
632 count=read(file,xml+i,(
size_t) MagickMin(length-i,(
size_t)
633 MagickMaxBufferExtent));
643 file=close_utf8(file)-1;
644 xml=(
char *) RelinquishMagickMemory(xml);
645 return((
char *) NULL);
649 if (LocaleCompare(filename,
"-") != 0)
650 file=close_utf8(file);
652 xml=(
char *) RelinquishMagickMemory(xml);
681 assert((xml_info->signature == MagickCoreSignature) ||
682 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
683 if (IsEventLogging() != MagickFalse)
684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
685 return(xml_info->next);
713MagickExport
const char *GetXMLTreeAttribute(
XMLTreeInfo *xml_info,
724 assert((xml_info->signature == MagickCoreSignature) ||
725 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
726 if (IsEventLogging() != MagickFalse)
727 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
728 if (xml_info->attributes == (
char **) NULL)
729 return((
const char *) NULL);
731 while ((xml_info->attributes[i] != (
char *) NULL) &&
732 (strcmp(xml_info->attributes[i],tag) != 0))
734 if (xml_info->attributes[i] != (
char *) NULL)
735 return(xml_info->attributes[i+1]);
740 while ((root->attributes[i] != (
char **) NULL) &&
741 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
743 if (root->attributes[i] == (
char **) NULL)
744 return((
const char *) NULL);
746 while ((root->attributes[i][j] != (
char *) NULL) &&
747 (strcmp(root->attributes[i][j],tag) != 0))
749 if (root->attributes[i][j] == (
char *) NULL)
750 return((
const char *) NULL);
751 return(root->attributes[i][j+1]);
780MagickPrivate MagickBooleanType GetXMLTreeAttributes(
787 assert((xml_info->signature == MagickCoreSignature) ||
788 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
790 if (IsEventLogging() != MagickFalse)
791 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
792 if (xml_info->attributes == (
char **) NULL)
795 while (xml_info->attributes[i] != (
char *) NULL)
797 (void) AddValueToSplayTree(attributes,
798 ConstantString(xml_info->attributes[i]),
799 ConstantString(xml_info->attributes[i+1]));
834 assert((xml_info->signature == MagickCoreSignature) ||
835 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
836 if (IsEventLogging() != MagickFalse)
837 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
838 child=xml_info->child;
839 if (tag != (
const char *) NULL)
840 while ((child != (
XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
841 child=child->sibling;
868MagickExport
const char *GetXMLTreeContent(
XMLTreeInfo *xml_info)
871 assert((xml_info->signature == MagickCoreSignature) ||
872 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
873 if (IsEventLogging() != MagickFalse)
874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
875 return(xml_info->content);
903 assert((xml_info->signature == MagickCoreSignature) ||
904 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
905 if (IsEventLogging() != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
907 return(xml_info->ordered);
940 subnode[MagickPathExtent],
941 tag[MagickPathExtent];
954 assert((xml_info->signature == MagickCoreSignature) ||
955 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
956 if (IsEventLogging() != MagickFalse)
957 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
959 components=GetPathComponents(path,&number_components);
960 if (components == (
char **) NULL)
962 for (i=0; i < (ssize_t) number_components; i++)
964 GetPathComponent(components[i],SubimagePath,subnode);
965 GetPathComponent(components[i],CanonicalPath,tag);
966 node=GetXMLTreeChild(node,tag);
969 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
971 node=GetXMLTreeOrdered(node);
977 components[i]=DestroyString(components[i]);
979 for ( ; i < (ssize_t) number_components; i++)
980 components[i]=DestroyString(components[i]);
981 components=(
char **) RelinquishMagickMemory(components);
1009MagickPrivate
const char **GetXMLTreeProcessingInstructions(
1019 assert((xml_info->signature == MagickCoreSignature) ||
1020 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1021 if (IsEventLogging() != MagickFalse)
1022 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1027 while ((root->processing_instructions[i] != (
char **) NULL) &&
1028 (strcmp(root->processing_instructions[i][0],target) != 0))
1030 if (root->processing_instructions[i] == (
char **) NULL)
1031 return((
const char **) sentinel);
1032 return((
const char **) (root->processing_instructions[i]+1));
1060 assert((xml_info->signature == MagickCoreSignature) ||
1061 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1062 if (IsEventLogging() != MagickFalse)
1063 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1064 return(xml_info->sibling);
1089MagickExport
const char *GetXMLTreeTag(
XMLTreeInfo *xml_info)
1092 assert((xml_info->signature == MagickCoreSignature) ||
1093 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1094 if (IsEventLogging() != MagickFalse)
1095 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1096 return(xml_info->tag);
1138 child->offset=offset;
1139 child->parent=xml_info;
1142 xml_info->child=child;
1145 head=xml_info->child;
1146 if (head->offset > offset)
1148 child->ordered=head;
1149 xml_info->child=child;
1155 (node->ordered->offset <= offset))
1157 child->ordered=node->ordered;
1158 node->ordered=child;
1162 while ((node != (
XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1167 if ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1170 (node->next->offset <= offset))
1172 child->next=node->next;
1178 previous->sibling=node->sibling;
1182 while ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1187 child->sibling=node;
1189 previous->sibling=child;
1220static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1238 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1239 if (utf8 == (
char *) NULL)
1240 return((
char *) NULL);
1241 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1247 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1253 for (i=2; i < (ssize_t) (*length-1); i+=2)
1255 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1256 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1257 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1259 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1260 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1261 (content[i] & 0xff);
1262 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
1264 if ((
size_t) (j+MagickPathExtent) > extent)
1266 extent=(size_t) j+MagickPathExtent;
1267 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1268 if (utf8 == (
char *) NULL)
1281 for (bits=0;
byte != 0;
byte/=2)
1284 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1288 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1293 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1294 if (utf8 != (
char *) NULL)
1299static char *ParseEntities(
char *xml,
char **entities,
int state)
1323 for ( ; *xml !=
'\0'; xml++)
1324 while (*xml ==
'\r')
1328 (void) memmove(xml,xml+1,strlen(xml));
1332 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1333 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1345 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1351 c=strtol(xml+2,&entity,10);
1353 c=strtol(xml+3,&entity,16);
1354 if ((c == 0) || (*entity !=
';'))
1370 for (i=0;
byte != 0;
byte/=2)
1373 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1378 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1382 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1385 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1386 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1392 while ((entities[i] != (
char *) NULL) &&
1393 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1395 if (entities[i++] == (
char *) NULL)
1398 if (entities[i] != (
char *) NULL)
1403 length=strlen(entities[i]);
1404 entity=strchr(xml,
';');
1405 if ((entity != (
char *) NULL) &&
1406 ((length-1L) >= (
size_t) (entity-xml)))
1408 offset=(ssize_t) (xml-p);
1409 extent=((size_t) offset+length+strlen(entity));
1412 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1420 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1421 sizeof(*extent_xml));
1422 if (extent_xml != (
char *) NULL)
1424 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1425 (void) CopyMagickString(extent_xml,p,extent*
1426 sizeof(*extent_xml));
1430 if (p == (
char *) NULL)
1431 ThrowFatalException(ResourceLimitFatalError,
1432 "MemoryAllocationFailed");
1434 entity=strchr(xml,
';');
1436 if (entity != (
char *) NULL)
1437 (void) memmove(xml+length,entity+1,strlen(entity));
1438 (void) memcpy(xml,entities[i],length);
1442 if (((state ==
' ') || (state ==
'*')) &&
1443 (isspace((
int) ((
unsigned char) *xml)) != 0))
1453 for (xml=p; *xml !=
'\0'; xml++)
1458 i=(ssize_t) strspn(xml,accept);
1460 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1461 while ((*xml !=
'\0') && (*xml !=
' '))
1467 if ((xml >= p) && (*xml ==
' '))
1470 return(p == q ? ConstantString(p) : p);
1473static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1474 const size_t length,
const char state)
1479 xml_info=root->node;
1480 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1484 xml=ParseEntities(xml,root->entities,state);
1485 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1487 (void) ConcatenateString(&xml_info->content,xml);
1488 xml=DestroyString(xml);
1492 if (xml_info->content != (
char *) NULL)
1493 xml_info->content=DestroyString(xml_info->content);
1494 xml_info->content=xml;
1502 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1504 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1505 "ParseError",
"unexpected closing tag </%s>",tag);
1506 return(&root->root);
1508 root->node=root->node->parent;
1512static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1513 const size_t depth,
char **entities)
1521 if (depth > MagickMaxRecursionDepth)
1522 return(MagickFalse);
1525 while ((*xml !=
'\0') && (*xml !=
'&'))
1529 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1530 return(MagickFalse);
1532 while ((entities[i] != (
char *) NULL) &&
1533 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1535 if ((entities[i] != (
char *) NULL) &&
1536 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1537 return(MagickFalse);
1541static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1553 xml+=strcspn(xml,XMLWhitespace);
1557 xml+=strspn(xml+1,XMLWhitespace)+1;
1559 if (strcmp(target,
"xml") == 0)
1561 xml=strstr(xml,
"standalone");
1562 if ((xml != (
char *) NULL) &&
1563 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1564 root->standalone=MagickTrue;
1567 if (root->processing_instructions[0] == (
char **) NULL)
1569 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1570 *root->processing_instructions));
1571 *root->processing_instructions=(
char **) NULL;
1574 while ((root->processing_instructions[i] != (
char **) NULL) &&
1575 (strcmp(target,root->processing_instructions[i][0]) != 0))
1577 if (root->processing_instructions[i] == (
char **) NULL)
1579 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1580 root->processing_instructions,(
size_t) (i+2),
1581 sizeof(*root->processing_instructions));
1582 if (root->processing_instructions == (
char ***) NULL)
1583 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1584 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1585 sizeof(**root->processing_instructions));
1586 if (root->processing_instructions[i] == (
char **) NULL)
1587 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1588 root->processing_instructions[i+1]=(
char **) NULL;
1589 root->processing_instructions[i][0]=ConstantString(target);
1590 root->processing_instructions[i][1]=(
char *)
1591 root->processing_instructions[i+1];
1592 root->processing_instructions[i+1]=(
char **) NULL;
1593 root->processing_instructions[i][2]=ConstantString(
"");
1596 while (root->processing_instructions[i][j] != (
char *) NULL)
1598 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1599 root->processing_instructions[i],(
size_t) (j+3),
1600 sizeof(**root->processing_instructions));
1601 if (root->processing_instructions[i] == (
char **) NULL)
1602 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1603 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1604 root->processing_instructions[i][j+1],(
size_t) (j+1),
1605 sizeof(***root->processing_instructions));
1606 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1607 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1608 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1609 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1610 root->processing_instructions[i][j]=ConstantString(xml);
1611 root->processing_instructions[i][j+1]=(
char *) NULL;
1614static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1621 **predefined_entities,
1631 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1632 if (predefined_entities == (
char **) NULL)
1633 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1634 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1635 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1637 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1641 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1646 if (strspn(xml+8,XMLWhitespace) == 0)
1648 xml+=strspn(xml+8,XMLWhitespace)+8;
1650 n=xml+strspn(xml,XMLWhitespace
"%");
1651 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1653 xml=n+strcspn(n,XMLWhitespace);
1657 v=xml+strspn(xml+1,XMLWhitespace)+1;
1660 if ((q !=
'"') && (q !=
'\''))
1665 xml=strchr(xml,
'>');
1668 entities=(*c ==
'%') ? predefined_entities : root->entities;
1669 for (i=0; entities[i] != (
char *) NULL; i++) ;
1670 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1672 if (entities == (
char **) NULL)
1673 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1675 predefined_entities=entities;
1677 root->entities=entities;
1681 if (xml != (
char *) NULL)
1686 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1687 entities[i+2]=(
char *) NULL;
1688 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1692 if (entities[i+1] != v)
1693 entities[i+1]=DestroyString(entities[i+1]);
1694 (void) ThrowMagickException(exception,GetMagickModule(),
1695 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1696 predefined_entities=(
char **) RelinquishMagickMemory(
1697 predefined_entities);
1698 return(MagickFalse);
1702 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1707 t=xml+strspn(xml+9,XMLWhitespace)+9;
1710 (void) ThrowMagickException(exception,GetMagickModule(),
1711 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1712 predefined_entities=(
char **) RelinquishMagickMemory(
1713 predefined_entities);
1714 return(MagickFalse);
1716 xml=t+strcspn(t,XMLWhitespace
">");
1721 while ((root->attributes[i] != (
char **) NULL) &&
1722 (n != (
char *) NULL) &&
1723 (strcmp(n,root->attributes[i][0]) != 0))
1725 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1728 xml=n+strcspn(n,XMLWhitespace);
1733 (void) ThrowMagickException(exception,GetMagickModule(),
1734 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1735 predefined_entities=(
char **) RelinquishMagickMemory(
1736 predefined_entities);
1737 return(MagickFalse);
1739 xml+=strspn(xml+1,XMLWhitespace)+1;
1740 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1741 if (strncmp(xml,
"NOTATION",8) == 0)
1742 xml+=strspn(xml+8,XMLWhitespace)+8;
1743 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1744 strcspn(xml,XMLWhitespace);
1745 if (xml == (
char *) NULL)
1747 (void) ThrowMagickException(exception,GetMagickModule(),
1748 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1749 predefined_entities=(
char **) RelinquishMagickMemory(
1750 predefined_entities);
1751 return(MagickFalse);
1753 xml+=strspn(xml,XMLWhitespace
")");
1754 if (strncmp(xml,
"#FIXED",6) == 0)
1755 xml+=strspn(xml+6,XMLWhitespace)+6;
1758 xml+=strcspn(xml,XMLWhitespace
">")-1;
1764 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1765 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1769 (void) ThrowMagickException(exception,GetMagickModule(),
1770 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1771 predefined_entities=(
char **) RelinquishMagickMemory(
1772 predefined_entities);
1773 return(MagickFalse);
1775 if (root->attributes[i] == (
char **) NULL)
1781 root->attributes=(
char ***) AcquireQuantumMemory(2,
1782 sizeof(*root->attributes));
1784 root->attributes=(
char ***) ResizeQuantumMemory(
1785 root->attributes,(
size_t) (i+2),
1786 sizeof(*root->attributes));
1787 if (root->attributes == (
char ***) NULL)
1788 ThrowFatalException(ResourceLimitFatalError,
1789 "MemoryAllocationFailed");
1790 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1791 sizeof(**root->attributes));
1792 if (root->attributes[i] == (
char **) NULL)
1793 ThrowFatalException(ResourceLimitFatalError,
1794 "MemoryAllocationFailed");
1795 root->attributes[i][0]=ConstantString(t);
1796 root->attributes[i][1]=(
char *) NULL;
1797 root->attributes[i+1]=(
char **) NULL;
1799 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1800 root->attributes[i]=(
char **) ResizeQuantumMemory(
1801 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1802 if (root->attributes[i] == (
char **) NULL)
1803 ThrowFatalException(ResourceLimitFatalError,
1804 "MemoryAllocationFailed");
1805 root->attributes[i][j+3]=(
char *) NULL;
1806 root->attributes[i][j+2]=ConstantString(c);
1807 root->attributes[i][j+1]=(
char *) NULL;
1808 if (v != (
char *) NULL)
1809 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1810 root->attributes[i][j]=ConstantString(n);
1814 if (strncmp(xml,
"<!--", 4) == 0)
1815 xml=strstr(xml+4,
"-->");
1817 if (strncmp(xml,
"<?", 2) == 0)
1821 if (xml != (
char *) NULL)
1823 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1829 xml=strchr(xml,
'>');
1831 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1834 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1838static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1843 xml_info=root->node;
1844 if (xml_info->tag == (
char *) NULL)
1845 xml_info->tag=ConstantString(tag);
1847 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1849 xml_info->attributes=attributes;
1850 root->node=xml_info;
1861static inline MagickBooleanType IsSkipTag(
const char *tag)
1867 while (ignore_tags[i] != (
const char *) NULL)
1869 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1873 return(MagickFalse);
1907 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1909 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1910 "ParseError",
"root tag missing");
1913 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1915 utf8=ConvertUTF16ToUTF8(xml,&length);
1916 if (utf8 == (
char *) NULL)
1918 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1919 "ParseError",
"UTF16 to UTF8 failed");
1922 terminal=utf8[MagickMax(length-1,0)];
1923 utf8[MagickMax(length-1,0)]=
'\0';
1925 while ((*p !=
'\0') && (*p !=
'<'))
1929 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1930 "ParseError",
"root tag missing");
1931 utf8=DestroyString(utf8);
1934 attribute=(
char **) NULL;
1939 attributes=(
char **) sentinel;
1942 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1943 (*p ==
':') || (c <
'\0'))
1950 (void) ThrowMagickException(exception,GetMagickModule(),
1951 OptionWarning,
"ParseError",
"root tag missing");
1952 utf8=DestroyString(utf8);
1953 return(&root->root);
1955 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1956 while (isspace((
int) ((
unsigned char) *p)) != 0)
1958 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1959 (ignore_depth == 0))
1961 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1967 while ((root->attributes[i] != (
char **) NULL) &&
1968 (strcmp(root->attributes[i][0],tag) != 0))
1970 attribute=root->attributes[i];
1972 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1978 attributes=(
char **) AcquireQuantumMemory(4,
1979 sizeof(*attributes));
1981 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1982 (l+4),
sizeof(*attributes));
1983 if (attributes == (
char **) NULL)
1985 (void) ThrowMagickException(exception,GetMagickModule(),
1986 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1987 utf8=DestroyString(utf8);
1988 return(&root->root);
1990 attributes[l+2]=(
char *) NULL;
1991 attributes[l+1]=(
char *) NULL;
1993 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1994 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1995 attributes[l]=ConstantString(
"");
1999 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2001 if ((c ==
'"') || (c ==
'\''))
2008 while ((*p !=
'\0') && (*p != c))
2014 attributes[l]=ConstantString(
"");
2015 attributes[l+1]=ConstantString(
"");
2016 (void) DestroyXMLTreeAttributes(attributes);
2017 (void) ThrowMagickException(exception,
2018 GetMagickModule(),OptionWarning,
"ParseError",
2020 utf8=DestroyString(utf8);
2021 return(&root->root);
2024 while ((attribute != (
char **) NULL) &&
2025 (attribute[j] != (
char *) NULL) &&
2026 (strcmp(attribute[j],attributes[l]) != 0))
2028 attributes[l+1]=ParseEntities(attributes[l+1],
2029 root->entities,(attribute != (
char **) NULL) &&
2030 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2033 attributes[l]=ConstantString(attributes[l]);
2035 while (isspace((
int) ((
unsigned char) *p)) != 0)
2041 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2050 if (((*p !=
'\0') && (*p !=
'>')) ||
2051 ((*p ==
'\0') && (terminal !=
'>')))
2054 (void) DestroyXMLTreeAttributes(attributes);
2055 (void) ThrowMagickException(exception,GetMagickModule(),
2056 OptionWarning,
"ParseError",
"missing >");
2057 utf8=DestroyString(utf8);
2058 return(&root->root);
2060 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2061 (void) DestroyXMLTreeAttributes(attributes);
2064 ParseOpenTag(root,tag,attributes);
2065 (void) ParseCloseTag(root,tag,exception);
2071 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2074 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2075 ParseOpenTag(root,tag,attributes);
2079 (void) DestroyXMLTreeAttributes(attributes);
2086 (void) DestroyXMLTreeAttributes(attributes);
2087 (void) ThrowMagickException(exception,GetMagickModule(),
2088 OptionWarning,
"ParseError",
"missing >");
2089 utf8=DestroyString(utf8);
2090 return(&root->root);
2101 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2103 if ((c ==
'\0') && (terminal !=
'>'))
2105 (void) ThrowMagickException(exception,GetMagickModule(),
2106 OptionWarning,
"ParseError",
"missing >");
2107 utf8=DestroyString(utf8);
2108 return(&root->root);
2111 if ((ignore_depth == 0) &&
2112 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2114 utf8=DestroyString(utf8);
2115 return(&root->root);
2117 if (ignore_depth > 0)
2120 if (isspace((
int) ((
unsigned char) *p)) != 0)
2121 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2124 if (strncmp(p,
"!--",3) == 0)
2130 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2131 ((*p ==
'\0') && (terminal !=
'>')))
2133 (void) ThrowMagickException(exception,GetMagickModule(),
2134 OptionWarning,
"ParseError",
"unclosed <!--");
2135 utf8=DestroyString(utf8);
2136 return(&root->root);
2140 if (strncmp(p,
"![CDATA[",8) == 0)
2146 if (p != (
char *) NULL)
2149 if (ignore_depth == 0)
2150 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2154 (void) ThrowMagickException(exception,GetMagickModule(),
2155 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2156 utf8=DestroyString(utf8);
2157 return(&root->root);
2161 if (strncmp(p,
"!DOCTYPE",8) == 0)
2166 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2167 ((l != 0) && ((*p !=
']') ||
2168 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2169 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2170 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2171 if ((*p ==
'\0') && (terminal !=
'>'))
2173 (void) ThrowMagickException(exception,GetMagickModule(),
2174 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2175 utf8=DestroyString(utf8);
2176 return(&root->root);
2179 tag=strchr(tag,
'[')+1;
2182 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2184 if (status == MagickFalse)
2186 utf8=DestroyString(utf8);
2187 return(&root->root);
2201 if (p == (
char *) NULL)
2204 }
while ((*p !=
'\0') && (*p !=
'>'));
2205 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2208 (void) ThrowMagickException(exception,GetMagickModule(),
2209 OptionWarning,
"ParseError",
"unclosed <?");
2210 utf8=DestroyString(utf8);
2211 return(&root->root);
2213 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2217 (void) ThrowMagickException(exception,GetMagickModule(),
2218 OptionWarning,
"ParseError",
"unexpected <");
2219 utf8=DestroyString(utf8);
2220 return(&root->root);
2222 if ((p == (
char *) NULL) || (*p ==
'\0'))
2226 if ((*p !=
'\0') && (*p !=
'<'))
2231 while ((*p !=
'\0') && (*p !=
'<'))
2235 if (ignore_depth == 0)
2236 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2242 utf8=DestroyString(utf8);
2244 return(&root->root);
2245 if (root->node->tag == (
char *) NULL)
2247 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2248 "ParseError",
"root tag missing");
2249 return(&root->root);
2251 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2252 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2253 return(&root->root);
2278MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2281 *predefined_entities[NumberPredefinedEntities+1] =
2283 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2284 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2290 root=(
XMLTreeRoot *) AcquireMagickMemory(
sizeof(*root));
2293 (void) memset(root,0,
sizeof(*root));
2294 root->root.tag=(
char *) NULL;
2295 if (tag != (
char *) NULL)
2296 root->root.tag=ConstantString(tag);
2297 root->node=(&root->root);
2298 root->root.content=ConstantString(
"");
2299 root->entities=(
char **) AcquireMagickMemory(
sizeof(predefined_entities));
2300 if (root->entities == (
char **) NULL)
2302 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2303 root->root.attributes=sentinel;
2304 root->attributes=(
char ***) root->root.attributes;
2305 root->processing_instructions=(
char ***) root->root.attributes;
2306 root->debug=IsEventLogging();
2307 root->signature=MagickCoreSignature;
2308 return(&root->root);
2340 assert((xml_info->signature == MagickCoreSignature) ||
2341 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2342 if (IsEventLogging() != MagickFalse)
2343 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2345 xml_info->next->sibling=xml_info->sibling;
2348 node=xml_info->parent->child;
2349 if (node == xml_info)
2350 xml_info->parent->child=xml_info->ordered;
2353 while (node->ordered != xml_info)
2355 node->ordered=node->ordered->ordered;
2356 node=xml_info->parent->child;
2357 if (strcmp(node->tag,xml_info->tag) != 0)
2359 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2361 if (node->sibling != xml_info)
2364 node->sibling=(xml_info->next != (
XMLTreeInfo *) NULL) ?
2365 xml_info->next : node->sibling->sibling;
2368 (node->next != xml_info))
2371 node->next=node->next->next;
2409 const char *tag,
const char *value)
2416 assert((xml_info->signature == MagickCoreSignature) ||
2417 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2418 if (IsEventLogging() != MagickFalse)
2419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2421 while ((xml_info->attributes[i] != (
char *) NULL) &&
2422 (strcmp(xml_info->attributes[i],tag) != 0))
2424 if (xml_info->attributes[i] == (
char *) NULL)
2429 if (value == (
const char *) NULL)
2431 if (xml_info->attributes != sentinel)
2432 xml_info->attributes=(
char **) ResizeQuantumMemory(
2433 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2436 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2437 sizeof(*xml_info->attributes));
2438 if (xml_info->attributes != (
char **) NULL)
2439 xml_info->attributes[1]=ConstantString(
"");
2441 if (xml_info->attributes == (
char **) NULL)
2442 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2443 xml_info->attributes[i]=ConstantString(tag);
2444 xml_info->attributes[i+2]=(
char *) NULL;
2445 (void) strlen(xml_info->attributes[i+1]);
2450 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2451 if (xml_info->attributes[i+1] != (
char *) NULL)
2452 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2453 if (value != (
const char *) NULL)
2455 xml_info->attributes[i+1]=ConstantString(value);
2458 if (xml_info->attributes[i] != (
char *) NULL)
2459 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2460 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(
size_t)
2461 (j-i)*
sizeof(*xml_info->attributes));
2462 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2463 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2464 if (xml_info->attributes == (
char **) NULL)
2465 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2467 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2468 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2499 const char *content)
2502 assert((xml_info->signature == MagickCoreSignature) ||
2503 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2504 if (IsEventLogging() != MagickFalse)
2505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2506 if (xml_info->content != (
char *) NULL)
2507 xml_info->content=DestroyString(xml_info->content);
2508 xml_info->content=(
char *) ConstantString(content);
2535static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2536 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2542 canonical_content=CanonicalXMLContent(source,pedantic);
2548 content=AcquireString(source);
2549 content[offset]=
'\0';
2550 canonical_content=CanonicalXMLContent(content,pedantic);
2551 content=DestroyString(content);
2553 if (canonical_content == (
char *) NULL)
2554 return(*destination);
2555 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2557 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2558 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2559 sizeof(**destination));
2560 if (*destination == (
char *) NULL)
2561 return(*destination);
2563 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2565 canonical_content=DestroyString(canonical_content);
2566 return(*destination);
2569static char *XMLTreeTagToXML(
XMLTreeInfo *xml_info,
char **source,
size_t *length,
2570 size_t *extent,
size_t start,
char ***attributes)
2585 content=(
char *)
"";
2587 content=xml_info->parent->content;
2589 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2590 start),source,length,extent,MagickFalse);
2591 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2593 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2594 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2595 if (*source == (
char *) NULL)
2598 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2599 "<%s",xml_info->tag);
2600 for (i=0; xml_info->attributes[i]; i+=2)
2602 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2603 if (attribute != xml_info->attributes[i+1])
2605 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2607 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2608 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2609 if (*source == (
char *) NULL)
2610 return((
char *) NULL);
2612 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2613 xml_info->attributes[i]);
2614 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2616 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2619 while ((attributes[i] != (
char **) NULL) &&
2620 (strcmp(attributes[i][0],xml_info->tag) != 0))
2623 while ((attributes[i] != (
char **) NULL) &&
2624 (attributes[i][j] != (
char *) NULL))
2626 if ((attributes[i][j+1] == (
char *) NULL) ||
2627 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2632 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2634 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2635 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2636 if (*source == (
char *) NULL)
2637 return((
char *) NULL);
2639 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2641 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2643 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2646 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2647 *xml_info->content ?
">" :
"/>");
2649 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2651 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2653 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2655 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2656 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2657 if (*source == (
char *) NULL)
2658 return((
char *) NULL);
2660 if (*xml_info->content !=
'\0')
2661 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2663 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2666 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2669 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2674MagickExport
char *XMLTreeInfoToXML(
XMLTreeInfo *xml_info)
2698 assert((xml_info->signature == MagickCoreSignature) ||
2699 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2700 if (IsEventLogging() != MagickFalse)
2701 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2702 if (xml_info->tag == (
char *) NULL)
2703 return((
char *) NULL);
2704 xml=AcquireString((
char *) NULL);
2706 extent=MagickPathExtent;
2710 parent=xml_info->parent;
2712 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2717 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2718 p=root->processing_instructions[i][1];
2719 for (j=1; p != (
char *) NULL; j++)
2721 if (root->processing_instructions[i][k][j-1] ==
'>')
2723 p=root->processing_instructions[i][j];
2726 q=root->processing_instructions[i][0];
2727 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2729 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2730 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2731 if (xml == (
char *) NULL)
2734 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2735 *p !=
'\0' ?
" " :
"",p);
2736 p=root->processing_instructions[i][j];
2739 ordered=xml_info->ordered;
2742 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2743 xml_info->parent=parent;
2744 xml_info->ordered=ordered;
2746 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2751 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2752 p=root->processing_instructions[i][1];
2753 for (j=1; p != (
char *) NULL; j++)
2755 if (root->processing_instructions[i][k][j-1] ==
'<')
2757 p=root->processing_instructions[i][j];
2760 q=root->processing_instructions[i][0];
2761 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2763 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2764 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2765 if (xml == (
char *) NULL)
2768 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2769 *p !=
'\0' ?
" " :
"",p);
2770 p=root->processing_instructions[i][j];
2773 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));