code wiki / _hdl_build / nx_nmz_index.nx

nx_nmz_index.nx source

↩ module page · 102 lines · 7018 B

1// nx_nmz_index.nx -- the CONTENT-ADDRESSED CATALOGUE of all liberated media. The convergence capstone of the 2// format-liberation arc: every lock-in format (EPUB/MOBI/HUFF/KF8/FB2/CBZ/MP3/M4B) was migrated into the owned 3// .nmz; this enumerates every .nmz under knowledge/staging/media/nmz/ and emits a catalogue (nmz_index.json) with, 4// per file: the sha256 content CID (uxf_cid_profiled, the substrate's address), the @kind (book/comic/audio), the 5// title (from the packed manifest), and byte size. Content-addressing => dedup + provenance for free. Feeds the 6// library workstream's nx_library (coordinate to register these CIDs as library objects); does NOT duplicate the 7// substrate -- it reads via nx_canon_bin + nx_uxf_cid. Usage: nx_nmz_index. expect_exit: 0 license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_canon_bin.nx" 10import "nx_uxf_cid.nx" 11const K_MAGIC_65536: i64 = 65536 12const K_MAGIC_1024: i64 = 1024 13const K_MAGIC_16384: i64 = 16384 14 15func ix_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 16func ix_w(fd: i64, s: *u8) -> i64 { sys_write(fd, s, ix_slen(s)); return 0 } 17func ix_p(s: *u8) -> i64 { ix_w(1, s); return 0 } 18func ix_wn(fd: i64, v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(fd,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(fd,o,k); return 0 } 19func ix_pn(v: i64) -> i64 { ix_wn(1, v); return 0 } 20func ensure_dir(path: *u8) -> i64 { __syscall(258, 0-100, path, 0x1ed, 0, 0, 0); return 0 } 21func cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){d[o+i]=s[i];i=i+1} return o+i } 22func wjson(fd: i64, s: *u8, n: i64) -> i64 { 23 var i: i64 = 0 24 while i < n { let c: i64 = s[i] as i64; if c == 0x22 { sys_write(fd, "\\\"" as *u8, 2) } else { if c == 0x5c { sys_write(fd, "\\\\" as *u8, 2) } else { if c < 0x20 { sys_write(fd, " " as *u8, 1) } else { sys_write(fd, (s as i64 + i) as *u8, 1) } } } i = i + 1 } 25 return 0 26} 27// extract the JSON string value after "title":" from a manifest buffer; ret len. 28func man_title(m: *u8, n: i64, out: *u8, cap: i64) -> i64 { 29 let key: *u8 = "\"title\":\"" as *u8; let kl: i64 = ix_slen(key) 30 var i: i64 = 0 31 while i + kl <= n { var k: i64=0; var hit: i64=1; while k<kl { if m[i+k]!=key[k]{hit=0;k=kl}else{k=k+1} } if hit==1 { 32 var j: i64 = i + kl; var o: i64 = 0 33 while j < n { let c: i64 = m[j] as i64; if c==0x22 { j=n } else { if c==0x5c { if j+1<n { if o<cap-1{out[o]=m[j+1];o=o+1} j=j+2 } else { j=j+1 } } else { if o<cap-1{out[o]=m[j] as u8;o=o+1} j=j+1 } } } 34 out[o]=0 as u8; return o 35 } i=i+1 } 36 out[0]=0 as u8; return 0 37} 38 39func main() -> i64 { 40 let outp: *u8 = "knowledge/staging/media/nmz_index.json" as *u8 41 let fd: i64 = sys_openat_wr(outp, 0x1a4) 42 if fd < 0 { ix_p("NMZ-INDEX-FAIL open out\n" as *u8); sys_exit(1); return 1 } 43 ix_w(fd, "{\"library\":\"nishi-liberated-media\",\"items\":[" as *u8) 44 45 let dirp: *u8 = "knowledge/staging/media/nmz\x00" as *u8 46 let dfd: i64 = __syscall(257, 0-100, dirp, 0x10000, 0, 0, 0) 47 var nitems: i64 = 0 48 var nbook: i64 = 0; var ncomic: i64 = 0; var naudio: i64 = 0 49 let nm: *u8 = sys_mmap(512) 50 if dfd >= 0 { 51 let buf: *u8 = sys_mmap(K_MAGIC_65536) 52 var go: i64 = 1 53 while go == 1 { 54 let nread: i64 = __syscall(217, dfd, buf, K_MAGIC_65536, 0, 0, 0) 55 if nread <= 0 { go = 0 } else { 56 var pos: i64 = 0 57 while pos < nread { 58 let reclen: i64 = (buf[pos+16] as i64) | ((buf[pos+17] as i64) << 8) 59 var nl: i64 = 0 60 while buf[pos+19+nl] != (0 as u8) { nm[nl]=buf[pos+19+nl]; nl=nl+1 } nm[nl]=0 as u8 61 // accept only "*.nmz" files (suffix .nmz, len>4) 62 var isnmz: i64 = 0 63 if nl > 4 { if nm[nl-4]==(0x2e as u8) { if nm[nl-3]==(0x6e as u8) { if nm[nl-2]==(0x6d as u8) { if nm[nl-1]==(0x7a as u8) { isnmz=1 } } } } } 64 if isnmz == 1 { 65 let path: *u8 = sys_mmap(K_MAGIC_1024); var pl: i64 = cat(path, 0, "knowledge/staging/media/nmz/" as *u8); pl = cat(path, pl, nm); path[pl]=0 as u8 66 let lp: *i64 = sys_mmap(8) as *i64; lp[0]=0 67 let m: *u8 = sys_read_file(path, lp) 68 if (m as i64)!=0 { 69 let n: i64 = lp[0] 70 let cid: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m, n, cid) 71 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(m, n, "@kind\x00" as *u8, kindb, 64) 72 if gk <= 0 { kindb[0]=0 as u8 } 73 // title from whichever manifest is present 74 let manb: *u8 = sys_mmap(K_MAGIC_16384); var gm: i64 = canon_get_bin(m, n, "book.json\x00" as *u8, manb, K_MAGIC_16384) 75 if gm <= 0 { gm = canon_get_bin(m, n, "audio.json\x00" as *u8, manb, K_MAGIC_16384) } 76 if gm <= 0 { gm = canon_get_bin(m, n, "cbx.json\x00" as *u8, manb, K_MAGIC_16384) } 77 let title: *u8 = sys_mmap(K_MAGIC_1024); title[0]=0 as u8 78 if gm > 0 { man_title(manb, gm, title, K_MAGIC_1024) } 79 if nitems > 0 { ix_w(fd, "," as *u8) } 80 ix_w(fd, "{\"slug\":\"" as *u8); wjson(fd, nm, nl - 4) 81 ix_w(fd, "\",\"cid\":\"" as *u8); ix_w(fd, cid) 82 ix_w(fd, "\",\"kind\":\"" as *u8); if gk>0 { ix_w(fd, kindb) } 83 ix_w(fd, "\",\"title\":\"" as *u8); wjson(fd, title, ix_slen(title)) 84 ix_w(fd, "\",\"bytes\":" as *u8); ix_wn(fd, n); ix_w(fd, "}" as *u8) 85 nitems = nitems + 1 86 if kindb[0]==(0x62 as u8) { nbook=nbook+1 } // 'b'ook 87 if kindb[0]==(0x63 as u8) { ncomic=ncomic+1 } // 'c'omic 88 if kindb[0]==(0x61 as u8) { naudio=naudio+1 } // 'a'udio 89 } 90 } 91 if reclen <= 0 { pos = nread } else { pos = pos + reclen } 92 } 93 } 94 } 95 sys_close(dfd) 96 } 97 ix_w(fd, "],\"count\":" as *u8); ix_wn(fd, nitems) 98 ix_w(fd, ",\"by_kind\":{\"book\":" as *u8); ix_wn(fd, nbook); ix_w(fd, ",\"comic\":" as *u8); ix_wn(fd, ncomic); ix_w(fd, ",\"audio\":" as *u8); ix_wn(fd, naudio); ix_w(fd, "}}" as *u8) 99 sys_close(fd) 100 ix_p("NMZ-INDEX items=" as *u8); ix_pn(nitems); ix_p(" (book=" as *u8); ix_pn(nbook); ix_p(" comic=" as *u8); ix_pn(ncomic); ix_p(" audio=" as *u8); ix_pn(naudio); ix_p(") -> knowledge/staging/media/nmz_index.json\n" as *u8) 101 ix_p("NMZ-INDEX-OK\n" as *u8); sys_exit(0); return 0 102}