added: #primRtlGenRandomInto:
authorClaus Gittinger <cg@exept.de>
Mon, 08 Mar 2010 13:43:38 +0100
changeset 12773 0924f835772c
parent 12772 731875678c5b
child 12774 3d5e2c85faef
added: #primRtlGenRandomInto:
Win32OperatingSystem.st
--- a/Win32OperatingSystem.st	Mon Mar 08 13:00:07 2010 +0100
+++ b/Win32OperatingSystem.st	Mon Mar 08 13:43:38 2010 +0100
@@ -8361,6 +8361,57 @@
     "
 !
 
+primRtlGenRandomInto:buffer
+    "fill a given buffer with random bytes from the RtlGenRandom function"
+
+%{
+//    BOOLEAN RtlGenRandom(
+//      __out  PVOID RandomBuffer,
+//      __in   ULONG RandomBufferLength
+//    );
+    static BOOL (__stdcall *P_RtlGenRandom)(PVOID , ULONG) = 0;
+    unsigned char *__buffer;
+    int __bufferSize;
+
+    if (__isString(buffer)) {
+        __buffer = __stringVal(buffer);
+        __bufferSize = __stringSize(buffer);
+    } else {
+        if (__isByteArray(buffer)) {
+            __buffer = __byteArrayVal(buffer);
+            __bufferSize = __byteArraySize(buffer);
+        } else {
+            goto error;
+        }
+    }
+
+    if (P_RtlGenRandom == 0) {
+        HINSTANCE hAdvapi32 = LoadLibrary("advapi32.dll");
+        // console_printf("hAdvapi32: %x\n", hAdvapi32);
+        console_printf("hAdvapi32: %x\n", hAdvapi32);
+        if (hAdvapi32) {
+            P_RtlGenRandom = (BOOL (__stdcall *)(PVOID , ULONG))
+                                GetProcAddress(hAdvapi32, "SystemFunction036");
+            console_printf("P_RtlGenRandom: %x\n", P_RtlGenRandom);
+            if (P_RtlGenRandom == 0) {
+                goto error;
+            }
+        }
+    }
+    if ((*P_RtlGenRandom)(__buffer, __bufferSize)) {
+        RETURN (buffer);
+    }
+error: ;
+%}.
+    self primitiveFailed.
+    ^ nil
+
+    "
+     Win32OperatingSystem 
+        primRtlGenRandomInto:(ByteArray new:4)
+    "
+!
+
 setEnvironment:aStringOrSymbol to:newValueString
     "set an environment variable"
 
@@ -16446,11 +16497,11 @@
 !Win32OperatingSystem class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.390 2010-02-26 10:08:48 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.391 2010-03-08 12:43:38 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.390 2010-02-26 10:08:48 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Win32OperatingSystem.st,v 1.391 2010-03-08 12:43:38 cg Exp $'
 ! !
 
 Win32OperatingSystem initialize!