code wiki / _hdl_build / nx_nmz_comic_gate.nx

nx_nmz_comic_gate.nx source

↩ module page · 83 lines · 6010 B

1// nx_nmz_comic_gate.nx -- liar-kill gate for COMICS migrating into the omni .nmz (@kind=comic), end to end: 2// CBZ fixture -> nx_cbx_pages (the existing SC43 extractor) -> reader/<slug>/ (cbx.json + page<NNN>.jpg) 3// -> nx_nmz_pack <slug> comic -> .nmz. 4// Asserts: @kind=comic; the cbz manifest round-trips; and the PAGE IMAGE bytes (which contain embedded 0x00) come 5// back BYTE-EXACT out of the binary-safe canon (string-only canon_encode would have truncated at the first NUL) + 6// the substrate UXF_ARCHIVE CID. GREEN = a real comic (binary media) is now a first-class .nmz, proving the omni 7// container holds more than books. expect_exit: 0 license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_canon_bin.nx" 10import "nx_uxf_cid.nx" 11 12func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func gnum(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 } 14func gstreq(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 ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 16 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 17 if nl == 0 { return 0 } 18 var i: i64 = 0 19 while i + nl <= hl { var k: i64=0; var hit: i64=1; while k<nl { if hay[i+k]!=needle[k]{hit=0;k=nl}else{k=k+1} } if hit==1 {return 1} i=i+1 } 20 return 0 21} 22 23func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 24 let pid: i64 = sys_fork() 25 if pid == 0 { 26 let fd: i64 = sys_openat_wr(redir, 420) 27 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 28 let argv: *i64 = sys_mmap(64) as *i64 29 var n: i64 = 0 30 argv[0] = path as i64; n = 1 31 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 32 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 33 argv[n] = 0 34 let envp: *i64 = sys_mmap(16) as *i64 35 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 36 sys_execve(path, argv, envp) 37 sys_exit(127) 38 } 39 let st: *i64 = sys_mmap(16) as *i64 40 sys_wait4(pid, st, 0) 41 return (st[0] >> 8) & 0xff 42} 43 44func main() -> i64 { 45 gp("=== nx_nmz_comic_gate: CBZ comic -> .nmz @kind=comic (binary page images via the binary-safe canon) ===\n" as *u8) 46 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 47 let SCRATCH: *u8 = "knowledge/status/nmz_comic_gate_scratch.log\x00" as *u8 48 49 spawn(RUNNER, "nx_cbz_fixture\x00" as *u8, 0 as *u8, SCRATCH) // build the CBZ 50 spawn(RUNNER, "nx_cbx_pages\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin 51 let rc_x: i64 = spawn("_offc/nx_cbx_pages.elf\x00" as *u8, "knowledge/fixtures/nishi_comic.cbz\x00" as *u8, "nishi_comic\x00" as *u8, SCRATCH) 52 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin (w/ cbx.json fallback) 53 let rc_p: i64 = spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "nishi_comic\x00" as *u8, "comic\x00" as *u8, SCRATCH) 54 55 let lm: *i64 = sys_mmap(8) as *i64; lm[0]=0 56 let m: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_comic.nmz\x00" as *u8, lm) 57 let n: i64 = lm[0] 58 if (m as i64)==0 { gp("NMZ-COMIC-GATE verdict=RED reason=comic-nmz-missing rc_x=" as *u8); gnum(rc_x); gp(" rc_p=" as *u8); gnum(rc_p); gp("\n" as *u8); sys_exit(1); return 1 } 59 60 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(m, n, "@kind\x00" as *u8, kindb, 64) 61 let cbxb: *u8 = sys_mmap(8192); let gc: i64 = canon_get_bin(m, n, "cbx.json\x00" as *u8, cbxb, 8192) 62 let pageb: *u8 = sys_mmap(4096); let gpg: i64 = canon_get_bin(m, n, "page000.jpg\x00" as *u8, pageb, 4096) 63 let cid: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m, n, cid) 64 65 gp(" nmz bytes=" as *u8); gnum(n); gp(" @kind=" as *u8); if gk>0 { gp(kindb) } 66 gp(" page000.jpg_len=" as *u8); gnum(gpg) 67 if gpg >= 3 { gp(" page[0..2]=" as *u8); gnum(pageb[0] as i64); gp("," as *u8); gnum(pageb[1] as i64); gp("," as *u8); gnum(pageb[2] as i64) } 68 gp(" cid=" as *u8); gp(cid); gp("\n" as *u8) 69 70 var pass: i64 = 0; var fail: i64 = 0 71 if rc_x == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cbx_pages-nonzero\n" as *u8) } 72 if rc_p == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz_pack-nonzero\n" as *u8) } 73 if gk > 0 { if gstreq(kindb, "comic" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kind!=comic\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-@kind\n" as *u8) } 74 if gc > 0 { if ghas(cbxb, gc, "\"format\":\"cbz\"" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cbx-manifest-not-packed\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-cbx.json-entry\n" as *u8) } 75 // THE binary-safety proof: the page image (8 bytes incl NULs) round-trips byte-exact 76 if gpg == 8 { pass=pass+1 } else { fail=fail+1; gp(" FAIL page-image-len!=8 (binary truncated?)\n" as *u8) } 77 if gpg >= 5 { if pageb[0]==(0xff as u8) { if pageb[1]==(0xd8 as u8) { if pageb[2]==(0x00 as u8) { if pageb[4]==(0x00 as u8) { pass=pass+1 } else { fail=fail+1; gp(" FAIL page-NUL-byte-not-preserved\n" as *u8) } } else { fail=fail+1; gp(" FAIL page-byte2\n" as *u8) } } else { fail=fail+1; gp(" FAIL page-not-jpeg-SOI\n" as *u8) } } else { fail=fail+1; gp(" FAIL page-byte0\n" as *u8) } } else { fail=fail+1; gp(" FAIL page-too-short\n" as *u8) } 78 if uxf_codec_of_cid(cid) == UXF_ARCHIVE { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-codec\n" as *u8) } 79 80 gp("NMZ-COMIC-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 81 if fail == 0 { gp(" verdict=GREEN (CBZ comic -> .nmz @kind=comic; page image bytes incl NULs round-trip byte-exact; UXF_ARCHIVE CID)\n" as *u8); sys_exit(0); return 0 } 82 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 83}