code wiki / (root) / nx_torrent_heal_research_fetch.nx

nx_torrent_heal_research_fetch.nx source

↩ module page · 110 lines · 8503 B

1// nx_torrent_heal_research_fetch.nx -- SOVEREIGN multi-source web-fetch for the TORRENT-HEAL / "resurrect 2// torrents with gaps" research ladder (the Nishi-researcher FETCH half of the partnership; Claude 3// deep-research is the SYNTH half, run wf_e739d2f5-643). Reuses the proven sovereign TLS-1.3 stack 4// (nx_https_fetch_follow) + the Mozilla CA store, and SAVES each source to knowledge/fetched/th_<dim>_*.raw 5// so research COMPOUNDS to the Library and can be grepped to CITE (>=2-source corroboration / no-hearsay). 6// 100% sovereign (own TLS, nx_cc->nxasm, no curl/wget/gcc). Only non-Nishi inputs = CA-root DATA + fetched pages. 7// 8// Dimensions: CW crypto wall (why AI can't make a gapped torrent VERIFY) / BX byte-exact healing (complete it 9// from another source) / VI video inpainting+interpolation / AU audio packet-loss concealment / EC bitstream 10// (GOP/keyframe)-aware error concealment + the codecs a missing piece desyncs. expect_exit: 0 license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "nx_x509_trust_store.nx" 13import "nx_trust_store_load_from_certdata.nx" 14import "nx_https_fetch_follow.nx" 15const K_MAGIC_4194304: i64 = 4194304 16const K_MAGIC_8388608: i64 = 8388608 17 18func gf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 19func gf_putn(v: i64) -> i64 { 20 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 21 var m: i64 = v 22 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 23 let d: *u8 = sys_mmap(24); var k: i64 = 0 24 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 25 var j: i64 = k - 1 26 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 27 return 0 28} 29 30// idempotent (rule #10): a source already on disk is skipped, so re-runs only fill gaps. Also the LEAK 31// WORKAROUND -- the TLS stack leaks mmap per fetch, so the set completes across a few resumable passes 32// instead of one OOM-killed run. Re-run the same arg-free command until SAVED == total. 33func have_file(path: *u8) -> i64 { 34 let fd: i64 = sys_openat_rd(path) 35 if fd < 0 { return 0 } 36 sys_close(fd) 37 return 1 38} 39 40func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 { 41 if have_file(opath) == 1 { gf_puts(opath); gf_puts(" [have-skip]\n"); return 1 } 42 let status: *i64 = sys_mmap(8) as *i64 43 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status) 44 gf_puts(url); gf_puts(" status="); gf_putn(status[0]); gf_puts(" bytes="); gf_putn(n) 45 if n <= 0 { gf_puts(" FETCH-FAIL\n"); return 0 } 46 var gz: i64 = 0 47 if n >= 2 { if out[0] == 0x1f as u8 { if out[1] == 0x8b as u8 { gz = 1 } } } 48 if gz == 1 { gf_puts(" [GZIP-skip]\n"); return 0 } 49 let fd: i64 = sys_openat_wr(opath, 0x1a4) 50 if fd < 0 { gf_puts(" SAVE-FAIL\n"); return 0 } 51 sys_write(fd, out, n) 52 sys_close(fd) 53 gf_puts(" SAVED\n") 54 return 1 55} 56 57func main() -> i64 { 58 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 59 if r <= 0 { gf_puts("TH: certdata load failed\n"); return 1 } 60 let store: *TrustStore = r as *TrustStore 61 gf_puts("CA roots="); gf_putn(trust_store_count(store)); gf_puts("\n") 62 let cap: i64 = K_MAGIC_8388608 63 let out: *u8 = sys_mmap(cap) 64 var ok: i64 = 0 65 66 gf_puts("== CW CRYPTO WALL (why a synthesized piece can't VERIFY: preimage, not collision) ==\n") 67 ok = ok + fetch_save("https://en.wikipedia.org/wiki/SHA-1" as *u8, "knowledge/fetched/th_cw_sha1.raw" as *u8, store, out, cap) 68 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Preimage_attack" as *u8, "knowledge/fetched/th_cw_preimage.raw" as *u8, store, out, cap) 69 ok = ok + fetch_save("https://en.wikipedia.org/wiki/SHA-2" as *u8, "knowledge/fetched/th_cw_sha2.raw" as *u8, store, out, cap) 70 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Collision_attack" as *u8, "knowledge/fetched/th_cw_collision.raw" as *u8, store, out, cap) 71 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Cryptographic_hash_function" as *u8, "knowledge/fetched/th_cw_hashfn.raw" as *u8, store, out, cap) 72 ok = ok + fetch_save("https://en.wikipedia.org/wiki/BitTorrent" as *u8, "knowledge/fetched/th_cw_bittorrent.raw" as *u8, store, out, cap) 73 74 gf_puts("== BX BYTE-EXACT HEALING (complete the torrent from another source; redundancy must pre-exist) ==\n") 75 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Web_seeding" as *u8, "knowledge/fetched/th_bx_webseed.raw" as *u8, store, out, cap) 76 ok = ok + fetch_save("https://www.bittorrent.org/beps/bep_0019.html" as *u8, "knowledge/fetched/th_bx_bep19.raw" as *u8, store, out, cap) 77 ok = ok + fetch_save("https://www.bittorrent.org/beps/bep_0052.html" as *u8, "knowledge/fetched/th_bx_bep52.raw" as *u8, store, out, cap) 78 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Parchive" as *u8, "knowledge/fetched/th_bx_par2.raw" as *u8, store, out, cap) 79 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Erasure_code" as *u8, "knowledge/fetched/th_bx_erasure.raw" as *u8, store, out, cap) 80 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Fountain_code" as *u8, "knowledge/fetched/th_bx_fountain.raw" as *u8, store, out, cap) 81 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Raptor_code" as *u8, "knowledge/fetched/th_bx_raptorq.raw" as *u8, store, out, cap) 82 ok = ok + fetch_save("https://www.rfc-editor.org/rfc/rfc6330.txt" as *u8, "knowledge/fetched/th_bx_rfc6330.raw" as *u8, store, out, cap) 83 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Perceptual_hashing" as *u8, "knowledge/fetched/th_bx_phash.raw" as *u8, store, out, cap) 84 85 gf_puts("== VI VIDEO INPAINTING + FRAME INTERPOLATION (perceptual fill of lost regions/frames) ==\n") 86 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Inpainting" as *u8, "knowledge/fetched/th_vi_inpainting.raw" as *u8, store, out, cap) 87 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Motion_interpolation" as *u8, "knowledge/fetched/th_vi_frameinterp.raw" as *u8, store, out, cap) 88 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Video_super-resolution" as *u8, "knowledge/fetched/th_vi_vsr.raw" as *u8, store, out, cap) 89 ok = ok + fetch_save("https://arxiv.org/abs/2309.03897" as *u8, "knowledge/fetched/th_vi_propainter.raw" as *u8, store, out, cap) 90 ok = ok + fetch_save("https://arxiv.org/abs/2204.02663" as *u8, "knowledge/fetched/th_vi_e2fgvi.raw" as *u8, store, out, cap) 91 ok = ok + fetch_save("https://arxiv.org/abs/2011.06294" as *u8, "knowledge/fetched/th_vi_rife.raw" as *u8, store, out, cap) 92 ok = ok + fetch_save("https://arxiv.org/abs/2202.04901" as *u8, "knowledge/fetched/th_vi_film.raw" as *u8, store, out, cap) 93 94 gf_puts("== AU AUDIO PACKET-LOSS CONCEALMENT (fill the missing audio) ==\n") 95 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Packet_loss_concealment" as *u8, "knowledge/fetched/th_au_plc.raw" as *u8, store, out, cap) 96 ok = ok + fetch_save("https://arxiv.org/abs/2210.13438" as *u8, "knowledge/fetched/th_au_encodec.raw" as *u8, store, out, cap) 97 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Comfort_noise" as *u8, "knowledge/fetched/th_au_comfortnoise.raw" as *u8, store, out, cap) 98 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Speech_coding" as *u8, "knowledge/fetched/th_au_speechcoding.raw" as *u8, store, out, cap) 99 100 gf_puts("== EC BITSTREAM / GOP-AWARE REPAIR (a missing piece desyncs the decoder to the next keyframe) ==\n") 101 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Error_concealment" as *u8, "knowledge/fetched/th_ec_errconceal.raw" as *u8, store, out, cap) 102 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Group_of_pictures" as *u8, "knowledge/fetched/th_ec_gop.raw" as *u8, store, out, cap) 103 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Inter_frame" as *u8, "knowledge/fetched/th_ec_interframe.raw" as *u8, store, out, cap) 104 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Advanced_Video_Coding" as *u8, "knowledge/fetched/th_ec_h264.raw" as *u8, store, out, cap) 105 ok = ok + fetch_save("https://en.wikipedia.org/wiki/High_Efficiency_Video_Coding" as *u8, "knowledge/fetched/th_ec_h265.raw" as *u8, store, out, cap) 106 ok = ok + fetch_save("https://en.wikipedia.org/wiki/Network_Abstraction_Layer" as *u8, "knowledge/fetched/th_ec_nal.raw" as *u8, store, out, cap) 107 108 gf_puts("READABLE SOURCES SAVED: "); gf_putn(ok); gf_puts(" / 32 (re-run to fill gaps -- idempotent have-skip)\n") 109 return 0 110}