code wiki / _hdl_build / nx_varfacts_gate.nx
nx_varfacts_gate.nx source
↩ module page · 153 lines · 7432 B
1// nx_varfacts_gate.nx -- PROVE THE .var LANE READS THE MANIFEST, NEVER THE ASSETS, AND REFUSES
2// RATHER THAN UNDERCOUNTS. The fixture ZIP is hand-built from the published PKZIP layout (local
3// headers + central directory + EOCD), not by our writer -- cross-implementation discipline.
4// license_tier: ORIGINAL No hw writes (Rule 26).
5import "nx_syscalls.nx"
6import "nx_gate_verdict.nx"
7import "nx_varfacts_lib.nx"
8
9func zg_u16(b: *u8, at: i64, v: i64) -> i64 { b[at] = (v & 0xff) as u8; b[at+1] = ((v >> 8) & 0xff) as u8; return at + 2 }
10func zg_u32(b: *u8, at: i64, v: i64) -> i64 { b[at]=(v&0xff) as u8; b[at+1]=((v>>8)&0xff) as u8; b[at+2]=((v>>16)&0xff) as u8; b[at+3]=((v>>24)&0xff) as u8; return at + 4 }
11func zg_s(b: *u8, at: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = at; while s[i] != (0 as u8) { b[p] = s[i]; p = p + 1; i = i + 1 } return p }
12func zg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
13
14// one local header + body; returns new offset. Records the entry's lho into lhos[idx].
15func zg_local(b: *u8, at: i64, name: *u8, body: *u8, blen: i64, method: i64, usize: i64, lhos: *i64, idx: i64) -> i64 {
16 lhos[idx] = at
17 var p: i64 = at
18 p = zg_u32(b, p, 0x04034b50)
19 p = zg_u16(b, p, 20)
20 p = zg_u16(b, p, 0)
21 p = zg_u16(b, p, method)
22 p = zg_u16(b, p, 0)
23 p = zg_u16(b, p, 0)
24 p = zg_u32(b, p, 0)
25 p = zg_u32(b, p, blen)
26 p = zg_u32(b, p, usize)
27 p = zg_u16(b, p, zg_len(name))
28 p = zg_u16(b, p, 0)
29 p = zg_s(b, p, name)
30 var i: i64 = 0
31 while i < blen { b[p] = body[i]; p = p + 1; i = i + 1 }
32 return p
33}
34// one central-directory entry; returns new offset
35func zg_cd(b: *u8, at: i64, name: *u8, blen: i64, method: i64, usize: i64, lho: i64) -> i64 {
36 var p: i64 = at
37 p = zg_u32(b, p, 0x02014b50)
38 p = zg_u16(b, p, 20)
39 p = zg_u16(b, p, 20)
40 p = zg_u16(b, p, 0)
41 p = zg_u16(b, p, method)
42 p = zg_u16(b, p, 0)
43 p = zg_u16(b, p, 0)
44 p = zg_u32(b, p, 0)
45 p = zg_u32(b, p, blen)
46 p = zg_u32(b, p, usize)
47 p = zg_u16(b, p, zg_len(name))
48 p = zg_u16(b, p, 0)
49 p = zg_u16(b, p, 0)
50 p = zg_u16(b, p, 0)
51 p = zg_u16(b, p, 0)
52 p = zg_u32(b, p, 0)
53 p = zg_u32(b, p, lho)
54 p = zg_s(b, p, name)
55 return p
56}
57
58func main(argc: i64, argv: *i64) -> i64 {
59 let ctr: *i64 = gv_ctr()
60 gv_head("nx_varfacts_gate -- the manifest is read, the assets are not, refusal beats undercount" as *u8)
61
62 let b: *u8 = sys_mmap(16384)
63 let lhos: *i64 = sys_mmap(16 * 8) as *i64
64 let facts: *i64 = sys_mmap(VF_N_SLOTS * 8) as *i64
65
66 // ---- fixture: 8 entries covering every class; meta.json STORED ----
67 let meta: *u8 = "{\"licenseType\":\"CC BY\",\"contentList\":[]}" as *u8
68 let ml: i64 = zg_len(meta)
69 let tiny: *u8 = "{}" as *u8
70 var p: i64 = 0
71 p = zg_local(b, p, "meta.json" as *u8, meta, ml, 0, ml, lhos, 0)
72 p = zg_local(b, p, "Custom/Atom/Person/Morphs/female/a.vmi" as *u8, tiny, 2, 0, 2, lhos, 1)
73 p = zg_local(b, p, "Saves/scene/s.json" as *u8, tiny, 2, 0, 2, lhos, 2)
74 p = zg_local(b, p, "Custom/Skin/t.jpg" as *u8, tiny, 2, 0, 2, lhos, 3)
75 p = zg_local(b, p, "Custom/Clothing/x/i.vam" as *u8, tiny, 2, 0, 2, lhos, 4)
76 p = zg_local(b, p, "Custom/Hair/h/z.vam" as *u8, tiny, 2, 0, 2, lhos, 5)
77 p = zg_local(b, p, "ADD.cs" as *u8, tiny, 2, 0, 2, lhos, 6)
78 p = zg_local(b, p, "look.vap" as *u8, tiny, 2, 0, 2, lhos, 7)
79 let cd0: i64 = p
80 p = zg_cd(b, p, "meta.json" as *u8, ml, 0, ml, lhos[0])
81 p = zg_cd(b, p, "Custom/Atom/Person/Morphs/female/a.vmi" as *u8, 2, 0, 2, lhos[1])
82 p = zg_cd(b, p, "Saves/scene/s.json" as *u8, 2, 0, 2, lhos[2])
83 p = zg_cd(b, p, "Custom/Skin/t.jpg" as *u8, 2, 0, 2, lhos[3])
84 p = zg_cd(b, p, "Custom/Clothing/x/i.vam" as *u8, 2, 0, 2, lhos[4])
85 p = zg_cd(b, p, "Custom/Hair/h/z.vam" as *u8, 2, 0, 2, lhos[5])
86 p = zg_cd(b, p, "ADD.cs" as *u8, 2, 0, 2, lhos[6])
87 p = zg_cd(b, p, "look.vap" as *u8, 2, 0, 2, lhos[7])
88 let cdsz: i64 = p - cd0
89 p = zg_u32(b, p, 0x06054b50)
90 p = zg_u16(b, p, 0)
91 p = zg_u16(b, p, 0)
92 p = zg_u16(b, p, 8)
93 p = zg_u16(b, p, 8)
94 p = zg_u32(b, p, cdsz)
95 p = zg_u32(b, p, cd0)
96 p = zg_u16(b, p, 0)
97 let total: i64 = p
98
99 // T1 full census from the central directory
100 vf_probe(b, total, total, facts)
101 var t1: i64 = 0
102 if facts[0] == 1 { if facts[1] == 8 { if facts[2] == 1 { if facts[3] == 1 { if facts[4] == 1 { if facts[5] == 1 { if facts[6] == 1 { if facts[7] == 1 { if facts[8] == 1 { if facts[9] == 1 { t1 = 1 } } } } } } } } } }
103 gv_check("T1 MANIFEST CENSUS: 8 entries classify into morphs/scenes/textures/clothing/hair/plugins/appearance and meta.json is located -- all from central-directory NAMES, no member opened" as *u8, t1, ctr)
104
105 // T2 the declared license reads from a STORED meta.json body
106 let lic: *u8 = sys_mmap(64)
107 let mbody: *u8 = ((b as i64) + lhos[0] + 30 + 9) as *u8
108 let r2: i64 = vf_license(mbody, ml, 0, ml, lic)
109 var t2: i64 = 0
110 if r2 > 0 { if lic[0] == 67 { if lic[1] == 67 { if lic[2] == 32 { if lic[3] == 66 { if lic[4] == 89 { t2 = 1 } } } } } }
111 gv_check("T2 DECLARED LICENSE READ (stored): licenseType CC BY extracted from the package's own meta.json" as *u8, t2, ctr)
112
113 // T3 method-8 meta.json: a raw-DEFLATE stored-block wrap of the same body decodes through the
114 // INCUMBENT nx_deflate_inflate and yields the same label
115 let dz: *u8 = sys_mmap(256)
116 dz[0] = 1 as u8
117 zg_u16(dz, 1, ml)
118 zg_u16(dz, 3, 65535 - ml)
119 var di: i64 = 0
120 while di < ml { dz[5+di] = meta[di]; di = di + 1 }
121 let r3: i64 = vf_license(dz, 5 + ml, 8, ml, lic)
122 var t3: i64 = 0
123 if r3 > 0 { if lic[0] == 67 { if lic[4] == 89 { t3 = 1 } } }
124 gv_check("T3 DEFLATED meta.json DECODES: ZIP method 8 rides the incumbent raw-DEFLATE core (nx_deflate_inflate) and yields the same CC BY" as *u8, t3, ctr)
125
126 // T4 garbage refuses: no EOCD -> ok=0 and no counts
127 let g: *u8 = sys_mmap(256)
128 var gi: i64 = 0
129 while gi < 200 { g[gi] = 0x41 as u8; gi = gi + 1 }
130 vf_probe(g, 200, 200, facts)
131 var t4: i64 = 0
132 if facts[0] == 0 { if facts[1] == 0 - 1 { t4 = 1 } }
133 gv_check("T4 NOT-A-ZIP REFUSES: no EOCD yields ok=0 and every count stays -1" as *u8, t4, ctr)
134
135 // T5 window honesty: pretend the file is 1000 bytes longer than the window -> the CD's absolute
136 // offset lands before the window start -> REFUSE, never a partial census
137 vf_probe(b, total, total + 1000, facts)
138 var t5: i64 = 0
139 if facts[0] == 0 { t5 = 1 }
140 gv_check("T5 A PARTIAL CENSUS IS REFUSED: when the central directory starts before the tail window the probe returns ok=0 rather than an undercount" as *u8, t5, ctr)
141
142 // T6 tail semantics: hand the probe only the LAST total-50 bytes (as a real tail read would)
143 // and the census still resolves through absolute-offset arithmetic
144 let tailp: *u8 = ((b as i64) + 50) as *u8
145 vf_probe(tailp, total - 50, total, facts)
146 var t6: i64 = 0
147 if facts[0] == 1 { if facts[1] == 8 { if facts[2] == 1 { t6 = 1 } } }
148 gv_check("T6 TAIL-WINDOW CONTRACT: the census is identical when only the archive tail is in memory -- a 519MB look pack measures from its last few MB" as *u8, t6, ctr)
149
150 let rc: i64 = gv_verdict("VARFACTS-GATE", ctr, "hand-built PKZIP fixture, manifest-only reads, refusal over undercount, incumbent deflate" as *u8)
151 sys_exit(rc)
152 return rc
153}