@@ -21,6 +21,13 @@ use rt::io::{file, Open, Read};
21
21
22
22
#[ cfg( windows) ]
23
23
use cast;
24
+ #[ cfg( windows) ]
25
+ use libc:: { c_long, DWORD , BYTE } ;
26
+ #[ cfg( windows) ]
27
+ type HCRYPTPROV = c_long ;
28
+ // the extern functions imported from the runtime on Windows are
29
+ // implemented so that they either succeed or abort(), so we can just
30
+ // assume they work when we call them.
24
31
25
32
/// A random number generator that retrieves randomness straight from
26
33
/// the operating system. On Unix-like systems this reads from
@@ -38,7 +45,7 @@ pub struct OSRng {
38
45
/// This does not block.
39
46
#[ cfg( windows) ]
40
47
pub struct OSRng {
41
- priv hcryptprov : raw :: HCRYPTPROV
48
+ priv hcryptprov : HCRYPTPROV
42
49
}
43
50
44
51
impl OSRng {
@@ -53,10 +60,11 @@ impl OSRng {
53
60
54
61
/// Create a new `OSRng`.
55
62
#[ cfg( windows) ]
56
- #[ fixed_stack_segment] #[ inline( never) ]
57
63
pub fn new ( ) -> OSRng {
64
+ externfn ! ( fn rust_win32_rand_acquire( phProv: * mut HCRYPTPROV ) )
65
+
58
66
let mut hcp = 0 ;
59
- unsafe { raw :: rust_win32_rand_acquire ( & mut hcp) } ;
67
+ unsafe { rust_win32_rand_acquire ( & mut hcp) } ;
60
68
61
69
OSRng { hcryptprov : hcp }
62
70
}
@@ -87,12 +95,11 @@ impl Rng for OSRng {
87
95
self . fill_bytes ( v) ;
88
96
unsafe { cast:: transmute ( v) }
89
97
}
90
- #[ fixed_stack_segment] #[ inline( never) ]
91
98
fn fill_bytes ( & mut self , v : & mut [ u8 ] ) {
92
- use libc :: DWORD ;
99
+ externfn ! ( fn rust_win32_rand_gen ( hProv : HCRYPTPROV , dwLen : DWORD , pbBuffer : * mut BYTE ) )
93
100
94
101
do v. as_mut_buf |ptr, len| {
95
- unsafe { raw :: rust_win32_rand_gen ( self . hcryptprov , len as DWORD , ptr) }
102
+ unsafe { rust_win32_rand_gen ( self . hcryptprov , len as DWORD , ptr) }
96
103
}
97
104
}
98
105
}
@@ -105,27 +112,14 @@ impl Drop for OSRng {
105
112
}
106
113
107
114
#[ cfg( windows) ]
108
- #[ fixed_stack_segment] #[ inline( never) ]
109
115
fn drop ( & mut self ) {
110
- unsafe { raw:: rust_win32_rand_release ( self . hcryptprov ) }
111
- }
112
- }
116
+ externfn ! ( fn rust_win32_rand_release( hProv: HCRYPTPROV ) )
113
117
114
- #[ cfg( windows) ]
115
- mod raw {
116
- use libc:: { c_long, DWORD , BYTE } ;
117
-
118
- pub type HCRYPTPROV = c_long ;
119
-
120
- // these functions are implemented so that they either succeed or
121
- // abort(), so we can just assume they work when we call them.
122
- extern {
123
- pub fn rust_win32_rand_acquire ( phProv : * mut HCRYPTPROV ) ;
124
- pub fn rust_win32_rand_gen ( hProv : HCRYPTPROV , dwLen : DWORD , pbBuffer : * mut BYTE ) ;
125
- pub fn rust_win32_rand_release ( hProv : HCRYPTPROV ) ;
118
+ unsafe { rust_win32_rand_release ( self . hcryptprov ) }
126
119
}
127
120
}
128
121
122
+
129
123
#[ cfg( test) ]
130
124
mod test {
131
125
use super :: * ;
0 commit comments