code wiki / _hdl_build / nx_mobi_book_gate.nx
nx_mobi_book_gate.nx source
↩ module page · 79 lines · 5262 B
1// nx_mobi_book_gate.nx -- liar-kill gate for the sovereign MOBI/AZW reader. Builds the DRM-free fixture +
2// a DRM-flagged variant, then asserts:
3// DECODE : a DRM-FREE MOBI -> book.json (title+author from EXTH, format=mobi) + chap0.txt (PalmDOC-decompressed
4// real prose) -- i.e. a purchased book LIBERATED from the lock-in container into the Nishi format.
5// DECLINE: a DRM-flagged MOBI -> exit 2 (detected + DECLINED, NOT circumvented -- the legal line).
6// GREEN = we read DRM-free Kindle books on our own device, and refuse DRM rather than break it. expect_exit: 0
7import "nx_syscalls.nx"
8
9func xp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func xn(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 xslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12
13func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
14 let pid: i64 = sys_fork()
15 if pid == 0 {
16 let fd: i64 = sys_openat_wr(redir, 420)
17 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
18 let argv: *i64 = sys_mmap(64) as *i64
19 var n: i64 = 0
20 argv[0] = path as i64; n = 1
21 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
22 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
23 argv[n] = 0
24 let envp: *i64 = sys_mmap(16) as *i64
25 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
26 sys_execve(path, argv, envp)
27 sys_exit(127)
28 }
29 let st: *i64 = sys_mmap(16) as *i64
30 sys_wait4(pid, st, 0)
31 return (st[0] >> 8) & 0xff
32}
33
34func xfind(hay: *u8, hl: i64, needle: *u8) -> i64 {
35 let nl: i64 = xslen(needle); if nl == 0 { return 0-1 }
36 var i: i64 = 0
37 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 i} i=i+1 }
38 return 0-1
39}
40func xhas(hay: *u8, hl: i64, needle: *u8) -> i64 { if xfind(hay, hl, needle) >= 0 { return 1 } return 0 }
41
42func main() -> i64 {
43 xp("=== nx_mobi_book_gate: sovereign MOBI/AZW reader (decode DRM-free, DECLINE DRM) ===\n" as *u8)
44 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
45 let SCRATCH: *u8 = "knowledge/status/mobi_gate_scratch.log\x00" as *u8
46
47 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SCRATCH) // build DRM-free + DRM-flagged fixtures
48 let rc_ok: i64 = spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SCRATCH) // rebuild+refresh _offc + decode the DRM-free default
49
50 let lj: *i64 = sys_mmap(8) as *i64; lj[0]=0
51 let bj: *u8 = sys_read_file("knowledge/staging/media/reader/nishi_mobi_fixture/book.json\x00" as *u8, lj)
52 let bn: i64 = lj[0]
53 let lc: *i64 = sys_mmap(8) as *i64; lc[0]=0
54 let ct: *u8 = sys_read_file("knowledge/staging/media/reader/nishi_mobi_fixture/chap0.txt\x00" as *u8, lc)
55 let cn: i64 = lc[0]
56
57 // DRM-flagged variant must be DECLINED (exit 2), never decoded
58 let rc_drm: i64 = spawn("_offc/nx_mobi_book.elf\x00" as *u8, "knowledge/fixtures/nishi_mobi_drm.mobi\x00" as *u8, "nishi_mobi_drm\x00" as *u8, SCRATCH)
59
60 if (bj as i64) == 0 { xp("MOBI-GATE verdict=RED reason=book.json-missing\n" as *u8); sys_exit(1); return 1 }
61 xp(" book.json (" as *u8); xn(bn); xp(" B): " as *u8); sys_write(1, bj, bn); xp("\n" as *u8)
62 xp(" rc_decode=" as *u8); xn(rc_ok); xp(" rc_drm=" as *u8); xn(rc_drm); xp(" chap0_bytes=" as *u8); xn(cn); xp("\n" as *u8)
63
64 var pass: i64 = 0; var fail: i64 = 0
65 if rc_ok == 0 { pass=pass+1 } else { fail=fail+1; xp(" FAIL decode-nonzero-exit\n" as *u8) }
66 if xhas(bj, bn, "\"format\":\"mobi\"" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; xp(" FAIL not-mobi-format\n" as *u8) }
67 if xhas(bj, bn, "\"title\":\"The Liberation of Alice\"" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; xp(" FAIL title-not-from-EXTH\n" as *u8) }
68 if xhas(bj, bn, "\"author\":\"Lewis Carroll\"" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; xp(" FAIL author-not-from-EXTH\n" as *u8) }
69 // PalmDOC decompressed real prose into chap0.txt
70 if (ct as i64) != 0 { if xhas(ct, cn, "Alice was beginning to get very tired" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; xp(" FAIL prose-not-decompressed\n" as *u8) } } else { fail=fail+1; xp(" FAIL chap0-missing\n" as *u8) }
71 if xhas(ct, cn, "White Rabbit" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; xp(" FAIL prose-truncated\n" as *u8) }
72 if cn > 200 { pass=pass+1 } else { fail=fail+1; xp(" FAIL chap-too-short\n" as *u8) }
73 // THE legal line: DRM detected + declined (exit 2), not circumvented
74 if rc_drm == 2 { pass=pass+1 } else { fail=fail+1; xp(" FAIL drm-not-declined(rc=" as *u8); xn(rc_drm); xp(")\n" as *u8) }
75
76 xp("MOBI-GATE pass=" as *u8); xn(pass); xp(" fail=" as *u8); xn(fail)
77 if fail == 0 { xp(" verdict=GREEN (DRM-free MOBI liberated to Nishi format; DRM detected + declined, not circumvented)\n" as *u8); sys_exit(0); return 0 }
78 xp(" verdict=RED\n" as *u8); sys_exit(1); return 1
79}