]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/random.c
Use memset() rather than ResetMagickMemory()
[imagemagick] / MagickCore / random.c
index 07e1846c551e45e24be2e6087ddff9f4a47b8fa0..68fb6ea6c163ccfccc9238ceb793bb6381deb86b 100644 (file)
 %                              December 2001                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2018 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  %
 %  obtain a copy of the License at                                            %
 %                                                                             %
-%    http://www.imagemagick.org/script/license.php                            %
+%    https://www.imagemagick.org/script/license.php                           %
 %                                                                             %
 %  Unless required by applicable law or agreed to in writing, software        %
 %  distributed under the License is distributed on an "AS IS" BASIS,          %
 #if defined(__VMS)
 #include <time.h>
 #endif
-#if defined(__MINGW32__) || defined(__MINGW64__)
+#if defined(__MINGW32__)
 #include <sys/time.h>
 #endif
 #include "MagickCore/studio.h"
 #include "MagickCore/exception.h"
 #include "MagickCore/exception-private.h"
+#include "MagickCore/image-private.h"
 #include "MagickCore/memory_.h"
+#include "MagickCore/memory-private.h"
 #include "MagickCore/semaphore.h"
 #include "MagickCore/random_.h"
 #include "MagickCore/random-private.h"
@@ -159,13 +161,6 @@ static StringInfo
 %
 */
 
