• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _CRT_RAND_S
2 #include <stdlib.h>
3 #include <windows.h>
4 #include <ntsecapi.h>
5 #include <errno.h>
6 #include <msvcrt.h>
7 
8 static BOOLEAN (WINAPI *pRtlGenRandom)(void*,ULONG);
9 
mingw_rand_s(unsigned int * pval)10 static errno_t mingw_rand_s(unsigned int *pval)
11 {
12     return !pval || !pRtlGenRandom || !pRtlGenRandom(pval, sizeof(*pval)) ? EINVAL : 0;
13 }
14 
15 static errno_t __cdecl init_rand_s(unsigned int*);
16 
17 errno_t (__cdecl *__MINGW_IMP_SYMBOL(rand_s))(unsigned int*) = init_rand_s;
18 
init_rand_s(unsigned int * val)19 static errno_t __cdecl init_rand_s(unsigned int *val)
20 {
21     int (__cdecl *func)(unsigned int*);
22 
23     func = (void*)GetProcAddress(__mingw_get_msvcrt_handle(), "rand_s");
24     if(!func) {
25         func = mingw_rand_s;
26         pRtlGenRandom = (void*)GetProcAddress(LoadLibraryW(L"advapi32.dll"), "SystemFunction036");
27     }
28 
29     return (__MINGW_IMP_SYMBOL(rand_s) = func)(val);
30 }
31