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);
362static char **DestroyXMLTreeAttributes(
char **attributes)
370 if ((attributes == (
char **) NULL) || (attributes == sentinel))
371 return((
char **) NULL);
372 for (i=0; attributes[i] != (
char *) NULL; i+=2)
377 if (attributes[i] != (
char *) NULL)
378 attributes[i]=DestroyString(attributes[i]);
379 if (attributes[i+1] != (
char *) NULL)
380 attributes[i+1]=DestroyString(attributes[i+1]);
382 attributes=(
char **) RelinquishMagickMemory(attributes);
383 return((
char **) NULL);
386static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
392 child=xml_info->child;
393 while(child != (XMLTreeInfo *) NULL)
397 node->child=(XMLTreeInfo *) NULL;
398 (void) DestroyXMLTree(node);
402static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
408 ordered=xml_info->ordered;
409 while(ordered != (XMLTreeInfo *) NULL)
412 ordered=node->ordered;
413 node->ordered=(XMLTreeInfo *) NULL;
414 (void) DestroyXMLTree(node);
418static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
430 assert(xml_info != (XMLTreeInfo *) NULL);
431 assert((xml_info->signature == MagickCoreSignature) ||
432 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
433 if (IsEventLogging() != MagickFalse)
434 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
435 if (xml_info->parent != (XMLTreeInfo *) NULL)
440 root=(XMLTreeRoot *) xml_info;
441 for (i=NumberPredefinedEntities; root->entities[i] != (
char *) NULL; i+=2)
442 root->entities[i+1]=DestroyString(root->entities[i+1]);
443 root->entities=(
char **) RelinquishMagickMemory(root->entities);
444 for (i=0; root->attributes[i] != (
char **) NULL; i++)
446 attributes=root->attributes[i];
447 if (attributes[0] != (
char *) NULL)
448 attributes[0]=DestroyString(attributes[0]);
449 for (j=1; attributes[j] != (
char *) NULL; j+=3)
451 if (attributes[j] != (
char *) NULL)
452 attributes[j]=DestroyString(attributes[j]);
453 if (attributes[j+1] != (
char *) NULL)
454 attributes[j+1]=DestroyString(attributes[j+1]);
455 if (attributes[j+2] != (
char *) NULL)
456 attributes[j+2]=DestroyString(attributes[j+2]);
458 attributes=(
char **) RelinquishMagickMemory(attributes);
460 if (root->attributes[0] != (
char **) NULL)
461 root->attributes=(
char ***) RelinquishMagickMemory(root->attributes);
462 if (root->processing_instructions[0] != (
char **) NULL)
464 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
466 for (j=0; root->processing_instructions[i][j] != (
char *) NULL; j++)
467 root->processing_instructions[i][j]=DestroyString(
468 root->processing_instructions[i][j]);
469 root->processing_instructions[i][j+1]=DestroyString(
470 root->processing_instructions[i][j+1]);
471 root->processing_instructions[i]=(
char **) RelinquishMagickMemory(
472 root->processing_instructions[i]);
474 root->processing_instructions=(
char ***) RelinquishMagickMemory(
475 root->processing_instructions);
479MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
481 assert(xml_info != (XMLTreeInfo *) NULL);
482 assert((xml_info->signature == MagickCoreSignature) ||
483 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
484 if (IsEventLogging() != MagickFalse)
485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
486 DestroyXMLTreeChild(xml_info);
487 DestroyXMLTreeOrdered(xml_info);
488 DestroyXMLTreeRoot(xml_info);
489 xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
490 xml_info->content=DestroyString(xml_info->content);
491 xml_info->tag=DestroyString(xml_info->tag);
492 xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
493 return((XMLTreeInfo *) NULL);
520MagickPrivate
char *FileToXML(
const char *filename,
const size_t extent)
541 assert(filename != (
const char *) NULL);
544 if (LocaleCompare(filename,
"-") != 0)
545 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
547 return((
char *) NULL);
548 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
550 if ((file == fileno(stdin)) || (offset < 0) ||
551 (offset != (MagickOffsetType) ((ssize_t) offset)))
562 offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
563 quantum=(size_t) MagickMaxBufferExtent;
564 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
565 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
566 xml=(
char *) AcquireQuantumMemory(quantum,
sizeof(*xml));
567 for (i=0; xml != (
char *) NULL; i+=(size_t) count)
569 count=read(file,xml+i,quantum);
576 if (~((
size_t) i) < (quantum+1))
578 xml=(
char *) RelinquishMagickMemory(xml);
581 xml=(
char *) ResizeQuantumMemory(xml,i+quantum+1,
sizeof(*xml));
582 if ((i+(
size_t) count) >= extent)
585 if (LocaleCompare(filename,
"-") != 0)
586 file=close_utf8(file);
587 if (xml == (
char *) NULL)
588 return((
char *) NULL);
591 xml=(
char *) RelinquishMagickMemory(xml);
592 return((
char *) NULL);
594 length=MagickMin(i+(
size_t) count,extent);
598 length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
600 if (~length >= (MagickPathExtent-1))
601 xml=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*xml));
602 if (xml == (
char *) NULL)
604 file=close_utf8(file);
605 return((
char *) NULL);
607 map=MapBlob(file,ReadMode,0,length);
608 if (map != (
char *) NULL)
610 (void) memcpy(xml,map,length);
611 (void) UnmapBlob(map,length);
615 (void) lseek(file,0,SEEK_SET);
616 for (i=0; i < length; i+=(size_t) count)
618 count=read(file,xml+i,(
size_t) MagickMin(length-i,(
size_t)
619 MagickMaxBufferExtent));
629 file=close_utf8(file)-1;
630 xml=(
char *) RelinquishMagickMemory(xml);
631 return((
char *) NULL);
635 if (LocaleCompare(filename,
"-") != 0)
636 file=close_utf8(file);
638 xml=(
char *) RelinquishMagickMemory(xml);
664MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
666 assert(xml_info != (XMLTreeInfo *) NULL);
667 assert((xml_info->signature == MagickCoreSignature) ||
668 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
669 if (IsEventLogging() != MagickFalse)
670 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
671 return(xml_info->next);
699MagickExport
const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
709 assert(xml_info != (XMLTreeInfo *) NULL);
710 assert((xml_info->signature == MagickCoreSignature) ||
711 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
712 if (IsEventLogging() != MagickFalse)
713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
714 if (xml_info->attributes == (
char **) NULL)
715 return((
const char *) NULL);
717 while ((xml_info->attributes[i] != (
char *) NULL) &&
718 (strcmp(xml_info->attributes[i],tag) != 0))
720 if (xml_info->attributes[i] != (
char *) NULL)
721 return(xml_info->attributes[i+1]);
722 root=(XMLTreeRoot*) xml_info;
723 while (root->root.parent != (XMLTreeInfo *) NULL)
724 root=(XMLTreeRoot *) root->root.parent;
726 while ((root->attributes[i] != (
char **) NULL) &&
727 (strcmp(root->attributes[i][0],xml_info->tag) != 0))
729 if (root->attributes[i] == (
char **) NULL)
730 return((
const char *) NULL);
732 while ((root->attributes[i][j] != (
char *) NULL) &&
733 (strcmp(root->attributes[i][j],tag) != 0))
735 if (root->attributes[i][j] == (
char *) NULL)
736 return((
const char *) NULL);
737 return(root->attributes[i][j+1]);
766MagickPrivate MagickBooleanType GetXMLTreeAttributes(
767 const XMLTreeInfo *xml_info,SplayTreeInfo *attributes)
772 assert(xml_info != (XMLTreeInfo *) NULL);
773 assert((xml_info->signature == MagickCoreSignature) ||
774 (((
const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
775 assert(attributes != (SplayTreeInfo *) NULL);
776 if (IsEventLogging() != MagickFalse)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
778 if (xml_info->attributes == (
char **) NULL)
781 while (xml_info->attributes[i] != (
char *) NULL)
783 (void) AddValueToSplayTree(attributes,
784 ConstantString(xml_info->attributes[i]),
785 ConstantString(xml_info->attributes[i+1]));
814MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,
const char *tag)
819 assert(xml_info != (XMLTreeInfo *) NULL);
820 assert((xml_info->signature == MagickCoreSignature) ||
821 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
822 if (IsEventLogging() != MagickFalse)
823 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
824 child=xml_info->child;
825 if (tag != (
const char *) NULL)
826 while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
827 child=child->sibling;
854MagickExport
const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
856 assert(xml_info != (XMLTreeInfo *) NULL);
857 assert((xml_info->signature == MagickCoreSignature) ||
858 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
859 if (IsEventLogging() != MagickFalse)
860 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
861 return(xml_info->content);
886MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
888 assert(xml_info != (XMLTreeInfo *) NULL);
889 assert((xml_info->signature == MagickCoreSignature) ||
890 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
891 if (IsEventLogging() != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
893 return(xml_info->ordered);
921MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
926 subnode[MagickPathExtent],
927 tag[MagickPathExtent];
939 assert(xml_info != (XMLTreeInfo *) NULL);
940 assert((xml_info->signature == MagickCoreSignature) ||
941 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
942 if (IsEventLogging() != MagickFalse)
943 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
945 components=GetPathComponents(path,&number_components);
946 if (components == (
char **) NULL)
947 return((XMLTreeInfo *) NULL);
948 for (i=0; i < (ssize_t) number_components; i++)
950 GetPathComponent(components[i],SubimagePath,subnode);
951 GetPathComponent(components[i],CanonicalPath,tag);
952 node=GetXMLTreeChild(node,tag);
953 if (node == (XMLTreeInfo *) NULL)
955 for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
957 node=GetXMLTreeOrdered(node);
958 if (node == (XMLTreeInfo *) NULL)
961 if (node == (XMLTreeInfo *) NULL)
963 components[i]=DestroyString(components[i]);
965 for ( ; i < (ssize_t) number_components; i++)
966 components[i]=DestroyString(components[i]);
967 components=(
char **) RelinquishMagickMemory(components);
995MagickPrivate
const char **GetXMLTreeProcessingInstructions(
996 XMLTreeInfo *xml_info,
const char *target)
1004 assert(xml_info != (XMLTreeInfo *) NULL);
1005 assert((xml_info->signature == MagickCoreSignature) ||
1006 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1007 if (IsEventLogging() != MagickFalse)
1008 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1009 root=(XMLTreeRoot *) xml_info;
1010 while (root->root.parent != (XMLTreeInfo *) NULL)
1011 root=(XMLTreeRoot *) root->root.parent;
1013 while ((root->processing_instructions[i] != (
char **) NULL) &&
1014 (strcmp(root->processing_instructions[i][0],target) != 0))
1016 if (root->processing_instructions[i] == (
char **) NULL)
1017 return((
const char **) sentinel);
1018 return((
const char **) (root->processing_instructions[i]+1));
1043MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
1045 assert(xml_info != (XMLTreeInfo *) NULL);
1046 assert((xml_info->signature == MagickCoreSignature) ||
1047 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1048 if (IsEventLogging() != MagickFalse)
1049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1050 return(xml_info->sibling);
1075MagickExport
const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
1077 assert(xml_info != (XMLTreeInfo *) NULL);
1078 assert((xml_info->signature == MagickCoreSignature) ||
1079 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
1080 if (IsEventLogging() != MagickFalse)
1081 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1082 return(xml_info->tag);
1113MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
1114 XMLTreeInfo *child,
const size_t offset)
1121 child->ordered=(XMLTreeInfo *) NULL;
1122 child->sibling=(XMLTreeInfo *) NULL;
1123 child->next=(XMLTreeInfo *) NULL;
1124 child->offset=offset;
1125 child->parent=xml_info;
1126 if (xml_info->child == (XMLTreeInfo *) NULL)
1128 xml_info->child=child;
1131 head=xml_info->child;
1132 if (head->offset > offset)
1134 child->ordered=head;
1135 xml_info->child=child;
1140 while ((node->ordered != (XMLTreeInfo *) NULL) &&
1141 (node->ordered->offset <= offset))
1143 child->ordered=node->ordered;
1144 node->ordered=child;
1146 previous=(XMLTreeInfo *) NULL;
1148 while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
1153 if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1155 while ((node->next != (XMLTreeInfo *) NULL) &&
1156 (node->next->offset <= offset))
1158 child->next=node->next;
1163 if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
1164 previous->sibling=node->sibling;
1166 previous=(XMLTreeInfo *) NULL;
1168 while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
1173 child->sibling=node;
1174 if (previous != (XMLTreeInfo *) NULL)
1175 previous->sibling=child;
1206static char *ConvertUTF16ToUTF8(
const char *content,
size_t *length)
1224 utf8=(
char *) AcquireQuantumMemory(*length+1,
sizeof(*utf8));
1225 if (utf8 == (
char *) NULL)
1226 return((
char *) NULL);
1227 encoding=(*content ==
'\xFE') ? 1 : (*content ==
'\xFF') ? 0 : -1;
1233 (void) memcpy(utf8,content,*length*
sizeof(*utf8));
1239 for (i=2; i < (ssize_t) (*length-1); i+=2)
1241 c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
1242 ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
1243 if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
1245 byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
1246 (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
1247 (content[i] & 0xff);
1248 c=(((c & 0x3ff) << 10) | (
byte & 0x3ff))+0x10000;
1250 if ((
size_t) (j+MagickPathExtent) > extent)
1252 extent=(size_t) j+MagickPathExtent;
1253 utf8=(
char *) ResizeQuantumMemory(utf8,extent,
sizeof(*utf8));
1254 if (utf8 == (
char *) NULL)
1267 for (bits=0;
byte != 0;
byte/=2)
1270 utf8[j++]=(char) ((0xFF << (7-bits)) | (c >> (6*bits)));
1274 utf8[j]=(char) (0x80 | ((c >> (6*bits)) & 0x3f));
1279 utf8=(
char *) ResizeQuantumMemory(utf8,(*length+1),
sizeof(*utf8));
1280 if (utf8 != (
char *) NULL)
1285static char *ParseEntities(
char *xml,
char **entities,
int state)
1309 for ( ; *xml !=
'\0'; xml++)
1310 while (*xml ==
'\r')
1314 (void) memmove(xml,xml+1,strlen(xml));
1318 while ((*xml !=
'\0') && (*xml !=
'&') && ((*xml !=
'%') ||
1319 (state !=
'%')) && (isspace((
int) ((
unsigned char) *xml)) == 0))
1331 if ((state !=
'c') && (strncmp(xml,
"&#",2) == 0))
1337 c=strtol(xml+2,&entity,10);
1339 c=strtol(xml+3,&entity,16);
1340 if ((c == 0) || (*entity !=
';'))
1356 for (i=0;
byte != 0;
byte/=2)
1359 *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
1364 *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
1368 (void) memmove(xml,strchr(xml,
';')+1,strlen(strchr(xml,
';')));
1371 if (((*xml ==
'&') && ((state ==
'&') || (state ==
' ') ||
1372 (state ==
'*'))) || ((state ==
'%') && (*xml ==
'%')))
1378 while ((entities[i] != (
char *) NULL) &&
1379 (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
1381 if (entities[i++] == (
char *) NULL)
1384 if (entities[i] != (
char *) NULL)
1389 length=strlen(entities[i]);
1390 entity=strchr(xml,
';');
1391 if ((entity != (
char *) NULL) &&
1392 ((length-1L) >= (
size_t) (entity-xml)))
1394 offset=(ssize_t) (xml-p);
1395 extent=((size_t) offset+length+strlen(entity));
1398 p=(
char *) ResizeQuantumMemory(p,extent+1,
sizeof(*p));
1399 if (p == (
char *) NULL)
1400 ThrowFatalException(ResourceLimitFatalError,
1401 "MemoryAllocationFailed");
1409 extent_xml=(
char *) AcquireQuantumMemory(extent+1,
1410 sizeof(*extent_xml));
1411 if (extent_xml != (
char *) NULL)
1413 memset(extent_xml,0,extent*
sizeof(*extent_xml));
1414 (void) CopyMagickString(extent_xml,p,extent*
1415 sizeof(*extent_xml));
1419 if (p == (
char *) NULL)
1420 ThrowFatalException(ResourceLimitFatalError,
1421 "MemoryAllocationFailed");
1423 entity=strchr(xml,
';');
1425 if (entity != (
char *) NULL)
1426 (void) memmove(xml+length,entity+1,strlen(entity));
1427 (void) memcpy(xml,entities[i],length);
1431 if (((state ==
' ') || (state ==
'*')) &&
1432 (isspace((
int) ((
unsigned char) *xml)) != 0))
1442 for (xml=p; *xml !=
'\0'; xml++)
1447 i=(ssize_t) strspn(xml,accept);
1449 (void) memmove(xml,xml+i,strlen(xml+i)+1);
1450 while ((*xml !=
'\0') && (*xml !=
' '))
1456 if ((xml >= p) && (*xml ==
' '))
1459 return(p == q ? ConstantString(p) : p);
1462static void ParseCharacterContent(XMLTreeRoot *root,
char *xml,
1463 const size_t length,
const char state)
1468 xml_info=root->node;
1469 if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (
char *) NULL) ||
1473 xml=ParseEntities(xml,root->entities,state);
1474 if ((xml_info->content != (
char *) NULL) && (*xml_info->content !=
'\0'))
1476 (void) ConcatenateString(&xml_info->content,xml);
1477 xml=DestroyString(xml);
1481 if (xml_info->content != (
char *) NULL)
1482 xml_info->content=DestroyString(xml_info->content);
1483 xml_info->content=xml;
1487static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,
char *tag,
1488 ExceptionInfo *exception)
1490 if ((root->node == (XMLTreeInfo *) NULL) ||
1491 (root->node->tag == (
char *) NULL) || (strcmp(tag,root->node->tag) != 0))
1493 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1494 "ParseError",
"unexpected closing tag </%s>",tag);
1495 return(&root->root);
1497 root->node=root->node->parent;
1498 return((XMLTreeInfo *) NULL);
1501static MagickBooleanType ValidateEntities(
char *tag,
char *xml,
1502 const size_t depth,
char **entities)
1510 if (depth > MagickMaxRecursionDepth)
1511 return(MagickFalse);
1514 while ((*xml !=
'\0') && (*xml !=
'&'))
1518 if (strncmp(xml+1,tag,strlen(tag)) == 0)
1519 return(MagickFalse);
1521 while ((entities[i] != (
char *) NULL) &&
1522 (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
1524 if ((entities[i] != (
char *) NULL) &&
1525 (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
1526 return(MagickFalse);
1530static void ParseProcessingInstructions(XMLTreeRoot *root,
char *xml,
1542 xml+=strcspn(xml,XMLWhitespace);
1546 xml+=strspn(xml+1,XMLWhitespace)+1;
1548 if (strcmp(target,
"xml") == 0)
1550 xml=strstr(xml,
"standalone");
1551 if ((xml != (
char *) NULL) &&
1552 (strncmp(xml+strspn(xml+10,XMLWhitespace
"='\"")+10,
"yes",3) == 0))
1553 root->standalone=MagickTrue;
1556 if (root->processing_instructions[0] == (
char **) NULL)
1558 root->processing_instructions=(
char ***) AcquireCriticalMemory(
sizeof(
1559 *root->processing_instructions));
1560 *root->processing_instructions=(
char **) NULL;
1563 while ((root->processing_instructions[i] != (
char **) NULL) &&
1564 (strcmp(target,root->processing_instructions[i][0]) != 0))
1566 if (root->processing_instructions[i] == (
char **) NULL)
1568 root->processing_instructions=(
char ***) ResizeQuantumMemory(
1569 root->processing_instructions,(
size_t) (i+2),
1570 sizeof(*root->processing_instructions));
1571 if (root->processing_instructions == (
char ***) NULL)
1572 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1573 root->processing_instructions[i]=(
char **) AcquireQuantumMemory(3,
1574 sizeof(**root->processing_instructions));
1575 if (root->processing_instructions[i] == (
char **) NULL)
1576 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1577 root->processing_instructions[i+1]=(
char **) NULL;
1578 root->processing_instructions[i][0]=ConstantString(target);
1579 root->processing_instructions[i][1]=(
char *)
1580 root->processing_instructions[i+1];
1581 root->processing_instructions[i+1]=(
char **) NULL;
1582 root->processing_instructions[i][2]=ConstantString(
"");
1585 while (root->processing_instructions[i][j] != (
char *) NULL)
1587 root->processing_instructions[i]=(
char **) ResizeQuantumMemory(
1588 root->processing_instructions[i],(
size_t) (j+3),
1589 sizeof(**root->processing_instructions));
1590 if (root->processing_instructions[i] == (
char **) NULL)
1591 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1592 root->processing_instructions[i][j+2]=(
char *) ResizeQuantumMemory(
1593 root->processing_instructions[i][j+1],(
size_t) (j+1),
1594 sizeof(***root->processing_instructions));
1595 if (root->processing_instructions[i][j+2] == (
char *) NULL)
1596 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1597 (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
1598 root->root.tag != (
char *) NULL ?
">" :
"<",2);
1599 root->processing_instructions[i][j]=ConstantString(xml);
1600 root->processing_instructions[i][j+1]=(
char *) NULL;
1603static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,
char *xml,
1604 size_t length,ExceptionInfo *exception)
1610 **predefined_entities,
1620 predefined_entities=(
char **) AcquireMagickMemory(
sizeof(sentinel));
1621 if (predefined_entities == (
char **) NULL)
1622 ThrowFatalException(ResourceLimitError,
"MemoryAllocationFailed");
1623 (void) memcpy(predefined_entities,sentinel,
sizeof(sentinel));
1624 for (xml[length]=
'\0'; xml != (
char *) NULL; )
1626 while ((*xml !=
'\0') && (*xml !=
'<') && (*xml !=
'%'))
1630 if ((strlen(xml) > 9) && (strncmp(xml,
"<!ENTITY",8) == 0))
1635 if (strspn(xml+8,XMLWhitespace) == 0)
1637 xml+=strspn(xml+8,XMLWhitespace)+8;
1639 n=xml+strspn(xml,XMLWhitespace
"%");
1640 if ((isalpha((
int) ((
unsigned char) *n)) == 0) && (*n !=
'_'))
1642 xml=n+strcspn(n,XMLWhitespace);
1646 v=xml+strspn(xml+1,XMLWhitespace)+1;
1649 if ((q !=
'"') && (q !=
'\''))
1654 xml=strchr(xml,
'>');
1657 entities=(*c ==
'%') ? predefined_entities : root->entities;
1658 for (i=0; entities[i] != (
char *) NULL; i++) ;
1659 entities=(
char **) ResizeQuantumMemory(entities,(
size_t) (i+3),
1661 if (entities == (
char **) NULL)
1662 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1664 predefined_entities=entities;
1666 root->entities=entities;
1670 if (xml != (
char *) NULL)
1675 entities[i+1]=ParseEntities(v,predefined_entities,
'%');
1676 entities[i+2]=(
char *) NULL;
1677 if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
1681 if (entities[i+1] != v)
1682 entities[i+1]=DestroyString(entities[i+1]);
1683 (void) ThrowMagickException(exception,GetMagickModule(),
1684 OptionWarning,
"ParseError",
"circular entity declaration &%s",n);
1685 predefined_entities=(
char **) RelinquishMagickMemory(
1686 predefined_entities);
1687 return(MagickFalse);
1691 if (strncmp(xml,
"<!ATTLIST",9) == 0)
1696 t=xml+strspn(xml+9,XMLWhitespace)+9;
1699 (void) ThrowMagickException(exception,GetMagickModule(),
1700 OptionWarning,
"ParseError",
"unclosed <!ATTLIST");
1701 predefined_entities=(
char **) RelinquishMagickMemory(
1702 predefined_entities);
1703 return(MagickFalse);
1705 xml=t+strcspn(t,XMLWhitespace
">");
1710 while ((root->attributes[i] != (
char **) NULL) &&
1711 (n != (
char *) NULL) &&
1712 (strcmp(n,root->attributes[i][0]) != 0))
1714 while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) !=
'\0') &&
1717 xml=n+strcspn(n,XMLWhitespace);
1722 (void) ThrowMagickException(exception,GetMagickModule(),
1723 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1724 predefined_entities=(
char **) RelinquishMagickMemory(
1725 predefined_entities);
1726 return(MagickFalse);
1728 xml+=strspn(xml+1,XMLWhitespace)+1;
1729 c=(
char *) (strncmp(xml,
"CDATA",5) != 0 ?
"*" :
" ");
1730 if (strncmp(xml,
"NOTATION",8) == 0)
1731 xml+=strspn(xml+8,XMLWhitespace)+8;
1732 xml=(*xml ==
'(') ? strchr(xml,
')') : xml+
1733 strcspn(xml,XMLWhitespace);
1734 if (xml == (
char *) NULL)
1736 (void) ThrowMagickException(exception,GetMagickModule(),
1737 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1738 predefined_entities=(
char **) RelinquishMagickMemory(
1739 predefined_entities);
1740 return(MagickFalse);
1742 xml+=strspn(xml,XMLWhitespace
")");
1743 if (strncmp(xml,
"#FIXED",6) == 0)
1744 xml+=strspn(xml+6,XMLWhitespace)+6;
1747 xml+=strcspn(xml,XMLWhitespace
">")-1;
1753 if (((*xml ==
'"') || (*xml ==
'\'')) &&
1754 ((xml=strchr(v=xml+1,*xml)) != (
char *) NULL))
1758 (void) ThrowMagickException(exception,GetMagickModule(),
1759 OptionWarning,
"ParseError",
"malformed <!ATTLIST");
1760 predefined_entities=(
char **) RelinquishMagickMemory(
1761 predefined_entities);
1762 return(MagickFalse);
1764 if (root->attributes[i] == (
char **) NULL)
1770 root->attributes=(
char ***) AcquireQuantumMemory(2,
1771 sizeof(*root->attributes));
1773 root->attributes=(
char ***) ResizeQuantumMemory(
1774 root->attributes,(
size_t) (i+2),
1775 sizeof(*root->attributes));
1776 if (root->attributes == (
char ***) NULL)
1777 ThrowFatalException(ResourceLimitFatalError,
1778 "MemoryAllocationFailed");
1779 root->attributes[i]=(
char **) AcquireQuantumMemory(2,
1780 sizeof(**root->attributes));
1781 if (root->attributes[i] == (
char **) NULL)
1782 ThrowFatalException(ResourceLimitFatalError,
1783 "MemoryAllocationFailed");
1784 root->attributes[i][0]=ConstantString(t);
1785 root->attributes[i][1]=(
char *) NULL;
1786 root->attributes[i+1]=(
char **) NULL;
1788 for (j=1; root->attributes[i][j] != (
char *) NULL; j+=3) ;
1789 root->attributes[i]=(
char **) ResizeQuantumMemory(
1790 root->attributes[i],(
size_t) (j+4),
sizeof(**root->attributes));
1791 if (root->attributes[i] == (
char **) NULL)
1792 ThrowFatalException(ResourceLimitFatalError,
1793 "MemoryAllocationFailed");
1794 root->attributes[i][j+3]=(
char *) NULL;
1795 root->attributes[i][j+2]=ConstantString(c);
1796 root->attributes[i][j+1]=(
char *) NULL;
1797 if (v != (
char *) NULL)
1798 root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
1799 root->attributes[i][j]=ConstantString(n);
1803 if (strncmp(xml,
"<!--", 4) == 0)
1804 xml=strstr(xml+4,
"-->");
1806 if (strncmp(xml,
"<?", 2) == 0)
1810 if (xml != (
char *) NULL)
1812 ParseProcessingInstructions(root,c,(
size_t) (xml-c));
1818 xml=strchr(xml,
'>');
1820 if ((*(xml++) ==
'%') && (root->standalone == MagickFalse))
1823 predefined_entities=(
char **) RelinquishMagickMemory(predefined_entities);
1827static void ParseOpenTag(XMLTreeRoot *root,
char *tag,
char **attributes)
1832 xml_info=root->node;
1833 if (xml_info->tag == (
char *) NULL)
1834 xml_info->tag=ConstantString(tag);
1836 xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
1837 if (xml_info != (XMLTreeInfo *) NULL)
1838 xml_info->attributes=attributes;
1839 root->node=xml_info;
1850static inline MagickBooleanType IsSkipTag(
const char *tag)
1856 while (ignore_tags[i] != (
const char *) NULL)
1858 if (LocaleCompare(tag,ignore_tags[i]) == 0)
1862 return(MagickFalse);
1865MagickExport XMLTreeInfo *NewXMLTree(
const char *xml,ExceptionInfo *exception)
1896 if ((xml == (
const char *) NULL) || (strlen(xml) == 0))
1898 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1899 "ParseError",
"root tag missing");
1900 return((XMLTreeInfo *) NULL);
1902 root=(XMLTreeRoot *) NewXMLTreeTag((
char *) NULL);
1904 utf8=ConvertUTF16ToUTF8(xml,&length);
1905 if (utf8 == (
char *) NULL)
1907 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1908 "ParseError",
"UTF16 to UTF8 failed");
1909 return((XMLTreeInfo *) NULL);
1911 terminal=utf8[length-1];
1912 utf8[length-1]=
'\0';
1914 while ((*p !=
'\0') && (*p !=
'<'))
1918 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1919 "ParseError",
"root tag missing");
1920 utf8=DestroyString(utf8);
1921 return((XMLTreeInfo *) NULL);
1923 attribute=(
char **) NULL;
1928 attributes=(
char **) sentinel;
1931 if ((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_') ||
1932 (*p ==
':') || (c <
'\0'))
1937 if (root->node == (XMLTreeInfo *) NULL)
1939 (void) ThrowMagickException(exception,GetMagickModule(),
1940 OptionWarning,
"ParseError",
"root tag missing");
1941 utf8=DestroyString(utf8);
1942 return(&root->root);
1944 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"/>");
1945 while (isspace((
int) ((
unsigned char) *p)) != 0)
1947 if (((isalpha((
int) ((
unsigned char) *p)) != 0) || (*p ==
'_')) &&
1948 (ignore_depth == 0))
1950 if ((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
1956 while ((root->attributes[i] != (
char **) NULL) &&
1957 (strcmp(root->attributes[i][0],tag) != 0))
1959 attribute=root->attributes[i];
1961 for (l=0; (*p !=
'\0') && (*p !=
'/') && (*p !=
'>'); l+=2)
1967 attributes=(
char **) AcquireQuantumMemory(4,
1968 sizeof(*attributes));
1970 attributes=(
char **) ResizeQuantumMemory(attributes,(
size_t)
1971 (l+4),
sizeof(*attributes));
1972 if (attributes == (
char **) NULL)
1974 (void) ThrowMagickException(exception,GetMagickModule(),
1975 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1976 utf8=DestroyString(utf8);
1977 return(&root->root);
1979 attributes[l+2]=(
char *) NULL;
1980 attributes[l+1]=(
char *) NULL;
1982 p+=(ptrdiff_t) strcspn(p,XMLWhitespace
"=/>");
1983 if ((*p !=
'=') && (isspace((
int) ((
unsigned char) *p)) == 0))
1984 attributes[l]=ConstantString(
"");
1988 p+=(ptrdiff_t) strspn(p,XMLWhitespace
"=");
1990 if ((c ==
'"') || (c ==
'\''))
1997 while ((*p !=
'\0') && (*p != c))
2003 attributes[l]=ConstantString(
"");
2004 attributes[l+1]=ConstantString(
"");
2005 (void) DestroyXMLTreeAttributes(attributes);
2006 (void) ThrowMagickException(exception,
2007 GetMagickModule(),OptionWarning,
"ParseError",
2009 utf8=DestroyString(utf8);
2010 return(&root->root);
2013 while ((attribute != (
char **) NULL) &&
2014 (attribute[j] != (
char *) NULL) &&
2015 (strcmp(attribute[j],attributes[l]) != 0))
2017 attributes[l+1]=ParseEntities(attributes[l+1],
2018 root->entities,(attribute != (
char **) NULL) &&
2019 (attribute[j] != (
char *) NULL) ? *attribute[j+2] :
2022 attributes[l]=ConstantString(attributes[l]);
2024 while (isspace((
int) ((
unsigned char) *p)) != 0)
2030 while((*p !=
'\0') && (*p !=
'/') && (*p !=
'>'))
2039 if (((*p !=
'\0') && (*p !=
'>')) ||
2040 ((*p ==
'\0') && (terminal !=
'>')))
2043 (void) DestroyXMLTreeAttributes(attributes);
2044 (void) ThrowMagickException(exception,GetMagickModule(),
2045 OptionWarning,
"ParseError",
"missing >");
2046 utf8=DestroyString(utf8);
2047 return(&root->root);
2049 if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
2050 (void) DestroyXMLTreeAttributes(attributes);
2053 ParseOpenTag(root,tag,attributes);
2054 (void) ParseCloseTag(root,tag,exception);
2060 if ((*p ==
'>') || ((*p ==
'\0') && (terminal ==
'>')))
2063 if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
2064 ParseOpenTag(root,tag,attributes);
2068 (void) DestroyXMLTreeAttributes(attributes);
2075 (void) DestroyXMLTreeAttributes(attributes);
2076 (void) ThrowMagickException(exception,GetMagickModule(),
2077 OptionWarning,
"ParseError",
"missing >");
2078 utf8=DestroyString(utf8);
2079 return(&root->root);
2090 p+=(ptrdiff_t) strcspn(tag,XMLWhitespace
">")+1;
2092 if ((c ==
'\0') && (terminal !=
'>'))
2094 (void) ThrowMagickException(exception,GetMagickModule(),
2095 OptionWarning,
"ParseError",
"missing >");
2096 utf8=DestroyString(utf8);
2097 return(&root->root);
2100 if ((ignore_depth == 0) &&
2101 (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
2103 utf8=DestroyString(utf8);
2104 return(&root->root);
2106 if (ignore_depth > 0)
2109 if (isspace((
int) ((
unsigned char) *p)) != 0)
2110 p+=(ptrdiff_t) strspn(p,XMLWhitespace);
2113 if (strncmp(p,
"!--",3) == 0)
2119 if ((p == (
char *) NULL) || ((*(p+=2) !=
'>') && (*p !=
'\0')) ||
2120 ((*p ==
'\0') && (terminal !=
'>')))
2122 (void) ThrowMagickException(exception,GetMagickModule(),
2123 OptionWarning,
"ParseError",
"unclosed <!--");
2124 utf8=DestroyString(utf8);
2125 return(&root->root);
2129 if (strncmp(p,
"![CDATA[",8) == 0)
2135 if (p != (
char *) NULL)
2138 if (ignore_depth == 0)
2139 ParseCharacterContent(root,tag+8,(
size_t) (p-tag-10),
'c');
2143 (void) ThrowMagickException(exception,GetMagickModule(),
2144 OptionWarning,
"ParseError",
"unclosed <![CDATA[");
2145 utf8=DestroyString(utf8);
2146 return(&root->root);
2150 if (strncmp(p,
"!DOCTYPE",8) == 0)
2155 for (l=0; (*p !=
'\0') && (((l == 0) && (*p !=
'>')) ||
2156 ((l != 0) && ((*p !=
']') ||
2157 (*(p+strspn(p+1,XMLWhitespace)+1) !=
'>'))));
2158 l=(ssize_t) ((*p ==
'[') ? 1 : l))
2159 p+=(ptrdiff_t) strcspn(p+1,
"[]>")+1;
2160 if ((*p ==
'\0') && (terminal !=
'>'))
2162 (void) ThrowMagickException(exception,GetMagickModule(),
2163 OptionWarning,
"ParseError",
"unclosed <!DOCTYPE");
2164 utf8=DestroyString(utf8);
2165 return(&root->root);
2168 tag=strchr(tag,
'[')+1;
2171 status=ParseInternalDoctype(root,tag,(
size_t) (p-tag),
2173 if (status == MagickFalse)
2175 utf8=DestroyString(utf8);
2176 return(&root->root);
2190 if (p == (
char *) NULL)
2193 }
while ((*p !=
'\0') && (*p !=
'>'));
2194 if ((p == (
char *) NULL) || ((*p ==
'\0') &&
2197 (void) ThrowMagickException(exception,GetMagickModule(),
2198 OptionWarning,
"ParseError",
"unclosed <?");
2199 utf8=DestroyString(utf8);
2200 return(&root->root);
2202 ParseProcessingInstructions(root,tag+1,(
size_t) (p-tag-2));
2206 (void) ThrowMagickException(exception,GetMagickModule(),
2207 OptionWarning,
"ParseError",
"unexpected <");
2208 utf8=DestroyString(utf8);
2209 return(&root->root);
2211 if ((p == (
char *) NULL) || (*p ==
'\0'))
2215 if ((*p !=
'\0') && (*p !=
'<'))
2220 while ((*p !=
'\0') && (*p !=
'<'))
2224 if (ignore_depth == 0)
2225 ParseCharacterContent(root,tag,(
size_t) (p-tag),
'&');
2231 utf8=DestroyString(utf8);
2232 if (root->node == (XMLTreeInfo *) NULL)
2233 return(&root->root);
2234 if (root->node->tag == (
char *) NULL)
2236 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2237 "ParseError",
"root tag missing");
2238 return(&root->root);
2240 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
2241 "ParseError",
"unclosed tag: '%s'",root->node->tag);
2242 return(&root->root);
2267MagickExport XMLTreeInfo *NewXMLTreeTag(
const char *tag)
2270 *predefined_entities[NumberPredefinedEntities+1] =
2272 "lt;",
"<",
"gt;",
">",
"quot;",
""",
2273 "apos;",
"'",
"amp;",
"&", (
char *) NULL
2279 root=(XMLTreeRoot *) AcquireCriticalMemory(
sizeof(*root));
2280 (void) memset(root,0,
sizeof(*root));
2281 root->root.tag=(
char *) NULL;
2282 if (tag != (
char *) NULL)
2283 root->root.tag=ConstantString(tag);
2284 root->node=(&root->root);
2285 root->root.content=ConstantString(
"");
2286 root->entities=(
char **) AcquireCriticalMemory(
sizeof(predefined_entities));
2287 (void) memcpy(root->entities,predefined_entities,
sizeof(predefined_entities));
2288 root->root.attributes=sentinel;
2289 root->attributes=(
char ***) root->root.attributes;
2290 root->processing_instructions=(
char ***) root->root.attributes;
2291 root->debug=IsEventLogging();
2292 root->signature=MagickCoreSignature;
2293 return(&root->root);
2319MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
2324 assert(xml_info != (XMLTreeInfo *) NULL);
2325 assert((xml_info->signature == MagickCoreSignature) ||
2326 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2327 if (IsEventLogging() != MagickFalse)
2328 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2329 if (xml_info->next != (XMLTreeInfo *) NULL)
2330 xml_info->next->sibling=xml_info->sibling;
2331 if (xml_info->parent != (XMLTreeInfo *) NULL)
2333 node=xml_info->parent->child;
2334 if (node == xml_info)
2335 xml_info->parent->child=xml_info->ordered;
2338 while (node->ordered != xml_info)
2340 node->ordered=node->ordered->ordered;
2341 node=xml_info->parent->child;
2342 if (strcmp(node->tag,xml_info->tag) != 0)
2344 while (strcmp(node->sibling->tag,xml_info->tag) != 0)
2346 if (node->sibling != xml_info)
2349 node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
2350 xml_info->next : node->sibling->sibling;
2352 while ((node->next != (XMLTreeInfo *) NULL) &&
2353 (node->next != xml_info))
2355 if (node->next != (XMLTreeInfo *) NULL)
2356 node->next=node->next->next;
2359 xml_info->ordered=(XMLTreeInfo *) NULL;
2360 xml_info->sibling=(XMLTreeInfo *) NULL;
2361 xml_info->next=(XMLTreeInfo *) NULL;
2393MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
2394 const char *tag,
const char *value)
2400 assert(xml_info != (XMLTreeInfo *) NULL);
2401 assert((xml_info->signature == MagickCoreSignature) ||
2402 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2403 if (IsEventLogging() != MagickFalse)
2404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2406 while ((xml_info->attributes[i] != (
char *) NULL) &&
2407 (strcmp(xml_info->attributes[i],tag) != 0))
2409 if (xml_info->attributes[i] == (
char *) NULL)
2414 if (value == (
const char *) NULL)
2416 if (xml_info->attributes != sentinel)
2417 xml_info->attributes=(
char **) ResizeQuantumMemory(
2418 xml_info->attributes,(
size_t) (i+4),
sizeof(*xml_info->attributes));
2421 xml_info->attributes=(
char **) AcquireQuantumMemory(4,
2422 sizeof(*xml_info->attributes));
2423 if (xml_info->attributes != (
char **) NULL)
2424 xml_info->attributes[1]=ConstantString(
"");
2426 if (xml_info->attributes == (
char **) NULL)
2427 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2428 xml_info->attributes[i]=ConstantString(tag);
2429 xml_info->attributes[i+2]=(
char *) NULL;
2430 (void) strlen(xml_info->attributes[i+1]);
2435 for (j=i; xml_info->attributes[j] != (
char *) NULL; j+=2) ;
2436 if (xml_info->attributes[i+1] != (
char *) NULL)
2437 xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
2438 if (value != (
const char *) NULL)
2440 xml_info->attributes[i+1]=ConstantString(value);
2443 if (xml_info->attributes[i] != (
char *) NULL)
2444 xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
2445 (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(
size_t)
2446 (j-i)*
sizeof(*xml_info->attributes));
2447 xml_info->attributes=(
char **) ResizeQuantumMemory(xml_info->attributes,
2448 (
size_t) (j+2),
sizeof(*xml_info->attributes));
2449 if (xml_info->attributes == (
char **) NULL)
2450 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
2452 (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
2453 (i/2)+1,(
size_t) (((j+2)/2)-(i/2))*
sizeof(**xml_info->attributes));
2483MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
2484 const char *content)
2486 assert(xml_info != (XMLTreeInfo *) NULL);
2487 assert((xml_info->signature == MagickCoreSignature) ||
2488 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2489 if (IsEventLogging() != MagickFalse)
2490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2491 if (xml_info->content != (
char *) NULL)
2492 xml_info->content=DestroyString(xml_info->content);
2493 xml_info->content=(
char *) ConstantString(content);
2520static char *EncodePredefinedEntities(
const char *source,ssize_t offset,
2521 char **destination,
size_t *length,
size_t *extent,MagickBooleanType pedantic)
2527 canonical_content=CanonicalXMLContent(source,pedantic);
2533 content=AcquireString(source);
2534 content[offset]=
'\0';
2535 canonical_content=CanonicalXMLContent(content,pedantic);
2536 content=DestroyString(content);
2538 if (canonical_content == (
char *) NULL)
2539 return(*destination);
2540 if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
2542 *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
2543 *destination=(
char *) ResizeQuantumMemory(*destination,*extent,
2544 sizeof(**destination));
2545 if (*destination == (
char *) NULL)
2546 return(*destination);
2548 *length+=(size_t) FormatLocaleString(*destination+(*length),*extent,
"%s",
2550 canonical_content=DestroyString(canonical_content);
2551 return(*destination);
2554static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,
char **source,
size_t *length,
2555 size_t *extent,
size_t start,
char ***attributes)
2570 content=(
char *)
"";
2571 if (xml_info->parent != (XMLTreeInfo *) NULL)
2572 content=xml_info->parent->content;
2574 *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
2575 start),source,length,extent,MagickFalse);
2576 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2578 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2579 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2580 if (*source == (
char *) NULL)
2583 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2584 "<%s",xml_info->tag);
2585 for (i=0; xml_info->attributes[i]; i+=2)
2587 attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
2588 if (attribute != xml_info->attributes[i+1])
2590 if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
2592 *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
2593 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2594 if (*source == (
char *) NULL)
2595 return((
char *) NULL);
2597 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2598 xml_info->attributes[i]);
2599 (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
2601 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2604 while ((attributes[i] != (
char **) NULL) &&
2605 (strcmp(attributes[i][0],xml_info->tag) != 0))
2608 while ((attributes[i] != (
char **) NULL) &&
2609 (attributes[i][j] != (
char *) NULL))
2611 if ((attributes[i][j+1] == (
char *) NULL) ||
2612 (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
2617 if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
2619 *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
2620 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2621 if (*source == (
char *) NULL)
2622 return((
char *) NULL);
2624 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
" %s=\"",
2626 (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
2628 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"\"");
2631 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
2632 *xml_info->content ?
">" :
"/>");
2633 if (xml_info->child != (XMLTreeInfo *) NULL)
2634 *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
2636 *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
2638 if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
2640 *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
2641 *source=(
char *) ResizeQuantumMemory(*source,*extent,
sizeof(**source));
2642 if (*source == (
char *) NULL)
2643 return((
char *) NULL);
2645 if (*xml_info->content !=
'\0')
2646 *length+=(size_t) FormatLocaleString(*source+(*length),*extent,
"</%s>",
2648 while ((offset < xml_info->offset) && (content[offset] !=
'\0'))
2650 if (xml_info->ordered != (XMLTreeInfo *) NULL)
2651 content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
2654 content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
2659MagickExport
char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
2682 assert(xml_info != (XMLTreeInfo *) NULL);
2683 assert((xml_info->signature == MagickCoreSignature) ||
2684 (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
2685 if (IsEventLogging() != MagickFalse)
2686 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2687 if (xml_info->tag == (
char *) NULL)
2688 return((
char *) NULL);
2689 xml=AcquireString((
char *) NULL);
2691 extent=MagickPathExtent;
2692 root=(XMLTreeRoot *) xml_info;
2693 while (root->root.parent != (XMLTreeInfo *) NULL)
2694 root=(XMLTreeRoot *) root->root.parent;
2695 parent=xml_info->parent;
2696 if (parent == (XMLTreeInfo *) NULL)
2697 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2702 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2703 p=root->processing_instructions[i][1];
2704 for (j=1; p != (
char *) NULL; j++)
2706 if (root->processing_instructions[i][k][j-1] ==
'>')
2708 p=root->processing_instructions[i][j];
2711 q=root->processing_instructions[i][0];
2712 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2714 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2715 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2716 if (xml == (
char *) NULL)
2719 length+=(size_t) FormatLocaleString(xml+length,extent,
"<?%s%s%s?>\n",q,
2720 *p !=
'\0' ?
" " :
"",p);
2721 p=root->processing_instructions[i][j];
2724 ordered=xml_info->ordered;
2725 xml_info->parent=(XMLTreeInfo *) NULL;
2726 xml_info->ordered=(XMLTreeInfo *) NULL;
2727 xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
2728 xml_info->parent=parent;
2729 xml_info->ordered=ordered;
2730 if (parent == (XMLTreeInfo *) NULL)
2731 for (i=0; root->processing_instructions[i] != (
char **) NULL; i++)
2736 for (k=2; root->processing_instructions[i][k-1]; k++) ;
2737 p=root->processing_instructions[i][1];
2738 for (j=1; p != (
char *) NULL; j++)
2740 if (root->processing_instructions[i][k][j-1] ==
'<')
2742 p=root->processing_instructions[i][j];
2745 q=root->processing_instructions[i][0];
2746 if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
2748 extent=length+strlen(p)+strlen(q)+MagickPathExtent;
2749 xml=(
char *) ResizeQuantumMemory(xml,extent,
sizeof(*xml));
2750 if (xml == (
char *) NULL)
2753 length+=(size_t) FormatLocaleString(xml+length,extent,
"\n<?%s%s%s?>",q,
2754 *p !=
'\0' ?
" " :
"",p);
2755 p=root->processing_instructions[i][j];
2758 return((
char *) ResizeQuantumMemory(xml,length+1,
sizeof(*xml)));