code wiki / _hdl_build / nx_pdf_gate.nx

nx_pdf_gate.nx source

↩ module page · 129 lines · 6519 B

1// nx_pdf_gate.nx -- gate for the PDF viewer (nx_pdf: nx_pdf_text extract -> language-filter -> 0-JS preview). 2// Evidence source = a REAL 634KB PubMed/Cochrane PDF fixture (knowledge/fetched/pmc_cd004504.pdf), not a toy. 3// Rows: 1 instruments 2 render-ok 3 real-content(Cochrane Library) 4 readable-head (filter kills the binary 4// prefix: "Cochrane" within the first 3600 bytes) 5 valid-0js-html 6 registry-dispatch(.pdf via nx_doc_view) 5// 7 loud-fail-missing[neg]. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8func pg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n } 9func pg_p(s: *u8) -> i64 { sys_write(1, s, pg_slen(s)); return 0 } 10func pg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off+i]=s[i]; i=i+1 } return off+i } 11func pg_catn(dst: *u8, off: i64, v: i64) -> i64 { 12 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m } 13 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 } 14 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 15 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k 16} 17func pg_exists(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } sys_close(fd); return 1 } 18func pg_readn(path: *u8, buf: *u8, cap: i64) -> i64 { 19 let fd: i64=sys_openat_rd(path); if fd<0 { return 0-1 } 20 var got: i64=0; var n: i64=1 21 while n>0 { if got>=cap { n=0 } else { n=sys_read(fd,(buf as i64+got) as *u8,cap-got); if n>0 { got=got+n } } } 22 sys_close(fd); return got 23} 24func pg_scan(buf: *u8, sz: i64, needle: *u8) -> i64 { 25 let n: i64=pg_slen(needle); if sz<n { return 0 } 26 var i: i64=0 27 while i+n<=sz { var ok: i64=1; var j: i64=0; while j<n { if buf[i+j]!=needle[j] { ok=0; j=n } else { j=j+1 } } if ok==1 { return 1 } i=i+1 } 28 return 0 29} 30func pg_has(path: *u8, needle: *u8) -> i64 { 31 let buf: *u8=sys_mmap(33554432) 32 let sz: i64=pg_readn(path, buf, 33554400) 33 if sz<0 { return 0 } 34 return pg_scan(buf, sz, needle) 35} 36func pg_has_pre(path: *u8, needle: *u8, cap: i64) -> i64 { 37 let buf: *u8=sys_mmap(65536) 38 let sz: i64=pg_readn(path, buf, cap) 39 if sz<0 { return 0 } 40 return pg_scan(buf, sz, needle) 41} 42func pg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 43 let pid: i64=sys_fork() 44 if pid==0 { 45 if (outpath as i64)!=0 { let ofd: i64=sys_openat_wr(outpath,0x1a4); if ofd>=0 { sys_dup3(ofd,1,0); sys_dup3(ofd,2,0) } } 46 let argv: *i64=sys_mmap(128) as *i64 47 argv[0]=elf as i64; var i: i64=0; var go: i64=1 48 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } } 49 argv[i+1]=0 50 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0 51 sys_execve(elf, argv, envp); sys_exit(127) 52 } 53 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0) 54 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig } 55 return (st[0]>>8)&0xff 56} 57func pg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 { 58 let a: *i64=sys_mmap(64) as *i64; a[0]=a1 as i64; a[1]=a2 as i64; a[2]=a3 as i64; a[3]=a4 as i64; a[4]=0; return a 59} 60func pg_row(name: *u8, pass: i64) -> i64 { 61 pg_p("ROW " as *u8); pg_p(name); if pass==1 { pg_p(" PASS\n" as *u8) } else { pg_p(" FAIL\n" as *u8) } return pass 62} 63 64func main(argc: i64, argv: *i64) -> i64 { 65 pg_p("=== PDF VIEWER GATE: real 634KB Cochrane PDF -> filtered text-layer preview + registry dispatch ===\n" as *u8) 66 let fixture: *u8="knowledge/fetched/pmc_cd004504.pdf" as *u8 67 68 var pass: i64=0; var r: i64=0 69 r=1 70 if pg_exists("_offc/nx_pdf_text.elf" as *u8)==0 { r=0 } 71 if pg_exists("_offc/nx_pdf.elf" as *u8)==0 { r=0 } 72 if pg_exists("_offc/nx_doc_view.elf" as *u8)==0 { r=0 } 73 if pg_exists(fixture)==0 { r=0 } 74 pass=pass+pg_row("instruments" as *u8, r) 75 76 // row 2: render 77 let rc2: i64=pg_runv("_offc/nx_pdf.elf" as *u8, pg_args4("html" as *u8, fixture, "/tmp/pg_pdf.html" as *u8, "/sample.pdf" as *u8), "/tmp/pg_r2.txt" as *u8) 78 r=0; if rc2==0 { if pg_has("/tmp/pg_r2.txt" as *u8, "PDF-HTML-OK path=/tmp/pg_pdf.html bytes=" as *u8)==1 { r=1 } } 79 pass=pass+pg_row("render-ok" as *u8, r) 80 81 // row 3: real content extracted 82 r=0 83 if pg_has("/tmp/pg_pdf.html" as *u8, "Cochrane Library" as *u8)==1 { 84 if pg_has("/tmp/pg_pdf.html" as *u8, "Herbal medicine for low" as *u8)==1 { r=1 } 85 } 86 pass=pass+pg_row("real-content" as *u8, r) 87 88 // row 4: the language filter killed the binary prefix -- readable content appears EARLY 89 r=pg_has_pre("/tmp/pg_pdf.html" as *u8, "Cochrane" as *u8, 3600) 90 pass=pass+pg_row("readable-head-filtered" as *u8, r) 91 92 // row 5: valid + sovereign 93 r=0 94 if pg_has_pre("/tmp/pg_pdf.html" as *u8, "<!DOCTYPE html>" as *u8, 64)==1 { 95 if pg_has("/tmp/pg_pdf.html" as *u8, "<script" as *u8)==0 { 96 if pg_has("/tmp/pg_pdf.html" as *u8, "Download .pdf" as *u8)==1 { r=1 } 97 } 98 } 99 pass=pass+pg_row("valid-0js-download" as *u8, r) 100 101 // row 6: registry dispatch routes .pdf 102 let rc6: i64=pg_runv("_offc/nx_doc_view.elf" as *u8, pg_args4(fixture, "/tmp/pg_dv.html" as *u8, "/sample.pdf" as *u8, 0 as *u8), "/tmp/pg_r6.txt" as *u8) 103 r=0; if rc6==0 { if pg_has("/tmp/pg_r6.txt" as *u8, "DOC-VIEW-OK ext=pdf" as *u8)==1 { if pg_has("/tmp/pg_dv.html" as *u8, "Cochrane Library" as *u8)==1 { r=1 } } } 104 pass=pass+pg_row("registry-dispatch-pdf" as *u8, r) 105 106 // row 7 (neg): missing pdf loud-fails 107 let rc7: i64=pg_runv("_offc/nx_pdf.elf" as *u8, pg_args4("html" as *u8, "/tmp/pg_nope.pdf" as *u8, "/tmp/pg_x.html" as *u8, 0 as *u8), "/tmp/pg_r7.txt" as *u8) 108 r=0; if rc7==1 { if pg_has("/tmp/pg_r7.txt" as *u8, "PDF-HTML-FAIL" as *u8)==1 { r=1 } } 109 pass=pass+pg_row("loud-fail-missing" as *u8, r) 110 111 let permil: i64=(pass*1000)/7 112 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 113 var fdi: i64=0 114 while fdi<2 { 115 var fd: i64=1; if fdi==1 { fd=logfd } 116 if fd>0 { 117 let line: *u8=sys_mmap(256); var o: i64=0 118 o=pg_cat(line,o,"PDF-VIEW-GATE epoch=" as *u8); o=pg_catn(line,o,sys_now_realtime_sec()) 119 o=pg_cat(line,o," rows=7 pass=" as *u8); o=pg_catn(line,o,pass) 120 o=pg_cat(line,o," permil=" as *u8); o=pg_catn(line,o,permil) 121 if pass==7 { o=pg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=pg_cat(line,o," verdict=RED\n" as *u8) } 122 sys_write(fd, line, o) 123 } 124 fdi=fdi+1 125 } 126 if logfd>0 { sys_close(logfd) } 127 if pass==7 { return 0 } 128 return 1 129}