]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/misc: use a proper context for Random_Quick
authorVMware, Inc <>
Mon, 26 Apr 2010 18:24:30 +0000 (11:24 -0700)
committerMarcelo Vanzin <mvanzin@vmware.com>
Tue, 27 Apr 2010 03:48:53 +0000 (20:48 -0700)
The Random_Quick context pointer is "void *" when it should be
"struct rqContext *". Use the not having to define the structure
trick so as to not expose the contents of the context.

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/lib/dnd/dndCommon.c
open-vm-tools/lib/file/file.c
open-vm-tools/lib/include/random.h
open-vm-tools/lib/misc/random.c

index 4d543d24411fa35cec1386128819c02f5f27c708..e6c94df83597d641e9d8621ea9b97c0592c1f6b7 100644 (file)
@@ -127,16 +127,16 @@ DnD_CreateStagingDirectory(void)
 
    /* Only create a directory if we didn't find one above. */
    if (!found) {
-      void *p;
+      rqContext *context;
 
-      p = Random_QuickSeed((unsigned)time(NULL));
+      context = Random_QuickSeed((unsigned)time(NULL));
 
       for (i = 0; i < 10; i++) {
          Unicode temp;
 
          /* Each staging directory is given a random name. */
          Unicode_Free(ret);
-         temp = Unicode_Format("%08x%c", Random_Quick(p), DIRSEPC);
+         temp = Unicode_Format("%08x%c", Random_Quick(context), DIRSEPC);
          ASSERT_MEM_ALLOC(temp);
          ret = Unicode_Append(root, temp);
          Unicode_Free(temp);
@@ -148,7 +148,7 @@ DnD_CreateStagingDirectory(void)
          }
       }
 
-      free(p);
+      free(context);
    }
 
 exit:
index 6cffaa7e56993f25583e84bb9218ff443c749e13..7e3062ef56aa5e5fe0bfda84018cc164a94465a5 100644 (file)
@@ -2358,12 +2358,12 @@ uint32
 FileSimpleRandom(void)
 {
    static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
-   char *context;
+   rqContext *context;
 
    context = Atomic_ReadPtr(&atomic);
 
    if (UNLIKELY(context == NULL)) {
-      void *p;
+      rqContext *newContext;
       uint32 value;
 
       /*
@@ -2378,10 +2378,10 @@ FileSimpleRandom(void)
       value = getpid();
 #endif
 
-      p = Random_QuickSeed(value);
+      newContext = Random_QuickSeed(value);
 
-      if (Atomic_ReadIfEqualWritePtr(&atomic, NULL, p)) {
-         free(p);
+      if (Atomic_ReadIfEqualWritePtr(&atomic, NULL, (void *) newContext)) {
+         free(newContext);
       }
 
       context = Atomic_ReadPtr(&atomic);
index 19beb25bc2b2417591485720b5f1ce9cdaccbc94..fe12f0638fabade7de10e98e9bca20ee6280aa1e 100644 (file)
 
 #include "vm_basic_types.h"
 
-
 Bool
 Random_Crypto(unsigned int size, // IN
               void *buffer);     // OUT
 
 /*
- * High quality random number generator.
+ * High quality - research grade - random number generator.
+ *
+ * Despite its apparent complexity this RNG is extremely fast.
  */
 
-void *
-Random_QuickSeed(uint32 seed); // IN
+typedef struct rqContext rqContext;
+
+rqContext *
+Random_QuickSeed(uint32 seed);
 
 uint32
-Random_Quick(void *context);
+Random_Quick(rqContext *context);
+
+/*
+ * Simple multiplicative conguential RNG.
+ */
 
 int
 Random_Simple(int seed);
index fb51136f4a5131414229ebb749132901d06ae180..5b7aa92c133168af86ff3cc74f15075cffab0d68 100644 (file)
@@ -175,6 +175,9 @@ Random_Crypto(unsigned int size,  // IN:
  * Side Effects:
  *      None
  *
+ * NOTE:
+ *      Despite the look of the code this RNG is extremely fast.
+ *
  *-----------------------------------------------------------------------------
  */
 
@@ -188,15 +191,15 @@ Random_Crypto(unsigned int size,  // IN:
 #define C 0xDB8B0000
 #define L 16
 
-struct rngstate {
+struct rqContext {
   uint32 x[N];
   int p, q;
 };
 
-void *
+rqContext *
 Random_QuickSeed(uint32 seed)  // IN:
 {
-   struct rngstate *rs;
+   struct rqContext *rs;
 
    const uint32 xx[N] = {
       0x95F24DAB, 0x0B685215, 0xE76CCAE7, 0xAF3EC239, 0x715FAD23,
@@ -206,7 +209,7 @@ Random_QuickSeed(uint32 seed)  // IN:
       0x512C0C03, 0xEA857CCD, 0x4CC1D30F, 0x8891A8A1, 0xA6B7AADB
    };
 
-   rs = (struct rngstate *) malloc(sizeof *rs);
+   rs = (struct rqContext *) malloc(sizeof *rs);
 
    if (rs != NULL) {
       uint32 i;
@@ -219,7 +222,7 @@ Random_QuickSeed(uint32 seed)  // IN:
       rs->q = N - M - 1;
    }
 
-   return (void *) rs;
+   return rs;
 }
 
 
@@ -239,17 +242,18 @@ Random_QuickSeed(uint32 seed)  // IN:
  * Side Effects:
  *      The RNG context is modified for later use by Random_Quick.
  *
+ * NOTE:
+ *      Despite the look of the code this RNG is extremely fast.
+ *
  *-----------------------------------------------------------------------------
  */
 
 uint32
-Random_Quick(void *context)  // IN/OUT:
+Random_Quick(rqContext *rs)  // IN/OUT:
 {
    uint32 y, z;
 
-   struct rngstate *rs = (struct rngstate *) context;
-
-   ASSERT(context);
+   ASSERT(rs);
 
    if (rs->p == N - 1) {
       rs->p = 0;
@@ -290,17 +294,16 @@ Random_Quick(void *context)  // IN/OUT:
  *
  * Random_Simple --
  *
- *      Lifted from Util_FastRand() in
- *      /vmkernel-main/bora/vmcore/vmm/main/util_monitor.c The header
- *      comment of Util_FastRand(): Generates the next random number in the
- *      pseudo-random sequence defined by the multiplicative linear
- *      congruential generator S' = 16807 * S mod (2^31 - 1).  This is the
- *      ACM "minimal standard random number generator".  Based on method
- *      described by D.G. Carta in CACM, January 1990.  Usage: provide
- *      previous random number as the seed for next one.
+ *      Generates the next random number in the pseudo-random sequence
+ *      defined by the multiplicative linear congruential generator
+ *      S' = 33614 * S mod (2^31 - 1).  This is the ACM "minimal standard
+ *      random number generator". Based on method described by D.G. Carta
+ *      in CACM, January 1990.
+ *
+ *      Usage: provide previous random number as the seed for next one.
  *
  * Results:
- *      A random integrer number is returned.
+ *      A random integer number is returned.
  *
  * Side Effects:
  *      None.
@@ -311,8 +314,8 @@ Random_Quick(void *context)  // IN/OUT:
 int
 Random_Simple(int seed)  // IN:
 {
-   uint64 product    = 33614 * (uint64)seed;
-   uint32 product_lo = (uint32)(product & 0xFFFFFFFF) >> 1;
+   uint64 product    = 33614 * (uint64) seed;
+   uint32 product_lo = (uint32) (product & 0xFFFFFFFF) >> 1;
    uint32 product_hi = product >> 32;
    int32  test       = product_lo + product_hi;