nx_world_capture_meta.nx source
↩ module page · 64 lines · 2862 B
1// Versioned capture envelope: bounded ASCII tokens, no allocation, no percent decoding.
2// Output columns: world, capture, release SHA256, WASM SHA256, renderer, timing mode, resolution.
3import "nx_syscalls.nx"
4const WCM_FIELDS: i64 = 7
5const WCM_TOKEN_MAX: i64 = 64
6const WCM_OUT_CAP: i64 = WCM_FIELDS*(WCM_TOKEN_MAX+1)
7func wcm_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n=n+1 }; return n }
8func wcm_equal(b: *u8,s: i64,e: i64,k: *u8) -> i64 { if e-s != wcm_len(k) { return 0 }; var i: i64=0; while i<e-s { if b[s+i]!=k[i] { return 0 }; i=i+1 }; return 1 }
9func wcm_key(b: *u8,s: i64,e: i64) -> i64 {
10 if wcm_equal(b,s,e,"w" as *u8)==1 { return 0 }
11 if wcm_equal(b,s,e,"capture" as *u8)==1 { return 1 }
12 if wcm_equal(b,s,e,"release" as *u8)==1 { return 2 }
13 if wcm_equal(b,s,e,"wasm" as *u8)==1 { return 3 }
14 if wcm_equal(b,s,e,"renderer" as *u8)==1 { return 4 }
15 if wcm_equal(b,s,e,"mode" as *u8)==1 { return 5 }
16 if wcm_equal(b,s,e,"res" as *u8)==1 { return 6 }
17 return -1
18}
19func wcm_token(b: *u8,s: i64,e: i64,field: i64) -> i64 {
20 if e<=s { return 0 }; if e-s>WCM_TOKEN_MAX { return 0 }
21 if field==0 { if e-s>32 { return 0 } }
22 if field==2 || field==3 { if e-s!=64 { return 0 } }
23 var i: i64=s; var sep: i64=0; var digits: i64=0; var nonzero: i64=0
24 while i<e {
25 let c: i64=b[i] as i64
26 if field==2 || field==3 {
27 if (c<48 || c>57) && (c<97 || c>102) { return 0 }
28 } else { if field==6 {
29 if c==120 { if sep!=0 || digits==0 || nonzero==0 { return 0 }; sep=1; digits=0; nonzero=0 }
30 else { if c<48 || c>57 { return 0 }; digits=digits+1; if c!=48 { nonzero=1 } }
31 } else {
32 if (c<97 || c>122) && (c<48 || c>57) && c!=95 && (field==0 || c!=45) { return 0 }
33 } }
34 i=i+1
35 }
36 if field==6 { if sep!=1 || digits==0 || nonzero==0 { return 0 } }
37 return 1
38}
39// Caller owns 2*WCM_FIELDS span words and WCM_OUT_CAP bytes. Negative return never changes output.
40func wcm_parse(b: *u8,n: i64,spans: *i64,out: *u8,cap: i64) -> i64 {
41 if n<0 || cap<WCM_OUT_CAP { return -1 }
42 var k: i64=0; while k<WCM_FIELDS*2 { spans[k]=-1; k=k+1 }
43 var p: i64=0; while p<n { if b[p]==63 as u8 { break }; p=p+1 }
44 if p==n { return -1 }; p=p+1
45 var count: i64=0
46 while p<n {
47 let s: i64=p; while p<n { if b[p]==61 as u8 || b[p]==38 as u8 { break }; p=p+1 }
48 if p==n { return -1 }; if b[p]!=61 as u8 { return -1 }
49 let field: i64=wcm_key(b,s,p); if field<0 { return -1 }; if spans[field*2]>=0 { return -1 }
50 p=p+1; let vs: i64=p
51 while p<n { if b[p]==38 as u8 { break }; p=p+1 }
52 if wcm_token(b,vs,p,field)==0 { return -1 }
53 spans[field*2]=vs; spans[field*2+1]=p; count=count+1
54 if p<n { p=p+1; if p==n { return -1 } }
55 }
56 if count!=WCM_FIELDS { return -1 }
57 var o: i64=0; k=0
58 while k<WCM_FIELDS {
59 if k>0 { out[o]=9 as u8; o=o+1 }
60 var j: i64=spans[k*2]; while j<spans[k*2+1] { out[o]=b[j]; o=o+1; j=j+1 }
61 k=k+1
62 }
63 out[o]=0 as u8; return o
64}