nx_wasm_layout_read_lib.nx source
↩ module page · 37 lines · 1269 B
1// nx_wasm_layout_read_lib.nx -- Parses a hexadecimal layout comment from WebAssembly text and validates it against a shared header.
2import "nx_wasm_layout_verify_lib.nx"
3
4func wlv_hex(c: i64) -> i64 {
5 if c>=48 && c<=57 { return c-48 }
6 if c>=97 && c<=102 { return c-97+10 }
7 return -1
8}
9// Read only the compiler's final, newline-terminated evidence comment.
10func wlv_wat(wat: *u8,n: i64,out: *u8,cap: i64,shared: i64) -> i64 {
11 if wat==(0 as *u8) || out==(0 as *u8) || n<1 { return 0 }
12 if wat[n-1]!=10 { return 0 }
13 var start: i64=n-1
14 while start>0 && wat[start-1]!=10 { start=start-1 }
15 let prefix: *u8=";; nishi-layout-v1 " as *u8
16 var p: i64=0
17 while prefix[p]!=(0 as u8) {
18 if p>=n-start || wat[start+p]!=prefix[p] { return 0 }
19 p=p+1
20 }
21 let digits: i64=n-start-p-1
22 if digits<0 || digits%2!=0 { return 0 }
23 let size: i64=digits/2
24 if size<WLV_HEADER || size>cap { return 0 }
25 var k: i64=0
26 while k<digits {
27 if wlv_hex(wat[start+p+k] as i64)<0 { return 0 }
28 k=k+1
29 }
30 k=0
31 while k<size {
32 out[k]=(wlv_hex(wat[start+p+k*2] as i64)*16+wlv_hex(wat[start+p+k*2+1] as i64)) as u8
33 k=k+1
34 }
35 if wlv_check(out,size,shared)!=1 { return 0 }
36 return size
37}