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