code wiki / _hdl_build / nx_model_get_lib.nx

nx_model_get_lib.nx source

↩ module page · 155 lines · 5486 B

1// nx_model_get_lib.nx -- LIB: the pure decision half of G9 mg_fetch (model acquisition). Everything here is 2// offline-testable by nx_model_get_gate; the network half lives in runtime/nx_model_get.nx and COMPOSES the 3// incumbents (nx_https_fetch_range windows, nx_sha256 streaming via the fetch unit, wp_hexenc/wp_streq from 4// nx_weights_pull). This lib deliberately re-implements NO transport and NO crypto: it parses, validates and 5// decides. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8func mg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 9func mg_lc(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c } 10func mg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i]; o=o+1; i=i+1} return o } 11 12// normalize a caller-supplied sha256 into out (lowercased, NUL-terminated). 13// 1 = exactly 64 hex chars; 0 = anything else (wrong length, non-hex) -- the caller REFUSES on 0. 14func mg_hex_norm(s: *u8, out: *u8) -> i64 { 15 var i: i64=0 16 var ok: i64=1 17 while s[i]!=(0 as u8) { 18 if i>=64 { ok=0; out[0]=0 as u8; return 0 } 19 let c: i64 = mg_lc(s[i] as i64) 20 var hex: i64=0 21 if c>=48 { if c<=57 { hex=1 } } 22 if c>=97 { if c<=102 { hex=1 } } 23 if hex==0 { ok=0 } 24 out[i]=c as u8 25 i=i+1 26 } 27 out[i]=0 as u8 28 if i!=64 { return 0 } 29 return ok 30} 31 32// dest-name admission: 1 = safe relative name (may contain subdirs like diffusion/x.gguf), 0 = REFUSED. 33// Refuses: empty, leading '/', backslash anywhere, any ".." run (path escape). 34func mg_dest_ok(name: *u8) -> i64 { 35 if name[0]==(0 as u8) { return 0 } 36 if name[0]==(47 as u8) { return 0 } 37 var i: i64=0 38 while name[i]!=(0 as u8) { 39 if name[i]==(92 as u8) { return 0 } 40 if name[i]==(46 as u8) { if name[i+1]==(46 as u8) { return 0 } } 41 i=i+1 42 } 43 return 1 44} 45 46func mg_join(root: *u8, name: *u8, out: *u8) -> i64 { 47 var o: i64 = mg_cat(out, 0, root) 48 if o>0 { if out[o-1]!=(47 as u8) { out[o]=47 as u8; o=o+1 } } 49 o = mg_cat(out, o, name) 50 out[o]=0 as u8 51 return o 52} 53 54// case-insensitive find of NUL-terminated needle in buf[0..n); start index or -1. 55func mg_find_ci(buf: *u8, n: i64, needle: *u8) -> i64 { 56 let m: i64 = mg_slen(needle) 57 if m==0 { return 0-1 } 58 var i: i64=0 59 while i+m<=n { 60 var j: i64=0 61 var ok: i64=1 62 while j<m { if mg_lc(buf[i+j] as i64)!=mg_lc(needle[j] as i64) { ok=0; j=m } else { j=j+1 } } 63 if ok==1 { return i } 64 i=i+1 65 } 66 return 0-1 67} 68 69func mg_parse_uint(buf: *u8, n: i64, pos: i64) -> i64 { 70 var v: i64=0 71 var i: i64=pos 72 var any: i64=0 73 var go: i64=1 74 while go==1 { 75 if i>=n { go=0 } else { 76 let c: i64 = buf[i] as i64 77 if c>=48 { if c<=57 { v=v*10+(c-48); any=1; i=i+1 } else { go=0 } } else { go=0 } 78 } 79 } 80 if any==0 { return 0-1 } 81 return v 82} 83 84// total size from a 206 header block: "Content-Range: bytes S-E/TOTAL". -1 if absent/unparseable. 85func mg_content_range_total(hdr: *u8, n: i64) -> i64 { 86 let p: i64 = mg_find_ci(hdr, n, "content-range:" as *u8) 87 if p<0 { return 0-1 } 88 var i: i64=p 89 var slash: i64=0-1 90 var go: i64=1 91 while go==1 { 92 if i>=n { go=0 } else { 93 let c: i64 = hdr[i] as i64 94 if c==10 { go=0 } else { if c==47 { slash=i; go=0 } else { i=i+1 } } 95 } 96 } 97 if slash<0 { return 0-1 } 98 return mg_parse_uint(hdr, n, slash+1) 99} 100 101// Content-Length from a header block; -1 if absent. Matched at line start only. 102func mg_content_length(hdr: *u8, n: i64) -> i64 { 103 var i: i64=0 104 while i<n { 105 var atline: i64=0 106 if i==0 { atline=1 } else { if hdr[i-1]==(10 as u8) { atline=1 } } 107 if atline==1 { 108 var j: i64=0 109 let pat: *u8 = "content-length:" as *u8 110 var ok: i64=1 111 while j<15 { if i+j>=n { ok=0; j=15 } else { if mg_lc(hdr[i+j] as i64)!=mg_lc(pat[j] as i64) { ok=0; j=15 } else { j=j+1 } } } 112 if ok==1 { 113 var p: i64=i+15 114 while p<n { if hdr[p]==(32 as u8) { p=p+1 } else { return mg_parse_uint(hdr, n, p) } } 115 return 0-1 116 } 117 } 118 i=i+1 119 } 120 return 0-1 121} 122 123// conf value: find "<key>=" at a line start in conf[0..n); copy value (to end-of-line) into out. len or 0. 124func mg_conf_val(conf: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64 { 125 let kl: i64 = mg_slen(key) 126 var i: i64=0 127 while i<n { 128 var atline: i64=0 129 if i==0 { atline=1 } else { if conf[i-1]==(10 as u8) { atline=1 } } 130 if atline==1 { 131 var j: i64=0 132 var ok: i64=1 133 while j<kl { if i+j>=n { ok=0; j=kl } else { if conf[i+j]!=key[j] { ok=0; j=kl } else { j=j+1 } } } 134 if ok==1 { if i+kl<n { if conf[i+kl]==(61 as u8) { 135 var p: i64=i+kl+1 136 var o: i64=0 137 var go: i64=1 138 while go==1 { 139 if p>=n { go=0 } else { 140 let c: i64 = conf[p] as i64 141 if c==10 { go=0 } else { if c==13 { go=0 } else { 142 if o<cap-1 { out[o]=conf[p]; o=o+1 } 143 p=p+1 144 } } 145 } 146 } 147 out[o]=0 as u8 148 return o 149 } } } 150 } 151 i=i+1 152 } 153 out[0]=0 as u8 154 return 0 155}