code wiki / _hdl_build / nx_fb2_book_gate.nx
nx_fb2_book_gate.nx source
↩ module page · 112 lines · 7849 B
1// nx_fb2_book_gate.nx -- liar-kill gate for FB2 (FictionBook 2.0) liberation, end to end:
2// nx_fb2_fixture -> nishi_fb2_fixture.fb2 -> nx_fb2_book -> reader/<slug>/ (book.json + chap0.txt)
3// -> nx_nmz_pack <slug> book -> .nmz @kind=book.
4// Asserts title+author come from <description>, the body prose (incl the <emphasis>White Rabbit</emphasis> inline
5// tag, stripped) is extracted, the section heading survives, the .nmz carries @kind=book + the entries round-trip,
6// and -- the liar-kill -- the author "Carroll" does NOT leak into the body chapter (proves <body>-only extraction,
7// not a whole-file dump). FB2 = the Nishi equivalent of Calibre's FB2 input plugin. expect_exit: 0 tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_canon_bin.nx"
10import "nx_uxf_cid.nx"
11import "nx_gate_verdict.nx"
12
13func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func 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 }
15func 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 }
16func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 {
17 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
18 if nl == 0 { return 0 }
19 var i: i64 = 0
20 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 }
21 return 0
22}
23
24func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
25 let pid: i64 = sys_fork()
26 if pid == 0 {
27 let fd: i64 = sys_openat_wr(redir, 420)
28 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
29 let argv: *i64 = sys_mmap(64) as *i64
30 var n: i64 = 0
31 argv[0] = path as i64; n = 1
32 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
33 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
34 argv[n] = 0
35 let envp: *i64 = sys_mmap(16) as *i64
36 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
37 sys_execve(path, argv, envp)
38 sys_exit(127)
39 }
40 let st: *i64 = sys_mmap(16) as *i64
41 sys_wait4(pid, st, 0)
42 return (st[0] >> 8) & 0xff
43}
44
45func main() -> i64 {
46 gp("=== nx_fb2_book_gate: FB2 (FictionBook) -> reader -> .nmz @kind=book (Nishi equiv of Calibre FB2 input) ===\n" as *u8)
47 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
48 let SCRATCH: *u8 = "knowledge/status/fb2_gate_scratch.log\x00" as *u8
49
50 spawn(RUNNER, "nx_fb2_fixture\x00" as *u8, 0 as *u8, SCRATCH) // write the .fb2
51 spawn(RUNNER, "nx_fb2_book\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin
52 let rc_d: i64 = spawn("_offc/nx_fb2_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fb2_fixture.fb2\x00" as *u8, "nishi_fb2_fixture\x00" as *u8, SCRATCH)
53 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin
54 let rc_p: i64 = spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "nishi_fb2_fixture\x00" as *u8, "book\x00" as *u8, SCRATCH)
55
56 // --- read the decoded reader/<slug>/ artifacts ---
57 let lb: *i64 = sys_mmap(8) as *i64; lb[0]=0
58 let bj: *u8 = sys_read_file("knowledge/staging/media/reader/nishi_fb2_fixture/book.json\x00" as *u8, lb)
59 let bn: i64 = lb[0]
60 let lc: *i64 = sys_mmap(8) as *i64; lc[0]=0
61 let cj: *u8 = sys_read_file("knowledge/staging/media/reader/nishi_fb2_fixture/chap0.txt\x00" as *u8, lc)
62 let cn: i64 = lc[0]
63
64 // --- read the packed .nmz ---
65 let lm: *i64 = sys_mmap(8) as *i64; lm[0]=0
66 let m: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_fb2_fixture.nmz\x00" as *u8, lm)
67 let n: i64 = lm[0]
68 let kindb: *u8 = sys_mmap(64); var gk: i64 = 0-1
69 let nbjb: *u8 = sys_mmap(8192); var gb: i64 = 0-1
70 let ncjb: *u8 = sys_mmap(8192); var gc: i64 = 0-1
71 let cid: *u8 = sys_mmap(96); cid[0]=0 as u8
72 if (m as i64)!=0 {
73 gk = canon_get_bin(m, n, "@kind\x00" as *u8, kindb, 64)
74 gb = canon_get_bin(m, n, "book.json\x00" as *u8, nbjb, 8192)
75 gc = canon_get_bin(m, n, "chap0.txt\x00" as *u8, ncjb, 8192)
76 uxf_cid_profiled(UXF_ARCHIVE, m, n, cid)
77 }
78
79 gp(" rc_decode=" as *u8); gnum(rc_d); gp(" rc_pack=" as *u8); gnum(rc_p)
80 gp(" book.json=" as *u8); gnum(bn); gp("B chap0.txt=" as *u8); gnum(cn); gp("B nmz=" as *u8); gnum(n); gp("B\n" as *u8)
81 gp(" @kind=" as *u8); if gk>0 { gp(kindb) } gp(" cid=" as *u8); gp(cid); gp("\n" as *u8)
82
83 var pass: i64 = 0; var fail: i64 = 0
84 if rc_d == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL fb2_book-nonzero\n" as *u8) }
85 if rc_p == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz_pack-nonzero\n" as *u8) }
86 // book.json: title + author from <description>, format tagged fb2
87 if (bj as i64)!=0 { if ghas(bj, bn, "Alice (Nishi FB2 Fixture)" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL title-missing\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-book.json\n" as *u8) }
88 if (bj as i64)!=0 { if ghas(bj, bn, "Lewis Carroll" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL author-missing\n" as *u8) } }
89 if (bj as i64)!=0 { if ghas(bj, bn, "\"format\":\"fb2\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL format!=fb2\n" as *u8) } }
90 // chap0.txt: prose extracted (incl inline <emphasis> stripped to "White Rabbit") + section heading
91 if (cj as i64)!=0 { if ghas(cj, cn, "Alice was beginning" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL prose-missing\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-chap0\n" as *u8) }
92 if (cj as i64)!=0 { if ghas(cj, cn, "White Rabbit" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL emphasis-not-stripped\n" as *u8) } }
93 if (cj as i64)!=0 { if ghas(cj, cn, "Down the Rabbit-Hole" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL section-heading-missing\n" as *u8) } }
94 // LIAR-KILL: the <description> author must NOT bleed into the body chapter (proves <body>-only, not whole-file)
95 if (cj as i64)!=0 { if ghas(cj, cn, "Carroll" as *u8)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL description-leaked-into-body\n" as *u8) } }
96 // .nmz: @kind=book, entries round-trip, substrate CID
97 if gk > 0 { if gstreq(kindb, "book" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kind!=book\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-@kind\n" as *u8) }
98 if gb > 0 { if ghas(nbjb, gb, "\"format\":\"fb2\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz-book.json-not-fb2\n" as *u8) } } else { fail=fail+1; gp(" FAIL nmz-no-book.json\n" as *u8) }
99 if gc > 0 { if ghas(ncjb, gc, "Alice was beginning" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz-chap0-prose-missing\n" as *u8) } } else { fail=fail+1; gp(" FAIL nmz-no-chap0\n" as *u8) }
100 if uxf_codec_of_cid(cid) == UXF_ARCHIVE { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-codec\n" as *u8) }
101
102 gp("FB2-BOOK-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
103 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
104 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
105 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
106 let ctr__dry: *i64 = gv_ctr()
107 ctr__dry[0] = pass
108 ctr__dry[1] = pass + fail
109 let rc__dry: i64 = gv_verdict("FB2-BOOK-GATE" as *u8, ctr__dry, "FB2 FictionBook -> reader -> .nmz @kind=book; <description> title/author + <body> prose; author does NOT leak into body)" as *u8)
110 sys_exit(rc__dry)
111 return rc__dry
112}