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++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1288 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1293 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1294 if (utf8 != (
char *) NULL)
1299static char *ParseEntities(
char *xml,
char **entities,
int state)
1323 for ( ; *xml !=
'\0'; xml++)
1324 while (*xml ==
'\r')
1328 (void) memmove(xml,xml+1,strlen(xml));
1332 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1333 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1345 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1351 c=strtol(xml+2,&entity,10);
1353 c=strtol(xml+3,&entity,16);
1354 if ((c == 0) || (*entity !=
';'))
1370 for (i=0;
byte != 0;
byte/=2)
1373 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1378 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1382 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1385 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1386 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1392 while ((entities[i] != (
char *) NULL) &&
1393 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1395 if (entities[i++] == (
char *) NULL)
1398 if (entities[i] != (
char *) NULL)
1403 length=strlen(entities[i]);
1404 entity=strchr(xml,
';');
1405 if ((entity != (
char *) NULL) &&
1406 ((length-1L) >= (
size_t) (entity-xml)))
1408 offset=(ssize_t) (xml-p);
1409 extent=((size_t) offset+length+strlen(entity));
1412 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1420 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1421 sizeof(*extent_xml));
1422 if (extent_xml != (
char *) NULL)
1424 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1425 (void) CopyMagickString(extent_xml,p,extent*
1426 sizeof(*extent_xml));
1430 if (p == (
char *) NULL)
1431 ThrowFatalException(ResourceLimitFatalError,
1432 "MemoryAllocationFailed");
1434 entity=strchr(xml,
';');
1436 if (entity != (
char *) NULL)
1437 (void) memmove(xml+length,entity+1,strlen(entity));
1438 (void) memcpy(xml,entities[i],length);
1442 if (((state ==
' ') || (state ==
'*')) &&
1443 (isspace((
int) ((
unsigned char) *xml)) != 0))
1453 for (xml=p; *xml !=
'\0'; xml++)
1458 i=(ssize_t) strspn(xml,accept);
1460 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1461 while ((*xml !=
'\0') && (*xml !=
' '))
1467 if ((xml >= p) && (*xml ==
' '))
1470 return(p == q ? ConstantString(p) : p);
1473static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1474 const size_t length,
const char state)
1479 xml_info=root->node;
1480 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1484 xml=ParseEntities(xml,root->entities,state);
1485 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1487 (void) ConcatenateString(&xml_info->content,xml);
1488 xml=DestroyString(xml);
1492 if (xml_info->content != (
char *) NULL)
1493 xml_info->content=DestroyString(xml_info->content);
1494 xml_info->content=xml;
1502 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1504 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1505 "ParseError",
"unexpected closing tag </%s>",tag);
1506 return(&root->root);
1508 root->node=root->node->parent;
1512static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1513 const size_t depth,
char **entities)
1521 if (depth > MagickMaxRecursionDepth)
1522 return(MagickFalse);
1525 while ((*xml !=
'\0') && (*xml !=
'&'))
1529 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1530 return(MagickFalse);
1532 while ((entities[i] != (
char *) NULL) &&
1533 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1535 if ((entities[i] != (
char *) NULL) &&
1536 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1537 return(MagickFalse);
1541static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1553 xml+=strcspn(xml,XMLWhitespace);
1557 xml+=strspn(xml+1,XMLWhitespace)+1;
1559 if (strcmp(target,
"xml") == 0)
1561 xml=strstr(xml,
"standalone");
1562 if ((xml != (
char *) NULL) &&
1563 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1564 root->standalone=MagickTrue;
1567 if (root->processing_instructions[0] == (
char **) NULL)
1569 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1570 *root->processing_instructions));
1571 *root->processing_instructions=(
char **) NULL;
1574 while ((root->processing_instructions[i] != (
char **) NULL) &&
1575 (strcmp(target,root->processing_instructions[i][0]) != 0))
1577 if (root->processing_instructions[i] == (
char **) NULL)
1579 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1580 root->processing_instructions,(
size_t) (i+2),
1581 sizeof(*root->processing_instructions));
1582 if (root->processing_instructions == (
char ***) NULL)
1583 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1584 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1585 sizeof(**root->processing_instructions));
1586 if (root->processing_instructions[i] == (
char **) NULL)
1587 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1588 root->processing_instructions[i+1]=(
char **) NULL;
1589 root->processing_instructions[i][0]=ConstantString(target);
1590 root->processing_instructions[i][1]=(
char *)
1591 root->processing_instructions[i+1];
1592 root->processing_instructions[i+1]=(
char **) NULL;
1593 root->processing_instructions[i][2]=ConstantString(
"");
1596 while (root->processing_instructions[i][j] != (
char *) NULL)
1598 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1599 root->processing_instructions[i],(
size_t) (j+3),
1600 sizeof(**root->processing_instructions));
1601 if (root->processing_instructions[i] == (
char **) NULL)
1602 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1603 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1604 root->processing_instructions[i][j+1],(
size_t) (j+1),
1605 sizeof(***root->processing_instructions));
1606 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1607 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1608 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1609 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1610 root->processing_instructions[i][j]=ConstantString(xml);
1611 root->processing_instructions[i][j+1]=(
char *) NULL;
1614static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1621 **predefined_entities,
1631 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1632 if (predefined_entities == (
char **) NULL)
1633 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1634 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1635 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1637 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1641 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1646 if (strspn(xml+8,XMLWhitespace) == 0)
1648 xml+=strspn(xml+8,XMLWhitespace)+8;
1650 n=xml+strspn(xml,XMLWhitespace
"%");
1651 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1653 xml=n+strcspn(n,XMLWhitespace);
1657 v=xml+strspn(xml+1,XMLWhitespace)+1;
1660 if ((q !=
'"') && (q !=
'\''))
1665 xml=strchr(xml,
'>');
1668 entities=(*c ==
'%') ? predefined_entities : root->entities;
1669 for (i=0; entities[i] != (
char *) NULL; i++) ;
1670 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1672 if (entities == (
char **) NULL)
1673 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1675 predefined_entities=entities;
1677 root->entities=entities;
1681 if (xml != (
char *) NULL)
1686 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1687 entities[i+2]=(
char *) NULL;
1688 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1692 if (entities[i+1] != v)
1693 entities[i+1]=DestroyString(entities[i+1]);
1694 (void) ThrowMagickException(exception,GetMagickModule(),
1695 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1696 predefined_entities=(
char **) RelinquishMagickMemory(
1697 predefined_entities);
1698 return(MagickFalse);
1702 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1707 t=xml+strspn(xml+9,XMLWhitespace)+9;
1710 (void) ThrowMagickException(exception,GetMagickModule(),
1711 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1712 predefined_entities=(
char **) RelinquishMagickMemory(
1713 predefined_entities);
1714 return(MagickFalse);
1716 xml=t+strcspn(t,XMLWhitespace
">");
1721 while ((root->attributes[i] != (
char **) NULL) &&
1722 (n != (
char *) NULL) &&
1723 (strcmp(n,root->attributes[i][0]) != 0))
1725 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1728 xml=n+strcspn(n,XMLWhitespace);
1733 (void) ThrowMagickException(exception,GetMagickModule(),
1734 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1735 predefined_entities=(
char **) RelinquishMagickMemory(
1736 predefined_entities);
1737 return(MagickFalse);
1739 xml+=strspn(xml+1,XMLWhitespace)+1;
1740 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1741 if (strncmp(xml,
"NOTATION",8) == 0)
1742 xml+=strspn(xml+8,XMLWhitespace)+8;
1743 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1744 strcspn(xml,XMLWhitespace);
1745 if (xml == (
char *) NULL)
1747 (void) ThrowMagickException(exception,GetMagickModule(),
1748 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1749 predefined_entities=(
char **) RelinquishMagickMemory(
1750 predefined_entities);
1751 return(MagickFalse);
1753 xml+=strspn(xml,XMLWhitespace
")");
1754 if (strncmp(xml,
"#FIXED",6) == 0)
1755 xml+=strspn(xml+6,XMLWhitespace)+6;
1758 xml+=strcspn(xml,XMLWhitespace
">")-1;
1764 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1765 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1769 (void) ThrowMagickException(exception,GetMagickModule(),
1770 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1771 predefined_entities=(
char **) RelinquishMagickMemory(
1772 predefined_entities);
1773 return(MagickFalse);
1775 if (root->attributes[i] == (
char **) NULL)
1781 root->attributes=(
char ***) AcquireQuantumMemory(2,
1782 sizeof(*root->attributes));
1784 root->attributes=(
char ***) ResizeQuantumMemory(
1785 root->attributes,(
size_t) (i+2),
1786 sizeof(*root->attributes));
1787 if (root->attributes == (
char ***) NULL)
1788 ThrowFatalException(ResourceLimitFatalError,
1789 "MemoryAllocationFailed");
1790 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1791 sizeof(**root->attributes));
1792 if (root->attributes[i] == (
char **) NULL)
1793 ThrowFatalException(ResourceLimitFatalError,
1794 "MemoryAllocationFailed");
1795 root->attributes[i][0]=ConstantString(t);
1796 root->attributes[i][1]=(
char *) NULL;
1797 root->attributes[i+1]=(
char **) NULL;
1799 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1800 root->attributes[i]=(
char **) ResizeQuantumMemory(
1801 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1802 if (root->attributes[i] == (
char **) NULL)
1803 ThrowFatalException(ResourceLimitFatalError,
1804 "MemoryAllocationFailed");
1805 root->attributes[i][j+3]=(
char *) NULL;
1806 root->attributes[i][j+2]=ConstantString(c);
1807 root->attributes[i][j+1]=(
char *) NULL;
1808 if (v != (
char *) NULL)
1809 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1810 root->attributes[i][j]=ConstantString(n);
1814 if (strncmp(xml,
"<!--", 4) == 0)
1815 xml=strstr(xml+4,
"-->");
1817 if (strncmp(xml,
"<?", 2) == 0)
1821 if (xml != (
char *) NULL)
1823 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1829 xml=strchr(xml,
'>');
1831 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1834 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1838static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1843 xml_info=root->node;
1844 if (xml_info->tag == (
char *) NULL)
1845 xml_info->tag=ConstantString(tag);
1847 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1849 xml_info->attributes=attributes;
1850 root->node=xml_info;
1861static inline MagickBooleanType IsSkipTag(
const char *tag)
1867 while (ignore_tags[i] != (
const char *) NULL)
1869 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1873 return(MagickFalse);
1907 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1909 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1910 "ParseError",
"root tag missing");
1913 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1915 utf8=ConvertUTF16ToUTF8(xml,&length);
1916 if (utf8 == (
char *) NULL)
1918 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1919 "ParseError",
"UTF16 to UTF8 failed");
1924 utf8=DestroyString(utf8);
1925 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1926 "ParseError",
"root tag missing");
1929 terminal=utf8[length-1];
1930 utf8[length-1]=
'\0';
1932 while ((*p !=
'\0') && (*p !=
'<'))
1936 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1937 "ParseError",
"root tag missing");
1938 utf8=DestroyString(utf8);
1941 attribute=(
char **) NULL;
1946 attributes=(
char **) sentinel;
1949 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1950 (*p ==
':') || (c <
'\0'))
1957 (void) ThrowMagickException(exception,GetMagickModule(),
1958 OptionWarning,
"ParseError",
"root tag missing");
1959 utf8=DestroyString(utf8);
1960 return(&root->root);
1962 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1963 while (isspace((
int) ((
unsigned char) *p)) != 0)
1965 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1966 (ignore_depth == 0))
1968 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1974 while ((root->attributes[i] != (
char **) NULL) &&
1975 (strcmp(root->attributes[i][0],tag) != 0))
1977 attribute=root->attributes[i];
1979 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1985 attributes=(
char **) AcquireQuantumMemory(4,
1986 sizeof(*attributes));
1988 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1989 (l+4),
sizeof(*attributes));
1990 if (attributes == (
char **) NULL)
1992 (void) ThrowMagickException(exception,GetMagickModule(),
1993 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1994 utf8=DestroyString(utf8);
1995 return(&root->root);
1997 attributes[l+2]=(
char *) NULL;
1998 attributes[l+1]=(
char *) NULL;
2000 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
2001 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2002 attributes[l]=ConstantString(
"");
2006 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2008 if ((c ==
'"') || (c ==
'\''))
2015 while ((*p !=
'\0') && (*p != c))
2021 attributes[l]=ConstantString(
"");
2022 attributes[l+1]=ConstantString(
"");
2023 (void) DestroyXMLTreeAttributes(attributes);
2024 (void) ThrowMagickException(exception,
2025 GetMagickModule(),OptionWarning,
"ParseError",
2027 utf8=DestroyString(utf8);
2028 return(&root->root);
2031 while ((attribute != (
char **) NULL) &&
2032 (attribute[j] != (
char *) NULL) &&
2033 (strcmp(attribute[j],attributes[l]) != 0))
2035 attributes[l+1]=ParseEntities(attributes[l+1],
2036 root->entities,(attribute != (
char **) NULL) &&
2037 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2040 attributes[l]=ConstantString(attributes[l]);
2042 while (isspace((
int) ((
unsigned char) *p)) != 0)
2048 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2057 if (((*p !=
'\0') && (*p !=
'>')) ||
2058 ((*p ==
'\0') && (terminal !=
'>')))
2061 (void) DestroyXMLTreeAttributes(attributes);
2062 (void) ThrowMagickException(exception,GetMagickModule(),
2063 OptionWarning,
"ParseError",
"missing >");
2064 utf8=DestroyString(utf8);
2065 return(&root->root);
2067 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2068 (void) DestroyXMLTreeAttributes(attributes);
2071 ParseOpenTag(root,tag,attributes);
2072 (void) ParseCloseTag(root,tag,exception);
2078 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2081 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2082 ParseOpenTag(root,tag,attributes);
2086 (void) DestroyXMLTreeAttributes(attributes);
2093 (void) DestroyXMLTreeAttributes(attributes);
2094 (void) ThrowMagickException(exception,GetMagickModule(),
2095 OptionWarning,
"ParseError",
"missing >");
2096 utf8=DestroyString(utf8);
2097 return(&root->root);
2108 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2110 if ((c ==
'\0') && (terminal !=
'>'))
2112 (void) ThrowMagickException(exception,GetMagickModule(),
2113 OptionWarning,
"ParseError",
"missing >");
2114 utf8=DestroyString(utf8);
2115 return(&root->root);
2118 if ((ignore_depth == 0) &&
2119 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2121 utf8=DestroyString(utf8);
2122 return(&root->root);
2124 if (ignore_depth > 0)
2127 if (isspace((
int) ((
unsigned char) *p)) != 0)
2128 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2131 if (strncmp(p,
"!--",3) == 0)
2137 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2138 ((*p ==
'\0') && (terminal !=
'>')))
2140 (void) ThrowMagickException(exception,GetMagickModule(),
2141 OptionWarning,
"ParseError",
"unclosed <!--");
2142 utf8=DestroyString(utf8);
2143 return(&root->root);
2147 if (strncmp(p,
"![CDATA[",8) == 0)
2153 if (p != (
char *) NULL)
2156 if (ignore_depth == 0)
2157 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2161 (void) ThrowMagickException(exception,GetMagickModule(),
2162 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2163 utf8=DestroyString(utf8);
2164 return(&root->root);
2168 if (strncmp(p,
"!DOCTYPE",8) == 0)
2173 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2174 ((l != 0) && ((*p !=
']') ||
2175 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2176 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2177 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2178 if ((*p ==
'\0') && (terminal !=
'>'))
2180 (void) ThrowMagickException(exception,GetMagickModule(),
2181 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2182 utf8=DestroyString(utf8);
2183 return(&root->root);
2186 tag=strchr(tag,
'[')+1;
2189 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2191 if (status == MagickFalse)
2193 utf8=DestroyString(utf8);
2194 return(&root->root);
2208 if (p == (
char *) NULL)
2211 }
while ((*p !=
'\0') && (*p !=
'>'));
2212 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2215 (void) ThrowMagickException(exception,GetMagickModule(),
2216 OptionWarning,
"ParseError",
"unclosed <?");
2217 utf8=DestroyString(utf8);
2218 return(&root->root);
2220 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2224 (void) ThrowMagickException(exception,GetMagickModule(),
2225 OptionWarning,
"ParseError",
"unexpected <");
2226 utf8=DestroyString(utf8);
2227 return(&root->root);
2229 if ((p == (
char *) NULL) || (*p ==
'\0'))
2233 if ((*p !=
'\0') && (*p !=
'<'))
2238 while ((*p !=
'\0') && (*p !=
'<'))
2242 if (ignore_depth == 0)
2243 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2249 utf8=DestroyString(utf8);
2251 return(&root->root);
2252 if (root->node->tag == (
char *) NULL)
2254 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2255 "ParseError",
"root tag missing");
2256 return(&root->root);
2258 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2259 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2260 return(&root->root);
2285MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2288 *predefined_entities[NumberPredefinedEntities+1] =
2290 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2291 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2297 root=(
XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2298 (void) memset(root,0,
sizeof(*root));
2299 root->root.tag=(
char *) NULL;
2300 if (tag != (
char *) NULL)
2301 root->root.tag=ConstantString(tag);
2302 root->node=(&root->root);
2303 root->root.content=ConstantString(
"");
2304 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
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)));