code wiki / _hdl_build / _pe_alspec1.nx
_pe_alspec1.nx source
↩ module page · 27 lines · 996 B
1// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_TLV v3 le/pad/trail) -- [type][len][body][trail] wire walk, no Claude logic
2import "nx_syscalls.nx"
3func _pe_alspec1_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 }
4func _pe_alspec1_count(b: *u8, n: i64) -> i64 {
5 var off: i64 = 0
6 var c: i64 = 0
7 while off < n {
8 if off + 4 > n { return 0 - 1 }
9 let l: i64 = _pe_alspec1_rdbe(b, off + 2, 2)
10 if off + 4 + l > n { return 0 - 1 }
11 off = off + 4 + l
12 c = c + 1
13 }
14 return c
15}
16func _pe_alspec1_find(b: *u8, n: i64, t: i64) -> i64 {
17 var off: i64 = 0
18 while off < n {
19 if off + 4 > n { return 0 - 2 }
20 let ty: i64 = _pe_alspec1_rdbe(b, off + 0, 2)
21 let l: i64 = _pe_alspec1_rdbe(b, off + 2, 2)
22 if off + 4 + l > n { return 0 - 2 }
23 if ty == t { return off + 4 }
24 off = off + 4 + l
25 }
26 return 0 - 1
27}