| author | |
| committer | |
| log | a11113097730693fb915114cec79df9cb4adcc93 |
| tree | 5b4e08a513bc3ec7916335617be3b319bfe0d5fa |
| parent | 9f3f9fb40f605de2f2bdcd0ce7a396e06b8b8c0d |
| parent | 6248ac535b9bfc7113101c1ff0f0bb1e41c394d4 |
| signature |
std: add CCRandomGenerateBytes macOs native api.2 files changed, 41 insertions(+), 2 deletions(-)
lib/std/c/darwin.zig+28| ... | ... | @@ -897,7 +897,35 @@ pub extern "c" fn pthread_attr_get_qos_class_np(attr: *pthread_attr_t, qos_class |
| 897 | 897 | pub extern "c" fn pthread_set_qos_class_self_np(qos_class: qos_class_t, relative_priority: c_int) c_int; |
| 898 | 898 | pub extern "c" fn pthread_get_qos_class_np(pthread: std.c.pthread_t, qos_class: *qos_class_t, relative_priority: *c_int) c_int; |
| 899 | 899 | |
| 900 | pub const CCryptorStatus = enum(i32) { | |
| 901 | /// Operation completed | |
| 902 | kCCSuccess = 0, | |
| 903 | /// Illegal parameter | |
| 904 | kCCParamError = -4300, | |
| 905 | /// Provided buffer too small | |
| 906 | kCCBufferTooSmall = -4301, | |
| 907 | /// Failed memory allocation | |
| 908 | kCCMemoryFailure = -4302, | |
| 909 | /// Size alignment issue | |
| 910 | kCCAlignmentError = -4303, | |
| 911 | /// Decoding issue | |
| 912 | kCCDecodeError = -4304, | |
| 913 | /// Call not implemented | |
| 914 | kCCUnimplemented = -4305, | |
| 915 | kCCOverflow = -4306, | |
| 916 | kCCRNGFailure = -4307, | |
| 917 | /// Unspecified error | |
| 918 | kCCUnspecifiedError = -4308, | |
| 919 | kCCCallSequenceError = -4309, | |
| 920 | kCCKeySizeError = -4310, | |
| 921 | /// Invalid key | |
| 922 | kCCInvalidKey = -4311, | |
| 923 | }; | |
| 924 | ||
| 925 | pub const CCRNGStatus = CCryptorStatus; | |
| 926 | ||
| 900 | 927 | pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; |
| 928 | pub extern "c" fn CCRandomGenerateBytes(bytes: ?*anyopaque, count: usize) CCRNGStatus; | |
| 901 | 929 | |
| 902 | 930 | // Grand Central Dispatch is exposed by libSystem. |
| 903 | 931 | pub extern "c" fn dispatch_release(object: *anyopaque) void; |
lib/std/os.zig+13-2| ... | ... | @@ -512,7 +512,18 @@ pub fn getrandom(buffer: []u8) GetRandomError!void { |
| 512 | 512 | return; |
| 513 | 513 | } |
| 514 | 514 | switch (builtin.os.tag) { |
| 515 | .netbsd, .openbsd, .macos, .ios, .tvos, .watchos => { | |
| 515 | .macos, .ios => { | |
| 516 | const rc = darwin.CCRandomGenerateBytes(buffer.ptr, buffer.len); | |
| 517 | if (rc != darwin.CCRNGStatus.kCCSuccess) { | |
| 518 | if (rc == darwin.CCRNGStatus.kCCParamError or rc == darwin.CCRNGStatus.kCCBufferTooSmall) { | |
| 519 | return error.InvalidHandle; | |
| 520 | } else { | |
| 521 | return error.SystemResources; | |
| 522 | } | |
| 523 | } | |
| 524 | return; | |
| 525 | }, | |
| 526 | .netbsd, .openbsd, .tvos, .watchos => { | |
| 516 | 527 | system.arc4random_buf(buffer.ptr, buffer.len); |
| 517 | 528 | return; |
| 518 | 529 | }, |
| ... | ... | @@ -991,7 +1002,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { |
| 991 | 1002 | if (have_pread_but_not_preadv) { |
| 992 | 1003 | // We could loop here; but proper usage of `preadv` must handle partial reads anyway. |
| 993 | 1004 | // So we simply read into the first vector only. |
| 994 | if (iov.len == 0) return @as(usize, 0); | |
| 1005 | if (iov.len == 0) return @intCast(usize, 0); | |
| 995 | 1006 | const first = iov[0]; |
| 996 | 1007 | return pread(fd, first.iov_base[0..first.iov_len], offset); |
| 997 | 1008 | } |