code wiki / _hdl_build / nx_pattern_emit8.nx
nx_pattern_emit8.nx source
↩ module page · 421 lines · 22135 B
1// nx_pattern_emit8.nx -- PATTERN EMITTER: STRUCT_WALK (shape 13 -- race-1's first named growth
2// rung: the assignment-DECOMPOSITION step Claude still had to hand-write). Authors a fixed+var
3// field-sequence walker from a FIELD TABLE (the spec is the config, rule 11):
4// kind 0 = FIXED(w) skip w bytes (w from widths[i])
5// kind 1 = VAR8 1-byte length prefix + body
6// kind 2 = VAR16 2-byte BE length prefix + body
7// kind 3 = TAIL16 2-byte BE length prefix; the walker RETURNS this final block
8// Authored fn: <name>_locate(b, n, out) -> 0 ok (out[0]=block off, out[1]=block len) / -1 = any
9// truncation REFUSED (never a clipped read). The ClientHello layout (hdr/ver/random/sid/suites/
10// comp/extensions) is exactly this table -- so are the B5 container headers. Per-field bounds
11// checks + offsets are BAKED at emit time field-by-field; the core has no table interpreter at
12// runtime (composition of straight-line checks, rule 22). Test KATs are COMPUTED BY THE EMITTER
13// from a synthetic image it constructs to the same table (exact off/len + truncation refusal).
14// REFUSAL RAILS: unknown kind, TAIL16 not last, last not TAIL16, FIXED width < 1, nfields < 1
15// -> spec REFUSED, never authored. Extends emit chain ..emit7(WIRE_TLV).
16// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL
17import "nx_syscalls.nx"
18
19func p8_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd, s, n); return 0 }
20func p8_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
21
22// the refusal rail: table must be well-formed (see header)
23func p8_spec_ok(kinds: *i64, widths: *i64, nfields: i64) -> i64 {
24 if nfields < 1 { return 0 }
25 var i: i64 = 0
26 while i < nfields {
27 let k: i64 = kinds[i]
28 if k < 0 { return 0 }
29 if k > 3 { return 0 }
30 if k == 0 { if widths[i] < 1 { return 0 } }
31 if k == 3 { if i != nfields - 1 { return 0 } }
32 i = i + 1
33 }
34 if kinds[nfields - 1] != 3 { return 0 }
35 return 1
36}
37
38// emit ONE field's straight-line walk step (bounds check baked per kind)
39func p8_emit_field(fd: i64, kind: i64, width: i64) -> i64 {
40 if kind == 0 {
41 p8_w(fd, " off = off + " as *u8); p8_wn(fd, width); p8_w(fd, "\n" as *u8)
42 p8_w(fd, " if off > n { return 0 - 1 }\n" as *u8)
43 }
44 if kind == 1 {
45 p8_w(fd, " if off + 1 > n { return 0 - 1 }\n" as *u8)
46 p8_w(fd, " off = off + 1 + (b[off] & 0xff)\n" as *u8)
47 p8_w(fd, " if off > n { return 0 - 1 }\n" as *u8)
48 }
49 if kind == 2 {
50 p8_w(fd, " if off + 2 > n { return 0 - 1 }\n" as *u8)
51 p8_w(fd, " off = off + 2 + (((b[off] & 0xff) << 8) | (b[off + 1] & 0xff))\n" as *u8)
52 p8_w(fd, " if off > n { return 0 - 1 }\n" as *u8)
53 }
54 if kind == 3 {
55 p8_w(fd, " if off + 2 > n { return 0 - 1 }\n" as *u8)
56 p8_w(fd, " let tl: i64 = ((b[off] & 0xff) << 8) | (b[off + 1] & 0xff)\n" as *u8)
57 p8_w(fd, " if off + 2 + tl > n { return 0 - 1 }\n" as *u8)
58 p8_w(fd, " out[0] = off + 2\n out[1] = tl\n return 0\n" as *u8)
59 }
60 return 0
61}
62
63// author the STRUCT_WALK core from the field table
64func pe8_emit_struct_walk(fd: i64, name: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
65 p8_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STRUCT_WALK) -- field-table walk, no Claude logic\n" as *u8)
66 p8_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
67 p8_w(fd, "func " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b: *u8, n: i64, out: *i64) -> i64 {\n" as *u8)
68 p8_w(fd, " var off: i64 = 0\n" as *u8)
69 var i: i64 = 0
70 while i < nfields {
71 p8_emit_field(fd, kinds[i], widths[i])
72 i = i + 1
73 }
74 p8_w(fd, " return 0 - 1\n}\n" as *u8)
75 return 1
76}
77
78// construct the synthetic KAT image to the same table; returns total bytes,
79// writes expected block off/len to exp[0]/exp[1]. Var lens: VAR8=2, VAR16=3, TAIL16=4.
80func p8_build_image(img: *u8, kinds: *i64, widths: *i64, nfields: i64, exp: *i64) -> i64 {
81 var off: i64 = 0
82 var i: i64 = 0
83 while i < nfields {
84 let k: i64 = kinds[i]
85 if k == 0 {
86 var w: i64 = 0
87 while w < widths[i] { img[off + w] = 0x55 as u8; w = w + 1 }
88 off = off + widths[i]
89 }
90 if k == 1 {
91 img[off] = 2 as u8
92 img[off + 1] = 0x66 as u8; img[off + 2] = 0x66 as u8
93 off = off + 3
94 }
95 if k == 2 {
96 img[off] = 0 as u8; img[off + 1] = 3 as u8
97 img[off + 2] = 0x66 as u8; img[off + 3] = 0x66 as u8; img[off + 4] = 0x66 as u8
98 off = off + 5
99 }
100 if k == 3 {
101 img[off] = 0 as u8; img[off + 1] = 4 as u8
102 var w3: i64 = 0
103 while w3 < 4 { img[off + 2 + w3] = 0x77 as u8; w3 = w3 + 1 }
104 exp[0] = off + 2
105 exp[1] = 4
106 off = off + 6
107 }
108 i = i + 1
109 }
110 return off
111}
112
113// author the test: KATs computed here from the same table (exact off/len, truncation refusals)
114func pe8_emit_struct_walk_test(fd: i64, name: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
115 let img: *u8 = sys_mmap(512)
116 let exp: *i64 = sys_mmap(32) as *i64
117 let total: i64 = p8_build_image(img, kinds, widths, nfields, exp)
118 p8_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STRUCT_WALK test) -- KATs computed from the table at emit time\n" as *u8)
119 p8_w(fd, "import \"" as *u8); p8_w(fd, name); p8_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
120 p8_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(512)\n" as *u8)
121 var i: i64 = 0
122 while i < total {
123 p8_w(fd, " b[" as *u8); p8_wn(fd, i); p8_w(fd, "] = " as *u8); p8_wn(fd, img[i] & 0xff); p8_w(fd, " as u8\n" as *u8)
124 i = i + 1
125 }
126 p8_w(fd, " let out: *i64 = sys_mmap(32) as *i64\n" as *u8)
127 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, " as *u8); p8_wn(fd, total); p8_w(fd, ", out) == 0 {\n" as *u8)
128 p8_w(fd, " if out[0] == " as *u8); p8_wn(fd, exp[0]); p8_w(fd, " {\n" as *u8)
129 p8_w(fd, " if out[1] == " as *u8); p8_wn(fd, exp[1]); p8_w(fd, " {\n" as *u8)
130 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, " as *u8); p8_wn(fd, total - 1); p8_w(fd, ", out) == 0 - 1 {\n" as *u8)
131 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, 3, out) == 0 - 1 { sys_exit(0) } } } } }\n" as *u8)
132 p8_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
133 return 1
134}
135
136// author module+test to disk; REFUSES a malformed table (returns 0, writes nothing)
137func pe8_author_struct_walk(name: *u8, modpath: *u8, testpath: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
138 if p8_spec_ok(kinds, widths, nfields) != 1 { return 0 }
139 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
140 pe8_emit_struct_walk(mf, name, kinds, widths, nfields); sys_close(mf)
141 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
142 pe8_emit_struct_walk_test(tf, name, kinds, widths, nfields); sys_close(tf)
143 return 1
144}
145
146// ---- v2 (race-5 growth): two kinds named by race-5a's loss -- container headers start with a
147// MAGIC that must MATCH (bad magic = refuse everything) and end with an untagged rest-of-file
148// block (RIFF has no length-prefixed tail):
149// kind 4 = MAGIC4 widths[i] = the EXPECTED packed-BE FourCC; mismatch -> locate refuses
150// kind 5 = TAILREST the walker RETURNS [off .. n) as the block (must be last)
151// Additive: v1 tables (kinds 0-3) untouched. ----
152
153// ====================================================================================
154// DECLARED-SCAFFOLD (operator-authorized R3, X-AUT-NCF-001 / spec 2026-06-13-keystone-
155// newcontrolflow-route-decision.md). The two field-kinds below (6 KEYWORD_NUL_SPLIT,
156// 7 CRC32_TRAILER) are HAND-WRITTEN seed primitives -- NOT organ-authored. They are
157// DEBITED vs autonomy: their register row is tagged author=tutor (cl_register_dual),
158// so the A2 meter counts them in the denominator (auth_all) but NEVER in auth_emit.
159// Tagging them author=emitter to inflate A2 would be the cheat and is REFUSED.
160// BOOTSTRAP-PROVISIONAL: they stay scaffold until >=2 independent parsers ride them
161// (X-AUT-NCF-BOOT); the first rider (the PNG-tEXt reader) is authored BY nx_auto_builder
162// hands-off from a DATA spec (author=emitter), NOT hand-written.
163// kind 6 = KEYWORD_NUL_SPLIT GENERIC NUL-split (PNG tEXt / ID3 text frame / gzip name
164// / ZIP local header / DNS label all = keyword NUL value):
165// scan from off to the first 0x00; emit key_off/key_len/
166// val_off into a baked out-slot base; advance past the NUL.
167// No PNG/tEXt literal -- pure byte-cursor logic.
168// kind 7 = CRC32_TRAILER GENERIC trailing 4-byte BE integrity checksum verify (PNG
169// crc32 / gzip CRC32 / ZIP CRC32 / Ethernet FCS / IEEE 802.3):
170// crc32 (poly 0xEDB88320 reflected, table BAKED at emit time
171// as a math constant, rule 22 -- no runtime table interpreter)
172// over [crc_start .. off); read the BE stored trailer; refuse
173// on mismatch. The verify rail; valid as a terminal field.
174// SLOT/RANGE DATA-DRIVEN VIA widths[i] (kinds 6/7 don't carry a byte-width, so the width
175// column is repurposed -- rule 11, the table is the config): for kind 6, widths[i] = the
176// out-slot base S (writes out[S]=key_off, out[S+1]=key_len, out[S+2]=val_off); for kind 7,
177// widths[i] = the crc range START offset (crc over [START .. off), trailer 4 BE bytes).
178// GENERIC TEST (no fake-green): if kinds 6/7 are correctly body-relative + multi-slot, the
179// organ COMPOSES the reader from a 5-int DATA spec with zero hand-written reader logic.
180// ====================================================================================
181
182func p8_spec2_ok(kinds: *i64, widths: *i64, nfields: i64) -> i64 {
183 if nfields < 1 { return 0 }
184 var i: i64 = 0
185 while i < nfields {
186 let k: i64 = kinds[i]
187 if k < 0 { return 0 }
188 if k > 7 { return 0 }
189 if k == 0 { if widths[i] < 1 { return 0 } }
190 if k == 3 { if i != nfields - 1 { return 0 } }
191 if k == 4 { if widths[i] == 0 { return 0 } }
192 if k == 5 { if i != nfields - 1 { return 0 } }
193 // kind 6 KEYWORD_NUL_SPLIT (scaffold): out-slot base must clear the [0]/[1] block
194 // slots the v1/v2 returns use, and a value-consuming context must follow it.
195 if k == 6 { if widths[i] < 2 { return 0 } }
196 if k == 6 { if i == nfields - 1 { return 0 } }
197 // kind 7 CRC32_TRAILER (scaffold): crc range START must be a non-negative offset.
198 if k == 7 { if widths[i] < 0 { return 0 } }
199 i = i + 1
200 }
201 var last_ok: i64 = 0
202 if kinds[nfields - 1] == 3 { last_ok = 1 }
203 if kinds[nfields - 1] == 5 { last_ok = 1 }
204 // kind 7 CRC32_TRAILER is a valid TERMINAL field: it verifies + returns 0 on match.
205 if kinds[nfields - 1] == 7 { last_ok = 1 }
206 return last_ok
207}
208
209// emit one v2 field step (falls through to v1 kinds)
210func p8_emit_field2(fd: i64, kind: i64, width: i64) -> i64 {
211 if kind < 4 { p8_emit_field(fd, kind, width); return 0 }
212 if kind == 4 {
213 p8_w(fd, " if off + 4 > n { return 0 - 1 }\n" as *u8)
214 var bi: i64 = 0
215 while bi < 4 {
216 p8_w(fd, " if (b[off + " as *u8); p8_wn(fd, bi)
217 p8_w(fd, "] & 0xff) != " as *u8); p8_wn(fd, (width >> ((3 - bi) * 8)) & 0xff)
218 p8_w(fd, " { return 0 - 1 }\n" as *u8)
219 bi = bi + 1
220 }
221 p8_w(fd, " off = off + 4\n" as *u8)
222 }
223 if kind == 5 {
224 p8_w(fd, " out[0] = off\n out[1] = n - off\n return 0\n" as *u8)
225 }
226 // ---- DECLARED-SCAFFOLD kinds 6/7 (author=tutor, debited; see header block) ----
227 // kind 6 KEYWORD_NUL_SPLIT: scan off -> first 0x00; out[base]=key_off, out[base+1]=
228 // key_len, out[base+2]=val_off; advance off past the NUL. width = out-slot base.
229 // GENERIC: a NUL-terminated keyword followed by a value is the universal shape of PNG
230 // tEXt, ID3v2 text frames, gzip/ZIP filename fields, DNS labels -- it is a NUL-split.
231 if kind == 6 {
232 p8_w(fd, " if off >= n { return 0 - 1 }\n" as *u8)
233 p8_w(fd, " var ks: i64 = off\n" as *u8)
234 p8_w(fd, " while b[off] != (0 as u8) {\n" as *u8)
235 p8_w(fd, " off = off + 1\n" as *u8)
236 p8_w(fd, " if off >= n { return 0 - 1 }\n" as *u8)
237 p8_w(fd, " }\n" as *u8)
238 p8_w(fd, " out[" as *u8); p8_wn(fd, width); p8_w(fd, "] = ks\n" as *u8)
239 p8_w(fd, " out[" as *u8); p8_wn(fd, width + 1); p8_w(fd, "] = off - ks\n" as *u8)
240 p8_w(fd, " out[" as *u8); p8_wn(fd, width + 2); p8_w(fd, "] = off + 1\n" as *u8)
241 p8_w(fd, " off = off + 1\n" as *u8)
242 }
243 // kind 7 CRC32_TRAILER: crc32 (poly 0xEDB88320 reflected, BAKED math constant -- no
244 // runtime table interpreter, rule 22) over [width .. off); read the 4-byte BE stored
245 // trailer at off; REFUSE on mismatch. Returns the verified block [width .. off) and 0.
246 // GENERIC: a trailing 4-byte integrity checksum over preceding bytes is PNG crc32, gzip
247 // CRC32, ZIP CRC32, Ethernet FCS, IEEE 802.3 -- it is a crc32-trailer verify, not a hack.
248 if kind == 7 {
249 p8_w(fd, " if off + 4 > n { return 0 - 1 }\n" as *u8)
250 p8_w(fd, " var cc: i64 = 0xffffffff\n" as *u8)
251 p8_w(fd, " var ci: i64 = " as *u8); p8_wn(fd, width); p8_w(fd, "\n" as *u8)
252 p8_w(fd, " while ci < off {\n" as *u8)
253 p8_w(fd, " cc = cc ^ (b[ci] & 0xff)\n" as *u8)
254 p8_w(fd, " var cb: i64 = 0\n" as *u8)
255 p8_w(fd, " while cb < 8 {\n" as *u8)
256 p8_w(fd, " let cm: i64 = 0 - (cc & 1)\n" as *u8)
257 p8_w(fd, " cc = (cc >> 1) ^ (0xedb88320 & cm)\n" as *u8)
258 p8_w(fd, " cb = cb + 1\n" as *u8)
259 p8_w(fd, " }\n" as *u8)
260 p8_w(fd, " ci = ci + 1\n" as *u8)
261 p8_w(fd, " }\n" as *u8)
262 p8_w(fd, " let cv: i64 = (cc ^ 0xffffffff) & 0xffffffff\n" as *u8)
263 p8_w(fd, " let cs: i64 = (((b[off] & 0xff) << 24) | ((b[off + 1] & 0xff) << 16)) | (((b[off + 2] & 0xff) << 8) | (b[off + 3] & 0xff))\n" as *u8)
264 p8_w(fd, " if cv != cs { return 0 - 1 }\n" as *u8)
265 p8_w(fd, " out[0] = " as *u8); p8_wn(fd, width); p8_w(fd, "\n" as *u8)
266 p8_w(fd, " out[1] = off - " as *u8); p8_wn(fd, width); p8_w(fd, "\n" as *u8)
267 p8_w(fd, " off = off + 4\n return 0\n" as *u8)
268 }
269 return 0
270}
271
272func pe8_emit_struct_walk2(fd: i64, name: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
273 p8_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STRUCT_WALK v2 magic/tailrest) -- field-table walk, no Claude logic\n" as *u8)
274 p8_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
275 p8_w(fd, "func " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b: *u8, n: i64, out: *i64) -> i64 {\n" as *u8)
276 p8_w(fd, " var off: i64 = 0\n" as *u8)
277 var i: i64 = 0
278 while i < nfields {
279 p8_emit_field2(fd, kinds[i], widths[i])
280 i = i + 1
281 }
282 p8_w(fd, " return 0 - 1\n}\n" as *u8)
283 return 1
284}
285
286// host-side crc32 (poly 0xEDB88320 reflected) over img[s .. e) -- the SAME baked math the
287// kind-7 emit step authors, so the KAT trailer is EMITTER-COMPUTED, never hand-typed.
288func p8_crc32(img: *u8, s: i64, e: i64) -> i64 {
289 var cc: i64 = 0xffffffff
290 var ci: i64 = s
291 while ci < e {
292 cc = cc ^ (img[ci] & 0xff)
293 var cb: i64 = 0
294 while cb < 8 {
295 let cm: i64 = 0 - (cc & 1)
296 cc = (cc >> 1) ^ (0xedb88320 & cm)
297 cb = cb + 1
298 }
299 ci = ci + 1
300 }
301 return (cc ^ 0xffffffff) & 0xffffffff
302}
303
304// v2 KAT image: MAGIC4 writes its expected bytes, TAILREST contributes 4 tail bytes.
305// SCAFFOLD kinds 6/7: kind 6 writes "abc\x00val" (keyword NUL value); kind 7 appends the
306// EMITTER-COMPUTED crc32 of the preceding bytes (exp = the verified block it returns).
307func p8_build_image2(img: *u8, kinds: *i64, widths: *i64, nfields: i64, exp: *i64) -> i64 {
308 var off: i64 = 0
309 var i: i64 = 0
310 while i < nfields {
311 let k: i64 = kinds[i]
312 if k < 4 {
313 // reuse v1 building for kinds 0-3 by inlining the same byte rules
314 if k == 0 {
315 var w: i64 = 0
316 while w < widths[i] { img[off + w] = 0x55 as u8; w = w + 1 }
317 off = off + widths[i]
318 }
319 if k == 1 { img[off] = 2 as u8; img[off + 1] = 0x66 as u8; img[off + 2] = 0x66 as u8; off = off + 3 }
320 if k == 2 { img[off] = 0 as u8; img[off + 1] = 3 as u8; img[off + 2] = 0x66 as u8; img[off + 3] = 0x66 as u8; img[off + 4] = 0x66 as u8; off = off + 5 }
321 if k == 3 {
322 img[off] = 0 as u8; img[off + 1] = 4 as u8
323 var w3: i64 = 0
324 while w3 < 4 { img[off + 2 + w3] = 0x77 as u8; w3 = w3 + 1 }
325 exp[0] = off + 2; exp[1] = 4
326 off = off + 6
327 }
328 }
329 if k == 4 {
330 var mb: i64 = 0
331 while mb < 4 { img[off + mb] = ((widths[i] >> ((3 - mb) * 8)) & 0xff) as u8; mb = mb + 1 }
332 off = off + 4
333 }
334 if k == 5 {
335 var tb: i64 = 0
336 while tb < 4 { img[off + tb] = 0x77 as u8; tb = tb + 1 }
337 exp[0] = off; exp[1] = 4
338 off = off + 4
339 }
340 // SCAFFOLD kind 6: write "abc\x00" -- keyword(3) NUL; kind 6 splits keyword from the
341 // VALUE-START only (it does NOT consume the value -- the value length comes from the
342 // outer length context / a following field), so the image advances past the NUL only.
343 // exp = the split coordinates the kind-6 step writes to its out-slot base.
344 if k == 6 {
345 img[off] = 97 as u8; img[off + 1] = 98 as u8; img[off + 2] = 99 as u8
346 img[off + 3] = 0 as u8
347 exp[0] = off; exp[1] = 3
348 off = off + 4
349 }
350 // SCAFFOLD kind 7: append the EMITTER-COMPUTED crc32 of [width .. off) as a 4-byte BE
351 // trailer; exp = the verified block the kind-7 step returns on a crc MATCH.
352 if k == 7 {
353 let cv: i64 = p8_crc32(img, widths[i], off)
354 img[off + 0] = ((cv >> 24) & 0xff) as u8
355 img[off + 1] = ((cv >> 16) & 0xff) as u8
356 img[off + 2] = ((cv >> 8) & 0xff) as u8
357 img[off + 3] = (cv & 0xff) as u8
358 exp[0] = widths[i]; exp[1] = off - widths[i]
359 off = off + 4
360 }
361 i = i + 1
362 }
363 return off
364}
365
366// v2 test: exact block + header-cut refusal + MAGIC-MISMATCH refusal (the new rail, proven)
367func pe8_emit_struct_walk_test2(fd: i64, name: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
368 let img: *u8 = sys_mmap(512)
369 let exp: *i64 = sys_mmap(32) as *i64
370 let total: i64 = p8_build_image2(img, kinds, widths, nfields, exp)
371 p8_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STRUCT_WALK v2 test) -- KATs computed from the table at emit time\n" as *u8)
372 p8_w(fd, "import \"" as *u8); p8_w(fd, name); p8_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
373 p8_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(512)\n" as *u8)
374 var i: i64 = 0
375 while i < total {
376 p8_w(fd, " b[" as *u8); p8_wn(fd, i); p8_w(fd, "] = " as *u8); p8_wn(fd, img[i] & 0xff); p8_w(fd, " as u8\n" as *u8)
377 i = i + 1
378 }
379 p8_w(fd, " let out: *i64 = sys_mmap(32) as *i64\n" as *u8)
380 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, " as *u8); p8_wn(fd, total); p8_w(fd, ", out) == 0 {\n" as *u8)
381 p8_w(fd, " if out[0] == " as *u8); p8_wn(fd, exp[0]); p8_w(fd, " {\n" as *u8)
382 p8_w(fd, " if out[1] == " as *u8); p8_wn(fd, exp[1]); p8_w(fd, " {\n" as *u8)
383 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, 3, out) == 0 - 1 {\n" as *u8)
384 // tamper-branch selection (mutually exclusive, flat -- no &&/||):
385 var did_tamper: i64 = 0
386 if kinds[0] == 4 {
387 // the new rail, proven: flip one magic byte -> locate must refuse
388 p8_w(fd, " b[0] = (((b[0] & 0xff) + 1) & 0xff) as u8
389" as *u8)
390 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, " as *u8); p8_wn(fd, total); p8_w(fd, ", out) == 0 - 1 { sys_exit(0) } } } } }
391" as *u8)
392 did_tamper = 1
393 }
394 // SCAFFOLD kind 7 CRC tamper (no-fake-green): a record whose LAST field is CRC32_TRAILER
395 // and whose FIRST field is not MAGIC4 -- flip one body byte WITHOUT updating the trailer;
396 // the crc verify MUST refuse (-1). Proves the integrity check FIRES.
397 if did_tamper == 0 {
398 if kinds[nfields - 1] == 7 {
399 p8_w(fd, " b[0] = (((b[0] & 0xff) + 1) & 0xff) as u8
400" as *u8)
401 p8_w(fd, " if " as *u8); p8_w(fd, name); p8_w(fd, "_locate(b, " as *u8); p8_wn(fd, total); p8_w(fd, ", out) == 0 - 1 { sys_exit(0) } } } } }
402" as *u8)
403 did_tamper = 1
404 }
405 }
406 if did_tamper == 0 {
407 p8_w(fd, " sys_exit(0) } } } }
408" as *u8)
409 }
410 p8_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
411 return 1
412}
413
414func pe8_author_struct_walk2(name: *u8, modpath: *u8, testpath: *u8, kinds: *i64, widths: *i64, nfields: i64) -> i64 {
415 if p8_spec2_ok(kinds, widths, nfields) != 1 { return 0 }
416 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
417 pe8_emit_struct_walk2(mf, name, kinds, widths, nfields); sys_close(mf)
418 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
419 pe8_emit_struct_walk_test2(tf, name, kinds, widths, nfields); sys_close(tf)
420 return 1
421}