code wiki / _hdl_build / nx_kf8_gate.nx

nx_kf8_gate.nx source

↩ module page · 73 lines · 5206 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" 8 9func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func 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 } 11func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 12 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 13 if nl == 0 { return 0 } 14 var i: i64 = 0 15 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 } 16 return 0 17} 18func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 19func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 20 let pid: i64 = sys_fork() 21 if pid == 0 { 22 let fd: i64 = sys_openat_wr(redir, 420) 23 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 24 let argv: *i64 = sys_mmap(64) as *i64 25 var n: i64 = 0 26 argv[0] = path as i64; n = 1 27 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 28 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 29 argv[n] = 0 30 let envp: *i64 = sys_mmap(16) as *i64 31 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 32 sys_execve(path, argv, envp) 33 sys_exit(127) 34 } 35 let st: *i64 = sys_mmap(16) as *i64 36 sys_wait4(pid, st, 0) 37 return (st[0] >> 8) & 0xff 38} 39 40func main() -> i64 { 41 gp("=== nx_kf8_gate: REAL dual MOBI6+KF8 -> decode the KF8 part -> readable book.json -> .nmz (KF8 rung 1) ===\n" as *u8) 42 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 43 let SC: *u8 = "knowledge/status/kf8_gate_scratch.log\x00" as *u8 44 let REF: *u8 = "knowledge/benchmarks/refs/libmobi-sample-huffdic.ref\x00" as *u8 45 46 let lr: *i64 = sys_mmap(8) as *i64; let rf: *u8 = rd(REF, lr) 47 if (rf as i64)==0 { gp("KF8-GATE verdict=SKIP reason=ref-absent\n" as *u8); sys_exit(0); return 0 } 48 49 let rc: i64 = spawn(RUNNER, "nx_kf8_book\x00" as *u8, 0 as *u8, SC) // decodes the default sample -> reader/real_kf8/ 50 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SC) 51 let rp: i64 = spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "real_kf8\x00" as *u8, "book\x00" as *u8, SC) 52 53 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] 54 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] 55 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] 56 57 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) 58 59 var pass: i64 = 0; var fail: i64 = 0 60 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kf8-decode-nonzero\n" as *u8) } 61 if rp == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz_pack-nonzero\n" as *u8) } 62 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) } 63 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) } } 64 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) } 65 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) } } 66 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) } } 67 if cn > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decoded-too-short\n" as *u8) } 68 if mn > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz-too-small\n" as *u8) } 69 70 gp("KF8-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 71 if fail == 0 { gp(" verdict=GREEN (REAL KF8/AZW3 part decoded to readable text via boundary-relative HUFF/CDIC -> .nmz; rung1 text, reassembly=rung2)\n" as *u8); sys_exit(0); return 0 } 72 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 73}