1
0
forked from 0ad/0ad

Windows fixes.

Avoid Boost.Random since it adds a dependency on a new library.

This was SVN commit r7654.
This commit is contained in:
Ykkrosh 2010-06-30 23:40:51 +00:00
parent 1c0536bf08
commit 20c50d32d4
9 changed files with 67 additions and 11 deletions

View File

@ -71,8 +71,8 @@ end
-- * defines: a table of symbols to define
extern_lib_defs = {
boost = {
unix_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt", "boost_random-mt" },
osx_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt", "boost_random-mt" }
unix_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt" },
osx_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt" }
},
cryptopp = {
win_names = { "cryptopp" },

View File

@ -204,3 +204,23 @@ std::wstring sys_get_user_name()
return L"";
}
LibError sys_generate_random_bytes(u8* buf, size_t count)
{
FILE* f = fopen("/dev/urandom", "rb");
if (!f)
WARN_RETURN(ERR::FAIL);
while (count)
{
size_t numread = fread(buf, 1, count, f);
if (numread == 0)
WARN_RETURN(ERR::FAIL);
buf += numread;
count -= numread;
}
fclose(f);
return INFO::OK;
}

View File

@ -1020,7 +1020,7 @@ int SDL_ShowCursor(int toggle)
// the flood (and only call SDL_SetVideoMode once a frame or similar).
// note: SDL uses WM_WINDOWPOSCHANGING, which requires calling
// GetClientRect and suffers from false alarms.
static void OnSize(HWND hWnd, UINT UNUSED(state), int clientWidth, int clientHeight)
static void OnSize(HWND UNUSED(hWnd), UINT UNUSED(state), int clientWidth, int clientHeight)
{
// if we don't prevent SDL_SetVideoMode from triggering SDL_VIDEORESIZE,
// the app's once-per-frame throttle still results in infinite recursion.

View File

@ -29,6 +29,7 @@
#include "lib/sysdep/os/win/win.h" // includes windows.h; must come before shlobj
#include <shlobj.h> // pick_dir
#include <Wincrypt.h>
#include "lib/sysdep/clipboard.h"
#include "lib/sysdep/os/win/error_dialog.h"
@ -411,3 +412,19 @@ LibError sys_pick_directory(fs::wpath& path)
return LibError_from_GLE();
}
LibError sys_generate_random_bytes(u8* buf, size_t count)
{
HCRYPTPROV hCryptProv;
if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
WARN_RETURN(ERR::FAIL);
if(!CryptGenRandom(hCryptProv, count, buf))
WARN_RETURN(ERR::FAIL);
if (!CryptReleaseContext(hCryptProv, 0))
WARN_RETURN(ERR::FAIL);
return INFO::OK;
}

View File

@ -146,6 +146,14 @@ extern LibError sys_pick_directory(fs::wpath& path);
**/
extern size_t sys_max_sector_size();
/**
* generate high-quality random bytes.
*
* this should only be used with small numbers of bytes, to avoid
* hogging the system's entropy.
**/
extern LibError sys_generate_random_bytes(u8* buf, size_t count);
/**
* directory separation character
**/

View File

@ -86,6 +86,14 @@ public:
TS_ASSERT_EQUALS(fmaxf(0.001f, 0.00001f), 0.001f);
}
void test_random()
{
u64 a = 0, b = 0;
TS_ASSERT_OK(sys_generate_random_bytes((u8*)&a, sizeof(a)));
TS_ASSERT_OK(sys_generate_random_bytes((u8*)&b, sizeof(b)));
TS_ASSERT_DIFFERS(a, b);
}
void test_sys_get_executable_name()
{
fs::wpath path;

View File

@ -23,6 +23,7 @@
#include "NetSession.h"
#include "NetTurnManager.h"
#include "lib/sysdep/sysdep.h"
#include "ps/CConsole.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
@ -30,9 +31,6 @@
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
#include <boost/nondet_random.hpp>
#include <boost/random.hpp>
CNetClient *g_NetClient = NULL;
CNetClient::CNetClient(CGame* game) :
@ -378,15 +376,13 @@ CStr CNetClient::GenerateGUID()
// a host to masquerade as someone else.
// For now, just try to pick a very random number.
boost::random_device rng;
boost::uniform_int<u32> dist(0, std::numeric_limits<u32>::max());
boost::variate_generator<boost::random_device&, boost::uniform_int<u32> > gen(rng, dist);
CStr guid;
for (size_t i = 0; i < 2; ++i)
{
u32 r = 0;
sys_generate_random_bytes((u8*)&r, sizeof(r));
char buf[32];
sprintf_s(buf, ARRAY_SIZE(buf), "%08X", gen());
sprintf_s(buf, ARRAY_SIZE(buf), "%08X", r);
guid += buf;
}

View File

@ -184,6 +184,7 @@ protected:
*/
class CNetServerTurnManager
{
NONCOPYABLE(CNetServerTurnManager);
public:
CNetServerTurnManager(CNetServer& server);

View File

@ -29,6 +29,8 @@
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
#include <enet/enet.h>
class TestNetComms : public CxxTest::TestSuite
{
public:
@ -38,10 +40,14 @@ public:
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/L"mods/public", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir()/L"_testcache"));
CXeromyces::Startup();
enet_initialize();
}
void tearDown()
{
enet_deinitialize();
CXeromyces::Terminate();
g_VFS.reset();
DeleteDirectory(DataDir()/L"_testcache");