1import{e as at,o as q,f as ct,r as Q}from"./handler-Bx_Dj2Np.js";const lt=new TextDecoder("latin1"),L=(t,i,n)=>lt.decode(t.subarray(i,n)),O=t=>t===0||t===9||t===10||t===12||t===13||t===32,ft=t=>t===40||t===41||t===60||t===62||t===91||t===93||t===123||t===125||t===47||t===37,b=t=>t>=48&&t<=57,X=t=>t!==void 0&&!O(t)&&!ft(t),y=t=>{const i=new Uint8Array(t.length);for(let n=0;n<t.length;n++)i[n]=t.charCodeAt(n)&255;return i},dt=y("obj"),tt=y("endstream");function _(t,i,n,e=t.length){const r=i[0],o=i.length;let c=n;for(;;){if(c=t.indexOf(r,c),c<0||c+o>e)return-1;let s=1;for(;s<o&&t[c+s]===i[s];)s++;if(s===o)return c;c++}}const et=(t,i,n)=>{let e=0;for(let r=i;r<n;r++)e=e*10+(t[r]-48);return e};function ut(t,i){let n=i;for(;;){if(n=_(t,dt,n),n<0)return null;const e=n+3;if(X(t[e])){n=e;continue}let r=n-1;if(r<0||!O(t[r])){n=e;continue}for(;r>=0&&O(t[r]);)r--;const o=r+1;for(;r>=0&&b(t[r]);)r--;const c=r+1;if(c===o||r<0||!O(t[r])){n=e;continue}for(;r>=0&&O(t[r]);)r--;const s=r+1;for(;r>=0&&b(t[r]);)r--;const f=r+1;if(f===s){n=e;continue}return{num:et(t,f,s),gen:et(t,c,o),start:f,bodyAt:e}}}function Z(t,i){let n=0,e=i;const r=t.length;for(;e<r;){const o=t[e];if(o===92){e+=2;continue}if(o===40)n++;else if(o===41&&--n===0)return e+1;e++}return r}function ht(t,i){const n=[],e=t.length;let r=i,o=null;for(;r<e;){const c=t[r];if(O(c)){r++;continue}if(c===37){for(;r<e&&t[r]!==10&&t[r]!==13;)r++;continue}if(c===40){const l=Z(t,r);n.push({s:r,e:l,hex:!1,key:o}),o=null,r=l;continue}if(c===60){if(t[r+1]===60){r+=2,o=null;continue}const l=t.indexOf(62,r+1);if(l<0)return{strings:n,end:e,kind:"eof"};n.push({s:r,e:l+1,hex:!0,key:o}),o=null,r=l+1;continue}if(c===47){let l=r+1;for(;l<e&&X(t[l]);)l++;o=L(t,r+1,l),r=l;continue}if(ft(c)){r++;continue}let s=r;for(;s<e&&X(t[s]);)s++;const f=s-r;if(f===6&&L(t,r,s)==="stream")return{strings:n,end:r,kind:"stream"};if(f===6&&L(t,r,s)==="endobj")return{strings:n,end:r,kind:"endobj"};if(f===3&&t[r]===111&&t[r+1]===98&&t[r+2]===106)return{strings:n,end:r,kind:"next"};r=s}return{strings:n,end:e,kind:"eof"}}function xt(t,i){let n=0,e=i;const r=t.length;for(;e<r;){const o=t[e];if(o===40){e=Z(t,e);continue}if(o===60&&t[e+1]===60){n++,e+=2;continue}if(o===62&&t[e+1]===62){if(n--,e+=2,n===0)return e;continue}if(o===60){const c=t.indexOf(62,e+1);e=c<0?r:c+1;continue}e++}return r}const gt=/\/Length\s+(\d+)(?:\s+(\d+)\s+R)?(?![\w.])/;function nt(t,i,n,e,r){if(n<=i)return;const o=L(t,i,n),c=/\/Encrypt(?![A-Za-z0-9#_.-])\s*(?:(\d+)\s+(\d+)\s+R|<<)/g;let s;for(;s=c.exec(o);){const h=i+s.index;if(s[1]!==void 0)e.encryptRefs.push({s:h,e:h+s[0].length,num:+s[1],gen:+s[2]});else{const p=h+s[0].length-2,u=xt(t,p);e.encryptRefs.push({s:h,e:u,direct:{from:p,to:u}}),c.lastIndex=u-i}}const f=/\/ID\s*\[\s*(<|\()/g;for(;s=f.exec(o);){const h=i+s.index+s[0].length-1,p=s[1]==="<"?G(t,h+1,t.indexOf(62,h+1)):J(t,h+1,Z(t,h)-1);p&&!e.ids.some(u=>mt(u,p))&&e.ids.push(p)}const l=/\/Root\s+(\d+\s+\d+\s+R)/.exec(o);if(l||r||/trailer/.test(o)){const h=/\/Info\s+(\d+\s+\d+\s+R)/.exec(o),p=/\/Size\s+(\d+)/.exec(o),u=/\/ID\s*\[[^\]]*\]/.exec(o);e.trailers.push({at:i,to:n,xrefStream:!!r,root:l&&l[1],info:h&&h[1],size:p?+p[1]:0,id:u&&!u[0].includes("(")?u[0]:null})}}function pt(t){const i={objects:[],encryptRefs:[],ids:[],trailers:[],streamRanges:[]},n=t.length;let e=0;for(;;){const c=ut(t,e);if(nt(t,e,c?c.start:n,i,!1),!c)break;const s=ht(t,c.bodyAt),f={num:c.num,gen:c.gen,start:c.start,bodyAt:c.bodyAt,dictEnd:s.end,strings:s.strings,stream:null,dictText:""};if(i.objects.push(f),s.kind!=="stream"){s.strings.length&&(f.dictText=L(t,f.bodyAt,f.dictEnd)),e=s.kind==="endobj"?s.end+6:s.kind==="next"?s.end:n,s.kind==="endobj"&&(f.endobjAt=s.end);continue}f.dictText=L(t,f.bodyAt,f.dictEnd);let l=s.end+6;t[l]===13&&l++,t[l]===10&&l++;const h={dataStart:l,endstreamAt:-1,direct:null,ref:null,lenAt:-1,lenLen:0},p=gt.exec(f.dictText);if(p&&(h.lenAt=f.bodyAt+p.index,h.lenLen=p[0].length,p[2]!==void 0?h.ref={num:+p[1],gen:+p[2]}:h.direct=+p[1]),h.direct!==null&&l+h.direct<=n){let u=l+h.direct;for(;u<n&&O(t[u]);)u++;_(t,tt,u,u+9)===u&&(h.endstreamAt=u)}h.endstreamAt<0&&(h.endstreamAt=_(t,tt,l)),h.endstreamAt<0&&(h.endstreamAt=n),f.stream=h,i.streamRanges.push([l,h.endstreamAt]),/\/Type\s*\/XRef\b/.test(f.dictText)&&(f.isXRef=!0,nt(t,f.bodyAt,f.dictEnd,i,!0),i.trailers[i.trailers.length-1].at=f.start),e=h.endstreamAt+9}const o=[...L(t,Math.max(0,n-2048),n).matchAll(/startxref\s+(\d+)/g)];i.startxref=o.length?+o[o.length-1][1]:-1,i.lastDef=new Map;for(const c of i.objects)i.lastDef.set(`${c.num} ${c.gen}`,c);return i}const mt=(t,i)=>t.length===i.length&&t.every((n,e)=>n===i[e]);function G(t,i,n){if(n<i)return null;const e=[];for(let c=i;c<n;c++){const s=t[c];(s>=48&&s<=57||s>=65&&s<=70||s>=97&&s<=102)&&e.push(s)}e.length%2&&e.push(48);
1const r=new Uint8Array(e.length/2),o=c=>c<=57?c-48:(c|32)-87;for(let c=0;c<r.length;c++)r[c]=o(e[2*c])<<4|o(e[2*c+1]);return r}function J(t,i,n){const e=[],r={110:10,114:13,116:9,98:8,102:12};for(let o=i;o<n;o++){const c=t[o];if(c===92){const s=t[++o];if(o>=n)break;if(r[s]!==void 0)e.push(r[s]);else if(s>=48&&s<=55){let f=s-48;for(let l=0;l<2&&o+1<n&&t[o+1]>=48&&t[o+1]<=55;l++)f=f*8+(t[++o]-48);e.push(f&255)}else s===13?t[o+1]===10&&o++:s===10||e.push(s);continue}if(c===13){t[o+1]===10&&o++,e.push(10);continue}e.push(c)}return new Uint8Array(e)}function At(t){const i=new Uint8Array(t.length),n=[];for(let r=0;r<t.length;r++)t[r]===40?n.push(r):t[r]===41&&(n.length?n.pop():i[r]=1);for(const r of n)i[r]=1;const e=[40];for(let r=0;r<t.length;r++){const o=t[r];o===92?e.push(92,92):o===13?e.push(92,114):i[r]?e.push(92,o):e.push(o)}return e.push(41),Uint8Array.from(e)}const rt="0123456789ABCDEF";function it(t){const i=new Uint8Array(t.length*2+2);i[0]=60;for(let n=0;n<t.length;n++)i[1+2*n]=rt.charCodeAt(t[n]>>4),i[2+2*n]=rt.charCodeAt(t[n]&15);return i[i.length-1]=62,i}function wt(t){const i=t.split("");let n=0;for(let e=0;e<t.length-1;e++){if(t[e]==="<"&&t[e+1]==="<"){n++,n>=2&&(i[e]=" ",i[e+1]=" "),e++;continue}if(t[e]===">"&&t[e+1]===">"){n>=2&&(i[e]=" ",i[e+1]=" "),n--,e++;continue}n>=2&&(i[e]=" ")}return i.join("")}function B(t,i){const n=new RegExp(`/${i}\\s+(-?\\d+)`).exec(t);return n?+n[1]:null}function W(t,i){const n=new RegExp(`/${i}\\s*/([A-Za-z0-9]+)`).exec(t);return n?n[1]:null}function Et(t,i){const n=new RegExp(`/${i}\\s+(true|false)`).exec(t);return n?n[1]==="true":null}function K(t,i,n){const e=new RegExp(`/${n}(?![A-Za-z0-9])\\s*([(<])`).exec(t);if(!e)return null;const r=e.index+e[0].length-1;return e[1]==="<"?G(i,r+1,i.indexOf(62,r+1)):J(i,r+1,Z(i,r)-1)}function st(t,i,n){if(!i||i==="Identity")return"Identity";const e=new RegExp(`/${i}\\s*<<`).exec(t);if(e){const r=t.slice(e.index+e[0].length),o=r.indexOf(">>"),c=/\/CFM\s*\/([A-Za-z0-9]+)/.exec(o<0?r:r.slice(0,o));return c?c[1]==="None"?"Identity":c[1]:"Identity"}return n}function St(t,i){if(!i.encryptRefs.length)return{encrypted:!1};const n=i.encryptRefs.find(f=>!f.direct)||i.encryptRefs[0];let e;if(n.direct)e=t.subarray(n.direct.from,n.direct.to);else{const f=i.lastDef.get(`${n.num} ${n.gen}`)||[...i.objects].reverse().find(l=>l.num===n.num);if(!f)return{encrypted:!0,error:"the /Encrypt object is missing"};e=t.subarray(f.bodyAt,Math.min(f.dictEnd,f.bodyAt+65536))}const r=lt.decode(e),o=wt(r),c=W(o,"Filter");if(c&&c!=="Standard")return{encrypted:!0,error:`unsupported security handler /${c}`};const s={V:B(o,"V")??0,R:B(o,"R")??2,Length:B(o,"Length")??40,P:B(o,"P")??-1,EncryptMetadata:Et(o,"EncryptMetadata"),O:K(o,e,"O"),U:K(o,e,"U"),OE:K(o,e,"OE"),UE:K(o,e,"UE"),objNum:n.direct?-1:n.num};if(!s.O||!s.U)return{encrypted:!0,error:"the /Encrypt dictionary is incomplete"};if(s.V>=4){const f=/\/CFM\s*\/([A-Za-z0-9]+)/.exec(r),l=f?f[1]:s.V===5?"AESV3":"V2";s.stm=st(r,W(o,"StmF"),l),s.str=st(r,W(o,"StrF"),l)}else s.stm=s.str="V2";s.cfm=s.stm!=="Identity"?s.stm:s.str;for(const f of[s.stm,s.str])if(!["V2","AESV2","AESV3","Identity"].includes(f))return{encrypted:!0,error:`unsupported crypt filter /${f}`};return{encrypted:!0,enc:s}}async function $t(t,i=""){const n=pt(t),e=St(t,n);if(!e.encrypted)return{ok:!0,encrypted:!1,pages:null};if(e.error)return{ok:!1,encrypted:!0,reason:e.error};const{enc:r}=e,o=n.ids.length?[...n.ids,new Uint8Array(0)]:[new Uint8Array(0)];let c=null;for(const s of r.R>=5?o.slice(0,1):o)if(c=await at(r,s,i),c)break;return c?{ok:!0,encrypted:!0,as:c.as,method:`${r.cfm} R${r.R}`,apply:s=>Rt(t,n,r,c.key,c.as,s)}:{ok:!1,encrypted:!0,reason:"wrong password"}}async function Lt(t,i="",n={}){const e=await $t(t,i);return e.ok?e.encrypted?e.apply(n):{ok:!0,bytes:t,as:"none",method:"not encrypted"}:e}const ot=()=>new Promise(t=>setTimeout(t,0));async function yt(t,i){const n=globalThis.crypto&&globalThis.crypto.subtle;if(n&&i.length>=4096&&(i.length-16)%16===0)try{const e=await n.importKey("raw",t,"AES-CBC",!1,["decrypt"]),r=await n.decrypt({name:"AES-CBC",iv:i.subarray(0,16)},e,i.subarray(16));return new Uint8Array(r)}catch{}return ct(t,i)}async function Rt(t,i,n,e,r,{inPlace:o=!1,onProgress:c}={}){const s=n.stm==="AESV2"||n.stm==="AESV3",f=n.str==="AESV2"||n.str==="AESV3",l=o?t:new
1Uint8Array(t),h=t.length||1;let p=0,u=0,U=0,D=0;const $=new Map,V=x=>{let a=$.get(x);return a||$.set(x,a={reps:[],dataLen:-1}),a},T=new Map;for(const x of i.objects)if(x.stream&&x.stream.ref){const a=`${x.stream.ref.num} ${x.stream.ref.gen}`;T.set(a,(T.get(a)||0)+1)}const C=x=>{const a=i.lastDef.get(`${x.num} ${x.gen}`);if(!a||a.stream||a.endobjAt===void 0)return null;const w=L(t,a.bodyAt,a.endobjAt),R=/^(\s*)(\d+)\s*$/.exec(w);return R?{at:a.bodyAt+R[1].length,digits:R[2].length,value:+R[2]}:null},d=new Map;for(const x of T.keys()){const[a,w]=x.split(" ").map(Number);d.set(x,C({num:a,gen:w}))}let m=0;for(let x=0;x<i.objects.length;x++){const a=i.objects[x];if((x&4095)===4095&&(c&&c(Math.min(.99,a.start/h)),await ot()),a.num===n.objNum||a.isXRef)continue;let w=null,R=null;if(n.str!=="Identity"&&a.strings.length){const v=/\/ByteRange\b/.test(a.dictText);w=q(e,a.num,a.gen,f,n.R);for(const E of a.strings){if(v&&E.key==="Contents")continue;const N=E.hex?G(t,E.s+1,E.e-1):J(t,E.s+1,E.e-1);if(!N||!N.length)continue;const F=f?ct(w,N):Q(w,N);if(!F){D++;continue}U++;const Y=E.e-E.s;let z=At(F);z.length>Y&&(z=it(F)),z.length<=Y?(l.set(z,E.s),l.fill(32,E.s+z.length,E.e)):V(a).reps.push({s:E.s,e:E.e,bytes:it(F)})}}const g=a.stream;if(!g||n.stm==="Identity"||/\/Crypt\b/.test(a.dictText)&&/\/Identity\b/.test(a.dictText)||n.EncryptMetadata===!1&&/\/Type\s*\/Metadata\b/.test(a.dictText))continue;const H=g.ref?`${g.ref.num} ${g.ref.gen}`:null,I=H?d.get(H):null;let S;if(g.direct!==null&&g.dataStart+g.direct<=g.endstreamAt?S=g.direct:I&&g.dataStart+I.value<=g.endstreamAt?S=I.value:(S=g.endstreamAt-g.dataStart,s?S-=S%16:(t[g.dataStart+S-1]===10&&S--,t[g.dataStart+S-1]===13&&S--)),S<=0)continue;const P=t.subarray(g.dataStart,g.dataStart+S);p++,R=s===f&&w?w:q(e,a.num,a.gen,s,n.R);const M=s?await yt(R,P):Q(R,P);if(!M){u++;continue}if(l.set(M,g.dataStart),M.length<S){l.fill(32,g.dataStart+M.length,g.dataStart+S);const v=`/Length ${M.length}`;if(g.direct!==null&&g.lenAt>=0&&v.length<=g.lenLen)l.set(y(v.padEnd(g.lenLen," ")),g.lenAt);else if(I&&T.get(H)===1){const E=String(M.length).padEnd(I.digits," ");l.set(y(E),I.at)}else if(g.lenAt>=0){const E=V(a);E.reps.push({s:g.lenAt,e:g.lenAt+g.lenLen,bytes:y(v)}),E.dataLen=M.length}else u++}$.has(a)&&$.get(a).dataLen<0&&($.get(a).dataLen=M.length),m+=S,m>4*1024*1024&&(m=0,c&&c(Math.min(.99,a.start/h)),await ot())}const k=p;if(k&&u/k>.2)return{ok:!1,reason:"the password opened the file but the contents did not decrypt"};for(const x of i.encryptRefs)l.fill(32,x.s,x.e);for(const[x,a]of $){if(!x.stream||a.dataLen>=0)continue;const w=x.stream,R=w.ref?d.get(`${w.ref.num} ${w.ref.gen}`):null;a.dataLen=w.direct??(R?R.value:w.endstreamAt-w.dataStart)}let j=l,A=0;if($.size){const x=kt(l,i,$);x?(j=new Uint8Array(l.length+x.length),j.set(l),j.set(x,l.length),A=$.size):D+=[...$.values()].reduce((a,w)=>a+w.reps.length,0)}return c&&c(1),{ok:!0,bytes:j,as:r,method:`${n.cfm} R${n.R}`,streams:p,failed:u,strings:U,stringsFailed:D,appended:A}}function kt(t,i,n){const e=i.startxref;if(e<0||!i.trailers.length)return null;const r=i.trailers.find(d=>d.at>=e&&d.at<=e+64)||i.trailers[i.trailers.length-1],o=d=>r[d]||[...i.trailers].reverse().find(m=>m[d])?.[d],c=o("root");if(!c)return null;const s=o("info"),f=o("id");let l=Math.max(0,...i.trailers.map(d=>d.size));for(const d of i.objects)l=Math.max(l,d.num+1);const h=[];let p=t.length;const u=d=>{h.push(d),p+=d.length};t[t.length-1]!==10&&t[t.length-1]!==13&&u(y(` 2`));const U=[],D=[...n.entries()].sort((d,m)=>d[0].num-m[0].num);for(const[d,m]of D){U.push({num:d.num,gen:d.gen,offset:p}),u(y(`${d.num} ${d.gen} obj 3`));const k=m.reps.sort((A,x)=>A.s-x.s);let j=d.bodyAt;for(const A of k)u(t.slice(j,A.s)),u(A.bytes),j=A.e;if(u(t.slice(j,d.dictEnd)),d.stream){const A=m.dataLen;u(y(` 4stream 5`)),u(t.slice(d.stream.dataStart,d.stream.dataStart+A)),u(y(` 6endstream`))}u(y(` 7endobj 8`))}const $=`/Root ${c}`+(s?` /Info ${s}`:"")+(f?` ${f}`:"")+` /Prev ${e}`;if(r.xrefStream){const d=l;U.push({num:d,gen:0,offset:p});const m=new Uint8Array(U.length*7);U.forEach((A,x)=>{const a=x*7;m[a]=1,m[a+1]=A.offset>>>24&255,m[a+2]=A.offset>>>16&255,m[a+3]=A.offset>>>8&255,m[a+4]=A.offset&255,m[a+5]=A.gen>>8&255,m[a+6]=A.gen&255});const k=U.map(A=>`${A.num} 1`).join(" "),j=p;u(y(`${d} 0 obj 9<< /Type /XRef /Size ${d+1} /W [1 4 2] /Index [${k}] ${$} /Length ${m.length} >> 10stream 11`)),u(m),u(y(` 12endstream 13endobj 14startxref 15${j} 16%%EOF 17`))}else{const d=p;let m=`xref 18`;for(const k of U)m+=`${k.num} 1 19${String(k.offset).padStart(10,"0")} ${String(k.gen).padStart(5,"0")} n\r 20`;m+=`trailer 21<< /Size ${l} ${$} >> 22startxref 23${d} 24%%EOF 25`,u(y(m))}const V=h.reduce((d,m)=>d+m.length,0),T=new Uint8Array(V);let C=0;for(const d of h)T.set(d,C),C+=d.length;return T}function Ut(t){const i=L(t,Math.max(0,t.length-4096),t.length);return/\/Encrypt\s/.test(i)||/\/Encrypt\s/.test(L(t,0,Math.min(4096,t.length)))}export{Lt as decryptPdf,Ut as isEncrypted,$t as prepareDecrypt};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.