Commit 0a5dff777 for imagemagick.org

commit 0a5dff777156dc03fff56bb84081836efe2a335e
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Tue Nov 17 18:59:28 2026 -0500

    dmr:path is now mandatory

diff --git a/coders/dmr.c b/coders/dmr.c
index 93a795bc7..e6185f96c 100644
--- a/coders/dmr.c
+++ b/coders/dmr.c
@@ -42,6 +42,7 @@
 #include "MagickCore/studio.h"
 #include "MagickCore/blob.h"
 #include "MagickCore/blob-private.h"
+#include "MagickCore/cipher.h"
 #include "MagickCore/client.h"
 #include "MagickCore/colormap.h"
 #include "MagickCore/exception.h"
@@ -63,8 +64,30 @@
 #if defined(MAGICKCORE_DMR_DELEGATE)
 #include <MagickCache/MagickCache.h>
 #endif
-
+
 #if defined(MAGICKCORE_DMR_DELEGATE)
+/*
+  Define declarations.
+*/
+#define DMRDefaultBlobFormat  "MIFF"
+
+#define DestroyDMRResources() \
+do \
+{ \
+  if (cache != (MagickCache *) NULL) \
+    cache=DestroyMagickCache(cache); \
+  if (message != (char *) NULL) \
+    message=DestroyString(message); \
+  if (passkey != (StringInfo *) NULL) \
+    passkey=DestroyDMRSecret(passkey); \
+  if (passphrase != (StringInfo *) NULL) \
+    passphrase=DestroyDMRSecret(passphrase); \
+  if (path != (char *) NULL) \
+    path=DestroyString(path); \
+  if (resource != (MagickCacheResource *) NULL) \
+    resource=DestroyMagickCacheResource(resource); \
+} while (0)
+
 /*
   Forward declarations.
 */
@@ -98,30 +121,38 @@ static MagickBooleanType
 %    o exception: return any errors or warnings in this structure.
 %
 */
+
+static StringInfo *DestroyDMRSecret(StringInfo *secret)
+{
+  if (secret != (StringInfo *) NULL)
+    {
+      volatile unsigned char
+        *p;
+
+      size_t
+        i,
+        length;
+
+      p=(volatile unsigned char *) GetStringInfoDatum(secret);
+      length=GetStringInfoLength(secret);
+      if (p != (volatile unsigned char *) NULL)
+        for (i=0; i < length; i++)
+          p[i]=0;
+      (void) DestroyStringInfo(secret);
+    }
+  return((StringInfo *) NULL);
+}
+
 static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
-#define DestroyDMRResources() \
-{ \
-  if (cache != (MagickCache *) NULL) \
-    cache=DestroyMagickCache(cache); \
-  if (message != (char *) NULL) \
-    message=DestroyString(message); \
-  if (passkey != (StringInfo *) NULL) \
-    passkey=DestroyStringInfo(passkey); \
-  if (passphrase != (StringInfo *) NULL) \
-    passphrase=DestroyStringInfo(passphrase); \
-  if (path != (char *) NULL) \
-    path=DestroyString(path); \
-  if (resource != (MagickCacheResource *) NULL) \
-    resource=DestroyMagickCacheResource(resource); \
-}
 #define ThrowDMRReadException() \
+do \
 { \
   DestroyDMRResources(); \
   if (image != (Image *) NULL) \
     image=DestroyImageList(image); \
   return(image); \
-}
+} while (0)

   char
     *message = (char *) NULL,
@@ -134,6 +165,7 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
     *image = (Image *) NULL;

   MagickBooleanType
+    decipher = MagickFalse,
     status;

   MagickCache
@@ -157,8 +189,13 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
       image_info->filename);
   option=GetImageOption(image_info,"dmr:path");
-  if (option != (const char *) NULL)
-    path=ConstantString(option);
+  if (option == (const char *) NULL)
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "missing digital media repository path","`%s'",image_info->filename);
+      ThrowDMRReadException();
+    }
+  path=ConstantString(option);
   option=GetImageOption(image_info,"dmr:passkey");
   if (option != (const char *) NULL)
     {
@@ -192,6 +229,12 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
       ThrowDMRReadException();
     }
   resource=AcquireMagickCacheResource(cache,image_info->filename);
+  if (resource == (MagickCacheResource *) NULL)
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "unable to acquire resource","`%s'",image_info->filename);
+      ThrowDMRReadException();
+    }
   type=GetMagickCacheResourceType(resource);
   image=(Image *) NULL;
   switch (type)
@@ -201,6 +244,10 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image=GetMagickCacheResourceImage(cache,resource,(const char *) NULL);
       if (image != (Image *) NULL)
         image=CloneImageList(image,exception);
+      /*
+        Only image resources are enciphered by WriteDMRImage().
+      */
+      decipher=MagickTrue;
       break;
     }
     case BlobResourceType:
