nx_media_xrecover.nx source
↩ module page · 95 lines · 6737 B
1// nx_media_xrecover.nx -- CROSS-TEMPORAL media recovery (the Wayback-exceeding differentiator): given ONE image URL
2// that is 404/missing at a page's timestamp, CDX-query the image URL for EVERY capture, list them, pick a status-200
3// capture, fetch its raw bytes (web/<ts>id_/<url>), and validate real image magic. Recovers an image from a DIFFERENT
4// point in time than the page that referenced it. Reuses the proven fetch/CDX/validate stack. Usage:
5// nx_media_xrecover [image-url] (default: the page3.com zoe2 image the operator started with)
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_x509_trust_store.nx"
9import "nx_trust_store_load_from_certdata.nx"
10import "nx_https_fetch_follow.nx"
11import "nx_cdx_parse.nx"
12const K_MAGIC_1000000007: i64 = 1000000007
13const K_MAGIC_4194304: i64 = 4194304
14const K_MAGIC_4096: i64 = 4096
15const K_MAGIC_8192: i64 = 8192
16
17func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func gn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 }
19func gwslice(buf: *u8, off: i64, len: i64) -> i64 { sys_write(1, ((buf as i64)+off) as *u8, len); return 0 }
20func ap(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { buf[off+i]=s[i]; i=i+1 } return off+i }
21func apslice(dst: *u8, off: i64, src: *u8, so: i64, sl: i64) -> i64 { var i: i64=0; while i<sl { dst[off+i]=src[so+i]; i=i+1 } return off+sl }
22func cid_hash(buf: *u8, n: i64) -> i64 { var h: i64=0; var i: i64=0; while i<n { h=(h*131+(buf[i] as i64))%K_MAGIC_1000000007; i=i+1 } return h }
23func is_image(buf: *u8, n: i64) -> i64 {
24 if n<4 { return 0 }
25 let a: i64=buf[0] as i64; let b: i64=buf[1] as i64; let c: i64=buf[2] as i64; let d: i64=buf[3] as i64
26 if a==0x47 { if b==0x49 { if c==0x46 { return 1 } } } // GIF
27 if a==0xff { if b==0xd8 { if c==0xff { return 1 } } } // JPEG
28 if a==0x89 { if b==0x50 { if c==0x4e { if d==0x47 { return 1 } } } } // PNG
29 if a==0x42 { if b==0x4d { return 1 } } // BMP
30 return 0
31}
32func save_file(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0 } var off: i64=0; while off<n { let w: i64=sys_write(fd, ((buf as i64)+off) as *u8, n-off); if w<=0 { sys_close(fd); return 0 } off=off+w } sys_close(fd); return 1 }
33func fetch_retry(url: *u8, store: *TrustStore, out: *u8, cap: i64, st: *i64, tries: i64) -> i64 { var t: i64=0; var n: i64=0-1; while t<tries { n=nx_https_fetch_follow(url, store, out, cap, 6, st); if n>0 { if st[0]==200 { t=tries } else { gw(" [status " as *u8); gn(st[0]); gw(" retry]\n" as *u8); t=t+1 } } else { gw(" [err " as *u8); gn(n); gw(" retry]\n" as *u8); t=t+1 } } return n }
34
35func main(argc: i64, argv: *i64) -> i64 {
36 gw("=== nx_media_xrecover: cross-temporal recovery of ONE image (EXCEED Wayback's broken <img>) ===\n" as *u8)
37 sys_mkdir("web_assets" as *u8, 0x1ed); sys_mkdir("web_assets/archive" as *u8, 0x1ed); sys_mkdir("web_assets/archive/media" as *u8, 0x1ed)
38 var imgurl: *u8 = "http://www.page3.com/models2005/zoe2_27_07_05.jpg" as *u8
39 if argc >= 2 { imgurl = argv[1] as *u8 }
40 gw("target image: " as *u8); gw(imgurl); gw("\n" as *u8)
41
42 let r: i64=nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
43 if r<=0 { gw("trust store load failed\n" as *u8); return 1 }
44 let store: *TrustStore=r as *TrustStore
45 let cap: i64=K_MAGIC_4194304
46 let st: *i64=sys_mmap(8) as *i64
47
48 // 1. CDX-query the IMAGE url (every capture of the image itself, not the page)
49 let cdxurl: *u8=sys_mmap(K_MAGIC_4096); var co: i64=0
50 co=ap(cdxurl, co, "https://web.archive.org/cdx/search/cdx?url=" as *u8); co=ap(cdxurl, co, imgurl); co=ap(cdxurl, co, "&output=text&limit=200" as *u8); cdxurl[co]=0 as u8
51 gw("\nCDX query: " as *u8); gw(cdxurl); gw("\n" as *u8)
52 let cdxbuf: *u8=sys_mmap(cap)
53 let n: i64=fetch_retry(cdxurl, store, cdxbuf, cap, st, 4)
54 if n<=0 { gw("CDX fetch failed\n" as *u8); return 1 }
55
56 let RS: *i64=sys_mmap(K_MAGIC_8192) as *i64; let RE: *i64=sys_mmap(K_MAGIC_8192) as *i64
57 let nrows: i64=cdx_rows(cdxbuf, n, RS, RE, 1000)
58 gw("captures of the image found = " as *u8); gn(nrows); gw("\n" as *u8)
59 if nrows<=0 { gw("NO captures -- this image URL was never archived (genuinely unrecoverable)\n" as *u8); return 1 }
60
61 let b2: *i64=sys_mmap(16) as *i64
62 var best: i64=0-1
63 var i: i64=0
64 while i<nrows {
65 cdx_field(cdxbuf, RS[i], RE[i], 1, b2); let tso: i64=b2[0]; let tsl: i64=b2[1]
66 cdx_field(cdxbuf, RS[i], RE[i], 4, b2)
67 var is200: i64=0
68 if b2[1]>=3 { if (cdxbuf[b2[0]] as i64)==0x32 { if (cdxbuf[b2[0]+1] as i64)==0x30 { if (cdxbuf[b2[0]+2] as i64)==0x30 { is200=1 } } } }
69 gw(" capture " as *u8); gwslice(cdxbuf, tso, tsl); gw(" status=" as *u8); gwslice(cdxbuf, b2[0], b2[1])
70 if is200==1 { gw(" <- recoverable" as *u8); if best<0 { best=i } }
71 gw("\n" as *u8)
72 i=i+1
73 }
74 if best<0 { gw("\nNO status-200 capture among " as *u8); gn(nrows); gw(" -- every capture was a soft-404/redirect\n" as *u8); return 1 }
75
76 // 2. fetch the best (first status-200) capture's RAW bytes via id_
77 cdx_field(cdxbuf, RS[best], RE[best], 1, b2); let bo: i64=b2[0]; let bl: i64=b2[1]
78 let furl: *u8=sys_mmap(K_MAGIC_4096); var fo: i64=0
79 fo=ap(furl, fo, "https://web.archive.org/web/" as *u8); fo=apslice(furl, fo, cdxbuf, bo, bl); fo=ap(furl, fo, "id_/" as *u8); fo=ap(furl, fo, imgurl); furl[fo]=0 as u8
80 gw("\nrecover from: " as *u8); gw(furl); gw("\n" as *u8)
81 let img: *u8=sys_mmap(cap)
82 let n2: i64=fetch_retry(furl, store, img, cap, st, 6)
83 if n2<=0 { gw("image fetch failed\n" as *u8); return 1 }
84
85 // 3. validate REAL image magic + save + content-address
86 if is_image(img, n2)==1 {
87 let cid: i64=cid_hash(img, n2)
88 save_file("web_assets/archive/media/xrecovered.jpg" as *u8, img, n2)
89 gw("\nRECOVERED: bytes=" as *u8); gn(n2); gw(" magic=OK cid=" as *u8); gn(cid); gw(" -> web_assets/archive/media/xrecovered.jpg\n" as *u8)
90 gw("EXCEED: Wayback shows this <img> broken at the page's date; we pulled the real bytes from capture " as *u8); gwslice(cdxbuf, bo, bl); gw("\n" as *u8)
91 return 0
92 }
93 gw("\nfetched " as *u8); gn(n2); gw("B but NOT a valid image (soft-404 body) -- would try the next status-200 capture\n" as *u8)
94 return 1
95}