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=MagickRead(file,xml+i,quantum);
586 if (~((
size_t) i) < (quantum+1))
588 xml=(
char *) RelinquishMagickMemory(xml);
591 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
592 if ((i+(
size_t) count) >= extent)
595 if (LocaleCompare(filename,
"-") != 0)
596 file=close_utf8(file);
597 if (xml == (
char *) NULL)
598 return((
char *) NULL);
601 xml=(
char *) RelinquishMagickMemory(xml);
602 return((
char *) NULL);
604 length=MagickMin(i+(
size_t) count,extent);
608 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
610 if (~length >= (MagickPathExtent-1))
611 xml=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*xml));
612 if (xml == (
char *) NULL)
614 file=close_utf8(file);
615 return((
char *) NULL);
617 map=MapBlob(file,ReadMode,0,length);
618 if (map != (
char *) NULL)
620 (void) memcpy(xml,map,length);
621 (void) UnmapBlob(map,length);
625 (void) lseek(file,0,SEEK_SET);
626 for (i=0; i < length; i+=(size_t) count)
628 count=MagickRead(file,xml+i,(
size_t) MagickMin(length-i,(
size_t)
629 MagickMaxBufferExtent));
635 file=close_utf8(file)-1;
636 xml=(
char *) RelinquishMagickMemory(xml);
637 return((
char *) NULL);
641 if (LocaleCompare(filename,
"-") != 0)
642 file=close_utf8(file);
644 xml=(
char *) RelinquishMagickMemory(xml);
670MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
672 assert(xml_info != (XMLTreeInfo *) NULL);
673 assert((xml_info->signature == MagickCoreSignature) ||
674 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
675 if (IsEventLogging() != MagickFalse)
676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
677 return(xml_info->next);
705MagickExport
const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
715 assert(xml_info != (XMLTreeInfo *) NULL);
716 assert((xml_info->signature == MagickCoreSignature) ||
717 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
718 if (IsEventLogging() != MagickFalse)
719 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
720 if (xml_info->attributes == (
char **) NULL)
721 return((
const char *) NULL);
723 while ((xml_info->attributes[i] != (
char *) NULL) &&
724 (strcmp(xml_info->attributes[i],tag) != 0))
726 if (xml_info->attributes[i] != (
char *) NULL)
727 return(xml_info->attributes[i+1]);
728 root=(XMLTreeRoot*) xml_info;
729 while (root->root.parent != (XMLTreeInfo *) NULL)
730 root=(XMLTreeRoot *) root->root.parent;
732 while ((root->attributes[i] != (
char **) NULL) &&
733 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
735 if (root->attributes[i] == (
char **) NULL)
736 return((
const char *) NULL);
738 while ((root->attributes[i][j] != (
char *) NULL) &&
739 (strcmp(root->attributes[i][j],tag) != 0))
741 if (root->attributes[i][j] == (
char *) NULL)
742 return((
const char *) NULL);
743 return(root->attributes[i][j+1]);
772MagickPrivate MagickBooleanType GetXMLTreeAttributes(
773 const XMLTreeInfo *xml_info,SplayTreeInfo *attributes)
778 assert(xml_info != (XMLTreeInfo *) NULL);
779 assert((xml_info->signature == MagickCoreSignature) ||
780 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
781 assert(attributes != (SplayTreeInfo *) NULL);
782 if (IsEventLogging() != MagickFalse)
783 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
784 if (xml_info->attributes == (
char **) NULL)
787 while (xml_info->attributes[i] != (
char *) NULL)
789 (void) AddValueToSplayTree(attributes,
790 ConstantString(xml_info->attributes[i]),
791 ConstantString(xml_info->attributes[i+1]));
820MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
825 assert(xml_info != (XMLTreeInfo *) NULL);
826 assert((xml_info->signature == MagickCoreSignature) ||
827 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
828 if (IsEventLogging() != MagickFalse)
829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
830 child=xml_info->child;
831 if (tag != (
const char *) NULL)
832 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
833 child=child->sibling;
860MagickExport
const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
862 assert(xml_info != (XMLTreeInfo *) NULL);
863 assert((xml_info->signature == MagickCoreSignature) ||
864 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
865 if (IsEventLogging() != MagickFalse)
866 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
867 return(xml_info->content);
892MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
894 assert(xml_info != (XMLTreeInfo *) NULL);
895 assert((xml_info->signature == MagickCoreSignature) ||
896 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
897 if (IsEventLogging() != MagickFalse)
898 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
899 return(xml_info->ordered);
927MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
932 subnode[MagickPathExtent],
933 tag[MagickPathExtent];
945 assert(xml_info != (XMLTreeInfo *) NULL);
946 assert((xml_info->signature == MagickCoreSignature) ||
947 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
948 if (IsEventLogging() != MagickFalse)
949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
951 components=GetPathComponents(path,&number_components);
952 if (components == (
char **) NULL)
953 return((XMLTreeInfo *) NULL);
954 for (i=0; i < (ssize_t) number_components; i++)
956 GetPathComponent(components[i],SubimagePath,subnode);
957 GetPathComponent(components[i],CanonicalPath,tag);
958 node=GetXMLTreeChild(node,tag);
959 if (node == (XMLTreeInfo *) NULL)
961 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
963 node=GetXMLTreeOrdered(node);
964 if (node == (XMLTreeInfo *) NULL)
967 if (node == (XMLTreeInfo *) NULL)
969 components[i]=DestroyString(components[i]);
971 for ( ; i < (ssize_t) number_components; i++)
972 components[i]=DestroyString(components[i]);
973 components=(
char **) RelinquishMagickMemory(components);
1001MagickPrivate
const char **GetXMLTreeProcessingInstructions(
1002 XMLTreeInfo *xml_info,
const char *target)
1010 assert(xml_info != (XMLTreeInfo *) NULL);
1011 assert((xml_info->signature == MagickCoreSignature) ||
1012 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1013 if (IsEventLogging() != MagickFalse)
1014 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1015 root=(XMLTreeRoot *) xml_info;
1016 while (root->root.parent != (XMLTreeInfo *) NULL)
1017 root=(XMLTreeRoot *) root->root.parent;
1019 while ((root->processing_instructions[i] != (
char **) NULL) &&
1020 (strcmp(root->processing_instructions[i][0],target) != 0))
1022 if (root->processing_instructions[i] == (
char **) NULL)
1023 return((
const char **) sentinel);
1024 return((
const char **) (root->processing_instructions[i]+1));
1049MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1051 assert(xml_info != (XMLTreeInfo *) NULL);
1052 assert((xml_info->signature == MagickCoreSignature) ||
1053 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1054 if (IsEventLogging() != MagickFalse)
1055 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1056 return(xml_info->sibling);
1081MagickExport
const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1083 assert(xml_info != (XMLTreeInfo *) NULL);
1084 assert((xml_info->signature == MagickCoreSignature) ||
1085 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1086 if (IsEventLogging() != MagickFalse)
1087 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1088 return(xml_info->tag);
1119MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1120 XMLTreeInfo *child,
const size_t offset)
1127 child->ordered=(XMLTreeInfo *) NULL;
1128 child->sibling=(XMLTreeInfo *) NULL;
1129 child->next=(XMLTreeInfo *) NULL;
1130 child->offset=offset;
1131 child->parent=xml_info;
1132 if (xml_info->child == (XMLTreeInfo *) NULL)
1134 xml_info->child=child;
1137 head=xml_info->child;
1138 if (head->offset > offset)
1140 child->ordered=head;
1141 xml_info->child=child;
1146 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1147 (node->ordered->offset <= offset))
1149 child->ordered=node->ordered;
1150 node->ordered=child;
1152 previous=(XMLTreeInfo *) NULL;
1154 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1159 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1161 while ((node->next != (XMLTreeInfo *) NULL) &&
1162 (node->next->offset <= offset))
1164 child->next=node->next;
1169 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1170 previous->sibling=node->sibling;
1172 previous=(XMLTreeInfo *) NULL;
1174 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1179 child->sibling=node;
1180 if (previous != (XMLTreeInfo *) NULL)
1181 previous->sibling=child;
1212static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1230 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1231 if (utf8 == (
char *) NULL)
1232 return((
char *) NULL);
1233 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1239 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1245 for (i=2; i < (ssize_t) (*length-1); i+=2)
1247 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1248 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1249 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1251 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1252 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1253 (content[i] & 0xff);
1254 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
1256 if ((
size_t) (j+MagickPathExtent) > extent)
1258 extent=(size_t) j+MagickPathExtent;
1259 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1260 if (utf8 == (
char *) NULL)
1273 for (bits=0;
byte != 0;
byte/=2)
1276 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1280 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1285 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1286 if (utf8 != (
char *) NULL)
1291static char *ParseEntities(
char *xml,
char **entities,
int state)
1315 for ( ; *xml !=
'\0'; xml++)
1316 while (*xml ==
'\r')
1320 (void) memmove(xml,xml+1,strlen(xml));
1324 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1325 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1337 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1343 c=strtol(xml+2,&entity,10);
1345 c=strtol(xml+3,&entity,16);
1346 if ((c == 0) || (*entity !=
';'))
1362 for (i=0;
byte != 0;
byte/=2)
1365 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1370 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1374 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1377 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1378 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1384 while ((entities[i] != (
char *) NULL) &&
1385 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1387 if (entities[i++] == (
char *) NULL)
1390 if (entities[i] != (
char *) NULL)
1395 length=strlen(entities[i]);
1396 entity=strchr(xml,
';');
1397 if ((entity != (
char *) NULL) &&
1398 ((length-1L) >= (
size_t) (entity-xml)))
1400 offset=(ssize_t) (xml-p);
1401 extent=((size_t) offset+length+strlen(entity));
1404 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1405 if (p != (
char *) NULL)
1413 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1414 sizeof(*extent_xml));
1415 if (extent_xml != (
char *) NULL)
1417 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1418 (void) CopyMagickString(extent_xml,p,extent*
1419 sizeof(*extent_xml));
1423 if (p == (
char *) NULL)
1424 ThrowFatalException(ResourceLimitFatalError,
1425 "MemoryAllocationFailed");
1427 entity=strchr(xml,
';');
1429 if (entity != (
char *) NULL)
1430 (void) memmove(xml+length,entity+1,strlen(entity));
1431 (void) memcpy(xml,entities[i],length);
1435 if (((state ==
' ') || (state ==
'*')) &&
1436 (isspace((
int) ((
unsigned char) *xml)) != 0))
1446 for (xml=p; *xml !=
'\0'; xml++)
1451 i=(ssize_t) strspn(xml,accept);
1453 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1454 while ((*xml !=
'\0') && (*xml !=
' '))
1460 if ((xml >= p) && (*xml ==
' '))
1463 return(p == q ? ConstantString(p) : p);
1466static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1467 const size_t length,
const char state)
1472 xml_info=root->node;
1473 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1477 xml=ParseEntities(xml,root->entities,state);
1478 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1480 (void) ConcatenateString(&xml_info->content,xml);
1481 xml=DestroyString(xml);
1485 if (xml_info->content != (
char *) NULL)
1486 xml_info->content=DestroyString(xml_info->content);
1487 xml_info->content=xml;
1491static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1492 ExceptionInfo *exception)
1494 if ((root->node == (XMLTreeInfo *) NULL) ||
1495 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1497 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1498 "ParseError",
"unexpected closing tag </%s>",tag);
1499 return(&root->root);
1501 root->node=root->node->parent;
1502 return((XMLTreeInfo *) NULL);
1505static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1506 const size_t depth,
char **entities)
1514 if (depth > MagickMaxRecursionDepth)
1515 return(MagickFalse);
1518 while ((*xml !=
'\0') && (*xml !=
'&'))
1522 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1523 return(MagickFalse);
1525 while ((entities[i] != (
char *) NULL) &&
1526 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1528 if ((entities[i] != (
char *) NULL) &&
1529 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1530 return(MagickFalse);
1534static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1546 xml+=strcspn(xml,XMLWhitespace);
1550 xml+=strspn(xml+1,XMLWhitespace)+1;
1552 if (strcmp(target,
"xml") == 0)
1554 xml=strstr(xml,
"standalone");
1555 if ((xml != (
char *) NULL) &&
1556 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1557 root->standalone=MagickTrue;
1560 if (root->processing_instructions[0] == (
char **) NULL)
1562 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1563 *root->processing_instructions));
1564 *root->processing_instructions=(
char **) NULL;
1567 while ((root->processing_instructions[i] != (
char **) NULL) &&
1568 (strcmp(target,root->processing_instructions[i][0]) != 0))
1570 if (root->processing_instructions[i] == (
char **) NULL)
1572 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1573 root->processing_instructions,(
size_t) (i+2),
1574 sizeof(*root->processing_instructions));
1575 if (root->processing_instructions == (
char ***) NULL)
1576 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1577 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1578 sizeof(**root->processing_instructions));
1579 if (root->processing_instructions[i] == (
char **) NULL)
1580 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1581 root->processing_instructions[i+1]=(
char **) NULL;
1582 root->processing_instructions[i][0]=ConstantString(target);
1583 root->processing_instructions[i][1]=(
char *)
1584 root->processing_instructions[i+1];
1585 root->processing_instructions[i+1]=(
char **) NULL;
1586 root->processing_instructions[i][2]=ConstantString(
"");
1589 while (root->processing_instructions[i][j] != (
char *) NULL)
1591 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1592 root->processing_instructions[i],(
size_t) (j+3),
1593 sizeof(**root->processing_instructions));
1594 if (root->processing_instructions[i] == (
char **) NULL)
1595 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1596 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1597 root->processing_instructions[i][j+1],(
size_t) (j+1),
1598 sizeof(***root->processing_instructions));
1599 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1600 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1601 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1602 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1603 root->processing_instructions[i][j]=ConstantString(xml);
1604 root->processing_instructions[i][j+1]=(
char *) NULL;
1607static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1608 size_t length,ExceptionInfo *exception)
1614 **predefined_entities,
1624 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1625 if (predefined_entities == (
char **) NULL)
1626 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1627 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1628 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1630 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1634 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1639 if (strspn(xml+8,XMLWhitespace) == 0)
1641 xml+=strspn(xml+8,XMLWhitespace)+8;
1643 n=xml+strspn(xml,XMLWhitespace
"%");
1644 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1646 xml=n+strcspn(n,XMLWhitespace);
1650 v=xml+strspn(xml+1,XMLWhitespace)+1;
1653 if ((q !=
'"') && (q !=
'\''))
1658 xml=strchr(xml,
'>');
1661 entities=(*c ==
'%') ? predefined_entities : root->entities;
1662 for (i=0; entities[i] != (
char *) NULL; i++) ;
1663 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1665 if (entities == (
char **) NULL)
1666 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1668 predefined_entities=entities;
1670 root->entities=entities;
1674 if (xml != (
char *) NULL)
1679 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1680 entities[i+2]=(
char *) NULL;
1681 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1685 if (entities[i+1] != v)
1686 entities[i+1]=DestroyString(entities[i+1]);
1687 (void) ThrowMagickException(exception,GetMagickModule(),
1688 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1689 predefined_entities=(
char **) RelinquishMagickMemory(
1690 predefined_entities);
1691 return(MagickFalse);
1695 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1700 t=xml+strspn(xml+9,XMLWhitespace)+9;
1703 (void) ThrowMagickException(exception,GetMagickModule(),
1704 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1705 predefined_entities=(
char **) RelinquishMagickMemory(
1706 predefined_entities);
1707 return(MagickFalse);
1709 xml=t+strcspn(t,XMLWhitespace
">");
1714 while ((root->attributes[i] != (
char **) NULL) &&
1715 (n != (
char *) NULL) &&
1716 (strcmp(n,root->attributes[i][0]) != 0))
1718 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1721 xml=n+strcspn(n,XMLWhitespace);
1726 (void) ThrowMagickException(exception,GetMagickModule(),
1727 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1728 predefined_entities=(
char **) RelinquishMagickMemory(
1729 predefined_entities);
1730 return(MagickFalse);
1732 xml+=strspn(xml+1,XMLWhitespace)+1;
1733 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1734 if (strncmp(xml,
"NOTATION",8) == 0)
1735 xml+=strspn(xml+8,XMLWhitespace)+8;
1736 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1737 strcspn(xml,XMLWhitespace);
1738 if (xml == (
char *) NULL)
1740 (void) ThrowMagickException(exception,GetMagickModule(),
1741 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1742 predefined_entities=(
char **) RelinquishMagickMemory(
1743 predefined_entities);
1744 return(MagickFalse);
1746 xml+=strspn(xml,XMLWhitespace
")");
1747 if (strncmp(xml,
"#FIXED",6) == 0)
1748 xml+=strspn(xml+6,XMLWhitespace)+6;
1751 xml+=strcspn(xml,XMLWhitespace
">")-1;
1757 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1758 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1762 (void) ThrowMagickException(exception,GetMagickModule(),
1763 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1764 predefined_entities=(
char **) RelinquishMagickMemory(
1765 predefined_entities);
1766 return(MagickFalse);
1768 if (root->attributes[i] == (
char **) NULL)
1774 root->attributes=(
char ***) AcquireQuantumMemory(2,
1775 sizeof(*root->attributes));
1777 root->attributes=(
char ***) ResizeQuantumMemory(
1778 root->attributes,(
size_t) (i+2),
1779 sizeof(*root->attributes));
1780 if (root->attributes == (
char ***) NULL)
1781 ThrowFatalException(ResourceLimitFatalError,
1782 "MemoryAllocationFailed");
1783 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1784 sizeof(**root->attributes));
1785 if (root->attributes[i] == (
char **) NULL)
1786 ThrowFatalException(ResourceLimitFatalError,
1787 "MemoryAllocationFailed");
1788 root->attributes[i][0]=ConstantString(t);
1789 root->attributes[i][1]=(
char *) NULL;
1790 root->attributes[i+1]=(
char **) NULL;
1792 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1793 root->attributes[i]=(
char **) ResizeQuantumMemory(
1794 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1795 if (root->attributes[i] == (
char **) NULL)
1796 ThrowFatalException(ResourceLimitFatalError,
1797 "MemoryAllocationFailed");
1798 root->attributes[i][j+3]=(
char *) NULL;
1799 root->attributes[i][j+2]=ConstantString(c);
1800 root->attributes[i][j+1]=(
char *) NULL;
1801 if (v != (
char *) NULL)
1802 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1803 root->attributes[i][j]=ConstantString(n);
1807 if (strncmp(xml,
"<!--", 4) == 0)
1808 xml=strstr(xml+4,
"-->");
1810 if (strncmp(xml,
"<?", 2) == 0)
1814 if (xml != (
char *) NULL)
1816 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1822 xml=strchr(xml,
'>');
1824 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1827 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1831static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
1836 xml_info=root->node;
1837 if (xml_info->tag == (
char *) NULL)
1838 xml_info->tag=ConstantString(tag);
1840 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1841 if (xml_info != (XMLTreeInfo *) NULL)
1842 xml_info->attributes=attributes;
1843 root->node=xml_info;
1854static inline MagickBooleanType IsSkipTag(
const char *tag)
1860 while (ignore_tags[i] != (
const char *) NULL)
1862 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1866 return(MagickFalse);
1869MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1900 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1902 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1903 "ParseError",
"root tag missing");
1904 return((XMLTreeInfo *) NULL);
1906 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1908 utf8=ConvertUTF16ToUTF8(xml,&length);
1909 if (utf8 == (
char *) NULL)
1911 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1912 "ParseError",
"UTF16 to UTF8 failed");
1913 return((XMLTreeInfo *) NULL);
1917 utf8=DestroyString(utf8);
1918 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1919 "ParseError",
"root tag missing");
1920 return((XMLTreeInfo *) NULL);
1922 terminal=utf8[length-1];
1923 utf8[length-1]=
'\0';
1925 while ((*p !=
'\0') && (*p !=
'<'))
1929 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1930 "ParseError",
"root tag missing");
1931 utf8=DestroyString(utf8);
1932 return((XMLTreeInfo *) NULL);
1934 attribute=(
char **) NULL;
1939 attributes=(
char **) sentinel;
1942 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1943 (*p ==
':') || (c <
'\0'))
1948 if (root->node == (XMLTreeInfo *) NULL)
1950 (void) ThrowMagickException(exception,GetMagickModule(),
1951 OptionWarning,
"ParseError",
"root tag missing");
1952 utf8=DestroyString(utf8);
1953 return(&root->root);
1955 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1956 while (isspace((
int) ((
unsigned char) *p)) != 0)
1958 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1959 (ignore_depth == 0))
1961 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1967 while ((root->attributes[i] != (
char **) NULL) &&
1968 (strcmp(root->attributes[i][0],tag) != 0))
1970 attribute=root->attributes[i];
1972 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1978 attributes=(
char **) AcquireQuantumMemory(4,
1979 sizeof(*attributes));
1981 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1982 (l+4),
sizeof(*attributes));
1983 if (attributes == (
char **) NULL)
1985 (void) ThrowMagickException(exception,GetMagickModule(),
1986 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1987 utf8=DestroyString(utf8);
1988 return(&root->root);
1990 attributes[l+2]=(
char *) NULL;
1991 attributes[l+1]=(
char *) NULL;
1993 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1994 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1995 attributes[l]=ConstantString(
"");
1999 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
2001 if ((c ==
'"') || (c ==
'\''))
2008 while ((*p !=
'\0') && (*p != c))
2014 attributes[l]=ConstantString(
"");
2015 attributes[l+1]=ConstantString(
"");
2016 (void) DestroyXMLTreeAttributes(attributes);
2017 (void) ThrowMagickException(exception,
2018 GetMagickModule(),OptionWarning,
"ParseError",
2020 utf8=DestroyString(utf8);
2021 return(&root->root);
2024 while ((attribute != (
char **) NULL) &&
2025 (attribute[j] != (
char *) NULL) &&
2026 (strcmp(attribute[j],attributes[l]) != 0))
2028 attributes[l+1]=ParseEntities(attributes[l+1],
2029 root->entities,(attribute != (
char **) NULL) &&
2030 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2033 attributes[l]=ConstantString(attributes[l]);
2035 while (isspace((
int) ((
unsigned char) *p)) != 0)
2041 while ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2050 if (((*p !=
'\0') && (*p !=
'>')) ||
2051 ((*p ==
'\0') && (terminal !=
'>')))
2054 (void) DestroyXMLTreeAttributes(attributes);
2055 (void) ThrowMagickException(exception,GetMagickModule(),
2056 OptionWarning,
"ParseError",
"missing >");
2057 utf8=DestroyString(utf8);
2058 return(&root->root);
2060 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2061 (void) DestroyXMLTreeAttributes(attributes);
2064 ParseOpenTag(root,tag,attributes);
2065 (void) ParseCloseTag(root,tag,exception);
2071 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2074 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2075 ParseOpenTag(root,tag,attributes);
2079 (void) DestroyXMLTreeAttributes(attributes);
2086 (void) DestroyXMLTreeAttributes(attributes);
2087 (void) ThrowMagickException(exception,GetMagickModule(),
2088 OptionWarning,
"ParseError",
"missing >");
2089 utf8=DestroyString(utf8);
2090 return(&root->root);
2101 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2103 if ((c ==
'\0') && (terminal !=
'>'))
2105 (void) ThrowMagickException(exception,GetMagickModule(),
2106 OptionWarning,
"ParseError",
"missing >");
2107 utf8=DestroyString(utf8);
2108 return(&root->root);
2111 if ((ignore_depth == 0) &&
2112 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2114 utf8=DestroyString(utf8);
2115 return(&root->root);
2117 if (ignore_depth > 0)
2120 if (isspace((
int) ((
unsigned char) *p)) != 0)
2121 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2124 if (strncmp(p,
"!--",3) == 0)
2130 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2131 ((*p ==
'\0') && (terminal !=
'>')))
2133 (void) ThrowMagickException(exception,GetMagickModule(),
2134 OptionWarning,
"ParseError",
"unclosed <!--");
2135 utf8=DestroyString(utf8);
2136 return(&root->root);
2140 if (strncmp(p,
"![CDATA[",8) == 0)
2146 if (p != (
char *) NULL)
2149 if (ignore_depth == 0)
2150 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2154 (void) ThrowMagickException(exception,GetMagickModule(),
2155 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2156 utf8=DestroyString(utf8);
2157 return(&root->root);
2161 if (strncmp(p,
"!DOCTYPE",8) == 0)
2166 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2167 ((l != 0) && ((*p !=
']') ||
2168 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2169 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2170 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2171 if ((*p ==
'\0') && (terminal !=
'>'))
2173 (void) ThrowMagickException(exception,GetMagickModule(),
2174 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2175 utf8=DestroyString(utf8);
2176 return(&root->root);
2179 tag=strchr(tag,
'[')+1;
2182 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2184 if (status == MagickFalse)
2186 utf8=DestroyString(utf8);
2187 return(&root->root);
2201 if (p == (
char *) NULL)
2204 }
while ((*p !=
'\0') && (*p !=
'>'));
2205 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2208 (void) ThrowMagickException(exception,GetMagickModule(),
2209 OptionWarning,
"ParseError",
"unclosed <?");
2210 utf8=DestroyString(utf8);
2211 return(&root->root);
2213 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2217 (void) ThrowMagickException(exception,GetMagickModule(),
2218 OptionWarning,
"ParseError",
"unexpected <");
2219 utf8=DestroyString(utf8);
2220 return(&root->root);
2222 if ((p == (
char *) NULL) || (*p ==
'\0'))
2226 if ((*p !=
'\0') && (*p !=
'<'))
2231 while ((*p !=
'\0') && (*p !=
'<'))
2235 if (ignore_depth == 0)
2236 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2242 utf8=DestroyString(utf8);
2243 if (root->node == (XMLTreeInfo *) NULL)
2244 return(&root->root);
2245 if (root->node->tag == (
char *) NULL)
2247 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2248 "ParseError",
"root tag missing");
2249 return(&root->root);
2251 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2252 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2253 return(&root->root);
2278MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2281 *predefined_entities[NumberPredefinedEntities+1] =
2283 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2284 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2290 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2291 (void) memset(root,0,
sizeof(*root));
2292 root->root.tag=(
char *) NULL;
2293 if (tag != (
char *) NULL)
2294 root->root.tag=ConstantString(tag);
2295 root->node=(&root->root);
2296 root->root.content=ConstantString(
"");
2297 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2298 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2299 root->root.attributes=sentinel;
2300 root->attributes=(
char ***) root->root.attributes;
2301 root->processing_instructions=(
char ***) root->root.attributes;
2302 root->debug=IsEventLogging();
2303 root->signature=MagickCoreSignature;
2304 return(&root->root);
2330MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2335 assert(xml_info != (XMLTreeInfo *) NULL);
2336 assert((xml_info->signature == MagickCoreSignature) ||
2337 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2338 if (IsEventLogging() != MagickFalse)
2339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2340 if (xml_info->next != (XMLTreeInfo *) NULL)
2341 xml_info->next->sibling=xml_info->sibling;
2342 if (xml_info->parent != (XMLTreeInfo *) NULL)
2344 node=xml_info->parent->child;
2345 if (node == xml_info)
2346 xml_info->parent->child=xml_info->ordered;
2349 while (node->ordered != xml_info)
2351 node->ordered=node->ordered->ordered;
2352 node=xml_info->parent->child;
2353 if (strcmp(node->tag,xml_info->tag) != 0)
2355 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2357 if (node->sibling != xml_info)
2360 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2361 xml_info->next : node->sibling->sibling;
2363 while ((node->next != (XMLTreeInfo *) NULL) &&
2364 (node->next != xml_info))
2366 if (node->next != (XMLTreeInfo *) NULL)
2367 node->next=node->next->next;
2370 xml_info->ordered=(XMLTreeInfo *) NULL;
2371 xml_info->sibling=(XMLTreeInfo *) NULL;
2372 xml_info->next=(XMLTreeInfo *) NULL;
2404MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2405 const char *tag,
const char *value)
2411 assert(xml_info != (XMLTreeInfo *) NULL);
2412 assert((xml_info->signature == MagickCoreSignature) ||
2413 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2414 if (IsEventLogging() != MagickFalse)
2415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2417 while ((xml_info->attributes[i] != (
char *) NULL) &&
2418 (strcmp(xml_info->attributes[i],tag) != 0))
2420 if (xml_info->attributes[i] == (
char *) NULL)
2425 if (value == (
const char *) NULL)
2427 if (xml_info->attributes != sentinel)
2428 xml_info->attributes=(
char **) ResizeQuantumMemory(
2429 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2432 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2433 sizeof(*xml_info->attributes));
2434 if (xml_info->attributes != (
char **) NULL)
2435 xml_info->attributes[1]=ConstantString(
"");
2437 if (xml_info->attributes == (
char **) NULL)
2438 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2439 xml_info->attributes[i]=ConstantString(tag);
2440 xml_info->attributes[i+2]=(
char *) NULL;
2441 (void) strlen(xml_info->attributes[i+1]);
2446 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2447 if (xml_info->attributes[i+1] != (
char *) NULL)
2448 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2449 if (value != (
const char *) NULL)
2451 xml_info->attributes[i+1]=ConstantString(value);
2454 if (xml_info->attributes[i] != (
char *) NULL)
2455 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2456 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(
size_t)
2457 (j-i)*
sizeof(*xml_info->attributes));
2458 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2459 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2460 if (xml_info->attributes == (
char **) NULL)
2461 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2463 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2464 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2494MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2495 const char *content)
2497 assert(xml_info != (XMLTreeInfo *) NULL);
2498 assert((xml_info->signature == MagickCoreSignature) ||
2499 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2500 if (IsEventLogging() != MagickFalse)
2501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2502 if (xml_info->content != (
char *) NULL)
2503 xml_info->content=DestroyString(xml_info->content);
2504 xml_info->content=(
char *) ConstantString(content);
2531static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2532 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2538 canonical_content=CanonicalXMLContent(source,pedantic);
2544 content=AcquireString(source);
2545 content[offset]=
'\0';
2546 canonical_content=CanonicalXMLContent(content,pedantic);
2547 content=DestroyString(content);
2549 if (canonical_content == (
char *) NULL)
2550 return(*destination);
2551 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2553 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2554 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2555 sizeof(**destination));
2556 if (*destination == (
char *) NULL)
2557 return(*destination);
2559 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2561 canonical_content=DestroyString(canonical_content);
2562 return(*destination);
2565static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2566 size_t *extent,
size_t start,
char ***attributes)
2581 content=(
char *)
"";
2582 if (xml_info->parent != (XMLTreeInfo *) NULL)
2583 content=xml_info->parent->content;
2585 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2586 start),source,length,extent,MagickFalse);
2587 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2589 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2590 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2591 if (*source == (
char *) NULL)
2594 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2595 "<%s",xml_info->tag);
2596 for (i=0; xml_info->attributes[i]; i+=2)
2598 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2599 if (attribute != xml_info->attributes[i+1])
2601 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2603 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2604 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2605 if (*source == (
char *) NULL)
2606 return((
char *) NULL);
2608 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2609 xml_info->attributes[i]);
2610 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2612 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2615 while ((attributes[i] != (
char **) NULL) &&
2616 (strcmp(attributes[i][0],xml_info->tag) != 0))
2619 while ((attributes[i] != (
char **) NULL) &&
2620 (attributes[i][j] != (
char *) NULL))
2622 if ((attributes[i][j+1] == (
char *) NULL) ||
2623 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2628 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2630 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2631 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2632 if (*source == (
char *) NULL)
2633 return((
char *) NULL);
2635 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2637 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2639 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2642 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2643 *xml_info->content ?
">" :
"/>");
2644 if (xml_info->child != (XMLTreeInfo *) NULL)
2645 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2647 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2649 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2651 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2652 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2653 if (*source == (
char *) NULL)
2654 return((
char *) NULL);
2656 if (*xml_info->content !=
'\0')
2657 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2659 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2661 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2662 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2665 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2670MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2693 assert(xml_info != (XMLTreeInfo *) NULL);
2694 assert((xml_info->signature == MagickCoreSignature) ||
2695 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2696 if (IsEventLogging() != MagickFalse)
2697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2698 if (xml_info->tag == (
char *) NULL)
2699 return((
char *) NULL);
2700 xml=AcquireString((
char *) NULL);
2702 extent=MagickPathExtent;
2703 root=(XMLTreeRoot *) xml_info;
2704 while (root->root.parent != (XMLTreeInfo *) NULL)
2705 root=(XMLTreeRoot *) root->root.parent;
2706 parent=xml_info->parent;
2707 if (parent == (XMLTreeInfo *) NULL)
2708 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2713 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2714 p=root->processing_instructions[i][1];
2715 for (j=1; p != (
char *) NULL; j++)
2717 if (root->processing_instructions[i][k][j-1] ==
'>')
2719 p=root->processing_instructions[i][j];
2722 q=root->processing_instructions[i][0];
2723 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2725 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2726 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2727 if (xml == (
char *) NULL)
2730 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2731 *p !=
'\0' ?
" " :
"",p);
2732 p=root->processing_instructions[i][j];
2735 ordered=xml_info->ordered;
2736 xml_info->parent=(XMLTreeInfo *) NULL;
2737 xml_info->ordered=(XMLTreeInfo *) NULL;
2738 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2739 xml_info->parent=parent;
2740 xml_info->ordered=ordered;
2741 if (parent == (XMLTreeInfo *) NULL)
2742 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2747 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2748 p=root->processing_instructions[i][1];
2749 for (j=1; p != (
char *) NULL; j++)
2751 if (root->processing_instructions[i][k][j-1] ==
'<')
2753 p=root->processing_instructions[i][j];
2756 q=root->processing_instructions[i][0];
2757 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2759 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2760 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2761 if (xml == (
char *) NULL)
2764 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2765 *p !=
'\0' ?
" " :
"",p);
2766 p=root->processing_instructions[i][j];
2769 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));