77// except according to those terms.
88
99//! Implementation for FreeBSD and NetBSD
10- use crate :: { util_libc:: sys_fill_exact, Error } ;
10+ use crate :: { util :: raw_chunks , util_libc:: sys_fill_exact, Error } ;
1111use core:: ptr;
1212
13- fn kern_arnd ( buf : & mut [ u8 ] ) -> libc:: ssize_t {
13+ unsafe fn kern_arnd ( dst : * mut u8 , mut len : usize ) -> libc:: ssize_t {
1414 static MIB : [ libc:: c_int ; 2 ] = [ libc:: CTL_KERN , libc:: KERN_ARND ] ;
15- let mut len = buf. len ( ) ;
16- let ret = unsafe {
17- libc:: sysctl (
18- MIB . as_ptr ( ) ,
19- MIB . len ( ) as libc:: c_uint ,
20- buf. as_mut_ptr ( ) as * mut _ ,
21- & mut len,
22- ptr:: null ( ) ,
23- 0 ,
24- )
25- } ;
15+ // TODO: use `cast` on MSRV bump to 1.38
16+ let ret = libc:: sysctl (
17+ MIB . as_ptr ( ) ,
18+ MIB . len ( ) as libc:: c_uint ,
19+ dst as * mut libc:: c_void ,
20+ & mut len,
21+ ptr:: null ( ) ,
22+ 0 ,
23+ ) ;
2624 if ret == -1 {
2725 -1
2826 } else {
2927 len as libc:: ssize_t
3028 }
3129}
3230
33- pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
31+ pub unsafe fn getrandom_inner ( dst : * mut u8 , len : usize ) -> Result < ( ) , Error > {
3432 // getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0
3533 #[ cfg( target_os = "freebsd" ) ]
3634 {
@@ -40,14 +38,13 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
4038 unsafe extern "C" fn ( * mut u8 , libc:: size_t , libc:: c_uint ) -> libc:: ssize_t ;
4139
4240 if let Some ( fptr) = GETRANDOM . ptr ( ) {
43- let func: GetRandomFn = unsafe { core:: mem:: transmute ( fptr) } ;
44- return sys_fill_exact ( dest , |buf| unsafe { func ( buf . as_mut_ptr ( ) , buf . len ( ) , 0 ) } ) ;
41+ let func: GetRandomFn = core:: mem:: transmute ( fptr) ;
42+ return sys_fill_exact ( dst , len , |cdst , clen| func ( cdst , clen , 0 ) ) ;
4543 }
4644 }
4745 // Both FreeBSD and NetBSD will only return up to 256 bytes at a time, and
4846 // older NetBSD kernels will fail on longer buffers.
49- for chunk in dest. chunks_mut ( 256 ) {
50- sys_fill_exact ( chunk, kern_arnd) ?
51- }
52- Ok ( ( ) )
47+ raw_chunks ( dst, len, 256 , |cdst, clen| {
48+ sys_fill_exact ( cdst, clen, |cdst2, clen2| kern_arnd ( cdst2, clen2) )
49+ } )
5350}
0 commit comments