]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/magic.c
Update web pages
[imagemagick] / MagickCore / magic.c
index 93772aa1a4dc3648db1bba4e3213be52a6821f69..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  %
@@ -63,7 +63,7 @@
   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.
@@ -89,118 +89,118 @@ 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") },
-    { "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
@@ -243,11 +243,26 @@ static MagickBooleanType
 %    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[MaxTextExtent];
+    path[MagickPathExtent];
 
   const StringInfo
     *option;
@@ -274,7 +289,7 @@ static LinkedListInfo *AcquireMagicCache(const char *filename,
   option=(const StringInfo *) GetNextValueInLinkedList(options);
   while (option != (const StringInfo *) NULL)
   {
-    (void) CopyMagickString(path,GetStringInfoPath(option),MaxTextExtent);
+    (void) CopyMagickString(path,GetStringInfoPath(option),MagickPathExtent);
     status&=LoadMagicCache(magic_cache,(const char *)
       GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
     option=(const StringInfo *) GetNextValueInLinkedList(options);
@@ -307,8 +322,9 @@ static LinkedListInfo *AcquireMagicCache(const char *filename,
     magic_info->magic=(unsigned char *) p->magic;
     magic_info->length=p->length;
     magic_info->exempt=MagickTrue;
-    magic_info->signature=MagickSignature;
-    status&=AppendValueToLinkedList(magic_cache,magic_info);
+    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);
@@ -321,7 +337,7 @@ static LinkedListInfo *AcquireMagicCache(const char *filename,
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   G e t M a g i c I n f o                                                   %
+%   G e t M a g i c I n f o                                                   %
 %                                                                             %
 %                                                                             %
 %                                                                             %
@@ -379,6 +395,60 @@ MagickExport const MagicInfo *GetMagicInfo(const unsigned char *magic,
   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
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -594,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
@@ -758,18 +828,18 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
   ExceptionInfo *exception)
 {
   char
-    keyword[MaxTextExtent],
+    keyword[MagickPathExtent],
     *token;
 
   const char
     *q;
 
-  MagickBooleanType
-    status;
-
   MagicInfo
     *magic_info;
 
+  MagickStatusType
+    status;
+
   /*
     Load the magic map file.
   */
@@ -788,7 +858,7 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
     GetMagickToken(q,&q,token);
     if (*token == '\0')
       break;
-    (void) CopyMagickString(keyword,token,MaxTextExtent);
+    (void) CopyMagickString(keyword,token,MagickPathExtent);
     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
       {
         /*
@@ -814,7 +884,7 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
         */
         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
         {
-          (void) CopyMagickString(keyword,token,MaxTextExtent);
+          (void) CopyMagickString(keyword,token,MagickPathExtent);
           GetMagickToken(q,&q,token);
           if (*token != '=')
             continue;
@@ -827,17 +897,17 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
               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);
+                    (void) ConcatenateMagickString(path,token,MagickPathExtent);
                   xml=FileToXML(path,~0UL);
                   if (xml != (char *) NULL)
                     {
@@ -861,14 +931,15 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
         (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_cache,magic_info);
+        status=InsertValueInSortedLinkedList(magic_cache,CompareMagickInfoSize,
+          NULL,magic_info);
         if (status == MagickFalse)
           (void) ThrowMagickException(exception,GetMagickModule(),
             ResourceLimitError,"MemoryAllocationFailed","`%s'",
@@ -978,7 +1049,7 @@ static MagickBooleanType LoadMagicCache(LinkedListInfo *magic_cache,
     }
   }
   token=(char *) RelinquishMagickMemory(token);
-  return(status);
+  return(status != 0 ? MagickTrue : MagickFalse);
 }
 \f
 /*