@@ -99,47 +99,46 @@ pub const GetRandomError = OpenError;
99
99
/// When linking against libc, this calls the
100
100
/// appropriate OS-specific library call. Otherwise it uses the zig standard
101
101
/// library implementation.
102
- pub fn getrandom (buf : []u8 ) GetRandomError ! void {
102
+ pub fn getrandom (buffer : []u8 ) GetRandomError ! void {
103
103
if (windows .is_the_target ) {
104
- return windows .RtlGenRandom (buf );
104
+ return windows .RtlGenRandom (buffer );
105
105
}
106
- if (linux .is_the_target ) {
107
- while (true ) {
108
- const err = if (std .c .versionCheck (builtin.Version { .major = 2 , .minor = 25 , .patch = 0 }).ok ) blk : {
109
- break :blk errno (std .c .getrandom (buf .ptr , buf .len , 0 ));
106
+ if (linux .is_the_target or freebsd .is_the_target ) {
107
+ var buf = buffer ;
108
+ const use_c = ! linux .is_the_target or
109
+ std .c .versionCheck (builtin.Version { .major = 2 , .minor = 25 , .patch = 0 }).ok ;
110
+
111
+ while (buf .len != 0 ) {
112
+ var err : u16 = undefined ;
113
+
114
+ const num_read = if (use_c ) blk : {
115
+ const rc = std .c .getrandom (buf .ptr , buf .len , 0 );
116
+ err = std .c .getErrno (rc );
117
+ break :blk @bitCast (usize , rc );
110
118
} else blk : {
111
- break :blk linux .getErrno (linux .getrandom (buf .ptr , buf .len , 0 ));
119
+ const rc = linux .getrandom (buf .ptr , buf .len , 0 );
120
+ err = linux .getErrno (rc );
121
+ break :blk rc ;
112
122
};
113
- switch (err ) {
114
- 0 = > return ,
115
- EINVAL = > unreachable ,
116
- EFAULT = > unreachable ,
117
- EINTR = > continue ,
118
- ENOSYS = > return getRandomBytesDevURandom (buf ),
119
- else = > return unexpectedErrno (err ),
120
- }
121
- }
122
- }
123
- if (freebsd .is_the_target ) {
124
- while (true ) {
125
- const err = std .c .getErrno (std .c .getrandom (buf .ptr , buf .len , 0 ));
126
123
127
124
switch (err ) {
128
- 0 = > return ,
125
+ 0 = > buf = buf [ num_read .. ] ,
129
126
EINVAL = > unreachable ,
130
127
EFAULT = > unreachable ,
131
128
EINTR = > continue ,
129
+ ENOSYS = > return getRandomBytesDevURandom (buf ),
132
130
else = > return unexpectedErrno (err ),
133
131
}
134
132
}
133
+ return ;
135
134
}
136
135
if (wasi .is_the_target ) {
137
- switch (wasi .random_get (buf .ptr , buf .len )) {
136
+ switch (wasi .random_get (buffer .ptr , buffer .len )) {
138
137
0 = > return ,
139
138
else = > | err | return unexpectedErrno (err ),
140
139
}
141
140
}
142
- return getRandomBytesDevURandom (buf );
141
+ return getRandomBytesDevURandom (buffer );
143
142
}
144
143
145
144
fn getRandomBytesDevURandom (buf : []u8 ) ! void {
0 commit comments