]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/random.c
(no commit message)
[imagemagick] / MagickCore / random.c
index 818e7ca96444984919302a7f138a3cf8f3ce7b91..f167e004b59f845ea35b1df0627cca0ff608c49a 100644 (file)
@@ -16,7 +16,7 @@
 %                              December 2001                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2012 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/memory_.h"
 #include "MagickCore/semaphore.h"
 #include "MagickCore/random_.h"
+#include "MagickCore/random-private.h"
 #include "MagickCore/resource_.h"
 #include "MagickCore/signature-private.h"
 #include "MagickCore/string_.h"
 #include "MagickCore/thread_.h"
 #include "MagickCore/thread-private.h"
 #include "MagickCore/utility.h"
+#include "MagickCore/utility-private.h"
 /*
   Define declarations.
 */
@@ -90,6 +92,9 @@ struct _RandomInfo
   double
     normalize;
 
+  unsigned long
+    secret_key;
+
   unsigned short
     protocol_major,
     protocol_minor;
@@ -122,7 +127,7 @@ static SemaphoreInfo
   *random_semaphore = (SemaphoreInfo *) NULL;
 
 static unsigned long
-  random_seed = ~0UL;
+  secret_key = ~0UL;
 
 static MagickBooleanType
   gather_true_random = MagickFalse;
@@ -184,9 +189,10 @@ MagickExport RandomInfo *AcquireRandomInfo(void)
     random_info->signature_info));
   ResetStringInfo(random_info->reservoir);
   random_info->normalize=1.0/(~0UL);
-  random_info->semaphore=AllocateSemaphoreInfo();
+  random_info->secret_key=secret_key;
   random_info->protocol_major=RandomProtocolMajorVersion;
   random_info->protocol_minor=RandomProtocolMinorVersion;
+  random_info->semaphore=AllocateSemaphoreInfo();
   random_info->timestamp=(ssize_t) time(0);
   random_info->signature=MagickSignature;
   /*
@@ -217,9 +223,9 @@ MagickExport RandomInfo *AcquireRandomInfo(void)
   /*
     Seed pseudo random number generator.
   */
-  if (random_seed == ~0UL)
+  if (random_info->secret_key == ~0UL)
     {
-      key=GetRandomKey(random_info,sizeof(random_seed));
+      key=GetRandomKey(random_info,sizeof(random_info->secret_key));
       (void) CopyMagickMemory(random_info->seed,GetStringInfoDatum(key),
         GetStringInfoLength(key));
       key=DestroyStringInfo(key);
@@ -230,8 +236,8 @@ MagickExport RandomInfo *AcquireRandomInfo(void)
         *signature_info;
 
       signature_info=AcquireSignatureInfo();
-      key=AcquireStringInfo(sizeof(random_seed));
-      SetStringInfoDatum(key,(unsigned char *) &random_seed);
+      key=AcquireStringInfo(sizeof(random_info->secret_key));
+      SetStringInfoDatum(key,(unsigned char *) &random_info->secret_key);
       UpdateSignature(signature_info,key);
       key=DestroyStringInfo(key);
       FinalizeSignature(signature_info);
@@ -454,7 +460,7 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
 #endif
     if (file != -1)
       (void) close(file);
-    (void) remove(filename);
+    (void) remove_utf8(filename);
     SetStringInfoLength(chaos,strlen(filename));
     SetStringInfoDatum(chaos,(unsigned char *) filename);
     ConcatenateStringInfo(entropy,chaos);
@@ -490,6 +496,7 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
     */
     SetStringInfoLength(chaos,MaxEntropyExtent);
     status=NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
+    (void) status;
     ConcatenateStringInfo(entropy,chaos);
   }
 #else
@@ -527,7 +534,7 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
     filename=AcquireString("/dev/urandom");
     device=StringToStringInfo(filename);
     device=DestroyStringInfo(device);
-    file=open(filename,O_RDONLY | O_BINARY);
+    file=open_utf8(filename,O_RDONLY | O_BINARY,0);
     filename=DestroyString(filename);
     if (file != -1)
       {
@@ -545,14 +552,14 @@ static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
         filename=AcquireString("/dev/random");
         device=StringToStringInfo(filename);
         device=DestroyStringInfo(device);
-        file=open(filename,O_RDONLY | O_BINARY);
+        file=open_utf8(filename,O_RDONLY | O_BINARY,0);
         filename=DestroyString(filename);
         if (file == -1)
           {
             filename=AcquireString("/dev/srandom");
             device=StringToStringInfo(filename);
             device=DestroyStringInfo(device);
-            file=open(filename,O_RDONLY | O_BINARY);
+            file=open_utf8(filename,O_RDONLY | O_BINARY,0);
           }
         if (file != -1)
           {
@@ -655,6 +662,32 @@ MagickExport StringInfo *GetRandomKey(RandomInfo *random_info,
 %                                                                             %
 %                                                                             %
 %                                                                             %
+%   G e t R a n d o m S e c r e t K e y                                       %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  GetRandomSecretKey() returns the random secet key.
+%
+%  The format of the GetRandomSecretKey method is:
+%
+%      unsigned long GetRandomSecretKey(const RandomInfo *random_info)
+%
+%  A description of each parameter follows:
+%
+%    o random_info: the random info.
+*/
+MagickExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
+{
+  return(random_info->secret_key);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 %   G e t R a n d o m V a l u e                                               %
 %                                                                             %
 %                                                                             %
@@ -702,7 +735,7 @@ MagickExport double GetRandomValue(RandomInfo *random_info)
 %      MagickBooleanType RandomComponentGenesis(void)
 %
 */
-MagickExport MagickBooleanType RandomComponentGenesis(void)
+MagickPrivate MagickBooleanType RandomComponentGenesis(void)
 {
   AcquireSemaphoreInfo(&random_semaphore);
   return(MagickTrue);
@@ -726,7 +759,7 @@ MagickExport MagickBooleanType RandomComponentGenesis(void)
 %      RandomComponentTerminus(void)
 %
 */
-MagickExport void RandomComponentTerminus(void)
+MagickPrivate void RandomComponentTerminus(void)
 {
   if (random_semaphore == (SemaphoreInfo *) NULL)
     AcquireSemaphoreInfo(&random_semaphore);
@@ -738,34 +771,6 @@ MagickExport void RandomComponentTerminus(void)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-%   S e e d P s e u d o R a n d o m G e n e r a t o r                         %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  SeedPseudoRandomGenerator() initializes the pseudo-random number generator
-%  with a random seed.
-%
-%  The format of the SeedPseudoRandomGenerator method is:
-%
-%      void SeedPseudoRandomGenerator(const unsigned long seed)
-%
-%  A description of each parameter follows:
-%
-%    o seed: the seed.
-%
-*/
-MagickExport void SeedPseudoRandomGenerator(const unsigned long seed)
-{
-  random_seed=seed;
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 %   S e t R a n d o m K e y                                                   %
 %                                                                             %
 %                                                                             %
@@ -867,6 +872,33 @@ MagickExport void SetRandomKey(RandomInfo *random_info,const size_t length,
 %                                                                             %
 %                                                                             %
 %                                                                             %
+%   S e t R a n d o m S e c r e t K e y                                       %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  SetRandomSecretKey() sets the pseudo-random number generator secret key.
+%
+%  The format of the SetRandomSecretKey method is:
+%
+%      void SetRandomSecretKey(const unsigned long key)
+%
+%  A description of each parameter follows:
+%
+%    o key: the secret seed.
+%
+*/
+MagickExport void SetRandomSecretKey(const unsigned long key)
+{
+  secret_key=key;
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 %   S e t R a n d o m T r u e R a n d o m                                     %
 %                                                                             %
 %                                                                             %