Fix function signature and use a loop to ensure buffer is filled.

This commit is contained in:
Euan Torano 2019-08-06 19:32:22 +01:00
parent 7f23dac6dc
commit 2d25348f63
No known key found for this signature in database
GPG Key ID: 8DCD87C3275A7B24
2 changed files with 13 additions and 4 deletions

View File

@ -138,10 +138,19 @@ pub const RtlGenRandomError = error{Unexpected};
/// https://github.com/rust-lang-nursery/rand/issues/111
/// https://bugzilla.mozilla.org/show_bug.cgi?id=504270
pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void {
if (advapi32.RtlGenRandom(output.ptr, output.len) == 0) {
switch (kernel32.GetLastError()) {
else => |err| return unexpectedError(err),
var total_read: usize = 0;
var buff: []u8 = output[0..];
const max_read_size: ULONG = ULONG(maxInt(ULONG));
while (total_read < output.len) {
const to_read: ULONG = @intCast(ULONG, math.min(buff.len, max_read_size));
if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) {
return unexpectedError(kernel32.GetLastError());
}
total_read += @intCast(usize, to_read);
buff = buff[to_read..];
}
}

View File

@ -19,5 +19,5 @@ pub extern "advapi32" stdcallcc fn RegQueryValueExW(
// RtlGenRandom is known as SystemFunction036 under advapi32
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL;
pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: ULONG) BOOL;
pub const RtlGenRandom = SystemFunction036;