Reformatting using AStyle

This commit is contained in:
Vinnie Falco
2013-06-14 08:45:13 -07:00
parent 36bd8f7173
commit 521e812fc4
294 changed files with 54609 additions and 47598 deletions

View File

@@ -1,18 +1,18 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
Copyright (c) 2011-2013, OpenCoin, Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
@@ -55,15 +55,15 @@ void RandomNumbers::fillBytes (void* destinationBuffer, int numberOfBytes)
}
#ifdef PURIFY
memset (destinationBuffer, 0, numberOfBytes);
memset (destinationBuffer, 0, numberOfBytes);
#endif
if (RAND_bytes (reinterpret_cast <unsigned char*> (destinationBuffer), numberOfBytes) != 1)
{
assert(false);
if (RAND_bytes (reinterpret_cast <unsigned char*> (destinationBuffer), numberOfBytes) != 1)
{
assert (false);
throw std::runtime_error ("Entropy pool not seeded");
}
}
}
RandomNumbers& RandomNumbers::getInstance ()
@@ -82,68 +82,72 @@ RandomNumbers& RandomNumbers::getInstance ()
// Get entropy from the Windows crypto provider
bool RandomNumbers::platformAddEntropy ()
{
char name[512], rand[128];
DWORD count = 500;
HCRYPTPROV cryptoHandle;
char name[512], rand[128];
DWORD count = 500;
HCRYPTPROV cryptoHandle;
if (!CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count))
{
if (!CryptGetDefaultProvider (PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count))
{
#ifdef DEBUG
std::cerr << "Unable to get default crypto provider" << std::endl;
std::cerr << "Unable to get default crypto provider" << std::endl;
#endif
return false;
}
return false;
}
if (!CryptAcquireContext(&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
if (!CryptAcquireContext (&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
#ifdef DEBUG
std::cerr << "Unable to acquire crypto provider" << std::endl;
std::cerr << "Unable to acquire crypto provider" << std::endl;
#endif
return false;
}
return false;
}
if(!CryptGenRandom(cryptoHandle, 128, reinterpret_cast<BYTE*>(rand)))
{
if (!CryptGenRandom (cryptoHandle, 128, reinterpret_cast<BYTE*> (rand)))
{
#ifdef DEBUG
std::cerr << "Unable to get entropy from crypto provider" << std::endl;
std::cerr << "Unable to get entropy from crypto provider" << std::endl;
#endif
CryptReleaseContext(cryptoHandle, 0);
return false;
}
CryptReleaseContext (cryptoHandle, 0);
return false;
}
CryptReleaseContext(cryptoHandle, 0);
RAND_seed(rand, 128);
CryptReleaseContext (cryptoHandle, 0);
RAND_seed (rand, 128);
return true;
return true;
}
#else
bool RandomNumbers::platformAddEntropy ()
{
char rand[128];
std::ifstream reader;
char rand[128];
std::ifstream reader;
reader.open("/dev/urandom", std::ios::in | std::ios::binary);
if (!reader.is_open())
{
#ifdef DEBUG
std::cerr << "Unable to open random source" << std::endl;
#endif
return false;
}
reader.read(rand, 128);
reader.open ("/dev/urandom", std::ios::in | std::ios::binary);
int bytesRead = reader.gcount();
if (bytesRead == 0)
{
if (!reader.is_open ())
{
#ifdef DEBUG
std::cerr << "Unable to read from random source" << std::endl;
std::cerr << "Unable to open random source" << std::endl;
#endif
return false;
}
RAND_seed(rand, bytesRead);
return bytesRead >= 64;
return false;
}
reader.read (rand, 128);
int bytesRead = reader.gcount ();
if (bytesRead == 0)
{
#ifdef DEBUG
std::cerr << "Unable to read from random source" << std::endl;
#endif
return false;
}
RAND_seed (rand, bytesRead);
return bytesRead >= 64;
}
#endif
@@ -161,63 +165,67 @@ bool RandomNumbers::platformAddEntropy ()
void RandomNumbers::platformAddPerformanceMonitorEntropy ()
{
// VFALCO TODO Remove all this fancy stuff
struct
{
int64 operator() () const
{
return time (NULL);
}
} GetTime;
struct
{
int64 operator () () const
{
return time (NULL);
}
} GetTime;
struct
{
void operator() ()
{
struct
{
// VFALCO TODO clean this up
int64 operator() () const
{
int64 nCounter = 0;
struct
{
void operator () ()
{
struct
{
// VFALCO TODO clean this up
int64 operator () () const
{
int64 nCounter = 0;
#if defined(WIN32) || defined(WIN64)
QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
QueryPerformanceCounter ((LARGE_INTEGER*)&nCounter);
#else
timeval t;
gettimeofday(&t, NULL);
nCounter = t.tv_sec * 1000000 + t.tv_usec;
timeval t;
gettimeofday (&t, NULL);
nCounter = t.tv_sec * 1000000 + t.tv_usec;
#endif
return nCounter;
}
} GetPerformanceCounter;
return nCounter;
}
} GetPerformanceCounter;
// Seed with CPU performance counter
int64 nCounter = GetPerformanceCounter();
RAND_add(&nCounter, sizeof(nCounter), 1.5);
memset(&nCounter, 0, sizeof(nCounter));
}
} RandAddSeed;
// Seed with CPU performance counter
int64 nCounter = GetPerformanceCounter ();
RAND_add (&nCounter, sizeof (nCounter), 1.5);
memset (&nCounter, 0, sizeof (nCounter));
}
} RandAddSeed;
RandAddSeed();
RandAddSeed ();
// This can take up to 2 seconds, so only do it every 10 minutes
static int64 nLastPerfmon;
if (GetTime () < nLastPerfmon + 10 * 60)
return;
nLastPerfmon = GetTime ();
// This can take up to 2 seconds, so only do it every 10 minutes
static int64 nLastPerfmon;
if (GetTime () < nLastPerfmon + 10 * 60)
return;
nLastPerfmon = GetTime ();
#ifdef WIN32
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
// Seed with the entire set of perfmon data
unsigned char pdata[250000];
memset(pdata, 0, sizeof(pdata));
unsigned long nSize = sizeof(pdata);
long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS)
{
RAND_add(pdata, nSize, nSize/100.0);
memset(pdata, 0, nSize);
//printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
}
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
// Seed with the entire set of perfmon data
unsigned char pdata[250000];
memset (pdata, 0, sizeof (pdata));
unsigned long nSize = sizeof (pdata);
long ret = RegQueryValueExA (HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
RegCloseKey (HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS)
{
RAND_add (pdata, nSize, nSize / 100.0);
memset (pdata, 0, nSize);
//printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
}
#endif
}