mirror of
https://github.com/ares-emulator/ares.git
synced 2026-10-12 02:11:04 +00:00
Begin the process of splitting nall into separate translation units. It can now be used in two ways: 1) as a header-only library by defining NALL_HEADER_ONLY 2) by compiling nall/nall.cpp sourcery has been updated to demonstrate the header-only use and everything else now builds nall.cpp. So far the only things moved into .cpp files are implementations that call Windows APIs. This allows most of ares to build without including windows.h and enables the removal of the abomination that was windows/guard.hpp. The things that actually need windows.h (hiro, ruby) must avoid using names reserved by Windows headers, but the rest of ares is free to use names like "far", "boolean", and "interface" with reckless abandon. This work is done in preparation for building against MSVC and Windows SDK headers/libraries an an alternative to MinGW-w64.
41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#include <nall/random.hpp>
|
|
|
|
#if defined(PLATFORM_LINUX) && __has_include(<sys/random.h>)
|
|
#include <sys/random.h>
|
|
#elif defined(PLATFORM_ANDROID) && __has_include(<sys/syscall.h>)
|
|
#include <sys/syscall.h>
|
|
#elif defined(PLATFORM_WINDOWS) && __has_include(<wincrypt.h>)
|
|
#include <wincrypt.h>
|
|
#else
|
|
#include <stdio.h>
|
|
#endif
|
|
|
|
namespace nall {
|
|
|
|
NALL_HEADER_INLINE auto RNGBase::randomSeed() -> u256 {
|
|
u256 seed = 0;
|
|
#if defined(PLATFORM_BSD) || defined(PLATFORM_MACOS)
|
|
for(u32 n : range(8)) seed = seed << 32 | (u32)arc4random();
|
|
#elif defined(PLATFORM_LINUX) && __has_include(<sys/random.h>)
|
|
getrandom(&seed, 32, GRND_NONBLOCK);
|
|
#elif defined(PLATFORM_ANDROID) && __has_include(<sys/syscall.h>)
|
|
syscall(__NR_getrandom, &seed, 32, 0x0001); //GRND_NONBLOCK
|
|
#elif defined(PLATFORM_WINDOWS) && __has_include(<wincrypt.h>)
|
|
HCRYPTPROV provider;
|
|
if(CryptAcquireContext(&provider, nullptr, MS_STRONG_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
|
|
CryptGenRandom(provider, 32, (BYTE*)&seed);
|
|
CryptReleaseContext(provider, 0);
|
|
}
|
|
#else
|
|
srand(time(nullptr));
|
|
for(u32 n : range(32)) seed = seed << 8 | (u8)rand();
|
|
if(auto fp = fopen("/dev/urandom", "rb")) {
|
|
fread(&seed, 32, 1, fp);
|
|
fclose(fp);
|
|
}
|
|
#endif
|
|
return seed;
|
|
}
|
|
|
|
}
|