diff options
author | Brad King <brad.king@kitware.com> | 2011-05-24 18:34:51 (GMT) |
---|---|---|
committer | CMake Topic Stage <kwrobot@kitware.com> | 2011-05-24 18:34:51 (GMT) |
commit | 44b7f2f5bcdafaed0723d293d9aa5029cc5dd06c (patch) | |
tree | da306faf1f4447030b409d3993fc7f71b441291a /Source | |
parent | 0f843584f32132969ea1c9f11970af1ea6adabc9 (diff) | |
parent | 7ff98b7a8c097b3ffcc91289c38244c627c7e142 (diff) | |
download | CMake-44b7f2f5bcdafaed0723d293d9aa5029cc5dd06c.zip CMake-44b7f2f5bcdafaed0723d293d9aa5029cc5dd06c.tar.gz CMake-44b7f2f5bcdafaed0723d293d9aa5029cc5dd06c.tar.bz2 |
Merge topic 'string-RANDOM-seed'
7ff98b7 Fix forced-seed argument type in string(RANDOM)
3d92c8c Explicitly cast time value in cmSystemTools::RandomSeed
eb6f461 VS 6: Define _WIN32_WINNT to load wincrypt.h correctly
e1b0a11 Improve string(RANDOM) default seed
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmStringCommand.cxx | 6 | ||||
-rw-r--r-- | Source/cmSystemTools.cxx | 70 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 3 |
3 files changed, 76 insertions, 3 deletions
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 19d2369..3c74dc9 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -770,7 +770,7 @@ bool cmStringCommand static bool seeded = false; bool force_seed = false; - int seed = (int) time(NULL); + unsigned int seed = 0; int length = 5; const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm" "QWERTYUIOPASDFGHJKLZXCVBNM" @@ -797,7 +797,7 @@ bool cmStringCommand else if ( args[i] == "RANDOM_SEED" ) { ++i; - seed = atoi(args[i].c_str()); + seed = static_cast<unsigned int>(atoi(args[i].c_str())); force_seed = true; } } @@ -825,7 +825,7 @@ bool cmStringCommand if (!seeded || force_seed) { seeded = true; - srand(seed); + srand(force_seed? seed : cmSystemTools::RandomSeed()); } const char* alphaPtr = alphabet.c_str(); diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 7bc89a4..b992054 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -9,6 +9,9 @@ implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more information. ============================================================================*/ +#if defined(_MSC_VER) && _MSC_VER < 1300 +# define _WIN32_WINNT 0x0400 /* for wincrypt.h */ +#endif #include "cmSystemTools.h" #include <ctype.h> #include <errno.h> @@ -31,7 +34,9 @@ #if defined(_WIN32) # include <windows.h> +# include <wincrypt.h> #else +# include <sys/time.h> # include <sys/types.h> # include <unistd.h> # include <utime.h> @@ -2233,6 +2238,71 @@ bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t) } //---------------------------------------------------------------------------- +#ifdef _WIN32 +# ifndef CRYPT_SILENT +# define CRYPT_SILENT 0x40 /* Not defined by VS 6 version of header. */ +# endif +static int WinCryptRandom(void* data, size_t size) +{ + int result = 0; + HCRYPTPROV hProvider = 0; + if(CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) + { + result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data)? 1:0; + CryptReleaseContext(hProvider, 0); + } + return result; +} +#endif + +//---------------------------------------------------------------------------- +unsigned int cmSystemTools::RandomSeed() +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + unsigned int seed = 0; + + // Try using a real random source. + if(WinCryptRandom(&seed, sizeof(seed))) + { + return seed; + } + + // Fall back to the time and pid. + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime); + unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime); + unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId()); + return t1 ^ t2 ^ pid; +#else + union + { + unsigned int integer; + char bytes[sizeof(unsigned int)]; + } seed; + + // Try using a real random source. + std::ifstream fin("/dev/urandom"); + if(fin && fin.read(seed.bytes, sizeof(seed)) && + fin.gcount() == sizeof(seed)) + { + return seed.integer; + } + + // Fall back to the time and pid. + struct timeval t; + gettimeofday(&t, 0); + unsigned int pid = static_cast<unsigned int>(getpid()); + unsigned int tv_sec = static_cast<unsigned int>(t.tv_sec); + unsigned int tv_usec = static_cast<unsigned int>(t.tv_usec); + // Since tv_usec never fills more than 11 bits we shift it to fill + // in the slow-changing high-order bits of tv_sec. + return tv_sec ^ (tv_usec << 21) ^ pid; +#endif +} + +//---------------------------------------------------------------------------- static std::string cmSystemToolsExecutableDirectory; void cmSystemTools::FindExecutableDirectory(const char* argv0) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 6f9147c..78b9abf 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -402,6 +402,9 @@ public: static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t); static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t); + /** Random seed generation. */ + static unsigned int RandomSeed(); + /** Find the directory containing the running executable. Save it in a global location to be queried by GetExecutableDirectory later. */ |