code wiki / _hdl_build / nx_img_harvest_gate.nx
nx_img_harvest_gate.nx source
↩ module page · 97 lines · 5669 B
1import "nx_gate_grow.nx"
2import "nx_gate_gn.nx"
3// nx_img_harvest_gate.nx -- SOVEREIGN referee for the s-class-exceed multi-strategy image harvester. Proves it
4// finds LAZY-LOADED + JS/JSON-embedded page URLs the old <img src> floor missed, resolves rel->abs, dedups, and
5// REJECTS UI junk (favicon/.svg/.css/.js/data:). One synthetic page exercises every strategy.
6// T1 count: exactly the 7 DISTINCT real page images (img src + data-src + data-original + srcset[2] + JSON[2]),
7// with a duplicate and junk present -> dedup + junk-reject both proven by the count
8// T2 resolution: a root-relative /a/1.jpg -> https://ex.com/a/1.jpg ; JSON escaped "\/\/cdn..." -> //->:// clean
9// T3 the srcset second candidate (/a/s2.jpg) IS collected (tokenized past "1x,")
10// T4 junk NOT collected: favicon.ico n/a (not img-ext), logo.svg rejected, style.css/app.js rejected, data: URI rejected
11// T5 order = appearance order (first collected is the img-src one)
12// GREEN iff 5/5. license_tier: ORIGINAL expect_exit: 0
13import "nx_syscalls.nx"
14import "nx_img_harvest.nx"
15
16func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func g_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]==(0 as u8){return 1} return 0 }
18// is `needle` among the collected urls?
19func g_among(url_buf: *u8, offs: *i64, lens: *i64, n: i64, needle: *u8) -> i64 {
20 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
21 var i: i64 = 0
22 while i < n {
23 if lens[i] == nl {
24 var eq: i64 = 1; var k: i64 = 0
25 let bp: *u8 = ((url_buf as i64) + offs[i]) as *u8
26 while k < nl { if bp[k] != needle[k] { eq = 0; k = nl } else { k = k + 1 } }
27 if eq == 1 { return 1 }
28 }
29 i = i + 1
30 }
31 return 0
32}
33func g_nth(url_buf: *u8, offs: *i64, lens: *i64, idx: i64, out: *u8) -> i64 {
34 let bp: *u8 = ((url_buf as i64) + offs[idx]) as *u8
35 var k: i64 = 0; while k < lens[idx] { out[k] = bp[k]; k = k + 1 } out[lens[idx]] = 0 as u8
36 return lens[idx]
37}
38
39func main() -> i64 {
40 gw("img-harvest SOVEREIGN gate (multi-strategy: img/lazy/srcset/JSON + resolve + dedup + junk-reject)\n" as *u8)
41
42 // one synthetic page. base = https://ex.com/read/ch1/
43 // real pages: img-src /a/1.jpg ; data-src /a/2.png ; data-original https://cdn.x/a/3.webp ;
44 // srcset "/a/s1.jpg 1x, /a/s2.jpg 2x" (2) ; JSON "pages":["https:\/\/cdn.x\/a\/6.jpg","/a/1.jpg"(dup),"/a/7.gif"]
45 // junk: favicon /favicon.ico(not img-ext) ; logo /logo.svg ; css /style.css ; js /app.js ; data: URI
46 let html: *u8 = "<html><head><link rel=icon href='/favicon.ico'><link href='/style.css'><script src='/app.js'></script></head><body><img src='/a/1.jpg'><img data-src='/a/2.png' src='data:image/gif;base64,AAAA'><img data-original='https://cdn.x/a/3.webp'><img srcset='/a/s1.jpg 1x, /a/s2.jpg 2x'><img src='/logo.svg'><script>window.__data={\"pages\":[\"https:\\/\\/cdn.x\\/a\\/6.jpg\",\"/a/1.jpg\",\"/a/7.gif\"]}</script></body></html>" as *u8
47 var hl: i64 = 0; while html[hl] != (0 as u8) { hl = hl + 1 }
48 let base: *u8 = "https://ex.com/read/ch1/" as *u8
49 var bl: i64 = 0; while base[bl] != (0 as u8) { bl = bl + 1 }
50
51 let ub: *u8 = sys_mmap(65536)
52 let offs: *i64 = sys_mmap(8 * 128) as *i64
53 let lens: *i64 = sys_mmap(8 * 128) as *i64
54 let n: i64 = nx_img_harvest(html, hl, base, bl, ub, 65536, offs, lens, 128)
55 gw(" harvested n=" as *u8); gn(n); gw(" :\n" as *u8)
56 var d: i64 = 0
57 while d < n { let u: *u8 = sys_mmap(512); g_nth(ub, offs, lens, d, u); gw(" " as *u8); gw(u); gw("\n" as *u8); d = d + 1 }
58 var pass: i64 = 0
59
60 // T1 count = 7 distinct real pages
61 pass = pass + grow("T1 count = 7 distinct pages (dedup + junk-reject proven)\x00" as *u8, n == 7)
62
63 // T2 resolution
64 var t2: i64 = 0
65 if g_among(ub, offs, lens, n, "https://ex.com/a/1.jpg" as *u8) == 1 {
66 if g_among(ub, offs, lens, n, "https://cdn.x/a/6.jpg" as *u8) == 1 { // JSON escaped \/ -> clean
67 if g_among(ub, offs, lens, n, "https://cdn.x/a/3.webp" as *u8) == 1 { t2 = 1 }
68 }
69 }
70 pass = pass + grow("T2 resolve: root-rel->abs + JSON \\/-unescape + absolute passthrough\x00" as *u8, t2)
71
72 // T3 srcset both candidates
73 var t3: i64 = 0
74 if g_among(ub, offs, lens, n, "https://ex.com/a/s1.jpg" as *u8) == 1 { if g_among(ub, offs, lens, n, "https://ex.com/a/s2.jpg" as *u8) == 1 { t3 = 1 } }
75 pass = pass + grow("T3 srcset: BOTH candidates collected (tokenized past '1x,')\x00" as *u8, t3)
76
77 // T4 junk rejected
78 var t4: i64 = 0
79 if g_among(ub, offs, lens, n, "https://ex.com/logo.svg" as *u8) == 0 {
80 if g_among(ub, offs, lens, n, "https://ex.com/style.css" as *u8) == 0 {
81 if g_among(ub, offs, lens, n, "https://ex.com/app.js" as *u8) == 0 {
82 if g_among(ub, offs, lens, n, "https://ex.com/favicon.ico" as *u8) == 0 { t4 = 1 }
83 }
84 }
85 }
86 pass = pass + grow("T4 junk rejected: .svg/.css/.js/.ico/data: NOT collected\x00" as *u8, t4)
87
88 // T5 order: first = the img-src page
89 var t5: i64 = 0
90 let u0: *u8 = sys_mmap(512); g_nth(ub, offs, lens, 0, u0)
91 if g_streq(u0, "https://ex.com/a/1.jpg" as *u8) == 1 { t5 = 1 }
92 pass = pass + grow("T5 order = appearance (first = img-src page)\x00" as *u8, t5)
93
94 gw("pass=" as *u8); gn(pass); gw("/5\n" as *u8)
95 if pass == 5 { gw("verdict=GREEN (s-class harvester: lazy+srcset+JSON strategies, resolved+deduped+junk-filtered)\n" as *u8); sys_exit(0); return 0 }
96 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
97}