diff options
| author | bors <bors@rust-lang.org> | 2024-09-23 16:40:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-09-23 16:40:39 +0000 |
| commit | 9d6039ccae68a2f1930ed9c1542d387b2c0c0ba6 (patch) | |
| tree | d0c8632af11209d8b85d840c1fcae8695f81aac1 /library/std/src/sys/random/windows.rs | |
| parent | 648d024a7859e1ab7fdffe5e419b6e35ccb16a4a (diff) | |
| parent | c7abc8518175a088b922d88a2b58908b4149a6b0 (diff) | |
Auto merge of #130755 - workingjubilee:rollup-zpja9b3, r=workingjubilee
Rollup of 7 pull requests Successful merges: - #129201 (std: implement the `random` feature (alternative version)) - #130536 (bootstrap: Set the dylib path when building books with rustdoc) - #130551 (Fix `break_last_token`.) - #130657 (Remove x86_64-fuchsia and aarch64-fuchsia target aliases) - #130721 (Add more test cases for block-no-opening-brace) - #130736 (Add rustfmt 2024 reformatting to git blame ignore) - #130746 (readd `@tgross35` and `@joboet` to the review rotation) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src/sys/random/windows.rs')
| -rw-r--r-- | library/std/src/sys/random/windows.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/std/src/sys/random/windows.rs b/library/std/src/sys/random/windows.rs new file mode 100644 index 00000000000..7566000f9e6 --- /dev/null +++ b/library/std/src/sys/random/windows.rs @@ -0,0 +1,20 @@ +use crate::sys::c; + +#[cfg(not(target_vendor = "win7"))] +#[inline] +pub fn fill_bytes(bytes: &mut [u8]) { + let ret = unsafe { c::ProcessPrng(bytes.as_mut_ptr(), bytes.len()) }; + // ProcessPrng is documented as always returning `TRUE`. + // https://learn.microsoft.com/en-us/windows/win32/seccng/processprng#return-value + debug_assert_eq!(ret, c::TRUE); +} + +#[cfg(target_vendor = "win7")] +pub fn fill_bytes(mut bytes: &mut [u8]) { + while !bytes.is_empty() { + let len = bytes.len().try_into().unwrap_or(u32::MAX); + let ret = unsafe { c::RtlGenRandom(bytes.as_mut_ptr().cast(), len) }; + assert_ne!(ret, 0, "failed to generate random data"); + bytes = &mut bytes[len as usize..]; + } +} |
