code wiki / _hdl_build / nx_mobi_huff_gate.nx
nx_mobi_huff_gate.nx source
↩ module page · 72 lines · 5284 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"
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_huff_gate: HUFF/CDIC MOBI (compression 17480) -> readable book.json (end-to-end) ===\n" as *u8)
46 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
47 let SC: *u8 = "knowledge/status/mobi_huff_gate_scratch.log\x00" as *u8
48
49 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC) // writes nishi_mobi_huff.mobi
50 spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SC) // refresh _offc twin (with HUFF wire-in)
51 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)
52
53 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]
54 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]
55
56 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)
57 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) }
58
59 var pass: i64 = 0; var fail: i64 = 0
60 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL mobi_book-nonzero (HUFF decode failed)\n" as *u8) }
61 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) }
62 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) } }
63 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) } }
64 // the HUFF-decoded prose: phrase0 + phrase1
65 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) }
66 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) } }
67 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) } }
68
69 gp("MOBI-HUFF-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
70 if fail == 0 { gp(" verdict=GREEN (HUFF/CDIC MOBI 17480 -> huff_load+cdic_load+hc_unpack -> readable book.json, end-to-end)\n" as *u8); sys_exit(0); return 0 }
71 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
72}