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) MAGICK_SSIZE_MAX));
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+1),
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));
1470 if (p == (
char *) NULL)
1471 ThrowFatalException(ResourceLimitFatalError,
1472 "MemoryAllocationFailed");
1480 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1481 sizeof(*extent_xml));
1482 if (extent_xml != (
char *) NULL)
1484 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1485 (void) CopyMagickString(extent_xml,p,extent*
1486 sizeof(*extent_xml));
1490 if (p == (
char *) NULL)
1491 ThrowFatalException(ResourceLimitFatalError,
1492 "MemoryAllocationFailed");
1494 entity=strchr(xml,
';');
1496 if (entity != (
char *) NULL)
1497 (void) memmove(xml+length,entity+1,strlen(entity));
1498 (void) memcpy(xml,entities[i],length);
1502 if (((state ==
' ') || (state ==
'*')) &&
1503 (isspace((
int) ((
unsigned char) *xml) != 0)))
1514 for (xml=p; *xml !=
'\0'; xml++)
1519 i=(ssize_t) strspn(xml,accept);
1521 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1522 while ((*xml !=
'\0') && (*xml !=
' '))
1528 if ((xml >= p) && (*xml ==
' '))
1531 return(p == q ? ConstantString(p) : p);
1534static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1535 const size_t length,
const char state)
1540 xml_info=root->node;
1541 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1545 xml=ParseEntities(xml,root->entities,state);
1546 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1548 (void) ConcatenateString(&xml_info->content,xml);
1549 xml=DestroyString(xml);
1553 if (xml_info->content != (
char *) NULL)
1554 xml_info->content=DestroyString(xml_info->content);
1555 xml_info->content=xml;
1563 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1565 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1566 "ParseError",
"unexpected closing tag </%s>",tag);
1567 return(&root->root);
1569 root->node=root->node->parent;
1573static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1574 const size_t depth,
char **entities)
1582 if (depth > MagickMaxRecursionDepth)
1583 return(MagickFalse);
1586 while ((*xml !=
'\0') && (*xml !=
'&'))
1590 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1591 return(MagickFalse);
1593 while ((entities[i] != (
char *) NULL) &&
1594 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1596 if ((entities[i] != (
char *) NULL) &&
1597 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1598 return(MagickFalse);
1602static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1614 xml+=strcspn(xml,XMLWhitespace);
1618 xml+=strspn(xml+1,XMLWhitespace)+1;
1620 if (strcmp(target,
"xml") == 0)
1622 xml=strstr(xml,
"standalone");
1623 if ((xml != (
char *) NULL) &&
1624 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1625 root->standalone=MagickTrue;
1628 if (root->processing_instructions[0] == (
char **) NULL)
1630 root->processing_instructions=(
char ***) AcquireMagickMemory(
sizeof(
1631 *root->processing_instructions));
1632 if (root->processing_instructions ==(
char ***) NULL)
1633 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1634 *root->processing_instructions=(
char **) NULL;
1637 while ((root->processing_instructions[i] != (
char **) NULL) &&
1638 (strcmp(target,root->processing_instructions[i][0]) != 0))
1640 if (root->processing_instructions[i] == (
char **) NULL)
1642 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1643 root->processing_instructions,(
size_t) (i+2),
1644 sizeof(*root->processing_instructions));
1645 if (root->processing_instructions == (
char ***) NULL)
1646 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1647 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1648 sizeof(**root->processing_instructions));
1649 if (root->processing_instructions[i] == (
char **) NULL)
1650 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1651 root->processing_instructions[i+1]=(
char **) NULL;
1652 root->processing_instructions[i][0]=ConstantString(target);
1653 root->processing_instructions[i][1]=(
char *)
1654 root->processing_instructions[i+1];
1655 root->processing_instructions[i+1]=(
char **) NULL;
1656 root->processing_instructions[i][2]=ConstantString(
"");
1659 while (root->processing_instructions[i][j] != (
char *) NULL)
1661 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1662 root->processing_instructions[i],(
size_t) (j+3),
1663 sizeof(**root->processing_instructions));
1664 if (root->processing_instructions[i] == (
char **) NULL)
1665 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1666 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1667 root->processing_instructions[i][j+1],(
size_t) (j+1),
1668 sizeof(***root->processing_instructions));
1669 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1670 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1671 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1672 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1673 root->processing_instructions[i][j]=ConstantString(xml);
1674 root->processing_instructions[i][j+1]=(
char *) NULL;
1677static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1684 **predefined_entities,
1694 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1695 if (predefined_entities == (
char **) NULL)
1696 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1697 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1698 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1700 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1704 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1709 if (strspn(xml+8,XMLWhitespace) == 0)
1711 xml+=strspn(xml+8,XMLWhitespace)+8;
1713 n=xml+strspn(xml,XMLWhitespace
"%");
1714 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1716 xml=n+strcspn(n,XMLWhitespace);
1720 v=xml+strspn(xml+1,XMLWhitespace)+1;
1723 if ((q !=
'"') && (q !=
'\''))
1728 xml=strchr(xml,
'>');
1731 entities=(*c ==
'%') ? predefined_entities : root->entities;
1732 for (i=0; entities[i] != (
char *) NULL; i++) ;
1733 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1735 if (entities == (
char **) NULL)
1736 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1738 predefined_entities=entities;
1740 root->entities=entities;
1744 if (xml != (
char *) NULL)
1749 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1750 entities[i+2]=(
char *) NULL;
1751 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1755 if (entities[i+1] != v)
1756 entities[i+1]=DestroyString(entities[i+1]);
1757 (void) ThrowMagickException(exception,GetMagickModule(),
1758 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1759 predefined_entities=(
char **) RelinquishMagickMemory(
1760 predefined_entities);
1761 return(MagickFalse);
1765 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1770 t=xml+strspn(xml+9,XMLWhitespace)+9;
1773 (void) ThrowMagickException(exception,GetMagickModule(),
1774 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1775 predefined_entities=(
char **) RelinquishMagickMemory(
1776 predefined_entities);
1777 return(MagickFalse);
1779 xml=t+strcspn(t,XMLWhitespace
">");
1784 while ((root->attributes[i] != (
char **) NULL) &&
1785 (n != (
char *) NULL) &&
1786 (strcmp(n,root->attributes[i][0]) != 0))
1788 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1791 xml=n+strcspn(n,XMLWhitespace);
1796 (void) ThrowMagickException(exception,GetMagickModule(),
1797 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1798 predefined_entities=(
char **) RelinquishMagickMemory(
1799 predefined_entities);
1800 return(MagickFalse);
1802 xml+=strspn(xml+1,XMLWhitespace)+1;
1803 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1804 if (strncmp(xml,
"NOTATION",8) == 0)
1805 xml+=strspn(xml+8,XMLWhitespace)+8;
1806 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1807 strcspn(xml,XMLWhitespace);
1808 if (xml == (
char *) NULL)
1810 (void) ThrowMagickException(exception,GetMagickModule(),
1811 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1812 predefined_entities=(
char **) RelinquishMagickMemory(
1813 predefined_entities);
1814 return(MagickFalse);
1816 xml+=strspn(xml,XMLWhitespace
")");
1817 if (strncmp(xml,
"#FIXED",6) == 0)
1818 xml+=strspn(xml+6,XMLWhitespace)+6;
1821 xml+=strcspn(xml,XMLWhitespace
">")-1;
1827 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1828 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1832 (void) ThrowMagickException(exception,GetMagickModule(),
1833 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1834 predefined_entities=(
char **) RelinquishMagickMemory(
1835 predefined_entities);
1836 return(MagickFalse);
1838 if (root->attributes[i] == (
char **) NULL)
1844 root->attributes=(
char ***) AcquireQuantumMemory(2,
1845 sizeof(*root->attributes));
1847 root->attributes=(
char ***) ResizeQuantumMemory(
1848 root->attributes,(
size_t) (i+2),
1849 sizeof(*root->attributes));
1850 if (root->attributes == (
char ***) NULL)
1851 ThrowFatalException(ResourceLimitFatalError,
1852 "MemoryAllocationFailed");
1853 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1854 sizeof(**root->attributes));
1855 if (root->attributes[i] == (
char **) NULL)
1856 ThrowFatalException(ResourceLimitFatalError,
1857 "MemoryAllocationFailed");
1858 root->attributes[i][0]=ConstantString(t);
1859 root->attributes[i][1]=(
char *) NULL;
1860 root->attributes[i+1]=(
char **) NULL;
1862 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1863 root->attributes[i]=(
char **) ResizeQuantumMemory(
1864 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1865 if (root->attributes[i] == (
char **) NULL)
1866 ThrowFatalException(ResourceLimitFatalError,
1867 "MemoryAllocationFailed");
1868 root->attributes[i][j+3]=(
char *) NULL;
1869 root->attributes[i][j+2]=ConstantString(c);
1870 root->attributes[i][j+1]=(
char *) NULL;
1871 if (v != (
char *) NULL)
1872 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1873 root->attributes[i][j]=ConstantString(n);
1877 if (strncmp(xml,
"<!--", 4) == 0)
1878 xml=strstr(xml+4,
"-->");
1880 if (strncmp(xml,
"<?", 2) == 0)
1884 if (xml != (
char *) NULL)
1886 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1892 xml=strchr(xml,
'>');
1894 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1897 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1901static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1906 xml_info=root->node;
1907 if (xml_info->tag == (
char *) NULL)
1908 xml_info->tag=ConstantString(tag);
1910 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1912 xml_info->attributes=attributes;
1913 root->node=xml_info;
1924static inline MagickBooleanType IsSkipTag(
const char *tag)
1930 while (skip_tags[i] != (
const char *) NULL)
1932 if (LocaleCompare(tag,skip_tags[i]) == 0)
1936 return(MagickFalse);
1970 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1972 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1973 "ParseError",
"root tag missing");
1976 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1978 utf8=ConvertUTF16ToUTF8(xml,&length);
1979 if (utf8 == (
char *) NULL)
1981 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1982 "ParseError",
"UTF16 to UTF8 failed");
1985 terminal=utf8[length-1];
1986 utf8[length-1]=
'\0';
1988 while ((*p !=
'\0') && (*p !=
'<'))
1992 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1993 "ParseError",
"root tag missing");
1994 utf8=DestroyString(utf8);
1997 attribute=(
char **) NULL;
2002 attributes=(
char **) sentinel;
2005 if ((isalpha((
int) ((
unsigned char) *p)) !=0) || (*p ==
'_') ||
2006 (*p ==
':') || (c <
'\0'))
2013 (void) ThrowMagickException(exception,GetMagickModule(),
2014 OptionWarning,
"ParseError",
"root tag missing");
2015 utf8=DestroyString(utf8);
2016 return(&root->root);
2018 p+=strcspn(p,XMLWhitespace
"/>");
2019 while (isspace((
int) ((
unsigned char) *p)) != 0)
2021 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
2022 (ignore_depth == 0))
2024 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2030 while ((root->attributes[i] != (
char **) NULL) &&
2031 (strcmp(root->attributes[i][0],tag) != 0))
2033 attribute=root->attributes[i];
2035 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
2041 attributes=(
char **) AcquireQuantumMemory(4,
2042 sizeof(*attributes));
2044 attributes=(
char **) ResizeQuantumMemory(attributes,
2045 (
size_t) (l+4),
sizeof(*attributes));
2046 if (attributes == (
char **) NULL)
2048 (void) ThrowMagickException(exception,GetMagickModule(),
2049 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2050 utf8=DestroyString(utf8);
2051 return(&root->root);
2053 attributes[l+2]=(
char *) NULL;
2054 attributes[l+1]=(
char *) NULL;
2056 p+=strcspn(p,XMLWhitespace
"=/>");
2057 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2058 attributes[l]=ConstantString(
"");
2062 p+=strspn(p,XMLWhitespace
"=");
2064 if ((c ==
'"') || (c ==
'\''))
2071 while ((*p !=
'\0') && (*p != c))
2077 attributes[l]=ConstantString(
"");
2078 attributes[l+1]=ConstantString(
"");
2079 (void) DestroyXMLTreeAttributes(attributes);
2080 (void) ThrowMagickException(exception,
2081 GetMagickModule(),OptionWarning,
"ParseError",
2083 utf8=DestroyString(utf8);
2084 return(&root->root);
2087 while ((attribute != (
char **) NULL) &&
2088 (attribute[j] != (
char *) NULL) &&
2089 (strcmp(attribute[j],attributes[l]) != 0))
2091 attributes[l+1]=ParseEntities(attributes[l+1],
2092 root->entities,(attribute != (
char **) NULL) &&
2093 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2096 attributes[l]=ConstantString(attributes[l]);
2098 while (isspace((
int) ((
unsigned char) *p)) != 0)
2104 while((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2113 if (((*p !=
'\0') && (*p !=
'>')) ||
2114 ((*p ==
'\0') && (terminal !=
'>')))
2117 (void) DestroyXMLTreeAttributes(attributes);
2118 (void) ThrowMagickException(exception,GetMagickModule(),
2119 OptionWarning,
"ParseError",
"missing >");
2120 utf8=DestroyString(utf8);
2121 return(&root->root);
2123 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2124 (void) DestroyXMLTreeAttributes(attributes);
2127 ParseOpenTag(root,tag,attributes);
2128 (void) ParseCloseTag(root,tag,exception);
2134 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2137 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2138 ParseOpenTag(root,tag,attributes);
2142 (void) DestroyXMLTreeAttributes(attributes);
2149 (void) DestroyXMLTreeAttributes(attributes);
2150 (void) ThrowMagickException(exception,GetMagickModule(),
2151 OptionWarning,
"ParseError",
"missing >");
2152 utf8=DestroyString(utf8);
2153 return(&root->root);
2164 p+=strcspn(tag,XMLWhitespace
">")+1;
2166 if ((c ==
'\0') && (terminal !=
'>'))
2168 (void) ThrowMagickException(exception,GetMagickModule(),
2169 OptionWarning,
"ParseError",
"missing >");
2170 utf8=DestroyString(utf8);
2171 return(&root->root);
2174 if ((ignore_depth == 0) &&
2175 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2177 utf8=DestroyString(utf8);
2178 return(&root->root);
2180 if (ignore_depth > 0)
2183 if (isspace((
int) ((
unsigned char) *p)) != 0)
2184 p+=strspn(p,XMLWhitespace);
2187 if (strncmp(p,
"!--",3) == 0)
2193 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2194 ((*p ==
'\0') && (terminal !=
'>')))
2196 (void) ThrowMagickException(exception,GetMagickModule(),
2197 OptionWarning,
"ParseError",
"unclosed <!--");
2198 utf8=DestroyString(utf8);
2199 return(&root->root);
2203 if (strncmp(p,
"![CDATA[",8) == 0)
2209 if (p != (
char *) NULL)
2212 if (ignore_depth == 0)
2213 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2217 (void) ThrowMagickException(exception,GetMagickModule(),
2218 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2219 utf8=DestroyString(utf8);
2220 return(&root->root);
2224 if (strncmp(p,
"!DOCTYPE",8) == 0)
2229 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2230 ((l != 0) && ((*p !=
']') ||
2231 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2232 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2233 p+=strcspn(p+1,
"[]>")+1;
2234 if ((*p ==
'\0') && (terminal !=
'>'))
2236 (void) ThrowMagickException(exception,GetMagickModule(),
2237 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2238 utf8=DestroyString(utf8);
2239 return(&root->root);
2242 tag=strchr(tag,
'[')+1;
2245 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2247 if (status == MagickFalse)
2249 utf8=DestroyString(utf8);
2250 return(&root->root);
2264 if (p == (
char *) NULL)
2267 }
while ((*p !=
'\0') && (*p !=
'>'));
2268 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2271 (void) ThrowMagickException(exception,GetMagickModule(),
2272 OptionWarning,
"ParseError",
"unclosed <?");
2273 utf8=DestroyString(utf8);
2274 return(&root->root);
2276 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2280 (void) ThrowMagickException(exception,GetMagickModule(),
2281 OptionWarning,
"ParseError",
"unexpected <");
2282 utf8=DestroyString(utf8);
2283 return(&root->root);
2285 if ((p == (
char *) NULL) || (*p ==
'\0'))
2289 if ((*p !=
'\0') && (*p !=
'<'))
2294 while ((*p !=
'\0') && (*p !=
'<'))
2298 if (ignore_depth == 0)
2299 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2305 utf8=DestroyString(utf8);
2307 return(&root->root);
2308 if (root->node->tag == (
char *) NULL)
2310 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2311 "ParseError",
"root tag missing");
2312 return(&root->root);
2314 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2315 "ParseError",
"unclosed tag: `%s'",root->node->tag);
2316 return(&root->root);
2341MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2344 *predefined_entities[NumberPredefinedEntities+1] =
2346 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2347 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2353 root=(
XMLTreeRoot *) AcquireMagickMemory(
sizeof(*root));
2356 (void) memset(root,0,
sizeof(*root));
2357 root->root.tag=(
char *) NULL;
2358 if (tag != (
char *) NULL)
2359 root->root.tag=ConstantString(tag);
2360 root->node=(&root->root);
2361 root->root.content=ConstantString(
"");
2362 root->entities=(
char **) AcquireMagickMemory(
sizeof(predefined_entities));
2363 if (root->entities == (
char **) NULL)
2365 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2366 root->root.attributes=sentinel;
2367 root->attributes=(
char ***) root->root.attributes;
2368 root->processing_instructions=(
char ***) root->root.attributes;
2369 root->debug=IsEventLogging();
2370 root->signature=MagickCoreSignature;
2371 return(&root->root);
2403 assert((xml_info->signature == MagickCoreSignature) ||
2404 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2405 if (IsEventLogging() != MagickFalse)
2406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2408 xml_info->next->sibling=xml_info->sibling;
2411 node=xml_info->parent->child;
2412 if (node == xml_info)
2413 xml_info->parent->child=xml_info->ordered;
2416 while (node->ordered != xml_info)
2418 node->ordered=node->ordered->ordered;
2419 node=xml_info->parent->child;
2420 if (strcmp(node->tag,xml_info->tag) != 0)
2422 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2424 if (node->sibling != xml_info)
2427 node->sibling=(xml_info->next != (
XMLTreeInfo *) NULL) ?
2428 xml_info->next : node->sibling->sibling;
2431 (node->next != xml_info))
2434 node->next=node->next->next;
2472 const char *tag,
const char *value)
2479 assert((xml_info->signature == MagickCoreSignature) ||
2480 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2481 if (IsEventLogging() != MagickFalse)
2482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2484 while ((xml_info->attributes[i] != (
char *) NULL) &&
2485 (strcmp(xml_info->attributes[i],tag) != 0))
2487 if (xml_info->attributes[i] == (
char *) NULL)
2492 if (value == (
const char *) NULL)
2494 if (xml_info->attributes != sentinel)
2495 xml_info->attributes=(
char **) ResizeQuantumMemory(
2496 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2499 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2500 sizeof(*xml_info->attributes));
2501 if (xml_info->attributes != (
char **) NULL)
2502 xml_info->attributes[1]=ConstantString(
"");
2504 if (xml_info->attributes == (
char **) NULL)
2505 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2506 xml_info->attributes[i]=ConstantString(tag);
2507 xml_info->attributes[i+2]=(
char *) NULL;
2508 (void) strlen(xml_info->attributes[i+1]);
2513 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2514 if (xml_info->attributes[i+1] != (
char *) NULL)
2515 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2516 if (value != (
const char *) NULL)
2518 xml_info->attributes[i+1]=ConstantString(value);
2521 if (xml_info->attributes[i] != (
char *) NULL)
2522 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2523 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,
2524 (
size_t) (j-i)*
sizeof(*xml_info->attributes));
2525 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2526 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2527 if (xml_info->attributes == (
char **) NULL)
2528 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2530 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2531 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2562 const char *content)
2565 assert((xml_info->signature == MagickCoreSignature) ||
2566 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2567 if (IsEventLogging() != MagickFalse)
2568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2569 if (xml_info->content != (
char *) NULL)
2570 xml_info->content=DestroyString(xml_info->content);
2571 xml_info->content=(
char *) ConstantString(content);
2598static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2599 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2605 canonical_content=CanonicalXMLContent(source,pedantic);
2611 content=AcquireString(source);
2612 content[offset]=
'\0';
2613 canonical_content=CanonicalXMLContent(content,pedantic);
2614 content=DestroyString(content);
2616 if (canonical_content == (
char *) NULL)
2617 return(*destination);
2618 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2620 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2621 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2622 sizeof(**destination));
2623 if (*destination == (
char *) NULL)
2624 return(*destination);
2626 *length+=FormatLocaleString(*destination+(*length),*extent,
"%s",
2628 canonical_content=DestroyString(canonical_content);
2629 return(*destination);
2632static char *XMLTreeTagToXML(
XMLTreeInfo *xml_info,
char **source,
size_t *length,
2633 size_t *extent,
size_t start,
char ***attributes)
2648 content=(
char *)
"";
2650 content=xml_info->parent->content;
2652 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2653 start),source,length,extent,MagickFalse);
2654 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2656 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2657 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2658 if (*source == (
char *) NULL)
2661 *length+=FormatLocaleString(*source+(*length),*extent,
"<%s",xml_info->tag);
2662 for (i=0; xml_info->attributes[i]; i+=2)
2664 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2665 if (attribute != xml_info->attributes[i+1])
2667 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2669 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2670 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2671 if (*source == (
char *) NULL)
2672 return((
char *) NULL);
2674 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2675 xml_info->attributes[i]);
2676 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2678 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2681 while ((attributes[i] != (
char **) NULL) &&
2682 (strcmp(attributes[i][0],xml_info->tag) != 0))
2685 while ((attributes[i] != (
char **) NULL) &&
2686 (attributes[i][j] != (
char *) NULL))
2688 if ((attributes[i][j+1] == (
char *) NULL) ||
2689 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2694 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2696 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2697 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2698 if (*source == (
char *) NULL)
2699 return((
char *) NULL);
2701 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2703 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2705 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2708 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2711 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2713 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2715 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2717 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2718 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2719 if (*source == (
char *) NULL)
2720 return((
char *) NULL);
2722 if (*xml_info->content !=
'\0')
2723 *length+=FormatLocaleString(*source+(*length),*extent,
"</%s>",
2725 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2728 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2731 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2736MagickExport
char *XMLTreeInfoToXML(
XMLTreeInfo *xml_info)
2760 assert((xml_info->signature == MagickCoreSignature) ||
2761 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2762 if (IsEventLogging() != MagickFalse)
2763 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2764 if (xml_info->tag == (
char *) NULL)
2765 return((
char *) NULL);
2766 xml=AcquireString((
char *) NULL);
2768 extent=MaxTextExtent;
2772 parent=xml_info->parent;
2774 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2779 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2780 p=root->processing_instructions[i][1];
2781 for (j=1; p != (
char *) NULL; j++)
2783 if (root->processing_instructions[i][k][j-1] ==
'>')
2785 p=root->processing_instructions[i][j];
2788 q=root->processing_instructions[i][0];
2789 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2791 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2792 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2793 if (xml == (
char *) NULL)
2796 length+=FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2797 *p !=
'\0' ?
" " :
"",p);
2798 p=root->processing_instructions[i][j];
2801 ordered=xml_info->ordered;
2804 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2805 xml_info->parent=parent;
2806 xml_info->ordered=ordered;
2808 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2813 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2814 p=root->processing_instructions[i][1];
2815 for (j=1; p != (
char *) NULL; j++)
2817 if (root->processing_instructions[i][k][j-1] ==
'<')
2819 p=root->processing_instructions[i][j];
2822 q=root->processing_instructions[i][0];
2823 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2825 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2826 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2827 if (xml == (
char *) NULL)
2830 length+=FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2831 *p !=
'\0' ?
" " :
"",p);
2832 p=root->processing_instructions[i][j];
2835 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));