code wiki / _hdl_build / nx_kf8_gate.nx
nx_kf8_gate.nx source
↩ module page · 81 lines · 5588 B
1// nx_kf8_gate.nx -- gate for KF8/AZW3 rung-1 text liberation on a REAL dual MOBI6+KF8 file (the libmobi sample,
2// registered libmobi-sample-huffdic.ref). nx_kf8_book finds the KF8 part via EXTH 121 (boundary record 46),
3// decodes its HUFF/CDIC text records (record refs RELATIVE to the boundary), html_to_text -> book.json -> .nmz.
4// Asserts the KF8 part decodes to GENUINE readable prose (distinctive phrases) + EXTH metadata + the owned .nmz.
5// HONEST: rung 1 = text extraction (the text is all there; minor inline SKEL/FRAG markers remain). Rung 2 =
6// FDST/SKEL/FRAG reassembly for exact structure. Depends on the durable reference. expect_exit: 0 tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_gate_verdict.nx"
9
10func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func 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 }
12func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 {
13 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
14 if nl == 0 { return 0 }
15 var i: i64 = 0
16 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 }
17 return 0
18}
19func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) }
20func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
21 let pid: i64 = sys_fork()
22 if pid == 0 {
23 let fd: i64 = sys_openat_wr(redir, 420)
24 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
25 let argv: *i64 = sys_mmap(64) as *i64
26 var n: i64 = 0
27 argv[0] = path as i64; n = 1
28 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
29 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
30 argv[n] = 0
31 let envp: *i64 = sys_mmap(16) as *i64
32 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
33 sys_execve(path, argv, envp)
34 sys_exit(127)
35 }
36 let st: *i64 = sys_mmap(16) as *i64
37 sys_wait4(pid, st, 0)
38 return (st[0] >> 8) & 0xff
39}
40
41func main() -> i64 {
42 gp("=== nx_kf8_gate: REAL dual MOBI6+KF8 -> decode the KF8 part -> readable book.json -> .nmz (KF8 rung 1) ===\n" as *u8)
43 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
44 let SC: *u8 = "knowledge/status/kf8_gate_scratch.log\x00" as *u8
45 let REF: *u8 = "knowledge/benchmarks/refs/libmobi-sample-huffdic.ref\x00" as *u8
46
47 let lr: *i64 = sys_mmap(8) as *i64; let rf: *u8 = rd(REF, lr)
48 if (rf as i64)==0 { gp("KF8-GATE verdict=SKIP reason=ref-absent\n" as *u8); sys_exit(0); return 0 }
49
50 let rc: i64 = spawn(RUNNER, "nx_kf8_book\x00" as *u8, 0 as *u8, SC) // decodes the default sample -> reader/real_kf8/
51 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SC)
52 let rp: i64 = spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "real_kf8\x00" as *u8, "book\x00" as *u8, SC)
53
54 let lb: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/real_kf8/book.json\x00" as *u8, lb); let bn: i64 = lb[0]
55 let lc: *i64 = sys_mmap(8) as *i64; let cj: *u8 = rd("knowledge/staging/media/reader/real_kf8/chap0.txt\x00" as *u8, lc); let cn: i64 = lc[0]
56 let lm: *i64 = sys_mmap(8) as *i64; let nm: *u8 = rd("knowledge/staging/media/nmz/real_kf8.nmz\x00" as *u8, lm); let mn: i64 = lm[0]
57
58 gp(" rc_decode=" as *u8); gnum(rc); gp(" chap0=" as *u8); gnum(cn); gp("B nmz=" as *u8); gnum(mn); gp("B\n" as *u8)
59
60 var pass: i64 = 0; var fail: i64 = 0
61 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kf8-decode-nonzero\n" as *u8) }
62 if rp == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz_pack-nonzero\n" as *u8) }
63 if (bj as i64)!=0 { if ghas(bj, bn, "\"format\":\"kf8\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-kf8\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-book.json\n" as *u8) }
64 if (bj as i64)!=0 { if ghas(bj, bn, "Bartek Fabiszewski" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL author-missing\n" as *u8) } }
65 if (cj as i64)!=0 { if ghas(cj, cn, "sample for testing libmobi" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL prose1-missing\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-chap0\n" as *u8) }
66 if (cj as i64)!=0 { if ghas(cj, cn, "Mobipocket" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL prose2-missing\n" as *u8) } }
67 if (cj as i64)!=0 { if ghas(cj, cn, "kindlegen" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL prose3-missing\n" as *u8) } }
68 if cn > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decoded-too-short\n" as *u8) }
69 if mn > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz-too-small\n" as *u8) }
70
71 gp("KF8-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
72 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
73 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
74 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
75 let ctr__dry: *i64 = gv_ctr()
76 ctr__dry[0] = pass
77 ctr__dry[1] = pass + fail
78 let rc__dry: i64 = gv_verdict("KF8-GATE" as *u8, ctr__dry, "REAL KF8/AZW3 part decoded to readable text via boundary-relative HUFF/CDIC -> .nmz; rung1 text, reassembly=rung2)" as *u8)
79 sys_exit(rc__dry)
80 return rc__dry
81}