@@ -207,120 +207,3 @@ fn test_multithreading() {
207207 tx. send ( ( ) ) . unwrap ( ) ;
208208 }
209209}
210-
211- #[ cfg( getrandom_backend = "custom" ) ]
212- mod custom {
213- use getrandom:: Error ;
214-
215- struct Xoshiro128PlusPlus {
216- s : [ u32 ; 4 ] ,
217- }
218-
219- impl Xoshiro128PlusPlus {
220- fn new ( mut seed : u64 ) -> Self {
221- const PHI : u64 = 0x9e3779b97f4a7c15 ;
222- let mut s = [ 0u32 ; 4 ] ;
223- for val in s. iter_mut ( ) {
224- seed = seed. wrapping_add ( PHI ) ;
225- let mut z = seed;
226- z = ( z ^ ( z >> 30 ) ) . wrapping_mul ( 0xbf58476d1ce4e5b9 ) ;
227- z = ( z ^ ( z >> 27 ) ) . wrapping_mul ( 0x94d049bb133111eb ) ;
228- z = z ^ ( z >> 31 ) ;
229- * val = z as u32 ;
230- }
231- Self { s }
232- }
233-
234- fn next_u32 ( & mut self ) -> u32 {
235- let res = self . s [ 0 ]
236- . wrapping_add ( self . s [ 3 ] )
237- . rotate_left ( 7 )
238- . wrapping_add ( self . s [ 0 ] ) ;
239-
240- let t = self . s [ 1 ] << 9 ;
241-
242- self . s [ 2 ] ^= self . s [ 0 ] ;
243- self . s [ 3 ] ^= self . s [ 1 ] ;
244- self . s [ 1 ] ^= self . s [ 2 ] ;
245- self . s [ 0 ] ^= self . s [ 3 ] ;
246-
247- self . s [ 2 ] ^= t;
248-
249- self . s [ 3 ] = self . s [ 3 ] . rotate_left ( 11 ) ;
250-
251- res
252- }
253- }
254-
255- // This implementation uses current timestamp as a PRNG seed.
256- //
257- // WARNING: this custom implementation is for testing purposes ONLY!
258-
259- #[ unsafe( no_mangle) ]
260- unsafe extern "Rust" fn __getrandom_v03_custom ( dest : * mut u8 , len : usize ) -> Result < ( ) , Error > {
261- use std:: time:: { SystemTime , UNIX_EPOCH } ;
262-
263- assert_ne ! ( len, 0 ) ;
264-
265- if len == 142 {
266- return Err ( Error :: new_custom ( 142 ) ) ;
267- }
268-
269- let dest_u32 = dest. cast :: < u32 > ( ) ;
270- let ts = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) ;
271- let mut rng = Xoshiro128PlusPlus :: new ( ts. as_nanos ( ) as u64 ) ;
272- for i in 0 ..len / 4 {
273- let val = rng. next_u32 ( ) ;
274- unsafe { core:: ptr:: write_unaligned ( dest_u32. add ( i) , val) } ;
275- }
276- if len % 4 != 0 {
277- let start = 4 * ( len / 4 ) ;
278- for i in start..len {
279- let val = rng. next_u32 ( ) ;
280- unsafe { core:: ptr:: write_unaligned ( dest. add ( i) , val as u8 ) } ;
281- }
282- }
283- Ok ( ( ) )
284- }
285-
286- // Test that enabling the custom feature indeed uses the custom implementation
287- #[ test]
288- fn test_custom ( ) {
289- let mut buf = [ 0u8 ; 142 ] ;
290- let res = getrandom:: fill ( & mut buf) ;
291- assert ! ( res. is_err( ) ) ;
292- }
293- }
294-
295- #[ cfg( getrandom_backend = "extern_impl" ) ]
296- mod extern_item_impls {
297- use core:: mem:: MaybeUninit ;
298- use getrandom:: Error ;
299-
300- // This implementation for fill_uninit will always fail.
301- //
302- // WARNING: this custom implementation is for testing purposes ONLY!
303-
304- #[ getrandom:: implementation:: fill_uninit]
305- fn my_fill_uninit_implementation ( _dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
306- Err ( Error :: new_custom ( 4 ) )
307- }
308-
309- // This implementation returns a fixed value to demonstrate overriding defaults.
310- //
311- // WARNING: this custom implementation is for testing purposes ONLY!
312-
313- #[ getrandom:: implementation:: u32]
314- fn my_u32_implementation ( ) -> Result < u32 , Error > {
315- // Chosen by fair dice roll
316- Ok ( 4 )
317- }
318-
319- // Test that enabling the custom feature indeed uses the custom implementation
320- #[ test]
321- fn test_extern_item_impls ( ) {
322- let mut buf = [ 0u8 ; 123 ] ;
323- assert_eq ! ( getrandom:: fill( & mut buf) , Err ( Error :: new_custom( 4 ) ) ) ;
324- assert_eq ! ( getrandom:: u32 ( ) , Ok ( 4 ) ) ;
325- }
326- }
0 commit comments