xz-compat 0.3.2 → 1.0.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/README.md +7 -7
- package/dist/cjs/compat.js.map +1 -1
- package/dist/cjs/filters/bcj/Bcj.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjArm.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjArm64.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjArmt.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjIa64.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjPpc.js.map +1 -1
- package/dist/cjs/filters/bcj/BcjSparc.js.map +1 -1
- package/dist/cjs/filters/delta/Delta.js.map +1 -1
- package/dist/cjs/filters/index.js.map +1 -1
- package/dist/cjs/index.d.cts +4 -3
- package/dist/cjs/index.d.ts +4 -3
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lzma/index.js.map +1 -1
- package/dist/cjs/lzma/lib/Lzma2ChunkParser.js.map +1 -1
- package/dist/cjs/lzma/stream/transforms.js.map +1 -1
- package/dist/cjs/lzma/sync/Lzma2Decoder.js.map +1 -1
- package/dist/cjs/lzma/sync/LzmaDecoder.d.cts +3 -3
- package/dist/cjs/lzma/sync/LzmaDecoder.d.ts +3 -3
- package/dist/cjs/lzma/sync/LzmaDecoder.js.map +1 -1
- package/dist/cjs/lzma/sync/RangeDecoder.js.map +1 -1
- package/dist/cjs/lzma/types.js.map +1 -1
- package/dist/cjs/native.d.cts +10 -15
- package/dist/cjs/native.d.ts +10 -15
- package/dist/cjs/native.js +88 -12
- package/dist/cjs/native.js.map +1 -1
- package/dist/cjs/sevenz.d.cts +8 -16
- package/dist/cjs/sevenz.d.ts +8 -16
- package/dist/cjs/sevenz.js +52 -38
- package/dist/cjs/sevenz.js.map +1 -1
- package/dist/cjs/utils/createBufferingDecoder.js.map +1 -1
- package/dist/cjs/utils/runDecode.d.cts +19 -0
- package/dist/cjs/utils/runDecode.d.ts +19 -0
- package/dist/cjs/utils/runDecode.js +90 -0
- package/dist/cjs/utils/runDecode.js.map +1 -0
- package/dist/cjs/xz/Decoder.d.cts +5 -8
- package/dist/cjs/xz/Decoder.d.ts +5 -8
- package/dist/cjs/xz/Decoder.js +25 -37
- package/dist/cjs/xz/Decoder.js.map +1 -1
- package/dist/esm/compat.js.map +1 -1
- package/dist/esm/filters/bcj/Bcj.js.map +1 -1
- package/dist/esm/filters/bcj/BcjArm.js.map +1 -1
- package/dist/esm/filters/bcj/BcjArm64.js.map +1 -1
- package/dist/esm/filters/bcj/BcjArmt.js.map +1 -1
- package/dist/esm/filters/bcj/BcjIa64.js.map +1 -1
- package/dist/esm/filters/bcj/BcjPpc.js.map +1 -1
- package/dist/esm/filters/bcj/BcjSparc.js.map +1 -1
- package/dist/esm/filters/delta/Delta.js.map +1 -1
- package/dist/esm/filters/index.js.map +1 -1
- package/dist/esm/index.d.ts +4 -3
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lzma/index.js.map +1 -1
- package/dist/esm/lzma/lib/Lzma2ChunkParser.js.map +1 -1
- package/dist/esm/lzma/stream/transforms.js.map +1 -1
- package/dist/esm/lzma/sync/Lzma2Decoder.js.map +1 -1
- package/dist/esm/lzma/sync/LzmaDecoder.d.ts +3 -3
- package/dist/esm/lzma/sync/LzmaDecoder.js +3 -3
- package/dist/esm/lzma/sync/LzmaDecoder.js.map +1 -1
- package/dist/esm/lzma/sync/RangeDecoder.js.map +1 -1
- package/dist/esm/lzma/types.js.map +1 -1
- package/dist/esm/native.d.ts +10 -15
- package/dist/esm/native.js +85 -13
- package/dist/esm/native.js.map +1 -1
- package/dist/esm/sevenz.d.ts +8 -16
- package/dist/esm/sevenz.js +36 -50
- package/dist/esm/sevenz.js.map +1 -1
- package/dist/esm/utils/createBufferingDecoder.js.map +1 -1
- package/dist/esm/utils/runDecode.d.ts +19 -0
- package/dist/esm/utils/runDecode.js +62 -0
- package/dist/esm/utils/runDecode.js.map +1 -0
- package/dist/esm/xz/Decoder.d.ts +5 -8
- package/dist/esm/xz/Decoder.js +22 -42
- package/dist/esm/xz/Decoder.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ xz-compat is a complete pure JavaScript implementation of XZ decompression with
|
|
|
20
20
|
- ✅ **Delta Filter**: Byte-level delta encoding
|
|
21
21
|
- ✅ **Streaming & Sync**: Both streaming transforms and synchronous decoding
|
|
22
22
|
- ✅ **Node 0.8+**: Works on legacy Node.js versions
|
|
23
|
-
- ✅ **Native Acceleration**: Optional
|
|
23
|
+
- ✅ **Native Acceleration**: Optional lzma-native on Node.js 10+ for 3-5x performance boost
|
|
24
24
|
|
|
25
25
|
## Installation
|
|
26
26
|
|
|
@@ -30,13 +30,13 @@ npm install xz-compat
|
|
|
30
30
|
|
|
31
31
|
### Optional Native Acceleration
|
|
32
32
|
|
|
33
|
-
For Node.js
|
|
33
|
+
For Node.js 10+, install `lzma-native` for automatic performance boost:
|
|
34
34
|
|
|
35
35
|
```bash
|
|
36
|
-
npm install
|
|
36
|
+
npm install lzma-native
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
This provides 3-5x faster decompression. The library automatically detects and uses native bindings when available, falling back to pure JavaScript on older Node versions or when
|
|
39
|
+
This provides 3-5x faster decompression. The library automatically detects and uses native bindings when available, falling back to pure JavaScript on older Node versions, when installation fails, or when disabled via `LZMA_NATIVE_DISABLE=1`.
|
|
40
40
|
|
|
41
41
|
## Quick Start
|
|
42
42
|
|
|
@@ -117,7 +117,7 @@ const unfilteredArm = decodeBcjArm(armData);
|
|
|
117
117
|
#### XZ Decompression
|
|
118
118
|
#### `decodeXZ(buffer: Buffer): Buffer`
|
|
119
119
|
Synchronously decompresses XZ format data.
|
|
120
|
-
- **Automatic native acceleration**: Uses
|
|
120
|
+
- **Automatic native acceleration**: Uses lzma-native when available on Node 10+
|
|
121
121
|
- **Self-describing**: Properties embedded in XZ format
|
|
122
122
|
|
|
123
123
|
#### `createXZDecoder(): Transform`
|
|
@@ -128,7 +128,7 @@ Creates a streaming Transform for XZ decompression.
|
|
|
128
128
|
#### `decode7zLzma2(data: Buffer, properties: Buffer, unpackSize?: number): Buffer`
|
|
129
129
|
Decompresses LZMA2 data from a 7z file.
|
|
130
130
|
- Accepts properties separately (matching 7z format)
|
|
131
|
-
- Tries native acceleration via
|
|
131
|
+
- Tries native acceleration via lzma-native automatically
|
|
132
132
|
- Falls back to pure JavaScript if native unavailable
|
|
133
133
|
|
|
134
134
|
#### `decode7zLzma(data: Buffer, properties: Buffer, unpackSize: number): Buffer`
|
|
@@ -293,4 +293,4 @@ npm test
|
|
|
293
293
|
|
|
294
294
|
- [XZ Format Specification](https://tukaani.org/xz/xz-file-format.txt)
|
|
295
295
|
- [LZMA SDK](https://www.7-zip.org/sdk.html)
|
|
296
|
-
- [XZ Utils](https://tukaani.org/xz/)
|
|
296
|
+
- [XZ Utils](https://tukaani.org/xz/)
|
package/dist/cjs/compat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/compat.ts"],"sourcesContent":["/**\n * Compatibility Layer for Node.js 0.8+\n * Local to this package - contains only needed functions.\n */\nimport os from 'os';\n\nexport function tmpdir(): string {\n return typeof os.tmpdir === 'function' ? os.tmpdir() : require('os-shim').tmpdir();\n}\n"],"names":["tmpdir","os","require"],"mappings":"AAAA;;;CAGC;;;;+BAGeA;;;eAAAA;;;yDAFD;;;;;;AAER,SAASA;IACd,OAAO,OAAOC,WAAE,CAACD,MAAM,KAAK,aAAaC,WAAE,CAACD,MAAM,KAAKE,QAAQ,WAAWF,MAAM;AAClF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/xz-compat/src/filters/bcj/Bcj.ts"],"sourcesContent":["// BCJ (x86) filter codec - converts x86 CALL/JMP relative addresses\n// This is a simple filter that makes executables more compressible by LZMA\n//\n// BCJ transforms relative addresses in x86 CALL (0xE8) and JMP (0xE9) instructions\n// to absolute addresses, which creates more repetitive patterns for compression.\n//\n// Reference: https://github.com/tukaani-project/xz/blob/master/src/liblzma/simple/x86.c\n//\n// This implementation uses true streaming - processes data chunk by chunk\n// while buffering incomplete instructions across chunk boundaries.\n\nimport { allocBuffer, bufferFrom, Transform } from 'extract-base-iterator';\n\n// Test if byte is 0x00 or 0xFF (valid MSB for converted addresses)\nfunction Test86MSByte(b: number): boolean {\n return b === 0 || b === 0xff;\n}\n\n// Lookup table for mask to bit number conversion (used in false positive prevention)\nconst MASK_TO_BIT_NUMBER = [0, 1, 2, 2, 3];\n\n/**\n * BCJ x86 filter state\n */\ninterface BcjX86State {\n prevMask: number;\n prevPos: number;\n}\n\n/**\n * Core x86 BCJ conversion function (matches reference x86_code)\n * Works for both encoding and decoding based on isEncoder flag\n *\n * @param state - Filter state (prevMask and prevPos)\n * @param nowPos - Current position in the overall stream\n * @param isEncoder - true for encoding, false for decoding\n * @param buffer - Buffer to process (modified in place)\n * @param size - Size of buffer\n * @returns Number of bytes processed\n */\nfunction x86Code(state: BcjX86State, nowPos: number, isEncoder: boolean, buffer: Buffer, size: number): number {\n let prevMask = state.prevMask;\n let prevPos = state.prevPos;\n\n if (size < 5) {\n return 0;\n }\n\n // Decay prev_pos if too far from current position\n if (nowPos - prevPos > 5) {\n prevPos = nowPos - 5;\n }\n\n const limit = size - 5;\n let bufferPos = 0;\n\n while (bufferPos <= limit) {\n const opcode = buffer[bufferPos];\n\n // Check for CALL (0xE8) or JMP (0xE9) opcode\n if (opcode !== 0xe8 && opcode !== 0xe9) {\n bufferPos++;\n continue;\n }\n\n // Calculate offset from previous position\n const offset = nowPos + bufferPos - prevPos;\n prevPos = nowPos + bufferPos;\n\n // Update mask based on offset\n if (offset > 5) {\n prevMask = 0;\n } else {\n for (let i = 0; i < offset; i++) {\n prevMask &= 0x77;\n prevMask <<= 1;\n }\n }\n\n // Get the high byte of the address\n let b = buffer[bufferPos + 4];\n\n // Check if this looks like a valid address to convert\n if (Test86MSByte(b) && prevMask >> 1 <= 4 && prevMask >> 1 !== 3) {\n // Read 32-bit address (big-endian style: high byte first in src)\n let src = (b << 24) | (buffer[bufferPos + 3] << 16) | (buffer[bufferPos + 2] << 8) | buffer[bufferPos + 1];\n\n // Make src unsigned 32-bit\n src = src >>> 0;\n\n let dest: number;\n\n // Conversion loop with false positive correction\n while (true) {\n if (isEncoder) {\n dest = (src + (nowPos + bufferPos + 5)) >>> 0;\n } else {\n dest = (src - (nowPos + bufferPos + 5)) >>> 0;\n }\n\n if (prevMask === 0) {\n break;\n }\n\n const i = MASK_TO_BIT_NUMBER[prevMask >> 1];\n b = (dest >>> (24 - i * 8)) & 0xff;\n\n if (!Test86MSByte(b)) {\n break;\n }\n\n // XOR correction for false positive prevention\n src = (dest ^ ((1 << (32 - i * 8)) - 1)) >>> 0;\n }\n\n // Write back the converted address\n // High byte: ~(((dest >> 24) & 1) - 1) produces 0x00 or 0xFF\n buffer[bufferPos + 4] = ~(((dest >>> 24) & 1) - 1) & 0xff;\n buffer[bufferPos + 3] = (dest >>> 16) & 0xff;\n buffer[bufferPos + 2] = (dest >>> 8) & 0xff;\n buffer[bufferPos + 1] = dest & 0xff;\n\n bufferPos += 5;\n prevMask = 0;\n } else {\n bufferPos++;\n prevMask |= 1;\n if (Test86MSByte(b)) {\n prevMask |= 0x10;\n }\n }\n }\n\n // Save state\n state.prevMask = prevMask;\n state.prevPos = prevPos;\n\n return bufferPos;\n}\n\n/**\n * Decode BCJ (x86) filtered data (synchronous, for buffered use)\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * @param input - BCJ filtered data\n * @param _properties - Unused for BCJ\n * @param _unpackSize - Unused for BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcj(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n\n const state: BcjX86State = {\n prevMask: 0,\n prevPos: 0xfffffffb, // (uint32_t)(-5) in C\n };\n\n x86Code(state, 0, false, output, output.length);\n\n return output;\n}\n\n/**\n * Create a streaming BCJ decoder Transform.\n * Processes data chunk by chunk, buffering incomplete instructions.\n */\nexport function createBcjDecoder(_properties?: Buffer, _unpackSize?: number): InstanceType<typeof Transform> {\n // State that persists across chunks\n const state: BcjX86State = {\n prevMask: 0,\n prevPos: 0xfffffffb, // (uint32_t)(-5) in C\n };\n let globalPos = 0; // Position in the overall stream\n let pending: Buffer | null = null; // Bytes pending from previous chunk\n\n const transform = new Transform({\n transform: (chunk: Buffer, _encoding: string, callback: (err?: Error | null, data?: Buffer) => void) => {\n // Combine pending bytes with new chunk\n let data: Buffer;\n if (pending && pending.length > 0) {\n data = Buffer.concat([pending, chunk]);\n } else {\n data = chunk;\n }\n\n // We need at least 5 bytes to process an instruction\n if (data.length < 5) {\n pending = data;\n callback(null, allocBuffer(0));\n return;\n }\n\n // Process the buffer\n const output = bufferFrom(data);\n const processed = x86Code(state, globalPos, false, output, output.length);\n\n if (processed === 0) {\n // Not enough data to process\n pending = data;\n callback(null, allocBuffer(0));\n return;\n }\n\n // Output processed bytes, keep unprocessed as pending\n const result = output.slice(0, processed);\n pending = output.slice(processed);\n globalPos += processed;\n\n callback(null, result);\n },\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n // Output any remaining pending bytes\n if (pending && pending.length > 0) {\n // Process final bytes - can't convert incomplete instructions\n this.push(pending);\n }\n callback(null);\n },\n });\n\n return transform;\n}\n"],"names":["createBcjDecoder","decodeBcj","Test86MSByte","b","MASK_TO_BIT_NUMBER","x86Code","state","nowPos","isEncoder","buffer","size","prevMask","prevPos","limit","bufferPos","opcode","offset","i","src","dest","input","_properties","_unpackSize","output","bufferFrom","length","globalPos","pending","transform","Transform","chunk","_encoding","callback","data","Buffer","concat","allocBuffer","processed","result","slice","flush","push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YACAK,SAAS;QACX;IACF;IAEA,OAAOJ;AACT"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/Bcj.ts"],"sourcesContent":["// BCJ (x86) filter codec - converts x86 CALL/JMP relative addresses\n// This is a simple filter that makes executables more compressible by LZMA\n//\n// BCJ transforms relative addresses in x86 CALL (0xE8) and JMP (0xE9) instructions\n// to absolute addresses, which creates more repetitive patterns for compression.\n//\n// Reference: https://github.com/tukaani-project/xz/blob/master/src/liblzma/simple/x86.c\n//\n// This implementation uses true streaming - processes data chunk by chunk\n// while buffering incomplete instructions across chunk boundaries.\n\nimport { allocBuffer, bufferFrom, Transform } from 'extract-base-iterator';\n\n// Test if byte is 0x00 or 0xFF (valid MSB for converted addresses)\nfunction Test86MSByte(b: number): boolean {\n return b === 0 || b === 0xff;\n}\n\n// Lookup table for mask to bit number conversion (used in false positive prevention)\nconst MASK_TO_BIT_NUMBER = [0, 1, 2, 2, 3];\n\n/**\n * BCJ x86 filter state\n */\ninterface BcjX86State {\n prevMask: number;\n prevPos: number;\n}\n\n/**\n * Core x86 BCJ conversion function (matches reference x86_code)\n * Works for both encoding and decoding based on isEncoder flag\n *\n * @param state - Filter state (prevMask and prevPos)\n * @param nowPos - Current position in the overall stream\n * @param isEncoder - true for encoding, false for decoding\n * @param buffer - Buffer to process (modified in place)\n * @param size - Size of buffer\n * @returns Number of bytes processed\n */\nfunction x86Code(state: BcjX86State, nowPos: number, isEncoder: boolean, buffer: Buffer, size: number): number {\n let prevMask = state.prevMask;\n let prevPos = state.prevPos;\n\n if (size < 5) {\n return 0;\n }\n\n // Decay prev_pos if too far from current position\n if (nowPos - prevPos > 5) {\n prevPos = nowPos - 5;\n }\n\n const limit = size - 5;\n let bufferPos = 0;\n\n while (bufferPos <= limit) {\n const opcode = buffer[bufferPos];\n\n // Check for CALL (0xE8) or JMP (0xE9) opcode\n if (opcode !== 0xe8 && opcode !== 0xe9) {\n bufferPos++;\n continue;\n }\n\n // Calculate offset from previous position\n const offset = nowPos + bufferPos - prevPos;\n prevPos = nowPos + bufferPos;\n\n // Update mask based on offset\n if (offset > 5) {\n prevMask = 0;\n } else {\n for (let i = 0; i < offset; i++) {\n prevMask &= 0x77;\n prevMask <<= 1;\n }\n }\n\n // Get the high byte of the address\n let b = buffer[bufferPos + 4];\n\n // Check if this looks like a valid address to convert\n if (Test86MSByte(b) && prevMask >> 1 <= 4 && prevMask >> 1 !== 3) {\n // Read 32-bit address (big-endian style: high byte first in src)\n let src = (b << 24) | (buffer[bufferPos + 3] << 16) | (buffer[bufferPos + 2] << 8) | buffer[bufferPos + 1];\n\n // Make src unsigned 32-bit\n src = src >>> 0;\n\n let dest: number;\n\n // Conversion loop with false positive correction\n while (true) {\n if (isEncoder) {\n dest = (src + (nowPos + bufferPos + 5)) >>> 0;\n } else {\n dest = (src - (nowPos + bufferPos + 5)) >>> 0;\n }\n\n if (prevMask === 0) {\n break;\n }\n\n const i = MASK_TO_BIT_NUMBER[prevMask >> 1];\n b = (dest >>> (24 - i * 8)) & 0xff;\n\n if (!Test86MSByte(b)) {\n break;\n }\n\n // XOR correction for false positive prevention\n src = (dest ^ ((1 << (32 - i * 8)) - 1)) >>> 0;\n }\n\n // Write back the converted address\n // High byte: ~(((dest >> 24) & 1) - 1) produces 0x00 or 0xFF\n buffer[bufferPos + 4] = ~(((dest >>> 24) & 1) - 1) & 0xff;\n buffer[bufferPos + 3] = (dest >>> 16) & 0xff;\n buffer[bufferPos + 2] = (dest >>> 8) & 0xff;\n buffer[bufferPos + 1] = dest & 0xff;\n\n bufferPos += 5;\n prevMask = 0;\n } else {\n bufferPos++;\n prevMask |= 1;\n if (Test86MSByte(b)) {\n prevMask |= 0x10;\n }\n }\n }\n\n // Save state\n state.prevMask = prevMask;\n state.prevPos = prevPos;\n\n return bufferPos;\n}\n\n/**\n * Decode BCJ (x86) filtered data (synchronous, for buffered use)\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * @param input - BCJ filtered data\n * @param _properties - Unused for BCJ\n * @param _unpackSize - Unused for BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcj(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n\n const state: BcjX86State = {\n prevMask: 0,\n prevPos: 0xfffffffb, // (uint32_t)(-5) in C\n };\n\n x86Code(state, 0, false, output, output.length);\n\n return output;\n}\n\n/**\n * Create a streaming BCJ decoder Transform.\n * Processes data chunk by chunk, buffering incomplete instructions.\n */\nexport function createBcjDecoder(_properties?: Buffer, _unpackSize?: number): InstanceType<typeof Transform> {\n // State that persists across chunks\n const state: BcjX86State = {\n prevMask: 0,\n prevPos: 0xfffffffb, // (uint32_t)(-5) in C\n };\n let globalPos = 0; // Position in the overall stream\n let pending: Buffer | null = null; // Bytes pending from previous chunk\n\n const transform = new Transform({\n transform: (chunk: Buffer, _encoding: string, callback: (err?: Error | null, data?: Buffer) => void) => {\n // Combine pending bytes with new chunk\n let data: Buffer;\n if (pending && pending.length > 0) {\n data = Buffer.concat([pending, chunk]);\n } else {\n data = chunk;\n }\n\n // We need at least 5 bytes to process an instruction\n if (data.length < 5) {\n pending = data;\n callback(null, allocBuffer(0));\n return;\n }\n\n // Process the buffer\n const output = bufferFrom(data);\n const processed = x86Code(state, globalPos, false, output, output.length);\n\n if (processed === 0) {\n // Not enough data to process\n pending = data;\n callback(null, allocBuffer(0));\n return;\n }\n\n // Output processed bytes, keep unprocessed as pending\n const result = output.slice(0, processed);\n pending = output.slice(processed);\n globalPos += processed;\n\n callback(null, result);\n },\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n // Output any remaining pending bytes\n if (pending && pending.length > 0) {\n // Process final bytes - can't convert incomplete instructions\n this.push(pending);\n }\n callback(null);\n },\n });\n\n return transform;\n}\n"],"names":["createBcjDecoder","decodeBcj","Test86MSByte","b","MASK_TO_BIT_NUMBER","x86Code","state","nowPos","isEncoder","buffer","size","prevMask","prevPos","limit","bufferPos","opcode","offset","i","src","dest","input","_properties","_unpackSize","output","bufferFrom","length","globalPos","pending","transform","Transform","chunk","_encoding","callback","data","Buffer","concat","allocBuffer","processed","result","slice","flush","push"],"mappings":"AAAA,oEAAoE;AACpE,2EAA2E;AAC3E,EAAE;AACF,mFAAmF;AACnF,iFAAiF;AACjF,EAAE;AACF,wFAAwF;AACxF,EAAE;AACF,0EAA0E;AAC1E,mEAAmE;;;;;;;;;;;;QA6JnDA;eAAAA;;QAjBAC;eAAAA;;;mCA1ImC;AAEnD,mEAAmE;AACnE,SAASC,aAAaC,CAAS;IAC7B,OAAOA,MAAM,KAAKA,MAAM;AAC1B;AAEA,qFAAqF;AACrF,IAAMC,qBAAqB;IAAC;IAAG;IAAG;IAAG;IAAG;CAAE;AAU1C;;;;;;;;;;CAUC,GACD,SAASC,QAAQC,KAAkB,EAAEC,MAAc,EAAEC,SAAkB,EAAEC,MAAc,EAAEC,IAAY;IACnG,IAAIC,WAAWL,MAAMK,QAAQ;IAC7B,IAAIC,UAAUN,MAAMM,OAAO;IAE3B,IAAIF,OAAO,GAAG;QACZ,OAAO;IACT;IAEA,kDAAkD;IAClD,IAAIH,SAASK,UAAU,GAAG;QACxBA,UAAUL,SAAS;IACrB;IAEA,IAAMM,QAAQH,OAAO;IACrB,IAAII,YAAY;IAEhB,MAAOA,aAAaD,MAAO;QACzB,IAAME,SAASN,MAAM,CAACK,UAAU;QAEhC,6CAA6C;QAC7C,IAAIC,WAAW,QAAQA,WAAW,MAAM;YACtCD;YACA;QACF;QAEA,0CAA0C;QAC1C,IAAME,SAAST,SAASO,YAAYF;QACpCA,UAAUL,SAASO;QAEnB,8BAA8B;QAC9B,IAAIE,SAAS,GAAG;YACdL,WAAW;QACb,OAAO;YACL,IAAK,IAAIM,IAAI,GAAGA,IAAID,QAAQC,IAAK;gBAC/BN,YAAY;gBACZA,aAAa;YACf;QACF;QAEA,mCAAmC;QACnC,IAAIR,IAAIM,MAAM,CAACK,YAAY,EAAE;Q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AAsC;YAC3F,qCAAqC;YACrC,IAAIL,WAAWA,QAAQF,MAAM,GAAG,GAAG;gBACjC,8DAA8D;gBAC9D,IAAI,CAACgB,IAAI,CAACd;YACZ;YACAK,SAAS;QACX;IACF;IAEA,OAAOJ;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjArm.ts"],"sourcesContent":["// BCJ (ARM 32-bit) filter codec - converts ARM branch instruction addresses\n// This filter makes ARM executables more compressible by LZMA\n//\n// ARM branch instructions (BL) use relative addressing. The filter converts\n// these to absolute addresses during compression, and back during decompression.\n//\n// Reference: https://github.com/tukaani-project/xz/blob/master/src/liblzma/simple/arm.c\n//\n// This implementation uses true streaming - processes data chunk by chunk.\n\nimport { allocBuffer, bufferFrom, Transform } from 'extract-base-iterator';\n\n/**\n * Core ARM BCJ conversion function (matches reference arm_code)\n * Works for both encoding and decoding based on isEncoder flag\n *\n * ARM BL instruction format:\n * - 4 bytes aligned\n * - Byte pattern: XX XX XX EB (where EB = 0xEB opcode for BL)\n * - Lower 24 bits are signed offset (in words, not bytes)\n * - ARM pipeline adds +8 to the effective address\n *\n * @param nowPos - Current position in the overall stream\n * @param isEncoder - true for encoding, false for decoding\n * @param buffer - Buffer to process (modified in place)\n * @param size - Size of buffer\n * @returns Number of bytes processed\n */\nfunction armCode(nowPos: number, isEncoder: boolean, buffer: Buffer, size: number): number {\n // Only process complete 4-byte groups\n size = size & ~3;\n\n let i = 0;\n for (; i < size; i += 4) {\n // Check for BL instruction: byte 3 is 0xEB\n if (buffer[i + 3] === 0xeb) {\n // Read 24-bit value (little-endian in bytes 0-2)\n let src = (buffer[i + 2] << 16) | (buffer[i + 1] << 8) | buffer[i + 0];\n\n // Left shift by 2 (convert from words to bytes)\n src <<= 2;\n\n // Sign-extend from 26-bit to 32-bit\n if (src & 0x02000000) {\n src |= 0xfc000000;\n }\n src = src | 0; // Make signed 32-bit\n\n let dest: number;\n if (isEncoder) {\n // Encoding: relative to absolute\n // dest = now_pos + i + 8 + src\n dest = nowPos + i + 8 + src;\n } else {\n // Decoding: absolute to relative\n // dest = src - (now_pos + i + 8)\n dest = src - (nowPos + i + 8);\n }\n\n // Right shift by 2 (convert back from bytes to words)\n dest >>>= 2;\n\n // Write back lower 24 bits (little-endian)\n buffer[i + 2] = (dest >>> 16) & 0xff;\n buffer[i + 1] = (dest >>> 8) & 0xff;\n buffer[i + 0] = dest & 0xff;\n }\n }\n\n return i;\n}\n\n/**\n * Decode ARM BCJ filtered data (synchronous, for buffered use)\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * @param input - ARM BCJ filtered data\n * @param _properties - Unused for ARM BCJ\n * @param _unpackSize - Unused for ARM BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n\n armCode(0, false, output, output.length);\n\n return output;\n}\n\n/**\n * Create a streaming ARM BCJ decoder Transform.\n * Processes data in 4-byte aligned chunks.\n */\nexport function createBcjArmDecoder(_properties?: Buffer, _unpackSize?: number): InstanceType<typeof Transform> {\n let globalPos = 0; // Position in the overall stream (in bytes)\n let pending: Buffer | null = null; // Incomplete 4-byte group\n\n const transform = new Transform({\n transform: (chunk: Buffer, _encoding: string, callback: (err?: Error | null, data?: Buffer) => void) => {\n // Combine pending bytes with new chunk\n let data: Buffer;\n if (pending && pending.length > 0) {\n data = Buffer.concat([pending, chunk]);\n } else {\n data = chunk;\n }\n\n // Process only complete 4-byte groups\n const completeBytes = data.length & ~3;\n if (completeBytes === 0) {\n pending = data;\n callback(null, allocBuffer(0));\n return;\n }\n\n const output = bufferFrom(data.slice(0, completeBytes));\n pending = data.length > completeBytes ? data.slice(completeBytes) : null;\n\n armCode(globalPos, false, output, output.length);\n globalPos += completeBytes;\n\n callback(null, output);\n },\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n if (pending && pending.length > 0) {\n this.push(pending);\n }\n callback(null);\n },\n });\n\n return transform;\n}\n"],"names":["createBcjArmDecoder","decodeBcjArm","armCode","nowPos","isEncoder","buffer","size","i","src","dest","input","_properties","_unpackSize","output","bufferFrom","length","globalPos","pending","transform","Transform","chunk","_encoding","callback","data","Buffer","concat","completeBytes","allocBuffer","slice","flush","push"],"mappings":"AAAA,4EAA4E;AAC5E,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,iFAAiF;AACjF,EAAE;AACF,wFAAwF;AACxF,EAAE;AACF,2EAA2E;;;;;;;;;;;;QAqF3DA;eAAAA;;QAZAC;eAAAA;;;mCAvEmC;AAEnD;;;;;;;;;;;;;;;CAeC,GACD,SAASC,QAAQC,MAAc,EAAEC,SAAkB,EAAEC,MAAc,EAAEC,IAAY;IAC/E,sCAAsC;IACtCA,OAAOA,OAAO,CAAC;IAEf,IAAIC,IAAI;IACR,MAAOA,IAAID,MAAMC,KAAK,EAAG;QACvB,2CAA2C;QAC3C,IAAIF,MAAM,CAACE,IAAI,EAAE,KAAK,MAAM;YAC1B,iDAAiD;YACjD,IAAIC,MAAM,AAACH,MAAM,CAACE,IAAI,EAAE,IAAI,KAAOF,MAAM,CAACE,IAAI,EAAE,IAAI,IAAKF,MAAM,CAACE,IAAI,EAAE;YAEtE,gDAAgD;YAChDC,QAAQ;YAER,oCAAoC;YACpC,IAAIA,MAAM,YAAY;gBACpBA,OAAO;YACT;YACAA,MAAMA,MAAM,GAAG,qBAAqB;YAEpC,IAAIC,OAAAA,KAAAA;YACJ,IAAIL,WAAW;gBACb,iCAAiC;gBACjC,+BAA+B;gBAC/BK,OAAON,SAASI,IAAI,IAAIC;YAC1B,OAAO;gBACL,iCAAiC;gBACjC,iCAAiC;gBACjCC,OAAOD,MAAOL,CAAAA,SAASI,IAAI,CAAA;YAC7B;YAEA,sDAAsD;YACtDE,UAAU;YAEV,2CAA2C;YA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AK,SAAS;QACX;IACF;IAEA,OAAOJ;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjArm64.ts"],"sourcesContent":["// BCJ (ARM64/AArch64) filter codec - converts ARM64 branch instruction addresses\n// This filter makes ARM64 executables more compressible by LZMA\n//\n// ARM64 uses 32-bit fixed-width instructions. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'stream';\nimport createBufferingDecoder from '../../utils/createBufferingDecoder.ts';\n\n/**\n * Decode ARM64 BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM64 B/BL instruction format (little-endian):\n * - 4 bytes aligned\n * - B: opcode 0x14 (000101xx)\n * - BL: opcode 0x94 (100101xx)\n * - Bits 0-25 are 26-bit signed offset (in words)\n *\n * @param input - ARM64 BCJ filtered data\n * @param _properties - Unused for ARM64 BCJ\n * @param _unpackSize - Unused for ARM64 BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm64(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read 32-bit value (little-endian)\n let instr = output[pos] | (output[pos + 1] << 8) | (output[pos + 2] << 16) | ((output[pos + 3] << 24) >>> 0);\n\n // Check for B/BL instruction: (instr & 0x7C000000) === 0x14000000\n // This matches both B (0x14000000) and BL (0x94000000)\n if ((instr & 0x7c000000) === 0x14000000) {\n // Extract 26-bit offset\n let addr = instr & 0x03ffffff;\n\n // Sign-extend 26-bit to 32-bit\n if (addr & 0x02000000) {\n addr |= 0xfc000000;\n }\n\n // Convert absolute to relative: subtract current position (in words)\n const relAddr = addr - (pos >>> 2);\n\n // Clear old offset and write new one, preserve opcode\n instr = (instr & 0xfc000000) | (relAddr & 0x03ffffff);\n\n // Write back (little-endian)\n output[pos] = instr & 0xff;\n output[pos + 1] = (instr >>> 8) & 0xff;\n output[pos + 2] = (instr >>> 16) & 0xff;\n output[pos + 3] = (instr >>> 24) & 0xff;\n }\n\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create an ARM64 BCJ decoder Transform stream\n */\nexport function createBcjArm64Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjArm64, properties, unpackSize);\n}\n"],"names":["createBcjArm64Decoder","decodeBcjArm64","input","_properties","_unpackSize","output","bufferFrom","pos","length","instr","addr","relAddr","properties","unpackSize","createBufferingDecoder"],"mappings":"AAAA,iFAAiF;AACjF,gEAAgE;AAChE,EAAE;AACF,6FAA6F;AAC7F,EAAE;AACF,+DAA+D;;;;;;;;;;;;QA+D/CA;eAAAA;;QA1CAC;eAAAA;;;mCAnBW;+EAEQ;;;;;;AAiB5B,SAASA,eAAeC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACtF,IAAMC,SAASC,IAAAA,+BAAU,EAACJ,QAAQ,gCAAgC;IAClE,IAAIK,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKF,OAAOG,MAAM,CAAE;QAC/B,oCAAoC;QACpC,IAAIC,QAAQJ,MAAM,CAACE,IAAI,GAAIF,MAAM,CAACE,MAAM,EAAE,IAAI,IAAMF,MAAM,CAACE,MAAM,EAAE,IAAI,KAAO,AAACF,MAAM,CAACE,MAAM,EAAE,IAAI,OAAQ;QAE1G,kEAAkE;QAClE,uDAAuD;QACvD,IAAI,AAACE,CAAAA,QAAQ,UAAS,MAAO,YAAY;YACvC,wBAAwB;YACxB,IAAIC,OAAOD,QAAQ;YAEnB,+BAA+B;YAC/B,IAAIC,OAAO,YAAY;gBACrBA,QAAQ;YACV;YAEA,qEAAqE;YACrE,IAAMC,UAAUD,OAAQH,CAAAA,QAAQ,CAAA;YAEhC,sDAAsD;YACtDE,QAAQ,AAACA,QAAQ,aAAeE,UAAU;YAE1C,6BAA6B;YAC7BN,MAAM,CAACE,IAAI,GAAGE,QAAQ;YACtBJ,MAAM,CAACE,MAAM,EAAE,GAAG,AAACE,UAAU,IAAK;YAClCJ,MAAM,CAACE,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;YACnCJ,MAAM,CAACE,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;QACrC;QAEAF,OAAO;IACT;IAEA,OAAOF;AACT;AAKO,SAASL,sBAAsBY,UAAmB,EAAEC,UAAmB;IAC5E,OAAOC,IAAAA,iCAAsB,EAACb,gBAAgBW,YAAYC;AAC5D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjArmt.ts"],"sourcesContent":["// BCJ (ARM Thumb) filter codec - converts ARM Thumb branch instruction addresses\n// This filter makes ARM Thumb executables more compressible by LZMA\n//\n// ARM Thumb uses 16-bit instructions, but BL (branch with link) spans two 16-bit words.\n// The filter converts relative addresses to absolute during compression.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'stream';\nimport createBufferingDecoder from '../../utils/createBufferingDecoder.ts';\n\n/**\n * Decode ARM Thumb BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM Thumb BL instruction format (2 x 16-bit):\n * - First half-word: 1111 0xxx xxxx xxxx (high bits of offset)\n * - Second half-word: 1111 1xxx xxxx xxxx (low bits of offset)\n *\n * @param input - ARM Thumb BCJ filtered data\n * @param _properties - Unused for ARM Thumb BCJ\n * @param _unpackSize - Unused for ARM Thumb BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArmt(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 2-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read two 16-bit values (little-endian)\n const w0 = output[pos] | (output[pos + 1] << 8);\n const w1 = output[pos + 2] | (output[pos + 3] << 8);\n\n // Check for BL instruction pair:\n // First word: 0xF000-0xF7FF (1111 0xxx xxxx xxxx)\n // Second word: 0xF800-0xFFFF (1111 1xxx xxxx xxxx)\n if ((w0 & 0xf800) === 0xf000 && (w1 & 0xf800) === 0xf800) {\n // Extract and combine the offset parts\n // High 11 bits from w0, low 11 bits from w1\n const hi = w0 & 0x7ff;\n const lo = w1 & 0x7ff;\n\n // Combine into 22-bit offset (in half-words)\n let addr = (hi << 11) | lo;\n\n // Sign-extend 22-bit to 32-bit\n if (addr & 0x200000) {\n addr |= 0xffc00000;\n }\n\n // Convert absolute to relative:\n // Subtract current position (in half-words, so divide by 2)\n // Thumb PC is 2 half-words (4 bytes) ahead\n const relAddr = addr - (pos >>> 1);\n\n // Write back\n const newHi = (relAddr >>> 11) & 0x7ff;\n const newLo = relAddr & 0x7ff;\n\n output[pos] = newHi & 0xff;\n output[pos + 1] = 0xf0 | ((newHi >>> 8) & 0x07);\n output[pos + 2] = newLo & 0xff;\n output[pos + 3] = 0xf8 | ((newLo >>> 8) & 0x07);\n\n pos += 4;\n } else {\n pos += 2;\n }\n }\n\n return output;\n}\n\n/**\n * Create an ARM Thumb BCJ decoder Transform stream\n */\nexport function createBcjArmtDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjArmt, properties, unpackSize);\n}\n"],"names":["createBcjArmtDecoder","decodeBcjArmt","input","_properties","_unpackSize","output","bufferFrom","pos","length","w0","w1","hi","lo","addr","relAddr","newHi","newLo","properties","unpackSize","createBufferingDecoder"],"mappings":"AAAA,iFAAiF;AACjF,oEAAoE;AACpE,EAAE;AACF,wFAAwF;AACxF,yEAAyE;AACzE,EAAE;AACF,+DAA+D;;;;;;;;;;;;QAwE/CA;eA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,UAAmB;IAC3E,OAAOC,IAAAA,iCAAsB,EAAClB,eAAegB,YAAYC;AAC3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjIa64.ts"],"sourcesContent":["// BCJ (IA64/Itanium) filter codec - converts IA64 branch instruction addresses\n// This filter makes IA64 executables more compressible by LZMA\n//\n// IA64 uses 128-bit instruction bundles with 3 instructions per bundle.\n// Branch instructions use 21-bit signed offsets (in bundles).\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'stream';\nimport createBufferingDecoder from '../../utils/createBufferingDecoder.ts';\n\n// IA64 branch instruction slot mask\n// Each bundle has a 5-bit template and 3 x 41-bit instruction slots\nconst kBranchTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0, 7, 7, 4, 4, 0, 0, 4, 4, 0, 0];\n\n/**\n * Decode IA64 BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * @param input - IA64 BCJ filtered data\n * @param _properties - Unused for IA64 BCJ\n * @param _unpackSize - Unused for IA64 BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjIa64(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 16-byte aligned bundles\n while (pos + 16 <= output.length) {\n // Get template (low 5 bits of first byte)\n const template = output[pos] & 0x1f;\n const mask = kBranchTable[template];\n\n // Check each instruction slot (3 slots per bundle)\n for (let slot = 0; slot < 3; slot++) {\n if ((mask & (1 << slot)) === 0) {\n continue;\n }\n\n // Calculate bit position for this slot\n // Slot 0: bits 5-45, Slot 1: bits 46-86, Slot 2: bits 87-127\n const bitPos = 5 + slot * 41;\n const bytePos = bitPos >>> 3;\n const bitOffset = bitPos & 7;\n\n // Read 8 bytes to get the instruction (may span bytes)\n // We need at least 6 bytes for a 41-bit instruction\n if (pos + bytePos + 6 > output.length) {\n break;\n }\n\n // Extract instruction bytes\n const instr0 = output[pos + bytePos];\n const instr1 = output[pos + bytePos + 1];\n const instr2 = output[pos + bytePos + 2];\n const instr3 = output[pos + bytePos + 3];\n const instr4 = output[pos + bytePos + 4];\n const instr5 = output[pos + bytePos + 5];\n\n // Build instruction value (we only need the immediate field)\n // The immediate is in bits 13-32 of the instruction (20 bits)\n // Plus bit 36 as the sign bit\n\n // For decoding, we extract the address that was encoded and convert back\n let instrLo = (instr0 >>> bitOffset) | (instr1 << (8 - bitOffset)) | (instr2 << (16 - bitOffset)) | (instr3 << (24 - bitOffset));\n\n let instrHi = (instr4 >>> bitOffset) | (instr5 << (8 - bitOffset));\n\n // Check opcode for branch (opcode 4 or 5 in bits 37-40)\n const opcode = (instrHi >>> (37 - 32 - bitOffset)) & 0xf;\n if (opcode !== 4 && opcode !== 5) {\n continue;\n }\n\n // Extract 21-bit immediate (bits 13-32 + sign bit 36)\n const imm20 = (instrLo >>> 13) & 0xfffff;\n const sign = (instrHi >>> (36 - 32)) & 1;\n\n // Combine into 21-bit signed value\n let addr = imm20 | (sign << 20);\n if (sign) {\n addr |= 0xffe00000; // Sign-extend\n }\n\n // Convert absolute to relative: subtract current position (in bundles)\n const relAddr = addr - (pos >>> 4);\n\n // Write back\n const newImm20 = relAddr & 0xfffff;\n const newSign = (relAddr >>> 20) & 1;\n\n // Clear old immediate and write new one\n instrLo = (instrLo & ~(0xfffff << 13)) | (newImm20 << 13);\n instrHi = (instrHi & ~(1 << (36 - 32))) | (newSign << (36 - 32));\n\n // Write back bytes\n output[pos + bytePos] = (output[pos + bytePos] & ((1 << bitOffset) - 1)) | ((instrLo & 0xff) << bitOffset);\n output[pos + bytePos + 1] = (instrLo >>> (8 - bitOffset)) & 0xff;\n output[pos + bytePos + 2] = (instrLo >>> (16 - bitOffset)) & 0xff;\n output[pos + bytePos + 3] = (instrLo >>> (24 - bitOffset)) & 0xff;\n output[pos + bytePos + 4] = ((instrLo >>> (32 - bitOffset)) & ((1 << bitOffset) - 1)) | ((instrHi & 0xff) << bitOffset);\n output[pos + bytePos + 5] = (output[pos + bytePos + 5] & ~((1 << bitOffset) - 1)) | ((instrHi >>> (8 - bitOffset)) & ((1 << bitOffset) - 1));\n }\n\n pos += 16;\n }\n\n return output;\n}\n\n/**\n * Create an IA64 BCJ decoder Transform stream\n */\nexport function createBcjIa64Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjIa64, properties, unpackSize);\n}\n"],"names":["createBcjIa64Decoder","decodeBcjIa64","kBranchTable","input","_properties","_unpackSize","output","bufferFrom","pos","length","template","mask","slot","bitPos","bytePos","bitOffset","instr0","instr1","instr2","instr3","instr4","instr5","instrLo","instrHi","opcode","imm20","sign","addr","relAddr","newImm20","newSign","properties","unpackSize","createBufferingDecoder"],"mappings":"AAAA,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,wEAAwE;AACxE,8DAA8D;AAC9D,EAAE;AACF,+DAA+D;;;;;;;;;;;;QA6G/CA;eAAAA;;QA1FAC;eAAAA;;;mCAjBW;+EAEQ;;;;;;AAEnC,oCAAoC;AACpC,oEAAoE;AACpE,IAAMC,eAAe;IAAC;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;CAAE;AAW9G,SAASD,cAAcE,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACrF,IAAMC,SAASC,IAAAA,+BAAU,EAACJ,QAAQ,gCAAgC;IAClE,IAAIK,MAAM;IAEV,kCAAkC;IAClC,MAAOA,MAAM,MAAMF,OAAOG,MAAM,C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AC3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjPpc.ts"],"sourcesContent":["// BCJ (PowerPC) filter codec - converts PowerPC branch instruction addresses\n// This filter makes PowerPC executables more compressible by LZMA\n//\n// PowerPC is big-endian. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'stream';\nimport createBufferingDecoder from '../../utils/createBufferingDecoder.ts';\n\n/**\n * Decode PowerPC BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * PowerPC B/BL instruction format (big-endian):\n * - 4 bytes aligned\n * - Opcode 0x48 in high byte with AA=0, LK=1 (0x48000001 mask 0xFC000003)\n * - Bits 6-29 are 24-bit signed offset (in words)\n *\n * @param input - PowerPC BCJ filtered data\n * @param _properties - Unused for PowerPC BCJ\n * @param _unpackSize - Unused for PowerPC BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjPpc(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read 32-bit value (big-endian)\n let instr = (output[pos] << 24) | (output[pos + 1] << 16) | (output[pos + 2] << 8) | output[pos + 3];\n\n // Check for B/BL instruction: (instr & 0xFC000003) === 0x48000001\n if ((instr & 0xfc000003) === 0x48000001) {\n // Extract 26-bit offset (bits 2-27, the LI field)\n let addr = instr & 0x03fffffc;\n\n // Sign-extend 26-bit to 32-bit\n if (addr & 0x02000000) {\n addr |= 0xfc000000;\n }\n\n // Convert absolute to relative: subtract current position\n const relAddr = addr - pos;\n\n // Clear old offset and write new one\n instr = (instr & 0xfc000003) | (relAddr & 0x03fffffc);\n\n // Write back (big-endian)\n output[pos] = (instr >>> 24) & 0xff;\n output[pos + 1] = (instr >>> 16) & 0xff;\n output[pos + 2] = (instr >>> 8) & 0xff;\n output[pos + 3] = instr & 0xff;\n }\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create a PowerPC BCJ decoder Transform stream\n */\nexport function createBcjPpcDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjPpc, properties, unpackSize);\n}\n"],"names":["createBcjPpcDecoder","decodeBcjPpc","input","_properties","_unpackSize","output","bufferFrom","pos","length","instr","addr","relAddr","properties","unpackSize","createBufferingDecoder"],"mappings":"AAAA,6EAA6E;AAC7E,kEAAkE;AAClE,EAAE;AACF,wEAAwE;AACxE,EAAE;AACF,+DAA+D;;;;;;;;;;;;QA4D/CA;eAAAA;;QAxCAC;eAAAA;;;mCAlBW;+EAEQ;;;;;;AAgB5B,SAASA,aAAaC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACpF,IAAMC,SAASC,IAAAA,+BAAU,EAACJ,QAAQ,gCAAgC;IAClE,IAAIK,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKF,OAAOG,MAAM,CAAE;QAC/B,iCAAiC;QACjC,IAAIC,QAAQ,AAACJ,MAAM,CAACE,IAAI,IAAI,KAAOF,MAAM,CAACE,MAAM,EAAE,IAAI,KAAOF,MAAM,CAACE,MAAM,EAAE,IAAI,IAAKF,MAAM,CAACE,MAAM,EAAE;QAEpG,kEAAkE;QAClE,IAAI,AAACE,CAAAA,QAAQ,UAAS,MAAO,YAAY;YACvC,kDAAkD;YAClD,IAAIC,OAAOD,QAAQ;YAEnB,+BAA+B;YAC/B,IAAIC,OAAO,YAAY;gBACrBA,QAAQ;YACV;YAEA,0DAA0D;YAC1D,IAAMC,UAAUD,OAAOH;YAEvB,qCAAqC;YACrCE,QAAQ,AAACA,QAAQ,aAAeE,UAAU;YAE1C,0BAA0B;YAC1BN,MAAM,CAACE,IAAI,GAAG,AAACE,UAAU,KAAM;YAC/BJ,MAAM,CAACE,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;YACnCJ,MAAM,CAACE,MAAM,EAAE,GAAG,AAACE,UAAU,IAAK;YAClCJ,MAAM,CAACE,MAAM,EAAE,GAAGE,QAAQ;QAC5B;QACAF,OAAO;IACT;IAEA,OAAOF;AACT;AAKO,SAASL,oBAAoBY,UAAmB,EAAEC,UAAmB;IAC1E,OAAOC,IAAAA,iCAAsB,EAACb,cAAcW,YAAYC;AAC1D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/bcj/BcjSparc.ts"],"sourcesContent":["// BCJ (SPARC) filter codec - converts SPARC branch instruction addresses\n// This filter makes SPARC executables more compressible by LZMA\n//\n// SPARC is big-endian. CALL instructions use 30-bit signed offsets.\n// The filter only transforms CALL instructions with specific byte patterns.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'stream';\nimport createBufferingDecoder from '../../utils/createBufferingDecoder.ts';\n\n/**\n * Decode SPARC BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * SPARC CALL instruction matching (big-endian):\n * - First byte 0x40 and (second byte & 0xC0) == 0x00, OR\n * - First byte 0x7F and (second byte & 0xC0) == 0xC0\n *\n * @param input - SPARC BCJ filtered data\n * @param _properties - Unused for SPARC BCJ\n * @param _unpackSize - Unused for SPARC BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjSparc(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n const b0 = output[pos];\n const b1 = output[pos + 1];\n\n // Check for CALL instruction with specific byte patterns:\n // (b0 == 0x40 && (b1 & 0xC0) == 0x00) || (b0 == 0x7F && (b1 & 0xC0) == 0xC0)\n if ((b0 === 0x40 && (b1 & 0xc0) === 0x00) || (b0 === 0x7f && (b1 & 0xc0) === 0xc0)) {\n // Read 32-bit value (big-endian)\n let src = (b0 << 24) | (b1 << 16) | (output[pos + 2] << 8) | output[pos + 3];\n\n // Shift left by 2 (multiply by 4 for word addressing)\n src <<= 2;\n\n // Decoding: subtract position\n let dest = src - pos;\n\n // Shift right by 2\n dest >>>= 2;\n\n // Reconstruct with sign extension and opcode\n // (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000\n const signBit = (dest >>> 22) & 1;\n const signExtend = signBit ? 0x3fc00000 : 0;\n dest = signExtend | (dest & 0x3fffff) | 0x40000000;\n\n // Write back (big-endian)\n output[pos] = (dest >>> 24) & 0xff;\n output[pos + 1] = (dest >>> 16) & 0xff;\n output[pos + 2] = (dest >>> 8) & 0xff;\n output[pos + 3] = dest & 0xff;\n }\n\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create a SPARC BCJ decoder Transform stream\n */\nexport function createBcjSparcDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjSparc, properties, unpackSize);\n}\n"],"names":["createBcjSparcDecoder","decodeBcjSparc","input","_properties","_unpackSize","output","bufferFrom","pos","length","b0","b1","src","dest","signBit","signExtend","properties","unpackSize","createBufferingDecoder"],"mappings":"AAAA,yEAAyE;AACzE,gEAAgE;AAChE,EAAE;AACF,oEAAoE;AACpE,4EAA4E;AAC5E,EAAE;AACF,+DAA+D;;;;;;;;;;;;QAiE/CA;eAAAA;;QA9CAC;eAAAA;;;mCAjBW;+EAEQ;;;;;;AAe5B,SAASA,eAAeC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACtF,IAAMC,SAASC,IAAAA,+BAAU,EAACJ,QAAQ,gCAAgC;IAClE,IAAIK,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKF,OAAOG,MAAM,CAAE;QAC/B,IAAMC,KAAKJ,MAAM,CAACE,IAAI;QACtB,IAAMG,KAAKL,MAAM,CAACE,MAAM,EAAE;QAE1B,0DAA0D;QAC1D,6EAA6E;QAC7E,IAAI,AAACE,OAAO,QAAQ,AAACC,CAAAA,KAAK,IAAG,MAAO,QAAUD,OAAO,QAAQ,AAACC,CAAAA,KAAK,IAAG,MAAO,MAAO;YAClF,iCAAiC;YACjC,IAAIC,MAAM,AAACF,MAAM,KAAOC,MAAM,KAAOL,MAAM,CAACE,MAAM,EAAE,IAAI,IAAKF,MAAM,CAACE,MAAM,EAAE;YAE5E,sDAAsD;YACtDI,QAAQ;YAER,8BAA8B;YAC9B,IAAIC,OAAOD,MAAMJ;YAEjB,mBAAmB;YACnBK,UAAU;YAEV,6CAA6C;YAC7C,mFAAmF;YACnF,IAAMC,UAAU,AAACD,SAAS,KAAM;YAChC,IAAME,aAAaD,UAAU,aAAa;YAC1CD,OAAOE,aAAcF,OAAO,WAAY;YAExC,0BAA0B;YAC1BP,MAAM,CAACE,IAAI,GAAG,AAACK,SAAS,KAAM;YAC9BP,MAAM,CAACE,MAAM,EAAE,GAAG,AAACK,SAAS,KAAM;YAClCP,MAAM,CAACE,MAAM,EAAE,GAAG,AAACK,SAAS,IAAK;YACjCP,MAAM,CAACE,MAAM,EAAE,GAAGK,OAAO;QAC3B;QAEAL,OAAO;IACT;IAEA,OAAOF;AACT;AAKO,SAASL,sBAAsBe,UAAmB,EAAEC,UAAmB;IAC5E,OAAOC,IAAAA,iCAAsB,EAAChB,gBAAgBc,YAAYC;AAC5D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/delta/Delta.ts"],"sourcesContent":["// Delta filter codec - stores differences between consecutive bytes\n// Useful for data with gradual changes (images, audio, sensor data)\n//\n// The Delta filter stores the difference between each byte and the byte\n// N positions before it, where N is the \"distance\" parameter (default 1).\n// This makes data with regular patterns more compressible.\n//\n// This implementation uses true streaming - processes data chunk by chunk\n// while maintaining state between chunks.\n\nimport { allocBuffer, bufferFrom, Transform } from 'extract-base-iterator';\n\n/**\n * Decode Delta filtered data (synchronous, for buffered use)\n * Reverses the delta transformation by adding previous values\n *\n * @param input - Delta filtered data\n * @param properties - Optional 1-byte properties (distance - 1)\n * @param _unpackSize - Unused for Delta\n * @returns Unfiltered data\n */\nexport function decodeDelta(input: Buffer, properties?: Buffer, _unpackSize?: number): Buffer {\n // Distance parameter: default is 1\n let distance = 1;\n if (properties && properties.length >= 1) {\n // Properties byte contains (distance - 1)\n distance = properties[0] + 1;\n }\n\n const output = bufferFrom(input); // Copy since we modify in place\n\n // State buffer for multi-byte distance\n const state: number[] = [];\n for (let i = 0; i < distance; i++) {\n state.push(0);\n }\n\n for (let j = 0; j < output.length; j++) {\n const idx = j % distance;\n state[idx] = (state[idx] + output[j]) & 0xff;\n output[j] = state[idx];\n }\n\n return output;\n}\n\n/**\n * Create a streaming Delta decoder Transform.\n * Processes data chunk by chunk, maintaining state between chunks.\n */\nexport function createDeltaDecoder(properties?: Buffer, _unpackSize?: number): InstanceType<typeof Transform> {\n // Distance parameter: default is 1\n let distance = 1;\n if (properties && properties.length >= 1) {\n distance = properties[0] + 1;\n }\n\n // State buffer for multi-byte distance\n const state: number[] = [];\n for (let i = 0; i < distance; i++) {\n state.push(0);\n }\n\n let byteIndex = 0;\n\n return new Transform({\n transform: (chunk: Buffer, _encoding: string, callback: (err?: Error | null, data?: Buffer) => void) => {\n const output = allocBuffer(chunk.length);\n\n for (let j = 0; j < chunk.length; j++) {\n const idx = byteIndex % distance;\n state[idx] = (state[idx] + chunk[j]) & 0xff;\n output[j] = state[idx];\n byteIndex++;\n }\n\n callback(null, output);\n },\n });\n}\n"],"names":["createDeltaDecoder","decodeDelta","input","properties","_unpackSize","distance","length","output","bufferFrom","state","i","push","j","idx","byteIndex","Transform","transform","chunk","_encoding","callback","allocB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AAAA,gCAAW,EAACH,MAAMX,MAAM;YAEvC,IAAK,IAAIM,IAAI,GAAGA,IAAIK,MAAMX,MAAM,EAAEM,IAAK;gBACrC,IAAMC,MAAMC,YAAYT;gBACxBI,KAAK,CAACI,IAAI,GAAG,AAACJ,KAAK,CAACI,IAAI,GAAGI,KAAK,CAACL,EAAE,GAAI;gBACvCL,MAAM,CAACK,EAAE,GAAGH,KAAK,CAACI,IAAI;gBACtBC;YACF;YAEAK,SAAS,MAAMZ;QACjB;IACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/filters/index.ts"],"sourcesContent":["// Filter implementations for XZ/LZMA\n\nexport * from './bcj/Bcj.ts';\nexport * from './bcj/BcjArm.ts';\nexport * from './bcj/BcjArm64.ts';\nexport * from './bcj/BcjArmt.ts';\nexport * from './bcj/BcjIa64.ts';\nexport * from './bcj/BcjPpc.ts';\nexport * from './bcj/BcjSparc.ts';\nexport * from './delta/Delta.ts';\n"],"names":[],"mappings":"AAAA,qCAAqC;;;;;qBAEvB;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* XZ-Compat: XZ/LZMA Decompression Library
|
|
3
3
|
*
|
|
4
4
|
* Pure JavaScript implementation with optional native acceleration
|
|
5
|
-
* via
|
|
5
|
+
* via lzma-native on Node.js 10+.
|
|
6
6
|
*
|
|
7
7
|
* Works on Node.js 0.8+ with automatic performance optimization
|
|
8
8
|
* when native bindings are available.
|
|
9
9
|
*/
|
|
10
|
-
export { decode7zLzma, decode7zLzma2 } from './sevenz.js';
|
|
11
|
-
export { createXZDecoder, decodeXZ } from './xz/Decoder.js';
|
|
10
|
+
export { decode7zLzma, decode7zLzma2, type SevenZDecodeCallback } from './sevenz.js';
|
|
11
|
+
export { createXZDecoder, decodeXZ, type XzDecodeCallback } from './xz/Decoder.js';
|
|
12
12
|
export { createLzma2Decoder, createLzmaDecoder, decodeLzma, decodeLzma2 } from './lzma/index.js';
|
|
13
13
|
export * from './filters/index.js';
|
|
14
14
|
export { isNativeAvailable } from './native.js';
|
|
15
|
+
export type { DecodeCallback } from './utils/runDecode.js';
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* XZ-Compat: XZ/LZMA Decompression Library
|
|
3
3
|
*
|
|
4
4
|
* Pure JavaScript implementation with optional native acceleration
|
|
5
|
-
* via
|
|
5
|
+
* via lzma-native on Node.js 10+.
|
|
6
6
|
*
|
|
7
7
|
* Works on Node.js 0.8+ with automatic performance optimization
|
|
8
8
|
* when native bindings are available.
|
|
9
9
|
*/
|
|
10
|
-
export { decode7zLzma, decode7zLzma2 } from './sevenz.js';
|
|
11
|
-
export { createXZDecoder, decodeXZ } from './xz/Decoder.js';
|
|
10
|
+
export { decode7zLzma, decode7zLzma2, type SevenZDecodeCallback } from './sevenz.js';
|
|
11
|
+
export { createXZDecoder, decodeXZ, type XzDecodeCallback } from './xz/Decoder.js';
|
|
12
12
|
export { createLzma2Decoder, createLzmaDecoder, decodeLzma, decodeLzma2 } from './lzma/index.js';
|
|
13
13
|
export * from './filters/index.js';
|
|
14
14
|
export { isNativeAvailable } from './native.js';
|
|
15
|
+
export type { DecodeCallback } from './utils/runDecode.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* XZ-Compat: XZ/LZMA Decompression Library
|
|
3
3
|
*
|
|
4
4
|
* Pure JavaScript implementation with optional native acceleration
|
|
5
|
-
* via
|
|
5
|
+
* via lzma-native on Node.js 10+.
|
|
6
6
|
*
|
|
7
7
|
* Works on Node.js 0.8+ with automatic performance optimization
|
|
8
8
|
* when native bindings are available.
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/index.ts"],"sourcesContent":["/**\n * XZ-Compat: XZ/LZMA Decompression Library\n *\n * Pure JavaScript implementation with optional native acceleration\n * via lzma-native on Node.js 10+.\n *\n * Works on Node.js 0.8+ with automatic performance optimization\n * when native bindings are available.\n */\n\n// ============================================================================\n// High-Level APIs (Recommended)\n// ============================================================================\n\n// 7z-specific decoders - accept properties separately, try native automatically\nexport { decode7zLzma, decode7zLzma2, type SevenZDecodeCallback } from './sevenz.ts';\n// XZ container format - self-describing, works great with native acceleration\nexport { createXZDecoder, decodeXZ, type XzDecodeCallback } from './xz/Decoder.ts';\n\n// ============================================================================\n// Low-Level APIs (Backward Compatibility)\n// ============================================================================\n\n// Raw LZMA decoders (for specialized use cases)\nexport { createLzma2Decoder, createLzmaDecoder, decodeLzma, decodeLzma2 } from './lzma/index.ts';\n\n// ============================================================================\n// Supporting APIs\n// ============================================================================\n\n// Preprocessing filters (BCJ/Delta - used by 7z-iterator)\nexport * from './filters/index.ts';\n\n// Native acceleration utilities\nexport { isNativeAvailable } from './native.ts';\n\n// Callback type used by async decoders\nexport type { DecodeCallback } from './utils/runDecode.ts';\n"],"names":["createLzma2Decoder","createLzmaDecoder","createXZDecoder","decode7zLzma","decode7zLzma2","decodeLzma","decodeLzma2","decodeXZ","isNativeAvailable"],"mappings":"AAAA;;;;;;;;CAQC,GAED,+EAA+E;AAC/E,gCAAgC;AAChC,+EAA+E;AAE/E,gFAAgF;;;;;;;;;;;;QAUvEA;eAAAA,2BAAkB;;QAAEC;eAAAA,0BAAiB;;QAPrCC;eAAAA,0BAAe;;QAFfC;eAAAA,sBAAY;;QAAEC;eAAAA,uBAAa;;QASYC;eAAAA,mBAAU;;QAAEC;eAAAA,oBAAW;;QAP7CC;eAAAA,mBAAQ;;QAiBzBC;eAAAA,2BAAiB;;;wBAnB6C;yBAEN;uBAOc;qBAOjE;wBAGoB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/lzma/index.ts"],"sourcesContent":["/**\n * LZMA Decoder Module\n *\n * Provides both synchronous and streaming LZMA1/LZMA2 decoders.\n *\n * Synchronous API: Use when input is a complete Buffer\n * Streaming API: Use with Transform streams for memory-efficient decompression\n *\n * LZMA1 vs LZMA2:\n * - LZMA2 is chunked and supports true streaming with bounded memory\n * - LZMA1 has no chunk boundaries and requires buffering all input for streaming\n */\n\n// Streaming decoders (Transform streams)\nexport { createLzma2Decoder, createLzmaDecoder } from './stream/transforms.ts';\nexport { decodeLzma2 } from './sync/Lzma2Decoder.ts';\n// Synchronous decoders (for Buffer input)\nexport { decodeLzma } from './sync/LzmaDecoder.ts';\n"],"names":["createLzma2Decoder","createLzmaDecoder","decodeLzma","decodeLzma2"],"mappings":"AAAA;;;;;;;;;;;CAWC,GAED,yCAAyC;;;;;;;;;;;;QAChCA;eAAAA,gCAAkB;;QAAEC;eAAAA,+BAAiB;;QAGrCC;eAAAA,yBAAU;;QAFVC;eAAAA,2BAAW;;;4BADkC;8BAC1B;6BAED"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/iterators/xz-compat/src/lzma/lib/Lzma2ChunkParser.ts"],"sourcesContent":["/**\n * LZMA2 Chunk Parser\n *\n * Shared parsing logic for LZMA2 chunk headers.\n * Used by both synchronous and streaming decoders.\n *\n * LZMA2 control byte ranges:\n * 0x00 = End of stream\n * 0x01 = Uncompressed chunk, dictionary reset\n * 0x02 = Uncompressed chunk, no dictionary reset\n * 0x80-0x9F = LZMA chunk, no reset (solid mode)\n * 0xA0-0xBF = LZMA chunk, reset state (probabilities)\n * 0xC0-0xDF = LZMA chunk, reset state + new properties\n * 0xE0-0xFF = LZMA chunk, reset dictionary + state + new properties\n */\n\n/**\n * LZMA properties extracted from chunk header\n */\nexport interface LzmaChunkProps {\n lc: number;\n lp: number;\n pb: number;\n}\n\n/**\n * Parsed LZMA2 chunk information\n */\nexport interface Lzma2Chunk {\n /** Chunk type */\n type: 'end' | 'uncompressed' | 'lzma';\n /** Total bytes consumed by header (including control byte) */\n headerSize: number;\n /** Whether to reset dictionary */\n dictReset: boolean;\n /** Whether to reset state/probabilities */\n stateReset: boolean;\n /** New LZMA properties (only for control >= 0xC0) */\n newProps: LzmaChunkProps | null;\n /** Uncompressed data size */\n uncompSize: number;\n /** Compressed data size (0 for uncompressed chunks) */\n compSize: number;\n}\n\n/**\n * Result of parsing attempt\n */\nexport type ParseResult = { success: true; chunk: Lzma2Chunk } | { success: false; needBytes: number };\n\n/**\n * Parse an LZMA2 chunk header\n *\n * @param input - Input buffer\n * @param offset - Offset to start parsing\n * @returns Parsed chunk info or number of bytes needed\n */\nexport function parseLzma2ChunkHeader(input: Buffer, offset: number): ParseResult {\n if (offset >= input.length) {\n return { success: false, needBytes: 1 };\n }\n\n const control = input[offset];\n\n // End of stream\n if (control === 0x00) {\n return {\n success: true,\n chunk: {\n type: 'end',\n headerSize: 1,\n dictReset: false,\n stateReset: false,\n newProps: null,\n uncompSize: 0,\n compSize: 0,\n },\n };\n }\n\n // Uncompressed chunk\n if (control === 0x01 || control === 0x02) {\n // Need 3 bytes: control + 2 size bytes\n if (offset + 3 > input.length) {\n return { success: false, needBytes: 3 - (input.length - offset) };\n }\n\n const uncompSize = ((input[offset + 1] << 8) | input[offset + 2]) + 1;\n\n return {\n success: true,\n chunk: {\n type: 'uncompressed',\n headerSize: 3,\n dictReset: control === 0x01,\n stateReset: false,\n newProps: null,\n uncompSize,\n compSize: 0,\n },\n };\n }\n\n // LZMA compressed chunk\n if (control >= 0x80) {\n const hasNewProps = control >= 0xc0;\n const minHeaderSize = hasNewProps ? 6 : 5; // control + 2 uncomp + 2 comp + (1 props)\n\n if (offset + minHeaderSize > input.length) {\n return { success: false, needBytes: minHeaderSize - (input.length - offset) };\n }\n\n // Parse sizes\n const uncompHigh = control & 0x1f;\n const uncompSize = ((uncompHigh << 16) | (input[offset + 1] << 8) | input[offset + 2]) + 1;\n const compSize = ((input[offset + 3] << 8) | input[offset + 4]) + 1;\n\n // Parse properties if present\n let newProps: LzmaChunkProps | null = null;\n if (hasNewProps) {\n const propsByte = input[offset + 5];\n const lc = propsByte % 9;\n const remainder = ~~(propsByte / 9);\n const lp = remainder % 5;\n const pb = ~~(remainder / 5);\n newProps = { lc, lp, pb };\n }\n\n return {\n success: true,\n chunk: {\n type: 'lzma',\n headerSize: minHeaderSize,\n dictReset: control >= 0xe0,\n stateReset: control >= 0xa0,\n newProps,\n uncompSize,\n compSize,\n },\n };\n }\n\n // Invalid control byte\n throw new Error(`Invalid LZMA2 control byte: 0x${control.toString(16)}`);\n}\n\n/** Result type for hasCompleteChunk with totalSize included on success */\nexport type CompleteChunkResult = { success: true; chunk: Lzma2Chunk; totalSize: number } | { success: false; needBytes: number };\n\n/**\n * Check if we have enough data for the complete chunk (header + data)\n */\nexport function hasCompleteChunk(input: Buffer, offset: number): CompleteChunkResult {\n const result = parseLzma2ChunkHeader(input, offset);\n\n if (result.success === false) {\n return { success: false, needBytes: result.needBytes };\n }\n\n const { chunk } = result;\n const dataSize = chunk.type === 'uncompressed' ? chunk.uncompSize : chunk.compSize;\n const totalSize = chunk.headerSize + dataSize;\n\n if (offset + totalSize > input.length) {\n return { success: false, needBytes: totalSize - (input.length - offset) };\n }\n\n return { success: true, chunk, totalSize };\n}\n"],"names":["hasCompleteChunk","parseLzma2ChunkHeader","input","offset","length","success","needBytes","control","chunk","type","headerSize","dictReset","stateReset","newProps","uncompSize","compSize","hasNewProps","minHeaderSize","uncompHigh","propsByte","lc","remainder","lp","pb","Error","toString","result","dataSize","totalSize"],"mappings":"AAAA;;;;;;;;;;;;;;CAcC,GAED;;CAEC;;;;;;;;;;;QAsIeA;eAAAA;;QA/FAC;eAAAA;;;AAAT,S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AAS;QAAMG,OAAAA;QAAOoB,WAAAA;IAAU;AAC3C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/xz-compat/src/lzma/stream/transforms.ts"],"sourcesContent":["/**\n * LZMA Transform Stream Wrappers\n *\n * Provides Transform streams for LZMA1 and LZMA2 decompression.\n *\n * LZMA2 streaming works by buffering until a complete chunk is available,\n * then decoding synchronously. LZMA2 chunks are bounded in size (~2MB max\n * uncompressed), so memory usage is predictable and bounded.\n *\n * Performance Optimization:\n * - Uses OutputSink pattern for zero-copy output during decode\n * - Each decoded byte written directly to stream (not buffered then copied)\n * - ~4x faster than previous buffering approach\n *\n * True byte-by-byte async LZMA streaming would require rewriting the entire\n * decoder with continuation-passing style, which is complex and not worth\n * the effort given LZMA2's chunked format.\n */\n\nimport { allocBufferUnsafe, Transform } from 'extract-base-iterator';\nimport { hasCompleteChunk } from '../lib/Lzma2ChunkParser.ts';\nimport { LzmaDecoder } from '../sync/LzmaDecoder.ts';\nimport { parseLzma2DictionarySize } from '../types.ts';\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * This is a streaming decoder that processes LZMA2 chunks incrementally.\n * Memory usage is O(dictionary_size + max_chunk_size) instead of O(folder_size).\n *\n * @param properties - 1-byte LZMA2 properties (dictionary size)\n * @returns Transform stream that decompresses LZMA2 data\n */\nexport function createLzma2Decoder(properties: Buffer | Uint8Array): InstanceType<typeof Transform> {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = parseLzma2DictionarySize(properties[0]);\n\n // LZMA decoder instance - reused across chunks for solid mode\n const decoder = new LzmaDecoder();\n decoder.setDictionarySize(dictSize);\n\n // Track current LZMA properties\n let propsSet = false;\n\n // Store lc/lp/pb for reuse in stream decoder\n let currentLc: number | undefined;\n let currentLp: number | undefined;\n let currentPb: number | undefined;\n\n // Buffer for incomplete chunk data\n let pending: Buffer | null = null;\n let finished = false;\n\n return new Transform({\n transform: function (this: InstanceType<typeof Transform>, chunk: Buffer, _encoding: string, callback: (err?: Error | null) => void) {\n if (finished) {\n callback(null);\n return;\n }\n\n // Combine with pending data\n let input: Buffer;\n if (pending && pending.length > 0) {\n input = Buffer.concat([pending, chunk]);\n pending = null;\n } else {\n input = chunk;\n }\n\n let offset = 0;\n\n try {\n while (offset < input.length && !finished) {\n const result = hasCompleteChunk(input, offset);\n\n if (!result.success) {\n // Need more data\n pending = input.slice(offset);\n break;\n }\n\n const { chunk: chunkInfo, totalSize } = result;\n\n if (chunkInfo.type === 'end') {\n finished = true;\n break;\n }\n\n // Handle dictionary reset\n if (chunkInfo.dictReset) {\n decoder.resetDictionary();\n }\n\n const dataOffset = offset + chunkInfo.headerSize;\n\n if (chunkInfo.type === 'uncompressed') {\n const uncompData = input.slice(dataOffset, dataOffset + chunkInfo.uncompSize);\n this.push(uncompData);\n\n // Feed uncompressed data to dictionary for subsequent LZMA chunks\n decoder.feedUncompressed(uncompData);\n } else {\n // LZMA compressed chunk\n\n // Variables to store properties (used for both decoders)\n let lc: number;\n let lp: number;\n let pb: number;\n\n // Apply new properties if present\n if (chunkInfo.newProps) {\n ({ lc, lp, pb } = chunkInfo.newProps);\n // Store properties for reuse in stream decoder\n currentLc = lc;\n currentLp = lp;\n currentPb = pb;\n if (!decoder.setLcLpPb(lc, lp, pb)) {\n throw new Error(`Invalid LZMA properties: lc=${lc} lp=${lp} pb=${pb}`);\n }\n propsSet = true;\n } else {\n // No new properties, check if we already have them\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n }\n\n // Reset probabilities if state reset\n if (chunkInfo.stateReset) {\n decoder.resetProbabilities();\n }\n\n // Determine solid mode - preserve dictionary if not resetting state or if only resetting state (not dict)\n const useSolid = !chunkInfo.stateReset || (chunkInfo.stateReset && !chunkInfo.dictReset);\n\n const compData = input.slice(dataOffset, dataOffset + chunkInfo.compSize);\n\n // Enhanced: Use OutputSink for direct emission (zero-copy)\n // Create a decoder with direct stream emission\n const streamDecoder = new LzmaDecoder({\n write: (chunk: Buffer) => this.push(chunk),\n });\n streamDecoder.setDictionarySize(dictSize);\n // Set properties from current values (from first chunk or newProps)\n if (currentLc !== undefined && currentLp !== undefined && currentPb !== undefined) {\n streamDecoder.setLcLpPb(currentLc, currentLp, currentPb);\n }\n\n // Use solid mode based on chunk properties\n streamDecoder.decodeWithSink(compData, 0, chunkInfo.uncompSize, useSolid);\n\n // Flush any remaining data in the OutWindow\n streamDecoder.flushOutWindow();\n }\n\n offset += totalSize;\n }\n\n callback(null);\n } catch (err) {\n callback(err as Error);\n }\n },\n\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n if (pending && pending.length > 0 && !finished) {\n callback(new Error('Truncated LZMA2 stream'));\n } else {\n callback(null);\n }\n },\n });\n}\n\n/**\n * Create an LZMA1 decoder Transform stream\n *\n * Note: LZMA1 has no chunk boundaries, so this requires knowing the\n * uncompressed size upfront. The stream buffers all input, then\n * decompresses when complete.\n *\n * For true streaming, use LZMA2 which has built-in chunking.\n *\n * Optimization: Pre-allocates input buffer and copies chunks once,\n * avoiding the double-buffering of Buffer.concat().\n *\n * @param properties - 5-byte LZMA properties\n * @param unpackSize - Expected uncompressed size\n * @returns Transform stream that decompresses LZMA1 data\n */\nexport function createLzmaDecoder(properties: Buffer | Uint8Array, unpackSize: number): InstanceType<typeof Transform> {\n const decoder = new LzmaDecoder();\n decoder.setDecoderProperties(properties);\n\n const chunks: Buffer[] = [];\n let totalSize = 0;\n\n return new Transform({\n transform: function (this: InstanceType<typeof Transform>, chunk: Buffer, _encoding: string, callback: (err?: Error | null) => void) {\n chunks.push(chunk);\n totalSize += chunk.length;\n callback(null);\n },\n\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n try {\n // Optimization: Pre-allocate single buffer instead of Buffer.concat()\n // This reduces peak memory usage by ~50% during concatenation\n const input = allocBufferUnsafe(totalSize);\n let offset = 0;\n\n // Copy each chunk into the pre-allocated buffer\n for (let i = 0; i < chunks.length; i++) {\n const chunk = chunks[i];\n chunk.copy(input, offset);\n offset += chunk.length;\n }\n\n // Enhanced: Use OutputSink for direct emission (zero-copy)\n // Create a decoder with direct stream emission\n const streamDecoder = new LzmaDecoder({\n write: (chunk: Buffer) => this.push(chunk),\n });\n streamDecoder.setDecoderProperties(properties);\n streamDecoder.decodeWithSink(input, 0, unpackSize, false);\n\n // Flush any remaining data in the OutWindow\n streamDecoder.flushOutWindow();\n\n callback(null);\n } catch (err) {\n callback(err as Error);\n }\n },\n });\n}\n"],"names":["createLzma2Decoder","createLzmaDecoder","properties","length","Error","dictSize","parseLzma2DictionarySize","decoder","LzmaDecoder","setDictionarySize","propsSet","currentLc","currentLp","currentPb","pending","finished","Transform","transform","chunk","_encoding","callback","input","Buffer","concat","offset","result","hasCompleteChunk","success","slice","chunkInfo","totalSize","type","dictReset","resetDictionary","dataOffset","headerSize","uncompData","uncompSize","push","feedUncompressed","lc","lp","pb","newProps","setLcLpPb","stateReset","resetProbabilities","useSolid","compData","compSize","streamDecoder","write","undefined","decodeWithSink","flushOutWindow","err","flush","unpackSize","setDecoderProperties","chunks","allocBufferUnsafe","i","copy"],"mappings":"AAAA;;;;;;;;;;;;;;;;;CAiBC;;;;;;;;;;;QAgBeA;eAAAA;;QAgKAC;eAAAA;;;mCA9K6B;kCACZ;6BACL;uBACa;AAWlC,SAASD,mBAAmBE,UAA+B;IAChE,IAAI,CAACA,cAAcA,WAAWC,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAMC,WAAWC,IAAAA,iCAAwB,EAACJ,UAAU,CAAC,EAAE;IAEvD,8DAA8D;IAC9D,IAAMK,UAAU,IAAIC,0BAAW;IAC/BD,QAAQE,iBAAiB,CAACJ;IAE1B,gCAAgC;IAChC,IAAIK,WAAW;IAEf,6CAA6C;IAC7C,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IAEJ,mCAAmC;IACnC,IAAIC,UAAyB;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Transform Stream Wrappers\n *\n * Provides Transform streams for LZMA1 and LZMA2 decompression.\n *\n * LZMA2 streaming works by buffering until a complete chunk is available,\n * then decoding synchronously. LZMA2 chunks are bounded in size (~2MB max\n * uncompressed), so memory usage is predictable and bounded.\n *\n * Performance Optimization:\n * - Uses OutputSink pattern for zero-copy output during decode\n * - Each decoded byte written directly to stream (not buffered then copied)\n * - ~4x faster than previous buffering approach\n *\n * True byte-by-byte async LZMA streaming would require rewriting the entire\n * decoder with continuation-passing style, which is complex and not worth\n * the effort given LZMA2's chunked format.\n */\n\nimport { allocBufferUnsafe, Transform } from 'extract-base-iterator';\nimport { hasCompleteChunk } from '../lib/Lzma2ChunkParser.ts';\nimport { LzmaDecoder } from '../sync/LzmaDecoder.ts';\nimport { parseLzma2DictionarySize } from '../types.ts';\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * This is a streaming decoder that processes LZMA2 chunks incrementally.\n * Memory usage is O(dictionary_size + max_chunk_size) instead of O(folder_size).\n *\n * @param properties - 1-byte LZMA2 properties (dictionary size)\n * @returns Transform stream that decompresses LZMA2 data\n */\nexport function createLzma2Decoder(properties: Buffer | Uint8Array): InstanceType<typeof Transform> {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = parseLzma2DictionarySize(properties[0]);\n\n // LZMA decoder instance - reused across chunks for solid mode\n const decoder = new LzmaDecoder();\n decoder.setDictionarySize(dictSize);\n\n // Track current LZMA properties\n let propsSet = false;\n\n // Store lc/lp/pb for reuse in stream decoder\n let currentLc: number | undefined;\n let currentLp: number | undefined;\n let currentPb: number | undefined;\n\n // Buffer for incomplete chunk data\n let pending: Buffer | null = null;\n let finished = false;\n\n return new Transform({\n transform: function (this: InstanceType<typeof Transform>, chunk: Buffer, _encoding: string, callback: (err?: Error | null) => void) {\n if (finished) {\n callback(null);\n return;\n }\n\n // Combine with pending data\n let input: Buffer;\n if (pending && pending.length > 0) {\n input = Buffer.concat([pending, chunk]);\n pending = null;\n } else {\n input = chunk;\n }\n\n let offset = 0;\n\n try {\n while (offset < input.length && !finished) {\n const result = hasCompleteChunk(input, offset);\n\n if (!result.success) {\n // Need more data\n pending = input.slice(offset);\n break;\n }\n\n const { chunk: chunkInfo, totalSize } = result;\n\n if (chunkInfo.type === 'end') {\n finished = true;\n break;\n }\n\n // Handle dictionary reset\n if (chunkInfo.dictReset) {\n decoder.resetDictionary();\n }\n\n const dataOffset = offset + chunkInfo.headerSize;\n\n if (chunkInfo.type === 'uncompressed') {\n const uncompData = input.slice(dataOffset, dataOffset + chunkInfo.uncompSize);\n this.push(uncompData);\n\n // Feed uncompressed data to dictionary for subsequent LZMA chunks\n decoder.feedUncompressed(uncompData);\n } else {\n // LZMA compressed chunk\n\n // Variables to store properties (used for both decoders)\n let lc: number;\n let lp: number;\n let pb: number;\n\n // Apply new properties if present\n if (chunkInfo.newProps) {\n ({ lc, lp, pb } = chunkInfo.newProps);\n // Store properties for reuse in stream decoder\n currentLc = lc;\n currentLp = lp;\n currentPb = pb;\n if (!decoder.setLcLpPb(lc, lp, pb)) {\n throw new Error(`Invalid LZMA properties: lc=${lc} lp=${lp} pb=${pb}`);\n }\n propsSet = true;\n } else {\n // No new properties, check if we already have them\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n }\n\n // Reset probabilities if state reset\n if (chunkInfo.stateReset) {\n decoder.resetProbabilities();\n }\n\n // Determine solid mode - preserve dictionary if not resetting state or if only resetting state (not dict)\n const useSolid = !chunkInfo.stateReset || (chunkInfo.stateReset && !chunkInfo.dictReset);\n\n const compData = input.slice(dataOffset, dataOffset + chunkInfo.compSize);\n\n // Enhanced: Use OutputSink for direct emission (zero-copy)\n // Create a decoder with direct stream emission\n const streamDecoder = new LzmaDecoder({\n write: (chunk: Buffer) => this.push(chunk),\n });\n streamDecoder.setDictionarySize(dictSize);\n // Set properties from current values (from first chunk or newProps)\n if (currentLc !== undefined && currentLp !== undefined && currentPb !== undefined) {\n streamDecoder.setLcLpPb(currentLc, currentLp, currentPb);\n }\n\n // Use solid mode based on chunk properties\n streamDecoder.decodeWithSink(compData, 0, chunkInfo.uncompSize, useSolid);\n\n // Flush any remaining data in the OutWindow\n streamDecoder.flushOutWindow();\n }\n\n offset += totalSize;\n }\n\n callback(null);\n } catch (err) {\n callback(err as Error);\n }\n },\n\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n if (pending && pending.length > 0 && !finished) {\n callback(new Error('Truncated LZMA2 stream'));\n } else {\n callback(null);\n }\n },\n });\n}\n\n/**\n * Create an LZMA1 decoder Transform stream\n *\n * Note: LZMA1 has no chunk boundaries, so this requires knowing the\n * uncompressed size upfront. The stream buffers all input, then\n * decompresses when complete.\n *\n * For true streaming, use LZMA2 which has built-in chunking.\n *\n * Optimization: Pre-allocates input buffer and copies chunks once,\n * avoiding the double-buffering of Buffer.concat().\n *\n * @param properties - 5-byte LZMA properties\n * @param unpackSize - Expected uncompressed size\n * @returns Transform stream that decompresses LZMA1 data\n */\nexport function createLzmaDecoder(properties: Buffer | Uint8Array, unpackSize: number): InstanceType<typeof Transform> {\n const decoder = new LzmaDecoder();\n decoder.setDecoderProperties(properties);\n\n const chunks: Buffer[] = [];\n let totalSize = 0;\n\n return new Transform({\n transform: function (this: InstanceType<typeof Transform>, chunk: Buffer, _encoding: string, callback: (err?: Error | null) => void) {\n chunks.push(chunk);\n totalSize += chunk.length;\n callback(null);\n },\n\n flush: function (this: InstanceType<typeof Transform>, callback: (err?: Error | null) => void) {\n try {\n // Optimization: Pre-allocate single buffer instead of Buffer.concat()\n // This reduces peak memory usage by ~50% during concatenation\n const input = allocBufferUnsafe(totalSize);\n let offset = 0;\n\n // Copy each chunk into the pre-allocated buffer\n for (let i = 0; i < chunks.length; i++) {\n const chunk = chunks[i];\n chunk.copy(input, offset);\n offset += chunk.length;\n }\n\n // Enhanced: Use OutputSink for direct emission (zero-copy)\n // Create a decoder with direct stream emission\n const streamDecoder = new LzmaDecoder({\n write: (chunk: Buffer) => this.push(chunk),\n });\n streamDecoder.setDecoderProperties(properties);\n streamDecoder.decodeWithSink(input, 0, unpackSize, false);\n\n // Flush any remaining data in the OutWindow\n streamDecoder.flushOutWindow();\n\n callback(null);\n } catch (err) {\n callback(err as Error);\n }\n },\n });\n}\n"],"names":["createLzma2Decoder","createLzmaDecoder","properties","length","Error","dictSize","parseLzma2DictionarySize","decoder","LzmaDecoder","setDictionarySize","propsSet","currentLc","currentLp","currentPb","pending","finished","Transform","transform","chunk","_encoding","callback","input","Buffer","concat","offset","result","hasCompleteChunk","success","slice","chunkInfo","totalSize","type","dictReset","resetDictionary","dataOffset","headerSize","uncompData","uncompSize","push","feedUncompressed","lc","lp","pb","newProps","setLcLpPb","stateReset","resetProbabilities","useSolid","compData","compSize","streamDecoder","write","undefined","decodeWithSink","flushOutWindow","err","flush","unpackSize","setDecoderProperties","chunks","allocBufferUnsafe","i","copy"],"mappings":"AAAA;;;;;;;;;;;;;;;;;CAiBC;;;;;;;;;;;QAgBeA;eAAAA;;QAgKAC;eAAAA;;;mCA9K6B;kCACZ;6BACL;uBACa;AAWlC,SAASD,mBAAmBE,UAA+B;IAChE,IAAI,CAACA,cAAcA,WAAWC,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAMC,WAAWC,IAAAA,iCAAwB,EAACJ,UAAU,CAAC,EAAE;IAEvD,8DAA8D;IAC9D,IAAMK,UAAU,IAAIC,0BAAW;IAC/BD,QAAQE,iBAAiB,CAACJ;IAE1B,gCAAgC;IAChC,IAAIK,WAAW;IAEf,6CAA6C;IAC7C,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IAEJ,mCAAmC;IACnC,IAAIC,UAAyB;IAC7B,IAAIC,WAAW;IAEf,OAAO,IAAIC,8BAAS,CAAC;QACnBC,WAAW,SAAXA,UAA2DC,KAAa,EAAEC,SAAiB,EAAEC,QAAsC;;YACjI,IAAIL,UAAU;gBACZK,SAAS;gBACT;YACF;YAEA,4BAA4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