]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/magic.c
Update web pages
[imagemagick] / MagickCore / magic.c
index 6a6672fe0e0b12b9d3c302c061cc544afc311357..5d446ee55427eb9bcbae7e774002dff933c26256 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 2000                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "MagickCore/utility.h"
 #include "MagickCore/utility-private.h"
 #include "MagickCore/xml-tree.h"
+#include "MagickCore/xml-tree-private.h"
 \f
 /*
   Define declarations.
 */
 #define MagicFilename  "magic.xml"
-#define MagickString(magic)  (const unsigned char *) (magic), sizeof(magic)-1
+#define MagicPattern(magic)  (const unsigned char *) (magic), sizeof(magic)-1
 \f
 /*
   Typedef declarations.
@@ -88,143 +89,255 @@ typedef struct _MagicMapInfo
 static const MagicMapInfo
   MagicMap[] =
   {
-    { "8BIMWTEXT", 0, MagickString("8\000B\000I\000M\000#") },
-    { "8BIMTEXT", 0, MagickString("8BIM#") },
-    { "8BIM", 0, MagickString("8BIM") },
-    { "BMP", 0, MagickString("BA") },
-    { "BMP", 0, MagickString("BM") },
-    { "BMP", 0, MagickString("CI") },
-    { "BMP", 0, MagickString("CP") },
-    { "BMP", 0, MagickString("IC") },
-    { "PICT", 0, MagickString("PICT") },
-    { "BMP", 0, MagickString("PI") },
-    { "CALS", 21, MagickString("version: MIL-STD-1840") },
-    { "CALS", 0, MagickString("srcdocid:") },
-    { "CALS", 9, MagickString("srcdocid:") },
-    { "CALS", 8, MagickString("rorient:") },
-    { "CGM", 0, MagickString("BEGMF") },
-    { "CIN", 0, MagickString("\200\052\137\327") },
-    { "CRW", 0, MagickString("II\x1a\x00\x00\x00HEAPCCDR") },
-    { "DCM", 128, MagickString("DICM") },
-    { "DCX", 0, MagickString("\261\150\336\72") },
-    { "DIB", 0, MagickString("\050\000") },
-    { "DDS", 0, MagickString("DDS ") },
-    { "DJVU", 0, MagickString("AT&TFORM") },
-    { "DOT", 0, MagickString("digraph") },
-    { "DPX", 0, MagickString("SDPX") },
-    { "DPX", 0, MagickString("XPDS") },
-    { "EMF", 40, MagickString("\040\105\115\106\000\000\001\000") },
-    { "EPT", 0, MagickString("\305\320\323\306") },
-    { "EXR", 0, MagickString("\166\057\061\001") },
-    { "FAX", 0, MagickString("DFAX") },
-    { "FIG", 0, MagickString("#FIG") },
-    { "FITS", 0, MagickString("IT0") },
-    { "FITS", 0, MagickString("SIMPLE") },
-    { "FPX", 0, MagickString("\320\317\021\340") },
-    { "GIF", 0, MagickString("GIF8") },
-    { "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
-    { "HDF", 1, MagickString("HDF") },
-    { "HDR", 0, MagickString("#?RADIANCE") },
-    { "HDR", 0, MagickString("#?RGBE") },
-    { "HPGL", 0, MagickString("IN;") },
-    { "HTML", 1, MagickString("HTML") },
-    { "HTML", 1, MagickString("html") },
-    { "ILBM", 8, MagickString("ILBM") },
-    { "IPTCWTEXT", 0, MagickString("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
-    { "IPTCTEXT", 0, MagickString("2#0=\042�\042") },
-    { "IPTC", 0, MagickString("\034\002") },
-    { "JNG", 0, MagickString("\213JNG\r\n\032\n") },
-    { "JPEG", 0, MagickString("\377\330\377") },
-    { "J2K", 0, MagickString("\xff\x4f\xff\x51") },
-    { "JPC", 0, MagickString("\x0d\x0a\x87\x0a") },
-    { "JP2", 4, MagickString("\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a") },
-    { "MAT", 0, MagickString("MATLAB 5.0 MAT-file,") },
-    { "MIFF", 0, MagickString("Id=ImageMagick") },
-    { "MIFF", 0, MagickString("id=ImageMagick") },
-    { "MNG", 0, MagickString("\212MNG\r\n\032\n") },
-    { "MPC", 0, MagickString("id=MagickCache") },
-    { "MPEG", 0, MagickString("\000\000\001\263") },
-    { "MRW", 0, MagickString("\x00MRM") },
-    { "MVG", 0, MagickString("push graphic-context") },
-    { "ORF", 0, MagickString("IIRO\x08\x00\x00\x00") },
-    { "PCD", 2048, MagickString("PCD_") },
-    { "PCL", 0, MagickString("\033E\033") },
-    { "PCX", 0, MagickString("\012\002") },
-    { "PCX", 0, MagickString("\012\005") },
-    { "PDB", 60, MagickString("vIMGView") },
-    { "PDF", 0, MagickString("%PDF-") },
-    { "PES", 0, MagickString("#PES") },
-    { "PFA", 0, MagickString("%!PS-AdobeFont-1.0") },
-    { "PFB", 6, MagickString("%!PS-AdobeFont-1.0") },
-    { "PGX", 0, MagickString("\050\107\020\115\046") },
-    { "PICT", 522, MagickString("\000\021\002\377\014\000") },
-    { "PNG", 0, MagickString("\211PNG\r\n\032\n") },
-    { "PBM", 0, MagickString("P1") },
-    { "PGM", 0, MagickString("P2") },
-    { "PPM", 0, MagickString("P3") },
-    { "PBM", 0, MagickString("P4") },
-    { "PGM", 0, MagickString("P5") },
-    { "PPM", 0, MagickString("P6") },
-    { "PAM", 0, MagickString("P7") },
-    { "PFM", 0, MagickString("PF") },
-    { "PFM", 0, MagickString("Pf") },
-    { "PS", 0, MagickString("%!") },
-    { "PS", 0, MagickString("\004%!") },
-    { "PS", 0, MagickString("\305\320\323\306") },
-    { "PSB", 0, MagickString("8BPB") },
-    { "PSD", 0, MagickString("8BPS") },
-    { "PWP", 0, MagickString("SFW95") },
-    { "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
-    { "RLE", 0, MagickString("\122\314") },
-    { "SCT", 0, MagickString("CT") },
-    { "SFW", 0, MagickString("SFW94") },
-    { "SGI", 0, MagickString("\001\332") },
-    { "SUN", 0, MagickString("\131\246\152\225") },
-    { "SVG", 1, MagickString("?XML") },
-    { "SVG", 1, MagickString("?xml") },
-    { "TIFF", 0, MagickString("\115\115\000\052") },
-    { "TIFF", 0, MagickString("\111\111\052\000") },
-    { "TIFF64", 0, MagickString("\115\115\000\053\000\010\000\000") },
-    { "TIFF64", 0, MagickString("\111\111\053\000\010\000\000\000") },
-    { "TTF", 0, MagickString("\000\001\000\000\000") },
-    { "TXT", 0, MagickString("# ImageMagick pixel enumeration:") },
-    { "VICAR", 0, MagickString("LBLSIZE") },
-    { "VICAR", 0, MagickString("NJPL1I") },
-    { "VIFF", 0, MagickString("\253\001") },
-    { "WEBP", 8, MagickString("WEBP") },
-    { "WMF", 0, MagickString("\327\315\306\232") },
-    { "WMF", 0, MagickString("\001\000\011\000") },
-    { "WPG", 0, MagickString("\377WPC") },
-    { "XBM", 0, MagickString("#define") },
-    { "XCF", 0, MagickString("gimp xcf") },
-    { "XEF", 0, MagickString("FOVb") },
-    { "XPM", 1, MagickString("* XPM *") },
-    { "XWD", 4, MagickString("\007\000\000") },
-    { "XWD", 5, MagickString("\000\000\007") }
+    { "8BIMWTEXT", 0, MagicPattern("8\000B\000I\000M\000#") },
+    { "8BIMTEXT", 0, MagicPattern("8BIM#") },
+    { "8BIM", 0, MagicPattern("8BIM") },
+    { "BMP", 0, MagicPattern("BA") },
+    { "BMP", 0, MagicPattern("BM") },
+    { "BMP", 0, MagicPattern("CI") },
+    { "BMP", 0, MagicPattern("CP") },
+    { "BMP", 0, MagicPattern("IC") },
+    { "PICT", 0, MagicPattern("PICT") },
+    { "BMP", 0, MagicPattern("PI") },
+    { "CALS", 21, MagicPattern("version: MIL-STD-1840") },
+    { "CALS", 0, MagicPattern("srcdocid:") },
+    { "CALS", 9, MagicPattern("srcdocid:") },
+    { "CALS", 8, MagicPattern("rorient:") },
+    { "CGM", 0, MagicPattern("BEGMF") },
+    { "CIN", 0, MagicPattern("\200\052\137\327") },
+    { "CRW", 0, MagicPattern("II\x1a\x00\x00\x00HEAPCCDR") },
+    { "DCM", 128, MagicPattern("DICM") },
+    { "DCX", 0, MagicPattern("\261\150\336\72") },
+    { "DIB", 0, MagicPattern("\050\000") },
+    { "DDS", 0, MagicPattern("DDS ") },
+    { "DJVU", 0, MagicPattern("AT&TFORM") },
+    { "DOT", 0, MagicPattern("digraph") },
+    { "DPX", 0, MagicPattern("SDPX") },
+    { "DPX", 0, MagicPattern("XPDS") },
+    { "EMF", 40, MagicPattern("\040\105\115\106\000\000\001\000") },
+    { "EPT", 0, MagicPattern("\305\320\323\306") },
+    { "EXR", 0, MagicPattern("\166\057\061\001") },
+    { "FAX", 0, MagicPattern("DFAX") },
+    { "FIG", 0, MagicPattern("#FIG") },
+    { "FITS", 0, MagicPattern("IT0") },
+    { "FITS", 0, MagicPattern("SIMPLE") },
+    { "GIF", 0, MagicPattern("GIF8") },
+    { "GPLT", 0, MagicPattern("#!/usr/local/bin/gnuplot") },
+    { "HDF", 1, MagicPattern("HDF") },
+    { "HDR", 0, MagicPattern("#?RADIANCE") },
+    { "HDR", 0, MagicPattern("#?RGBE") },
+    { "HPGL", 0, MagicPattern("IN;") },
+    { "HTML", 1, MagicPattern("HTML") },
+    { "HTML", 1, MagicPattern("html") },
+    { "ILBM", 8, MagicPattern("ILBM") },
+    { "IPTCWTEXT", 0, MagicPattern("\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000") },
+    { "IPTCTEXT", 0, MagicPattern("2#0=\042�\042") },
+    { "IPTC", 0, MagicPattern("\034\002") },
+    { "JNG", 0, MagicPattern("\213JNG\r\n\032\n") },
+    { "JPEG", 0, MagicPattern("\377\330\377") },
+    { "J2K", 0, MagicPattern("\xff\x4f\xff\x51") },
+    { "JPC", 0, MagicPattern("\x0d\x0a\x87\x0a") },
+    { "JP2", 4, MagicPattern("\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a") },
+    { "MAT", 0, MagicPattern("MATLAB 5.0 MAT-file,") },
+    { "MIFF", 0, MagicPattern("Id=ImageMagick") },
+    { "MIFF", 0, MagicPattern("id=ImageMagick") },
+    { "MNG", 0, MagicPattern("\212MNG\r\n\032\n") },
+    { "MPC", 0, MagicPattern("id=MagickCache") },
+    { "MPEG", 0, MagicPattern("\000\000\001\263") },
+    { "MRW", 0, MagicPattern("\x00MRM") },
+    { "MVG", 0, MagicPattern("push graphic-context") },
+    { "ORF", 0, MagicPattern("IIRO\x08\x00\x00\x00") },
+    { "PCD", 2048, MagicPattern("PCD_") },
+    { "PCL", 0, MagicPattern("\033E\033") },
+    { "PCX", 0, MagicPattern("\012\002") },
+    { "PCX", 0, MagicPattern("\012\005") },
+    { "PDB", 60, MagicPattern("vIMGView") },
+    { "PDF", 0, MagicPattern("%PDF-") },
+    { "PES", 0, MagicPattern("#PES") },
+    { "PFA", 0, MagicPattern("%!PS-AdobeFont-1.0") },
+    { "PFB", 6, MagicPattern("%!PS-AdobeFont-1.0") },
+    { "PGX", 0, MagicPattern("\050\107\020\115\046") },
+    { "PICT", 522, MagicPattern("\000\021\002\377\014\000") },
+    { "PNG", 0, MagicPattern("\211PNG\r\n\032\n") },
+    { "PBM", 0, MagicPattern("P1") },
+    { "PGM", 0, MagicPattern("P2") },
+    { "PPM", 0, MagicPattern("P3") },
+    { "PBM", 0, MagicPattern("P4") },
+    { "PGM", 0, MagicPattern("P5") },
+    { "PPM", 0, MagicPattern("P6") },
+    { "PAM", 0, MagicPattern("P7") },
+    { "PFM", 0, MagicPattern("PF") },
+    { "PFM", 0, MagicPattern("Pf") },
+    { "PS", 0, MagicPattern("%!") },
+    { "PS", 0, MagicPattern("\004%!") },
+    { "PS", 0, MagicPattern("\305\320\323\306") },
+    { "PSB", 0, MagicPattern("8BPB") },
+    { "PSD", 0, MagicPattern("8BPS") },
+    { "PWP", 0, MagicPattern("SFW95") },
+    { "RAF", 0, MagicPattern("FUJIFILMCCD-RAW ") },
+    { "RLE", 0, MagicPattern("\122\314") },
+    { "SCT", 0, MagicPattern("CT") },
+    { "SFW", 0, MagicPattern("SFW94") },
+    { "SGI", 0, MagicPattern("\001\332") },
+    { "SUN", 0, MagicPattern("\131\246\152\225") },
+    { "SVG", 1, MagicPattern("?XML") },
+    { "SVG", 1, MagicPattern("?xml") },
+    { "TIFF", 0, MagicPattern("\115\115\000\052") },
+    { "TIFF", 0, MagicPattern("\111\111\052\000") },
+    { "TIFF64", 0, MagicPattern("\115\115\000\053\000\010\000\000") },
+    { "TIFF64", 0, MagicPattern("\111\111\053\000\010\000\000\000") },
+    { "TTF", 0, MagicPattern("\000\001\000\000\000") },
+    { "TXT", 0, MagicPattern("# ImageMagick pixel enumeration:") },
+    { "VICAR", 0, MagicPattern("LBLSIZE") },
+    { "VICAR", 0, MagicPattern("NJPL1I") },
+    { "VIFF", 0, MagicPattern("\253\001") },
+    { "WEBP", 8, MagicPattern("WEBP") },
+    { "WMF", 0, MagicPattern("\327\315\306\232") },
+    { "WMF", 0, MagicPattern("\001\000\011\000") },
+    { "WPG", 0, MagicPattern("\377WPC") },
+    { "XBM", 0, MagicPattern("#define") },
+    { "XCF", 0, MagicPattern("gimp xcf") },
+    { "XEF", 0, MagicPattern("FOVb") },
+    { "XPM", 1, MagicPattern("* XPM *") },
+    { "XWD", 4, MagicPattern("\007\000\000") },
+    { "XWD", 5, MagicPattern("\000\000\007") }
  };
 
 static LinkedListInfo
-  *magic_list = (LinkedListInfo *) NULL;
+  *magic_cache = (LinkedListInfo *) NULL;
 
 static SemaphoreInfo
   *magic_semaphore = (SemaphoreInfo *) NULL;
-
-static volatile MagickBooleanType
-  instantiate_magic = MagickFalse;
 \f
 /*
   Forward declarations.
 */
 static MagickBooleanType
