mirror of
https://github.com/amadvance/snapraid.git
synced 2026-11-26 01:46:32 -05:00
Add alternatives for RtlGenRandom
This commit is contained in:
parent
10dba1356d
commit
8e2cb48ea7
2 changed files with 34 additions and 12 deletions
|
|
@ -130,12 +130,11 @@ void os_init(int opt)
|
||||||
/* check for Wine presence */
|
/* check for Wine presence */
|
||||||
is_wine = GetProcAddress(ntdll, "wine_get_version") != 0;
|
is_wine = GetProcAddress(ntdll, "wine_get_version") != 0;
|
||||||
|
|
||||||
/* load functions not always available */
|
/* setup the standard random generator used as fallback */
|
||||||
|
srand(GetTickCount());
|
||||||
|
|
||||||
|
/* get pointer to RtlGenRandom, note that it was reported missing in some cases */
|
||||||
ptr_RtlGenRandom = (void*)GetProcAddress(dll_advapi32, "SystemFunction036");
|
ptr_RtlGenRandom = (void*)GetProcAddress(dll_advapi32, "SystemFunction036");
|
||||||
if (!ptr_RtlGenRandom) {
|
|
||||||
log_fatal("Error loading RtlGenRandom() from the ADVAPI32 module.\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set the thread execution level to avoid sleep */
|
/* set the thread execution level to avoid sleep */
|
||||||
/* first try for Windows 7 */
|
/* first try for Windows 7 */
|
||||||
|
|
@ -1797,12 +1796,30 @@ uint64_t tick_ms(void)
|
||||||
return GetTickCount();
|
return GetTickCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
int randomize(void* ptr, size_t size)
|
int randomize(void* void_ptr, size_t size)
|
||||||
{
|
{
|
||||||
if (!ptr_RtlGenRandom(ptr, size)) {
|
size_t i;
|
||||||
windows_errno(GetLastError());
|
unsigned char* ptr = void_ptr;
|
||||||
return -1;
|
|
||||||
|
/* try RtlGenRandom */
|
||||||
|
if (ptr_RtlGenRandom != 0 && ptr_RtlGenRandom(ptr, size) != 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* try rand_s */
|
||||||
|
for (i = 0; i < size; ++i) {
|
||||||
|
unsigned v = 0;
|
||||||
|
|
||||||
|
if (rand_s(&v) != 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ptr[i] = v;
|
||||||
}
|
}
|
||||||
|
if (i == size)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* fallback to standard rand */
|
||||||
|
for (i = 0; i < size; ++i)
|
||||||
|
ptr[i] = rand();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,10 @@
|
||||||
*/
|
*/
|
||||||
#define __USE_MINGW_ANSI_STDIO 1
|
#define __USE_MINGW_ANSI_STDIO 1
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Define the MSVCRT version targetting Windows XP.
|
* Define the MSVCRT version targetting Windows Vista.
|
||||||
*/
|
*/
|
||||||
#define __MSVCRT_VERSION__ 0x0601
|
#define __MSVCRT_VERSION__ 0x0600
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Include Windows Vista headers.
|
* Include Windows Vista headers.
|
||||||
|
|
@ -46,6 +46,11 @@
|
||||||
*/
|
*/
|
||||||
#define _WIN32_WINNT 0x600
|
#define _WIN32_WINNT 0x600
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable the rand_s() function.l
|
||||||
|
*/
|
||||||
|
#define _CRT_RAND_S
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue