mirror of
https://github.com/winscp/winscp.git
synced 2026-10-24 02:28:55 +03:00
Reverting Expat 2.2.8 change that replaced RtlGenRandom with rand_s
rand_s is not in C++ Builder Full Expat change description: #299 #302 Windows: Replace LoadLibrary hack to access unofficial API function SystemFunction036 (RtlGenRandom) by using official API function rand_s (needs WinXP+) Source commit: b48f871e2c70d4479e061699b529954694b8f065
This commit is contained in:
@@ -734,28 +734,32 @@ writeRandomBytes_arc4random(void *target, size_t count) {
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// WINSCP - See writeRandomBytes_rand_s
|
||||
typedef BOOLEAN (APIENTRY *RTLGENRANDOM_FUNC)(PVOID, ULONG);
|
||||
HMODULE _Expat_LoadLibrary(LPCTSTR filename); /* see loadlibrary.c */
|
||||
|
||||
/* Obtain entropy on Windows using the rand_s() function which
|
||||
* generates cryptographically secure random numbers. Internally it
|
||||
* uses RtlGenRandom API which is present in Windows XP and later.
|
||||
*/
|
||||
static int
|
||||
writeRandomBytes_rand_s(void *target, size_t count) {
|
||||
size_t bytesWrittenTotal = 0;
|
||||
// WINSCP, we do not have rand_s in C++ Builder
|
||||
int success = 0; /* full count bytes written? */
|
||||
const HMODULE advapi32 = _Expat_LoadLibrary(TEXT("ADVAPI32.DLL"));
|
||||
|
||||
while (bytesWrittenTotal < count) {
|
||||
unsigned int random32 = 0;
|
||||
size_t i = 0;
|
||||
|
||||
if (rand_s(&random32))
|
||||
return 0; /* failure */
|
||||
|
||||
for (; (i < sizeof(random32)) && (bytesWrittenTotal < count);
|
||||
i++, bytesWrittenTotal++) {
|
||||
const uint8_t random8 = (uint8_t)(random32 >> (i * 8));
|
||||
((uint8_t *)target)[bytesWrittenTotal] = random8;
|
||||
if (advapi32) {
|
||||
const RTLGENRANDOM_FUNC RtlGenRandom
|
||||
= (RTLGENRANDOM_FUNC)GetProcAddress(advapi32, "SystemFunction036");
|
||||
if (RtlGenRandom) {
|
||||
if (RtlGenRandom((PVOID)target, (ULONG)count) == TRUE) {
|
||||
success = 1;
|
||||
}
|
||||
}
|
||||
FreeLibrary(advapi32);
|
||||
}
|
||||
return 1; /* success */
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif /* _WIN32 */
|
||||
|
||||
Reference in New Issue
Block a user