code wiki / (root) / nx_varfacts.nx

nx_varfacts.nx source

↩ module page · 249 lines · 10133 B

1// nx_varfacts.nx -- CLI: VaM .var package measurements into the refcorpus plane. 2// 3// nx_varfacts probe <file.var> 4// nx_varfacts sweep <listfile> <source> <provenance> <license> [put] 5// 6// The capability-target lane made mechanical (standing policy 1785794796): the package's own 7// MANIFEST (central directory names/sizes) and its own DECLARED license are read; asset bytes 8// are never opened, never converted, never published. Distribution rows ride rc_put_row's k>=8 9// admission wall like every reference corpus. Tail-window IO: a 519MB look pack costs an lseek 10// and a few MB read. 11// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 12import "nx_syscalls.nx" 13import "nx_varfacts_lib.nx" 14import "nx_mediafacts_lib.nx" // mf_median (sorts in place -- min/max only valid AFTER the call) 15import "nx_refcorpus.nx" 16const VFC_MAGIC_4194304: i64 = 4194304 17 18const VFC_TAIL: i64 = 8388608 19const VFC_MAX_FILES: i64 = 1024 20const VFC_PATH_MAX: i64 = 384 21 22func vfc_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 23func vfc_w(s: *u8) -> i64 { sys_write(1, s, vfc_slen(s)); return 0 } 24func vfc_e(s: *u8) -> i64 { sys_write(2, s, vfc_slen(s)); return 0 } 25func vfc_num(v: i64) -> i64 { 26 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 27 var m: i64 = v 28 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 29 let t: *u8 = sys_mmap(32) 30 var k: i64 = 0 31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 32 let o: *u8 = sys_mmap(32) 33 var i: i64 = 0 34 while i < k { o[i] = t[k - 1 - i]; i = i + 1 } 35 sys_write(1, o, k) 36 return 0 37} 38func vfc_itoa(v: i64, out: *u8) -> i64 { 39 var m: i64 = v 40 var p: i64 = 0 41 if m < 0 { out[p] = 45 as u8; p = p + 1; m = 0 - m } 42 if m == 0 { out[p] = 48 as u8; out[p+1] = 0 as u8; return p + 1 } 43 let t: *u8 = sys_mmap(32) 44 var k: i64 = 0 45 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 46 var i: i64 = 0 47 while i < k { out[p] = t[k - 1 - i]; p = p + 1; i = i + 1 } 48 out[p] = 0 as u8 49 return p 50} 51 52// tail-window read: lseek to the end, read the last min(size, VFC_TAIL) bytes. 53// sizebox[0] = true size. Returns tail bytes read, or -1. 54func vfc_read_tail(path: *u8, buf: *u8, sizebox: *i64) -> i64 { 55 let fd: i64 = sys_openat_rd(path) 56 if fd < 0 { return 0 - 1 } 57 let size: i64 = sys_lseek(fd, 0, 2) 58 if size < 0 { sys_close(fd); return 0 - 1 } 59 sizebox[0] = size 60 var want: i64 = size 61 if want > VFC_TAIL { want = VFC_TAIL } 62 sys_lseek(fd, size - want, 0) 63 var got: i64 = 0 64 var stop: i64 = 0 65 while stop == 0 { 66 if got >= want { stop = 1 } else { 67 let k: i64 = sys_read(fd, ((buf as i64) + got) as *u8, want - got) 68 if k <= 0 { stop = 1 } else { got = got + k } 69 } 70 } 71 sys_close(fd) 72 return got 73} 74 75// read meta.json's body by its local-header offset and extract the declared license label. 76func vfc_license(path: *u8, facts: *i64, out: *u8) -> i64 { 77 out[0] = 0 as u8 78 if facts[9] != 1 { return 0 - 1 } 79 let csize: i64 = facts[12] 80 if csize <= 0 { return 0 - 1 } 81 if csize > VFC_MAGIC_4194304 { return 0 - 1 } 82 let fd: i64 = sys_openat_rd(path) 83 if fd < 0 { return 0 - 1 } 84 let hdr: *u8 = sys_mmap(64) 85 sys_lseek(fd, facts[10], 0) 86 var got: i64 = 0 87 while got < 30 { let k: i64 = sys_read(fd, ((hdr as i64) + got) as *u8, 30 - got); if k <= 0 { sys_close(fd); return 0 - 1 } got = got + k } 88 let fnl: i64 = vf_u16(hdr, 26) 89 let exl: i64 = vf_u16(hdr, 28) 90 sys_lseek(fd, facts[10] + 30 + fnl + exl, 0) 91 let body: *u8 = sys_mmap(csize + 16) 92 got = 0 93 while got < csize { let k2: i64 = sys_read(fd, ((body as i64) + got) as *u8, csize - got); if k2 <= 0 { sys_close(fd); return 0 - 1 } got = got + k2 } 94 sys_close(fd) 95 return vf_license(body, csize, facts[11], facts[13], out) 96} 97 98func vfc_print(facts: *i64, lic: *u8) -> i64 { 99 vfc_w("VARFACT ok=" as *u8); vfc_num(facts[0]) 100 if facts[0] == 1 { 101 vfc_w(" entries=" as *u8); vfc_num(facts[1]) 102 vfc_w(" morphs=" as *u8); vfc_num(facts[2]) 103 vfc_w(" scenes=" as *u8); vfc_num(facts[3]) 104 vfc_w(" textures=" as *u8); vfc_num(facts[4]) 105 vfc_w(" clothing=" as *u8); vfc_num(facts[5]) 106 vfc_w(" hair=" as *u8); vfc_num(facts[6]) 107 vfc_w(" plugins=" as *u8); vfc_num(facts[7]) 108 vfc_w(" appearance=" as *u8); vfc_num(facts[8]) 109 vfc_w(" license=" as *u8) 110 if lic[0] != (0 as u8) { vfc_w(lic) } else { vfc_w("unparsed" as *u8) } 111 } 112 vfc_w("\n" as *u8) 113 return 0 114} 115 116func vfc_row(key: *u8, val: i64, unit: *u8, cnt: i64, src: *u8, prov: *u8, lic: *u8, do_put: i64, wrote: *i64) -> i64 { 117 vfc_w("VARFACT-DIST " as *u8); vfc_w(key); vfc_w("=" as *u8); vfc_num(val) 118 vfc_w(" unit=" as *u8); vfc_w(unit); vfc_w(" n=" as *u8); vfc_num(cnt); vfc_w("\n" as *u8) 119 if do_put == 0 { return 0 } 120 let vs: *u8 = sys_mmap(32) 121 vfc_itoa(val, vs) 122 let r: i64 = rc_put_row(src, key, vs, unit, cnt, prov, lic) 123 if r == 0 { wrote[0] = wrote[0] + 1 } 124 return r 125} 126 127func vfc_mmm(base: *u8, arr: *i64, cnt: i64, unit: *u8, src: *u8, prov: *u8, lic: *u8, do_put: i64, wrote: *i64) -> i64 { 128 if cnt <= 0 { return 0 } 129 let med: i64 = mf_median(arr, cnt) 130 let key: *u8 = sys_mmap(96) 131 var bl: i64 = 0 132 while base[bl] != (0 as u8) { key[bl] = base[bl]; bl = bl + 1 } 133 let sfx1: *u8 = "_min" as *u8 134 var i: i64 = 0 135 while sfx1[i] != (0 as u8) { key[bl+i] = sfx1[i]; i = i + 1 } 136 key[bl+i] = 0 as u8 137 vfc_row(key, arr[0], unit, cnt, src, prov, lic, do_put, wrote) 138 let sfx2: *u8 = "_median" as *u8 139 i = 0 140 while sfx2[i] != (0 as u8) { key[bl+i] = sfx2[i]; i = i + 1 } 141 key[bl+i] = 0 as u8 142 vfc_row(key, med, unit, cnt, src, prov, lic, do_put, wrote) 143 let sfx3: *u8 = "_max" as *u8 144 i = 0 145 while sfx3[i] != (0 as u8) { key[bl+i] = sfx3[i]; i = i + 1 } 146 key[bl+i] = 0 as u8 147 vfc_row(key, arr[cnt-1], unit, cnt, src, prov, lic, do_put, wrote) 148 return 0 149} 150 151func main(argc: i64, argv: *i64) -> i64 { 152 if argc < 3 { 153 vfc_e("usage: nx_varfacts probe <file.var>\n" as *u8) 154 vfc_e(" nx_varfacts sweep <listfile> <source> <provenance> <license> [put]\n" as *u8) 155 vfc_e(" manifest + declared license only; asset bytes are never opened.\n" as *u8) 156 sys_exit(2) 157 return 2 158 } 159 let verb: *u8 = argv[1] as *u8 160 let tail: *u8 = sys_mmap(VFC_TAIL) 161 let sizebox: *i64 = sys_mmap(16) as *i64 162 let facts: *i64 = sys_mmap(VF_N_SLOTS * 8) as *i64 163 let lic0: *u8 = sys_mmap(64) 164 165 if verb[0] == (112 as u8) { // probe 166 let n: i64 = vfc_read_tail(argv[2] as *u8, tail, sizebox) 167 if n < 0 { vfc_e("ERROR: cannot open file\n" as *u8); sys_exit(1); return 1 } 168 vf_probe(tail, n, sizebox[0], facts) 169 vfc_license(argv[2] as *u8, facts, lic0) 170 vfc_print(facts, lic0) 171 if facts[0] == 0 { sys_exit(4); return 4 } 172 sys_exit(0) 173 return 0 174 } 175 176 if argc < 6 { vfc_e("sweep needs <listfile> <source> <provenance> <license> [put]\n" as *u8); sys_exit(2); return 2 } 177 let src: *u8 = argv[3] as *u8 178 let prov: *u8 = argv[4] as *u8 179 let lic: *u8 = argv[5] as *u8 180 var do_put: i64 = 0 181 if argc >= 7 { let pv: *u8 = argv[6] as *u8; if pv[0] == (112 as u8) { do_put = 1 } } 182 183 let lp: *i64 = sys_mmap(16) as *i64 184 let lst: *u8 = sys_read_file(argv[2] as *u8, lp) 185 if (lst as i64) == 0 { vfc_e("ERROR: cannot read listfile\n" as *u8); sys_exit(1); return 1 } 186 let ln: i64 = lp[0] 187 188 let s_ent: *i64 = sys_mmap(VFC_MAX_FILES * 8) as *i64 189 let s_mor: *i64 = sys_mmap(VFC_MAX_FILES * 8) as *i64 190 let s_tex: *i64 = sys_mmap(VFC_MAX_FILES * 8) as *i64 191 let s_scn: *i64 = sys_mmap(VFC_MAX_FILES * 8) as *i64 192 var cnt: i64 = 0 193 var meta_ok: i64 = 0 194 var refused: i64 = 0 195 var skipped: i64 = 0 196 197 let path: *u8 = sys_mmap(VFC_PATH_MAX + 8) 198 var pos: i64 = 0 199 while pos < ln { 200 var pl: i64 = 0 201 var eol: i64 = 0 202 while eol == 0 { 203 if pos >= ln { eol = 1 } else { 204 let c: i64 = lst[pos] as i64 & 0xff 205 pos = pos + 1 206 if c == 10 { eol = 1 } else { 207 if c != 13 { if pl < VFC_PATH_MAX { path[pl] = c as u8; pl = pl + 1 } } 208 } 209 } 210 } 211 path[pl] = 0 as u8 212 if pl > 0 { if cnt < VFC_MAX_FILES { 213 let n2: i64 = vfc_read_tail(path, tail, sizebox) 214 if n2 <= 0 { skipped = skipped + 1 } else { 215 vf_probe(tail, n2, sizebox[0], facts) 216 if facts[0] == 1 { 217 s_ent[cnt] = facts[1] 218 s_mor[cnt] = facts[2] 219 s_tex[cnt] = facts[4] 220 s_scn[cnt] = facts[3] 221 cnt = cnt + 1 222 if facts[9] == 1 { meta_ok = meta_ok + 1 } 223 } else { refused = refused + 1 } 224 } 225 } } 226 } 227 228 vfc_w("VARFACTS-SWEEP parsed=" as *u8); vfc_num(cnt) 229 vfc_w(" refused=" as *u8); vfc_num(refused) 230 vfc_w(" unreadable=" as *u8); vfc_num(skipped); vfc_w("\n" as *u8) 231 232 let wrote: *i64 = sys_mmap(16) as *i64 233 wrote[0] = 0 234 if cnt > 0 { 235 vfc_row("var_files_measured" as *u8, cnt, "files" as *u8, cnt, src, prov, lic, do_put, wrote) 236 vfc_row("var_meta_declared" as *u8, meta_ok, "files" as *u8, cnt, src, prov, lic, do_put, wrote) 237 vfc_mmm("var_entries" as *u8, s_ent, cnt, "entries" as *u8, src, prov, lic, do_put, wrote) 238 vfc_mmm("var_morphs" as *u8, s_mor, cnt, "morphs" as *u8, src, prov, lic, do_put, wrote) 239 vfc_mmm("var_textures" as *u8, s_tex, cnt, "textures" as *u8, src, prov, lic, do_put, wrote) 240 vfc_mmm("var_scenes" as *u8, s_scn, cnt, "scenes" as *u8, src, prov, lic, do_put, wrote) 241 } 242 243 if do_put == 1 { 244 vfc_w("VARFACTS-PUT rows_admitted=" as *u8); vfc_num(wrote[0]); vfc_w("\n" as *u8) 245 if wrote[0] == 0 { vfc_e("VARFACTS-REFUSED-ALL nothing met the admission walls\n" as *u8); sys_exit(3); return 3 } 246 } 247 sys_exit(0) 248 return 0 249}