From: VMware, Inc <> Date: Mon, 26 Apr 2010 18:24:30 +0000 (-0700) Subject: lib/misc: use a proper context for Random_Quick X-Git-Tag: 2010.04.25-253928~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f0b37c5a467f4877e333ff1bf39962dc34a05f4f;p=thirdparty%2Fopen-vm-tools.git lib/misc: use a proper context for Random_Quick 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 --- diff --git a/open-vm-tools/lib/dnd/dndCommon.c b/open-vm-tools/lib/dnd/dndCommon.c index 4d543d244..e6c94df83 100644 --- a/open-vm-tools/lib/dnd/dndCommon.c +++ b/open-vm-tools/lib/dnd/dndCommon.c @@ -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: diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 6cffaa7e5..7e3062ef5 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -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); diff --git a/open-vm-tools/lib/include/random.h b/open-vm-tools/lib/include/random.h index 19beb25bc..fe12f0638 100644 --- a/open-vm-tools/lib/include/random.h +++ b/open-vm-tools/lib/include/random.h @@ -29,20 +29,27 @@ #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); diff --git a/open-vm-tools/lib/misc/random.c b/open-vm-tools/lib/misc/random.c index fb51136f4..5b7aa92c1 100644 --- a/open-vm-tools/lib/misc/random.c +++ b/open-vm-tools/lib/misc/random.c @@ -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;