code wiki / _hdl_build / nx_epub_art_gate.nx
nx_epub_art_gate.nx source
↩ module page · 114 lines · 6995 B
1// nx_epub_art_gate.nx -- END-TO-END liar-kill gate for R-ART: proves nx_epub_book extracts the cover + embedded
2// image assets from an EPUB, writes them to the served slug dir BYTE-EXACT, and references them in book.json
3// (cover detected, is_cover flagged, distinct content). Self-contained + sovereign (fork/exec, NO shell): regen
4// the fixture + book.json, then assert book.json asset fields AND byte-exact round-trip of the extracted files.
5// Negative control: the two extracted assets are NOT byte-identical (real distinct extraction, no duplication).
6// expect_exit: 0
7import "nx_syscalls.nx"
8
9func ar_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func ar_n(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
11func ar_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12
13func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
14 let pid: i64 = sys_fork()
15 if pid == 0 {
16 let fd: i64 = sys_openat_wr(redir, 420)
17 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
18 let argv: *i64 = sys_mmap(64) as *i64
19 var n: i64 = 0
20 argv[0] = path as i64; n = 1
21 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
22 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
23 argv[n] = 0
24 let envp: *i64 = sys_mmap(16) as *i64
25 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
26 sys_execve(path, argv, envp)
27 sys_exit(127)
28 }
29 let st: *i64 = sys_mmap(16) as *i64
30 sys_wait4(pid, st, 0)
31 return (st[0] >> 8) & 0xff
32}
33
34func ar_find(hay: *u8, hl: i64, needle: *u8) -> i64 {
35 let nl: i64 = ar_slen(needle)
36 if nl == 0 { return 0-1 }
37 var i: i64 = 0
38 while i + nl <= hl {
39 var k: i64 = 0; var hit: i64 = 1
40 while k < nl { if hay[i+k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } }
41 if hit == 1 { return i }
42 i = i + 1
43 }
44 return 0-1
45}
46
47// read a whole file into a fresh mmap; sets lp[0]=len; returns ptr (0 on fail).
48func ar_read(path: *u8, lp: *i64) -> *u8 { lp[0] = 0; return sys_read_file(path, lp) }
49
50// 1 iff buf[0,n) equals the NUL-terminated literal lit exactly (length + bytes).
51func ar_eq_lit(buf: *u8, n: i64, lit: *u8) -> i64 {
52 if ar_slen(lit) != n { return 0 }
53 var i: i64 = 0
54 while i < n { if buf[i] != lit[i] { return 0 } i = i + 1 }
55 return 1
56}
57// 1 iff two byte ranges are identical (length + bytes).
58func ar_eq_bytes(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
59 if an != bn { return 0 }
60 var i: i64 = 0
61 while i < an { if a[i] != b[i] { return 0 } i = i + 1 }
62 return 1
63}
64
65func main() -> i64 {
66 ar_p("=== nx_epub_art_gate: end-to-end cover + image extraction (R-ART) ===\n" as *u8)
67 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
68 let SCRATCH: *u8 = "knowledge/status/epub_art_gate_scratch.log\x00" as *u8
69
70 spawn(RUNNER, "nx_epub_fixture_build\x00" as *u8, 0 as *u8, SCRATCH)
71 spawn(RUNNER, "nx_epub_book\x00" as *u8, 0 as *u8, SCRATCH)
72 let rc: i64 = spawn("_offc/nx_epub_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fixture.epub\x00" as *u8, "nishi_fixture_art\x00" as *u8, SCRATCH)
73 ar_p(" nx_epub_book on fixture exit=" as *u8); ar_n(rc); ar_p("\n" as *u8)
74
75 var pass: i64 = 0
76 var fail: i64 = 0
77
78 // ---- book.json asset references ----
79 let lp: *i64 = sys_mmap(8) as *i64
80 let bj: *u8 = ar_read("knowledge/staging/media/reader/nishi_fixture_art/book.json\x00" as *u8, lp)
81 if (bj as i64) == 0 { ar_p("ART-GATE verdict=RED reason=book.json-missing\n" as *u8); sys_exit(1); return 1 }
82 let bn: i64 = lp[0]
83 if ar_find(bj, bn, "\"cover\":\"cover.png\"" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL cover-field\n" as *u8) }
84 if ar_find(bj, bn, "\"nassets\":2" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL nassets=2\n" as *u8) }
85 if ar_find(bj, bn, "\"file\":\"cover.png\",\"bytes\":93,\"fp\":" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL cover-asset-entry\n" as *u8) }
86 if ar_find(bj, bn, "\"file\":\"fig1.png\"" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL fig-asset-entry\n" as *u8) }
87 if ar_find(bj, bn, "\"is_cover\":1" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL is_cover-flag\n" as *u8) }
88
89 // ---- byte-exact round-trip of the extracted files (content integrity = the real proof) ----
90 let lpc: *i64 = sys_mmap(8) as *i64
91 let cov: *u8 = ar_read("knowledge/staging/media/reader/nishi_fixture_art/cover.png\x00" as *u8, lpc)
92 let cn: i64 = lpc[0]
93 let lpf: *i64 = sys_mmap(8) as *i64
94 let fig: *u8 = ar_read("knowledge/staging/media/reader/nishi_fixture_art/fig1.png\x00" as *u8, lpf)
95 let fn: i64 = lpf[0]
96 ar_p(" extracted cover.png bytes=" as *u8); ar_n(cn); ar_p(" fig1.png bytes=" as *u8); ar_n(fn); ar_p("\n" as *u8)
97 if (cov as i64) != 0 { if ar_eq_lit(cov, cn, "NISHI-FIXTURE-COVER-IMAGE-BYTES-v1-DISTINCTIVE-PLACEHOLDER-FOR-CID-ROUNDTRIP-0123456789abcdef" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL cover-roundtrip\n" as *u8) } } else { fail=fail+1; ar_p(" FAIL cover-file-missing\n" as *u8) }
98 if (fig as i64) != 0 { if ar_eq_lit(fig, fn, "NISHI-FIXTURE-FIG1-INLINE-ILLUSTRATION-BYTES-v1-DISTINCTIVE-PLACEHOLDER-fedcba9876543210" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL fig-roundtrip\n" as *u8) } } else { fail=fail+1; ar_p(" FAIL fig-file-missing\n" as *u8) }
99
100 // ---- R-ART-2: the inline <img> position is preserved as a marker in the chapter text (ch2 = chap1.txt) ----
101 let lpm: *i64 = sys_mmap(8) as *i64
102 let ch1: *u8 = ar_read("knowledge/staging/media/reader/nishi_fixture_art/chap1.txt\x00" as *u8, lpm)
103 if (ch1 as i64) != 0 { if ar_find(ch1, lpm[0], "[[nximg:fig1.png]]" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL inline-img-marker\n" as *u8) } } else { fail=fail+1; ar_p(" FAIL chap1-missing\n" as *u8) }
104
105 // ---- NEG control: the two assets are NOT identical (distinct extraction, no duplication/fabrication) ----
106 if (cov as i64) != 0 { if (fig as i64) != 0 { if ar_eq_bytes(cov, cn, fig, fn) == 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL assets-identical(dup)\n" as *u8) } } }
107 // teeth: a bogus cover name must be ABSENT
108 if ar_find(bj, bn, "\"cover\":\"WRONG.png\"" as *u8) < 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL teeth\n" as *u8) }
109 if rc == 0 { pass=pass+1 } else { fail=fail+1; ar_p(" FAIL book-exit\n" as *u8) }
110
111 ar_p("EPUB-ART-GATE pass=" as *u8); ar_n(pass); ar_p(" fail=" as *u8); ar_n(fail)
112 if fail == 0 { ar_p(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
113 ar_p(" verdict=RED\n" as *u8); sys_exit(1); return 1
114}