2017-03-26 18:39:28 +08:00
|
|
|
pub const windows = @import("windows.zig");
|
|
|
|
pub const darwin = @import("darwin.zig");
|
|
|
|
pub const linux = @import("linux.zig");
|
|
|
|
pub const posix = switch(@compileVar("os")) {
|
|
|
|
Os.linux => linux,
|
|
|
|
Os.darwin, Os.macosx, Os.ios => darwin,
|
|
|
|
Os.windows => windows,
|
2016-09-12 12:01:06 +08:00
|
|
|
else => @compileError("Unsupported OS"),
|
|
|
|
};
|
2017-03-26 18:39:28 +08:00
|
|
|
|
2016-02-28 13:06:46 +08:00
|
|
|
const errno = @import("errno.zig");
|
2017-03-26 18:39:28 +08:00
|
|
|
const linking_libc = @import("../target.zig").linking_libc;
|
|
|
|
const c = @import("../c/index.zig");
|
2016-02-04 16:00:54 +08:00
|
|
|
|
2016-12-22 12:34:14 +08:00
|
|
|
error Unexpected;
|
2016-02-04 16:00:54 +08:00
|
|
|
|
2017-03-23 14:59:58 +08:00
|
|
|
/// Fills `buf` with random bytes. If linking against libc, this calls the
|
|
|
|
/// appropriate OS-specific library call. Otherwise it uses the zig standard
|
|
|
|
/// library implementation.
|
2016-08-18 11:11:04 +08:00
|
|
|
pub fn getRandomBytes(buf: []u8) -> %void {
|
2016-09-12 12:01:06 +08:00
|
|
|
while (true) {
|
2017-03-23 14:59:58 +08:00
|
|
|
const err = switch (@compileVar("os")) {
|
|
|
|
Os.linux => {
|
|
|
|
if (linking_libc) {
|
|
|
|
if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0
|
|
|
|
} else {
|
|
|
|
posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Os.darwin, Os.macosx, Os.ios => {
|
|
|
|
if (linking_libc) {
|
|
|
|
if (posix.getrandom(buf.ptr, buf.len) == -1) *c._errno() else 0
|
|
|
|
} else {
|
|
|
|
posix.getErrno(posix.getrandom(buf.ptr, buf.len))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Os.windows => {
|
|
|
|
var hCryptProv: windows.HCRYPTPROV = undefined;
|
|
|
|
if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) {
|
|
|
|
return error.Unexpected;
|
|
|
|
}
|
|
|
|
defer _ = windows.CryptReleaseContext(hCryptProv, 0);
|
|
|
|
|
|
|
|
if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) {
|
|
|
|
return error.Unexpected;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
else => @compileError("Unsupported OS"),
|
2016-09-12 12:01:06 +08:00
|
|
|
};
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
2017-03-26 16:58:48 +08:00
|
|
|
errno.EINVAL => unreachable,
|
|
|
|
errno.EFAULT => unreachable,
|
2016-09-12 12:01:06 +08:00
|
|
|
errno.EINTR => continue,
|
|
|
|
else => error.Unexpected,
|
2016-02-14 13:59:49 +08:00
|
|
|
}
|
2016-09-12 12:01:06 +08:00
|
|
|
}
|
|
|
|
return;
|
2016-02-04 16:00:54 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 07:42:56 +08:00
|
|
|
|
2017-03-23 14:59:58 +08:00
|
|
|
/// Raises a signal in the current kernel thread, ending its execution.
|
|
|
|
/// If linking against libc, this calls the abort() libc function. Otherwise
|
|
|
|
/// it uses the zig standard library implementation.
|
2017-03-26 16:58:48 +08:00
|
|
|
pub coldcc fn abort() -> noreturn {
|
2017-03-23 14:59:58 +08:00
|
|
|
if (linking_libc) {
|
|
|
|
c.abort();
|
|
|
|
}
|
2016-08-18 11:11:04 +08:00
|
|
|
switch (@compileVar("os")) {
|
2017-03-23 14:59:58 +08:00
|
|
|
Os.linux => {
|
|
|
|
_ = posix.raise(posix.SIGABRT);
|
|
|
|
_ = posix.raise(posix.SIGKILL);
|
2016-08-18 11:11:04 +08:00
|
|
|
while (true) {}
|
|
|
|
},
|
2017-03-23 14:59:58 +08:00
|
|
|
else => @compileError("Unsupported OS"),
|
2016-08-18 11:11:04 +08:00
|
|
|
}
|
2016-04-19 07:42:56 +08:00
|
|
|
}
|