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],
236 assert((xml_info->signature == MagickCoreSignature) ||
237 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
238 if (IsEventLogging() != MagickFalse)
239 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
241 components=GetPathComponents(path,&number_components);
242 if (components == (
char **) NULL)
244 for (i=0; i < (ssize_t) number_components; i++)
246 GetPathComponent(components[i],SubimagePath,subnode);
247 GetPathComponent(components[i],CanonicalPath,tag);
248 child=GetXMLTreeChild(node,tag);
250 child=AddChildToXMLTree(node,tag,offset);
254 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
256 node=GetXMLTreeOrdered(node);
262 components[i]=DestroyString(components[i]);
264 for ( ; i < (ssize_t) number_components; i++)
265 components[i]=DestroyString(components[i]);
266 components=(
char **) RelinquishMagickMemory(components);
298 MagickExport
char *CanonicalXMLContent(
const char *content,
299 const MagickBooleanType pedantic)
318 utf8=ConvertLatin1ToUTF8((
const unsigned char *) content);
319 if (utf8 == (
unsigned char *) NULL)
320 return((
char *) NULL);
321 for (p=utf8; *p !=
'\0'; p++)
322 if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
329 base64=Base64Encode(utf8,strlen((
char *) utf8),&length);
330 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
331 if (base64 == (
char *) NULL)
332 return((
char *) NULL);
333 canonical_content=AcquireString(
"<base64>");
334 (void) ConcatenateString(&canonical_content,base64);
335 base64=DestroyString(base64);
336 (void) ConcatenateString(&canonical_content,
"</base64>");
337 return(canonical_content);
343 canonical_content=AcquireString((
char *) NULL);
344 extent=MaxTextExtent;
345 for (p=utf8; *p !=
'\0'; p++)
347 if ((i+MaxTextExtent) > (ssize_t) extent)
349 extent+=MaxTextExtent;
350 canonical_content=(
char *) ResizeQuantumMemory(canonical_content,extent,
351 sizeof(*canonical_content));
352 if (canonical_content == (
char *) NULL)
353 return(canonical_content);
359 i+=FormatLocaleString(canonical_content+i,extent,
"&");
364 i+=FormatLocaleString(canonical_content+i,extent,
"<");
369 i+=FormatLocaleString(canonical_content+i,extent,
">");
374 i+=FormatLocaleString(canonical_content+i,extent,
""");
379 if (pedantic == MagickFalse)
381 canonical_content[i++]=(char) (*p);
384 i+=FormatLocaleString(canonical_content+i,extent,
"
");
389 if (pedantic == MagickFalse)
391 canonical_content[i++]=(char) (*p);
394 i+=FormatLocaleString(canonical_content+i,extent,
"	");
399 i+=FormatLocaleString(canonical_content+i,extent,
"
");
404 canonical_content[i++]=(char) (*p);
409 canonical_content[i]=
'\0';
410 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
411 return(canonical_content);
437 static char **DestroyXMLTreeAttributes(
char **attributes)
445 if ((attributes == (
char **) NULL) || (attributes == sentinel))
446 return((
char **) NULL);
447 for (i=0; attributes[i] != (
char *) NULL; i+=2)
452 if (attributes[i] != (
char *) NULL)
453 attributes[i]=DestroyString(attributes[i]);
454 if (attributes[i+1] != (
char *) NULL)
455 attributes[i+1]=DestroyString(attributes[i+1]);
457 attributes=(
char **) RelinquishMagickMemory(attributes);
458 return((
char **) NULL);
461 static void DestroyXMLTreeChild(
XMLTreeInfo *xml_info)
467 child=xml_info->child;
473 (void) DestroyXMLTree(node);
477 static void DestroyXMLTreeOrdered(
XMLTreeInfo *xml_info)
483 ordered=xml_info->ordered;
487 ordered=node->ordered;
489 (void) DestroyXMLTree(node);
493 static void DestroyXMLTreeRoot(
XMLTreeInfo *xml_info)
508 assert((xml_info->signature == MagickCoreSignature) ||
509 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
510 if (IsEventLogging() != MagickFalse)
511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
518 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
519 root->entities[i+1]=DestroyString(root->entities[i+1]);
520 root->entities=(
char **) RelinquishMagickMemory(root->entities);
521 for (i=0; root->attributes[i] != (
char **) NULL; i++)
523 attributes=root->attributes[i];
524 if (attributes[0] != (
char *) NULL)
525 attributes[0]=DestroyString(attributes[0]);
526 for (j=1; attributes[j] != (
char *) NULL; j+=3)
528 if (attributes[j] != (
char *) NULL)
529 attributes[j]=DestroyString(attributes[j]);
530 if (attributes[j+1] != (
char *) NULL)
531 attributes[j+1]=DestroyString(attributes[j+1]);
532 if (attributes[j+2] != (
char *) NULL)
533 attributes[j+2]=DestroyString(attributes[j+2]);
535 attributes=(
char **) RelinquishMagickMemory(attributes);
537 if (root->attributes[0] != (
char **) NULL)
538 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
539 if (root->processing_instructions[0] != (
char **) NULL)
541 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
543 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
544 root->processing_instructions[i][j]=DestroyString(
545 root->processing_instructions[i][j]);
546 root->processing_instructions[i][j+1]=DestroyString(
547 root->processing_instructions[i][j+1]);
548 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
549 root->processing_instructions[i]);
551 root->processing_instructions=(
char ***) RelinquishMagickMemory(
552 root->processing_instructions);
559 assert((xml_info->signature == MagickCoreSignature) ||
560 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
561 if (IsEventLogging() != MagickFalse)
562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
563 DestroyXMLTreeChild(xml_info);
564 DestroyXMLTreeOrdered(xml_info);
565 DestroyXMLTreeRoot(xml_info);
566 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
567 xml_info->content=DestroyString(xml_info->content);
568 xml_info->tag=DestroyString(xml_info->tag);
569 xml_info=(
XMLTreeInfo *) RelinquishMagickMemory(xml_info);
597 MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
620 assert(filename != (
const char *) NULL);
623 if (LocaleCompare(filename,
"-") != 0)
624 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
626 return((
char *) NULL);
627 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
629 if ((file == fileno(stdin)) || (offset < 0) ||
630 (offset != (MagickOffsetType) ((ssize_t) offset)))
641 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
642 quantum=(size_t) MagickMaxBufferExtent;
643 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
644 quantum=(
size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
645 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
646 for (i=0; xml != (
char *) NULL; i+=count)
648 count=read(file,xml+i,quantum);
655 if (~((
size_t) i) < (quantum+1))
657 xml=(
char *) RelinquishMagickMemory(xml);
660 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
661 if ((
size_t) (i+count) >= extent)
664 if (LocaleCompare(filename,
"-") != 0)
666 if (xml == (
char *) NULL)
667 return((
char *) NULL);
670 xml=(
char *) RelinquishMagickMemory(xml);
671 return((
char *) NULL);
673 length=(size_t) MagickMin(i+count,extent);
677 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
679 if (~length >= (MaxTextExtent-1))
680 xml=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
sizeof(*xml));
681 if (xml == (
char *) NULL)
684 return((
char *) NULL);
686 map=MapBlob(file,ReadMode,0,length);
687 if (map != (
char *) NULL)
689 (void) memcpy(xml,map,length);
690 (void) UnmapBlob(map,length);
694 (void) lseek(file,0,SEEK_SET);
695 for (i=0; i < length; i+=count)
697 count=read(file,xml+i,(
size_t) MagickMin(length-i,(
size_t) MAGICK_SSIZE_MAX));
708 xml=(
char *) RelinquishMagickMemory(xml);
709 return((
char *) NULL);
713 if (LocaleCompare(filename,
"-") != 0)
716 xml=(
char *) RelinquishMagickMemory(xml);
745 assert((xml_info->signature == MagickCoreSignature) ||
746 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
747 if (IsEventLogging() != MagickFalse)
748 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
749 return(xml_info->next);
777 MagickExport
const char *GetXMLTreeAttribute(
XMLTreeInfo *xml_info,
790 assert((xml_info->signature == MagickCoreSignature) ||
791 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
792 if (IsEventLogging() != MagickFalse)
793 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
794 if (xml_info->attributes == (
char **) NULL)
795 return((
const char *) NULL);
797 while ((xml_info->attributes[i] != (
char *) NULL) &&
798 (strcmp(xml_info->attributes[i],tag) != 0))
800 if (xml_info->attributes[i] != (
char *) NULL)
801 return(xml_info->attributes[i+1]);
806 while ((root->attributes[i] != (
char **) NULL) &&
807 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
809 if (root->attributes[i] == (
char **) NULL)
810 return((
const char *) NULL);
812 while ((root->attributes[i][j] != (
char *) NULL) &&
813 (strcmp(root->attributes[i][j],tag) != 0))
815 if (root->attributes[i][j] == (
char *) NULL)
816 return((
const char *) NULL);
817 return(root->attributes[i][j+1]);
846 MagickExport MagickBooleanType GetXMLTreeAttributes(
const XMLTreeInfo *xml_info,
853 assert((xml_info->signature == MagickCoreSignature) ||
854 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
856 if (IsEventLogging() != MagickFalse)
857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
858 if (xml_info->attributes == (
char **) NULL)
861 while (xml_info->attributes[i] != (
char *) NULL)
863 (void) AddValueToSplayTree(attributes,
864 ConstantString(xml_info->attributes[i]),
865 ConstantString(xml_info->attributes[i+1]));
900 assert((xml_info->signature == MagickCoreSignature) ||
901 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
902 if (IsEventLogging() != MagickFalse)
903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
904 child=xml_info->child;
905 if (tag != (
const char *) NULL)
906 while ((child != (
XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
907 child=child->sibling;
934 MagickExport
const char *GetXMLTreeContent(
XMLTreeInfo *xml_info)
937 assert((xml_info->signature == MagickCoreSignature) ||
938 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
939 if (IsEventLogging() != MagickFalse)
940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
941 return(xml_info->content);
969 assert((xml_info->signature == MagickCoreSignature) ||
970 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
971 if (IsEventLogging() != MagickFalse)
972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
973 return(xml_info->ordered);
1005 subnode[MaxTextExtent],
1021 assert((xml_info->signature == MagickCoreSignature) ||
1022 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1023 if (IsEventLogging() != MagickFalse)
1024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1026 components=GetPathComponents(path,&number_components);
1027 if (components == (
char **) NULL)
1029 for (i=0; i < (ssize_t) number_components; i++)
1031 GetPathComponent(components[i],SubimagePath,subnode);
1032 GetPathComponent(components[i],CanonicalPath,tag);
1033 node=GetXMLTreeChild(node,tag);
1036 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
1038 node=GetXMLTreeOrdered(node);
1044 components[i]=DestroyString(components[i]);
1046 for ( ; i < (ssize_t) number_components; i++)
1047 components[i]=DestroyString(components[i]);
1048 components=(
char **) RelinquishMagickMemory(components);
1076 MagickExport
const char **GetXMLTreeProcessingInstructions(
1086 assert((xml_info->signature == MagickCoreSignature) ||
1087 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1088 if (IsEventLogging() != MagickFalse)
1089 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1094 while ((root->processing_instructions[i] != (
char **) NULL) &&
1095 (strcmp(root->processing_instructions[i][0],target) != 0))
1097 if (root->processing_instructions[i] == (
char **) NULL)
1098 return((
const char **) sentinel);
1099 return((
const char **) (root->processing_instructions[i]+1));
1127 assert((xml_info->signature == MagickCoreSignature) ||
1128 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1129 if (IsEventLogging() != MagickFalse)
1130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1131 return(xml_info->sibling);
1156 MagickExport
const char *GetXMLTreeTag(
XMLTreeInfo *xml_info)
1159 assert((xml_info->signature == MagickCoreSignature) ||
1160 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1161 if (IsEventLogging() != MagickFalse)
1162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1163 return(xml_info->tag);
1205 child->offset=offset;
1206 child->parent=xml_info;
1209 xml_info->child=child;
1212 head=xml_info->child;
1213 if (head->offset > offset)
1215 child->ordered=head;
1216 xml_info->child=child;
1222 (node->ordered->offset <= offset))
1224 child->ordered=node->ordered;
1225 node->ordered=child;
1229 while ((node != (
XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1234 if ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1237 (node->next->offset <= offset))
1239 child->next=node->next;
1245 previous->sibling=node->sibling;
1249 while ((node != (
XMLTreeInfo *) NULL) && (node->offset <= offset))
1254 child->sibling=node;
1256 previous->sibling=child;
1287 static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1307 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1308 if (utf8 == (
char *) NULL)
1309 return((
char *) NULL);
1310 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1316 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1322 for (i=2; i < (ssize_t) (*length-1); i+=2)
1324 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1325 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1326 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1328 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1329 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1330 (content[i] & 0xff);
1331 c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
1333 if ((
size_t) (j+MaxTextExtent) > extent)
1335 extent=(size_t) j+MaxTextExtent;
1336 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1337 if (utf8 == (
char *) NULL)
1350 for (bits=0; byte != 0; byte/=2)
1353 utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
1357 utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
1362 utf8=(
char *) ResizeQuantumMemory(utf8,*length,
sizeof(*utf8));
1363 if (utf8 != (
char *) NULL)
1368 static char *ParseEntities(
char *xml,
char **entities,
int state)
1396 for ( ; *xml !=
'\0'; xml++)
1397 while (*xml ==
'\r')
1401 (void) memmove(xml,xml+1,strlen(xml));
1405 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1406 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml) == 0)))
1418 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1424 c=strtol(xml+2,&entity,10);
1426 c=strtol(xml+3,&entity,16);
1427 if ((c == 0) || (*entity !=
';'))
1443 for (i=0; byte != 0; byte/=2)
1446 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1451 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1455 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1458 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1459 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1465 while ((entities[i] != (
char *) NULL) &&
1466 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1468 if (entities[i++] == (
char *) NULL)
1471 if (entities[i] != (
char *) NULL)
1476 length=strlen(entities[i]);
1477 entity=strchr(xml,
';');
1478 if ((entity != (
char *) NULL) &&
1479 ((length-1L) >= (
size_t) (entity-xml)))
1481 offset=(ssize_t) (xml-p);
1482 extent=(size_t) (offset+length+strlen(entity));
1485 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1493 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1494 sizeof(*extent_xml));
1495 if (extent_xml != (
char *) NULL)
1497 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1498 (void) CopyMagickString(extent_xml,p,extent*
1499 sizeof(*extent_xml));
1503 if (p == (
char *) NULL)
1504 ThrowFatalException(ResourceLimitFatalError,
1505 "MemoryAllocationFailed");
1507 entity=strchr(xml,
';');
1509 if (entity != (
char *) NULL)
1510 (void) memmove(xml+length,entity+1,strlen(entity));
1511 (void) memcpy(xml,entities[i],length);
1515 if (((state ==
' ') || (state ==
'*')) &&
1516 (isspace((
int) ((
unsigned char) *xml) != 0)))
1527 for (xml=p; *xml !=
'\0'; xml++)
1532 i=(ssize_t) strspn(xml,accept);
1534 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1535 while ((*xml !=
'\0') && (*xml !=
' '))
1541 if ((xml >= p) && (*xml ==
' '))
1544 return(p == q ? ConstantString(p) : p);
1547 static void ParseCharacterContent(
XMLTreeRoot *root,
char *xml,
1548 const size_t length,
const char state)
1553 xml_info=root->node;
1554 if ((xml_info == (
XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1558 xml=ParseEntities(xml,root->entities,state);
1559 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1561 (void) ConcatenateString(&xml_info->content,xml);
1562 xml=DestroyString(xml);
1566 if (xml_info->content != (
char *) NULL)
1567 xml_info->content=DestroyString(xml_info->content);
1568 xml_info->content=xml;
1576 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1578 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1579 "ParseError",
"unexpected closing tag </%s>",tag);
1580 return(&root->root);
1582 root->node=root->node->parent;
1586 static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1587 const size_t depth,
char **entities)
1595 if (depth > MagickMaxRecursionDepth)
1596 return(MagickFalse);
1599 while ((*xml !=
'\0') && (*xml !=
'&'))
1603 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1604 return(MagickFalse);
1606 while ((entities[i] != (
char *) NULL) &&
1607 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1609 if ((entities[i] != (
char *) NULL) &&
1610 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1611 return(MagickFalse);
1615 static void ParseProcessingInstructions(
XMLTreeRoot *root,
char *xml,
1629 xml+=strcspn(xml,XMLWhitespace);
1633 xml+=strspn(xml+1,XMLWhitespace)+1;
1635 if (strcmp(target,
"xml") == 0)
1637 xml=strstr(xml,
"standalone");
1638 if ((xml != (
char *) NULL) &&
1639 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1640 root->standalone=MagickTrue;
1643 if (root->processing_instructions[0] == (
char **) NULL)
1645 root->processing_instructions=(
char ***) AcquireMagickMemory(
sizeof(
1646 *root->processing_instructions));
1647 if (root->processing_instructions ==(
char ***) NULL)
1648 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1649 *root->processing_instructions=(
char **) NULL;
1652 while ((root->processing_instructions[i] != (
char **) NULL) &&
1653 (strcmp(target,root->processing_instructions[i][0]) != 0))
1655 if (root->processing_instructions[i] == (
char **) NULL)
1657 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1658 root->processing_instructions,(
size_t) (i+2),
1659 sizeof(*root->processing_instructions));
1660 if (root->processing_instructions == (
char ***) NULL)
1661 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1662 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1663 sizeof(**root->processing_instructions));
1664 if (root->processing_instructions[i] == (
char **) NULL)
1665 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1666 root->processing_instructions[i+1]=(
char **) NULL;
1667 root->processing_instructions[i][0]=ConstantString(target);
1668 root->processing_instructions[i][1]=(
char *)
1669 root->processing_instructions[i+1];
1670 root->processing_instructions[i+1]=(
char **) NULL;
1671 root->processing_instructions[i][2]=ConstantString(
"");
1674 while (root->processing_instructions[i][j] != (
char *) NULL)
1676 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1677 root->processing_instructions[i],(
size_t) (j+3),
1678 sizeof(**root->processing_instructions));
1679 if (root->processing_instructions[i] == (
char **) NULL)
1680 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1681 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1682 root->processing_instructions[i][j+1],(
size_t) (j+1),
1683 sizeof(***root->processing_instructions));
1684 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1685 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1686 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1687 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1688 root->processing_instructions[i][j]=ConstantString(xml);
1689 root->processing_instructions[i][j+1]=(
char *) NULL;
1692 static MagickBooleanType ParseInternalDoctype(
XMLTreeRoot *root,
char *xml,
1699 **predefined_entitites,
1711 predefined_entitites=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1712 if (predefined_entitites == (
char **) NULL)
1713 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1714 (void) memcpy(predefined_entitites,sentinel,
sizeof(sentinel));
1715 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1717 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1721 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1726 if (strspn(xml+8,XMLWhitespace) == 0)
1728 xml+=strspn(xml+8,XMLWhitespace)+8;
1730 n=xml+strspn(xml,XMLWhitespace
"%");
1731 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1733 xml=n+strcspn(n,XMLWhitespace);
1737 v=xml+strspn(xml+1,XMLWhitespace)+1;
1740 if ((q !=
'"') && (q !=
'\''))
1745 xml=strchr(xml,
'>');
1748 entities=(*c ==
'%') ? predefined_entitites : root->entities;
1749 for (i=0; entities[i] != (
char *) NULL; i++) ;
1750 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1752 if (entities == (
char **) NULL)
1753 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1755 predefined_entitites=entities;
1757 root->entities=entities;
1761 if (xml != (
char *) NULL)
1766 entities[i+1]=ParseEntities(v,predefined_entitites,
'%');
1767 entities[i+2]=(
char *) NULL;
1768 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1772 if (entities[i+1] != v)
1773 entities[i+1]=DestroyString(entities[i+1]);
1774 (void) ThrowMagickException(exception,GetMagickModule(),
1775 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1776 predefined_entitites=(
char **) RelinquishMagickMemory(
1777 predefined_entitites);
1778 return(MagickFalse);
1782 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1787 t=xml+strspn(xml+9,XMLWhitespace)+9;
1790 (void) ThrowMagickException(exception,GetMagickModule(),
1791 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1792 predefined_entitites=(
char **) RelinquishMagickMemory(
1793 predefined_entitites);
1794 return(MagickFalse);
1796 xml=t+strcspn(t,XMLWhitespace
">");
1801 while ((root->attributes[i] != (
char **) NULL) &&
1802 (n != (
char *) NULL) &&
1803 (strcmp(n,root->attributes[i][0]) != 0))
1805 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1808 xml=n+strcspn(n,XMLWhitespace);
1813 (void) ThrowMagickException(exception,GetMagickModule(),
1814 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1815 predefined_entitites=(
char **) RelinquishMagickMemory(
1816 predefined_entitites);
1817 return(MagickFalse);
1819 xml+=strspn(xml+1,XMLWhitespace)+1;
1820 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1821 if (strncmp(xml,
"NOTATION",8) == 0)
1822 xml+=strspn(xml+8,XMLWhitespace)+8;
1823 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1824 strcspn(xml,XMLWhitespace);
1825 if (xml == (
char *) NULL)
1827 (void) ThrowMagickException(exception,GetMagickModule(),
1828 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1829 predefined_entitites=(
char **) RelinquishMagickMemory(
1830 predefined_entitites);
1831 return(MagickFalse);
1833 xml+=strspn(xml,XMLWhitespace
")");
1834 if (strncmp(xml,
"#FIXED",6) == 0)
1835 xml+=strspn(xml+6,XMLWhitespace)+6;
1838 xml+=strcspn(xml,XMLWhitespace
">")-1;
1844 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1845 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1849 (void) ThrowMagickException(exception,GetMagickModule(),
1850 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1851 predefined_entitites=(
char **) RelinquishMagickMemory(
1852 predefined_entitites);
1853 return(MagickFalse);
1855 if (root->attributes[i] == (
char **) NULL)
1861 root->attributes=(
char ***) AcquireQuantumMemory(2,
1862 sizeof(*root->attributes));
1864 root->attributes=(
char ***) ResizeQuantumMemory(
1865 root->attributes,(
size_t) (i+2),
1866 sizeof(*root->attributes));
1867 if (root->attributes == (
char ***) NULL)
1868 ThrowFatalException(ResourceLimitFatalError,
1869 "MemoryAllocationFailed");
1870 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1871 sizeof(**root->attributes));
1872 if (root->attributes[i] == (
char **) NULL)
1873 ThrowFatalException(ResourceLimitFatalError,
1874 "MemoryAllocationFailed");
1875 root->attributes[i][0]=ConstantString(t);
1876 root->attributes[i][1]=(
char *) NULL;
1877 root->attributes[i+1]=(
char **) NULL;
1879 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1880 root->attributes[i]=(
char **) ResizeQuantumMemory(
1881 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1882 if (root->attributes[i] == (
char **) NULL)
1883 ThrowFatalException(ResourceLimitFatalError,
1884 "MemoryAllocationFailed");
1885 root->attributes[i][j+3]=(
char *) NULL;
1886 root->attributes[i][j+2]=ConstantString(c);
1887 root->attributes[i][j+1]=(
char *) NULL;
1888 if (v != (
char *) NULL)
1889 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1890 root->attributes[i][j]=ConstantString(n);
1894 if (strncmp(xml,
"<!--", 4) == 0)
1895 xml=strstr(xml+4,
"-->");
1897 if (strncmp(xml,
"<?", 2) == 0)
1901 if (xml != (
char *) NULL)
1903 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1909 xml=strchr(xml,
'>');
1911 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1914 predefined_entitites=(
char **) RelinquishMagickMemory(predefined_entitites);
1918 static void ParseOpenTag(
XMLTreeRoot *root,
char *tag,
char **attributes)
1923 xml_info=root->node;
1924 if (xml_info->tag == (
char *) NULL)
1925 xml_info->tag=ConstantString(tag);
1927 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1929 xml_info->attributes=attributes;
1930 root->node=xml_info;
1941 static inline MagickBooleanType IsSkipTag(
const char *tag)
1947 while (skip_tags[i] != (
const char *) NULL)
1949 if (LocaleCompare(tag,skip_tags[i]) == 0)
1953 return(MagickFalse);
1991 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1993 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1994 "ParseError",
"root tag missing");
1997 root=(
XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1999 utf8=ConvertUTF16ToUTF8(xml,&length);
2000 if (utf8 == (
char *) NULL)
2002 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2003 "ParseError",
"UTF16 to UTF8 failed");
2006 terminal=utf8[length-1];
2007 utf8[length-1]=
'\0';
2009 while ((*p !=
'\0') && (*p !=
'<'))
2013 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2014 "ParseError",
"root tag missing");
2015 utf8=DestroyString(utf8);
2018 attribute=(
char **) NULL;
2023 attributes=(
char **) sentinel;
2026 if ((isalpha((
int) ((
unsigned char) *p)) !=0) || (*p ==
'_') ||
2027 (*p ==
':') || (c <
'\0'))
2034 (void) ThrowMagickException(exception,GetMagickModule(),
2035 OptionWarning,
"ParseError",
"root tag missing");
2036 utf8=DestroyString(utf8);
2037 return(&root->root);
2039 p+=strcspn(p,XMLWhitespace
"/>");
2040 while (isspace((
int) ((
unsigned char) *p)) != 0)
2042 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
2043 (ignore_depth == 0))
2045 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2051 while ((root->attributes[i] != (
char **) NULL) &&
2052 (strcmp(root->attributes[i][0],tag) != 0))
2054 attribute=root->attributes[i];
2056 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
2062 attributes=(
char **) AcquireQuantumMemory(4,
2063 sizeof(*attributes));
2065 attributes=(
char **) ResizeQuantumMemory(attributes,
2066 (
size_t) (l+4),
sizeof(*attributes));
2067 if (attributes == (
char **) NULL)
2069 (void) ThrowMagickException(exception,GetMagickModule(),
2070 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2071 utf8=DestroyString(utf8);
2072 return(&root->root);
2074 attributes[l+2]=(
char *) NULL;
2075 attributes[l+1]=(
char *) NULL;
2077 p+=strcspn(p,XMLWhitespace
"=/>");
2078 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2079 attributes[l]=ConstantString(
"");
2083 p+=strspn(p,XMLWhitespace
"=");
2085 if ((c ==
'"') || (c ==
'\''))
2092 while ((*p !=
'\0') && (*p != c))
2098 attributes[l]=ConstantString(
"");
2099 attributes[l+1]=ConstantString(
"");
2100 (void) DestroyXMLTreeAttributes(attributes);
2101 (void) ThrowMagickException(exception,
2102 GetMagickModule(),OptionWarning,
"ParseError",
2104 utf8=DestroyString(utf8);
2105 return(&root->root);
2108 while ((attribute != (
char **) NULL) &&
2109 (attribute[j] != (
char *) NULL) &&
2110 (strcmp(attribute[j],attributes[l]) != 0))
2112 attributes[l+1]=ParseEntities(attributes[l+1],
2113 root->entities,(attribute != (
char **) NULL) &&
2114 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2117 attributes[l]=ConstantString(attributes[l]);
2119 while (isspace((
int) ((
unsigned char) *p)) != 0)
2125 while((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2134 if (((*p !=
'\0') && (*p !=
'>')) ||
2135 ((*p ==
'\0') && (terminal !=
'>')))
2138 (void) DestroyXMLTreeAttributes(attributes);
2139 (void) ThrowMagickException(exception,GetMagickModule(),
2140 OptionWarning,
"ParseError",
"missing >");
2141 utf8=DestroyString(utf8);
2142 return(&root->root);
2144 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2145 (void) DestroyXMLTreeAttributes(attributes);
2148 ParseOpenTag(root,tag,attributes);
2149 (void) ParseCloseTag(root,tag,exception);
2155 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2158 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2159 ParseOpenTag(root,tag,attributes);
2163 (void) DestroyXMLTreeAttributes(attributes);
2170 (void) DestroyXMLTreeAttributes(attributes);
2171 (void) ThrowMagickException(exception,GetMagickModule(),
2172 OptionWarning,
"ParseError",
"missing >");
2173 utf8=DestroyString(utf8);
2174 return(&root->root);
2185 p+=strcspn(tag,XMLWhitespace
">")+1;
2187 if ((c ==
'\0') && (terminal !=
'>'))
2189 (void) ThrowMagickException(exception,GetMagickModule(),
2190 OptionWarning,
"ParseError",
"missing >");
2191 utf8=DestroyString(utf8);
2192 return(&root->root);
2195 if ((ignore_depth == 0) &&
2196 (ParseCloseTag(root,tag,exception) != (
XMLTreeInfo *) NULL))
2198 utf8=DestroyString(utf8);
2199 return(&root->root);
2201 if (ignore_depth > 0)
2204 if (isspace((
int) ((
unsigned char) *p)) != 0)
2205 p+=strspn(p,XMLWhitespace);
2208 if (strncmp(p,
"!--",3) == 0)
2214 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2215 ((*p ==
'\0') && (terminal !=
'>')))
2217 (void) ThrowMagickException(exception,GetMagickModule(),
2218 OptionWarning,
"ParseError",
"unclosed <!--");
2219 utf8=DestroyString(utf8);
2220 return(&root->root);
2224 if (strncmp(p,
"![CDATA[",8) == 0)
2230 if (p != (
char *) NULL)
2233 if (ignore_depth == 0)
2234 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2238 (void) ThrowMagickException(exception,GetMagickModule(),
2239 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2240 utf8=DestroyString(utf8);
2241 return(&root->root);
2245 if (strncmp(p,
"!DOCTYPE",8) == 0)
2250 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2251 ((l != 0) && ((*p !=
']') ||
2252 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2253 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2254 p+=strcspn(p+1,
"[]>")+1;
2255 if ((*p ==
'\0') && (terminal !=
'>'))
2257 (void) ThrowMagickException(exception,GetMagickModule(),
2258 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2259 utf8=DestroyString(utf8);
2260 return(&root->root);
2263 tag=strchr(tag,
'[')+1;
2266 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2268 if (status == MagickFalse)
2270 utf8=DestroyString(utf8);
2271 return(&root->root);
2285 if (p == (
char *) NULL)
2288 }
while ((*p !=
'\0') && (*p !=
'>'));
2289 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2292 (void) ThrowMagickException(exception,GetMagickModule(),
2293 OptionWarning,
"ParseError",
"unclosed <?");
2294 utf8=DestroyString(utf8);
2295 return(&root->root);
2297 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2301 (void) ThrowMagickException(exception,GetMagickModule(),
2302 OptionWarning,
"ParseError",
"unexpected <");
2303 utf8=DestroyString(utf8);
2304 return(&root->root);
2306 if ((p == (
char *) NULL) || (*p ==
'\0'))
2310 if ((*p !=
'\0') && (*p !=
'<'))
2315 while ((*p !=
'\0') && (*p !=
'<'))
2319 if (ignore_depth == 0)
2320 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2326 utf8=DestroyString(utf8);
2328 return(&root->root);
2329 if (root->node->tag == (
char *) NULL)
2331 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2332 "ParseError",
"root tag missing");
2333 return(&root->root);
2335 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2336 "ParseError",
"unclosed tag: `%s'",root->node->tag);
2337 return(&root->root);
2362 MagickExport
XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2365 *predefined_entities[NumberPredefinedEntities+1] =
2367 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2368 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2374 root=(
XMLTreeRoot *) AcquireMagickMemory(
sizeof(*root));
2377 (void) memset(root,0,
sizeof(*root));
2378 root->root.tag=(
char *) NULL;
2379 if (tag != (
char *) NULL)
2380 root->root.tag=ConstantString(tag);
2381 root->node=(&root->root);
2382 root->root.content=ConstantString(
"");
2383 root->entities=(
char **) AcquireMagickMemory(
sizeof(predefined_entities));
2384 if (root->entities == (
char **) NULL)
2386 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2387 root->root.attributes=sentinel;
2388 root->attributes=(
char ***) root->root.attributes;
2389 root->processing_instructions=(
char ***) root->root.attributes;
2390 root->debug=IsEventLogging();
2391 root->signature=MagickCoreSignature;
2392 return(&root->root);
2424 assert((xml_info->signature == MagickCoreSignature) ||
2425 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2426 if (IsEventLogging() != MagickFalse)
2427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2429 xml_info->next->sibling=xml_info->sibling;
2432 node=xml_info->parent->child;
2433 if (node == xml_info)
2434 xml_info->parent->child=xml_info->ordered;
2437 while (node->ordered != xml_info)
2439 node->ordered=node->ordered->ordered;
2440 node=xml_info->parent->child;
2441 if (strcmp(node->tag,xml_info->tag) != 0)
2443 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2445 if (node->sibling != xml_info)
2448 node->sibling=(xml_info->next != (
XMLTreeInfo *) NULL) ?
2449 xml_info->next : node->sibling->sibling;
2452 (node->next != xml_info))
2455 node->next=node->next->next;
2493 const char *tag,
const char *value)
2502 assert((xml_info->signature == MagickCoreSignature) ||
2503 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2504 if (IsEventLogging() != MagickFalse)
2505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2507 while ((xml_info->attributes[i] != (
char *) NULL) &&
2508 (strcmp(xml_info->attributes[i],tag) != 0))
2510 if (xml_info->attributes[i] == (
char *) NULL)
2515 if (value == (
const char *) NULL)
2517 if (xml_info->attributes != sentinel)
2518 xml_info->attributes=(
char **) ResizeQuantumMemory(
2519 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2522 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2523 sizeof(*xml_info->attributes));
2524 if (xml_info->attributes != (
char **) NULL)
2525 xml_info->attributes[1]=ConstantString(
"");
2527 if (xml_info->attributes == (
char **) NULL)
2528 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2529 xml_info->attributes[i]=ConstantString(tag);
2530 xml_info->attributes[i+2]=(
char *) NULL;
2531 (void) strlen(xml_info->attributes[i+1]);
2536 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2537 if (xml_info->attributes[i+1] != (
char *) NULL)
2538 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2539 if (value != (
const char *) NULL)
2541 xml_info->attributes[i+1]=ConstantString(value);
2544 if (xml_info->attributes[i] != (
char *) NULL)
2545 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2546 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,
2547 (
size_t) (j-i)*
sizeof(*xml_info->attributes));
2548 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2549 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2550 if (xml_info->attributes == (
char **) NULL)
2551 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2553 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2554 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2585 const char *content)
2588 assert((xml_info->signature == MagickCoreSignature) ||
2589 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2590 if (IsEventLogging() != MagickFalse)
2591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2592 if (xml_info->content != (
char *) NULL)
2593 xml_info->content=DestroyString(xml_info->content);
2594 xml_info->content=(
char *) ConstantString(content);
2621 static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2622 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2628 canonical_content=CanonicalXMLContent(source,pedantic);
2634 content=AcquireString(source);
2635 content[offset]=
'\0';
2636 canonical_content=CanonicalXMLContent(content,pedantic);
2637 content=DestroyString(content);
2639 if (canonical_content == (
char *) NULL)
2640 return(*destination);
2641 if ((*length+strlen(canonical_content)+MaxTextExtent) > *extent)
2643 *extent=(*length)+strlen(canonical_content)+MaxTextExtent;
2644 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2645 sizeof(**destination));
2646 if (*destination == (
char *) NULL)
2647 return(*destination);
2649 *length+=FormatLocaleString(*destination+(*length),*extent,
"%s",
2651 canonical_content=DestroyString(canonical_content);
2652 return(*destination);
2655 static char *XMLTreeTagToXML(
XMLTreeInfo *xml_info,
char **source,
size_t *length,
2656 size_t *extent,
size_t start,
char ***attributes)
2673 content=(
char *)
"";
2675 content=xml_info->parent->content;
2677 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2678 start),source,length,extent,MagickFalse);
2679 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2681 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2682 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2683 if (*source == (
char *) NULL)
2686 *length+=FormatLocaleString(*source+(*length),*extent,
"<%s",xml_info->tag);
2687 for (i=0; xml_info->attributes[i]; i+=2)
2689 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2690 if (attribute != xml_info->attributes[i+1])
2692 if ((*length+strlen(xml_info->attributes[i])+MaxTextExtent) > *extent)
2694 *extent=(*length)+strlen(xml_info->attributes[i])+MaxTextExtent;
2695 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2696 if (*source == (
char *) NULL)
2697 return((
char *) NULL);
2699 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2700 xml_info->attributes[i]);
2701 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2703 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2706 while ((attributes[i] != (
char **) NULL) &&
2707 (strcmp(attributes[i][0],xml_info->tag) != 0))
2710 while ((attributes[i] != (
char **) NULL) &&
2711 (attributes[i][j] != (
char *) NULL))
2713 if ((attributes[i][j+1] == (
char *) NULL) ||
2714 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2719 if ((*length+strlen(attributes[i][j])+MaxTextExtent) > *extent)
2721 *extent=(*length)+strlen(attributes[i][j])+MaxTextExtent;
2722 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2723 if (*source == (
char *) NULL)
2724 return((
char *) NULL);
2726 *length+=FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2728 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2730 *length+=FormatLocaleString(*source+(*length),*extent,
"\"");
2733 *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
2736 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2738 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2740 if ((*length+strlen(xml_info->tag)+MaxTextExtent) > *extent)
2742 *extent=(*length)+strlen(xml_info->tag)+MaxTextExtent;
2743 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2744 if (*source == (
char *) NULL)
2745 return((
char *) NULL);
2747 if (*xml_info->content !=
'\0')
2748 *length+=FormatLocaleString(*source+(*length),*extent,
"</%s>",
2750 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2753 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2756 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2761 MagickExport
char *XMLTreeInfoToXML(
XMLTreeInfo *xml_info)
2789 assert((xml_info->signature == MagickCoreSignature) ||
2790 (((
XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2791 if (IsEventLogging() != MagickFalse)
2792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2793 if (xml_info->tag == (
char *) NULL)
2794 return((
char *) NULL);
2795 xml=AcquireString((
char *) NULL);
2797 extent=MaxTextExtent;
2801 parent=xml_info->parent;
2803 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2808 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2809 p=root->processing_instructions[i][1];
2810 for (j=1; p != (
char *) NULL; j++)
2812 if (root->processing_instructions[i][k][j-1] ==
'>')
2814 p=root->processing_instructions[i][j];
2817 q=root->processing_instructions[i][0];
2818 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2820 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2821 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2822 if (xml == (
char *) NULL)
2825 length+=FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2826 *p !=
'\0' ?
" " :
"",p);
2827 p=root->processing_instructions[i][j];
2830 ordered=xml_info->ordered;
2833 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2834 xml_info->parent=parent;
2835 xml_info->ordered=ordered;
2837 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2842 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2843 p=root->processing_instructions[i][1];
2844 for (j=1; p != (
char *) NULL; j++)
2846 if (root->processing_instructions[i][k][j-1] ==
'<')
2848 p=root->processing_instructions[i][j];
2851 q=root->processing_instructions[i][0];
2852 if ((length+strlen(p)+strlen(q)+MaxTextExtent) > extent)
2854 extent=length+strlen(p)+strlen(q)+MaxTextExtent;
2855 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2856 if (xml == (
char *) NULL)
2859 length+=FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2860 *p !=
'\0' ?
" " :
"",p);
2861 p=root->processing_instructions[i][j];
2864 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));