nx_sevenz_gate.nx source
↩ module page · 363 lines · 22518 B
1// nx_sevenz_gate.nx -- THE 7z READER GATE (/compare/modding MD29 sz_walk, 2026-09-06). The subjects are two REAL archives mirrored
2// from the py7zr test corpus (knowledge/fetched/cmp_modding_py7zr_test_1.7z, an LZMA2 archive with an encoded header, and
3// cmp_modding_py7zr_copy.7z, a Copy-coder archive), never a fixture this lane authored -- a reader tested only on archives its
4// author wrote proves it can read its author. The oracle is the archive's OWN per-member CRC-32 digests written by the outside
5// encoder: every data member must decode to exactly its declared size AND hash to the digest the archive carries, through the
6// decoder nx_lzma_lib and the container reader nx_sevenz_lib, with nothing third-party in the path. Named refusals are
7// planted at runtime (a cut container, a forged start-header CRC, a forged header CRC, a corrupted packed byte) and each must be
8// refused by NAME, never read clean. The rung's done-rule fixture (an OBJ, its MTL, four PNG maps, a VMD) is written by the
9// estate's own Copy-coder writer and must walk to the SAME typed partition MD1's zip fixture reaches, through the lib and
10// through the bundle walker that dispatches on the magic. The command is a second witness through the ladder live -> staged ->
11// build fossil, and an extract is re-hashed by the SAME crc32 the archive used.
12// usage: nx_sevenz_gate (no args)
13// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
14import "nx_syscalls.nx"
15import "nx_gate_verdict.nx"
16import "nx_gatekit_lib.nx"
17import "nx_bundle_ingest_lib.nx"
18import "nx_sevenz_lib.nx"
19
20const ZG_DIR: *u8 = "/tmp/nx_sevenz_gate"
21const ZG_T1: *u8 = "knowledge/fetched/cmp_modding_py7zr_test_1.7z"
22const ZG_CP: *u8 = "knowledge/fetched/cmp_modding_py7zr_copy.7z"
23const ZG_CUT: *u8 = "/tmp/nx_sevenz_gate/cut.7z"
24const ZG_BADSTART: *u8 = "/tmp/nx_sevenz_gate/badstart.7z"
25const ZG_BADHDR: *u8 = "/tmp/nx_sevenz_gate/badhdr.7z"
26const ZG_BADPACK: *u8 = "/tmp/nx_sevenz_gate/badpack.7z"
27const ZG_TYPED: *u8 = "/tmp/nx_sevenz_gate/typed.7z"
28const ZG_M0: *u8 = "/tmp/nx_sevenz_gate/member0.bin"
29const ZG_ELF_LIVE: *u8 = "./nx_sevenz.elf"
30const ZG_ELF_STAGE: *u8 = "./nx_sevenz.sov.elf.new"
31const ZG_ELF_BUILD: *u8 = "buildroot/_build/nx_sevenz.sov.elf"
32const ZG_BI_LIVE: *u8 = "./nx_bundle_ingest.elf"
33const ZG_BI_STAGE: *u8 = "./nx_bundle_ingest.sov.elf.new"
34const ZG_BI_BUILD: *u8 = "buildroot/_build/nx_bundle_ingest.sov.elf"
35const ZG_DIR_MODE: i64 = 493
36const ZG_FILE_MODE: i64 = 420
37const ZG_CAP: i64 = 65536
38const ZG_NAMES_HEADROOM: i64 = 4096
39const ZG_ST_N: i64 = 8
40const ZG_START_CRC_OFF: i64 = 8
41const ZG_FIRST_PACK_OFF: i64 = 32
42const ZG_TYPED_N: i64 = 7
43const ZG_PNG_LEN: i64 = 40
44const ZG_VMD_LEN: i64 = 30
45const ZG_VMD_MAGIC_LEN: i64 = 25
46const ZG_TYPED_CAP: i64 = 4096
47
48func zg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
49func zg_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
50func zg_write_file(path: *u8, b: *u8, n: i64) -> i64 {
51 let fd: i64 = sys_openat_wr(path, ZG_FILE_MODE)
52 if fd < 0 { return 0 - 1 }
53 var done: i64 = 0
54 while done < n { let k: i64 = sys_write(fd, b + done, n - done); if k <= 0 { break } done = done + k }
55 sys_close(fd)
56 return done
57}
58// a byte-flipped twin of b written to path; returns the twin
59func zg_twin(b: *u8, n: i64, flip_off: i64, path: *u8) -> *u8 {
60 let t: *u8 = sys_mmap(n + 16)
61 var i: i64 = 0
62 while i < n { t[i] = b[i]; i = i + 1 }
63 if flip_off >= 0 { if flip_off < n { t[flip_off] = (((t[flip_off] & 0xff) as i64) ^ 255) as u8 } }
64 zg_write_file(path, t, n)
65 return t
66}
67// walk an in-memory archive into a fresh table; returns rows (or the named error); st gets dirs/empty/folders/encoded
68func zg_walk(b: *u8, n: i64, out_tbl: *i64, out_names: *i64, st: *i64) -> i64 {
69 let total: i64 = sz_count(b, n)
70 if total < 0 { return total }
71 let tbl: *i64 = sys_mmap(8 * BI_F_N * (total + 1)) as *i64
72 let ncap: i64 = n * 2 + ZG_NAMES_HEADROOM
73 let names: *u8 = sys_mmap(ncap)
74 let m: i64 = sz_walk(b, n, tbl, total, names, ncap, st)
75 out_tbl[0] = tbl as i64
76 out_names[0] = names as i64
77 return m
78}
79// per-archive census over the walked table: res[0]=data rows, [1]=ok, [2]=stored, [3]=failed, [4]=unsupported, [5]=rows with a
80// non-zero digest, [6]=rows whose bytes equal their declared size, [7]=rows with a non-empty name, [8]=rows on the lzma2 coder,
81// [9]=rows on the copy coder, [10]=empty files
82func zg_census(tbl: *i64, m: i64, res: *i64) -> i64 {
83 var k: i64 = 0
84 while k < 12 { res[k] = 0; k = k + 1 }
85 var i: i64 = 0
86 while i < m {
87 let r: *i64 = bi_rec(tbl, i)
88 if r[BI_F_NAMELEN] > 0 { res[7] = res[7] + 1 }
89 if r[BI_F_USIZE] == 0 { if r[BI_F_INFLATE] == BI_INF_STORED { res[10] = res[10] + 1 } }
90 else {
91 res[0] = res[0] + 1
92 if r[BI_F_INFLATE] == BI_INF_OK { res[1] = res[1] + 1 }
93 else { if r[BI_F_INFLATE] == BI_INF_STORED { res[2] = res[2] + 1 }
94 else { if r[BI_F_INFLATE] == BI_INF_UNSUPPORTED { res[4] = res[4] + 1 } else { res[3] = res[3] + 1 } } }
95 if r[BI_F_CRC] != 0 { res[5] = res[5] + 1 }
96 if r[BI_F_DLEN] == r[BI_F_USIZE] { res[6] = res[6] + 1 }
97 if r[BI_F_METHOD] == SZ_CODER_LZMA2 { res[8] = res[8] + 1 }
98 if r[BI_F_METHOD] == SZ_CODER_COPY { res[9] = res[9] + 1 }
99 }
100 i = i + 1
101 }
102 return m
103}
104// the ladder: live, then staged, then the build fossil
105func zg_ladder(live: *u8, stage: *u8, build: *u8) -> *u8 {
106 if zg_exists(live) == 1 { return live }
107 if zg_exists(stage) == 1 { return stage }
108 if zg_exists(build) == 1 { return build }
109 return 0 as *u8
110}
111
112func main(argc: i64, argv: *i64) -> i64 {
113 let ctr: *i64 = gv_ctr()
114 sys_mkdir(ZG_DIR, ZG_DIR_MODE)
115 let l1: *i64 = sys_mmap(16) as *i64
116 let b1: *u8 = sys_read_file(ZG_T1, l1)
117 gv_need("real-lzma2-archive-mirrored (py7zr test_1.7z)" as *u8, ((b1 as i64) != 0) as i64, ctr)
118 let l2: *i64 = sys_mmap(16) as *i64
119 let b2: *u8 = sys_read_file(ZG_CP, l2)
120 gv_need("real-copy-coder-archive-mirrored (py7zr copy.7z)" as *u8, ((b2 as i64) != 0) as i64, ctr)
121 if (b1 as i64) == 0 { return gv_verdict("NX-SEVENZ" as *u8, ctr, "fixtures absent" as *u8) }
122 if (b2 as i64) == 0 { return gv_verdict("NX-SEVENZ" as *u8, ctr, "fixtures absent" as *u8) }
123 let n1: i64 = l1[0]
124 let n2: i64 = l2[0]
125 // ---- 1. signature ----
126 gv_check("signature-recognised-on-the-real-archive" as *u8, sz_is_7z(b1, n1), ctr)
127 let notz: *u8 = sys_mmap(64)
128 notz[0] = 80 as u8; notz[1] = 75 as u8; notz[2] = 3 as u8; notz[3] = 4 as u8
129 gv_check("neg-control-zip-magic-is-not-a-7z" as *u8, (sz_is_7z(notz, 64) == 0) as i64, ctr)
130 gv_check_eq("neg-control-zip-magic-count-refuses-not-a-7z" as *u8, sz_count(notz, 64), SZ_ERR_NOT_7Z, ctr)
131 // ---- 2. the LZMA2 archive walks and every member matches the archive's own digest ----
132 let ot: *i64 = sys_mmap(16) as *i64
133 let on: *i64 = sys_mmap(16) as *i64
134 let st: *i64 = sys_mmap(8 * ZG_ST_N) as *i64
135 let m1: i64 = zg_walk(b1, n1, ot, on, st)
136 gv_check("lzma2-archive-walks (rows>0)" as *u8, (m1 > 0) as i64, ctr)
137 let res: *i64 = sys_mmap(8 * 16) as *i64
138 if m1 > 0 {
139 zg_census(ot[0] as *i64, m1, res)
140 gv_kv("t1_rows" as *u8, m1); gv_kv("t1_data" as *u8, res[0]); gv_kv("t1_ok" as *u8, res[1]); gv_kv("t1_failed" as *u8, res[3])
141 gv_kv("t1_unsupported" as *u8, res[4]); gv_kv("t1_digests" as *u8, res[5]); gv_kv("t1_folders" as *u8, st[2]); gv_kv("t1_encoded_header" as *u8, st[3]); gv_kv("t1_dirs" as *u8, st[0])
142 gv_check("fixture-reached-the-condition: at least one data member" as *u8, (res[0] > 0) as i64, ctr)
143 gv_check("fixture-reached-the-condition: members ride the lzma2 coder" as *u8, (res[8] > 0) as i64, ctr)
144 gv_check("fixture-reached-the-condition: the archive carries per-member digests" as *u8, (res[5] > 0) as i64, ctr)
145 gv_check_eq("every-data-member-decoded-and-crc-verified (ok == data)" as *u8, res[1], res[0], ctr)
146 gv_check_eq("no-member-failed" as *u8, res[3], 0, ctr)
147 gv_check_eq("no-member-unsupported" as *u8, res[4], 0, ctr)
148 gv_check_eq("every-data-member-yields-exactly-its-declared-size" as *u8, res[6], res[0], ctr)
149 gv_check_eq("every-row-carries-a-name" as *u8, res[7], m1, ctr)
150 gv_check_eq("partition-sums: data == ok + stored + failed + unsupported" as *u8, res[1] + res[2] + res[3] + res[4], res[0], ctr)
151 gv_check_eq("rows == data + empty" as *u8, res[0] + res[10], m1, ctr)
152 // determinism: a second walk yields byte-identical member bytes
153 let ot2: *i64 = sys_mmap(16) as *i64
154 let on2: *i64 = sys_mmap(16) as *i64
155 let st2: *i64 = sys_mmap(8 * ZG_ST_N) as *i64
156 let m1b: i64 = zg_walk(b1, n1, ot2, on2, st2)
157 var same: i64 = 0
158 if m1b == m1 {
159 same = 1
160 var i: i64 = 0
161 while i < m1 {
162 let ra: *i64 = bi_rec(ot[0] as *i64, i)
163 let rb: *i64 = bi_rec(ot2[0] as *i64, i)
164 if ra[BI_F_DLEN] != rb[BI_F_DLEN] { same = 0 }
165 else {
166 let da: *u8 = ra[BI_F_DATA] as *u8
167 let db: *u8 = rb[BI_F_DATA] as *u8
168 var k: i64 = 0
169 while k < ra[BI_F_DLEN] { if da[k] != db[k] { same = 0; break } k = k + 1 }
170 }
171 i = i + 1
172 }
173 }
174 gv_check("walk-is-deterministic (second walk byte-identical)" as *u8, same, ctr)
175 }
176 // ---- 3. the Copy-coder archive ----
177 let ot3: *i64 = sys_mmap(16) as *i64
178 let on3: *i64 = sys_mmap(16) as *i64
179 let st3: *i64 = sys_mmap(8 * ZG_ST_N) as *i64
180 let m2: i64 = zg_walk(b2, n2, ot3, on3, st3)
181 gv_check("copy-archive-walks (rows>0)" as *u8, (m2 > 0) as i64, ctr)
182 let res2: *i64 = sys_mmap(8 * 16) as *i64
183 if m2 > 0 {
184 zg_census(ot3[0] as *i64, m2, res2)
185 gv_kv("cp_rows" as *u8, m2); gv_kv("cp_data" as *u8, res2[0]); gv_kv("cp_stored" as *u8, res2[2]); gv_kv("cp_copy_coder" as *u8, res2[9]); gv_kv("cp_encoded_header" as *u8, st3[3])
186 gv_check("fixture-reached-the-condition: copy archive has a data member" as *u8, (res2[0] > 0) as i64, ctr)
187 gv_check_eq("copy-members-ride-the-copy-coder" as *u8, res2[9], res2[0], ctr)
188 gv_check_eq("copy-members-read-in-place-and-crc-verified (stored == data)" as *u8, res2[2], res2[0], ctr)
189 gv_check_eq("copy-archive-no-failures" as *u8, res2[3] + res2[4], 0, ctr)
190 }
191 // ---- 3b. the rung's done-rule fixture: an OBJ, its MTL, four PNG maps and a VMD in a 7z the estate wrote itself (Copy
192 // coder), walked to the SAME typed partition MD1's zip fixture reaches, textures bound through the MTL ----
193 let fnames: *i64 = sys_mmap(8 * 8) as *i64
194 let fdatas: *i64 = sys_mmap(8 * 8) as *i64
195 let flens: *i64 = sys_mmap(8 * 8) as *i64
196 let obj: *u8 = "v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n" as *u8
197 let mtl: *u8 = "newmtl skin\nmap_Kd skin.png\n" as *u8
198 let vmdsrc: *u8 = "Vocaloid Motion Data 0002" as *u8
199 let vmd: *u8 = sys_mmap(64)
200 var vi: i64 = 0
201 while vi < ZG_VMD_MAGIC_LEN { vmd[vi] = vmdsrc[vi]; vi = vi + 1 }
202 let png: *u8 = sys_mmap(64)
203 png[0] = 137 as u8; png[1] = 80 as u8; png[2] = 78 as u8; png[3] = 71 as u8; png[4] = 13 as u8; png[5] = 10 as u8; png[6] = 26 as u8; png[7] = 10 as u8
204 var pk: i64 = 8
205 while pk < ZG_PNG_LEN { png[pk] = (pk * 7 % 251) as u8; pk = pk + 1 }
206 let nm0: *u8 = "cube.obj" as *u8
207 let nm1: *u8 = "cube.mtl" as *u8
208 let nm2: *u8 = "skin.png" as *u8
209 let nm3: *u8 = "maps/normal.png" as *u8
210 let nm4: *u8 = "maps/rough.png" as *u8
211 let nm5: *u8 = "maps/ao.png" as *u8
212 let nm6: *u8 = "dance.vmd" as *u8
213 fnames[0] = nm0 as i64; fdatas[0] = obj as i64; flens[0] = zg_len(obj)
214 fnames[1] = nm1 as i64; fdatas[1] = mtl as i64; flens[1] = zg_len(mtl)
215 fnames[2] = nm2 as i64; fdatas[2] = png as i64; flens[2] = ZG_PNG_LEN
216 fnames[3] = nm3 as i64; fdatas[3] = png as i64; flens[3] = ZG_PNG_LEN
217 fnames[4] = nm4 as i64; fdatas[4] = png as i64; flens[4] = ZG_PNG_LEN
218 fnames[5] = nm5 as i64; fdatas[5] = png as i64; flens[5] = ZG_PNG_LEN
219 fnames[6] = nm6 as i64; fdatas[6] = vmd as i64; flens[6] = ZG_VMD_LEN
220 let tbuf: *u8 = sys_mmap(ZG_TYPED_CAP)
221 let tn: i64 = sz_write_copy(fnames, fdatas, flens, ZG_TYPED_N, tbuf, ZG_TYPED_CAP)
222 gv_check("typed-fixture-written-by-the-copy-writer (bytes>0)" as *u8, (tn > 0) as i64, ctr)
223 gv_kv("typed_bytes" as *u8, tn)
224 if tn > 0 {
225 zg_write_file(ZG_TYPED, tbuf, tn)
226 gv_check("typed-fixture-carries-the-signature" as *u8, sz_is_7z(tbuf, tn), ctr)
227 gv_check_eq("typed-fixture-counts-7-members" as *u8, sz_count(tbuf, tn), ZG_TYPED_N, ctr)
228 let ot4: *i64 = sys_mmap(16) as *i64
229 let on4: *i64 = sys_mmap(16) as *i64
230 let st4: *i64 = sys_mmap(8 * ZG_ST_N) as *i64
231 let m4: i64 = zg_walk(tbuf, tn, ot4, on4, st4)
232 gv_check_eq("typed-fixture-walks-7-rows" as *u8, m4, ZG_TYPED_N, ctr)
233 if m4 == ZG_TYPED_N {
234 let t4: *i64 = ot4[0] as *i64
235 let nm4b: *u8 = on4[0] as *u8
236 bi_bind_textures(t4, m4, nm4b)
237 let part: *i64 = sys_mmap(8 * BI_P_N) as *i64
238 let psum: i64 = bi_partition(t4, m4, part)
239 gv_check_eq("typed-partition-sums-to-the-member-count" as *u8, psum, ZG_TYPED_N, ctr)
240 gv_check_eq("typed-partition-model-1 (the OBJ typed by its bytes)" as *u8, part[BI_P_MODEL], 1, ctr)
241 gv_check_eq("typed-partition-material-1 (the MTL)" as *u8, part[BI_P_MATERIAL], 1, ctr)
242 gv_check_eq("typed-partition-texture-4 (the PNG magics)" as *u8, part[BI_P_TEXTURE], 4, ctr)
243 gv_check_eq("typed-partition-motion-1 (the VMD)" as *u8, part[BI_P_MOTION], 1, ctr)
244 gv_check_eq("typed-partition-unknown-0" as *u8, part[BI_P_UNKNOWN], 0, ctr)
245 gv_check_eq("typed-members-all-stored-and-crc-verified" as *u8, part[BI_P_STORED], ZG_TYPED_N, ctr)
246 gv_check_eq("typed-members-none-failed" as *u8, part[BI_P_FAILED], 0, ctr)
247 gv_check_eq("typed-skin.png-bound-through-the-MTL (bound 1)" as *u8, part[BI_P_BOUND], 1, ctr)
248 gv_check_eq("typed-three-maps-unbound (nothing references them)" as *u8, part[BI_P_UNBOUND], 3, ctr)
249 let rm: *i64 = bi_rec(t4, 3)
250 gv_check_eq("typed-utf16-path-name-round-trips (maps/normal.png = 15 bytes)" as *u8, rm[BI_F_NAMELEN], zg_len(nm3), ctr)
251 }
252 // the bundle walker as the third witness: it must dispatch on the MAGIC and reach the same partition
253 let belf: *u8 = zg_ladder(ZG_BI_LIVE, ZG_BI_STAGE, ZG_BI_BUILD)
254 gv_need("bundle-walker-witness-present (live, staged or build fossil of nx_bundle_ingest)" as *u8, ((belf as i64) != 0) as i64, ctr)
255 if (belf as i64) != 0 {
256 let out3: *u8 = sys_mmap(ZG_CAP)
257 let bl3: *i64 = sys_mmap(16) as *i64
258 let rcb: i64 = gk_run_capture(belf, ZG_TYPED, 0 as *u8, 0 as *u8, 0 as *u8, out3, ZG_CAP, bl3)
259 gv_check_eq("bundle-walker-exits-0-on-the-typed-7z" as *u8, rcb, 0, ctr)
260 gv_check("bundle-walker-names-the-container-7z" as *u8, gk_out_has(out3, bl3[0], "container=7z" as *u8), ctr)
261 gv_check("bundle-walker-reaches-the-typed-partition (model=1 material=1 texture=4 motion=1)" as *u8, gk_out_has(out3, bl3[0], "model=1 material=1 texture=4 motion=1" as *u8), ctr)
262 gv_check("bundle-walker-partition-sums-to-7" as *u8, gk_out_has(out3, bl3[0], "sum=7" as *u8), ctr)
263 gv_check("bundle-walker-binds-the-skin-map (textures_bound=1)" as *u8, gk_out_has(out3, bl3[0], "textures_bound=1" as *u8), ctr)
264 }
265 }
266 // ---- 3c. the x86 BCJ branch converter, transliterated from the mirrored Bra86.c: proven by ROUND-TRIP against its own
267 // encode direction on a buffer carrying CALL/JMP rel32 patterns -- a self-consistency proof, not an outside oracle;
268 // a real 7z with a BCJ folder written by 7-Zip is the oracle still owed and is named on the board ----
269 let xb: *u8 = sys_mmap(512)
270 let xo: *u8 = sys_mmap(512)
271 var xi: i64 = 0
272 while xi < 400 { xb[xi] = ((xi * 37 + 11) % 251) as u8; xi = xi + 1 }
273 // plant six CALL rel32 and two JMP rel32 sites whose top byte is 00 or FF (the converter only touches those)
274 var site: i64 = 8
275 var planted: i64 = 0
276 while site + 5 < 380 {
277 if (planted & 1) == 0 { xb[site] = 232 as u8 } else { xb[site] = 233 as u8 }
278 xb[site + 1] = ((site * 3) % 256) as u8; xb[site + 2] = 1 as u8; xb[site + 3] = 0 as u8
279 if (planted & 2) == 0 { xb[site + 4] = 0 as u8 } else { xb[site + 4] = 255 as u8 }
280 planted = planted + 1
281 site = site + 47
282 }
283 xi = 0
284 while xi < 400 { xo[xi] = xb[xi]; xi = xi + 1 }
285 let xs: *i64 = sys_mmap(16) as *i64
286 xs[0] = 0
287 let enc_n: i64 = lz_bcj_x86(xb, 400, 0, xs, 1)
288 var changed: i64 = 0
289 xi = 0
290 while xi < 400 { if xb[xi] != xo[xi] { changed = changed + 1 } xi = xi + 1 }
291 gv_kv("bcj_sites_planted" as *u8, planted); gv_kv("bcj_bytes_changed_by_encode" as *u8, changed); gv_kv("bcj_encode_processed" as *u8, enc_n)
292 gv_check("fixture-reached-the-condition: bcj encode changed bytes (the planted rel32 sites were converted)" as *u8, (changed > 0) as i64, ctr)
293 gv_check("bcj-encode-processed-the-buffer-up-to-the-four-byte-tail" as *u8, (enc_n >= 400 - 4 - 4) as i64, ctr)
294 xs[0] = 0
295 lz_bcj_x86(xb, 400, 0, xs, 0)
296 var back: i64 = 1
297 xi = 0
298 while xi < 400 { if xb[xi] != xo[xi] { back = 0 } xi = xi + 1 }
299 gv_check("bcj-decode-restores-the-original-byte-for-byte (round trip over 400 bytes)" as *u8, back, ctr)
300 xs[0] = 0
301 gv_check_eq("bcj-refuses-a-buffer-shorter-than-five-bytes (returns 0, touches nothing)" as *u8, lz_bcj_x86(xb, 4, 0, xs, 0), 0, ctr)
302 gv_check("bcj-chain-coder-class-is-named" as *u8, gk_out_has(sz_coder_name(SZ_CODER_BCJ_X86), 12, "bcj-x86+lzma" as *u8), ctr)
303 // ---- 4. named refusals, planted at runtime on the real archive ----
304 let cut: *u8 = zg_twin(b1, n1, 0 - 1, ZG_CUT)
305 gv_check_eq("neg-control-container-cut-before-its-end-header-is-refused-by-name" as *u8, sz_count(cut, n1 - 1), SZ_ERR_TRUNCATED, ctr)
306 gv_check_eq("neg-control-half-a-container-is-refused-by-name" as *u8, sz_count(cut, n1 / 2), SZ_ERR_TRUNCATED, ctr)
307 let bs: *u8 = zg_twin(b1, n1, ZG_START_CRC_OFF, ZG_BADSTART)
308 gv_check_eq("neg-control-forged-start-header-crc-is-refused-by-name" as *u8, sz_count(bs, n1), SZ_ERR_START_CRC, ctr)
309 let bh: *u8 = zg_twin(b1, n1, n1 - 1, ZG_BADHDR)
310 gv_check_eq("neg-control-forged-header-byte-is-refused-by-name (header crc)" as *u8, sz_count(bh, n1), SZ_ERR_HEADER_CRC, ctr)
311 let bp: *u8 = zg_twin(b1, n1, ZG_FIRST_PACK_OFF, ZG_BADPACK)
312 let otp: *i64 = sys_mmap(16) as *i64
313 let onp: *i64 = sys_mmap(16) as *i64
314 let stp: *i64 = sys_mmap(8 * ZG_ST_N) as *i64
315 let mp: i64 = zg_walk(bp, n1, otp, onp, stp)
316 var corrupt_seen: i64 = 0
317 var corrupt_how: i64 = 0
318 if mp < 0 { corrupt_seen = 1; corrupt_how = 1 }
319 else {
320 let resp: *i64 = sys_mmap(8 * 16) as *i64
321 zg_census(otp[0] as *i64, mp, resp)
322 if resp[3] > 0 { corrupt_seen = 1; corrupt_how = 2 }
323 }
324 gv_kv("badpack_named_by" as *u8, corrupt_how)
325 gv_check("neg-control-corrupted-packed-byte-is-never-read-clean (header refused=1 or a member FAILED its digest=2)" as *u8, corrupt_seen, ctr)
326 // the cut twin was written by this gate to disk at full length; the cut is applied by LENGTH at the call, so the disk file
327 // is the full archive and the on-disk witness below (the command's cut run) needs a genuinely short file
328 let cutn: i64 = n1 - 1
329 zg_write_file(ZG_CUT, cut, cutn)
330 let lc: *i64 = sys_mmap(16) as *i64
331 let cb: *u8 = sys_read_file(ZG_CUT, lc)
332 var cut_ok: i64 = 0
333 if (cb as i64) != 0 { if lc[0] == cutn { cut_ok = 1 } }
334 gv_check("fixture-reached-the-condition: the cut twin on disk is one byte short" as *u8, cut_ok, ctr)
335 // ---- 5. the command as a second witness, through the ladder ----
336 let elf: *u8 = zg_ladder(ZG_ELF_LIVE, ZG_ELF_STAGE, ZG_ELF_BUILD)
337 gv_need("cli-witness-present (live, staged or build fossil of nx_sevenz)" as *u8, ((elf as i64) != 0) as i64, ctr)
338 if (elf as i64) != 0 {
339 let out2: *u8 = sys_mmap(ZG_CAP)
340 let bl2: *i64 = sys_mmap(16) as *i64
341 let rcl: i64 = gk_run_capture(elf, "list" as *u8, ZG_T1, 0 as *u8, 0 as *u8, out2, ZG_CAP, bl2)
342 gv_check_eq("cli-list-exits-0-on-the-real-archive" as *u8, rcl, 0, ctr)
343 gv_check("cli-summary-reads-GREEN" as *u8, gk_out_has(out2, bl2[0], "verdict=GREEN" as *u8), ctr)
344 gv_check("cli-summary-names-the-coder" as *u8, gk_out_has(out2, bl2[0], "coder=lzma2" as *u8), ctr)
345 let rcc: i64 = gk_run_capture(elf, "list" as *u8, ZG_CUT, 0 as *u8, 0 as *u8, out2, ZG_CAP, bl2)
346 gv_check_eq("cli-refuses-the-cut-container-with-exit-3" as *u8, rcc, 3, ctr)
347 gv_check("cli-names-the-cut-refusal" as *u8, gk_out_has(out2, bl2[0], "SEVENZ-REFUSE truncated" as *u8), ctr)
348 // extract member 0 and re-hash it with the archive's own crc
349 let rce: i64 = gk_run_capture(elf, "extract" as *u8, ZG_T1, "0" as *u8, ZG_M0, out2, ZG_CAP, bl2)
350 gv_check_eq("cli-extract-exits-0" as *u8, rce, 0, ctr)
351 if m1 > 0 {
352 let r0: *i64 = bi_rec(ot[0] as *i64, 0)
353 let lm: *i64 = sys_mmap(16) as *i64
354 let mb: *u8 = sys_read_file(ZG_M0, lm)
355 var mlen: i64 = 0 - 1
356 var mcrc: i64 = 0 - 1
357 if (mb as i64) != 0 { mlen = lm[0]; mcrc = nxzip_crc32(mb, mlen) }
358 gv_check_eq("extracted-member-0-has-its-declared-size" as *u8, mlen, r0[BI_F_USIZE], ctr)
359 gv_check_eq("extracted-member-0-rehashes-to-the-archive-digest" as *u8, mcrc, r0[BI_F_CRC], ctr)
360 }
361 }
362 return gv_verdict("NX-SEVENZ" as *u8, ctr, "two real py7zr archives (lzma2 with an encoded header, copy coder) walk through the one container reader and the one decoder to members that match the archive's own digests, the rung's typed fixture reaches MD1's partition through the lib and the bundle walker, every planted corruption is refused by name, and the command agrees through the ladder" as *u8)
363}