code wiki / _hdl_build / nx_mobi_huff_gate.nx

nx_mobi_huff_gate.nx source

↩ module page · 80 lines · 5672 B

1// nx_mobi_huff_gate.nx -- end-to-end gate for HUFF/CDIC MOBI liberation (compression 17480): 2// nx_mobi_fixture_build -> nishi_mobi_huff.mobi (PDB w/ compression=17480 + HUFF record + CDIC record + a 3// HUFF-compressed text record) -> nx_mobi_book (now routes 17480 through nx_huffcdic) -> reader/<slug>/. 4// Asserts the Kindle HUFF container flows ALL THE WAY to a readable book.json: format=mobi, EXTH title+author, and 5// the HUFF-decoded prose ("Alice was beginning" from phrase0 + "White Rabbit" from phrase1). This proves the 17480 6// pipeline -- Huffman-record gathering (MOBI 0x70/0x74) + huff_load + cdic_load + hc_unpack + text assembly -- 7// end to end on a real on-disk MOBI. HONEST: the fixture is the minimal all-terminal codelen-1 Huffman case; real 8// multi-codelen Kindle HUFF (the mincode-walk + recursive phrases) needs a real DRM-free file (no open HUFF encoder 9// exists to synthesize a richer one). expect_exit: 0 license_tier: ORIGINAL 10import "nx_syscalls.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 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} 22func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 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_mobi_huff_gate: HUFF/CDIC MOBI (compression 17480) -> readable book.json (end-to-end) ===\n" as *u8) 47 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 48 let SC: *u8 = "knowledge/status/mobi_huff_gate_scratch.log\x00" as *u8 49 50 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC) // writes nishi_mobi_huff.mobi 51 spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SC) // refresh _offc twin (with HUFF wire-in) 52 let rc: i64 = spawn("_offc/nx_mobi_book.elf\x00" as *u8, "knowledge/fixtures/nishi_mobi_huff.mobi\x00" as *u8, "nishi_mobi_huff\x00" as *u8, SC) 53 54 let lb: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_huff/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/nishi_mobi_huff/chap0.txt\x00" as *u8, lc); let cn: i64 = lc[0] 56 57 gp(" rc_decode=" as *u8); gnum(rc); gp(" book.json=" as *u8); gnum(bn); gp("B chap0.txt=" as *u8); gnum(cn); gp("B\n" as *u8) 58 if (cj as i64)!=0 { gp(" decoded prose: \"" as *u8); var pn: i64 = cn; if pn > 90 { pn = 90 } sys_write(1, cj, pn); gp("...\"\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 mobi_book-nonzero (HUFF decode failed)\n" as *u8) } 62 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) } 63 if (bj as i64)!=0 { if ghas(bj, bn, "The Liberation of Alice" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL title-missing\n" as *u8) } } 64 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) } } 65 // the HUFF-decoded prose: phrase0 + phrase1 66 if (cj as i64)!=0 { if ghas(cj, cn, "Alice was beginning" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL phrase0-not-decoded\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-chap0\n" as *u8) } 67 if (cj as i64)!=0 { if ghas(cj, cn, "very tired" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL phrase0-body-missing\n" as *u8) } } 68 if (cj as i64)!=0 { if ghas(cj, cn, "White Rabbit" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL phrase1-not-decoded\n" as *u8) } } 69 70 gp("MOBI-HUFF-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 71 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 72 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 73 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 74 let ctr__dry: *i64 = gv_ctr() 75 ctr__dry[0] = pass 76 ctr__dry[1] = pass + fail 77 let rc__dry: i64 = gv_verdict("MOBI-HUFF-GATE" as *u8, ctr__dry, "HUFF/CDIC MOBI 17480 -> huff_load+cdic_load+hc_unpack -> readable book.json, end-to-end)" as *u8) 78 sys_exit(rc__dry) 79 return rc__dry 80}