code wiki / (root) / nx_compare_serve.nx

nx_compare_serve.nx source

↩ module page · 51 lines · 3104 B

1// nx_compare_serve.nx -- fork-exec backend for the `nishi_compare` MCP tool. v2 SINGLE-SOURCE: serves the EXACT 2// file the public REST endpoint serves (sites/nishifamily/compare/... relative to the tools-API CWD = nishihost) 3// -- there is NO second copy to drift (operator 2026-07-09: no frozen snapshots). Domain map: "index" -> the 4// comparison index api.json, "openapi" -> the OpenAPI 3.1 contract, else -> <domain>/api.json. No re-measurement 5// here (measurement happens in nx_compare_regen against the buildroot source tree, gate-checked); this serves the 6// latest gate-GREEN generation. SECURITY: the caller-supplied domain is sanitized to [a-z]{1,40} ONLY -> no path 7// traversal / injection. Absent file -> structured JSON error envelope. license_tier: ORIGINAL expect_exit:0 8import "nx_syscalls.nx" 9const K_MAGIC_1048576: i64 = 1048576 10 11func w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 12func wc(fd: i64, code: i64) -> i64 { let t: *u8 = sys_mmap(2); t[0] = code as u8; sys_write(fd, t, 1); return 0 } 13func scopy(dst: *u8, doff: i64, src: *u8) -> i64 { var i: i64 = 0; while src[i] != (0 as u8) { dst[doff+i] = src[i]; i = i + 1 } return doff + i } 14func streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } 15func san_ok(s: *u8) -> i64 { 16 var i: i64 = 0 17 while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c < 97 { return 0 } if c > 122 { return 0 } i = i + 1; if i > 40 { return 0 } } 18 if i == 0 { return 0 } 19 return 1 20} 21func err_json(code: *u8, dom: *u8) -> i64 { 22 wc(1, 123); wc(1, 34); w(1, "v" as *u8); wc(1, 34); wc(1, 58); wc(1, 49); wc(1, 44) 23 wc(1, 34); w(1, "error" as *u8); wc(1, 34); wc(1, 58); wc(1, 123) 24 wc(1, 34); w(1, "code" as *u8); wc(1, 34); wc(1, 58); wc(1, 34); w(1, code); wc(1, 34); wc(1, 44) 25 wc(1, 34); w(1, "domain" as *u8); wc(1, 34); wc(1, 58); wc(1, 34); w(1, dom); wc(1, 34) 26 wc(1, 125); wc(1, 125); wc(1, 10) 27 return 0 28} 29func main(argc: i64, argv: *i64) -> i64 { 30 var dom: *u8 = "index" as *u8 31 if argc >= 2 { dom = argv[1] as *u8 } 32 if san_ok(dom) == 0 { err_json("bad_domain" as *u8, "invalid" as *u8); sys_exit(0); return 0 } 33 let path: *u8 = sys_mmap(512) 34 var o: i64 = scopy(path, 0, "sites/nishifamily/compare/" as *u8) 35 if streq(dom, "index" as *u8) == 1 { o = scopy(path, o, "api.json" as *u8) } else { 36 if streq(dom, "openapi" as *u8) == 1 { o = scopy(path, o, "openapi.json" as *u8) } else { 37 o = scopy(path, o, dom) 38 o = scopy(path, o, "/api.json" as *u8) 39 } 40 } 41 path[o] = 0 as u8 42 let fd: i64 = sys_openat_rd(path) 43 if fd < 0 { err_json("not_found" as *u8, dom); sys_exit(0); return 0 } 44 let cap: i64 = K_MAGIC_1048576 45 let buf: *u8 = sys_mmap(cap) 46 var tot: i64 = 0 47 while tot < cap { let r: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot); if r <= 0 { break } tot = tot + r } 48 sys_close(fd) 49 sys_write(1, buf, tot) 50 sys_exit(0); return 0 51}