]> granicus.if.org Git - imagemagick/blobdiff - coders/meta.c
Fixed assignment.
[imagemagick] / coders / meta.c
index f876742f9ae1b0d637b98d75fc99356132a8d658..f659ce180456feac1d184ad0feff76c433f68943 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 2001                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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 declarations.
 */
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/profile.h"
-#include "magick/splay-tree.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/module.h"
-#include "magick/token.h"
-#include "magick/utility.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/channel.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/module.h"
+#include "MagickCore/profile.h"
+#include "MagickCore/splay-tree.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/string-private.h"
+#include "MagickCore/token.h"
+#include "MagickCore/utility.h"
 \f
 /*
   Forward declarations.
 */
 static MagickBooleanType
-  WriteMETAImage(const ImageInfo *,Image *);
+  WriteMETAImage(const ImageInfo *,Image *,ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -161,7 +163,7 @@ static html_code html_codes[] = {
 
 static int stringnicmp(const char *p,const char *q,size_t n)
 {
-  register long
+  register ssize_t
     i,
     j;
 
@@ -287,7 +289,7 @@ static char *super_fgets(char **b, int *blen, Image *file)
 #define IPTC_ID 1028
 #define THUMBNAIL_ID 1033
 
-static long parse8BIM(Image *ifile, Image *ofile)
+static ssize_t parse8BIM(Image *ifile, Image *ofile)
 {
   char
     brkused,
@@ -310,14 +312,14 @@ static long parse8BIM(Image *ifile, Image *ofile)
   int
     inputlen = BUFFER_SZ;
 
-  long
-    savedolen = 0L,
-    outputlen = 0L;
-
   MagickOffsetType
     savedpos,
     currentpos;
 
+  ssize_t
+    savedolen = 0L,
+    outputlen = 0L;
+
   TokenInfo
     *token_info;
 
@@ -358,10 +360,10 @@ static long parse8BIM(Image *ifile, Image *ofile)
                 if (strcmp(newstr,"8BIM")==0)
                   dataset = 255;
                 else
-                  dataset = (unsigned char) atoi(newstr);
+                  dataset = (unsigned char) StringToLong(newstr);
                 break;
               case 1:
-                recnum = (unsigned int) atoi(newstr);
+                recnum = (unsigned int) StringToUnsignedLong(newstr);
                 break;
               case 2:
                 name=(char *) AcquireQuantumMemory(strlen(newstr)+MaxTextExtent,
@@ -379,7 +381,7 @@ static long parse8BIM(Image *ifile, Image *ofile)
             int
               next;
 
-            long
+            ssize_t
               len;
 
             char
@@ -387,7 +389,7 @@ static long parse8BIM(Image *ifile, Image *ofile)
               quoted;
 
             next=0;
-            len = (long) strlen(token);
+            len = (ssize_t) strlen(token);
             while (Tokenizer(token_info,0,newstr,(size_t) inputlen,token,"","&",
               "",0,&brkused,&next,&quoted)==0)
             {
@@ -396,7 +398,7 @@ static long parse8BIM(Image *ifile, Image *ofile)
                   char
                     *s = &token[next-1];
 
-                  len -= (long) convertHTMLcodes(s,(int) strlen(s));
+                  len -= (ssize_t) convertHTMLcodes(s,(int) strlen(s));
                 }
             }
 
@@ -413,12 +415,12 @@ static long parse8BIM(Image *ifile, Image *ofile)
                     MagickOffsetType
                       offset;
 
-                    long diff = outputlen - savedolen;
+                    ssize_t diff = outputlen - savedolen;
                     currentpos = TellBlob(ofile);
                     offset=SeekBlob(ofile,savedpos,SEEK_SET);
                     if (offset < 0)
                       return(-1);
-                    (void) WriteBlobMSBLong(ofile,(unsigned long) diff);
+                    (void) WriteBlobMSBLong(ofile,(unsigned int) diff);
                     offset=SeekBlob(ofile,currentpos,SEEK_SET);
                     if (offset < 0)
                       return(-1);
@@ -446,7 +448,7 @@ static long parse8BIM(Image *ifile, Image *ofile)
                   }
                 if (recnum != IPTC_ID)
                   {
-                    (void) WriteBlobMSBLong(ofile, (unsigned long) len);
+                    (void) WriteBlobMSBLong(ofile, (unsigned int) len);
                     outputlen += 4;
 
                     next=0;
@@ -464,7 +466,7 @@ static long parse8BIM(Image *ifile, Image *ofile)
                   {
                     /* patch in a fake length for now and fix it later */
                     savedpos = TellBlob(ofile);
-                    (void) WriteBlobMSBLong(ofile,0xFFFFFFFFUL);
+                    (void) WriteBlobMSBLong(ofile,0xFFFFFFFFU);
                     outputlen += 4;
                     savedolen = outputlen;
                   }
@@ -499,13 +501,13 @@ static long parse8BIM(Image *ifile, Image *ofile)
       MagickOffsetType
         offset;
 
-      long diff = outputlen - savedolen;
+      ssize_t diff = outputlen - savedolen;
 
       currentpos = TellBlob(ofile);
       offset=SeekBlob(ofile,savedpos,SEEK_SET);
       if (offset < 0)
         return(-1);
-      (void) WriteBlobMSBLong(ofile,(unsigned long) diff);
+      (void) WriteBlobMSBLong(ofile,(unsigned int) diff);
       offset=SeekBlob(ofile,currentpos,SEEK_SET);
       if (offset < 0)
         return(-1);
@@ -563,7 +565,7 @@ static char *super_fgets_w(char **b, int *blen, Image *file)
   return((char *) p);
 }
 
-static long parse8BIMW(Image *ifile, Image *ofile)
+static ssize_t parse8BIMW(Image *ifile, Image *ofile)
 {
   char
     brkused,
@@ -586,7 +588,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
   int
     inputlen = BUFFER_SZ;
 
-  long
+  ssize_t
     savedolen = 0L,
     outputlen = 0L;
 
@@ -634,10 +636,10 @@ static long parse8BIMW(Image *ifile, Image *ofile)
                 if (strcmp(newstr,"8BIM")==0)
                   dataset = 255;
                 else
-                  dataset = (unsigned char) atoi(newstr);
+                  dataset = (unsigned char) StringToLong(newstr);
                 break;
               case 1:
-                recnum=(unsigned int) atoi(newstr);
+                recnum=(unsigned int) StringToUnsignedLong(newstr);
                 break;
               case 2:
                 name=(char *) AcquireQuantumMemory(strlen(newstr)+MaxTextExtent,
@@ -655,7 +657,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
             int
               next;
 
-            long
+            ssize_t
               len;
 
             char
@@ -663,7 +665,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
               quoted;
 
             next=0;
-            len = (long) strlen(token);
+            len = (ssize_t) strlen(token);
             while (Tokenizer(token_info,0,newstr,(size_t) inputlen,token,"","&",
               "",0,&brkused,&next,&quoted)==0)
             {
@@ -672,7 +674,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
                   char
                     *s = &token[next-1];
 
-                  len -= (long) convertHTMLcodes(s,(int) strlen(s));
+                  len -= (ssize_t) convertHTMLcodes(s,(int) strlen(s));
                 }
             }
 
@@ -689,12 +691,12 @@ static long parse8BIMW(Image *ifile, Image *ofile)
                     MagickOffsetType
                       offset;
 
-                    long diff = outputlen - savedolen;
+                    ssize_t diff = outputlen - savedolen;
                     currentpos = TellBlob(ofile);
                     offset=SeekBlob(ofile,savedpos,SEEK_SET);
                     if (offset < 0)
                       return(-1);
-                    (void) WriteBlobMSBLong(ofile,(unsigned long) diff);
+                    (void) WriteBlobMSBLong(ofile,(unsigned int) diff);
                     offset=SeekBlob(ofile,currentpos,SEEK_SET);
                     if (offset < 0)
                       return(-1);
@@ -722,7 +724,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
                   }
                 if (recnum != IPTC_ID)
                   {
-                    (void) WriteBlobMSBLong(ofile,(unsigned long) len);
+                    (void) WriteBlobMSBLong(ofile,(unsigned int) len);
                     outputlen += 4;
 
                     next=0;
@@ -740,7 +742,7 @@ static long parse8BIMW(Image *ifile, Image *ofile)
                   {
                     /* patch in a fake length for now and fix it later */
                     savedpos = TellBlob(ofile);
-                    (void) WriteBlobMSBLong(ofile,0xFFFFFFFFUL);
+                    (void) WriteBlobMSBLong(ofile,0xFFFFFFFFU);
                     outputlen += 4;
                     savedolen = outputlen;
                   }
@@ -774,13 +776,13 @@ static long parse8BIMW(Image *ifile, Image *ofile)
       MagickOffsetType
         offset;
 
-      long diff = outputlen - savedolen;
+      ssize_t diff = outputlen - savedolen;
 
       currentpos = TellBlob(ofile);
       offset=SeekBlob(ofile,savedpos,SEEK_SET);
       if (offset < 0)
         return(-1);
-      (void) WriteBlobMSBLong(ofile,(unsigned long) diff);
+      (void) WriteBlobMSBLong(ofile,(unsigned int) diff);
       offset=SeekBlob(ofile,currentpos,SEEK_SET);
       if (offset < 0)
         return(-1);
@@ -949,8 +951,6 @@ static int jpeg_skip_till_marker(Image *ifile, int marker)
 }
 #endif
 
-static char psheader[] = "\xFF\xED\0\0Photoshop 3.0\08BIM\x04\x04\0\0\0\0";
-
 /* Embed binary IPTC data into a JPEG image. */
 static int jpeg_embed(Image *ifile, Image *ofile, Image *iptc)
 {
@@ -993,6 +993,9 @@ static int jpeg_embed(Image *ifile, Image *ofile, Image *iptc)
 
         if (iptc != (Image *)NULL)
           {
+            char
+              psheader[] = "\xFF\xED\0\0Photoshop 3.0\0" "8BIM\x04\x04\0\0\0\0";
+
             len=(unsigned int) GetBlobSize(iptc);
             if (len & 1)
               len++; /* make the length even */
@@ -1060,6 +1063,39 @@ static int jpeg_extract(Image *ifile, Image *ofile)
 }
 #endif
 
+static inline void CopyBlob(Image *source,Image *destination)
+{
+  ssize_t
+    i;
+
+  unsigned char
+    *buffer;
+
+  ssize_t
+    count,
+    length;
+
+  buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
+    sizeof(*buffer));
+  if (buffer != (unsigned char *) NULL)
+    {
+      i=0;
+      while ((length=ReadBlob(source,MagickMaxBufferExtent,buffer)) != 0)
+      {
+        count=0;
+        for (i=0; i < (ssize_t) length; i+=count)
+        {
+          count=WriteBlob(destination,(size_t) (length-i),buffer+i);
+          if (count <= 0)
+            break;
+        }
+        if (i < (ssize_t) length)
+          break;
+      }
+      buffer=(unsigned char *) RelinquishMagickMemory(buffer);
+    }
+}
+
 static Image *ReadMETAImage(const ImageInfo *image_info,
   ExceptionInfo *exception)
 {
@@ -1067,9 +1103,6 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
     *buff,
     *image;
 
-  int
-    c;
-
   MagickBooleanType
     status;
 
@@ -1092,7 +1125,7 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  image=AcquireImage(image_info);
+  image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
     {
@@ -1101,14 +1134,18 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
     }
   image->columns=1;
   image->rows=1;
-  (void) SetImageBackgroundColor(image);
+  if (SetImageBackgroundColor(image,exception) == MagickFalse)
+    {
+      image=DestroyImageList(image);
+      return((Image *) NULL);
+    }
   length=1;
   if (LocaleNCompare(image_info->magick,"8BIM",4) == 0)
     {
       /*
         Read 8BIM binary metadata.
       */
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       blob=(unsigned char *) AcquireQuantumMemory(length,sizeof(unsigned char));
@@ -1131,18 +1168,12 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
             (void) WriteBlobByte(buff,0x0);
         }
       else
-        {
-          for ( ; ; )
-          {
-            c=ReadBlobByte(image);
-            if (c == EOF)
-              break;
-            (void) WriteBlobByte(buff,(unsigned char) c);
-          }
-        }
-      profile=AcquireStringInfo((size_t) GetBlobSize(buff));
-      SetStringInfoDatum(profile,GetBlobStreamData(buff));
-      status=SetImageProfile(image,"8bim",profile);
+        CopyBlob(image,buff);
+      profile=BlobToStringInfo(GetBlobStreamData(buff),(size_t)
+        GetBlobSize(buff));
+      if (profile == (StringInfo *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      status=SetImageProfile(image,"8bim",profile,exception);
       profile=DestroyStringInfo(profile);
       if (status == MagickFalse)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
@@ -1155,8 +1186,8 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
       char
         name[MaxTextExtent];
 
-      (void) FormatMagickString(name,MaxTextExtent,"APP%d",1);
-      buff=AcquireImage((ImageInfo *) NULL);
+      (void) FormatLocaleString(name,MaxTextExtent,"APP%d",1);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       blob=(unsigned char *) AcquireQuantumMemory(length,sizeof(unsigned char));
@@ -1182,7 +1213,7 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
               ThrowReaderException(CoderError,"NoIPTCProfileAvailable");
             }
           profile=CloneStringInfo((StringInfo *) image_info->profile);
-          iptc=AcquireImage((ImageInfo *) NULL);
+          iptc=AcquireImage((ImageInfo *) NULL,exception);
           if (iptc == (Image *) NULL)
             {
               blob=DetachBlob(buff->blob);
@@ -1205,51 +1236,12 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
             }
         }
       else
-        {
-#ifdef SLOW_METHOD
-          for ( ; ; )
-          {
-            /* Really - really slow - FIX ME PLEASE!!!! */
-            c=ReadBlobByte(image);
-            if (c == EOF)
-              break;
-            (void) WriteBlobByte(buff,c);
-          }
-#else
-          long
-            i;
-
-          unsigned char
-            *buffer;
-
-          ssize_t
-            count,
-            length;
-
-          buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
-            sizeof(*buffer));
-          if (buffer != (unsigned char *) NULL)
-            {
-              i=0;
-              while ((length=ReadBlob(image,MagickMaxBufferExtent,buffer)) != 0)
-              {
-                count=0;
-                for (i=0; i < (long) length; i+=count)
-                {
-                  count=WriteBlob(buff,(size_t) (length-i),buffer+i);
-                  if (count <= 0)
-                    break;
-                }
-                if (i < (long) length)
-                  break;
-              }
-              buffer=(unsigned char *) RelinquishMagickMemory(buffer);
-            }
-#endif
-        }
-      profile=AcquireStringInfo((size_t) GetBlobSize(buff));
-      SetStringInfoDatum(profile,GetBlobStreamData(buff));
-      status=SetImageProfile(image,name,profile);
+        CopyBlob(image,buff);
+      profile=BlobToStringInfo(GetBlobStreamData(buff),(size_t)
+        GetBlobSize(buff));
+      if (profile == (StringInfo *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      status=SetImageProfile(image,name,profile,exception);
       profile=DestroyStringInfo(profile);
       if (status == MagickFalse)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
@@ -1260,7 +1252,7 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
   if ((LocaleCompare(image_info->magick,"ICC") == 0) ||
       (LocaleCompare(image_info->magick,"ICM") == 0))
     {
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       blob=(unsigned char *) AcquireQuantumMemory(length,sizeof(unsigned char));
@@ -1270,16 +1262,12 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         }
       AttachBlob(buff->blob,blob,length);
-      for ( ; ; )
-      {
-        c=ReadBlobByte(image);
-        if (c == EOF)
-          break;
-        (void) WriteBlobByte(buff,(unsigned char) c);
-      }
-      profile=AcquireStringInfo((size_t) GetBlobSize(buff));
-      SetStringInfoDatum(profile,GetBlobStreamData(buff));
-      (void) SetImageProfile(image,"icc",profile);
+      CopyBlob(image,buff);
+      profile=BlobToStringInfo(GetBlobStreamData(buff),(size_t)
+        GetBlobSize(buff));
+      if (profile == (StringInfo *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      (void) SetImageProfile(image,"icc",profile,exception);
       profile=DestroyStringInfo(profile);
       blob=DetachBlob(buff->blob);
       blob=(unsigned char *) RelinquishMagickMemory(blob);
@@ -1287,10 +1275,7 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
     }
   if (LocaleCompare(image_info->magick,"IPTC") == 0)
     {
-      register unsigned char
-        *p;
-
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       blob=(unsigned char *) AcquireQuantumMemory(length,sizeof(unsigned char));
@@ -1300,38 +1285,12 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         }
       AttachBlob(buff->blob,blob,length);
-      /* write out the header - length field patched below */
-      (void) WriteBlob(buff,11,(unsigned char *) "8BIM\04\04\0\0\0\0\01");
-      (void) WriteBlobByte(buff,0xe0);
-      if (LocaleCompare(image_info->magick,"IPTCTEXT") == 0)
-        {
-          length=(size_t) parse8BIM(image,buff);
-          if (length & 1)
-            (void) WriteBlobByte(buff,0x00);
-        }
-      else if (LocaleCompare(image_info->magick,"IPTCWTEXT") == 0)
-        {
-        }
-      else
-        {
-          for ( ; ; )
-          {
-            c=ReadBlobByte(image);
-            if (c == EOF)
-              break;
-            (void) WriteBlobByte(buff,(unsigned char) c);
-          }
-        }
-      profile=AcquireStringInfo((size_t) GetBlobSize(buff));
-      /*
-        subtract off the length of the 8BIM stuff.
-      */
-      length=GetStringInfoLength(profile)-12;
-      p=GetStringInfoDatum(profile);
-      p[10]=(unsigned char) (length >> 8);
-      p[11]=(unsigned char) (length & 0xff);
-      SetStringInfoDatum(profile,GetBlobStreamData(buff));
-      (void) SetImageProfile(image,"8bim",profile);
+      CopyBlob(image,buff);
+      profile=BlobToStringInfo(GetBlobStreamData(buff),(size_t)
+        GetBlobSize(buff));
+      if (profile == (StringInfo *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      (void) SetImageProfile(image,"8bim",profile,exception);
       profile=DestroyStringInfo(profile);
       blob=DetachBlob(buff->blob);
       blob=(unsigned char *) RelinquishMagickMemory(blob);
@@ -1339,7 +1298,7 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
     }
   if (LocaleCompare(image_info->magick,"XMP") == 0)
     {
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       blob=(unsigned char *) AcquireQuantumMemory(length,sizeof(unsigned char));
@@ -1349,16 +1308,12 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         }
       AttachBlob(buff->blob,blob,length);
-      for ( ; ; )
-      {
-        c=ReadBlobByte(image);
-        if (c == EOF)
-          break;
-        (void) WriteBlobByte(buff,(unsigned char) c);
-      }
-      profile=AcquireStringInfo((size_t) GetBlobSize(buff));
-      SetStringInfoDatum(profile,GetBlobStreamData(buff));
-      (void) SetImageProfile(image,"xmp",profile);
+      CopyBlob(image,buff);
+      profile=BlobToStringInfo(GetBlobStreamData(buff),(size_t)
+        GetBlobSize(buff));
+      if (profile == (StringInfo *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      (void) SetImageProfile(image,"xmp",profile,exception);
       profile=DestroyStringInfo(profile);
       blob=DetachBlob(buff->blob);
       blob=(unsigned char *) RelinquishMagickMemory(blob);
@@ -1388,10 +1343,10 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
 %
 %  The format of the RegisterMETAImage method is:
 %
-%      unsigned long RegisterMETAImage(void)
+%      size_t RegisterMETAImage(void)
 %
 */
-ModuleExport unsigned long RegisterMETAImage(void)
+ModuleExport size_t RegisterMETAImage(void)
 {
   MagickInfo
     *entry;
@@ -1405,7 +1360,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Photoshop resource format");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("8BIMTEXT");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1415,7 +1369,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Photoshop resource text format");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("8BIMWTEXT");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1425,7 +1378,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Photoshop resource wide text format");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("APP1");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1435,7 +1387,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Raw application information");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("APP1JPEG");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1445,7 +1396,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Raw JPEG binary data");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("EXIF");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1455,7 +1405,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Exif digital camera binary data");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("XMP");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1465,7 +1414,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("Adobe XML metadata");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("ICM");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1475,7 +1423,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("ICC Color Profile");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("ICC");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1485,7 +1432,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("ICC Color Profile");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("IPTC");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1495,7 +1441,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("IPTC Newsphoto");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("IPTCTEXT");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1505,7 +1450,6 @@ ModuleExport unsigned long RegisterMETAImage(void)
   entry->description=ConstantString("IPTC Newsphoto text format");
   entry->module=ConstantString("META");
   (void) RegisterMagickInfo(entry);
-
   entry=SetMagickInfo("IPTCWTEXT");
   entry->decoder=(DecodeImageHandler *) ReadMETAImage;
   entry->encoder=(EncodeImageHandler *) WriteMETAImage;
@@ -1570,7 +1514,7 @@ ModuleExport void UnregisterMETAImage(void)
 %  The format of the WriteMETAImage method is:
 %
 %      MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
-%        Image *image)
+%        Image *image,ExceptionInfo *exception)
 %
 %  Compression code contributed by Kyle Shorter.
 %
@@ -1580,6 +1524,8 @@ ModuleExport void UnregisterMETAImage(void)
 %
 %    o image: A pointer to a Image structure.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 
 static size_t GetIPTCStream(unsigned char **info,size_t length)
@@ -1587,7 +1533,7 @@ static size_t GetIPTCStream(unsigned char **info,size_t length)
   int
     c;
 
-  register long
+  register ssize_t
     i;
 
   register unsigned char
@@ -1597,13 +1543,10 @@ static size_t GetIPTCStream(unsigned char **info,size_t length)
     extent,
     info_length;
 
-  unsigned char
-    buffer[4] = { '\0', '\0', '\0', '\0' };
-
   unsigned int
     marker;
 
-  unsigned long
+  size_t
     tag_length;
 
   p=(*info);
@@ -1631,8 +1574,8 @@ static size_t GetIPTCStream(unsigned char **info,size_t length)
     extent-=c;
     if (extent < 4)
       break;
-    tag_length=(((unsigned long) *p) << 24) | (((unsigned long) *(p+1)) << 16) |
-      (((unsigned long) *(p+2)) << 8) | ((unsigned long) *(p+3));
+    tag_length=(((size_t) *p) << 24) | (((size_t) *(p+1)) << 16) |
+      (((size_t) *(p+2)) << 8) | ((size_t) *(p+3));
     p+=4;
     extent-=4;
     if (tag_length > extent)
@@ -1703,7 +1646,7 @@ iptc_find:
       goto iptc_find;
     info_length++;
     /*
-      Decode the length of the block that follows - long or short format.
+      Decode the length of the block that follows - ssize_t or short format.
     */
     c=(*p++);
     length--;
@@ -1712,27 +1655,32 @@ iptc_find:
     info_length++;
     if ((c & 0x80) != 0)
       {
+        /*
+          Long format.
+        */
+        tag_length=0;
         for (i=0; i < 4; i++)
         {
-          buffer[i]=(*p++);
+          tag_length<<=8;
+          tag_length|=(*p++);
           length--;
           if (length == 0)
             break;
           info_length++;
         }
-        tag_length=(((unsigned long) buffer[0]) << 24) |
-          (((unsigned long) buffer[1]) << 16) |
-          (((unsigned long) buffer[2]) << 8) | (((unsigned long) buffer[3])); 
       }
     else
       {
-        tag_length=(unsigned long) (c << 8);
+        /*
+          Short format.
+        */
+        tag_length=((long) c) << 8;
         c=(*p++);
         length--;
         if (length == 0)
           break;
         info_length++;
-        tag_length|=c;
+        tag_length|=(long) c;
       }
     if (tag_length > (length+1))
       break;
@@ -1773,13 +1721,13 @@ static void formatString(Image *ofile, const char *s, int len)
         (void) WriteBlobByte(ofile,(unsigned char) *s);
       else
         {
-          (void) FormatMagickString(temp,MaxTextExtent,"&#%d;", c & 255);
+          (void) FormatLocaleString(temp,MaxTextExtent,"&#%d;", c & 255);
           (void) WriteBlobString(ofile,temp);
         }
       break;
     }
   }
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
   (void) WriteBlobString(ofile,"\"\r\n");
 #else
 #if defined(macintosh)
@@ -1872,7 +1820,7 @@ static int formatIPTC(Image *ifile, Image *ofile)
     *readable,
     *str;
 
-  long
+  ssize_t
     tagindx,
     taglen;
 
@@ -1917,7 +1865,7 @@ static int formatIPTC(Image *ifile, Image *ofile)
     else
       readable = (unsigned char *) "";
     /*
-      We decode the length of the block that follows - long or short fmt.
+      We decode the length of the block that follows - ssize_t or short fmt.
     */
     c=ReadBlobByte(ifile);
     if (c == EOF) return -1;
@@ -1951,10 +1899,10 @@ static int formatIPTC(Image *ifile, Image *ofile)
 
     /* now finish up by formatting this binary data into ASCII equivalent */
     if (strlen((char *)readable) > 0)
-      (void) FormatMagickString(temp,MaxTextExtent,"%d#%d#%s=",
+      (void) FormatLocaleString(temp,MaxTextExtent,"%d#%d#%s=",
         (unsigned int) dataset, (unsigned int) recnum, readable);
     else
-      (void) FormatMagickString(temp,MaxTextExtent,"%d#%d=",
+      (void) FormatLocaleString(temp,MaxTextExtent,"%d#%d=",
         (unsigned int) dataset,(unsigned int) recnum);
     (void) WriteBlobString(ofile,temp);
     formatString( ofile, (char *)str, taglen );
@@ -1967,7 +1915,7 @@ static int formatIPTC(Image *ifile, Image *ofile)
   return((int) tagsfound);
 }
 
-static int readWordFromBuffer(char **s, long *len)
+static int readWordFromBuffer(char **s, ssize_t *len)
 {
   unsigned char
     buffer[2];
@@ -1986,7 +1934,7 @@ static int readWordFromBuffer(char **s, long *len)
          (((int) buffer[ 1 ]));
 }
 
-static int formatIPTCfromBuffer(Image *ofile, char *s, long len)
+static int formatIPTCfromBuffer(Image *ofile, char *s, ssize_t len)
 {
   char
     temp[MaxTextExtent];
@@ -2003,7 +1951,7 @@ static int formatIPTCfromBuffer(Image *ofile, char *s, long len)
     *readable,
     *str;
 
-  long
+  ssize_t
     tagindx,
     taglen;
 
@@ -2047,7 +1995,7 @@ static int formatIPTCfromBuffer(Image *ofile, char *s, long len)
     else
       readable=(unsigned char *) "";
     /*
-      We decode the length of the block that follows - long or short fmt.
+      We decode the length of the block that follows - ssize_t or short fmt.
     */
     c=(*s++);
     len--;
@@ -2082,10 +2030,10 @@ static int formatIPTCfromBuffer(Image *ofile, char *s, long len)
 
     /* now finish up by formatting this binary data into ASCII equivalent */
     if (strlen((char *)readable) > 0)
-      (void) FormatMagickString(temp,MaxTextExtent,"%d#%d#%s=",
+      (void) FormatLocaleString(temp,MaxTextExtent,"%d#%d#%s=",
         (unsigned int) dataset,(unsigned int) recnum, readable);
     else
-      (void) FormatMagickString(temp,MaxTextExtent,"%d#%d=",
+      (void) FormatLocaleString(temp,MaxTextExtent,"%d#%d=",
         (unsigned int) dataset,(unsigned int) recnum);
     (void) WriteBlobString(ofile,temp);
     formatString( ofile, (char *)str, taglen );
@@ -2119,6 +2067,7 @@ static int format8BIM(Image *ifile, Image *ofile)
 
   resCount=0;
   foundOSType=0; /* found the OSType */
+  (void) foundOSType;
   c=ReadBlobByte(ifile);
   while (c != EOF)
   {
@@ -2181,7 +2130,7 @@ static int format8BIM(Image *ifile, Image *ofile)
           return(-1);
       }
     }
-    count = (ssize_t) ReadBlobMSBLong(ifile);
+    count = (int) ReadBlobMSBLong(ifile);
     if (count < 0) return -1;
     /* make a buffer to hold the datand snag it from the input stream */
     str=(unsigned char *) AcquireQuantumMemory((size_t) count,sizeof(*str));
@@ -2190,7 +2139,7 @@ static int format8BIM(Image *ifile, Image *ofile)
         printf("MemoryAllocationFailed");
         return 0;
       }
-    for (i=0; i < (long) count; i++)
+    for (i=0; i < (ssize_t) count; i++)
     {
       c=ReadBlobByte(ifile);
       if (c == EOF)
@@ -2207,18 +2156,18 @@ static int format8BIM(Image *ifile, Image *ofile)
          * ASCII equivalent
          */
         if (strlen((const char *)PString) > 0)
-          (void) FormatMagickString(temp,MaxTextExtent,"8BIM#%d#%s=",ID,
+          (void) FormatLocaleString(temp,MaxTextExtent,"8BIM#%d#%s=",ID,
             PString);
         else
-          (void) FormatMagickString(temp,MaxTextExtent,"8BIM#%d=",ID);
+          (void) FormatLocaleString(temp,MaxTextExtent,"8BIM#%d=",ID);
         (void) WriteBlobString(ofile,temp);
         if (ID == IPTC_ID)
           {
             formatString(ofile, "IPTC", 4);
-            formatIPTCfromBuffer(ofile, (char *)str, (long) count);
+            formatIPTCfromBuffer(ofile, (char *)str, (ssize_t) count);
           }
         else
-          formatString(ofile, (char *)str, (long) count);
+          formatString(ofile, (char *)str, (ssize_t) count);
       }
     str=(unsigned char *) RelinquishMagickMemory(str);
     PString=(unsigned char *) RelinquishMagickMemory(PString);
@@ -2229,7 +2178,7 @@ static int format8BIM(Image *ifile, Image *ofile)
 }
 
 static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
-  Image *image)
+  Image *image,ExceptionInfo *exception)
 {
   const StringInfo
     *profile;
@@ -2258,7 +2207,9 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       profile=GetImageProfile(image,"8bim");
       if (profile == (StringInfo *) NULL)
         ThrowWriterException(CoderError,"No8BIMDataIsAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
       (void) WriteBlob(image,GetStringInfoLength(profile),
@@ -2274,10 +2225,14 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       unsigned char
         *info;
 
-      profile=GetImageProfile(image,"8bim");
+      profile=GetImageProfile(image,"iptc");
+      if (profile == (StringInfo *) NULL)
+        profile=GetImageProfile(image,"8bim");
       if (profile == (StringInfo *) NULL)
         ThrowWriterException(CoderError,"No8BIMDataIsAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       info=GetStringInfoDatum(profile);
       length=GetStringInfoLength(profile);
       length=GetIPTCStream(&info,length);
@@ -2295,10 +2250,12 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       profile=GetImageProfile(image,"8bim");
       if (profile == (StringInfo *) NULL)
         ThrowWriterException(CoderError,"No8BIMDataIsAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
       AttachBlob(buff->blob,GetStringInfoDatum(profile),
@@ -2327,10 +2284,12 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       length=GetIPTCStream(&info,length);
       if (length == 0)
         ThrowWriterException(CoderError,"NoIPTCProfileAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
-      buff=AcquireImage((ImageInfo *) NULL);
+      buff=AcquireImage((ImageInfo *) NULL,exception);
       if (buff == (Image *) NULL)
         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
       AttachBlob(buff->blob,info,length);
@@ -2352,7 +2311,9 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       profile=GetImageProfile(image,image_info->magick);
       if (profile == (StringInfo *) NULL)
         ThrowWriterException(CoderError,"NoAPP1DataIsAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
       (void) WriteBlob(image,GetStringInfoLength(profile),
@@ -2369,7 +2330,9 @@ static MagickBooleanType WriteMETAImage(const ImageInfo *image_info,
       profile=GetImageProfile(image,"icc");
       if (profile == (StringInfo *) NULL)
         ThrowWriterException(CoderError,"NoColorProfileIsAvailable");
-      status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+      assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
       (void) WriteBlob(image,GetStringInfoLength(profile),