code wiki / _hdl_build / nx_pattern_emit7.nx
nx_pattern_emit7.nx source
↩ module page · 356 lines · 21515 B
1// nx_pattern_emit7.nx -- PATTERN EMITTER: WIRE_TLV (shape 12 -- the browser-grade binary-parser
2// shape; the BROWSER arc's autonomy lever). The single most recurring browser core is the
3// [type][length][body] wire walk: TLS extensions + handshake messages (type-first, widths 2/2 or
4// 1/3), PNG chunks (length-first, 4/4), JPEG segments (1/2). emit4's PARSER_KAT covers text->int;
5// this authors the BINARY wire walker from 3 spec params:
6// type_w bytes in the type field (1/2/4)
7// len_w bytes in the length field (1/2/4)
8// type_first 1 = [type][len] (TLS/JPEG), 0 = [len][type] (PNG)
9// Field offsets are COMPUTED AT EMIT TIME and baked as integer literals -- the spec is the config
10// (rule 11); the authored core has ZERO layout branching. Authored functions:
11// <name>_rdbe(b,off,w) big-endian field read
12// <name>_count(b,n) well-formed entry count; -1 = TRUNCATED ENTRY REFUSED (never clipped)
13// <name>_find(b,n,t) body offset of first entry of type t; -1 absent; -2 malformed
14// Test KATs are COMPUTED BY THE EMITTER from the same spec (synthetic 2-entry image: exact count,
15// exact body offsets, absent type, truncation refusal). REFUSAL RAILS at author time: widths must
16// be 1/2/4 and type_first 0/1 -- a bad spec is REFUSED, never authored. Drives the BROWSER arc's
17// bp_claude_touch toward 0 (B2 ClientHello census + B5 PNG/JPEG walks are this shape).
18// Extends emit(231)/emit2(235)/emit3(241)/emit4(262)/emit5(264)/emit6(MATH_KERNEL).
19// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL
20import "nx_syscalls.nx"
21
22func p7_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 }
23func p7_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 }
24
25// big-endian field write into the emitter's OWN synthetic KAT image
26func p7_put_be(b: *u8, off: i64, v: i64, w: i64) -> i64 {
27 var i: i64 = 0
28 while i < w { b[off + i] = ((v >> ((w - 1 - i) * 8)) & 0xff) as u8; i = i + 1 }
29 return 0
30}
31
32// spec sanity: widths 1/2/4 only, type_first 0/1 only (the refusal rail)
33func p7_spec_ok(type_w: i64, len_w: i64, type_first: i64) -> i64 {
34 var tw_ok: i64 = 0
35 if type_w == 1 { tw_ok = 1 }
36 if type_w == 2 { tw_ok = 1 }
37 if type_w == 4 { tw_ok = 1 }
38 var lw_ok: i64 = 0
39 if len_w == 1 { lw_ok = 1 }
40 if len_w == 2 { lw_ok = 1 }
41 if len_w == 4 { lw_ok = 1 }
42 var tf_ok: i64 = 0
43 if type_first == 0 { tf_ok = 1 }
44 if type_first == 1 { tf_ok = 1 }
45 if tw_ok == 1 { if lw_ok == 1 { if tf_ok == 1 { return 1 } } }
46 return 0
47}
48
49// author the WIRE_TLV core: layout offsets baked from the spec
50func pe7_emit_wire_tlv(fd: i64, name: *u8, type_w: i64, len_w: i64, type_first: i64) -> i64 {
51 let hdrw: i64 = type_w + len_w
52 var toff: i64 = 0
53 var loff: i64 = type_w
54 if type_first == 0 { toff = len_w; loff = 0 }
55 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV) -- [type][len][body] wire walk, no Claude logic\n" as *u8)
56 p7_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
57 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b: *u8, off: i64, w: i64) -> i64 { var v: i64 = 0; var i: i64 = 0; while i < w { v = (v << 8) | (b[off + i] & 0xff); i = i + 1 } return v }\n" as *u8)
58 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_count(b: *u8, n: i64) -> i64 {\n" as *u8)
59 p7_w(fd, " var off: i64 = 0\n var c: i64 = 0\n while off < n {\n" as *u8)
60 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 1 }\n" as *u8)
61 p7_w(fd, " let l: i64 = " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b, off + " as *u8); p7_wn(fd, loff); p7_w(fd, ", " as *u8); p7_wn(fd, len_w); p7_w(fd, ")\n" as *u8)
62 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l > n { return 0 - 1 }\n" as *u8)
63 p7_w(fd, " off = off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l\n c = c + 1\n }\n return c\n}\n" as *u8)
64 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_find(b: *u8, n: i64, t: i64) -> i64 {\n" as *u8)
65 p7_w(fd, " var off: i64 = 0\n while off < n {\n" as *u8)
66 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 2 }\n" as *u8)
67 p7_w(fd, " let ty: i64 = " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b, off + " as *u8); p7_wn(fd, toff); p7_w(fd, ", " as *u8); p7_wn(fd, type_w); p7_w(fd, ")\n" as *u8)
68 p7_w(fd, " let l: i64 = " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b, off + " as *u8); p7_wn(fd, loff); p7_w(fd, ", " as *u8); p7_wn(fd, len_w); p7_w(fd, ")\n" as *u8)
69 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l > n { return 0 - 2 }\n" as *u8)
70 p7_w(fd, " if ty == t { return off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " }\n" as *u8)
71 p7_w(fd, " off = off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l\n }\n return 0 - 1\n}\n" as *u8)
72 return 1
73}
74
75// author the test: KATs computed HERE from the same spec (synthetic image: type 5 w/ 3-byte body,
76// type 13 w/ 1-byte body -> exact count/offsets/absent/truncation expectations)
77func pe7_emit_wire_tlv_test(fd: i64, name: *u8, type_w: i64, len_w: i64, type_first: i64) -> i64 {
78 let hdrw: i64 = type_w + len_w
79 var toff: i64 = 0
80 var loff: i64 = type_w
81 if type_first == 0 { toff = len_w; loff = 0 }
82 let img: *u8 = sys_mmap(64)
83 p7_put_be(img, toff, 5, type_w)
84 p7_put_be(img, loff, 3, len_w)
85 img[hdrw] = 0xAA as u8; img[hdrw + 1] = 0xBB as u8; img[hdrw + 2] = 0xCC as u8
86 let e2: i64 = hdrw + 3
87 p7_put_be(img, e2 + toff, 13, type_w)
88 p7_put_be(img, e2 + loff, 1, len_w)
89 img[e2 + hdrw] = 0xEE as u8
90 let total: i64 = e2 + hdrw + 1
91 let f5: i64 = hdrw
92 let f13: i64 = e2 + hdrw
93 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV test) -- KATs computed from the spec at emit time\n" as *u8)
94 p7_w(fd, "import \"" as *u8); p7_w(fd, name); p7_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
95 p7_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(64)\n" as *u8)
96 var i: i64 = 0
97 while i < total {
98 p7_w(fd, " b[" as *u8); p7_wn(fd, i); p7_w(fd, "] = " as *u8); p7_wn(fd, img[i] & 0xff); p7_w(fd, " as u8\n" as *u8)
99 i = i + 1
100 }
101 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, total); p7_w(fd, ") == 2 {\n" as *u8)
102 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 5) == " as *u8); p7_wn(fd, f5); p7_w(fd, " {\n" as *u8)
103 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 13) == " as *u8); p7_wn(fd, f13); p7_w(fd, " {\n" as *u8)
104 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 99) == 0 - 1 {\n" as *u8)
105 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, total - 1); p7_w(fd, ") == 0 - 1 { sys_exit(0) } } } } }\n" as *u8)
106 p7_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
107 return 1
108}
109
110// author module+test to disk; REFUSES a bad spec (returns 0, writes nothing)
111func pe7_author_wire_tlv(name: *u8, modpath: *u8, testpath: *u8, type_w: i64, len_w: i64, type_first: i64) -> i64 {
112 if p7_spec_ok(type_w, len_w, type_first) != 1 { return 0 }
113 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
114 pe7_emit_wire_tlv(mf, name, type_w, len_w, type_first); sys_close(mf)
115 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
116 pe7_emit_wire_tlv_test(tf, name, type_w, len_w, type_first); sys_close(tf)
117 return 1
118}
119
120// ---- v2 (race-5 growth): LITTLE-ENDIAN length fields + even-padding (the RIFF/WAV family).
121// Named by race-5a's measured loss (LANE0-BEHIND 480: BE misread of LE lengths). Spec is an
122// ARRAY (<=6-arg law): spec[0]=type_w spec[1]=len_w spec[2]=type_first spec[3]=len_le spec[4]=pad2.
123// Additive: v1 callers untouched; len_le=0 pad2=0 reproduces v1 semantics exactly. ----
124
125// little-endian field write into the emitter's OWN synthetic KAT image
126func p7_put_le(b: *u8, off: i64, v: i64, w: i64) -> i64 {
127 var i: i64 = 0
128 while i < w { b[off + i] = ((v >> (i * 8)) & 0xff) as u8; i = i + 1 }
129 return 0
130}
131
132// v2 refusal rail: v1 rails + len_le/pad2 strictly 0/1
133func p7_spec2_ok(spec: *i64) -> i64 {
134 if p7_spec_ok(spec[0], spec[1], spec[2]) != 1 { return 0 }
135 var le_ok: i64 = 0
136 if spec[3] == 0 { le_ok = 1 }
137 if spec[3] == 1 { le_ok = 1 }
138 var pd_ok: i64 = 0
139 if spec[4] == 0 { pd_ok = 1 }
140 if spec[4] == 1 { pd_ok = 1 }
141 if le_ok == 1 { if pd_ok == 1 { return 1 } }
142 return 0
143}
144
145// emit the length-read expression: rdle when len_le=1, rdbe otherwise
146func p7_emit_lenread(fd: i64, name: *u8, spec: *i64, loff: i64) -> i64 {
147 p7_w(fd, name)
148 if spec[3] == 1 { p7_w(fd, "_rdle(b, off + " as *u8) }
149 if spec[3] == 0 { p7_w(fd, "_rdbe(b, off + " as *u8) }
150 p7_wn(fd, loff); p7_w(fd, ", " as *u8); p7_wn(fd, spec[1]); p7_w(fd, ")\n" as *u8)
151 return 0
152}
153
154// emit the advance: hdrw + l, plus the odd-pad byte when pad2=1
155func p7_emit_adv(fd: i64, hdrw: i64, pad2: i64) -> i64 {
156 p7_w(fd, " off = off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l" as *u8)
157 if pad2 == 1 { p7_w(fd, " + (l & 1)" as *u8) }
158 p7_w(fd, "\n" as *u8)
159 return 0
160}
161
162func pe7_emit_wire_tlv2(fd: i64, name: *u8, spec: *i64) -> i64 {
163 let hdrw: i64 = spec[0] + spec[1]
164 var toff: i64 = 0
165 var loff: i64 = spec[0]
166 if spec[2] == 0 { toff = spec[1]; loff = 0 }
167 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV v2 le/pad) -- [type][len][body] wire walk, no Claude logic\n" as *u8)
168 p7_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
169 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b: *u8, off: i64, w: i64) -> i64 { var v: i64 = 0; var i: i64 = 0; while i < w { v = (v << 8) | (b[off + i] & 0xff); i = i + 1 } return v }\n" as *u8)
170 if spec[3] == 1 {
171 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_rdle(b: *u8, off: i64, w: i64) -> i64 { var v: i64 = 0; var i: i64 = 0; while i < w { v = v | ((b[off + i] & 0xff) << (i * 8)); i = i + 1 } return v }\n" as *u8)
172 }
173 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_count(b: *u8, n: i64) -> i64 {\n" as *u8)
174 p7_w(fd, " var off: i64 = 0\n var c: i64 = 0\n while off < n {\n" as *u8)
175 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 1 }\n" as *u8)
176 p7_w(fd, " let l: i64 = " as *u8); p7_emit_lenread(fd, name, spec, loff)
177 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l > n { return 0 - 1 }\n" as *u8)
178 p7_emit_adv(fd, hdrw, spec[4])
179 p7_w(fd, " c = c + 1\n }\n return c\n}\n" as *u8)
180 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_find(b: *u8, n: i64, t: i64) -> i64 {\n" as *u8)
181 p7_w(fd, " var off: i64 = 0\n while off < n {\n" as *u8)
182 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 2 }\n" as *u8)
183 p7_w(fd, " let ty: i64 = " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b, off + " as *u8); p7_wn(fd, toff); p7_w(fd, ", " as *u8); p7_wn(fd, spec[0]); p7_w(fd, ")\n" as *u8)
184 p7_w(fd, " let l: i64 = " as *u8); p7_emit_lenread(fd, name, spec, loff)
185 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " + l > n { return 0 - 2 }\n" as *u8)
186 p7_w(fd, " if ty == t { return off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " }\n" as *u8)
187 p7_emit_adv(fd, hdrw, spec[4])
188 p7_w(fd, " }\n return 0 - 1\n}\n" as *u8)
189 return 1
190}
191
192// v2 KATs computed from the same spec rules: entry type 5 / 3-byte body (ODD -> pad when pad2),
193// entry type 13 / 1-byte body (ODD -> pad). Truncation cut lands INSIDE the last body.
194func pe7_emit_wire_tlv_test2(fd: i64, name: *u8, spec: *i64) -> i64 {
195 let hdrw: i64 = spec[0] + spec[1]
196 var toff: i64 = 0
197 var loff: i64 = spec[0]
198 if spec[2] == 0 { toff = spec[1]; loff = 0 }
199 let img: *u8 = sys_mmap(64)
200 p7_put_be(img, toff, 5, spec[0])
201 if spec[3] == 0 { p7_put_be(img, loff, 3, spec[1]) }
202 if spec[3] == 1 { p7_put_le(img, loff, 3, spec[1]) }
203 img[hdrw] = 0xAA as u8; img[hdrw + 1] = 0xBB as u8; img[hdrw + 2] = 0xCC as u8
204 var e2: i64 = hdrw + 3
205 if spec[4] == 1 { img[e2] = 0 as u8; e2 = e2 + 1 }
206 p7_put_be(img, e2 + toff, 13, spec[0])
207 if spec[3] == 0 { p7_put_be(img, e2 + loff, 1, spec[1]) }
208 if spec[3] == 1 { p7_put_le(img, e2 + loff, 1, spec[1]) }
209 img[e2 + hdrw] = 0xEE as u8
210 var total: i64 = e2 + hdrw + 1
211 if spec[4] == 1 { img[total] = 0 as u8; total = total + 1 }
212 let f5: i64 = hdrw
213 let f13: i64 = e2 + hdrw
214 var trunc_n: i64 = total - 1
215 if spec[4] == 1 { trunc_n = trunc_n - 1 }
216 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV v2 test) -- KATs computed from the spec at emit time\n" as *u8)
217 p7_w(fd, "import \"" as *u8); p7_w(fd, name); p7_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
218 p7_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(64)\n" as *u8)
219 var i: i64 = 0
220 while i < total {
221 p7_w(fd, " b[" as *u8); p7_wn(fd, i); p7_w(fd, "] = " as *u8); p7_wn(fd, img[i] & 0xff); p7_w(fd, " as u8\n" as *u8)
222 i = i + 1
223 }
224 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, total); p7_w(fd, ") == 2 {\n" as *u8)
225 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 5) == " as *u8); p7_wn(fd, f5); p7_w(fd, " {\n" as *u8)
226 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 13) == " as *u8); p7_wn(fd, f13); p7_w(fd, " {\n" as *u8)
227 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 99) == 0 - 1 {\n" as *u8)
228 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, trunc_n); p7_w(fd, ") == 0 - 1 { sys_exit(0) } } } } }\n" as *u8)
229 p7_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
230 return 1
231}
232
233// v2 author: spec array, refusal-railed; writes nothing on a bad spec
234func pe7_author_wire_tlv2(name: *u8, modpath: *u8, testpath: *u8, spec: *i64) -> i64 {
235 if p7_spec2_ok(spec) != 1 { return 0 }
236 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
237 pe7_emit_wire_tlv2(mf, name, spec); sys_close(mf)
238 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
239 pe7_emit_wire_tlv_test2(tf, name, spec); sys_close(tf)
240 return 1
241}
242
243// ---- v3 (race-6 growth): TRAILING FIELD after the body (PNG's per-chunk CRC; any checksum/
244// footer family). Named by race-6a's measured loss: 8 lost rows, all ONE concept -- the walk's
245// bounds and advance must include spec[5]=trail_w bytes after body(+pad). trail_w=0 reproduces
246// v2 exactly; rails: trail_w 0..8. ----
247
248func p7_spec3_ok(spec: *i64) -> i64 {
249 if p7_spec2_ok(spec) != 1 { return 0 }
250 if spec[5] < 0 { return 0 }
251 if spec[5] > 8 { return 0 }
252 return 1
253}
254
255// emit the v3 bounds guard: off + hdrw + l + trail > n -> refuse rc
256func p7_emit_guard3(fd: i64, hdrw: i64, trail: i64, rc: *u8) -> i64 {
257 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw + trail)
258 p7_w(fd, " + l > n { return " as *u8); p7_w(fd, rc); p7_w(fd, " }\n" as *u8)
259 return 0
260}
261
262// emit the v3 advance: hdrw + l (+ pad) + trail
263func p7_emit_adv3(fd: i64, hdrw: i64, pad2: i64, trail: i64) -> i64 {
264 p7_w(fd, " off = off + " as *u8); p7_wn(fd, hdrw + trail); p7_w(fd, " + l" as *u8)
265 if pad2 == 1 { p7_w(fd, " + (l & 1)" as *u8) }
266 p7_w(fd, "\n" as *u8)
267 return 0
268}
269
270func pe7_emit_wire_tlv3(fd: i64, name: *u8, spec: *i64) -> i64 {
271 let hdrw: i64 = spec[0] + spec[1]
272 var toff: i64 = 0
273 var loff: i64 = spec[0]
274 if spec[2] == 0 { toff = spec[1]; loff = 0 }
275 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV v3 le/pad/trail) -- [type][len][body][trail] wire walk, no Claude logic\n" as *u8)
276 p7_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
277 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b: *u8, off: i64, w: i64) -> i64 { var v: i64 = 0; var i: i64 = 0; while i < w { v = (v << 8) | (b[off + i] & 0xff); i = i + 1 } return v }\n" as *u8)
278 if spec[3] == 1 {
279 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_rdle(b: *u8, off: i64, w: i64) -> i64 { var v: i64 = 0; var i: i64 = 0; while i < w { v = v | ((b[off + i] & 0xff) << (i * 8)); i = i + 1 } return v }\n" as *u8)
280 }
281 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_count(b: *u8, n: i64) -> i64 {\n" as *u8)
282 p7_w(fd, " var off: i64 = 0\n var c: i64 = 0\n while off < n {\n" as *u8)
283 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 1 }\n" as *u8)
284 p7_w(fd, " let l: i64 = " as *u8); p7_emit_lenread(fd, name, spec, loff)
285 p7_emit_guard3(fd, hdrw, spec[5], "0 - 1" as *u8)
286 p7_emit_adv3(fd, hdrw, spec[4], spec[5])
287 p7_w(fd, " c = c + 1\n }\n return c\n}\n" as *u8)
288 p7_w(fd, "func " as *u8); p7_w(fd, name); p7_w(fd, "_find(b: *u8, n: i64, t: i64) -> i64 {\n" as *u8)
289 p7_w(fd, " var off: i64 = 0\n while off < n {\n" as *u8)
290 p7_w(fd, " if off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " > n { return 0 - 2 }\n" as *u8)
291 p7_w(fd, " let ty: i64 = " as *u8); p7_w(fd, name); p7_w(fd, "_rdbe(b, off + " as *u8); p7_wn(fd, toff); p7_w(fd, ", " as *u8); p7_wn(fd, spec[0]); p7_w(fd, ")\n" as *u8)
292 p7_w(fd, " let l: i64 = " as *u8); p7_emit_lenread(fd, name, spec, loff)
293 p7_emit_guard3(fd, hdrw, spec[5], "0 - 2" as *u8)
294 p7_w(fd, " if ty == t { return off + " as *u8); p7_wn(fd, hdrw); p7_w(fd, " }\n" as *u8)
295 p7_emit_adv3(fd, hdrw, spec[4], spec[5])
296 p7_w(fd, " }\n return 0 - 1\n}\n" as *u8)
297 return 1
298}
299
300// v3 KATs from the same spec rules: two entries (type 5 / 3-byte body, type 13 / 1-byte body),
301// trail bytes appended per entry; truncation cut removes the LAST trail byte (refuses iff
302// trail>0 bounds hold) or lands in the last body when trail=0 (the v2 rule).
303func pe7_emit_wire_tlv_test3(fd: i64, name: *u8, spec: *i64) -> i64 {
304 let hdrw: i64 = spec[0] + spec[1]
305 var toff: i64 = 0
306 var loff: i64 = spec[0]
307 if spec[2] == 0 { toff = spec[1]; loff = 0 }
308 let img: *u8 = sys_mmap(96)
309 p7_put_be(img, toff, 5, spec[0])
310 if spec[3] == 0 { p7_put_be(img, loff, 3, spec[1]) }
311 if spec[3] == 1 { p7_put_le(img, loff, 3, spec[1]) }
312 img[hdrw] = 0xAA as u8; img[hdrw + 1] = 0xBB as u8; img[hdrw + 2] = 0xCC as u8
313 var e1end: i64 = hdrw + 3
314 if spec[4] == 1 { img[e1end] = 0 as u8; e1end = e1end + 1 }
315 var tr: i64 = 0
316 while tr < spec[5] { img[e1end + tr] = 0xDD as u8; tr = tr + 1 }
317 let e2: i64 = e1end + spec[5]
318 p7_put_be(img, e2 + toff, 13, spec[0])
319 if spec[3] == 0 { p7_put_be(img, e2 + loff, 1, spec[1]) }
320 if spec[3] == 1 { p7_put_le(img, e2 + loff, 1, spec[1]) }
321 img[e2 + hdrw] = 0xEE as u8
322 var total: i64 = e2 + hdrw + 1
323 if spec[4] == 1 { img[total] = 0 as u8; total = total + 1 }
324 tr = 0
325 while tr < spec[5] { img[total + tr] = 0xDD as u8; tr = tr + 1 }
326 total = total + spec[5]
327 let f5: i64 = hdrw
328 let f13: i64 = e2 + hdrw
329 var trunc_n: i64 = total - 1
330 if spec[5] == 0 { if spec[4] == 1 { trunc_n = trunc_n - 1 } }
331 p7_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV v3 test) -- KATs computed from the spec at emit time\n" as *u8)
332 p7_w(fd, "import \"" as *u8); p7_w(fd, name); p7_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
333 p7_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(96)\n" as *u8)
334 var i: i64 = 0
335 while i < total {
336 p7_w(fd, " b[" as *u8); p7_wn(fd, i); p7_w(fd, "] = " as *u8); p7_wn(fd, img[i] & 0xff); p7_w(fd, " as u8\n" as *u8)
337 i = i + 1
338 }
339 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, total); p7_w(fd, ") == 2 {\n" as *u8)
340 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 5) == " as *u8); p7_wn(fd, f5); p7_w(fd, " {\n" as *u8)
341 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 13) == " as *u8); p7_wn(fd, f13); p7_w(fd, " {\n" as *u8)
342 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_find(b, " as *u8); p7_wn(fd, total); p7_w(fd, ", 99) == 0 - 1 {\n" as *u8)
343 p7_w(fd, " if " as *u8); p7_w(fd, name); p7_w(fd, "_count(b, " as *u8); p7_wn(fd, trunc_n); p7_w(fd, ") == 0 - 1 { sys_exit(0) } } } } }\n" as *u8)
344 p7_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
345 return 1
346}
347
348// v3 author: spec array incl. trail_w, refusal-railed; writes nothing on a bad spec
349func pe7_author_wire_tlv3(name: *u8, modpath: *u8, testpath: *u8, spec: *i64) -> i64 {
350 if p7_spec3_ok(spec) != 1 { return 0 }
351 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
352 pe7_emit_wire_tlv3(mf, name, spec); sys_close(mf)
353 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
354 pe7_emit_wire_tlv_test3(tf, name, spec); sys_close(tf)
355 return 1
356}