-static inline size_t MagickMin(const size_t x,const size_t y)
-{
-  if (x < y)
-    return(x);
-  return(y);
-}
-
 MagickExport RandomInfo *AcquireRandomInfo(void)
 {
   const StringInfo
@@ -179,10 +174,8 @@ MagickExport RandomInfo *AcquireRandomInfo(void)
     *key,
     *nonce;
 
-  random_info=(RandomInfo *) AcquireMagickMemory(sizeof(*random_info));
-  if (random_info == (RandomInfo *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
-  (void) ResetMagickMemory(random_info,0,sizeof(*random_info));
+  random_info=(RandomInfo *) AcquireCriticalMemory(sizeof(*random_info));
+  (void) memset(random_info,0,sizeof(*random_info));
   random_info->signature_info=AcquireSignatureInfo();
   random_info->nonce=AcquireStringInfo(2*GetSignatureDigestsize(
     random_info->signature_info));
@@ -196,7 +189,7 @@ MagickExport RandomInfo *AcquireRandomInfo(void)
   random_info->protocol_minor=RandomProtocolMinorVersion;
   random_info->semaphore=AcquireSemaphoreInfo();
   random_info->timestamp=(ssize_t) time(0);
-  random_info->signature=MagickSignature;
+  random_info->signature=MagickCoreSignature;
   /*
     Seed random nonce.
   */
@@ -282,7 +275,7 @@ MagickExport RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
 {
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
   assert(random_info != (RandomInfo *) NULL);
-  assert(random_info->signature == MagickSignature);
+  assert(random_info->signature == MagickCoreSignature);
   LockSemaphoreInfo(random_info->semaphore);
   if (random_info->reservoir != (StringInfo *) NULL)
     random_info->reservoir=DestroyStringInfo(random_info->reservoir);
@@ -291,8 +284,8 @@ MagickExport RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
   if (random_info->signature_info != (SignatureInfo *) NULL)
     random_info->signature_info=DestroySignatureInfo(
       random_info->signature_info);
-  (void) ResetMagickMemory(random_info->seed,0,sizeof(*random_info->seed));
-  random_info->signature=(~MagickSignature);
+  (void) memset(random_info->seed,0,sizeof(*random_info->seed));
+  random_info->signature=(~MagickCoreSignature);
   UnlockSemaphoreInfo(random_info->semaphore);
   RelinquishSemaphoreInfo(&random_info->semaphore);
   random_info=(RandomInfo *) RelinquishMagickMemory(random_info);
@@ -387,6 +380,17 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
   SetStringInfoLength(chaos,sizeof(tid));
   SetStringInfoDatum(chaos,(unsigned char *) &tid);
   ConcatenateStringInfo(entropy,chaos);
+#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
+  { 
+    ssize_t
+      pages;
+    
+    pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
+    SetStringInfoLength(chaos,sizeof(pages));
+    SetStringInfoDatum(chaos,(unsigned char *) &pages);
+    ConcatenateStringInfo(entropy,chaos);
+  }
+#endif
 #if defined(MAGICKCORE_HAVE_GETRUSAGE) && defined(RUSAGE_SELF)
   {
     struct rusage
@@ -450,18 +454,23 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
 #if defined(MAGICKCORE_HAVE_MKSTEMP)
   {
     char
-      path[MaxTextExtent];
+      path[MagickPathExtent];
 
     int
       file;
 
-    (void) GetPathTemplate(path);
+    (void) strcpy(path,"XXXXXX");
     file=mkstemp(path);
+    if (file != -1)
+      {
+#if defined(MAGICKCORE_HAVE_FCHMOD)
+        (void) fchmod(file,0600);
+#endif
 #if defined(__OS2__)
-    setmode(file,O_BINARY);
+        setmode(file,O_BINARY);
 #endif
-    if (file != -1)
-      (void) close(file);
+        (void) close(file);
+      }
     (void) remove_utf8(path);
     SetStringInfoLength(chaos,strlen(path));
     SetStringInfoDatum(chaos,(unsigned char *) path);
@@ -471,33 +480,29 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
   {
     double
-      seconds;
+      datum;
 
     LARGE_INTEGER
-      nanoseconds;
-
-    MagickBooleanType
-      status;
+      datum1;
 
     /*
       Not crytographically strong but better than nothing.
     */
-    seconds=NTElapsedTime()+NTUserTime();
-    SetStringInfoLength(chaos,sizeof(seconds));
-    SetStringInfoDatum(chaos,(unsigned char *) &seconds);
+    datum=NTElapsedTime()+NTUserTime();
+    SetStringInfoLength(chaos,sizeof(datum));
+    SetStringInfoDatum(chaos,(unsigned char *) &datum);
     ConcatenateStringInfo(entropy,chaos);
-    if (QueryPerformanceCounter(&nanoseconds) != 0)
+    if (QueryPerformanceCounter(&datum1) != 0)
       {
-        SetStringInfoLength(chaos,sizeof(nanoseconds));
-        SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
+        SetStringInfoLength(chaos,sizeof(datum1));
+        SetStringInfoDatum(chaos,(unsigned char *) &datum1);
         ConcatenateStringInfo(entropy,chaos);
       }
     /*
       Our best hope for true entropy.
     */
     SetStringInfoLength(chaos,MaxEntropyExtent);
-    status=NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
-    (void) status;
+    (void) NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
     ConcatenateStringInfo(entropy,chaos);
   }
 #else
@@ -627,6 +632,62 @@ MagickExport double GetPseudoRandomValue(RandomInfo *random_info)
 %                                                                             %
 %                                                                             %
 %                                                                             %
++   G e t R a n d o m I n f o N o r m a l i z e                               %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  GetRandomInfoNormalize() returns the random normalize value.
+%
+%  The format of the GetRandomInfoNormalize method is:
+%
+%      double GetRandomInfoNormalize(const RandomInfo *random_info)
+%
+%  A description of each parameter follows:
+%
+%    o random_info: the random info.
+%
+*/
+MagickPrivate double GetRandomInfoNormalize(const RandomInfo *random_info)
+{
+  assert(random_info != (const RandomInfo *) NULL);
+  return(random_info->normalize);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   G e t R a n d o m I n f o S e e d                                         %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  GetRandomInfoSeed() returns the random seed.
+%
+%  The format of the GetRandomInfoSeed method is:
+%
+%      unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
+%
+%  A description of each parameter follows:
+%
+%    o random_info: the random info.
+%
+*/
+MagickPrivate unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
+{
+  assert(random_info != (RandomInfo *) NULL);
+  return(random_info->seed);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 %   G e t R a n d o m K e y                                                   %
 %                                                                             %
 %                                                                             %
@@ -738,7 +799,8 @@ MagickExport double GetRandomValue(RandomInfo *random_info)
 */
 MagickPrivate MagickBooleanType RandomComponentGenesis(void)
 {
-  random_semaphore=AcquireSemaphoreInfo();
+  if (random_semaphore == (SemaphoreInfo *) NULL)
+    random_semaphore=AcquireSemaphoreInfo();
   return(MagickTrue);
 }
 \f
@@ -923,15 +985,3 @@ MagickExport void SetRandomTrueRandom(const MagickBooleanType true_random)
 {
   gather_true_random=true_random;
 }
-
-MagickPrivate unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
-{
-  assert(random_info != (RandomInfo *) NULL);
-  return random_info->seed;
-}
-
-MagickPrivate double GetRandomInfoNormalize(RandomInfo *random_info)
-{
-  assert(random_info != (RandomInfo *) NULL);
-  return random_info->normalize;
-}