code wiki / _hdl_build / nx_b64_cli.nx
nx_b64_cli.nx source
↩ module page · 36 lines · 1798 B
1// nx_b64_cli.nx -- base64 a file to stdout (and optionally to a file).
2// WHY THIS EXISTS: runtime/nx_base64.nx carries a complete b64_encode/b64_decode pair and NO main, so the
3// capability could not be INVOKED -- the same shape as rf_fetch_bank before nx_research_fetch_cli, and the
4// reason evidence work kept stalling on "the bytes are NAS-side and the third-party decoder is laptop-side".
5// A text-safe transport turns any binary artifact (a rendered PNG, an emitted wasm) into something an
6// external validator can be handed, which is exactly what an EXPERIENTIAL witness needs.
7// usage: nx_b64_cli <infile> [outfile] exit 0 ok, 1 usage, 2 unreadable
8// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_base64.nx"
11
12const BC_MAX_IN: i64 = 8388608
13const BC_MAX_OUT: i64 = 12582912
14
15func bc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
16
17func main(argc: i64, argv: *i64) -> i64 {
18 if argc < 2 { bc_puts("usage: nx_b64_cli <infile> [outfile]\n" as *u8); return 1 }
19 let path: *u8 = argv[1] as *u8
20 let lb: *i64 = sys_mmap(16) as *i64
21 let buf: *u8 = sys_read_file(path, lb)
22 if (buf as i64) == 0 { bc_puts("REJECT: cannot read input\n" as *u8); return 2 }
23 let n: i64 = lb[0]
24 if n <= 0 { bc_puts("REJECT: empty input\n" as *u8); return 2 }
25 if n > BC_MAX_IN { bc_puts("REJECT: input over cap\n" as *u8); return 2 }
26 let out: *u8 = sys_mmap(BC_MAX_OUT)
27 let en: i64 = b64_encode(buf, n, out)
28 sys_write(1, out, en)
29 sys_write(1, "\n" as *u8, 1)
30 if argc >= 3 {
31 let opath: *u8 = argv[2] as *u8
32 let fd: i64 = sys_openat_wr(opath, 0x1a4)
33 if fd >= 0 { sys_write(fd, out, en); sys_close(fd) }
34 }
35 return 0
36}