nx_refintake_lib.nx source
↩ module page · 739 lines · 29835 B
1// nx_refintake_lib.nx -- REFERENCE INTAKE AS AN ESTATE CAPABILITY (aesthetictwin AT22 rp_reference_intake, 2026-09-16).
2// Operator: "build the collection into the nishi estate and let it do this via its capabilities" and, the same day, "build all
3// of these fetch capabilities in the nishi estate". A declared subject (knowledge/refintake.conf, DATA) yields mirrored pages,
4// discovered media links (patterns in knowledge/refintake.hosts, DATA: babepedia, Bing image search, Wikimedia Commons), mirrored
5// media with a licence state beside every byte, an image-magic verdict on every mirror (a bot-challenge stub can never be journaled
6// as a picture), and a per-subject journal. This lib is the decision core; nx_refintake is the CLI (pages, discover, mirror,
7// verify, and the all workflow) and nx_refintake_gate proves it on fixtures, never on the production conf and never on the network.
8// STANDING RULE (operator, repeated): references are usable through BETA -- fitting, testing, proposals -- and only the
9// production publish is gated on the licence row. licence: REVIEW beta-usable | SHIP_OK licensed | REFUSED never.
10// The intake measures on DECLARED MORPHOLOGY AXES only. A tag whose axis is not declared is refused BY NAME; the
11// intake carries no ethnic, racial or religious label of any person, because those are not measurable axes.
12// Composes: nx_research_fetch (mirror + PROVENANCE line), nx_tool_run (fork and capture). No hw writes (Rule 26).
13// license_tier: ORIGINAL
14import "nx_syscalls.nx"
15import "nx_tool_run.nx"
16
17const RI_CONF: *u8 = "knowledge/refintake.conf"
18const RI_HOSTS: *u8 = "knowledge/refintake.hosts"
19const RI_DIR: *u8 = "knowledge/refintake"
20const RI_FETCHED: *u8 = "knowledge/fetched/"
21const RI_FETCHER_OFFC: *u8 = "_offc/nx_research_fetch.elf"
22const RI_FETCHER_HERE: *u8 = "./nx_research_fetch.elf"
23const RI_FETCHER_UP: *u8 = "../nx_research_fetch.elf"
24const RI_AXES: *u8 = " malar_projection bizyg_bigon midface_height canthal_tilt palpebral_fissure vthird_upper vthird_mid vthird_lower facial_index intercanthal nasal_index lip_ratio mandibular_angle whr bust_waist frame hair_length hair_curl hair_colour skin_undertone skin_tan age_at_photo "
25const RI_HTTPS_PREFIX: *u8 = "https:"
26const RI_NL: i64 = 10
27const RI_SP: i64 = 32
28const RI_QUOTE: i64 = 34
29const RI_AMP: i64 = 38
30const RI_PIPE: i64 = 124
31const RI_PCT: i64 = 37
32const RI_DASH: i64 = 45
33const RI_DOT: i64 = 46
34const RI_SLASH: i64 = 47
35const RI_LT: i64 = 60
36const RI_DIGIT0: i64 = 48
37const RI_DIGIT9: i64 = 57
38const RI_UNDERSCORE: i64 = 95
39const RI_LC_G: i64 = 103
40const RI_LC_N: i64 = 110
41const RI_LC_P: i64 = 112
42const RI_MODE644: i64 = 420
43const RI_MODE755: i64 = 493
44const RI_PATHB: i64 = 1024
45const RI_ROWB: i64 = 4096
46const RI_NUMB: i64 = 24
47const RI_CAPB: i64 = 262144
48const RI_I64: i64 = 8
49const RI_ARGV_N: i64 = 4
50const RI_SHA_HEX: i64 = 64
51const RI_PROV_PREFIX_LEN: i64 = 11
52const RI_HREF_LEN: i64 = 6
53const RI_MAGIC_N: i64 = 16
54const RI_OK: i64 = 0
55const RI_E_CONF: i64 = 0 - 3
56const RI_E_NOSUBJECT: i64 = 0 - 4
57const RI_E_REFUSED: i64 = 0 - 5
58const RI_E_AXIS: i64 = 0 - 6
59const RI_E_FETCH: i64 = 0 - 7
60const RI_E_NOPROV: i64 = 0 - 8
61const RI_STDOUT: i64 = 1
62const RI_F_SLUG: i64 = 1
63const RI_SUBJ_LIC: i64 = 3
64const RI_SRC_KIND: i64 = 2
65const RI_SRC_URL: i64 = 3
66const RI_SRC_MIRROR: i64 = 4
67const RI_SRC_DATE: i64 = 5
68const RI_TAG_AXIS: i64 = 2
69const RI_PROV_SHA: i64 = 4
70const RI_PROV_BYTES: i64 = 5
71// host pattern rows: host|<name>|<url prefix>|<needle>|<skip>|<terminator>|<base>|<note>
72const RI_F_HOST_PREFIX: i64 = 2
73const RI_F_HOST_NEEDLE: i64 = 3
74const RI_F_HOST_SKIP: i64 = 4
75const RI_F_HOST_TERM: i64 = 5
76const RI_F_HOST_BASE: i64 = 6
77// image classes by magic bytes
78const RI_IMG_UNREADABLE: i64 = 0
79const RI_IMG_JPEG: i64 = 1
80const RI_IMG_PNG: i64 = 2
81const RI_IMG_GIF: i64 = 3
82const RI_IMG_WEBP: i64 = 4
83const RI_IMG_HTML: i64 = 5
84const RI_IMG_OTHER: i64 = 6
85const RI_IMG_MP4: i64 = 7
86const RI_IMG_N: i64 = 8
87// journal rows: mirror|epoch|slug|url|path|sha|bytes|licence|status
88const RI_J_URL: i64 = 3
89const RI_J_PATH: i64 = 4
90const RI_J_STATUS: i64 = 8
91
92func ri_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
93func ri_streq(a: *u8, b: *u8) -> i64 {
94 var i: i64 = 0
95 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
96 if b[i] != (0 as u8) { return 0 }
97 return 1
98}
99func ri_cat(d: *u8, o: i64, s: *u8) -> i64 {
100 var i: i64 = 0
101 var p: i64 = o
102 while s[i] != (0 as u8) { d[p] = s[i]; p = p + 1; i = i + 1 }
103 d[p] = 0 as u8
104 return p
105}
106func ri_catn(d: *u8, o: i64, v: i64) -> i64 {
107 let b: *u8 = sys_mmap(RI_NUMB)
108 var x: i64 = v
109 var neg: i64 = 0
110 if x < 0 { neg = 1; x = 0 - x }
111 var k: i64 = RI_NUMB - 1
112 b[k] = 0 as u8
113 k = k - 1
114 if x == 0 { b[k] = RI_DIGIT0 as u8; k = k - 1 }
115 while x > 0 { b[k] = (x - (x / 10) * 10 + RI_DIGIT0) as u8; k = k - 1; x = x / 10 }
116 if neg == 1 { b[k] = RI_DASH as u8; k = k - 1 }
117 return ri_cat(d, o, (b as i64 + k + 1) as *u8)
118}
119func ri_puts(s: *u8) -> i64 { return sys_write(RI_STDOUT, s, ri_slen(s)) }
120func ri_putn(v: i64) -> i64 { let b: *u8 = sys_mmap(RI_NUMB); ri_catn(b, 0, v); return ri_puts(b) }
121func ri_atoi(s: *u8) -> i64 {
122 var i: i64 = 0
123 var v: i64 = 0
124 var go: i64 = 1
125 while go == 1 {
126 let c: i64 = s[i] as i64
127 if c < RI_DIGIT0 { go = 0 } else {
128 if c > RI_DIGIT9 { go = 0 } else { v = v * 10 + (c - RI_DIGIT0); i = i + 1 }
129 }
130 }
131 return v
132}
133// the byte index of the newline that ends the line starting at i, or n
134func ri_line_end(buf: *u8, n: i64, i: i64) -> i64 {
135 var p: i64 = i
136 while p < n { if buf[p] == (RI_NL as u8) { return p } p = p + 1 }
137 return n
138}
139func ri_starts(buf: *u8, n: i64, i: i64, lit: *u8) -> i64 {
140 var k: i64 = 0
141 while lit[k] != (0 as u8) {
142 if i + k >= n { return 0 }
143 if buf[i + k] != lit[k] { return 0 }
144 k = k + 1
145 }
146 return 1
147}
148// is lit a prefix of s? (both NUL-terminated)
149func ri_prefix(s: *u8, lit: *u8) -> i64 {
150 var k: i64 = 0
151 while lit[k] != (0 as u8) {
152 if s[k] != lit[k] { return 0 }
153 k = k + 1
154 }
155 return 1
156}
157// copy pipe-field <idx> of the row at [i, e) into out (NUL-terminated); returns the field length, or -1 when absent
158func ri_field(buf: *u8, i: i64, e: i64, idx: i64, out: *u8, cap: i64) -> i64 {
159 var p: i64 = i
160 var f: i64 = 0
161 while f < idx {
162 if p >= e { out[0] = 0 as u8; return 0 - 1 }
163 if buf[p] == (RI_PIPE as u8) { f = f + 1 }
164 p = p + 1
165 }
166 var o: i64 = 0
167 var go: i64 = 1
168 while go == 1 {
169 if p >= e { go = 0 } else {
170 if buf[p] == (RI_PIPE as u8) { go = 0 } else {
171 if o < cap - 1 { out[o] = buf[p]; o = o + 1 }
172 p = p + 1
173 }
174 }
175 }
176 out[o] = 0 as u8
177 return o
178}
179// read a whole file; lenp[0] <= 0 means unreadable or empty
180func ri_load(path: *u8, lenp: *i64) -> *u8 {
181 lenp[0] = 0
182 let b: *u8 = sys_read_file(path, lenp)
183 return b
184}
185// find the subject row of <slug>; on hit copy its licence into lic and return 1
186func ri_subject_licence(buf: *u8, n: i64, slug: *u8, lic: *u8) -> i64 {
187 var i: i64 = 0
188 let f: *u8 = sys_mmap(RI_PATHB)
189 while i < n {
190 let e: i64 = ri_line_end(buf, n, i)
191 if ri_starts(buf, n, i, "subject|" as *u8) == 1 {
192 ri_field(buf, i, e, RI_F_SLUG, f, RI_PATHB)
193 if ri_streq(f, slug) == 1 {
194 ri_field(buf, i, e, RI_SUBJ_LIC, lic, RI_PATHB)
195 return 1
196 }
197 }
198 i = e + 1
199 }
200 lic[0] = 0 as u8
201 return 0
202}
203// count rows of <kindword> ("source|" or "tag|") for <slug>; srckind filters the source kind ("page", "image") or 0 for all
204func ri_count_rows(buf: *u8, n: i64, kindword: *u8, slug: *u8, srckind: *u8) -> i64 {
205 var i: i64 = 0
206 var c: i64 = 0
207 let f: *u8 = sys_mmap(RI_PATHB)
208 let k: *u8 = sys_mmap(RI_PATHB)
209 while i < n {
210 let e: i64 = ri_line_end(buf, n, i)
211 if ri_starts(buf, n, i, kindword) == 1 {
212 ri_field(buf, i, e, RI_F_SLUG, f, RI_PATHB)
213 if ri_streq(f, slug) == 1 {
214 if srckind as i64 == 0 { c = c + 1 } else {
215 ri_field(buf, i, e, RI_SRC_KIND, k, RI_PATHB)
216 if ri_streq(k, srckind) == 1 { c = c + 1 }
217 }
218 }
219 }
220 i = e + 1
221 }
222 return c
223}
224// the idx-th (0-based) source row of <slug>: copies kind, url, mirror, date; returns 1 on hit
225func ri_source_at(buf: *u8, n: i64, slug: *u8, idx: i64, kind: *u8, url: *u8, mirror: *u8, date: *u8) -> i64 {
226 var i: i64 = 0
227 var seen: i64 = 0
228 let f: *u8 = sys_mmap(RI_PATHB)
229 while i < n {
230 let e: i64 = ri_line_end(buf, n, i)
231 if ri_starts(buf, n, i, "source|" as *u8) == 1 {
232 ri_field(buf, i, e, RI_F_SLUG, f, RI_PATHB)
233 if ri_streq(f, slug) == 1 {
234 if seen == idx {
235 ri_field(buf, i, e, RI_SRC_KIND, kind, RI_PATHB)
236 ri_field(buf, i, e, RI_SRC_URL, url, RI_ROWB)
237 ri_field(buf, i, e, RI_SRC_MIRROR, mirror, RI_PATHB)
238 ri_field(buf, i, e, RI_SRC_DATE, date, RI_PATHB)
239 return 1
240 }
241 seen = seen + 1
242 }
243 }
244 i = e + 1
245 }
246 return 0
247}
248// is <axis> one of the declared morphology axes? (whole-word match inside the space-delimited list)
249func ri_axis_declared(axis: *u8) -> i64 {
250 let w: *u8 = sys_mmap(RI_PATHB)
251 var o: i64 = 0
252 w[o] = RI_SP as u8; o = o + 1
253 o = ri_cat(w, o, axis)
254 w[o] = RI_SP as u8; o = o + 1
255 w[o] = 0 as u8
256 let hay: *u8 = RI_AXES
257 let hn: i64 = ri_slen(hay)
258 var i: i64 = 0
259 while i + o <= hn {
260 var k: i64 = 0
261 var hit: i64 = 1
262 while k < o { if hay[i + k] != w[k] { hit = 0; k = o } else { k = k + 1 } }
263 if hit == 1 { return 1 }
264 i = i + 1
265 }
266 return 0
267}
268// every tag| row must name a declared axis. Returns the number of REFUSED tags; the first offending axis is copied to bad
269func ri_tags_check(buf: *u8, n: i64, bad: *u8) -> i64 {
270 var i: i64 = 0
271 var badc: i64 = 0
272 let ax: *u8 = sys_mmap(RI_PATHB)
273 bad[0] = 0 as u8
274 while i < n {
275 let e: i64 = ri_line_end(buf, n, i)
276 if ri_starts(buf, n, i, "tag|" as *u8) == 1 {
277 ri_field(buf, i, e, RI_TAG_AXIS, ax, RI_PATHB)
278 if ri_axis_declared(ax) == 0 {
279 if badc == 0 { ri_cat(bad, 0, ax) }
280 badc = badc + 1
281 }
282 }
283 i = e + 1
284 }
285 return badc
286}
287// does the NUL-terminated line <s> already occur as a whole line in buf?
288func ri_has_line(buf: *u8, n: i64, s: *u8) -> i64 {
289 let sl: i64 = ri_slen(s)
290 var i: i64 = 0
291 while i < n {
292 let e: i64 = ri_line_end(buf, n, i)
293 if e - i == sl {
294 var k: i64 = 0
295 var hit: i64 = 1
296 while k < sl { if buf[i + k] != s[k] { hit = 0; k = sl } else { k = k + 1 } }
297 if hit == 1 { return 1 }
298 }
299 i = e + 1
300 }
301 return 0
302}
303// does buf contain the NUL-terminated needle anywhere?
304func ri_contains(buf: *u8, n: i64, s: *u8) -> i64 {
305 let sl: i64 = ri_slen(s)
306 if sl == 0 { return 0 }
307 var i: i64 = 0
308 while i + sl <= n {
309 var k: i64 = 0
310 var hit: i64 = 1
311 while k < sl { if buf[i + k] != s[k] { hit = 0; k = sl } else { k = k + 1 } }
312 if hit == 1 { return 1 }
313 i = i + 1
314 }
315 return 0
316}
317// the two babepedia link patterns, built at runtime because the lexer owns the quote byte: href="/pics/ and href="/user-uploads/
318func ri_pattern(out: *u8, tail: *u8) -> i64 {
319 var o: i64 = ri_cat(out, 0, "href=" as *u8)
320 out[o] = RI_QUOTE as u8; o = o + 1
321 o = ri_cat(out, o, tail)
322 return o
323}
324// DISCOVER links on a mirrored page with ONE pattern: at every occurrence of <needle> the link starts <skip> bytes in and ends at
325// <term>; a link that is neither http... nor //... gets <base> in front (unless base is -); //... gets https:; & is decoded;
326// spaces are percent-encoded; links containing _thumb are skipped; duplicates against what is already in out are dropped.
327// Each hit becomes one line "<url>\n" appended to out. Returns the NEW urls written.
328func ri_discover_pat(page: *u8, n: i64, needle: *u8, skip: i64, term: *u8, base: *u8, out: *u8, cap: i64, outlen: *i64) -> i64 {
329 var i: i64 = 0
330 var found: i64 = 0
331 let one: *u8 = sys_mmap(RI_ROWB)
332 let raw: *u8 = sys_mmap(RI_ROWB)
333 let nl: i64 = ri_slen(needle)
334 if nl == 0 { return 0 }
335 while i < n {
336 if ri_starts(page, n, i, needle) == 1 {
337 var p: i64 = i + skip
338 var r: i64 = 0
339 var ok: i64 = 1
340 var go: i64 = 1
341 while go == 1 {
342 if p >= n { go = 0 } else {
343 if ri_starts(page, n, p, term) == 1 { go = 0 } else {
344 if r >= RI_ROWB - 8 { ok = 0; go = 0 } else {
345 let c: i64 = page[p] as i64
346 if c == RI_NL { ok = 0; go = 0 } else {
347 if ri_starts(page, n, p, "&" as *u8) == 1 { raw[r] = RI_AMP as u8; r = r + 1; p = p + 5 }
348 else { raw[r] = c as u8; r = r + 1; p = p + 1 }
349 }
350 }
351 }
352 }
353 }
354 raw[r] = 0 as u8
355 if r == 0 { ok = 0 }
356 if ok == 1 {
357 var o: i64 = 0
358 if ri_prefix(raw, "http" as *u8) == 1 { o = 0 } else {
359 if raw[0] == (RI_SLASH as u8) { if raw[1] == (RI_SLASH as u8) { o = ri_cat(one, 0, RI_HTTPS_PREFIX) } else { if ri_streq(base, "-" as *u8) == 0 { o = ri_cat(one, 0, base) } } }
360 else { if ri_streq(base, "-" as *u8) == 0 { o = ri_cat(one, 0, base) } }
361 }
362 var q: i64 = 0
363 while q < r {
364 let c2: i64 = raw[q] as i64
365 if c2 == RI_SP { one[o] = RI_PCT as u8; one[o + 1] = (RI_DIGIT0 + 2) as u8; one[o + 2] = RI_DIGIT0 as u8; o = o + 3 }
366 else { one[o] = c2 as u8; o = o + 1 }
367 q = q + 1
368 }
369 one[o] = 0 as u8
370 if ri_contains(one, o, "_thumb" as *u8) == 1 { ok = 0 }
371 if ok == 1 {
372 if ri_has_line(out, outlen[0], one) == 0 {
373 if outlen[0] + o + 2 < cap {
374 var w: i64 = outlen[0]
375 w = ri_cat(out, w, one)
376 out[w] = RI_NL as u8; w = w + 1
377 outlen[0] = w
378 found = found + 1
379 }
380 }
381 }
382 }
383 i = i + nl
384 } else { i = i + 1 }
385 }
386 return found
387}
388// DISCOVER full-size image links on a mirrored babepedia gallery page (the first host pattern, kept as the named composition of
389// ri_discover_pat so the original teeth still hold): href="/pics/ and href="/user-uploads/ links, host in front
390func ri_discover(page: *u8, n: i64, host: *u8, out: *u8, cap: i64, outlen: *i64) -> i64 {
391 let pat1: *u8 = sys_mmap(RI_PATHB)
392 let pat2: *u8 = sys_mmap(RI_PATHB)
393 let term: *u8 = sys_mmap(RI_NUMB)
394 ri_pattern(pat1, "/pics/" as *u8)
395 ri_pattern(pat2, "/user-uploads/" as *u8)
396 term[0] = RI_QUOTE as u8
397 term[1] = 0 as u8
398 var found: i64 = ri_discover_pat(page, n, pat1, RI_HREF_LEN, term, host, out, cap, outlen)
399 found = found + ri_discover_pat(page, n, pat2, RI_HREF_LEN, term, host, out, cap, outlen)
400 return found
401}
402// the idx-th host row (0-based, among rows whose url prefix matches <url>): copies needle, skip, terminator and base; 1 on hit
403func ri_host_at(hosts: *u8, hn: i64, url: *u8, idx: i64, needle: *u8, skipp: *i64, term: *u8, base: *u8) -> i64 {
404 var i: i64 = 0
405 var seen: i64 = 0
406 let pre: *u8 = sys_mmap(RI_ROWB)
407 let f: *u8 = sys_mmap(RI_PATHB)
408 while i < hn {
409 let e: i64 = ri_line_end(hosts, hn, i)
410 if ri_starts(hosts, hn, i, "host|" as *u8) == 1 {
411 ri_field(hosts, i, e, RI_F_HOST_PREFIX, pre, RI_ROWB)
412 if ri_prefix(url, pre) == 1 {
413 if seen == idx {
414 ri_field(hosts, i, e, RI_F_HOST_NEEDLE, needle, RI_PATHB)
415 ri_field(hosts, i, e, RI_F_HOST_SKIP, f, RI_PATHB)
416 skipp[0] = ri_atoi(f)
417 ri_field(hosts, i, e, RI_F_HOST_TERM, term, RI_PATHB)
418 ri_field(hosts, i, e, RI_F_HOST_BASE, base, RI_PATHB)
419 return 1
420 }
421 seen = seen + 1
422 }
423 }
424 i = e + 1
425 }
426 return 0
427}
428// IMAGE CLASS by magic bytes: a fetched "image" that is really an HTML challenge page must never be journaled as a picture
429func ri_image_class(path: *u8) -> i64 {
430 let fd: i64 = sys_openat_rd(path)
431 if fd < 0 { return RI_IMG_UNREADABLE }
432 let m: *u8 = sys_mmap(RI_MAGIC_N + 1)
433 let got: i64 = sys_read(fd, m, RI_MAGIC_N)
434 sys_close(fd)
435 if got < 4 { return RI_IMG_UNREADABLE }
436 if m[0] == (255 as u8) { if m[1] == (216 as u8) { if m[2] == (255 as u8) { return RI_IMG_JPEG } } }
437 if m[0] == (137 as u8) { if m[1] == (80 as u8) { if m[2] == (78 as u8) { if m[3] == (71 as u8) { return RI_IMG_PNG } } } }
438 if m[0] == (71 as u8) { if m[1] == (73 as u8) { if m[2] == (70 as u8) { if m[3] == (56 as u8) { return RI_IMG_GIF } } } }
439 if got >= 12 { if m[0] == (82 as u8) { if m[1] == (73 as u8) { if m[2] == (70 as u8) { if m[3] == (70 as u8) { if m[8] == (87 as u8) { if m[9] == (69 as u8) { if m[10] == (66 as u8) { if m[11] == (80 as u8) { return RI_IMG_WEBP } } } } } } } } }
440 if got >= 12 { if m[4] == (102 as u8) { if m[5] == (116 as u8) { if m[6] == (121 as u8) { if m[7] == (112 as u8) { return RI_IMG_MP4 } } } } }
441 var k: i64 = 0
442 while k < got {
443 let c: i64 = m[k] as i64
444 if c == RI_LT { return RI_IMG_HTML }
445 if c > RI_SP { k = got } else { k = k + 1 }
446 }
447 return RI_IMG_OTHER
448}
449func ri_class_word(c: i64) -> *u8 {
450 if c == RI_IMG_JPEG { return "JPEG" as *u8 }
451 if c == RI_IMG_PNG { return "PNG" as *u8 }
452 if c == RI_IMG_GIF { return "GIF" as *u8 }
453 if c == RI_IMG_WEBP { return "WEBP" as *u8 }
454 if c == RI_IMG_HTML { return "HTML-STUB" as *u8 }
455 if c == RI_IMG_OTHER { return "OTHER" as *u8 }
456 if c == RI_IMG_MP4 { return "MP4" as *u8 }
457 return "UNREADABLE" as *u8
458}
459func ri_is_video(c: i64) -> i64 {
460 if c == RI_IMG_MP4 { return 1 }
461 return 0
462}
463func ri_is_image(c: i64) -> i64 {
464 if c == RI_IMG_JPEG { return 1 }
465 if c == RI_IMG_PNG { return 1 }
466 if c == RI_IMG_GIF { return 1 }
467 if c == RI_IMG_WEBP { return 1 }
468 return 0
469}
470// parse a fetch receipt (the fetcher's stdout) for its PROVENANCE row: copies the sha (64 hex) and returns the byte count,
471// or RI_E_NOPROV when the receipt carries no provenance row -- the mirror leg never recomputes a digest itself
472func ri_prov_parse(rcpt: *u8, n: i64, sha: *u8) -> i64 {
473 var i: i64 = 0
474 let f: *u8 = sys_mmap(RI_PATHB)
475 sha[0] = 0 as u8
476 while i < n {
477 let e: i64 = ri_line_end(rcpt, n, i)
478 if ri_starts(rcpt, n, i, "PROVENANCE prov|" as *u8) == 1 {
479 let r: i64 = i + RI_PROV_PREFIX_LEN
480 ri_field(rcpt, r, e, RI_PROV_SHA, sha, RI_PATHB)
481 ri_field(rcpt, r, e, RI_PROV_BYTES, f, RI_PATHB)
482 if ri_slen(sha) != RI_SHA_HEX { return RI_E_NOPROV }
483 return ri_atoi(f)
484 }
485 i = e + 1
486 }
487 return RI_E_NOPROV
488}
489// the fetcher, from whichever root serves it (the job lane's CWD decides which spelling resolves)
490func ri_fetcher(out: *u8) -> i64 {
491 var fd: i64 = sys_openat_rd(RI_FETCHER_OFFC)
492 if fd >= 0 { sys_close(fd); ri_cat(out, 0, RI_FETCHER_OFFC); return 1 }
493 fd = sys_openat_rd(RI_FETCHER_HERE)
494 if fd >= 0 { sys_close(fd); ri_cat(out, 0, RI_FETCHER_HERE); return 1 }
495 fd = sys_openat_rd(RI_FETCHER_UP)
496 if fd >= 0 { sys_close(fd); ri_cat(out, 0, RI_FETCHER_UP); return 1 }
497 out[0] = 0 as u8
498 return 0
499}
500// MIRROR one url through nx_research_fetch; sha and bytes come from ITS provenance row
501func ri_mirror_one(url: *u8, mirror: *u8, sha: *u8, bytesp: *i64) -> i64 {
502 let elf: *u8 = sys_mmap(RI_PATHB)
503 if ri_fetcher(elf) == 0 { bytesp[0] = 0; return RI_E_FETCH }
504 let av: *i64 = sys_mmap(RI_ARGV_N * RI_I64) as *i64
505 av[0] = elf as i64
506 av[1] = url as i64
507 av[2] = mirror as i64
508 av[3] = 0
509 let capb: *u8 = sys_mmap(RI_CAPB)
510 let cl: *i64 = sys_mmap(RI_I64) as *i64
511 cl[0] = 0
512 let rc: i64 = tr_run_capture(elf, av, capb, RI_CAPB - 1, cl)
513 let b: i64 = ri_prov_parse(capb, cl[0], sha)
514 if b < 0 { bytesp[0] = 0; if rc != 0 { return RI_E_FETCH } return RI_E_NOPROV }
515 bytesp[0] = b
516 return RI_OK
517}
518func ri_journal_path(slug: *u8, out: *u8) -> i64 {
519 var o: i64 = ri_cat(out, 0, RI_DIR)
520 out[o] = RI_SLASH as u8; o = o + 1
521 o = ri_cat(out, o, slug)
522 o = ri_cat(out, o, ".jrnl" as *u8)
523 return o
524}
525func ri_sources_path(slug: *u8, out: *u8) -> i64 {
526 var o: i64 = ri_cat(out, 0, RI_DIR)
527 out[o] = RI_SLASH as u8; o = o + 1
528 o = ri_cat(out, o, slug)
529 o = ri_cat(out, o, ".sources" as *u8)
530 return o
531}
532// append one journal row: mirror|epoch|slug|url|path|sha|bytes|licence|status
533func ri_journal(slug: *u8, url: *u8, mirror: *u8, sha: *u8, bytes: i64, lic: *u8, status: *u8) -> i64 {
534 sys_mkdir(RI_DIR, RI_MODE755)
535 let path: *u8 = sys_mmap(RI_PATHB)
536 ri_journal_path(slug, path)
537 let row: *u8 = sys_mmap(RI_ROWB)
538 var o: i64 = ri_cat(row, 0, "mirror|" as *u8)
539 o = ri_catn(row, o, sys_now_realtime_sec())
540 row[o] = RI_PIPE as u8; o = o + 1
541 o = ri_cat(row, o, slug)
542 row[o] = RI_PIPE as u8; o = o + 1
543 o = ri_cat(row, o, url)
544 row[o] = RI_PIPE as u8; o = o + 1
545 o = ri_cat(row, o, RI_FETCHED)
546 o = ri_cat(row, o, mirror)
547 row[o] = RI_PIPE as u8; o = o + 1
548 o = ri_cat(row, o, sha)
549 row[o] = RI_PIPE as u8; o = o + 1
550 o = ri_catn(row, o, bytes)
551 row[o] = RI_PIPE as u8; o = o + 1
552 o = ri_cat(row, o, lic)
553 row[o] = RI_PIPE as u8; o = o + 1
554 o = ri_cat(row, o, status)
555 row[o] = RI_NL as u8; o = o + 1
556 let fd: i64 = sys_openat_append(path, RI_MODE644)
557 if fd < 0 { return 0 - 1 }
558 let w: i64 = sys_write(fd, row, o)
559 sys_close(fd)
560 return w
561}
562// the mirror file name for the k-th discovered image of a subject: ri_<slug>_<k>.<png when the url ends .png, else jpg>
563func ri_mirror_name(slug: *u8, k: i64, url: *u8, out: *u8) -> i64 {
564 var o: i64 = ri_cat(out, 0, "ri_" as *u8)
565 o = ri_cat(out, o, slug)
566 out[o] = RI_UNDERSCORE as u8; o = o + 1
567 o = ri_catn(out, o, k)
568 let ul: i64 = ri_slen(url)
569 var png: i64 = 0
570 if ul > 4 {
571 if url[ul - 4] == (RI_DOT as u8) { if url[ul - 3] == (RI_LC_P as u8) { if url[ul - 2] == (RI_LC_N as u8) { if url[ul - 1] == (RI_LC_G as u8) { png = 1 } } } }
572 }
573 if png == 1 { o = ri_cat(out, o, ".png" as *u8) } else { o = ri_cat(out, o, ".jpg" as *u8) }
574 return o
575}
576// may this subject be mirrored? RI_OK for REVIEW or SHIP_OK; RI_E_REFUSED for REFUSED or an unknown word; RI_E_NOSUBJECT when undeclared
577func ri_mirror_allowed(buf: *u8, n: i64, slug: *u8) -> i64 {
578 let lic: *u8 = sys_mmap(RI_PATHB)
579 if ri_subject_licence(buf, n, slug, lic) == 0 { return RI_E_NOSUBJECT }
580 if ri_streq(lic, "REFUSED" as *u8) == 1 { return RI_E_REFUSED }
581 if ri_streq(lic, "REVIEW" as *u8) == 1 { return RI_OK }
582 if ri_streq(lic, "SHIP_OK" as *u8) == 1 { return RI_OK }
583 return RI_E_REFUSED
584}
585// KVS PLAYER MEDIA (AT24 video intake, 2026-09-16). A Kernel Video Sharing page carries video_url: 'function/0/<url>' and
586// license_code: '$<digits>'; the 32-hex hash segment of that url is permuted by a token derived from the licence digits. The
587// transform is a public-domain algorithm (the yt-dlp generic extractor, Unlicense) ported here from the first byte; a url
588// without the function/0/ prefix passes through unchanged. The live fetch is the referee: the mirrored bytes must carry the
589// ftyp magic or the row is journaled NOT-VIDEO, never as a clip.
590const RI_KVS_PREFIX: *u8 = "function/0/"
591const RI_KVS_URL_KEY: *u8 = "video_url: '"
592const RI_KVS_CODE_KEY: *u8 = "license_code: '"
593const RI_KVS_HASH_LEN: i64 = 32
594const RI_KVS_OFFSETS: i64 = 4
595const RI_KVS_MOD: i64 = 10
596const RI_KVS_QUAD: i64 = 4
597const RI_KVS_APOS: i64 = 39
598const RI_KVS_COLON: i64 = 58
599const RI_KVS_MAXDIGITS: i64 = 18
600const RI_KVS_HASH_SLASHES: i64 = 3
601const RI_KVS_TOKEN_CAP: i64 = 96
602const RI_KVS_E_CODE: i64 = 0 - 1
603const RI_KVS_E_URL: i64 = 0 - 2
604
605// the licence token from '$<digits>': returns its length (RI_KVS_HASH_LEN or more is needed) or RI_KVS_E_CODE
606func ri_kvs_license_token(code: *u8, out: *i64, cap: i64) -> i64 {
607 let vals: *i64 = sys_mmap(RI_KVS_MAXDIGITS * RI_I64) as *i64
608 var n: i64 = 0
609 var i: i64 = 0
610 while code[i] != (0 as u8) {
611 let c: i64 = code[i] as i64
612 if c >= RI_DIGIT0 { if c <= RI_DIGIT9 { if n >= RI_KVS_MAXDIGITS { return RI_KVS_E_CODE } vals[n] = c - RI_DIGIT0; n = n + 1 } }
613 i = i + 1
614 }
615 if n < 2 { return RI_KVS_E_CODE }
616 let center: i64 = n / 2
617 var front: i64 = 0
618 var k: i64 = 0
619 while k <= center { var d: i64 = vals[k]; if d == 0 { d = 1 } front = front * RI_KVS_MOD + d; k = k + 1 }
620 var back: i64 = 0
621 k = center
622 while k < n { var d2: i64 = vals[k]; if d2 == 0 { d2 = 1 } back = back * RI_KVS_MOD + d2; k = k + 1 }
623 var diff: i64 = front - back
624 if diff < 0 { diff = 0 - diff }
625 diff = diff * RI_KVS_QUAD
626 let dig: *u8 = sys_mmap(RI_NUMB)
627 ri_catn(dig, 0, diff)
628 let dn: i64 = ri_slen(dig)
629 var take: i64 = center + 1
630 if dn < take { take = dn }
631 var t: i64 = 0
632 var idx: i64 = 0
633 while idx < take {
634 let cur: i64 = (dig[idx] as i64) - RI_DIGIT0
635 var off: i64 = 0
636 while off < RI_KVS_OFFSETS {
637 if idx + off >= n { return RI_KVS_E_CODE }
638 if t >= cap { return RI_KVS_E_CODE }
639 let s: i64 = vals[idx + off] + cur
640 out[t] = s - (s / RI_KVS_MOD) * RI_KVS_MOD
641 t = t + 1
642 off = off + 1
643 }
644 idx = idx + 1
645 }
646 return t
647}
648// the real media url: 1 transformed into out, 0 passed through unchanged (no function/0/ prefix), RI_KVS_E_CODE or
649// RI_KVS_E_URL refused (out is then empty)
650func ri_kvs_real_url(vurl: *u8, code: *u8, out: *u8) -> i64 {
651 out[0] = 0 as u8
652 if ri_prefix(vurl, RI_KVS_PREFIX) == 0 { ri_cat(out, 0, vurl); return 0 }
653 let src: *u8 = (vurl as i64 + ri_slen(RI_KVS_PREFIX)) as *u8
654 let sn: i64 = ri_slen(src)
655 var i: i64 = 0
656 var q: i64 = 0 - 1
657 var go: i64 = 1
658 while go == 1 {
659 if i + 2 >= sn { go = 0 } else {
660 if src[i] == (RI_KVS_COLON as u8) { if src[i + 1] == (RI_SLASH as u8) { if src[i + 2] == (RI_SLASH as u8) { q = i + 3; go = 0 } } }
661 i = i + 1
662 }
663 }
664 if q < 0 { return RI_KVS_E_URL }
665 var slashes: i64 = 0
666 var hs: i64 = 0 - 1
667 i = q
668 go = 1
669 while go == 1 {
670 if i >= sn { go = 0 } else {
671 if src[i] == (RI_SLASH as u8) { slashes = slashes + 1; if slashes == RI_KVS_HASH_SLASHES { hs = i + 1; go = 0 } }
672 i = i + 1
673 }
674 }
675 if hs < 0 { return RI_KVS_E_URL }
676 var he: i64 = hs
677 go = 1
678 while go == 1 {
679 if he >= sn { go = 0 } else { if src[he] == (RI_SLASH as u8) { go = 0 } else { he = he + 1 } }
680 }
681 if he - hs < RI_KVS_HASH_LEN { return RI_KVS_E_URL }
682 let tok: *i64 = sys_mmap(RI_KVS_TOKEN_CAP * RI_I64) as *i64
683 let tn: i64 = ri_kvs_license_token(code, tok, RI_KVS_TOKEN_CAP)
684 if tn < RI_KVS_HASH_LEN { return RI_KVS_E_CODE }
685 let idx: *i64 = sys_mmap(RI_KVS_HASH_LEN * RI_I64) as *i64
686 var k: i64 = 0
687 while k < RI_KVS_HASH_LEN { idx[k] = k; k = k + 1 }
688 var accum: i64 = 0
689 var s: i64 = RI_KVS_HASH_LEN - 1
690 while s >= 0 {
691 accum = accum + tok[s]
692 let dest: i64 = (s + accum) - ((s + accum) / RI_KVS_HASH_LEN) * RI_KVS_HASH_LEN
693 let tmp: i64 = idx[s]
694 idx[s] = idx[dest]
695 idx[dest] = tmp
696 s = s - 1
697 }
698 var o: i64 = 0
699 while o < hs { out[o] = src[o]; o = o + 1 }
700 k = 0
701 while k < RI_KVS_HASH_LEN { out[o] = src[hs + idx[k]]; o = o + 1; k = k + 1 }
702 var r: i64 = hs + RI_KVS_HASH_LEN
703 while r < sn { out[o] = src[r]; o = o + 1; r = r + 1 }
704 out[o] = 0 as u8
705 return 1
706}
707// copy the single-quoted value that follows <key> on the page into out; 1 on hit
708func ri_kvs_value(page: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64 {
709 let kl: i64 = ri_slen(key)
710 var i: i64 = 0
711 out[0] = 0 as u8
712 while i + kl <= n {
713 if ri_starts(page, n, i, key) == 1 {
714 var p: i64 = i + kl
715 var o: i64 = 0
716 var go: i64 = 1
717 while go == 1 {
718 if p >= n { go = 0 } else {
719 if page[p] == (RI_KVS_APOS as u8) { go = 0 } else {
720 if o < cap - 1 { out[o] = page[p]; o = o + 1 }
721 p = p + 1
722 }
723 }
724 }
725 out[o] = 0 as u8
726 if o > 0 { return 1 }
727 return 0
728 }
729 i = i + 1
730 }
731 return 0
732}
733// EXTRACT the player's video_url and license_code from a mirrored KVS page; 1 when both are present
734func ri_kvs_extract(page: *u8, n: i64, vurl: *u8, code: *u8) -> i64 {
735 let a: i64 = ri_kvs_value(page, n, RI_KVS_URL_KEY, vurl, RI_ROWB)
736 let b: i64 = ri_kvs_value(page, n, RI_KVS_CODE_KEY, code, RI_PATHB)
737 if a == 1 { if b == 1 { return 1 } }
738 return 0
739}