浏览代码

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
Martin Prikryl 6 年之前
父节点
当前提交
2d5ba44df9
共有 1 个文件被更改,包括 18 次插入14 次删除
  1. 18 14
      libs/expat/lib/xmlparse.c

+ 18 - 14
libs/expat/lib/xmlparse.c

@@ -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;
-
-  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;
+  // 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"));
+
+  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 */