@mdgate/containers 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/archive.d.ts CHANGED
@@ -3,7 +3,6 @@ import { type Element } from './xml.js';
3
3
  /** A ZIP-based document package (OOXML, ODF, EPUB). */
4
4
  export declare class Package {
5
5
  private readonly zip;
6
- private totalRead;
7
6
  private readonly cache;
8
7
  private constructor();
9
8
  static open(bytes: Uint8Array): Package;
@@ -12,14 +11,14 @@ export declare class Package {
12
11
  * it exists but cannot be read.
13
12
  */
14
13
  part(name: string): Uint8Array | undefined;
15
- /** True when a part exists, without reading (or budget-charging) it. */
14
+ /** True when a part exists, without reading it. */
16
15
  hasPart(name: string): boolean;
17
16
  /** Part names present in the archive (no leading slash). */
18
17
  partNames(): string[];
19
18
  requiredPart(name: string): Uint8Array;
20
19
  /**
21
20
  * Absent is valid (`undefined`, silent); an unreadable part is skipped
22
- * with a log; fatal resource-limit errors always propagate.
21
+ * with a log.
23
22
  */
24
23
  optionalPart(name: string): Uint8Array | undefined;
25
24
  optionalXmlPart(name: string): Element | undefined;
package/dist/binary.d.ts CHANGED
@@ -1,12 +1,10 @@
1
1
  import type { CompoundFile } from './cfb.js';
2
- import { MAX_ENTRY_BYTES } from './limits.js';
3
2
  /** Little-endian `u16` at `off`; `undefined` when out of bounds. */
4
3
  export declare function getU16(b: Uint8Array, off: number): number | undefined;
5
4
  /** Little-endian `u32` at `off`; `undefined` when out of bounds. */
6
5
  export declare function getU32(b: Uint8Array, off: number): number | undefined;
7
6
  /**
8
7
  * Read a named stream from an OLE2 compound file. A missing stream is
9
- * `missingPart`; the read is hard-capped at `MAX_ENTRY_BYTES`.
8
+ * `missingPart`.
10
9
  */
11
10
  export declare function readOleStream(ole: CompoundFile, name: string): Uint8Array;
12
- export { MAX_ENTRY_BYTES };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,6 @@ export { Package, probeOle } from './archive.js';
2
2
  export { getU16, getU32, readOleStream } from './binary.js';
3
3
  export { CompoundFile, hasOleMagic, OLE_MAGIC_BYTES } from './cfb.js';
4
4
  export { detectOleDoc, detectZipDoc, type OleDocKind, type ZipDocKind, } from './detect.js';
5
- export { MAX_ENTRY_BYTES, MAX_ENTRY_COUNT, MAX_RECORD_DEPTH, MAX_RECORDS, MAX_TOTAL_BYTES, MAX_XML_DEPTH, MAX_XML_NODES, } from './limits.js';
6
5
  export { type MimeHeader, type MimePart, mimeAttachments, mimeHeader, mimeTextHtml, mimeTextPlain, parseMime, walkMimeParts, } from './mime.js';
7
6
  export { decodeFragment, resolve, type Target } from './path.js';
8
7
  export { type Relationship, Relationships, type RelTarget, readRels, relsPartFor, relTargetBytes, relType, type TargetMode, } from './relationships.js';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import{ConvertError as P}from"@mdgate/core";import{decode as n0,InflateLimitError as i0,inflateRaw as r0,warn as o6}from"@mdgate/utils";import{ConvertError as _}from"@mdgate/core";var B=134217728,Y6=536870912,n=1e5,i=256,r=2000000,A0=64,k0=16000000;var j6=[208,207,17,224,161,177,26,225],s=4294967295,v6=4294967294,_0=4294967293,w0=4294967292,g0=4294967295,M=4294967290,x6=0;var S6=2,T0=5;class C{bytes;sectorSize;miniSectorSize;miniCutoff;fat;miniFat;entries;miniStream;constructor($,J,Q,K,W,Z,G){this.bytes=$,this.sectorSize=J,this.miniSectorSize=Q,this.miniCutoff=K,this.fat=W,this.miniFat=Z,this.entries=G}static open($){if($.length<512||!h6($,j6))throw _.malformed("not an OLE2 compound file");let J=new DataView($.buffer,$.byteOffset,$.byteLength);if(J.getUint16(28,!0)!==65534)throw _.malformed("not an OLE2 compound file: bad byte order");let K=J.getUint16(30,!0);if(K!==9&&K!==12)throw _.malformed("not an OLE2 compound file: bad sector shift");let W=1<<K,Z=J.getUint16(32,!0),G=Z===0?64:1<<Z,U=J.getUint16(26,!0),D=J.getUint32(44,!0),V=J.getUint32(48,!0),H=J.getUint32(56,!0)||4096,Y=J.getUint32(60,!0),X=J.getUint32(64,!0),g=J.getUint32(68,!0),T=J.getUint32(72,!0),L=[];for(let F=0;F<109;F+=1){let q=J.getUint32(76+F*4,!0);if(q<=M)L.push(q)}let k=g,S=new Set;for(let F=0;F<T&&k<=M;F+=1){if(S.has(k))break;S.add(k);let q=F6(k,W),z=W/4;if(q+W>$.length)break;let o=new DataView($.buffer,$.byteOffset+q,W);for(let u=0;u<z-1;u+=1){let v=o.getUint32(u*4,!0);if(v<=M)L.push(v)}k=o.getUint32((z-1)*4,!0)}if(L.length<D);let j=[],H6=Math.min(L.length,D||L.length,1e6);for(let F=0;F<H6;F+=1){let q=L[F],z=F6(q,W);if(z+W>$.length)break;let o=new DataView($.buffer,$.byteOffset+z,W),u=W/4;for(let v=0;v<u;v+=1)j.push(o.getUint32(v*4,!0))}let R=Math.max(0,Math.floor(($.length+W-1)/W)-1);while(j.length>R){let F=j[j.length-1];if(F===0||F===w0||F===_0||F===s)j.pop();else break}while(j.length>R&&j[j.length-1]===s)j.pop();if(j.length>R)throw _.malformed(`not an OLE2 compound file: Malformed FAT (FAT has ${j.length} entries, but file has only ${R} sectors)`);while(j.length<R)j.push(s);let d=[];if(X>0&&Y<=M){let F=a($,j,W,Y,X*W),q=new DataView(F.buffer,F.byteOffset,F.byteLength);for(let z=0;z+4<=F.length;z+=4)d.push(q.getUint32(z,!0))}let y=a($,j,W,V,B),E=[];for(let F=0;F+128<=y.length;F+=128)if(E.push(S0(y,F,U)),E.length>1e5)break;if(E.length===0||E[0].type!==T0)throw _.malformed("not an OLE2 compound file: missing root storage");return new C($,W,G,H,j,d,E)}readRootStorage(){let $=this.entries[0];if($===void 0)return[];let J=[];return this.walkSiblings($.child,J),J}exists($){return this.lookup($)!==void 0}readStream($){let J=this.lookup($);if(J===void 0||J.type!==S6)throw _.missingPart($);if(J.size>B)throw _.resourceLimit("max_entry_bytes",`${$} stream exceeds the read cap`);let Q=this.readEntryBytes(J,B+1);if(Q.length>B)throw _.resourceLimit("max_entry_bytes",`${$} stream exceeds the read cap`);return Q}lookup($){let J=$.split("/").filter((W)=>W.length>0);if(J.length===0)return this.entries[0];let Q=this.entries[0]?.child??g0,K;for(let W of J){if(K=this.findChild(Q,W),K===void 0)return;Q=K.child}return K}findChild($,J){let Q=new Set,K=[$];while(K.length>0){let W=K.pop();if(W>M||Q.has(W))continue;Q.add(W);let Z=this.entries[W];if(Z===void 0||Z.type===x6)continue;if(x0(Z.name,J))return Z;K.push(Z.left,Z.right)}return}walkSiblings($,J){let Q=new Set,K=(W)=>{if(W>M||Q.has(W)||Q.size>1e5)return;Q.add(W);let Z=this.entries[W];if(Z===void 0||Z.type===x6)return;K(Z.left),J.push({name:Z.name,type:Z.type}),K(Z.right)};K($)}readEntryBytes($,J){let Q=Math.min($.size,J);if($.size<this.miniCutoff&&$.type===S6)return this.readMiniChain($.start,Q);return a(this.bytes,this.fat,this.sectorSize,$.start,Q)}readMiniChain($,J){if(this.miniStream===void 0){let G=this.entries[0];this.miniStream=a(this.bytes,this.fat,this.sectorSize,G.start,G.size)}let Q=new Uint8Array(J),K=0,W=$,Z=new Set;while(K<J&&W<=M){if(Z.has(W))break;Z.add(W);let G=W*this.miniSectorSize,U=Math.min(this.miniSectorSize,J-K);if(G+U>this.miniStream.length)break;Q.set(this.miniStream.subarray(G,G+U),K),K+=U,W=this.miniFat[W]??v6}return K===J?Q:Q.subarray(0,K)}}var C0=j6;function f($){return h6($,j6)}function h6($,J){if($.length<J.length)return!1;for(let Q=0;Q<J.length;Q+=1)if($[Q]!==J[Q])return!1;return!0}function x0($,J){return $.length===J.length&&$.toLowerCase()===J.toLowerCase()}function F6($,J){return($+1)*J}function a($,J,Q,K,W){if(K>M||W<=0)return new Uint8Array(0);let Z=[],G=0,U=K,D=new Set;while(G<W&&U<=M){if(D.has(U)||D.size>1e6)break;D.add(U);let V=F6(U,Q);if(V>=$.length)break;let H=Math.min(Q,W-G,$.length-V);if(H<=0)break;if(Z.push($.subarray(V,V+H)),G+=H,U=J[U]??v6,U===s)break}return v0(Z,G)}function S0($,J,Q){let K=new DataView($.buffer,$.byteOffset+J,128),W=K.getUint16(64,!0),Z=Math.max(0,Math.min(64,W>0?W-2:0)),G=new TextDecoder("utf-16le").decode($.subarray(J,J+Z)),U=$[J+66]??0,D=K.getUint32(68,!0),V=K.getUint32(72,!0),H=K.getUint32(76,!0),Y=K.getUint32(116,!0),X=Number(K.getBigUint64(120,!0));if(Q===3)X=K.getUint32(120,!0);if(!Number.isFinite(X)||X<0)X=0;return{name:G,type:U,left:D,right:V,child:H,start:Y,size:X}}function v0($,J){if($.length===1&&$[0].length===J)return $[0];let Q=new Uint8Array(J),K=0;for(let W of $)Q.set(W,K),K+=W.length;return Q}import{ConvertError as B6}from"@mdgate/core";import{debug as E6,decode as h0,encodingExists as E0,warn as u0}from"@mdgate/utils";var w={W:"http://schemas.openxmlformats.org/wordprocessingml/2006/main",R:"http://schemas.openxmlformats.org/officeDocument/2006/relationships",A:"http://schemas.openxmlformats.org/drawingml/2006/main",PIC:"http://schemas.openxmlformats.org/drawingml/2006/picture",WP:"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",MC:"http://schemas.openxmlformats.org/markup-compatibility/2006",CHART:"http://schemas.openxmlformats.org/drawingml/2006/chart",DGM:"http://schemas.openxmlformats.org/drawingml/2006/diagram",P:"http://schemas.openxmlformats.org/presentationml/2006/main",PKG_RELS:"http://schemas.openxmlformats.org/package/2006/relationships",OFFICE:"urn:oasis:names:tc:opendocument:xmlns:office:1.0",TEXT:"urn:oasis:names:tc:opendocument:xmlns:text:1.0",TABLE:"urn:oasis:names:tc:opendocument:xmlns:table:1.0",DRAW:"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",STYLE:"urn:oasis:names:tc:opendocument:xmlns:style:1.0",FO:"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",PRESENTATION:"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",MANIFEST:"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",XLINK:"http://www.w3.org/1999/xlink",XML:"http://www.w3.org/XML/1998/namespace",SVG_COMPAT:"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",VML:"urn:schemas-microsoft-com:vml",O_VML:"urn:schemas-microsoft-com:office:office",WPS:"http://schemas.microsoft.com/office/word/2010/wordprocessingShape",WPG:"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"},f0=w.XML,p0="http://www.w3.org/2000/xmlns/",p6=[];class $6{ns;local;attrs;children;_elems;constructor($,J,Q=p6,K=[]){this.ns=$,this.local=J,this.attrs=Q,this.children=K}is($,J){return this.local===J&&this.ns===$}attr($,J){return this.attrQualified($,J)??this.attrUnqualified(J)}attrQualified($,J){let Q=this.attrs;for(let K=0;K<Q.length;K+=1){let W=Q[K];if(W.local===J&&W.ns===$)return W.value}return}attrUnqualified($){let J=this.attrs;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.local===$&&K.ns===void 0)return K.value}return}attrAny($){let J=this.attrs;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.local===$)return K.value}return}childElems(){if(this._elems!==void 0)return this._elems;let $=[],J=this.children;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.type==="elem")$.push(K.elem)}return this._elems=$,$}find($,J){let Q=this.children;for(let K=0;K<Q.length;K+=1){let W=Q[K];if(W.type==="elem"&&W.elem.local===J&&W.elem.ns===$)return W.elem}return}findAll($,J){let Q=[],K=this.children;for(let W=0;W<K.length;W+=1){let Z=K[W];if(Z.type==="elem"&&Z.elem.local===J&&Z.elem.ns===$)Q.push(Z.elem)}return Q}*descendantNodes(){let $=this.children.slice();$.reverse();while($.length>0){let J=$.pop();if(J.type==="elem"){let Q=$.length;for(let K of J.elem.children)$.push(K);y0($,Q)}yield J}}*descendantElems(){for(let $ of this.descendantNodes())if($.type==="elem")yield $.elem}firstDescendant($,J){for(let Q of this.descendantElems())if(Q.is($,J))return Q;return}*descendants($,J){for(let Q of this.descendantElems())if(Q.is($,J))yield Q}*descendantsAny($){for(let J of this.descendantElems())if(J.local===$)yield J}text(){let $="";for(let J of this.descendantNodes())if(J.type==="text")$+=J.text;return $}}class l6{prefixes=[new Map];ownsMap=[!0];defaultNs=[void 0];push(){this.prefixes.push(this.prefixes[this.prefixes.length-1]),this.ownsMap.push(!1),this.defaultNs.push(this.defaultNs[this.defaultNs.length-1])}pop(){this.prefixes.pop(),this.ownsMap.pop(),this.defaultNs.pop()}applyDecls($){for(let J of $)if(J.rawName==="xmlns")this.defaultNs[this.defaultNs.length-1]=J.value.length===0?void 0:J.value;else if(J.rawName.startsWith("xmlns:")){if(!this.ownsMap[this.ownsMap.length-1])this.prefixes[this.prefixes.length-1]=new Map(this.prefixes[this.prefixes.length-1]),this.ownsMap[this.ownsMap.length-1]=!0;let Q=J.rawName.slice(6);this.prefixes[this.prefixes.length-1].set(Q,J.value.length===0?void 0:J.value)}}lookupPrefix($){if($==="xml")return f0;if($==="xmlns")return p0;return this.prefixes[this.prefixes.length-1].get($)}resolveElement($){let J=$.indexOf(":");if(J>=0){let K=$.slice(0,J),W=$.slice(J+1),Z=this.lookupPrefix(K);return{ns:Z,local:W,bound:Z!==void 0||K==="xml"||K==="xmlns"}}let Q=this.defaultNs[this.defaultNs.length-1];return{ns:Q,local:$,bound:Q!==void 0}}resolveAttribute($){let J=$.indexOf(":");if(J>=0){let Q=$.slice(0,J),K=$.slice(J+1);return{ns:this.lookupPrefix(Q),local:K}}return{ns:void 0,local:$}}}function l($){let J=o0($,"http://purl.oclc.org/ooxml/");if(J===void 0)return;let Q=J.indexOf("/");if(Q<0)return;let K=J.slice(0,Q),W=J.slice(Q+1);return`http://schemas.openxmlformats.org/${K}/2006/${W}`}var m6=0,c6=1,b6=2,p=3,L6=4,d6=5;function J6($){let J=m0($),Q=new y6(J),K=new l6,W=new Map,Z=new Map,G=new $6(void 0,""),U=[],D=0,V=!1;for(;;){let H;try{H=Q.next()}catch(Y){let X=Y instanceof Error?Y.message:String(Y);throw B6.malformed(`unparseable xml: ${X}`)}switch(H){case c6:{if(!Q.empty&&U.length>=i)throw B6.resourceLimit("max_xml_depth",`element nesting exceeds ${i}`);t(D),D+=1,K.push(),K.applyDecls(Q.attrs);let Y=l0(Q.name,Q.attrs,K,W,Z);if(Q.empty)P6(U,G,{type:"elem",elem:Y}),K.pop();else U.push(Y);break}case b6:{let Y=U.pop();if(Y===void 0)V=!0;else{if(Y.local!==Q.local)V=!0;P6(U,G,{type:"elem",elem:Y}),K.pop()}break}case p:{if(Q.text.length>0)t(D),D+=1,X6(U,G,Q.text);break}case L6:{let Y=O6(Q.name)??`&${Q.name};`;t(D),D+=1,X6(U,G,Y);break}case d6:{t(D),D+=1,X6(U,G,Q.text);break}case m6:if(U.length>0){V=!0;while(U.length>0)P6(U,G,{type:"elem",elem:U.pop()})}if(V)u0("recovered malformed xml (unclosed or mismatched elements)");return G}}}function t($){if($+1>r)throw B6.resourceLimit("max_xml_nodes",`part exceeds ${r} xml nodes`)}function P6($,J,Q){let K=$[$.length-1];if(K!==void 0)K.children.push(Q);else J.children.push(Q)}function X6($,J,Q){let K=$[$.length-1]?.children??J.children,W=K[K.length-1];if(W?.type==="text")W.text+=Q;else K.push({type:"text",text:Q})}function u6($,J){let Q=$.get(J);if(Q!==void 0)return Q;let K=l(J)??J;return $.set(J,K),K}function f6($,J){let Q=$.get(J);if(Q!==void 0)return Q;return $.set(J,J),J}function l0($,J,Q,K,W){let Z=Q.resolveElement($),G=Z.ns!==void 0?u6(K,Z.ns):void 0,U=p6,D=!1;for(let V of J){if(V.rawName==="xmlns"||V.rawName.startsWith("xmlns:"))continue;let H=Q.resolveAttribute(V.rawName);if(!D)U=[],D=!0;U.push({ns:H.ns!==void 0?u6(K,H.ns):void 0,local:f6(W,H.local),value:V.value})}if(Z.local==="Choice"&&G===w.MC){let V=U.find((H)=>H.local==="Requires");if(V!==void 0)V.value=V.value.split(/\s+/).filter((H)=>H.length>0).map((H)=>{let Y=Q.resolveElement(`${H}:x`);if(Y.bound&&Y.ns!==void 0)return l(Y.ns)??Y.ns;return H}).join(" ")}return new $6(G,f6(W,Z.local),U)}function m0($){if($.length>=2&&$[0]===255&&$[1]===254)return new TextDecoder("utf-16le").decode($);if($.length>=2&&$[0]===254&&$[1]===255)return new TextDecoder("utf-16be").decode($);if($.length>=3&&$[0]===239&&$[1]===187&&$[2]===191)return new TextDecoder("utf-8").decode($.subarray(3));let J=$.subarray(0,Math.min($.length,200)),Q=c0(J);if(Q!==void 0){let K=b0(Q);if(K!==void 0&&K!=="utf8"&&K!=="utf-8")return h0($,K)}return new TextDecoder("utf-8").decode($)}function c0($){let J;try{J=new TextDecoder("utf-8",{fatal:!0}).decode($)}catch{return}let Q=J.indexOf("encoding");if(Q<0)return;let K=J.slice(Q+8).trimStart();if(!K.startsWith("="))return;K=K.slice(1).trimStart();let W=K[0];if(W!=='"'&&W!=="'")return;K=K.slice(1);let Z=K.indexOf(W);if(Z<0)return;return K.slice(0,Z)}function b0($){let J=$.trim().toLowerCase().replace(/_/g,"-"),K={"utf-8":"utf8",utf8:"utf8","utf-16":"utf16-le","utf-16le":"utf16-le","utf-16be":"utf16-be","iso-8859-1":"iso-8859-1","latin-1":"iso-8859-1",latin1:"iso-8859-1","windows-1252":"windows-1252","cp-1252":"windows-1252",cp1252:"windows-1252","windows-1251":"windows-1251","windows-1250":"windows-1250","windows-874":"windows-874",gbk:"gbk",gb2312:"gb2312",gb18030:"gb18030",big5:"big5","shift-jis":"shiftjis",shiftjis:"shiftjis","euc-jp":"euc-jp","euc-kr":"euc-kr",koi8r:"koi8-r","koi8-r":"koi8-r"}[J]??J;return E0(K)?K:void 0}function O6($){if($.startsWith("#")){let Q=$.slice(1),K=Q.startsWith("x")||Q.startsWith("X"),W=K?Q.slice(1):Q,Z=Number.parseInt(W,K?16:10);if(!Number.isFinite(Z))return;try{return String.fromCodePoint(Z)}catch{return}}return{amp:"&",lt:"<",gt:">",apos:"'",quot:'"',nbsp:" ",shy:"­",mdash:"—",ndash:"–",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",hellip:"…",copy:"©",reg:"®",trade:"™",deg:"°",middot:"·",bull:"•",sect:"§",para:"¶",laquo:"«",raquo:"»",times:"×",divide:"÷",plusmn:"±",frac12:"½",frac14:"¼",eacute:"é",egrave:"è",agrave:"à",ccedil:"ç",uuml:"ü",ouml:"ö",auml:"ä",szlig:"ß",aring:"å",oslash:"ø",aelig:"æ",euro:"€",pound:"£",yen:"¥",cent:"¢"}[$]}class y6{s;i=0;name="";local="";text="";empty=!1;attrs=[];constructor($){this.s=$}next(){if(this.i>=this.s.length)return m6;if(this.s.charCodeAt(this.i)===60)return this.markup();return this.textRun()}markup(){if(this.startsWith("<?"))return this.skipUntil("?>"),this.next();if(this.startsWith("<!--")){this.i+=4;let $=this.s.indexOf("-->",this.i);if($<0)throw Error("unterminated comment");return this.i=$+3,this.next()}if(this.startsWith("<![CDATA[")){this.i+=9;let $=this.s.indexOf("]]>",this.i);if($<0)throw Error("unterminated cdata");return this.text=this.s.slice(this.i,$),this.i=$+3,d6}if(this.startsWith("<!"))return this.skipDoctype(),this.next();if(this.startsWith("</")){this.i+=2;let $=this.readName();if(this.skipWs(),this.s[this.i]===">")this.i+=1;return this.local=d0($),b6}if(this.i+=1,this.name=this.readName(),this.attrs=this.readAttrs(),this.skipWs(),this.empty=!1,this.s[this.i]==="/"&&this.s[this.i+1]===">")this.i+=2,this.empty=!0;else if(this.s[this.i]===">")this.i+=1;return c6}textRun(){let $=this.i;while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===60)break;if(J===38){if(this.i>$)return this.text=this.s.slice($,this.i),p;let Q=this.readReference();if(Q.type==="general")return this.name=Q.name,L6;return this.i=$,this.textRunSlow()}this.i+=1}return this.text=this.s.slice($,this.i),p}textRunSlow(){let $="";while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===60)break;if(J===38){let K=this.readReference();if(K.type==="general"){if($.length>0)return this.i=K.rewind,this.text=$,p;return this.name=K.name,L6}$+=K.text;continue}let Q=this.i;this.i+=1;while(this.i<this.s.length){let K=this.s.charCodeAt(this.i);if(K===60||K===38)break;this.i+=1}$+=this.s.slice(Q,this.i)}return this.text=$,p}readReference(){let $=this.i;this.i+=1;let J=this.s.indexOf(";",this.i);if(J<0)return{type:"char",text:"&"};let Q=this.s.slice(this.i,J);if(this.i=J+1,Q.startsWith("#")||Q==="amp"||Q==="lt"||Q==="gt"||Q==="apos"||Q==="quot")return{type:"char",text:O6(Q)??`&${Q};`};return{type:"general",name:Q,rewind:$}}readName(){let $=this.i;while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===47||J===62||J===61||J===32||J===9||J===10||J===13)break;this.i+=1}return this.s.slice($,this.i)}readAttrs(){let $=[];for(;;){this.skipWs();let J=this.s[this.i];if(J===void 0||J===">"||J==="/")break;let Q=this.readName();if(Q.length===0){this.i+=1;continue}if(this.skipWs(),this.s[this.i]!=="="){E6(`dropping undecodable attribute: ${Q}`);continue}this.i+=1,this.skipWs();let K=this.readAttrValue();if(K===void 0){E6(`dropping undecodable attribute: ${Q}`);continue}$.push({rawName:Q,value:K})}return $}readAttrValue(){let $=this.s.charCodeAt(this.i);if($!==34&&$!==39)return;this.i+=1;let J=this.i,Q=!1;while(this.i<this.s.length){let W=this.s.charCodeAt(this.i);if(W===$){let Z=this.s.slice(J,this.i);return this.i+=1,Q?e(Z):Z}if(W===38)return this.i=J,this.readAttrValueSlow($);if(W===9||W===10||W===13)Q=!0;this.i+=1}let K=this.s.slice(J,this.i);return Q?e(K):K}readAttrValueSlow($){let J="";while(this.i<this.s.length){let Q=this.s.charCodeAt(this.i);if(Q===$)return this.i+=1,e(J);if(Q===38){let W=this.readReference();if(W.type==="char")J+=W.text;else J+=O6(W.name)??`&${W.name};`;continue}let K=this.i;this.i+=1;while(this.i<this.s.length){let W=this.s.charCodeAt(this.i);if(W===$||W===38)break;this.i+=1}J+=this.s.slice(K,this.i)}return e(J)}skipWs(){while(this.i<this.s.length){let $=this.s.charCodeAt(this.i);if($!==32&&$!==9&&$!==10&&$!==13)break;this.i+=1}}startsWith($){return this.s.startsWith($,this.i)}skipUntil($){let J=this.s.indexOf($,this.i);this.i=J<0?this.s.length:J+$.length}skipDoctype(){this.i+=2;let $=0;while(this.i<this.s.length){let J=this.s[this.i];if(J==='"'||J==="'"){let Q=J;this.i+=1;while(this.i<this.s.length&&this.s[this.i]!==Q)this.i+=1;if(this.i<this.s.length)this.i+=1;continue}if(J==="<")$+=1;else if(J===">"){if($===0){this.i+=1;return}$-=1}this.i+=1}}}function d0($){let J=$.indexOf(":");return J<0?$:$.slice(J+1)}function e($){if($.indexOf("\t")<0&&$.indexOf(`
2
- `)<0&&$.indexOf("\r")<0)return $;return $.replace(/[\t\n\r]/g," ")}function y0($,J){let Q=J,K=$.length-1;while(Q<K){let W=$[Q];$[Q]=$[K],$[K]=W,Q+=1,K-=1}}function o0($,J){return $.startsWith(J)?$.slice(J.length):void 0}var a0=67324752,s0=33639248;var t0=101075792,e0=117853008,$$=0,J$=8;class N6{length;bytes;byName=new Map;constructor($,J){this.bytes=$,this.length=J.length;for(let Q of J)if(!this.byName.has(Q.name))this.byName.set(Q.name,Q)}static open($){let J=W$($);if(J<0)throw P.malformed("not a readable zip archive: invalid Zip archive: Could not find EOCD");let Q=Q6($),K=O(Q,J+10),W=N(Q,J+12),Z=N(Q,J+16);if(K===65535||W===4294967295||Z===4294967295){let V=Z$($,J);if(V!==void 0)K=V.entryCount,W=V.cdSize,Z=V.cdOffset}if(K>n);if(Z+W>$.length)throw P.malformed("not a readable zip archive: truncated central directory");let G=[],U=Z,D=Math.min($.length,Z+W);while(U+46<=D&&G.length<K){if(N(Q,U)!==s0)throw P.malformed("not a readable zip archive: bad central directory");let V=O(Q,U+8),H=O(Q,U+10),Y=N(Q,U+16),X=N(Q,U+20),g=N(Q,U+24),T=O(Q,U+28),L=O(Q,U+30),k=O(Q,U+32),S=N(Q,U+42),j=$.subarray(U+46,U+46+T);if(U+46+T+L+k>$.length)throw P.malformed("not a readable zip archive: truncated entry");let H6=$.subarray(U+46+T,U+46+T+L),R=G$(H6,{uncompressedSize:g,compressedSize:X,headerOffset:S});g=R.uncompressedSize,X=R.compressedSize,S=R.headerOffset;let d=(V&2048)!==0,y=U$(j,d);G.push({name:y,method:H,flags:V,crc:Y,compressedSize:X,uncompressedSize:g,headerOffset:S}),U+=46+T+L+k}return new N6($,G)}indexForName($){return this.byName.get($)}partNames(){return[...this.byName.keys()]}readCompressed($){let J=Q6(this.bytes),Q=$.headerOffset;if(Q+30>this.bytes.length||N(J,Q)!==a0)throw Error("bad local file header");let K=O(J,Q+26),W=O(J,Q+28),Z=Q+30+K+W,G=Z+$.compressedSize;if(Z>this.bytes.length)throw Error("truncated local file header");if($.compressedSize>0&&G>this.bytes.length)throw Error("truncated compressed data");return this.bytes.subarray(Z,Math.min(G,this.bytes.length))}}class m{zip;totalRead=0;cache=new Map;constructor($){this.zip=$}static open($){let J;try{J=N6.open($)}catch(Q){if(Q instanceof P)throw Q;let K=Q instanceof Error?Q.message:String(Q);throw P.malformed(`not a readable zip archive: ${K}`)}if(J.length>n)throw P.resourceLimit("max_entry_count",`archive contains ${J.length} entries`);return new m(J)}part($){$=n6($);let J=this.cache.get($);if(J!==void 0)return J;let Q=this.zip.indexForName($);if(Q===void 0)return;if(Q.uncompressedSize>B)throw P.resourceLimit("max_entry_bytes",`${$} declares ${Q.uncompressedSize} decompressed bytes`);let K=Math.max(0,Y6-this.totalRead),W=Math.min(B,K),Z;try{Z=this.decompress(Q,W+1)}catch(G){if(G instanceof P)throw G;let U=G instanceof Error?G.message:String(G);throw P.malformedPart($,`corrupt archive entry: ${U}`)}if(Z.length>W){if(K<B)throw P.resourceLimit("max_total_bytes",`${$} exceeds the archive's remaining decompression budget`);throw P.resourceLimit("max_entry_bytes",`${$} exceeds the decompression cap`)}return this.totalRead+=Z.length,this.cache.set($,Z),Z}hasPart($){return this.zip.indexForName(n6($))!==void 0}partNames(){return this.zip.partNames()}requiredPart($){let J=this.part($);if(J===void 0)throw P.missingPart($);return J}optionalPart($){try{return this.part($)}catch(J){if(J instanceof P&&J.isFatal())throw J;o6(`skipping unreadable part ${$}: ${J instanceof Error?J.message:String(J)}`);return}}optionalXmlPart($){let J=this.optionalPart($);if(J===void 0)return;try{return J6(J)}catch(Q){if(Q instanceof P&&Q.isFatal())throw Q;o6(`skipping corrupt part ${$}: ${Q instanceof Error?Q.message:String(Q)}`);return}}requiredXmlPart($){return J6(this.requiredPart($))}decompress($,J){if(($.flags&1)!==0)throw Error("encrypted zip entry");let Q;try{Q=this.zip.readCompressed($)}catch(W){let Z=W instanceof Error?W.message:String(W);throw P.malformedPart($.name,`unreadable archive entry: ${Z}`)}let K;if($.method===$$){let W=$.uncompressedSize>0?$.uncompressedSize:Q.length;K=Q.subarray(0,Math.min(W,J,Q.length))}else if($.method===J$)K=K$(Q,J);else throw Error(`unsupported compression method ${$.method}`);if(K.length<=J-1&&$.crc!==0&&Y$(K)!==$.crc)throw Error("crc mismatch");return K}}function Q$($){if(!f($))return;try{let J=C.open($);if(J.exists("EncryptionInfo")||J.exists("EncryptedPackage"))return P.encrypted()}catch{}return P.malformed("OLE compound document where an OOXML package was expected (legacy binary format?)")}function K$($,J){try{return r0($,J)}catch(Q){if(Q instanceof i0)return new Uint8Array(J);throw Q}}function W$($){let J=Math.max(0,$.length-65557);for(let Q=$.length-22;Q>=J;Q-=1)if($[Q]===80&&$[Q+1]===75&&$[Q+2]===5&&$[Q+3]===6){let K=$[Q+20]|$[Q+21]<<8;if(Q+22+K===$.length)return Q}return-1}function Z$($,J){if(J<20)return;let Q=Q6($),K=J-20;if(N(Q,K)!==e0)return;let W=x(Q,K+8);if(W+56>$.length||N(Q,W)!==t0)return;return{entryCount:x(Q,W+32),cdSize:x(Q,W+40),cdOffset:x(Q,W+48)}}function G$($,J){let Q=0,K=Q6($);while(Q+4<=$.length){let W=O(K,Q),Z=O(K,Q+2),G=Q+4;if(G+Z>$.length)break;if(W===1){let U=G,{uncompressedSize:D,compressedSize:V,headerOffset:H}=J;if(D===4294967295&&U+8<=G+Z)D=x(K,U),U+=8;if(V===4294967295&&U+8<=G+Z)V=x(K,U),U+=8;if(H===4294967295&&U+8<=G+Z)H=x(K,U);return{uncompressedSize:D,compressedSize:V,headerOffset:H}}Q=G+Z}return J}function U$($,J){if(J)return new TextDecoder("utf-8",{fatal:!1}).decode($);return n0($,"cp437")}function n6($){let J=0;while(J<$.length&&$.charCodeAt(J)===47)J+=1;return $.slice(J)}function Q6($){return new DataView($.buffer,$.byteOffset,$.byteLength)}function O($,J){return $.getUint16(J,!0)}function N($,J){return $.getUint32(J,!0)}function x($,J){let Q=$.getUint32(J,!0),K=$.getUint32(J+4,!0);if(K>2097151)return Number.POSITIVE_INFINITY;return K*4294967296+Q}var D$=V$();function V$(){let $=new Uint32Array(256);for(let J=0;J<256;J+=1){let Q=J;for(let K=0;K<8;K+=1)Q=Q&1?3988292384^Q>>>1:Q>>>1;$[J]=Q>>>0}return $}function H$($){let J=4294967295;for(let Q=0;Q<$.length;Q+=1)J=D$[(J^$[Q])&255]^J>>>8;return(J^4294967295)>>>0}function Y$($){return H$($)}import{ConvertError as i6}from"@mdgate/core";function F$($,J){if(!Number.isSafeInteger(J)||J<0||J+2>$.length)return;return $[J]|$[J+1]<<8}function j$($,J){if(!Number.isSafeInteger(J)||J<0||J+4>$.length)return;return($[J]|$[J+1]<<8|$[J+2]<<16|$[J+3]<<24)>>>0}function P$($,J){try{return $.readStream(J)}catch(Q){if(Q instanceof i6)throw Q;let K=Q instanceof Error?Q.message:String(Q);throw i6.malformedPart(J,`unreadable stream: ${K}`)}}import{trim as $0}from"@mdgate/utils";import{ConvertError as r6}from"@mdgate/core";function c($,J){let Q,K=J.indexOf("#");if(K>=0)Q=I6(J.slice(K+1)),J=J.slice(0,K);let W=J.indexOf("?");if(W>=0)J=J.slice(0,W);if(J.length===0)return{path:$,fragment:Q};let Z=[];if(!J.startsWith("/")){let G=$.lastIndexOf("/");if(G>=0){for(let U of $.slice(0,G).split("/"))if(U.length>0)Z.push(U)}}for(let G of J.split("/")){if(G.length===0||G===".")continue;if(G===".."){Z.pop();continue}let U=I6(G);if(U.includes("/")||U.includes("\\"))throw r6.malformed(`percent-encoded separator in package reference segment ${t6(G)}`);if(U==="."||U==="..")throw r6.malformed(`percent-encoded traversal in package reference segment ${t6(G)}`);Z.push(U)}return{path:Z.join("/"),fragment:Q}}function X$($){return I6($)}var B$=new TextEncoder;function I6($){let J=B$.encode($),Q=[],K=0;while(K<J.length){if(J[K]===37&&K+2<J.length){let W=J[K+1],Z=J[K+2];if(a6(W)&&a6(Z)){Q.push(s6(W)<<4|s6(Z)),K+=3;continue}}Q.push(J[K]),K+=1}return new TextDecoder("utf-8",{fatal:!1}).decode(Uint8Array.from(Q))}function a6($){return $>=48&&$<=57||$>=65&&$<=70||$>=97&&$<=102}function s6($){if($>=48&&$<=57)return $-48;if($>=65&&$<=70)return $-55;return $-87}function t6($){return JSON.stringify($)}import{warn as e6}from"@mdgate/utils";var R6={OFFICE_DOCUMENT:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",STYLES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",NUMBERING:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",FOOTNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",ENDNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"};class K6{map;constructor($=new Map){this.map=$}get($){return this.map.get($)}internalTarget($){let J=this.map.get($);return J!==void 0&&J.mode==="internal"?J.target:void 0}entries(){return this.map.entries()}firstOfType($){let J,Q;for(let[K,W]of this.map)if(W.relType===$&&W.mode==="internal"){if(J===void 0||K<J)J=K,Q=W}return Q}}function q6($,J){let Q=$.optionalXmlPart(J);if(Q===void 0)return new K6;let K=new Map;for(let W of Q.descendants(w.PKG_RELS,"Relationship")){let Z=W.attrAny("Id"),G=W.attrAny("Target");if(Z===void 0||G===void 0)continue;let U=W.attrAny("TargetMode"),D=U!==void 0&&U.toLowerCase()==="external"?"external":"internal",V=W.attrAny("Type")??"",H=l(V)??V;K.set(Z,{target:G,relType:H,mode:D})}return new K6(K)}function L$($,J,Q,K){let W=J.get(K);if(W===void 0||W.mode!=="internal")return;let Z;try{Z=c(Q,W.target)}catch(U){e6(`skipping unresolvable relationship target ${JSON.stringify(W.target)}: ${U instanceof Error?U.message:String(U)}`);return}let G=$.optionalPart(Z.path);if(G===void 0){e6(`relationship target ${Z.path} is missing`);return}return[Z.path,G]}function O$($){let J=$.lastIndexOf("/");if(J>=0)return`${$.slice(0,J)}/_rels/${$.slice(J+1)}.rels`;return`_rels/${$}.rels`}var J0="http://schemas.openxmlformats.org/package/2006/content-types",z6="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",N$="http://schemas.openxmlformats.org/spreadsheetml/2006/main";function I$($){if($.length<4||$[0]!==80||$[1]!==75||$[2]!==3||$[3]!==4)return;try{return q$($)}catch{return}}function R$($){if(!f($))return;let J;try{J=C.open($)}catch{return}try{for(let Q of J.readRootStorage()){let K=Q.name;if(I(K,"WordDocument"))return"doc";if(I(K,"PowerPoint Document"))return"ppt";if(I(K,"Workbook")||I(K,"Book"))return"xls";if(I(K,"VisioDocument"))return"vsd";if(g$(K))return"hwp";if(T$(K))return"one"}}catch{return}return}function q$($){let J;try{J=m.open($)}catch{return}let Q=M$(J,"mimetype");if(Q!==void 0){let W=C$(Q);if(W===void 0)return;return Q0($0(W))}try{let Z=q6(J,"_rels/.rels").firstOfType(R6.OFFICE_DOCUMENT);if(Z!==void 0){let G;try{G=c("",Z.target)}catch{G=void 0}if(G!==void 0){let U=M6(J,"[Content_Types].xml");if(U!==void 0){let V=A$(U,G.path);if(V!==void 0){let H=k$(V);if(H!==void 0)return H}}let D=M6(J,G.path);if(D!==void 0){let V=D.childElems()[0],H=V!==void 0?_$(V):void 0;if(H!==void 0)return H}return w$(G.path)}}}catch{}for(let[W,Z]of[["word/document.xml","docx"],["ppt/presentation.xml","pptx"],["xl/workbook.xml","xlsx"],["xl/workbook.bin","xlsx"],["visio/document.xml","vsdx"],["Contents/content.hpf","hwpx"]])if(J.hasPart(W))return Z;try{let W=M6(J,"META-INF/manifest.xml");if(W!==void 0){for(let Z of W.descendants(z6,"file-entry"))if(Z.attr(z6,"full-path")==="/"){let G=Z.attr(z6,"media-type");if(G!==void 0)return Q0($0(G))}}}catch{}if(J.hasPart("META-INF/container.xml"))return"epub";let K=z$(J);if(K!==void 0)return K;return}function z$($){let J=$.partNames();if(J.length===0)return;let Q=!1,K=!1,W=!1,Z=!1,G=!1;for(let U of J){let D=U.toLowerCase();if(D.includes("encrypted")||D.endsWith(".iwae"))return;if(D.endsWith(".iwa")){if(Q=!0,D.endsWith("/document.iwa")||D==="document.iwa")W=!0;if(D.includes("slide")||D.includes("masterslide"))Z=!0;if(D.includes("calculationengine")||D.includes("tables/"))G=!0}if(D==="index.zip")K=!0}if(!Q&&!K)return;if(!Q&&K)return;if(!W&&!K&&!Z&&!G)return;if(Z&&!G)return"keynote";if(G&&!Z)return"numbers";if(W)return"pages";return}function M$($,J){try{return $.optionalPart(J)}catch{return}}function M6($,J){try{return $.optionalXmlPart(J)}catch{return}}function Q0($){switch($.endsWith("-template")?$.slice(0,-9):$){case"application/epub+zip":return"epub";case"application/vnd.oasis.opendocument.text":return"odt";case"application/vnd.oasis.opendocument.spreadsheet":return"ods";case"application/vnd.oasis.opendocument.presentation":return"odp";case"application/vnd.oasis.opendocument.graphics":return"odg";case"application/hwp+zip":case"application/vnd.hancom.hwpx":return"hwpx";default:return}}function A$($,J){let Q=`/${J}`;for(let Z of $.descendants(J0,"Override")){let G=Z.attrAny("PartName");if(G!==void 0&&I(G,Q)){let U=Z.attrAny("ContentType");if(U!==void 0)return U}}let K=J.lastIndexOf(".");if(K<0)return;let W=J.slice(K+1);for(let Z of $.descendants(J0,"Default")){let G=Z.attrAny("Extension");if(G!==void 0&&I(G,W))return Z.attrAny("ContentType")}return}function k$($){let J=$.toLowerCase();if(J.includes("wordprocessingml"))return"docx";if(J.includes("presentationml"))return"pptx";if(J.includes("spreadsheetml")||J.includes("ms-excel"))return"xlsx";if(J.includes("visio")||J.includes("vnd.ms-visio"))return"vsdx";return}function _$($){switch($.ns){case w.W:return"docx";case w.P:return"pptx";case N$:return"xlsx";default:return}}function w$($){if($.startsWith("word/"))return"docx";if($.startsWith("ppt/"))return"pptx";if($.startsWith("xl/"))return"xlsx";if($.startsWith("visio/"))return"vsdx";return}function g$($){let J=$.charCodeAt(0)===5?$.slice(1):$;return I($,"FileHeader")||I(J,"HwpSummaryInformation")||I($,"HWPDocumentInfo")}function T$($){return $.length>=7&&$.slice(0,7).toLowerCase()==="onenote"}function I($,J){return $.length===J.length&&$.toLowerCase()===J.toLowerCase()}function C$($){try{return new TextDecoder("utf-8",{fatal:!0}).decode($)}catch{return}}import{ConvertError as A}from"@mdgate/core";import{asciiStartsWith as F0,decode as K0,trim as h}from"@mdgate/utils";var W0=32,W6=1e4,b=262144;function x$($){let J={parts:0};if(u$($)){let Q=f$($);if(Q.length>1||Q.length===1&&F0($,"From ")){let K=Q.map((W)=>Z6(W,1,J));return{contentType:"application/mbox",headers:[],filename:void 0,bytes:new Uint8Array(0),parts:K}}}return Z6(E$($),1,J)}function S$($){let J=[],Q=(K)=>{J.push(K);for(let W of K.parts)Q(W)};return Q($),J}function j0($){let J=L0($);if(J!==void 0){let Q=O0($,J);if(Q!==void 0&&Z0(Q)&&!k6(Q))return Q}return C6($,(Q)=>Z0(Q)&&!k6(Q))}function P0($){return C6($,(J)=>J.contentType==="text/plain"&&J.parts.length===0&&!k6(J))}function v$($){let J=j0($),Q=P0($),K=[],W=(Z)=>{if(Z.parts.length>0){for(let G of Z.parts)W(G);return}if(Z===J||Z===Q)return;if(X0(Z.contentType)||B0(Z.contentType))return;K.push(Z)};return W($),K}function V6($,J){let Q=J.toLowerCase();for(let K=$.headers.length-1;K>=0;K-=1)if($.headers[K].name===Q)return $.headers[K].value;return}function Z6($,J,Q){h$(Q,J);let K=l$($),W=c$(K.headers),Z=_6(W,"content-type")??"text/plain",G=I0(Z),U=y$(W,G.params),D=(_6(W,"content-transfer-encoding")??"7bit").trim().toLowerCase(),V=a$(K.body,D);if(X0(G.type)){let H=G.params.get("boundary");if(H===void 0||H.length===0)return{contentType:G.type,headers:W,filename:U,bytes:V,parts:[]};let X=e$(V,H).map((g)=>Z6(g,J+1,Q));return{contentType:G.type,headers:W,filename:U,bytes:new Uint8Array(0),parts:X}}if(B0(G.type)){let H=Z6(V,J+1,Q);return{contentType:G.type,headers:W,filename:U,bytes:V,parts:[H]}}return{contentType:G.type,headers:W,filename:U,bytes:V,parts:[]}}function h$($,J){if(J>W0)throw A.resourceLimit("max_mime_depth",`part nesting exceeds ${W0}`);if($.parts+=1,$.parts>W6)throw A.resourceLimit("max_mime_parts",`message exceeds ${W6} parts`)}function X0($){return $.startsWith("multipart/")}function B0($){return $==="message/rfc822"||$==="message/global"||$==="message/news"}function Z0($){return $.contentType==="text/html"&&$.parts.length===0}function k6($){let J=V6($,"content-disposition");if(J===void 0)return!1;let Q=J.indexOf(";");return(Q<0?J:J.slice(0,Q)).trim().toLowerCase()==="attachment"}function C6($,J){if(J($))return $;for(let Q of $.parts){let K=C6(Q,J);if(K!==void 0)return K}return}function L0($){let J=V6($,"content-type");if($.contentType==="multipart/related"&&J!==void 0){let Q=I0(J).params.get("start");if(Q!==void 0&&Q.length>0)return N0(Q)}for(let Q of $.parts){let K=L0(Q);if(K!==void 0)return K}return}function O0($,J){let Q=V6($,"content-id");if(Q!==void 0&&N0(Q)===J)return $;for(let K of $.parts){let W=O0(K,J);if(W!==void 0)return W}return}function N0($){let J=h($);if(J.startsWith("<")&&J.endsWith(">")&&J.length>=2)J=J.slice(1,-1);return J}function _6($,J){let Q=J.toLowerCase();for(let K=$.length-1;K>=0;K-=1)if($[K].name===Q)return $[K].value;return}function E$($){if($.length>=3&&$[0]===239&&$[1]===187&&$[2]===191)return $.subarray(3);return $}function u$($){return F0($,"From ")}function f$($){let J=[],Q=0;while(Q<$.length){if(w6($,Q))J.push(Q);Q=G6($,Q)}if(J.length===0)return[];let K=[];for(let W=0;W<J.length;W+=1){let Z=J[W],G=G6($,Z),U=W+1<J.length?J[W+1]:$.length;if(G>U)continue;K.push(p$($.subarray(G,U)))}return K}function w6($,J){return J+5<=$.length&&$[J]===70&&$[J+1]===114&&$[J+2]===111&&$[J+3]===109&&$[J+4]===32}function p$($){let J=0,Q=0;while(Q<$.length){if($[Q]===62&&w6($,Q+1))J+=1;Q=G6($,Q)}if(J===0)return $;let K=new Uint8Array($.length-J),W=0,Z=0;while(W<$.length){if($[W]===62&&w6($,W+1))W+=1;let G=G6($,W);K.set($.subarray(W,G),Z),Z+=G-W,W=G}return K.subarray(0,Z)}function G6($,J){while(J<$.length){let Q=$[J];if(J+=1,Q===10)return J;if(Q===13){if(J<$.length&&$[J]===10)J+=1;return J}}return J}function l$($){let J=m$($);if(J!==void 0){if(J.sep>b)throw A.resourceLimit("max_mime_headers",`header block exceeds ${b} bytes`);return{headers:$.subarray(0,J.sep),body:$.subarray(J.body)}}if($.length>b&&G0($))throw A.resourceLimit("max_mime_headers",`header block exceeds ${b} bytes`);if(G0($))return{headers:$,body:$.subarray($.length)};return{headers:$.subarray(0,0),body:$}}function m$($){let J=Math.min($.length,b+4);for(let Q=0;Q<J;Q+=1){let K=$[Q];if(K===10&&Q+1<$.length&&$[Q+1]===10)return{sep:Q+1,body:Q+2};if(K===13&&Q+1<$.length&&$[Q+1]===10){if(Q+3<$.length&&$[Q+2]===13&&$[Q+3]===10)return{sep:Q+2,body:Q+4};if(Q+2<$.length&&$[Q+2]===10)return{sep:Q+2,body:Q+3}}if(K===13&&Q+1<$.length&&$[Q+1]===13)return{sep:Q+1,body:Q+2}}return}function G0($){let J=0;while(J<$.length&&($[J]===32||$[J]===9))J+=1;if(J>=$.length)return!1;let Q=J;while(J<$.length&&$[J]!==13&&$[J]!==10&&$[J]!==58)J+=1;if(J>=$.length||$[J]!==58||J===Q)return!1;for(let K=Q;K<J;K+=1){let W=$[K];if(!(W>=65&&W<=90||W>=97&&W<=122||W>=48&&W<=57||W===45))return!1}return!0}function c$($){if($.length===0)return[];let J=b$(M0($)),Q=[],K=0;while(K<J.length){let W=J.indexOf(`
3
- `,K);if(W<0)W=J.length;let Z=J.slice(K,W);if(Z.endsWith("\r"))Z=Z.slice(0,-1);if(K=W+1,Z.length===0)continue;let G=Z.indexOf(":");if(G<=0)continue;let U=Z.slice(0,G).trim().toLowerCase();if(U.length===0)continue;let D=o$(h(Z.slice(G+1)));Q.push({name:U,value:D})}return Q}function b$($){let J="";for(let Q=0;Q<$.length;Q+=1){let K=$[Q];if(K==="\r"&&$[Q+1]===`
4
- `&&g6($[Q+2])){J+=" ",Q+=2;continue}if((K===`
5
- `||K==="\r")&&g6($[Q+1])){J+=" ",Q+=1;continue}J+=K}return J}function g6($){return $===" "||$==="\t"}function I0($){let J=T6($,";"),Q=h(J<0?$:$.slice(0,J)).toLowerCase(),K=Q.length>0?Q:"application/octet-stream",W=R0(J<0?"":$.slice(J+1));return{type:K,params:W}}function R0($){let J=new Map,Q=0;while(Q<$.length){while(Q<$.length&&($[Q]===";"||$[Q]===" "||$[Q]==="\t"))Q+=1;if(Q>=$.length)break;let K=$.slice(Q),W=T6(K,";"),Z=h(W<0?K:K.slice(0,W));Q+=W<0?K.length:W+1;let G=T6(Z,"=");if(G<=0)continue;let U=h(Z.slice(0,G)).toLowerCase();if(U.length===0)continue;J.set(U,d$(h(Z.slice(G+1))))}return J}function T6($,J){let Q=!1;for(let K=0;K<$.length;K+=1){let W=$[K];if(Q){if(W==="\\"){K+=1;continue}if(W==='"')Q=!1;continue}if(W==='"'){Q=!0;continue}if(W===J)return K}return-1}function d$($){if($.length>=2&&$.startsWith('"')&&$.endsWith('"')){let J="";for(let Q=1;Q<$.length-1;Q+=1){if($[Q]==="\\"&&Q+1<$.length-1){Q+=1,J+=$[Q];continue}J+=$[Q]}return J}return $}function y$($,J){let Q=_6($,"content-disposition");if(Q!==void 0){let K=U0(R0(Q));if(K!==void 0)return K}return U0(J)}function U0($){let J=D0($,"filename")??$.get("filename*");if(J!==void 0){let Z=V0(J);if(Z.length>0)return Z}let Q=$.get("filename");if(Q!==void 0&&Q.length>0)return Q;let K=D0($,"name")??$.get("name*");if(K!==void 0){let Z=V0(K);if(Z.length>0)return Z}let W=$.get("name");return W!==void 0&&W.length>0?W:void 0}function D0($,J){if(!$.has(`${J}*0`)&&!$.has(`${J}*0*`))return;let Q="";for(let K=0;K<64;K+=1){let W=$.get(`${J}*${K}*`);if(W!==void 0){Q+=W;continue}let Z=$.get(`${J}*${K}`);if(Z===void 0)break;Q+=Z}return Q.length>0?Q:void 0}function V0($){let J=$.indexOf("'");if(J<0)return A6($,"utf-8");let Q=$.indexOf("'",J+1);if(Q<0)return A6($,"utf-8");let K=$.slice(0,J)||"utf-8";return A6($.slice(Q+1),K)}function A6($,J){let Q=new Uint8Array($.length),K=0;for(let W=0;W<$.length;W+=1){if($[W]==="%"&&W+2<$.length&&U6($[W+1])&&U6($[W+2])){Q[K]=D6($[W+1])<<4|D6($[W+2]),K+=1,W+=2;continue}Q[K]=$.charCodeAt(W)&255,K+=1}return q0(Q.subarray(0,K),J)}function o$($){let J=/=\?([^?]+)\?([bBqQ])\?([^?]*)\?=/g,Q="",K=0,W=-1,Z;while((Z=J.exec($))!==void 0&&Z!==null){let G=Z.index,U=$.slice(K,G);if(W>=0&&n$(U));else Q+=U;Q+=i$(Z[1],Z[2],Z[3]),K=G+Z[0].length,W=K}if(K===0)return $;return Q+$.slice(K)}function n$($){for(let J=0;J<$.length;J+=1)if(!g6($[J]))return!1;return $.length>0}function i$($,J,Q){let K=J.toUpperCase(),W;if(K==="B")W=z0(new TextEncoder().encode(Q));else W=r$(Q);return q0(W,$)}function r$($){let J=new Uint8Array($.length),Q=0;for(let K=0;K<$.length;K+=1){let W=$[K];if(W==="_"){J[Q]=32,Q+=1;continue}if(W==="="&&K+2<$.length&&U6($[K+1])&&U6($[K+2])){J[Q]=D6($[K+1])<<4|D6($[K+2]),Q+=1,K+=2;continue}J[Q]=$.charCodeAt(K)&255,Q+=1}return J.subarray(0,Q)}function q0($,J){try{return K0($,J)}catch{try{return K0($,"utf-8")}catch{return M0($)}}}function a$($,J){if(J==="base64")return z0($);if(J==="quoted-printable")return s$($);return $}function s$($){if($.length>B)throw A.resourceLimit("max_entry_bytes","quoted-printable body exceeds the read cap");let J=new Uint8Array($.length),Q=0;for(let K=0;K<$.length;K+=1){let W=$[K];if(W===61){let Z=$[K+1],G=$[K+2];if(Z===13&&G===10){K+=2;continue}if(Z===10||Z===13){K+=1;continue}if(Z!==void 0&&G!==void 0&&H0(Z)&&H0(G)){J[Q]=Y0(Z)<<4|Y0(G),Q+=1,K+=2;continue}}if(W===9||W===32){let Z=K+1;while(Z<$.length&&($[Z]===9||$[Z]===32))Z+=1;if(Z<$.length&&($[Z]===13||$[Z]===10)){K=Z-1;continue}}J[Q]=W,Q+=1}return J.subarray(0,Q)}var t$=(()=>{let $=new Uint8Array(128).fill(255),J="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(let Q=0;Q<64;Q+=1)$["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(Q)]=Q;return $[45]=62,$[95]=63,$})();function z0($){let J=Math.floor($.length/4)*3+3;if(J>B&&$.length>B)throw A.resourceLimit("max_entry_bytes","base64 body exceeds the read cap");let Q=Math.min(J,B+1),K=new Uint8Array(Q),W=0,Z=0,G=0;for(let U=0;U<$.length;U+=1){let D=$[U];if(D===61)break;if(D>=128)continue;let V=t$[D];if(V===255)continue;if(W=W<<6|V,Z+=6,Z>=8){if(Z-=8,G>=Q)throw A.resourceLimit("max_entry_bytes","base64 body exceeds the read cap");K[G]=W>>Z&255,G+=1}}if(G>B)throw A.resourceLimit("max_entry_bytes","base64 body exceeds the read cap");return K.subarray(0,G)}function e$($,J){if(J.length===0)return[];let Q=new TextEncoder().encode(`--${J}`),K=[],W=[],Z=0;while(Z+Q.length<=$.length){let U=KJ($,Q,Z);if(U<0)break;if(!$J($,U)){Z=U+1;continue}K.push(U);let D=U+Q.length;if(W.push($[D]===45&&$[D+1]===45),Z=D,K.length>W6+2)throw A.resourceLimit("max_mime_parts",`message exceeds ${W6} parts`)}let G=[];for(let U=0;U<K.length;U+=1){if(W[U])break;let D=JJ($,K[U]+Q.length),V=U+1<K.length?K[U+1]:-1;if(V<0)break;G.push($.subarray(D,QJ($,D,V)))}return G}function $J($,J){if(J===0)return!0;let Q=$[J-1];return Q===10||Q===13}function JJ($,J){if($[J]===45&&$[J+1]===45)J+=2;while(J<$.length&&($[J]===32||$[J]===9))J+=1;if($[J]===13)J+=1;if($[J]===10)J+=1;return J}function QJ($,J,Q){let K=Q;if(K>J&&$[K-1]===10)K-=1;if(K>J&&$[K-1]===13)K-=1;return K}function KJ($,J,Q){if(J.length===0)return Q;let K=J[0],W=Q+($.length-J.length);for(let Z=Q;Z<=W;Z+=1){if($[Z]!==K)continue;let G=!0;for(let U=1;U<J.length;U+=1)if($[Z+U]!==J[U]){G=!1;break}if(G)return Z}return-1}function M0($){if($.length<=4096)return String.fromCharCode(...$);let Q=[];for(let K=0;K<$.length;K+=4096)Q.push(String.fromCharCode(...$.subarray(K,K+4096)));return Q.join("")}function U6($){return $>="0"&&$<="9"||$>="a"&&$<="f"||$>="A"&&$<="F"}function D6($){if($>="0"&&$<="9")return $.charCodeAt(0)-48;if($>="a"&&$<="f")return $.charCodeAt(0)-87;return $.charCodeAt(0)-55}function H0($){return $>=48&&$<=57||$>=65&&$<=70||$>=97&&$<=102}function Y0($){if($>=48&&$<=57)return $-48;if($>=65&&$<=70)return $-55;return $-87}export{S$ as walkMimeParts,c as resolve,O$ as relsPartFor,R6 as relType,L$ as relTargetBytes,q6 as readRels,P$ as readOleStream,Q$ as probeOle,J6 as parseXml,x$ as parseMime,w as ns,l as normalizeOoxmlUri,P0 as mimeTextPlain,j0 as mimeTextHtml,V6 as mimeHeader,v$ as mimeAttachments,f as hasOleMagic,j$ as getU32,F$ as getU16,I$ as detectZipDoc,R$ as detectOleDoc,X$ as decodeFragment,K6 as Relationships,m as Package,C0 as OLE_MAGIC_BYTES,r as MAX_XML_NODES,i as MAX_XML_DEPTH,Y6 as MAX_TOTAL_BYTES,A0 as MAX_RECORD_DEPTH,k0 as MAX_RECORDS,n as MAX_ENTRY_COUNT,B as MAX_ENTRY_BYTES,$6 as Element,C as CompoundFile};
1
+ import{ConvertError as j}from"@mdgate/core";import{decode as E0,inflateRaw as v0,warn as h6}from"@mdgate/utils";import{ConvertError as g}from"@mdgate/core";var Y6=[208,207,17,224,161,177,26,225],y=4294967295,A6=4294967294,B0=4294967293,j0=4294967292,P0=4294967295,O=4294967290,I6=0;var O6=2,M0=5;class T{bytes;sectorSize;miniSectorSize;miniCutoff;fat;miniFat;entries;miniStream;constructor($,J,Q,K,Y,W,G){this.bytes=$,this.sectorSize=J,this.miniSectorSize=Q,this.miniCutoff=K,this.fat=Y,this.miniFat=W,this.entries=G}static open($){if($.length<512||!q6($,Y6))throw g.malformed("not an OLE2 compound file");let J=new DataView($.buffer,$.byteOffset,$.byteLength);if(J.getUint16(28,!0)!==65534)throw g.malformed("not an OLE2 compound file: bad byte order");let K=J.getUint16(30,!0);if(K!==9&&K!==12)throw g.malformed("not an OLE2 compound file: bad sector shift");let Y=1<<K,W=J.getUint16(32,!0),G=W===0?64:1<<W,Z=J.getUint16(26,!0),X=J.getUint32(44,!0),U=J.getUint32(48,!0),D=J.getUint32(56,!0)||4096,B=J.getUint32(60,!0),P=J.getUint32(64,!0),S=J.getUint32(68,!0),z=J.getUint32(72,!0),M=[];for(let V=0;V<109;V+=1){let L=J.getUint32(76+V*4,!0);if(L<=O)M.push(L)}let A=S,w=new Set;for(let V=0;V<z&&A<=O;V+=1){if(w.has(A))break;w.add(A);let L=K6(A,Y),I=Y/4;if(L+Y>$.length)break;let c=new DataView($.buffer,$.byteOffset+L,Y);for(let E=0;E<I-1;E+=1){let C=c.getUint32(E*4,!0);if(C<=O)M.push(C)}A=c.getUint32((I-1)*4,!0)}if(M.length<X);let H=[],Q6=Math.min(M.length,X||M.length,1e6);for(let V=0;V<Q6;V+=1){let L=M[V],I=K6(L,Y);if(I+Y>$.length)break;let c=new DataView($.buffer,$.byteOffset+I,Y),E=Y/4;for(let C=0;C<E;C+=1)H.push(c.getUint32(C*4,!0))}let _=Math.max(0,Math.floor(($.length+Y-1)/Y)-1);while(H.length>_){let V=H[H.length-1];if(V===0||V===j0||V===B0||V===y)H.pop();else break}while(H.length>_&&H[H.length-1]===y)H.pop();if(H.length>_)throw g.malformed(`not an OLE2 compound file: Malformed FAT (FAT has ${H.length} entries, but file has only ${_} sectors)`);while(H.length<_)H.push(y);let l=[];if(P>0&&B<=O){let V=d($,H,Y,B,P*Y),L=new DataView(V.buffer,V.byteOffset,V.byteLength);for(let I=0;I+4<=V.length;I+=4)l.push(L.getUint32(I,!0))}let m=d($,H,Y,U,$.length),b=[];for(let V=0;V+128<=m.length;V+=128)b.push(R0(m,V,Z));if(b.length===0||b[0].type!==M0)throw g.malformed("not an OLE2 compound file: missing root storage");return new T($,Y,G,D,H,l,b)}readRootStorage(){let $=this.entries[0];if($===void 0)return[];let J=[];return this.walkSiblings($.child,J),J}exists($){return this.lookup($)!==void 0}readStream($){let J=this.lookup($);if(J===void 0||J.type!==O6)throw g.missingPart($);return this.readEntryBytes(J,Number.MAX_SAFE_INTEGER)}lookup($){let J=$.split("/").filter((Y)=>Y.length>0);if(J.length===0)return this.entries[0];let Q=this.entries[0]?.child??P0,K;for(let Y of J){if(K=this.findChild(Q,Y),K===void 0)return;Q=K.child}return K}findChild($,J){let Q=new Set,K=[$];while(K.length>0){let Y=K.pop();if(Y>O||Q.has(Y))continue;Q.add(Y);let W=this.entries[Y];if(W===void 0||W.type===I6)continue;if(N0(W.name,J))return W;K.push(W.left,W.right)}return}walkSiblings($,J){let Q=new Set,K=(Y)=>{if(Y>O||Q.has(Y))return;Q.add(Y);let W=this.entries[Y];if(W===void 0||W.type===I6)return;K(W.left),J.push({name:W.name,type:W.type}),K(W.right)};K($)}readEntryBytes($,J){let Q=Math.min($.size,J);if($.size<this.miniCutoff&&$.type===O6)return this.readMiniChain($.start,Q);return d(this.bytes,this.fat,this.sectorSize,$.start,Q)}readMiniChain($,J){if(this.miniStream===void 0){let G=this.entries[0];this.miniStream=d(this.bytes,this.fat,this.sectorSize,G.start,G.size)}let Q=new Uint8Array(J),K=0,Y=$,W=new Set;while(K<J&&Y<=O){if(W.has(Y))break;W.add(Y);let G=Y*this.miniSectorSize,Z=Math.min(this.miniSectorSize,J-K);if(G+Z>this.miniStream.length)break;Q.set(this.miniStream.subarray(G,G+Z),K),K+=Z,Y=this.miniFat[Y]??A6}return K===J?Q:Q.subarray(0,K)}}var F0=Y6;function v($){return q6($,Y6)}function q6($,J){if($.length<J.length)return!1;for(let Q=0;Q<J.length;Q+=1)if($[Q]!==J[Q])return!1;return!0}function N0($,J){return $.length===J.length&&$.toLowerCase()===J.toLowerCase()}function K6($,J){return($+1)*J}function d($,J,Q,K,Y){if(K>O||Y<=0)return new Uint8Array(0);let W=[],G=0,Z=K,X=new Set;while(G<Y&&Z<=O){if(X.has(Z)||X.size>1e6)break;X.add(Z);let U=K6(Z,Q);if(U>=$.length)break;let D=Math.min(Q,Y-G,$.length-U);if(D<=0)break;if(W.push($.subarray(U,U+D)),G+=D,Z=J[Z]??A6,Z===y)break}return _0(W,G)}function R0($,J,Q){let K=new DataView($.buffer,$.byteOffset+J,128),Y=K.getUint16(64,!0),W=Math.max(0,Math.min(64,Y>0?Y-2:0)),G=new TextDecoder("utf-16le").decode($.subarray(J,J+W)),Z=$[J+66]??0,X=K.getUint32(68,!0),U=K.getUint32(72,!0),D=K.getUint32(76,!0),B=K.getUint32(116,!0),P=Number(K.getBigUint64(120,!0));if(Q===3)P=K.getUint32(120,!0);if(!Number.isFinite(P)||P<0)P=0;return{name:G,type:Z,left:X,right:U,child:D,start:B,size:P}}function _0($,J){if($.length===1&&$[0].length===J)return $[0];let Q=new Uint8Array(J),K=0;for(let Y of $)Q.set(Y,K),K+=Y.length;return Q}import{ConvertError as L0}from"@mdgate/core";import{debug as z6,decode as I0,encodingExists as O0,warn as A0}from"@mdgate/utils";var q={W:"http://schemas.openxmlformats.org/wordprocessingml/2006/main",R:"http://schemas.openxmlformats.org/officeDocument/2006/relationships",A:"http://schemas.openxmlformats.org/drawingml/2006/main",PIC:"http://schemas.openxmlformats.org/drawingml/2006/picture",WP:"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",MC:"http://schemas.openxmlformats.org/markup-compatibility/2006",CHART:"http://schemas.openxmlformats.org/drawingml/2006/chart",DGM:"http://schemas.openxmlformats.org/drawingml/2006/diagram",P:"http://schemas.openxmlformats.org/presentationml/2006/main",PKG_RELS:"http://schemas.openxmlformats.org/package/2006/relationships",OFFICE:"urn:oasis:names:tc:opendocument:xmlns:office:1.0",TEXT:"urn:oasis:names:tc:opendocument:xmlns:text:1.0",TABLE:"urn:oasis:names:tc:opendocument:xmlns:table:1.0",DRAW:"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",STYLE:"urn:oasis:names:tc:opendocument:xmlns:style:1.0",FO:"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",PRESENTATION:"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",MANIFEST:"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",XLINK:"http://www.w3.org/1999/xlink",XML:"http://www.w3.org/XML/1998/namespace",SVG_COMPAT:"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",VML:"urn:schemas-microsoft-com:vml",O_VML:"urn:schemas-microsoft-com:office:office",WPS:"http://schemas.microsoft.com/office/word/2010/wordprocessingShape",WPG:"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"},q0=q.XML,z0="http://www.w3.org/2000/xmlns/",w6=[];class n{ns;local;attrs;children;_elems;constructor($,J,Q=w6,K=[]){this.ns=$,this.local=J,this.attrs=Q,this.children=K}is($,J){return this.local===J&&this.ns===$}attr($,J){return this.attrQualified($,J)??this.attrUnqualified(J)}attrQualified($,J){let Q=this.attrs;for(let K=0;K<Q.length;K+=1){let Y=Q[K];if(Y.local===J&&Y.ns===$)return Y.value}return}attrUnqualified($){let J=this.attrs;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.local===$&&K.ns===void 0)return K.value}return}attrAny($){let J=this.attrs;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.local===$)return K.value}return}childElems(){if(this._elems!==void 0)return this._elems;let $=[],J=this.children;for(let Q=0;Q<J.length;Q+=1){let K=J[Q];if(K.type==="elem")$.push(K.elem)}return this._elems=$,$}find($,J){let Q=this.children;for(let K=0;K<Q.length;K+=1){let Y=Q[K];if(Y.type==="elem"&&Y.elem.local===J&&Y.elem.ns===$)return Y.elem}return}findAll($,J){let Q=[],K=this.children;for(let Y=0;Y<K.length;Y+=1){let W=K[Y];if(W.type==="elem"&&W.elem.local===J&&W.elem.ns===$)Q.push(W.elem)}return Q}*descendantNodes(){let $=this.children.slice();$.reverse();while($.length>0){let J=$.pop();if(J.type==="elem"){let Q=$.length;for(let K of J.elem.children)$.push(K);x0($,Q)}yield J}}*descendantElems(){for(let $ of this.descendantNodes())if($.type==="elem")yield $.elem}firstDescendant($,J){for(let Q of this.descendantElems())if(Q.is($,J))return Q;return}*descendants($,J){for(let Q of this.descendantElems())if(Q.is($,J))yield Q}*descendantsAny($){for(let J of this.descendantElems())if(J.local===$)yield J}text(){let $="";for(let J of this.descendantNodes())if(J.type==="text")$+=J.text;return $}}class C6{prefixes=[new Map];ownsMap=[!0];defaultNs=[void 0];push(){this.prefixes.push(this.prefixes[this.prefixes.length-1]),this.ownsMap.push(!1),this.defaultNs.push(this.defaultNs[this.defaultNs.length-1])}pop(){this.prefixes.pop(),this.ownsMap.pop(),this.defaultNs.pop()}applyDecls($){for(let J of $)if(J.rawName==="xmlns")this.defaultNs[this.defaultNs.length-1]=J.value.length===0?void 0:J.value;else if(J.rawName.startsWith("xmlns:")){if(!this.ownsMap[this.ownsMap.length-1])this.prefixes[this.prefixes.length-1]=new Map(this.prefixes[this.prefixes.length-1]),this.ownsMap[this.ownsMap.length-1]=!0;let Q=J.rawName.slice(6);this.prefixes[this.prefixes.length-1].set(Q,J.value.length===0?void 0:J.value)}}lookupPrefix($){if($==="xml")return q0;if($==="xmlns")return z0;return this.prefixes[this.prefixes.length-1].get($)}resolveElement($){let J=$.indexOf(":");if(J>=0){let K=$.slice(0,J),Y=$.slice(J+1),W=this.lookupPrefix(K);return{ns:W,local:Y,bound:W!==void 0||K==="xml"||K==="xmlns"}}let Q=this.defaultNs[this.defaultNs.length-1];return{ns:Q,local:$,bound:Q!==void 0}}resolveAttribute($){let J=$.indexOf(":");if(J>=0){let Q=$.slice(0,J),K=$.slice(J+1);return{ns:this.lookupPrefix(Q),local:K}}return{ns:void 0,local:$}}}function u($){let J=S0($,"http://purl.oclc.org/ooxml/");if(J===void 0)return;let Q=J.indexOf("/");if(Q<0)return;let K=J.slice(0,Q),Y=J.slice(Q+1);return`http://schemas.openxmlformats.org/${K}/2006/${Y}`}var g6=0,x6=1,S6=2,h=3,G6=4,E6=5;function i($){let J=k0($),Q=new v6(J),K=new C6,Y=new Map,W=new Map,G=new n(void 0,""),Z=[],X=!1;for(;;){let U;try{U=Q.next()}catch(D){let B=D instanceof Error?D.message:String(D);throw L0.malformed(`unparseable xml: ${B}`)}switch(U){case x6:{K.push(),K.applyDecls(Q.attrs);let D=T0(Q.name,Q.attrs,K,Y,W);if(Q.empty)W6(Z,G,{type:"elem",elem:D}),K.pop();else Z.push(D);break}case S6:{let D=Z.pop();if(D===void 0)X=!0;else{if(D.local!==Q.local)X=!0;W6(Z,G,{type:"elem",elem:D}),K.pop()}break}case h:{if(Q.text.length>0)Z6(Z,G,Q.text);break}case G6:{let D=X6(Q.name)??`&${Q.name};`;Z6(Z,G,D);break}case E6:{Z6(Z,G,Q.text);break}case g6:if(Z.length>0){X=!0;while(Z.length>0)W6(Z,G,{type:"elem",elem:Z.pop()})}if(X)A0("recovered malformed xml (unclosed or mismatched elements)");return G}}}function W6($,J,Q){let K=$[$.length-1];if(K!==void 0)K.children.push(Q);else J.children.push(Q)}function Z6($,J,Q){let K=$[$.length-1]?.children??J.children,Y=K[K.length-1];if(Y?.type==="text")Y.text+=Q;else K.push({type:"text",text:Q})}function T6($,J){let Q=$.get(J);if(Q!==void 0)return Q;let K=u(J)??J;return $.set(J,K),K}function k6($,J){let Q=$.get(J);if(Q!==void 0)return Q;return $.set(J,J),J}function T0($,J,Q,K,Y){let W=Q.resolveElement($),G=W.ns!==void 0?T6(K,W.ns):void 0,Z=w6,X=!1;for(let U of J){if(U.rawName==="xmlns"||U.rawName.startsWith("xmlns:"))continue;let D=Q.resolveAttribute(U.rawName);if(!X)Z=[],X=!0;Z.push({ns:D.ns!==void 0?T6(K,D.ns):void 0,local:k6(Y,D.local),value:U.value})}if(W.local==="Choice"&&G===q.MC){let U=Z.find((D)=>D.local==="Requires");if(U!==void 0)U.value=U.value.split(/\s+/).filter((D)=>D.length>0).map((D)=>{let B=Q.resolveElement(`${D}:x`);if(B.bound&&B.ns!==void 0)return u(B.ns)??B.ns;return D}).join(" ")}return new n(G,k6(Y,W.local),Z)}function k0($){if($.length>=2&&$[0]===255&&$[1]===254)return new TextDecoder("utf-16le").decode($);if($.length>=2&&$[0]===254&&$[1]===255)return new TextDecoder("utf-16be").decode($);if($.length>=3&&$[0]===239&&$[1]===187&&$[2]===191)return new TextDecoder("utf-8").decode($.subarray(3));let J=$.subarray(0,Math.min($.length,200)),Q=w0(J);if(Q!==void 0){let K=C0(Q);if(K!==void 0&&K!=="utf8"&&K!=="utf-8")return I0($,K)}return new TextDecoder("utf-8").decode($)}function w0($){let J;try{J=new TextDecoder("utf-8",{fatal:!0}).decode($)}catch{return}let Q=J.indexOf("encoding");if(Q<0)return;let K=J.slice(Q+8).trimStart();if(!K.startsWith("="))return;K=K.slice(1).trimStart();let Y=K[0];if(Y!=='"'&&Y!=="'")return;K=K.slice(1);let W=K.indexOf(Y);if(W<0)return;return K.slice(0,W)}function C0($){let J=$.trim().toLowerCase().replace(/_/g,"-"),K={"utf-8":"utf8",utf8:"utf8","utf-16":"utf16-le","utf-16le":"utf16-le","utf-16be":"utf16-be","iso-8859-1":"iso-8859-1","latin-1":"iso-8859-1",latin1:"iso-8859-1","windows-1252":"windows-1252","cp-1252":"windows-1252",cp1252:"windows-1252","windows-1251":"windows-1251","windows-1250":"windows-1250","windows-874":"windows-874",gbk:"gbk",gb2312:"gb2312",gb18030:"gb18030",big5:"big5","shift-jis":"shiftjis",shiftjis:"shiftjis","euc-jp":"euc-jp","euc-kr":"euc-kr",koi8r:"koi8-r","koi8-r":"koi8-r"}[J]??J;return O0(K)?K:void 0}function X6($){if($.startsWith("#")){let Q=$.slice(1),K=Q.startsWith("x")||Q.startsWith("X"),Y=K?Q.slice(1):Q,W=Number.parseInt(Y,K?16:10);if(!Number.isFinite(W))return;try{return String.fromCodePoint(W)}catch{return}}return{amp:"&",lt:"<",gt:">",apos:"'",quot:'"',nbsp:" ",shy:"­",mdash:"—",ndash:"–",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",hellip:"…",copy:"©",reg:"®",trade:"™",deg:"°",middot:"·",bull:"•",sect:"§",para:"¶",laquo:"«",raquo:"»",times:"×",divide:"÷",plusmn:"±",frac12:"½",frac14:"¼",eacute:"é",egrave:"è",agrave:"à",ccedil:"ç",uuml:"ü",ouml:"ö",auml:"ä",szlig:"ß",aring:"å",oslash:"ø",aelig:"æ",euro:"€",pound:"£",yen:"¥",cent:"¢"}[$]}class v6{s;i=0;name="";local="";text="";empty=!1;attrs=[];constructor($){this.s=$}next(){if(this.i>=this.s.length)return g6;if(this.s.charCodeAt(this.i)===60)return this.markup();return this.textRun()}markup(){if(this.startsWith("<?"))return this.skipUntil("?>"),this.next();if(this.startsWith("<!--")){this.i+=4;let $=this.s.indexOf("-->",this.i);if($<0)throw Error("unterminated comment");return this.i=$+3,this.next()}if(this.startsWith("<![CDATA[")){this.i+=9;let $=this.s.indexOf("]]>",this.i);if($<0)throw Error("unterminated cdata");return this.text=this.s.slice(this.i,$),this.i=$+3,E6}if(this.startsWith("<!"))return this.skipDoctype(),this.next();if(this.startsWith("</")){this.i+=2;let $=this.readName();if(this.skipWs(),this.s[this.i]===">")this.i+=1;return this.local=g0($),S6}if(this.i+=1,this.name=this.readName(),this.attrs=this.readAttrs(),this.skipWs(),this.empty=!1,this.s[this.i]==="/"&&this.s[this.i+1]===">")this.i+=2,this.empty=!0;else if(this.s[this.i]===">")this.i+=1;return x6}textRun(){let $=this.i;while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===60)break;if(J===38){if(this.i>$)return this.text=this.s.slice($,this.i),h;let Q=this.readReference();if(Q.type==="general")return this.name=Q.name,G6;return this.i=$,this.textRunSlow()}this.i+=1}return this.text=this.s.slice($,this.i),h}textRunSlow(){let $="";while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===60)break;if(J===38){let K=this.readReference();if(K.type==="general"){if($.length>0)return this.i=K.rewind,this.text=$,h;return this.name=K.name,G6}$+=K.text;continue}let Q=this.i;this.i+=1;while(this.i<this.s.length){let K=this.s.charCodeAt(this.i);if(K===60||K===38)break;this.i+=1}$+=this.s.slice(Q,this.i)}return this.text=$,h}readReference(){let $=this.i;this.i+=1;let J=this.s.indexOf(";",this.i);if(J<0)return{type:"char",text:"&"};let Q=this.s.slice(this.i,J);if(this.i=J+1,Q.startsWith("#")||Q==="amp"||Q==="lt"||Q==="gt"||Q==="apos"||Q==="quot")return{type:"char",text:X6(Q)??`&${Q};`};return{type:"general",name:Q,rewind:$}}readName(){let $=this.i;while(this.i<this.s.length){let J=this.s.charCodeAt(this.i);if(J===47||J===62||J===61||J===32||J===9||J===10||J===13)break;this.i+=1}return this.s.slice($,this.i)}readAttrs(){let $=[];for(;;){this.skipWs();let J=this.s[this.i];if(J===void 0||J===">"||J==="/")break;let Q=this.readName();if(Q.length===0){this.i+=1;continue}if(this.skipWs(),this.s[this.i]!=="="){z6(`dropping undecodable attribute: ${Q}`);continue}this.i+=1,this.skipWs();let K=this.readAttrValue();if(K===void 0){z6(`dropping undecodable attribute: ${Q}`);continue}$.push({rawName:Q,value:K})}return $}readAttrValue(){let $=this.s.charCodeAt(this.i);if($!==34&&$!==39)return;this.i+=1;let J=this.i,Q=!1;while(this.i<this.s.length){let Y=this.s.charCodeAt(this.i);if(Y===$){let W=this.s.slice(J,this.i);return this.i+=1,Q?o(W):W}if(Y===38)return this.i=J,this.readAttrValueSlow($);if(Y===9||Y===10||Y===13)Q=!0;this.i+=1}let K=this.s.slice(J,this.i);return Q?o(K):K}readAttrValueSlow($){let J="";while(this.i<this.s.length){let Q=this.s.charCodeAt(this.i);if(Q===$)return this.i+=1,o(J);if(Q===38){let Y=this.readReference();if(Y.type==="char")J+=Y.text;else J+=X6(Y.name)??`&${Y.name};`;continue}let K=this.i;this.i+=1;while(this.i<this.s.length){let Y=this.s.charCodeAt(this.i);if(Y===$||Y===38)break;this.i+=1}J+=this.s.slice(K,this.i)}return o(J)}skipWs(){while(this.i<this.s.length){let $=this.s.charCodeAt(this.i);if($!==32&&$!==9&&$!==10&&$!==13)break;this.i+=1}}startsWith($){return this.s.startsWith($,this.i)}skipUntil($){let J=this.s.indexOf($,this.i);this.i=J<0?this.s.length:J+$.length}skipDoctype(){this.i+=2;let $=0;while(this.i<this.s.length){let J=this.s[this.i];if(J==='"'||J==="'"){let Q=J;this.i+=1;while(this.i<this.s.length&&this.s[this.i]!==Q)this.i+=1;if(this.i<this.s.length)this.i+=1;continue}if(J==="<")$+=1;else if(J===">"){if($===0){this.i+=1;return}$-=1}this.i+=1}}}function g0($){let J=$.indexOf(":");return J<0?$:$.slice(J+1)}function o($){if($.indexOf("\t")<0&&$.indexOf(`
2
+ `)<0&&$.indexOf("\r")<0)return $;return $.replace(/[\t\n\r]/g," ")}function x0($,J){let Q=J,K=$.length-1;while(Q<K){let Y=$[Q];$[Q]=$[K],$[K]=Y,Q+=1,K-=1}}function S0($,J){return $.startsWith(J)?$.slice(J.length):void 0}var h0=67324752,u0=33639248;var f0=101075792,p0=117853008,l0=0,m0=8;class D6{length;bytes;byName=new Map;constructor($,J){this.bytes=$,this.length=J.length;for(let Q of J)if(!this.byName.has(Q.name))this.byName.set(Q.name,Q)}static open($){let J=d0($);if(J<0)throw j.malformed("not a readable zip archive: invalid Zip archive: Could not find EOCD");let Q=r($),K=F(Q,J+10),Y=N(Q,J+12),W=N(Q,J+16);if(K===65535||Y===4294967295||W===4294967295){let U=y0($,J);if(U!==void 0)K=U.entryCount,Y=U.cdSize,W=U.cdOffset}if(W+Y>$.length)throw j.malformed("not a readable zip archive: truncated central directory");let G=[],Z=W,X=Math.min($.length,W+Y);while(Z+46<=X&&G.length<K){if(N(Q,Z)!==u0)throw j.malformed("not a readable zip archive: bad central directory");let U=F(Q,Z+8),D=F(Q,Z+10),B=N(Q,Z+16),P=N(Q,Z+20),S=N(Q,Z+24),z=F(Q,Z+28),M=F(Q,Z+30),A=F(Q,Z+32),w=N(Q,Z+42),H=$.subarray(Z+46,Z+46+z);if(Z+46+z+M+A>$.length)throw j.malformed("not a readable zip archive: truncated entry");let Q6=$.subarray(Z+46+z,Z+46+z+M),_=o0(Q6,{uncompressedSize:S,compressedSize:P,headerOffset:w});S=_.uncompressedSize,P=_.compressedSize,w=_.headerOffset;let l=(U&2048)!==0,m=n0(H,l);G.push({name:m,method:D,flags:U,crc:B,compressedSize:P,uncompressedSize:S,headerOffset:w}),Z+=46+z+M+A}return new D6($,G)}indexForName($){return this.byName.get($)}partNames(){return[...this.byName.keys()]}readCompressed($){let J=r(this.bytes),Q=$.headerOffset;if(Q+30>this.bytes.length||N(J,Q)!==h0)throw Error("bad local file header");let K=F(J,Q+26),Y=F(J,Q+28),W=Q+30+K+Y,G=W+$.compressedSize;if(W>this.bytes.length)throw Error("truncated local file header");if($.compressedSize>0&&G>this.bytes.length)throw Error("truncated compressed data");return this.bytes.subarray(W,Math.min(G,this.bytes.length))}}class f{zip;cache=new Map;constructor($){this.zip=$}static open($){let J;try{J=D6.open($)}catch(Q){if(Q instanceof j)throw Q;let K=Q instanceof Error?Q.message:String(Q);throw j.malformed(`not a readable zip archive: ${K}`)}return new f(J)}part($){$=u6($);let J=this.cache.get($);if(J!==void 0)return J;let Q=this.zip.indexForName($);if(Q===void 0)return;let K;try{K=this.decompress(Q,Number.MAX_SAFE_INTEGER)}catch(Y){if(Y instanceof j)throw Y;let W=Y instanceof Error?Y.message:String(Y);throw j.malformedPart($,`corrupt archive entry: ${W}`)}return this.cache.set($,K),K}hasPart($){return this.zip.indexForName(u6($))!==void 0}partNames(){return this.zip.partNames()}requiredPart($){let J=this.part($);if(J===void 0)throw j.missingPart($);return J}optionalPart($){try{return this.part($)}catch(J){if(J instanceof j&&J.isFatal())throw J;h6(`skipping unreadable part ${$}: ${J instanceof Error?J.message:String(J)}`);return}}optionalXmlPart($){let J=this.optionalPart($);if(J===void 0)return;try{return i(J)}catch(Q){if(Q instanceof j&&Q.isFatal())throw Q;h6(`skipping corrupt part ${$}: ${Q instanceof Error?Q.message:String(Q)}`);return}}requiredXmlPart($){return i(this.requiredPart($))}decompress($,J){if(($.flags&1)!==0)throw Error("encrypted zip entry");let Q;try{Q=this.zip.readCompressed($)}catch(Y){let W=Y instanceof Error?Y.message:String(Y);throw j.malformedPart($.name,`unreadable archive entry: ${W}`)}let K;if($.method===l0){let Y=$.uncompressedSize>0?$.uncompressedSize:Q.length;K=Q.subarray(0,Math.min(Y,Q.length))}else if($.method===m0)K=c0(Q,J);else throw Error(`unsupported compression method ${$.method}`);if(K.length<=J-1&&$.crc!==0&&s0(K)!==$.crc)throw Error("crc mismatch");return K}}function b0($){if(!v($))return;try{let J=T.open($);if(J.exists("EncryptionInfo")||J.exists("EncryptedPackage"))return j.encrypted()}catch{}return j.malformed("OLE compound document where an OOXML package was expected (legacy binary format?)")}function c0($,J){return v0($,J)}function d0($){let J=Math.max(0,$.length-65557);for(let Q=$.length-22;Q>=J;Q-=1)if($[Q]===80&&$[Q+1]===75&&$[Q+2]===5&&$[Q+3]===6){let K=$[Q+20]|$[Q+21]<<8;if(Q+22+K===$.length)return Q}return-1}function y0($,J){if(J<20)return;let Q=r($),K=J-20;if(N(Q,K)!==p0)return;let Y=k(Q,K+8);if(Y+56>$.length||N(Q,Y)!==f0)return;return{entryCount:k(Q,Y+32),cdSize:k(Q,Y+40),cdOffset:k(Q,Y+48)}}function o0($,J){let Q=0,K=r($);while(Q+4<=$.length){let Y=F(K,Q),W=F(K,Q+2),G=Q+4;if(G+W>$.length)break;if(Y===1){let Z=G,{uncompressedSize:X,compressedSize:U,headerOffset:D}=J;if(X===4294967295&&Z+8<=G+W)X=k(K,Z),Z+=8;if(U===4294967295&&Z+8<=G+W)U=k(K,Z),Z+=8;if(D===4294967295&&Z+8<=G+W)D=k(K,Z);return{uncompressedSize:X,compressedSize:U,headerOffset:D}}Q=G+W}return J}function n0($,J){if(J)return new TextDecoder("utf-8",{fatal:!1}).decode($);return E0($,"cp437")}function u6($){let J=0;while(J<$.length&&$.charCodeAt(J)===47)J+=1;return $.slice(J)}function r($){return new DataView($.buffer,$.byteOffset,$.byteLength)}function F($,J){return $.getUint16(J,!0)}function N($,J){return $.getUint32(J,!0)}function k($,J){let Q=$.getUint32(J,!0),K=$.getUint32(J+4,!0);if(K>2097151)return Number.POSITIVE_INFINITY;return K*4294967296+Q}var i0=r0();function r0(){let $=new Uint32Array(256);for(let J=0;J<256;J+=1){let Q=J;for(let K=0;K<8;K+=1)Q=Q&1?3988292384^Q>>>1:Q>>>1;$[J]=Q>>>0}return $}function a0($){let J=4294967295;for(let Q=0;Q<$.length;Q+=1)J=i0[(J^$[Q])&255]^J>>>8;return(J^4294967295)>>>0}function s0($){return a0($)}import{ConvertError as f6}from"@mdgate/core";function t0($,J){if(!Number.isSafeInteger(J)||J<0||J+2>$.length)return;return $[J]|$[J+1]<<8}function e0($,J){if(!Number.isSafeInteger(J)||J<0||J+4>$.length)return;return($[J]|$[J+1]<<8|$[J+2]<<16|$[J+3]<<24)>>>0}function $$($,J){try{return $.readStream(J)}catch(Q){if(Q instanceof f6)throw Q;let K=Q instanceof Error?Q.message:String(Q);throw f6.malformedPart(J,`unreadable stream: ${K}`)}}import{trim as d6}from"@mdgate/utils";import{ConvertError as p6}from"@mdgate/core";function p($,J){let Q,K=J.indexOf("#");if(K>=0)Q=U6(J.slice(K+1)),J=J.slice(0,K);let Y=J.indexOf("?");if(Y>=0)J=J.slice(0,Y);if(J.length===0)return{path:$,fragment:Q};let W=[];if(!J.startsWith("/")){let G=$.lastIndexOf("/");if(G>=0){for(let Z of $.slice(0,G).split("/"))if(Z.length>0)W.push(Z)}}for(let G of J.split("/")){if(G.length===0||G===".")continue;if(G===".."){W.pop();continue}let Z=U6(G);if(Z.includes("/")||Z.includes("\\"))throw p6.malformed(`percent-encoded separator in package reference segment ${b6(G)}`);if(Z==="."||Z==="..")throw p6.malformed(`percent-encoded traversal in package reference segment ${b6(G)}`);W.push(Z)}return{path:W.join("/"),fragment:Q}}function J$($){return U6($)}var Q$=new TextEncoder;function U6($){let J=Q$.encode($),Q=[],K=0;while(K<J.length){if(J[K]===37&&K+2<J.length){let Y=J[K+1],W=J[K+2];if(l6(Y)&&l6(W)){Q.push(m6(Y)<<4|m6(W)),K+=3;continue}}Q.push(J[K]),K+=1}return new TextDecoder("utf-8",{fatal:!1}).decode(Uint8Array.from(Q))}function l6($){return $>=48&&$<=57||$>=65&&$<=70||$>=97&&$<=102}function m6($){if($>=48&&$<=57)return $-48;if($>=65&&$<=70)return $-55;return $-87}function b6($){return JSON.stringify($)}import{warn as c6}from"@mdgate/utils";var V6={OFFICE_DOCUMENT:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",STYLES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",NUMBERING:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",FOOTNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",ENDNOTES:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"};class a{map;constructor($=new Map){this.map=$}get($){return this.map.get($)}internalTarget($){let J=this.map.get($);return J!==void 0&&J.mode==="internal"?J.target:void 0}entries(){return this.map.entries()}firstOfType($){let J,Q;for(let[K,Y]of this.map)if(Y.relType===$&&Y.mode==="internal"){if(J===void 0||K<J)J=K,Q=Y}return Q}}function H6($,J){let Q=$.optionalXmlPart(J);if(Q===void 0)return new a;let K=new Map;for(let Y of Q.descendants(q.PKG_RELS,"Relationship")){let W=Y.attrAny("Id"),G=Y.attrAny("Target");if(W===void 0||G===void 0)continue;let Z=Y.attrAny("TargetMode"),X=Z!==void 0&&Z.toLowerCase()==="external"?"external":"internal",U=Y.attrAny("Type")??"",D=u(U)??U;K.set(W,{target:G,relType:D,mode:X})}return new a(K)}function K$($,J,Q,K){let Y=J.get(K);if(Y===void 0||Y.mode!=="internal")return;let W;try{W=p(Q,Y.target)}catch(Z){c6(`skipping unresolvable relationship target ${JSON.stringify(Y.target)}: ${Z instanceof Error?Z.message:String(Z)}`);return}let G=$.optionalPart(W.path);if(G===void 0){c6(`relationship target ${W.path} is missing`);return}return[W.path,G]}function Y$($){let J=$.lastIndexOf("/");if(J>=0)return`${$.slice(0,J)}/_rels/${$.slice(J+1)}.rels`;return`_rels/${$}.rels`}var y6="http://schemas.openxmlformats.org/package/2006/content-types",B6="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",W$="http://schemas.openxmlformats.org/spreadsheetml/2006/main";function Z$($){if($.length<4||$[0]!==80||$[1]!==75||$[2]!==3||$[3]!==4)return;try{return X$($)}catch{return}}function G$($){if(!v($))return;let J;try{J=T.open($)}catch{return}try{for(let Q of J.readRootStorage()){let K=Q.name;if(R(K,"WordDocument"))return"doc";if(R(K,"PowerPoint Document"))return"ppt";if(R(K,"Workbook")||R(K,"Book"))return"xls";if(R(K,"VisioDocument"))return"vsd";if(P$(K))return"hwp";if(M$(K))return"one"}}catch{return}return}function X$($){let J;try{J=f.open($)}catch{return}let Q=U$(J,"mimetype");if(Q!==void 0){let Y=F$(Q);if(Y===void 0)return;return o6(d6(Y))}try{let W=H6(J,"_rels/.rels").firstOfType(V6.OFFICE_DOCUMENT);if(W!==void 0){let G;try{G=p("",W.target)}catch{G=void 0}if(G!==void 0){let Z=j6(J,"[Content_Types].xml");if(Z!==void 0){let U=V$(Z,G.path);if(U!==void 0){let D=H$(U);if(D!==void 0)return D}}let X=j6(J,G.path);if(X!==void 0){let U=X.childElems()[0],D=U!==void 0?B$(U):void 0;if(D!==void 0)return D}return j$(G.path)}}}catch{}for(let[Y,W]of[["word/document.xml","docx"],["ppt/presentation.xml","pptx"],["xl/workbook.xml","xlsx"],["xl/workbook.bin","xlsx"],["visio/document.xml","vsdx"],["Contents/content.hpf","hwpx"]])if(J.hasPart(Y))return W;try{let Y=j6(J,"META-INF/manifest.xml");if(Y!==void 0){for(let W of Y.descendants(B6,"file-entry"))if(W.attr(B6,"full-path")==="/"){let G=W.attr(B6,"media-type");if(G!==void 0)return o6(d6(G))}}}catch{}if(J.hasPart("META-INF/container.xml"))return"epub";let K=D$(J);if(K!==void 0)return K;return}function D$($){let J=$.partNames();if(J.length===0)return;let Q=!1,K=!1,Y=!1,W=!1,G=!1;for(let Z of J){let X=Z.toLowerCase();if(X.includes("encrypted")||X.endsWith(".iwae"))return;if(X.endsWith(".iwa")){if(Q=!0,X.endsWith("/document.iwa")||X==="document.iwa")Y=!0;if(X.includes("slide")||X.includes("masterslide"))W=!0;if(X.includes("calculationengine")||X.includes("tables/"))G=!0}if(X==="index.zip")K=!0}if(!Q&&!K)return;if(!Q&&K)return;if(!Y&&!K&&!W&&!G)return;if(W&&!G)return"keynote";if(G&&!W)return"numbers";if(Y)return"pages";return}function U$($,J){try{return $.optionalPart(J)}catch{return}}function j6($,J){try{return $.optionalXmlPart(J)}catch{return}}function o6($){switch($.endsWith("-template")?$.slice(0,-9):$){case"application/epub+zip":return"epub";case"application/vnd.oasis.opendocument.text":return"odt";case"application/vnd.oasis.opendocument.spreadsheet":return"ods";case"application/vnd.oasis.opendocument.presentation":return"odp";case"application/vnd.oasis.opendocument.graphics":return"odg";case"application/hwp+zip":case"application/vnd.hancom.hwpx":return"hwpx";default:return}}function V$($,J){let Q=`/${J}`;for(let W of $.descendants(y6,"Override")){let G=W.attrAny("PartName");if(G!==void 0&&R(G,Q)){let Z=W.attrAny("ContentType");if(Z!==void 0)return Z}}let K=J.lastIndexOf(".");if(K<0)return;let Y=J.slice(K+1);for(let W of $.descendants(y6,"Default")){let G=W.attrAny("Extension");if(G!==void 0&&R(G,Y))return W.attrAny("ContentType")}return}function H$($){let J=$.toLowerCase();if(J.includes("wordprocessingml"))return"docx";if(J.includes("presentationml"))return"pptx";if(J.includes("spreadsheetml")||J.includes("ms-excel"))return"xlsx";if(J.includes("visio")||J.includes("vnd.ms-visio"))return"vsdx";return}function B$($){switch($.ns){case q.W:return"docx";case q.P:return"pptx";case W$:return"xlsx";default:return}}function j$($){if($.startsWith("word/"))return"docx";if($.startsWith("ppt/"))return"pptx";if($.startsWith("xl/"))return"xlsx";if($.startsWith("visio/"))return"vsdx";return}function P$($){let J=$.charCodeAt(0)===5?$.slice(1):$;return R($,"FileHeader")||R(J,"HwpSummaryInformation")||R($,"HWPDocumentInfo")}function M$($){return $.length>=7&&$.slice(0,7).toLowerCase()==="onenote"}function R($,J){return $.length===J.length&&$.toLowerCase()===J.toLowerCase()}function F$($){try{return new TextDecoder("utf-8",{fatal:!0}).decode($)}catch{return}}import{asciiStartsWith as $0,decode as n6,trim as x}from"@mdgate/utils";function N$($){if(I$($)){let J=O$($);if(J.length>1||J.length===1&&$0($,"From "))return{contentType:"application/mbox",headers:[],filename:void 0,bytes:new Uint8Array(0),parts:J.map((Q)=>s(Q))}}return s(L$($))}function R$($){let J=[],Q=(K)=>{J.push(K);for(let Y of K.parts)Q(Y)};return Q($),J}function J0($){let J=W0($);if(J!==void 0){let Q=Z0($,J);if(Q!==void 0&&i6(Q)&&!M6(Q))return Q}return L6($,(Q)=>i6(Q)&&!M6(Q))}function Q0($){return L6($,(J)=>J.contentType==="text/plain"&&J.parts.length===0&&!M6(J))}function _$($){let J=J0($),Q=Q0($),K=[],Y=(W)=>{if(W.parts.length>0){for(let G of W.parts)Y(G);return}if(W===J||W===Q)return;if(K0(W.contentType)||Y0(W.contentType))return;K.push(W)};return Y($),K}function J6($,J){let Q=J.toLowerCase();for(let K=$.headers.length-1;K>=0;K-=1)if($.headers[K].name===Q)return $.headers[K].value;return}function s($){let J=q$($),Q=k$(J.headers),K=F6(Q,"content-type")??"text/plain",Y=X0(K),W=g$(Q,Y.params),G=(F6(Q,"content-transfer-encoding")??"7bit").trim().toLowerCase(),Z=h$(J.body,G);if(K0(Y.type)){let X=Y.params.get("boundary");if(X===void 0||X.length===0)return{contentType:Y.type,headers:Q,filename:W,bytes:Z,parts:[]};let D=p$(Z,X).map((B)=>s(B));return{contentType:Y.type,headers:Q,filename:W,bytes:new Uint8Array(0),parts:D}}if(Y0(Y.type)){let X=s(Z);return{contentType:Y.type,headers:Q,filename:W,bytes:Z,parts:[X]}}return{contentType:Y.type,headers:Q,filename:W,bytes:Z,parts:[]}}function K0($){return $.startsWith("multipart/")}function Y0($){return $==="message/rfc822"||$==="message/global"||$==="message/news"}function i6($){return $.contentType==="text/html"&&$.parts.length===0}function M6($){let J=J6($,"content-disposition");if(J===void 0)return!1;let Q=J.indexOf(";");return(Q<0?J:J.slice(0,Q)).trim().toLowerCase()==="attachment"}function L6($,J){if(J($))return $;for(let Q of $.parts){let K=L6(Q,J);if(K!==void 0)return K}return}function W0($){let J=J6($,"content-type");if($.contentType==="multipart/related"&&J!==void 0){let Q=X0(J).params.get("start");if(Q!==void 0&&Q.length>0)return G0(Q)}for(let Q of $.parts){let K=W0(Q);if(K!==void 0)return K}return}function Z0($,J){let Q=J6($,"content-id");if(Q!==void 0&&G0(Q)===J)return $;for(let K of $.parts){let Y=Z0(K,J);if(Y!==void 0)return Y}return}function G0($){let J=x($);if(J.startsWith("<")&&J.endsWith(">")&&J.length>=2)J=J.slice(1,-1);return J}function F6($,J){let Q=J.toLowerCase();for(let K=$.length-1;K>=0;K-=1)if($[K].name===Q)return $[K].value;return}function L$($){if($.length>=3&&$[0]===239&&$[1]===187&&$[2]===191)return $.subarray(3);return $}function I$($){return $0($,"From ")}function O$($){let J=[],Q=0;while(Q<$.length){if(N6($,Q))J.push(Q);Q=t($,Q)}if(J.length===0)return[];let K=[];for(let Y=0;Y<J.length;Y+=1){let W=J[Y],G=t($,W),Z=Y+1<J.length?J[Y+1]:$.length;if(G>Z)continue;K.push(A$($.subarray(G,Z)))}return K}function N6($,J){return J+5<=$.length&&$[J]===70&&$[J+1]===114&&$[J+2]===111&&$[J+3]===109&&$[J+4]===32}function A$($){let J=0,Q=0;while(Q<$.length){if($[Q]===62&&N6($,Q+1))J+=1;Q=t($,Q)}if(J===0)return $;let K=new Uint8Array($.length-J),Y=0,W=0;while(Y<$.length){if($[Y]===62&&N6($,Y+1))Y+=1;let G=t($,Y);K.set($.subarray(Y,G),W),W+=G-Y,Y=G}return K.subarray(0,W)}function t($,J){while(J<$.length){let Q=$[J];if(J+=1,Q===10)return J;if(Q===13){if(J<$.length&&$[J]===10)J+=1;return J}}return J}function q$($){let J=z$($);if(J!==void 0)return{headers:$.subarray(0,J.sep),body:$.subarray(J.body)};if(T$($))return{headers:$,body:$.subarray($.length)};return{headers:$.subarray(0,0),body:$}}function z$($){let J=$.length;for(let Q=0;Q<J;Q+=1){let K=$[Q];if(K===10&&Q+1<$.length&&$[Q+1]===10)return{sep:Q+1,body:Q+2};if(K===13&&Q+1<$.length&&$[Q+1]===10){if(Q+3<$.length&&$[Q+2]===13&&$[Q+3]===10)return{sep:Q+2,body:Q+4};if(Q+2<$.length&&$[Q+2]===10)return{sep:Q+2,body:Q+3}}if(K===13&&Q+1<$.length&&$[Q+1]===13)return{sep:Q+1,body:Q+2}}return}function T$($){let J=0;while(J<$.length&&($[J]===32||$[J]===9))J+=1;if(J>=$.length)return!1;let Q=J;while(J<$.length&&$[J]!==13&&$[J]!==10&&$[J]!==58)J+=1;if(J>=$.length||$[J]!==58||J===Q)return!1;for(let K=Q;K<J;K+=1){let Y=$[K];if(!(Y>=65&&Y<=90||Y>=97&&Y<=122||Y>=48&&Y<=57||Y===45))return!1}return!0}function k$($){if($.length===0)return[];let J=w$(H0($)),Q=[],K=0;while(K<J.length){let Y=J.indexOf(`
3
+ `,K);if(Y<0)Y=J.length;let W=J.slice(K,Y);if(W.endsWith("\r"))W=W.slice(0,-1);if(K=Y+1,W.length===0)continue;let G=W.indexOf(":");if(G<=0)continue;let Z=W.slice(0,G).trim().toLowerCase();if(Z.length===0)continue;let X=x$(x(W.slice(G+1)));Q.push({name:Z,value:X})}return Q}function w$($){let J="";for(let Q=0;Q<$.length;Q+=1){let K=$[Q];if(K==="\r"&&$[Q+1]===`
4
+ `&&R6($[Q+2])){J+=" ",Q+=2;continue}if((K===`
5
+ `||K==="\r")&&R6($[Q+1])){J+=" ",Q+=1;continue}J+=K}return J}function R6($){return $===" "||$==="\t"}function X0($){let J=_6($,";"),Q=x(J<0?$:$.slice(0,J)).toLowerCase(),K=Q.length>0?Q:"application/octet-stream",Y=D0(J<0?"":$.slice(J+1));return{type:K,params:Y}}function D0($){let J=new Map,Q=0;while(Q<$.length){while(Q<$.length&&($[Q]===";"||$[Q]===" "||$[Q]==="\t"))Q+=1;if(Q>=$.length)break;let K=$.slice(Q),Y=_6(K,";"),W=x(Y<0?K:K.slice(0,Y));Q+=Y<0?K.length:Y+1;let G=_6(W,"=");if(G<=0)continue;let Z=x(W.slice(0,G)).toLowerCase();if(Z.length===0)continue;J.set(Z,C$(x(W.slice(G+1))))}return J}function _6($,J){let Q=!1;for(let K=0;K<$.length;K+=1){let Y=$[K];if(Q){if(Y==="\\"){K+=1;continue}if(Y==='"')Q=!1;continue}if(Y==='"'){Q=!0;continue}if(Y===J)return K}return-1}function C$($){if($.length>=2&&$.startsWith('"')&&$.endsWith('"')){let J="";for(let Q=1;Q<$.length-1;Q+=1){if($[Q]==="\\"&&Q+1<$.length-1){Q+=1,J+=$[Q];continue}J+=$[Q]}return J}return $}function g$($,J){let Q=F6($,"content-disposition");if(Q!==void 0){let K=r6(D0(Q));if(K!==void 0)return K}return r6(J)}function r6($){let J=a6($,"filename")??$.get("filename*");if(J!==void 0){let W=s6(J);if(W.length>0)return W}let Q=$.get("filename");if(Q!==void 0&&Q.length>0)return Q;let K=a6($,"name")??$.get("name*");if(K!==void 0){let W=s6(K);if(W.length>0)return W}let Y=$.get("name");return Y!==void 0&&Y.length>0?Y:void 0}function a6($,J){if(!$.has(`${J}*0`)&&!$.has(`${J}*0*`))return;let Q="";for(let K=0;K<64;K+=1){let Y=$.get(`${J}*${K}*`);if(Y!==void 0){Q+=Y;continue}let W=$.get(`${J}*${K}`);if(W===void 0)break;Q+=W}return Q.length>0?Q:void 0}function s6($){let J=$.indexOf("'");if(J<0)return P6($,"utf-8");let Q=$.indexOf("'",J+1);if(Q<0)return P6($,"utf-8");let K=$.slice(0,J)||"utf-8";return P6($.slice(Q+1),K)}function P6($,J){let Q=new Uint8Array($.length),K=0;for(let Y=0;Y<$.length;Y+=1){if($[Y]==="%"&&Y+2<$.length&&e($[Y+1])&&e($[Y+2])){Q[K]=$6($[Y+1])<<4|$6($[Y+2]),K+=1,Y+=2;continue}Q[K]=$.charCodeAt(Y)&255,K+=1}return U0(Q.subarray(0,K),J)}function x$($){let J=/=\?([^?]+)\?([bBqQ])\?([^?]*)\?=/g,Q="",K=0,Y=-1,W;while((W=J.exec($))!==void 0&&W!==null){let G=W.index,Z=$.slice(K,G);if(Y>=0&&S$(Z));else Q+=Z;Q+=E$(W[1],W[2],W[3]),K=G+W[0].length,Y=K}if(K===0)return $;return Q+$.slice(K)}function S$($){for(let J=0;J<$.length;J+=1)if(!R6($[J]))return!1;return $.length>0}function E$($,J,Q){let K=J.toUpperCase(),Y;if(K==="B")Y=V0(new TextEncoder().encode(Q));else Y=v$(Q);return U0(Y,$)}function v$($){let J=new Uint8Array($.length),Q=0;for(let K=0;K<$.length;K+=1){let Y=$[K];if(Y==="_"){J[Q]=32,Q+=1;continue}if(Y==="="&&K+2<$.length&&e($[K+1])&&e($[K+2])){J[Q]=$6($[K+1])<<4|$6($[K+2]),Q+=1,K+=2;continue}J[Q]=$.charCodeAt(K)&255,Q+=1}return J.subarray(0,Q)}function U0($,J){try{return n6($,J)}catch{try{return n6($,"utf-8")}catch{return H0($)}}}function h$($,J){if(J==="base64")return V0($);if(J==="quoted-printable")return u$($);return $}function u$($){let J=new Uint8Array($.length),Q=0;for(let K=0;K<$.length;K+=1){let Y=$[K];if(Y===61){let W=$[K+1],G=$[K+2];if(W===13&&G===10){K+=2;continue}if(W===10||W===13){K+=1;continue}if(W!==void 0&&G!==void 0&&t6(W)&&t6(G)){J[Q]=e6(W)<<4|e6(G),Q+=1,K+=2;continue}}if(Y===9||Y===32){let W=K+1;while(W<$.length&&($[W]===9||$[W]===32))W+=1;if(W<$.length&&($[W]===13||$[W]===10)){K=W-1;continue}}J[Q]=Y,Q+=1}return J.subarray(0,Q)}var f$=(()=>{let $=new Uint8Array(128).fill(255),J="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(let Q=0;Q<64;Q+=1)$["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(Q)]=Q;return $[45]=62,$[95]=63,$})();function V0($){let J=Math.floor($.length/4)*3+3,Q=new Uint8Array(J),K=0,Y=0,W=0;for(let G=0;G<$.length;G+=1){let Z=$[G];if(Z===61)break;if(Z>=128)continue;let X=f$[Z];if(X===255)continue;if(K=K<<6|X,Y+=6,Y>=8)Y-=8,Q[W]=K>>Y&255,W+=1}return Q.subarray(0,W)}function p$($,J){if(J.length===0)return[];let Q=new TextEncoder().encode(`--${J}`),K=[],Y=[],W=0;while(W+Q.length<=$.length){let Z=c$($,Q,W);if(Z<0)break;if(!l$($,Z)){W=Z+1;continue}K.push(Z);let X=Z+Q.length;Y.push($[X]===45&&$[X+1]===45),W=X}let G=[];for(let Z=0;Z<K.length;Z+=1){if(Y[Z])break;let X=m$($,K[Z]+Q.length),U=Z+1<K.length?K[Z+1]:-1;if(U<0)break;G.push($.subarray(X,b$($,X,U)))}return G}function l$($,J){if(J===0)return!0;let Q=$[J-1];return Q===10||Q===13}function m$($,J){if($[J]===45&&$[J+1]===45)J+=2;while(J<$.length&&($[J]===32||$[J]===9))J+=1;if($[J]===13)J+=1;if($[J]===10)J+=1;return J}function b$($,J,Q){let K=Q;if(K>J&&$[K-1]===10)K-=1;if(K>J&&$[K-1]===13)K-=1;return K}function c$($,J,Q){if(J.length===0)return Q;let K=J[0],Y=Q+($.length-J.length);for(let W=Q;W<=Y;W+=1){if($[W]!==K)continue;let G=!0;for(let Z=1;Z<J.length;Z+=1)if($[W+Z]!==J[Z]){G=!1;break}if(G)return W}return-1}function H0($){if($.length<=4096)return String.fromCharCode(...$);let Q=[];for(let K=0;K<$.length;K+=4096)Q.push(String.fromCharCode(...$.subarray(K,K+4096)));return Q.join("")}function e($){return $>="0"&&$<="9"||$>="a"&&$<="f"||$>="A"&&$<="F"}function $6($){if($>="0"&&$<="9")return $.charCodeAt(0)-48;if($>="a"&&$<="f")return $.charCodeAt(0)-87;return $.charCodeAt(0)-55}function t6($){return $>=48&&$<=57||$>=65&&$<=70||$>=97&&$<=102}function e6($){if($>=48&&$<=57)return $-48;if($>=65&&$<=70)return $-55;return $-87}export{R$ as walkMimeParts,p as resolve,Y$ as relsPartFor,V6 as relType,K$ as relTargetBytes,H6 as readRels,$$ as readOleStream,b0 as probeOle,i as parseXml,N$ as parseMime,q as ns,u as normalizeOoxmlUri,Q0 as mimeTextPlain,J0 as mimeTextHtml,J6 as mimeHeader,_$ as mimeAttachments,v as hasOleMagic,e0 as getU32,t0 as getU16,Z$ as detectZipDoc,G$ as detectOleDoc,J$ as decodeFragment,a as Relationships,f as Package,F0 as OLE_MAGIC_BYTES,n as Element,T as CompoundFile};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/containers",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "mdgate container parsing: ZIP/OPC packages, OLE compound files, XML",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,8 +22,8 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/core": "0.5.0",
26
- "@mdgate/utils": "0.5.0"
25
+ "@mdgate/core": "0.5.1",
26
+ "@mdgate/utils": "0.5.1"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"
package/dist/limits.d.ts DELETED
@@ -1,18 +0,0 @@
1
- /** Maximum decompressed size of a single archive entry: 128 MiB. */
2
- export declare const MAX_ENTRY_BYTES: number;
3
- /** Maximum total decompressed bytes read from one archive: 512 MiB. */
4
- export declare const MAX_TOTAL_BYTES: number;
5
- /** Maximum number of entries in one archive. */
6
- export declare const MAX_ENTRY_COUNT = 100000;
7
- /** Maximum XML element nesting depth. */
8
- export declare const MAX_XML_DEPTH = 256;
9
- /**
10
- * Maximum number of XML nodes (elements + text runs) in one part. Sized
11
- * from the measured worst-case DOM cost (~400 bytes/node) so a saturating
12
- * part stays around the archive budget.
13
- */
14
- export declare const MAX_XML_NODES = 2000000;
15
- /** Maximum nesting depth of binary record containers (legacy PPT stream). */
16
- export declare const MAX_RECORD_DEPTH = 64;
17
- /** Maximum total binary records visited in one legacy record stream. */
18
- export declare const MAX_RECORDS = 16000000;