From e6a19e9067d649d6bbfe0fe396027dd410a54b42 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 30 Jan 2022 22:52:44 -0500 Subject: [PATCH] Match more typical getrandom semantics --- src/lib.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0e7db60..ecdfa69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,15 +71,29 @@ unsafe extern "C" fn clock_gettime( #[no_mangle] unsafe extern "C" fn getrandom( buf: *mut libc::c_void, - buflen: libc::size_t, - _flags: libc::c_uint, + mut buflen: libc::size_t, + flags: libc::c_uint, ) -> libc::ssize_t { - let ret = ctru_sys::psInit(); - if ret != 0 { - return ret.try_into().unwrap(); + // TODO: is this needed? Maybe just `buflen = buflen.min(libc::ssize_t::MAX)` ? + buflen = buflen.min(0x1FFFFFF); + + if flags != 0 { + // no flags are supported on 3DS + *__errno() = libc::EINVAL; + return -1; } - ctru_sys::PS_GenerateRandomBytes(buf, buflen.try_into().unwrap()) - .try_into() - .unwrap() + let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen as libc::c_uint) as libc::ssize_t; + if ret < 0 { + // this is kind of a hack, but at least gives some visibility to the + // error code returned by PS_GenerateRandomBytes I guess? Another option + // might be to panic, which could use a payload of a specific error type + // that the ctru panic handler could decode into 3DS-specific human-readable + // errors. + *__errno() = ret as libc::c_int; + -1 + } else { + // safe because above ensures buflen < isize::MAX + buflen as libc::ssize_t + } }