mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 05:20:34 +00:00
Fix function signature and use a loop to ensure buffer is filled.
This commit is contained in:
parent
7f23dac6dc
commit
2d25348f63
@ -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..];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user