@awacloud/fw 0.1.0
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/CHANGELOG.md +79 -0
- package/LICENSE +176 -0
- package/NOTICE +12 -0
- package/README.md +355 -0
- package/dist/build/fw.core.pure.meta.json +29 -0
- package/dist/build/fw.core.pure.min.js +26 -0
- package/dist/build/fw.full.pure.meta.json +224 -0
- package/dist/build/fw.full.pure.min.js +60 -0
- package/dist/build/fw.minimal.pure.meta.json +21 -0
- package/dist/build/fw.minimal.pure.min.js +26 -0
- package/dist/build/fw.pack.compress-heavy.pure.meta.json +29 -0
- package/dist/build/fw.pack.compress-heavy.pure.min.js +8 -0
- package/dist/build/fw.pack.crypto-advanced.pure.meta.json +38 -0
- package/dist/build/fw.pack.crypto-advanced.pure.min.js +6 -0
- package/dist/build/fw.pack.crypto-basic.pure.meta.json +29 -0
- package/dist/build/fw.pack.crypto-basic.pure.min.js +6 -0
- package/dist/build/fw.pack.crypto-identity.pure.meta.json +29 -0
- package/dist/build/fw.pack.crypto-identity.pure.min.js +9 -0
- package/dist/build/fw.pack.realtime.pure.meta.json +21 -0
- package/dist/build/fw.pack.realtime.pure.min.js +6 -0
- package/dist/build/fw.pack.sensors.pure.meta.json +21 -0
- package/dist/build/fw.pack.sensors.pure.min.js +6 -0
- package/dist/build/fw.pack.structures-advanced.pure.meta.json +20 -0
- package/dist/build/fw.pack.structures-advanced.pure.min.js +6 -0
- package/dist/build/fw.pack.text-advanced.pure.meta.json +20 -0
- package/dist/build/fw.pack.text-advanced.pure.min.js +6 -0
- package/dist/build/fw.pwa.pure.meta.json +127 -0
- package/dist/build/fw.pwa.pure.min.js +55 -0
- package/dist/build/fw.site-interactive.pure.meta.json +81 -0
- package/dist/build/fw.site-interactive.pure.min.js +52 -0
- package/dist/build/fw.site.pure.meta.json +51 -0
- package/dist/build/fw.site.pure.min.js +35 -0
- package/dist/build/fw.spa.pure.meta.json +108 -0
- package/dist/build/fw.spa.pure.min.js +55 -0
- package/dist/build/io/compress/brotli_dict.bin +432 -0
- package/dist/build/sanity-base-classic.min.js +6 -0
- package/dist/build/sanity-community-classic.min.js +6 -0
- package/dist/build/sanity.min.js +6 -0
- package/dist/types/core/logger.d.ts +71 -0
- package/dist/types/core/logger.d.ts.map +1 -0
- package/dist/types/core/modules.d.ts +3962 -0
- package/dist/types/core/modules.d.ts.map +1 -0
- package/dist/types/core/readyState.d.ts +14 -0
- package/dist/types/core/readyState.d.ts.map +1 -0
- package/dist/types/core/runtime.d.ts +625 -0
- package/dist/types/core/runtime.d.ts.map +1 -0
- package/dist/types/core/worker-helper.d.ts +153 -0
- package/dist/types/core/worker-helper.d.ts.map +1 -0
- package/dist/types/crypto/cipher/aes.d.ts +55 -0
- package/dist/types/crypto/cipher/aes.d.ts.map +1 -0
- package/dist/types/crypto/cipher/chacha20.d.ts +33 -0
- package/dist/types/crypto/cipher/chacha20.d.ts.map +1 -0
- package/dist/types/crypto/hash/adf.d.ts +65 -0
- package/dist/types/crypto/hash/adf.d.ts.map +1 -0
- package/dist/types/crypto/hash/argon2.d.ts +79 -0
- package/dist/types/crypto/hash/argon2.d.ts.map +1 -0
- package/dist/types/crypto/hash/blake2b.d.ts +40 -0
- package/dist/types/crypto/hash/blake2b.d.ts.map +1 -0
- package/dist/types/crypto/hash/hkdf.d.ts +88 -0
- package/dist/types/crypto/hash/hkdf.d.ts.map +1 -0
- package/dist/types/crypto/hash/hmac.d.ts +85 -0
- package/dist/types/crypto/hash/hmac.d.ts.map +1 -0
- package/dist/types/crypto/hash/pbkdf2.d.ts +80 -0
- package/dist/types/crypto/hash/pbkdf2.d.ts.map +1 -0
- package/dist/types/crypto/hash/poly1305.d.ts +31 -0
- package/dist/types/crypto/hash/poly1305.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha224.d.ts +80 -0
- package/dist/types/crypto/hash/sha224.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha256.d.ts +82 -0
- package/dist/types/crypto/hash/sha256.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha3.d.ts +111 -0
- package/dist/types/crypto/hash/sha3.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha384.d.ts +84 -0
- package/dist/types/crypto/hash/sha384.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha512.d.ts +87 -0
- package/dist/types/crypto/hash/sha512.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha512_224.d.ts +84 -0
- package/dist/types/crypto/hash/sha512_224.d.ts.map +1 -0
- package/dist/types/crypto/hash/sha512_256.d.ts +84 -0
- package/dist/types/crypto/hash/sha512_256.d.ts.map +1 -0
- package/dist/types/crypto/mode/cbc.d.ts +38 -0
- package/dist/types/crypto/mode/cbc.d.ts.map +1 -0
- package/dist/types/crypto/mode/chacha20poly1305.d.ts +71 -0
- package/dist/types/crypto/mode/chacha20poly1305.d.ts.map +1 -0
- package/dist/types/crypto/mode/cmac.d.ts +56 -0
- package/dist/types/crypto/mode/cmac.d.ts.map +1 -0
- package/dist/types/crypto/mode/ctr.d.ts +52 -0
- package/dist/types/crypto/mode/ctr.d.ts.map +1 -0
- package/dist/types/crypto/mode/gcm.d.ts +88 -0
- package/dist/types/crypto/mode/gcm.d.ts.map +1 -0
- package/dist/types/crypto/mode/kw.d.ts +60 -0
- package/dist/types/crypto/mode/kw.d.ts.map +1 -0
- package/dist/types/crypto/pkc/ecc.d.ts +284 -0
- package/dist/types/crypto/pkc/ecc.d.ts.map +1 -0
- package/dist/types/crypto/pkc/ed25519.d.ts +107 -0
- package/dist/types/crypto/pkc/ed25519.d.ts.map +1 -0
- package/dist/types/crypto/pkc/hybridKem.d.ts +154 -0
- package/dist/types/crypto/pkc/hybridKem.d.ts.map +1 -0
- package/dist/types/crypto/pkc/hybridSign.d.ts +383 -0
- package/dist/types/crypto/pkc/hybridSign.d.ts.map +1 -0
- package/dist/types/crypto/pkc/ml_dsa.d.ts +174 -0
- package/dist/types/crypto/pkc/ml_dsa.d.ts.map +1 -0
- package/dist/types/crypto/pkc/ml_kem.d.ts +194 -0
- package/dist/types/crypto/pkc/ml_kem.d.ts.map +1 -0
- package/dist/types/crypto/pkc/rsa.d.ts +175 -0
- package/dist/types/crypto/pkc/rsa.d.ts.map +1 -0
- package/dist/types/crypto/pkc/slh_dsa.d.ts +232 -0
- package/dist/types/crypto/pkc/slh_dsa.d.ts.map +1 -0
- package/dist/types/crypto/pkc/x25519.d.ts +37 -0
- package/dist/types/crypto/pkc/x25519.d.ts.map +1 -0
- package/dist/types/crypto/utils/aes_ctr.d.ts +84 -0
- package/dist/types/crypto/utils/aes_ctr.d.ts.map +1 -0
- package/dist/types/crypto/utils/aes_modes.d.ts +128 -0
- package/dist/types/crypto/utils/aes_modes.d.ts.map +1 -0
- package/dist/types/crypto/utils/asn1-oid.d.ts +45 -0
- package/dist/types/crypto/utils/asn1-oid.d.ts.map +1 -0
- package/dist/types/crypto/utils/asn1.d.ts +115 -0
- package/dist/types/crypto/utils/asn1.d.ts.map +1 -0
- package/dist/types/crypto/utils/bitArray.d.ts +75 -0
- package/dist/types/crypto/utils/bitArray.d.ts.map +1 -0
- package/dist/types/crypto/utils/bn.d.ts +214 -0
- package/dist/types/crypto/utils/bn.d.ts.map +1 -0
- package/dist/types/crypto/utils/jws.d.ts +159 -0
- package/dist/types/crypto/utils/jws.d.ts.map +1 -0
- package/dist/types/crypto/utils/keyformat.d.ts +100 -0
- package/dist/types/crypto/utils/keyformat.d.ts.map +1 -0
- package/dist/types/crypto/utils/pad.d.ts +44 -0
- package/dist/types/crypto/utils/pad.d.ts.map +1 -0
- package/dist/types/crypto/utils/pem.d.ts +41 -0
- package/dist/types/crypto/utils/pem.d.ts.map +1 -0
- package/dist/types/crypto/utils/random.d.ts +203 -0
- package/dist/types/crypto/utils/random.d.ts.map +1 -0
- package/dist/types/crypto/utils/rsaKeygen.d.ts +162 -0
- package/dist/types/crypto/utils/rsaKeygen.d.ts.map +1 -0
- package/dist/types/crypto/utils/totp.d.ts +183 -0
- package/dist/types/crypto/utils/totp.d.ts.map +1 -0
- package/dist/types/crypto/utils/uuid.d.ts +29 -0
- package/dist/types/crypto/utils/uuid.d.ts.map +1 -0
- package/dist/types/crypto/wasm/aes.d.ts +59 -0
- package/dist/types/crypto/wasm/aes.d.ts.map +1 -0
- package/dist/types/crypto/wasm/argon2.d.ts +70 -0
- package/dist/types/crypto/wasm/argon2.d.ts.map +1 -0
- package/dist/types/crypto/wasm/blake2b.d.ts +59 -0
- package/dist/types/crypto/wasm/blake2b.d.ts.map +1 -0
- package/dist/types/crypto/wasm/chacha20poly1305.d.ts +43 -0
- package/dist/types/crypto/wasm/chacha20poly1305.d.ts.map +1 -0
- package/dist/types/crypto/wasm/cmac.d.ts +42 -0
- package/dist/types/crypto/wasm/cmac.d.ts.map +1 -0
- package/dist/types/crypto/wasm/ecc.d.ts +56 -0
- package/dist/types/crypto/wasm/ecc.d.ts.map +1 -0
- package/dist/types/crypto/wasm/ed25519.d.ts +52 -0
- package/dist/types/crypto/wasm/ed25519.d.ts.map +1 -0
- package/dist/types/crypto/wasm/hkdf.d.ts +40 -0
- package/dist/types/crypto/wasm/hkdf.d.ts.map +1 -0
- package/dist/types/crypto/wasm/hmac.d.ts +42 -0
- package/dist/types/crypto/wasm/hmac.d.ts.map +1 -0
- package/dist/types/crypto/wasm/ml_dsa.d.ts +48 -0
- package/dist/types/crypto/wasm/ml_dsa.d.ts.map +1 -0
- package/dist/types/crypto/wasm/ml_kem.d.ts +58 -0
- package/dist/types/crypto/wasm/ml_kem.d.ts.map +1 -0
- package/dist/types/crypto/wasm/pbkdf2.d.ts +38 -0
- package/dist/types/crypto/wasm/pbkdf2.d.ts.map +1 -0
- package/dist/types/crypto/wasm/rsa.d.ts +56 -0
- package/dist/types/crypto/wasm/rsa.d.ts.map +1 -0
- package/dist/types/crypto/wasm/runtime.d.ts +99 -0
- package/dist/types/crypto/wasm/runtime.d.ts.map +1 -0
- package/dist/types/crypto/wasm/sha2.d.ts +45 -0
- package/dist/types/crypto/wasm/sha2.d.ts.map +1 -0
- package/dist/types/crypto/wasm/sha3.d.ts +57 -0
- package/dist/types/crypto/wasm/sha3.d.ts.map +1 -0
- package/dist/types/crypto/wasm/slh_dsa.d.ts +62 -0
- package/dist/types/crypto/wasm/slh_dsa.d.ts.map +1 -0
- package/dist/types/crypto/wasm/x25519.d.ts +53 -0
- package/dist/types/crypto/wasm/x25519.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/aes.d.ts +59 -0
- package/dist/types/crypto/webcrypto/aes.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/aeskw.d.ts +40 -0
- package/dist/types/crypto/webcrypto/aeskw.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/digest.d.ts +43 -0
- package/dist/types/crypto/webcrypto/digest.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/ecc.d.ts +51 -0
- package/dist/types/crypto/webcrypto/ecc.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/ed25519.d.ts +43 -0
- package/dist/types/crypto/webcrypto/ed25519.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/hkdf.d.ts +18 -0
- package/dist/types/crypto/webcrypto/hkdf.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/hmac.d.ts +43 -0
- package/dist/types/crypto/webcrypto/hmac.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/pbkdf2.d.ts +18 -0
- package/dist/types/crypto/webcrypto/pbkdf2.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/rsa.d.ts +51 -0
- package/dist/types/crypto/webcrypto/rsa.d.ts.map +1 -0
- package/dist/types/crypto/webcrypto/x25519.d.ts +43 -0
- package/dist/types/crypto/webcrypto/x25519.d.ts.map +1 -0
- package/dist/types/dom/display/animate.d.ts +35 -0
- package/dist/types/dom/display/animate.d.ts.map +1 -0
- package/dist/types/dom/display/fullscreen.d.ts +59 -0
- package/dist/types/dom/display/fullscreen.d.ts.map +1 -0
- package/dist/types/dom/fs/download.d.ts +36 -0
- package/dist/types/dom/fs/download.d.ts.map +1 -0
- package/dist/types/dom/fs/fsAccess.d.ts +107 -0
- package/dist/types/dom/fs/fsAccess.d.ts.map +1 -0
- package/dist/types/dom/fs/indexedDB.d.ts +62 -0
- package/dist/types/dom/fs/indexedDB.d.ts.map +1 -0
- package/dist/types/dom/fs/remoteStore.d.ts +64 -0
- package/dist/types/dom/fs/remoteStore.d.ts.map +1 -0
- package/dist/types/dom/fs/storage.d.ts +56 -0
- package/dist/types/dom/fs/storage.d.ts.map +1 -0
- package/dist/types/dom/lifecycle/idle.d.ts +91 -0
- package/dist/types/dom/lifecycle/idle.d.ts.map +1 -0
- package/dist/types/dom/lifecycle/visibility.d.ts +49 -0
- package/dist/types/dom/lifecycle/visibility.d.ts.map +1 -0
- package/dist/types/dom/lifecycle/wakeLock.d.ts +69 -0
- package/dist/types/dom/lifecycle/wakeLock.d.ts.map +1 -0
- package/dist/types/dom/net/ajax.d.ts +27 -0
- package/dist/types/dom/net/ajax.d.ts.map +1 -0
- package/dist/types/dom/net/broadcastChannel.d.ts +51 -0
- package/dist/types/dom/net/broadcastChannel.d.ts.map +1 -0
- package/dist/types/dom/net/network.d.ts +80 -0
- package/dist/types/dom/net/network.d.ts.map +1 -0
- package/dist/types/dom/net/sse.d.ts +72 -0
- package/dist/types/dom/net/sse.d.ts.map +1 -0
- package/dist/types/dom/net/webrtc.d.ts +149 -0
- package/dist/types/dom/net/webrtc.d.ts.map +1 -0
- package/dist/types/dom/net/ws.d.ts +70 -0
- package/dist/types/dom/net/ws.d.ts.map +1 -0
- package/dist/types/dom/query/dnd.d.ts +86 -0
- package/dist/types/dom/query/dnd.d.ts.map +1 -0
- package/dist/types/dom/query/dom.d.ts +194 -0
- package/dist/types/dom/query/dom.d.ts.map +1 -0
- package/dist/types/dom/query/events.d.ts +135 -0
- package/dist/types/dom/query/events.d.ts.map +1 -0
- package/dist/types/dom/query/gesture.d.ts +59 -0
- package/dist/types/dom/query/gesture.d.ts.map +1 -0
- package/dist/types/dom/query/media.d.ts +156 -0
- package/dist/types/dom/query/media.d.ts.map +1 -0
- package/dist/types/dom/rendering/chart.d.ts +143 -0
- package/dist/types/dom/rendering/chart.d.ts.map +1 -0
- package/dist/types/dom/rendering/component.d.ts +92 -0
- package/dist/types/dom/rendering/component.d.ts.map +1 -0
- package/dist/types/dom/rendering/devtools-ui.d.ts +83 -0
- package/dist/types/dom/rendering/devtools-ui.d.ts.map +1 -0
- package/dist/types/dom/rendering/devtools.d.ts +68 -0
- package/dist/types/dom/rendering/devtools.d.ts.map +1 -0
- package/dist/types/dom/rendering/parser.d.ts +216 -0
- package/dist/types/dom/rendering/parser.d.ts.map +1 -0
- package/dist/types/dom/rendering/reactiveBind.d.ts +101 -0
- package/dist/types/dom/rendering/reactiveBind.d.ts.map +1 -0
- package/dist/types/dom/rendering/render.d.ts +121 -0
- package/dist/types/dom/rendering/render.d.ts.map +1 -0
- package/dist/types/dom/rendering/sanitize.d.ts +48 -0
- package/dist/types/dom/rendering/sanitize.d.ts.map +1 -0
- package/dist/types/dom/rendering/secPolicy.d.ts +89 -0
- package/dist/types/dom/rendering/secPolicy.d.ts.map +1 -0
- package/dist/types/dom/rendering/template.d.ts +251 -0
- package/dist/types/dom/rendering/template.d.ts.map +1 -0
- package/dist/types/dom/rendering/themeTokens.d.ts +61 -0
- package/dist/types/dom/rendering/themeTokens.d.ts.map +1 -0
- package/dist/types/dom/rendering/uiSession-core.d.ts +192 -0
- package/dist/types/dom/rendering/uiSession-core.d.ts.map +1 -0
- package/dist/types/dom/rendering/uiSession-direct.d.ts +63 -0
- package/dist/types/dom/rendering/uiSession-direct.d.ts.map +1 -0
- package/dist/types/dom/rendering/uiSession-list.d.ts +205 -0
- package/dist/types/dom/rendering/uiSession-list.d.ts.map +1 -0
- package/dist/types/dom/rendering/uiSession.d.ts +285 -0
- package/dist/types/dom/rendering/uiSession.d.ts.map +1 -0
- package/dist/types/dom/rendering/virtualScroll.d.ts +80 -0
- package/dist/types/dom/rendering/virtualScroll.d.ts.map +1 -0
- package/dist/types/dom/sensors/battery.d.ts +71 -0
- package/dist/types/dom/sensors/battery.d.ts.map +1 -0
- package/dist/types/dom/sensors/geolocation.d.ts +83 -0
- package/dist/types/dom/sensors/geolocation.d.ts.map +1 -0
- package/dist/types/dom/sensors/networkInfo.d.ts +82 -0
- package/dist/types/dom/sensors/networkInfo.d.ts.map +1 -0
- package/dist/types/dom/sensors/sensors.d.ts +191 -0
- package/dist/types/dom/sensors/sensors.d.ts.map +1 -0
- package/dist/types/dom/sw/backgroundSync.d.ts +48 -0
- package/dist/types/dom/sw/backgroundSync.d.ts.map +1 -0
- package/dist/types/dom/sw/cache.d.ts +71 -0
- package/dist/types/dom/sw/cache.d.ts.map +1 -0
- package/dist/types/dom/sw/push.d.ts +77 -0
- package/dist/types/dom/sw/push.d.ts.map +1 -0
- package/dist/types/dom/sw/serviceWorker.d.ts +57 -0
- package/dist/types/dom/sw/serviceWorker.d.ts.map +1 -0
- package/dist/types/dom/sw/sharedWorker.d.ts +81 -0
- package/dist/types/dom/sw/sharedWorker.d.ts.map +1 -0
- package/dist/types/dom/utils/a11y.d.ts +53 -0
- package/dist/types/dom/utils/a11y.d.ts.map +1 -0
- package/dist/types/dom/utils/clipboard.d.ts +44 -0
- package/dist/types/dom/utils/clipboard.d.ts.map +1 -0
- package/dist/types/dom/utils/entropyCollector.d.ts +58 -0
- package/dist/types/dom/utils/entropyCollector.d.ts.map +1 -0
- package/dist/types/dom/utils/focus.d.ts +123 -0
- package/dist/types/dom/utils/focus.d.ts.map +1 -0
- package/dist/types/dom/utils/form.d.ts +227 -0
- package/dist/types/dom/utils/form.d.ts.map +1 -0
- package/dist/types/dom/utils/formKit.d.ts +206 -0
- package/dist/types/dom/utils/formKit.d.ts.map +1 -0
- package/dist/types/dom/utils/keybindings.d.ts +53 -0
- package/dist/types/dom/utils/keybindings.d.ts.map +1 -0
- package/dist/types/dom/utils/leaderElection.d.ts +70 -0
- package/dist/types/dom/utils/leaderElection.d.ts.map +1 -0
- package/dist/types/dom/utils/notifications.d.ts +164 -0
- package/dist/types/dom/utils/notifications.d.ts.map +1 -0
- package/dist/types/dom/utils/permissions.d.ts +35 -0
- package/dist/types/dom/utils/permissions.d.ts.map +1 -0
- package/dist/types/dom/utils/route.d.ts +117 -0
- package/dist/types/dom/utils/route.d.ts.map +1 -0
- package/dist/types/dom/utils/ua.d.ts +35 -0
- package/dist/types/dom/utils/ua.d.ts.map +1 -0
- package/dist/types/dom/utils/webauthn.d.ts +98 -0
- package/dist/types/dom/utils/webauthn.d.ts.map +1 -0
- package/dist/types/io/binary/reader.d.ts +59 -0
- package/dist/types/io/binary/reader.d.ts.map +1 -0
- package/dist/types/io/binary/writer.d.ts +56 -0
- package/dist/types/io/binary/writer.d.ts.map +1 -0
- package/dist/types/io/calc/adler32.d.ts +31 -0
- package/dist/types/io/calc/adler32.d.ts.map +1 -0
- package/dist/types/io/calc/bigint.d.ts +74 -0
- package/dist/types/io/calc/bigint.d.ts.map +1 -0
- package/dist/types/io/calc/crc32.d.ts +36 -0
- package/dist/types/io/calc/crc32.d.ts.map +1 -0
- package/dist/types/io/calc/easing.d.ts +60 -0
- package/dist/types/io/calc/easing.d.ts.map +1 -0
- package/dist/types/io/codec/b64.d.ts +28 -0
- package/dist/types/io/codec/b64.d.ts.map +1 -0
- package/dist/types/io/codec/base32.d.ts +28 -0
- package/dist/types/io/codec/base32.d.ts.map +1 -0
- package/dist/types/io/codec/base58.d.ts +28 -0
- package/dist/types/io/codec/base58.d.ts.map +1 -0
- package/dist/types/io/codec/buffer.d.ts +66 -0
- package/dist/types/io/codec/buffer.d.ts.map +1 -0
- package/dist/types/io/codec/cbor.d.ts +43 -0
- package/dist/types/io/codec/cbor.d.ts.map +1 -0
- package/dist/types/io/codec/csv.d.ts +35 -0
- package/dist/types/io/codec/csv.d.ts.map +1 -0
- package/dist/types/io/codec/hex.d.ts +28 -0
- package/dist/types/io/codec/hex.d.ts.map +1 -0
- package/dist/types/io/codec/mime.d.ts +100 -0
- package/dist/types/io/codec/mime.d.ts.map +1 -0
- package/dist/types/io/codec/msgpack.d.ts +36 -0
- package/dist/types/io/codec/msgpack.d.ts.map +1 -0
- package/dist/types/io/codec/url.d.ts +52 -0
- package/dist/types/io/codec/url.d.ts.map +1 -0
- package/dist/types/io/codec/utf8.d.ts +22 -0
- package/dist/types/io/codec/utf8.d.ts.map +1 -0
- package/dist/types/io/codec/xml.d.ts +91 -0
- package/dist/types/io/codec/xml.d.ts.map +1 -0
- package/dist/types/io/compress/bitstream.d.ts +51 -0
- package/dist/types/io/compress/bitstream.d.ts.map +1 -0
- package/dist/types/io/compress/brotli.d.ts +86 -0
- package/dist/types/io/compress/brotli.d.ts.map +1 -0
- package/dist/types/io/compress/brotli_dict.d.ts +76 -0
- package/dist/types/io/compress/brotli_dict.d.ts.map +1 -0
- package/dist/types/io/compress/brotli_dict_words.d.ts +43 -0
- package/dist/types/io/compress/brotli_dict_words.d.ts.map +1 -0
- package/dist/types/io/compress/brotli_frame.d.ts +166 -0
- package/dist/types/io/compress/brotli_frame.d.ts.map +1 -0
- package/dist/types/io/compress/brotli_shared.d.ts +165 -0
- package/dist/types/io/compress/brotli_shared.d.ts.map +1 -0
- package/dist/types/io/compress/deflate.d.ts +89 -0
- package/dist/types/io/compress/deflate.d.ts.map +1 -0
- package/dist/types/io/compress/gzip.d.ts +100 -0
- package/dist/types/io/compress/gzip.d.ts.map +1 -0
- package/dist/types/io/compress/huffman.d.ts +37 -0
- package/dist/types/io/compress/huffman.d.ts.map +1 -0
- package/dist/types/io/compress/lz4.d.ts +45 -0
- package/dist/types/io/compress/lz4.d.ts.map +1 -0
- package/dist/types/io/compress/lz77.d.ts +66 -0
- package/dist/types/io/compress/lz77.d.ts.map +1 -0
- package/dist/types/io/compress/lzw.d.ts +37 -0
- package/dist/types/io/compress/lzw.d.ts.map +1 -0
- package/dist/types/io/compress/zip.d.ts +112 -0
- package/dist/types/io/compress/zip.d.ts.map +1 -0
- package/dist/types/io/compress/zlib.d.ts +97 -0
- package/dist/types/io/compress/zlib.d.ts.map +1 -0
- package/dist/types/io/i18n/i18n.d.ts +64 -0
- package/dist/types/io/i18n/i18n.d.ts.map +1 -0
- package/dist/types/io/math/fixed-point.d.ts +30 -0
- package/dist/types/io/math/fixed-point.d.ts.map +1 -0
- package/dist/types/io/math/geom.d.ts +125 -0
- package/dist/types/io/math/geom.d.ts.map +1 -0
- package/dist/types/io/math/interp.d.ts +48 -0
- package/dist/types/io/math/interp.d.ts.map +1 -0
- package/dist/types/io/math/linalg.d.ts +117 -0
- package/dist/types/io/math/linalg.d.ts.map +1 -0
- package/dist/types/io/math/stats.d.ts +45 -0
- package/dist/types/io/math/stats.d.ts.map +1 -0
- package/dist/types/io/structures/btree.d.ts +84 -0
- package/dist/types/io/structures/btree.d.ts.map +1 -0
- package/dist/types/io/structures/heap.d.ts +75 -0
- package/dist/types/io/structures/heap.d.ts.map +1 -0
- package/dist/types/io/structures/lruCache.d.ts +88 -0
- package/dist/types/io/structures/lruCache.d.ts.map +1 -0
- package/dist/types/io/structures/ringBuffer.d.ts +82 -0
- package/dist/types/io/structures/ringBuffer.d.ts.map +1 -0
- package/dist/types/io/structures/tree-walker.d.ts +95 -0
- package/dist/types/io/structures/tree-walker.d.ts.map +1 -0
- package/dist/types/io/structures/trie.d.ts +71 -0
- package/dist/types/io/structures/trie.d.ts.map +1 -0
- package/dist/types/io/sync/abort.d.ts +59 -0
- package/dist/types/io/sync/abort.d.ts.map +1 -0
- package/dist/types/io/sync/atomics.d.ts +107 -0
- package/dist/types/io/sync/atomics.d.ts.map +1 -0
- package/dist/types/io/sync/cancellable.d.ts +69 -0
- package/dist/types/io/sync/cancellable.d.ts.map +1 -0
- package/dist/types/io/sync/channel.d.ts +70 -0
- package/dist/types/io/sync/channel.d.ts.map +1 -0
- package/dist/types/io/sync/mutex.d.ts +39 -0
- package/dist/types/io/sync/mutex.d.ts.map +1 -0
- package/dist/types/io/sync/semaphore.d.ts +40 -0
- package/dist/types/io/sync/semaphore.d.ts.map +1 -0
- package/dist/types/io/sync/tokenBucket.d.ts +53 -0
- package/dist/types/io/sync/tokenBucket.d.ts.map +1 -0
- package/dist/types/io/text/ansi.d.ts +125 -0
- package/dist/types/io/text/ansi.d.ts.map +1 -0
- package/dist/types/io/text/html-entities.d.ts +35 -0
- package/dist/types/io/text/html-entities.d.ts.map +1 -0
- package/dist/types/io/text/semver.d.ts +75 -0
- package/dist/types/io/text/semver.d.ts.map +1 -0
- package/dist/types/io/text/str.d.ts +88 -0
- package/dist/types/io/text/str.d.ts.map +1 -0
- package/dist/types/io/text/unicode.d.ts +70 -0
- package/dist/types/io/text/unicode.d.ts.map +1 -0
- package/dist/types/io/time/date.d.ts +73 -0
- package/dist/types/io/time/date.d.ts.map +1 -0
- package/dist/types/io/timing/clock.d.ts +39 -0
- package/dist/types/io/timing/clock.d.ts.map +1 -0
- package/dist/types/io/timing/rateLimit.d.ts +40 -0
- package/dist/types/io/timing/rateLimit.d.ts.map +1 -0
- package/dist/types/io/timing/scheduler.d.ts +122 -0
- package/dist/types/io/timing/scheduler.d.ts.map +1 -0
- package/dist/types/io/utils/bitmap.d.ts +34 -0
- package/dist/types/io/utils/bitmap.d.ts.map +1 -0
- package/dist/types/io/utils/errors.d.ts +94 -0
- package/dist/types/io/utils/errors.d.ts.map +1 -0
- package/dist/types/io/utils/eventBus.d.ts +68 -0
- package/dist/types/io/utils/eventBus.d.ts.map +1 -0
- package/dist/types/io/utils/queue.d.ts +69 -0
- package/dist/types/io/utils/queue.d.ts.map +1 -0
- package/dist/types/io/utils/signal.d.ts +362 -0
- package/dist/types/io/utils/signal.d.ts.map +1 -0
- package/dist/types/io/utils/ui8.d.ts +34 -0
- package/dist/types/io/utils/ui8.d.ts.map +1 -0
- package/dist/types/io/utils/valid.d.ts +130 -0
- package/dist/types/io/utils/valid.d.ts.map +1 -0
- package/dist/types/main.d.ts +25 -0
- package/dist/types/main.d.ts.map +1 -0
- package/dist/types/process/message.d.ts +64 -0
- package/dist/types/process/message.d.ts.map +1 -0
- package/dist/types/process/rpc.d.ts +35 -0
- package/dist/types/process/rpc.d.ts.map +1 -0
- package/dist/types/process/workerPool.d.ts +62 -0
- package/dist/types/process/workerPool.d.ts.map +1 -0
- package/dist/types/sanity/base.d.ts +28 -0
- package/dist/types/sanity/base.d.ts.map +1 -0
- package/dist/types/sanity/community.d.ts +20 -0
- package/dist/types/sanity/community.d.ts.map +1 -0
- package/dist/types/sanity/lockdown-apply.d.ts +7 -0
- package/dist/types/sanity/lockdown-apply.d.ts.map +1 -0
- package/dist/types/sanity/lockdown.d.ts +43 -0
- package/dist/types/sanity/lockdown.d.ts.map +1 -0
- package/dist/types/sanity/wasm-gate.d.ts +128 -0
- package/dist/types/sanity/wasm-gate.d.ts.map +1 -0
- package/docs/README.md +301 -0
- package/docs/api/README.md +33 -0
- package/docs/api/core/README.md +14 -0
- package/docs/api/core/logger.md +151 -0
- package/docs/api/core/main.md +105 -0
- package/docs/api/core/modules.md +123 -0
- package/docs/api/core/readyState.md +94 -0
- package/docs/api/core/runtime.md +499 -0
- package/docs/api/core/worker-helper.md +180 -0
- package/docs/api/crypto/README.md +39 -0
- package/docs/api/crypto/cipher/README.md +24 -0
- package/docs/api/crypto/cipher/aes.md +101 -0
- package/docs/api/crypto/cipher/chacha20.md +85 -0
- package/docs/api/crypto/hash/README.md +49 -0
- package/docs/api/crypto/hash/adf.md +61 -0
- package/docs/api/crypto/hash/argon2.md +92 -0
- package/docs/api/crypto/hash/blake2b.md +88 -0
- package/docs/api/crypto/hash/hkdf.md +82 -0
- package/docs/api/crypto/hash/hmac.md +90 -0
- package/docs/api/crypto/hash/pbkdf2.md +93 -0
- package/docs/api/crypto/hash/poly1305.md +71 -0
- package/docs/api/crypto/hash/sha224.md +61 -0
- package/docs/api/crypto/hash/sha256.md +84 -0
- package/docs/api/crypto/hash/sha3.md +89 -0
- package/docs/api/crypto/hash/sha384.md +58 -0
- package/docs/api/crypto/hash/sha512.md +73 -0
- package/docs/api/crypto/hash/sha512_224.md +59 -0
- package/docs/api/crypto/hash/sha512_256.md +58 -0
- package/docs/api/crypto/mode/README.md +33 -0
- package/docs/api/crypto/mode/cbc.md +69 -0
- package/docs/api/crypto/mode/chacha20poly1305.md +77 -0
- package/docs/api/crypto/mode/cmac.md +79 -0
- package/docs/api/crypto/mode/ctr.md +66 -0
- package/docs/api/crypto/mode/gcm.md +93 -0
- package/docs/api/crypto/mode/kw.md +84 -0
- package/docs/api/crypto/pkc/README.md +48 -0
- package/docs/api/crypto/pkc/ecc.md +91 -0
- package/docs/api/crypto/pkc/ed25519.md +99 -0
- package/docs/api/crypto/pkc/hybridKem.md +178 -0
- package/docs/api/crypto/pkc/hybridSign.md +192 -0
- package/docs/api/crypto/pkc/ml_dsa.md +99 -0
- package/docs/api/crypto/pkc/ml_kem.md +90 -0
- package/docs/api/crypto/pkc/rsa.md +87 -0
- package/docs/api/crypto/pkc/slh_dsa.md +86 -0
- package/docs/api/crypto/pkc/x25519.md +84 -0
- package/docs/api/crypto/utils/README.md +39 -0
- package/docs/api/crypto/utils/aes_ctr.md +71 -0
- package/docs/api/crypto/utils/aes_modes.md +81 -0
- package/docs/api/crypto/utils/asn1-oid.md +130 -0
- package/docs/api/crypto/utils/asn1.md +69 -0
- package/docs/api/crypto/utils/bitArray.md +77 -0
- package/docs/api/crypto/utils/bn.md +67 -0
- package/docs/api/crypto/utils/jws.md +94 -0
- package/docs/api/crypto/utils/keyformat.md +78 -0
- package/docs/api/crypto/utils/pad.md +66 -0
- package/docs/api/crypto/utils/pem.md +63 -0
- package/docs/api/crypto/utils/random.md +103 -0
- package/docs/api/crypto/utils/rsaKeygen.md +118 -0
- package/docs/api/crypto/utils/totp.md +159 -0
- package/docs/api/crypto/utils/uuid.md +98 -0
- package/docs/api/crypto/wasm/README.md +76 -0
- package/docs/api/crypto/wasm/aes.md +128 -0
- package/docs/api/crypto/wasm/argon2.md +110 -0
- package/docs/api/crypto/wasm/blake2b.md +103 -0
- package/docs/api/crypto/wasm/chacha20poly1305.md +120 -0
- package/docs/api/crypto/wasm/cmac.md +119 -0
- package/docs/api/crypto/wasm/ecc.md +114 -0
- package/docs/api/crypto/wasm/ed25519.md +117 -0
- package/docs/api/crypto/wasm/hkdf.md +163 -0
- package/docs/api/crypto/wasm/hmac.md +129 -0
- package/docs/api/crypto/wasm/ml_dsa.md +99 -0
- package/docs/api/crypto/wasm/ml_kem.md +91 -0
- package/docs/api/crypto/wasm/pbkdf2.md +111 -0
- package/docs/api/crypto/wasm/rsa.md +113 -0
- package/docs/api/crypto/wasm/runtime.md +172 -0
- package/docs/api/crypto/wasm/sha2.md +95 -0
- package/docs/api/crypto/wasm/sha3.md +127 -0
- package/docs/api/crypto/wasm/slh_dsa.md +89 -0
- package/docs/api/crypto/wasm/x25519.md +116 -0
- package/docs/api/crypto/webcrypto/README.md +56 -0
- package/docs/api/crypto/webcrypto/aes.md +140 -0
- package/docs/api/crypto/webcrypto/aeskw.md +120 -0
- package/docs/api/crypto/webcrypto/digest.md +103 -0
- package/docs/api/crypto/webcrypto/ecc.md +122 -0
- package/docs/api/crypto/webcrypto/ed25519.md +121 -0
- package/docs/api/crypto/webcrypto/hkdf.md +141 -0
- package/docs/api/crypto/webcrypto/hmac.md +112 -0
- package/docs/api/crypto/webcrypto/pbkdf2.md +140 -0
- package/docs/api/crypto/webcrypto/rsa.md +149 -0
- package/docs/api/crypto/webcrypto/x25519.md +96 -0
- package/docs/api/dom/README.md +117 -0
- package/docs/api/dom/display/README.md +27 -0
- package/docs/api/dom/display/animate.md +113 -0
- package/docs/api/dom/display/fullscreen.md +121 -0
- package/docs/api/dom/fs/README.md +36 -0
- package/docs/api/dom/fs/download.md +108 -0
- package/docs/api/dom/fs/fsAccess.md +200 -0
- package/docs/api/dom/fs/indexedDB.md +219 -0
- package/docs/api/dom/fs/remoteStore.md +158 -0
- package/docs/api/dom/fs/storage.md +197 -0
- package/docs/api/dom/lifecycle/README.md +11 -0
- package/docs/api/dom/lifecycle/idle.md +89 -0
- package/docs/api/dom/lifecycle/visibility.md +73 -0
- package/docs/api/dom/lifecycle/wakeLock.md +94 -0
- package/docs/api/dom/net/README.md +31 -0
- package/docs/api/dom/net/ajax.md +176 -0
- package/docs/api/dom/net/broadcastChannel.md +110 -0
- package/docs/api/dom/net/network.md +137 -0
- package/docs/api/dom/net/sse.md +141 -0
- package/docs/api/dom/net/webrtc.md +158 -0
- package/docs/api/dom/net/ws.md +168 -0
- package/docs/api/dom/query/README.md +30 -0
- package/docs/api/dom/query/dnd.md +137 -0
- package/docs/api/dom/query/dom.md +275 -0
- package/docs/api/dom/query/events.md +211 -0
- package/docs/api/dom/query/gesture.md +137 -0
- package/docs/api/dom/query/media.md +188 -0
- package/docs/api/dom/rendering/README.md +41 -0
- package/docs/api/dom/rendering/chart.md +149 -0
- package/docs/api/dom/rendering/component.md +287 -0
- package/docs/api/dom/rendering/devtools-ui.md +129 -0
- package/docs/api/dom/rendering/devtools.md +157 -0
- package/docs/api/dom/rendering/parser.md +200 -0
- package/docs/api/dom/rendering/reactiveBind.md +234 -0
- package/docs/api/dom/rendering/render.md +219 -0
- package/docs/api/dom/rendering/sanitize.md +97 -0
- package/docs/api/dom/rendering/secPolicy.md +155 -0
- package/docs/api/dom/rendering/template.md +301 -0
- package/docs/api/dom/rendering/themeTokens.md +175 -0
- package/docs/api/dom/rendering/uiSession.md +546 -0
- package/docs/api/dom/rendering/virtualScroll.md +126 -0
- package/docs/api/dom/sensors/README.md +12 -0
- package/docs/api/dom/sensors/battery.md +70 -0
- package/docs/api/dom/sensors/geolocation.md +115 -0
- package/docs/api/dom/sensors/networkInfo.md +73 -0
- package/docs/api/dom/sensors/sensors.md +123 -0
- package/docs/api/dom/sw/README.md +31 -0
- package/docs/api/dom/sw/backgroundSync.md +145 -0
- package/docs/api/dom/sw/cache.md +116 -0
- package/docs/api/dom/sw/push.md +114 -0
- package/docs/api/dom/sw/serviceWorker.md +90 -0
- package/docs/api/dom/sw/sharedWorker.md +131 -0
- package/docs/api/dom/utils/README.md +25 -0
- package/docs/api/dom/utils/a11y.md +143 -0
- package/docs/api/dom/utils/clipboard.md +146 -0
- package/docs/api/dom/utils/entropyCollector.md +175 -0
- package/docs/api/dom/utils/focus.md +140 -0
- package/docs/api/dom/utils/form.md +196 -0
- package/docs/api/dom/utils/formKit.md +181 -0
- package/docs/api/dom/utils/keybindings.md +138 -0
- package/docs/api/dom/utils/leaderElection.md +130 -0
- package/docs/api/dom/utils/notifications.md +106 -0
- package/docs/api/dom/utils/permissions.md +82 -0
- package/docs/api/dom/utils/route.md +112 -0
- package/docs/api/dom/utils/ua.md +119 -0
- package/docs/api/dom/utils/webauthn.md +190 -0
- package/docs/api/io/README.md +20 -0
- package/docs/api/io/binary/README.md +29 -0
- package/docs/api/io/binary/reader.md +126 -0
- package/docs/api/io/binary/writer.md +127 -0
- package/docs/api/io/calc/README.md +22 -0
- package/docs/api/io/calc/adler32.md +75 -0
- package/docs/api/io/calc/bigint.md +105 -0
- package/docs/api/io/calc/crc32.md +74 -0
- package/docs/api/io/calc/easing.md +118 -0
- package/docs/api/io/codec/README.md +35 -0
- package/docs/api/io/codec/b64.md +92 -0
- package/docs/api/io/codec/base32.md +98 -0
- package/docs/api/io/codec/base58.md +104 -0
- package/docs/api/io/codec/buffer.md +144 -0
- package/docs/api/io/codec/cbor.md +179 -0
- package/docs/api/io/codec/csv.md +178 -0
- package/docs/api/io/codec/hex.md +75 -0
- package/docs/api/io/codec/mime.md +156 -0
- package/docs/api/io/codec/msgpack.md +142 -0
- package/docs/api/io/codec/url.md +265 -0
- package/docs/api/io/codec/utf8.md +84 -0
- package/docs/api/io/codec/xml.md +87 -0
- package/docs/api/io/compress/README.md +62 -0
- package/docs/api/io/compress/bitstream.md +131 -0
- package/docs/api/io/compress/brotli.md +232 -0
- package/docs/api/io/compress/brotli_dict.md +119 -0
- package/docs/api/io/compress/brotli_dict_words.md +142 -0
- package/docs/api/io/compress/brotli_frame.md +214 -0
- package/docs/api/io/compress/brotli_shared.md +144 -0
- package/docs/api/io/compress/deflate.md +474 -0
- package/docs/api/io/compress/gzip.md +130 -0
- package/docs/api/io/compress/huffman.md +210 -0
- package/docs/api/io/compress/lz4.md +278 -0
- package/docs/api/io/compress/lz77.md +180 -0
- package/docs/api/io/compress/lzw.md +121 -0
- package/docs/api/io/compress/zip.md +209 -0
- package/docs/api/io/compress/zlib.md +125 -0
- package/docs/api/io/i18n/README.md +23 -0
- package/docs/api/io/i18n/i18n.md +128 -0
- package/docs/api/io/math/README.md +24 -0
- package/docs/api/io/math/fixed-point.md +115 -0
- package/docs/api/io/math/geom.md +144 -0
- package/docs/api/io/math/interp.md +115 -0
- package/docs/api/io/math/linalg.md +138 -0
- package/docs/api/io/math/stats.md +109 -0
- package/docs/api/io/structures/README.md +31 -0
- package/docs/api/io/structures/btree.md +155 -0
- package/docs/api/io/structures/heap.md +166 -0
- package/docs/api/io/structures/lruCache.md +156 -0
- package/docs/api/io/structures/ringBuffer.md +188 -0
- package/docs/api/io/structures/tree-walker.md +132 -0
- package/docs/api/io/structures/trie.md +138 -0
- package/docs/api/io/sync/README.md +29 -0
- package/docs/api/io/sync/abort.md +129 -0
- package/docs/api/io/sync/atomics.md +159 -0
- package/docs/api/io/sync/cancellable.md +176 -0
- package/docs/api/io/sync/channel.md +125 -0
- package/docs/api/io/sync/mutex.md +93 -0
- package/docs/api/io/sync/semaphore.md +106 -0
- package/docs/api/io/sync/tokenBucket.md +139 -0
- package/docs/api/io/text/README.md +23 -0
- package/docs/api/io/text/ansi.md +182 -0
- package/docs/api/io/text/html-entities.md +79 -0
- package/docs/api/io/text/semver.md +158 -0
- package/docs/api/io/text/str.md +129 -0
- package/docs/api/io/text/unicode.md +89 -0
- package/docs/api/io/time/README.md +22 -0
- package/docs/api/io/time/date.md +128 -0
- package/docs/api/io/timing/README.md +32 -0
- package/docs/api/io/timing/clock.md +102 -0
- package/docs/api/io/timing/rateLimit.md +137 -0
- package/docs/api/io/timing/scheduler.md +163 -0
- package/docs/api/io/utils/README.md +30 -0
- package/docs/api/io/utils/bitmap.md +96 -0
- package/docs/api/io/utils/errors.md +165 -0
- package/docs/api/io/utils/eventBus.md +162 -0
- package/docs/api/io/utils/queue.md +141 -0
- package/docs/api/io/utils/signal.md +388 -0
- package/docs/api/io/utils/ui8.md +111 -0
- package/docs/api/io/utils/valid.md +272 -0
- package/docs/api/process/README.md +18 -0
- package/docs/api/process/message.md +166 -0
- package/docs/api/process/rpc.md +161 -0
- package/docs/api/process/workerPool.md +130 -0
- package/docs/api/sanity/base.md +132 -0
- package/docs/api/sanity/community.md +96 -0
- package/docs/api/sanity/lockdown.md +128 -0
- package/docs/api/typed.md +85 -0
- package/docs/dev/README.md +25 -0
- package/docs/dev/provenance.md +80 -0
- package/docs/dev/publishing.md +31 -0
- package/docs/dev/runtime.md +64 -0
- package/docs/dev/testing.md +61 -0
- package/docs/dev/types.md +35 -0
- package/docs/evidence/pqc-hybrid-evidence.md +438 -0
- package/docs/guide/README.md +38 -0
- package/docs/guide/_QUICK_REF.md +341 -0
- package/docs/guide/by-use-case.md +160 -0
- package/docs/guide/doc-format.md +306 -0
- package/docs/guide/gesture-dnd-side-channel.md +119 -0
- package/docs/guide/getting-started.md +134 -0
- package/docs/guide/hmr.md +187 -0
- package/docs/guide/integration-bundlers.md +193 -0
- package/docs/guide/integrations-hmr.md +103 -0
- package/docs/guide/module-creation-workflow.md +342 -0
- package/docs/guide/module-pattern.md +244 -0
- package/docs/guide/rendering-pipeline.md +422 -0
- package/docs/guide/security.md +346 -0
- package/docs/guide/test-format.md +190 -0
- package/docs/guide/tutorial-todo.md +298 -0
- package/docs/guide/typescript.md +215 -0
- package/docs/guide/worker-confinement.md +301 -0
- package/docs/guide/workers.md +168 -0
- package/docs/notes/lz77-deflate-factorization.md +96 -0
- package/docs/notes/worker-confinement-spike.md +302 -0
- package/docs/tools/README.md +56 -0
- package/docs/tools/aot.md +108 -0
- package/docs/tools/audit.md +61 -0
- package/docs/tools/bundler.md +215 -0
- package/docs/tools/codegen.md +64 -0
- package/docs/tools/jsx.md +236 -0
- package/docs/tools/precompilation.md +88 -0
- package/docs/tools/standalone.md +107 -0
- package/fw.config.json +129 -0
- package/integrations/README.md +52 -0
- package/integrations/_shared/catalog.generated.json +207 -0
- package/integrations/_shared/config-resolve.js +201 -0
- package/integrations/_shared/core.js +362 -0
- package/integrations/astro/README.md +96 -0
- package/integrations/astro/index.d.ts +42 -0
- package/integrations/astro/index.js +94 -0
- package/integrations/bun/README.md +62 -0
- package/integrations/bun/index.d.ts +30 -0
- package/integrations/bun/index.js +134 -0
- package/integrations/deno/README.md +55 -0
- package/integrations/deno/deno.json +10 -0
- package/integrations/esbuild/README.md +75 -0
- package/integrations/esbuild/backend.js +147 -0
- package/integrations/esbuild/index.d.ts +32 -0
- package/integrations/esbuild/index.js +147 -0
- package/integrations/eslint/README.md +27 -0
- package/integrations/eslint/no-factory-capture.js +99 -0
- package/integrations/jest/README.md +45 -0
- package/integrations/jest/bun-test-shim.js +43 -0
- package/integrations/nestjs/README.md +68 -0
- package/integrations/nestjs/index.d.ts +38 -0
- package/integrations/nestjs/index.js +99 -0
- package/integrations/nextjs/README.md +149 -0
- package/integrations/nextjs/index.d.ts +30 -0
- package/integrations/nextjs/index.js +98 -0
- package/integrations/nodejs/README.md +46 -0
- package/integrations/nodejs/package.node.example.json +31 -0
- package/integrations/rollup/README.md +65 -0
- package/integrations/rollup/backend.js +132 -0
- package/integrations/rollup/index.d.ts +32 -0
- package/integrations/rollup/index.js +84 -0
- package/integrations/turbopack/README.md +56 -0
- package/integrations/turbopack/index.d.ts +47 -0
- package/integrations/turbopack/index.js +164 -0
- package/integrations/typedoc/README.md +57 -0
- package/integrations/typedoc/categorize.js +121 -0
- package/integrations/typedoc/tsconfig.typedoc.json +14 -0
- package/integrations/typedoc/typedoc.json +19 -0
- package/integrations/vite/README.md +115 -0
- package/integrations/vite/index.d.ts +29 -0
- package/integrations/vite/index.js +182 -0
- package/integrations/vite/package.json +8 -0
- package/integrations/vite/vite-env.d.ts +36 -0
- package/integrations/vitest/README.md +77 -0
- package/integrations/vitest/bun-test-shim.js +92 -0
- package/integrations/vitest/vitest.config.js +41 -0
- package/integrations/webpack/README.md +53 -0
- package/integrations/webpack/index.d.ts +29 -0
- package/integrations/webpack/index.js +182 -0
- package/package.json +303 -0
- package/src/core/logger.js +373 -0
- package/src/core/modules.js +289 -0
- package/src/core/readyState.js +119 -0
- package/src/core/runtime.js +929 -0
- package/src/core/worker-helper.js +261 -0
- package/src/crypto/NIST_CONFORMANCE.md +249 -0
- package/src/crypto/cipher/aes.acvp.md +74 -0
- package/src/crypto/cipher/aes.js +512 -0
- package/src/crypto/cipher/chacha20.acvp.md +117 -0
- package/src/crypto/cipher/chacha20.js +147 -0
- package/src/crypto/hash/adf.acvp.md +144 -0
- package/src/crypto/hash/adf.js +127 -0
- package/src/crypto/hash/argon2.acvp.md +144 -0
- package/src/crypto/hash/argon2.js +517 -0
- package/src/crypto/hash/blake2b.acvp.md +122 -0
- package/src/crypto/hash/blake2b.js +367 -0
- package/src/crypto/hash/hkdf.acvp.md +128 -0
- package/src/crypto/hash/hkdf.js +145 -0
- package/src/crypto/hash/hmac.acvp.md +110 -0
- package/src/crypto/hash/hmac.js +177 -0
- package/src/crypto/hash/pbkdf2.acvp.md +122 -0
- package/src/crypto/hash/pbkdf2.js +120 -0
- package/src/crypto/hash/poly1305.acvp.md +111 -0
- package/src/crypto/hash/poly1305.js +126 -0
- package/src/crypto/hash/sha224.acvp.md +81 -0
- package/src/crypto/hash/sha224.js +60 -0
- package/src/crypto/hash/sha256.acvp.md +75 -0
- package/src/crypto/hash/sha256.js +217 -0
- package/src/crypto/hash/sha3.acvp.md +133 -0
- package/src/crypto/hash/sha3.js +391 -0
- package/src/crypto/hash/sha384.acvp.md +82 -0
- package/src/crypto/hash/sha384.js +61 -0
- package/src/crypto/hash/sha512.acvp.md +84 -0
- package/src/crypto/hash/sha512.js +314 -0
- package/src/crypto/hash/sha512_224.acvp.md +90 -0
- package/src/crypto/hash/sha512_224.js +66 -0
- package/src/crypto/hash/sha512_256.acvp.md +96 -0
- package/src/crypto/hash/sha512_256.js +75 -0
- package/src/crypto/mode/cbc.acvp.md +105 -0
- package/src/crypto/mode/cbc.js +136 -0
- package/src/crypto/mode/chacha20poly1305.acvp.md +116 -0
- package/src/crypto/mode/chacha20poly1305.js +190 -0
- package/src/crypto/mode/cmac.acvp.md +106 -0
- package/src/crypto/mode/cmac.js +172 -0
- package/src/crypto/mode/ctr.acvp.md +110 -0
- package/src/crypto/mode/ctr.js +174 -0
- package/src/crypto/mode/gcm.acvp.md +129 -0
- package/src/crypto/mode/gcm.js +333 -0
- package/src/crypto/mode/kw.acvp.md +145 -0
- package/src/crypto/mode/kw.js +323 -0
- package/src/crypto/pkc/ecc.acvp.md +181 -0
- package/src/crypto/pkc/ecc.js +941 -0
- package/src/crypto/pkc/ed25519.acvp.md +172 -0
- package/src/crypto/pkc/ed25519.js +582 -0
- package/src/crypto/pkc/hybrid-kat.provenance.md +89 -0
- package/src/crypto/pkc/hybridKem.js +255 -0
- package/src/crypto/pkc/hybridSign.js +437 -0
- package/src/crypto/pkc/ml_dsa.acvp.md +146 -0
- package/src/crypto/pkc/ml_dsa.js +984 -0
- package/src/crypto/pkc/ml_kem.acvp.md +117 -0
- package/src/crypto/pkc/ml_kem.js +596 -0
- package/src/crypto/pkc/rsa.acvp.md +187 -0
- package/src/crypto/pkc/rsa.js +470 -0
- package/src/crypto/pkc/slh_dsa.acvp.md +155 -0
- package/src/crypto/pkc/slh_dsa.js +820 -0
- package/src/crypto/pkc/x25519.acvp.md +113 -0
- package/src/crypto/pkc/x25519.js +290 -0
- package/src/crypto/utils/aes_ctr.acvp.md +67 -0
- package/src/crypto/utils/aes_ctr.js +134 -0
- package/src/crypto/utils/aes_modes.acvp.md +66 -0
- package/src/crypto/utils/aes_modes.js +241 -0
- package/src/crypto/utils/asn1-oid.js +369 -0
- package/src/crypto/utils/asn1.js +343 -0
- package/src/crypto/utils/bitArray.js +312 -0
- package/src/crypto/utils/bn.js +825 -0
- package/src/crypto/utils/jws.js +222 -0
- package/src/crypto/utils/keyformat.js +303 -0
- package/src/crypto/utils/pad.js +198 -0
- package/src/crypto/utils/pem.js +90 -0
- package/src/crypto/utils/random.acvp.md +167 -0
- package/src/crypto/utils/random.js +846 -0
- package/src/crypto/utils/rsaKeygen.acvp.md +158 -0
- package/src/crypto/utils/rsaKeygen.js +448 -0
- package/src/crypto/utils/totp.js +409 -0
- package/src/crypto/utils/uuid.acvp.md +137 -0
- package/src/crypto/utils/uuid.js +176 -0
- package/src/crypto/wasm/aes.js +494 -0
- package/src/crypto/wasm/argon2.js +236 -0
- package/src/crypto/wasm/blake2b.js +213 -0
- package/src/crypto/wasm/chacha20poly1305.js +238 -0
- package/src/crypto/wasm/cmac.js +207 -0
- package/src/crypto/wasm/ecc.js +437 -0
- package/src/crypto/wasm/ed25519.js +307 -0
- package/src/crypto/wasm/hkdf.js +183 -0
- package/src/crypto/wasm/hmac.js +215 -0
- package/src/crypto/wasm/ml_dsa.js +375 -0
- package/src/crypto/wasm/ml_kem.js +313 -0
- package/src/crypto/wasm/pbkdf2.js +205 -0
- package/src/crypto/wasm/rsa.js +674 -0
- package/src/crypto/wasm/runtime.js +400 -0
- package/src/crypto/wasm/sha2.js +183 -0
- package/src/crypto/wasm/sha3.js +221 -0
- package/src/crypto/wasm/slh_dsa.js +397 -0
- package/src/crypto/wasm/x25519.js +241 -0
- package/src/crypto/webcrypto/aes.js +370 -0
- package/src/crypto/webcrypto/aeskw.js +185 -0
- package/src/crypto/webcrypto/digest.js +136 -0
- package/src/crypto/webcrypto/ecc.js +371 -0
- package/src/crypto/webcrypto/ed25519.js +248 -0
- package/src/crypto/webcrypto/hkdf.js +132 -0
- package/src/crypto/webcrypto/hmac.js +213 -0
- package/src/crypto/webcrypto/pbkdf2.js +146 -0
- package/src/crypto/webcrypto/rsa.js +415 -0
- package/src/crypto/webcrypto/x25519.js +250 -0
- package/src/dom/display/animate.js +154 -0
- package/src/dom/display/fullscreen.js +305 -0
- package/src/dom/fs/download.js +118 -0
- package/src/dom/fs/fsAccess.js +307 -0
- package/src/dom/fs/indexedDB.js +465 -0
- package/src/dom/fs/remoteStore.js +484 -0
- package/src/dom/fs/storage.js +440 -0
- package/src/dom/lifecycle/idle.js +133 -0
- package/src/dom/lifecycle/visibility.js +140 -0
- package/src/dom/lifecycle/wakeLock.js +187 -0
- package/src/dom/net/ajax.js +303 -0
- package/src/dom/net/broadcastChannel.js +89 -0
- package/src/dom/net/network.js +260 -0
- package/src/dom/net/sse.js +295 -0
- package/src/dom/net/webrtc.js +381 -0
- package/src/dom/net/ws.js +290 -0
- package/src/dom/query/dnd.js +409 -0
- package/src/dom/query/dom.js +831 -0
- package/src/dom/query/events.js +425 -0
- package/src/dom/query/gesture.js +396 -0
- package/src/dom/query/media.js +616 -0
- package/src/dom/rendering/chart.js +663 -0
- package/src/dom/rendering/component.js +469 -0
- package/src/dom/rendering/devtools-ui.js +399 -0
- package/src/dom/rendering/devtools.js +352 -0
- package/src/dom/rendering/parser.js +1055 -0
- package/src/dom/rendering/reactiveBind.js +341 -0
- package/src/dom/rendering/render.js +601 -0
- package/src/dom/rendering/sanitize.js +364 -0
- package/src/dom/rendering/secPolicy.js +281 -0
- package/src/dom/rendering/template.js +1031 -0
- package/src/dom/rendering/themeTokens.js +522 -0
- package/src/dom/rendering/uiSession-core.js +597 -0
- package/src/dom/rendering/uiSession-direct.js +251 -0
- package/src/dom/rendering/uiSession-list.js +751 -0
- package/src/dom/rendering/uiSession.js +145 -0
- package/src/dom/rendering/virtualScroll.js +341 -0
- package/src/dom/sensors/battery.js +153 -0
- package/src/dom/sensors/geolocation.js +156 -0
- package/src/dom/sensors/networkInfo.js +136 -0
- package/src/dom/sensors/sensors.js +340 -0
- package/src/dom/sw/backgroundSync.js +113 -0
- package/src/dom/sw/cache.js +139 -0
- package/src/dom/sw/push.js +181 -0
- package/src/dom/sw/serviceWorker.js +193 -0
- package/src/dom/sw/sharedWorker.js +192 -0
- package/src/dom/utils/a11y.js +244 -0
- package/src/dom/utils/clipboard.js +113 -0
- package/src/dom/utils/entropyCollector.js +344 -0
- package/src/dom/utils/focus.js +421 -0
- package/src/dom/utils/form.js +535 -0
- package/src/dom/utils/formKit.js +427 -0
- package/src/dom/utils/keybindings.js +574 -0
- package/src/dom/utils/leaderElection.js +280 -0
- package/src/dom/utils/notifications.js +218 -0
- package/src/dom/utils/permissions.js +107 -0
- package/src/dom/utils/route.js +482 -0
- package/src/dom/utils/ua.js +109 -0
- package/src/dom/utils/webauthn.js +369 -0
- package/src/external/README.md +15 -0
- package/src/io/binary/reader.js +374 -0
- package/src/io/binary/writer.js +351 -0
- package/src/io/calc/adler32.js +94 -0
- package/src/io/calc/bigint.js +276 -0
- package/src/io/calc/crc32.js +111 -0
- package/src/io/calc/easing.js +514 -0
- package/src/io/codec/b64.js +235 -0
- package/src/io/codec/base32.js +147 -0
- package/src/io/codec/base58.js +167 -0
- package/src/io/codec/buffer.js +630 -0
- package/src/io/codec/cbor.js +550 -0
- package/src/io/codec/csv.js +299 -0
- package/src/io/codec/hex.js +129 -0
- package/src/io/codec/mime.js +398 -0
- package/src/io/codec/msgpack.js +491 -0
- package/src/io/codec/url.js +530 -0
- package/src/io/codec/utf8.js +181 -0
- package/src/io/codec/xml.js +428 -0
- package/src/io/compress/bitstream.js +199 -0
- package/src/io/compress/brotli.js +3073 -0
- package/src/io/compress/brotli_dict.bin +432 -0
- package/src/io/compress/brotli_dict.js +356 -0
- package/src/io/compress/brotli_dict_words.js +157 -0
- package/src/io/compress/brotli_frame.js +420 -0
- package/src/io/compress/brotli_shared.js +715 -0
- package/src/io/compress/deflate.js +1432 -0
- package/src/io/compress/gzip.js +406 -0
- package/src/io/compress/huffman.js +322 -0
- package/src/io/compress/lz4.js +451 -0
- package/src/io/compress/lz77.js +408 -0
- package/src/io/compress/lzw.js +332 -0
- package/src/io/compress/zip.js +815 -0
- package/src/io/compress/zlib.js +340 -0
- package/src/io/i18n/i18n.js +236 -0
- package/src/io/math/fixed-point.js +269 -0
- package/src/io/math/geom.js +350 -0
- package/src/io/math/interp.js +263 -0
- package/src/io/math/linalg.js +472 -0
- package/src/io/math/stats.js +339 -0
- package/src/io/structures/btree.js +357 -0
- package/src/io/structures/heap.js +249 -0
- package/src/io/structures/lruCache.js +219 -0
- package/src/io/structures/ringBuffer.js +208 -0
- package/src/io/structures/tree-walker.js +413 -0
- package/src/io/structures/trie.js +219 -0
- package/src/io/sync/abort.js +196 -0
- package/src/io/sync/atomics.js +342 -0
- package/src/io/sync/cancellable.js +279 -0
- package/src/io/sync/channel.js +301 -0
- package/src/io/sync/mutex.js +148 -0
- package/src/io/sync/semaphore.js +156 -0
- package/src/io/sync/tokenBucket.js +208 -0
- package/src/io/text/ansi.js +536 -0
- package/src/io/text/html-entities.js +143 -0
- package/src/io/text/semver.js +375 -0
- package/src/io/text/str.js +243 -0
- package/src/io/text/unicode.js +188 -0
- package/src/io/time/date.js +355 -0
- package/src/io/timing/clock.js +79 -0
- package/src/io/timing/rateLimit.js +264 -0
- package/src/io/timing/scheduler.js +351 -0
- package/src/io/utils/bitmap.js +94 -0
- package/src/io/utils/errors.js +269 -0
- package/src/io/utils/eventBus.js +315 -0
- package/src/io/utils/queue.js +223 -0
- package/src/io/utils/signal.js +858 -0
- package/src/io/utils/ui8.js +99 -0
- package/src/io/utils/valid.js +556 -0
- package/src/main.js +41 -0
- package/src/polyfill/README.md +9 -0
- package/src/process/message.js +257 -0
- package/src/process/rpc.js +286 -0
- package/src/process/workerPool.js +392 -0
- package/src/sanity/base.js +374 -0
- package/src/sanity/community.js +254 -0
- package/src/sanity/lockdown-apply.js +58 -0
- package/src/sanity/lockdown.js +408 -0
- package/src/sanity/wasm-gate.js +316 -0
- package/src/typed.js +53 -0
- package/third-party/NOTICE-penner-easing +88 -0
- package/third-party/NOTICE-sjcl +50 -0
- package/tools/_lib/cli-help.js +55 -0
- package/tools/rendering/aot/index.js +148 -0
- package/tools/rendering/aot/lib/dom-codegen.js +295 -0
- package/tools/rendering/jsx/index.js +654 -0
- package/tools/rendering/precompilation/index.js +368 -0
- package/types/registry.generated.d.ts +217 -0
- package/types/typed.d.ts +106 -0
|
@@ -0,0 +1,3073 @@
|
|
|
1
|
+
// Copyright (c) 2026 AwaCloud SAS
|
|
2
|
+
// Author: Matthieu Bouilloux
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview Brotli codec - RFC 7932 (Brotli Compressed Data Format).
|
|
7
|
+
*
|
|
8
|
+
* Decoder **100% RFC 7932** + dynamic encoder (LZ77 + Huffman + adaptive
|
|
9
|
+
* context modeling + block splitting `NBLTYPES_L ∈ {1, 2}` KL-driven +
|
|
10
|
+
* static-dict refs 121/121 transforms + quality levels 0..11).
|
|
11
|
+
*
|
|
12
|
+
* For the RFC 9841 extensions (large window, shared dictionary, §5 parser),
|
|
13
|
+
* use the companion module [`brotliShared`](./brotli_shared.md) which
|
|
14
|
+
* depends on this one. The private `opts._ext` channel is consumed by
|
|
15
|
+
* `brotliShared` to inject the RFC 9841 hooks:
|
|
16
|
+
*
|
|
17
|
+
* - `_ext.allowLargeWindow` - accept the §6 large-window WBITS prefix
|
|
18
|
+
* - `_ext.lz77Dict` / `_ext.lz77Prefix` - virtual prefix prepended to the
|
|
19
|
+
* output (decoder) or input (encoder) per §3.2
|
|
20
|
+
* - `_ext.resolveStaticDictRef` - callback replacing the RFC 7932 dict-ref
|
|
21
|
+
* path with the §3.1 custom dictionaries
|
|
22
|
+
*
|
|
23
|
+
* No user caller should touch `_ext` directly - use `brotliShared` instead.
|
|
24
|
+
*
|
|
25
|
+
* ## Public API
|
|
26
|
+
*
|
|
27
|
+
* | Method | Returns |
|
|
28
|
+
* |---|---|
|
|
29
|
+
* | `brotliCompressSync(data, opts?)` | `Uint8Array` - sync compression |
|
|
30
|
+
* | `brotliDecompressSync(data, opts?)` | `Uint8Array` - sync decompression |
|
|
31
|
+
* | `brotliCompress(data, opts?)` | `Promise<Uint8Array>` - async variant |
|
|
32
|
+
* | `brotliDecompress(data, opts?)` | `Promise<Uint8Array>` - async variant |
|
|
33
|
+
* | `new BrotliCompressStream(opts?, ondata)` | streaming compressor (buffered) |
|
|
34
|
+
* | `new BrotliDecompressStream(opts?, ondata)` | streaming decompressor (EAGAIN-incremental) |
|
|
35
|
+
*
|
|
36
|
+
* One public option: `opts.quality ∈ [0, 11]` (default `6`) - speed/ratio
|
|
37
|
+
* profile (`0` = trivial uncompressed, `1..3` chainDepth 2..4 lazy off,
|
|
38
|
+
* `4..7` lazy on, `8..11` block splitting + chainDepth 16..32).
|
|
39
|
+
*
|
|
40
|
+
* ## Static dictionary
|
|
41
|
+
*
|
|
42
|
+
* Streams that reference the static dictionary (RFC 7932 §8) require the
|
|
43
|
+
* 122,784-byte blob vendored in
|
|
44
|
+
* [`brotliDictWords`](./brotli_dict_words.md). The decoder wires
|
|
45
|
+
* `brotliDict.setWords(brotliDictWords.blob)` on the first dict-ref if and
|
|
46
|
+
* only if `brotliDictWords.isLoaded === true`. On the browser side, call
|
|
47
|
+
* `await brotliDictWords.load(url)` once at boot; on Node/Bun, call
|
|
48
|
+
* `brotliDictWords.setBlob(fs.readFileSync(...))`. Streams that never use
|
|
49
|
+
* the dictionary (short text fully resolved by LZ77) decode without the blob.
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Streaming Brotli compressor instance shape.
|
|
55
|
+
* @typedef {object} BrotliCompressStreamInstance
|
|
56
|
+
* @property {(chunk: Uint8Array, isFinal: boolean) => void} ondata
|
|
57
|
+
* @property {(chunk: Uint8Array, final?: boolean) => void} push
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Constructor for `BrotliCompressStream`.
|
|
62
|
+
* @typedef {new (opts?: object|((chunk: Uint8Array, isFinal: boolean) => void), ondata?: (chunk: Uint8Array, isFinal: boolean) => void) => BrotliCompressStreamInstance} BrotliCompressStreamCtor
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Streaming Brotli decompressor instance shape.
|
|
67
|
+
* @typedef {object} BrotliDecompressStreamInstance
|
|
68
|
+
* @property {(chunk: Uint8Array, isFinal: boolean) => void} ondata
|
|
69
|
+
* @property {(chunk: Uint8Array, final?: boolean) => void} push
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Constructor for `BrotliDecompressStream`.
|
|
74
|
+
* @typedef {new (opts?: object|((chunk: Uint8Array, isFinal: boolean) => void), ondata?: (chunk: Uint8Array, isFinal: boolean) => void) => BrotliDecompressStreamInstance} BrotliDecompressStreamCtor
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Public surface of `brotli.factory(...)`.
|
|
79
|
+
* @typedef {object} BrotliApi
|
|
80
|
+
* @property {(data: Uint8Array, opts?: object) => Uint8Array} brotliCompressSync
|
|
81
|
+
* @property {(data: Uint8Array, opts?: object) => Uint8Array} brotliDecompressSync
|
|
82
|
+
* @property {(data: Uint8Array, opts?: object) => Promise<Uint8Array>} brotliCompress
|
|
83
|
+
* @property {(data: Uint8Array, opts?: object) => Promise<Uint8Array>} brotliDecompress
|
|
84
|
+
* @property {BrotliCompressStreamCtor} BrotliCompressStream
|
|
85
|
+
* @property {BrotliDecompressStreamCtor} BrotliDecompressStream
|
|
86
|
+
* @property {object} _internal
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
import { bitstream } from './bitstream.js';
|
|
90
|
+
import { huffman } from './huffman.js';
|
|
91
|
+
import { lz77 } from './lz77.js';
|
|
92
|
+
import { brotliDict } from './brotli_dict.js';
|
|
93
|
+
import { brotliDictWords } from './brotli_dict_words.js';
|
|
94
|
+
|
|
95
|
+
export const brotli = {
|
|
96
|
+
name: 'brotli',
|
|
97
|
+
version: '2.4.0',
|
|
98
|
+
type: 'fw.io.compress',
|
|
99
|
+
dependencies: ['bitstream', 'huffman', 'lz77', 'brotliDict', 'brotliDictWords'],
|
|
100
|
+
deps: [bitstream, huffman, lz77, brotliDict, brotliDictWords],
|
|
101
|
+
|
|
102
|
+
/** @returns {BrotliApi} */
|
|
103
|
+
factory(bitstream, huffman, lz77, brotliDict, brotliDictWords) {
|
|
104
|
+
|
|
105
|
+
const u8 = Uint8Array;
|
|
106
|
+
|
|
107
|
+
// --- Error helper ---
|
|
108
|
+
|
|
109
|
+
function _err(code, msg, bitPos) {
|
|
110
|
+
const e = new Error(
|
|
111
|
+
'brotli: ' + msg + (bitPos != null ? ' @ bit ' + bitPos : '')
|
|
112
|
+
);
|
|
113
|
+
// @ts-ignore - Error.code is a non-standard but widely-used extension
|
|
114
|
+
e.code = code;
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// --- Bit reader over input data ---
|
|
119
|
+
//
|
|
120
|
+
// The brotli wire format reads bits LSB-first within each byte. We
|
|
121
|
+
// wrap the input in a small reader object that owns a bit cursor `p`
|
|
122
|
+
// and a padded buffer (4 extra zero bytes) so that bit reads can
|
|
123
|
+
// safely fetch up to 32 bits past the cursor without bounds checks.
|
|
124
|
+
|
|
125
|
+
function _makeReader(data, canExpectMore) {
|
|
126
|
+
const padded = new u8(data.length + 4);
|
|
127
|
+
padded.set(data);
|
|
128
|
+
// padded[data.length..] already zero
|
|
129
|
+
const state = {
|
|
130
|
+
d: padded,
|
|
131
|
+
p: 0, // bit position
|
|
132
|
+
eb: data.length * 8, // exclusive bit end of real data
|
|
133
|
+
rb: data.length, // real byte length
|
|
134
|
+
eofBits: data.length * 8,
|
|
135
|
+
canExpectMore: !!canExpectMore,
|
|
136
|
+
};
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function _eofThrow(r) {
|
|
141
|
+
const e = new Error(r.canExpectMore
|
|
142
|
+
? 'EAGAIN: input exhausted, more bytes expected'
|
|
143
|
+
: 'EBADSTREAM: unexpected end of input');
|
|
144
|
+
// @ts-ignore - Error.code is a non-standard but widely-used extension
|
|
145
|
+
e.code = r.canExpectMore ? 'EAGAIN' : 'EBADSTREAM';
|
|
146
|
+
throw e;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Read 1 bit
|
|
150
|
+
function _readBit(r) {
|
|
151
|
+
if (r.p >= r.eofBits) _eofThrow(r);
|
|
152
|
+
const v = (r.d[r.p >> 3] >> (r.p & 7)) & 1;
|
|
153
|
+
r.p += 1;
|
|
154
|
+
return v;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Read 1–24 bits at the cursor and advance.
|
|
158
|
+
//
|
|
159
|
+
// Note: `bitstream.readBits` only loads 2 bytes and is therefore
|
|
160
|
+
// safe only for `n + shift ≤ 16`. Brotli reads up to 24-bit fields
|
|
161
|
+
// (MLEN, MSKIPLEN, INSERT/COPY extras) at any byte offset, so we
|
|
162
|
+
// load 4 bytes from the padded reader buffer here. `_readBitsBig`
|
|
163
|
+
// is kept as a synonym for callsites that document an intent to
|
|
164
|
+
// read more than 16 bits.
|
|
165
|
+
function _readBits(r, n) {
|
|
166
|
+
if (r.p + n > r.eofBits) _eofThrow(r);
|
|
167
|
+
const o = r.p >> 3;
|
|
168
|
+
const shift = r.p & 7;
|
|
169
|
+
const v = ((r.d[o] | (r.d[o + 1] << 8) | (r.d[o + 2] << 16) | (r.d[o + 3] << 24)) >>> shift)
|
|
170
|
+
& ((1 << n) - 1);
|
|
171
|
+
r.p += n;
|
|
172
|
+
return v;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Read 1..50 bits into a Number. For n ≤ 24, defers to _readBits.
|
|
176
|
+
// Beyond 24 bits, splits into a low 24-bit chunk + high (n-24) chunk
|
|
177
|
+
// and combines via Math.pow(2, 24) - exact as long as the assembled
|
|
178
|
+
// value stays under 2^53 (n ≤ 50, since high chunk ≤ 26 bits).
|
|
179
|
+
function _readBitsBig(r, n) {
|
|
180
|
+
if (n <= 24) return _readBits(r, n);
|
|
181
|
+
if (r.p + n > r.eofBits) _eofThrow(r);
|
|
182
|
+
const lo = _readBits(r, 24);
|
|
183
|
+
const hi = _readBits(r, n - 24);
|
|
184
|
+
return hi * 0x1000000 + lo;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Advance the cursor to the next byte boundary
|
|
188
|
+
function _alignToByte(r) {
|
|
189
|
+
r.p = (r.p + 7) & ~7;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Read 25..62 bits into a BigInt. Used by `_decodeDistanceSymbol`
|
|
193
|
+
// when `ndistbits` exceeds Number's safe range for the intermediate
|
|
194
|
+
// arithmetic (RFC 9841 §6 large window streams with wbits 51..62).
|
|
195
|
+
function _readBitsBigInt(r, n) {
|
|
196
|
+
if (r.p + n > r.eofBits) _eofThrow(r);
|
|
197
|
+
let v = 0n;
|
|
198
|
+
let shift = 0n;
|
|
199
|
+
let remaining = n;
|
|
200
|
+
while (remaining > 24) {
|
|
201
|
+
const chunk = _readBits(r, 24);
|
|
202
|
+
v |= BigInt(chunk) << shift;
|
|
203
|
+
shift += 24n;
|
|
204
|
+
remaining -= 24;
|
|
205
|
+
}
|
|
206
|
+
const last = _readBits(r, remaining);
|
|
207
|
+
v |= BigInt(last) << shift;
|
|
208
|
+
return v;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Verify all remaining bits in current byte (after p) are zero
|
|
212
|
+
function _verifyByteTailZero(r) {
|
|
213
|
+
const bit = r.p & 7;
|
|
214
|
+
if (bit === 0) return;
|
|
215
|
+
const tail = r.d[r.p >> 3] >> bit;
|
|
216
|
+
if (tail !== 0) _err('EBADSTREAM', 'non-zero fill bits', r.p);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Verify that, starting from the current byte boundary, the rest of
|
|
220
|
+
// the input contains only zero bytes - used to validate stream end.
|
|
221
|
+
function _verifyTrailingZero(r) {
|
|
222
|
+
_verifyByteTailZero(r);
|
|
223
|
+
for (let i = (r.p + 7) >> 3; i < r.rb; ++i) {
|
|
224
|
+
if (r.d[i]) _err('EBADSTREAM', 'non-zero trailing byte at ' + i);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// --- §9.1 - Stream header: WBITS ---
|
|
229
|
+
//
|
|
230
|
+
// Variable-length code 1..7 bits. See RFC 7932 §9.1 table.
|
|
231
|
+
// Decode tree (LSB-first reading):
|
|
232
|
+
// bit0 = 0 → WBITS = 16
|
|
233
|
+
// bit0 = 1, bits[1..3] != 0 (4-bit code) → WBITS = 17 + bits[1..3]
|
|
234
|
+
// bit0 = 1, bits[1..3] == 0 (7-bit code) → read bits[4..6] as v
|
|
235
|
+
// v == 0 → WBITS = 17
|
|
236
|
+
// v == 1 → reserved in RFC 7932 ; RFC 9841 §6 extends with one
|
|
237
|
+
// more bit:
|
|
238
|
+
// bit7 = 0 → large window mode: 8-bit prefix
|
|
239
|
+
// "00010001" + 6 bits WBITS ∈ [10, 62]
|
|
240
|
+
// bit7 = 1 → invalid
|
|
241
|
+
// v ∈ [2..7] → WBITS = 8 + v
|
|
242
|
+
//
|
|
243
|
+
// Large window mode (RFC 9841 §6) is an extension-protocol
|
|
244
|
+
// option: brotli.js refuses the large-window WBITS prefix unless
|
|
245
|
+
// the internal `opts._ext.allowLargeWindow` flag is set (the
|
|
246
|
+
// `brotli_shared` module sets it on behalf of callers that pass
|
|
247
|
+
// the public `allowLargeWindow: true` opt). When enabled, this
|
|
248
|
+
// decoder accepts the full spec range - WBITS values 10..62.
|
|
249
|
+
// For wbits ≤ 50, distance arithmetic uses Number (safe to 2^53);
|
|
250
|
+
// for wbits 51..62, distance reads + arithmetic use BigInt.
|
|
251
|
+
|
|
252
|
+
function _readWBITS(r, allowLargeWindow) {
|
|
253
|
+
if (!_readBit(r)) return { wbits: 16, largeWindow: false };
|
|
254
|
+
const mid = _readBits(r, 3);
|
|
255
|
+
if (mid !== 0) return { wbits: 17 + mid, largeWindow: false };
|
|
256
|
+
const tail = _readBits(r, 3);
|
|
257
|
+
if (tail === 0) return { wbits: 17, largeWindow: false };
|
|
258
|
+
if (tail === 1) {
|
|
259
|
+
// RFC 9841 §6 large window prefix candidate
|
|
260
|
+
const next = _readBit(r);
|
|
261
|
+
if (next !== 0) {
|
|
262
|
+
_err('EBADSTREAM', 'invalid WBITS pattern (8th bit must be 0 for large window)', r.p);
|
|
263
|
+
}
|
|
264
|
+
if (!allowLargeWindow) {
|
|
265
|
+
_err('EBADSTREAM', 'large window brotli stream - use the brotliShared module with { allowLargeWindow: true }', r.p);
|
|
266
|
+
}
|
|
267
|
+
const wbits = _readBits(r, 6);
|
|
268
|
+
if (wbits < 10 || wbits > 62) {
|
|
269
|
+
_err('EBADSTREAM', 'large window WBITS ' + wbits + ' out of range [10, 62]', r.p);
|
|
270
|
+
}
|
|
271
|
+
// wbits 51..62 use a BigInt distance-decode path (see
|
|
272
|
+
// `_decodeDistanceSymbol`). Practical distances always
|
|
273
|
+
// fit in Number - only intermediate arithmetic needs
|
|
274
|
+
// BigInt precision.
|
|
275
|
+
return { wbits, largeWindow: true };
|
|
276
|
+
}
|
|
277
|
+
return { wbits: 8 + tail, largeWindow: false };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Inverse of `_readWBITS` - emit the stream-header WBITS bits
|
|
281
|
+
// for the chosen `wbits` value. Layout (RFC 7932 §9.1 / RFC 9841 §6):
|
|
282
|
+
//
|
|
283
|
+
// wbits = 16 → 1 bit "0"
|
|
284
|
+
// wbits ∈ [18, 24] → 4 bits "1" + 3 bits (wbits − 17)
|
|
285
|
+
// wbits = 17 → 7 bits "1, 000, 000"
|
|
286
|
+
// wbits ∈ [10, 15] → 7 bits "1, 000, (wbits − 8)"
|
|
287
|
+
// wbits ∈ [10, 62] LW → 14 bits "1, 000, 001, 0, wbits" (RFC 9841 §6)
|
|
288
|
+
//
|
|
289
|
+
// Caller must use `largeWindow=true` only on encoders that have
|
|
290
|
+
// declared the corresponding `_ext.allowLargeWindow` on the
|
|
291
|
+
// decoder side - RFC 7932 decoders reject this prefix.
|
|
292
|
+
function _emitWBITS(w, wbits, largeWindow) {
|
|
293
|
+
if (largeWindow) {
|
|
294
|
+
if (wbits < 10 || wbits > 62) {
|
|
295
|
+
_err('EBADARG', 'large window wbits ' + wbits + ' out of [10, 62]');
|
|
296
|
+
}
|
|
297
|
+
// 14 bits LSB-first: bit0=1, mid=0 (3b), tail=1 (3b), next=0,
|
|
298
|
+
// then 6 bits of wbits. Packed value = 0x11 | (wbits << 8).
|
|
299
|
+
w.bits(14, 0x11 | (wbits << 8));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (wbits === 16) { w.bits(1, 0); return; }
|
|
303
|
+
if (wbits >= 18 && wbits <= 24) {
|
|
304
|
+
w.bits(4, ((wbits - 17) << 1) | 1);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (wbits === 17) { w.bits(7, 1); return; }
|
|
308
|
+
if (wbits >= 10 && wbits <= 15) {
|
|
309
|
+
w.bits(7, ((wbits - 8) << 4) | 1);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
_err('EBADARG', 'wbits ' + wbits + ' out of standard range [10, 24]');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// --- §9.2 - Meta-block header & body ---
|
|
316
|
+
//
|
|
317
|
+
// Decodes one meta-block starting at the current bit position. On a
|
|
318
|
+
// compressed meta-block (not yet implemented), throws ENOTIMPL.
|
|
319
|
+
// Returns:
|
|
320
|
+
// { done: true } - ISLASTEMPTY observed
|
|
321
|
+
// { done: islast, chunk } - uncompressed payload produced
|
|
322
|
+
// { done: false } - metadata meta-block (no output)
|
|
323
|
+
|
|
324
|
+
// Decodes ONE meta-block, mutating `state` (out / outLen / p1 / p2 /
|
|
325
|
+
// lastDist). Returns true when the stream's ISLASTEMPTY terminator
|
|
326
|
+
// is consumed or when an ISLAST non-empty meta-block has completed
|
|
327
|
+
// (caller must then verify trailing-byte fill bits).
|
|
328
|
+
// [RFC 9841 §5 Shared Dictionary Stream parser - moved to brotli_shared.js]
|
|
329
|
+
// The parser + the `_readSharedVarint` helper live in the
|
|
330
|
+
// `brotliShared` module; callers that need `parseSharedDictionary`
|
|
331
|
+
// import from there. brotli.js retains no §5 surface.
|
|
332
|
+
|
|
333
|
+
function _decodeMetaBlockInto(r, state) {
|
|
334
|
+
const islast = _readBit(r);
|
|
335
|
+
if (islast) {
|
|
336
|
+
const islastempty = _readBit(r);
|
|
337
|
+
if (islastempty) {
|
|
338
|
+
_verifyTrailingZero(r);
|
|
339
|
+
return { done: true, last: true };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// MNIBBLES - 2-bit code: 11→0, 00→4, 01→5, 10→6
|
|
344
|
+
const mnibblesCode = _readBits(r, 2);
|
|
345
|
+
const mnibbles = mnibblesCode === 3 ? 0 : mnibblesCode + 4;
|
|
346
|
+
|
|
347
|
+
if (mnibbles === 0) {
|
|
348
|
+
// Metadata meta-block (§9.2) - does not emit uncompressed
|
|
349
|
+
// bytes and is not part of the sliding window.
|
|
350
|
+
const reserved = _readBit(r);
|
|
351
|
+
if (reserved) _err('EBADSTREAM', 'metadata reserved bit must be 0', r.p);
|
|
352
|
+
const mskipbytes = _readBits(r, 2);
|
|
353
|
+
let mskiplen = 0;
|
|
354
|
+
if (mskipbytes > 0) {
|
|
355
|
+
mskiplen = mskipbytes <= 2
|
|
356
|
+
? _readBits(r, mskipbytes * 8)
|
|
357
|
+
: _readBitsBig(r, mskipbytes * 8);
|
|
358
|
+
if (mskipbytes > 1) {
|
|
359
|
+
const topByte = (mskiplen >>> ((mskipbytes - 1) * 8)) & 0xFF;
|
|
360
|
+
if (topByte === 0) _err('EBADSTREAM', 'metadata MSKIPLEN top byte must be non-zero', r.p);
|
|
361
|
+
}
|
|
362
|
+
mskiplen += 1;
|
|
363
|
+
}
|
|
364
|
+
_alignToByte(r);
|
|
365
|
+
if ((r.p >> 3) + mskiplen > r.rb) {
|
|
366
|
+
if (r.canExpectMore) _eofThrow(r);
|
|
367
|
+
_err('EBADSTREAM', 'metadata MSKIPLEN exceeds stream');
|
|
368
|
+
}
|
|
369
|
+
r.p += mskiplen * 8;
|
|
370
|
+
return { done: false, last: false };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const mlenBits = mnibbles * 4;
|
|
374
|
+
const mlenRaw = mlenBits <= 16 ? _readBits(r, mlenBits) : _readBitsBig(r, mlenBits);
|
|
375
|
+
if (mnibbles > 4) {
|
|
376
|
+
const topNibble = (mlenRaw >>> ((mnibbles - 1) * 4)) & 0xF;
|
|
377
|
+
if (topNibble === 0) _err('EBADSTREAM', 'meta-block MLEN top nibble must be non-zero', r.p);
|
|
378
|
+
}
|
|
379
|
+
const mlen = mlenRaw + 1;
|
|
380
|
+
|
|
381
|
+
let isuncompressed = 0;
|
|
382
|
+
if (!islast) isuncompressed = _readBit(r);
|
|
383
|
+
|
|
384
|
+
if (isuncompressed) {
|
|
385
|
+
_verifyByteTailZero(r);
|
|
386
|
+
_alignToByte(r);
|
|
387
|
+
const o = r.p >> 3;
|
|
388
|
+
if (o + mlen > r.rb) {
|
|
389
|
+
// For sync: malformed stream. For streaming: input not
|
|
390
|
+
// yet complete - caller can retry on next push.
|
|
391
|
+
if (r.canExpectMore) _eofThrow(r);
|
|
392
|
+
_err('EBADSTREAM', 'uncompressed MLEN exceeds stream');
|
|
393
|
+
}
|
|
394
|
+
_ensureCapacity(state, mlen);
|
|
395
|
+
state.out.set(r.d.subarray(o, o + mlen), state.outLen);
|
|
396
|
+
state.outLen += mlen;
|
|
397
|
+
if (mlen >= 2) {
|
|
398
|
+
state.p1 = state.out[state.outLen - 1];
|
|
399
|
+
state.p2 = state.out[state.outLen - 2];
|
|
400
|
+
} else if (mlen === 1) {
|
|
401
|
+
state.p2 = state.p1;
|
|
402
|
+
state.p1 = state.out[state.outLen - 1];
|
|
403
|
+
}
|
|
404
|
+
r.p += mlen * 8;
|
|
405
|
+
return { done: islast === 1, last: islast === 1 };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Compressed meta-block: header + body
|
|
409
|
+
const h = _readCompressedMetaBlockHeader(r, state.largeWindow);
|
|
410
|
+
_decodeCompressedBody(r, h, state, mlen);
|
|
411
|
+
return { done: islast === 1, last: islast === 1 };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// --- §3.4 / §3.5 - Prefix codes ---
|
|
415
|
+
//
|
|
416
|
+
// Two forms share a single entry point `_readPrefixCode(r, n)`:
|
|
417
|
+
//
|
|
418
|
+
// - Simple (head bits = 1, §3.4): explicit list of 1–4 symbols with
|
|
419
|
+
// fixed length patterns. NSYM=4 has a tree-select bit.
|
|
420
|
+
// - Complex (head bits ∈ {0, 2, 3} = HSKIP, §3.5): code lengths for
|
|
421
|
+
// the 18-symbol code-length alphabet (decoded via a tiny fixed
|
|
422
|
+
// table), then RLE-encoded code lengths for the target alphabet,
|
|
423
|
+
// then a canonical Huffman code built via `huffman.buildMap`.
|
|
424
|
+
//
|
|
425
|
+
// Returned shape: `{ read(r): symbol, alphabetSize }`. For codes
|
|
426
|
+
// with a single non-zero symbol the result is a 0-bit code: `read`
|
|
427
|
+
// consumes no bits and always returns the symbol.
|
|
428
|
+
|
|
429
|
+
const u16 = Uint16Array;
|
|
430
|
+
|
|
431
|
+
// Smallest k such that 2^k >= n. n ∈ [1, ...].
|
|
432
|
+
function _alphabetBits(n) {
|
|
433
|
+
let k = 0;
|
|
434
|
+
while ((1 << k) < n) ++k;
|
|
435
|
+
return k;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Code-length-symbol decode table (§3.5).
|
|
439
|
+
// Index = next 4 LSB of stream. Entry = (symbol << 4) | length.
|
|
440
|
+
// 0b0000 → sym 0, len 2 0b1000 → sym 0, len 2
|
|
441
|
+
// 0b0001 → sym 4, len 2 0b1001 → sym 4, len 2
|
|
442
|
+
// 0b0010 → sym 3, len 2 0b1010 → sym 3, len 2
|
|
443
|
+
// 0b0011 → sym 2, len 3 0b1011 → sym 2, len 3
|
|
444
|
+
// 0b0100 → sym 0, len 2 0b1100 → sym 0, len 2
|
|
445
|
+
// 0b0101 → sym 4, len 2 0b1101 → sym 4, len 2
|
|
446
|
+
// 0b0110 → sym 3, len 2 0b1110 → sym 3, len 2
|
|
447
|
+
// 0b0111 → sym 1, len 4 0b1111 → sym 5, len 4
|
|
448
|
+
const _CL_TABLE = new u16([
|
|
449
|
+
0x02, 0x42, 0x32, 0x23, 0x02, 0x42, 0x32, 0x14,
|
|
450
|
+
0x02, 0x42, 0x32, 0x23, 0x02, 0x42, 0x32, 0x54,
|
|
451
|
+
]);
|
|
452
|
+
|
|
453
|
+
// Order in which code-length-symbol lengths are read (§3.5).
|
|
454
|
+
const _CL_ORDER = new u8([
|
|
455
|
+
1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
456
|
+
]);
|
|
457
|
+
|
|
458
|
+
function _makeSingleSymbolDecoder(symbol) {
|
|
459
|
+
return {
|
|
460
|
+
read(_r) { return symbol; },
|
|
461
|
+
singleSymbol: symbol,
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function _makeTableDecoder(codeLens, alphabetSize) {
|
|
466
|
+
// Find max code length actually used (1..15).
|
|
467
|
+
let maxBits = 0;
|
|
468
|
+
for (let i = 0; i < codeLens.length; ++i) if (codeLens[i] > maxBits) maxBits = codeLens[i];
|
|
469
|
+
if (maxBits === 0) _err('EBADSTREAM', 'prefix code with all-zero lengths');
|
|
470
|
+
const table = huffman.buildMap(codeLens, maxBits, 1);
|
|
471
|
+
const mask = (1 << maxBits) - 1;
|
|
472
|
+
return {
|
|
473
|
+
read(r) {
|
|
474
|
+
// The peek reads `maxBits` bits unconditionally. In
|
|
475
|
+
// streaming mode, if those bits would cross eofBits,
|
|
476
|
+
// bail with EAGAIN so the caller buffers more before
|
|
477
|
+
// we use any zero-padded "ghost" bits in the lookup.
|
|
478
|
+
if (r.canExpectMore && r.p + maxBits > r.eofBits) _eofThrow(r);
|
|
479
|
+
// Peek with a 3-byte load (the reader buffer is padded
|
|
480
|
+
// by 4 zero bytes): maxBits reaches 15 and the bit shift
|
|
481
|
+
// 7, i.e. up to 22 bits - beyond `bitstream.readBits`'
|
|
482
|
+
// 2-byte (16-bit) window, which silently truncated codes
|
|
483
|
+
// longer than 9 bits.
|
|
484
|
+
const o = r.p >> 3;
|
|
485
|
+
const d = r.d;
|
|
486
|
+
const entry = table[((d[o] | (d[o + 1] << 8) | (d[o + 2] << 16)) >>> (r.p & 7)) & mask];
|
|
487
|
+
const len = entry & 0x0F;
|
|
488
|
+
if (len === 0) _err('EBADSTREAM', 'undefined prefix code', r.p);
|
|
489
|
+
r.p += len;
|
|
490
|
+
if (r.p > r.eofBits) _eofThrow(r);
|
|
491
|
+
return entry >>> 4;
|
|
492
|
+
},
|
|
493
|
+
alphabetSize,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function _readSimplePrefixCode(r, alphabetSize) {
|
|
498
|
+
const nsym = _readBits(r, 2) + 1;
|
|
499
|
+
const abits = _alphabetBits(alphabetSize);
|
|
500
|
+
const syms = new Array(nsym);
|
|
501
|
+
for (let i = 0; i < nsym; ++i) {
|
|
502
|
+
const s = abits === 0 ? 0
|
|
503
|
+
: abits <= 16 ? _readBits(r, abits)
|
|
504
|
+
: _readBitsBig(r, abits);
|
|
505
|
+
if (s >= alphabetSize) _err('EBADSTREAM', 'simple prefix code symbol ' + s + ' >= alphabet ' + alphabetSize, r.p);
|
|
506
|
+
for (let j = 0; j < i; ++j) {
|
|
507
|
+
if (syms[j] === s) _err('EBADSTREAM', 'simple prefix code duplicate symbol ' + s, r.p);
|
|
508
|
+
}
|
|
509
|
+
syms[i] = s;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (nsym === 1) {
|
|
513
|
+
return _makeSingleSymbolDecoder(syms[0]);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const lens = new u8(alphabetSize);
|
|
517
|
+
if (nsym === 2) {
|
|
518
|
+
lens[syms[0]] = 1;
|
|
519
|
+
lens[syms[1]] = 1;
|
|
520
|
+
} else if (nsym === 3) {
|
|
521
|
+
lens[syms[0]] = 1;
|
|
522
|
+
lens[syms[1]] = 2;
|
|
523
|
+
lens[syms[2]] = 2;
|
|
524
|
+
} else {
|
|
525
|
+
// nsym === 4
|
|
526
|
+
const treeSelect = _readBit(r);
|
|
527
|
+
if (treeSelect === 0) {
|
|
528
|
+
lens[syms[0]] = 2;
|
|
529
|
+
lens[syms[1]] = 2;
|
|
530
|
+
lens[syms[2]] = 2;
|
|
531
|
+
lens[syms[3]] = 2;
|
|
532
|
+
} else {
|
|
533
|
+
lens[syms[0]] = 1;
|
|
534
|
+
lens[syms[1]] = 2;
|
|
535
|
+
lens[syms[2]] = 3;
|
|
536
|
+
lens[syms[3]] = 3;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return _makeTableDecoder(lens, alphabetSize);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Read one code-length-symbol via the fixed CL_TABLE.
|
|
543
|
+
function _readClSymbol(r) {
|
|
544
|
+
// Peek 4 bits - see comment in `_makeTableDecoder.read`.
|
|
545
|
+
if (r.canExpectMore && r.p + 4 > r.eofBits) _eofThrow(r);
|
|
546
|
+
const idx = bitstream.readBits(r.d, r.p, 0x0F);
|
|
547
|
+
const entry = _CL_TABLE[idx];
|
|
548
|
+
r.p += entry & 0x0F;
|
|
549
|
+
if (r.p > r.eofBits) _eofThrow(r);
|
|
550
|
+
return entry >>> 4;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function _readComplexPrefixCode(r, alphabetSize, hskip) {
|
|
554
|
+
// --- Phase 1: read code lengths for the CL alphabet (§3.5) ---
|
|
555
|
+
//
|
|
556
|
+
// The CL alphabet has 18 symbols (0-15 = literal CL, 16/17 =
|
|
557
|
+
// repeat opcodes). Their CL-of-CL values ∈ [0, 5] are stored in
|
|
558
|
+
// the fixed-shape variable-length code decoded via `_CL_TABLE`.
|
|
559
|
+
// Read order is `_CL_ORDER`; HSKIP first entries are skipped
|
|
560
|
+
// (implicitly zero).
|
|
561
|
+
|
|
562
|
+
const clOfCl = new u8(18);
|
|
563
|
+
let clOfClKraft = 0;
|
|
564
|
+
for (let i = hskip; i < 18; ++i) {
|
|
565
|
+
const v = _readClSymbol(r);
|
|
566
|
+
clOfCl[_CL_ORDER[i]] = v;
|
|
567
|
+
if (v > 0) {
|
|
568
|
+
clOfClKraft += 32 >> v;
|
|
569
|
+
if (clOfClKraft > 32) {
|
|
570
|
+
_err('EBADSTREAM', 'CL-of-CL Kraft > 1', r.p);
|
|
571
|
+
}
|
|
572
|
+
if (clOfClKraft === 32) {
|
|
573
|
+
// Code is now complete; trailing CL-of-CL values
|
|
574
|
+
// are implicitly 0 (already from typed-array init).
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (clOfClKraft !== 32) {
|
|
580
|
+
// Single-symbol CL-of-CL: only one non-zero CL → 0-bit code
|
|
581
|
+
// for that single symbol. Count to confirm.
|
|
582
|
+
let nz = 0;
|
|
583
|
+
for (let i = 0; i < 18; ++i) if (clOfCl[i]) ++nz;
|
|
584
|
+
if (nz !== 1) {
|
|
585
|
+
_err('EBADSTREAM', 'CL-of-CL Kraft incomplete (sum=' + clOfClKraft + ')', r.p);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Build the CL decoder (over the CL alphabet, 18 symbols).
|
|
590
|
+
const clDecoder = clOfClKraft === 32
|
|
591
|
+
? _makeTableDecoder(clOfCl, 18)
|
|
592
|
+
: (() => {
|
|
593
|
+
// Single-symbol case
|
|
594
|
+
let sym = 0;
|
|
595
|
+
for (let i = 0; i < 18; ++i) if (clOfCl[i]) { sym = i; break; }
|
|
596
|
+
return _makeSingleSymbolDecoder(sym);
|
|
597
|
+
})();
|
|
598
|
+
|
|
599
|
+
// --- Phase 2: read RLE-encoded code lengths for the target alphabet ---
|
|
600
|
+
|
|
601
|
+
const lens = new u8(alphabetSize);
|
|
602
|
+
let i = 0;
|
|
603
|
+
let prev = 8; // initial "previous non-zero code length"
|
|
604
|
+
let prevRep = 0; // last opcode seen: 0=none, 16, or 17
|
|
605
|
+
let extraTotal = 0; // running count of the active repeat chain
|
|
606
|
+
let kraftSum = 0; // sum of (32768 >> cl) over emitted non-zero CLs
|
|
607
|
+
|
|
608
|
+
while (i < alphabetSize) {
|
|
609
|
+
const sym = clDecoder.read(r);
|
|
610
|
+
if (sym < 16) {
|
|
611
|
+
lens[i++] = sym;
|
|
612
|
+
if (sym > 0) {
|
|
613
|
+
prev = sym;
|
|
614
|
+
kraftSum += 32768 >> sym;
|
|
615
|
+
if (kraftSum > 32768) {
|
|
616
|
+
_err('EBADSTREAM', 'target-alphabet Kraft > 1', r.p);
|
|
617
|
+
}
|
|
618
|
+
if (kraftSum === 32768) break; // complete; rest implicitly 0
|
|
619
|
+
}
|
|
620
|
+
prevRep = 0;
|
|
621
|
+
} else if (sym === 16) {
|
|
622
|
+
let extra;
|
|
623
|
+
if (prevRep === 16) {
|
|
624
|
+
const newCount = 4 * (extraTotal - 2) + _readBits(r, 2) + 3;
|
|
625
|
+
extra = newCount - extraTotal;
|
|
626
|
+
extraTotal = newCount;
|
|
627
|
+
} else {
|
|
628
|
+
extraTotal = _readBits(r, 2) + 3;
|
|
629
|
+
extra = extraTotal;
|
|
630
|
+
}
|
|
631
|
+
prevRep = 16;
|
|
632
|
+
if (i + extra > alphabetSize) {
|
|
633
|
+
_err('EBADSTREAM', 'CL repeat-16 overflows alphabet', r.p);
|
|
634
|
+
}
|
|
635
|
+
const inc = 32768 >> prev;
|
|
636
|
+
for (let k = 0; k < extra; ++k) {
|
|
637
|
+
lens[i++] = prev;
|
|
638
|
+
kraftSum += inc;
|
|
639
|
+
if (kraftSum > 32768) {
|
|
640
|
+
_err('EBADSTREAM', 'target-alphabet Kraft > 1 mid-repeat', r.p);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
if (kraftSum === 32768) break;
|
|
644
|
+
} else { // sym === 17 - repeat zeros
|
|
645
|
+
let extra;
|
|
646
|
+
if (prevRep === 17) {
|
|
647
|
+
const newCount = 8 * (extraTotal - 2) + _readBits(r, 3) + 3;
|
|
648
|
+
extra = newCount - extraTotal;
|
|
649
|
+
extraTotal = newCount;
|
|
650
|
+
} else {
|
|
651
|
+
extraTotal = _readBits(r, 3) + 3;
|
|
652
|
+
extra = extraTotal;
|
|
653
|
+
}
|
|
654
|
+
prevRep = 17;
|
|
655
|
+
if (i + extra > alphabetSize) {
|
|
656
|
+
_err('EBADSTREAM', 'CL repeat-17 overflows alphabet', r.p);
|
|
657
|
+
}
|
|
658
|
+
for (let k = 0; k < extra; ++k) lens[i++] = 0;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Validate completeness
|
|
663
|
+
if (kraftSum === 32768) {
|
|
664
|
+
return _makeTableDecoder(lens, alphabetSize);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Spec: exactly one non-zero CL → 0-bit code for that single symbol.
|
|
668
|
+
let nz = 0, nzSym = 0;
|
|
669
|
+
for (let j = 0; j < lens.length; ++j) if (lens[j]) { ++nz; nzSym = j; }
|
|
670
|
+
if (nz === 1) return _makeSingleSymbolDecoder(nzSym);
|
|
671
|
+
|
|
672
|
+
_err('EBADSTREAM', 'target-alphabet Kraft incomplete (sum=' + kraftSum + ', non-zero=' + nz + ')', r.p);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function _readPrefixCode(r, alphabetSize) {
|
|
676
|
+
if (alphabetSize < 1) _err('EBADARG', 'prefix code alphabet size must be >= 1');
|
|
677
|
+
const head = _readBits(r, 2);
|
|
678
|
+
if (head === 1) return _readSimplePrefixCode(r, alphabetSize);
|
|
679
|
+
return _readComplexPrefixCode(r, alphabetSize, head);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// --- §6, §7, §9.2 - Compressed meta-block header ---
|
|
683
|
+
//
|
|
684
|
+
// NBLTYPES{L,I,D}, NTREES{L,D}: variable-length 1..11-bit code
|
|
685
|
+
// (RFC 7932 §9.2 table). 1-bit "0" → value 1. Otherwise 4-bit
|
|
686
|
+
// category prefix indicates how many extra bits encode the value.
|
|
687
|
+
|
|
688
|
+
function _readVarLenCount(r) {
|
|
689
|
+
if (!_readBit(r)) return 1;
|
|
690
|
+
const v = _readBits(r, 3);
|
|
691
|
+
if (v === 0) return 2;
|
|
692
|
+
return (1 << v) + 1 + _readBits(r, v);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// --- §6 - Block count code alphabet (26 symbols) ---
|
|
696
|
+
|
|
697
|
+
const _BLOCK_COUNT_EXTRA = new u8([
|
|
698
|
+
2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
|
|
699
|
+
6, 6, 7, 8, 9, 10, 11, 12, 13, 24,
|
|
700
|
+
]);
|
|
701
|
+
const _BLOCK_COUNT_BASE = new Int32Array([
|
|
702
|
+
1, 5, 9, 13, 17, 25, 33, 41, 49, 65, 81, 97, 113, 145, 177, 209,
|
|
703
|
+
241, 305, 369, 497, 753, 1265, 2289, 4337, 8433, 16625,
|
|
704
|
+
]);
|
|
705
|
+
|
|
706
|
+
function _readBlockCount(r, blockCountCode) {
|
|
707
|
+
const sym = blockCountCode.read(r);
|
|
708
|
+
if (sym < 0 || sym >= 26) _err('EBADSTREAM', 'block count code out of range', r.p);
|
|
709
|
+
const extraBits = _BLOCK_COUNT_EXTRA[sym];
|
|
710
|
+
const extra = extraBits <= 16 ? _readBits(r, extraBits) : _readBitsBig(r, extraBits);
|
|
711
|
+
return _BLOCK_COUNT_BASE[sym] + extra;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// --- §7.3 - RLEMAX (1..5 bits) ---
|
|
715
|
+
|
|
716
|
+
function _readRleMax(r) {
|
|
717
|
+
if (!_readBit(r)) return 0;
|
|
718
|
+
return _readBits(r, 4) + 1;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// --- §7.3 - Inverse move-to-front transform on a Uint8Array ---
|
|
722
|
+
|
|
723
|
+
function _inverseMoveToFront(v) {
|
|
724
|
+
const mtf = new u8(256);
|
|
725
|
+
for (let i = 0; i < 256; ++i) mtf[i] = i;
|
|
726
|
+
for (let i = 0; i < v.length; ++i) {
|
|
727
|
+
const idx = v[i];
|
|
728
|
+
const val = mtf[idx];
|
|
729
|
+
v[i] = val;
|
|
730
|
+
for (let j = idx; j > 0; --j) mtf[j] = mtf[j - 1];
|
|
731
|
+
mtf[0] = val;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// --- §7.3 - Context map decoding ---
|
|
736
|
+
//
|
|
737
|
+
// Format: RLEMAX (1..5 bits), prefix code over alphabet
|
|
738
|
+
// `ntrees + rlemax`, then `size` symbols using RLE for zero values,
|
|
739
|
+
// then IMTF bit. If RLEMAX is 0, symbols are direct values.
|
|
740
|
+
|
|
741
|
+
function _readContextMap(r, size, ntrees) {
|
|
742
|
+
const rlemax = _readRleMax(r);
|
|
743
|
+
const cmap = new u8(size);
|
|
744
|
+
const code = _readPrefixCode(r, ntrees + rlemax);
|
|
745
|
+
let i = 0;
|
|
746
|
+
while (i < size) {
|
|
747
|
+
const sym = code.read(r);
|
|
748
|
+
if (sym === 0) {
|
|
749
|
+
cmap[i++] = 0;
|
|
750
|
+
} else if (sym <= rlemax) {
|
|
751
|
+
// RLE: repeat zero (1 << sym) to (1 << (sym+1)) - 1 times
|
|
752
|
+
const reps = (1 << sym) + _readBits(r, sym);
|
|
753
|
+
if (i + reps > size) _err('EBADSTREAM', 'context map RLE overflow', r.p);
|
|
754
|
+
for (let j = 0; j < reps; ++j) cmap[i++] = 0;
|
|
755
|
+
} else {
|
|
756
|
+
cmap[i++] = sym - rlemax;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (_readBit(r)) _inverseMoveToFront(cmap);
|
|
760
|
+
return cmap;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// --- §9.2 - Compressed meta-block header ---
|
|
764
|
+
//
|
|
765
|
+
// Returns a "header" object holding everything the command loop
|
|
766
|
+
// (step 6+) needs to start decoding the body: per-category block
|
|
767
|
+
// type/count codes & first block counts, NPOSTFIX/NDIRECT,
|
|
768
|
+
// context modes, context maps, and the three arrays of prefix
|
|
769
|
+
// codes (literal, insert-and-copy, distance).
|
|
770
|
+
|
|
771
|
+
function _readBlockCategory(r, h, cat) {
|
|
772
|
+
const nbltypes = _readVarLenCount(r);
|
|
773
|
+
h['nbltypes' + cat] = nbltypes;
|
|
774
|
+
if (nbltypes >= 2) {
|
|
775
|
+
h['htreeBtype' + cat] = _readPrefixCode(r, nbltypes + 2);
|
|
776
|
+
h['htreeBlen' + cat] = _readPrefixCode(r, 26);
|
|
777
|
+
h['blen' + cat] = _readBlockCount(r, h['htreeBlen' + cat]);
|
|
778
|
+
} else {
|
|
779
|
+
// Spec §9.2/§10: with a single block type the count is
|
|
780
|
+
// effectively unbounded - initialise to a value larger
|
|
781
|
+
// than any plausible MLEN so the command loop never trips
|
|
782
|
+
// a block-switch.
|
|
783
|
+
h['blen' + cat] = 16777216;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function _readCompressedMetaBlockHeader(r, largeWindow) {
|
|
788
|
+
const h = {};
|
|
789
|
+
|
|
790
|
+
_readBlockCategory(r, h, 'L');
|
|
791
|
+
_readBlockCategory(r, h, 'I');
|
|
792
|
+
_readBlockCategory(r, h, 'D');
|
|
793
|
+
|
|
794
|
+
// NPOSTFIX (2 bits) + NDIRECT (4 bits, then left-shifted by NPOSTFIX)
|
|
795
|
+
h.npostfix = _readBits(r, 2);
|
|
796
|
+
h.ndirect = _readBits(r, 4) << h.npostfix;
|
|
797
|
+
|
|
798
|
+
// Context modes: NBLTYPESL * 2 bits, one mode per literal block type
|
|
799
|
+
h.cmode = new u8(h.nbltypesL);
|
|
800
|
+
for (let i = 0; i < h.nbltypesL; ++i) {
|
|
801
|
+
h.cmode[i] = _readBits(r, 2);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// NTREESL + CMAPL (literal context map)
|
|
805
|
+
h.ntreesL = _readVarLenCount(r);
|
|
806
|
+
const cmapLSize = 64 * h.nbltypesL;
|
|
807
|
+
h.cmapL = h.ntreesL >= 2
|
|
808
|
+
? _readContextMap(r, cmapLSize, h.ntreesL)
|
|
809
|
+
: new u8(cmapLSize);
|
|
810
|
+
|
|
811
|
+
// NTREESD + CMAPD (distance context map)
|
|
812
|
+
h.ntreesD = _readVarLenCount(r);
|
|
813
|
+
const cmapDSize = 4 * h.nbltypesD;
|
|
814
|
+
h.cmapD = h.ntreesD >= 2
|
|
815
|
+
? _readContextMap(r, cmapDSize, h.ntreesD)
|
|
816
|
+
: new u8(cmapDSize);
|
|
817
|
+
|
|
818
|
+
// NTREESL prefix codes for literals (alphabet 256)
|
|
819
|
+
h.htreeL = new Array(h.ntreesL);
|
|
820
|
+
for (let i = 0; i < h.ntreesL; ++i) h.htreeL[i] = _readPrefixCode(r, 256);
|
|
821
|
+
|
|
822
|
+
// NBLTYPESI prefix codes for insert-and-copy lengths (alphabet 704)
|
|
823
|
+
h.htreeI = new Array(h.nbltypesI);
|
|
824
|
+
for (let i = 0; i < h.nbltypesI; ++i) h.htreeI[i] = _readPrefixCode(r, 704);
|
|
825
|
+
|
|
826
|
+
// NTREESD prefix codes for distances. Alphabet size per §4 of
|
|
827
|
+
// RFC 7932 - (48 << NPOSTFIX) - or §6 of RFC 9841 for large
|
|
828
|
+
// window mode - (124 << NPOSTFIX).
|
|
829
|
+
const distMult = largeWindow ? 124 : 48;
|
|
830
|
+
const distAlphabet = 16 + h.ndirect + (distMult << h.npostfix);
|
|
831
|
+
h.htreeD = new Array(h.ntreesD);
|
|
832
|
+
for (let i = 0; i < h.ntreesD; ++i) h.htreeD[i] = _readPrefixCode(r, distAlphabet);
|
|
833
|
+
|
|
834
|
+
h.distAlphabet = distAlphabet;
|
|
835
|
+
h.largeWindow = !!largeWindow;
|
|
836
|
+
return h;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// --- §5 - Insert-and-copy length codes ---
|
|
840
|
+
//
|
|
841
|
+
// The IAC alphabet has 704 symbols partitioned into 11 "cells" of
|
|
842
|
+
// 64 symbols. The cell index gives (insertCodeBase, copyCodeBase)
|
|
843
|
+
// and whether the distance code is the implicit zero (cells 0/1).
|
|
844
|
+
// Within a cell, bits 0..2 of the IAC sym choose the copy code
|
|
845
|
+
// (within 8 codes from the base) and bits 3..5 choose the insert
|
|
846
|
+
// code. See spec table at §5.
|
|
847
|
+
|
|
848
|
+
const _IAC_I_BASE = new u8([0, 0, 0, 0, 8, 8, 0, 16, 8, 16, 16]);
|
|
849
|
+
const _IAC_C_BASE = new u8([0, 8, 0, 8, 0, 8, 16, 0, 16, 8, 16]);
|
|
850
|
+
// Cells 0, 1 (IAC sym < 128) use implicit distance code 0.
|
|
851
|
+
|
|
852
|
+
const _INSERT_EXTRA = new u8([
|
|
853
|
+
0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
|
|
854
|
+
6, 7, 8, 9, 10, 12, 14, 24,
|
|
855
|
+
]);
|
|
856
|
+
const _INSERT_BASE = new Int32Array([
|
|
857
|
+
0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50, 66, 98,
|
|
858
|
+
130, 194, 322, 578, 1090, 2114, 6210, 22594,
|
|
859
|
+
]);
|
|
860
|
+
const _COPY_EXTRA = new u8([
|
|
861
|
+
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
|
|
862
|
+
5, 5, 6, 7, 8, 9, 10, 24,
|
|
863
|
+
]);
|
|
864
|
+
const _COPY_BASE = new Int32Array([
|
|
865
|
+
2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30, 38, 54,
|
|
866
|
+
70, 102, 134, 198, 326, 582, 1094, 2118,
|
|
867
|
+
]);
|
|
868
|
+
|
|
869
|
+
function _splitIacSym(iacSym) {
|
|
870
|
+
const cell = iacSym >> 6;
|
|
871
|
+
if (cell >= 11) _err('EBADSTREAM', 'iac symbol ' + iacSym + ' out of range');
|
|
872
|
+
return {
|
|
873
|
+
insertCode: _IAC_I_BASE[cell] + ((iacSym >> 3) & 7),
|
|
874
|
+
copyCode: _IAC_C_BASE[cell] + (iacSym & 7),
|
|
875
|
+
distZero: cell < 2,
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function _readLength(r, baseArr, extraArr, code) {
|
|
880
|
+
const eb = extraArr[code];
|
|
881
|
+
const extra = eb <= 16 ? _readBits(r, eb) : _readBitsBig(r, eb);
|
|
882
|
+
return baseArr[code] + extra;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// --- §4 - Distance decoding ---
|
|
886
|
+
//
|
|
887
|
+
// Special symbols 0..15 reference past distances with offsets.
|
|
888
|
+
// Symbols 16..15+NDIRECT are direct distances 1..NDIRECT.
|
|
889
|
+
// Symbols >= 16+NDIRECT use the NPOSTFIX/NDIRECT formula.
|
|
890
|
+
|
|
891
|
+
// Maps dsym 0..15 → [lastDistIndex, offset]
|
|
892
|
+
const _SPECIAL_DIST = new Int8Array([
|
|
893
|
+
0, 0, 1, 0, 2, 0, 3, 0,
|
|
894
|
+
0, -1, 0, 1, 0, -2, 0, 2, 0, -3, 0, 3,
|
|
895
|
+
1, -1, 1, 1, 1, -2, 1, 2, 1, -3, 1, 3,
|
|
896
|
+
]);
|
|
897
|
+
|
|
898
|
+
function _decodeDistanceSymbol(r, dsym, lastDist, npostfix, ndirect) {
|
|
899
|
+
if (dsym < 16) {
|
|
900
|
+
const idx = _SPECIAL_DIST[dsym * 2];
|
|
901
|
+
const off = _SPECIAL_DIST[dsym * 2 + 1];
|
|
902
|
+
const d = lastDist[idx] + off;
|
|
903
|
+
if (d <= 0) _err('EBADSTREAM', 'special distance code ' + dsym + ' resolved to non-positive ' + d, r.p);
|
|
904
|
+
return d;
|
|
905
|
+
}
|
|
906
|
+
if (dsym < 16 + ndirect) return dsym - 15;
|
|
907
|
+
const dcOffset = dsym - ndirect - 16;
|
|
908
|
+
const ndistbits = 1 + (dcOffset >> (npostfix + 1));
|
|
909
|
+
// RFC 9841 §6 allows up to 62-bit distances. ndistbits caps
|
|
910
|
+
// at 62 (per spec). Three arithmetic paths:
|
|
911
|
+
// - ndistbits ≤ 29 : 32-bit shift (fastest)
|
|
912
|
+
// - ndistbits ≤ 50 : Math.pow + Number multiplications
|
|
913
|
+
// (Number safe to 2^53)
|
|
914
|
+
// - ndistbits > 50 : BigInt intermediate, Number final
|
|
915
|
+
// (valid streams' distances fit Number
|
|
916
|
+
// since outLen ≤ 2³² and dict capacity
|
|
917
|
+
// ≤ 2²⁰)
|
|
918
|
+
if (ndistbits > 62) {
|
|
919
|
+
_err('EBADSTREAM', 'distance ndistbits ' + ndistbits + ' > 62 (spec ceiling)', r.p);
|
|
920
|
+
}
|
|
921
|
+
const hcode = dcOffset >> npostfix;
|
|
922
|
+
const lcode = dcOffset & ((1 << npostfix) - 1);
|
|
923
|
+
|
|
924
|
+
if (ndistbits <= 29) {
|
|
925
|
+
const dextra = _readBits(r, ndistbits);
|
|
926
|
+
const offset = ((2 + (hcode & 1)) << ndistbits) - 4;
|
|
927
|
+
return ((offset + dextra) << npostfix) + lcode + ndirect + 1;
|
|
928
|
+
}
|
|
929
|
+
if (ndistbits <= 50) {
|
|
930
|
+
const dextra = _readBitsBig(r, ndistbits);
|
|
931
|
+
const pow = Math.pow(2, ndistbits);
|
|
932
|
+
const offset = (2 + (hcode & 1)) * pow - 4;
|
|
933
|
+
return (offset + dextra) * (1 << npostfix) + lcode + ndirect + 1;
|
|
934
|
+
}
|
|
935
|
+
// BigInt path for ndistbits 51..62.
|
|
936
|
+
const dextra = _readBitsBigInt(r, ndistbits);
|
|
937
|
+
const pow = 1n << BigInt(ndistbits);
|
|
938
|
+
const offset = BigInt(2 + (hcode & 1)) * pow - 4n;
|
|
939
|
+
const distBig = (offset + dextra) * BigInt(1 << npostfix)
|
|
940
|
+
+ BigInt(lcode + ndirect + 1);
|
|
941
|
+
if (distBig > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
942
|
+
_err('EBADSTREAM', 'distance exceeds Number safe range (' + distBig + ')', r.p);
|
|
943
|
+
}
|
|
944
|
+
return Number(distBig);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// --- §7.1 - Literal context lookup tables ---
|
|
948
|
+
//
|
|
949
|
+
// CRCs (RFC 7932 §7.1): Lut0=0x8e91efb7, Lut1=0xd01a32f4, Lut2=0x0dd7a0d6.
|
|
950
|
+
|
|
951
|
+
const _LUT0 = new u8([
|
|
952
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,
|
|
953
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
954
|
+
8,12,16,12,12,20,12,16,24,28,12,12,32,12,36,12,
|
|
955
|
+
44,44,44,44,44,44,44,44,44,44,32,32,24,40,28,12,
|
|
956
|
+
12,48,52,52,52,48,52,52,52,48,52,52,52,52,52,48,
|
|
957
|
+
52,52,52,52,52,48,52,52,52,52,52,24,12,28,12,12,
|
|
958
|
+
12,56,60,60,60,56,60,60,60,56,60,60,60,60,60,56,
|
|
959
|
+
60,60,60,60,60,56,60,60,60,60,60,24,12,28,12, 0,
|
|
960
|
+
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
|
|
961
|
+
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
|
|
962
|
+
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
|
|
963
|
+
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
|
|
964
|
+
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
|
|
965
|
+
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
|
|
966
|
+
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
|
|
967
|
+
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
|
|
968
|
+
]);
|
|
969
|
+
const _LUT1 = new u8([
|
|
970
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
971
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
972
|
+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
973
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
|
|
974
|
+
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
975
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
|
|
976
|
+
1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
977
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0,
|
|
978
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
979
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
980
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
981
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
982
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
983
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
984
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
985
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
986
|
+
]);
|
|
987
|
+
const _LUT2 = new u8([
|
|
988
|
+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
989
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
990
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
991
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
992
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
993
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
994
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
995
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
996
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
997
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
998
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
999
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
1000
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
1001
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
1002
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
1003
|
+
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
|
|
1004
|
+
]);
|
|
1005
|
+
|
|
1006
|
+
function _contextIdLit(mode, p1, p2) {
|
|
1007
|
+
if (mode === 0) return p1 & 0x3F;
|
|
1008
|
+
if (mode === 1) return p1 >> 2;
|
|
1009
|
+
if (mode === 2) return _LUT0[p1] | _LUT1[p2];
|
|
1010
|
+
return (_LUT2[p1] << 3) | _LUT2[p2];
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// --- §6 - Block type decoding ---
|
|
1014
|
+
|
|
1015
|
+
function _decodeBlockType(code, nbltypes, current, prev, r) {
|
|
1016
|
+
const sym = code.read(r);
|
|
1017
|
+
if (sym === 0) return prev;
|
|
1018
|
+
if (sym === 1) return (current + 1) % nbltypes;
|
|
1019
|
+
const t = sym - 2;
|
|
1020
|
+
if (t >= nbltypes) _err('EBADSTREAM', 'block type ' + sym + ' out of range', r.p);
|
|
1021
|
+
return t;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// --- Output buffer growth ---
|
|
1025
|
+
|
|
1026
|
+
function _ensureCapacity(state, additional) {
|
|
1027
|
+
if (state.outLen + additional > state.out.length) {
|
|
1028
|
+
let newSize = state.out.length;
|
|
1029
|
+
while (newSize < state.outLen + additional) newSize *= 2;
|
|
1030
|
+
const grown = new u8(newSize);
|
|
1031
|
+
grown.set(state.out.subarray(0, state.outLen));
|
|
1032
|
+
state.out = grown;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function _ensureDictLoaded(r) {
|
|
1037
|
+
if (brotliDict.hasWords()) return;
|
|
1038
|
+
if (brotliDictWords.isLoaded) {
|
|
1039
|
+
brotliDict.setWords(brotliDictWords.blob);
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
_err('ENEEDDICT',
|
|
1043
|
+
'brotli static dictionary required for this stream - load it via ' +
|
|
1044
|
+
'brotliDictWords.setBlob(bytes) or .load(url), then the decoder ' +
|
|
1045
|
+
'will auto-wire on the next dict-ref',
|
|
1046
|
+
r.p);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// --- §10 - Command loop for one compressed meta-block ---
|
|
1050
|
+
|
|
1051
|
+
function _decodeCompressedBody(r, h, state, mlen) {
|
|
1052
|
+
const startLen = state.outLen;
|
|
1053
|
+
|
|
1054
|
+
// Per-meta-block block-switch state (per §6: initial types 0/1)
|
|
1055
|
+
let btypeL = 0, prevBtypeL = 1;
|
|
1056
|
+
let btypeI = 0, prevBtypeI = 1;
|
|
1057
|
+
let btypeD = 0, prevBtypeD = 1;
|
|
1058
|
+
let blenL = h.blenL, blenI = h.blenI, blenD = h.blenD;
|
|
1059
|
+
|
|
1060
|
+
while (state.outLen - startLen < mlen) {
|
|
1061
|
+
// --- Block-switch for IAC ---
|
|
1062
|
+
if (blenI === 0) {
|
|
1063
|
+
const nb = _decodeBlockType(h.htreeBtypeI, h.nbltypesI, btypeI, prevBtypeI, r);
|
|
1064
|
+
prevBtypeI = btypeI; btypeI = nb;
|
|
1065
|
+
blenI = _readBlockCount(r, h.htreeBlenI);
|
|
1066
|
+
}
|
|
1067
|
+
blenI--;
|
|
1068
|
+
|
|
1069
|
+
// --- IAC sym → ILEN, CLEN, distZero ---
|
|
1070
|
+
const iacSym = h.htreeI[btypeI].read(r);
|
|
1071
|
+
const { insertCode, copyCode, distZero } = _splitIacSym(iacSym);
|
|
1072
|
+
const ilen = _readLength(r, _INSERT_BASE, _INSERT_EXTRA, insertCode);
|
|
1073
|
+
const clen = _readLength(r, _COPY_BASE, _COPY_EXTRA, copyCode);
|
|
1074
|
+
|
|
1075
|
+
// --- Insert ILEN literals ---
|
|
1076
|
+
// §10: "if number of uncompressed bytes produced in the
|
|
1077
|
+
// loop for this meta-block is MLEN, then break from loop".
|
|
1078
|
+
// The Google reference decoder still consumes the literal
|
|
1079
|
+
// codes from the stream (encoder put them there) but skips
|
|
1080
|
+
// writing them. We follow the same lenient interpretation:
|
|
1081
|
+
// read every literal, write only as many as fit in MLEN.
|
|
1082
|
+
if (ilen > 0) {
|
|
1083
|
+
_ensureCapacity(state, ilen);
|
|
1084
|
+
for (let k = 0; k < ilen; ++k) {
|
|
1085
|
+
if (blenL === 0) {
|
|
1086
|
+
const nb = _decodeBlockType(h.htreeBtypeL, h.nbltypesL, btypeL, prevBtypeL, r);
|
|
1087
|
+
prevBtypeL = btypeL; btypeL = nb;
|
|
1088
|
+
blenL = _readBlockCount(r, h.htreeBlenL);
|
|
1089
|
+
}
|
|
1090
|
+
blenL--;
|
|
1091
|
+
|
|
1092
|
+
const cidL = _contextIdLit(h.cmode[btypeL], state.p1, state.p2);
|
|
1093
|
+
const treeIdx = h.cmapL[64 * btypeL + cidL];
|
|
1094
|
+
const lit = h.htreeL[treeIdx].read(r);
|
|
1095
|
+
if (state.outLen - startLen < mlen) {
|
|
1096
|
+
state.out[state.outLen++] = lit;
|
|
1097
|
+
state.p2 = state.p1; state.p1 = lit;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
// --- Last command's copy is ignored if we've reached MLEN ---
|
|
1103
|
+
if (state.outLen - startLen >= mlen) break;
|
|
1104
|
+
|
|
1105
|
+
// --- Resolve distance ---
|
|
1106
|
+
let distance, dsym = 0;
|
|
1107
|
+
if (distZero) {
|
|
1108
|
+
distance = state.lastDist[0];
|
|
1109
|
+
} else {
|
|
1110
|
+
if (blenD === 0) {
|
|
1111
|
+
const nb = _decodeBlockType(h.htreeBtypeD, h.nbltypesD, btypeD, prevBtypeD, r);
|
|
1112
|
+
prevBtypeD = btypeD; btypeD = nb;
|
|
1113
|
+
blenD = _readBlockCount(r, h.htreeBlenD);
|
|
1114
|
+
}
|
|
1115
|
+
blenD--;
|
|
1116
|
+
|
|
1117
|
+
const cidD = clen > 4 ? 3 : clen - 2;
|
|
1118
|
+
const treeIdx = h.cmapD[4 * btypeD + cidD];
|
|
1119
|
+
dsym = h.htreeD[treeIdx].read(r);
|
|
1120
|
+
distance = _decodeDistanceSymbol(r, dsym, state.lastDist, h.npostfix, h.ndirect);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
const maxAllowed = state.outLen < state.windowSize ? state.outLen : state.windowSize;
|
|
1124
|
+
const lz77DictLen = state.lz77Dict ? state.lz77Dict.length : 0;
|
|
1125
|
+
// RFC 9841 §3.2: distance values in (max+1)..(max+L) are
|
|
1126
|
+
// refs into the LZ77 dictionary; only beyond (max+L) is a
|
|
1127
|
+
// static-dict reference (with word_id offset by L).
|
|
1128
|
+
const isStaticDictRef = distance > maxAllowed + lz77DictLen;
|
|
1129
|
+
const isLz77DictRef = !isStaticDictRef && distance > maxAllowed && lz77DictLen > 0;
|
|
1130
|
+
|
|
1131
|
+
// Push to ring buffer unless code 0 or *static* dict ref.
|
|
1132
|
+
// LZ77 dictionary references DO push (treated as regular
|
|
1133
|
+
// LZ77 copies per §3.2).
|
|
1134
|
+
if (!distZero && dsym !== 0 && !isStaticDictRef) {
|
|
1135
|
+
state.lastDist[3] = state.lastDist[2];
|
|
1136
|
+
state.lastDist[2] = state.lastDist[1];
|
|
1137
|
+
state.lastDist[1] = state.lastDist[0];
|
|
1138
|
+
state.lastDist[0] = distance;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
// --- Copy from past output / LZ77 dictionary / static dictionary ---
|
|
1142
|
+
// §10 ("copy length of the last command can have any value")
|
|
1143
|
+
// is interpreted permissively: clamp to remaining MLEN.
|
|
1144
|
+
const remaining = mlen - (state.outLen - startLen);
|
|
1145
|
+
if (isStaticDictRef) {
|
|
1146
|
+
const wordId = distance - maxAllowed - 1 - lz77DictLen;
|
|
1147
|
+
let transformed;
|
|
1148
|
+
|
|
1149
|
+
if (state.resolveStaticDictRef) {
|
|
1150
|
+
// Extension hook (RFC 9841 §3.1 custom-dict path,
|
|
1151
|
+
// owned by `brotli_shared`). The callback handles
|
|
1152
|
+
// length bounds, dict selection, transform
|
|
1153
|
+
// application - brotli.js stays RFC 7932 here.
|
|
1154
|
+
const cidL = _contextIdLit(h.cmode[btypeL], state.p1, state.p2);
|
|
1155
|
+
transformed = state.resolveStaticDictRef(state, wordId, clen, cidL, r);
|
|
1156
|
+
} else {
|
|
1157
|
+
// RFC 7932 §8 - built-in static dictionary.
|
|
1158
|
+
if (clen < 4 || clen > 24) {
|
|
1159
|
+
_err('EBADSTREAM', 'static dict ref length ' + clen + ' not in [4, 24]', r.p);
|
|
1160
|
+
}
|
|
1161
|
+
_ensureDictLoaded(r);
|
|
1162
|
+
const nw = brotliDict.NWORDS(clen);
|
|
1163
|
+
const index = wordId % nw;
|
|
1164
|
+
const transformId = (wordId - index) / nw;
|
|
1165
|
+
if (transformId >= 121) _err('EBADSTREAM', 'static dict transform id ' + transformId + ' >= 121', r.p);
|
|
1166
|
+
const baseWord = brotliDict.lookupWord(clen, index);
|
|
1167
|
+
transformed = brotliDict.applyTransform(transformId, baseWord);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
const writeLen = transformed.length < remaining ? transformed.length : remaining;
|
|
1171
|
+
_ensureCapacity(state, writeLen);
|
|
1172
|
+
state.out.set(transformed.subarray(0, writeLen), state.outLen);
|
|
1173
|
+
state.outLen += writeLen;
|
|
1174
|
+
} else if (isLz77DictRef) {
|
|
1175
|
+
// RFC 9841 §3.2: dictionary_address = L + maxAllowed - distance
|
|
1176
|
+
// First copy from lz77Dict[dictAddr..L), then if more
|
|
1177
|
+
// is needed continue from output[windowStart..) (the
|
|
1178
|
+
// oldest byte still in the regular LZ77 window).
|
|
1179
|
+
const dictAddr = lz77DictLen + maxAllowed - distance;
|
|
1180
|
+
const fromDictBytes = lz77DictLen - dictAddr;
|
|
1181
|
+
const writeLen = clen < remaining ? clen : remaining;
|
|
1182
|
+
_ensureCapacity(state, writeLen);
|
|
1183
|
+
const outStart = state.outLen;
|
|
1184
|
+
const windowStart = outStart > state.windowSize ? outStart - state.windowSize : 0;
|
|
1185
|
+
let k = 0;
|
|
1186
|
+
while (k < writeLen && k < fromDictBytes) {
|
|
1187
|
+
state.out[state.outLen++] = state.lz77Dict[dictAddr + k];
|
|
1188
|
+
++k;
|
|
1189
|
+
}
|
|
1190
|
+
while (k < writeLen) {
|
|
1191
|
+
state.out[state.outLen++] = state.out[windowStart + (k - fromDictBytes)];
|
|
1192
|
+
++k;
|
|
1193
|
+
}
|
|
1194
|
+
} else {
|
|
1195
|
+
const writeLen = clen < remaining ? clen : remaining;
|
|
1196
|
+
_ensureCapacity(state, writeLen);
|
|
1197
|
+
for (let k = 0; k < writeLen; ++k) {
|
|
1198
|
+
state.out[state.outLen] = state.out[state.outLen - distance];
|
|
1199
|
+
state.outLen++;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
// Update p1, p2 from last 2 emitted bytes
|
|
1204
|
+
if (state.outLen >= 2) {
|
|
1205
|
+
state.p1 = state.out[state.outLen - 1];
|
|
1206
|
+
state.p2 = state.out[state.outLen - 2];
|
|
1207
|
+
} else if (state.outLen === 1) {
|
|
1208
|
+
state.p2 = state.p1;
|
|
1209
|
+
state.p1 = state.out[state.outLen - 1];
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// --- Public decompressor ---
|
|
1215
|
+
|
|
1216
|
+
function brotliDecompressSync(data, opts) {
|
|
1217
|
+
if (!(data instanceof u8)) _err('EBADARG', 'expected Uint8Array');
|
|
1218
|
+
if (data.length === 0) _err('EBADSTREAM', 'empty input');
|
|
1219
|
+
|
|
1220
|
+
// `opts._ext` is the private extension protocol consumed by
|
|
1221
|
+
// `brotli_shared`. brotli.js itself accepts only RFC 7932 opts
|
|
1222
|
+
// on its public surface - see brotli_shared.js for the user-
|
|
1223
|
+
// facing RFC 9841 options.
|
|
1224
|
+
const ext = (opts && opts._ext) || null;
|
|
1225
|
+
|
|
1226
|
+
const r = _makeReader(data);
|
|
1227
|
+
const wbitsInfo = _readWBITS(r, ext && ext.allowLargeWindow);
|
|
1228
|
+
const wbits = wbitsInfo.wbits;
|
|
1229
|
+
const largeWindow = wbitsInfo.largeWindow;
|
|
1230
|
+
// For WBITS ≤ 30 (32-bit-safe), (1 << wbits) is exact in JS.
|
|
1231
|
+
const windowSize = wbits >= 31 ? Math.pow(2, wbits) - 16 : (1 << wbits) - 16;
|
|
1232
|
+
|
|
1233
|
+
// Per-stream decoder state (§4 initialises last-distance ring
|
|
1234
|
+
// buffer at stream start, NOT per meta-block). The lz77Dict /
|
|
1235
|
+
// resolveStaticDictRef fields are extension points wired by
|
|
1236
|
+
// `brotli_shared` for RFC 9841 §3.1 / §3.2 - when absent the
|
|
1237
|
+
// decoder behaves as a pure RFC 7932 implementation.
|
|
1238
|
+
const state = {
|
|
1239
|
+
out: new u8(Math.max(data.length * 4, 1024)),
|
|
1240
|
+
outLen: 0,
|
|
1241
|
+
p1: 0,
|
|
1242
|
+
p2: 0,
|
|
1243
|
+
lastDist: new Int32Array([4, 11, 15, 16]),
|
|
1244
|
+
windowSize,
|
|
1245
|
+
lz77Dict: (ext && ext.lz77Dict) || null,
|
|
1246
|
+
largeWindow,
|
|
1247
|
+
resolveStaticDictRef: (ext && ext.resolveStaticDictRef) || null,
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
let sawLast = false;
|
|
1251
|
+
while (!sawLast) {
|
|
1252
|
+
const res = _decodeMetaBlockInto(r, state);
|
|
1253
|
+
if (res.last) sawLast = true;
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
// §9.3: "If the last command of the last non-empty meta-block
|
|
1257
|
+
// does not end on a byte boundary, the unused bits in the last
|
|
1258
|
+
// byte must be zeros."
|
|
1259
|
+
_verifyByteTailZero(r);
|
|
1260
|
+
|
|
1261
|
+
return state.outLen === state.out.length
|
|
1262
|
+
? state.out
|
|
1263
|
+
: state.out.slice(0, state.outLen);
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
// --- Stubs for unimplemented operations ---
|
|
1267
|
+
|
|
1268
|
+
// --- §9 / §11.1 - Encoder ---
|
|
1269
|
+
//
|
|
1270
|
+
// Produces a valid brotli stream by framing the input as a
|
|
1271
|
+
// sequence of uncompressed meta-blocks (max 65 536 bytes each)
|
|
1272
|
+
// followed by an empty ISLASTEMPTY terminator. The output is
|
|
1273
|
+
// ALWAYS larger than the input (overhead is ≈ 5 bytes plus 3
|
|
1274
|
+
// bytes per 64 KB chunk) - actual compression at higher quality
|
|
1275
|
+
// levels is the subject of steps 8+. Until those land, all
|
|
1276
|
+
// requested `quality` values fall back to this trivial encoder
|
|
1277
|
+
// (the output is still RFC 7932-conformant; encoders are simply
|
|
1278
|
+
// allowed to be lazy).
|
|
1279
|
+
//
|
|
1280
|
+
// Empty input is encoded as the single byte `0x06`
|
|
1281
|
+
// (WBITS=16 + ISLAST + ISLASTEMPTY).
|
|
1282
|
+
|
|
1283
|
+
function _encodeUncompressed(data) {
|
|
1284
|
+
if (data.length === 0) return new u8([0x06]);
|
|
1285
|
+
|
|
1286
|
+
// Conservative size: WBITS + per-chunk headers (≤4 bytes each
|
|
1287
|
+
// counting alignment) + data + final empty meta-block. The
|
|
1288
|
+
// exact bit math is handled by the writer below.
|
|
1289
|
+
const numChunks = Math.ceil(data.length / 65536);
|
|
1290
|
+
const buf = new u8(data.length + 5 + numChunks * 4);
|
|
1291
|
+
let p = 0;
|
|
1292
|
+
|
|
1293
|
+
// WBITS = 16 → 1 bit "0" (buf already zero-filled)
|
|
1294
|
+
p += 1;
|
|
1295
|
+
|
|
1296
|
+
for (let i = 0; i < data.length; i += 65536) {
|
|
1297
|
+
const chunkLen = Math.min(65536, data.length - i);
|
|
1298
|
+
// ISLAST=0, MNIBBLES code "00" (=4) - 3 zero bits (no-op)
|
|
1299
|
+
p += 3;
|
|
1300
|
+
// MLEN-1 over 16 bits
|
|
1301
|
+
bitstream.writeBits16(buf, p, chunkLen - 1);
|
|
1302
|
+
p += 16;
|
|
1303
|
+
// ISUNCOMPRESSED=1
|
|
1304
|
+
bitstream.writeBits(buf, p, 1);
|
|
1305
|
+
p += 1;
|
|
1306
|
+
// Align to byte boundary (fill bits already zero)
|
|
1307
|
+
p = (p + 7) & ~7;
|
|
1308
|
+
// Copy chunk payload
|
|
1309
|
+
buf.set(data.subarray(i, i + chunkLen), p >> 3);
|
|
1310
|
+
p += chunkLen * 8;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// Final empty meta-block: ISLAST=1 + ISLASTEMPTY=1
|
|
1314
|
+
bitstream.writeBits(buf, p, 3);
|
|
1315
|
+
p += 2;
|
|
1316
|
+
p = (p + 7) & ~7;
|
|
1317
|
+
|
|
1318
|
+
return buf.slice(0, p >> 3);
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// --- Step 9 - LZ77 + dynamic Huffman encoder ---
|
|
1322
|
+
//
|
|
1323
|
+
// Single compressed meta-block with:
|
|
1324
|
+
// * NBLTYPES{L,I,D} = 1 (no block switching)
|
|
1325
|
+
// * NPOSTFIX = 0, NDIRECT = 0 → distance alphabet 64
|
|
1326
|
+
// * CMODE = 2 (UTF8) - recorded but with NTREESL=1 the context
|
|
1327
|
+
// does not affect tree selection. Real context modelling
|
|
1328
|
+
// (NTREESL >= 2 with CMAPL) is queued for step 10.
|
|
1329
|
+
// * Dynamic canonical Huffman codes for the three per-category
|
|
1330
|
+
// alphabets (literal/IAC/distance), built from LZ77-derived
|
|
1331
|
+
// symbol frequencies.
|
|
1332
|
+
// * Prefix-code descriptors emitted in complex form (§3.5).
|
|
1333
|
+
|
|
1334
|
+
// §3.5 CL-of-CL code - fixed variable-length code mapping the
|
|
1335
|
+
// CL-of-CL value (0..5) to (bit-length, LSB-first bits). Derived
|
|
1336
|
+
// from the spec table by reading each "right-to-left" pattern.
|
|
1337
|
+
const _CL_OF_CL_VAL = new u8([0, 7, 3, 2, 1, 15]);
|
|
1338
|
+
const _CL_OF_CL_LEN = new u8([2, 4, 3, 2, 2, 4]);
|
|
1339
|
+
|
|
1340
|
+
function _insertCodeFor(len) {
|
|
1341
|
+
for (let c = 23; c >= 0; --c) {
|
|
1342
|
+
if (_INSERT_BASE[c] <= len) {
|
|
1343
|
+
return { code: c, extra: len - _INSERT_BASE[c], extraBits: _INSERT_EXTRA[c] };
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return null;
|
|
1347
|
+
}
|
|
1348
|
+
function _copyCodeFor(len) {
|
|
1349
|
+
for (let c = 23; c >= 0; --c) {
|
|
1350
|
+
if (_COPY_BASE[c] <= len) {
|
|
1351
|
+
return { code: c, extra: len - _COPY_BASE[c], extraBits: _COPY_EXTRA[c] };
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
return null;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
function _findIacSym(iCode, cCode, distZero) {
|
|
1358
|
+
const iHi = iCode >> 3, iLo = iCode & 7;
|
|
1359
|
+
const cHi = cCode >> 3, cLo = cCode & 7;
|
|
1360
|
+
let cell;
|
|
1361
|
+
if (distZero) {
|
|
1362
|
+
if (iHi !== 0 || cHi > 1) return -1;
|
|
1363
|
+
cell = cHi;
|
|
1364
|
+
} else {
|
|
1365
|
+
const T = [[2, 3, 6], [4, 5, 8], [7, 9, 10]];
|
|
1366
|
+
cell = T[iHi][cHi];
|
|
1367
|
+
}
|
|
1368
|
+
return (cell << 6) | (iLo << 3) | cLo;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// Encode a distance under arbitrary (NPOSTFIX, NDIRECT) per
|
|
1372
|
+
// RFC 7932 §4. Returns `{ dsym, extra, extraBits }` such that the
|
|
1373
|
+
// decoder's `_decodeDistanceSymbol` reproduces `distance`. The
|
|
1374
|
+
// direct range (distances 1..NDIRECT) gets dedicated symbols
|
|
1375
|
+
// 16..15+NDIRECT with no extra bits; longer distances use the
|
|
1376
|
+
// (ndistbits, hcode, lcode) decomposition.
|
|
1377
|
+
function _distanceCodeFor(distance, npostfix, ndirect) {
|
|
1378
|
+
if (npostfix == null) npostfix = 0;
|
|
1379
|
+
if (ndirect == null) ndirect = 0;
|
|
1380
|
+
if (distance >= 1 && distance <= ndirect) {
|
|
1381
|
+
return { dsym: 16 + (distance - 1), extra: 0, extraBits: 0 };
|
|
1382
|
+
}
|
|
1383
|
+
const dPrime = distance - ndirect - 1;
|
|
1384
|
+
if (dPrime < 0) return null;
|
|
1385
|
+
const lcodeMask = (1 << npostfix) - 1;
|
|
1386
|
+
const lcode = dPrime & lcodeMask;
|
|
1387
|
+
const rest = dPrime >> npostfix;
|
|
1388
|
+
for (let k = 1; k <= 24; ++k) {
|
|
1389
|
+
// hbit=0 bucket: offset = ((2 + 0) << k) - 4 = (1<<(k+1)) - 4
|
|
1390
|
+
const evenLo = (1 << (k + 1)) - 4;
|
|
1391
|
+
const evenHi = evenLo + (1 << k) - 1;
|
|
1392
|
+
if (rest >= evenLo && rest <= evenHi) {
|
|
1393
|
+
const dextra = rest - evenLo;
|
|
1394
|
+
const hcode = (k - 1) * 2;
|
|
1395
|
+
const dcOffset = (hcode << npostfix) | lcode;
|
|
1396
|
+
return { dsym: 16 + ndirect + dcOffset, extra: dextra, extraBits: k };
|
|
1397
|
+
}
|
|
1398
|
+
// hbit=1 bucket: offset = ((2 + 1) << k) - 4 = (3<<k) - 4
|
|
1399
|
+
const oddLo = (3 << k) - 4;
|
|
1400
|
+
const oddHi = oddLo + (1 << k) - 1;
|
|
1401
|
+
if (rest >= oddLo && rest <= oddHi) {
|
|
1402
|
+
const dextra = rest - oddLo;
|
|
1403
|
+
const hcode = (k - 1) * 2 + 1;
|
|
1404
|
+
const dcOffset = (hcode << npostfix) | lcode;
|
|
1405
|
+
return { dsym: 16 + ndirect + dcOffset, extra: dextra, extraBits: k };
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
return null;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
function _makeWriter(initialCapacity) {
|
|
1412
|
+
let buf = new u8(Math.max(initialCapacity | 0, 256));
|
|
1413
|
+
let bp = 0;
|
|
1414
|
+
function ensure(extraBits) {
|
|
1415
|
+
const need = ((bp + extraBits + 7) >> 3) + 4;
|
|
1416
|
+
if (need > buf.length) {
|
|
1417
|
+
let n = buf.length;
|
|
1418
|
+
while (n < need) n *= 2;
|
|
1419
|
+
const g = new u8(n);
|
|
1420
|
+
g.set(buf);
|
|
1421
|
+
buf = g;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
return {
|
|
1425
|
+
bits(n, v) {
|
|
1426
|
+
ensure(n);
|
|
1427
|
+
bitstream.writeBits16(buf, bp, v & ((1 << n) - 1));
|
|
1428
|
+
bp += n;
|
|
1429
|
+
},
|
|
1430
|
+
align() { bp = (bp + 7) & ~7; ensure(0); },
|
|
1431
|
+
bytes() { return buf.slice(0, (bp + 7) >> 3); },
|
|
1432
|
+
pos() { return bp; },
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
function _firstNonZero(arr) {
|
|
1437
|
+
for (let i = 0; i < arr.length; ++i) if (arr[i]) return i;
|
|
1438
|
+
return -1;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
function _padFreqs(freqs) {
|
|
1442
|
+
let nz = 0;
|
|
1443
|
+
for (const v of freqs) if (v) { if (++nz === 2) return; }
|
|
1444
|
+
let need = 2 - nz;
|
|
1445
|
+
for (let i = 0; i < freqs.length && need > 0; ++i) {
|
|
1446
|
+
if (!freqs[i]) { freqs[i] = 1; --need; }
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
function _padTo(arr, n) {
|
|
1451
|
+
if (arr.length === n) return arr;
|
|
1452
|
+
const out = new u8(n);
|
|
1453
|
+
for (let i = 0; i < arr.length && i < n; ++i) out[i] = arr[i];
|
|
1454
|
+
return out;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// Emit a complex prefix-code descriptor (§3.5).
|
|
1458
|
+
function _emitPrefixCodeDescriptor(w, codeLens) {
|
|
1459
|
+
// Find last non-zero target CL - we emit up to there.
|
|
1460
|
+
let lastNonZeroTgt = -1;
|
|
1461
|
+
for (let i = 0; i < codeLens.length; ++i) {
|
|
1462
|
+
if (codeLens[i] > 0) lastNonZeroTgt = i;
|
|
1463
|
+
}
|
|
1464
|
+
if (lastNonZeroTgt < 0) {
|
|
1465
|
+
_err('EINTERNAL', 'prefix code with all-zero lengths cannot be emitted');
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
// Histogram of CL values used (up to and including last).
|
|
1469
|
+
const clHist = new Int32Array(18);
|
|
1470
|
+
for (let i = 0; i <= lastNonZeroTgt; ++i) clHist[codeLens[i]]++;
|
|
1471
|
+
|
|
1472
|
+
// Build CL-of-CL canonical code, capped at 5 bits.
|
|
1473
|
+
const { t: clOfClRaw, l: clOfClMaxBits } = huffman.buildTree(clHist, 5);
|
|
1474
|
+
const clOfCl = new u8(18);
|
|
1475
|
+
for (let i = 0; i < clOfClRaw.length && i < 18; ++i) clOfCl[i] = clOfClRaw[i];
|
|
1476
|
+
|
|
1477
|
+
// HSKIP = 0
|
|
1478
|
+
w.bits(2, 0);
|
|
1479
|
+
|
|
1480
|
+
// Emit CL-of-CL values via the fixed §3.5 code.
|
|
1481
|
+
// - If ≥ 2 non-zero CL-of-CL values, stop at the last non-zero
|
|
1482
|
+
// in _CL_ORDER (decoder breaks on Kraft=32).
|
|
1483
|
+
// - If only one non-zero, emit all 18 (decoder reads them
|
|
1484
|
+
// all, then applies the single-symbol rule).
|
|
1485
|
+
let nzClOfCl = 0;
|
|
1486
|
+
for (let i = 0; i < 18; ++i) if (clOfCl[i]) ++nzClOfCl;
|
|
1487
|
+
|
|
1488
|
+
let stopAt = 18;
|
|
1489
|
+
if (nzClOfCl >= 2) {
|
|
1490
|
+
let last = -1;
|
|
1491
|
+
for (let i = 0; i < 18; ++i) {
|
|
1492
|
+
if (clOfCl[_CL_ORDER[i]] > 0) last = i;
|
|
1493
|
+
}
|
|
1494
|
+
stopAt = last + 1;
|
|
1495
|
+
}
|
|
1496
|
+
for (let i = 0; i < stopAt; ++i) {
|
|
1497
|
+
const v = clOfCl[_CL_ORDER[i]];
|
|
1498
|
+
w.bits(_CL_OF_CL_LEN[v], _CL_OF_CL_VAL[v]);
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// Build canonical encode map for the CL alphabet.
|
|
1502
|
+
const clMap = nzClOfCl >= 1
|
|
1503
|
+
? huffman.buildMap(clOfCl, Math.max(clOfClMaxBits, 1), 0)
|
|
1504
|
+
: null;
|
|
1505
|
+
const singleCl = nzClOfCl === 1 ? _firstNonZero(clOfCl) : -1;
|
|
1506
|
+
|
|
1507
|
+
// Emit target alphabet CLs. We break early on Kraft=32768.
|
|
1508
|
+
let tgtKraft = 0;
|
|
1509
|
+
for (let i = 0; i <= lastNonZeroTgt; ++i) {
|
|
1510
|
+
const v = codeLens[i];
|
|
1511
|
+
if (nzClOfCl === 1) {
|
|
1512
|
+
// Single-symbol CL decoder: must be the only CL used.
|
|
1513
|
+
if (v !== singleCl) {
|
|
1514
|
+
_err('EINTERNAL', 'CL ' + v + ' incompatible with single-symbol CL-of-CL (' + singleCl + ')');
|
|
1515
|
+
}
|
|
1516
|
+
// 0 bits emitted per CL.
|
|
1517
|
+
} else {
|
|
1518
|
+
const len = clOfCl[v];
|
|
1519
|
+
if (len === 0) _err('EINTERNAL', 'no CL-of-CL assignment for CL value ' + v);
|
|
1520
|
+
w.bits(len, clMap[v]);
|
|
1521
|
+
}
|
|
1522
|
+
if (v > 0) {
|
|
1523
|
+
tgtKraft += 32768 >> v;
|
|
1524
|
+
if (tgtKraft === 32768) break;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
// Cluster 64 UTF8 contexts into NTREESL_OPT literal trees by a
|
|
1530
|
+
// simple high-bits hash. Better clustering (data-driven) is a
|
|
1531
|
+
// potential follow-up - current heuristic gives noticeable gains
|
|
1532
|
+
// on English text by separating "alphabetic" from "punctuation"
|
|
1533
|
+
// contexts.
|
|
1534
|
+
const _NTREESL_OPT = 4;
|
|
1535
|
+
function _clusterContext(ctxId) { return ctxId >> 4; }
|
|
1536
|
+
|
|
1537
|
+
// Pick the best CMODE ∈ {0=LSB6, 1=MSB6, 2=UTF8, 3=Signed} for
|
|
1538
|
+
// the given input, by computing per-mode literal entropy under
|
|
1539
|
+
// the encoder's NTREESL=4 clustering. The mode with the lowest
|
|
1540
|
+
// total bit count wins.
|
|
1541
|
+
//
|
|
1542
|
+
// Sampled to first 16 KiB to bound the pre-pass cost (worst case
|
|
1543
|
+
// ~64 K loop iterations per mode = 256 K total - fast).
|
|
1544
|
+
function _pickCMode(data) {
|
|
1545
|
+
const SAMPLE = data.length < 16384 ? data.length : 16384;
|
|
1546
|
+
if (SAMPLE < 64) return 2; // UTF8 default for tiny inputs
|
|
1547
|
+
|
|
1548
|
+
const NTL = _NTREESL_OPT;
|
|
1549
|
+
let bestMode = 2;
|
|
1550
|
+
let bestBits = Infinity;
|
|
1551
|
+
|
|
1552
|
+
for (let mode = 0; mode < 4; ++mode) {
|
|
1553
|
+
// Per-tree frequencies + totals
|
|
1554
|
+
const freqs = new Array(NTL);
|
|
1555
|
+
for (let t = 0; t < NTL; ++t) freqs[t] = new Int32Array(256);
|
|
1556
|
+
const totals = new Int32Array(NTL);
|
|
1557
|
+
|
|
1558
|
+
let p1 = 0, p2 = 0;
|
|
1559
|
+
for (let i = 0; i < SAMPLE; ++i) {
|
|
1560
|
+
const byte = data[i];
|
|
1561
|
+
const ctxId = _contextIdLit(mode, p1, p2);
|
|
1562
|
+
const tree = ctxId >> 4;
|
|
1563
|
+
++freqs[tree][byte];
|
|
1564
|
+
++totals[tree];
|
|
1565
|
+
p2 = p1; p1 = byte;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// Entropy in bits: sum over trees of sum over bytes of
|
|
1569
|
+
// count * log2(treeTotal / count).
|
|
1570
|
+
let bits = 0;
|
|
1571
|
+
for (let t = 0; t < NTL; ++t) {
|
|
1572
|
+
const total = totals[t];
|
|
1573
|
+
if (total === 0) continue;
|
|
1574
|
+
const f = freqs[t];
|
|
1575
|
+
const logTotal = Math.log2(total);
|
|
1576
|
+
for (let s = 0; s < 256; ++s) {
|
|
1577
|
+
const c = f[s];
|
|
1578
|
+
if (c > 0) bits += c * (logTotal - Math.log2(c));
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
if (bits < bestBits) { bestBits = bits; bestMode = mode; }
|
|
1582
|
+
}
|
|
1583
|
+
return bestMode;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
// Emit a context map (§7.3) with RLEMAX=0 and IMTF=0. Values
|
|
1587
|
+
// are in [0, ntrees-1]. We rebuild a small prefix code over the
|
|
1588
|
+
// 'ntrees'-sized alphabet, then write each value directly.
|
|
1589
|
+
function _emitContextMap(w, cmap, ntrees) {
|
|
1590
|
+
// RLEMAX = 0
|
|
1591
|
+
w.bits(1, 0);
|
|
1592
|
+
// Build a prefix code over alphabet `ntrees` from the
|
|
1593
|
+
// frequencies in cmap.
|
|
1594
|
+
const freq = new Int32Array(ntrees);
|
|
1595
|
+
for (const v of cmap) ++freq[v];
|
|
1596
|
+
_padFreqs(freq);
|
|
1597
|
+
const { t: lens0, l: maxBits } = huffman.buildTree(freq, 15);
|
|
1598
|
+
const lens = _padTo(lens0, ntrees);
|
|
1599
|
+
const map = huffman.buildMap(lens, Math.max(maxBits, 1), 0);
|
|
1600
|
+
_emitPrefixCodeDescriptor(w, lens);
|
|
1601
|
+
// Emit each cmap entry
|
|
1602
|
+
for (const v of cmap) w.bits(lens[v], map[v]);
|
|
1603
|
+
// IMTF = 0
|
|
1604
|
+
w.bits(1, 0);
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
// Encode NTREES (or NBLTYPES) via the §9.2 variable-length code.
|
|
1608
|
+
// 1 → "0" (1 bit, val 0)
|
|
1609
|
+
// 2 → "0001" (4 bits LSB-first val 1)
|
|
1610
|
+
// 3..4 → "x0011" (5 bits, base 3, 1 extra)
|
|
1611
|
+
// 5..8 → "xx0101" (6 bits, base 5, 2 extra)
|
|
1612
|
+
// …
|
|
1613
|
+
// 129..256 → "xxxxxxx1111" (11 bits, base 129, 7 extra)
|
|
1614
|
+
function _emitVarLenCount(w, n) {
|
|
1615
|
+
if (n === 1) { w.bits(1, 0); return; }
|
|
1616
|
+
// Signal !=1 with bit 0 = 1
|
|
1617
|
+
// Category v ∈ [0, 7]; n = (1<<v) + 1 + extra, extra ∈ [0, (1<<v)-1].
|
|
1618
|
+
if (n === 2) { w.bits(4, 0b0001); return; }
|
|
1619
|
+
let v = 0;
|
|
1620
|
+
while ((1 << v) + 1 + ((1 << v) - 1) < n) ++v;
|
|
1621
|
+
const base = (1 << v) + 1;
|
|
1622
|
+
const extra = n - base;
|
|
1623
|
+
// bit 0 = 1, bits 1..3 = v, bits 4..(4+v-1) = extra
|
|
1624
|
+
// Build as a single integer LSB-first.
|
|
1625
|
+
const valPrefix = 1 | (v << 1); // 4 bits
|
|
1626
|
+
w.bits(4, valPrefix);
|
|
1627
|
+
if (v > 0) w.bits(v, extra);
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
// Lazy-built hash tables over the brotli static dictionary.
|
|
1631
|
+
//
|
|
1632
|
+
// `table` indexes the first 4 bytes of every word - used by every
|
|
1633
|
+
// Identity / FermentFirst / FermentAll / OmitLastK scan path.
|
|
1634
|
+
//
|
|
1635
|
+
// `ofTables[k]` indexes the 4 bytes at offset `k` of every word
|
|
1636
|
+
// of length >= k+4 - used by the OmitFirstK scan. k ∈ {1..7, 9}
|
|
1637
|
+
// (k=8 has no transform, per RFC 7932 §8).
|
|
1638
|
+
//
|
|
1639
|
+
// Built once per `brotli.factory(...)` instance and cached for
|
|
1640
|
+
// the life of the closure.
|
|
1641
|
+
let _staticDictHash = null;
|
|
1642
|
+
let _staticDictHashTried = false;
|
|
1643
|
+
|
|
1644
|
+
// OmitFirstK k values, matching `_OF_TID_BY_K` below.
|
|
1645
|
+
const _OF_K_VALUES = [1, 2, 3, 4, 5, 6, 7, 9];
|
|
1646
|
+
|
|
1647
|
+
function _getStaticDictHash() {
|
|
1648
|
+
if (_staticDictHash !== null) return _staticDictHash;
|
|
1649
|
+
if (_staticDictHashTried) return null;
|
|
1650
|
+
_staticDictHashTried = true;
|
|
1651
|
+
if (!brotliDictWords.isLoaded) return null;
|
|
1652
|
+
if (!brotliDict.hasWords()) brotliDict.setWords(brotliDictWords.blob);
|
|
1653
|
+
|
|
1654
|
+
const words = brotliDictWords.blob;
|
|
1655
|
+
const table = new Map();
|
|
1656
|
+
const ofTables = new Array(10); // sparse, indexed by k
|
|
1657
|
+
for (const k of _OF_K_VALUES) ofTables[k] = new Map();
|
|
1658
|
+
|
|
1659
|
+
for (let len = 4; len <= 24; ++len) {
|
|
1660
|
+
const nwords = brotliDict.NWORDS(len);
|
|
1661
|
+
if (nwords === 0) continue;
|
|
1662
|
+
const dofs = brotliDict.DOFFSET(len);
|
|
1663
|
+
for (let idx = 0; idx < nwords; ++idx) {
|
|
1664
|
+
const off = dofs + len * idx;
|
|
1665
|
+
const h = (words[off] | (words[off + 1] << 8) | (words[off + 2] << 16) | (words[off + 3] << 24)) >>> 0;
|
|
1666
|
+
let bucket = table.get(h);
|
|
1667
|
+
if (!bucket) { bucket = []; table.set(h, bucket); }
|
|
1668
|
+
bucket.push({ len, idx, off });
|
|
1669
|
+
|
|
1670
|
+
// OmitFirstK auxiliary hashes
|
|
1671
|
+
for (const k of _OF_K_VALUES) {
|
|
1672
|
+
if (len < k + 4) continue;
|
|
1673
|
+
const ofh = (words[off + k] | (words[off + k + 1] << 8) | (words[off + k + 2] << 16) | (words[off + k + 3] << 24)) >>> 0;
|
|
1674
|
+
const ofTable = ofTables[k];
|
|
1675
|
+
let ofBucket = ofTable.get(ofh);
|
|
1676
|
+
if (!ofBucket) { ofBucket = []; ofTable.set(ofh, ofBucket); }
|
|
1677
|
+
ofBucket.push({ len, idx, off });
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
_staticDictHash = { table, words, ofTables };
|
|
1682
|
+
return _staticDictHash;
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
// Map k (OmitFirstK depth) → transform id (RFC 7932 §8 Table 1).
|
|
1686
|
+
const _OF_TID_BY_K = new Map([
|
|
1687
|
+
[1, 3], [2, 11], [3, 26], [4, 34], [5, 39],
|
|
1688
|
+
[6, 40], [7, 55], [9, 54],
|
|
1689
|
+
]);
|
|
1690
|
+
|
|
1691
|
+
// Build, once per factory instance, the list of Identity-kind
|
|
1692
|
+
// (kind=0), FermentFirst-kind (kind=1) and FermentAll-kind
|
|
1693
|
+
// (kind=2) transforms from `brotliDict.transforms`, grouped by
|
|
1694
|
+
// their UTF-8 prefix bytes. Each group lists the (id, suffix)
|
|
1695
|
+
// pairs sharing the prefix, sorted longest-suffix-first.
|
|
1696
|
+
//
|
|
1697
|
+
// FermentFirst transforms (~19 of 121) match dict words appearing
|
|
1698
|
+
// capitalized in input (e.g. "The", "When"). FermentAll transforms
|
|
1699
|
+
// (~16 of 121) match dict words appearing fully uppercased
|
|
1700
|
+
// (e.g. "THE", "ERROR"). Both encoder scans are restricted to
|
|
1701
|
+
// ASCII fermenting (a..z → A..Z); 2-byte and 3-byte UTF-8
|
|
1702
|
+
// codepoint case-folding on the encode side would require a
|
|
1703
|
+
// separate hash over case-folded variants and remains future work.
|
|
1704
|
+
//
|
|
1705
|
+
// OmitFirstK and OmitLastK transforms remain encoder-unsupported.
|
|
1706
|
+
let _idTransformGroups = null;
|
|
1707
|
+
let _ffTransformGroups = null;
|
|
1708
|
+
let _faTransformGroups = null;
|
|
1709
|
+
// OmitLastK: a Map<k, Array<{id, suffix}>> for k ∈ {1..9}, sorted
|
|
1710
|
+
// longest-suffix-first. All OL transforms have prefix="" (per RFC
|
|
1711
|
+
// 7932 §8) so no prefix index is needed.
|
|
1712
|
+
let _olByK = null;
|
|
1713
|
+
function _buildEncoderTransformGroups() {
|
|
1714
|
+
if (_idTransformGroups !== null) return;
|
|
1715
|
+
const idByPrefix = new Map();
|
|
1716
|
+
const ffByPrefix = new Map();
|
|
1717
|
+
const faByPrefix = new Map();
|
|
1718
|
+
const olByK = new Map();
|
|
1719
|
+
const tlist = brotliDict.transforms;
|
|
1720
|
+
for (let id = 0; id < tlist.length; ++id) {
|
|
1721
|
+
const t = tlist[id];
|
|
1722
|
+
if (t.kind === 0 || t.kind === 1 || t.kind === 2) {
|
|
1723
|
+
const bucket = t.kind === 0 ? idByPrefix
|
|
1724
|
+
: t.kind === 1 ? ffByPrefix
|
|
1725
|
+
: faByPrefix;
|
|
1726
|
+
const key = Array.from(t.prefix).join(',');
|
|
1727
|
+
let g = bucket.get(key);
|
|
1728
|
+
if (!g) {
|
|
1729
|
+
g = { prefix: t.prefix, suffixes: [] };
|
|
1730
|
+
bucket.set(key, g);
|
|
1731
|
+
}
|
|
1732
|
+
g.suffixes.push({ id, suffix: t.suffix });
|
|
1733
|
+
} else if (t.kind >= 12 && t.kind <= 20) {
|
|
1734
|
+
// OmitLastK, k = param. All have prefix="" in RFC 7932.
|
|
1735
|
+
if (t.prefix.length !== 0) continue;
|
|
1736
|
+
const k = t.param;
|
|
1737
|
+
let arr = olByK.get(k);
|
|
1738
|
+
if (!arr) { arr = []; olByK.set(k, arr); }
|
|
1739
|
+
arr.push({ id, suffix: t.suffix });
|
|
1740
|
+
}
|
|
1741
|
+
// OmitFirstK (kinds 3..11) not yet handled - needs a
|
|
1742
|
+
// separate hash table keyed on dict-byte offsets > 0.
|
|
1743
|
+
}
|
|
1744
|
+
const finalize = (m) => {
|
|
1745
|
+
const groups = Array.from(m.values());
|
|
1746
|
+
for (const g of groups) {
|
|
1747
|
+
g.suffixes.sort((a, b) => b.suffix.length - a.suffix.length);
|
|
1748
|
+
}
|
|
1749
|
+
groups.sort((a, b) => a.prefix.length - b.prefix.length);
|
|
1750
|
+
return groups;
|
|
1751
|
+
};
|
|
1752
|
+
_idTransformGroups = finalize(idByPrefix);
|
|
1753
|
+
_ffTransformGroups = finalize(ffByPrefix);
|
|
1754
|
+
_faTransformGroups = finalize(faByPrefix);
|
|
1755
|
+
for (const arr of olByK.values()) {
|
|
1756
|
+
arr.sort((a, b) => b.suffix.length - a.suffix.length);
|
|
1757
|
+
}
|
|
1758
|
+
_olByK = olByK;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
// Find the longest brotli static-dict match at `data[pos]`. Tries
|
|
1762
|
+
// every Identity-kind transform from RFC 7932 §8 (ids 0, 1, 2, 5,
|
|
1763
|
+
// 6, 7, 8, 10, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 25, 28, 29,
|
|
1764
|
+
// 31, 32, 33, 35, 36, 37, 38, 41, 43, 45, 46, 47, 50, 51, 52, 53,
|
|
1765
|
+
// 57, 60, 61, 62, 67, 70, 71, 72, 73, 75, 76, 77, 80, 81, 82, 84,
|
|
1766
|
+
// 86, 89, 90, 92, 93, 95, 98, 100, 102, 103, 106) - i.e. all
|
|
1767
|
+
// transforms whose body is the dict word verbatim with arbitrary
|
|
1768
|
+
// UTF-8 prefix/suffix bytes.
|
|
1769
|
+
//
|
|
1770
|
+
// Returns `{ outputLen, dictLen, idx, transformId }` where
|
|
1771
|
+
// - dictLen is the dict word length (= CLEN to emit)
|
|
1772
|
+
// - outputLen is the bytes produced by the transform (used to
|
|
1773
|
+
// advance the input/output positions)
|
|
1774
|
+
function _findStaticDictMatchAt(data, pos, end, dictHash, minLen) {
|
|
1775
|
+
const words = dictHash.words;
|
|
1776
|
+
_buildEncoderTransformGroups();
|
|
1777
|
+
let bestOutLen = 0, bestDictLen = 0, bestIdx = 0, bestTransform = 0;
|
|
1778
|
+
|
|
1779
|
+
// Inner: given prefix-verified `hashStart`, a `firstByte`
|
|
1780
|
+
// override (for FermentFirst un-ferment), the dict-word body
|
|
1781
|
+
// verification function `bodyByteAt(c, m)`, and the suffix
|
|
1782
|
+
// list, update the best candidate.
|
|
1783
|
+
// (Inlined by hand below in two near-identical loops to keep
|
|
1784
|
+
// the hot path branchless.)
|
|
1785
|
+
|
|
1786
|
+
// --- Identity scan (+ OmitLastK on partial body match) ---
|
|
1787
|
+
const idGroups = _idTransformGroups;
|
|
1788
|
+
for (let gi = 0; gi < idGroups.length; ++gi) {
|
|
1789
|
+
const g = idGroups[gi];
|
|
1790
|
+
const pre = g.prefix;
|
|
1791
|
+
const preLen = pre.length;
|
|
1792
|
+
if (pos + preLen + 4 > end) continue;
|
|
1793
|
+
let mp = 0;
|
|
1794
|
+
while (mp < preLen && data[pos + mp] === pre[mp]) ++mp;
|
|
1795
|
+
if (mp !== preLen) continue;
|
|
1796
|
+
|
|
1797
|
+
const hashStart = pos + preLen;
|
|
1798
|
+
const h = (data[hashStart] | (data[hashStart + 1] << 8) | (data[hashStart + 2] << 16) | (data[hashStart + 3] << 24)) >>> 0;
|
|
1799
|
+
const bucket = dictHash.table.get(h);
|
|
1800
|
+
if (!bucket) continue;
|
|
1801
|
+
|
|
1802
|
+
for (let i = 0; i < bucket.length; ++i) {
|
|
1803
|
+
const c = bucket[i];
|
|
1804
|
+
// Walk the body as far as it matches - capture m even
|
|
1805
|
+
// when it stops short, so we can offer the prefix to
|
|
1806
|
+
// OmitLastK when this is the empty-prefix Identity
|
|
1807
|
+
// group.
|
|
1808
|
+
const bodyEnd = hashStart + c.len > end ? end : hashStart + c.len;
|
|
1809
|
+
let m = 4;
|
|
1810
|
+
while (hashStart + m < bodyEnd && data[hashStart + m] === words[c.off + m]) ++m;
|
|
1811
|
+
|
|
1812
|
+
if (m === c.len) {
|
|
1813
|
+
const sufStart = hashStart + c.len;
|
|
1814
|
+
const sufs = g.suffixes;
|
|
1815
|
+
for (let si = 0; si < sufs.length; ++si) {
|
|
1816
|
+
const sb = sufs[si].suffix;
|
|
1817
|
+
const sl = sb.length;
|
|
1818
|
+
if (sufStart + sl > end) continue;
|
|
1819
|
+
let sm = 0;
|
|
1820
|
+
while (sm < sl && data[sufStart + sm] === sb[sm]) ++sm;
|
|
1821
|
+
if (sm !== sl) continue;
|
|
1822
|
+
const ol = preLen + c.len + sl;
|
|
1823
|
+
if (ol > bestOutLen) {
|
|
1824
|
+
bestOutLen = ol;
|
|
1825
|
+
bestDictLen = c.len;
|
|
1826
|
+
bestIdx = c.idx;
|
|
1827
|
+
bestTransform = sufs[si].id;
|
|
1828
|
+
}
|
|
1829
|
+
break;
|
|
1830
|
+
}
|
|
1831
|
+
} else if (preLen === 0) {
|
|
1832
|
+
// Partial body match - try OmitLastK with k = c.len - m.
|
|
1833
|
+
// OL transforms all have prefix="" so they only
|
|
1834
|
+
// apply to the empty-prefix group.
|
|
1835
|
+
const k = c.len - m;
|
|
1836
|
+
const olOpts = _olByK.get(k);
|
|
1837
|
+
if (olOpts) {
|
|
1838
|
+
const sufStart = hashStart + m;
|
|
1839
|
+
for (let oi = 0; oi < olOpts.length; ++oi) {
|
|
1840
|
+
const sb = olOpts[oi].suffix;
|
|
1841
|
+
const sl = sb.length;
|
|
1842
|
+
if (sufStart + sl > end) continue;
|
|
1843
|
+
let sm = 0;
|
|
1844
|
+
while (sm < sl && data[sufStart + sm] === sb[sm]) ++sm;
|
|
1845
|
+
if (sm !== sl) continue;
|
|
1846
|
+
const ol = m + sl;
|
|
1847
|
+
if (ol > bestOutLen) {
|
|
1848
|
+
bestOutLen = ol;
|
|
1849
|
+
bestDictLen = c.len;
|
|
1850
|
+
bestIdx = c.idx;
|
|
1851
|
+
bestTransform = olOpts[oi].id;
|
|
1852
|
+
}
|
|
1853
|
+
break; // longest suffix for this k
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
// --- FermentFirst scan (ASCII + 2-byte + 3-byte UTF-8) ---
|
|
1861
|
+
// For each FF transform's prefix, check if data[pos..] begins
|
|
1862
|
+
// with the prefix followed by a "fermentable" first codepoint.
|
|
1863
|
+
// `_ferment` (RFC 7932 §8) modifies:
|
|
1864
|
+
// - 1-byte ASCII a..z → A..Z (XOR 0x20 on byte 0)
|
|
1865
|
+
// - 2-byte UTF-8 lead (C2..DF) → byte 1 ^= 0x20
|
|
1866
|
+
// - 3-byte UTF-8 lead (E0..EF) → byte 2 ^= 0x05
|
|
1867
|
+
// We un-ferment the affected byte to recover the dict-side key,
|
|
1868
|
+
// then verify the first codepoint bytes exactly before walking
|
|
1869
|
+
// the rest of the body verbatim.
|
|
1870
|
+
const ffGroups = _ffTransformGroups;
|
|
1871
|
+
for (let gi = 0; gi < ffGroups.length; ++gi) {
|
|
1872
|
+
const g = ffGroups[gi];
|
|
1873
|
+
const pre = g.prefix;
|
|
1874
|
+
const preLen = pre.length;
|
|
1875
|
+
if (pos + preLen + 4 > end) continue;
|
|
1876
|
+
let mp = 0;
|
|
1877
|
+
while (mp < preLen && data[pos + mp] === pre[mp]) ++mp;
|
|
1878
|
+
if (mp !== preLen) continue;
|
|
1879
|
+
|
|
1880
|
+
const hashStart = pos + preLen;
|
|
1881
|
+
const b0 = data[hashStart];
|
|
1882
|
+
const b1 = data[hashStart + 1];
|
|
1883
|
+
const b2 = data[hashStart + 2];
|
|
1884
|
+
const b3 = data[hashStart + 3];
|
|
1885
|
+
|
|
1886
|
+
// Determine ferment class and compute un-fermented bytes 0..3.
|
|
1887
|
+
// u0..u3 = dict-side bytes that hash should match.
|
|
1888
|
+
let u0, u1, u2, u3;
|
|
1889
|
+
let cpLen; // bytes verified exactly by hash key construction
|
|
1890
|
+
if (b0 >= 0x41 && b0 <= 0x5A) {
|
|
1891
|
+
// ASCII uppercase → unferment byte 0 to lowercase
|
|
1892
|
+
u0 = b0 | 0x20; u1 = b1; u2 = b2; u3 = b3;
|
|
1893
|
+
cpLen = 1;
|
|
1894
|
+
} else if (b0 >= 0xC2 && b0 <= 0xDF) {
|
|
1895
|
+
// 2-byte UTF-8 codepoint - unferment byte 1
|
|
1896
|
+
u0 = b0; u1 = b1 ^ 0x20; u2 = b2; u3 = b3;
|
|
1897
|
+
cpLen = 2;
|
|
1898
|
+
} else if (b0 >= 0xE0 && b0 <= 0xEF) {
|
|
1899
|
+
// 3-byte UTF-8 codepoint - unferment byte 2 (XOR 0x05)
|
|
1900
|
+
u0 = b0; u1 = b1; u2 = b2 ^ 0x05; u3 = b3;
|
|
1901
|
+
cpLen = 3;
|
|
1902
|
+
} else {
|
|
1903
|
+
continue;
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
const h = (u0 | (u1 << 8) | (u2 << 16) | (u3 << 24)) >>> 0;
|
|
1907
|
+
const bucket = dictHash.table.get(h);
|
|
1908
|
+
if (!bucket) continue;
|
|
1909
|
+
|
|
1910
|
+
for (let i = 0; i < bucket.length; ++i) {
|
|
1911
|
+
const c = bucket[i];
|
|
1912
|
+
if (hashStart + c.len > end) continue;
|
|
1913
|
+
// Verify the un-fermented codepoint bytes match the
|
|
1914
|
+
// dict word's leading bytes. (The hash key has 32-bit
|
|
1915
|
+
// collisions; this rules them out cheaply.)
|
|
1916
|
+
if (words[c.off] !== u0) continue;
|
|
1917
|
+
if (cpLen >= 2 && words[c.off + 1] !== u1) continue;
|
|
1918
|
+
if (cpLen >= 3 && words[c.off + 2] !== u2) continue;
|
|
1919
|
+
let m = 4;
|
|
1920
|
+
while (m < c.len && data[hashStart + m] === words[c.off + m]) ++m;
|
|
1921
|
+
if (m !== c.len) continue;
|
|
1922
|
+
|
|
1923
|
+
const sufStart = hashStart + c.len;
|
|
1924
|
+
const sufs = g.suffixes;
|
|
1925
|
+
for (let si = 0; si < sufs.length; ++si) {
|
|
1926
|
+
const sb = sufs[si].suffix;
|
|
1927
|
+
const sl = sb.length;
|
|
1928
|
+
if (sufStart + sl > end) continue;
|
|
1929
|
+
let sm = 0;
|
|
1930
|
+
while (sm < sl && data[sufStart + sm] === sb[sm]) ++sm;
|
|
1931
|
+
if (sm !== sl) continue;
|
|
1932
|
+
const ol = preLen + c.len + sl;
|
|
1933
|
+
if (ol > bestOutLen) {
|
|
1934
|
+
bestOutLen = ol;
|
|
1935
|
+
bestDictLen = c.len;
|
|
1936
|
+
bestIdx = c.idx;
|
|
1937
|
+
bestTransform = sufs[si].id;
|
|
1938
|
+
}
|
|
1939
|
+
break;
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
// --- FermentAll scan (full UTF-8 support) ---
|
|
1945
|
+
// Under FA (RFC 7932 §8 / RFC 9841 §3.1.1), each codepoint's
|
|
1946
|
+
// LAST byte is fermented in the output:
|
|
1947
|
+
// - 1-byte ASCII a..z → A..Z (byte XOR 0x20)
|
|
1948
|
+
// - 2-byte codepoint: lead unchanged, continuation XOR 0x20
|
|
1949
|
+
// - 3-byte codepoint: leads unchanged, last byte XOR 0x05
|
|
1950
|
+
//
|
|
1951
|
+
// Lead bytes (high bits 0xxx / 110x / 1110) are themselves
|
|
1952
|
+
// preserved by ferment, so the codepoint boundary structure
|
|
1953
|
+
// of the FA output matches the dict word's. We use that to
|
|
1954
|
+
// walk input bytes 0..3 and compute un-fermented values
|
|
1955
|
+
// (= dict bytes) for the hash key, then verify the body by
|
|
1956
|
+
// walking dict codepoints and applying the same rule.
|
|
1957
|
+
const faGroups = _faTransformGroups;
|
|
1958
|
+
for (let gi = 0; gi < faGroups.length; ++gi) {
|
|
1959
|
+
const g = faGroups[gi];
|
|
1960
|
+
const pre = g.prefix;
|
|
1961
|
+
const preLen = pre.length;
|
|
1962
|
+
if (pos + preLen + 4 > end) continue;
|
|
1963
|
+
let mp = 0;
|
|
1964
|
+
while (mp < preLen && data[pos + mp] === pre[mp]) ++mp;
|
|
1965
|
+
if (mp !== preLen) continue;
|
|
1966
|
+
|
|
1967
|
+
const hashStart = pos + preLen;
|
|
1968
|
+
|
|
1969
|
+
// Walk input bytes 0..3 detecting codepoint boundaries
|
|
1970
|
+
// from UTF-8 lead bit patterns. Build un-fermented bytes
|
|
1971
|
+
// u[0..3]. Reject if structure is invalid, if any byte is
|
|
1972
|
+
// [a-z] (impossible under FA), or if no byte was fermented
|
|
1973
|
+
// (Identity-equivalent → covered by Identity scan with a
|
|
1974
|
+
// smaller transform id).
|
|
1975
|
+
const u0a = data[hashStart];
|
|
1976
|
+
const u1a = data[hashStart + 1];
|
|
1977
|
+
const u2a = data[hashStart + 2];
|
|
1978
|
+
const u3a = data[hashStart + 3];
|
|
1979
|
+
let u0, u1, u2, u3;
|
|
1980
|
+
let validCp = true;
|
|
1981
|
+
let hasFermented = false;
|
|
1982
|
+
|
|
1983
|
+
// Codepoint 1 starting at byte 0
|
|
1984
|
+
let i = 0;
|
|
1985
|
+
if (u0a < 0x80) {
|
|
1986
|
+
if (u0a >= 0x61 && u0a <= 0x7A) { validCp = false; }
|
|
1987
|
+
else if (u0a >= 0x41 && u0a <= 0x5A) { u0 = u0a | 0x20; hasFermented = true; i = 1; }
|
|
1988
|
+
else { u0 = u0a; i = 1; }
|
|
1989
|
+
} else if (u0a >= 0xC2 && u0a <= 0xDF) {
|
|
1990
|
+
u0 = u0a;
|
|
1991
|
+
u1 = u1a ^ 0x20; hasFermented = true; i = 2;
|
|
1992
|
+
} else if (u0a >= 0xE0 && u0a <= 0xEF) {
|
|
1993
|
+
u0 = u0a; u1 = u1a;
|
|
1994
|
+
u2 = u2a ^ 0x05; hasFermented = true; i = 3;
|
|
1995
|
+
} else {
|
|
1996
|
+
validCp = false;
|
|
1997
|
+
}
|
|
1998
|
+
// Codepoint 2 if room
|
|
1999
|
+
if (validCp && i < 4) {
|
|
2000
|
+
const b = i === 1 ? u1a : (i === 2 ? u2a : u3a);
|
|
2001
|
+
if (b < 0x80) {
|
|
2002
|
+
if (b >= 0x61 && b <= 0x7A) validCp = false;
|
|
2003
|
+
else if (b >= 0x41 && b <= 0x5A) {
|
|
2004
|
+
if (i === 1) { u1 = b | 0x20; } else if (i === 2) { u2 = b | 0x20; } else { u3 = b | 0x20; }
|
|
2005
|
+
hasFermented = true; i += 1;
|
|
2006
|
+
} else {
|
|
2007
|
+
if (i === 1) { u1 = b; } else if (i === 2) { u2 = b; } else { u3 = b; }
|
|
2008
|
+
i += 1;
|
|
2009
|
+
}
|
|
2010
|
+
} else if (b >= 0xC2 && b <= 0xDF) {
|
|
2011
|
+
if (i + 2 > 4) {
|
|
2012
|
+
// 2-byte codepoint straddles hash boundary -
|
|
2013
|
+
// lead is the last byte of the window.
|
|
2014
|
+
if (i === 3) { u3 = b; }
|
|
2015
|
+
i = 4;
|
|
2016
|
+
} else {
|
|
2017
|
+
const nb = i === 1 ? u2a : u3a;
|
|
2018
|
+
if (i === 1) { u1 = b; u2 = nb ^ 0x20; }
|
|
2019
|
+
else { u2 = b; u3 = nb ^ 0x20; }
|
|
2020
|
+
hasFermented = true; i += 2;
|
|
2021
|
+
}
|
|
2022
|
+
} else if (b >= 0xE0 && b <= 0xEF) {
|
|
2023
|
+
if (i + 3 > 4) {
|
|
2024
|
+
if (i === 1) { u1 = b; u2 = u2a; }
|
|
2025
|
+
else if (i === 2) { u2 = b; u3 = u3a; }
|
|
2026
|
+
else { u3 = b; }
|
|
2027
|
+
i = 4;
|
|
2028
|
+
} else {
|
|
2029
|
+
// i must be 1 (3-byte cp starting at 1 → bytes 1,2,3)
|
|
2030
|
+
u1 = b; u2 = u2a; u3 = u3a ^ 0x05;
|
|
2031
|
+
hasFermented = true; i = 4;
|
|
2032
|
+
}
|
|
2033
|
+
} else {
|
|
2034
|
+
validCp = false;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
// Codepoint 3 if room
|
|
2038
|
+
if (validCp && i < 4) {
|
|
2039
|
+
const b = i === 2 ? u2a : u3a;
|
|
2040
|
+
if (b < 0x80) {
|
|
2041
|
+
if (b >= 0x61 && b <= 0x7A) validCp = false;
|
|
2042
|
+
else if (b >= 0x41 && b <= 0x5A) {
|
|
2043
|
+
if (i === 2) { u2 = b | 0x20; } else { u3 = b | 0x20; }
|
|
2044
|
+
hasFermented = true; i += 1;
|
|
2045
|
+
} else {
|
|
2046
|
+
if (i === 2) { u2 = b; } else { u3 = b; }
|
|
2047
|
+
i += 1;
|
|
2048
|
+
}
|
|
2049
|
+
} else if (b >= 0xC2 && b <= 0xDF) {
|
|
2050
|
+
if (i + 2 > 4) {
|
|
2051
|
+
if (i === 3) { u3 = b; }
|
|
2052
|
+
i = 4;
|
|
2053
|
+
} else {
|
|
2054
|
+
// i must be 2 (2-byte cp starting at 2 → bytes 2,3)
|
|
2055
|
+
u2 = b; u3 = u3a ^ 0x20;
|
|
2056
|
+
hasFermented = true; i = 4;
|
|
2057
|
+
}
|
|
2058
|
+
} else {
|
|
2059
|
+
validCp = false;
|
|
2060
|
+
}
|
|
2061
|
+
}
|
|
2062
|
+
// Codepoint 4 (single ASCII at byte 3)
|
|
2063
|
+
if (validCp && i < 4) {
|
|
2064
|
+
const b = u3a;
|
|
2065
|
+
if (b < 0x80) {
|
|
2066
|
+
if (b >= 0x61 && b <= 0x7A) validCp = false;
|
|
2067
|
+
else if (b >= 0x41 && b <= 0x5A) { u3 = b | 0x20; hasFermented = true; }
|
|
2068
|
+
else { u3 = b; }
|
|
2069
|
+
} else {
|
|
2070
|
+
validCp = false;
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
if (!validCp || !hasFermented) continue;
|
|
2075
|
+
|
|
2076
|
+
const h = (u0 | (u1 << 8) | (u2 << 16) | (u3 << 24)) >>> 0;
|
|
2077
|
+
const bucket = dictHash.table.get(h);
|
|
2078
|
+
if (!bucket) continue;
|
|
2079
|
+
|
|
2080
|
+
for (let bi = 0; bi < bucket.length; ++bi) {
|
|
2081
|
+
const c = bucket[bi];
|
|
2082
|
+
if (hashStart + c.len > end) continue;
|
|
2083
|
+
// Body verification: walk dict codepoints from offset
|
|
2084
|
+
// 0, applying ferment per codepoint position.
|
|
2085
|
+
let m = 0;
|
|
2086
|
+
let ok = true;
|
|
2087
|
+
while (m < c.len) {
|
|
2088
|
+
const w0 = words[c.off + m];
|
|
2089
|
+
const cpKind = w0 < 0x80 ? 1 : (w0 < 0xE0 ? 2 : 3);
|
|
2090
|
+
if (m + cpKind > c.len) {
|
|
2091
|
+
// Truncated codepoint at word end - compare
|
|
2092
|
+
// remaining bytes verbatim.
|
|
2093
|
+
while (m < c.len) {
|
|
2094
|
+
if (data[hashStart + m] !== words[c.off + m]) { ok = false; break; }
|
|
2095
|
+
++m;
|
|
2096
|
+
}
|
|
2097
|
+
break;
|
|
2098
|
+
}
|
|
2099
|
+
if (cpKind === 1) {
|
|
2100
|
+
const expected = (w0 >= 0x61 && w0 <= 0x7A) ? (w0 ^ 0x20) : w0;
|
|
2101
|
+
if (data[hashStart + m] !== expected) { ok = false; break; }
|
|
2102
|
+
m += 1;
|
|
2103
|
+
} else if (cpKind === 2) {
|
|
2104
|
+
if (data[hashStart + m] !== w0) { ok = false; break; }
|
|
2105
|
+
const w1 = words[c.off + m + 1];
|
|
2106
|
+
if (data[hashStart + m + 1] !== (w1 ^ 0x20)) { ok = false; break; }
|
|
2107
|
+
m += 2;
|
|
2108
|
+
} else {
|
|
2109
|
+
if (data[hashStart + m] !== w0) { ok = false; break; }
|
|
2110
|
+
if (data[hashStart + m + 1] !== words[c.off + m + 1]) { ok = false; break; }
|
|
2111
|
+
const w2 = words[c.off + m + 2];
|
|
2112
|
+
if (data[hashStart + m + 2] !== (w2 ^ 0x05)) { ok = false; break; }
|
|
2113
|
+
m += 3;
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
if (!ok) continue;
|
|
2117
|
+
|
|
2118
|
+
const sufStart = hashStart + c.len;
|
|
2119
|
+
const sufs = g.suffixes;
|
|
2120
|
+
for (let si = 0; si < sufs.length; ++si) {
|
|
2121
|
+
const sb = sufs[si].suffix;
|
|
2122
|
+
const sl = sb.length;
|
|
2123
|
+
if (sufStart + sl > end) continue;
|
|
2124
|
+
let sm = 0;
|
|
2125
|
+
while (sm < sl && data[sufStart + sm] === sb[sm]) ++sm;
|
|
2126
|
+
if (sm !== sl) continue;
|
|
2127
|
+
const ol = preLen + c.len + sl;
|
|
2128
|
+
if (ol > bestOutLen) {
|
|
2129
|
+
bestOutLen = ol;
|
|
2130
|
+
bestDictLen = c.len;
|
|
2131
|
+
bestIdx = c.idx;
|
|
2132
|
+
bestTransform = sufs[si].id;
|
|
2133
|
+
}
|
|
2134
|
+
break;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
// --- OmitFirstK scan ---
|
|
2140
|
+
// OF transforms have empty prefix and empty suffix (per RFC
|
|
2141
|
+
// 7932 §8). Output = words[c.off+k..c.off+c.len]. We hash on
|
|
2142
|
+
// data[pos..pos+3] which corresponds to bytes [k..k+3] of the
|
|
2143
|
+
// candidate dict word, then verify the remaining bytes.
|
|
2144
|
+
if (pos + 4 <= end && dictHash.ofTables) {
|
|
2145
|
+
const h = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>> 0;
|
|
2146
|
+
for (let ki = 0; ki < _OF_K_VALUES.length; ++ki) {
|
|
2147
|
+
const k = _OF_K_VALUES[ki];
|
|
2148
|
+
const ofTable = dictHash.ofTables[k];
|
|
2149
|
+
if (!ofTable) continue;
|
|
2150
|
+
const bucket = ofTable.get(h);
|
|
2151
|
+
if (!bucket) continue;
|
|
2152
|
+
const ofTid = _OF_TID_BY_K.get(k);
|
|
2153
|
+
for (let i = 0; i < bucket.length; ++i) {
|
|
2154
|
+
const c = bucket[i];
|
|
2155
|
+
const bodyLen = c.len - k;
|
|
2156
|
+
if (pos + bodyLen > end) continue;
|
|
2157
|
+
// First 4 bytes guaranteed by hash; verify rest.
|
|
2158
|
+
let m = 4;
|
|
2159
|
+
while (m < bodyLen && data[pos + m] === words[c.off + k + m]) ++m;
|
|
2160
|
+
if (m !== bodyLen) continue;
|
|
2161
|
+
if (bodyLen > bestOutLen) {
|
|
2162
|
+
bestOutLen = bodyLen;
|
|
2163
|
+
bestDictLen = c.len;
|
|
2164
|
+
bestIdx = c.idx;
|
|
2165
|
+
bestTransform = ofTid;
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
return bestOutLen >= minLen
|
|
2172
|
+
? { outputLen: bestOutLen, dictLen: bestDictLen, idx: bestIdx, transformId: bestTransform }
|
|
2173
|
+
: null;
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
// Walk the LZ77-produced commands and split literal runs whenever
|
|
2177
|
+
// a long-enough static-dict match is found. Replaces "N literals
|
|
2178
|
+
// then a copy" with "K literals + dict-ref-copy", followed by the
|
|
2179
|
+
// remainder. Threshold of 6 bytes is empirical - shorter matches
|
|
2180
|
+
// typically don't beat Huffman-coded literals once IAC + distance
|
|
2181
|
+
// overhead is paid.
|
|
2182
|
+
// Augment LZ77 commands with static-dict references. The match
|
|
2183
|
+
// finder is either:
|
|
2184
|
+
// - the built-in RFC 7932 Appendix A/B scan (`_findStaticDictMatchAt`,
|
|
2185
|
+
// used when no `_ext.findCustomDictMatch` hook is set), or
|
|
2186
|
+
// - a custom-dict scan supplied by `brotli_shared` via the
|
|
2187
|
+
// `_ext.findCustomDictMatch(data, pos, end, minLen) =>
|
|
2188
|
+
// { outputLen, dictLen, wordId } | null` hook (RFC 9841 §3.1).
|
|
2189
|
+
//
|
|
2190
|
+
// The distance emitted accounts for both the LZ77 shared-dict
|
|
2191
|
+
// prefix length L (RFC 9841 §3.2) and the word_id : the decoder
|
|
2192
|
+
// recovers `wordId = distance − maxAllowed − 1 − L`.
|
|
2193
|
+
function _augmentWithStaticDictRefs(commands, data, dictHash, windowSize, customMatchFinder, lz77PrefixLen) {
|
|
2194
|
+
const augmented = [];
|
|
2195
|
+
let outPos = 0;
|
|
2196
|
+
const MIN_DICT_MATCH = 6;
|
|
2197
|
+
const L = lz77PrefixLen | 0;
|
|
2198
|
+
|
|
2199
|
+
for (let ci = 0; ci < commands.length; ++ci) {
|
|
2200
|
+
const cmd = commands[ci];
|
|
2201
|
+
const litEnd = cmd.insertStart + cmd.insertLen;
|
|
2202
|
+
let litStart = cmd.insertStart;
|
|
2203
|
+
let pos = litStart;
|
|
2204
|
+
|
|
2205
|
+
while (pos < litEnd) {
|
|
2206
|
+
let m, wordId, dictWordOff;
|
|
2207
|
+
if (customMatchFinder) {
|
|
2208
|
+
m = customMatchFinder(data, pos, litEnd, MIN_DICT_MATCH);
|
|
2209
|
+
if (!m) { pos++; continue; }
|
|
2210
|
+
wordId = m.wordId;
|
|
2211
|
+
dictWordOff = null; // custom-dict words aren't in the built-in blob
|
|
2212
|
+
} else {
|
|
2213
|
+
m = _findStaticDictMatchAt(data, pos, litEnd, dictHash, MIN_DICT_MATCH);
|
|
2214
|
+
if (!m) { pos++; continue; }
|
|
2215
|
+
wordId = m.transformId * brotliDict.NWORDS(m.dictLen) + m.idx;
|
|
2216
|
+
dictWordOff = brotliDict.DOFFSET(m.dictLen) + m.dictLen * m.idx;
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// distance = maxAllowed + 1 + L + word_id (RFC 9841 §3.1 / §3.2)
|
|
2220
|
+
const outAtDictRef = outPos + (pos - litStart);
|
|
2221
|
+
const maxAllowed = outAtDictRef < windowSize ? outAtDictRef : windowSize;
|
|
2222
|
+
const dictDistance = maxAllowed + 1 + L + wordId;
|
|
2223
|
+
if (!_distanceCodeFor(dictDistance, 0, 0)) {
|
|
2224
|
+
pos++;
|
|
2225
|
+
continue;
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
augmented.push({
|
|
2229
|
+
insertStart: litStart,
|
|
2230
|
+
insertLen: pos - litStart,
|
|
2231
|
+
copyLen: m.dictLen, // CLEN = dict word length
|
|
2232
|
+
distance: dictDistance,
|
|
2233
|
+
isDictRef: true,
|
|
2234
|
+
dictOutputLen: m.outputLen, // bytes actually emitted
|
|
2235
|
+
dictWordOff,
|
|
2236
|
+
});
|
|
2237
|
+
outPos = outAtDictRef + m.outputLen;
|
|
2238
|
+
pos += m.outputLen;
|
|
2239
|
+
litStart = pos;
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
// Tail: remaining literals + the command's original (LZ77) copy.
|
|
2243
|
+
// When dict refs consumed every literal of a copy-less command
|
|
2244
|
+
// (the stream's final literal run), the tail is empty: emitting
|
|
2245
|
+
// it would put a dangling IAC symbol after MLEN is reached,
|
|
2246
|
+
// which no decoder reads (non-zero fill bits / node rejects).
|
|
2247
|
+
if (litEnd === litStart && cmd.copyLen === 0) continue;
|
|
2248
|
+
augmented.push({
|
|
2249
|
+
insertStart: litStart,
|
|
2250
|
+
insertLen: litEnd - litStart,
|
|
2251
|
+
copyLen: cmd.copyLen,
|
|
2252
|
+
distance: cmd.distance,
|
|
2253
|
+
isDictRef: false,
|
|
2254
|
+
});
|
|
2255
|
+
outPos += (litEnd - litStart) + cmd.copyLen;
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
return augmented;
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
// Pick the best (NPOSTFIX, NDIRECT_HI) for the distance
|
|
2262
|
+
// distribution of `cmds` (RFC 7932 §4). We score a small set of
|
|
2263
|
+
// candidate configs by estimating total distance bits under each:
|
|
2264
|
+
//
|
|
2265
|
+
// bits = sum over symbols of (count * -log2(count/total))
|
|
2266
|
+
// [Huffman ≈ entropy lower bound]
|
|
2267
|
+
// + sum of extra bits per command
|
|
2268
|
+
// + alphabet header overhead estimate (~5 * distAlphabet)
|
|
2269
|
+
//
|
|
2270
|
+
// Candidates cover the common useful settings: (0,0) default,
|
|
2271
|
+
// (0,4)/(0,8)/(0,12) for streams with many short repeats, and
|
|
2272
|
+
// (1,0)/(2,0) for finer granularity at large distances.
|
|
2273
|
+
const _DIST_PARAM_CANDIDATES = [
|
|
2274
|
+
{ npostfix: 0, ndirectHi: 0 }, // default - 64 symbols
|
|
2275
|
+
{ npostfix: 0, ndirectHi: 4 }, // NDIRECT=4 - 68 symbols
|
|
2276
|
+
{ npostfix: 0, ndirectHi: 8 }, // NDIRECT=8 - 72 symbols
|
|
2277
|
+
{ npostfix: 0, ndirectHi: 12 }, // NDIRECT=12 - 76 symbols
|
|
2278
|
+
{ npostfix: 1, ndirectHi: 0 }, // 112 symbols, finer high range
|
|
2279
|
+
{ npostfix: 2, ndirectHi: 0 }, // 208 symbols
|
|
2280
|
+
{ npostfix: 3, ndirectHi: 0 }, // 400 symbols
|
|
2281
|
+
];
|
|
2282
|
+
|
|
2283
|
+
// Encode a block count into (sym, extra, extraBits) under the
|
|
2284
|
+
// 26-symbol block-count alphabet (RFC 7932 §6). Inverse of
|
|
2285
|
+
// `_readBlockCount`.
|
|
2286
|
+
function _blockCountCodeFor(count) {
|
|
2287
|
+
for (let s = 0; s < 26; ++s) {
|
|
2288
|
+
const base = _BLOCK_COUNT_BASE[s];
|
|
2289
|
+
const extraBits = _BLOCK_COUNT_EXTRA[s];
|
|
2290
|
+
const max = base + (1 << extraBits) - 1;
|
|
2291
|
+
if (count >= base && count <= max) {
|
|
2292
|
+
return { sym: s, extra: count - base, extraBits };
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
return null;
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
// Decide whether to split the literal channel into 2 block types
|
|
2299
|
+
// (RFC 7932 §6). Returns `{ splitBytePos }` or `null`.
|
|
2300
|
+
//
|
|
2301
|
+
// Heuristic: compute the KL divergence between byte distributions
|
|
2302
|
+
// of the first and second halves of the input. If they differ
|
|
2303
|
+
// measurably AND the input is long enough that the header
|
|
2304
|
+
// overhead (extra trees + CMAPL + block-length codes ~= 1-2 KiB)
|
|
2305
|
+
// is amortised, recommend a midpoint split.
|
|
2306
|
+
function _pickLitBlockSplit(data, quality) {
|
|
2307
|
+
if (data.length < 32768) return null;
|
|
2308
|
+
if (quality < 6) return null;
|
|
2309
|
+
const half = data.length >> 1;
|
|
2310
|
+
const freqA = new Int32Array(256);
|
|
2311
|
+
const freqB = new Int32Array(256);
|
|
2312
|
+
for (let i = 0; i < half; ++i) ++freqA[data[i]];
|
|
2313
|
+
for (let i = half; i < data.length; ++i) ++freqB[data[i]];
|
|
2314
|
+
const totalA = half;
|
|
2315
|
+
const totalB = data.length - half;
|
|
2316
|
+
// KL(B || A) - smoothing avoids log(0).
|
|
2317
|
+
const epsA = 1 / (totalA + 256);
|
|
2318
|
+
let kl = 0;
|
|
2319
|
+
for (let s = 0; s < 256; ++s) {
|
|
2320
|
+
if (freqB[s] === 0) continue;
|
|
2321
|
+
const pA = freqA[s] / totalA || epsA;
|
|
2322
|
+
const pB = freqB[s] / totalB;
|
|
2323
|
+
kl += pB * Math.log2(pB / pA);
|
|
2324
|
+
}
|
|
2325
|
+
return kl > 0.3 ? { splitBytePos: half } : null;
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
function _pickDistParams(cmds) {
|
|
2329
|
+
// Collect distances from commands (skip insert-only & special).
|
|
2330
|
+
const distances = [];
|
|
2331
|
+
for (let i = 0; i < cmds.length; ++i) {
|
|
2332
|
+
const c = cmds[i];
|
|
2333
|
+
if (c.copyLen > 0 && c.distance > 0) distances.push(c.distance);
|
|
2334
|
+
}
|
|
2335
|
+
if (distances.length === 0) {
|
|
2336
|
+
return { npostfix: 0, ndirect: 0, distAlphabet: 64 };
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
let bestScore = Infinity;
|
|
2340
|
+
let bestNpost = 0, bestNdir = 0, bestAlpha = 64;
|
|
2341
|
+
for (const cand of _DIST_PARAM_CANDIDATES) {
|
|
2342
|
+
const np = cand.npostfix;
|
|
2343
|
+
const nd = cand.ndirectHi << np;
|
|
2344
|
+
const alpha = 16 + nd + (48 << np);
|
|
2345
|
+
if (alpha > 520) continue; // distAlphabet hard cap
|
|
2346
|
+
// Tabulate per-symbol counts + sum of extra bits.
|
|
2347
|
+
const cnt = new Int32Array(alpha);
|
|
2348
|
+
let extraBitsSum = 0;
|
|
2349
|
+
let valid = true;
|
|
2350
|
+
for (const d of distances) {
|
|
2351
|
+
const enc = _distanceCodeFor(d, np, nd);
|
|
2352
|
+
if (!enc || enc.dsym >= alpha) { valid = false; break; }
|
|
2353
|
+
++cnt[enc.dsym];
|
|
2354
|
+
extraBitsSum += enc.extraBits;
|
|
2355
|
+
}
|
|
2356
|
+
if (!valid) continue;
|
|
2357
|
+
// Entropy in bits + extras + alphabet overhead.
|
|
2358
|
+
const total = distances.length;
|
|
2359
|
+
let entropyBits = 0;
|
|
2360
|
+
const logTotal = Math.log2(total);
|
|
2361
|
+
for (let s = 0; s < alpha; ++s) {
|
|
2362
|
+
const c = cnt[s];
|
|
2363
|
+
if (c > 0) entropyBits += c * (logTotal - Math.log2(c));
|
|
2364
|
+
}
|
|
2365
|
+
const headerOverheadBits = alpha * 5; // crude estimate
|
|
2366
|
+
const score = entropyBits + extraBitsSum + headerOverheadBits;
|
|
2367
|
+
if (score < bestScore) {
|
|
2368
|
+
bestScore = score;
|
|
2369
|
+
bestNpost = np;
|
|
2370
|
+
bestNdir = nd;
|
|
2371
|
+
bestAlpha = alpha;
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
return { npostfix: bestNpost, ndirect: bestNdir, distAlphabet: bestAlpha };
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
function _encodeCompressed(data, opts) {
|
|
2378
|
+
const len = data.length;
|
|
2379
|
+
const ntreesL = _NTREESL_OPT;
|
|
2380
|
+
|
|
2381
|
+
// Stream WBITS - by default we declare 22, matching the LZ77
|
|
2382
|
+
// engine's `windowBits: 21` (max distance 2²¹ ≤ 2²² − 16). With
|
|
2383
|
+
// `_ext.windowBits`, callers (typically `brotli_shared`) can
|
|
2384
|
+
// request a different value; combined with `_ext.largeWindow`
|
|
2385
|
+
// (RFC 9841 §6) values up to 62 become legal.
|
|
2386
|
+
const _ext = (opts && opts._ext) || null;
|
|
2387
|
+
const largeWindow = !!(_ext && _ext.largeWindow);
|
|
2388
|
+
const wbits = (_ext && _ext.windowBits) || 22;
|
|
2389
|
+
if (!largeWindow && (wbits < 10 || wbits > 24)) {
|
|
2390
|
+
_err('EBADARG', 'wbits ' + wbits + ' out of RFC 7932 range [10, 24]');
|
|
2391
|
+
}
|
|
2392
|
+
if (largeWindow && (wbits < 10 || wbits > 50)) {
|
|
2393
|
+
_err('EBADARG', 'large window wbits ' + wbits + ' out of supported [10, 50]');
|
|
2394
|
+
}
|
|
2395
|
+
const windowSize = wbits >= 31
|
|
2396
|
+
? Math.pow(2, wbits) - 16
|
|
2397
|
+
: (1 << wbits) - 16;
|
|
2398
|
+
|
|
2399
|
+
// Pick best CMODE for this input (RFC 7932 §7.1). The same
|
|
2400
|
+
// CMODE drives both the frequency-collection pass and the
|
|
2401
|
+
// bit-writing pass; we emit it in the meta-block header.
|
|
2402
|
+
const cmode = _pickCMode(data);
|
|
2403
|
+
|
|
2404
|
+
// Extension hook: `opts._ext.lz77Prefix` (Uint8Array) is a
|
|
2405
|
+
// virtual prefix to the input, used so that LZ77 can find
|
|
2406
|
+
// matches into a shared dictionary. The decoder side (when
|
|
2407
|
+
// also passed an `_ext.lz77Dict` matching this prefix)
|
|
2408
|
+
// resolves distances `pos - dist < L` via §3.2's dict-address
|
|
2409
|
+
// formula. brotli.js itself does not validate or interpret
|
|
2410
|
+
// this beyond the prepending - `brotli_shared` owns the
|
|
2411
|
+
// user-facing `sharedDictionary` option.
|
|
2412
|
+
//
|
|
2413
|
+
// Constraint: works only when `outPos ≤ windowSize` at emit;
|
|
2414
|
+
// inputs > windowSize bypass the compressed path so this holds.
|
|
2415
|
+
const ext = (opts && opts._ext) || null;
|
|
2416
|
+
let lz77Prefix = (ext && ext.lz77Prefix) || null;
|
|
2417
|
+
if (lz77Prefix && !(lz77Prefix instanceof u8)) {
|
|
2418
|
+
_err('EBADARG', '_ext.lz77Prefix must be Uint8Array');
|
|
2419
|
+
}
|
|
2420
|
+
if (lz77Prefix && lz77Prefix.length + data.length > windowSize) {
|
|
2421
|
+
lz77Prefix = null;
|
|
2422
|
+
}
|
|
2423
|
+
const prefixLen = lz77Prefix ? lz77Prefix.length : 0;
|
|
2424
|
+
let inputBuf = data;
|
|
2425
|
+
if (prefixLen > 0) {
|
|
2426
|
+
inputBuf = new u8(prefixLen + data.length);
|
|
2427
|
+
inputBuf.set(lz77Prefix, 0);
|
|
2428
|
+
inputBuf.set(data, prefixLen);
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// --- 1. LZ77 commands --------------------------------------
|
|
2432
|
+
const commands = [];
|
|
2433
|
+
let insertStart = 0;
|
|
2434
|
+
let insertLen = 0;
|
|
2435
|
+
|
|
2436
|
+
// Quality-driven LZ77 tuning (RFC 7932 §10 informative). The
|
|
2437
|
+
// mapping balances speed vs ratio: low quality = shallow chain,
|
|
2438
|
+
// no lazy match; high quality = deep chain, lazy enabled.
|
|
2439
|
+
// q=0 is handled by `_encodeUncompressed` (never reaches here).
|
|
2440
|
+
const quality = (opts && opts.quality != null) ? opts.quality : 6;
|
|
2441
|
+
const qParams =
|
|
2442
|
+
quality <= 1 ? { chainDepth: 2, lazy: false } :
|
|
2443
|
+
quality <= 3 ? { chainDepth: 4, lazy: false } :
|
|
2444
|
+
quality <= 5 ? { chainDepth: 6, lazy: true } :
|
|
2445
|
+
quality <= 7 ? { chainDepth: 10, lazy: true } :
|
|
2446
|
+
quality <= 9 ? { chainDepth: 16, lazy: true } :
|
|
2447
|
+
{ chainDepth: 32, lazy: true };
|
|
2448
|
+
lz77.encode(inputBuf, { windowBits: 21, minMatch: 4, ...qParams }, {
|
|
2449
|
+
literal(pos) {
|
|
2450
|
+
if (pos < prefixLen) return; // dict prefix, not output
|
|
2451
|
+
const dpos = pos - prefixLen;
|
|
2452
|
+
if (insertLen === 0) insertStart = dpos;
|
|
2453
|
+
insertLen++;
|
|
2454
|
+
},
|
|
2455
|
+
match(pos, mlen, dist) {
|
|
2456
|
+
if (pos < prefixLen) return; // match fully inside dict, not output
|
|
2457
|
+
while (insertLen > 22593) {
|
|
2458
|
+
commands.push({ insertStart, insertLen: 22593, copyLen: 2, distance: 1 });
|
|
2459
|
+
insertStart += 22593;
|
|
2460
|
+
insertLen -= 22593;
|
|
2461
|
+
}
|
|
2462
|
+
commands.push({ insertStart, insertLen, copyLen: mlen, distance: dist });
|
|
2463
|
+
insertStart = 0;
|
|
2464
|
+
insertLen = 0;
|
|
2465
|
+
},
|
|
2466
|
+
});
|
|
2467
|
+
if (insertLen > 0) {
|
|
2468
|
+
commands.push({ insertStart, insertLen, copyLen: 0, distance: 0 });
|
|
2469
|
+
}
|
|
2470
|
+
if (commands.length === 0) {
|
|
2471
|
+
commands.push({ insertStart: 0, insertLen: 0, copyLen: 0, distance: 0 });
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
// --- 1b. Augment with static-dict references.
|
|
2475
|
+
// Priority: custom-dict hook (RFC 9841 §3.1) over built-in
|
|
2476
|
+
// RFC 7932 dict. When neither is usable, leave commands as-is.
|
|
2477
|
+
const customMatchFinder = _ext && _ext.findCustomDictMatch;
|
|
2478
|
+
const dictHash = customMatchFinder ? null : _getStaticDictHash();
|
|
2479
|
+
const cmds = (customMatchFinder || dictHash)
|
|
2480
|
+
? _augmentWithStaticDictRefs(commands, data, dictHash, windowSize, customMatchFinder, prefixLen)
|
|
2481
|
+
: commands;
|
|
2482
|
+
|
|
2483
|
+
// --- 1c. Pick distance encoding params (NPOSTFIX, NDIRECT)
|
|
2484
|
+
// based on the actual distance distribution.
|
|
2485
|
+
const distParams = _pickDistParams(cmds);
|
|
2486
|
+
const npostfix = distParams.npostfix;
|
|
2487
|
+
const ndirect = distParams.ndirect;
|
|
2488
|
+
const distAlphabet = distParams.distAlphabet;
|
|
2489
|
+
|
|
2490
|
+
// --- 1d. Decide whether to split literals into 2 block types
|
|
2491
|
+
// (RFC 7932 §6). Activated when:
|
|
2492
|
+
// - input is long enough that header overhead amortises;
|
|
2493
|
+
// - the two halves have measurably different byte
|
|
2494
|
+
// distributions (KL divergence > threshold).
|
|
2495
|
+
//
|
|
2496
|
+
// When `splitLiteral` is non-null the literal channel is
|
|
2497
|
+
// encoded with NBLTYPES_L = 2 + NTREESL = 8 (4 trees per
|
|
2498
|
+
// block), with a single block-switch at byte position
|
|
2499
|
+
// `splitLiteral.splitBytePos`.
|
|
2500
|
+
const splitLiteral = _pickLitBlockSplit(data, quality);
|
|
2501
|
+
const useSplit = !!splitLiteral;
|
|
2502
|
+
const ntreesLActual = useSplit ? 8 : ntreesL;
|
|
2503
|
+
|
|
2504
|
+
// --- 2. Resolve commands; compute IAC + distance frequencies.
|
|
2505
|
+
// For literals, ALSO compute the per-tree frequencies
|
|
2506
|
+
// by tracking p1, p2 across literals and matches.
|
|
2507
|
+
const litFreqPerTree = new Array(ntreesLActual);
|
|
2508
|
+
for (let t = 0; t < ntreesLActual; ++t) litFreqPerTree[t] = new Int32Array(256);
|
|
2509
|
+
const iacFreq = new Int32Array(704);
|
|
2510
|
+
const distFreq = new Int32Array(distAlphabet);
|
|
2511
|
+
const cmdAux = new Array(cmds.length);
|
|
2512
|
+
|
|
2513
|
+
let outPos = 0, p1 = 0, p2 = 0;
|
|
2514
|
+
const splitBytePos = useSplit ? splitLiteral.splitBytePos : -1;
|
|
2515
|
+
// Block-switch wire format counts LITERALS, not output bytes.
|
|
2516
|
+
// We tally per-block literal counts during freq collection.
|
|
2517
|
+
let litCountBlock0 = 0;
|
|
2518
|
+
let litCountBlock1 = 0;
|
|
2519
|
+
|
|
2520
|
+
for (let ci = 0; ci < cmds.length; ++ci) {
|
|
2521
|
+
const c = cmds[ci];
|
|
2522
|
+
// Literals - frequencies AND state update
|
|
2523
|
+
for (let k = 0; k < c.insertLen; ++k) {
|
|
2524
|
+
const byte = data[c.insertStart + k];
|
|
2525
|
+
const ctxId = _contextIdLit(cmode, p1, p2);
|
|
2526
|
+
const baseTree = _clusterContext(ctxId);
|
|
2527
|
+
const inBlock1 = useSplit && outPos >= splitBytePos;
|
|
2528
|
+
const treeId = inBlock1 ? 4 + baseTree : baseTree;
|
|
2529
|
+
++litFreqPerTree[treeId][byte];
|
|
2530
|
+
if (useSplit) {
|
|
2531
|
+
if (inBlock1) ++litCountBlock1;
|
|
2532
|
+
else ++litCountBlock0;
|
|
2533
|
+
}
|
|
2534
|
+
p2 = p1; p1 = byte;
|
|
2535
|
+
++outPos;
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
// IAC + distance resolution
|
|
2539
|
+
const isLastNoCopy = c.copyLen === 0;
|
|
2540
|
+
const effCopyLen = isLastNoCopy ? 2 : c.copyLen;
|
|
2541
|
+
const ic = _insertCodeFor(c.insertLen);
|
|
2542
|
+
const cc = _copyCodeFor(effCopyLen);
|
|
2543
|
+
if (!ic || !cc) _err('EINTERNAL', 'cannot encode I=' + c.insertLen + ' C=' + effCopyLen);
|
|
2544
|
+
|
|
2545
|
+
const iacSym = _findIacSym(ic.code, cc.code, false);
|
|
2546
|
+
if (iacSym < 0) _err('EINTERNAL', 'no IAC sym for I=' + ic.code + ' C=' + cc.code);
|
|
2547
|
+
++iacFreq[iacSym];
|
|
2548
|
+
|
|
2549
|
+
let distEnc = null;
|
|
2550
|
+
if (!isLastNoCopy) {
|
|
2551
|
+
distEnc = _distanceCodeFor(c.distance, npostfix, ndirect);
|
|
2552
|
+
if (!distEnc || distEnc.dsym >= distAlphabet) {
|
|
2553
|
+
_err('EINTERNAL', 'distance ' + c.distance + ' outside alphabet');
|
|
2554
|
+
}
|
|
2555
|
+
++distFreq[distEnc.dsym];
|
|
2556
|
+
|
|
2557
|
+
// Advance p1, p2 across the copy.
|
|
2558
|
+
// - Static dict ref: copied bytes = data[inputPos + k]
|
|
2559
|
+
// (the dict word matches `data` at that position,
|
|
2560
|
+
// so reading `data` is equivalent and avoids
|
|
2561
|
+
// needing the brotli dict here).
|
|
2562
|
+
// - Backward ref / LZ77 dict ref: byte at virtual
|
|
2563
|
+
// source position `outPos - distance`. Negative
|
|
2564
|
+
// indices reach into the LZ77 shared dictionary.
|
|
2565
|
+
// @ts-ignore - isDictRef/dictOutputLen are present when isDictRef===true; TS cannot narrow union from this field
|
|
2566
|
+
if (c.isDictRef) {
|
|
2567
|
+
const inputPos = c.insertStart + c.insertLen;
|
|
2568
|
+
// @ts-ignore - isDictRef/dictOutputLen are present when isDictRef===true; TS cannot narrow union from this field
|
|
2569
|
+
const outLen = c.dictOutputLen != null ? c.dictOutputLen : c.copyLen;
|
|
2570
|
+
for (let k = 0; k < outLen; ++k) {
|
|
2571
|
+
const byte = data[inputPos + k];
|
|
2572
|
+
p2 = p1; p1 = byte;
|
|
2573
|
+
++outPos;
|
|
2574
|
+
}
|
|
2575
|
+
} else {
|
|
2576
|
+
for (let k = 0; k < c.copyLen; ++k) {
|
|
2577
|
+
const srcPos = outPos - c.distance;
|
|
2578
|
+
const byte = srcPos >= 0 ? data[srcPos] : lz77Prefix[prefixLen + srcPos];
|
|
2579
|
+
p2 = p1; p1 = byte;
|
|
2580
|
+
++outPos;
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
cmdAux[ci] = { ic, cc, iacSym, distEnc };
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2587
|
+
// Validity check: a split with empty block 0 or block 1 is
|
|
2588
|
+
// not encodable (block-length must be ≥ 1). Edge case where
|
|
2589
|
+
// a copy spans the split byte position with no literals in
|
|
2590
|
+
// one of the halves. Fall back to single-block: merge block-1
|
|
2591
|
+
// tree frequencies into block-0 trees, repurpose just the
|
|
2592
|
+
// first 4 trees.
|
|
2593
|
+
let useSplitFinal = useSplit;
|
|
2594
|
+
if (useSplit && (litCountBlock0 === 0 || litCountBlock1 === 0)) {
|
|
2595
|
+
for (let t = 0; t < 4; ++t) {
|
|
2596
|
+
const src = litFreqPerTree[4 + t];
|
|
2597
|
+
const dst = litFreqPerTree[t];
|
|
2598
|
+
for (let s = 0; s < 256; ++s) dst[s] += src[s];
|
|
2599
|
+
}
|
|
2600
|
+
useSplitFinal = false;
|
|
2601
|
+
}
|
|
2602
|
+
const nbltypesLFinal = useSplitFinal ? 2 : 1;
|
|
2603
|
+
const ntreesLFinal = useSplitFinal ? 8 : 4;
|
|
2604
|
+
|
|
2605
|
+
// Pad to dodge single-symbol-target edge case in each alphabet.
|
|
2606
|
+
for (let t = 0; t < ntreesLFinal; ++t) _padFreqs(litFreqPerTree[t]);
|
|
2607
|
+
_padFreqs(iacFreq);
|
|
2608
|
+
_padFreqs(distFreq);
|
|
2609
|
+
|
|
2610
|
+
// --- 3. Canonical Huffman --------------------------------
|
|
2611
|
+
const litTrees = new Array(ntreesLFinal);
|
|
2612
|
+
const litLensArr = new Array(ntreesLFinal);
|
|
2613
|
+
const litMapArr = new Array(ntreesLFinal);
|
|
2614
|
+
for (let t = 0; t < ntreesLFinal; ++t) {
|
|
2615
|
+
const tr = huffman.buildTree(litFreqPerTree[t], 15);
|
|
2616
|
+
litTrees[t] = tr;
|
|
2617
|
+
litLensArr[t] = _padTo(tr.t, 256);
|
|
2618
|
+
litMapArr[t] = huffman.buildMap(litLensArr[t], Math.max(tr.l, 1), 0);
|
|
2619
|
+
}
|
|
2620
|
+
const iacTree = huffman.buildTree(iacFreq, 15);
|
|
2621
|
+
const distTree = huffman.buildTree(distFreq, 15);
|
|
2622
|
+
|
|
2623
|
+
const iacLens = _padTo(iacTree.t, 704);
|
|
2624
|
+
const distLens = _padTo(distTree.t, distAlphabet);
|
|
2625
|
+
const iacMap = huffman.buildMap(iacLens, Math.max(iacTree.l, 1), 0);
|
|
2626
|
+
const distMap = huffman.buildMap(distLens, Math.max(distTree.l, 1), 0);
|
|
2627
|
+
|
|
2628
|
+
// --- 4. CMAPL - 64 entries per block type.
|
|
2629
|
+
// - Single block: cmapL[i] = cluster(i)
|
|
2630
|
+
// - Split: cmapL[i] = cluster(i % 64) for i ∈ [0,64);
|
|
2631
|
+
// cmapL[i] = 4 + cluster(i % 64) for i ∈ [64,128)
|
|
2632
|
+
// so block 0 uses trees 0..3 and block 1 uses 4..7.
|
|
2633
|
+
const cmapLSize = 64 * nbltypesLFinal;
|
|
2634
|
+
const cmapL = new u8(cmapLSize);
|
|
2635
|
+
for (let i = 0; i < 64; ++i) {
|
|
2636
|
+
const cl = _clusterContext(i);
|
|
2637
|
+
cmapL[i] = cl;
|
|
2638
|
+
if (useSplitFinal) cmapL[64 + i] = 4 + cl;
|
|
2639
|
+
}
|
|
2640
|
+
|
|
2641
|
+
// --- 5. Emit ----------------------------------------------
|
|
2642
|
+
const w = _makeWriter(len + 256);
|
|
2643
|
+
|
|
2644
|
+
// Stream + meta-block headers
|
|
2645
|
+
_emitWBITS(w, wbits, largeWindow); // RFC 7932 §9.1 / RFC 9841 §6
|
|
2646
|
+
w.bits(1, 1); // ISLAST = 1
|
|
2647
|
+
w.bits(1, 0); // ISLASTEMPTY = 0
|
|
2648
|
+
// MNIBBLES & MLEN
|
|
2649
|
+
let mnibbles, mnibblesCode;
|
|
2650
|
+
if (len <= 65536) { mnibbles = 4; mnibblesCode = 0; }
|
|
2651
|
+
else if (len <= 1048576) { mnibbles = 5; mnibblesCode = 1; }
|
|
2652
|
+
else { mnibbles = 6; mnibblesCode = 2; }
|
|
2653
|
+
w.bits(2, mnibblesCode);
|
|
2654
|
+
if (mnibbles <= 4) {
|
|
2655
|
+
w.bits(mnibbles * 4, (len - 1) & 0xFFFF);
|
|
2656
|
+
} else if (mnibbles === 5) {
|
|
2657
|
+
w.bits(20, (len - 1) & 0xFFFFF);
|
|
2658
|
+
} else {
|
|
2659
|
+
w.bits(24, (len - 1) & 0xFFFFFF);
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
// Compressed meta-block header.
|
|
2663
|
+
// NBLTYPES_L: 1 (no split) or 2 (with literal block split).
|
|
2664
|
+
// The decoder reads block-type / block-length Huffman trees
|
|
2665
|
+
// only when NBLTYPES >= 2.
|
|
2666
|
+
let blTypeMap = null, blTypeLens = null, blLenMap = null, blLenLens = null;
|
|
2667
|
+
let blcL1 = null;
|
|
2668
|
+
const splitL0 = useSplitFinal ? litCountBlock0 : 0;
|
|
2669
|
+
if (useSplitFinal) {
|
|
2670
|
+
_emitVarLenCount(w, 2); // NBLTYPESL = 2
|
|
2671
|
+
// Block-type Huffman over alphabet 4. We only ever emit
|
|
2672
|
+
// sym=1 (toggle to next block type), but use a 2-symbol
|
|
2673
|
+
// tree {0:1bit, 1:1bit} to avoid the single-symbol decoder
|
|
2674
|
+
// corner case at encode time.
|
|
2675
|
+
const blTypeFreq = new Int32Array(4);
|
|
2676
|
+
blTypeFreq[0] = 1;
|
|
2677
|
+
blTypeFreq[1] = 1;
|
|
2678
|
+
const blTypeTree = huffman.buildTree(blTypeFreq, 15);
|
|
2679
|
+
blTypeLens = _padTo(blTypeTree.t, 4);
|
|
2680
|
+
blTypeMap = huffman.buildMap(blTypeLens, Math.max(blTypeTree.l, 1), 0);
|
|
2681
|
+
_emitPrefixCodeDescriptor(w, blTypeLens);
|
|
2682
|
+
// Block-length Huffman over alphabet 26. Frequencies seeded
|
|
2683
|
+
// by the two block lengths (in LITERAL counts, not bytes).
|
|
2684
|
+
const blcL0 = _blockCountCodeFor(litCountBlock0);
|
|
2685
|
+
blcL1 = _blockCountCodeFor(litCountBlock1);
|
|
2686
|
+
if (!blcL0 || !blcL1) _err('EINTERNAL', 'block-length out of range');
|
|
2687
|
+
const blLenFreq = new Int32Array(26);
|
|
2688
|
+
++blLenFreq[blcL0.sym];
|
|
2689
|
+
++blLenFreq[blcL1.sym];
|
|
2690
|
+
_padFreqs(blLenFreq);
|
|
2691
|
+
const blLenTree = huffman.buildTree(blLenFreq, 15);
|
|
2692
|
+
blLenLens = _padTo(blLenTree.t, 26);
|
|
2693
|
+
blLenMap = huffman.buildMap(blLenLens, Math.max(blLenTree.l, 1), 0);
|
|
2694
|
+
_emitPrefixCodeDescriptor(w, blLenLens);
|
|
2695
|
+
// Initial block length L0
|
|
2696
|
+
w.bits(blLenLens[blcL0.sym], blLenMap[blcL0.sym]);
|
|
2697
|
+
if (blcL0.extraBits > 0) w.bits(blcL0.extraBits, blcL0.extra);
|
|
2698
|
+
} else {
|
|
2699
|
+
w.bits(1, 0); // NBLTYPESL = 1
|
|
2700
|
+
}
|
|
2701
|
+
w.bits(1, 0); // NBLTYPESI = 1
|
|
2702
|
+
w.bits(1, 0); // NBLTYPESD = 1
|
|
2703
|
+
w.bits(2, npostfix); // NPOSTFIX (RFC 7932 §4)
|
|
2704
|
+
w.bits(4, ndirect >> npostfix); // NDIRECT_HI (NDIRECT = HI << NPOSTFIX)
|
|
2705
|
+
// CMODE - one per literal block type
|
|
2706
|
+
w.bits(2, cmode);
|
|
2707
|
+
if (useSplitFinal) w.bits(2, cmode); // same global cmode for block 1
|
|
2708
|
+
// NTREESL via var-len code
|
|
2709
|
+
_emitVarLenCount(w, ntreesLFinal);
|
|
2710
|
+
// CMAPL - for the split case, 128 entries mapping (btype, cidL)
|
|
2711
|
+
// pairs to one of 8 trees (4 per block).
|
|
2712
|
+
_emitContextMap(w, cmapL, ntreesLFinal);
|
|
2713
|
+
// NTREESD = 1
|
|
2714
|
+
w.bits(1, 0);
|
|
2715
|
+
|
|
2716
|
+
// Prefix-code descriptors: NTREESL literal trees, then IAC, then distance.
|
|
2717
|
+
for (let t = 0; t < ntreesLFinal; ++t) {
|
|
2718
|
+
_emitPrefixCodeDescriptor(w, litLensArr[t]);
|
|
2719
|
+
}
|
|
2720
|
+
_emitPrefixCodeDescriptor(w, iacLens);
|
|
2721
|
+
_emitPrefixCodeDescriptor(w, distLens);
|
|
2722
|
+
|
|
2723
|
+
// Commands - re-track p1, p2 to mirror frequency-collection pass.
|
|
2724
|
+
outPos = 0; p1 = 0; p2 = 0;
|
|
2725
|
+
let splitEmitted = false;
|
|
2726
|
+
// Track literal count for block-switch trigger (the wire
|
|
2727
|
+
// format counts LITERALS, not output bytes).
|
|
2728
|
+
let litCount = 0;
|
|
2729
|
+
|
|
2730
|
+
for (let ci = 0; ci < cmds.length; ++ci) {
|
|
2731
|
+
const c = cmds[ci];
|
|
2732
|
+
const aux = cmdAux[ci];
|
|
2733
|
+
w.bits(iacLens[aux.iacSym], iacMap[aux.iacSym]);
|
|
2734
|
+
if (aux.ic.extraBits > 0) w.bits(aux.ic.extraBits, aux.ic.extra);
|
|
2735
|
+
if (aux.cc.extraBits > 0) w.bits(aux.cc.extraBits, aux.cc.extra);
|
|
2736
|
+
for (let k = 0; k < c.insertLen; ++k) {
|
|
2737
|
+
// Block-switch trigger fires when `litCount === L0`
|
|
2738
|
+
// just before emitting the (L0+1)-th literal - mirrors
|
|
2739
|
+
// the decoder's `if (blenL === 0)` check.
|
|
2740
|
+
if (useSplitFinal && !splitEmitted && litCount === splitL0) {
|
|
2741
|
+
// Block-switch: block-type symbol (sym=1 toggles
|
|
2742
|
+
// to next block type) + block-length L1.
|
|
2743
|
+
w.bits(blTypeLens[1], blTypeMap[1]);
|
|
2744
|
+
w.bits(blLenLens[blcL1.sym], blLenMap[blcL1.sym]);
|
|
2745
|
+
if (blcL1.extraBits > 0) w.bits(blcL1.extraBits, blcL1.extra);
|
|
2746
|
+
splitEmitted = true;
|
|
2747
|
+
}
|
|
2748
|
+
const byte = data[c.insertStart + k];
|
|
2749
|
+
const ctxId = _contextIdLit(cmode, p1, p2);
|
|
2750
|
+
const baseTree = _clusterContext(ctxId);
|
|
2751
|
+
const treeId = (useSplitFinal && litCount >= splitL0)
|
|
2752
|
+
? 4 + baseTree
|
|
2753
|
+
: baseTree;
|
|
2754
|
+
w.bits(litLensArr[treeId][byte], litMapArr[treeId][byte]);
|
|
2755
|
+
++litCount;
|
|
2756
|
+
p2 = p1; p1 = byte;
|
|
2757
|
+
++outPos;
|
|
2758
|
+
}
|
|
2759
|
+
if (aux.distEnc) {
|
|
2760
|
+
w.bits(distLens[aux.distEnc.dsym], distMap[aux.distEnc.dsym]);
|
|
2761
|
+
if (aux.distEnc.extraBits > 0) w.bits(aux.distEnc.extraBits, aux.distEnc.extra);
|
|
2762
|
+
// @ts-ignore - isDictRef/dictOutputLen are present when isDictRef===true; TS cannot narrow union from this field
|
|
2763
|
+
if (c.isDictRef) {
|
|
2764
|
+
const inputPos = c.insertStart + c.insertLen;
|
|
2765
|
+
// @ts-ignore - isDictRef/dictOutputLen are present when isDictRef===true; TS cannot narrow union from this field
|
|
2766
|
+
const outLen = c.dictOutputLen != null ? c.dictOutputLen : c.copyLen;
|
|
2767
|
+
for (let k = 0; k < outLen; ++k) {
|
|
2768
|
+
const byte = data[inputPos + k];
|
|
2769
|
+
p2 = p1; p1 = byte;
|
|
2770
|
+
++outPos;
|
|
2771
|
+
}
|
|
2772
|
+
} else {
|
|
2773
|
+
for (let k = 0; k < c.copyLen; ++k) {
|
|
2774
|
+
const srcPos = outPos - c.distance;
|
|
2775
|
+
const byte = srcPos >= 0 ? data[srcPos] : lz77Prefix[prefixLen + srcPos];
|
|
2776
|
+
p2 = p1; p1 = byte;
|
|
2777
|
+
++outPos;
|
|
2778
|
+
}
|
|
2779
|
+
}
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
|
|
2783
|
+
w.align();
|
|
2784
|
+
return w.bytes();
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2787
|
+
function brotliCompressSync(data, opts) {
|
|
2788
|
+
if (!(data instanceof u8)) _err('EBADARG', 'expected Uint8Array');
|
|
2789
|
+
const quality = (opts && opts.quality != null) ? opts.quality : 6;
|
|
2790
|
+
if (typeof quality !== 'number' || quality < 0 || quality > 11 || (quality | 0) !== quality) {
|
|
2791
|
+
_err('EBADARG', 'quality must be an integer in [0, 11], got ' + quality);
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2794
|
+
// Quality 0 → trivial uncompressed (smallest, fastest). Quality
|
|
2795
|
+
// 1..11 → LZ77 + dynamic Huffman, with lz77 chainDepth/lazy
|
|
2796
|
+
// tuned per quality bucket inside `_encodeCompressed`. Very
|
|
2797
|
+
// short inputs or inputs > 16 MiB fall back to trivial
|
|
2798
|
+
// (header overhead / MNIBBLES range).
|
|
2799
|
+
if (quality === 0 || data.length < 32 || data.length > 16777216) {
|
|
2800
|
+
return _encodeUncompressed(data);
|
|
2801
|
+
}
|
|
2802
|
+
try {
|
|
2803
|
+
const compressed = _encodeCompressed(data, opts);
|
|
2804
|
+
const trivial = _encodeUncompressed(data);
|
|
2805
|
+
return compressed.length <= trivial.length ? compressed : trivial;
|
|
2806
|
+
} catch (e) {
|
|
2807
|
+
if (e.code === 'EINTERNAL') return _encodeUncompressed(data);
|
|
2808
|
+
throw e;
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
function brotliCompress(data, opts) {
|
|
2813
|
+
return new Promise((resolve, reject) => {
|
|
2814
|
+
queueMicrotask(() => {
|
|
2815
|
+
try { resolve(brotliCompressSync(data, opts)); } catch (e) { reject(e); }
|
|
2816
|
+
});
|
|
2817
|
+
});
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
function brotliDecompress(data, opts) {
|
|
2821
|
+
return new Promise((resolve, reject) => {
|
|
2822
|
+
queueMicrotask(() => {
|
|
2823
|
+
try { resolve(brotliDecompressSync(data, opts)); } catch (e) { reject(e); }
|
|
2824
|
+
});
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
|
|
2828
|
+
// --- Step 11 - Streaming wrappers ---
|
|
2829
|
+
//
|
|
2830
|
+
// Both `BrotliCompressStream` and `BrotliDecompressStream` follow
|
|
2831
|
+
// the deflate/gzip streaming convention: construct with an
|
|
2832
|
+
// `(opts?, ondata)` pair where `ondata(chunk, isFinal)` is called
|
|
2833
|
+
// when output is available. `push(chunk, final)` accepts an
|
|
2834
|
+
// input chunk and, when `final === true`, runs the full
|
|
2835
|
+
// sync codec on the accumulated buffer and emits the result.
|
|
2836
|
+
//
|
|
2837
|
+
// Buffering rather than true incremental codec - the underlying
|
|
2838
|
+
// brotli engine is not resumable. For inputs that must be
|
|
2839
|
+
// processed in true streaming fashion (e.g. arbitrary 4 GB
|
|
2840
|
+
// streams), the consumer should chunk into separate brotli
|
|
2841
|
+
// streams or fall back to deflate/gzip which do support partial
|
|
2842
|
+
// emissions.
|
|
2843
|
+
|
|
2844
|
+
function _bufferPush(self, chunk, final) {
|
|
2845
|
+
if (!self.ondata) _err('EBADARG', 'stream: no ondata handler set');
|
|
2846
|
+
if (self._done) _err('ESTREAMEND', 'stream already finalised');
|
|
2847
|
+
if (chunk != null && chunk.length > 0) {
|
|
2848
|
+
if (!(chunk instanceof u8)) _err('EBADARG', 'stream.push: chunk must be Uint8Array');
|
|
2849
|
+
self._chunks.push(chunk);
|
|
2850
|
+
self._totalLen += chunk.length;
|
|
2851
|
+
}
|
|
2852
|
+
if (final) {
|
|
2853
|
+
self._done = true;
|
|
2854
|
+
let full;
|
|
2855
|
+
if (self._chunks.length === 0) {
|
|
2856
|
+
full = new u8(0);
|
|
2857
|
+
} else if (self._chunks.length === 1) {
|
|
2858
|
+
full = self._chunks[0];
|
|
2859
|
+
} else {
|
|
2860
|
+
full = new u8(self._totalLen);
|
|
2861
|
+
let off = 0;
|
|
2862
|
+
for (const c of self._chunks) { full.set(c, off); off += c.length; }
|
|
2863
|
+
}
|
|
2864
|
+
self._chunks = null;
|
|
2865
|
+
const out = self._sync(full, self._opts);
|
|
2866
|
+
self.ondata(out, true);
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
function BrotliCompressStream(opts, ondata) {
|
|
2871
|
+
if (typeof opts === 'function') { ondata = opts; opts = {}; }
|
|
2872
|
+
this.ondata = ondata;
|
|
2873
|
+
this._opts = opts || {};
|
|
2874
|
+
this._chunks = [];
|
|
2875
|
+
this._totalLen = 0;
|
|
2876
|
+
this._done = false;
|
|
2877
|
+
}
|
|
2878
|
+
BrotliCompressStream.prototype.push = function (chunk, final) {
|
|
2879
|
+
this._sync = brotliCompressSync;
|
|
2880
|
+
_bufferPush(this, chunk, final);
|
|
2881
|
+
};
|
|
2882
|
+
|
|
2883
|
+
// Truly incremental decompress stream: each `push(chunk, final)`
|
|
2884
|
+
// tries to decode as many full meta-blocks as the buffered input
|
|
2885
|
+
// allows, emits the newly-produced bytes through `ondata`, and
|
|
2886
|
+
// retains the rest of the input for the next push.
|
|
2887
|
+
//
|
|
2888
|
+
// The reader's EAGAIN mechanism distinguishes "input exhausted,
|
|
2889
|
+
// wait for more" (during non-final pushes) from "unexpected EOI"
|
|
2890
|
+
// (during final pushes / sync calls). Each meta-block attempt is
|
|
2891
|
+
// wrapped in a save/restore - on EAGAIN, decoder state rolls back
|
|
2892
|
+
// to before the attempt; on success it commits.
|
|
2893
|
+
|
|
2894
|
+
function _saveDecoderState(state) {
|
|
2895
|
+
return {
|
|
2896
|
+
outLen: state.outLen,
|
|
2897
|
+
p1: state.p1,
|
|
2898
|
+
p2: state.p2,
|
|
2899
|
+
lastDist: new Int32Array(state.lastDist),
|
|
2900
|
+
};
|
|
2901
|
+
}
|
|
2902
|
+
function _restoreDecoderState(state, saved) {
|
|
2903
|
+
state.outLen = saved.outLen;
|
|
2904
|
+
state.p1 = saved.p1;
|
|
2905
|
+
state.p2 = saved.p2;
|
|
2906
|
+
state.lastDist.set(saved.lastDist);
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
function BrotliDecompressStream(opts, ondata) {
|
|
2910
|
+
if (typeof opts === 'function') { ondata = opts; opts = {}; }
|
|
2911
|
+
this.ondata = ondata;
|
|
2912
|
+
this._opts = opts || {};
|
|
2913
|
+
this._chunks = []; // buffered raw input
|
|
2914
|
+
this._totalLen = 0;
|
|
2915
|
+
this._done = false;
|
|
2916
|
+
this._consumedBits = 0; // bit position into the concatenated input
|
|
2917
|
+
this._emittedLen = 0; // bytes already passed to ondata
|
|
2918
|
+
this._state = null; // decoder state (lazy init at first push)
|
|
2919
|
+
this._initDone = false; // WBITS header consumed?
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
BrotliDecompressStream.prototype.push = function (chunk, final) {
|
|
2923
|
+
if (!this.ondata) _err('EBADARG', 'stream: no ondata handler set');
|
|
2924
|
+
if (this._done) _err('ESTREAMEND', 'stream already finalised');
|
|
2925
|
+
|
|
2926
|
+
if (chunk != null && chunk.length > 0) {
|
|
2927
|
+
if (!(chunk instanceof u8)) _err('EBADARG', 'stream.push: chunk must be Uint8Array');
|
|
2928
|
+
this._chunks.push(chunk);
|
|
2929
|
+
this._totalLen += chunk.length;
|
|
2930
|
+
}
|
|
2931
|
+
|
|
2932
|
+
// Concatenate all buffered input. (For typical chunk counts
|
|
2933
|
+
// this is cheap; a rope-style reader would be a future opt.)
|
|
2934
|
+
let buf;
|
|
2935
|
+
if (this._chunks.length === 0) buf = new u8(0);
|
|
2936
|
+
else if (this._chunks.length === 1) buf = this._chunks[0];
|
|
2937
|
+
else {
|
|
2938
|
+
buf = new u8(this._totalLen);
|
|
2939
|
+
let off = 0;
|
|
2940
|
+
for (const c of this._chunks) { buf.set(c, off); off += c.length; }
|
|
2941
|
+
this._chunks = [buf];
|
|
2942
|
+
}
|
|
2943
|
+
|
|
2944
|
+
// Build the reader with the current concatenated buffer and
|
|
2945
|
+
// restore the bit position from the previous push.
|
|
2946
|
+
const r = _makeReader(buf, !final);
|
|
2947
|
+
r.p = this._consumedBits;
|
|
2948
|
+
|
|
2949
|
+
// Lazy init: read the stream header + create state on first push.
|
|
2950
|
+
if (!this._initDone) {
|
|
2951
|
+
const savedP = r.p;
|
|
2952
|
+
try {
|
|
2953
|
+
const ext = (this._opts && this._opts._ext) || null;
|
|
2954
|
+
const wbitsInfo = _readWBITS(r, ext && ext.allowLargeWindow);
|
|
2955
|
+
const wbits = wbitsInfo.wbits;
|
|
2956
|
+
const windowSize = wbits >= 31 ? Math.pow(2, wbits) - 16 : (1 << wbits) - 16;
|
|
2957
|
+
|
|
2958
|
+
this._state = {
|
|
2959
|
+
out: new u8(1024),
|
|
2960
|
+
outLen: 0,
|
|
2961
|
+
p1: 0,
|
|
2962
|
+
p2: 0,
|
|
2963
|
+
lastDist: new Int32Array([4, 11, 15, 16]),
|
|
2964
|
+
windowSize,
|
|
2965
|
+
lz77Dict: (ext && ext.lz77Dict) || null,
|
|
2966
|
+
largeWindow: wbitsInfo.largeWindow,
|
|
2967
|
+
resolveStaticDictRef: (ext && ext.resolveStaticDictRef) || null,
|
|
2968
|
+
};
|
|
2969
|
+
this._initDone = true;
|
|
2970
|
+
// Persist consumed bit offset so the next push resumes
|
|
2971
|
+
// AFTER the WBITS header.
|
|
2972
|
+
this._consumedBits = r.p;
|
|
2973
|
+
} catch (e) {
|
|
2974
|
+
if (e.code === 'EAGAIN') {
|
|
2975
|
+
r.p = savedP;
|
|
2976
|
+
// Stay un-initialised; next push retries from the same buffer.
|
|
2977
|
+
return;
|
|
2978
|
+
}
|
|
2979
|
+
throw e;
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
// Drive the meta-block loop until we hit EAGAIN or ISLASTEMPTY.
|
|
2984
|
+
while (!this._done) {
|
|
2985
|
+
const savedP = r.p;
|
|
2986
|
+
const savedState = _saveDecoderState(this._state);
|
|
2987
|
+
try {
|
|
2988
|
+
const res = _decodeMetaBlockInto(r, this._state);
|
|
2989
|
+
this._consumedBits = r.p;
|
|
2990
|
+
if (res.last) {
|
|
2991
|
+
// Trailing-byte fill zeros check (sync path does the
|
|
2992
|
+
// same after the last meta-block).
|
|
2993
|
+
_verifyByteTailZero(r);
|
|
2994
|
+
this._done = true;
|
|
2995
|
+
}
|
|
2996
|
+
} catch (e) {
|
|
2997
|
+
if (e.code === 'EAGAIN') {
|
|
2998
|
+
r.p = savedP;
|
|
2999
|
+
_restoreDecoderState(this._state, savedState);
|
|
3000
|
+
break;
|
|
3001
|
+
}
|
|
3002
|
+
throw e;
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
// Emit newly-produced bytes.
|
|
3007
|
+
if (this._state.outLen > this._emittedLen) {
|
|
3008
|
+
const slice = this._state.out.slice(this._emittedLen, this._state.outLen);
|
|
3009
|
+
this._emittedLen = this._state.outLen;
|
|
3010
|
+
this.ondata(slice, this._done);
|
|
3011
|
+
} else if (this._done) {
|
|
3012
|
+
// Empty stream - emit a zero-length final chunk so consumers
|
|
3013
|
+
// see the `final=true` signal.
|
|
3014
|
+
this.ondata(new u8(0), true);
|
|
3015
|
+
} else if (final && !this._done) {
|
|
3016
|
+
_err('EBADSTREAM', 'stream finalised with unconsumed input or unterminated stream');
|
|
3017
|
+
}
|
|
3018
|
+
|
|
3019
|
+
// Free buffered bytes already consumed (compact the chunk buffer).
|
|
3020
|
+
if (this._chunks.length === 1) {
|
|
3021
|
+
const consumedBytes = this._consumedBits >> 3;
|
|
3022
|
+
if (consumedBytes > 0 && consumedBytes < this._chunks[0].length) {
|
|
3023
|
+
this._chunks[0] = this._chunks[0].subarray(consumedBytes);
|
|
3024
|
+
this._totalLen -= consumedBytes;
|
|
3025
|
+
this._consumedBits &= 7; // keep the sub-byte residue
|
|
3026
|
+
} else if (consumedBytes === this._chunks[0].length) {
|
|
3027
|
+
// Fully consumed (likely after final). Drop the buffer.
|
|
3028
|
+
this._chunks = [];
|
|
3029
|
+
this._totalLen = 0;
|
|
3030
|
+
this._consumedBits = this._consumedBits & 7;
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
};
|
|
3034
|
+
|
|
3035
|
+
return {
|
|
3036
|
+
brotliCompressSync,
|
|
3037
|
+
brotliDecompressSync,
|
|
3038
|
+
brotliCompress,
|
|
3039
|
+
brotliDecompress,
|
|
3040
|
+
BrotliCompressStream,
|
|
3041
|
+
BrotliDecompressStream,
|
|
3042
|
+
// Internal primitives shared with the codec's own tests and
|
|
3043
|
+
// potentially with future companion modules (e.g. a brotli
|
|
3044
|
+
// inspector / debugger). Same `_internal` convention as
|
|
3045
|
+
// chacha20, sha256, argon2, etc.
|
|
3046
|
+
_internal: {
|
|
3047
|
+
makeReader: _makeReader,
|
|
3048
|
+
readBit: _readBit,
|
|
3049
|
+
readBits: _readBits,
|
|
3050
|
+
readBitsBig: _readBitsBig,
|
|
3051
|
+
readWBITS: _readWBITS,
|
|
3052
|
+
readPrefixCode: _readPrefixCode,
|
|
3053
|
+
readVarLenCount: _readVarLenCount,
|
|
3054
|
+
readBlockCount: _readBlockCount,
|
|
3055
|
+
readRleMax: _readRleMax,
|
|
3056
|
+
inverseMoveToFront: _inverseMoveToFront,
|
|
3057
|
+
readContextMap: _readContextMap,
|
|
3058
|
+
readCompressedMetaBlockHeader: _readCompressedMetaBlockHeader,
|
|
3059
|
+
splitIacSym: _splitIacSym,
|
|
3060
|
+
decodeDistanceSymbol: _decodeDistanceSymbol,
|
|
3061
|
+
contextIdLit: _contextIdLit,
|
|
3062
|
+
LUT0: _LUT0,
|
|
3063
|
+
LUT1: _LUT1,
|
|
3064
|
+
LUT2: _LUT2,
|
|
3065
|
+
INSERT_BASE: _INSERT_BASE,
|
|
3066
|
+
INSERT_EXTRA: _INSERT_EXTRA,
|
|
3067
|
+
COPY_BASE: _COPY_BASE,
|
|
3068
|
+
COPY_EXTRA: _COPY_EXTRA,
|
|
3069
|
+
encodeUncompressed: _encodeUncompressed,
|
|
3070
|
+
},
|
|
3071
|
+
};
|
|
3072
|
+
}
|
|
3073
|
+
};
|