50#include "magick/studio.h"
51#include "magick/blob.h"
52#include "magick/blob-private.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/image-private.h"
56#include "magick/log.h"
57#include "magick/memory_.h"
58#include "magick/semaphore.h"
59#include "magick/string_.h"
60#include "magick/string-private.h"
61#include "magick/token-private.h"
62#include "magick/utility.h"
63#include "magick/utility-private.h"
64#include "magick/xml-tree.h"
65#include "magick/xml-tree-private.h"
70#define NumberPredefinedEntities 10
71#define XMLWhitespace "\t\r\n "
118 ***processing_instructions,
136 *sentinel[] = { (
char *) NULL };
167 const char *tag,
const size_t offset)
174 child=(
XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
177 (void) memset(child,0,
sizeof(*child));
178 child->tag=ConstantString(tag);
179 child->attributes=sentinel;
180 child->content=ConstantString(
"");
181 child->debug=IsEventLogging();
182 child->signature=MagickCoreSignature;
183 return(InsertTagIntoXMLTree(xml_info,child,offset));
215 const char *path,
const size_t offset)
219 subnode[MaxTextExtent],
234 assert((xml_info->signature == MagickCoreSignature) ||
235 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
236 if (IsEventLogging() != MagickFalse)
237 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
239 components=GetPathComponents(path,&number_components);
240 if (components == (
char **) NULL)
242 for (i=0; i < (ssize_t) number_components; i++)
244 GetPathComponent(components[i],SubimagePath,subnode);
245 GetPathComponent(components[i],CanonicalPath,tag);
246 child=GetXMLTreeChild(node,tag);
248 child=AddChildToXMLTree(node,tag,offset);
252 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
254 node=GetXMLTreeOrdered(node);
260 components[i]=DestroyString(components[i]);
262 for ( ; i < (ssize_t) number_components; i++)
263 components[i]=DestroyString(components[i]);
264 components=(
char **) RelinquishMagickMemory(components);
296MagickExport
char *CanonicalXMLContent(
const char *content,
297 const MagickBooleanType pedantic)
316 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
317 if (utf8 == (
unsigned char *) NULL)
318 return((
char *) NULL);
319 for (p=utf8; *p !=
'\0'; p++)
320 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
327 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
328 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
329 if (base64 == (
char *) NULL)
330 return((
char *) NULL);
331 canonical_content=AcquireString(
"<base64>");
332 (void) ConcatenateString(&canonical_content,base64);
333 base64=DestroyString(base64);
334 (void) ConcatenateString(&canonical_content,
"</base64>");
335 return(canonical_content);
341 canonical_content=AcquireString((
char *) NULL);
342 extent=MaxTextExtent;
343 for (p=utf8; *p !=
'\0'; p++)
345 if ((i+MaxTextExtent) > (ssize_t) extent)
347 extent+=MaxTextExtent;
348 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
349 sizeof(*canonical_content));
350 if (canonical_content == (
char *) NULL)
351 return(canonical_content);
357 i+=FormatLocaleString(canonical_content+i,extent,
"&");
362 i+=FormatLocaleString(canonical_content+i,extent,
"<");
367 i+=FormatLocaleString(canonical_content+i,extent,
">");
372 i+=FormatLocaleString(canonical_content+i,extent,
""");
377 if (pedantic == MagickFalse)
379 canonical_content[i++]=(char) (*p);
382 i+=FormatLocaleString(canonical_content+i,extent,
"
");
387 if (pedantic == MagickFalse)
389 canonical_content[i++]=(char) (*p);
392 i+=FormatLocaleString(canonical_content+i,extent,
"	");
397 i+=FormatLocaleString(canonical_content+i,extent,
"
");
402 canonical_content[i++]=(char) (*p);
407 canonical_content[i]=
'\0';
408 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
409 return(canonical_content);
435static char **DestroyXMLTreeAttributes(
char **attributes)
443 if ((attributes == (
char **) NULL) || (attributes == sentinel))
444 return((
char **) NULL);
445 for (i=0; attributes[i] != (
char *) NULL; i+=2)
450 if (attributes[i] != (
char *) NULL)
451 attributes[i]=DestroyString(attributes[i]);
452 if (attributes[i+1] != (
char *) NULL)
453 attributes[i+1]=DestroyString(attributes[i+1]);
455 attributes=(
char **) RelinquishMagickMemory(attributes);
456 return((
char **) NULL);
459static void DestroyXMLTreeChild(
XMLTreeInfo *xml_info)
465 child=xml_info->child;
471 (void) DestroyXMLTree(node);
475static void DestroyXMLTreeOrdered(
XMLTreeInfo *xml_info)
481 ordered=xml_info->ordered;
485 ordered=node->ordered;
487 (void) DestroyXMLTree(node);
491static void DestroyXMLTreeRoot(
XMLTreeInfo *xml_info)
504 assert((xml_info->signature == MagickCoreSignature) ||
505 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
506 if (IsEventLogging() != MagickFalse)
507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
514 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
515 root->entities[i+1]=DestroyString(root->entities[i+1]);
516 root->entities=(
char **) RelinquishMagickMemory(root->entities);
517 for (i=0; root->attributes[i] != (
char **) NULL; i++)
519 attributes=root->attributes[i];
520 if (attributes[0] != (
char *) NULL)
521 attributes[0]=DestroyString(attributes[0]);
522 for (j=1; attributes[j] != (
char *) NULL; j+=3)
524 if (attributes[j] != (
char *) NULL)
525 attributes[j]=DestroyString(attributes[j]);
526 if (attributes[j+1] != (
char *) NULL)
527 attributes[j+1]=DestroyString(attributes[j+1]);
528 if (attributes[j+2] != (
char *) NULL)
529 attributes[j+2]=DestroyString(attributes[j+2]);
531 attributes=(
char **) RelinquishMagickMemory(attributes);
533 if (root->attributes[0] != (
char **) NULL)
534 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
535 if (root->processing_instructions[0] != (
char **) NULL)
537 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
539 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
540 root->processing_instructions[i][j]=DestroyString(
541 root->processing_instructions[i][j]);
542 root->processing_instructions[i][j+1]=DestroyString(
543 root->processing_instructions[i][j+1]);
544 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
545 root->processing_instructions[i]);
547 root->processing_instructions=(
char ***) RelinquishMagickMemory(
548 root->processing_instructions);
555 assert((xml_info->signature == MagickCoreSignature) ||
556 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
557 if (IsEventLogging() != MagickFalse)
558 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
559 DestroyXMLTreeChild(xml_info);
560 DestroyXMLTreeOrdered(xml_info);
561 DestroyXMLTreeRoot(xml_info);
562 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
563 xml_info->content=DestroyString(xml_info->content);
564 xml_info->tag=DestroyString(xml_info->tag);
565 xml_info=(
XMLTreeInfo *) RelinquishMagickMemory(xml_info);
593MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
614 assert(filename != (
const char *) NULL);
617 if (LocaleCompare(filename,
"-") != 0)
618 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
620 return((
char *) NULL);
621 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
623 if ((file == fileno(stdin)) || (offset < 0) ||
624 (offset != (MagickOffsetType) ((ssize_t) offset)))
635 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
636 quantum=(size_t) MagickMaxBufferExtent;
637 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
638 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
639 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
640 for (i=0; xml != (
char *) NULL; i+=count)
642 count=read(file,xml+i,quantum);
649 if (~((
size_t) i) < (quantum+1))
651 xml=(
char *) RelinquishMagickMemory(xml);
654 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
655 if ((
size_t) (i+count) >= extent)
658 if (LocaleCompare(filename,
"-") != 0)
660 if (xml == (
char *) NULL)
661 return((
char *) NULL);
664 xml=(
char *) RelinquishMagickMemory(xml);
665 return((
char *) NULL);
667 length=(size_t) MagickMin(i+count,extent);
671 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
673 if (~length >= (MaxTextExtent-1))
674 xml=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*xml));
675 if (xml == (
char *) NULL)
678 return((
char *) NULL);
680 map=MapBlob(file,ReadMode,0,length);
681 if (map != (
char *) NULL)
683 (void) memcpy(xml,map,length);
684 (void) UnmapBlob(map,length);
688 (void) lseek(file,0,SEEK_SET);
689 for (i=0; i < length; i+=count)
691 count=read(file,xml+i,(
size_t) MagickMin(length-i,(
size_t) MagickMaxBufferExtent));
702 xml=(
char *) RelinquishMagickMemory(xml);
703 return((
char *) NULL);
707 if (LocaleCompare(filename,
"-") != 0)
710 xml=(
char *) RelinquishMagickMemory(xml);
739 assert((xml_info->signature == MagickCoreSignature) ||
740 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
741 if (IsEventLogging() != MagickFalse)
742 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
743 return(xml_info->next);
771MagickExport
const char *GetXMLTreeAttribute(
XMLTreeInfo *xml_info,
782 assert((xml_info->signature == MagickCoreSignature) ||
783 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
784 if (IsEventLogging() != MagickFalse)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
786 if (xml_info->attributes == (
char **) NULL)
787 return((
const char *) NULL);
789 while ((xml_info->attributes[i] != (
char *) NULL) &&
790 (strcmp(xml_info->attributes[i],tag) != 0))
792 if (xml_info->attributes[i] != (
char *) NULL)
793 return(xml_info->attributes[i+1]);
798 while ((root->attributes[i] != (
char **) NULL) &&
799 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
801 if (root->attributes[i] == (
char **) NULL)
802 return((
const char *) NULL);
804 while ((root->attributes[i][j] != (
char *) NULL) &&
805 (strcmp(root->attributes[i][j],tag) != 0))
807 if (root->attributes[i][j] == (
char *) NULL)
808 return((
const char *) NULL);
809 return(root->attributes[i][j+1]);
838MagickExport MagickBooleanType GetXMLTreeAttributes(
const XMLTreeInfo *xml_info,
845 assert((xml_info->signature == MagickCoreSignature) ||
846 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
848 if (IsEventLogging() != MagickFalse)
849 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
850 if (xml_info->attributes == (
char **) NULL)
853 while (xml_info->attributes[i] != (
char *) NULL)
855 (void) AddValueToSplayTree(attributes,
856 ConstantString(xml_info->attributes[i]),
857 ConstantString(xml_info->attributes[i+1]));
892 assert((xml_info->signature == MagickCoreSignature) ||
893 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
894 if (IsEventLogging() != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
896 child=xml_info->child;
897 if (tag != (
const char *) NULL)
898 while ((child != (
XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
899 child=child->sibling;
926MagickExport
const char *GetXMLTreeContent(
XMLTreeInfo *xml_info)
929 assert((xml_info->signature == MagickCoreSignature) ||
930 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
931 if (IsEventLogging() != MagickFalse)
932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
933 return(xml_info->content);
961 assert((xml_info->signature == MagickCoreSignature) ||
962 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
963 if (IsEventLogging() != MagickFalse)
964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
965 return(xml_info->ordered);
997 subnode[MaxTextExtent],
1011 assert((xml_info->signature == MagickCoreSignature) ||
1012 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1013 if (IsEventLogging() != MagickFalse)
1014 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1016 components=GetPathComponents(path,&number_components);
1017 if (components == (
char **) NULL)
1019 for (i=0; i < (ssize_t) number_components; i++)
1021 GetPathComponent(components[i],SubimagePath,subnode);
1022 GetPathComponent(components[i],CanonicalPath,tag);
1023 node=GetXMLTreeChild(node,tag);
1026 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1028 node=GetXMLTreeOrdered(node);
1034 components[i]=DestroyString(components[i]);
1036 for ( ; i < (ssize_t) number_components; i++)
1037 components[i]=DestroyString(components[i]);
1038 components=(
char **) RelinquishMagickMemory(components);
1066MagickExport
const char **GetXMLTreeProcessingInstructions(
1076 assert((xml_info->signature == MagickCoreSignature) ||
1077 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1078 if (IsEventLogging() != MagickFalse)
1079 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1084 while ((root->processing_instructions[i] != (
char **) NULL) &&
1085 (strcmp(root->processing_instructions[i][0],target) != 0))
1087 if (root->processing_instructions[i] == (
char **) NULL)
1088 return((
const char **) sentinel);
1089 return((
const char **) (root->processing_instructions[i]+1));
1117 assert((xml_info->signature == MagickCoreSignature) ||
1118 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1119 if (IsEventLogging() != MagickFalse)
1120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1121 return(xml_info->sibling);
1146MagickExport
const char *GetXMLTreeTag(
XMLTreeInfo *xml_info)
1149 assert((xml_info->signature == MagickCoreSignature) ||
1150 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1151 if (IsEventLogging() != MagickFalse)
1152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1153 return(xml_info->tag);
1195 child->offset=offset;
1196 child->parent=xml_info;
1199 xml_info->child=child;
1202 head=xml_info->child;
1203 if (head->offset > offset)
1205 child->ordered=head;
1206 xml_info->child=child;
1212 (node->ordered->offset <= offset))
1214 child->ordered=node->ordered;
1215 node->ordered=child;
1219 while ((node != (
XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1224 if ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1227 (node->next->offset <= offset))
1229 child->next=node->next;
1235 previous->sibling=node->sibling;
1239 while ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1244 child->sibling=node;
1246 previous->sibling=child;
1277static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1295 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1296 if (utf8 == (
char *) NULL)
1297 return((
char *) NULL);
1298 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1304 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1310 for (i=2; i < (ssize_t) (*length-1); i+=2)
1312 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1313 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1314 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1316 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1317 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1318 (content[i] & 0xff);
1319 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
1321 if ((
size_t) (j+MaxTextExtent) > extent)
1323 extent=(size_t) j+MaxTextExtent;
1324 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1325 if (utf8 == (
char *) NULL)
1338 for (bits=0;
byte != 0;
byte/=2)
1341 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1345 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1350 utf8=(
char *) ResizeQuantumMemory(utf8,*length,
sizeof(*utf8));
1351 if (utf8 != (
char *) NULL)
1356static char *ParseEntities(
char *xml,
char **entities,
int state)
1380 for ( ; *xml !=
'\0'; xml++)
1381 while (*xml ==
'\r')
1385 (void) memmove(xml,xml+1,strlen(xml));
1389 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1390 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1402 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1408 c=strtol(xml+2,&entity,10);
1410 c=strtol(xml+3,&entity,16);
1411 if ((c == 0) || (*entity !=
';'))
1427 for (i=0;
byte != 0;
byte/=2)
1430 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1435 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1439 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1442 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1443 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1449 while ((entities[i] != (
char *) NULL) &&
1450 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1452 if (entities[i++] == (
char *) NULL)
1455 if (entities[i] != (
char *) NULL)
1460 length=strlen(entities[i]);
1461 entity=strchr(xml,
';');
1462 if ((entity != (
char *) NULL) &&
1463 ((length-1L) >= (
size_t) (entity-xml)))
1465 offset=(ssize_t) (xml-p);
1466 extent=(size_t) (offset+length+strlen(entity));
1469 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1477 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1478 sizeof(*extent_xml));
1479 if (extent_xml != (
char *) NULL)
1481 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1482 (void) CopyMagickString(extent_xml,p,extent*
1483 sizeof(*extent_xml));
1487 if (p == (
char *) NULL)
1488 ThrowFatalException(ResourceLimitFatalError,
1489 "MemoryAllocationFailed");
1491 entity=strchr(xml,
';');
1493 if (entity != (
char *) NULL)
1494 (void) memmove(xml+length,entity+1,strlen(entity));
1495 (void) memcpy(xml,entities[i],length);
1499 if (((state ==
' ') || (state ==
'*')) &&
1500 (isspace((
int) ((
unsigned char) *xml) != 0)))
1511 for (xml=p; *xml !=
'\0'; xml++)
1516 i=(ssize_t) strspn(xml,accept);
1518 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1519 while ((*xml !=
'\0') && (*xml !=
' '))
1525 if ((xml >= p) && (*xml ==
' '))
1528 return(p == q ? ConstantString(p) : p);
1531static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1532 const size_t length,
const char state)
1537 xml_info=root->node;
1538 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1542 xml=ParseEntities(xml,root->entities,state);
1543 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1545 (void) ConcatenateString(&xml_info->content,xml);
1546 xml=DestroyString(xml);
1550 if (xml_info->content != (
char *) NULL)
1551 xml_info->content=DestroyString(xml_info->content);
1552 xml_info->content=xml;
1560 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1562 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1563 "ParseError",
"unexpected closing tag </%s>",tag);
1564 return(&root->root);
1566 root->node=root->node->parent;
1570static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1571 const size_t depth,
char **entities)
1579 if (depth > MagickMaxRecursionDepth)
1580 return(MagickFalse);
1583 while ((*xml !=
'\0') && (*xml !=
'&'))
1587 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1588 return(MagickFalse);
1590 while ((entities[i] != (
char *) NULL) &&
1591 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1593 if ((entities[i] != (
char *) NULL) &&
1594 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1595 return(MagickFalse);
1599static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1611 xml+=strcspn(xml,XMLWhitespace);
1615 xml+=strspn(xml+1,XMLWhitespace)+1;
1617 if (strcmp(target,
"xml") == 0)
1619 xml=strstr(xml,
"standalone");
1620 if ((xml != (
char *) NULL) &&
1621 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1622 root->standalone=MagickTrue;
1625 if (root->processing_instructions[0] == (
char **) NULL)
1627 root->processing_instructions=(
char ***) AcquireMagickMemory(
sizeof(
1628 *root->processing_instructions));
1629 if (root->processing_instructions ==(
char ***) NULL)
1630 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1631 *root->processing_instructions=(
char **) NULL;
1634 while ((root->processing_instructions[i] != (
char **) NULL) &&
1635 (strcmp(target,root->processing_instructions[i][0]) != 0))
1637 if (root->processing_instructions[i] == (
char **) NULL)
1639 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1640 root->processing_instructions,(
size_t) (i+2),
1641 sizeof(*root->processing_instructions));
1642 if (root->processing_instructions == (
char ***) NULL)
1643 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1644 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1645 sizeof(**root->processing_instructions));
1646 if (root->processing_instructions[i] == (
char **) NULL)
1647 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1648 root->processing_instructions[i+1]=(
char **) NULL;
1649 root->processing_instructions[i][0]=ConstantString(target);
1650 root->processing_instructions[i][1]=(
char *)
1651 root->processing_instructions[i+1];
1652 root->processing_instructions[i+1]=(
char **) NULL;
1653 root->processing_instructions[i][2]=ConstantString(
"");
1656 while (root->processing_instructions[i][j] != (
char *) NULL)
1658 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1659 root->processing_instructions[i],(
size_t) (j+3),
1660 sizeof(**root->processing_instructions));
1661 if (root->processing_instructions[i] == (
char **) NULL)
1662 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1663 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1664 root->processing_instructions[i][j+1],(
size_t) (j+1),
1665 sizeof(***root->processing_instructions));
1666 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1667 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1668 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1669 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1670 root->processing_instructions[i][j]=ConstantString(xml);
1671 root->processing_instructions[i][j+1]=(
char *) NULL;
1674static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1681 **predefined_entities,
1691 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1692 if (predefined_entities == (
char **) NULL)
1693 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1694 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1695 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1697 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1701 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1706 if (strspn(xml+8,XMLWhitespace) == 0)
1708 xml+=strspn(xml+8,XMLWhitespace)+8;
1710 n=xml+strspn(xml,XMLWhitespace
"%");
1711 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1713 xml=n+strcspn(n,XMLWhitespace);
1717 v=xml+strspn(xml+1,XMLWhitespace)+1;
1720 if ((q !=
'"') && (q !=
'\''))
1725 xml=strchr(xml,
'>');
1728 entities=(*c ==
'%') ? predefined_entities : root->entities;
1729 for (i=0; entities[i] != (
char *) NULL; i++) ;
1730 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1732 if (entities == (
char **) NULL)
1733 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1735 predefined_entities=entities;
1737 root->entities=entities;
1741 if (xml != (
char *) NULL)
1746 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1747 entities[i+2]=(
char *) NULL;
1748 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1752 if (entities[i+1] != v)
1753 entities[i+1]=DestroyString(entities[i+1]);
1754 (void) ThrowMagickException(exception,GetMagickModule(),
1755 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1756 predefined_entities=(
char **) RelinquishMagickMemory(
1757 predefined_entities);
1758 return(MagickFalse);
1762 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1767 t=xml+strspn(xml+9,XMLWhitespace)+9;
1770 (void) ThrowMagickException(exception,GetMagickModule(),
1771 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1772 predefined_entities=(
char **) RelinquishMagickMemory(
1773 predefined_entities);
1774 return(MagickFalse);
1776 xml=t+strcspn(t,XMLWhitespace
">");
1781 while ((root->attributes[i] != (
char **) NULL) &&
1782 (n != (
char *) NULL) &&
1783 (strcmp(n,root->attributes[i][0]) != 0))
1785 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1788 xml=n+strcspn(n,XMLWhitespace);
1793 (void) ThrowMagickException(exception,GetMagickModule(),
1794 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1795 predefined_entities=(
char **) RelinquishMagickMemory(
1796 predefined_entities);
1797 return(MagickFalse);
1799 xml+=strspn(xml+1,XMLWhitespace)+1;
1800 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1801 if (strncmp(xml,
"NOTATION",8) == 0)
1802 xml+=strspn(xml+8,XMLWhitespace)+8;
1803 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1804 strcspn(xml,XMLWhitespace);
1805 if (xml == (
char *) NULL)
1807 (void) ThrowMagickException(exception,GetMagickModule(),
1808 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1809 predefined_entities=(
char **) RelinquishMagickMemory(
1810 predefined_entities);
1811 return(MagickFalse);
1813 xml+=strspn(xml,XMLWhitespace
")");
1814 if (strncmp(xml,
"#FIXED",6) == 0)
1815 xml+=strspn(xml+6,XMLWhitespace)+6;
1818 xml+=strcspn(xml,XMLWhitespace
">")-1;
1824 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1825 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1829 (void) ThrowMagickException(exception,GetMagickModule(),
1830 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1831 predefined_entities=(
char **) RelinquishMagickMemory(
1832 predefined_entities);
1833 return(MagickFalse);
1835 if (root->attributes[i] == (
char **) NULL)
1841 root->attributes=(
char ***) AcquireQuantumMemory(2,
1842 sizeof(*root->attributes));
1844 root->attributes=(
char ***) ResizeQuantumMemory(
1845 root->attributes,(
size_t) (i+2),
1846 sizeof(*root->attributes));
1847 if (root->attributes == (
char ***) NULL)
1848 ThrowFatalException(ResourceLimitFatalError,
1849 "MemoryAllocationFailed");
1850 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1851 sizeof(**root->attributes));
1852 if (root->attributes[i] == (
char **) NULL)
1853 ThrowFatalException(ResourceLimitFatalError,
1854 "MemoryAllocationFailed");
1855 root->attributes[i][0]=ConstantString(t);
1856 root->attributes[i][1]=(
char *) NULL;
1857 root->attributes[i+1]=(
char **) NULL;
1859 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1860 root->attributes[i]=(
char **) ResizeQuantumMemory(
1861 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1862 if (root->attributes[i] == (
char **) NULL)
1863 ThrowFatalException(ResourceLimitFatalError,
1864 "MemoryAllocationFailed");
1865 root->attributes[i][j+3]=(
char *) NULL;
1866 root->attributes[i][j+2]=ConstantString(c);
1867 root->attributes[i][j+1]=(
char *) NULL;
1868 if (v != (
char *) NULL)
1869 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1870 root->attributes[i][j]=ConstantString(n);
1874 if (strncmp(xml,
"<!--", 4) == 0)
1875 xml=strstr(xml+4,
"-->");
1877 if (strncmp(xml,
"<?", 2) == 0)
1881 if (xml != (
char *) NULL)
1883 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1889 xml=strchr(xml,
'>');
1891 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1894 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1898static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1903 xml_info=root->node;
1904 if (xml_info->tag == (
char *) NULL)
1905 xml_info->tag=ConstantString(tag);
1907 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1909 xml_info->attributes=attributes;
1910 root->node=xml_info;
1921static inline MagickBooleanType IsSkipTag(
const char *tag)
1927 while (skip_tags[i] != (
const char *) NULL)
1929 if (LocaleCompare(tag,skip_tags[i]) == 0)
1933 return(MagickFalse);
1967 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1969 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1970 "ParseError",
"root tag missing");
1973 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1975 utf8=ConvertUTF16ToUTF8(xml,&length);
1976 if (utf8 == (
char *) NULL)
1978 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1979 "ParseError",
"UTF16 to UTF8 failed");
1982 terminal=utf8[length-1];
1983 utf8[length-1]=
'\0';
1985 while ((*p !=
'\0') && (*p !=
'<'))
1989 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1990 "ParseError",
"root tag missing");
1991 utf8=DestroyString(utf8);
1994 attribute=(
char **) NULL;
1999 attributes=(
char **) sentinel;
2002 if ((isalpha((
int) ((
unsigned char) *p)) !=0) || (*p ==
'_') ||
2003 (*p ==
':') || (c <
'\0'))
2010 (void) ThrowMagickException(exception,GetMagickModule(),
2011 OptionWarning,
"ParseError",
"root tag missing");
2012 utf8=DestroyString(utf8);
2013 return(&root->root);
2015 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
2016 while (isspace((
int) ((
unsigned char) *p)) != 0)
2018 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
2019 (ignore_depth == 0))
2021 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2027 while ((root->attributes[i] != (
char **) NULL) &&
2028 (strcmp(root->attributes[i][0],tag) != 0))
2030 attribute=root->attributes[i];
2032 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
2038 attributes=(
char **) AcquireQuantumMemory(4,
2039 sizeof(*attributes));
2041 attributes=(
char **) ResizeQuantumMemory(attributes,
2042 (
size_t) (l+4),
sizeof(*attributes));
2043 if (attributes == (
char **) NULL)
2045 (void) ThrowMagickException(exception,GetMagickModule(),
2046 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2047 utf8=DestroyString(utf8);
2048 return(&root->root);
2050 attributes[l+2]=(
char *) NULL;
2051 attributes[l+1]=(
char *) NULL;
2053 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
2054 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2055 attributes[l]=ConstantString(
"");
2059 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2061 if ((c ==
'"') || (c ==
'\''))
2068 while ((*p !=
'\0') && (*p != c))
2074 attributes[l]=ConstantString(
"");
2075 attributes[l+1]=ConstantString(
"");
2076 (void) DestroyXMLTreeAttributes(attributes);
2077 (void) ThrowMagickException(exception,
2078 GetMagickModule(),OptionWarning,
"ParseError",
2080 utf8=DestroyString(utf8);
2081 return(&root->root);
2084 while ((attribute != (
char **) NULL) &&
2085 (attribute[j] != (
char *) NULL) &&
2086 (strcmp(attribute[j],attributes[l]) != 0))
2088 attributes[l+1]=ParseEntities(attributes[l+1],
2089 root->entities,(attribute != (
char **) NULL) &&
2090 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2093 attributes[l]=ConstantString(attributes[l]);
2095 while (isspace((
int) ((
unsigned char) *p)) != 0)
2101 while((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2110 if (((*p !=
'\0') && (*p !=
'>')) ||
2111 ((*p ==
'\0') && (terminal !=
'>')))
2114 (void) DestroyXMLTreeAttributes(attributes);
2115 (void) ThrowMagickException(exception,GetMagickModule(),
2116 OptionWarning,
"ParseError",
"missing >");
2117 utf8=DestroyString(utf8);
2118 return(&root->root);
2120 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2121 (void) DestroyXMLTreeAttributes(attributes);
2124 ParseOpenTag(root,tag,attributes);
2125 (void) ParseCloseTag(root,tag,exception);
2131 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2134 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2135 ParseOpenTag(root,tag,attributes);
2139 (void) DestroyXMLTreeAttributes(attributes);
2146 (void) DestroyXMLTreeAttributes(attributes);
2147 (void) ThrowMagickException(exception,GetMagickModule(),
2148 OptionWarning,
"ParseError",
"missing >");
2149 utf8=DestroyString(utf8);
2150 return(&root->root);
2161 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2163 if ((c ==
'\0') && (terminal !=
'>'))
2165 (void) ThrowMagickException(exception,GetMagickModule(),
2166 OptionWarning,
"ParseError",
"missing >");
2167 utf8=DestroyString(utf8);
2168 return(&root->root);
2171 if ((ignore_depth == 0) &&
2172 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2174 utf8=DestroyString(utf8);
2175 return(&root->root);
2177 if (ignore_depth > 0)
2180 if (isspace((
int) ((
unsigned char) *p)) != 0)
2181 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2184 if (strncmp(p,
"!--",3) == 0)
2190 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2191 ((*p ==
'\0') && (terminal !=
'>')))
2193 (void) ThrowMagickException(exception,GetMagickModule(),
2194 OptionWarning,
"ParseError",
"unclosed <!--");
2195 utf8=DestroyString(utf8);
2196 return(&root->root);
2200 if (strncmp(p,
"![CDATA[",8) == 0)
2206 if (p != (
char *) NULL)
2209 if (ignore_depth == 0)
2210 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2214 (void) ThrowMagickException(exception,GetMagickModule(),
2215 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2216 utf8=DestroyString(utf8);
2217 return(&root->root);
2221 if (strncmp(p,
"!DOCTYPE",8) == 0)
2226 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2227 ((l != 0) && ((*p !=
']') ||
2228 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2229 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2230 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2231 if ((*p ==
'\0') && (terminal !=
'>'))
2233 (void) ThrowMagickException(exception,GetMagickModule(),
2234 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2235 utf8=DestroyString(utf8);
2236 return(&root->root);
2239 tag=strchr(tag,
'[')+1;
2242 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2244 if (status == MagickFalse)
2246 utf8=DestroyString(utf8);
2247 return(&root->root);
2261 if (p == (
char *) NULL)
2264 }
while ((*p !=
'\0') && (*p !=
'>'));
2265 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionWarning,
"ParseError",
"unclosed <?");
2270 utf8=DestroyString(utf8);
2271 return(&root->root);
2273 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2277 (void) ThrowMagickException(exception,GetMagickModule(),
2278 OptionWarning,
"ParseError",
"unexpected <");
2279 utf8=DestroyString(utf8);
2280 return(&root->root);
2282 if ((p == (
char *) NULL) || (*p ==
'\0'))
2286 if ((*p !=
'\0') && (*p !=
'<'))
2291 while ((*p !=
'\0') && (*p !=
'<'))
2295 if (ignore_depth == 0)
2296 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2302 utf8=DestroyString(utf8);
2304 return(&root->root);
2305 if (root->node->tag == (
char *) NULL)
2307 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2308 "ParseError",
"root tag missing");
2309 return(&root->root);
2311 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2312 "ParseError",
"unclosed tag: `%s'",root->node->tag);
2313 return(&root->root);
2338MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2341 *predefined_entities[NumberPredefinedEntities+1] =
2343 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2344 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2350 root=(
XMLTreeRoot *) AcquireMagickMemory(
sizeof(*root));
2353 (void) memset(root,0,
sizeof(*root));
2354 root->root.tag=(
char *) NULL;
2355 if (tag != (
char *) NULL)
2356 root->root.tag=ConstantString(tag);
2357 root->node=(&root->root);
2358 root->root.content=ConstantString(
"");
2359 root->entities=(
char **) AcquireMagickMemory(
sizeof(predefined_entities));
2360 if (root->entities == (
char **) NULL)
2362 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2363 root->root.attributes=sentinel;
2364 root->attributes=(
char ***) root->root.attributes;
2365 root->processing_instructions=(
char ***) root->root.attributes;
2366 root->debug=IsEventLogging();
2367 root->signature=MagickCoreSignature;
2368 return(&root->root);
2400 assert((xml_info->signature == MagickCoreSignature) ||
2401 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2402 if (IsEventLogging() != MagickFalse)
2403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2405 xml_info->next->sibling=xml_info->sibling;
2408 node=xml_info->parent->child;
2409 if (node == xml_info)
2410 xml_info->parent->child=xml_info->ordered;
2413 while (node->ordered != xml_info)
2415 node->ordered=node->ordered->ordered;
2416 node=xml_info->parent->child;
2417 if (strcmp(node->tag,xml_info->tag) != 0)
2419 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2421 if (node->sibling != xml_info)
2424 node->sibling=(xml_info->next != (
XMLTreeInfo *) NULL) ?
2425 xml_info->next : node->sibling->sibling;
2428 (node->next != xml_info))
2431 node->next=node->next->next;
2469 const char *tag,
const char *value)
2476 assert((xml_info->signature == MagickCoreSignature) ||
2477 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2478 if (IsEventLogging() != MagickFalse)
2479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2481 while ((xml_info->attributes[i] != (
char *) NULL) &&
2482 (strcmp(xml_info->attributes[i],tag) != 0))
2484 if (xml_info->attributes[i] == (
char *) NULL)
2489 if (value == (
const char *) NULL)
2491 if (xml_info->attributes != sentinel)
2492 xml_info->attributes=(
char **) ResizeQuantumMemory(
2493 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2496 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2497 sizeof(*xml_info->attributes));
2498 if (xml_info->attributes != (
char **) NULL)
2499 xml_info->attributes[1]=ConstantString(
"");
2501 if (xml_info->attributes == (
char **) NULL)
2502 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2503 xml_info->attributes[i]=ConstantString(tag);
2504 xml_info->attributes[i+2]=(
char *) NULL;
2505 (void) strlen(xml_info->attributes[i+1]);
2510 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2511 if (xml_info->attributes[i+1] != (
char *) NULL)
2512 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2513 if (value != (
const char *) NULL)
2515 xml_info->attributes[i+1]=ConstantString(value);
2518 if (xml_info->attributes[i] != (
char *) NULL)
2519 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2520 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,
2521 (
size_t) (j-i)*
sizeof(*xml_info->attributes));
2522 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2523 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2524 if (xml_info->attributes == (
char **) NULL)
2525 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2527 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2528 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2559 const char *content)
2562 assert((xml_info->signature == MagickCoreSignature) ||
2563 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2564 if (IsEventLogging() != MagickFalse)
2565 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2566 if (xml_info->content != (
char *) NULL)
2567 xml_info->content=DestroyString(xml_info->content);
2568 xml_info->content=(
char *) ConstantString(content);
2595static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2596 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2602 canonical_content=CanonicalXMLContent(source,pedantic);
2608 content=AcquireString(source);
2609 content[offset]=
'\0';
2610 canonical_content=CanonicalXMLContent(content,pedantic);
2611 content=DestroyString(content);
2613 if (canonical_content == (
char *) NULL)
2614 return(*destination);
2615 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2617 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2618 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2619 sizeof(**destination));
2620 if (*destination == (
char *) NULL)
2621 return(*destination);
2623 *length+=FormatLocaleString(*destination+(*length),*extent,
"%s",
2625 canonical_content=DestroyString(canonical_content);
2626 return(*destination);
2629static char *XMLTreeTagToXML(
XMLTreeInfo *xml_info,
char **source,
size_t *length,
2630 size_t *extent,
size_t start,
char ***attributes)
2645 content=(
char *)
"";
2647 content=xml_info->parent->content;
2649 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2650 start),source,length,extent,MagickFalse);
2651 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2653 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2654 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2655 if (*source == (
char *) NULL)
2658 *length+=FormatLocaleString(*source+(*length),*extent,
"<%s",xml_info->tag);
2659 for (i=0; xml_info->attributes[i]; i+=2)
2661 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2662 if (attribute != xml_info->attributes[i+1])
2664 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2666 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2667 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2668 if (*source == (
char *) NULL)
2669 return((
char *) NULL);
2671 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2672 xml_info->attributes[i]);
2673 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2675 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2678 while ((attributes[i] != (
char **) NULL) &&
2679 (strcmp(attributes[i][0],xml_info->tag) != 0))
2682 while ((attributes[i] != (
char **) NULL) &&
2683 (attributes[i][j] != (
char *) NULL))
2685 if ((attributes[i][j+1] == (
char *) NULL) ||
2686 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2691 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2693 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2694 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2695 if (*source == (
char *) NULL)
2696 return((
char *) NULL);
2698 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2700 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2702 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2705 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2708 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2710 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2712 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2714 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2715 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2716 if (*source == (
char *) NULL)
2717 return((
char *) NULL);
2719 if (*xml_info->content !=
'\0')
2720 *length+=FormatLocaleString(*source+(*length),*extent,
"</%s>",
2722 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2725 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2728 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2733MagickExport
char *XMLTreeInfoToXML(
XMLTreeInfo *xml_info)
2757 assert((xml_info->signature == MagickCoreSignature) ||
2758 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2759 if (IsEventLogging() != MagickFalse)
2760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2761 if (xml_info->tag == (
char *) NULL)
2762 return((
char *) NULL);
2763 xml=AcquireString((
char *) NULL);
2765 extent=MaxTextExtent;
2769 parent=xml_info->parent;
2771 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2776 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2777 p=root->processing_instructions[i][1];
2778 for (j=1; p != (
char *) NULL; j++)
2780 if (root->processing_instructions[i][k][j-1] ==
'>')
2782 p=root->processing_instructions[i][j];
2785 q=root->processing_instructions[i][0];
2786 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2788 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2789 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2790 if (xml == (
char *) NULL)
2793 length+=FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2794 *p !=
'\0' ?
" " :
"",p);
2795 p=root->processing_instructions[i][j];
2798 ordered=xml_info->ordered;
2801 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2802 xml_info->parent=parent;
2803 xml_info->ordered=ordered;
2805 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2810 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2811 p=root->processing_instructions[i][1];
2812 for (j=1; p != (
char *) NULL; j++)
2814 if (root->processing_instructions[i][k][j-1] ==
'<')
2816 p=root->processing_instructions[i][j];
2819 q=root->processing_instructions[i][0];
2820 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2822 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2823 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2824 if (xml == (
char *) NULL)
2827 length+=FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2828 *p !=
'\0' ?
" " :
"",p);
2829 p=root->processing_instructions[i][j];
2832 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));