code wiki / _hdl_build / nx_mobi_huffreal_gate.nx

nx_mobi_huffreal_gate.nx source

↩ module page · 82 lines · 6164 B

1// nx_mobi_huffreal_gate.nx -- REAL-FILE validation of the sovereign HUFF/CDIC decoder against a genuine 2// kindlegen-produced HUFF/CDIC MOBI (the libmobi LGPL test sample, durably registered as the benchmark reference 3// libmobi-sample-huffdic.ref). Unlike nx_mobi_huff_gate (the synthetic minimal codelen-1 case), this exercises the 4// REAL multi-codelen Huffman tables + the mincode-walk + recursive non-terminal phrases on 28 real text records. 5// nx_mobi_book (17480 path) -> reader/real_huffdic/ -> nx_nmz_pack -> .nmz @kind=book. 6// Asserts: decode succeeds; the decompressed length == the declared textLen (the strong correctness signal); 7// the prose is genuinely READABLE (distinctive phrases, not right-length garbage); EXTH metadata; and the .nmz. 8// Depends on the durable reference at knowledge/benchmarks/refs/ (re-fetch via nx_benchmark_ref if absent). 9// expect_exit: 0 license_tier: ORIGINAL 10import "nx_syscalls.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 ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 15 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 16 if nl == 0 { return 0 } 17 var i: i64 = 0 18 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 } 19 return 0 20} 21func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 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_mobi_huffreal_gate: REAL kindlegen HUFF/CDIC MOBI -> readable book.json -> .nmz (validation closure) ===\n" as *u8) 46 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 47 let SC: *u8 = "knowledge/status/mobi_huffreal_gate_scratch.log\x00" as *u8 48 let REF: *u8 = "knowledge/benchmarks/refs/libmobi-sample-huffdic.ref\x00" as *u8 49 50 // guard: the durable real-file reference must be present 51 let lr: *i64 = sys_mmap(8) as *i64; let rf: *u8 = rd(REF, lr) 52 if (rf as i64)==0 { gp("HUFFREAL-GATE verdict=SKIP reason=ref-absent (re-fetch+register libmobi-sample-huffdic)\n" as *u8); sys_exit(0); return 0 } 53 54 spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SC) 55 let rc: i64 = spawn("_offc/nx_mobi_book.elf\x00" as *u8, REF, "real_huffdic\x00" as *u8, SC) 56 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SC) 57 let rp: i64 = spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "real_huffdic\x00" as *u8, "book\x00" as *u8, SC) 58 59 let lb: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/real_huffdic/book.json\x00" as *u8, lb); let bn: i64 = lb[0] 60 let lc: *i64 = sys_mmap(8) as *i64; let cj: *u8 = rd("knowledge/staging/media/reader/real_huffdic/chap0.txt\x00" as *u8, lc); let cn: i64 = lc[0] 61 let lc1: *i64 = sys_mmap(8) as *i64; let cj1: *u8 = rd("knowledge/staging/media/reader/real_huffdic/chap1.txt\x00" as *u8, lc1); let cn1: i64 = lc1[0] // rung-2: the real file now splits into chapters 62 let lm: *i64 = sys_mmap(8) as *i64; let nm: *u8 = rd("knowledge/staging/media/nmz/real_huffdic.nmz\x00" as *u8, lm); let mn: i64 = lm[0] 63 64 gp(" ref_bytes=" as *u8); gnum(lr[0]); 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) 65 if (cj as i64)!=0 { gp(" decoded head: \"" as *u8); var pn: i64=cn; if pn>70 { pn=70 } sys_write(1, cj, pn); gp("...\"\n" as *u8) } 66 67 var pass: i64 = 0; var fail: i64 = 0 68 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decode-nonzero\n" as *u8) } 69 if rp == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz_pack-nonzero\n" as *u8) } 70 if (bj as i64)!=0 { if ghas(bj, bn, "\"format\":\"mobi\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-mobi\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-book.json\n" as *u8) } 71 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) } } 72 // the REAL multi-codelen HUFF decode produced genuine readable prose (not right-length garbage) 73 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) } 74 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) } } 75 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) } } 76 if cn + cn1 > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decoded-too-short (full text across chapters)\n" as *u8) } 77 if mn > 20000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL nmz-too-small\n" as *u8) } 78 79 gp("HUFFREAL-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 80 if fail == 0 { gp(" verdict=GREEN (REAL kindlegen HUFF/CDIC MOBI decoded to readable text + packed to .nmz -- multi-codelen mincode-walk + recursion VALIDATED on real data)\n" as *u8); sys_exit(0); return 0 } 81 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 82}