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));
1413 if (p == (
char *) NULL)
1414 ThrowFatalException(ResourceLimitFatalError,
1415 "MemoryAllocationFailed");
1423 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1424 sizeof(*extent_xml));
1425 if (extent_xml != (
char *) NULL)
1427 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1428 (void) CopyMagickString(extent_xml,p,extent*
1429 sizeof(*extent_xml));
1433 if (p == (
char *) NULL)
1434 ThrowFatalException(ResourceLimitFatalError,
1435 "MemoryAllocationFailed");
1437 entity=strchr(xml,
';');
1439 if (entity != (
char *) NULL)
1440 (void) memmove(xml+length,entity+1,strlen(entity));
1441 (void) memcpy(xml,entities[i],length);
1445 if (((state ==
' ') || (state ==
'*')) &&
1446 (isspace((
int) ((
unsigned char) *xml)) != 0))
1456 for (xml=p; *xml !=
'\0'; xml++)
1461 i=(ssize_t) strspn(xml,accept);
1463 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1464 while ((*xml !=
'\0') && (*xml !=
' '))
1470 if ((xml >= p) && (*xml ==
' '))
1473 return(p == q ? ConstantString(p) : p);
1476static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1477 const size_t length,
const char state)
1482 xml_info=root->node;
1483 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1487 xml=ParseEntities(xml,root->entities,state);
1488 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1490 (void) ConcatenateString(&xml_info->content,xml);
1491 xml=DestroyString(xml);
1495 if (xml_info->content != (
char *) NULL)
1496 xml_info->content=DestroyString(xml_info->content);
1497 xml_info->content=xml;
1505 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1507 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1508 "ParseError",
"unexpected closing tag </%s>",tag);
1509 return(&root->root);
1511 root->node=root->node->parent;
1515static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1516 const size_t depth,
char **entities)
1524 if (depth > MagickMaxRecursionDepth)
1525 return(MagickFalse);
1528 while ((*xml !=
'\0') && (*xml !=
'&'))
1532 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1533 return(MagickFalse);
1535 while ((entities[i] != (
char *) NULL) &&
1536 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1538 if ((entities[i] != (
char *) NULL) &&
1539 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1540 return(MagickFalse);
1544static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1556 xml+=strcspn(xml,XMLWhitespace);
1560 xml+=strspn(xml+1,XMLWhitespace)+1;
1562 if (strcmp(target,
"xml") == 0)
1564 xml=strstr(xml,
"standalone");
1565 if ((xml != (
char *) NULL) &&
1566 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1567 root->standalone=MagickTrue;
1570 if (root->processing_instructions[0] == (
char **) NULL)
1572 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1573 *root->processing_instructions));
1574 *root->processing_instructions=(
char **) NULL;
1577 while ((root->processing_instructions[i] != (
char **) NULL) &&
1578 (strcmp(target,root->processing_instructions[i][0]) != 0))
1580 if (root->processing_instructions[i] == (
char **) NULL)
1582 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1583 root->processing_instructions,(
size_t) (i+2),
1584 sizeof(*root->processing_instructions));
1585 if (root->processing_instructions == (
char ***) NULL)
1586 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1587 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1588 sizeof(**root->processing_instructions));
1589 if (root->processing_instructions[i] == (
char **) NULL)
1590 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1591 root->processing_instructions[i+1]=(
char **) NULL;
1592 root->processing_instructions[i][0]=ConstantString(target);
1593 root->processing_instructions[i][1]=(
char *)
1594 root->processing_instructions[i+1];
1595 root->processing_instructions[i+1]=(
char **) NULL;
1596 root->processing_instructions[i][2]=ConstantString(
"");
1599 while (root->processing_instructions[i][j] != (
char *) NULL)
1601 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1602 root->processing_instructions[i],(
size_t) (j+3),
1603 sizeof(**root->processing_instructions));
1604 if (root->processing_instructions[i] == (
char **) NULL)
1605 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1606 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1607 root->processing_instructions[i][j+1],(
size_t) (j+1),
1608 sizeof(***root->processing_instructions));
1609 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1610 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1611 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1612 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1613 root->processing_instructions[i][j]=ConstantString(xml);
1614 root->processing_instructions[i][j+1]=(
char *) NULL;
1617static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1624 **predefined_entities,
1634 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1635 if (predefined_entities == (
char **) NULL)
1636 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1637 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1638 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1640 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1644 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1649 if (strspn(xml+8,XMLWhitespace) == 0)
1651 xml+=strspn(xml+8,XMLWhitespace)+8;
1653 n=xml+strspn(xml,XMLWhitespace
"%");
1654 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1656 xml=n+strcspn(n,XMLWhitespace);
1660 v=xml+strspn(xml+1,XMLWhitespace)+1;
1663 if ((q !=
'"') && (q !=
'\''))
1668 xml=strchr(xml,
'>');
1671 entities=(*c ==
'%') ? predefined_entities : root->entities;
1672 for (i=0; entities[i] != (
char *) NULL; i++) ;
1673 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1675 if (entities == (
char **) NULL)
1676 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1678 predefined_entities=entities;
1680 root->entities=entities;
1684 if (xml != (
char *) NULL)
1689 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1690 entities[i+2]=(
char *) NULL;
1691 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1695 if (entities[i+1] != v)
1696 entities[i+1]=DestroyString(entities[i+1]);
1697 (void) ThrowMagickException(exception,GetMagickModule(),
1698 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1699 predefined_entities=(
char **) RelinquishMagickMemory(
1700 predefined_entities);
1701 return(MagickFalse);
1705 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1710 t=xml+strspn(xml+9,XMLWhitespace)+9;
1713 (void) ThrowMagickException(exception,GetMagickModule(),
1714 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1715 predefined_entities=(
char **) RelinquishMagickMemory(
1716 predefined_entities);
1717 return(MagickFalse);
1719 xml=t+strcspn(t,XMLWhitespace
">");
1724 while ((root->attributes[i] != (
char **) NULL) &&
1725 (n != (
char *) NULL) &&
1726 (strcmp(n,root->attributes[i][0]) != 0))
1728 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1731 xml=n+strcspn(n,XMLWhitespace);
1736 (void) ThrowMagickException(exception,GetMagickModule(),
1737 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1738 predefined_entities=(
char **) RelinquishMagickMemory(
1739 predefined_entities);
1740 return(MagickFalse);
1742 xml+=strspn(xml+1,XMLWhitespace)+1;
1743 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1744 if (strncmp(xml,
"NOTATION",8) == 0)
1745 xml+=strspn(xml+8,XMLWhitespace)+8;
1746 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1747 strcspn(xml,XMLWhitespace);
1748 if (xml == (
char *) NULL)
1750 (void) ThrowMagickException(exception,GetMagickModule(),
1751 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1752 predefined_entities=(
char **) RelinquishMagickMemory(
1753 predefined_entities);
1754 return(MagickFalse);
1756 xml+=strspn(xml,XMLWhitespace
")");
1757 if (strncmp(xml,
"#FIXED",6) == 0)
1758 xml+=strspn(xml+6,XMLWhitespace)+6;
1761 xml+=strcspn(xml,XMLWhitespace
">")-1;
1767 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1768 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1772 (void) ThrowMagickException(exception,GetMagickModule(),
1773 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1774 predefined_entities=(
char **) RelinquishMagickMemory(
1775 predefined_entities);
1776 return(MagickFalse);
1778 if (root->attributes[i] == (
char **) NULL)
1784 root->attributes=(
char ***) AcquireQuantumMemory(2,
1785 sizeof(*root->attributes));
1787 root->attributes=(
char ***) ResizeQuantumMemory(
1788 root->attributes,(
size_t) (i+2),
1789 sizeof(*root->attributes));
1790 if (root->attributes == (
char ***) NULL)
1791 ThrowFatalException(ResourceLimitFatalError,
1792 "MemoryAllocationFailed");
1793 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1794 sizeof(**root->attributes));
1795 if (root->attributes[i] == (
char **) NULL)
1796 ThrowFatalException(ResourceLimitFatalError,
1797 "MemoryAllocationFailed");
1798 root->attributes[i][0]=ConstantString(t);
1799 root->attributes[i][1]=(
char *) NULL;
1800 root->attributes[i+1]=(
char **) NULL;
1802 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1803 root->attributes[i]=(
char **) ResizeQuantumMemory(
1804 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1805 if (root->attributes[i] == (
char **) NULL)
1806 ThrowFatalException(ResourceLimitFatalError,
1807 "MemoryAllocationFailed");
1808 root->attributes[i][j+3]=(
char *) NULL;
1809 root->attributes[i][j+2]=ConstantString(c);
1810 root->attributes[i][j+1]=(
char *) NULL;
1811 if (v != (
char *) NULL)
1812 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1813 root->attributes[i][j]=ConstantString(n);
1817 if (strncmp(xml,
"<!--", 4) == 0)
1818 xml=strstr(xml+4,
"-->");
1820 if (strncmp(xml,
"<?", 2) == 0)
1824 if (xml != (
char *) NULL)
1826 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1832 xml=strchr(xml,
'>');
1834 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1837 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1841static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1846 xml_info=root->node;
1847 if (xml_info->tag == (
char *) NULL)
1848 xml_info->tag=ConstantString(tag);
1850 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1852 xml_info->attributes=attributes;
1853 root->node=xml_info;
1864static inline MagickBooleanType IsSkipTag(
const char *tag)
1870 while (ignore_tags[i] != (
const char *) NULL)
1872 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1876 return(MagickFalse);
1910 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1912 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1913 "ParseError",
"root tag missing");
1916 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1918 utf8=ConvertUTF16ToUTF8(xml,&length);
1919 if (utf8 == (
char *) NULL)
1921 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1922 "ParseError",
"UTF16 to UTF8 failed");
1925 terminal=utf8[MagickMax(length-1,0)];
1926 utf8[MagickMax(length-1,0)]=
'\0';
1928 while ((*p !=
'\0') && (*p !=
'<'))
1932 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1933 "ParseError",
"root tag missing");
1934 utf8=DestroyString(utf8);
1937 attribute=(
char **) NULL;
1942 attributes=(
char **) sentinel;
1945 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1946 (*p ==
':') || (c <
'\0'))
1953 (void) ThrowMagickException(exception,GetMagickModule(),
1954 OptionWarning,
"ParseError",
"root tag missing");
1955 utf8=DestroyString(utf8);
1956 return(&root->root);
1958 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1959 while (isspace((
int) ((
unsigned char) *p)) != 0)
1961 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1962 (ignore_depth == 0))
1964 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1970 while ((root->attributes[i] != (
char **) NULL) &&
1971 (strcmp(root->attributes[i][0],tag) != 0))
1973 attribute=root->attributes[i];
1975 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1981 attributes=(
char **) AcquireQuantumMemory(4,
1982 sizeof(*attributes));
1984 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1985 (l+4),
sizeof(*attributes));
1986 if (attributes == (
char **) NULL)
1988 (void) ThrowMagickException(exception,GetMagickModule(),
1989 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1990 utf8=DestroyString(utf8);
1991 return(&root->root);
1993 attributes[l+2]=(
char *) NULL;
1994 attributes[l+1]=(
char *) NULL;
1996 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1997 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1998 attributes[l]=ConstantString(
"");
2002 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2004 if ((c ==
'"') || (c ==
'\''))
2011 while ((*p !=
'\0') && (*p != c))
2017 attributes[l]=ConstantString(
"");
2018 attributes[l+1]=ConstantString(
"");
2019 (void) DestroyXMLTreeAttributes(attributes);
2020 (void) ThrowMagickException(exception,
2021 GetMagickModule(),OptionWarning,
"ParseError",
2023 utf8=DestroyString(utf8);
2024 return(&root->root);
2027 while ((attribute != (
char **) NULL) &&
2028 (attribute[j] != (
char *) NULL) &&
2029 (strcmp(attribute[j],attributes[l]) != 0))
2031 attributes[l+1]=ParseEntities(attributes[l+1],
2032 root->entities,(attribute != (
char **) NULL) &&
2033 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2036 attributes[l]=ConstantString(attributes[l]);
2038 while (isspace((
int) ((
unsigned char) *p)) != 0)
2044 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2053 if (((*p !=
'\0') && (*p !=
'>')) ||
2054 ((*p ==
'\0') && (terminal !=
'>')))
2057 (void) DestroyXMLTreeAttributes(attributes);
2058 (void) ThrowMagickException(exception,GetMagickModule(),
2059 OptionWarning,
"ParseError",
"missing >");
2060 utf8=DestroyString(utf8);
2061 return(&root->root);
2063 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2064 (void) DestroyXMLTreeAttributes(attributes);
2067 ParseOpenTag(root,tag,attributes);
2068 (void) ParseCloseTag(root,tag,exception);
2074 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2077 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2078 ParseOpenTag(root,tag,attributes);
2082 (void) DestroyXMLTreeAttributes(attributes);
2089 (void) DestroyXMLTreeAttributes(attributes);
2090 (void) ThrowMagickException(exception,GetMagickModule(),
2091 OptionWarning,
"ParseError",
"missing >");
2092 utf8=DestroyString(utf8);
2093 return(&root->root);
2104 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2106 if ((c ==
'\0') && (terminal !=
'>'))
2108 (void) ThrowMagickException(exception,GetMagickModule(),
2109 OptionWarning,
"ParseError",
"missing >");
2110 utf8=DestroyString(utf8);
2111 return(&root->root);
2114 if ((ignore_depth == 0) &&
2115 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2117 utf8=DestroyString(utf8);
2118 return(&root->root);
2120 if (ignore_depth > 0)
2123 if (isspace((
int) ((
unsigned char) *p)) != 0)
2124 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2127 if (strncmp(p,
"!--",3) == 0)
2133 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2134 ((*p ==
'\0') && (terminal !=
'>')))
2136 (void) ThrowMagickException(exception,GetMagickModule(),
2137 OptionWarning,
"ParseError",
"unclosed <!--");
2138 utf8=DestroyString(utf8);
2139 return(&root->root);
2143 if (strncmp(p,
"![CDATA[",8) == 0)
2149 if (p != (
char *) NULL)
2152 if (ignore_depth == 0)
2153 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2157 (void) ThrowMagickException(exception,GetMagickModule(),
2158 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2159 utf8=DestroyString(utf8);
2160 return(&root->root);
2164 if (strncmp(p,
"!DOCTYPE",8) == 0)
2169 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2170 ((l != 0) && ((*p !=
']') ||
2171 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2172 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2173 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2174 if ((*p ==
'\0') && (terminal !=
'>'))
2176 (void) ThrowMagickException(exception,GetMagickModule(),
2177 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2178 utf8=DestroyString(utf8);
2179 return(&root->root);
2182 tag=strchr(tag,
'[')+1;
2185 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2187 if (status == MagickFalse)
2189 utf8=DestroyString(utf8);
2190 return(&root->root);
2204 if (p == (
char *) NULL)
2207 }
while ((*p !=
'\0') && (*p !=
'>'));
2208 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2211 (void) ThrowMagickException(exception,GetMagickModule(),
2212 OptionWarning,
"ParseError",
"unclosed <?");
2213 utf8=DestroyString(utf8);
2214 return(&root->root);
2216 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2220 (void) ThrowMagickException(exception,GetMagickModule(),
2221 OptionWarning,
"ParseError",
"unexpected <");
2222 utf8=DestroyString(utf8);
2223 return(&root->root);
2225 if ((p == (
char *) NULL) || (*p ==
'\0'))
2229 if ((*p !=
'\0') && (*p !=
'<'))
2234 while ((*p !=
'\0') && (*p !=
'<'))
2238 if (ignore_depth == 0)
2239 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2245 utf8=DestroyString(utf8);
2247 return(&root->root);
2248 if (root->node->tag == (
char *) NULL)
2250 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2251 "ParseError",
"root tag missing");
2252 return(&root->root);
2254 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2255 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2256 return(&root->root);
2281MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2284 *predefined_entities[NumberPredefinedEntities+1] =
2286 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2287 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2293 root=(
XMLTreeRoot *) AcquireMagickMemory(
sizeof(*root));
2296 (void) memset(root,0,
sizeof(*root));
2297 root->root.tag=(
char *) NULL;
2298 if (tag != (
char *) NULL)
2299 root->root.tag=ConstantString(tag);
2300 root->node=(&root->root);
2301 root->root.content=ConstantString(
"");
2302 root->entities=(
char **) AcquireMagickMemory(
sizeof(predefined_entities));
2303 if (root->entities == (
char **) NULL)
2305 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2306 root->root.attributes=sentinel;
2307 root->attributes=(
char ***) root->root.attributes;
2308 root->processing_instructions=(
char ***) root->root.attributes;
2309 root->debug=IsEventLogging();
2310 root->signature=MagickCoreSignature;
2311 return(&root->root);
2343 assert((xml_info->signature == MagickCoreSignature) ||
2344 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2345 if (IsEventLogging() != MagickFalse)
2346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2348 xml_info->next->sibling=xml_info->sibling;
2351 node=xml_info->parent->child;
2352 if (node == xml_info)
2353 xml_info->parent->child=xml_info->ordered;
2356 while (node->ordered != xml_info)
2358 node->ordered=node->ordered->ordered;
2359 node=xml_info->parent->child;
2360 if (strcmp(node->tag,xml_info->tag) != 0)
2362 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2364 if (node->sibling != xml_info)
2367 node->sibling=(xml_info->next != (
XMLTreeInfo *) NULL) ?
2368 xml_info->next : node->sibling->sibling;
2371 (node->next != xml_info))
2374 node->next=node->next->next;
2412 const char *tag,
const char *value)
2419 assert((xml_info->signature == MagickCoreSignature) ||
2420 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2421 if (IsEventLogging() != MagickFalse)
2422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2424 while ((xml_info->attributes[i] != (
char *) NULL) &&
2425 (strcmp(xml_info->attributes[i],tag) != 0))
2427 if (xml_info->attributes[i] == (
char *) NULL)
2432 if (value == (
const char *) NULL)
2434 if (xml_info->attributes != sentinel)
2435 xml_info->attributes=(
char **) ResizeQuantumMemory(
2436 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2439 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2440 sizeof(*xml_info->attributes));
2441 if (xml_info->attributes != (
char **) NULL)
2442 xml_info->attributes[1]=ConstantString(
"");
2444 if (xml_info->attributes == (
char **) NULL)
2445 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2446 xml_info->attributes[i]=ConstantString(tag);
2447 xml_info->attributes[i+2]=(
char *) NULL;
2448 (void) strlen(xml_info->attributes[i+1]);
2453 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2454 if (xml_info->attributes[i+1] != (
char *) NULL)
2455 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2456 if (value != (
const char *) NULL)
2458 xml_info->attributes[i+1]=ConstantString(value);
2461 if (xml_info->attributes[i] != (
char *) NULL)
2462 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2463 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(
size_t)
2464 (j-i)*
sizeof(*xml_info->attributes));
2465 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2466 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2467 if (xml_info->attributes == (
char **) NULL)
2468 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2470 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2471 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2502 const char *content)
2505 assert((xml_info->signature == MagickCoreSignature) ||
2506 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2507 if (IsEventLogging() != MagickFalse)
2508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2509 if (xml_info->content != (
char *) NULL)
2510 xml_info->content=DestroyString(xml_info->content);
2511 xml_info->content=(
char *) ConstantString(content);
2538static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2539 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2545 canonical_content=CanonicalXMLContent(source,pedantic);
2551 content=AcquireString(source);
2552 content[offset]=
'\0';
2553 canonical_content=CanonicalXMLContent(content,pedantic);
2554 content=DestroyString(content);
2556 if (canonical_content == (
char *) NULL)
2557 return(*destination);
2558 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2560 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2561 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2562 sizeof(**destination));
2563 if (*destination == (
char *) NULL)
2564 return(*destination);
2566 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2568 canonical_content=DestroyString(canonical_content);
2569 return(*destination);
2572static char *XMLTreeTagToXML(
XMLTreeInfo *xml_info,
char **source,
size_t *length,
2573 size_t *extent,
size_t start,
char ***attributes)
2588 content=(
char *)
"";
2590 content=xml_info->parent->content;
2592 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2593 start),source,length,extent,MagickFalse);
2594 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2596 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2597 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2598 if (*source == (
char *) NULL)
2601 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2602 "<%s",xml_info->tag);
2603 for (i=0; xml_info->attributes[i]; i+=2)
2605 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2606 if (attribute != xml_info->attributes[i+1])
2608 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2610 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2611 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2612 if (*source == (
char *) NULL)
2613 return((
char *) NULL);
2615 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2616 xml_info->attributes[i]);
2617 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2619 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2622 while ((attributes[i] != (
char **) NULL) &&
2623 (strcmp(attributes[i][0],xml_info->tag) != 0))
2626 while ((attributes[i] != (
char **) NULL) &&
2627 (attributes[i][j] != (
char *) NULL))
2629 if ((attributes[i][j+1] == (
char *) NULL) ||
2630 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2635 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2637 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2638 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2639 if (*source == (
char *) NULL)
2640 return((
char *) NULL);
2642 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2644 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2646 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2649 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2650 *xml_info->content ?
">" :
"/>");
2652 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2654 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2656 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2658 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2659 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2660 if (*source == (
char *) NULL)
2661 return((
char *) NULL);
2663 if (*xml_info->content !=
'\0')
2664 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2666 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2669 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2672 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2677MagickExport
char *XMLTreeInfoToXML(
XMLTreeInfo *xml_info)
2701 assert((xml_info->signature == MagickCoreSignature) ||
2702 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2703 if (IsEventLogging() != MagickFalse)
2704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2705 if (xml_info->tag == (
char *) NULL)
2706 return((
char *) NULL);
2707 xml=AcquireString((
char *) NULL);
2709 extent=MagickPathExtent;
2713 parent=xml_info->parent;
2715 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2720 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2721 p=root->processing_instructions[i][1];
2722 for (j=1; p != (
char *) NULL; j++)
2724 if (root->processing_instructions[i][k][j-1] ==
'>')
2726 p=root->processing_instructions[i][j];
2729 q=root->processing_instructions[i][0];
2730 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2732 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2733 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2734 if (xml == (
char *) NULL)
2737 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2738 *p !=
'\0' ?
" " :
"",p);
2739 p=root->processing_instructions[i][j];
2742 ordered=xml_info->ordered;
2745 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2746 xml_info->parent=parent;
2747 xml_info->ordered=ordered;
2749 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2754 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2755 p=root->processing_instructions[i][1];
2756 for (j=1; p != (
char *) NULL; j++)
2758 if (root->processing_instructions[i][k][j-1] ==
'<')
2760 p=root->processing_instructions[i][j];
2763 q=root->processing_instructions[i][0];
2764 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2766 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2767 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2768 if (xml == (
char *) NULL)
2771 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2772 *p !=
'\0' ?
" " :
"",p);
2773 p=root->processing_instructions[i][j];
2776 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));