-  InitializeMagicList(ExceptionInfo *),
-  LoadMagicLists(const char *,ExceptionInfo *);
+  IsMagicCacheInstantiated(ExceptionInfo *),
+  LoadMagicCache(LinkedListInfo *,const char *,const char *,const size_t,
+    ExceptionInfo *);
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%  A c q u i r e M a g i c L i s t s                                          %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  AcquireMagicCache() caches one or more magic configurations which provides a
+%  mapping between magic attributes and a magic name.
+%
+%  The format of the AcquireMagicCache method is:
+%
+%      LinkedListInfo *AcquireMagicCache(const char *filename,
+%        ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o filename: the font file name.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+static int CompareMagickInfoSize(const void *a,const void *b)
+{
+  MagicInfo
+    *ma,
+    *mb;
+
+  ma=(MagicInfo *) a;
+  mb=(MagicInfo *) b;
+
+  if (ma->offset != mb->offset)
+    return((int) (ma->offset-mb->offset));
+
+  return((int) (mb->length-ma->length));
+}
+
+static LinkedListInfo *AcquireMagicCache(const char *filename,
+  ExceptionInfo *exception)
+{
+  char
+    path[MagickPathExtent];
+
+  const StringInfo
+    *option;
+
+  LinkedListInfo
+    *magic_cache,
+    *options;
+
+  MagickStatusType
+    status;
+
+  register ssize_t
+    i;
+
+  /*
+    Load external magic map.
+  */
+  magic_cache=NewLinkedList(0);
+  if (magic_cache == (LinkedListInfo *) NULL)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+  status=MagickTrue;
+  *path='\0';
+  options=GetConfigureOptions(filename,exception);
+  option=(const StringInfo *) GetNextValueInLinkedList(options);
+  while (option != (const StringInfo *) NULL)
+  {
+    (void) CopyMagickString(path,GetStringInfoPath(option),MagickPathExtent);
+    status&=LoadMagicCache(magic_cache,(const char *)
+      GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
+    option=(const StringInfo *) GetNextValueInLinkedList(options);
+  }
+  options=DestroyConfigureOptions(options);
+  /*
+    Load built-in magic map.
+  */
+  for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
+  {
+    MagicInfo
+      *magic_info;
+
+    register const MagicMapInfo
+      *p;
+
+    p=MagicMap+i;
+    magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
+    if (magic_info == (MagicInfo *) NULL)
+      {
+        (void) ThrowMagickException(exception,GetMagickModule(),
+          ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
+        continue;
+      }
+    (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
+    magic_info->path=(char *) "[built-in]";
+    magic_info->name=(char *) p->name;
+    magic_info->offset=p->offset;
+    magic_info->target=(char *) p->magic;
+    magic_info->magic=(unsigned char *) p->magic;
+    magic_info->length=p->length;
+    magic_info->exempt=MagickTrue;
+    magic_info->signature=MagickCoreSignature;
+    status&=InsertValueInSortedLinkedList(magic_cache,CompareMagickInfoSize,
+      NULL,magic_info);
+    if (status == MagickFalse)
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
+  }
+  return(magic_cache);
+}
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   G e t M a g i c I n f o                                                   %
+%   G e t M a g i c I n f o                                                   %
 %                                                                             %
 %                                                                             %
 %                                                                             %
@@ -255,37 +368,87 @@ MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
     *p;
 
   assert(exception != (ExceptionInfo *) NULL);
-  if ((magic_list == (LinkedListInfo *) NULL) ||
-      (instantiate_magic == MagickFalse))
-    if (InitializeMagicList(exception) == MagickFalse)
-      return((const MagicInfo *) NULL);
-  if ((magic_list == (LinkedListInfo *) NULL) ||
-      (IsLinkedListEmpty(magic_list) != MagickFalse))
-    return((const MagicInfo *) NULL);
-  if (magic == (const unsigned char *) NULL)
-    return((const MagicInfo *) GetValueFromLinkedList(magic_list,0));
-  if (length == 0)
+  if (IsMagicCacheInstantiated(exception) == MagickFalse)
     return((const MagicInfo *) NULL);
   /*
     Search for magic tag.
   */
   LockSemaphoreInfo(magic_semaphore);
-  ResetLinkedListIterator(magic_list);
-  p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+  ResetLinkedListIterator(magic_cache);
+  p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
+  if (magic == (const unsigned char *) NULL)
+    {
+      UnlockSemaphoreInfo(magic_semaphore);
+      return(p);
+    }
   while (p != (const MagicInfo *) NULL)
   {
     assert(p->offset >= 0);
     if (((size_t) (p->offset+p->length) <= length) &&
         (memcmp(magic+p->offset,p->magic,p->length) == 0))
       break;
-    p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+    p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
   }
   if (p != (const MagicInfo *) NULL)
-    (void) InsertValueInLinkedList(magic_list,0,
-      RemoveElementByValueFromLinkedList(magic_list,p));
+    (void) InsertValueInLinkedList(magic_cache,0,
+      RemoveElementByValueFromLinkedList(magic_cache,p));
   UnlockSemaphoreInfo(magic_semaphore);
   return(p);
 }
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   G e t M a g i c P a t t e r n E x t e n t                                 %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  GetMagicPatternExtent() returns the the extent of the buffer that is
+%  required to check all the MagickInfos. It returns zero if the list is empty.
+%
+%  The format of the GetMagicPatternExtent method is:
+%
+%      size_t GetMagicPatternExtent(ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+MagickExport size_t GetMagicPatternExtent(ExceptionInfo *exception)
+{
+  register const MagicInfo
+    *p;
+
+  size_t
+    magickSize,
+    max;
+
+  static size_t
+    size=0;
+
+  assert(exception != (ExceptionInfo *) NULL);
+  if ((size != 0) || (IsMagicCacheInstantiated(exception) == MagickFalse))
+    return(size);
+  LockSemaphoreInfo(magic_semaphore);
+  ResetLinkedListIterator(magic_cache);
+  max=0;
+  p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
+  while (p != (const MagicInfo *) NULL)
+  {
+    magickSize=(size_t) (p->offset+p->length);
+    if (magickSize > max)
+      max=magickSize;
+    p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
+  }
+  size=max;
+  UnlockSemaphoreInfo(magic_semaphore);
+  return(size);
+}
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -360,21 +523,21 @@ MagickExport const MagicInfo **GetMagicInfoList(const char *pattern,
   if (p == (const MagicInfo *) NULL)
     return((const MagicInfo **) NULL);
   aliases=(const MagicInfo **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
+    GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
   if (aliases == (const MagicInfo **) NULL)
     return((const MagicInfo **) NULL);
   /*
     Generate magic list.
   */
   LockSemaphoreInfo(magic_semaphore);
-  ResetLinkedListIterator(magic_list);
-  p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+  ResetLinkedListIterator(magic_cache);
+  p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
   for (i=0; p != (const MagicInfo *) NULL; )
   {
     if ((p->stealth == MagickFalse) &&
         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
       aliases[i++]=p;
-    p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+    p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
   }
   UnlockSemaphoreInfo(magic_semaphore);
   qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicInfoCompare);
@@ -432,8 +595,8 @@ static int MagicCompare(const void *x,const void *y)
 }
 #endif
 
-MagickExport char **GetMagicList(const char *pattern,
-  size_t *number_aliases,ExceptionInfo *exception)
+MagickExport char **GetMagicList(const char *pattern,size_t *number_aliases,
+  ExceptionInfo *exception)
 {
   char
     **aliases;
@@ -455,18 +618,18 @@ MagickExport char **GetMagicList(const char *pattern,
   if (p == (const MagicInfo *) NULL)
     return((char **) NULL);
   aliases=(char **) AcquireQuantumMemory((size_t)
-    GetNumberOfElementsInLinkedList(magic_list)+1UL,sizeof(*aliases));
+    GetNumberOfElementsInLinkedList(magic_cache)+1UL,sizeof(*aliases));
   if (aliases == (char **) NULL)
     return((char **) NULL);
   LockSemaphoreInfo(magic_semaphore);
-  ResetLinkedListIterator(magic_list);
-  p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+  ResetLinkedListIterator(magic_cache);
+  p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
   for (i=0; p != (const MagicInfo *) NULL; )
   {
     if ((p->stealth == MagickFalse) &&
         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
       aliases[i++]=ConstantString(p->name);
-    p=(const MagicInfo *) GetNextValueInLinkedList(magic_list);
+    p=(const MagicInfo *) GetNextValueInLinkedList(magic_cache);
   }
   UnlockSemaphoreInfo(magic_semaphore);
   qsort((void *) aliases,(size_t) i,sizeof(*aliases),MagicCompare);
@@ -501,7 +664,7 @@ MagickExport const char *GetMagicName(const MagicInfo *magic_info)
 {
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
   assert(magic_info != (MagicInfo *) NULL);
-  assert(magic_info->signature == MagickSignature);
+  assert(magic_info->signature == MagickCoreSignature);
   return(magic_info->name);
 }
 \f
@@ -510,40 +673,36 @@ MagickExport const char *GetMagicName(const MagicInfo *magic_info)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n i t i a l i z e M a g i c L i s t                                     %
++   I s M a g i c C a c h e I n s t a n t i a t e d                           %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  InitializeMagicList() initializes the magic list.
+%  IsMagicCacheInstantiated() determines if the magic list is instantiated.
+%  If not, it instantiates the list and returns it.
 %
-%  The format of the InitializeMagicList method is:
+%  The format of the IsMagicInstantiated method is:
 %
-%      MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
+%      MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType InitializeMagicList(ExceptionInfo *exception)
+static MagickBooleanType IsMagicCacheInstantiated(ExceptionInfo *exception)
 {
-  if ((magic_list == (LinkedListInfo *) NULL) &&
-      (instantiate_magic == MagickFalse))
+  if (magic_cache == (LinkedListInfo *) NULL)
     {
       if (magic_semaphore == (SemaphoreInfo *) NULL)
-        AcquireSemaphoreInfo(&magic_semaphore);
+        ActivateSemaphoreInfo(&magic_semaphore);
       LockSemaphoreInfo(magic_semaphore);
-      if ((magic_list == (LinkedListInfo *) NULL) &&
-          (instantiate_magic == MagickFalse))
-        {
-          (void) LoadMagicLists(MagicFilename,exception);
-          instantiate_magic=MagickTrue;
-        }
+      if (magic_cache == (LinkedListInfo *) NULL)
+        magic_cache=AcquireMagicCache(MagicFilename,exception);
       UnlockSemaphoreInfo(magic_semaphore);
     }
-  return(magic_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
+  return(magic_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
 }
 \f
 /*
@@ -644,13 +803,14 @@ MagickExport MagickBooleanType ListMagicInfo(FILE *file,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  LoadMagicList() loads the magic configuration file which provides a mapping
+%  LoadMagicCache() loads the magic configurations which provides a mapping
 %  between magic attributes and a magic name.
 %
-%  The format of the LoadMagicList method is:
+%  The format of the LoadMagicCache method is:
 %
-%      MagickBooleanType LoadMagicList(const char *xml,const char *filename,
-%        const size_t depth,ExceptionInfo *exception)
+%      MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
+%        const char *xml,const char *filename,const size_t depth,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -663,22 +823,23 @@ MagickExport MagickBooleanType ListMagicInfo(FILE *file,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
-  const size_t depth,ExceptionInfo *exception)
+static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
+  const char *xml,const char *filename,const size_t depth,
+  ExceptionInfo *exception)
 {
   char
-    keyword[MaxTextExtent],
+    keyword[MagickPathExtent],
     *token;
 
   const char
     *q;
 
-  MagickBooleanType
-    status;
-
   MagicInfo
     *magic_info;
 
+  MagickStatusType
+    status;
+
   /*
     Load the magic map file.
   */
@@ -686,16 +847,6 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
     "Loading magic configure file \"%s\" ...",filename);
   if (xml == (char *) NULL)
     return(MagickFalse);
-  if (magic_list == (LinkedListInfo *) NULL)
-    {
-      magic_list=NewLinkedList(0);
-      if (magic_list == (LinkedListInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
-    }
   status=MagickTrue;
   magic_info=(MagicInfo *) NULL;
   token=AcquireString(xml);
@@ -707,7 +858,7 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
     GetMagickToken(q,&q,token);
     if (*token == '\0')
       break;
-    (void) CopyMagickString(keyword,token,MaxTextExtent);
+    (void) CopyMagickString(keyword,token,MagickPathExtent);
     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
       {
         /*
@@ -733,7 +884,7 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
         */
         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
         {
-          (void) CopyMagickString(keyword,token,MaxTextExtent);
+          (void) CopyMagickString(keyword,token,MagickPathExtent);
           GetMagickToken(q,&q,token);
           if (*token != '=')
             continue;
@@ -746,21 +897,22 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
               else
                 {
                   char
-                    path[MaxTextExtent],
+                    path[MagickPathExtent],
                     *xml;
 
                   GetPathComponent(filename,HeadPath,path);
                   if (*path != '\0')
                     (void) ConcatenateMagickString(path,DirectorySeparator,
-                      MaxTextExtent);
+                      MagickPathExtent);
                   if (*token == *DirectorySeparator)
-                    (void) CopyMagickString(path,token,MaxTextExtent);
+                    (void) CopyMagickString(path,token,MagickPathExtent);
                   else
-                    (void) ConcatenateMagickString(path,token,MaxTextExtent);
-                  xml=FileToString(path,~0UL,exception);
+                    (void) ConcatenateMagickString(path,token,MagickPathExtent);
+                  xml=FileToXML(path,~0UL);
                   if (xml != (char *) NULL)
                     {
-                      status=LoadMagicList(xml,path,depth+1,exception);
+                      status&=LoadMagicCache(magic_cache,xml,path,depth+1,
+                        exception);
                       xml=(char *) RelinquishMagickMemory(xml);
                     }
                 }
@@ -779,19 +931,21 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
         (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
         magic_info->path=ConstantString(filename);
         magic_info->exempt=MagickFalse;
-        magic_info->signature=MagickSignature;
+        magic_info->signature=MagickCoreSignature;
         continue;
       }
     if (magic_info == (MagicInfo *) NULL)
       continue;
     if (LocaleCompare(keyword,"/>") == 0)
       {
-        status=AppendValueToLinkedList(magic_list,magic_info);
+        status=InsertValueInSortedLinkedList(magic_cache,CompareMagickInfoSize,
+          NULL,magic_info);
         if (status == MagickFalse)
           (void) ThrowMagickException(exception,GetMagickModule(),
             ResourceLimitError,"MemoryAllocationFailed","`%s'",
             magic_info->name);
         magic_info=(MagicInfo *) NULL;
+        continue;
       }
     GetMagickToken(q,(const char **) NULL,token);
     if (*token != '=')
@@ -895,111 +1049,6 @@ static MagickBooleanType LoadMagicList(const char *xml,const char *filename,
     }
   }
   token=(char *) RelinquishMagickMemory(token);
-  return(status);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%  L o a d M a g i c L i s t s                                                %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  LoadMagicLists() loads one or more magic configuration file which provides a
-%  mapping between magic attributes and a magic name.
-%
-%  The format of the LoadMagicLists method is:
-%
-%      MagickBooleanType LoadMagicLists(const char *filename,
-%        ExceptionInfo *exception)
-%
-%  A description of each parameter follows:
-%
-%    o filename: the font file name.
-%
-%    o exception: return any errors or warnings in this structure.
-%
-*/
-static MagickBooleanType LoadMagicLists(const char *filename,
-  ExceptionInfo *exception)
-{
-  char
-    path[MaxTextExtent];
-
-  const StringInfo
-    *option;
-
-  LinkedListInfo
-    *options;
-
-  MagickStatusType
-    status;
-
-  register ssize_t
-    i;
-
-  /*
-    Load external magic map.
-  */
-  if (magic_list == (LinkedListInfo *) NULL)
-    {
-      magic_list=NewLinkedList(0);
-      if (magic_list == (LinkedListInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
-    }
-  status=MagickTrue;
-  *path='\0';
-  options=GetConfigureOptions(filename,exception);
-  option=(const StringInfo *) GetNextValueInLinkedList(options);
-  while (option != (const StringInfo *) NULL)
-  {
-    (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
-    status&=LoadMagicList((const char *) GetStringInfoDatum(option),
-      GetStringInfoPath(option),0,exception);
-    option=(const StringInfo *) GetNextValueInLinkedList(options);
-  }
-  options=DestroyConfigureOptions(options);
-  /*
-    Load built-in magic map.
-  */
-  for (i=0; i < (ssize_t) (sizeof(MagicMap)/sizeof(*MagicMap)); i++)
-  {
-    MagicInfo
-      *magic_info;
-
-    register const MagicMapInfo
-      *p;
-
-    p=MagicMap+i;
-    magic_info=(MagicInfo *) AcquireMagickMemory(sizeof(*magic_info));
-    if (magic_info == (MagicInfo *) NULL)
-      {
-        (void) ThrowMagickException(exception,GetMagickModule(),
-          ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
-        continue;
-      }
-    (void) ResetMagickMemory(magic_info,0,sizeof(*magic_info));
-    magic_info->path=(char *) "[built-in]";
-    magic_info->name=(char *) p->name;
-    magic_info->offset=p->offset;
-    magic_info->target=(char *) p->magic;
-    magic_info->magic=(unsigned char *) p->magic;
-    magic_info->length=p->length;
-    magic_info->exempt=MagickTrue;
-    magic_info->signature=MagickSignature;
-    status&=AppendValueToLinkedList(magic_list,magic_info);
-    if (status == MagickFalse)
-      (void) ThrowMagickException(exception,GetMagickModule(),
-        ResourceLimitError,"MemoryAllocationFailed","`%s'",magic_info->name);
-  }
   return(status != 0 ? MagickTrue : MagickFalse);
 }
 \f
@@ -1023,7 +1072,8 @@ static MagickBooleanType LoadMagicLists(const char *filename,
 */
 MagickPrivate MagickBooleanType MagicComponentGenesis(void)
 {
-  AcquireSemaphoreInfo(&magic_semaphore);
+  if (magic_semaphore == (SemaphoreInfo *) NULL)
+    magic_semaphore=AcquireSemaphoreInfo();
   return(MagickTrue);
 }
 \f
@@ -1070,11 +1120,10 @@ static void *DestroyMagicElement(void *magic_info)
 MagickPrivate void MagicComponentTerminus(void)
 {
   if (magic_semaphore == (SemaphoreInfo *) NULL)
-    AcquireSemaphoreInfo(&magic_semaphore);
+    ActivateSemaphoreInfo(&magic_semaphore);
   LockSemaphoreInfo(magic_semaphore);
-  if (magic_list != (LinkedListInfo *) NULL)
-    magic_list=DestroyLinkedList(magic_list,DestroyMagicElement);
-  instantiate_magic=MagickFalse;
+  if (magic_cache != (LinkedListInfo *) NULL)
+    magic_cache=DestroyLinkedList(magic_cache,DestroyMagicElement);
   UnlockSemaphoreInfo(magic_semaphore);
-  DestroySemaphoreInfo(&magic_semaphore);
+  RelinquishSemaphoreInfo(&magic_semaphore);
 }