Skip to content

Commit adf42e7

Browse files
committed
Port js
1 parent bafe2e2 commit adf42e7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/js.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,33 @@ thread_local!(
2727
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
2828
);
2929

30-
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
30+
pub(crate) unsafe fn getrandom_inner(dst: *mut u8, len: usize) -> Result<(), Error> {
3131
RNG_SOURCE.with(|result| {
3232
let source = result.as_ref().map_err(|&e| e)?;
3333

3434
match source {
3535
RngSource::Node(n) => {
36-
if n.random_fill_sync(dest).is_err() {
36+
let dst = core::slice::from_raw_parts_mut(dst, len);
37+
if n.random_fill_sync(dst).is_err() {
3738
return Err(Error::NODE_RANDOM_FILL_SYNC);
3839
}
3940
}
4041
RngSource::Browser(crypto, buf) => {
4142
// getRandomValues does not work with all types of WASM memory,
4243
// so we initially write to browser memory to avoid exceptions.
43-
for chunk in dest.chunks_mut(BROWSER_CRYPTO_BUFFER_SIZE) {
44+
while len != 0 {
45+
let chunk_len = core::cmp::min(len, BROWSER_CRYPTO_BUFFER_SIZE);
4446
// The chunk can be smaller than buf's length, so we call to
4547
// JS to create a smaller view of buf without allocation.
46-
let sub_buf = buf.subarray(0, chunk.len() as u32);
48+
let sub_buf = buf.subarray(0, chunk_len as u32);
4749

4850
if crypto.get_random_values(&sub_buf).is_err() {
4951
return Err(Error::WEB_GET_RANDOM_VALUES);
5052
}
51-
sub_buf.copy_to(chunk);
53+
sub_buf.raw_copy_to_ptr(dst);
54+
55+
dst = dst.add(chunk_len);
56+
len -= chunk_len;
5257
}
5358
}
5459
};

0 commit comments

Comments
 (0)