code wiki / _hdl_build / nx_media_harvest_gate.nx
nx_media_harvest_gate.nx source
↩ module page · 110 lines · 6564 B
1import "nx_gate_grow.nx"
2import "nx_gate_gn.nx"
3// nx_media_harvest_gate.nx -- SOVEREIGN referee for the SUPERSET media harvester. Proves it finds not just
4// images but VIDEO (<video>/<source> .mp4/.webm), STREAM manifests (.m3u8 in <source> and in a data-attr),
5// AUDIO (.mp3), a GIF hidden in a JSON blob, an UNQUOTED CSS background url(/bg/hero.gif) (the pass-2 win),
6// resolves rel->abs + protocol-relative + JSON \/-unescape, dedups, CLASSIFIES each with the right kind, and
7// REJECTS junk (.ico/.svg/.css/.js/sprite/data:). One synthetic page exercises every strategy. GREEN iff 8/8.
8// license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_media_harvest.nx"
11
12func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
13
14// is `needle` among the collected urls?
15func g_among(url_buf: *u8, offs: *i64, lens: *i64, n: i64, needle: *u8) -> i64 {
16 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
17 var i: i64 = 0
18 while i < n {
19 if lens[i] == nl {
20 var eq: i64 = 1; var k: i64 = 0
21 let bp: *u8 = ((url_buf as i64) + offs[i]) as *u8
22 while k < nl { if bp[k] != needle[k] { eq = 0; k = nl } else { k = k + 1 } }
23 if eq == 1 { return 1 }
24 }
25 i = i + 1
26 }
27 return 0
28}
29// kind of the collected url that equals needle, or -99 if absent
30func g_kind_of(url_buf: *u8, offs: *i64, lens: *i64, kinds: *i64, n: i64, needle: *u8) -> i64 {
31 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
32 var i: i64 = 0
33 while i < n {
34 if lens[i] == nl {
35 var eq: i64 = 1; var k: i64 = 0
36 let bp: *u8 = ((url_buf as i64) + offs[i]) as *u8
37 while k < nl { if bp[k] != needle[k] { eq = 0; k = nl } else { k = k + 1 } }
38 if eq == 1 { return kinds[i] }
39 }
40 i = i + 1
41 }
42 return 0 - 99
43}
44
45func main() -> i64 {
46 gw("media-harvest SOVEREIGN gate (img/gif/video/stream/audio + srcset/JSON/dataattr/CSS-url() + classify)\n" as *u8)
47
48 // one synthetic page. base = https://ex.com/watch/
49 let html: *u8 = "<html><head><link rel=icon href='/favicon.ico'><link href='/style.css'><script src='/app.js'></script><style>.x{background-image:url('/bg/tile.png')}</style></head><body><img src='/a/1.jpg'><img data-src='/a/2.png' src='data:image/gif;base64,AAAA'><img srcset='/a/s1.webp 1x, /a/s2.avif 2x'><img src='/img/sprite.png'><img src='/logo.svg'><video src='/v/clip.mp4' poster='/v/poster.jpg'></video><source src='/v/hls/stream.m3u8' type='application/x-mpegURL'><source src='//cdn.x/v/alt.webm'><audio src='/au/track.mp3'></audio><div data-hls='/live/room.m3u8'></div><div style=background:url(/bg/hero.gif)>Hi</div><script>window.__d={\"g\":\"https:\\/\\/cdn.x\\/a\\/anim.gif\"}</script></body></html>" as *u8
50 var hl: i64 = 0; while html[hl] != (0 as u8) { hl = hl + 1 }
51 let base: *u8 = "https://ex.com/watch/" as *u8
52 var bl: i64 = 0; while base[bl] != (0 as u8) { bl = bl + 1 }
53
54 let ub: *u8 = sys_mmap(65536)
55 let offs: *i64 = sys_mmap(8 * 128) as *i64
56 let lens: *i64 = sys_mmap(8 * 128) as *i64
57 let kinds: *i64 = sys_mmap(8 * 128) as *i64
58 let n: i64 = nx_media_harvest(html, hl, base, bl, ub, 65536, offs, lens, kinds, 128)
59 gw(" harvested n=" as *u8); gn(n); gw(" :\n" as *u8)
60 var d: i64 = 0
61 while d < n {
62 let bp: *u8 = ((ub as i64) + offs[d]) as *u8
63 gw(" [" as *u8); gw(nx_media_kind_label(kinds[d])); gw("] " as *u8)
64 sys_write(1, bp, lens[d]); gw("\n" as *u8)
65 d = d + 1
66 }
67 var pass: i64 = 0
68
69 // T1 exact distinct count = 13 (dedup of the CSS tile + junk-reject both folded into the count)
70 pass = pass + grow("T1 count = 13 distinct media (dedup + junk-reject proven)\x00" as *u8, n == 13)
71
72 // T2 video (<video src>) present + classified video(2)
73 pass = pass + grow("T2 <video src> /v/clip.mp4 collected as kind=video\x00" as *u8, g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/v/clip.mp4" as *u8) == 2)
74
75 // T3 HLS stream in <source> AND in a data-attr, both kind=stream(3)
76 var t3: i64 = 0
77 if g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/v/hls/stream.m3u8" as *u8) == 3 { if g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/live/room.m3u8" as *u8) == 3 { t3 = 1 } }
78 pass = pass + grow("T3 HLS .m3u8 (<source> + data-attr) both kind=stream\x00" as *u8, t3)
79
80 // T4 gif from JSON blob AND unquoted CSS url() -- the pass-2 win -- both kind=gif(1)
81 var t4: i64 = 0
82 if g_kind_of(ub, offs, lens, kinds, n, "https://cdn.x/a/anim.gif" as *u8) == 1 { if g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/bg/hero.gif" as *u8) == 1 { t4 = 1 } }
83 pass = pass + grow("T4 gif via JSON(\\/-unescape) + UNQUOTED CSS url() both kind=gif\x00" as *u8, t4)
84
85 // T5 audio kind=audio(4)
86 pass = pass + grow("T5 <audio src> /au/track.mp3 collected as kind=audio\x00" as *u8, g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/au/track.mp3" as *u8) == 4)
87
88 // T6 protocol-relative //cdn.x/v/alt.webm -> https + kind=video
89 pass = pass + grow("T6 protocol-relative //cdn.x/v/alt.webm -> https, kind=video\x00" as *u8, g_kind_of(ub, offs, lens, kinds, n, "https://cdn.x/v/alt.webm" as *u8) == 2)
90
91 // T7 junk rejected: favicon.ico / logo.svg / style.css / app.js / sprite.png / data: NOT collected
92 var t7: i64 = 0
93 if g_among(ub, offs, lens, n, "https://ex.com/favicon.ico" as *u8) == 0 {
94 if g_among(ub, offs, lens, n, "https://ex.com/logo.svg" as *u8) == 0 {
95 if g_among(ub, offs, lens, n, "https://ex.com/style.css" as *u8) == 0 {
96 if g_among(ub, offs, lens, n, "https://ex.com/app.js" as *u8) == 0 {
97 if g_among(ub, offs, lens, n, "https://ex.com/img/sprite.png" as *u8) == 0 { t7 = 1 }
98 }
99 }
100 }
101 }
102 pass = pass + grow("T7 junk rejected: .ico/.svg/.css/.js/sprite/data: NOT collected\x00" as *u8, t7)
103
104 // T8 kind-integrity liar-kill: the poster is an IMAGE(0), NOT mislabeled as video -> kinds are real, not constant
105 pass = pass + grow("T8 liar-kill: poster /v/poster.jpg kind=image(0), not constant\x00" as *u8, g_kind_of(ub, offs, lens, kinds, n, "https://ex.com/v/poster.jpg" as *u8) == 0)
106
107 gw("pass=" as *u8); gn(pass); gw("/8\n" as *u8)
108 if pass == 8 { gw("verdict=GREEN (superset media harvester: img+gif+video+stream+audio, classified+resolved+deduped+junk-filtered)\n" as *u8); sys_exit(0); return 0 }
109 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
110}