code wiki / (root) / nx_archive_xcapture_fetch.nx

nx_archive_xcapture_fetch.nx source

↩ module page · 83 lines · 5819 B

1// nx_archive_xcapture_fetch.nx -- fetch mast3.gif from a SECOND (later) capture so we can prove a TRUE cross-capture 2// perceptual recovery: 1997 masthead vs the ~1999/2000 masthead, two genuinely-different archived copies. Reads the 3// cached CDX for a capture dated >= 1999-01-01, fetches web/<ts>id_/http://www.page3.com/mast3.gif -> 4// media/mast3_late.gif (status-200 + image-magic validated, cached). Fetch stack. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_x509_trust_store.nx" 7import "nx_trust_store_load_from_certdata.nx" 8import "nx_https_fetch_follow.nx" 9import "nx_cdx_parse.nx" 10const K_MAGIC_1000000007: i64 = 1000000007 11const K_MAGIC_4194304: i64 = 4194304 12const K_MAGIC_4096: i64 = 4096 13const K_MAGIC_19990101: i64 = 19990101 14const K_MAGIC_2048: i64 = 2048 15const K_MAGIC_288075837: i64 = 288075837 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 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 } 20func 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 } 21func 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 } 22func 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 } 23func read_file(path: *u8, buf: *u8, cap: i64, lenbox: *i64) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } let n: i64=sys_read(fd, buf, cap); sys_close(fd); if n<=0 { return 0 } lenbox[0]=n; return n } 24func is_image(buf: *u8, n: i64) -> i64 { if n<4 { return 0 } 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; if a==0x47 { if b==0x49 { if c==0x46 { return 1 } } } if a==0xff { if b==0xd8 { if c==0xff { return 1 } } } if a==0x89 { if b==0x50 { if c==0x4e { if d==0x47 { return 1 } } } } if a==0x42 { if b==0x4d { return 1 } } return 0 } 25func 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 } 26 27func main() -> i64 { 28 gw("=== nx_archive_xcapture_fetch: grab the later (~1999/2000) page3 masthead ===\n" as *u8) 29 sys_mkdir("web_assets" as *u8, 0x1ed) 30 sys_mkdir("web_assets/archive" as *u8, 0x1ed) 31 sys_mkdir("web_assets/archive/media" as *u8, 0x1ed) 32 let r: i64=nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 33 if r<=0 { gw("trust store load failed\n" as *u8); return 1 } 34 let store: *TrustStore=r as *TrustStore 35 let cap: i64=K_MAGIC_4194304 36 let lb: *i64=sys_mmap(8) as *i64 37 let st: *i64=sys_mmap(8) as *i64 38 39 let cdxbuf: *u8=sys_mmap(cap) 40 if read_file("web_assets/archive/_cache_cdx.txt" as *u8, cdxbuf, cap, lb)<=0 { gw("no cached CDX -- run nx_archive_viewer first\n" as *u8); return 1 } 41 let ncdx: i64=lb[0] 42 let RS: *i64=sys_mmap(K_MAGIC_4096) as *i64 43 let RE: *i64=sys_mmap(K_MAGIC_4096) as *i64 44 let nrows: i64=cdx_rows(cdxbuf, ncdx, RS, RE, 480) 45 let b2: *i64=sys_mmap(16) as *i64 46 var tso: i64=0-1 47 var tsl: i64=0 48 var tdate: i64=0 49 var i: i64=0 50 while i<nrows { 51 if tso<0 { 52 cdx_field(cdxbuf, RS[i], RE[i], 1, b2) 53 let d: i64=cdx_ts8(cdxbuf, b2[0], b2[1]) 54 if d>=K_MAGIC_19990101 { tso=b2[0]; tsl=b2[1]; tdate=d } 55 } 56 i=i+1 57 } 58 if tso<0 { gw("no capture >= 1999 in CDX\n" as *u8); return 1 } 59 gw("later capture date=" as *u8); gn(tdate); gw("\n" as *u8) 60 61 let img: *u8=sys_mmap(cap) 62 // cache-first 63 var n3: i64=read_file("web_assets/archive/media/mast3_late.gif" as *u8, img, cap, lb) 64 if n3>0 { n3=lb[0]; gw("mast3_late.gif [cache] bytes=" as *u8); gn(n3); gw("\n" as *u8) } else { 65 let url: *u8=sys_mmap(K_MAGIC_2048) 66 var uo: i64=0 67 uo=ap(url, uo, "https://web.archive.org/web/" as *u8) 68 uo=apslice(url, uo, cdxbuf, tso, tsl) 69 uo=ap(url, uo, "id_/http://www.page3.com/mast3.gif" as *u8) 70 url[uo]=0 as u8 71 gw("fetch " as *u8); gw(url); gw("\n" as *u8) 72 n3=fetch_retry(url, store, img, cap, st, 6) 73 if n3<=0 { gw("fetch failed\n" as *u8); return 1 } 74 if st[0]!=200 { gw("non-200 (mast3.gif not captured at this date)\n" as *u8); return 1 } 75 if is_image(img, n3)==0 { gw("not an image (404 page)\n" as *u8); return 1 } 76 save_file("web_assets/archive/media/mast3_late.gif" as *u8, img, n3) 77 gw("mast3_late.gif saved bytes=" as *u8); gn(n3); gw("\n" as *u8) 78 } 79 let cid: i64=cid_hash(img, n3) 80 gw("XCAPTURE-OK mast3_late.gif bytes=" as *u8); gn(n3); gw(" cid=" as *u8); gn(cid); gw(" (1997 mast3.gif cid=288075837)\n" as *u8) 81 if cid==K_MAGIC_288075837 { gw(" note: byte-identical to 1997 (masthead unchanged / Wayback dedup)\n" as *u8) } else { gw(" note: DIFFERENT bytes from 1997 -> a genuine second archived copy\n" as *u8) } 82 return 0 83}