From c67abf5a8e09afed8df82d52362d0277da570ac5 Mon Sep 17 00:00:00 2001 From: Toni Date: Thu, 12 Mar 2026 16:06:22 +0100 Subject: [PATCH] some new std functions --- src/std/crypto.zr | 15 +++++-------- src/std/std.zr | 55 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/std/crypto.zr b/src/std/crypto.zr index c15bd39..b462c4d 100644 --- a/src/std/crypto.zr +++ b/src/std/crypto.zr @@ -84,10 +84,8 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle mem.write64(h, mem.read64(h) ^ (0x01010000 ^ ((keylen << 8) ^ outlen))) if keylen > 0 - for i in 0..keylen - block[i] = key[i] - for i in (keylen)..128 - block[i] = 0 + mem.zero(block, 128) + mem.copy(key, block, keylen) c = 128 else c = 0 @@ -211,8 +209,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void let nonce12: ptr = mem.alloc(12) mem.zero(nonce12, 12) - for i in 0..8 - nonce12[i + 4] = nonce[16 + i] + mem.copy(nonce + 16, nonce12 + 4, 8) let blocknum = 0 let remaining: i64 = len @@ -223,8 +220,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void let take = 64 if remaining < 64 take = remaining - for i in 0..take - out[len - remaining + i] = block[i] + mem.copy(block, out + len - remaining, take) remaining = remaining - take blocknum = blocknum + 1 mem.zero_and_free(block, 64) @@ -351,8 +347,7 @@ func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr mem.write64(magic + 8, 1) // copy and clamp scalar - for i in 0..32 - clamped[i] = scalar[i] + mem.copy(scalar, clamped, 32) clamped[0] = clamped[0] & 0xf8 clamped[31] = (clamped[31] & 0x7f) | 0x40 diff --git a/src/std/std.zr b/src/std/std.zr index 466ca93..5993c62 100644 --- a/src/std/std.zr +++ b/src/std/std.zr @@ -148,8 +148,7 @@ func mem.realloc[x: ptr, new_size: i64] : ptr if !new_ptr return 0 - for i in 0..blk->size - new_ptr[i] = x[i] + mem.copy(x, new_ptr, blk->size) mem.free(x) return new_ptr @@ -162,6 +161,10 @@ func mem.zero[x: ptr, size: i64] : void for i in 0..size x[i] = 0 +func mem.copy[src: ptr, dst: ptr, n: i64] : void + for i in 0..n + dst[i] = src[i] + func mem.read8[x: ptr] : u8 return x[0] @@ -196,6 +199,12 @@ func io.println[x: str] : void func io.print_char[x: u8] : void io.print_sized(^x, 1) +func io.print_bool[x: bool] : void + if x + io.print("true") + else + io.print("false") + func io.print_i64[x: i64] : void let s: str = str.from_i64(x) io.print(s) @@ -226,6 +235,13 @@ func io.read_line[]: str buffer[n] = 0 return buffer +func io.file_exists[path: str] : bool + let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) + if fd >= 0 + _builtin_syscall(SYS_close, fd) + return true + return false + func io.read_file[path: str] : str let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) if fd <= 0 @@ -266,6 +282,14 @@ func io.write_file[path: str, content: str] : void _builtin_syscall(SYS_write, fd, content, str.len(content)) _builtin_syscall(SYS_close, fd) +func io.write_binary_file[path: str, content: ptr, size: i64] : void + let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644) + if fd < 0 + panic("failed to open file") // TODO + + _builtin_syscall(SYS_write, fd, content, size) + _builtin_syscall(SYS_close, fd) + func str.len[s: str] : i64 let i = 0 while s[i] @@ -275,8 +299,7 @@ func str.len[s: str] : i64 func str.copy[s: str] : str let size: i64 = str.len(s) + 1 let dup: str = mem.alloc(size) - for i in 0..size - dup[i] = s[i] + mem.copy(s, dup, size) return dup func str.equal[a: str, b: str] : bool @@ -312,13 +335,14 @@ func str.concat[a: str, b: str] : str let a_len: i64 = str.len(a) let b_len: i64 = str.len(b) let out: str = mem.alloc(a_len + b_len + 1) - for i in 0..a_len - out[i] = a[i] - for i in 0..b_len - out[a_len + i] = b[i] + mem.copy(a, out, a_len) + mem.copy(b, out + a_len, b_len) out[a_len + b_len] = 0 return out +func str.contains[haystack: str, needle: str] : bool + return str.find(haystack, needle) != -1 + func str.find[haystack: str, needle: str] : i64 let haystack_len: i64 = str.len(haystack) let needle_len: i64 = str.len(needle) @@ -341,8 +365,7 @@ func str.substr[s: str, start: i64, length: i64] : str panic("str.substr out of bounds") let out: str = mem.alloc(length + 1) - for i in 0..length - out[i] = s[start + i] + mem.copy(s + start, out, length) out[length] = 0 return out @@ -690,6 +713,17 @@ func alg.reduce[arr: Array, fn: ptr, acc: any] : any func os.exit[code: i64] : void _builtin_syscall(SYS_exit, code) +func os.getpid[] : i64 + return _builtin_syscall(SYS_getpid) + +func os.sleep[ms: i64] : void + // TODO: we really shouldnt need a heap allocation to sleep + let req: os.Timeval = new os.Timeval + req->tv_sec = ms / 1000 + req->tv_usec = (ms % 1000) * 1000000 + _builtin_syscall(SYS_nanosleep, req, 0) + mem.free(req) + func os.urandom[n: i64]: ptr let buffer: ptr = mem.alloc(n) let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0) @@ -708,6 +742,7 @@ struct os.Timeval tv_usec: i64 func os.time[] : i64 + // TODO: we really shouldnt need a heap allocation to check the time let tv: os.Timeval = new os.Timeval _builtin_syscall(SYS_gettimeofday, tv, 0) let out: i64 = tv->tv_sec * 1000 + tv->tv_usec / 1000