anyfile-cli 0.0.0-stage → 0.2.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/LICENSE +177 -0
- package/README.md +55 -2
- package/SKILL.md +101 -0
- package/chunks/browser-probe-5STDX455.mjs +17 -0
- package/chunks/browser-probe-F5RX44T3.mjs +24 -0
- package/chunks/chunk-2C34VMNV.mjs +108 -0
- package/chunks/chunk-3OZ56PUH.mjs +97 -0
- package/chunks/chunk-3YRWCQGV.mjs +38 -0
- package/chunks/chunk-53MJMIN4.mjs +23 -0
- package/chunks/chunk-5LOC7IST.mjs +372 -0
- package/chunks/chunk-5XTYLZF3.mjs +559 -0
- package/chunks/chunk-5ZHDYRGV.mjs +64 -0
- package/chunks/chunk-6CQL4K2K.mjs +21 -0
- package/chunks/chunk-6EVJJFOH.mjs +243 -0
- package/chunks/chunk-6NWSQGYE.mjs +68 -0
- package/chunks/chunk-7O5JLQE7.mjs +1 -0
- package/chunks/chunk-A6AQI6DF.mjs +321 -0
- package/chunks/chunk-AYF63UBE.mjs +79 -0
- package/chunks/chunk-B27PR6Q4.mjs +17 -0
- package/chunks/chunk-BP7LORL4.mjs +13 -0
- package/chunks/chunk-BVCRJLCD.mjs +109 -0
- package/chunks/chunk-CLSEE3VA.mjs +28 -0
- package/chunks/chunk-CO2ZAQIK.mjs +52 -0
- package/chunks/chunk-CQWM5GMU.mjs +167 -0
- package/chunks/chunk-CZFFYCMJ.mjs +136 -0
- package/chunks/chunk-D3CS6TIK.mjs +419 -0
- package/chunks/chunk-DJLFVMDJ.mjs +807 -0
- package/chunks/chunk-DJXVCKIO.mjs +30 -0
- package/chunks/chunk-EMXODK3K.mjs +94 -0
- package/chunks/chunk-EOJ7ZCDK.mjs +227 -0
- package/chunks/chunk-H4TBN22D.mjs +100 -0
- package/chunks/chunk-HI7TURZM.mjs +2256 -0
- package/chunks/chunk-HUSX3ZU3.mjs +58 -0
- package/chunks/chunk-IC3G2XE2.mjs +436 -0
- package/chunks/chunk-IGEGIF35.mjs +2034 -0
- package/chunks/chunk-IZGTVZHE.mjs +90 -0
- package/chunks/chunk-J4G7LUPA.mjs +117 -0
- package/chunks/chunk-JBAJW66L.mjs +4220 -0
- package/chunks/chunk-JKQUF2MM.mjs +438 -0
- package/chunks/chunk-KAMPC5TK.mjs +131 -0
- package/chunks/chunk-KSKEMJVC.mjs +145 -0
- package/chunks/chunk-LRHHRZLE.mjs +29 -0
- package/chunks/chunk-M37ZOAUG.mjs +93 -0
- package/chunks/chunk-MG7LUDXO.mjs +187 -0
- package/chunks/chunk-NGVHHTQD.mjs +125 -0
- package/chunks/chunk-NQ7J2NH6.mjs +50 -0
- package/chunks/chunk-NUMNHVSI.mjs +60 -0
- package/chunks/chunk-ORJ3DN65.mjs +58 -0
- package/chunks/chunk-QOUEBPPL.mjs +15 -0
- package/chunks/chunk-QR3Y3UKM.mjs +194 -0
- package/chunks/chunk-RFKYE7SE.mjs +50 -0
- package/chunks/chunk-SH57XOAV.mjs +33 -0
- package/chunks/chunk-TB5ISPA4.mjs +1 -0
- package/chunks/chunk-UGAU3V4V.mjs +206 -0
- package/chunks/chunk-WDN6NMVU.mjs +36 -0
- package/chunks/chunk-WWTLSPEG.mjs +3999 -0
- package/chunks/chunk-XDW56XVX.mjs +69 -0
- package/chunks/chunk-XRT7TAAY.mjs +27 -0
- package/chunks/chunk-XTW4HNIJ.mjs +418 -0
- package/chunks/chunk-YPLWNGCC.mjs +17 -0
- package/chunks/chunk-ZBR7RQQE.mjs +64 -0
- package/chunks/chunk-ZUHJIGHP.mjs +300 -0
- package/chunks/deflate-IINN6MRE.mjs +19 -0
- package/chunks/jpeg-N3DSYEAQ.mjs +964 -0
- package/chunks/jxl_oxide_wasm-3UN6G3YN.mjs +589 -0
- package/chunks/lerc-5LP7Q554.mjs +2086 -0
- package/chunks/lzw-Z2ZZBEWD.mjs +137 -0
- package/chunks/packbits-3S2JASDB.mjs +34 -0
- package/chunks/pdf.worker-PUGU2NS2.mjs +67416 -0
- package/chunks/probe-22WNVZ3I.mjs +17 -0
- package/chunks/probe-2P4CTFCY.mjs +14 -0
- package/chunks/probe-2QBNDPYK.mjs +18 -0
- package/chunks/probe-34TOVXUI.mjs +11 -0
- package/chunks/probe-3W2HOWBM.mjs +20 -0
- package/chunks/probe-42OBMD35.mjs +23 -0
- package/chunks/probe-43RVHUBG.mjs +17 -0
- package/chunks/probe-4O362IEX.mjs +18 -0
- package/chunks/probe-4WF34ZXS.mjs +11 -0
- package/chunks/probe-5ZITS3T5.mjs +40 -0
- package/chunks/probe-5ZS5DHFP.mjs +21 -0
- package/chunks/probe-62RFYKCP.mjs +14 -0
- package/chunks/probe-64JTFRH4.mjs +41 -0
- package/chunks/probe-6YJQJJ3R.mjs +518 -0
- package/chunks/probe-6YMOOV53.mjs +39 -0
- package/chunks/probe-BYBD2WAK.mjs +239 -0
- package/chunks/probe-CZZNJOHV.mjs +20 -0
- package/chunks/probe-DJMPK6HU.mjs +11 -0
- package/chunks/probe-FMWIF4LX.mjs +33 -0
- package/chunks/probe-FYPH6CYY.mjs +206 -0
- package/chunks/probe-G6PPXKBE.mjs +15 -0
- package/chunks/probe-GJ5OUOQ3.mjs +32 -0
- package/chunks/probe-H6J7TUY2.mjs +95 -0
- package/chunks/probe-IGZ47OE3.mjs +68 -0
- package/chunks/probe-IL5HO3OK.mjs +46 -0
- package/chunks/probe-INYCDZHO.mjs +27 -0
- package/chunks/probe-JFH545GO.mjs +11 -0
- package/chunks/probe-JZYDRITV.mjs +18 -0
- package/chunks/probe-K3WW5O5K.mjs +26 -0
- package/chunks/probe-KB4T466L.mjs +70 -0
- package/chunks/probe-KY7SIIXD.mjs +13 -0
- package/chunks/probe-L2P263WT.mjs +16 -0
- package/chunks/probe-LEP3RQPD.mjs +11 -0
- package/chunks/probe-M2QWULOI.mjs +119 -0
- package/chunks/probe-M7AYZBD3.mjs +35 -0
- package/chunks/probe-MEN3BMBB.mjs +22 -0
- package/chunks/probe-MNIGKDWT.mjs +163 -0
- package/chunks/probe-NFVFWB4M.mjs +56 -0
- package/chunks/probe-NJGSTL6R.mjs +161 -0
- package/chunks/probe-O73EHWT3.mjs +14 -0
- package/chunks/probe-OG2XYCG2.mjs +25 -0
- package/chunks/probe-OTKJKTNS.mjs +10 -0
- package/chunks/probe-OZKO3C2V.mjs +27 -0
- package/chunks/probe-PBL4KITG.mjs +12 -0
- package/chunks/probe-PEFZNH2T.mjs +21 -0
- package/chunks/probe-R4SSEXES.mjs +27 -0
- package/chunks/probe-RCAUVV27.mjs +29 -0
- package/chunks/probe-RPGBKGPI.mjs +20 -0
- package/chunks/probe-S2I46SEO.mjs +10 -0
- package/chunks/probe-SGDD4U4Q.mjs +165 -0
- package/chunks/probe-SPQO5LHI.mjs +9 -0
- package/chunks/probe-TBR6UGRZ.mjs +19 -0
- package/chunks/probe-TNKNILAW.mjs +21 -0
- package/chunks/probe-TVRJBPXH.mjs +48 -0
- package/chunks/probe-UESMTUQ5.mjs +33 -0
- package/chunks/probe-VDXFEZ5Q.mjs +16 -0
- package/chunks/probe-VX7JCDW6.mjs +39 -0
- package/chunks/probe-W7M55OPE.mjs +64 -0
- package/chunks/probe-XUBTIWGE.mjs +13 -0
- package/chunks/probe-ZF7KIRHO.mjs +36 -0
- package/chunks/raw-JJRPYAXO.mjs +16 -0
- package/chunks/tiff-metadata-7MWLNVW2.mjs +2990 -0
- package/chunks/webimage-D6XSCCTJ.mjs +56 -0
- package/chunks/zip-reader-YUGANW4Z.mjs +92 -0
- package/chunks/zstd-AH2S24PU.mjs +121 -0
- package/cli-main.mjs +297 -0
- package/cli.mjs +3 -0
- package/extractors/apple-plist.mjs +145 -0
- package/extractors/archive-metadata-viewer.mjs +1401 -0
- package/extractors/bittorrent-metadata.mjs +369 -0
- package/extractors/calendar-contacts.mjs +328 -0
- package/extractors/camera-raw.mjs +201 -0
- package/extractors/chm-reader.mjs +1842 -0
- package/extractors/comic-book-reader.mjs +307 -0
- package/extractors/design-source.mjs +1801 -0
- package/extractors/dev-array-viewer.mjs +825 -0
- package/extractors/dev-native-binary-viewer.mjs +1661 -0
- package/extractors/dev-source-map-viewer.mjs +416 -0
- package/extractors/dev-wasm-viewer.mjs +506 -0
- package/extractors/dicom.mjs +383 -0
- package/extractors/djvu-reader.mjs +337 -0
- package/extractors/duckdb-data.mjs +49174 -0
- package/extractors/epub-reader.mjs +222 -0
- package/extractors/excel-workbook.mjs +27057 -0
- package/extractors/fictionbook-reader.mjs +362 -0
- package/extractors/font-viewer.mjs +441 -0
- package/extractors/general-raster.mjs +444 -0
- package/extractors/hdr-image.mjs +1685 -0
- package/extractors/http-archive.mjs +306 -0
- package/extractors/ifc.mjs +341 -0
- package/extractors/iwork.mjs +640 -0
- package/extractors/microsoft-project.mjs +174 -0
- package/extractors/mind-map.mjs +607 -0
- package/extractors/ml-weights.mjs +649 -0
- package/extractors/modern-raster.mjs +620 -0
- package/extractors/ofd-reader.mjs +973 -0
- package/extractors/outlook-pst.mjs +997 -0
- package/extractors/pdfjs-pdf.mjs +32056 -0
- package/extractors/photoshop-document.mjs +17448 -0
- package/extractors/safe-svg.mjs +163 -0
- package/extractors/scientific-data.mjs +2041 -0
- package/extractors/sqlite-database.mjs +333 -0
- package/extractors/statistical-data.mjs +297 -0
- package/extractors/visio.mjs +141 -0
- package/extractors/x509-certificate.mjs +588 -0
- package/extractors/xps-reader.mjs +896 -0
- package/index.mjs +83 -0
- package/licenses/dicom-parser/1.8.21/LICENSE +21 -0
- package/licenses/libraw-wasm/1.6.0/COPYRIGHT.LibRaw +27 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.Emscripten +102 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.LibRaw-CDDL-1.0 +340 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.Little-CMS-MIT +22 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.libpng +134 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.libraw-wasm-ISC +15 -0
- package/licenses/libraw-wasm/1.6.0/LICENSE.zlib +22 -0
- package/licenses/libraw-wasm/1.6.0/NOTICE.IJG-libjpeg +49 -0
- package/licenses/libraw-wasm/1.6.0/SOURCE.md +25 -0
- package/licenses/libraw-wasm/1.6.0/THIRD_PARTY_NOTICES.md +14 -0
- package/licenses/mediabunny/1.55.3/MPL-2.0.txt +373 -0
- package/licenses/mediabunny/1.55.3/SOURCE.md +16 -0
- package/licenses/openskp/1.3.0/LICENSE +21 -0
- package/package.json +34 -4
- package/renderer-host/assets/ColladaComposer-TCm9fgZa.js +1 -0
- package/renderer-host/assets/ColladaParser-BHTzbhBz.js +3 -0
- package/renderer-host/assets/FBXLoader-D1vDnL3o.js +8 -0
- package/renderer-host/assets/agent-CKvDbj87.js +1 -0
- package/renderer-host/assets/amf-BkE--DNX.js +1 -0
- package/renderer-host/assets/animation-Dt0WW2cB.js +1 -0
- package/renderer-host/assets/archive-client-CL5yOgwH.js +1 -0
- package/renderer-host/assets/archive.worker-CcLnJ9FC.js +1 -0
- package/renderer-host/assets/audio-visualizer-CiyGNhY2.js +1 -0
- package/renderer-host/assets/basedecoder-BuG7fe0q.js +1 -0
- package/renderer-host/assets/book-source-28BQcBKR.js +1 -0
- package/renderer-host/assets/book-worker-DUt14LJO.js +1 -0
- package/renderer-host/assets/browser-C0pGRq_c.js +1 -0
- package/renderer-host/assets/client-CU2ERPtk.js +1 -0
- package/renderer-host/assets/container-inspection-ekEPWWcy.js +1 -0
- package/renderer-host/assets/data-table-OJQe3aN5.js +1 -0
- package/renderer-host/assets/decode-hBroL8C1.js +1 -0
- package/renderer-host/assets/decode-tiff-CP1xdrF_.js +5 -0
- package/renderer-host/assets/decoder-worker-B0sdTVlF.js +1 -0
- package/renderer-host/assets/decoder-worker-CBiHE971.js +3 -0
- package/renderer-host/assets/decoder-worker-Cr5oP97p.js +5 -0
- package/renderer-host/assets/decoder-worker-DIdLFebY.js +11 -0
- package/renderer-host/assets/decoder-worker-DLeuEYxq.js +1 -0
- package/renderer-host/assets/decoder.worker-BXAaF0Qj.js +1 -0
- package/renderer-host/assets/decoder.worker-yaM6uups.js +1 -0
- package/renderer-host/assets/deflate-DPjr4M2G.js +1 -0
- package/renderer-host/assets/deflate-S3QmZI-f.js +1 -0
- package/renderer-host/assets/dual-track-playback-CxjGms-W.js +1 -0
- package/renderer-host/assets/ext-searchbox-BIdon51Z.js +164 -0
- package/renderer-host/assets/ffmpeg-playback-DzCJz9Ny.js +1 -0
- package/renderer-host/assets/gltf-PozNLKNg.js +1 -0
- package/renderer-host/assets/hdf5.worker-BO3TrRup.js +1 -0
- package/renderer-host/assets/heif-worker-CC9DI3ZR.js +1 -0
- package/renderer-host/assets/id3-uzaDmDwy.js +1 -0
- package/renderer-host/assets/ifc.worker-KIbgAIsy.js +1 -0
- package/renderer-host/assets/image-load-SK3ULGWm.js +1 -0
- package/renderer-host/assets/index-DtiyOy_J.js +2 -0
- package/renderer-host/assets/jpeg-AGQ46WDa.js +1 -0
- package/renderer-host/assets/jpeg-Dm0yBuYB.js +1 -0
- package/renderer-host/assets/jszip.min-DCdQ6XWv.js +2 -0
- package/renderer-host/assets/jxl_oxide_wasm-BJKDqoXP.js +1 -0
- package/renderer-host/assets/jxl_oxide_wasm_bg-C5JqlrFv.wasm +0 -0
- package/renderer-host/assets/kernel.worker-B58X9XWu.js +1 -0
- package/renderer-host/assets/kernel.worker-BcQYKb39.js +8 -0
- package/renderer-host/assets/kernel.worker-BdWmPdqg.js +1 -0
- package/renderer-host/assets/kernel.worker-C7qxomuK.js +1 -0
- package/renderer-host/assets/kernel.worker-D88TbQnz.js +1 -0
- package/renderer-host/assets/laz-Lt10LsWd.js +1 -0
- package/renderer-host/assets/lerc-BxVqCKaH.js +3 -0
- package/renderer-host/assets/lerc-Cuor-Zm2.js +1 -0
- package/renderer-host/assets/lzw-D2FslT4L.js +1 -0
- package/renderer-host/assets/lzw-DODNa9l5.js +1 -0
- package/renderer-host/assets/mediabunny-viewer-BniHVEWJ.js +16 -0
- package/renderer-host/assets/model-Drd3vqcF.js +1 -0
- package/renderer-host/assets/obj-B3p81LL2.js +9 -0
- package/renderer-host/assets/off-QQbiKPTX.js +1 -0
- package/renderer-host/assets/ogg-probe-C2TT74FL.js +1 -0
- package/renderer-host/assets/ogg-viewer-Dc8K-tas.js +9 -0
- package/renderer-host/assets/packbits-DCimUQ1T.js +1 -0
- package/renderer-host/assets/paged-C8AQD7sX.js +319 -0
- package/renderer-host/assets/pako.esm-0UYEm1Jg.js +2 -0
- package/renderer-host/assets/pako.esm-CtVA0R7W.js +1 -0
- package/renderer-host/assets/pdf.worker.min-CHFwMXne.mjs +29 -0
- package/renderer-host/assets/ply-C3_RRA7n.js +2 -0
- package/renderer-host/assets/points.worker-CA8v75dl.js +2 -0
- package/renderer-host/assets/probe-asf-C_UpCBly.js +14 -0
- package/renderer-host/assets/range-reader-BMfLhWcp.js +1 -0
- package/renderer-host/assets/raw-A-vJRPpa.js +1 -0
- package/renderer-host/assets/resource-cache-CHocY8K3.js +1 -0
- package/renderer-host/assets/resources-6AmS6O5e.js +1 -0
- package/renderer-host/assets/rolldown-runtime-Dd_uD5pT.js +1 -0
- package/renderer-host/assets/safe-content-CWHf8jex.js +1 -0
- package/renderer-host/assets/sanitize-D56ODnaM.js +1 -0
- package/renderer-host/assets/scene-adapter-CaCLr1am.js +1 -0
- package/renderer-host/assets/scene.worker-CJViWLYo.js +24 -0
- package/renderer-host/assets/scene.worker-D7koFZY8.js +2 -0
- package/renderer-host/assets/skp-adapter-jl6q24fW.js +1 -0
- package/renderer-host/assets/skp.worker-D9ujTXFV.js +1 -0
- package/renderer-host/assets/src-1Won7FxU.js +88 -0
- package/renderer-host/assets/src-3RoHqOkA.js +7 -0
- package/renderer-host/assets/src-4zBOL6Jt.js +1 -0
- package/renderer-host/assets/src-6v8-hoR3.js +24 -0
- package/renderer-host/assets/src-8Jx9gbII.js +1 -0
- package/renderer-host/assets/src-B1WA0dRu.js +64 -0
- package/renderer-host/assets/src-B5ijbemo.js +9 -0
- package/renderer-host/assets/src-B8DH2GJe.js +17 -0
- package/renderer-host/assets/src-B9t_iEO3.js +58 -0
- package/renderer-host/assets/src-BCC58C0X.js +1 -0
- package/renderer-host/assets/src-BCwENs26.js +1 -0
- package/renderer-host/assets/src-BEirBh4c.js +27 -0
- package/renderer-host/assets/src-BHR6n7Fj.js +1 -0
- package/renderer-host/assets/src-BIlLq0QT.js +169 -0
- package/renderer-host/assets/src-BKlK1jcN.js +1 -0
- package/renderer-host/assets/src-BN0FUJhs.js +8 -0
- package/renderer-host/assets/src-BWElAdlq.js +52 -0
- package/renderer-host/assets/src-BXg11IjY.js +4 -0
- package/renderer-host/assets/src-BbBdnTjJ.js +2 -0
- package/renderer-host/assets/src-BbFZbZsL.js +2 -0
- package/renderer-host/assets/src-BdPbFRd-.js +24 -0
- package/renderer-host/assets/src-BpbGaBrT.js +182 -0
- package/renderer-host/assets/src-BwIl76_H.js +7 -0
- package/renderer-host/assets/src-C2e6QJIj.js +10 -0
- package/renderer-host/assets/src-C2gNidHF.js +989 -0
- package/renderer-host/assets/src-C9Uz3C-q.js +432 -0
- package/renderer-host/assets/src-CBUhG6W5.js +7 -0
- package/renderer-host/assets/src-CQDGi3Qa.js +10 -0
- package/renderer-host/assets/src-CRLCRg0N.js +21 -0
- package/renderer-host/assets/src-CS5rqxBy.js +10 -0
- package/renderer-host/assets/src-CWZdSJQw.js +7 -0
- package/renderer-host/assets/src-CXnpsVpU.js +27 -0
- package/renderer-host/assets/src-CnhXvqQY.js +8 -0
- package/renderer-host/assets/src-CpExg0ZA.js +9 -0
- package/renderer-host/assets/src-CrLmL3wU.js +5 -0
- package/renderer-host/assets/src-CrYEJ5uz.js +2 -0
- package/renderer-host/assets/src-D-iIkSqZ.js +222 -0
- package/renderer-host/assets/src-D6sH9uTT.js +23 -0
- package/renderer-host/assets/src-D8zHDJh_.js +10 -0
- package/renderer-host/assets/src-D9OFaPNV.js +1 -0
- package/renderer-host/assets/src-DCI_ff7c.js +1 -0
- package/renderer-host/assets/src-DCrbiTpH.js +18 -0
- package/renderer-host/assets/src-DK8DvtMd.js +115 -0
- package/renderer-host/assets/src-DWGlOVjt.js +17 -0
- package/renderer-host/assets/src-DbvEX_Ct.js +6 -0
- package/renderer-host/assets/src-Dezc2dj1.js +1 -0
- package/renderer-host/assets/src-Dh5VD1lL.js +11 -0
- package/renderer-host/assets/src-DlwABqw5.js +1 -0
- package/renderer-host/assets/src-DlxETlTh.js +44 -0
- package/renderer-host/assets/src-Dmd6BvBh.js +387 -0
- package/renderer-host/assets/src-DpgvaJNJ.js +24 -0
- package/renderer-host/assets/src-DpjRB2sv.js +1 -0
- package/renderer-host/assets/src-Dvn41FR7.js +90 -0
- package/renderer-host/assets/src-DwcPYp7X.js +1 -0
- package/renderer-host/assets/src-HUxhcDlO.js +2 -0
- package/renderer-host/assets/src-Jhqg3J8L.js +2 -0
- package/renderer-host/assets/src-MO5bLsLj.js +4126 -0
- package/renderer-host/assets/src-N8XBQaK4.js +20 -0
- package/renderer-host/assets/src-XZ_0U972.js +1 -0
- package/renderer-host/assets/src-Y6mXl4vh.js +29 -0
- package/renderer-host/assets/src-b9aXVJde.js +100 -0
- package/renderer-host/assets/src-d2U6neq8.js +37 -0
- package/renderer-host/assets/src-hCrza_Lf.js +1 -0
- package/renderer-host/assets/src-jUGjKzOV.js +1 -0
- package/renderer-host/assets/src-rJN_jaC5.js +1 -0
- package/renderer-host/assets/src-vOBd-1zk.js +5 -0
- package/renderer-host/assets/src-wrrVkfrK.js +1 -0
- package/renderer-host/assets/src-yCrAObB-.js +1 -0
- package/renderer-host/assets/src-zrBiCtay.js +7 -0
- package/renderer-host/assets/stl-adapter-CLkWHoOn.js +1 -0
- package/renderer-host/assets/stl.worker-C-anHijM.js +1 -0
- package/renderer-host/assets/three-mf-BSrcsdQD.js +2 -0
- package/renderer-host/assets/webimage-zQMH-_zR.js +1 -0
- package/renderer-host/assets/webm-14HHwfWy.js +1 -0
- package/renderer-host/assets/worker-QK2gViTu.js +1 -0
- package/renderer-host/assets/x6-dng-worker-CNf3zUP-.js +1 -0
- package/renderer-host/assets/xml.browser-CC8-XAyN.js +1 -0
- package/renderer-host/assets/zip-core-base-DD8akpjq.js +1 -0
- package/renderer-host/assets/zip-core-custom-CfPFs1lw.js +1 -0
- package/renderer-host/assets/zip-source-CLF3Hfcg.js +1 -0
- package/renderer-host/assets/zstd-CmwSLjHB.js +1 -0
- package/renderer-host/assets/zstd-i4Jwubnx.js +1 -0
- package/renderer-host/index.html +65 -0
- package/vendor-manifest.json +1433 -0
- package/worker.mjs +262 -0
|
@@ -0,0 +1,1842 @@
|
|
|
1
|
+
import { createRequire as __anyfileCreateRequire } from "node:module"; import { dirname as __anyfileDirname } from "node:path"; import { fileURLToPath as __anyfileFileURLToPath } from "node:url"; const require = __anyfileCreateRequire(import.meta.url); const __filename = __anyfileFileURLToPath(import.meta.url); const __dirname = __anyfileDirname(__filename);
|
|
2
|
+
import {
|
|
3
|
+
MARKUP_LIMITS,
|
|
4
|
+
elements,
|
|
5
|
+
localReference,
|
|
6
|
+
parseXml
|
|
7
|
+
} from "../chunks/chunk-6NWSQGYE.mjs";
|
|
8
|
+
import {
|
|
9
|
+
CONTENT_CSP,
|
|
10
|
+
publicationCopy
|
|
11
|
+
} from "../chunks/chunk-NUMNHVSI.mjs";
|
|
12
|
+
import {
|
|
13
|
+
inspectImageFile
|
|
14
|
+
} from "../chunks/chunk-A6AQI6DF.mjs";
|
|
15
|
+
import {
|
|
16
|
+
markupToMarkdown,
|
|
17
|
+
parseHtmlText
|
|
18
|
+
} from "../chunks/chunk-M37ZOAUG.mjs";
|
|
19
|
+
import {
|
|
20
|
+
checkBookAbort
|
|
21
|
+
} from "../chunks/chunk-BP7LORL4.mjs";
|
|
22
|
+
import "../chunks/chunk-53MJMIN4.mjs";
|
|
23
|
+
import {
|
|
24
|
+
AgentError,
|
|
25
|
+
ViewerError,
|
|
26
|
+
abortError,
|
|
27
|
+
isViewerAbortError,
|
|
28
|
+
markdownDocumentHeader,
|
|
29
|
+
readPagedTextDocument,
|
|
30
|
+
selectMessages
|
|
31
|
+
} from "../chunks/chunk-IC3G2XE2.mjs";
|
|
32
|
+
import "../chunks/chunk-ORJ3DN65.mjs";
|
|
33
|
+
|
|
34
|
+
// ../plugins/chm/src/encoding.ts
|
|
35
|
+
var LCID_ENCODING = {
|
|
36
|
+
1028: "big5",
|
|
37
|
+
3076: "big5",
|
|
38
|
+
5124: "big5",
|
|
39
|
+
2052: "gbk",
|
|
40
|
+
4100: "gbk",
|
|
41
|
+
1041: "shift_jis",
|
|
42
|
+
1042: "euc-kr",
|
|
43
|
+
1049: "windows-1251",
|
|
44
|
+
1058: "windows-1251",
|
|
45
|
+
1059: "windows-1251",
|
|
46
|
+
1029: "windows-1250",
|
|
47
|
+
1038: "windows-1250",
|
|
48
|
+
1045: "windows-1250",
|
|
49
|
+
1048: "windows-1250",
|
|
50
|
+
1050: "windows-1250",
|
|
51
|
+
1051: "windows-1250",
|
|
52
|
+
1060: "windows-1250",
|
|
53
|
+
1032: "windows-1253",
|
|
54
|
+
1055: "windows-1254",
|
|
55
|
+
1037: "windows-1255",
|
|
56
|
+
1025: "windows-1256",
|
|
57
|
+
2049: "windows-1256",
|
|
58
|
+
1065: "windows-1256",
|
|
59
|
+
1061: "windows-1257",
|
|
60
|
+
1062: "windows-1257",
|
|
61
|
+
1063: "windows-1257",
|
|
62
|
+
1066: "windows-1258",
|
|
63
|
+
1054: "windows-874"
|
|
64
|
+
};
|
|
65
|
+
var utf8Fatal = new TextDecoder("utf-8", { fatal: true });
|
|
66
|
+
var latin1Decoder = new TextDecoder("windows-1252");
|
|
67
|
+
function encodingFromLcid(lcid) {
|
|
68
|
+
return LCID_ENCODING[lcid & 65535] ?? "windows-1252";
|
|
69
|
+
}
|
|
70
|
+
function tryDecode(bytes, label) {
|
|
71
|
+
try {
|
|
72
|
+
return new TextDecoder(label, { fatal: true }).decode(bytes);
|
|
73
|
+
} catch {
|
|
74
|
+
try {
|
|
75
|
+
return new TextDecoder(label).decode(bytes);
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function detectHtmlCharset(bytes) {
|
|
82
|
+
const head = latin1Decoder.decode(bytes.subarray(0, Math.min(bytes.byteLength, 2048)));
|
|
83
|
+
const metaCharset = /<meta\b[^>]*\bcharset\s*=\s*["']?\s*([a-z0-9._-]+)/i.exec(head);
|
|
84
|
+
if (metaCharset) return metaCharset[1].toLowerCase();
|
|
85
|
+
const contentType = /<meta\b[^>]*\bhttp-equiv\s*=\s*["']?content-type["']?[^>]*\bcontent\s*=\s*["'][^"']*charset\s*=\s*([a-z0-9._-]+)/i.exec(
|
|
86
|
+
head
|
|
87
|
+
);
|
|
88
|
+
if (contentType) return contentType[1].toLowerCase();
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
function decodeChmText(bytes, fallbackEncoding, isHtml = false) {
|
|
92
|
+
if (bytes.byteLength === 0) return "";
|
|
93
|
+
if (bytes.byteLength >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191) {
|
|
94
|
+
return new TextDecoder("utf-8").decode(bytes.subarray(3));
|
|
95
|
+
}
|
|
96
|
+
if (bytes.byteLength >= 2 && bytes[0] === 255 && bytes[1] === 254) {
|
|
97
|
+
return new TextDecoder("utf-16le").decode(bytes.subarray(2));
|
|
98
|
+
}
|
|
99
|
+
if (bytes.byteLength >= 2 && bytes[0] === 254 && bytes[1] === 255) {
|
|
100
|
+
return new TextDecoder("utf-16be").decode(bytes.subarray(2));
|
|
101
|
+
}
|
|
102
|
+
if (isHtml) {
|
|
103
|
+
const declared = detectHtmlCharset(bytes);
|
|
104
|
+
if (declared && declared !== "utf-8" && declared !== "utf8") {
|
|
105
|
+
const decoded = tryDecode(bytes, declared);
|
|
106
|
+
if (decoded !== null) return decoded;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return utf8Fatal.decode(bytes);
|
|
111
|
+
} catch {
|
|
112
|
+
const fallback = tryDecode(bytes, fallbackEncoding);
|
|
113
|
+
if (fallback !== null) return fallback;
|
|
114
|
+
return latin1Decoder.decode(bytes);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ../plugins/chm/src/limits.ts
|
|
119
|
+
var CHM_LIMITS = {
|
|
120
|
+
maxFileBytes: 128 * 1024 * 1024,
|
|
121
|
+
maxHeaderBytes: 512,
|
|
122
|
+
maxDirChunks: 4096,
|
|
123
|
+
maxEntries: 2e4,
|
|
124
|
+
maxSectionBytes: 128 * 1024 * 1024,
|
|
125
|
+
maxSystemBytes: 4 * 1024 * 1024,
|
|
126
|
+
maxTopicBytes: 2 * 1024 * 1024,
|
|
127
|
+
maxTopics: 2e3,
|
|
128
|
+
maxTocItems: 1e4,
|
|
129
|
+
maxResetEntries: 65536,
|
|
130
|
+
maxResetIntervalBytes: 16 * 1024 * 1024,
|
|
131
|
+
maxCachedIntervals: 8
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// ../plugins/chm/src/chm-metadata.ts
|
|
135
|
+
function normalizeChmPath(rawPath) {
|
|
136
|
+
let clean = rawPath.trim().replace(/\\/g, "/");
|
|
137
|
+
try {
|
|
138
|
+
clean = decodeURIComponent(clean);
|
|
139
|
+
} catch {
|
|
140
|
+
}
|
|
141
|
+
clean = clean.replace(/^\/+/, "");
|
|
142
|
+
const parts = [];
|
|
143
|
+
for (const seg of clean.split("/")) {
|
|
144
|
+
if (!seg || seg === ".") continue;
|
|
145
|
+
if (seg === "..") {
|
|
146
|
+
if (parts.length > 0) parts.pop();
|
|
147
|
+
} else {
|
|
148
|
+
parts.push(seg);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return "/" + parts.join("/");
|
|
152
|
+
}
|
|
153
|
+
function resolveChmEntry(directory, basePath, reference, selfChmNames) {
|
|
154
|
+
const trimmed = reference.trim();
|
|
155
|
+
if (!trimmed || /^(?:javascript|vbscript|data|mailto|tel|https?|ftp):/i.test(trimmed)) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
let localRef = trimmed;
|
|
159
|
+
const msItsMatch = /^(?:ms-its|its|mk:@MSITStore):([^:]*)::(.*)$/i.exec(localRef);
|
|
160
|
+
if (msItsMatch) {
|
|
161
|
+
const chmName = msItsMatch[1].trim().replace(/\\/g, "/").split("/").pop()?.toLowerCase() ?? "";
|
|
162
|
+
if (chmName && (!selfChmNames || !selfChmNames.has(chmName))) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
const targetPath = msItsMatch[2].trim();
|
|
166
|
+
if (!targetPath.startsWith("/")) return null;
|
|
167
|
+
localRef = targetPath;
|
|
168
|
+
} else if (/^[a-z][\w+.-]*:/i.test(localRef)) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const [rawTarget, rawFragment = ""] = localRef.split("#", 2);
|
|
172
|
+
let fragment = rawFragment;
|
|
173
|
+
try {
|
|
174
|
+
fragment = decodeURIComponent(rawFragment);
|
|
175
|
+
} catch {
|
|
176
|
+
}
|
|
177
|
+
if (!rawTarget) {
|
|
178
|
+
const exact = directory.byExactPath.get(basePath) ?? directory.byLowerPath.get(basePath.toLowerCase());
|
|
179
|
+
return exact ? { entry: exact, fragment } : null;
|
|
180
|
+
}
|
|
181
|
+
const candidates = [];
|
|
182
|
+
if (rawTarget.startsWith("/")) {
|
|
183
|
+
candidates.push(normalizeChmPath(rawTarget));
|
|
184
|
+
} else {
|
|
185
|
+
const baseDir = basePath.includes("/") ? basePath.slice(0, basePath.lastIndexOf("/") + 1) : "/";
|
|
186
|
+
candidates.push(normalizeChmPath(baseDir + rawTarget));
|
|
187
|
+
candidates.push(normalizeChmPath("/" + rawTarget));
|
|
188
|
+
}
|
|
189
|
+
for (const candidate of candidates) {
|
|
190
|
+
const exact = directory.byExactPath.get(candidate) ?? directory.byLowerPath.get(candidate.toLowerCase());
|
|
191
|
+
if (exact) return { entry: exact, fragment };
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
function readNullTerminated(bytes, encoding) {
|
|
196
|
+
let end = bytes.indexOf(0);
|
|
197
|
+
if (end < 0) end = bytes.length;
|
|
198
|
+
return decodeChmText(bytes.subarray(0, end), encoding).trim();
|
|
199
|
+
}
|
|
200
|
+
function parseSystemFile(bytes, fallbackEncoding) {
|
|
201
|
+
let encoding = fallbackEncoding;
|
|
202
|
+
let title = "";
|
|
203
|
+
let compiledName = "";
|
|
204
|
+
let defaultTopic = "";
|
|
205
|
+
let hhcPath = "";
|
|
206
|
+
let hhkPath = "";
|
|
207
|
+
if (bytes.byteLength < 4) {
|
|
208
|
+
return { title, compiledName, defaultTopic, hhcPath, hhkPath, encoding };
|
|
209
|
+
}
|
|
210
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
211
|
+
const records = [];
|
|
212
|
+
let pos = 4;
|
|
213
|
+
while (pos + 4 <= bytes.byteLength) {
|
|
214
|
+
const code = view.getUint16(pos, true);
|
|
215
|
+
const len = view.getUint16(pos + 2, true);
|
|
216
|
+
pos += 4;
|
|
217
|
+
if (pos + len > bytes.byteLength) break;
|
|
218
|
+
const payload = bytes.subarray(pos, pos + len);
|
|
219
|
+
pos += len;
|
|
220
|
+
if (code === 4 && payload.byteLength >= 4) {
|
|
221
|
+
const lcid = new DataView(payload.buffer, payload.byteOffset, payload.byteLength).getUint32(0, true);
|
|
222
|
+
if (lcid > 0) encoding = encodingFromLcid(lcid);
|
|
223
|
+
}
|
|
224
|
+
records.push({ code, payload });
|
|
225
|
+
}
|
|
226
|
+
for (const { code, payload } of records) {
|
|
227
|
+
if (code === 0 && !hhcPath) hhcPath = readNullTerminated(payload, encoding);
|
|
228
|
+
else if (code === 1 && !hhkPath) hhkPath = readNullTerminated(payload, encoding);
|
|
229
|
+
else if (code === 2 && !defaultTopic) defaultTopic = readNullTerminated(payload, encoding);
|
|
230
|
+
else if (code === 3 && !title) title = readNullTerminated(payload, encoding);
|
|
231
|
+
else if (code === 6 && !compiledName) compiledName = readNullTerminated(payload, encoding);
|
|
232
|
+
}
|
|
233
|
+
return { title, compiledName, defaultTopic, hhcPath, hhkPath, encoding };
|
|
234
|
+
}
|
|
235
|
+
function readStringsAt(stringsBytes, offset, encoding) {
|
|
236
|
+
if (offset <= 0 || offset >= stringsBytes.byteLength) return "";
|
|
237
|
+
let end = offset;
|
|
238
|
+
while (end < stringsBytes.byteLength && stringsBytes[end] !== 0) end += 1;
|
|
239
|
+
return decodeChmText(stringsBytes.subarray(offset, end), encoding).trim();
|
|
240
|
+
}
|
|
241
|
+
function readUrlStrAt(urlStrBytes, offset, encoding) {
|
|
242
|
+
if (offset < 0 || offset + 8 >= urlStrBytes.byteLength) return "";
|
|
243
|
+
let pos = offset + 8;
|
|
244
|
+
while (pos < urlStrBytes.byteLength && urlStrBytes[pos] !== 0) pos += 1;
|
|
245
|
+
return decodeChmText(urlStrBytes.subarray(offset + 8, pos), encoding).trim();
|
|
246
|
+
}
|
|
247
|
+
function parseSitemapHtml(htmlText, directory, sitemapPath) {
|
|
248
|
+
const doc = parseHtmlText(htmlText);
|
|
249
|
+
let flatCount = 0;
|
|
250
|
+
function extractObject(li) {
|
|
251
|
+
const obj = Array.from(li.children).find((el) => el.localName === "object");
|
|
252
|
+
if (!obj) return null;
|
|
253
|
+
const type = (obj.getAttribute("type") ?? "").toLowerCase();
|
|
254
|
+
if (type !== "text/sitemap") return null;
|
|
255
|
+
let label = "";
|
|
256
|
+
let local = "";
|
|
257
|
+
for (const param of Array.from(obj.querySelectorAll("param"))) {
|
|
258
|
+
const name = (param.getAttribute("name") ?? "").toLowerCase();
|
|
259
|
+
const value = (param.getAttribute("value") ?? "").trim();
|
|
260
|
+
if (name === "name" && !label) label = value;
|
|
261
|
+
else if ((name === "local" || name === "url") && !local) local = value;
|
|
262
|
+
}
|
|
263
|
+
if (!label && !local) return null;
|
|
264
|
+
if (!local) return { label: label || "Section", path: "", fragment: "" };
|
|
265
|
+
const resolved = resolveChmEntry(directory, sitemapPath, local);
|
|
266
|
+
if (!resolved) return { label: label || local, path: "", fragment: "" };
|
|
267
|
+
return {
|
|
268
|
+
label: label || resolved.entry.path.split("/").pop() || resolved.entry.path,
|
|
269
|
+
path: resolved.entry.path,
|
|
270
|
+
fragment: resolved.fragment
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function walkList(ul, depth) {
|
|
274
|
+
if (depth > 32 || flatCount >= CHM_LIMITS.maxTocItems) return [];
|
|
275
|
+
const nodes = [];
|
|
276
|
+
for (const child of Array.from(ul.children)) {
|
|
277
|
+
if (flatCount >= CHM_LIMITS.maxTocItems) break;
|
|
278
|
+
if (child.localName === "li") {
|
|
279
|
+
const info = extractObject(child);
|
|
280
|
+
const childLists = Array.from(child.children).filter((el) => el.localName === "ul");
|
|
281
|
+
const children = childLists.flatMap((sub) => walkList(sub, depth + 1));
|
|
282
|
+
if (info) {
|
|
283
|
+
flatCount += 1;
|
|
284
|
+
nodes.push({
|
|
285
|
+
label: info.label,
|
|
286
|
+
path: info.path,
|
|
287
|
+
fragment: info.fragment,
|
|
288
|
+
children
|
|
289
|
+
});
|
|
290
|
+
} else if (children.length > 0) {
|
|
291
|
+
nodes.push(...children);
|
|
292
|
+
}
|
|
293
|
+
} else if (child.localName === "ul") {
|
|
294
|
+
const last = nodes[nodes.length - 1];
|
|
295
|
+
const nested = walkList(child, depth + 1);
|
|
296
|
+
if (last && nested.length > 0) {
|
|
297
|
+
nodes[nodes.length - 1] = {
|
|
298
|
+
...last,
|
|
299
|
+
children: [...last.children, ...nested]
|
|
300
|
+
};
|
|
301
|
+
} else {
|
|
302
|
+
nodes.push(...nested);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return nodes;
|
|
307
|
+
}
|
|
308
|
+
const rootLists = Array.from(doc.querySelectorAll("ul")).filter(
|
|
309
|
+
(ul) => !ul.parentElement || ul.parentElement.localName !== "li"
|
|
310
|
+
);
|
|
311
|
+
const topLists = rootLists.filter((ul) => {
|
|
312
|
+
let parent = ul.parentElement;
|
|
313
|
+
while (parent) {
|
|
314
|
+
if (parent.localName === "ul") return false;
|
|
315
|
+
parent = parent.parentElement;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
});
|
|
319
|
+
const tree = topLists.flatMap((ul) => walkList(ul, 0));
|
|
320
|
+
return { tree, flatCount };
|
|
321
|
+
}
|
|
322
|
+
function flattenIndex(nodes, out = []) {
|
|
323
|
+
for (const node of nodes) {
|
|
324
|
+
if (out.length >= CHM_LIMITS.maxTocItems) break;
|
|
325
|
+
if (node.path) {
|
|
326
|
+
out.push({ label: node.label, path: node.path, fragment: node.fragment });
|
|
327
|
+
}
|
|
328
|
+
if (node.children.length > 0) {
|
|
329
|
+
flattenIndex(node.children, out);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
async function readChmMetadata(directory, reader, signal) {
|
|
335
|
+
const initialEncoding = encodingFromLcid(directory.lcid);
|
|
336
|
+
let systemInfo = {
|
|
337
|
+
title: "",
|
|
338
|
+
compiledName: "",
|
|
339
|
+
defaultTopic: "",
|
|
340
|
+
hhcPath: "",
|
|
341
|
+
hhkPath: "",
|
|
342
|
+
encoding: initialEncoding
|
|
343
|
+
};
|
|
344
|
+
const systemEntry = directory.byLowerPath.get("/#system");
|
|
345
|
+
if (systemEntry) {
|
|
346
|
+
const bytes = await reader.readEntry(systemEntry, CHM_LIMITS.maxSystemBytes, signal);
|
|
347
|
+
systemInfo = parseSystemFile(bytes, initialEncoding);
|
|
348
|
+
}
|
|
349
|
+
const encoding = systemInfo.encoding;
|
|
350
|
+
const hhcResolved = (systemInfo.hhcPath ? resolveChmEntry(directory, "/", systemInfo.hhcPath)?.entry : null) ?? directory.entries.find((e) => !e.path.startsWith("::") && e.lowerPath.endsWith(".hhc")) ?? null;
|
|
351
|
+
const hhkResolved = (systemInfo.hhkPath ? resolveChmEntry(directory, "/", systemInfo.hhkPath)?.entry : null) ?? directory.entries.find((e) => !e.path.startsWith("::") && e.lowerPath.endsWith(".hhk")) ?? null;
|
|
352
|
+
let tocTree = [];
|
|
353
|
+
if (hhcResolved && hhcResolved.length > 0) {
|
|
354
|
+
const hhcBytes = await reader.readEntry(hhcResolved, CHM_LIMITS.maxTopicBytes, signal);
|
|
355
|
+
const hhcText = decodeChmText(hhcBytes, encoding, true);
|
|
356
|
+
tocTree = parseSitemapHtml(hhcText, directory, hhcResolved.path).tree;
|
|
357
|
+
}
|
|
358
|
+
let indexEntries = [];
|
|
359
|
+
if (hhkResolved && hhkResolved.length > 0) {
|
|
360
|
+
const hhkBytes = await reader.readEntry(hhkResolved, CHM_LIMITS.maxTopicBytes, signal);
|
|
361
|
+
const hhkText = decodeChmText(hhkBytes, encoding, true);
|
|
362
|
+
indexEntries = flattenIndex(parseSitemapHtml(hhkText, directory, hhkResolved.path).tree);
|
|
363
|
+
}
|
|
364
|
+
const systemTopics = [];
|
|
365
|
+
const topicsEntry = directory.byLowerPath.get("/#topics");
|
|
366
|
+
const stringsEntry = directory.byLowerPath.get("/#strings");
|
|
367
|
+
const urlTblEntry = directory.byLowerPath.get("/#urltbl");
|
|
368
|
+
const urlStrEntry = directory.byLowerPath.get("/#urlstr");
|
|
369
|
+
if (topicsEntry && urlTblEntry && urlStrEntry) {
|
|
370
|
+
const [topicsBytes, stringsBytes, urlTblBytes, urlStrBytes] = await Promise.all([
|
|
371
|
+
reader.readEntry(topicsEntry, CHM_LIMITS.maxSystemBytes, signal),
|
|
372
|
+
stringsEntry ? reader.readEntry(stringsEntry, CHM_LIMITS.maxSystemBytes, signal) : Promise.resolve(new Uint8Array(0)),
|
|
373
|
+
reader.readEntry(urlTblEntry, CHM_LIMITS.maxSystemBytes, signal),
|
|
374
|
+
reader.readEntry(urlStrEntry, CHM_LIMITS.maxSystemBytes, signal)
|
|
375
|
+
]);
|
|
376
|
+
const topicsView = new DataView(topicsBytes.buffer, topicsBytes.byteOffset, topicsBytes.byteLength);
|
|
377
|
+
const urlTblView = new DataView(urlTblBytes.buffer, urlTblBytes.byteOffset, urlTblBytes.byteLength);
|
|
378
|
+
const count = Math.min(Math.floor(topicsBytes.byteLength / 16), CHM_LIMITS.maxTopics);
|
|
379
|
+
for (let i = 0; i < count; i += 1) {
|
|
380
|
+
const base = i * 16;
|
|
381
|
+
const titleOffset = topicsView.getInt32(base + 4, true);
|
|
382
|
+
const urlTblOffset = topicsView.getUint32(base + 8, true);
|
|
383
|
+
if (urlTblOffset + 12 > urlTblBytes.byteLength) continue;
|
|
384
|
+
const urlStrOffset = urlTblView.getUint32(urlTblOffset + 8, true);
|
|
385
|
+
const rawUrl = readUrlStrAt(urlStrBytes, urlStrOffset, encoding);
|
|
386
|
+
if (!rawUrl) continue;
|
|
387
|
+
const resolved = resolveChmEntry(directory, "/", rawUrl);
|
|
388
|
+
if (!resolved) continue;
|
|
389
|
+
const topicTitle = titleOffset >= 0 ? readStringsAt(stringsBytes, titleOffset, encoding) : "";
|
|
390
|
+
systemTopics.push({ title: topicTitle, url: resolved.entry.path });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
let defaultTopic = "";
|
|
394
|
+
if (systemInfo.defaultTopic) {
|
|
395
|
+
const resolved = resolveChmEntry(directory, "/", systemInfo.defaultTopic);
|
|
396
|
+
if (resolved) defaultTopic = resolved.entry.path;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
title: systemInfo.title,
|
|
400
|
+
compiledName: systemInfo.compiledName,
|
|
401
|
+
encoding,
|
|
402
|
+
defaultTopic,
|
|
403
|
+
hhcPath: hhcResolved?.path ?? "",
|
|
404
|
+
hhkPath: hhkResolved?.path ?? "",
|
|
405
|
+
tocTree,
|
|
406
|
+
indexEntries,
|
|
407
|
+
systemTopics
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ../plugins/chm/src/itsf.ts
|
|
412
|
+
var textDecoder = new TextDecoder("utf-8", { fatal: true });
|
|
413
|
+
function readUint64Safe(view, offset) {
|
|
414
|
+
const low = view.getUint32(offset, true);
|
|
415
|
+
const high = view.getUint32(offset + 4, true);
|
|
416
|
+
if (high > 2097151) {
|
|
417
|
+
throw new ViewerError("invalid-file", "CHM 64-bit offset exceeds safe bounds.");
|
|
418
|
+
}
|
|
419
|
+
return high * 2 ** 32 + low;
|
|
420
|
+
}
|
|
421
|
+
function readEncInt(bytes, state, end) {
|
|
422
|
+
let value = 0;
|
|
423
|
+
let count = 0;
|
|
424
|
+
while (state.pos < end) {
|
|
425
|
+
const byte = bytes[state.pos++];
|
|
426
|
+
count += 1;
|
|
427
|
+
if (count > 8) {
|
|
428
|
+
throw new ViewerError("invalid-file", "CHM encint exceeds 64-bit limit.");
|
|
429
|
+
}
|
|
430
|
+
if (value > 4398046511103) {
|
|
431
|
+
throw new ViewerError("invalid-file", "CHM encint overflows safe integer.");
|
|
432
|
+
}
|
|
433
|
+
value = value * 128 + (byte & 127);
|
|
434
|
+
if ((byte & 128) === 0) return value;
|
|
435
|
+
}
|
|
436
|
+
throw new ViewerError("invalid-file", "Truncated CHM encint.");
|
|
437
|
+
}
|
|
438
|
+
async function readFileSlice(file, start, end, signal) {
|
|
439
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
440
|
+
if (start < 0 || end < start || end > file.size) {
|
|
441
|
+
throw new ViewerError("invalid-file", "CHM slice offset out of range.");
|
|
442
|
+
}
|
|
443
|
+
const buffer = await file.slice(start, end).arrayBuffer();
|
|
444
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
445
|
+
return new Uint8Array(buffer);
|
|
446
|
+
}
|
|
447
|
+
function ascii(bytes, offset, length) {
|
|
448
|
+
let out = "";
|
|
449
|
+
for (let i = 0; i < length; i += 1) out += String.fromCharCode(bytes[offset + i]);
|
|
450
|
+
return out;
|
|
451
|
+
}
|
|
452
|
+
function parsePmglChunk(chunk, entries, byExactPath, byLowerPath) {
|
|
453
|
+
if (ascii(chunk, 0, 4) !== "PMGL") {
|
|
454
|
+
throw new ViewerError("invalid-file", "Expected PMGL directory chunk.");
|
|
455
|
+
}
|
|
456
|
+
const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
457
|
+
const freeSpace = view.getUint32(4, true);
|
|
458
|
+
const nextChunk = view.getInt32(16, true);
|
|
459
|
+
if (freeSpace < 2 || freeSpace > chunk.byteLength - 20) {
|
|
460
|
+
throw new ViewerError("invalid-file", "Invalid PMGL free space.");
|
|
461
|
+
}
|
|
462
|
+
const entriesEnd = chunk.byteLength - freeSpace;
|
|
463
|
+
const state = { pos: 20 };
|
|
464
|
+
while (state.pos < entriesEnd) {
|
|
465
|
+
const nameLen = readEncInt(chunk, state, entriesEnd);
|
|
466
|
+
if (nameLen === 0 || state.pos + nameLen > entriesEnd) {
|
|
467
|
+
throw new ViewerError("invalid-file", "Invalid CHM entry name length.");
|
|
468
|
+
}
|
|
469
|
+
const nameBytes = chunk.subarray(state.pos, state.pos + nameLen);
|
|
470
|
+
state.pos += nameLen;
|
|
471
|
+
let path;
|
|
472
|
+
try {
|
|
473
|
+
path = textDecoder.decode(nameBytes);
|
|
474
|
+
} catch {
|
|
475
|
+
throw new ViewerError("invalid-file", "Invalid UTF-8 in CHM entry path.");
|
|
476
|
+
}
|
|
477
|
+
if (!path || path.includes("\0")) {
|
|
478
|
+
throw new ViewerError("invalid-file", "Invalid CHM entry path.");
|
|
479
|
+
}
|
|
480
|
+
const section = readEncInt(chunk, state, entriesEnd);
|
|
481
|
+
if (section !== 0 && section !== 1) {
|
|
482
|
+
throw new ViewerError("invalid-file", "Unsupported CHM section number.");
|
|
483
|
+
}
|
|
484
|
+
const offset = readEncInt(chunk, state, entriesEnd);
|
|
485
|
+
const length = readEncInt(chunk, state, entriesEnd);
|
|
486
|
+
if (entries.length >= CHM_LIMITS.maxEntries) {
|
|
487
|
+
throw new ViewerError("resource-limit", "CHM entry count limit exceeded.");
|
|
488
|
+
}
|
|
489
|
+
const entry = {
|
|
490
|
+
path,
|
|
491
|
+
lowerPath: path.toLowerCase(),
|
|
492
|
+
section,
|
|
493
|
+
offset,
|
|
494
|
+
length
|
|
495
|
+
};
|
|
496
|
+
entries.push(entry);
|
|
497
|
+
if (!byExactPath.has(entry.path)) byExactPath.set(entry.path, entry);
|
|
498
|
+
if (!byLowerPath.has(entry.lowerPath)) byLowerPath.set(entry.lowerPath, entry);
|
|
499
|
+
}
|
|
500
|
+
return nextChunk;
|
|
501
|
+
}
|
|
502
|
+
async function parseChmDirectory(file, signal) {
|
|
503
|
+
if (file.size > CHM_LIMITS.maxFileBytes) {
|
|
504
|
+
throw new ViewerError("resource-limit", "CHM file exceeds 128 MiB limit.");
|
|
505
|
+
}
|
|
506
|
+
if (file.size < 88 + 84) {
|
|
507
|
+
throw new ViewerError("invalid-file", "File is too small to be a CHM archive.");
|
|
508
|
+
}
|
|
509
|
+
const header = await readFileSlice(file, 0, Math.min(file.size, 96), signal);
|
|
510
|
+
if (ascii(header, 0, 4) !== "ITSF") {
|
|
511
|
+
throw new ViewerError("invalid-file", "Missing ITSF signature.");
|
|
512
|
+
}
|
|
513
|
+
const hView = new DataView(header.buffer, header.byteOffset, header.byteLength);
|
|
514
|
+
const version = hView.getUint32(4, true);
|
|
515
|
+
const headerLength = hView.getUint32(8, true);
|
|
516
|
+
if (version === 2) {
|
|
517
|
+
if (headerLength < 88 || headerLength > CHM_LIMITS.maxHeaderBytes) {
|
|
518
|
+
throw new ViewerError("invalid-file", "Invalid ITSF v2 header length.");
|
|
519
|
+
}
|
|
520
|
+
} else if (version === 3) {
|
|
521
|
+
if (headerLength < 96 || headerLength > CHM_LIMITS.maxHeaderBytes || header.byteLength < 96) {
|
|
522
|
+
throw new ViewerError("invalid-file", "Invalid ITSF v3 header length.");
|
|
523
|
+
}
|
|
524
|
+
} else {
|
|
525
|
+
throw new ViewerError("invalid-file", "Unsupported ITSF version.");
|
|
526
|
+
}
|
|
527
|
+
const headerLcid = hView.getUint32(20, true);
|
|
528
|
+
readUint64Safe(hView, 56);
|
|
529
|
+
readUint64Safe(hView, 64);
|
|
530
|
+
const dirOffset = readUint64Safe(hView, 72);
|
|
531
|
+
const dirLength = readUint64Safe(hView, 80);
|
|
532
|
+
if (dirOffset < headerLength || dirLength < 84 || dirOffset + dirLength > file.size) {
|
|
533
|
+
throw new ViewerError("invalid-file", "CHM directory bounds out of range.");
|
|
534
|
+
}
|
|
535
|
+
const contentOffset = version >= 3 ? readUint64Safe(hView, 88) : dirOffset + dirLength;
|
|
536
|
+
if (contentOffset < dirOffset + dirLength || contentOffset > file.size) {
|
|
537
|
+
throw new ViewerError("invalid-file", "Invalid CHM content offset.");
|
|
538
|
+
}
|
|
539
|
+
const itsp = await readFileSlice(file, dirOffset, dirOffset + 84, signal);
|
|
540
|
+
if (ascii(itsp, 0, 4) !== "ITSP") {
|
|
541
|
+
throw new ViewerError("invalid-file", "Missing ITSP directory header.");
|
|
542
|
+
}
|
|
543
|
+
const dView = new DataView(itsp.buffer, itsp.byteOffset, itsp.byteLength);
|
|
544
|
+
const itspVersion = dView.getUint32(4, true);
|
|
545
|
+
const itspLen = dView.getUint32(8, true);
|
|
546
|
+
const chunkSize = dView.getUint32(16, true);
|
|
547
|
+
const firstPmgl = dView.getInt32(32, true);
|
|
548
|
+
const lastPmgl = dView.getInt32(36, true);
|
|
549
|
+
const numChunks = dView.getUint32(44, true);
|
|
550
|
+
const dirLcid = dView.getUint32(48, true);
|
|
551
|
+
if (itspVersion !== 1 || itspLen < 84 || itspLen > CHM_LIMITS.maxHeaderBytes) {
|
|
552
|
+
throw new ViewerError("invalid-file", "Invalid ITSP header.");
|
|
553
|
+
}
|
|
554
|
+
if (chunkSize < 64 || chunkSize > 65536) {
|
|
555
|
+
throw new ViewerError("invalid-file", "Unsupported CHM directory chunk size.");
|
|
556
|
+
}
|
|
557
|
+
if (numChunks > CHM_LIMITS.maxDirChunks) {
|
|
558
|
+
throw new ViewerError("resource-limit", "CHM directory chunk count exceeds limit.");
|
|
559
|
+
}
|
|
560
|
+
const chunksStart = dirOffset + itspLen;
|
|
561
|
+
if (chunksStart + numChunks * chunkSize > dirOffset + dirLength) {
|
|
562
|
+
throw new ViewerError("invalid-file", "CHM directory chunks exceed directory bounds.");
|
|
563
|
+
}
|
|
564
|
+
const entries = [];
|
|
565
|
+
const byExactPath = /* @__PURE__ */ new Map();
|
|
566
|
+
const byLowerPath = /* @__PURE__ */ new Map();
|
|
567
|
+
if (numChunks > 0 && firstPmgl >= 0) {
|
|
568
|
+
if (firstPmgl >= numChunks || lastPmgl >= 0 && lastPmgl >= numChunks) {
|
|
569
|
+
throw new ViewerError("invalid-file", "CHM PMGL chunk index out of range.");
|
|
570
|
+
}
|
|
571
|
+
const visited = /* @__PURE__ */ new Set();
|
|
572
|
+
let current = firstPmgl;
|
|
573
|
+
while (current >= 0) {
|
|
574
|
+
if (current >= numChunks || visited.has(current)) {
|
|
575
|
+
throw new ViewerError("invalid-file", "CHM directory chunk loop or invalid link.");
|
|
576
|
+
}
|
|
577
|
+
visited.add(current);
|
|
578
|
+
const offset = chunksStart + current * chunkSize;
|
|
579
|
+
const chunk = await readFileSlice(file, offset, offset + chunkSize, signal);
|
|
580
|
+
const next = parsePmglChunk(chunk, entries, byExactPath, byLowerPath);
|
|
581
|
+
if (current === lastPmgl) {
|
|
582
|
+
if (next >= 0) {
|
|
583
|
+
throw new ViewerError("invalid-file", "CHM directory chunk loop or trailing link.");
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
current = next;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
version,
|
|
592
|
+
lcid: dirLcid || headerLcid,
|
|
593
|
+
contentOffset,
|
|
594
|
+
entries,
|
|
595
|
+
byExactPath,
|
|
596
|
+
byLowerPath
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ../rendering-publication/src/safe-content.ts
|
|
601
|
+
var TAGS = new Set(
|
|
602
|
+
"html head body title p div span section article aside header footer main nav h1 h2 h3 h4 h5 h6 ol ul li dl dt dd blockquote pre code em strong b i u s small sub sup br hr a img figure figcaption table caption colgroup col thead tbody tfoot tr th td ruby rt rp bdi bdo".split(
|
|
603
|
+
" "
|
|
604
|
+
)
|
|
605
|
+
);
|
|
606
|
+
var ATTRS = new Set("id class title lang dir alt width height colspan rowspan scope".split(" "));
|
|
607
|
+
var CSS_PROPERTIES = /^(?:color|background-color|font(?:-family|-size|-style|-weight|-variant)?|line-height|text-align|text-indent|text-decoration|white-space|word-break|overflow-wrap|vertical-align|margin(?:-top|-bottom|-left|-right)?|padding(?:-top|-bottom|-left|-right)?|border(?:-width|-style|-color|-collapse|-spacing)?|list-style-type|display|width|max-width|height|max-height|direction|writing-mode|ruby-position)$/;
|
|
608
|
+
var FONT_TYPES = /* @__PURE__ */ new Set([
|
|
609
|
+
"font/woff",
|
|
610
|
+
"font/woff2",
|
|
611
|
+
"font/ttf",
|
|
612
|
+
"font/otf",
|
|
613
|
+
"application/font-woff",
|
|
614
|
+
"application/vnd.ms-opentype",
|
|
615
|
+
"application/x-font-ttf",
|
|
616
|
+
"application/x-font-opentype"
|
|
617
|
+
]);
|
|
618
|
+
async function prepareChapter(zip, book, path, signal) {
|
|
619
|
+
const urls = /* @__PURE__ */ new Map();
|
|
620
|
+
const links = [];
|
|
621
|
+
let resourceBytes = 0, fonts = 0, pixels = 0, cssBytes = 0, missingResources = 0;
|
|
622
|
+
const dispose = () => {
|
|
623
|
+
for (const url of urls.values()) URL.revokeObjectURL(url);
|
|
624
|
+
urls.clear();
|
|
625
|
+
};
|
|
626
|
+
async function resource(base, value, font = false) {
|
|
627
|
+
const ref = localReference(base, value);
|
|
628
|
+
if (!ref) return null;
|
|
629
|
+
if (urls.has(ref.path)) return urls.get(ref.path);
|
|
630
|
+
const item = book.items.get(ref.path);
|
|
631
|
+
if (!item || !(font ? FONT_TYPES.has(item.type) : /^image\/(?:jpeg|png|gif|webp|avif|svg\+xml|bmp|x-icon)$/.test(item.type)))
|
|
632
|
+
return null;
|
|
633
|
+
if (!zip.entries.has(ref.path)) {
|
|
634
|
+
missingResources++;
|
|
635
|
+
return null;
|
|
636
|
+
}
|
|
637
|
+
if (urls.size >= MARKUP_LIMITS.resources || font && ++fonts > MARKUP_LIMITS.fonts)
|
|
638
|
+
throw new ViewerError("resource-limit", "Chapter resource count exceeded.");
|
|
639
|
+
const bytes = await zip.read(ref.path, font ? MARKUP_LIMITS.font : 16 * 1024 * 1024, signal);
|
|
640
|
+
resourceBytes += bytes.length;
|
|
641
|
+
if (resourceBytes > MARKUP_LIMITS.resourceBytes)
|
|
642
|
+
throw new ViewerError("resource-limit", "Chapter resource bytes exceeded.");
|
|
643
|
+
let blob;
|
|
644
|
+
if (item.type === "image/svg+xml") {
|
|
645
|
+
const svg = parseXml(bytes);
|
|
646
|
+
const root = svg.documentElement;
|
|
647
|
+
const viewBox = root.getAttribute("viewBox")?.trim().split(/[\s,]+/).map(Number);
|
|
648
|
+
if (viewBox && (viewBox.length !== 4 || !viewBox.every(Number.isFinite) || viewBox[2] <= 0 || viewBox[3] <= 0))
|
|
649
|
+
throw new ViewerError("invalid-file", "Invalid SVG viewBox.");
|
|
650
|
+
const dimension = (name, fallback) => {
|
|
651
|
+
const value2 = root.getAttribute(name)?.trim();
|
|
652
|
+
if (value2 && /^(?:\d+(?:\.\d*)?|\.\d+)%$/.test(value2) && parseFloat(value2) > 0) return fallback;
|
|
653
|
+
return Number(value2 ?? fallback);
|
|
654
|
+
};
|
|
655
|
+
const svgWidth = dimension("width", viewBox?.[2] ?? 300), svgHeight = dimension("height", viewBox?.[3] ?? 150);
|
|
656
|
+
if (!Number.isFinite(svgWidth * svgHeight) || svgWidth <= 0 || svgHeight <= 0)
|
|
657
|
+
throw new ViewerError("invalid-file", "Invalid SVG dimensions.");
|
|
658
|
+
pixels += svgWidth * svgHeight;
|
|
659
|
+
if (pixels > 16e6)
|
|
660
|
+
throw new ViewerError("resource-limit", "Chapter image pixels exceeded.");
|
|
661
|
+
const allowed = new Set(
|
|
662
|
+
"svg g path rect circle ellipse line polyline polygon text tspan defs linearGradient radialGradient stop clipPath title desc".split(
|
|
663
|
+
" "
|
|
664
|
+
)
|
|
665
|
+
);
|
|
666
|
+
for (const node of [
|
|
667
|
+
svg.documentElement,
|
|
668
|
+
...Array.from(svg.documentElement.querySelectorAll("*"))
|
|
669
|
+
]) {
|
|
670
|
+
if (!allowed.has(node.localName)) {
|
|
671
|
+
node.remove();
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
for (const attr of Array.from(node.attributes))
|
|
675
|
+
if (!/^(?:xmlns|viewBox|width|height|x|y|x1|x2|y1|y2|cx|cy|r|rx|ry|d|points|fill|stroke|stroke-width|transform|offset|stop-color|font-size|text-anchor)$/.test(
|
|
676
|
+
attr.name
|
|
677
|
+
) || /url\s*\(|[\\<>]/i.test(attr.value))
|
|
678
|
+
node.removeAttributeNode(attr);
|
|
679
|
+
}
|
|
680
|
+
if (svg.documentElement.localName !== "svg")
|
|
681
|
+
throw new ViewerError("invalid-file", "Invalid SVG image.");
|
|
682
|
+
root.setAttribute("width", String(svgWidth));
|
|
683
|
+
root.setAttribute("height", String(svgHeight));
|
|
684
|
+
blob = new Blob([new XMLSerializer().serializeToString(svg)], { type: item.type });
|
|
685
|
+
} else {
|
|
686
|
+
if (!font) {
|
|
687
|
+
const info = inspectImageFile(bytes);
|
|
688
|
+
if (!info?.width || !info.height)
|
|
689
|
+
throw new ViewerError("invalid-file", "Invalid chapter image.");
|
|
690
|
+
if (info.animated && !info.frameCount)
|
|
691
|
+
throw new ViewerError("resource-limit", "Unbounded animation frames.");
|
|
692
|
+
pixels += info.width * info.height * (info.frameCount ?? 1);
|
|
693
|
+
if (pixels > 16e6)
|
|
694
|
+
throw new ViewerError("resource-limit", "Chapter image pixels exceeded.");
|
|
695
|
+
}
|
|
696
|
+
blob = new Blob([bytes.slice().buffer], { type: item.type });
|
|
697
|
+
}
|
|
698
|
+
checkBookAbort(signal);
|
|
699
|
+
const url = URL.createObjectURL(blob);
|
|
700
|
+
urls.set(ref.path, url);
|
|
701
|
+
return url;
|
|
702
|
+
}
|
|
703
|
+
async function css(source, base) {
|
|
704
|
+
cssBytes += source.length;
|
|
705
|
+
if (cssBytes > 1024 * 1024 || source.length > 256 * 1024)
|
|
706
|
+
throw new ViewerError("resource-limit", "CSS limit exceeded.");
|
|
707
|
+
const sheet = new CSSStyleSheet();
|
|
708
|
+
sheet.replaceSync(source);
|
|
709
|
+
if (sheet.cssRules.length > 2e3)
|
|
710
|
+
throw new ViewerError("resource-limit", "CSS rules exceeded.");
|
|
711
|
+
let result = "";
|
|
712
|
+
for (const rule of Array.from(sheet.cssRules)) {
|
|
713
|
+
if (rule.type === 5) {
|
|
714
|
+
const style = rule.style;
|
|
715
|
+
const family = style.getPropertyValue("font-family");
|
|
716
|
+
const source2 = style.getPropertyValue("src");
|
|
717
|
+
const match = /^url\(\s*["']?([^"')]+)["']?\s*\)(?:\s+format\(["'][\w-]+["']\))?$/.exec(
|
|
718
|
+
source2
|
|
719
|
+
);
|
|
720
|
+
if (match && /^[\w\s"'-]+$/.test(family)) {
|
|
721
|
+
const url = await resource(base, match[1], true);
|
|
722
|
+
if (url) result += `@font-face{font-family:${family};src:url("${url}")}`;
|
|
723
|
+
}
|
|
724
|
+
} else if (rule.type === 1) {
|
|
725
|
+
const styleRule = rule;
|
|
726
|
+
if (!/^[\w\s.#,:>+~*()[\]="'|-]+$/.test(styleRule.selectorText)) continue;
|
|
727
|
+
const declarations = [];
|
|
728
|
+
for (let i = 0; i < styleRule.style.length; i++) {
|
|
729
|
+
const property = styleRule.style.item(i), value = styleRule.style.getPropertyValue(property);
|
|
730
|
+
if (CSS_PROPERTIES.test(property) && !/[\\<>@]|url\s*\(|expression|var\s*\(|image-set/i.test(value))
|
|
731
|
+
declarations.push(`${property}:${value}`);
|
|
732
|
+
}
|
|
733
|
+
result += `${styleRule.selectorText}{${declarations.join(";")}}`;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return result;
|
|
737
|
+
}
|
|
738
|
+
try {
|
|
739
|
+
const bytes = await zip.read(path, MARKUP_LIMITS.chapter, signal);
|
|
740
|
+
const isPlain = book.items.get(path)?.type === "text/plain";
|
|
741
|
+
const isHtml = isPlain || book.items.get(path)?.type === "text/html";
|
|
742
|
+
const template = isHtml ? document.createElement("template") : void 0;
|
|
743
|
+
if (template) {
|
|
744
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
745
|
+
if (isPlain) {
|
|
746
|
+
const pre = document.createElement("pre");
|
|
747
|
+
pre.textContent = text;
|
|
748
|
+
template.content.append(pre);
|
|
749
|
+
} else template.innerHTML = text.replace(/<\/?mbp:pagebreak\b[^>]*>/gi, "<hr>");
|
|
750
|
+
}
|
|
751
|
+
const source = template?.content ?? parseXml(bytes);
|
|
752
|
+
const stack = [[source, 0]];
|
|
753
|
+
let nodes = 0;
|
|
754
|
+
while (stack.length) {
|
|
755
|
+
const [node, depth] = stack.pop();
|
|
756
|
+
if (++nodes > MARKUP_LIMITS.nodes || depth > MARKUP_LIMITS.depth) throw new ViewerError("resource-limit", "Chapter structure limit exceeded.");
|
|
757
|
+
for (const child of Array.from(node.childNodes)) stack.push([child, depth + 1]);
|
|
758
|
+
}
|
|
759
|
+
const target = document.implementation.createHTMLDocument("");
|
|
760
|
+
let styles = "";
|
|
761
|
+
for (const style2 of elements(source, "style"))
|
|
762
|
+
styles += await css(style2.textContent ?? "", path);
|
|
763
|
+
for (const link of elements(source, "link")) {
|
|
764
|
+
if (link.getAttribute("rel") !== "stylesheet") continue;
|
|
765
|
+
const ref = localReference(path, link.getAttribute("href") ?? "");
|
|
766
|
+
if (ref && book.items.get(ref.path)?.type === "text/css") {
|
|
767
|
+
if (zip.entries.has(ref.path))
|
|
768
|
+
styles += await css(
|
|
769
|
+
new TextDecoder().decode(await zip.read(ref.path, 256 * 1024, signal)),
|
|
770
|
+
ref.path
|
|
771
|
+
);
|
|
772
|
+
else missingResources++;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
async function copy(node, parent) {
|
|
776
|
+
checkBookAbort(signal);
|
|
777
|
+
if (node.nodeType === 3) {
|
|
778
|
+
parent.appendChild(target.createTextNode(node.textContent ?? ""));
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
if (node.nodeType !== 1) return;
|
|
782
|
+
const element = node, rawTag = element.localName.toLowerCase(), tag = rawTag === "font" ? "span" : rawTag === "center" ? "div" : rawTag;
|
|
783
|
+
if (!TAGS.has(tag)) return;
|
|
784
|
+
const result = target.createElement(tag);
|
|
785
|
+
for (const attr of Array.from(element.attributes))
|
|
786
|
+
if (ATTRS.has(attr.name)) result.setAttribute(attr.name, attr.value);
|
|
787
|
+
if (element.hasAttribute("style"))
|
|
788
|
+
result.setAttribute(
|
|
789
|
+
"style",
|
|
790
|
+
(await css(`p{${element.getAttribute("style")}}`, path)).replace(/^p\{|\}$/g, "")
|
|
791
|
+
);
|
|
792
|
+
if (tag === "a" && element.hasAttribute("href")) {
|
|
793
|
+
const ref = localReference(path, element.getAttribute("href") ?? "");
|
|
794
|
+
if (ref && book.spine.some((item) => item.path === ref.path)) {
|
|
795
|
+
result.setAttribute("href", "#");
|
|
796
|
+
result.dataset.bookLink = String(links.length);
|
|
797
|
+
links.push(ref);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
if (tag === "img") {
|
|
801
|
+
const url = await resource(path, element.getAttribute("src") ?? "");
|
|
802
|
+
if (url) result.setAttribute("src", url);
|
|
803
|
+
}
|
|
804
|
+
for (const child of Array.from(node.childNodes)) await copy(child, result);
|
|
805
|
+
parent.appendChild(result);
|
|
806
|
+
}
|
|
807
|
+
const body = elements(source, "body")[0] ?? (isHtml ? source : void 0);
|
|
808
|
+
if (!body) throw new ViewerError("invalid-file", "Missing chapter body.");
|
|
809
|
+
target.body.dir = ("getAttribute" in body ? body.getAttribute("dir") : null) === "rtl" ? "rtl" : book.direction;
|
|
810
|
+
for (const child of Array.from(body.childNodes)) await copy(child, target.body);
|
|
811
|
+
const csp = target.createElement("meta");
|
|
812
|
+
csp.httpEquiv = "Content-Security-Policy";
|
|
813
|
+
csp.content = CONTENT_CSP;
|
|
814
|
+
target.head.append(csp);
|
|
815
|
+
const style = target.createElement("style");
|
|
816
|
+
style.textContent = styles;
|
|
817
|
+
target.head.append(style);
|
|
818
|
+
const userStyle = target.createElement("style");
|
|
819
|
+
userStyle.id = "book-reader-style";
|
|
820
|
+
target.head.append(userStyle);
|
|
821
|
+
return {
|
|
822
|
+
html: "<!doctype html>" + target.documentElement.outerHTML,
|
|
823
|
+
links,
|
|
824
|
+
missingResources,
|
|
825
|
+
dispose
|
|
826
|
+
};
|
|
827
|
+
} catch (error) {
|
|
828
|
+
dispose();
|
|
829
|
+
throw error;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// ../plugins/chm/src/ui.ts
|
|
834
|
+
function chmCopy(locale) {
|
|
835
|
+
return {
|
|
836
|
+
...publicationCopy(locale),
|
|
837
|
+
...selectMessages(locale, {
|
|
838
|
+
en: {
|
|
839
|
+
invalid: "The CHM help file is damaged or uses an unsupported layout.",
|
|
840
|
+
limit: "This CHM help file exceeds the safe reading resource limits.",
|
|
841
|
+
noTopics: "No readable HTML topics were found in this CHM archive.",
|
|
842
|
+
unsupportedObject: "Unsupported embedded object"
|
|
843
|
+
},
|
|
844
|
+
"zh-CN": {
|
|
845
|
+
invalid: "CHM \u5E2E\u52A9\u6587\u4EF6\u5DF2\u635F\u574F\u6216\u4F7F\u7528\u4E86\u4E0D\u652F\u6301\u7684\u683C\u5F0F\u7ED3\u6784\u3002",
|
|
846
|
+
limit: "\u6B64 CHM \u5E2E\u52A9\u6587\u4EF6\u8D85\u8FC7\u5B89\u5168\u9605\u8BFB\u8D44\u6E90\u4E0A\u9650\u3002",
|
|
847
|
+
noTopics: "\u5728\u6B64 CHM \u5F52\u6863\u4E2D\u672A\u627E\u5230\u53EF\u9605\u8BFB\u7684 HTML \u4E3B\u9898\u3002",
|
|
848
|
+
unsupportedObject: "\u4E0D\u652F\u6301\u7684\u5D4C\u5165\u5BF9\u8C61"
|
|
849
|
+
}
|
|
850
|
+
})
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// ../plugins/chm/src/publication.ts
|
|
855
|
+
var MIME_BY_EXT = {
|
|
856
|
+
".html": "text/html",
|
|
857
|
+
".htm": "text/html",
|
|
858
|
+
".xhtml": "text/html",
|
|
859
|
+
".css": "text/css",
|
|
860
|
+
".jpg": "image/jpeg",
|
|
861
|
+
".jpeg": "image/jpeg",
|
|
862
|
+
".png": "image/png",
|
|
863
|
+
".gif": "image/gif",
|
|
864
|
+
".webp": "image/webp",
|
|
865
|
+
".avif": "image/avif",
|
|
866
|
+
".svg": "image/svg+xml",
|
|
867
|
+
".bmp": "image/bmp",
|
|
868
|
+
".ico": "image/x-icon",
|
|
869
|
+
".woff": "font/woff",
|
|
870
|
+
".woff2": "font/woff2",
|
|
871
|
+
".ttf": "font/ttf",
|
|
872
|
+
".otf": "font/otf"
|
|
873
|
+
};
|
|
874
|
+
function chmMimeType(lowerPath) {
|
|
875
|
+
const dot = lowerPath.lastIndexOf(".");
|
|
876
|
+
if (dot < 0) return "application/octet-stream";
|
|
877
|
+
return MIME_BY_EXT[lowerPath.slice(dot)] ?? "application/octet-stream";
|
|
878
|
+
}
|
|
879
|
+
function isHtmlEntry(entry) {
|
|
880
|
+
if (entry.path.startsWith("::") || entry.path.startsWith("/#") || entry.path.startsWith("/$")) {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
return entry.lowerPath.endsWith(".html") || entry.lowerPath.endsWith(".htm") || entry.lowerPath.endsWith(".xhtml");
|
|
884
|
+
}
|
|
885
|
+
function chmKey(path) {
|
|
886
|
+
return path.replace(/^\/+/, "");
|
|
887
|
+
}
|
|
888
|
+
function relativeEncodedPath(fromKey, toKey) {
|
|
889
|
+
const fromParts = fromKey.split("/").slice(0, -1);
|
|
890
|
+
const toParts = toKey.split("/");
|
|
891
|
+
let common = 0;
|
|
892
|
+
while (common < fromParts.length && common < toParts.length - 1 && fromParts[common] === toParts[common]) {
|
|
893
|
+
common += 1;
|
|
894
|
+
}
|
|
895
|
+
const ups = Array.from({ length: fromParts.length - common }, () => "..");
|
|
896
|
+
const rest = toParts.slice(common);
|
|
897
|
+
return [...ups, ...rest].map((part) => part === ".." ? part : encodeURIComponent(part)).join("/");
|
|
898
|
+
}
|
|
899
|
+
function normalizeChmHtml(htmlText, entryPath, directory, selfChmNames, unsupportedObjectLabel, items) {
|
|
900
|
+
const template = document.createElement("template");
|
|
901
|
+
template.innerHTML = htmlText;
|
|
902
|
+
const fromKey = chmKey(entryPath);
|
|
903
|
+
for (const el of Array.from(template.content.querySelectorAll("object, embed, applet"))) {
|
|
904
|
+
const notice = template.content.ownerDocument.createElement("span");
|
|
905
|
+
notice.textContent = `[${unsupportedObjectLabel}]`;
|
|
906
|
+
el.replaceWith(notice);
|
|
907
|
+
}
|
|
908
|
+
for (const a of Array.from(template.content.querySelectorAll("a[href]"))) {
|
|
909
|
+
const raw = a.getAttribute("href") ?? "";
|
|
910
|
+
const resolved = resolveChmEntry(directory, entryPath, raw, selfChmNames);
|
|
911
|
+
if (resolved && isHtmlEntry(resolved.entry)) {
|
|
912
|
+
const rel = relativeEncodedPath(fromKey, chmKey(resolved.entry.path));
|
|
913
|
+
const frag = resolved.fragment ? `#${encodeURIComponent(resolved.fragment)}` : "";
|
|
914
|
+
a.setAttribute("href", `${rel}${frag}`);
|
|
915
|
+
} else {
|
|
916
|
+
a.removeAttribute("href");
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
for (const img of Array.from(template.content.querySelectorAll("img[src]"))) {
|
|
920
|
+
const raw = img.getAttribute("src") ?? "";
|
|
921
|
+
if (/^(?:https?:|ftp:|javascript:|vbscript:)/i.test(raw.trim())) {
|
|
922
|
+
img.removeAttribute("src");
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
const resolved = resolveChmEntry(directory, entryPath, raw, selfChmNames);
|
|
926
|
+
if (resolved) {
|
|
927
|
+
img.setAttribute("src", relativeEncodedPath(fromKey, chmKey(resolved.entry.path)));
|
|
928
|
+
} else {
|
|
929
|
+
const cleanLocal = raw.trim();
|
|
930
|
+
if (cleanLocal && !/^[a-z][\w+.-]*:/i.test(cleanLocal)) {
|
|
931
|
+
const baseDir = entryPath.includes("/") ? entryPath.slice(0, entryPath.lastIndexOf("/") + 1) : "/";
|
|
932
|
+
const missingKey = chmKey(normalizeChmPath(cleanLocal.startsWith("/") ? cleanLocal : baseDir + cleanLocal));
|
|
933
|
+
const mime = chmMimeType(missingKey.toLowerCase());
|
|
934
|
+
if (mime.startsWith("image/") && !items.has(missingKey)) {
|
|
935
|
+
items.set(missingKey, { id: missingKey, path: missingKey, type: mime, properties: [] });
|
|
936
|
+
}
|
|
937
|
+
img.setAttribute("src", relativeEncodedPath(fromKey, missingKey));
|
|
938
|
+
} else {
|
|
939
|
+
img.removeAttribute("src");
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
for (const link of Array.from(template.content.querySelectorAll("link[href]"))) {
|
|
944
|
+
if ((link.getAttribute("rel") ?? "").toLowerCase() !== "stylesheet") {
|
|
945
|
+
link.removeAttribute("href");
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
const raw = link.getAttribute("href") ?? "";
|
|
949
|
+
const resolved = resolveChmEntry(directory, entryPath, raw, selfChmNames);
|
|
950
|
+
if (resolved) {
|
|
951
|
+
link.setAttribute("href", relativeEncodedPath(fromKey, chmKey(resolved.entry.path)));
|
|
952
|
+
} else {
|
|
953
|
+
link.removeAttribute("href");
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return new TextEncoder().encode(template.innerHTML);
|
|
957
|
+
}
|
|
958
|
+
function createChmPublication(fileName, directory, metadata, reader, locale) {
|
|
959
|
+
const copy = chmCopy(locale);
|
|
960
|
+
const selfChmNames = /* @__PURE__ */ new Set();
|
|
961
|
+
const baseFileName = fileName.replace(/\\/g, "/").split("/").pop()?.toLowerCase();
|
|
962
|
+
if (baseFileName) selfChmNames.add(baseFileName);
|
|
963
|
+
if (metadata.compiledName) {
|
|
964
|
+
const compiled = metadata.compiledName.replace(/\\/g, "/").split("/").pop()?.toLowerCase();
|
|
965
|
+
if (compiled) {
|
|
966
|
+
selfChmNames.add(compiled);
|
|
967
|
+
if (!compiled.endsWith(".chm")) selfChmNames.add(`${compiled}.chm`);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
const entriesByKey = /* @__PURE__ */ new Map();
|
|
971
|
+
const bookEntries = /* @__PURE__ */ new Map();
|
|
972
|
+
const items = /* @__PURE__ */ new Map();
|
|
973
|
+
for (const entry of directory.entries) {
|
|
974
|
+
if (entry.path.startsWith("::") || entry.path.endsWith("/")) continue;
|
|
975
|
+
const key = chmKey(entry.path);
|
|
976
|
+
if (!key || entriesByKey.has(key)) continue;
|
|
977
|
+
entriesByKey.set(key, entry);
|
|
978
|
+
bookEntries.set(key, {
|
|
979
|
+
filename: key,
|
|
980
|
+
directory: false,
|
|
981
|
+
uncompressedSize: entry.length
|
|
982
|
+
});
|
|
983
|
+
items.set(key, {
|
|
984
|
+
id: key,
|
|
985
|
+
path: key,
|
|
986
|
+
type: chmMimeType(entry.lowerPath),
|
|
987
|
+
properties: []
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
const spineKeys = [];
|
|
991
|
+
const seenSpine = /* @__PURE__ */ new Set();
|
|
992
|
+
const pushTopicPath = (rawPath) => {
|
|
993
|
+
if (!rawPath || spineKeys.length >= CHM_LIMITS.maxTopics) return;
|
|
994
|
+
const key = chmKey(rawPath);
|
|
995
|
+
const entry = entriesByKey.get(key);
|
|
996
|
+
if (!entry || !isHtmlEntry(entry) || seenSpine.has(key)) return;
|
|
997
|
+
seenSpine.add(key);
|
|
998
|
+
spineKeys.push(key);
|
|
999
|
+
};
|
|
1000
|
+
if (metadata.defaultTopic) pushTopicPath(metadata.defaultTopic);
|
|
1001
|
+
const walkTocPaths = (nodes) => {
|
|
1002
|
+
for (const node of nodes) {
|
|
1003
|
+
if (node.path) pushTopicPath(node.path);
|
|
1004
|
+
if (node.children.length > 0) walkTocPaths(node.children);
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
walkTocPaths(metadata.tocTree);
|
|
1008
|
+
for (const idx of metadata.indexEntries) pushTopicPath(idx.path);
|
|
1009
|
+
for (const topic of metadata.systemTopics) pushTopicPath(topic.url);
|
|
1010
|
+
for (const entry of directory.entries) {
|
|
1011
|
+
if (isHtmlEntry(entry)) pushTopicPath(entry.path);
|
|
1012
|
+
}
|
|
1013
|
+
if (spineKeys.length === 0) {
|
|
1014
|
+
throw new ViewerError("invalid-file", copy.noTopics);
|
|
1015
|
+
}
|
|
1016
|
+
const spine = spineKeys.map((key) => items.get(key));
|
|
1017
|
+
const toc = [];
|
|
1018
|
+
const flattenToc = (nodes, depth) => {
|
|
1019
|
+
for (const node of nodes) {
|
|
1020
|
+
const key = node.path ? chmKey(node.path) : "";
|
|
1021
|
+
if (key && seenSpine.has(key)) {
|
|
1022
|
+
const prefix = depth > 0 ? `${"\u2014 ".repeat(Math.min(depth, 4))}` : "";
|
|
1023
|
+
toc.push({ label: `${prefix}${node.label}`, path: key, fragment: node.fragment });
|
|
1024
|
+
}
|
|
1025
|
+
if (node.children.length > 0) flattenToc(node.children, depth + 1);
|
|
1026
|
+
}
|
|
1027
|
+
};
|
|
1028
|
+
flattenToc(metadata.tocTree, 0);
|
|
1029
|
+
if (toc.length === 0) {
|
|
1030
|
+
const titleByKey = /* @__PURE__ */ new Map();
|
|
1031
|
+
for (const item of metadata.systemTopics) {
|
|
1032
|
+
if (item.title) titleByKey.set(chmKey(item.url), item.title);
|
|
1033
|
+
}
|
|
1034
|
+
for (const key of spineKeys) {
|
|
1035
|
+
toc.push({
|
|
1036
|
+
label: titleByKey.get(key) || key.split("/").pop() || key,
|
|
1037
|
+
path: key,
|
|
1038
|
+
fragment: ""
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
const mapTree = (nodes) => nodes.map((node) => {
|
|
1043
|
+
const key = node.path ? chmKey(node.path) : "";
|
|
1044
|
+
const validPath = key && seenSpine.has(key) ? key : "";
|
|
1045
|
+
const children = mapTree(node.children);
|
|
1046
|
+
if (!validPath && children.length === 0) return null;
|
|
1047
|
+
return {
|
|
1048
|
+
label: node.label,
|
|
1049
|
+
path: validPath,
|
|
1050
|
+
fragment: node.fragment,
|
|
1051
|
+
children
|
|
1052
|
+
};
|
|
1053
|
+
}).filter((node) => node !== null);
|
|
1054
|
+
const tocTree = mapTree(metadata.tocTree);
|
|
1055
|
+
const index = metadata.indexEntries.map((item) => ({
|
|
1056
|
+
label: item.label,
|
|
1057
|
+
path: chmKey(item.path),
|
|
1058
|
+
fragment: item.fragment
|
|
1059
|
+
})).filter((item) => seenSpine.has(item.path));
|
|
1060
|
+
const source = {
|
|
1061
|
+
entries: bookEntries,
|
|
1062
|
+
async read(key, limit, signal = new AbortController().signal) {
|
|
1063
|
+
const entry = entriesByKey.get(key);
|
|
1064
|
+
if (!entry) throw new ViewerError("missing-related-file", copy.missing);
|
|
1065
|
+
const rawBytes = await reader.readEntry(entry, limit, signal);
|
|
1066
|
+
const mime = chmMimeType(entry.lowerPath);
|
|
1067
|
+
if (mime === "text/html") {
|
|
1068
|
+
const text = decodeChmText(rawBytes, metadata.encoding, true);
|
|
1069
|
+
return normalizeChmHtml(text, entry.path, directory, selfChmNames, copy.unsupportedObject, items);
|
|
1070
|
+
}
|
|
1071
|
+
if (mime === "text/css") {
|
|
1072
|
+
const text = decodeChmText(rawBytes, metadata.encoding, false);
|
|
1073
|
+
return new TextEncoder().encode(text);
|
|
1074
|
+
}
|
|
1075
|
+
return rawBytes;
|
|
1076
|
+
},
|
|
1077
|
+
async dispose() {
|
|
1078
|
+
reader.dispose();
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
const book = {
|
|
1082
|
+
title: metadata.title || fileName,
|
|
1083
|
+
author: "",
|
|
1084
|
+
direction: "ltr",
|
|
1085
|
+
items,
|
|
1086
|
+
spine,
|
|
1087
|
+
toc
|
|
1088
|
+
};
|
|
1089
|
+
return {
|
|
1090
|
+
title: book.title,
|
|
1091
|
+
author: book.author,
|
|
1092
|
+
spine,
|
|
1093
|
+
toc,
|
|
1094
|
+
tocTree: tocTree.length > 0 ? tocTree : void 0,
|
|
1095
|
+
index: index.length > 0 ? index : void 0,
|
|
1096
|
+
loadSection: (path, signal) => prepareChapter(source, book, path, signal)
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
// ../plugins/chm/src/lzx.ts
|
|
1101
|
+
var LZX_FRAME_SIZE = 32768;
|
|
1102
|
+
var LZX_MAX_CODE_LEN = 16;
|
|
1103
|
+
var LZX_TABLE_BITS = 12;
|
|
1104
|
+
var LZX_PRETREE_SIZE = 20;
|
|
1105
|
+
var LZX_LENGTH_TREE_SIZE = 249;
|
|
1106
|
+
var LZX_ALIGNED_SIZE = 8;
|
|
1107
|
+
var POSITION_SLOTS = [30, 32, 34, 36, 38, 42, 50];
|
|
1108
|
+
var EXTRA_BITS = new Uint8Array(51);
|
|
1109
|
+
var POSITION_BASE = new Uint32Array(51);
|
|
1110
|
+
(() => {
|
|
1111
|
+
let base = 0;
|
|
1112
|
+
for (let i = 0; i < 51; i += 1) {
|
|
1113
|
+
const extra = i < 4 ? 0 : i < 36 ? (i >> 1) - 1 : 17;
|
|
1114
|
+
EXTRA_BITS[i] = extra;
|
|
1115
|
+
POSITION_BASE[i] = base;
|
|
1116
|
+
base += 1 << extra;
|
|
1117
|
+
}
|
|
1118
|
+
})();
|
|
1119
|
+
var LzxBitReader = class {
|
|
1120
|
+
constructor(data) {
|
|
1121
|
+
this.data = data;
|
|
1122
|
+
this.pos = 0;
|
|
1123
|
+
this.bitBuf = 0;
|
|
1124
|
+
this.bitsLeft = 0;
|
|
1125
|
+
this.eofPadded = false;
|
|
1126
|
+
}
|
|
1127
|
+
ensure(n) {
|
|
1128
|
+
while (this.bitsLeft < n) {
|
|
1129
|
+
if (this.pos >= this.data.length) {
|
|
1130
|
+
if (this.eofPadded) {
|
|
1131
|
+
throw new ViewerError("invalid-file", "Unexpected end of LZX compressed stream.");
|
|
1132
|
+
}
|
|
1133
|
+
this.eofPadded = true;
|
|
1134
|
+
this.bitsLeft += 16;
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
const lo = this.data[this.pos++];
|
|
1138
|
+
const hi = this.pos < this.data.length ? this.data[this.pos++] : 0;
|
|
1139
|
+
const word = lo | hi << 8;
|
|
1140
|
+
this.bitBuf = (this.bitBuf | word << 16 - this.bitsLeft) >>> 0;
|
|
1141
|
+
this.bitsLeft += 16;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
peek(n) {
|
|
1145
|
+
if (n === 0) return 0;
|
|
1146
|
+
this.ensure(n);
|
|
1147
|
+
return this.bitBuf >>> 32 - n;
|
|
1148
|
+
}
|
|
1149
|
+
skip(n) {
|
|
1150
|
+
if (this.eofPadded && n > this.bitsLeft - 16) {
|
|
1151
|
+
throw new ViewerError("invalid-file", "Unexpected end of LZX compressed stream.");
|
|
1152
|
+
}
|
|
1153
|
+
this.bitBuf = n >= 32 ? 0 : this.bitBuf << n >>> 0;
|
|
1154
|
+
this.bitsLeft -= n;
|
|
1155
|
+
}
|
|
1156
|
+
readBits(n) {
|
|
1157
|
+
if (n === 0) return 0;
|
|
1158
|
+
if (n <= 16) {
|
|
1159
|
+
const value = this.peek(n);
|
|
1160
|
+
this.skip(n);
|
|
1161
|
+
return value;
|
|
1162
|
+
}
|
|
1163
|
+
const high = this.readBits(16);
|
|
1164
|
+
const low = this.readBits(n - 16);
|
|
1165
|
+
return (high << n - 16 | low) >>> 0;
|
|
1166
|
+
}
|
|
1167
|
+
alignForUncompressed() {
|
|
1168
|
+
if (this.bitsLeft === 0) {
|
|
1169
|
+
this.ensure(16);
|
|
1170
|
+
}
|
|
1171
|
+
this.bitsLeft = 0;
|
|
1172
|
+
this.bitBuf = 0;
|
|
1173
|
+
const r0 = this.readRawUint32();
|
|
1174
|
+
const r1 = this.readRawUint32();
|
|
1175
|
+
const r2 = this.readRawUint32();
|
|
1176
|
+
return { r0, r1, r2 };
|
|
1177
|
+
}
|
|
1178
|
+
readRawBytes(dest, destOffset, count) {
|
|
1179
|
+
if (this.pos + count > this.data.length) {
|
|
1180
|
+
throw new ViewerError("invalid-file", "Truncated LZX uncompressed block.");
|
|
1181
|
+
}
|
|
1182
|
+
dest.set(this.data.subarray(this.pos, this.pos + count), destOffset);
|
|
1183
|
+
this.pos += count;
|
|
1184
|
+
}
|
|
1185
|
+
skipPadByte() {
|
|
1186
|
+
if (this.pos < this.data.length) {
|
|
1187
|
+
this.pos += 1;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
readRawUint32() {
|
|
1191
|
+
if (this.pos + 4 > this.data.length) {
|
|
1192
|
+
throw new ViewerError("invalid-file", "Truncated LZX uncompressed block header.");
|
|
1193
|
+
}
|
|
1194
|
+
const v = (this.data[this.pos] | this.data[this.pos + 1] << 8 | this.data[this.pos + 2] << 16 | this.data[this.pos + 3] << 24) >>> 0;
|
|
1195
|
+
this.pos += 4;
|
|
1196
|
+
return v;
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
function buildCanonicalTree(lengths, allowEmpty) {
|
|
1200
|
+
const numSyms = lengths.length;
|
|
1201
|
+
const tableSize = 1 << LZX_TABLE_BITS;
|
|
1202
|
+
const table = new Int32Array(tableSize).fill(-1);
|
|
1203
|
+
const left = new Int32Array(numSyms * 2 + 16).fill(-1);
|
|
1204
|
+
const right = new Int32Array(numSyms * 2 + 16).fill(-1);
|
|
1205
|
+
let nonZero = 0;
|
|
1206
|
+
let maxLen = 0;
|
|
1207
|
+
for (let i = 0; i < numSyms; i += 1) {
|
|
1208
|
+
const len = lengths[i];
|
|
1209
|
+
if (len > LZX_MAX_CODE_LEN) {
|
|
1210
|
+
throw new ViewerError("invalid-file", "Invalid LZX Huffman code length.");
|
|
1211
|
+
}
|
|
1212
|
+
if (len > 0) {
|
|
1213
|
+
nonZero += 1;
|
|
1214
|
+
if (len > maxLen) maxLen = len;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
if (nonZero === 0) {
|
|
1218
|
+
if (!allowEmpty) {
|
|
1219
|
+
throw new ViewerError("invalid-file", "Empty LZX Huffman table.");
|
|
1220
|
+
}
|
|
1221
|
+
return { empty: true, maxBits: 0, table, left, right };
|
|
1222
|
+
}
|
|
1223
|
+
let code = 0;
|
|
1224
|
+
let nextNode = numSyms;
|
|
1225
|
+
for (let bits = 1; bits <= maxLen; bits += 1) {
|
|
1226
|
+
for (let sym = 0; sym < numSyms; sym += 1) {
|
|
1227
|
+
if (lengths[sym] !== bits) continue;
|
|
1228
|
+
if (code >= 1 << bits) {
|
|
1229
|
+
throw new ViewerError("invalid-file", "Oversubscribed LZX Huffman table.");
|
|
1230
|
+
}
|
|
1231
|
+
if (bits <= LZX_TABLE_BITS) {
|
|
1232
|
+
const shift = LZX_TABLE_BITS - bits;
|
|
1233
|
+
const start = code << shift;
|
|
1234
|
+
const end = code + 1 << shift;
|
|
1235
|
+
table.fill(sym, start, end);
|
|
1236
|
+
} else {
|
|
1237
|
+
const prefix = code >>> bits - LZX_TABLE_BITS;
|
|
1238
|
+
let node = table[prefix];
|
|
1239
|
+
if (node === -1) {
|
|
1240
|
+
node = nextNode++;
|
|
1241
|
+
table[prefix] = node;
|
|
1242
|
+
}
|
|
1243
|
+
const extra = bits - LZX_TABLE_BITS;
|
|
1244
|
+
for (let step = extra - 1; step >= 0; step -= 1) {
|
|
1245
|
+
const bit = code >>> step & 1;
|
|
1246
|
+
if (step === 0) {
|
|
1247
|
+
if (bit) right[node] = sym;
|
|
1248
|
+
else left[node] = sym;
|
|
1249
|
+
} else {
|
|
1250
|
+
let child = bit ? right[node] : left[node];
|
|
1251
|
+
if (child === -1) {
|
|
1252
|
+
child = nextNode++;
|
|
1253
|
+
if (bit) right[node] = child;
|
|
1254
|
+
else left[node] = child;
|
|
1255
|
+
}
|
|
1256
|
+
node = child;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
code += 1;
|
|
1261
|
+
}
|
|
1262
|
+
code <<= 1;
|
|
1263
|
+
}
|
|
1264
|
+
return { empty: false, maxBits: maxLen, table, left, right };
|
|
1265
|
+
}
|
|
1266
|
+
function decodeSymbol(reader, tree, lengths, numSyms) {
|
|
1267
|
+
if (tree.empty) {
|
|
1268
|
+
throw new ViewerError("invalid-file", "Attempted decode from empty LZX Huffman table.");
|
|
1269
|
+
}
|
|
1270
|
+
const peek12 = reader.peek(LZX_TABLE_BITS);
|
|
1271
|
+
const sym = tree.table[peek12];
|
|
1272
|
+
if (sym < 0) {
|
|
1273
|
+
throw new ViewerError("invalid-file", "Invalid LZX Huffman prefix.");
|
|
1274
|
+
}
|
|
1275
|
+
if (sym < numSyms) {
|
|
1276
|
+
reader.skip(lengths[sym]);
|
|
1277
|
+
return sym;
|
|
1278
|
+
}
|
|
1279
|
+
reader.skip(LZX_TABLE_BITS);
|
|
1280
|
+
let node = sym;
|
|
1281
|
+
for (let depth = 0; depth < LZX_MAX_CODE_LEN - LZX_TABLE_BITS + 1; depth += 1) {
|
|
1282
|
+
const bit = reader.readBits(1);
|
|
1283
|
+
node = bit ? tree.right[node] : tree.left[node];
|
|
1284
|
+
if (node < 0) {
|
|
1285
|
+
throw new ViewerError("invalid-file", "Invalid LZX Huffman tree path.");
|
|
1286
|
+
}
|
|
1287
|
+
if (node < numSyms) return node;
|
|
1288
|
+
}
|
|
1289
|
+
throw new ViewerError("invalid-file", "LZX Huffman code exceeded max length.");
|
|
1290
|
+
}
|
|
1291
|
+
function readLengths(reader, lengths, first, last) {
|
|
1292
|
+
const preLengths = new Uint8Array(LZX_PRETREE_SIZE);
|
|
1293
|
+
for (let i = 0; i < LZX_PRETREE_SIZE; i += 1) {
|
|
1294
|
+
preLengths[i] = reader.readBits(4);
|
|
1295
|
+
}
|
|
1296
|
+
const preTree = buildCanonicalTree(preLengths, false);
|
|
1297
|
+
let x = first;
|
|
1298
|
+
while (x < last) {
|
|
1299
|
+
const y = decodeSymbol(reader, preTree, preLengths, LZX_PRETREE_SIZE);
|
|
1300
|
+
if (y === 17) {
|
|
1301
|
+
const z = reader.readBits(4) + 4;
|
|
1302
|
+
const end = Math.min(last, x + z);
|
|
1303
|
+
while (x < end) lengths[x++] = 0;
|
|
1304
|
+
} else if (y === 18) {
|
|
1305
|
+
const z = reader.readBits(5) + 20;
|
|
1306
|
+
const end = Math.min(last, x + z);
|
|
1307
|
+
while (x < end) lengths[x++] = 0;
|
|
1308
|
+
} else if (y === 19) {
|
|
1309
|
+
const z = reader.readBits(1) + 4;
|
|
1310
|
+
const sym = decodeSymbol(reader, preTree, preLengths, LZX_PRETREE_SIZE);
|
|
1311
|
+
const same = (lengths[x] - sym + 17) % 17;
|
|
1312
|
+
const end = Math.min(last, x + z);
|
|
1313
|
+
while (x < end) lengths[x++] = same;
|
|
1314
|
+
} else {
|
|
1315
|
+
lengths[x] = (lengths[x] - y + 17) % 17;
|
|
1316
|
+
x += 1;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
function decompressLzxStream(compressed, control, outputBytes, signal) {
|
|
1321
|
+
if (outputBytes > CHM_LIMITS.maxResetIntervalBytes && outputBytes > CHM_LIMITS.maxSectionBytes) {
|
|
1322
|
+
throw new ViewerError("resource-limit", "LZX decompression request exceeds budget.");
|
|
1323
|
+
}
|
|
1324
|
+
if (outputBytes === 0) return new Uint8Array(0);
|
|
1325
|
+
const reader = new LzxBitReader(compressed);
|
|
1326
|
+
const window = new Uint8Array(control.windowSize);
|
|
1327
|
+
const windowMask = control.windowSize - 1;
|
|
1328
|
+
const paddedOutputBytes = Math.ceil(outputBytes / LZX_FRAME_SIZE) * LZX_FRAME_SIZE;
|
|
1329
|
+
const output = new Uint8Array(paddedOutputBytes);
|
|
1330
|
+
const posnSlots = POSITION_SLOTS[control.windowBits - 15];
|
|
1331
|
+
const mainTreeSize = 256 + (posnSlots << 3);
|
|
1332
|
+
const mainLengths = new Uint8Array(mainTreeSize);
|
|
1333
|
+
const lengthLengths = new Uint8Array(LZX_LENGTH_TREE_SIZE);
|
|
1334
|
+
const alignedLengths = new Uint8Array(LZX_ALIGNED_SIZE);
|
|
1335
|
+
let mainTree = null;
|
|
1336
|
+
let lengthTree = null;
|
|
1337
|
+
let alignedTree = null;
|
|
1338
|
+
let r0 = 1;
|
|
1339
|
+
let r1 = 1;
|
|
1340
|
+
let r2 = 1;
|
|
1341
|
+
let headerRead = false;
|
|
1342
|
+
let intelFilesize = 0;
|
|
1343
|
+
let intelStarted = false;
|
|
1344
|
+
let blockType = 0;
|
|
1345
|
+
let blockLength = 0;
|
|
1346
|
+
let blockRemaining = 0;
|
|
1347
|
+
let windowPos = 0;
|
|
1348
|
+
let framePos = 0;
|
|
1349
|
+
let frame = 0;
|
|
1350
|
+
let outPos = 0;
|
|
1351
|
+
const resetState = () => {
|
|
1352
|
+
r0 = 1;
|
|
1353
|
+
r1 = 1;
|
|
1354
|
+
r2 = 1;
|
|
1355
|
+
headerRead = false;
|
|
1356
|
+
blockRemaining = 0;
|
|
1357
|
+
blockType = 0;
|
|
1358
|
+
mainLengths.fill(0);
|
|
1359
|
+
lengthLengths.fill(0);
|
|
1360
|
+
mainTree = null;
|
|
1361
|
+
lengthTree = null;
|
|
1362
|
+
alignedTree = null;
|
|
1363
|
+
};
|
|
1364
|
+
while (outPos < outputBytes) {
|
|
1365
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
1366
|
+
if (control.resetFrames > 0 && frame > 0 && frame % control.resetFrames === 0) {
|
|
1367
|
+
resetState();
|
|
1368
|
+
}
|
|
1369
|
+
if (!headerRead) {
|
|
1370
|
+
const hasIntel = reader.readBits(1);
|
|
1371
|
+
intelFilesize = hasIntel ? reader.readBits(16) << 16 | reader.readBits(16) : 0;
|
|
1372
|
+
headerRead = true;
|
|
1373
|
+
}
|
|
1374
|
+
const frameSize = LZX_FRAME_SIZE;
|
|
1375
|
+
let bytesTodo = frameSize;
|
|
1376
|
+
while (bytesTodo > 0) {
|
|
1377
|
+
if (blockRemaining === 0) {
|
|
1378
|
+
if (outPos + (frameSize - bytesTodo) >= outputBytes) {
|
|
1379
|
+
const partial = frameSize - bytesTodo;
|
|
1380
|
+
if (partial > 0) {
|
|
1381
|
+
output.set(window.subarray(framePos, framePos + partial), outPos);
|
|
1382
|
+
}
|
|
1383
|
+
return output.subarray(0, outputBytes);
|
|
1384
|
+
}
|
|
1385
|
+
if (blockType === 3 && (blockLength & 1) !== 0) {
|
|
1386
|
+
reader.skipPadByte();
|
|
1387
|
+
}
|
|
1388
|
+
blockType = reader.readBits(3);
|
|
1389
|
+
blockLength = reader.readBits(16) << 8 | reader.readBits(8);
|
|
1390
|
+
blockRemaining = blockLength;
|
|
1391
|
+
if (blockType === 2) {
|
|
1392
|
+
for (let i = 0; i < 8; i += 1) alignedLengths[i] = reader.readBits(3);
|
|
1393
|
+
alignedTree = buildCanonicalTree(alignedLengths, false);
|
|
1394
|
+
}
|
|
1395
|
+
if (blockType === 1 || blockType === 2) {
|
|
1396
|
+
readLengths(reader, mainLengths, 0, 256);
|
|
1397
|
+
readLengths(reader, mainLengths, 256, mainTreeSize);
|
|
1398
|
+
mainTree = buildCanonicalTree(mainLengths, false);
|
|
1399
|
+
if (mainLengths[232] !== 0) intelStarted = true;
|
|
1400
|
+
readLengths(reader, lengthLengths, 0, LZX_LENGTH_TREE_SIZE);
|
|
1401
|
+
lengthTree = buildCanonicalTree(lengthLengths, true);
|
|
1402
|
+
} else if (blockType === 3) {
|
|
1403
|
+
intelStarted = true;
|
|
1404
|
+
const regs = reader.alignForUncompressed();
|
|
1405
|
+
r0 = regs.r0;
|
|
1406
|
+
r1 = regs.r1;
|
|
1407
|
+
r2 = regs.r2;
|
|
1408
|
+
} else {
|
|
1409
|
+
throw new ViewerError("invalid-file", "Invalid LZX block type.");
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
const thisRun = Math.min(blockRemaining, bytesTodo);
|
|
1413
|
+
if (thisRun <= 0) {
|
|
1414
|
+
throw new ViewerError("invalid-file", "Zero-length LZX block.");
|
|
1415
|
+
}
|
|
1416
|
+
if (blockType === 3) {
|
|
1417
|
+
reader.readRawBytes(window, windowPos, thisRun);
|
|
1418
|
+
windowPos += thisRun;
|
|
1419
|
+
blockRemaining -= thisRun;
|
|
1420
|
+
bytesTodo -= thisRun;
|
|
1421
|
+
} else {
|
|
1422
|
+
let remaining = thisRun;
|
|
1423
|
+
while (remaining > 0) {
|
|
1424
|
+
const sym = decodeSymbol(reader, mainTree, mainLengths, mainTreeSize);
|
|
1425
|
+
if (sym < 256) {
|
|
1426
|
+
window[windowPos++] = sym;
|
|
1427
|
+
remaining -= 1;
|
|
1428
|
+
blockRemaining -= 1;
|
|
1429
|
+
bytesTodo -= 1;
|
|
1430
|
+
} else {
|
|
1431
|
+
const elem = sym - 256;
|
|
1432
|
+
let matchLen = elem & 7;
|
|
1433
|
+
if (matchLen === 7) {
|
|
1434
|
+
matchLen += decodeSymbol(reader, lengthTree, lengthLengths, LZX_LENGTH_TREE_SIZE);
|
|
1435
|
+
}
|
|
1436
|
+
matchLen += 2;
|
|
1437
|
+
const footer = elem >> 3;
|
|
1438
|
+
let matchOffset;
|
|
1439
|
+
if (footer === 0) {
|
|
1440
|
+
matchOffset = r0;
|
|
1441
|
+
} else if (footer === 1) {
|
|
1442
|
+
matchOffset = r1;
|
|
1443
|
+
r1 = r0;
|
|
1444
|
+
r0 = matchOffset;
|
|
1445
|
+
} else if (footer === 2) {
|
|
1446
|
+
matchOffset = r2;
|
|
1447
|
+
r2 = r0;
|
|
1448
|
+
r0 = matchOffset;
|
|
1449
|
+
} else {
|
|
1450
|
+
const extra = EXTRA_BITS[footer];
|
|
1451
|
+
const base = POSITION_BASE[footer] - 2;
|
|
1452
|
+
if (blockType === 2 && extra >= 3) {
|
|
1453
|
+
const verbatimBits = reader.readBits(extra - 3) << 3;
|
|
1454
|
+
const alignedBits = decodeSymbol(reader, alignedTree, alignedLengths, LZX_ALIGNED_SIZE);
|
|
1455
|
+
matchOffset = base + verbatimBits + alignedBits;
|
|
1456
|
+
} else {
|
|
1457
|
+
matchOffset = base + reader.readBits(extra);
|
|
1458
|
+
}
|
|
1459
|
+
r2 = r1;
|
|
1460
|
+
r1 = r0;
|
|
1461
|
+
r0 = matchOffset;
|
|
1462
|
+
}
|
|
1463
|
+
if (matchOffset <= 0 || matchOffset > control.windowSize) {
|
|
1464
|
+
throw new ViewerError("invalid-file", "LZX match offset out of window.");
|
|
1465
|
+
}
|
|
1466
|
+
if (matchLen > remaining) {
|
|
1467
|
+
throw new ViewerError("invalid-file", "LZX match crosses block or frame boundary.");
|
|
1468
|
+
}
|
|
1469
|
+
let srcPos = windowPos - matchOffset & windowMask;
|
|
1470
|
+
for (let k = 0; k < matchLen; k += 1) {
|
|
1471
|
+
window[windowPos++] = window[srcPos];
|
|
1472
|
+
srcPos = srcPos + 1 & windowMask;
|
|
1473
|
+
}
|
|
1474
|
+
remaining -= matchLen;
|
|
1475
|
+
blockRemaining -= matchLen;
|
|
1476
|
+
bytesTodo -= matchLen;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
const frameSlice = window.subarray(framePos, framePos + frameSize);
|
|
1482
|
+
output.set(frameSlice, outPos);
|
|
1483
|
+
if (intelStarted && intelFilesize > 0 && frame < 32768 && frameSize > 10) {
|
|
1484
|
+
let curPos = outPos;
|
|
1485
|
+
const limit = outPos + frameSize - 10;
|
|
1486
|
+
let i = outPos;
|
|
1487
|
+
while (i < limit) {
|
|
1488
|
+
if (output[i++] !== 232) {
|
|
1489
|
+
curPos += 1;
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
const absOff = output[i] | output[i + 1] << 8 | output[i + 2] << 16 | output[i + 3] << 24;
|
|
1493
|
+
if (absOff >= -curPos && absOff < intelFilesize) {
|
|
1494
|
+
const relOff = absOff >= 0 ? absOff - curPos : absOff + intelFilesize;
|
|
1495
|
+
output[i] = relOff & 255;
|
|
1496
|
+
output[i + 1] = relOff >>> 8 & 255;
|
|
1497
|
+
output[i + 2] = relOff >>> 16 & 255;
|
|
1498
|
+
output[i + 3] = relOff >>> 24 & 255;
|
|
1499
|
+
}
|
|
1500
|
+
i += 4;
|
|
1501
|
+
curPos += 5;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
outPos += frameSize;
|
|
1505
|
+
framePos += frameSize;
|
|
1506
|
+
frame += 1;
|
|
1507
|
+
if (windowPos === control.windowSize) windowPos = 0;
|
|
1508
|
+
if (framePos === control.windowSize) framePos = 0;
|
|
1509
|
+
}
|
|
1510
|
+
return output.subarray(0, outputBytes);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
// ../plugins/chm/src/section-reader.ts
|
|
1514
|
+
function readUint64Checked(view, offset) {
|
|
1515
|
+
const lo = view.getUint32(offset, true);
|
|
1516
|
+
const hi = view.getUint32(offset + 4, true);
|
|
1517
|
+
if (hi > 2097151) {
|
|
1518
|
+
throw new ViewerError("invalid-file", "CHM ResetTable 64-bit integer out of range.");
|
|
1519
|
+
}
|
|
1520
|
+
return hi * 2 ** 32 + lo;
|
|
1521
|
+
}
|
|
1522
|
+
function parseControlData(bytes) {
|
|
1523
|
+
if (bytes.byteLength < 28) {
|
|
1524
|
+
throw new ViewerError("invalid-file", "CHM ControlData is truncated.");
|
|
1525
|
+
}
|
|
1526
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1527
|
+
if (bytes[4] !== 76 || bytes[5] !== 90 || bytes[6] !== 88 || bytes[7] !== 67) {
|
|
1528
|
+
throw new ViewerError("invalid-file", "Invalid CHM LZXC signature.");
|
|
1529
|
+
}
|
|
1530
|
+
const version = view.getUint32(8, true);
|
|
1531
|
+
let resetInterval = view.getUint32(12, true);
|
|
1532
|
+
let windowSize = view.getUint32(16, true);
|
|
1533
|
+
if (version === 2) {
|
|
1534
|
+
resetInterval *= LZX_FRAME_SIZE;
|
|
1535
|
+
windowSize *= LZX_FRAME_SIZE;
|
|
1536
|
+
} else if (version !== 1) {
|
|
1537
|
+
throw new ViewerError("invalid-file", "Unsupported LZXC ControlData version.");
|
|
1538
|
+
}
|
|
1539
|
+
let windowBits = 0;
|
|
1540
|
+
for (let bits = 15; bits <= 21; bits += 1) {
|
|
1541
|
+
if (windowSize === 1 << bits) {
|
|
1542
|
+
windowBits = bits;
|
|
1543
|
+
break;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
if (!windowBits) {
|
|
1547
|
+
throw new ViewerError("invalid-file", "Unsupported LZX window size.");
|
|
1548
|
+
}
|
|
1549
|
+
if (resetInterval === 0 || resetInterval % LZX_FRAME_SIZE !== 0) {
|
|
1550
|
+
throw new ViewerError("invalid-file", "Invalid LZX reset interval.");
|
|
1551
|
+
}
|
|
1552
|
+
if (resetInterval > CHM_LIMITS.maxResetIntervalBytes) {
|
|
1553
|
+
throw new ViewerError("resource-limit", "LZX reset interval exceeds budget.");
|
|
1554
|
+
}
|
|
1555
|
+
return {
|
|
1556
|
+
windowBits,
|
|
1557
|
+
windowSize,
|
|
1558
|
+
resetInterval,
|
|
1559
|
+
resetFrames: resetInterval / LZX_FRAME_SIZE
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
function parseResetTable(bytes, compressedContentSize) {
|
|
1563
|
+
if (bytes.byteLength < 40) {
|
|
1564
|
+
throw new ViewerError("invalid-file", "CHM ResetTable is truncated.");
|
|
1565
|
+
}
|
|
1566
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1567
|
+
const version = view.getUint32(0, true);
|
|
1568
|
+
const numEntries = view.getUint32(4, true);
|
|
1569
|
+
const entrySize = view.getUint32(8, true);
|
|
1570
|
+
const tableOffset = view.getUint32(12, true);
|
|
1571
|
+
const uncompressedLength = readUint64Checked(view, 16);
|
|
1572
|
+
const compressedLength = readUint64Checked(view, 24);
|
|
1573
|
+
const frameLength = readUint64Checked(view, 32);
|
|
1574
|
+
if (version !== 2 || entrySize !== 4 && entrySize !== 8 || tableOffset < 40) {
|
|
1575
|
+
throw new ViewerError("invalid-file", "Invalid CHM ResetTable header.");
|
|
1576
|
+
}
|
|
1577
|
+
if (numEntries > CHM_LIMITS.maxResetEntries) {
|
|
1578
|
+
throw new ViewerError("resource-limit", "CHM ResetTable entry count exceeds budget.");
|
|
1579
|
+
}
|
|
1580
|
+
if (uncompressedLength > CHM_LIMITS.maxSectionBytes) {
|
|
1581
|
+
throw new ViewerError("resource-limit", "CHM uncompressed section exceeds 128 MiB limit.");
|
|
1582
|
+
}
|
|
1583
|
+
if (compressedLength > compressedContentSize) {
|
|
1584
|
+
throw new ViewerError("invalid-file", "CHM ResetTable compressed length exceeds content stream.");
|
|
1585
|
+
}
|
|
1586
|
+
if (tableOffset + numEntries * entrySize > bytes.byteLength) {
|
|
1587
|
+
throw new ViewerError("invalid-file", "CHM ResetTable entries exceed table bounds.");
|
|
1588
|
+
}
|
|
1589
|
+
const offsets = [];
|
|
1590
|
+
let prev = 0;
|
|
1591
|
+
for (let i = 0; i < numEntries; i += 1) {
|
|
1592
|
+
const pos = tableOffset + i * entrySize;
|
|
1593
|
+
const offset = entrySize === 8 ? readUint64Checked(view, pos) : view.getUint32(pos, true);
|
|
1594
|
+
if (offset < prev || offset > compressedContentSize) {
|
|
1595
|
+
throw new ViewerError("invalid-file", "Invalid CHM ResetTable entry offset.");
|
|
1596
|
+
}
|
|
1597
|
+
offsets.push(offset);
|
|
1598
|
+
prev = offset;
|
|
1599
|
+
}
|
|
1600
|
+
return {
|
|
1601
|
+
uncompressedLength,
|
|
1602
|
+
compressedLength,
|
|
1603
|
+
frameLength: frameLength || LZX_FRAME_SIZE,
|
|
1604
|
+
offsets
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
async function sliceFile(file, start, end, signal) {
|
|
1608
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
1609
|
+
if (start < 0 || end < start || end > file.size) {
|
|
1610
|
+
throw new ViewerError("invalid-file", "CHM file slice out of bounds.");
|
|
1611
|
+
}
|
|
1612
|
+
const buffer = await file.slice(start, end).arrayBuffer();
|
|
1613
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
1614
|
+
return new Uint8Array(buffer);
|
|
1615
|
+
}
|
|
1616
|
+
function createChmSectionReader(file, directory) {
|
|
1617
|
+
let lzxInfoPromise = null;
|
|
1618
|
+
const intervalCache = /* @__PURE__ */ new Map();
|
|
1619
|
+
async function readSection0(entry, maxBytes, signal) {
|
|
1620
|
+
if (entry.length > maxBytes) {
|
|
1621
|
+
throw new ViewerError("resource-limit", "CHM entry exceeds read budget.");
|
|
1622
|
+
}
|
|
1623
|
+
if (entry.length === 0) return new Uint8Array(0);
|
|
1624
|
+
const start = directory.contentOffset + entry.offset;
|
|
1625
|
+
return sliceFile(file, start, start + entry.length, signal);
|
|
1626
|
+
}
|
|
1627
|
+
async function ensureLzxInfo(signal) {
|
|
1628
|
+
if (lzxInfoPromise) return lzxInfoPromise;
|
|
1629
|
+
lzxInfoPromise = (async () => {
|
|
1630
|
+
const contentEntry = directory.byLowerPath.get("::dataspace/storage/mscompressed/content");
|
|
1631
|
+
const controlEntry = directory.byLowerPath.get("::dataspace/storage/mscompressed/controldata");
|
|
1632
|
+
if (!contentEntry || !controlEntry || contentEntry.section !== 0 || controlEntry.section !== 0) {
|
|
1633
|
+
throw new ViewerError("invalid-file", "Missing CHM MSCompressed stream metadata.");
|
|
1634
|
+
}
|
|
1635
|
+
const controlBytes = await readSection0(controlEntry, 1024, signal);
|
|
1636
|
+
const control = parseControlData(controlBytes);
|
|
1637
|
+
const resetEntry = directory.byLowerPath.get(
|
|
1638
|
+
"::dataspace/storage/mscompressed/transform/{7fc28940-9d31-11d0-9b27-00a0c91e9c7c}/instancedata/resettable"
|
|
1639
|
+
);
|
|
1640
|
+
let resetTable = null;
|
|
1641
|
+
let totalUncompressed = 0;
|
|
1642
|
+
if (resetEntry && resetEntry.section === 0) {
|
|
1643
|
+
const resetBytes = await readSection0(resetEntry, 2 * 1024 * 1024, signal);
|
|
1644
|
+
resetTable = parseResetTable(resetBytes, contentEntry.length);
|
|
1645
|
+
totalUncompressed = resetTable.uncompressedLength;
|
|
1646
|
+
} else {
|
|
1647
|
+
const spanEntry = directory.byLowerPath.get("::dataspace/storage/mscompressed/spaninfo");
|
|
1648
|
+
if (!spanEntry || spanEntry.section !== 0 || spanEntry.length < 8) {
|
|
1649
|
+
throw new ViewerError("invalid-file", "Missing CHM ResetTable and SpanInfo.");
|
|
1650
|
+
}
|
|
1651
|
+
const spanBytes = await readSection0(spanEntry, 64, signal);
|
|
1652
|
+
const view = new DataView(spanBytes.buffer, spanBytes.byteOffset, spanBytes.byteLength);
|
|
1653
|
+
totalUncompressed = readUint64Checked(view, 0);
|
|
1654
|
+
}
|
|
1655
|
+
if (totalUncompressed > CHM_LIMITS.maxSectionBytes) {
|
|
1656
|
+
throw new ViewerError("resource-limit", "CHM compressed section exceeds 128 MiB limit.");
|
|
1657
|
+
}
|
|
1658
|
+
return { contentEntry, control, resetTable, totalUncompressed };
|
|
1659
|
+
})();
|
|
1660
|
+
try {
|
|
1661
|
+
return await lzxInfoPromise;
|
|
1662
|
+
} catch (error) {
|
|
1663
|
+
lzxInfoPromise = null;
|
|
1664
|
+
throw error;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
async function readResetInterval(intervalIndex, signal) {
|
|
1668
|
+
const cached = intervalCache.get(intervalIndex);
|
|
1669
|
+
if (cached) {
|
|
1670
|
+
intervalCache.delete(intervalIndex);
|
|
1671
|
+
intervalCache.set(intervalIndex, cached);
|
|
1672
|
+
return cached;
|
|
1673
|
+
}
|
|
1674
|
+
const info = await ensureLzxInfo(signal);
|
|
1675
|
+
const { contentEntry, control, resetTable, totalUncompressed } = info;
|
|
1676
|
+
const baseOffset = directory.contentOffset + contentEntry.offset;
|
|
1677
|
+
let decoded;
|
|
1678
|
+
if (resetTable && resetTable.offsets.length > 0) {
|
|
1679
|
+
const startFrame = intervalIndex * control.resetFrames;
|
|
1680
|
+
if (startFrame >= resetTable.offsets.length) {
|
|
1681
|
+
throw new ViewerError("invalid-file", "CHM ResetTable frame index out of range.");
|
|
1682
|
+
}
|
|
1683
|
+
const compressedStart = resetTable.offsets[startFrame];
|
|
1684
|
+
const nextFrame = startFrame + control.resetFrames;
|
|
1685
|
+
const compressedEnd = nextFrame < resetTable.offsets.length ? resetTable.offsets[nextFrame] : resetTable.compressedLength || contentEntry.length;
|
|
1686
|
+
const uncompressedStart = startFrame * LZX_FRAME_SIZE;
|
|
1687
|
+
const uncompressedBytes = Math.min(control.resetInterval, totalUncompressed - uncompressedStart);
|
|
1688
|
+
if (uncompressedBytes <= 0) {
|
|
1689
|
+
throw new ViewerError("invalid-file", "CHM ResetTable interval is empty.");
|
|
1690
|
+
}
|
|
1691
|
+
const compressed = await sliceFile(file, baseOffset + compressedStart, baseOffset + compressedEnd, signal);
|
|
1692
|
+
decoded = decompressLzxStream(compressed, control, uncompressedBytes, signal);
|
|
1693
|
+
} else {
|
|
1694
|
+
if (intervalIndex !== 0) {
|
|
1695
|
+
throw new ViewerError("invalid-file", "CHM missing ResetTable for seek.");
|
|
1696
|
+
}
|
|
1697
|
+
if (totalUncompressed > CHM_LIMITS.maxResetIntervalBytes) {
|
|
1698
|
+
throw new ViewerError("resource-limit", "CHM unseekable compressed stream exceeds budget.");
|
|
1699
|
+
}
|
|
1700
|
+
const compressed = await sliceFile(file, baseOffset, baseOffset + contentEntry.length, signal);
|
|
1701
|
+
decoded = decompressLzxStream(compressed, control, totalUncompressed, signal);
|
|
1702
|
+
}
|
|
1703
|
+
if (intervalCache.size >= CHM_LIMITS.maxCachedIntervals) {
|
|
1704
|
+
const oldestKey = intervalCache.keys().next().value;
|
|
1705
|
+
if (oldestKey !== void 0) intervalCache.delete(oldestKey);
|
|
1706
|
+
}
|
|
1707
|
+
intervalCache.set(intervalIndex, decoded);
|
|
1708
|
+
return decoded;
|
|
1709
|
+
}
|
|
1710
|
+
return {
|
|
1711
|
+
async readEntry(entry, maxBytes, signal) {
|
|
1712
|
+
if (entry.length > maxBytes) {
|
|
1713
|
+
throw new ViewerError("resource-limit", "CHM entry exceeds read budget.");
|
|
1714
|
+
}
|
|
1715
|
+
if (entry.length === 0) return new Uint8Array(0);
|
|
1716
|
+
if (entry.section === 0) {
|
|
1717
|
+
return readSection0(entry, maxBytes, signal);
|
|
1718
|
+
}
|
|
1719
|
+
const info = await ensureLzxInfo(signal);
|
|
1720
|
+
if (entry.offset + entry.length > info.totalUncompressed) {
|
|
1721
|
+
throw new ViewerError("invalid-file", "CHM entry offset exceeds uncompressed section.");
|
|
1722
|
+
}
|
|
1723
|
+
const intervalBytes = info.resetTable ? info.control.resetInterval : info.totalUncompressed;
|
|
1724
|
+
const startInterval = Math.floor(entry.offset / intervalBytes);
|
|
1725
|
+
const endInterval = Math.floor((entry.offset + entry.length - 1) / intervalBytes);
|
|
1726
|
+
if (startInterval === endInterval) {
|
|
1727
|
+
const block = await readResetInterval(startInterval, signal);
|
|
1728
|
+
const relStart = entry.offset - startInterval * intervalBytes;
|
|
1729
|
+
return block.slice(relStart, relStart + entry.length);
|
|
1730
|
+
}
|
|
1731
|
+
const result = new Uint8Array(entry.length);
|
|
1732
|
+
let written = 0;
|
|
1733
|
+
for (let idx = startInterval; idx <= endInterval; idx += 1) {
|
|
1734
|
+
const block = await readResetInterval(idx, signal);
|
|
1735
|
+
const blockStart = idx * intervalBytes;
|
|
1736
|
+
const copyFrom = Math.max(0, entry.offset - blockStart);
|
|
1737
|
+
const copyTo = Math.min(block.length, entry.offset + entry.length - blockStart);
|
|
1738
|
+
result.set(block.subarray(copyFrom, copyTo), written);
|
|
1739
|
+
written += copyTo - copyFrom;
|
|
1740
|
+
}
|
|
1741
|
+
return result;
|
|
1742
|
+
},
|
|
1743
|
+
dispose() {
|
|
1744
|
+
intervalCache.clear();
|
|
1745
|
+
}
|
|
1746
|
+
};
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
// ../plugins/chm/src/extract.ts
|
|
1750
|
+
var PLUGIN_ID = "chm-reader";
|
|
1751
|
+
var MAX_TOC_ENTRIES = 1e3;
|
|
1752
|
+
function mapError(error) {
|
|
1753
|
+
if (error instanceof AgentError || isViewerAbortError(error)) throw error;
|
|
1754
|
+
if (error instanceof ViewerError && (error.code === "resource-limit" || error.code === "missing-related-file")) {
|
|
1755
|
+
throw new AgentError(error.code, `CHM: ${error.message}`, { pluginId: PLUGIN_ID, cause: error });
|
|
1756
|
+
}
|
|
1757
|
+
throw new AgentError("invalid-file", "The file is not a readable CHM help file.", { pluginId: PLUGIN_ID, cause: error });
|
|
1758
|
+
}
|
|
1759
|
+
function topicTitles(metadata, publication) {
|
|
1760
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
1761
|
+
const walk = (nodes) => {
|
|
1762
|
+
for (const node of nodes) {
|
|
1763
|
+
const key = chmKey(node.path);
|
|
1764
|
+
if (key && node.label && !byKey.has(key)) byKey.set(key, node.label);
|
|
1765
|
+
walk(node.children);
|
|
1766
|
+
}
|
|
1767
|
+
};
|
|
1768
|
+
walk(metadata.tocTree);
|
|
1769
|
+
for (const topic of metadata.systemTopics) {
|
|
1770
|
+
const key = chmKey(topic.url);
|
|
1771
|
+
if (topic.title && !byKey.has(key)) byKey.set(key, topic.title);
|
|
1772
|
+
}
|
|
1773
|
+
return publication.spine.map((item) => byKey.get(item.path) || item.path.split("/").pop() || item.path);
|
|
1774
|
+
}
|
|
1775
|
+
async function withBook(context, run) {
|
|
1776
|
+
if (context.signal.aborted) throw abortError();
|
|
1777
|
+
if (context.file.size > CHM_LIMITS.maxFileBytes) {
|
|
1778
|
+
throw new AgentError("resource-limit", "The CHM file exceeds safety limits.", { pluginId: PLUGIN_ID });
|
|
1779
|
+
}
|
|
1780
|
+
let reader;
|
|
1781
|
+
try {
|
|
1782
|
+
const directory = await parseChmDirectory(context.file, context.signal);
|
|
1783
|
+
reader = createChmSectionReader(context.file, directory);
|
|
1784
|
+
const metadata = await readChmMetadata(directory, reader, context.signal);
|
|
1785
|
+
const publication = createChmPublication(context.file.name, directory, metadata, reader, "en");
|
|
1786
|
+
return await run({ directory, reader, metadata, publication, titles: topicTitles(metadata, publication) });
|
|
1787
|
+
} catch (error) {
|
|
1788
|
+
return mapError(error);
|
|
1789
|
+
} finally {
|
|
1790
|
+
reader?.dispose();
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
async function topicMarkdown(book, index, signal) {
|
|
1794
|
+
if (signal.aborted) throw abortError();
|
|
1795
|
+
const key = book.publication.spine[index].path;
|
|
1796
|
+
const entry = book.directory.byExactPath.get(`/${key}`);
|
|
1797
|
+
if (!entry) throw new AgentError("missing-related-file", `CHM topic is missing: ${key}`, { pluginId: PLUGIN_ID });
|
|
1798
|
+
const html = decodeChmText(await book.reader.readEntry(entry, CHM_LIMITS.maxTopicBytes, signal), book.metadata.encoding, true);
|
|
1799
|
+
const doc = parseHtmlText(html);
|
|
1800
|
+
const text = markupToMarkdown(doc.body ?? doc.documentElement, { headingOffset: 2 });
|
|
1801
|
+
return [`## Topic ${index + 1}: ${book.titles[index]}`, "", text || "(no text in this topic)"].join("\n");
|
|
1802
|
+
}
|
|
1803
|
+
var chmExtractor = {
|
|
1804
|
+
inspect(context) {
|
|
1805
|
+
return withBook(context, async ({ directory, metadata, publication, titles }) => {
|
|
1806
|
+
const chapterOf = new Map(publication.spine.map((item, index) => [item.path, index + 1]));
|
|
1807
|
+
return {
|
|
1808
|
+
metadata: {
|
|
1809
|
+
title: publication.title,
|
|
1810
|
+
compiledName: metadata.compiledName || null,
|
|
1811
|
+
encoding: metadata.encoding,
|
|
1812
|
+
lcid: `0x${directory.lcid.toString(16).padStart(4, "0")}`,
|
|
1813
|
+
defaultTopic: metadata.defaultTopic || null,
|
|
1814
|
+
topicCount: publication.spine.length,
|
|
1815
|
+
indexEntryCount: metadata.indexEntries.length,
|
|
1816
|
+
toc: publication.toc.slice(0, MAX_TOC_ENTRIES).map((entry) => ({ label: entry.label, chapter: chapterOf.get(entry.path) ?? null })),
|
|
1817
|
+
...publication.toc.length > MAX_TOC_ENTRIES ? { tocTruncated: true } : {},
|
|
1818
|
+
sizeBytes: context.source.size
|
|
1819
|
+
},
|
|
1820
|
+
targets: publication.spine.map((item, index) => ({ id: `chapter:${index + 1}`, title: `${titles[index]} (${item.path})`, kind: "topic" }))
|
|
1821
|
+
};
|
|
1822
|
+
});
|
|
1823
|
+
},
|
|
1824
|
+
read(context, params) {
|
|
1825
|
+
return withBook(context, async (book) => {
|
|
1826
|
+
const fields = {};
|
|
1827
|
+
if (book.metadata.compiledName) fields["Compiled name"] = book.metadata.compiledName;
|
|
1828
|
+
fields.Encoding = book.metadata.encoding;
|
|
1829
|
+
fields.Topics = String(book.publication.spine.length);
|
|
1830
|
+
return readPagedTextDocument({
|
|
1831
|
+
pageCount: book.publication.spine.length,
|
|
1832
|
+
targetPrefix: "chapter",
|
|
1833
|
+
header: () => markdownDocumentHeader(book.publication.title, fields),
|
|
1834
|
+
page: (index) => topicMarkdown(book, index, context.signal)
|
|
1835
|
+
}, params, PLUGIN_ID);
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
};
|
|
1839
|
+
var extract_default = chmExtractor;
|
|
1840
|
+
export {
|
|
1841
|
+
extract_default as default
|
|
1842
|
+
};
|