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 };
167MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
168 const char *tag,
const size_t offset)
173 if (xml_info == (XMLTreeInfo *) NULL)
174 return((XMLTreeInfo *) NULL);
175 child=(XMLTreeInfo *) AcquireMagickMemory(
sizeof(*child));
176 if (child == (XMLTreeInfo *) NULL)
177 return((XMLTreeInfo *) NULL);
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));
215MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
216 const char *path,
const size_t offset)
220 subnode[MagickPathExtent],
221 tag[MagickPathExtent];
234 assert(xml_info != (XMLTreeInfo *) NULL);
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)
242 return((XMLTreeInfo *) 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);
248 if (child == (XMLTreeInfo *) NULL)
249 child=AddChildToXMLTree(node,tag,offset);
251 if (node == (XMLTreeInfo *) NULL)
253 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
255 node=GetXMLTreeOrdered(node);
256 if (node == (XMLTreeInfo *) NULL)
259 if (node == (XMLTreeInfo *) NULL)
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);
363 *DestroyXMLTree_(XMLTreeInfo *,
const size_t);
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;
397 while (child != (XMLTreeInfo *) NULL)
401 node->child=(XMLTreeInfo *) NULL;
402 (void) DestroyXMLTree_(node,depth+1);
406static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info,
413 ordered=xml_info->ordered;
414 while (ordered != (XMLTreeInfo *) NULL)
417 ordered=node->ordered;
418 node->ordered=(XMLTreeInfo *) NULL;
419 (void) DestroyXMLTree_(node,depth+1);
423static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
435 assert(xml_info != (XMLTreeInfo *) NULL);
436 assert((xml_info->signature == MagickCoreSignature) ||
437 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
438 if (IsEventLogging() != MagickFalse)
439 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
440 if (xml_info->parent != (XMLTreeInfo *) NULL)
445 root=(XMLTreeRoot *) xml_info;
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);
484static XMLTreeInfo *DestroyXMLTree_(XMLTreeInfo *xml_info,
487 assert(xml_info != (XMLTreeInfo *) NULL);
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);
502 return((XMLTreeInfo *) NULL);
505MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *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);
678MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
680 assert(xml_info != (XMLTreeInfo *) NULL);
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,
723 assert(xml_info != (XMLTreeInfo *) NULL);
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]);
736 root=(XMLTreeRoot*) xml_info;
737 while (root->root.parent != (XMLTreeInfo *) NULL)
738 root=(XMLTreeRoot *) root->root.parent;
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(
781 const XMLTreeInfo *xml_info,SplayTreeInfo *attributes)
786 assert(xml_info != (XMLTreeInfo *) NULL);
787 assert((xml_info->signature == MagickCoreSignature) ||
788 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
789 assert(attributes != (SplayTreeInfo *) NULL);
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]));
828MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
833 assert(xml_info != (XMLTreeInfo *) NULL);
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)
870 assert(xml_info != (XMLTreeInfo *) NULL);
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);
900MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
902 assert(xml_info != (XMLTreeInfo *) NULL);
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);
935MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
940 subnode[MagickPathExtent],
941 tag[MagickPathExtent];
953 assert(xml_info != (XMLTreeInfo *) NULL);
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)
961 return((XMLTreeInfo *) 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);
967 if (node == (XMLTreeInfo *) NULL)
969 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
971 node=GetXMLTreeOrdered(node);
972 if (node == (XMLTreeInfo *) NULL)
975 if (node == (XMLTreeInfo *) NULL)
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(
1010 XMLTreeInfo *xml_info,
const char *target)
1018 assert(xml_info != (XMLTreeInfo *) NULL);
1019 assert((xml_info->signature == MagickCoreSignature) ||
1020 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1021 if (IsEventLogging() != MagickFalse)
1022 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1023 root=(XMLTreeRoot *) xml_info;
1024 while (root->root.parent != (XMLTreeInfo *) NULL)
1025 root=(XMLTreeRoot *) root->root.parent;
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));
1057MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1059 assert(xml_info != (XMLTreeInfo *) NULL);
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)
1091 assert(xml_info != (XMLTreeInfo *) NULL);
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);
1127MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1128 XMLTreeInfo *child,
const size_t offset)
1135 child->ordered=(XMLTreeInfo *) NULL;
1136 child->sibling=(XMLTreeInfo *) NULL;
1137 child->next=(XMLTreeInfo *) NULL;
1138 child->offset=offset;
1139 child->parent=xml_info;
1140 if (xml_info->child == (XMLTreeInfo *) NULL)
1142 xml_info->child=child;
1145 head=xml_info->child;
1146 if (head->offset > offset)
1148 child->ordered=head;
1149 xml_info->child=child;
1154 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1155 (node->ordered->offset <= offset))
1157 child->ordered=node->ordered;
1158 node->ordered=child;
1160 previous=(XMLTreeInfo *) NULL;
1162 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1167 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1169 while ((node->next != (XMLTreeInfo *) NULL) &&
1170 (node->next->offset <= offset))
1172 child->next=node->next;
1177 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1178 previous->sibling=node->sibling;
1180 previous=(XMLTreeInfo *) NULL;
1182 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1187 child->sibling=node;
1188 if (previous != (XMLTreeInfo *) NULL)
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));
1413 if (p != (
char *) NULL)
1421 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1422 sizeof(*extent_xml));
1423 if (extent_xml != (
char *) NULL)
1425 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1426 (void) CopyMagickString(extent_xml,p,extent*
1427 sizeof(*extent_xml));
1431 if (p == (
char *) NULL)
1432 ThrowFatalException(ResourceLimitFatalError,
1433 "MemoryAllocationFailed");
1435 entity=strchr(xml,
';');
1437 if (entity != (
char *) NULL)
1438 (void) memmove(xml+length,entity+1,strlen(entity));
1439 (void) memcpy(xml,entities[i],length);
1443 if (((state ==
' ') || (state ==
'*')) &&
1444 (isspace((
int) ((
unsigned char) *xml)) != 0))
1454 for (xml=p; *xml !=
'\0'; xml++)
1459 i=(ssize_t) strspn(xml,accept);
1461 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1462 while ((*xml !=
'\0') && (*xml !=
' '))
1468 if ((xml >= p) && (*xml ==
' '))
1471 return(p == q ? ConstantString(p) : p);
1474static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1475 const size_t length,
const char state)
1480 xml_info=root->node;
1481 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1485 xml=ParseEntities(xml,root->entities,state);
1486 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1488 (void) ConcatenateString(&xml_info->content,xml);
1489 xml=DestroyString(xml);
1493 if (xml_info->content != (
char *) NULL)
1494 xml_info->content=DestroyString(xml_info->content);
1495 xml_info->content=xml;
1499static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1500 ExceptionInfo *exception)
1502 if ((root->node == (XMLTreeInfo *) NULL) ||
1503 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1505 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1506 "ParseError",
"unexpected closing tag </%s>",tag);
1507 return(&root->root);
1509 root->node=root->node->parent;
1510 return((XMLTreeInfo *) NULL);
1513static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1514 const size_t depth,
char **entities)
1522 if (depth > MagickMaxRecursionDepth)
1523 return(MagickFalse);
1526 while ((*xml !=
'\0') && (*xml !=
'&'))
1530 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1531 return(MagickFalse);
1533 while ((entities[i] != (
char *) NULL) &&
1534 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1536 if ((entities[i] != (
char *) NULL) &&
1537 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1538 return(MagickFalse);
1542static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1554 xml+=strcspn(xml,XMLWhitespace);
1558 xml+=strspn(xml+1,XMLWhitespace)+1;
1560 if (strcmp(target,
"xml") == 0)
1562 xml=strstr(xml,
"standalone");
1563 if ((xml != (
char *) NULL) &&
1564 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1565 root->standalone=MagickTrue;
1568 if (root->processing_instructions[0] == (
char **) NULL)
1570 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1571 *root->processing_instructions));
1572 *root->processing_instructions=(
char **) NULL;
1575 while ((root->processing_instructions[i] != (
char **) NULL) &&
1576 (strcmp(target,root->processing_instructions[i][0]) != 0))
1578 if (root->processing_instructions[i] == (
char **) NULL)
1580 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1581 root->processing_instructions,(
size_t) (i+2),
1582 sizeof(*root->processing_instructions));
1583 if (root->processing_instructions == (
char ***) NULL)
1584 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1585 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1586 sizeof(**root->processing_instructions));
1587 if (root->processing_instructions[i] == (
char **) NULL)
1588 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1589 root->processing_instructions[i+1]=(
char **) NULL;
1590 root->processing_instructions[i][0]=ConstantString(target);
1591 root->processing_instructions[i][1]=(
char *)
1592 root->processing_instructions[i+1];
1593 root->processing_instructions[i+1]=(
char **) NULL;
1594 root->processing_instructions[i][2]=ConstantString(
"");
1597 while (root->processing_instructions[i][j] != (
char *) NULL)
1599 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1600 root->processing_instructions[i],(
size_t) (j+3),
1601 sizeof(**root->processing_instructions));
1602 if (root->processing_instructions[i] == (
char **) NULL)
1603 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1604 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1605 root->processing_instructions[i][j+1],(
size_t) (j+1),
1606 sizeof(***root->processing_instructions));
1607 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1608 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1609 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1610 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1611 root->processing_instructions[i][j]=ConstantString(xml);
1612 root->processing_instructions[i][j+1]=(
char *) NULL;
1615static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1616 size_t length,ExceptionInfo *exception)
1622 **predefined_entities,
1632 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1633 if (predefined_entities == (
char **) NULL)
1634 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1635 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1636 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1638 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1642 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1647 if (strspn(xml+8,XMLWhitespace) == 0)
1649 xml+=strspn(xml+8,XMLWhitespace)+8;
1651 n=xml+strspn(xml,XMLWhitespace
"%");
1652 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1654 xml=n+strcspn(n,XMLWhitespace);
1658 v=xml+strspn(xml+1,XMLWhitespace)+1;
1661 if ((q !=
'"') && (q !=
'\''))
1666 xml=strchr(xml,
'>');
1669 entities=(*c ==
'%') ? predefined_entities : root->entities;
1670 for (i=0; entities[i] != (
char *) NULL; i++) ;
1671 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1673 if (entities == (
char **) NULL)
1674 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1676 predefined_entities=entities;
1678 root->entities=entities;
1682 if (xml != (
char *) NULL)
1687 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1688 entities[i+2]=(
char *) NULL;
1689 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1693 if (entities[i+1] != v)
1694 entities[i+1]=DestroyString(entities[i+1]);
1695 (void) ThrowMagickException(exception,GetMagickModule(),
1696 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1697 predefined_entities=(
char **) RelinquishMagickMemory(
1698 predefined_entities);
1699 return(MagickFalse);
1703 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1708 t=xml+strspn(xml+9,XMLWhitespace)+9;
1711 (void) ThrowMagickException(exception,GetMagickModule(),
1712 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1713 predefined_entities=(
char **) RelinquishMagickMemory(
1714 predefined_entities);
1715 return(MagickFalse);
1717 xml=t+strcspn(t,XMLWhitespace
">");
1722 while ((root->attributes[i] != (
char **) NULL) &&
1723 (n != (
char *) NULL) &&
1724 (strcmp(n,root->attributes[i][0]) != 0))
1726 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1729 xml=n+strcspn(n,XMLWhitespace);
1734 (void) ThrowMagickException(exception,GetMagickModule(),
1735 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1736 predefined_entities=(
char **) RelinquishMagickMemory(
1737 predefined_entities);
1738 return(MagickFalse);
1740 xml+=strspn(xml+1,XMLWhitespace)+1;
1741 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1742 if (strncmp(xml,
"NOTATION",8) == 0)
1743 xml+=strspn(xml+8,XMLWhitespace)+8;
1744 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1745 strcspn(xml,XMLWhitespace);
1746 if (xml == (
char *) NULL)
1748 (void) ThrowMagickException(exception,GetMagickModule(),
1749 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1750 predefined_entities=(
char **) RelinquishMagickMemory(
1751 predefined_entities);
1752 return(MagickFalse);
1754 xml+=strspn(xml,XMLWhitespace
")");
1755 if (strncmp(xml,
"#FIXED",6) == 0)
1756 xml+=strspn(xml+6,XMLWhitespace)+6;
1759 xml+=strcspn(xml,XMLWhitespace
">")-1;
1765 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1766 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1770 (void) ThrowMagickException(exception,GetMagickModule(),
1771 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1772 predefined_entities=(
char **) RelinquishMagickMemory(
1773 predefined_entities);
1774 return(MagickFalse);
1776 if (root->attributes[i] == (
char **) NULL)
1782 root->attributes=(
char ***) AcquireQuantumMemory(2,
1783 sizeof(*root->attributes));
1785 root->attributes=(
char ***) ResizeQuantumMemory(
1786 root->attributes,(
size_t) (i+2),
1787 sizeof(*root->attributes));
1788 if (root->attributes == (
char ***) NULL)
1789 ThrowFatalException(ResourceLimitFatalError,
1790 "MemoryAllocationFailed");
1791 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1792 sizeof(**root->attributes));
1793 if (root->attributes[i] == (
char **) NULL)
1794 ThrowFatalException(ResourceLimitFatalError,
1795 "MemoryAllocationFailed");
1796 root->attributes[i][0]=ConstantString(t);
1797 root->attributes[i][1]=(
char *) NULL;
1798 root->attributes[i+1]=(
char **) NULL;
1800 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1801 root->attributes[i]=(
char **) ResizeQuantumMemory(
1802 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1803 if (root->attributes[i] == (
char **) NULL)
1804 ThrowFatalException(ResourceLimitFatalError,
1805 "MemoryAllocationFailed");
1806 root->attributes[i][j+3]=(
char *) NULL;
1807 root->attributes[i][j+2]=ConstantString(c);
1808 root->attributes[i][j+1]=(
char *) NULL;
1809 if (v != (
char *) NULL)
1810 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1811 root->attributes[i][j]=ConstantString(n);
1815 if (strncmp(xml,
"<!--", 4) == 0)
1816 xml=strstr(xml+4,
"-->");
1818 if (strncmp(xml,
"<?", 2) == 0)
1822 if (xml != (
char *) NULL)
1824 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1830 xml=strchr(xml,
'>');
1832 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1835 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1839static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
1844 xml_info=root->node;
1845 if (xml_info->tag == (
char *) NULL)
1846 xml_info->tag=ConstantString(tag);
1848 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1849 if (xml_info != (XMLTreeInfo *) NULL)
1850 xml_info->attributes=attributes;
1851 root->node=xml_info;
1862static inline MagickBooleanType IsSkipTag(
const char *tag)
1868 while (ignore_tags[i] != (
const char *) NULL)
1870 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1874 return(MagickFalse);
1877MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1908 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1910 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1911 "ParseError",
"root tag missing");
1912 return((XMLTreeInfo *) NULL);
1914 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1916 utf8=ConvertUTF16ToUTF8(xml,&length);
1917 if (utf8 == (
char *) NULL)
1919 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1920 "ParseError",
"UTF16 to UTF8 failed");
1921 return((XMLTreeInfo *) NULL);
1925 utf8=DestroyString(utf8);
1926 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1927 "ParseError",
"root tag missing");
1928 return((XMLTreeInfo *) NULL);
1930 terminal=utf8[length-1];
1931 utf8[length-1]=
'\0';
1933 while ((*p !=
'\0') && (*p !=
'<'))
1937 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1938 "ParseError",
"root tag missing");
1939 utf8=DestroyString(utf8);
1940 return((XMLTreeInfo *) NULL);
1942 attribute=(
char **) NULL;
1947 attributes=(
char **) sentinel;
1950 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1951 (*p ==
':') || (c <
'\0'))
1956 if (root->node == (XMLTreeInfo *) NULL)
1958 (void) ThrowMagickException(exception,GetMagickModule(),
1959 OptionWarning,
"ParseError",
"root tag missing");
1960 utf8=DestroyString(utf8);
1961 return(&root->root);
1963 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1964 while (isspace((
int) ((
unsigned char) *p)) != 0)
1966 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1967 (ignore_depth == 0))
1969 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1975 while ((root->attributes[i] != (
char **) NULL) &&
1976 (strcmp(root->attributes[i][0],tag) != 0))
1978 attribute=root->attributes[i];
1980 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1986 attributes=(
char **) AcquireQuantumMemory(4,
1987 sizeof(*attributes));
1989 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1990 (l+4),
sizeof(*attributes));
1991 if (attributes == (
char **) NULL)
1993 (void) ThrowMagickException(exception,GetMagickModule(),
1994 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1995 utf8=DestroyString(utf8);
1996 return(&root->root);
1998 attributes[l+2]=(
char *) NULL;
1999 attributes[l+1]=(
char *) NULL;
2001 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
2002 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
2003 attributes[l]=ConstantString(
"");
2007 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2009 if ((c ==
'"') || (c ==
'\''))
2016 while ((*p !=
'\0') && (*p != c))
2022 attributes[l]=ConstantString(
"");
2023 attributes[l+1]=ConstantString(
"");
2024 (void) DestroyXMLTreeAttributes(attributes);
2025 (void) ThrowMagickException(exception,
2026 GetMagickModule(),OptionWarning,
"ParseError",
2028 utf8=DestroyString(utf8);
2029 return(&root->root);
2032 while ((attribute != (
char **) NULL) &&
2033 (attribute[j] != (
char *) NULL) &&
2034 (strcmp(attribute[j],attributes[l]) != 0))
2036 attributes[l+1]=ParseEntities(attributes[l+1],
2037 root->entities,(attribute != (
char **) NULL) &&
2038 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2041 attributes[l]=ConstantString(attributes[l]);
2043 while (isspace((
int) ((
unsigned char) *p)) != 0)
2049 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2058 if (((*p !=
'\0') && (*p !=
'>')) ||
2059 ((*p ==
'\0') && (terminal !=
'>')))
2062 (void) DestroyXMLTreeAttributes(attributes);
2063 (void) ThrowMagickException(exception,GetMagickModule(),
2064 OptionWarning,
"ParseError",
"missing >");
2065 utf8=DestroyString(utf8);
2066 return(&root->root);
2068 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2069 (void) DestroyXMLTreeAttributes(attributes);
2072 ParseOpenTag(root,tag,attributes);
2073 (void) ParseCloseTag(root,tag,exception);
2079 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2082 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2083 ParseOpenTag(root,tag,attributes);
2087 (void) DestroyXMLTreeAttributes(attributes);
2094 (void) DestroyXMLTreeAttributes(attributes);
2095 (void) ThrowMagickException(exception,GetMagickModule(),
2096 OptionWarning,
"ParseError",
"missing >");
2097 utf8=DestroyString(utf8);
2098 return(&root->root);
2109 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2111 if ((c ==
'\0') && (terminal !=
'>'))
2113 (void) ThrowMagickException(exception,GetMagickModule(),
2114 OptionWarning,
"ParseError",
"missing >");
2115 utf8=DestroyString(utf8);
2116 return(&root->root);
2119 if ((ignore_depth == 0) &&
2120 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2122 utf8=DestroyString(utf8);
2123 return(&root->root);
2125 if (ignore_depth > 0)
2128 if (isspace((
int) ((
unsigned char) *p)) != 0)
2129 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2132 if (strncmp(p,
"!--",3) == 0)
2138 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2139 ((*p ==
'\0') && (terminal !=
'>')))
2141 (void) ThrowMagickException(exception,GetMagickModule(),
2142 OptionWarning,
"ParseError",
"unclosed <!--");
2143 utf8=DestroyString(utf8);
2144 return(&root->root);
2148 if (strncmp(p,
"![CDATA[",8) == 0)
2154 if (p != (
char *) NULL)
2157 if (ignore_depth == 0)
2158 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2162 (void) ThrowMagickException(exception,GetMagickModule(),
2163 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2164 utf8=DestroyString(utf8);
2165 return(&root->root);
2169 if (strncmp(p,
"!DOCTYPE",8) == 0)
2174 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2175 ((l != 0) && ((*p !=
']') ||
2176 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2177 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2178 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2179 if ((*p ==
'\0') && (terminal !=
'>'))
2181 (void) ThrowMagickException(exception,GetMagickModule(),
2182 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2183 utf8=DestroyString(utf8);
2184 return(&root->root);
2187 tag=strchr(tag,
'[')+1;
2190 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2192 if (status == MagickFalse)
2194 utf8=DestroyString(utf8);
2195 return(&root->root);
2209 if (p == (
char *) NULL)
2212 }
while ((*p !=
'\0') && (*p !=
'>'));
2213 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2216 (void) ThrowMagickException(exception,GetMagickModule(),
2217 OptionWarning,
"ParseError",
"unclosed <?");
2218 utf8=DestroyString(utf8);
2219 return(&root->root);
2221 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2225 (void) ThrowMagickException(exception,GetMagickModule(),
2226 OptionWarning,
"ParseError",
"unexpected <");
2227 utf8=DestroyString(utf8);
2228 return(&root->root);
2230 if ((p == (
char *) NULL) || (*p ==
'\0'))
2234 if ((*p !=
'\0') && (*p !=
'<'))
2239 while ((*p !=
'\0') && (*p !=
'<'))
2243 if (ignore_depth == 0)
2244 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2250 utf8=DestroyString(utf8);
2251 if (root->node == (XMLTreeInfo *) NULL)
2252 return(&root->root);
2253 if (root->node->tag == (
char *) NULL)
2255 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2256 "ParseError",
"root tag missing");
2257 return(&root->root);
2259 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2260 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2261 return(&root->root);
2286MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2289 *predefined_entities[NumberPredefinedEntities+1] =
2291 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2292 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2298 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2299 (void) memset(root,0,
sizeof(*root));
2300 root->root.tag=(
char *) NULL;
2301 if (tag != (
char *) NULL)
2302 root->root.tag=ConstantString(tag);
2303 root->node=(&root->root);
2304 root->root.content=ConstantString(
"");
2305 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2306 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2307 root->root.attributes=sentinel;
2308 root->attributes=(
char ***) root->root.attributes;
2309 root->processing_instructions=(
char ***) root->root.attributes;
2310 root->debug=IsEventLogging();
2311 root->signature=MagickCoreSignature;
2312 return(&root->root);
2338MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2343 assert(xml_info != (XMLTreeInfo *) NULL);
2344 assert((xml_info->signature == MagickCoreSignature) ||
2345 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2346 if (IsEventLogging() != MagickFalse)
2347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2348 if (xml_info->next != (XMLTreeInfo *) NULL)
2349 xml_info->next->sibling=xml_info->sibling;
2350 if (xml_info->parent != (XMLTreeInfo *) NULL)
2352 node=xml_info->parent->child;
2353 if (node == xml_info)
2354 xml_info->parent->child=xml_info->ordered;
2357 while (node->ordered != xml_info)
2359 node->ordered=node->ordered->ordered;
2360 node=xml_info->parent->child;
2361 if (strcmp(node->tag,xml_info->tag) != 0)
2363 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2365 if (node->sibling != xml_info)
2368 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2369 xml_info->next : node->sibling->sibling;
2371 while ((node->next != (XMLTreeInfo *) NULL) &&
2372 (node->next != xml_info))
2374 if (node->next != (XMLTreeInfo *) NULL)
2375 node->next=node->next->next;
2378 xml_info->ordered=(XMLTreeInfo *) NULL;
2379 xml_info->sibling=(XMLTreeInfo *) NULL;
2380 xml_info->next=(XMLTreeInfo *) NULL;
2412MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2413 const char *tag,
const char *value)
2419 assert(xml_info != (XMLTreeInfo *) NULL);
2420 assert((xml_info->signature == MagickCoreSignature) ||
2421 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2422 if (IsEventLogging() != MagickFalse)
2423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2425 while ((xml_info->attributes[i] != (
char *) NULL) &&
2426 (strcmp(xml_info->attributes[i],tag) != 0))
2428 if (xml_info->attributes[i] == (
char *) NULL)
2433 if (value == (
const char *) NULL)
2435 if (xml_info->attributes != sentinel)
2436 xml_info->attributes=(
char **) ResizeQuantumMemory(
2437 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2440 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2441 sizeof(*xml_info->attributes));
2442 if (xml_info->attributes != (
char **) NULL)
2443 xml_info->attributes[1]=ConstantString(
"");
2445 if (xml_info->attributes == (
char **) NULL)
2446 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2447 xml_info->attributes[i]=ConstantString(tag);
2448 xml_info->attributes[i+2]=(
char *) NULL;
2449 (void) strlen(xml_info->attributes[i+1]);
2454 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2455 if (xml_info->attributes[i+1] != (
char *) NULL)
2456 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2457 if (value != (
const char *) NULL)
2459 xml_info->attributes[i+1]=ConstantString(value);
2462 if (xml_info->attributes[i] != (
char *) NULL)
2463 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2464 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(
size_t)
2465 (j-i)*
sizeof(*xml_info->attributes));
2466 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2467 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2468 if (xml_info->attributes == (
char **) NULL)
2469 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2471 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2472 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2502MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2503 const char *content)
2505 assert(xml_info != (XMLTreeInfo *) NULL);
2506 assert((xml_info->signature == MagickCoreSignature) ||
2507 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2508 if (IsEventLogging() != MagickFalse)
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2510 if (xml_info->content != (
char *) NULL)
2511 xml_info->content=DestroyString(xml_info->content);
2512 xml_info->content=(
char *) ConstantString(content);
2539static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2540 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2546 canonical_content=CanonicalXMLContent(source,pedantic);
2552 content=AcquireString(source);
2553 content[offset]=
'\0';
2554 canonical_content=CanonicalXMLContent(content,pedantic);
2555 content=DestroyString(content);
2557 if (canonical_content == (
char *) NULL)
2558 return(*destination);
2559 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2561 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2562 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2563 sizeof(**destination));
2564 if (*destination == (
char *) NULL)
2565 return(*destination);
2567 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2569 canonical_content=DestroyString(canonical_content);
2570 return(*destination);
2573static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2574 size_t *extent,
size_t start,
char ***attributes)
2589 content=(
char *)
"";
2590 if (xml_info->parent != (XMLTreeInfo *) NULL)
2591 content=xml_info->parent->content;
2593 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2594 start),source,length,extent,MagickFalse);
2595 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2597 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2598 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2599 if (*source == (
char *) NULL)
2602 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2603 "<%s",xml_info->tag);
2604 for (i=0; xml_info->attributes[i]; i+=2)
2606 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2607 if (attribute != xml_info->attributes[i+1])
2609 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2611 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2612 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2613 if (*source == (
char *) NULL)
2614 return((
char *) NULL);
2616 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2617 xml_info->attributes[i]);
2618 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2620 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2623 while ((attributes[i] != (
char **) NULL) &&
2624 (strcmp(attributes[i][0],xml_info->tag) != 0))
2627 while ((attributes[i] != (
char **) NULL) &&
2628 (attributes[i][j] != (
char *) NULL))
2630 if ((attributes[i][j+1] == (
char *) NULL) ||
2631 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2636 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2638 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2639 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2640 if (*source == (
char *) NULL)
2641 return((
char *) NULL);
2643 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2645 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2647 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2650 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2651 *xml_info->content ?
">" :
"/>");
2652 if (xml_info->child != (XMLTreeInfo *) NULL)
2653 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2655 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2657 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2659 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2660 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2661 if (*source == (
char *) NULL)
2662 return((
char *) NULL);
2664 if (*xml_info->content !=
'\0')
2665 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2667 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2669 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2670 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2673 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2678MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2701 assert(xml_info != (XMLTreeInfo *) NULL);
2702 assert((xml_info->signature == MagickCoreSignature) ||
2703 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2704 if (IsEventLogging() != MagickFalse)
2705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2706 if (xml_info->tag == (
char *) NULL)
2707 return((
char *) NULL);
2708 xml=AcquireString((
char *) NULL);
2710 extent=MagickPathExtent;
2711 root=(XMLTreeRoot *) xml_info;
2712 while (root->root.parent != (XMLTreeInfo *) NULL)
2713 root=(XMLTreeRoot *) root->root.parent;
2714 parent=xml_info->parent;
2715 if (parent == (XMLTreeInfo *) NULL)
2716 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2721 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2722 p=root->processing_instructions[i][1];
2723 for (j=1; p != (
char *) NULL; j++)
2725 if (root->processing_instructions[i][k][j-1] ==
'>')
2727 p=root->processing_instructions[i][j];
2730 q=root->processing_instructions[i][0];
2731 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2733 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2734 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2735 if (xml == (
char *) NULL)
2738 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2739 *p !=
'\0' ?
" " :
"",p);
2740 p=root->processing_instructions[i][j];
2743 ordered=xml_info->ordered;
2744 xml_info->parent=(XMLTreeInfo *) NULL;
2745 xml_info->ordered=(XMLTreeInfo *) NULL;
2746 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2747 xml_info->parent=parent;
2748 xml_info->ordered=ordered;
2749 if (parent == (XMLTreeInfo *) NULL)
2750 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2755 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2756 p=root->processing_instructions[i][1];
2757 for (j=1; p != (
char *) NULL; j++)
2759 if (root->processing_instructions[i][k][j-1] ==
'<')
2761 p=root->processing_instructions[i][j];
2764 q=root->processing_instructions[i][0];
2765 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2767 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2768 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2769 if (xml == (
char *) NULL)
2772 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2773 *p !=
'\0' ?
" " :
"",p);
2774 p=root->processing_instructions[i][j];
2777 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));