@@ -208,24 +255,39 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
       ImageInfo
         *read_info;

-      void *blob = GetMagickCacheResourceBlob(cache,resource);
-      if (blob == (void *) NULL)
+      size_t
+        extent;
+
+      void
+        *blob;
+
+      blob=GetMagickCacheResourceBlob(cache,resource);
+      extent=GetMagickCacheResourceExtent(resource);
+      if ((blob == (void *) NULL) || (extent == 0))
         break;
       read_info=AcquireImageInfo();
-      image=BlobToImage(read_info,blob,GetMagickCacheResourceExtent(
-        resource),exception);
+      image=BlobToImage(read_info,blob,extent,exception);
       read_info=DestroyImageInfo(read_info);
       break;
-		}
+    }
     case MetaResourceType:
     {
-      const char *meta = GetMagickCacheResourceMeta(cache,resource);
-      if (meta == (char *) NULL)
+      const char
+        *meta;
+
+      meta=GetMagickCacheResourceMeta(cache,resource);
+      if (meta == (const char *) NULL)
         break;
       image=AcquireImage(image_info,exception);
-      (void) SetImageExtent(image,1,1,exception);
-      (void) SetImageBackgroundColor(image,exception);
-      (void) SetImageProperty(image,"dmr:meta",meta,exception);
+      if (image == (Image *) NULL)
+        break;
+      status=SetImageExtent(image,1,1,exception);
+      if (status != MagickFalse)
+        status=SetImageBackgroundColor(image,exception);
+      if (status != MagickFalse)
+        status=SetImageProperty(image,"dmr:meta",meta,exception);
+      if (status == MagickFalse)
+        image=DestroyImageList(image);
       break;
     }
     default:
@@ -236,12 +298,12 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
     }
   }
   if (image == (Image *) NULL)
-    {
+    {
       (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
         "no such resource","`%s'",image_info->filename);
       ThrowDMRReadException();
     }
-  if (passphrase != (StringInfo *) NULL)
+  if ((decipher != MagickFalse) && (passphrase != (StringInfo *) NULL))
     {
       status=PasskeyDecipherImage(image,passphrase,exception);
       if (status == MagickFalse)
@@ -263,7 +325,7 @@ static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  RegisterDMRImage() adds attributes for the Magick Cache repository format
+%  RegisterDMRImage() adds attributes for the Digital Media Repository format
 %  to the list of supported formats.  The attributes include the image format
 %  tag, a method to read and/or write the format, whether the format supports
 %  the saving of more than one frame to the same file or blob, whether the
@@ -284,7 +346,7 @@ ModuleExport size_t RegisterDMRImage(void)
   entry->decoder=(DecodeImageHandler *) ReadDMRImage;
   entry->encoder=(EncodeImageHandler *) WriteDMRImage;
 #endif
-  entry->flags^=CoderBlobSupportFlag;
+  entry->flags&=~CoderBlobSupportFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -300,7 +362,7 @@ ModuleExport size_t RegisterDMRImage(void)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  UnregisterDMRImage() removes format registrations made by the MCR module
+%  UnregisterDMRImage() removes format registrations made by the DMR module
 %  from the list of supported formats.
 %
 %  The format of the UnregisterDMRImage method is:
@@ -345,10 +407,11 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
   ExceptionInfo *exception)
 {
 #define ThrowDMRWriteException() \
+do \
 { \
   DestroyDMRResources(); \
   return(MagickFalse); \
-}
+} while (0)

   char
     *message = (char *) NULL,
@@ -358,7 +421,7 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
     *option;

   MagickBooleanType
-    status;
+    status = MagickFalse;

   MagickCache
     *cache = (MagickCache *) NULL;
@@ -386,8 +449,13 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
       image_info->filename);
   option=GetImageOption(image_info,"dmr:path");
-  if (option != (const char *) NULL)
-    path=ConstantString(option);
+  if (option == (const char *) NULL)
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "missing digital media repository path","`%s'",image_info->filename);
+      ThrowDMRWriteException();
+    }
+  path=ConstantString(option);
   option=GetImageOption(image_info,"dmr:passkey");
   if (option != (const char *) NULL)
     {
@@ -418,28 +486,54 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
       char
         *q;

+      double
+        max_ttl,
+        scale,
+        value;
+
       /*
         Time to live, absolute or relative, e.g. 1440, 2 hours, 3 days, ...
       */
-      ttl=(time_t) InterpretLocaleValue(option,&q);
+      value=InterpretLocaleValue(option,&q);
       if (q != option)
         {
+          scale=1.0;
           while (isspace((int) ((unsigned char) *q)) != 0)
             q++;
-          if (LocaleNCompare(q,"second",6) == 0)
-            ttl*=1;
-          if (LocaleNCompare(q,"minute",6) == 0)
-            ttl*=60;
-          if (LocaleNCompare(q,"hour",4) == 0)
-            ttl*=3600;
-          if (LocaleNCompare(q,"day",3) == 0)
-            ttl*=86400;
-          if (LocaleNCompare(q,"week",4) == 0)
-            ttl*=604800;
-          if (LocaleNCompare(q,"month",5) == 0)
-            ttl*=2628000;
-          if (LocaleNCompare(q,"year",4) == 0)
-            ttl*=31536000;
+          if (*q != '\0')
+            {
+              if (LocaleNCompare(q,"second",6) == 0)
+                scale=1.0;
+              else
+                if (LocaleNCompare(q,"minute",6) == 0)
+                  scale=60.0;
+                else
+                  if (LocaleNCompare(q,"hour",4) == 0)
+                    scale=3600.0;
+                  else
+                    if (LocaleNCompare(q,"day",3) == 0)
+                      scale=86400.0;
+                    else
+                      if (LocaleNCompare(q,"week",4) == 0)
+                        scale=604800.0;
+                      else
+                        if (LocaleNCompare(q,"month",5) == 0)
+                          scale=2628000.0;
+                        else
+                          if (LocaleNCompare(q,"year",4) == 0)
+                            scale=31536000.0;
+                          else
+                            (void) ThrowMagickException(exception,
+                              GetMagickModule(),OptionWarning,
+                              "unrecognized ttl unit","`%s'",option);
+            }
+          value*=scale;
+          max_ttl=(sizeof(time_t) > 4) ? 253402300799.0 : 2147483647.0;
+          if (!(value >= 0.0))  /* negative or NaN */
+            value=0.0;
+          if (value > max_ttl)
+            value=max_ttl;
+          ttl=(time_t) value;
         }
     }
   cache=AcquireMagickCache(path,passkey);
@@ -451,6 +545,12 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
       ThrowDMRWriteException();
     }
   resource=AcquireMagickCacheResource(cache,image_info->filename);
+  if (resource == (MagickCacheResource *) NULL)
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "unable to acquire resource","`%s'",image_info->filename);
+      ThrowDMRWriteException();
+    }
   SetMagickCacheResourceTTL(resource,ttl);
   type=GetMagickCacheResourceType(resource);
   switch (type)
@@ -467,7 +567,10 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
         {
           status=PasskeyEncipherImage(resource_image,passphrase,exception);
           if (status == MagickFalse)
-            ThrowDMRWriteException();
+            {
+              resource_image=DestroyImageList(resource_image);
+              ThrowDMRWriteException();
+            }
         }
       status=PutMagickCacheResourceImage(cache,resource,resource_image);
       resource_image=DestroyImageList(resource_image);
@@ -475,11 +578,32 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
     }
     case BlobResourceType:
     {
+      ImageInfo
+        *write_info;
+
       size_t
         extent;

-      ImageInfo *write_info = AcquireImageInfo();
-      void *blob = ImageToBlob(write_info,image,&extent,exception);
+      void
+        *blob;
+
+      /*
+        Pick an explicit blob format: image->magick is "DMR" here and would
+        route ImageToBlob() back into this encoder.
+      */
+      write_info=AcquireImageInfo();
+      option=GetImageOption(image_info,"dmr:format");
+      if ((option != (const char *) NULL) && (*option != '\0'))
+        (void) CopyMagickString(write_info->magick,option,MagickPathExtent);
+      else
+        if (LocaleCompare(image->magick,"DMR") != 0)
+          (void) CopyMagickString(write_info->magick,image->magick,
+            MagickPathExtent);
+        else
+          (void) CopyMagickString(write_info->magick,DMRDefaultBlobFormat,
+            MagickPathExtent);
+      extent=0;
+      blob=ImageToBlob(write_info,image,&extent,exception);
       write_info=DestroyImageInfo(write_info);
       if (blob == (void *) NULL)
         ThrowDMRWriteException();
@@ -489,25 +613,32 @@ static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
     }
     case MetaResourceType:
     {
-      const char *meta = GetImageProperty(image,"dmr:meta",exception);
+      const char
+        *meta;
+
+      meta=GetImageProperty(image,"dmr:meta",exception);
       if (meta == (const char *) NULL)
-        ThrowDMRWriteException();
+        {
+          (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+            "no such metadata","`%s'",image->filename);
+          ThrowDMRWriteException();
+        }
       status=PutMagickCacheResourceMeta(cache,resource,meta);
-      if (status == MagickFalse)
-        (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
-          "no such metadata","`%s'",image->filename);
       break;
     }
     default:
-    {
+    {
       (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
         "resource type not supported","`%s'",image->filename);
       ThrowDMRWriteException();
-      break;
     }
   }
   if (status == MagickFalse)
-    ThrowDMRWriteException();
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+        "unable to write resource","`%s'",image->filename);
+      ThrowDMRWriteException();
+    }
   DestroyDMRResources();
   return(status);
 }