7z-iterator 0.3.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/assets/lzma-purejs/lib/LZMA/Decoder.js +26 -0
- package/dist/cjs/sevenz/NumberCodec.js.map +1 -1
- package/dist/cjs/sevenz/SevenZipParser.js +2 -2
- package/dist/cjs/sevenz/SevenZipParser.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Aes.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Lzma2.js +8 -1
- package/dist/cjs/sevenz/codecs/Lzma2.js.map +1 -1
- package/dist/esm/sevenz/codecs/Lzma2.js +8 -1
- package/dist/esm/sevenz/codecs/Lzma2.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
## 7z-iterator
|
|
2
2
|
|
|
3
|
-
Extract contents from
|
|
3
|
+
Extract contents from 7z archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms.
|
|
4
4
|
|
|
5
5
|
// asyncIterator
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
8
|
var assert = require('assert');
|
|
9
9
|
var fs = require('fs');
|
|
10
|
-
var
|
|
10
|
+
var SevenZipIterator = require('7z-iterator');
|
|
11
11
|
|
|
12
12
|
(async function() {
|
|
13
|
-
let iterator = new
|
|
13
|
+
let iterator = new SevenZipIterator('/path/to/archive');
|
|
14
14
|
|
|
15
15
|
try {
|
|
16
16
|
const links = [];
|
|
@@ -30,7 +30,7 @@ var ZipIterator = require('7z-iterator');
|
|
|
30
30
|
})();
|
|
31
31
|
|
|
32
32
|
(async function() {
|
|
33
|
-
let iterator = new
|
|
33
|
+
let iterator = new SevenZipIterator(fs.createReadStream('/path/to/archive'));
|
|
34
34
|
|
|
35
35
|
try {
|
|
36
36
|
const links = [];
|
|
@@ -54,11 +54,11 @@ var ZipIterator = require('7z-iterator');
|
|
|
54
54
|
|
|
55
55
|
```js
|
|
56
56
|
var assert = require('assert');
|
|
57
|
-
var
|
|
57
|
+
var SevenZipIterator = require('7z-iterator');
|
|
58
58
|
|
|
59
59
|
// one by one
|
|
60
60
|
(async function() {
|
|
61
|
-
let iterator = new
|
|
61
|
+
let iterator = new SevenZipIterator('/path/to/archive');
|
|
62
62
|
|
|
63
63
|
const links = [];
|
|
64
64
|
let entry = await iterator.next();
|
|
@@ -79,7 +79,7 @@ var ZipIterator = require('7z-iterator');
|
|
|
79
79
|
|
|
80
80
|
// infinite concurrency
|
|
81
81
|
(async function() {
|
|
82
|
-
let iterator = new
|
|
82
|
+
let iterator = new SevenZipIterator('/path/to/archive');
|
|
83
83
|
|
|
84
84
|
try {
|
|
85
85
|
const links = [];
|
|
@@ -108,9 +108,9 @@ var ZipIterator = require('7z-iterator');
|
|
|
108
108
|
```js
|
|
109
109
|
var assert = require('assert');
|
|
110
110
|
var Queue = require('queue-cb');
|
|
111
|
-
var
|
|
111
|
+
var SevenZipIterator = require('7z-iterator');
|
|
112
112
|
|
|
113
|
-
var iterator = new
|
|
113
|
+
var iterator = new SevenZipIterator('/path/to/archive');
|
|
114
114
|
|
|
115
115
|
// one by one
|
|
116
116
|
var links = [];
|
|
@@ -126,7 +126,7 @@ iterator.forEach(
|
|
|
126
126
|
},
|
|
127
127
|
{ callbacks: true, concurrency: 1 },
|
|
128
128
|
function (err) {
|
|
129
|
-
|
|
129
|
+
|
|
130
130
|
// create links after directories and files
|
|
131
131
|
var queue = new Queue();
|
|
132
132
|
for (var index = 0; index < links.length; index++) {
|
|
@@ -142,6 +142,32 @@ Decoder.prototype.setSolid = function(solid) {
|
|
|
142
142
|
this._solid = solid;
|
|
143
143
|
};
|
|
144
144
|
|
|
145
|
+
Decoder.prototype.resetProbabilities = function() {
|
|
146
|
+
// Reset probability tables (same as init() but without dictionary/stream reset)
|
|
147
|
+
// Used for LZMA2 control bytes 0xa0-0xdf (state reset without dictionary reset)
|
|
148
|
+
initBitModels(this._isMatchDecoders);
|
|
149
|
+
initBitModels(this._isRepDecoders);
|
|
150
|
+
initBitModels(this._isRepG0Decoders);
|
|
151
|
+
initBitModels(this._isRepG1Decoders);
|
|
152
|
+
initBitModels(this._isRepG2Decoders);
|
|
153
|
+
initBitModels(this._isRep0LongDecoders);
|
|
154
|
+
initBitModels(this._posDecoders);
|
|
155
|
+
this._literalDecoder.init();
|
|
156
|
+
for (var i = this._posSlotDecoder.length - 1; i >= 0; i--) {
|
|
157
|
+
this._posSlotDecoder[i].init();
|
|
158
|
+
}
|
|
159
|
+
this._lenDecoder.init();
|
|
160
|
+
this._repLenDecoder.init();
|
|
161
|
+
this._posAlignDecoder.init();
|
|
162
|
+
// Reset state variables
|
|
163
|
+
this._state = 0;
|
|
164
|
+
this._rep0 = 0;
|
|
165
|
+
this._rep1 = 0;
|
|
166
|
+
this._rep2 = 0;
|
|
167
|
+
this._rep3 = 0;
|
|
168
|
+
// DO NOT reset _nowPos64, _prevByte, or dictionary
|
|
169
|
+
};
|
|
170
|
+
|
|
145
171
|
Decoder.prototype.setDictionarySize = function(dictionarySize){
|
|
146
172
|
if (dictionarySize < 0) return false;
|
|
147
173
|
if (this._dictionarySize !== dictionarySize){
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/NumberCodec.ts"],"sourcesContent":["// Variable-length integer encoding for 7z format\n// Reference: https://py7zr.readthedocs.io/en/latest/archive_format.html\n//\n// 7z uses a space-efficient encoding where the first byte determines length:\n// 0xxxxxxx -> 1 byte (0-127)\n// 10xxxxxx xxxxxxxx -> 2 bytes (0-16383)\n// 110xxxxx + 2 bytes -> 3 bytes\n// 1110xxxx + 3 bytes -> 4 bytes\n// 11110xxx + 4 bytes -> 5 bytes\n// 111110xx + 5 bytes -> 6 bytes\n// 1111110x + 6 bytes -> 7 bytes\n// 11111110 + 7 bytes -> 8 bytes\n// 11111111 + 8 bytes -> 9 bytes (full 64-bit)\n//\n// NOTE: Returns JavaScript number which is accurate up to 2^53 - 1 (~9 PB).\n// This covers all practical file sizes.\n\nimport { readUInt64LE } from 'extract-base-iterator';\n\nexport interface NumberReadResult {\n value: number;\n bytesRead: number;\n}\n\n/**\n * Read a variable-length encoded number from a buffer\n * Uses 7z's variable-length uint64 encoding where the first byte indicates\n * how many additional bytes follow based on its value:\n * - 0x00-0x7F: 0 extra bytes (7 bits of data)\n * - 0x80-0xBF: 1 extra byte (14 bits of data)\n * - 0xC0-0xDF: 2 extra bytes (21 bits of data)\n * - 0xE0-0xEF: 3 extra bytes (28 bits of data)\n * - etc.\n * - 0xFF: 8 extra bytes (full 64-bit value)\n *\n * @param buf - Buffer containing encoded number\n * @param offset - Offset to start reading from\n * @returns Object with value and number of bytes consumed\n */\nexport function readNumber(buf: Buffer, offset: number): NumberReadResult {\n const firstByte = buf[offset];\n\n // Special case: 0xFF means 8 extra bytes (full 64-bit value)\n if (firstByte === 0xff) {\n return {\n value: readUInt64LE(buf, offset + 1),\n bytesRead: 9,\n };\n }\n\n // Determine number of extra bytes based on first byte value thresholds\n // This matches the 7z format specification\n let extraBytes = 0;\n let mask = 0x80;\n\n if (firstByte <= 0x7f) {\n extraBytes = 0;\n mask = 0x80;\n } else if (firstByte <= 0xbf) {\n extraBytes = 1;\n mask = 0x40;\n } else if (firstByte <= 0xdf) {\n extraBytes = 2;\n mask = 0x20;\n } else if (firstByte <= 0xef) {\n extraBytes = 3;\n mask = 0x10;\n } else if (firstByte <= 0xf7) {\n extraBytes = 4;\n mask = 0x08;\n } else if (firstByte <= 0xfb) {\n extraBytes = 5;\n mask = 0x04;\n } else if (firstByte <= 0xfd) {\n extraBytes = 6;\n mask = 0x02;\n } else {\n // 0xFE\n extraBytes = 7;\n mask = 0x01;\n }\n\n // Get high part from first byte (bits below the length indicator)\n const highPart = firstByte & (mask - 1);\n\n // Read extra bytes as LITTLE-ENDIAN\n let value = 0;\n for (let i = 0; i < extraBytes; i++) {\n value += buf[offset + 1 + i] * 256 ** i;\n }\n\n // Combine: value + (highPart << (extraBytes * 8))\n value += highPart * 256 ** extraBytes;\n\n return {\n value: value,\n bytesRead: 1 + extraBytes,\n };\n}\n\n/**\n * Read a raw 64-bit little-endian number (used in some fixed-size fields)\n * @param buf - Buffer containing the number\n * @param offset - Offset to start reading from\n * @returns The number value\n */\nexport function readRawNumber(buf: Buffer, offset: number): number {\n return readUInt64LE(buf, offset);\n}\n\n/**\n * Calculate the encoded size of a number\n * @param value - The number to encode\n * @returns Number of bytes needed to encode the value\n */\nexport function encodedSize(value: number): number {\n if (value < 0x80) return 1; // 7 bits\n if (value < 0x4000) return 2; // 14 bits\n if (value < 0x200000) return 3; // 21 bits\n if (value < 0x10000000) return 4; // 28 bits\n if (value < 0x800000000) return 5; // 35 bits\n if (value < 0x40000000000) return 6; // 42 bits\n if (value < 0x2000000000000) return 7; // 49 bits\n // 2^56 = 72057594037927936 (use calculated value to avoid precision loss)\n if (value < 72057594037927936) return 8; // 56 bits\n return 9; // 64 bits\n}\n\n/**\n * Read a boolean encoded as a single byte\n * @param buf - Buffer to read from\n * @param offset - Offset to read from\n * @returns true if byte is non-zero\n */\nexport function readBoolean(buf: Buffer, offset: number): boolean {\n return buf[offset] !== 0;\n}\n\n/**\n * Read a \"defined\" bitmask for an array of items.\n * Used when some items in a list have optional values.\n *\n * Format: If \"allDefined\" byte is 0, a bitmask follows indicating which items have values.\n * If \"allDefined\" byte is non-zero, all items are defined.\n *\n * @param buf - Buffer to read from\n * @param offset - Offset to start reading\n * @param count - Number of items\n * @returns Object with defined array and bytes consumed\n */\nexport function readDefinedVector(buf: Buffer, offset: number, count: number): { defined: boolean[]; bytesRead: number } {\n const allDefined = buf[offset] !== 0;\n let bytesRead = 1;\n const defined: boolean[] = [];\n\n if (allDefined) {\n // All items are defined\n for (let i = 0; i < count; i++) {\n defined.push(true);\n }\n } else {\n // Read bitmask\n const bitsNeeded = count;\n const bytesNeeded = Math.ceil(bitsNeeded / 8);\n\n for (let byteIdx = 0; byteIdx < bytesNeeded; byteIdx++) {\n const byte = buf[offset + 1 + byteIdx];\n for (let bit = 7; bit >= 0 && defined.length < count; bit--) {\n defined.push((byte & (1 << bit)) !== 0);\n }\n }\n bytesRead += bytesNeeded;\n }\n\n return { defined: defined, bytesRead: bytesRead };\n}\n\n/**\n * Read an array of variable-length numbers\n * @param buf - Buffer to read from\n * @param offset - Offset to start reading\n * @param count - Number of items to read\n * @returns Object with values array and bytes consumed\n */\nexport function readNumberArray(buf: Buffer, offset: number, count: number): { values: number[]; bytesRead: number } {\n const values: number[] = [];\n let totalBytesRead = 0;\n\n for (let i = 0; i < count; i++) {\n const result = readNumber(buf, offset + totalBytesRead);\n values.push(result.value);\n totalBytesRead += result.bytesRead;\n }\n\n return { values: values, bytesRead: totalBytesRead };\n}\n"],"names":["encodedSize","readBoolean","readDefinedVector","readNumber","readNumberArray","readRawNumber","buf","offset","firstByte","value","readUInt64LE","bytesRead","extraBytes","mask","highPart","i","count","allDefined","defined","push","bitsNeeded","bytesNeeded","Math","ceil","byteIdx","byte","bit","length","values","totalBytesRead","result"],"mappings":"AAAA,iDAAiD;AACjD,wEAAwE;AACxE,EAAE;AACF,6EAA6E;AAC7E,iDAAiD;AACjD,mDAAmD;AACnD,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,uDAAuD;AACvD,EAAE;AACF,4EAA4E;AAC5E,wCAAwC;;;;;;;;;;;;QAoGxBA;eAAAA;;QAmBAC;eAAAA;;QAgBAC;eAAAA;;QA/GAC;eAAAA;;QAiJAC;eAAAA;;QA9EAC;eAAAA;;;mCAzFa;AAsBtB,SAASF,WAAWG,GAAW,EAAEC,MAAc;IACpD,IAAMC,YAAYF,GAAG,CAACC,OAAO;IAE7B,6DAA6D;IAC7D,IAAIC,cAAc,MAAM;QACtB,OAAO;YACLC,OAAOC,IAAAA,iCAAY,EAACJ,KAAKC,SAAS;YAClCI,WAAW;QACb;IACF;IAEA,uEAAuE;IACvE,2CAA2C;IAC3C,IAAIC,aAAa;IACjB,IAAIC,OAAO;IAEX,IAAIL,aAAa,MAAM;QACrBI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO,IAAIL,aAAa,MAAM;QAC5BI,aAAa;QACbC,OAAO;IACT,OAAO;QACL,OAAO;QACPD,aAAa;QACbC,OAAO;IACT;IAEA,kEAAkE;IAClE,IAAMC,WAAWN,YAAaK,OAAO;IAErC,oCAAoC;IACpC,IAAIJ,QAAQ;IACZ,IAAK,IAAIM,IAAI,GAAGA,IAAIH,YAAYG,IAAK;QACnCN,SAASH,GAAG,CAACC,SAAS,IAAIQ,EAAE,
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/NumberCodec.ts"],"sourcesContent":["// Variable-length integer encoding for 7z format\n// Reference: https://py7zr.readthedocs.io/en/latest/archive_format.html\n//\n// 7z uses a space-efficient encoding where the first byte determines length:\n// 0xxxxxxx -> 1 byte (0-127)\n// 10xxxxxx xxxxxxxx -> 2 bytes (0-16383)\n// 110xxxxx + 2 bytes -> 3 bytes\n// 1110xxxx + 3 bytes -> 4 bytes\n// 11110xxx + 4 bytes -> 5 bytes\n// 111110xx + 5 bytes -> 6 bytes\n// 1111110x + 6 bytes -> 7 bytes\n// 11111110 + 7 bytes -> 8 bytes\n// 11111111 + 8 bytes -> 9 bytes (full 64-bit)\n//\n// NOTE: Returns JavaScript number which is accurate up to 2^53 - 1 (~9 PB).\n// This covers all practical file sizes.\n\nimport { readUInt64LE } from 'extract-base-iterator';\n\nexport interface NumberReadResult {\n value: number;\n bytesRead: number;\n}\n\n/**\n * Read a variable-length encoded number from a buffer\n * Uses 7z's variable-length uint64 encoding where the first byte indicates\n * how many additional bytes follow based on its value:\n * - 0x00-0x7F: 0 extra bytes (7 bits of data)\n * - 0x80-0xBF: 1 extra byte (14 bits of data)\n * - 0xC0-0xDF: 2 extra bytes (21 bits of data)\n * - 0xE0-0xEF: 3 extra bytes (28 bits of data)\n * - etc.\n * - 0xFF: 8 extra bytes (full 64-bit value)\n *\n * @param buf - Buffer containing encoded number\n * @param offset - Offset to start reading from\n * @returns Object with value and number of bytes consumed\n */\nexport function readNumber(buf: Buffer, offset: number): NumberReadResult {\n const firstByte = buf[offset];\n\n // Special case: 0xFF means 8 extra bytes (full 64-bit value)\n if (firstByte === 0xff) {\n return {\n value: readUInt64LE(buf, offset + 1),\n bytesRead: 9,\n };\n }\n\n // Determine number of extra bytes based on first byte value thresholds\n // This matches the 7z format specification\n let extraBytes = 0;\n let mask = 0x80;\n\n if (firstByte <= 0x7f) {\n extraBytes = 0;\n mask = 0x80;\n } else if (firstByte <= 0xbf) {\n extraBytes = 1;\n mask = 0x40;\n } else if (firstByte <= 0xdf) {\n extraBytes = 2;\n mask = 0x20;\n } else if (firstByte <= 0xef) {\n extraBytes = 3;\n mask = 0x10;\n } else if (firstByte <= 0xf7) {\n extraBytes = 4;\n mask = 0x08;\n } else if (firstByte <= 0xfb) {\n extraBytes = 5;\n mask = 0x04;\n } else if (firstByte <= 0xfd) {\n extraBytes = 6;\n mask = 0x02;\n } else {\n // 0xFE\n extraBytes = 7;\n mask = 0x01;\n }\n\n // Get high part from first byte (bits below the length indicator)\n const highPart = firstByte & (mask - 1);\n\n // Read extra bytes as LITTLE-ENDIAN\n let value = 0;\n for (let i = 0; i < extraBytes; i++) {\n value += buf[offset + 1 + i] * 256 ** i;\n }\n\n // Combine: value + (highPart << (extraBytes * 8))\n value += highPart * 256 ** extraBytes;\n\n return {\n value: value,\n bytesRead: 1 + extraBytes,\n };\n}\n\n/**\n * Read a raw 64-bit little-endian number (used in some fixed-size fields)\n * @param buf - Buffer containing the number\n * @param offset - Offset to start reading from\n * @returns The number value\n */\nexport function readRawNumber(buf: Buffer, offset: number): number {\n return readUInt64LE(buf, offset);\n}\n\n/**\n * Calculate the encoded size of a number\n * @param value - The number to encode\n * @returns Number of bytes needed to encode the value\n */\nexport function encodedSize(value: number): number {\n if (value < 0x80) return 1; // 7 bits\n if (value < 0x4000) return 2; // 14 bits\n if (value < 0x200000) return 3; // 21 bits\n if (value < 0x10000000) return 4; // 28 bits\n if (value < 0x800000000) return 5; // 35 bits\n if (value < 0x40000000000) return 6; // 42 bits\n if (value < 0x2000000000000) return 7; // 49 bits\n // 2^56 = 72057594037927936 (use calculated value to avoid precision loss)\n if (value < 72057594037927936) return 8; // 56 bits\n return 9; // 64 bits\n}\n\n/**\n * Read a boolean encoded as a single byte\n * @param buf - Buffer to read from\n * @param offset - Offset to read from\n * @returns true if byte is non-zero\n */\nexport function readBoolean(buf: Buffer, offset: number): boolean {\n return buf[offset] !== 0;\n}\n\n/**\n * Read a \"defined\" bitmask for an array of items.\n * Used when some items in a list have optional values.\n *\n * Format: If \"allDefined\" byte is 0, a bitmask follows indicating which items have values.\n * If \"allDefined\" byte is non-zero, all items are defined.\n *\n * @param buf - Buffer to read from\n * @param offset - Offset to start reading\n * @param count - Number of items\n * @returns Object with defined array and bytes consumed\n */\nexport function readDefinedVector(buf: Buffer, offset: number, count: number): { defined: boolean[]; bytesRead: number } {\n const allDefined = buf[offset] !== 0;\n let bytesRead = 1;\n const defined: boolean[] = [];\n\n if (allDefined) {\n // All items are defined\n for (let i = 0; i < count; i++) {\n defined.push(true);\n }\n } else {\n // Read bitmask\n const bitsNeeded = count;\n const bytesNeeded = Math.ceil(bitsNeeded / 8);\n\n for (let byteIdx = 0; byteIdx < bytesNeeded; byteIdx++) {\n const byte = buf[offset + 1 + byteIdx];\n for (let bit = 7; bit >= 0 && defined.length < count; bit--) {\n defined.push((byte & (1 << bit)) !== 0);\n }\n }\n bytesRead += bytesNeeded;\n }\n\n return { defined: defined, bytesRead: bytesRead };\n}\n\n/**\n * Read an array of variable-length numbers\n * @param buf - Buffer to read from\n * @param offset - Offset to start reading\n * @param count - Number of items to read\n * @returns Object with values array and bytes consumed\n */\nexport function readNumberArray(buf: Buffer, offset: number, count: number): { values: number[]; bytesRead: number } {\n const values: number[] = [];\n let totalBytesRead = 0;\n\n for (let i = 0; i < count; i++) {\n const result = readNumber(buf, offset + totalBytesRead);\n values.push(result.value);\n totalBytesRead += result.bytesRead;\n }\n\n return { values: values, bytesRead: totalBytesRead };\n}\n"],"names":["encodedSize","readBoolean","readDefinedVector","readNumber","readNumberArray","readRawNumber","buf","offset","firstByte","value","readUInt64LE","bytesRead","extraBytes","mask","highPart","i","count","allDefined","defined","push","bitsNeeded","bytesNeeded","Math","ceil","byteIdx","byte","bit","length","values","totalBytesRead","result"],"mappings":"AAAA,iDAAiD;AACjD,wEAAwE;AACxE,EAAE;AACF,6EAA6E;AAC7E,iDAAiD;AACjD,mDAAmD;AACnD,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,uDAAuD;AACvD,EAAE;AACF,4EAA4E;AAC5E,wCAAwC;;;;;;;;;;;;QAoGxBA;eAAAA;;QAmBAC;eAAAA;;QAgBAC;eAAAA;;QA/GAC;eAAAA;;QAiJAC;eAAAA;;QA9EAC;eAAAA;;;mCAzFa;AAsBtB,SAASF,WAAWG,GAAW,EAAEC,MAAc;IACpD,IAAMC,YAAYF,GAAG,CAACC,OAAO;IAE7B,6DAA6D;IAC7D,IAAIC,cAAc,MAAM;QACtB,OAAO;YACLC,OAAOC,IAAAA,iCAAY,EAACJ,KAAKC,SAAS;YAClCI,WAAW;QACb;IACF;IAEA,uEAAuE;IACvE,2CAA2C;IAC3C,IAAIC,aAAa;IACjB,I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C mismatch, need to search
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
|
-
} catch (
|
|
152
|
+
} catch (unused) {
|
|
153
153
|
decompressedHeader = null; // Decompression failed, need to search
|
|
154
154
|
}
|
|
155
155
|
// If initial decompression failed, search for the correct position as a fallback
|
|
@@ -179,7 +179,7 @@ var SevenZipParser = /*#__PURE__*/ function() {
|
|
|
179
179
|
decompressedHeader = candidateDecompressed;
|
|
180
180
|
break searchLoop;
|
|
181
181
|
}
|
|
182
|
-
} catch (
|
|
182
|
+
} catch (unused) {
|
|
183
183
|
// Decompression failed, continue searching
|
|
184
184
|
}
|
|
185
185
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/SevenZipParser.ts"],"sourcesContent":["/**\n * SevenZipParser - Main 7z archive parser\n *\n * Handles reading archive structure and providing file streams.\n *\n * Parser Flow:\n * 1. Read signature header (32 bytes) to get header location\n * 2. Read encoded header from nextHeaderOffset\n * 3. If header is compressed, decompress it first\n * 4. Parse streams info (folder structure, pack positions)\n * 5. Parse files info (names, sizes, attributes)\n * 6. Build entry list for iteration\n *\n * Decompression:\n * - 7z uses \"folders\" as decompression units\n * - Solid archives: multiple files share one folder (decompress once)\n * - Non-solid: one file per folder\n * - Supports LZMA, LZMA2, COPY, BCJ2, and other codecs\n */\n\nimport { crc32 } from 'extract-base-iterator';\nimport oo from 'on-one';\nimport Stream from 'stream';\nimport type { ArchiveSource } from './ArchiveSource.ts';\nimport { decodeBcj2Multi, getCodec, getCodecName, isBcj2Codec, isCodecSupported } from './codecs/index.ts';\n\n// Use native streams when available, readable-stream only for Node 0.x\nconst major = +process.versions.node.split('.')[0];\nlet PassThrough: typeof Stream.PassThrough;\nif (major > 0) {\n PassThrough = Stream.PassThrough;\n} else {\n PassThrough = require('readable-stream').PassThrough;\n}\ntype Readable = Stream.Readable;\n\nimport { type CodedError, createCodedError, ErrorCode, FileAttribute, PropertyId, SIGNATURE_HEADER_SIZE } from './constants.ts';\nimport { type FileInfo, parseEncodedHeader, parseHeaderContent, parseSignatureHeader, type SignatureHeader, type StreamsInfo } from './headers.ts';\nimport { readNumber } from './NumberCodec.ts';\n\n// Re-export for backwards compatibility\nexport { type ArchiveSource, BufferSource, FileSource } from './ArchiveSource.ts';\n\n// Callback type for async operations\ntype DecompressCallback = (err: Error | null, data?: Buffer) => void;\n\n// Entry type for iteration\nexport interface SevenZipEntry {\n name: string;\n path: string;\n type: 'file' | 'directory' | 'link';\n size: number;\n mtime?: Date;\n atime?: Date;\n ctime?: Date;\n mode?: number;\n isAntiFile: boolean;\n // Internal tracking\n _folderIndex: number;\n _streamIndex: number; // Global stream index\n _streamIndexInFolder: number; // Stream index within folder (for solid archives)\n _hasStream: boolean;\n _crc?: number; // Expected CRC32 for this file (if present in archive)\n}\n\n/**\n * SevenZipParser - parses 7z archives and provides entry iteration\n */\nexport class SevenZipParser {\n private source: ArchiveSource;\n private signature: SignatureHeader | null = null;\n private streamsInfo: StreamsInfo | null = null;\n private filesInfo: FileInfo[] = [];\n private entries: SevenZipEntry[] = [];\n private parsed = false;\n // Smart cache for decompressed solid blocks\n // Only caches when multiple files share a block, releases when last file extracted\n private decompressedCache: { [key: number]: Buffer } = {};\n // Track files per folder and how many have been extracted\n private filesPerFolder: { [key: number]: number } = {};\n private extractedPerFolder: { [key: number]: number } = {};\n\n constructor(source: ArchiveSource) {\n this.source = source;\n }\n\n /**\n * Parse the archive structure\n * Must be called before iterating entries\n */\n parse(): void {\n if (this.parsed) return;\n\n // Read signature header\n const sigBuf = this.source.read(0, SIGNATURE_HEADER_SIZE);\n if (sigBuf.length < SIGNATURE_HEADER_SIZE) {\n throw createCodedError('Archive too small', ErrorCode.TRUNCATED_ARCHIVE);\n }\n\n this.signature = parseSignatureHeader(sigBuf);\n\n // Read encoded header\n const headerOffset = SIGNATURE_HEADER_SIZE + this.signature.nextHeaderOffset;\n const headerBuf = this.source.read(headerOffset, this.signature.nextHeaderSize);\n\n if (headerBuf.length < this.signature.nextHeaderSize) {\n throw createCodedError('Truncated header', ErrorCode.TRUNCATED_ARCHIVE);\n }\n\n // Parse encoded header (may need decompression)\n try {\n const headerResult = parseEncodedHeader(headerBuf, this.signature.nextHeaderCRC);\n this.streamsInfo = headerResult.streamsInfo || null;\n this.filesInfo = headerResult.filesInfo;\n } catch (err: unknown) {\n const codedErr = err as CodedError;\n if (codedErr && codedErr.code === ErrorCode.COMPRESSED_HEADER) {\n // Header is compressed - need to decompress first\n this.handleCompressedHeader(headerBuf);\n } else {\n throw err;\n }\n }\n\n // Build entries list\n this.buildEntries();\n this.parsed = true;\n }\n\n /**\n * Handle compressed header (kEncodedHeader)\n */\n private handleCompressedHeader(headerBuf: Buffer): void {\n // Parse the encoded header info to get decompression parameters\n let offset = 1; // Skip kEncodedHeader byte\n\n // Should have StreamsInfo for the header itself\n const propertyId = headerBuf[offset++];\n if (propertyId !== PropertyId.kMainStreamsInfo && propertyId !== PropertyId.kPackInfo) {\n throw createCodedError('Expected StreamsInfo in encoded header', ErrorCode.CORRUPT_HEADER);\n }\n\n // For now, we parse the streams info from the encoded header block\n // This tells us how to decompress the actual header\n\n // Read pack info from the encoded header structure\n const packInfoResult = this.parseEncodedHeaderStreams(headerBuf, 1);\n\n // Calculate compressed header position\n // For simple archives: header is at SIGNATURE_HEADER_SIZE + packPos\n // For BCJ2/complex archives: header may be at the END of pack data area\n // The pack data area ends at nextHeaderOffset (where encoded header starts)\n const compressedStart = SIGNATURE_HEADER_SIZE + packInfoResult.packPos;\n const compressedData = this.source.read(compressedStart, packInfoResult.packSize);\n\n // Decompress using the specified codec\n const codec = getCodec(packInfoResult.codecId);\n let decompressedHeader: Buffer | null = null;\n\n // Try decompressing from the calculated position first\n try {\n decompressedHeader = codec.decode(compressedData, packInfoResult.properties, packInfoResult.unpackSize);\n // Verify CRC if present\n if (packInfoResult.unpackCRC !== undefined) {\n const actualCRC = crc32(decompressedHeader);\n if (actualCRC !== packInfoResult.unpackCRC) {\n decompressedHeader = null; // CRC mismatch, need to search\n }\n }\n } catch {\n decompressedHeader = null; // Decompression failed, need to search\n }\n\n // If initial decompression failed, search for the correct position as a fallback\n // This handles edge cases where packPos doesn't point directly to header pack data\n if (decompressedHeader === null && this.signature) {\n const packAreaEnd = SIGNATURE_HEADER_SIZE + this.signature.nextHeaderOffset;\n const searchStart = packAreaEnd - packInfoResult.packSize;\n const searchEnd = Math.max(SIGNATURE_HEADER_SIZE, compressedStart - 100000);\n\n // Scan for LZMA data starting with 0x00 (range coder init)\n // Try each candidate and validate with CRC\n const scanChunkSize = 4096;\n searchLoop: for (let chunkStart = searchStart; chunkStart >= searchEnd; chunkStart -= scanChunkSize) {\n const chunk = this.source.read(chunkStart, scanChunkSize + packInfoResult.packSize);\n for (let i = 0; i < Math.min(chunk.length, scanChunkSize); i++) {\n if (chunk[i] === 0x00) {\n const candidateData = chunk.subarray(i, i + packInfoResult.packSize);\n if (candidateData.length === packInfoResult.packSize) {\n try {\n const candidateDecompressed = codec.decode(candidateData, packInfoResult.properties, packInfoResult.unpackSize);\n if (packInfoResult.unpackCRC !== undefined) {\n const candCRC = crc32(candidateDecompressed);\n if (candCRC === packInfoResult.unpackCRC) {\n decompressedHeader = candidateDecompressed;\n break searchLoop;\n }\n } else {\n decompressedHeader = candidateDecompressed;\n break searchLoop;\n }\n } catch {\n // Decompression failed, continue searching\n }\n }\n }\n }\n }\n }\n\n if (decompressedHeader === null) {\n throw createCodedError('Failed to decompress header - could not find valid LZMA data', ErrorCode.CORRUPT_HEADER);\n }\n\n // Now parse the decompressed header\n // It should start with kHeader\n let decompOffset = 0;\n const headerId = decompressedHeader[decompOffset++];\n if (headerId !== PropertyId.kHeader) {\n throw createCodedError('Expected kHeader in decompressed header', ErrorCode.CORRUPT_HEADER);\n }\n\n // Parse the decompressed header using shared function from headers.ts\n const result = parseHeaderContent(decompressedHeader, decompOffset);\n this.streamsInfo = result.streamsInfo || null;\n this.filesInfo = result.filesInfo;\n }\n\n /**\n * Parse streams info from encoded header block\n * This is a simplified parser for the header's own compression info\n */\n private parseEncodedHeaderStreams(\n buf: Buffer,\n offset: number\n ): {\n packPos: number;\n packSize: number;\n unpackSize: number;\n codecId: number[];\n properties?: Buffer;\n unpackCRC?: number;\n } {\n // This is a simplified parser for the encoded header's own streams info\n let packPos = 0;\n let packSize = 0;\n let unpackSize = 0;\n let codecId: number[] = [];\n let properties: Buffer | undefined;\n let unpackCRC: number | undefined;\n\n while (offset < buf.length) {\n const propertyId = buf[offset++];\n\n if (propertyId === PropertyId.kEnd) {\n break;\n }\n\n switch (propertyId) {\n case PropertyId.kPackInfo: {\n const packPosResult = readNumber(buf, offset);\n packPos = packPosResult.value;\n offset += packPosResult.bytesRead;\n\n const numPackResult = readNumber(buf, offset);\n offset += numPackResult.bytesRead;\n\n // Read until kEnd\n while (buf[offset] !== PropertyId.kEnd) {\n if (buf[offset] === PropertyId.kSize) {\n offset++;\n const sizeResult = readNumber(buf, offset);\n packSize = sizeResult.value;\n offset += sizeResult.bytesRead;\n } else {\n offset++;\n }\n }\n offset++; // Skip kEnd\n break;\n }\n\n case PropertyId.kUnpackInfo:\n // Find folder/coder info\n while (offset < buf.length && buf[offset] !== PropertyId.kEnd) {\n if (buf[offset] === PropertyId.kFolder) {\n offset++;\n const numFoldersResult = readNumber(buf, offset);\n offset += numFoldersResult.bytesRead;\n offset++; // external flag\n\n // Parse coder\n const numCodersResult = readNumber(buf, offset);\n offset += numCodersResult.bytesRead;\n\n const flags = buf[offset++];\n const idSize = flags & 0x0f;\n const hasAttributes = (flags & 0x20) !== 0;\n\n codecId = [];\n for (let i = 0; i < idSize; i++) {\n codecId.push(buf[offset++]);\n }\n\n if (hasAttributes) {\n const propsLenResult = readNumber(buf, offset);\n offset += propsLenResult.bytesRead;\n properties = buf.slice(offset, offset + propsLenResult.value);\n offset += propsLenResult.value;\n }\n } else if (buf[offset] === PropertyId.kCodersUnpackSize) {\n offset++;\n // Read unpack size - needed for LZMA decoder\n const unpackSizeResult = readNumber(buf, offset);\n unpackSize = unpackSizeResult.value;\n offset += unpackSizeResult.bytesRead;\n } else if (buf[offset] === PropertyId.kCRC) {\n offset++;\n const allDefined = buf[offset++];\n if (allDefined) {\n unpackCRC = buf.readUInt32LE(offset);\n offset += 4;\n }\n } else {\n offset++;\n }\n }\n if (buf[offset] === PropertyId.kEnd) offset++;\n break;\n }\n }\n\n return { packPos: packPos, packSize: packSize, unpackSize: unpackSize, codecId: codecId, properties: properties, unpackCRC: unpackCRC };\n }\n\n /**\n * Build the entries list from parsed file info\n */\n private buildEntries(): void {\n this.entries = [];\n\n if (!this.streamsInfo) {\n // No streams info - just create entries from file info\n for (let i = 0; i < this.filesInfo.length; i++) {\n const file = this.filesInfo[i];\n this.entries.push(this.createEntry(file, 0, 0, 0));\n }\n return;\n }\n\n // Use the properly parsed numUnpackStreamsPerFolder from the archive header\n const streamsPerFolder = this.streamsInfo.numUnpackStreamsPerFolder;\n\n // Initialize files per folder count (for smart caching)\n for (let f = 0; f < streamsPerFolder.length; f++) {\n this.filesPerFolder[f] = streamsPerFolder[f];\n this.extractedPerFolder[f] = 0;\n }\n\n // Now build entries with proper folder/stream tracking\n let streamIndex = 0;\n let folderIndex = 0;\n let streamInFolder = 0;\n let folderStreamCount = streamsPerFolder[0] || 0;\n\n for (let j = 0; j < this.filesInfo.length; j++) {\n const fileInfo = this.filesInfo[j];\n\n // Get size from unpackSizes for files with streams\n let size = 0;\n if (fileInfo.hasStream && streamIndex < this.streamsInfo.unpackSizes.length) {\n size = this.streamsInfo.unpackSizes[streamIndex];\n }\n\n const entry = this.createEntry(fileInfo, size, folderIndex, streamInFolder);\n entry._streamIndex = streamIndex;\n // Set CRC if available\n if (fileInfo.hasStream && this.streamsInfo.unpackCRCs && this.streamsInfo.unpackCRCs[streamIndex] !== undefined) {\n entry._crc = this.streamsInfo.unpackCRCs[streamIndex];\n }\n this.entries.push(entry);\n\n // Advance stream tracking for files with streams\n if (fileInfo.hasStream) {\n streamIndex++;\n streamInFolder++;\n\n // Check if we've exhausted streams in this folder\n if (streamInFolder >= folderStreamCount) {\n folderIndex++;\n streamInFolder = 0;\n folderStreamCount = streamsPerFolder[folderIndex] || 0;\n }\n }\n }\n }\n\n /**\n * Create an entry from file info\n */\n private createEntry(file: FileInfo, size: number, folderIndex: number, streamInFolder: number): SevenZipEntry {\n // Determine entry type\n // Note: 7z format doesn't natively support symlinks. p7zip with -snl stores\n // symlinks as regular files with the target path as content.\n let type: 'file' | 'directory' | 'link' = 'file';\n if (file.isDirectory) {\n type = 'directory';\n }\n\n // Calculate mode from Windows attributes\n let mode: number | undefined;\n if (file.attributes !== undefined) {\n // Check for Unix extension bit\n if ((file.attributes & FileAttribute.UNIX_EXTENSION) !== 0) {\n mode = (file.attributes >>> 16) & 0xffff;\n // Check for symlink (S_IFLNK = 0xA000)\n // Note: Most 7z implementations don't preserve symlink mode bits\n if ((mode & 0xf000) === 0xa000) {\n type = 'link';\n }\n } else if (file.isDirectory) {\n mode = 493; // 0o755\n } else {\n mode = 420; // 0o644\n }\n }\n\n return {\n name: getBaseName(file.name),\n path: file.name,\n type: type,\n size: size,\n mtime: file.mtime,\n atime: file.atime,\n ctime: file.ctime,\n mode: mode,\n isAntiFile: file.isAntiFile,\n _folderIndex: folderIndex,\n _streamIndex: 0, // Set by caller\n _streamIndexInFolder: streamInFolder,\n _hasStream: file.hasStream,\n };\n }\n\n /**\n * Get the list of entries\n */\n getEntries(): SevenZipEntry[] {\n if (!this.parsed) {\n this.parse();\n }\n return this.entries;\n }\n\n /**\n * Get a readable stream for an entry's content\n */\n getEntryStream(entry: SevenZipEntry): Readable {\n if (!entry._hasStream || entry.type === 'directory') {\n // Return empty stream for directories and empty files\n const emptyStream = new PassThrough();\n emptyStream.end();\n return emptyStream;\n }\n\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n // Get folder info\n const folder = this.streamsInfo.folders[entry._folderIndex];\n if (!folder) {\n throw createCodedError('Invalid folder index', ErrorCode.CORRUPT_HEADER);\n }\n\n // Check codec support\n for (let i = 0; i < folder.coders.length; i++) {\n const coder = folder.coders[i];\n if (!isCodecSupported(coder.id)) {\n const codecName = getCodecName(coder.id);\n throw createCodedError(`Unsupported codec: ${codecName}`, ErrorCode.UNSUPPORTED_CODEC);\n }\n }\n\n // Get decompressed data for this folder (with smart caching)\n const folderIdx = entry._folderIndex;\n const data = this.getDecompressedFolder(folderIdx);\n\n // Calculate file offset within the decompressed block\n // For solid archives, multiple files are concatenated in the block\n let fileStart = 0;\n for (let m = 0; m < entry._streamIndexInFolder; m++) {\n // Sum sizes of all streams before this one in the folder\n const prevStreamGlobalIndex = entry._streamIndex - entry._streamIndexInFolder + m;\n fileStart += this.streamsInfo.unpackSizes[prevStreamGlobalIndex];\n }\n\n const fileSize = entry.size;\n\n // Create a PassThrough stream with the file data\n const outputStream = new PassThrough();\n\n // Bounds check to prevent \"oob\" error on older Node versions\n if (fileStart + fileSize > data.length) {\n throw createCodedError(`File data out of bounds: offset ${fileStart} + size ${fileSize} > decompressed length ${data.length}`, ErrorCode.DECOMPRESSION_FAILED);\n }\n\n const fileData = data.slice(fileStart, fileStart + fileSize);\n\n // Verify CRC if present\n if (entry._crc !== undefined) {\n const actualCRC = crc32(fileData);\n if (actualCRC !== entry._crc) {\n throw createCodedError(`CRC mismatch for ${entry.path}: expected ${entry._crc.toString(16)}, got ${actualCRC.toString(16)}`, ErrorCode.CRC_MISMATCH);\n }\n }\n\n outputStream.end(fileData);\n\n // Track extraction and release cache when all files from this folder are done\n this.extractedPerFolder[folderIdx] = (this.extractedPerFolder[folderIdx] || 0) + 1;\n if (this.extractedPerFolder[folderIdx] >= this.filesPerFolder[folderIdx]) {\n // All files from this folder extracted, release cache\n delete this.decompressedCache[folderIdx];\n }\n\n return outputStream;\n }\n\n /**\n * Get a readable stream for an entry's content (callback-based async version)\n * Uses streaming decompression for non-blocking I/O\n */\n getEntryStreamAsync(entry: SevenZipEntry, callback: (err: Error | null, stream?: Readable) => void): void {\n if (!entry._hasStream || entry.type === 'directory') {\n // Return empty stream for directories and empty files\n const emptyStream = new PassThrough();\n emptyStream.end();\n callback(null, emptyStream);\n return;\n }\n\n if (!this.streamsInfo) {\n callback(createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n // Get folder info\n const folder = this.streamsInfo.folders[entry._folderIndex];\n if (!folder) {\n callback(createCodedError('Invalid folder index', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n // Check codec support\n for (let i = 0; i < folder.coders.length; i++) {\n const coder = folder.coders[i];\n if (!isCodecSupported(coder.id)) {\n const codecName = getCodecName(coder.id);\n callback(createCodedError(`Unsupported codec: ${codecName}`, ErrorCode.UNSUPPORTED_CODEC));\n return;\n }\n }\n\n // Get decompressed data for this folder using async method\n const folderIdx = entry._folderIndex;\n const streamsInfo = this.streamsInfo;\n\n this.getDecompressedFolderAsync(folderIdx, (err, data) => {\n if (err) return callback(err);\n if (!data) return callback(new Error('No data returned from decompression'));\n\n // Calculate file offset within the decompressed block\n let fileStart = 0;\n for (let m = 0; m < entry._streamIndexInFolder; m++) {\n const prevStreamGlobalIndex = entry._streamIndex - entry._streamIndexInFolder + m;\n fileStart += streamsInfo.unpackSizes[prevStreamGlobalIndex];\n }\n\n const fileSize = entry.size;\n\n // Bounds check\n if (fileStart + fileSize > data.length) {\n return callback(createCodedError(`File data out of bounds: offset ${fileStart} + size ${fileSize} > decompressed length ${data.length}`, ErrorCode.DECOMPRESSION_FAILED));\n }\n\n // Create a PassThrough stream with the file data\n const outputStream = new PassThrough();\n const fileData = data.slice(fileStart, fileStart + fileSize);\n\n // Verify CRC if present\n if (entry._crc !== undefined) {\n const actualCRC = crc32(fileData);\n if (actualCRC !== entry._crc) {\n return callback(createCodedError(`CRC mismatch for ${entry.path}: expected ${entry._crc.toString(16)}, got ${actualCRC.toString(16)}`, ErrorCode.CRC_MISMATCH));\n }\n }\n\n outputStream.end(fileData);\n\n // Track extraction and release cache when all files from this folder are done\n this.extractedPerFolder[folderIdx] = (this.extractedPerFolder[folderIdx] || 0) + 1;\n if (this.extractedPerFolder[folderIdx] >= this.filesPerFolder[folderIdx]) {\n delete this.decompressedCache[folderIdx];\n }\n\n callback(null, outputStream);\n });\n }\n\n /**\n * Check if a folder uses BCJ2 codec\n */\n private folderHasBcj2(folder: { coders: { id: number[] }[] }): boolean {\n for (let i = 0; i < folder.coders.length; i++) {\n if (isBcj2Codec(folder.coders[i].id)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Get decompressed data for a folder, with smart caching for solid archives\n * Only caches when multiple files share a block, releases when last file extracted\n */\n private getDecompressedFolder(folderIndex: number): Buffer {\n // Check cache first\n if (this.decompressedCache[folderIndex]) {\n return this.decompressedCache[folderIndex];\n }\n\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Check how many files remain in this folder\n const filesInFolder = this.filesPerFolder[folderIndex] || 1;\n const extractedFromFolder = this.extractedPerFolder[folderIndex] || 0;\n const remainingFiles = filesInFolder - extractedFromFolder;\n // Only cache if more than 1 file remains (including the current one being extracted)\n const shouldCache = remainingFiles > 1;\n\n // Check if this folder uses BCJ2 (requires special multi-stream handling)\n if (this.folderHasBcj2(folder)) {\n const data = this.decompressBcj2Folder(folderIndex);\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data;\n }\n return data;\n }\n\n // Calculate packed data position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream this folder uses\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position of this pack stream\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n const packSize = this.streamsInfo.packSizes[packStreamIndex];\n\n // Read packed data\n const packedData = this.source.read(packPos, packSize);\n\n // Decompress through codec chain\n let data2 = packedData;\n for (let l = 0; l < folder.coders.length; l++) {\n const coderInfo = folder.coders[l];\n const codec = getCodec(coderInfo.id);\n // Get unpack size for this coder (needed by LZMA)\n const unpackSize = folder.unpackSizes[l];\n data2 = codec.decode(data2, coderInfo.properties, unpackSize);\n }\n\n // Cache only if more files remain in this folder\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data2;\n }\n\n return data2;\n }\n\n /**\n * Get decompressed data for a folder using streaming (callback-based async)\n * Uses createDecoder() streams for non-blocking decompression\n */\n private getDecompressedFolderAsync(folderIndex: number, callback: DecompressCallback): void {\n const self = this;\n\n // Check cache first\n if (this.decompressedCache[folderIndex]) {\n callback(null, this.decompressedCache[folderIndex]);\n return;\n }\n\n if (!this.streamsInfo) {\n callback(createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Check how many files remain in this folder\n const filesInFolder = this.filesPerFolder[folderIndex] || 1;\n const extractedFromFolder = this.extractedPerFolder[folderIndex] || 0;\n const remainingFiles = filesInFolder - extractedFromFolder;\n const shouldCache = remainingFiles > 1;\n\n // BCJ2 requires special handling - use sync version for now\n // TODO: Add async BCJ2 support\n if (this.folderHasBcj2(folder)) {\n try {\n const data = this.decompressBcj2Folder(folderIndex);\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data;\n }\n callback(null, data);\n } catch (err) {\n callback(err as Error);\n }\n return;\n }\n\n // Calculate packed data position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream this folder uses\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position of this pack stream\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n const packSize = this.streamsInfo.packSizes[packStreamIndex];\n\n // Read packed data\n const packedData = this.source.read(packPos, packSize);\n\n // Create decoder stream chain and decompress\n const coders = folder.coders;\n const unpackSizes = folder.unpackSizes;\n\n // Helper to decompress through a single codec stream\n function decompressWithStream(input: Buffer, coderIdx: number, cb: DecompressCallback): void {\n const coderInfo = coders[coderIdx];\n const codec = getCodec(coderInfo.id);\n const decoder = codec.createDecoder(coderInfo.properties, unpackSizes[coderIdx]);\n\n const chunks: Buffer[] = [];\n let errorOccurred = false;\n\n decoder.on('data', (chunk: Buffer) => {\n chunks.push(chunk);\n });\n\n oo(decoder, ['error', 'end', 'close', 'finish'], (err?: Error) => {\n if (errorOccurred) return;\n if (err) {\n errorOccurred = true;\n return cb(err);\n }\n cb(null, Buffer.concat(chunks));\n });\n\n // Write input data to decoder and signal end\n decoder.end(input);\n }\n\n // Chain decompression through all codecs\n function decompressChain(input: Buffer, idx: number): void {\n if (idx >= coders.length) {\n // All done - cache and return\n if (shouldCache) {\n self.decompressedCache[folderIndex] = input;\n }\n callback(null, input);\n return;\n }\n\n decompressWithStream(input, idx, (err, output) => {\n if (err) {\n callback(err);\n return;\n }\n decompressChain(output as Buffer, idx + 1);\n });\n }\n\n // Start the chain\n decompressChain(packedData, 0);\n }\n\n /**\n * Decompress a BCJ2 folder with multi-stream handling\n * BCJ2 uses 4 input streams: main, call, jump, range coder\n */\n private decompressBcj2Folder(folderIndex: number): Buffer {\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Calculate starting pack position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream index this folder starts at\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n // Read all pack streams for this folder\n const numPackStreams = folder.packedStreams.length;\n const packStreams: Buffer[] = [];\n let currentPos = packPos;\n\n for (let p = 0; p < numPackStreams; p++) {\n const size = this.streamsInfo.packSizes[packStreamIndex + p];\n packStreams.push(this.source.read(currentPos, size));\n currentPos += size;\n }\n\n // Build a map of coder outputs\n // For BCJ2, typical structure is:\n // Coder 0: LZMA2 (main stream) - 1 in, 1 out\n // Coder 1: LZMA (call stream) - 1 in, 1 out\n // Coder 2: LZMA (jump stream) - 1 in, 1 out\n // Coder 3: BCJ2 - 4 in, 1 out\n // Pack streams map to: coder inputs not bound to other coder outputs\n\n // First, decompress each non-BCJ2 coder\n const coderOutputs: { [key: number]: Buffer } = {};\n\n // Find the BCJ2 coder\n let bcj2CoderIndex = -1;\n for (let c = 0; c < folder.coders.length; c++) {\n if (isBcj2Codec(folder.coders[c].id)) {\n bcj2CoderIndex = c;\n break;\n }\n }\n\n if (bcj2CoderIndex === -1) {\n throw createCodedError('BCJ2 coder not found in folder', ErrorCode.CORRUPT_HEADER);\n }\n\n // Build input stream index -> pack stream mapping\n // folder.packedStreams tells us which input indices are unbound and their order\n const inputToPackStream: { [key: number]: number } = {};\n for (let pi = 0; pi < folder.packedStreams.length; pi++) {\n inputToPackStream[folder.packedStreams[pi]] = pi;\n }\n\n // Build output stream index -> coder mapping\n const outputToCoder: { [key: number]: number } = {};\n let totalOutputs = 0;\n for (let co = 0; co < folder.coders.length; co++) {\n const numOut = folder.coders[co].numOutStreams;\n for (let outp = 0; outp < numOut; outp++) {\n outputToCoder[totalOutputs + outp] = co;\n }\n totalOutputs += numOut;\n }\n\n // Decompress non-BCJ2 coders (LZMA, LZMA2)\n // We need to process in dependency order\n const processed: { [key: number]: boolean } = {};\n\n const processOrder = this.getCoderProcessOrder(folder, bcj2CoderIndex);\n\n for (let po = 0; po < processOrder.length; po++) {\n const coderIdx = processOrder[po];\n if (coderIdx === bcj2CoderIndex) continue;\n\n const coder = folder.coders[coderIdx];\n const codec = getCodec(coder.id);\n\n // Find input for this coder\n let coderInputStart = 0;\n for (let ci2 = 0; ci2 < coderIdx; ci2++) {\n coderInputStart += folder.coders[ci2].numInStreams;\n }\n\n // Get input data (from pack stream)\n const inputIdx = coderInputStart;\n const packStreamIdx = inputToPackStream[inputIdx];\n const inputData = packStreams[packStreamIdx];\n\n // Decompress\n const unpackSize = folder.unpackSizes[coderIdx];\n const outputData = codec.decode(inputData, coder.properties, unpackSize);\n\n // Store in coder outputs\n let coderOutputStart = 0;\n for (let co2 = 0; co2 < coderIdx; co2++) {\n coderOutputStart += folder.coders[co2].numOutStreams;\n }\n coderOutputs[coderOutputStart] = outputData;\n processed[coderIdx] = true;\n }\n\n // Now process BCJ2\n // BCJ2 has 4 inputs, need to map them correctly\n // Standard order: main(LZMA2 output), call(LZMA output), jump(LZMA output), range(raw pack)\n let bcj2InputStart = 0;\n for (let ci3 = 0; ci3 < bcj2CoderIndex; ci3++) {\n bcj2InputStart += folder.coders[ci3].numInStreams;\n }\n\n const bcj2Inputs: Buffer[] = [];\n for (let bi = 0; bi < 4; bi++) {\n const globalIdx = bcj2InputStart + bi;\n\n // Check if this input is bound to a coder output\n let boundOutput = -1;\n for (let bp2 = 0; bp2 < folder.bindPairs.length; bp2++) {\n if (folder.bindPairs[bp2].inIndex === globalIdx) {\n boundOutput = folder.bindPairs[bp2].outIndex;\n break;\n }\n }\n\n if (boundOutput >= 0) {\n // Get from coder outputs\n bcj2Inputs.push(coderOutputs[boundOutput]);\n } else {\n // Get from pack streams\n const psIdx = inputToPackStream[globalIdx];\n bcj2Inputs.push(packStreams[psIdx]);\n }\n }\n\n // Get BCJ2 unpack size\n let bcj2OutputStart = 0;\n for (let co3 = 0; co3 < bcj2CoderIndex; co3++) {\n bcj2OutputStart += folder.coders[co3].numOutStreams;\n }\n const bcj2UnpackSize = folder.unpackSizes[bcj2OutputStart];\n\n // Memory optimization: Clear intermediate buffers to help GC\n // These are no longer needed after bcj2Inputs is built\n for (const key in coderOutputs) {\n delete coderOutputs[key];\n }\n // Clear packStreams array (allows GC to free compressed data)\n packStreams.length = 0;\n\n // Decode BCJ2\n return decodeBcj2Multi(bcj2Inputs, undefined, bcj2UnpackSize);\n }\n\n /**\n * Get processing order for coders (dependency order)\n */\n private getCoderProcessOrder(folder: { coders: { numInStreams: number; numOutStreams: number }[]; bindPairs: { inIndex: number; outIndex: number }[] }, excludeIdx: number): number[] {\n const order: number[] = [];\n const processed: { [key: number]: boolean } = {};\n\n // Simple approach: process coders that don't depend on unprocessed outputs\n let changed = true;\n while (changed) {\n changed = false;\n for (let c = 0; c < folder.coders.length; c++) {\n if (processed[c] || c === excludeIdx) continue;\n\n // Check if all inputs are satisfied\n let inputStart = 0;\n for (let i = 0; i < c; i++) {\n inputStart += folder.coders[i].numInStreams;\n }\n\n let canProcess = true;\n for (let inp = 0; inp < folder.coders[c].numInStreams; inp++) {\n const globalIdx = inputStart + inp;\n // Check if bound to an unprocessed coder\n for (let bp = 0; bp < folder.bindPairs.length; bp++) {\n if (folder.bindPairs[bp].inIndex === globalIdx) {\n // Find which coder produces this output\n const outIdx = folder.bindPairs[bp].outIndex;\n let outStart = 0;\n for (let oc = 0; oc < folder.coders.length; oc++) {\n const numOut = folder.coders[oc].numOutStreams;\n if (outIdx < outStart + numOut) {\n if (!processed[oc] && oc !== excludeIdx) {\n canProcess = false;\n }\n break;\n }\n outStart += numOut;\n }\n }\n }\n }\n\n if (canProcess) {\n order.push(c);\n processed[c] = true;\n changed = true;\n }\n }\n }\n\n return order;\n }\n\n /**\n * Close the parser and release resources\n */\n close(): void {\n if (this.source) {\n this.source.close();\n }\n }\n}\n\n/**\n * Get base name from a path\n */\nfunction getBaseName(path: string): string {\n const lastSlash = path.lastIndexOf('/');\n const lastBackslash = path.lastIndexOf('\\\\');\n const lastSep = Math.max(lastSlash, lastBackslash);\n return lastSep >= 0 ? path.slice(lastSep + 1) : path;\n}\n"],"names":["BufferSource","FileSource","SevenZipParser","major","process","versions","node","split","PassThrough","Stream","require","source","signature","streamsInfo","filesInfo","entries","parsed","decompressedCache","filesPerFolder","extractedPerFolder","parse","sigBuf","read","SIGNATURE_HEADER_SIZE","length","createCodedError","ErrorCode","TRUNCATED_ARCHIVE","parseSignatureHeader","headerOffset","nextHeaderOffset","headerBuf","nextHeaderSize","headerResult","parseEncodedHeader","nextHeaderCRC","err","codedErr","code","COMPRESSED_HEADER","handleCompressedHeader","buildEntries","offset","propertyId","PropertyId","kMainStreamsInfo","kPackInfo","CORRUPT_HEADER","packInfoResult","parseEncodedHeaderStreams","compressedStart","packPos","compressedData","packSize","codec","getCodec","codecId","decompressedHeader","decode","properties","unpackSize","unpackCRC","undefined","actualCRC","crc32","packAreaEnd","searchStart","searchEnd","Math","max","scanChunkSize","searchLoop","chunkStart","chunk","i","min","candidateData","subarray","candidateDecompressed","candCRC","decompOffset","headerId","kHeader","result","parseHeaderContent","buf","kEnd","packPosResult","readNumber","value","bytesRead","numPackResult","kSize","sizeResult","kUnpackInfo","kFolder","numFoldersResult","numCodersResult","flags","idSize","hasAttributes","push","propsLenResult","slice","kCodersUnpackSize","unpackSizeResult","kCRC","allDefined","readUInt32LE","file","createEntry","streamsPerFolder","numUnpackStreamsPerFolder","f","streamIndex","folderIndex","streamInFolder","folderStreamCount","j","fileInfo","size","hasStream","unpackSizes","entry","_streamIndex","unpackCRCs","_crc","type","isDirectory","mode","attributes","FileAttribute","UNIX_EXTENSION","name","getBaseName","path","mtime","atime","ctime","isAntiFile","_folderIndex","_streamIndexInFolder","_hasStream","getEntries","getEntryStream","emptyStream","end","folder","folders","coders","coder","isCodecSupported","id","codecName","getCodecName","UNSUPPORTED_CODEC","folderIdx","data","getDecompressedFolder","fileStart","m","prevStreamGlobalIndex","fileSize","outputStream","DECOMPRESSION_FAILED","fileData","toString","CRC_MISMATCH","getEntryStreamAsync","callback","getDecompressedFolderAsync","Error","folderHasBcj2","isBcj2Codec","filesInFolder","extractedFromFolder","remainingFiles","shouldCache","decompressBcj2Folder","packStreamIndex","packedStreams","k","packSizes","packedData","data2","l","coderInfo","self","decompressWithStream","input","coderIdx","cb","decoder","createDecoder","chunks","errorOccurred","on","oo","Buffer","concat","decompressChain","idx","output","numPackStreams","packStreams","currentPos","p","coderOutputs","bcj2CoderIndex","c","inputToPackStream","pi","outputToCoder","totalOutputs","co","numOut","numOutStreams","outp","processed","processOrder","getCoderProcessOrder","po","coderInputStart","ci2","numInStreams","inputIdx","packStreamIdx","inputData","outputData","coderOutputStart","co2","bcj2InputStart","ci3","bcj2Inputs","bi","globalIdx","boundOutput","bp2","bindPairs","inIndex","outIndex","psIdx","bcj2OutputStart","co3","bcj2UnpackSize","key","decodeBcj2Multi","excludeIdx","order","changed","inputStart","canProcess","inp","bp","outIdx","outStart","oc","close","lastSlash","lastIndexOf","lastBackslash","lastSep"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;CAkBC;;;;;;;;;;;QAuB4BA;eAAAA,6BAAY;;QAAEC;eAAAA,2BAAU;;QA2BxCC;eAAAA;;;mCAhDS;4DACP;6DACI;uBAEoE;2BAYwB;yBACqB;6BACzG;+BAGkC;;;;;;;;;;;AAf7D,uEAAuE;AACvE,IAAMC,QAAQ,CAACC,QAAQC,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,IAAIC;AACJ,IAAIL,QAAQ,GAAG;IACbK,cAAcC,eAAM,CAACD,WAAW;AAClC,OAAO;IACLA,cAAcE,QAAQ,mBAAmBF,WAAW;AACtD;AAmCO,IAAA,AAAMN,+BAAN;;aAAMA,eAcCS,MAAqB;gCAdtBT;aAEHU,YAAoC;aACpCC,cAAkC;aAClCC,YAAwB,EAAE;aAC1BC,UAA2B,EAAE;aAC7BC,SAAS;QACjB,4CAA4C;QAC5C,mFAAmF;aAC3EC,oBAA+C,CAAC;QACxD,0DAA0D;aAClDC,iBAA4C,CAAC;aAC7CC,qBAAgD,CAAC;QAGvD,IAAI,CAACR,MAAM,GAAGA;;iBAfLT;IAkBX;;;GAGC,GACDkB,OAAAA,KAqCC,GArCDA,SAAAA;QACE,IAAI,IAAI,CAACJ,M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nC,IAAMC,gBAAgB7H,KAAK4H,WAAW,CAAC;IACvC,IAAME,UAAUjM,KAAKC,GAAG,CAAC6L,WAAWE;IACpC,OAAOC,WAAW,IAAI9H,KAAKhC,KAAK,CAAC8J,UAAU,KAAK9H;AAClD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/SevenZipParser.ts"],"sourcesContent":["/**\n * SevenZipParser - Main 7z archive parser\n *\n * Handles reading archive structure and providing file streams.\n *\n * Parser Flow:\n * 1. Read signature header (32 bytes) to get header location\n * 2. Read encoded header from nextHeaderOffset\n * 3. If header is compressed, decompress it first\n * 4. Parse streams info (folder structure, pack positions)\n * 5. Parse files info (names, sizes, attributes)\n * 6. Build entry list for iteration\n *\n * Decompression:\n * - 7z uses \"folders\" as decompression units\n * - Solid archives: multiple files share one folder (decompress once)\n * - Non-solid: one file per folder\n * - Supports LZMA, LZMA2, COPY, BCJ2, and other codecs\n */\n\nimport { crc32 } from 'extract-base-iterator';\nimport oo from 'on-one';\nimport Stream from 'stream';\nimport type { ArchiveSource } from './ArchiveSource.ts';\nimport { decodeBcj2Multi, getCodec, getCodecName, isBcj2Codec, isCodecSupported } from './codecs/index.ts';\n\n// Use native streams when available, readable-stream only for Node 0.x\nconst major = +process.versions.node.split('.')[0];\nlet PassThrough: typeof Stream.PassThrough;\nif (major > 0) {\n PassThrough = Stream.PassThrough;\n} else {\n PassThrough = require('readable-stream').PassThrough;\n}\ntype Readable = Stream.Readable;\n\nimport { type CodedError, createCodedError, ErrorCode, FileAttribute, PropertyId, SIGNATURE_HEADER_SIZE } from './constants.ts';\nimport { type FileInfo, parseEncodedHeader, parseHeaderContent, parseSignatureHeader, type SignatureHeader, type StreamsInfo } from './headers.ts';\nimport { readNumber } from './NumberCodec.ts';\n\n// Re-export for backwards compatibility\nexport { type ArchiveSource, BufferSource, FileSource } from './ArchiveSource.ts';\n\n// Callback type for async operations\ntype DecompressCallback = (err: Error | null, data?: Buffer) => void;\n\n// Entry type for iteration\nexport interface SevenZipEntry {\n name: string;\n path: string;\n type: 'file' | 'directory' | 'link';\n size: number;\n mtime?: Date;\n atime?: Date;\n ctime?: Date;\n mode?: number;\n isAntiFile: boolean;\n // Internal tracking\n _folderIndex: number;\n _streamIndex: number; // Global stream index\n _streamIndexInFolder: number; // Stream index within folder (for solid archives)\n _hasStream: boolean;\n _crc?: number; // Expected CRC32 for this file (if present in archive)\n}\n\n/**\n * SevenZipParser - parses 7z archives and provides entry iteration\n */\nexport class SevenZipParser {\n private source: ArchiveSource;\n private signature: SignatureHeader | null = null;\n private streamsInfo: StreamsInfo | null = null;\n private filesInfo: FileInfo[] = [];\n private entries: SevenZipEntry[] = [];\n private parsed = false;\n // Smart cache for decompressed solid blocks\n // Only caches when multiple files share a block, releases when last file extracted\n private decompressedCache: { [key: number]: Buffer } = {};\n // Track files per folder and how many have been extracted\n private filesPerFolder: { [key: number]: number } = {};\n private extractedPerFolder: { [key: number]: number } = {};\n\n constructor(source: ArchiveSource) {\n this.source = source;\n }\n\n /**\n * Parse the archive structure\n * Must be called before iterating entries\n */\n parse(): void {\n if (this.parsed) return;\n\n // Read signature header\n const sigBuf = this.source.read(0, SIGNATURE_HEADER_SIZE);\n if (sigBuf.length < SIGNATURE_HEADER_SIZE) {\n throw createCodedError('Archive too small', ErrorCode.TRUNCATED_ARCHIVE);\n }\n\n this.signature = parseSignatureHeader(sigBuf);\n\n // Read encoded header\n const headerOffset = SIGNATURE_HEADER_SIZE + this.signature.nextHeaderOffset;\n const headerBuf = this.source.read(headerOffset, this.signature.nextHeaderSize);\n\n if (headerBuf.length < this.signature.nextHeaderSize) {\n throw createCodedError('Truncated header', ErrorCode.TRUNCATED_ARCHIVE);\n }\n\n // Parse encoded header (may need decompression)\n try {\n const headerResult = parseEncodedHeader(headerBuf, this.signature.nextHeaderCRC);\n this.streamsInfo = headerResult.streamsInfo || null;\n this.filesInfo = headerResult.filesInfo;\n } catch (err: unknown) {\n const codedErr = err as CodedError;\n if (codedErr && codedErr.code === ErrorCode.COMPRESSED_HEADER) {\n // Header is compressed - need to decompress first\n this.handleCompressedHeader(headerBuf);\n } else {\n throw err;\n }\n }\n\n // Build entries list\n this.buildEntries();\n this.parsed = true;\n }\n\n /**\n * Handle compressed header (kEncodedHeader)\n */\n private handleCompressedHeader(headerBuf: Buffer): void {\n // Parse the encoded header info to get decompression parameters\n let offset = 1; // Skip kEncodedHeader byte\n\n // Should have StreamsInfo for the header itself\n const propertyId = headerBuf[offset++];\n if (propertyId !== PropertyId.kMainStreamsInfo && propertyId !== PropertyId.kPackInfo) {\n throw createCodedError('Expected StreamsInfo in encoded header', ErrorCode.CORRUPT_HEADER);\n }\n\n // For now, we parse the streams info from the encoded header block\n // This tells us how to decompress the actual header\n\n // Read pack info from the encoded header structure\n const packInfoResult = this.parseEncodedHeaderStreams(headerBuf, 1);\n\n // Calculate compressed header position\n // For simple archives: header is at SIGNATURE_HEADER_SIZE + packPos\n // For BCJ2/complex archives: header may be at the END of pack data area\n // The pack data area ends at nextHeaderOffset (where encoded header starts)\n const compressedStart = SIGNATURE_HEADER_SIZE + packInfoResult.packPos;\n const compressedData = this.source.read(compressedStart, packInfoResult.packSize);\n\n // Decompress using the specified codec\n const codec = getCodec(packInfoResult.codecId);\n let decompressedHeader: Buffer | null = null;\n\n // Try decompressing from the calculated position first\n try {\n decompressedHeader = codec.decode(compressedData, packInfoResult.properties, packInfoResult.unpackSize);\n // Verify CRC if present\n if (packInfoResult.unpackCRC !== undefined) {\n const actualCRC = crc32(decompressedHeader);\n if (actualCRC !== packInfoResult.unpackCRC) {\n decompressedHeader = null; // CRC mismatch, need to search\n }\n }\n } catch {\n decompressedHeader = null; // Decompression failed, need to search\n }\n\n // If initial decompression failed, search for the correct position as a fallback\n // This handles edge cases where packPos doesn't point directly to header pack data\n if (decompressedHeader === null && this.signature) {\n const packAreaEnd = SIGNATURE_HEADER_SIZE + this.signature.nextHeaderOffset;\n const searchStart = packAreaEnd - packInfoResult.packSize;\n const searchEnd = Math.max(SIGNATURE_HEADER_SIZE, compressedStart - 100000);\n\n // Scan for LZMA data starting with 0x00 (range coder init)\n // Try each candidate and validate with CRC\n const scanChunkSize = 4096;\n searchLoop: for (let chunkStart = searchStart; chunkStart >= searchEnd; chunkStart -= scanChunkSize) {\n const chunk = this.source.read(chunkStart, scanChunkSize + packInfoResult.packSize);\n for (let i = 0; i < Math.min(chunk.length, scanChunkSize); i++) {\n if (chunk[i] === 0x00) {\n const candidateData = chunk.subarray(i, i + packInfoResult.packSize);\n if (candidateData.length === packInfoResult.packSize) {\n try {\n const candidateDecompressed = codec.decode(candidateData, packInfoResult.properties, packInfoResult.unpackSize);\n if (packInfoResult.unpackCRC !== undefined) {\n const candCRC = crc32(candidateDecompressed);\n if (candCRC === packInfoResult.unpackCRC) {\n decompressedHeader = candidateDecompressed;\n break searchLoop;\n }\n } else {\n decompressedHeader = candidateDecompressed;\n break searchLoop;\n }\n } catch {\n // Decompression failed, continue searching\n }\n }\n }\n }\n }\n }\n\n if (decompressedHeader === null) {\n throw createCodedError('Failed to decompress header - could not find valid LZMA data', ErrorCode.CORRUPT_HEADER);\n }\n\n // Now parse the decompressed header\n // It should start with kHeader\n let decompOffset = 0;\n const headerId = decompressedHeader[decompOffset++];\n if (headerId !== PropertyId.kHeader) {\n throw createCodedError('Expected kHeader in decompressed header', ErrorCode.CORRUPT_HEADER);\n }\n\n // Parse the decompressed header using shared function from headers.ts\n const result = parseHeaderContent(decompressedHeader, decompOffset);\n this.streamsInfo = result.streamsInfo || null;\n this.filesInfo = result.filesInfo;\n }\n\n /**\n * Parse streams info from encoded header block\n * This is a simplified parser for the header's own compression info\n */\n private parseEncodedHeaderStreams(\n buf: Buffer,\n offset: number\n ): {\n packPos: number;\n packSize: number;\n unpackSize: number;\n codecId: number[];\n properties?: Buffer;\n unpackCRC?: number;\n } {\n // This is a simplified parser for the encoded header's own streams info\n let packPos = 0;\n let packSize = 0;\n let unpackSize = 0;\n let codecId: number[] = [];\n let properties: Buffer | undefined;\n let unpackCRC: number | undefined;\n\n while (offset < buf.length) {\n const propertyId = buf[offset++];\n\n if (propertyId === PropertyId.kEnd) {\n break;\n }\n\n switch (propertyId) {\n case PropertyId.kPackInfo: {\n const packPosResult = readNumber(buf, offset);\n packPos = packPosResult.value;\n offset += packPosResult.bytesRead;\n\n const numPackResult = readNumber(buf, offset);\n offset += numPackResult.bytesRead;\n\n // Read until kEnd\n while (buf[offset] !== PropertyId.kEnd) {\n if (buf[offset] === PropertyId.kSize) {\n offset++;\n const sizeResult = readNumber(buf, offset);\n packSize = sizeResult.value;\n offset += sizeResult.bytesRead;\n } else {\n offset++;\n }\n }\n offset++; // Skip kEnd\n break;\n }\n\n case PropertyId.kUnpackInfo:\n // Find folder/coder info\n while (offset < buf.length && buf[offset] !== PropertyId.kEnd) {\n if (buf[offset] === PropertyId.kFolder) {\n offset++;\n const numFoldersResult = readNumber(buf, offset);\n offset += numFoldersResult.bytesRead;\n offset++; // external flag\n\n // Parse coder\n const numCodersResult = readNumber(buf, offset);\n offset += numCodersResult.bytesRead;\n\n const flags = buf[offset++];\n const idSize = flags & 0x0f;\n const hasAttributes = (flags & 0x20) !== 0;\n\n codecId = [];\n for (let i = 0; i < idSize; i++) {\n codecId.push(buf[offset++]);\n }\n\n if (hasAttributes) {\n const propsLenResult = readNumber(buf, offset);\n offset += propsLenResult.bytesRead;\n properties = buf.slice(offset, offset + propsLenResult.value);\n offset += propsLenResult.value;\n }\n } else if (buf[offset] === PropertyId.kCodersUnpackSize) {\n offset++;\n // Read unpack size - needed for LZMA decoder\n const unpackSizeResult = readNumber(buf, offset);\n unpackSize = unpackSizeResult.value;\n offset += unpackSizeResult.bytesRead;\n } else if (buf[offset] === PropertyId.kCRC) {\n offset++;\n const allDefined = buf[offset++];\n if (allDefined) {\n unpackCRC = buf.readUInt32LE(offset);\n offset += 4;\n }\n } else {\n offset++;\n }\n }\n if (buf[offset] === PropertyId.kEnd) offset++;\n break;\n }\n }\n\n return { packPos: packPos, packSize: packSize, unpackSize: unpackSize, codecId: codecId, properties: properties, unpackCRC: unpackCRC };\n }\n\n /**\n * Build the entries list from parsed file info\n */\n private buildEntries(): void {\n this.entries = [];\n\n if (!this.streamsInfo) {\n // No streams info - just create entries from file info\n for (let i = 0; i < this.filesInfo.length; i++) {\n const file = this.filesInfo[i];\n this.entries.push(this.createEntry(file, 0, 0, 0));\n }\n return;\n }\n\n // Use the properly parsed numUnpackStreamsPerFolder from the archive header\n const streamsPerFolder = this.streamsInfo.numUnpackStreamsPerFolder;\n\n // Initialize files per folder count (for smart caching)\n for (let f = 0; f < streamsPerFolder.length; f++) {\n this.filesPerFolder[f] = streamsPerFolder[f];\n this.extractedPerFolder[f] = 0;\n }\n\n // Now build entries with proper folder/stream tracking\n let streamIndex = 0;\n let folderIndex = 0;\n let streamInFolder = 0;\n let folderStreamCount = streamsPerFolder[0] || 0;\n\n for (let j = 0; j < this.filesInfo.length; j++) {\n const fileInfo = this.filesInfo[j];\n\n // Get size from unpackSizes for files with streams\n let size = 0;\n if (fileInfo.hasStream && streamIndex < this.streamsInfo.unpackSizes.length) {\n size = this.streamsInfo.unpackSizes[streamIndex];\n }\n\n const entry = this.createEntry(fileInfo, size, folderIndex, streamInFolder);\n entry._streamIndex = streamIndex;\n // Set CRC if available\n if (fileInfo.hasStream && this.streamsInfo.unpackCRCs && this.streamsInfo.unpackCRCs[streamIndex] !== undefined) {\n entry._crc = this.streamsInfo.unpackCRCs[streamIndex];\n }\n this.entries.push(entry);\n\n // Advance stream tracking for files with streams\n if (fileInfo.hasStream) {\n streamIndex++;\n streamInFolder++;\n\n // Check if we've exhausted streams in this folder\n if (streamInFolder >= folderStreamCount) {\n folderIndex++;\n streamInFolder = 0;\n folderStreamCount = streamsPerFolder[folderIndex] || 0;\n }\n }\n }\n }\n\n /**\n * Create an entry from file info\n */\n private createEntry(file: FileInfo, size: number, folderIndex: number, streamInFolder: number): SevenZipEntry {\n // Determine entry type\n // Note: 7z format doesn't natively support symlinks. p7zip with -snl stores\n // symlinks as regular files with the target path as content.\n let type: 'file' | 'directory' | 'link' = 'file';\n if (file.isDirectory) {\n type = 'directory';\n }\n\n // Calculate mode from Windows attributes\n let mode: number | undefined;\n if (file.attributes !== undefined) {\n // Check for Unix extension bit\n if ((file.attributes & FileAttribute.UNIX_EXTENSION) !== 0) {\n mode = (file.attributes >>> 16) & 0xffff;\n // Check for symlink (S_IFLNK = 0xA000)\n // Note: Most 7z implementations don't preserve symlink mode bits\n if ((mode & 0xf000) === 0xa000) {\n type = 'link';\n }\n } else if (file.isDirectory) {\n mode = 493; // 0o755\n } else {\n mode = 420; // 0o644\n }\n }\n\n return {\n name: getBaseName(file.name),\n path: file.name,\n type: type,\n size: size,\n mtime: file.mtime,\n atime: file.atime,\n ctime: file.ctime,\n mode: mode,\n isAntiFile: file.isAntiFile,\n _folderIndex: folderIndex,\n _streamIndex: 0, // Set by caller\n _streamIndexInFolder: streamInFolder,\n _hasStream: file.hasStream,\n };\n }\n\n /**\n * Get the list of entries\n */\n getEntries(): SevenZipEntry[] {\n if (!this.parsed) {\n this.parse();\n }\n return this.entries;\n }\n\n /**\n * Get a readable stream for an entry's content\n */\n getEntryStream(entry: SevenZipEntry): Readable {\n if (!entry._hasStream || entry.type === 'directory') {\n // Return empty stream for directories and empty files\n const emptyStream = new PassThrough();\n emptyStream.end();\n return emptyStream;\n }\n\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n // Get folder info\n const folder = this.streamsInfo.folders[entry._folderIndex];\n if (!folder) {\n throw createCodedError('Invalid folder index', ErrorCode.CORRUPT_HEADER);\n }\n\n // Check codec support\n for (let i = 0; i < folder.coders.length; i++) {\n const coder = folder.coders[i];\n if (!isCodecSupported(coder.id)) {\n const codecName = getCodecName(coder.id);\n throw createCodedError(`Unsupported codec: ${codecName}`, ErrorCode.UNSUPPORTED_CODEC);\n }\n }\n\n // Get decompressed data for this folder (with smart caching)\n const folderIdx = entry._folderIndex;\n const data = this.getDecompressedFolder(folderIdx);\n\n // Calculate file offset within the decompressed block\n // For solid archives, multiple files are concatenated in the block\n let fileStart = 0;\n for (let m = 0; m < entry._streamIndexInFolder; m++) {\n // Sum sizes of all streams before this one in the folder\n const prevStreamGlobalIndex = entry._streamIndex - entry._streamIndexInFolder + m;\n fileStart += this.streamsInfo.unpackSizes[prevStreamGlobalIndex];\n }\n\n const fileSize = entry.size;\n\n // Create a PassThrough stream with the file data\n const outputStream = new PassThrough();\n\n // Bounds check to prevent \"oob\" error on older Node versions\n if (fileStart + fileSize > data.length) {\n throw createCodedError(`File data out of bounds: offset ${fileStart} + size ${fileSize} > decompressed length ${data.length}`, ErrorCode.DECOMPRESSION_FAILED);\n }\n\n const fileData = data.slice(fileStart, fileStart + fileSize);\n\n // Verify CRC if present\n if (entry._crc !== undefined) {\n const actualCRC = crc32(fileData);\n if (actualCRC !== entry._crc) {\n throw createCodedError(`CRC mismatch for ${entry.path}: expected ${entry._crc.toString(16)}, got ${actualCRC.toString(16)}`, ErrorCode.CRC_MISMATCH);\n }\n }\n\n outputStream.end(fileData);\n\n // Track extraction and release cache when all files from this folder are done\n this.extractedPerFolder[folderIdx] = (this.extractedPerFolder[folderIdx] || 0) + 1;\n if (this.extractedPerFolder[folderIdx] >= this.filesPerFolder[folderIdx]) {\n // All files from this folder extracted, release cache\n delete this.decompressedCache[folderIdx];\n }\n\n return outputStream;\n }\n\n /**\n * Get a readable stream for an entry's content (callback-based async version)\n * Uses streaming decompression for non-blocking I/O\n */\n getEntryStreamAsync(entry: SevenZipEntry, callback: (err: Error | null, stream?: Readable) => void): void {\n if (!entry._hasStream || entry.type === 'directory') {\n // Return empty stream for directories and empty files\n const emptyStream = new PassThrough();\n emptyStream.end();\n callback(null, emptyStream);\n return;\n }\n\n if (!this.streamsInfo) {\n callback(createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n // Get folder info\n const folder = this.streamsInfo.folders[entry._folderIndex];\n if (!folder) {\n callback(createCodedError('Invalid folder index', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n // Check codec support\n for (let i = 0; i < folder.coders.length; i++) {\n const coder = folder.coders[i];\n if (!isCodecSupported(coder.id)) {\n const codecName = getCodecName(coder.id);\n callback(createCodedError(`Unsupported codec: ${codecName}`, ErrorCode.UNSUPPORTED_CODEC));\n return;\n }\n }\n\n // Get decompressed data for this folder using async method\n const folderIdx = entry._folderIndex;\n const streamsInfo = this.streamsInfo;\n\n this.getDecompressedFolderAsync(folderIdx, (err, data) => {\n if (err) return callback(err);\n if (!data) return callback(new Error('No data returned from decompression'));\n\n // Calculate file offset within the decompressed block\n let fileStart = 0;\n for (let m = 0; m < entry._streamIndexInFolder; m++) {\n const prevStreamGlobalIndex = entry._streamIndex - entry._streamIndexInFolder + m;\n fileStart += streamsInfo.unpackSizes[prevStreamGlobalIndex];\n }\n\n const fileSize = entry.size;\n\n // Bounds check\n if (fileStart + fileSize > data.length) {\n return callback(createCodedError(`File data out of bounds: offset ${fileStart} + size ${fileSize} > decompressed length ${data.length}`, ErrorCode.DECOMPRESSION_FAILED));\n }\n\n // Create a PassThrough stream with the file data\n const outputStream = new PassThrough();\n const fileData = data.slice(fileStart, fileStart + fileSize);\n\n // Verify CRC if present\n if (entry._crc !== undefined) {\n const actualCRC = crc32(fileData);\n if (actualCRC !== entry._crc) {\n return callback(createCodedError(`CRC mismatch for ${entry.path}: expected ${entry._crc.toString(16)}, got ${actualCRC.toString(16)}`, ErrorCode.CRC_MISMATCH));\n }\n }\n\n outputStream.end(fileData);\n\n // Track extraction and release cache when all files from this folder are done\n this.extractedPerFolder[folderIdx] = (this.extractedPerFolder[folderIdx] || 0) + 1;\n if (this.extractedPerFolder[folderIdx] >= this.filesPerFolder[folderIdx]) {\n delete this.decompressedCache[folderIdx];\n }\n\n callback(null, outputStream);\n });\n }\n\n /**\n * Check if a folder uses BCJ2 codec\n */\n private folderHasBcj2(folder: { coders: { id: number[] }[] }): boolean {\n for (let i = 0; i < folder.coders.length; i++) {\n if (isBcj2Codec(folder.coders[i].id)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Get decompressed data for a folder, with smart caching for solid archives\n * Only caches when multiple files share a block, releases when last file extracted\n */\n private getDecompressedFolder(folderIndex: number): Buffer {\n // Check cache first\n if (this.decompressedCache[folderIndex]) {\n return this.decompressedCache[folderIndex];\n }\n\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Check how many files remain in this folder\n const filesInFolder = this.filesPerFolder[folderIndex] || 1;\n const extractedFromFolder = this.extractedPerFolder[folderIndex] || 0;\n const remainingFiles = filesInFolder - extractedFromFolder;\n // Only cache if more than 1 file remains (including the current one being extracted)\n const shouldCache = remainingFiles > 1;\n\n // Check if this folder uses BCJ2 (requires special multi-stream handling)\n if (this.folderHasBcj2(folder)) {\n const data = this.decompressBcj2Folder(folderIndex);\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data;\n }\n return data;\n }\n\n // Calculate packed data position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream this folder uses\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position of this pack stream\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n const packSize = this.streamsInfo.packSizes[packStreamIndex];\n\n // Read packed data\n const packedData = this.source.read(packPos, packSize);\n\n // Decompress through codec chain\n let data2 = packedData;\n for (let l = 0; l < folder.coders.length; l++) {\n const coderInfo = folder.coders[l];\n const codec = getCodec(coderInfo.id);\n // Get unpack size for this coder (needed by LZMA)\n const unpackSize = folder.unpackSizes[l];\n data2 = codec.decode(data2, coderInfo.properties, unpackSize);\n }\n\n // Cache only if more files remain in this folder\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data2;\n }\n\n return data2;\n }\n\n /**\n * Get decompressed data for a folder using streaming (callback-based async)\n * Uses createDecoder() streams for non-blocking decompression\n */\n private getDecompressedFolderAsync(folderIndex: number, callback: DecompressCallback): void {\n const self = this;\n\n // Check cache first\n if (this.decompressedCache[folderIndex]) {\n callback(null, this.decompressedCache[folderIndex]);\n return;\n }\n\n if (!this.streamsInfo) {\n callback(createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER));\n return;\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Check how many files remain in this folder\n const filesInFolder = this.filesPerFolder[folderIndex] || 1;\n const extractedFromFolder = this.extractedPerFolder[folderIndex] || 0;\n const remainingFiles = filesInFolder - extractedFromFolder;\n const shouldCache = remainingFiles > 1;\n\n // BCJ2 requires special handling - use sync version for now\n // TODO: Add async BCJ2 support\n if (this.folderHasBcj2(folder)) {\n try {\n const data = this.decompressBcj2Folder(folderIndex);\n if (shouldCache) {\n this.decompressedCache[folderIndex] = data;\n }\n callback(null, data);\n } catch (err) {\n callback(err as Error);\n }\n return;\n }\n\n // Calculate packed data position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream this folder uses\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position of this pack stream\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n const packSize = this.streamsInfo.packSizes[packStreamIndex];\n\n // Read packed data\n const packedData = this.source.read(packPos, packSize);\n\n // Create decoder stream chain and decompress\n const coders = folder.coders;\n const unpackSizes = folder.unpackSizes;\n\n // Helper to decompress through a single codec stream\n function decompressWithStream(input: Buffer, coderIdx: number, cb: DecompressCallback): void {\n const coderInfo = coders[coderIdx];\n const codec = getCodec(coderInfo.id);\n const decoder = codec.createDecoder(coderInfo.properties, unpackSizes[coderIdx]);\n\n const chunks: Buffer[] = [];\n let errorOccurred = false;\n\n decoder.on('data', (chunk: Buffer) => {\n chunks.push(chunk);\n });\n\n oo(decoder, ['error', 'end', 'close', 'finish'], (err?: Error) => {\n if (errorOccurred) return;\n if (err) {\n errorOccurred = true;\n return cb(err);\n }\n cb(null, Buffer.concat(chunks));\n });\n\n // Write input data to decoder and signal end\n decoder.end(input);\n }\n\n // Chain decompression through all codecs\n function decompressChain(input: Buffer, idx: number): void {\n if (idx >= coders.length) {\n // All done - cache and return\n if (shouldCache) {\n self.decompressedCache[folderIndex] = input;\n }\n callback(null, input);\n return;\n }\n\n decompressWithStream(input, idx, (err, output) => {\n if (err) {\n callback(err);\n return;\n }\n decompressChain(output as Buffer, idx + 1);\n });\n }\n\n // Start the chain\n decompressChain(packedData, 0);\n }\n\n /**\n * Decompress a BCJ2 folder with multi-stream handling\n * BCJ2 uses 4 input streams: main, call, jump, range coder\n */\n private decompressBcj2Folder(folderIndex: number): Buffer {\n if (!this.streamsInfo) {\n throw createCodedError('No streams info available', ErrorCode.CORRUPT_HEADER);\n }\n\n const folder = this.streamsInfo.folders[folderIndex];\n\n // Calculate starting pack position\n let packPos = SIGNATURE_HEADER_SIZE + this.streamsInfo.packPos;\n\n // Find which pack stream index this folder starts at\n let packStreamIndex = 0;\n for (let j = 0; j < folderIndex; j++) {\n packStreamIndex += this.streamsInfo.folders[j].packedStreams.length;\n }\n\n // Calculate position\n for (let k = 0; k < packStreamIndex; k++) {\n packPos += this.streamsInfo.packSizes[k];\n }\n\n // Read all pack streams for this folder\n const numPackStreams = folder.packedStreams.length;\n const packStreams: Buffer[] = [];\n let currentPos = packPos;\n\n for (let p = 0; p < numPackStreams; p++) {\n const size = this.streamsInfo.packSizes[packStreamIndex + p];\n packStreams.push(this.source.read(currentPos, size));\n currentPos += size;\n }\n\n // Build a map of coder outputs\n // For BCJ2, typical structure is:\n // Coder 0: LZMA2 (main stream) - 1 in, 1 out\n // Coder 1: LZMA (call stream) - 1 in, 1 out\n // Coder 2: LZMA (jump stream) - 1 in, 1 out\n // Coder 3: BCJ2 - 4 in, 1 out\n // Pack streams map to: coder inputs not bound to other coder outputs\n\n // First, decompress each non-BCJ2 coder\n const coderOutputs: { [key: number]: Buffer } = {};\n\n // Find the BCJ2 coder\n let bcj2CoderIndex = -1;\n for (let c = 0; c < folder.coders.length; c++) {\n if (isBcj2Codec(folder.coders[c].id)) {\n bcj2CoderIndex = c;\n break;\n }\n }\n\n if (bcj2CoderIndex === -1) {\n throw createCodedError('BCJ2 coder not found in folder', ErrorCode.CORRUPT_HEADER);\n }\n\n // Build input stream index -> pack stream mapping\n // folder.packedStreams tells us which input indices are unbound and their order\n const inputToPackStream: { [key: number]: number } = {};\n for (let pi = 0; pi < folder.packedStreams.length; pi++) {\n inputToPackStream[folder.packedStreams[pi]] = pi;\n }\n\n // Build output stream index -> coder mapping\n const outputToCoder: { [key: number]: number } = {};\n let totalOutputs = 0;\n for (let co = 0; co < folder.coders.length; co++) {\n const numOut = folder.coders[co].numOutStreams;\n for (let outp = 0; outp < numOut; outp++) {\n outputToCoder[totalOutputs + outp] = co;\n }\n totalOutputs += numOut;\n }\n\n // Decompress non-BCJ2 coders (LZMA, LZMA2)\n // We need to process in dependency order\n const processed: { [key: number]: boolean } = {};\n\n const processOrder = this.getCoderProcessOrder(folder, bcj2CoderIndex);\n\n for (let po = 0; po < processOrder.length; po++) {\n const coderIdx = processOrder[po];\n if (coderIdx === bcj2CoderIndex) continue;\n\n const coder = folder.coders[coderIdx];\n const codec = getCodec(coder.id);\n\n // Find input for this coder\n let coderInputStart = 0;\n for (let ci2 = 0; ci2 < coderIdx; ci2++) {\n coderInputStart += folder.coders[ci2].numInStreams;\n }\n\n // Get input data (from pack stream)\n const inputIdx = coderInputStart;\n const packStreamIdx = inputToPackStream[inputIdx];\n const inputData = packStreams[packStreamIdx];\n\n // Decompress\n const unpackSize = folder.unpackSizes[coderIdx];\n const outputData = codec.decode(inputData, coder.properties, unpackSize);\n\n // Store in coder outputs\n let coderOutputStart = 0;\n for (let co2 = 0; co2 < coderIdx; co2++) {\n coderOutputStart += folder.coders[co2].numOutStreams;\n }\n coderOutputs[coderOutputStart] = outputData;\n processed[coderIdx] = true;\n }\n\n // Now process BCJ2\n // BCJ2 has 4 inputs, need to map them correctly\n // Standard order: main(LZMA2 output), call(LZMA output), jump(LZMA output), range(raw pack)\n let bcj2InputStart = 0;\n for (let ci3 = 0; ci3 < bcj2CoderIndex; ci3++) {\n bcj2InputStart += folder.coders[ci3].numInStreams;\n }\n\n const bcj2Inputs: Buffer[] = [];\n for (let bi = 0; bi < 4; bi++) {\n const globalIdx = bcj2InputStart + bi;\n\n // Check if this input is bound to a coder output\n let boundOutput = -1;\n for (let bp2 = 0; bp2 < folder.bindPairs.length; bp2++) {\n if (folder.bindPairs[bp2].inIndex === globalIdx) {\n boundOutput = folder.bindPairs[bp2].outIndex;\n break;\n }\n }\n\n if (boundOutput >= 0) {\n // Get from coder outputs\n bcj2Inputs.push(coderOutputs[boundOutput]);\n } else {\n // Get from pack streams\n const psIdx = inputToPackStream[globalIdx];\n bcj2Inputs.push(packStreams[psIdx]);\n }\n }\n\n // Get BCJ2 unpack size\n let bcj2OutputStart = 0;\n for (let co3 = 0; co3 < bcj2CoderIndex; co3++) {\n bcj2OutputStart += folder.coders[co3].numOutStreams;\n }\n const bcj2UnpackSize = folder.unpackSizes[bcj2OutputStart];\n\n // Memory optimization: Clear intermediate buffers to help GC\n // These are no longer needed after bcj2Inputs is built\n for (const key in coderOutputs) {\n delete coderOutputs[key];\n }\n // Clear packStreams array (allows GC to free compressed data)\n packStreams.length = 0;\n\n // Decode BCJ2\n return decodeBcj2Multi(bcj2Inputs, undefined, bcj2UnpackSize);\n }\n\n /**\n * Get processing order for coders (dependency order)\n */\n private getCoderProcessOrder(folder: { coders: { numInStreams: number; numOutStreams: number }[]; bindPairs: { inIndex: number; outIndex: number }[] }, excludeIdx: number): number[] {\n const order: number[] = [];\n const processed: { [key: number]: boolean } = {};\n\n // Simple approach: process coders that don't depend on unprocessed outputs\n let changed = true;\n while (changed) {\n changed = false;\n for (let c = 0; c < folder.coders.length; c++) {\n if (processed[c] || c === excludeIdx) continue;\n\n // Check if all inputs are satisfied\n let inputStart = 0;\n for (let i = 0; i < c; i++) {\n inputStart += folder.coders[i].numInStreams;\n }\n\n let canProcess = true;\n for (let inp = 0; inp < folder.coders[c].numInStreams; inp++) {\n const globalIdx = inputStart + inp;\n // Check if bound to an unprocessed coder\n for (let bp = 0; bp < folder.bindPairs.length; bp++) {\n if (folder.bindPairs[bp].inIndex === globalIdx) {\n // Find which coder produces this output\n const outIdx = folder.bindPairs[bp].outIndex;\n let outStart = 0;\n for (let oc = 0; oc < folder.coders.length; oc++) {\n const numOut = folder.coders[oc].numOutStreams;\n if (outIdx < outStart + numOut) {\n if (!processed[oc] && oc !== excludeIdx) {\n canProcess = false;\n }\n break;\n }\n outStart += numOut;\n }\n }\n }\n }\n\n if (canProcess) {\n order.push(c);\n processed[c] = true;\n changed = true;\n }\n }\n }\n\n return order;\n }\n\n /**\n * Close the parser and release resources\n */\n close(): void {\n if (this.source) {\n this.source.close();\n }\n }\n}\n\n/**\n * Get base name from a path\n */\nfunction getBaseName(path: string): string {\n const lastSlash = path.lastIndexOf('/');\n const lastBackslash = path.lastIndexOf('\\\\');\n const lastSep = Math.max(lastSlash, lastBackslash);\n return lastSep >= 0 ? path.slice(lastSep + 1) : path;\n}\n"],"names":["BufferSource","FileSource","SevenZipParser","major","process","versions","node","split","PassThrough","Stream","require","source","signature","streamsInfo","filesInfo","entries","parsed","decompressedCache","filesPerFolder","extractedPerFolder","parse","sigBuf","read","SIGNATURE_HEADER_SIZE","length","createCodedError","ErrorCode","TRUNCATED_ARCHIVE","parseSignatureHeader","headerOffset","nextHeaderOffset","headerBuf","nextHeaderSize","headerResult","parseEncodedHeader","nextHeaderCRC","err","codedErr","code","COMPRESSED_HEADER","handleCompressedHeader","buildEntries","offset","propertyId","PropertyId","kMainStreamsInfo","kPackInfo","CORRUPT_HEADER","packInfoResult","parseEncodedHeaderStreams","compressedStart","packPos","compressedData","packSize","codec","getCodec","codecId","decompressedHeader","decode","properties","unpackSize","unpackCRC","undefined","actualCRC","crc32","packAreaEnd","searchStart","searchEnd","Math","max","scanChunkSize","searchLoop","chunkStart","chunk","i","min","candidateData","subarray","candidateDecompressed","candCRC","decompOffset","headerId","kHeader","result","parseHeaderContent","buf","kEnd","packPosResult","readNumber","value","bytesRead","numPackResult","kSize","sizeResult","kUnpackInfo","kFolder","numFoldersResult","numCodersResult","flags","idSize","hasAttributes","push","propsLenResult","slice","kCodersUnpackSize","unpackSizeResult","kCRC","allDefined","readUInt32LE","file","createEntry","streamsPerFolder","numUnpackStreamsPerFolder","f","streamIndex","folderIndex","streamInFolder","folderStreamCount","j","fileInfo","size","hasStream","unpackSizes","entry","_streamIndex","unpackCRCs","_crc","type","isDirectory","mode","attributes","FileAttribute","UNIX_EXTENSION","name","getBaseName","path","mtime","atime","ctime","isAntiFile","_folderIndex","_streamIndexInFolder","_hasStream","getEntries","getEntryStream","emptyStream","end","folder","folders","coders","coder","isCodecSupported","id","codecName","getCodecName","UNSUPPORTED_CODEC","folderIdx","data","getDecompressedFolder","fileStart","m","prevStreamGlobalIndex","fileSize","outputStream","DECOMPRESSION_FAILED","fileData","toString","CRC_MISMATCH","getEntryStreamAsync","callback","getDecompressedFolderAsync","Error","folderHasBcj2","isBcj2Codec","filesInFolder","extractedFromFolder","remainingFiles","shouldCache","decompressBcj2Folder","packStreamIndex","packedStreams","k","packSizes","packedData","data2","l","coderInfo","self","decompressWithStream","input","coderIdx","cb","decoder","createDecoder","chunks","errorOccurred","on","oo","Buffer","concat","decompressChain","idx","output","numPackStreams","packStreams","currentPos","p","coderOutputs","bcj2CoderIndex","c","inputToPackStream","pi","outputToCoder","totalOutputs","co","numOut","numOutStreams","outp","processed","processOrder","getCoderProcessOrder","po","coderInputStart","ci2","numInStreams","inputIdx","packStreamIdx","inputData","outputData","coderOutputStart","co2","bcj2InputStart","ci3","bcj2Inputs","bi","globalIdx","boundOutput","bp2","bindPairs","inIndex","outIndex","psIdx","bcj2OutputStart","co3","bcj2UnpackSize","key","decodeBcj2Multi","excludeIdx","order","changed","inputStart","canProcess","inp","bp","outIdx","outStart","oc","close","lastSlash","lastIndexOf","lastBackslash","lastSep"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;CAkBC;;;;;;;;;;;QAuB4BA;eAAAA,6BAAY;;QAAEC;eAAAA,2BAAU;;QA2BxCC;eAAAA;;;mCAhDS;4DACP;6DACI;uBAEoE;2BAYwB;yBACqB;6BACzG;+BAGkC;;;;;;;;;;;AAf7D,uEAAuE;AACvE,IAAMC,QAAQ,CAACC,QAAQC,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,IAAIC;AACJ,IAAIL,QAAQ,GAAG;IACbK,cAAcC,eAAM,CAACD,WAAW;AAClC,OAAO;IACLA,cAAcE,QAAQ,mBAAmBF,WAAW;AACtD;AAmCO,IAAA,AAAMN,+BAAN;;aAAMA,eAcCS,MAAqB;gCAdtBT;aAEHU,YAAoC;aACpCC,cAAkC;aAClCC,YAAwB,EAAE;aAC1BC,UAA2B,EAAE;aAC7BC,SAAS;QACjB,4CAA4C;QAC5C,mFAAmF;aAC3EC,oBAA+C,CAAC;QACxD,0DAA0D;aAClDC,iBAA4C,CAAC;aAC7CC,qBAAgD,CAAC;QAGvD,IAAI,CAACR,MAAM,GAAGA;;iBAfLT;IAkBX;;;GAGC,GACDkB,OAAAA,KAqCC,GArCDA,SAAAA;QACE,IAAI,IAAI,CAACJ,MAAM,EAAE;QAEjB,wBAAwB;QACxB,IAAMK,SAAS,IAAI,CAACV,MAAM,CAACW,IAAI,CAAC,GAAGC,kCAAqB;QACxD,IAAIF,OAAOG,MAAM,GAAGD,kCAAqB,EAAE;YACzC,MAAME,IAAAA,6BAAgB,EAAC,qBAAqBC,sBAAS,CAACC,iBAAiB;QACzE;QAEA,IAAI,CAACf,SAAS,GAAGgB,IAA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z/codecs/Aes.ts"],"sourcesContent":["// 7z AES-256-CBC codec with SHA-256 key derivation\n// Implements password-based decryption for encrypted 7z archives\n//\n// Properties format:\n// Byte 0: bits 0-5 = NumCyclesPower (iterations = 2^NumCyclesPower)\n// bit 6 = IV present flag\n// bit 7 = Salt present flag\n// Byte 1: upper nibble = salt size extension\n// lower nibble = IV size extension\n// Following bytes: salt data, then IV data\n//\n// Key derivation:\n// For each round (2^NumCyclesPower times):\n// hash = SHA256(salt + password_utf16le + round_counter_8bytes)\n// Final key = first 32 bytes of accumulated hash\n\nimport crypto from 'crypto';\nimport { allocBuffer, bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// Global password storage - set before decryption\nlet _password: string | null = null;\n\n/**\n * Set the password for AES decryption\n */\nexport function setPassword(password: string | null): void {\n _password = password;\n}\n\n/**\n * Get the current password\n */\nexport function getPassword(): string | null {\n return _password;\n}\n\n/**\n * Parse AES properties to extract key derivation parameters\n */\nfunction parseProperties(properties: Buffer): { numCyclesPower: number; salt: Buffer; iv: Buffer } {\n if (!properties || properties.length < 1) {\n throw new Error('AES: missing properties');\n }\n\n const b0 = properties[0];\n const numCyclesPower = b0 & 0x3f;\n\n // Check for special case: no salt/IV flags\n if ((b0 & 0xc0) === 0) {\n // No salt, no IV - use zeros\n return {\n numCyclesPower: numCyclesPower,\n salt: allocBuffer(0),\n iv: allocBuffer(16),\n };\n }\n\n if (properties.length < 2) {\n throw new Error('AES: properties too short');\n }\n\n const b1 = properties[1];\n\n // Calculate sizes\n // saltSize = ((b0 >> 7) & 1) + (b1 >> 4)\n // ivSize = ((b0 >> 6) & 1) + (b1 & 0x0F)\n const saltSize = ((b0 >>> 7) & 1) + (b1 >>> 4);\n const ivSize = ((b0 >>> 6) & 1) + (b1 & 0x0f);\n\n const expectedSize = 2 + saltSize + ivSize;\n if (properties.length < expectedSize) {\n throw new Error('AES: properties too short for salt/IV');\n }\n\n const salt = properties.slice(2, 2 + saltSize);\n const iv = allocBuffer(16);\n\n // Copy IV data (may be less than 16 bytes, rest is zeros)\n const ivData = properties.slice(2 + saltSize, 2 + saltSize + ivSize);\n for (let i = 0; i < ivData.length && i < 16; i++) {\n iv[i] = ivData[i];\n }\n\n return {\n numCyclesPower: numCyclesPower,\n salt: salt,\n iv: iv,\n };\n}\n\n/**\n * Convert password string to UTF-16LE buffer\n */\nfunction passwordToUtf16LE(password: string): Buffer {\n const buf = allocBuffer(password.length * 2);\n for (let i = 0; i < password.length; i++) {\n const code = password.charCodeAt(i);\n buf[i * 2] = code & 0xff;\n buf[i * 2 + 1] = (code >>> 8) & 0xff;\n }\n return buf;\n}\n\n/**\n * Derive AES-256 key from password using 7z's SHA-256 iteration scheme\n *\n * Algorithm:\n * For round = 0 to 2^numCyclesPower - 1:\n * hash.update(salt)\n * hash.update(password_utf16le)\n * hash.update(round as 8-byte little-endian)\n * key = hash.digest()\n */\nfunction deriveKey(password: string, salt: Buffer, numCyclesPower: number): Buffer {\n const passwordBuf = passwordToUtf16LE(password);\n const numRounds = 2 ** numCyclesPower;\n\n // For special case 0x3F, don't iterate\n if (numCyclesPower === 0x3f) {\n // Direct concatenation mode\n const key = allocBuffer(32);\n let offset = 0;\n for (let j = 0; j < salt.length && offset < 32; j++) {\n key[offset++] = salt[j];\n }\n for (let k = 0; k < passwordBuf.length && offset < 32; k++) {\n key[offset++] = passwordBuf[k];\n }\n return key;\n }\n\n // Counter buffer (8 bytes, little-endian)\n const counter = allocBuffer(8);\n\n // Create hash and iterate\n const hash = crypto.createHash('sha256');\n\n for (let round = 0; round < numRounds; round++) {\n // Write round counter as little-endian 64-bit\n counter[0] = round & 0xff;\n counter[1] = (round >>> 8) & 0xff;\n counter[2] = (round >>> 16) & 0xff;\n counter[3] = (round >>> 24) & 0xff;\n // Upper 32 bits - for large round counts\n const high = Math.floor(round / 0x100000000);\n counter[4] = high & 0xff;\n counter[5] = (high >>> 8) & 0xff;\n counter[6] = (high >>> 16) & 0xff;\n counter[7] = (high >>> 24) & 0xff;\n\n hash.update(salt);\n hash.update(passwordBuf);\n hash.update(counter);\n }\n\n return hash.digest() as Buffer;\n}\n\n/**\n * Decode AES-256-CBC encrypted data\n *\n * @param input - Encrypted data\n * @param properties - AES properties (numCyclesPower, salt, IV)\n * @param _unpackSize - Unused\n * @returns Decrypted data\n */\nexport function decodeAes(input: Buffer, properties?: Buffer, _unpackSize?: number): Buffer {\n if (!_password) {\n throw new Error('AES: password required but not set');\n }\n\n if (!properties) {\n throw new Error('AES: properties required');\n }\n\n const params = parseProperties(properties);\n const key = deriveKey(_password, params.salt, params.numCyclesPower);\n\n // Create AES-256-CBC decipher\n const decipher = crypto.createDecipheriv('aes-256-cbc', key, params.iv);\n decipher.setAutoPadding(false); // 7z doesn't use PKCS7 padding\n\n // Node 0.8 returns binary strings, newer Node returns Buffers\n // Use 'binary' encoding for compatibility\n // @ts-expect-error - 'binary' encoding is deprecated but required for Node 0.8 compatibility\n const decStr = decipher.update(input, 'binary', 'binary') + decipher.final('binary');\n const decrypted = bufferFrom(decStr, 'binary' as BufferEncoding);\n\n return decrypted;\n}\n\n/**\n * Create an AES decoder Transform stream\n */\nexport function createAesDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeAes, properties, unpackSize);\n}\n"],"names":["createAesDecoder","decodeAes","getPassword","setPassword","_password","password","parseProperties","properties","length","Error","b0","numCyclesPower","salt","allocBuffer","iv","b1","saltSize","ivSize","expectedSize","slice","ivData","i","passwordToUtf16LE","buf","code","charCodeAt","deriveKey","passwordBuf","numRounds","key","offset","j","k","counter","hash","crypto","createHash","round","high","Math","floor","update","digest","input","_unpackSize","params","decipher","createDecipheriv","setAutoPadding","decStr","final","decrypted","bufferFrom","unpackSize","createBufferingDecoder"],"mappings":"AAAA,mDAAmD;AACnD,iEAAiE;AACjE,EAAE;AACF,qBAAqB;AACrB,sEAAsE;AACtE,oCAAoC;AACpC,sCAAsC;AACtC,+CAA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I,KAAK,CAAC;IAC3E,IAAMC,YAAYC,IAAAA,+BAAU,EAACH,QAAQ;IAErC,OAAOE;AACT;AAKO,SAASnD,iBAAiBO,UAAmB,EAAE8C,UAAmB;IACvE,OAAOC,IAAAA,iCAAsB,EAACrD,WAAWM,YAAY8C;AACvD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Aes.ts"],"sourcesContent":["// 7z AES-256-CBC codec with SHA-256 key derivation\n// Implements password-based decryption for encrypted 7z archives\n//\n// Properties format:\n// Byte 0: bits 0-5 = NumCyclesPower (iterations = 2^NumCyclesPower)\n// bit 6 = IV present flag\n// bit 7 = Salt present flag\n// Byte 1: upper nibble = salt size extension\n// lower nibble = IV size extension\n// Following bytes: salt data, then IV data\n//\n// Key derivation:\n// For each round (2^NumCyclesPower times):\n// hash = SHA256(salt + password_utf16le + round_counter_8bytes)\n// Final key = first 32 bytes of accumulated hash\n\nimport crypto from 'crypto';\nimport { allocBuffer, bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// Global password storage - set before decryption\nlet _password: string | null = null;\n\n/**\n * Set the password for AES decryption\n */\nexport function setPassword(password: string | null): void {\n _password = password;\n}\n\n/**\n * Get the current password\n */\nexport function getPassword(): string | null {\n return _password;\n}\n\n/**\n * Parse AES properties to extract key derivation parameters\n */\nfunction parseProperties(properties: Buffer): { numCyclesPower: number; salt: Buffer; iv: Buffer } {\n if (!properties || properties.length < 1) {\n throw new Error('AES: missing properties');\n }\n\n const b0 = properties[0];\n const numCyclesPower = b0 & 0x3f;\n\n // Check for special case: no salt/IV flags\n if ((b0 & 0xc0) === 0) {\n // No salt, no IV - use zeros\n return {\n numCyclesPower: numCyclesPower,\n salt: allocBuffer(0),\n iv: allocBuffer(16),\n };\n }\n\n if (properties.length < 2) {\n throw new Error('AES: properties too short');\n }\n\n const b1 = properties[1];\n\n // Calculate sizes\n // saltSize = ((b0 >> 7) & 1) + (b1 >> 4)\n // ivSize = ((b0 >> 6) & 1) + (b1 & 0x0F)\n const saltSize = ((b0 >>> 7) & 1) + (b1 >>> 4);\n const ivSize = ((b0 >>> 6) & 1) + (b1 & 0x0f);\n\n const expectedSize = 2 + saltSize + ivSize;\n if (properties.length < expectedSize) {\n throw new Error('AES: properties too short for salt/IV');\n }\n\n const salt = properties.slice(2, 2 + saltSize);\n const iv = allocBuffer(16);\n\n // Copy IV data (may be less than 16 bytes, rest is zeros)\n const ivData = properties.slice(2 + saltSize, 2 + saltSize + ivSize);\n for (let i = 0; i < ivData.length && i < 16; i++) {\n iv[i] = ivData[i];\n }\n\n return {\n numCyclesPower: numCyclesPower,\n salt: salt,\n iv: iv,\n };\n}\n\n/**\n * Convert password string to UTF-16LE buffer\n */\nfunction passwordToUtf16LE(password: string): Buffer {\n const buf = allocBuffer(password.length * 2);\n for (let i = 0; i < password.length; i++) {\n const code = password.charCodeAt(i);\n buf[i * 2] = code & 0xff;\n buf[i * 2 + 1] = (code >>> 8) & 0xff;\n }\n return buf;\n}\n\n/**\n * Derive AES-256 key from password using 7z's SHA-256 iteration scheme\n *\n * Algorithm:\n * For round = 0 to 2^numCyclesPower - 1:\n * hash.update(salt)\n * hash.update(password_utf16le)\n * hash.update(round as 8-byte little-endian)\n * key = hash.digest()\n */\nfunction deriveKey(password: string, salt: Buffer, numCyclesPower: number): Buffer {\n const passwordBuf = passwordToUtf16LE(password);\n const numRounds = 2 ** numCyclesPower;\n\n // For special case 0x3F, don't iterate\n if (numCyclesPower === 0x3f) {\n // Direct concatenation mode\n const key = allocBuffer(32);\n let offset = 0;\n for (let j = 0; j < salt.length && offset < 32; j++) {\n key[offset++] = salt[j];\n }\n for (let k = 0; k < passwordBuf.length && offset < 32; k++) {\n key[offset++] = passwordBuf[k];\n }\n return key;\n }\n\n // Counter buffer (8 bytes, little-endian)\n const counter = allocBuffer(8);\n\n // Create hash and iterate\n const hash = crypto.createHash('sha256');\n\n for (let round = 0; round < numRounds; round++) {\n // Write round counter as little-endian 64-bit\n counter[0] = round & 0xff;\n counter[1] = (round >>> 8) & 0xff;\n counter[2] = (round >>> 16) & 0xff;\n counter[3] = (round >>> 24) & 0xff;\n // Upper 32 bits - for large round counts\n const high = Math.floor(round / 0x100000000);\n counter[4] = high & 0xff;\n counter[5] = (high >>> 8) & 0xff;\n counter[6] = (high >>> 16) & 0xff;\n counter[7] = (high >>> 24) & 0xff;\n\n hash.update(salt);\n hash.update(passwordBuf);\n hash.update(counter);\n }\n\n return hash.digest() as Buffer;\n}\n\n/**\n * Decode AES-256-CBC encrypted data\n *\n * @param input - Encrypted data\n * @param properties - AES properties (numCyclesPower, salt, IV)\n * @param _unpackSize - Unused\n * @returns Decrypted data\n */\nexport function decodeAes(input: Buffer, properties?: Buffer, _unpackSize?: number): Buffer {\n if (!_password) {\n throw new Error('AES: password required but not set');\n }\n\n if (!properties) {\n throw new Error('AES: properties required');\n }\n\n const params = parseProperties(properties);\n const key = deriveKey(_password, params.salt, params.numCyclesPower);\n\n // Create AES-256-CBC decipher\n const decipher = crypto.createDecipheriv('aes-256-cbc', key, params.iv);\n decipher.setAutoPadding(false); // 7z doesn't use PKCS7 padding\n\n // Node 0.8 returns binary strings, newer Node returns Buffers\n // Use 'binary' encoding for compatibility\n // @ts-expect-error - 'binary' encoding is deprecated but required for Node 0.8 compatibility\n const decStr = decipher.update(input, 'binary', 'binary') + decipher.final('binary');\n const decrypted = bufferFrom(decStr, 'binary' as BufferEncoding);\n\n return decrypted;\n}\n\n/**\n * Create an AES decoder Transform stream\n */\nexport function createAesDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeAes, properties, unpackSize);\n}\n"],"names":["createAesDecoder","decodeAes","getPassword","setPassword","_password","password","parseProperties","properties","length","Error","b0","numCyclesPower","salt","allocBuffer","iv","b1","saltSize","ivSize","expectedSize","slice","ivData","i","passwordToUtf16LE","buf","code","charCodeAt","deriveKey","passwordBuf","numRounds","key","offset","j","k","counter","hash","crypto","createHash","round","high","Math","floor","update","digest","input","_unpackSize","params","decipher","createDecipheriv","setAutoPadding","decStr","final","decrypted","bufferFrom","unpackSize","createBufferingDecoder"],"mappings":"AAAA,mDAAmD;AACnD,iEAAiE;AACjE,EAAE;AACF,qBAAqB;AACrB,sEAAsE;AACtE,oCAAoC;AACpC,sCAAsC;AACtC,+CAA+C;AAC/C,6CAA6C;AAC7C,6CAA6C;AAC7C,EAAE;AACF,kBAAkB;AAClB,6CAA6C;AAC7C,oEAAoE;AACpE,mDAAmD;;;;;;;;;;;;QAsLnCA;eAAAA;;QA5BAC;eAAAA;;QAtIAC;eAAAA;;QAPAC;eAAAA;;;6DAXG;mCACqB;+EAEL;;;;;;AAEnC,kDAAkD;AAClD,IAAIC,YAA2B;AAKxB,SAASD,YAAYE,QAAuB;IACjDD,YAAYC;AACd;AAKO,SAASH;IACd,OAAOE;AACT;AAEA;;CAEC,GACD,SAASE,gBAAgBC,UAAkB;IACzC,IAAI,CAACA,cAAcA,WAAWC,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAMC,KAAKH,UAAU,CAAC,EAAE;IACxB,IAAMI,iBAAiBD,KAAK;IAE5B,2CAA2C;IAC3C,IAAI,AAACA,CAAAA,KAAK,IAAG,MAAO,GAAG;QACrB,6BAA6B;QAC7B,OAAO;YACLC,gBAAgBA;YAChBC,MAAMC,IAAAA,gCAAW,EAAC;YAClBC,IAAID,IAAAA,gCAAW,EAAC;QAClB;IACF;IAEA,IAAIN,WAAWC,MAAM,GAAG,GAAG;QACzB,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAMM,KAAKR,UAAU,CAAC,EAAE;IAExB,kBAAkB;IAClB,yCAAyC;IACzC,yCAAyC;IACzC,IAAMS,WAAW,AAAC,CAAA,AAACN,OAAO,IAAK,CAAA,IAAMK,CAAAA,OAAO,CAAA;IAC5C,IAAME,SAAS,AAAC,CAAA,AAACP,O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EnE,8BAA8B;IAC9B,IAAMmC,WAAWX,eAAM,CAACY,gBAAgB,CAAC,eAAelB,KAAKgB,OAAO/B,EAAE;IACtEgC,SAASE,cAAc,CAAC,QAAQ,+BAA+B;IAE/D,8DAA8D;IAC9D,0CAA0C;IAC1C,6FAA6F;IAC7F,IAAMC,SAASH,SAASL,MAAM,CAACE,OAAO,UAAU,YAAYG,SAASI,KAAK,CAAC;IAC3E,IAAMC,YAAYC,IAAAA,+BAAU,EAACH,QAAQ;IAErC,OAAOE;AACT;AAKO,SAASnD,iBAAiBO,UAAmB,EAAE8C,UAAmB;IACvE,OAAOC,IAAAA,iCAAsB,EAACrD,WAAWM,YAAY8C;AACvD"}
|
|
@@ -186,7 +186,14 @@ function decodeLzma2(input, properties, unpackSize) {
|
|
|
186
186
|
var inStream = (0, _streamsts.createInputStream)(input, offset, compSize);
|
|
187
187
|
var outStream = (0, _streamsts.createOutputStream)(uncompSize2); // Pre-allocate for memory efficiency
|
|
188
188
|
// Set solid mode based on control byte - this preserves state across code() calls
|
|
189
|
-
|
|
189
|
+
// For state reset WITHOUT dict reset (0xa0-0xdf), use resetProbabilities() to
|
|
190
|
+
// reset probability tables while preserving _nowPos64 for dictionary references
|
|
191
|
+
if (resetState && !dictReset) {
|
|
192
|
+
decoder.resetProbabilities();
|
|
193
|
+
decoder.setSolid(true); // Preserve _nowPos64 in code()
|
|
194
|
+
} else {
|
|
195
|
+
decoder.setSolid(useSolidMode);
|
|
196
|
+
}
|
|
190
197
|
// Decode the chunk
|
|
191
198
|
var success = decoder.code(inStream, outStream, uncompSize2);
|
|
192
199
|
if (!success) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA2 codec - uses native lzma-native when available, falls back to lzma-purejs\n// LZMA2 is a container format that wraps LZMA chunks with framing\n//\n// LZMA2 format specification:\n// https://github.com/ulikunitz/xz/blob/master/doc/LZMA2.md\n//\n// Control byte values:\n// 0x00 = End of stream\n// 0x01 = Uncompressed chunk, dictionary reset\n// 0x02 = Uncompressed chunk, no dictionary reset\n// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)\n//\n// Native optimization: On Node.js 8+, lzma-native provides liblzma bindings\n// that decode LZMA2 streams natively for better performance.\n// Falls back to lzma-purejs for Node.js 0.8-7.x compatibility.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createNativeLzma2Decoder, hasNativeLzma } from './lzmaCompat.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\n// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\n// Path accounts for build output in dist/esm/sevenz/codecs/\nconst { LZMA } = _require('../../../../assets/lzma-purejs');\nconst LzmaDecoder = LZMA.Decoder;\n\n/**\n * Decode LZMA2 dictionary size from properties byte\n * Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar\n *\n * Per XZ spec, dictionary sizes are:\n * 0x00 = 4 KiB (2^12)\n * 0x01 = 6 KiB\n * 0x02 = 8 KiB (2^13)\n * ...\n * 0x28 = 1.5 GiB\n */\nfunction decodeDictionarySize(propByte: number): number {\n if (propByte > 40) {\n throw new Error(`Invalid LZMA2 dictionary size property: ${propByte}`);\n }\n if (propByte === 40) {\n // Max dictionary size: 4 GiB - 1\n return 0xffffffff;\n }\n // Dictionary size = 2 | (propByte & 1) << (propByte / 2 + 11)\n const base = 2 | (propByte & 1);\n const shift = Math.floor(propByte / 2) + 11;\n return base << shift;\n}\n\n/**\n * Decode LZMA2 compressed data to buffer\n *\n * @param input - LZMA2 compressed data\n * @param properties - Properties buffer (1 byte: dictionary size)\n * @param unpackSize - Expected output size (used for pre-allocation to reduce memory)\n * @returns Decompressed data\n */\nexport function decodeLzma2(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = decodeDictionarySize(properties[0]);\n\n // Memory optimization: pre-allocate output buffer if size is known\n // This avoids double-memory during Buffer.concat\n let outputBuffer: Buffer | null = null;\n let outputPos = 0;\n const outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n let offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The vendored decoder supports setSolid() for LZMA2 state preservation\n // The decoder also has _nowPos64 which tracks cumulative position for rep0 validation\n // and _prevByte which is used for literal decoder context selection\n const decoder = new LzmaDecoder() as InstanceType<typeof LzmaDecoder> & {\n setSolid: (solid: boolean) => void;\n _nowPos64: number;\n _prevByte: number;\n };\n decoder.setDictionarySize(dictSize);\n\n // Access internal _outWindow for dictionary management\n // We need to preserve dictionary state across LZMA2 chunks\n type OutWindowType = {\n _buffer: Buffer;\n _pos: number;\n _streamPos: number;\n _windowSize: number;\n init: (solid: boolean) => void;\n };\n const outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n let propsSet = false;\n\n while (offset < input.length) {\n const control = input[offset++];\n\n if (control === 0x00) {\n // End of LZMA2 stream\n break;\n }\n\n if (control === 0x01 || control === 0x02) {\n // Uncompressed chunk\n // 0x01 = dictionary reset + uncompressed\n // 0x02 = uncompressed (no reset)\n\n // Handle dictionary reset for 0x01\n if (control === 0x01) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n decoder._nowPos64 = 0;\n }\n\n if (offset + 2 > input.length) {\n throw new Error('Truncated LZMA2 uncompressed chunk header');\n }\n\n // Size is big-endian, 16-bit, value + 1\n const uncompSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n if (offset + uncompSize > input.length) {\n throw new Error('Truncated LZMA2 uncompressed data');\n }\n\n // Get the uncompressed data\n const uncompData = input.slice(offset, offset + uncompSize);\n\n // Copy uncompressed data to output\n if (outputBuffer) {\n uncompData.copy(outputBuffer, outputPos);\n outputPos += uncompData.length;\n } else {\n outputChunks?.push(uncompData);\n }\n\n // Also update the decoder's internal dictionary so subsequent LZMA chunks can reference it\n // The decoder needs to track this data for LZ77 back-references\n // We write directly to _buffer to avoid flush() which requires _stream to be set\n // We must also update _streamPos to match _pos so that flush() doesn't try to write\n for (let i = 0; i < uncompData.length; i++) {\n outWindow._buffer[outWindow._pos++] = uncompData[i];\n // Handle circular buffer wrap-around\n if (outWindow._pos >= outWindow._windowSize) {\n outWindow._pos = 0;\n }\n }\n // Keep _streamPos in sync so flush() doesn't try to write these bytes\n // (they're already in our output buffer)\n outWindow._streamPos = outWindow._pos;\n\n // Update decoder's cumulative position so subsequent LZMA chunks have correct rep0 validation\n decoder._nowPos64 += uncompSize;\n\n // Update prevByte for literal decoder context in subsequent LZMA chunks\n decoder._prevByte = uncompData[uncompData.length - 1];\n\n offset += uncompSize;\n } else if (control >= 0x80) {\n // LZMA compressed chunk\n // Control byte format (bits 7-0):\n // Bit 7: always 1 for LZMA chunk\n // Bits 6-5: reset mode (00=nothing, 01=state, 10=state+props, 11=all)\n // Bits 4-0: high 5 bits of uncompressed size - 1\n\n // Control byte ranges (based on bits 6-5):\n // 0x80-0x9F (00): no reset - continue existing state (solid mode)\n // 0xA0-0xBF (01): reset state only\n // 0xC0-0xDF (10): reset state + new properties\n // 0xE0-0xFF (11): reset dictionary + state + new properties\n const resetState = control >= 0xa0;\n const newProps = control >= 0xc0;\n const dictReset = control >= 0xe0;\n const useSolidMode = !resetState;\n\n // Handle dictionary reset for control bytes 0xE0-0xFF\n if (dictReset) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n }\n\n if (offset + 4 > input.length) {\n throw new Error('Truncated LZMA2 LZMA chunk header');\n }\n\n // Uncompressed size: 5 bits from control + 16 bits from next 2 bytes + 1\n const uncompHigh = control & 0x1f;\n const uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n const compSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // If new properties, read 1-byte LZMA properties\n if (newProps) {\n if (offset >= input.length) {\n throw new Error('Truncated LZMA2 properties byte');\n }\n const propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n const lc = propsByte % 9;\n const remainder = Math.floor(propsByte / 9);\n const lp = remainder % 5;\n const pb = Math.floor(remainder / 5);\n\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 }\n\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n\n if (offset + compSize > input.length) {\n throw new Error('Truncated LZMA2 compressed data');\n }\n\n // Decode LZMA chunk\n const inStream = createInputStream(input, offset, compSize);\n const outStream = createOutputStream(uncompSize2); // Pre-allocate for memory efficiency\n\n // Set solid mode based on control byte - this preserves state across code() calls\n decoder.setSolid(useSolidMode);\n\n // Decode the chunk\n const success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n const chunkOutput = outStream.toBuffer();\n if (outputBuffer) {\n chunkOutput.copy(outputBuffer, outputPos);\n outputPos += chunkOutput.length;\n } else {\n outputChunks?.push(chunkOutput);\n }\n\n offset += compSize;\n } else {\n throw new Error(`Invalid LZMA2 control byte: 0x${control.toString(16)}`);\n }\n }\n\n // Return pre-allocated buffer or concatenated chunks\n if (outputBuffer) {\n // Return only the used portion if we didn't fill the buffer\n return outputPos < outputBuffer.length ? outputBuffer.slice(0, outputPos) : outputBuffer;\n }\n return Buffer.concat(outputChunks);\n}\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * Uses native lzma-native when available for better performance,\n * falls back to lzma-purejs buffering decoder for Node.js 0.8+ compatibility.\n */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n // Try native decoder first (available on Node.js 8+ with lzma-native installed)\n if (hasNativeLzma && properties && properties.length >= 1) {\n const dictSize = decodeDictionarySize(properties[0]);\n const nativeDecoder = createNativeLzma2Decoder(dictSize);\n if (nativeDecoder) {\n return nativeDecoder;\n }\n }\n\n // Fall back to buffering decoder with pure JS implementation\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["createLzma2Decoder","decodeLzma2","_require","require","Module","createRequire","LZMA","LzmaDecoder","Decoder","decodeDictionarySize","propByte","Error","base","shift","Math","floor","input","properties","unpackSize","length","dictSize","outputBuffer","outputPos","outputChunks","allocBufferUnsafe","offset","decoder","setDictionarySize","outWindow","_outWindow","propsSet","control","_pos","_streamPos","_nowPos64","uncompSize","uncompData","slice","copy","push","i","_buffer","_windowSize","_prevByte","resetState","newProps","dictReset","useSolidMode","uncompHigh","uncompSize2","compSize","propsByte","lc","remainder","lp","pb","setLcLpPb","inStream","createInputStream","outStream","createOutputStream","setSolid","success","code","chunkOutput","toBuffer","toString","Buffer","concat","hasNativeLzma","nativeDecoder","createNativeLzma2Decoder","createBufferingDecoder"],"mappings":";;;;;;;;;;;QAsRgBA;eAAAA;;QAtNAC;eAAAA;;;6DAhEG;mCAoBe;+EAEC;4BACqB;yBACF;;;;;;AAtBtD,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAwB1F,sFAAsF;AACtF,4DAA4D;AAC5D,IAAM,AAAEG,OAASJ,SAAS,kCAAlBI;AACR,IAAMC,cAAcD,KAAKE,OAAO;AAEhC;;;;;;;;;;CAUC,GACD,SAASC,qBAAqBC,QAAgB;IAC5C,IAAIA,WAAW,IAAI;QACjB,MAAM,IAAIC,MAAM,AAAC,2CAAmD,OAATD;IAC7D;IACA,IAAIA,aAAa,IAAI;QACnB,iCAAiC;QACjC,OAAO;IACT;IACA,8DAA8D;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nB,IAAMe,UAAUpC,QAAQqC,IAAI,CAACN,UAAUE,WAAWV;YAClD,IAAI,CAACa,SAAS;gBACZ,MAAM,IAAInD,MAAM;YAClB;YAEA,IAAMqD,cAAcL,UAAUM,QAAQ;YACtC,IAAI5C,cAAc;gBAChB2C,YAAY1B,IAAI,CAACjB,cAAcC;gBAC/BA,aAAa0C,YAAY7C,MAAM;YACjC,OAAO;gBACLI,yBAAAA,mCAAAA,aAAcgB,IAAI,CAACyB;YACrB;YAEAvC,UAAUyB;QACZ,OAAO;YACL,MAAM,IAAIvC,MAAM,AAAC,iCAAqD,OAArBoB,QAAQmC,QAAQ,CAAC;QACpE;IACF;IAEA,qDAAqD;IACrD,IAAI7C,cAAc;QAChB,4DAA4D;QAC5D,OAAOC,YAAYD,aAAaF,MAAM,GAAGE,aAAagB,KAAK,CAAC,GAAGf,aAAaD;IAC9E;IACA,OAAO8C,OAAOC,MAAM,CAAC7C;AACvB;AAQO,SAASvB,mBAAmBiB,UAAmB,EAAEC,UAAmB;IACzE,gFAAgF;IAChF,IAAImD,2BAAa,IAAIpD,cAAcA,WAAWE,MAAM,IAAI,GAAG;QACzD,IAAMC,WAAWX,qBAAqBQ,UAAU,CAAC,EAAE;QACnD,IAAMqD,gBAAgBC,IAAAA,sCAAwB,EAACnD;QAC/C,IAAIkD,eAAe;YACjB,OAAOA;QACT;IACF;IAEA,6DAA6D;IAC7D,OAAOE,IAAAA,iCAAsB,EAACvE,aAAagB,YAAYC;AACzD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA2 codec - uses native lzma-native when available, falls back to lzma-purejs\n// LZMA2 is a container format that wraps LZMA chunks with framing\n//\n// LZMA2 format specification:\n// https://github.com/ulikunitz/xz/blob/master/doc/LZMA2.md\n//\n// Control byte values:\n// 0x00 = End of stream\n// 0x01 = Uncompressed chunk, dictionary reset\n// 0x02 = Uncompressed chunk, no dictionary reset\n// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)\n//\n// Native optimization: On Node.js 8+, lzma-native provides liblzma bindings\n// that decode LZMA2 streams natively for better performance.\n// Falls back to lzma-purejs for Node.js 0.8-7.x compatibility.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createNativeLzma2Decoder, hasNativeLzma } from './lzmaCompat.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\n// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\n// Path accounts for build output in dist/esm/sevenz/codecs/\nconst { LZMA } = _require('../../../../assets/lzma-purejs');\nconst LzmaDecoder = LZMA.Decoder;\n\n/**\n * Decode LZMA2 dictionary size from properties byte\n * Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar\n *\n * Per XZ spec, dictionary sizes are:\n * 0x00 = 4 KiB (2^12)\n * 0x01 = 6 KiB\n * 0x02 = 8 KiB (2^13)\n * ...\n * 0x28 = 1.5 GiB\n */\nfunction decodeDictionarySize(propByte: number): number {\n if (propByte > 40) {\n throw new Error(`Invalid LZMA2 dictionary size property: ${propByte}`);\n }\n if (propByte === 40) {\n // Max dictionary size: 4 GiB - 1\n return 0xffffffff;\n }\n // Dictionary size = 2 | (propByte & 1) << (propByte / 2 + 11)\n const base = 2 | (propByte & 1);\n const shift = Math.floor(propByte / 2) + 11;\n return base << shift;\n}\n\n/**\n * Decode LZMA2 compressed data to buffer\n *\n * @param input - LZMA2 compressed data\n * @param properties - Properties buffer (1 byte: dictionary size)\n * @param unpackSize - Expected output size (used for pre-allocation to reduce memory)\n * @returns Decompressed data\n */\nexport function decodeLzma2(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = decodeDictionarySize(properties[0]);\n\n // Memory optimization: pre-allocate output buffer if size is known\n // This avoids double-memory during Buffer.concat\n let outputBuffer: Buffer | null = null;\n let outputPos = 0;\n const outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n let offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The vendored decoder supports setSolid() for LZMA2 state preservation\n // The decoder also has _nowPos64 which tracks cumulative position for rep0 validation\n // and _prevByte which is used for literal decoder context selection\n const decoder = new LzmaDecoder() as InstanceType<typeof LzmaDecoder> & {\n setSolid: (solid: boolean) => void;\n resetProbabilities: () => void;\n _nowPos64: number;\n _prevByte: number;\n };\n decoder.setDictionarySize(dictSize);\n\n // Access internal _outWindow for dictionary management\n // We need to preserve dictionary state across LZMA2 chunks\n type OutWindowType = {\n _buffer: Buffer;\n _pos: number;\n _streamPos: number;\n _windowSize: number;\n init: (solid: boolean) => void;\n };\n const outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n let propsSet = false;\n\n while (offset < input.length) {\n const control = input[offset++];\n\n if (control === 0x00) {\n // End of LZMA2 stream\n break;\n }\n\n if (control === 0x01 || control === 0x02) {\n // Uncompressed chunk\n // 0x01 = dictionary reset + uncompressed\n // 0x02 = uncompressed (no reset)\n\n // Handle dictionary reset for 0x01\n if (control === 0x01) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n decoder._nowPos64 = 0;\n }\n\n if (offset + 2 > input.length) {\n throw new Error('Truncated LZMA2 uncompressed chunk header');\n }\n\n // Size is big-endian, 16-bit, value + 1\n const uncompSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n if (offset + uncompSize > input.length) {\n throw new Error('Truncated LZMA2 uncompressed data');\n }\n\n // Get the uncompressed data\n const uncompData = input.slice(offset, offset + uncompSize);\n\n // Copy uncompressed data to output\n if (outputBuffer) {\n uncompData.copy(outputBuffer, outputPos);\n outputPos += uncompData.length;\n } else {\n outputChunks?.push(uncompData);\n }\n\n // Also update the decoder's internal dictionary so subsequent LZMA chunks can reference it\n // The decoder needs to track this data for LZ77 back-references\n // We write directly to _buffer to avoid flush() which requires _stream to be set\n // We must also update _streamPos to match _pos so that flush() doesn't try to write\n for (let i = 0; i < uncompData.length; i++) {\n outWindow._buffer[outWindow._pos++] = uncompData[i];\n // Handle circular buffer wrap-around\n if (outWindow._pos >= outWindow._windowSize) {\n outWindow._pos = 0;\n }\n }\n // Keep _streamPos in sync so flush() doesn't try to write these bytes\n // (they're already in our output buffer)\n outWindow._streamPos = outWindow._pos;\n\n // Update decoder's cumulative position so subsequent LZMA chunks have correct rep0 validation\n decoder._nowPos64 += uncompSize;\n\n // Update prevByte for literal decoder context in subsequent LZMA chunks\n decoder._prevByte = uncompData[uncompData.length - 1];\n\n offset += uncompSize;\n } else if (control >= 0x80) {\n // LZMA compressed chunk\n // Control byte format (bits 7-0):\n // Bit 7: always 1 for LZMA chunk\n // Bits 6-5: reset mode (00=nothing, 01=state, 10=state+props, 11=all)\n // Bits 4-0: high 5 bits of uncompressed size - 1\n\n // Control byte ranges (based on bits 6-5):\n // 0x80-0x9F (00): no reset - continue existing state (solid mode)\n // 0xA0-0xBF (01): reset state only\n // 0xC0-0xDF (10): reset state + new properties\n // 0xE0-0xFF (11): reset dictionary + state + new properties\n const resetState = control >= 0xa0;\n const newProps = control >= 0xc0;\n const dictReset = control >= 0xe0;\n const useSolidMode = !resetState;\n\n // Handle dictionary reset for control bytes 0xE0-0xFF\n if (dictReset) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n }\n\n if (offset + 4 > input.length) {\n throw new Error('Truncated LZMA2 LZMA chunk header');\n }\n\n // Uncompressed size: 5 bits from control + 16 bits from next 2 bytes + 1\n const uncompHigh = control & 0x1f;\n const uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n const compSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // If new properties, read 1-byte LZMA properties\n if (newProps) {\n if (offset >= input.length) {\n throw new Error('Truncated LZMA2 properties byte');\n }\n const propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n const lc = propsByte % 9;\n const remainder = Math.floor(propsByte / 9);\n const lp = remainder % 5;\n const pb = Math.floor(remainder / 5);\n\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 }\n\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n\n if (offset + compSize > input.length) {\n throw new Error('Truncated LZMA2 compressed data');\n }\n\n // Decode LZMA chunk\n const inStream = createInputStream(input, offset, compSize);\n const outStream = createOutputStream(uncompSize2); // Pre-allocate for memory efficiency\n\n // Set solid mode based on control byte - this preserves state across code() calls\n // For state reset WITHOUT dict reset (0xa0-0xdf), use resetProbabilities() to\n // reset probability tables while preserving _nowPos64 for dictionary references\n if (resetState && !dictReset) {\n decoder.resetProbabilities();\n decoder.setSolid(true); // Preserve _nowPos64 in code()\n } else {\n decoder.setSolid(useSolidMode);\n }\n\n // Decode the chunk\n const success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n const chunkOutput = outStream.toBuffer();\n if (outputBuffer) {\n chunkOutput.copy(outputBuffer, outputPos);\n outputPos += chunkOutput.length;\n } else {\n outputChunks?.push(chunkOutput);\n }\n\n offset += compSize;\n } else {\n throw new Error(`Invalid LZMA2 control byte: 0x${control.toString(16)}`);\n }\n }\n\n // Return pre-allocated buffer or concatenated chunks\n if (outputBuffer) {\n // Return only the used portion if we didn't fill the buffer\n return outputPos < outputBuffer.length ? outputBuffer.slice(0, outputPos) : outputBuffer;\n }\n return Buffer.concat(outputChunks);\n}\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * Uses native lzma-native when available for better performance,\n * falls back to lzma-purejs buffering decoder for Node.js 0.8+ compatibility.\n */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n // Try native decoder first (available on Node.js 8+ with lzma-native installed)\n if (hasNativeLzma && properties && properties.length >= 1) {\n const dictSize = decodeDictionarySize(properties[0]);\n const nativeDecoder = createNativeLzma2Decoder(dictSize);\n if (nativeDecoder) {\n return nativeDecoder;\n }\n }\n\n // Fall back to buffering decoder with pure JS implementation\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["createLzma2Decoder","decodeLzma2","_require","require","Module","createRequire","LZMA","LzmaDecoder","Decoder","decodeDictionarySize","propByte","Error","base","shift","Math","floor","input","properties","unpackSize","length","dictSize","outputBuffer","outputPos","outputChunks","allocBufferUnsafe","offset","decoder","setDictionarySize","outWindow","_outWindow","propsSet","control","_pos","_streamPos","_nowPos64","uncompSize","uncompData","slice","copy","push","i","_buffer","_windowSize","_prevByte","resetState","newProps","dictReset","useSolidMode","uncompHigh","uncompSize2","compSize","propsByte","lc","remainder","lp","pb","setLcLpPb","inStream","createInputStream","outStream","createOutputStream","resetProbabilities","setSolid","success","code","chunkOutput","toBuffer","toString","Buffer","concat","hasNativeLzma","nativeDecoder","createNativeLzma2Decoder","createBufferingDecoder"],"mappings":";;;;;;;;;;;QA8RgBA;eAAAA;;QA9NAC;eAAAA;;;6DAhEG;mCAoBe;+EAEC;4BACqB;yBACF;;;;;;AAtBtD,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAwB1F,sFAAsF;AACtF,4DAA4D;AAC5D,IAAM,AAAEG,OAASJ,SAAS,kCAAlBI;AACR,IAAMC,cAAcD,KAAKE,OAAO;AAEhC;;;;;;;;;;CAUC,GACD,SAASC,qBAAqBC,QAAgB;IAC5C,IAAIA,WAAW,IAAI;QACjB,MAAM,IAAIC,MAAM,AAAC,2CAAmD,O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Stream(input, offset, compSize);
|
|
186
186
|
const outStream = createOutputStream(uncompSize2); // Pre-allocate for memory efficiency
|
|
187
187
|
// Set solid mode based on control byte - this preserves state across code() calls
|
|
188
|
-
|
|
188
|
+
// For state reset WITHOUT dict reset (0xa0-0xdf), use resetProbabilities() to
|
|
189
|
+
// reset probability tables while preserving _nowPos64 for dictionary references
|
|
190
|
+
if (resetState && !dictReset) {
|
|
191
|
+
decoder.resetProbabilities();
|
|
192
|
+
decoder.setSolid(true); // Preserve _nowPos64 in code()
|
|
193
|
+
} else {
|
|
194
|
+
decoder.setSolid(useSolidMode);
|
|
195
|
+
}
|
|
189
196
|
// Decode the chunk
|
|
190
197
|
const success = decoder.code(inStream, outStream, uncompSize2);
|
|
191
198
|
if (!success) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA2 codec - uses native lzma-native when available, falls back to lzma-purejs\n// LZMA2 is a container format that wraps LZMA chunks with framing\n//\n// LZMA2 format specification:\n// https://github.com/ulikunitz/xz/blob/master/doc/LZMA2.md\n//\n// Control byte values:\n// 0x00 = End of stream\n// 0x01 = Uncompressed chunk, dictionary reset\n// 0x02 = Uncompressed chunk, no dictionary reset\n// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)\n//\n// Native optimization: On Node.js 8+, lzma-native provides liblzma bindings\n// that decode LZMA2 streams natively for better performance.\n// Falls back to lzma-purejs for Node.js 0.8-7.x compatibility.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createNativeLzma2Decoder, hasNativeLzma } from './lzmaCompat.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\n// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\n// Path accounts for build output in dist/esm/sevenz/codecs/\nconst { LZMA } = _require('../../../../assets/lzma-purejs');\nconst LzmaDecoder = LZMA.Decoder;\n\n/**\n * Decode LZMA2 dictionary size from properties byte\n * Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar\n *\n * Per XZ spec, dictionary sizes are:\n * 0x00 = 4 KiB (2^12)\n * 0x01 = 6 KiB\n * 0x02 = 8 KiB (2^13)\n * ...\n * 0x28 = 1.5 GiB\n */\nfunction decodeDictionarySize(propByte: number): number {\n if (propByte > 40) {\n throw new Error(`Invalid LZMA2 dictionary size property: ${propByte}`);\n }\n if (propByte === 40) {\n // Max dictionary size: 4 GiB - 1\n return 0xffffffff;\n }\n // Dictionary size = 2 | (propByte & 1) << (propByte / 2 + 11)\n const base = 2 | (propByte & 1);\n const shift = Math.floor(propByte / 2) + 11;\n return base << shift;\n}\n\n/**\n * Decode LZMA2 compressed data to buffer\n *\n * @param input - LZMA2 compressed data\n * @param properties - Properties buffer (1 byte: dictionary size)\n * @param unpackSize - Expected output size (used for pre-allocation to reduce memory)\n * @returns Decompressed data\n */\nexport function decodeLzma2(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = decodeDictionarySize(properties[0]);\n\n // Memory optimization: pre-allocate output buffer if size is known\n // This avoids double-memory during Buffer.concat\n let outputBuffer: Buffer | null = null;\n let outputPos = 0;\n const outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n let offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The vendored decoder supports setSolid() for LZMA2 state preservation\n // The decoder also has _nowPos64 which tracks cumulative position for rep0 validation\n // and _prevByte which is used for literal decoder context selection\n const decoder = new LzmaDecoder() as InstanceType<typeof LzmaDecoder> & {\n setSolid: (solid: boolean) => void;\n _nowPos64: number;\n _prevByte: number;\n };\n decoder.setDictionarySize(dictSize);\n\n // Access internal _outWindow for dictionary management\n // We need to preserve dictionary state across LZMA2 chunks\n type OutWindowType = {\n _buffer: Buffer;\n _pos: number;\n _streamPos: number;\n _windowSize: number;\n init: (solid: boolean) => void;\n };\n const outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n let propsSet = false;\n\n while (offset < input.length) {\n const control = input[offset++];\n\n if (control === 0x00) {\n // End of LZMA2 stream\n break;\n }\n\n if (control === 0x01 || control === 0x02) {\n // Uncompressed chunk\n // 0x01 = dictionary reset + uncompressed\n // 0x02 = uncompressed (no reset)\n\n // Handle dictionary reset for 0x01\n if (control === 0x01) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n decoder._nowPos64 = 0;\n }\n\n if (offset + 2 > input.length) {\n throw new Error('Truncated LZMA2 uncompressed chunk header');\n }\n\n // Size is big-endian, 16-bit, value + 1\n const uncompSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n if (offset + uncompSize > input.length) {\n throw new Error('Truncated LZMA2 uncompressed data');\n }\n\n // Get the uncompressed data\n const uncompData = input.slice(offset, offset + uncompSize);\n\n // Copy uncompressed data to output\n if (outputBuffer) {\n uncompData.copy(outputBuffer, outputPos);\n outputPos += uncompData.length;\n } else {\n outputChunks?.push(uncompData);\n }\n\n // Also update the decoder's internal dictionary so subsequent LZMA chunks can reference it\n // The decoder needs to track this data for LZ77 back-references\n // We write directly to _buffer to avoid flush() which requires _stream to be set\n // We must also update _streamPos to match _pos so that flush() doesn't try to write\n for (let i = 0; i < uncompData.length; i++) {\n outWindow._buffer[outWindow._pos++] = uncompData[i];\n // Handle circular buffer wrap-around\n if (outWindow._pos >= outWindow._windowSize) {\n outWindow._pos = 0;\n }\n }\n // Keep _streamPos in sync so flush() doesn't try to write these bytes\n // (they're already in our output buffer)\n outWindow._streamPos = outWindow._pos;\n\n // Update decoder's cumulative position so subsequent LZMA chunks have correct rep0 validation\n decoder._nowPos64 += uncompSize;\n\n // Update prevByte for literal decoder context in subsequent LZMA chunks\n decoder._prevByte = uncompData[uncompData.length - 1];\n\n offset += uncompSize;\n } else if (control >= 0x80) {\n // LZMA compressed chunk\n // Control byte format (bits 7-0):\n // Bit 7: always 1 for LZMA chunk\n // Bits 6-5: reset mode (00=nothing, 01=state, 10=state+props, 11=all)\n // Bits 4-0: high 5 bits of uncompressed size - 1\n\n // Control byte ranges (based on bits 6-5):\n // 0x80-0x9F (00): no reset - continue existing state (solid mode)\n // 0xA0-0xBF (01): reset state only\n // 0xC0-0xDF (10): reset state + new properties\n // 0xE0-0xFF (11): reset dictionary + state + new properties\n const resetState = control >= 0xa0;\n const newProps = control >= 0xc0;\n const dictReset = control >= 0xe0;\n const useSolidMode = !resetState;\n\n // Handle dictionary reset for control bytes 0xE0-0xFF\n if (dictReset) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n }\n\n if (offset + 4 > input.length) {\n throw new Error('Truncated LZMA2 LZMA chunk header');\n }\n\n // Uncompressed size: 5 bits from control + 16 bits from next 2 bytes + 1\n const uncompHigh = control & 0x1f;\n const uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n const compSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // If new properties, read 1-byte LZMA properties\n if (newProps) {\n if (offset >= input.length) {\n throw new Error('Truncated LZMA2 properties byte');\n }\n const propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n const lc = propsByte % 9;\n const remainder = Math.floor(propsByte / 9);\n const lp = remainder % 5;\n const pb = Math.floor(remainder / 5);\n\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 }\n\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n\n if (offset + compSize > input.length) {\n throw new Error('Truncated LZMA2 compressed data');\n }\n\n // Decode LZMA chunk\n const inStream = createInputStream(input, offset, compSize);\n const outStream = createOutputStream(uncompSize2); // Pre-allocate for memory efficiency\n\n // Set solid mode based on control byte - this preserves state across code() calls\n decoder.setSolid(useSolidMode);\n\n // Decode the chunk\n const success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n const chunkOutput = outStream.toBuffer();\n if (outputBuffer) {\n chunkOutput.copy(outputBuffer, outputPos);\n outputPos += chunkOutput.length;\n } else {\n outputChunks?.push(chunkOutput);\n }\n\n offset += compSize;\n } else {\n throw new Error(`Invalid LZMA2 control byte: 0x${control.toString(16)}`);\n }\n }\n\n // Return pre-allocated buffer or concatenated chunks\n if (outputBuffer) {\n // Return only the used portion if we didn't fill the buffer\n return outputPos < outputBuffer.length ? outputBuffer.slice(0, outputPos) : outputBuffer;\n }\n return Buffer.concat(outputChunks);\n}\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * Uses native lzma-native when available for better performance,\n * falls back to lzma-purejs buffering decoder for Node.js 0.8+ compatibility.\n */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n // Try native decoder first (available on Node.js 8+ with lzma-native installed)\n if (hasNativeLzma && properties && properties.length >= 1) {\n const dictSize = decodeDictionarySize(properties[0]);\n const nativeDecoder = createNativeLzma2Decoder(dictSize);\n if (nativeDecoder) {\n return nativeDecoder;\n }\n }\n\n // Fall back to buffering decoder with pure JS implementation\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["Module","_require","require","createRequire","url","allocBufferUnsafe","createBufferingDecoder","createNativeLzma2Decoder","hasNativeLzma","createInputStream","createOutputStream","LZMA","LzmaDecoder","Decoder","decodeDictionarySize","propByte","Error","base","shift","Math","floor","decodeLzma2","input","properties","unpackSize","length","dictSize","outputBuffer","outputPos","outputChunks","offset","decoder","setDictionarySize","outWindow","_outWindow","propsSet","control","_pos","_streamPos","_nowPos64","uncompSize","uncompData","slice","copy","push","i","_buffer","_windowSize","_prevByte","resetState","newProps","dictReset","useSolidMode","uncompHigh","uncompSize2","compSize","propsByte","lc","remainder","lp","pb","setLcLpPb","inStream","outStream","setSolid","success","code","chunkOutput","toBuffer","toString","Buffer","concat","createLzma2Decoder","nativeDecoder"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAE5B,MAAMC,WAAW,OAAOC,YAAY,cAAcF,OAAOG,aAAa,CAAC,YAAYC,GAAG,IAAIF;AAE1F,kFAAkF;AAClF,kEAAkE;AAClE,EAAE;AACF,8BAA8B;AAC9B,2DAA2D;AAC3D,EAAE;AACF,uBAAuB;AACvB,+BAA+B;AAC/B,sDAAsD;AACtD,yDAAyD;AACzD,0EAA0E;AAC1E,EAAE;AACF,4EAA4E;AAC5E,6DAA6D;AAC7D,+DAA+D;AAE/D,SAASG,iBAAiB,QAAQ,wBAAwB;AAE1D,OAAOC,4BAA4B,8BAA8B;AACjE,SAASC,wBAAwB,EAAEC,aAAa,QAAQ,kBAAkB;AAC1E,SAASC,iBAAiB,EAAEC,kBAAkB,QAAQ,eAAe;AAErE,sFAAsF;AACtF,4DAA4D;AAC5D,MAAM,EAAEC,IAAI,EAAE,GAAGV,SAAS;AAC1B,MAAMW,cAAcD,KAAKE,OAAO;AAEhC;;;;;;;;;;CAUC,GACD,SAASC,qBAAqBC,QAAgB;IAC5C,IAAIA,WAAW,IAAI;QACjB,MAAM,IAAIC,MAAM,CAAC,wCAAwC,EAAED,UAAU;IACvE;IACA,IAAIA,aAAa,IAAI;QACnB,iCAAiC;QACjC,OAAO;IACT;IACA,8DAA8D;IAC9D,MAAME,OAAO,IAAKF,WAAW;IAC7B,MAAMG,QAAQC,KAAKC,KAAK,CAACL,WAAW,KAAK;IACzC,OAAOE,QAAQC;AACjB;AAEA;;;;;;;CAOC,GACD,OAAO,SAASG,YAAYC,KAAa,EAAEC,UAAmB,EAAEC,UAAmB;IACjF,IA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AI,CAAChB,cAAcC;gBAC/BA,aAAauC,YAAY1C,MAAM;YACjC,OAAO;gBACLI,yBAAAA,mCAAAA,aAAce,IAAI,CAACuB;YACrB;YAEArC,UAAUyB;QACZ,OAAO;YACL,MAAM,IAAIvC,MAAM,CAAC,8BAA8B,EAAEoB,QAAQiC,QAAQ,CAAC,KAAK;QACzE;IACF;IAEA,qDAAqD;IACrD,IAAI1C,cAAc;QAChB,4DAA4D;QAC5D,OAAOC,YAAYD,aAAaF,MAAM,GAAGE,aAAae,KAAK,CAAC,GAAGd,aAAaD;IAC9E;IACA,OAAO2C,OAAOC,MAAM,CAAC1C;AACvB;AAEA;;;;;CAKC,GACD,OAAO,SAAS2C,mBAAmBjD,UAAmB,EAAEC,UAAmB;IACzE,gFAAgF;IAChF,IAAIhB,iBAAiBe,cAAcA,WAAWE,MAAM,IAAI,GAAG;QACzD,MAAMC,WAAWZ,qBAAqBS,UAAU,CAAC,EAAE;QACnD,MAAMkD,gBAAgBlE,yBAAyBmB;QAC/C,IAAI+C,eAAe;YACjB,OAAOA;QACT;IACF;IAEA,6DAA6D;IAC7D,OAAOnE,uBAAuBe,aAAaE,YAAYC;AACzD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA2 codec - uses native lzma-native when available, falls back to lzma-purejs\n// LZMA2 is a container format that wraps LZMA chunks with framing\n//\n// LZMA2 format specification:\n// https://github.com/ulikunitz/xz/blob/master/doc/LZMA2.md\n//\n// Control byte values:\n// 0x00 = End of stream\n// 0x01 = Uncompressed chunk, dictionary reset\n// 0x02 = Uncompressed chunk, no dictionary reset\n// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)\n//\n// Native optimization: On Node.js 8+, lzma-native provides liblzma bindings\n// that decode LZMA2 streams natively for better performance.\n// Falls back to lzma-purejs for Node.js 0.8-7.x compatibility.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createNativeLzma2Decoder, hasNativeLzma } from './lzmaCompat.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\n// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\n// Path accounts for build output in dist/esm/sevenz/codecs/\nconst { LZMA } = _require('../../../../assets/lzma-purejs');\nconst LzmaDecoder = LZMA.Decoder;\n\n/**\n * Decode LZMA2 dictionary size from properties byte\n * Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar\n *\n * Per XZ spec, dictionary sizes are:\n * 0x00 = 4 KiB (2^12)\n * 0x01 = 6 KiB\n * 0x02 = 8 KiB (2^13)\n * ...\n * 0x28 = 1.5 GiB\n */\nfunction decodeDictionarySize(propByte: number): number {\n if (propByte > 40) {\n throw new Error(`Invalid LZMA2 dictionary size property: ${propByte}`);\n }\n if (propByte === 40) {\n // Max dictionary size: 4 GiB - 1\n return 0xffffffff;\n }\n // Dictionary size = 2 | (propByte & 1) << (propByte / 2 + 11)\n const base = 2 | (propByte & 1);\n const shift = Math.floor(propByte / 2) + 11;\n return base << shift;\n}\n\n/**\n * Decode LZMA2 compressed data to buffer\n *\n * @param input - LZMA2 compressed data\n * @param properties - Properties buffer (1 byte: dictionary size)\n * @param unpackSize - Expected output size (used for pre-allocation to reduce memory)\n * @returns Decompressed data\n */\nexport function decodeLzma2(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 1) {\n throw new Error('LZMA2 requires properties byte');\n }\n\n const dictSize = decodeDictionarySize(properties[0]);\n\n // Memory optimization: pre-allocate output buffer if size is known\n // This avoids double-memory during Buffer.concat\n let outputBuffer: Buffer | null = null;\n let outputPos = 0;\n const outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n let offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The vendored decoder supports setSolid() for LZMA2 state preservation\n // The decoder also has _nowPos64 which tracks cumulative position for rep0 validation\n // and _prevByte which is used for literal decoder context selection\n const decoder = new LzmaDecoder() as InstanceType<typeof LzmaDecoder> & {\n setSolid: (solid: boolean) => void;\n resetProbabilities: () => void;\n _nowPos64: number;\n _prevByte: number;\n };\n decoder.setDictionarySize(dictSize);\n\n // Access internal _outWindow for dictionary management\n // We need to preserve dictionary state across LZMA2 chunks\n type OutWindowType = {\n _buffer: Buffer;\n _pos: number;\n _streamPos: number;\n _windowSize: number;\n init: (solid: boolean) => void;\n };\n const outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n let propsSet = false;\n\n while (offset < input.length) {\n const control = input[offset++];\n\n if (control === 0x00) {\n // End of LZMA2 stream\n break;\n }\n\n if (control === 0x01 || control === 0x02) {\n // Uncompressed chunk\n // 0x01 = dictionary reset + uncompressed\n // 0x02 = uncompressed (no reset)\n\n // Handle dictionary reset for 0x01\n if (control === 0x01) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n decoder._nowPos64 = 0;\n }\n\n if (offset + 2 > input.length) {\n throw new Error('Truncated LZMA2 uncompressed chunk header');\n }\n\n // Size is big-endian, 16-bit, value + 1\n const uncompSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n if (offset + uncompSize > input.length) {\n throw new Error('Truncated LZMA2 uncompressed data');\n }\n\n // Get the uncompressed data\n const uncompData = input.slice(offset, offset + uncompSize);\n\n // Copy uncompressed data to output\n if (outputBuffer) {\n uncompData.copy(outputBuffer, outputPos);\n outputPos += uncompData.length;\n } else {\n outputChunks?.push(uncompData);\n }\n\n // Also update the decoder's internal dictionary so subsequent LZMA chunks can reference it\n // The decoder needs to track this data for LZ77 back-references\n // We write directly to _buffer to avoid flush() which requires _stream to be set\n // We must also update _streamPos to match _pos so that flush() doesn't try to write\n for (let i = 0; i < uncompData.length; i++) {\n outWindow._buffer[outWindow._pos++] = uncompData[i];\n // Handle circular buffer wrap-around\n if (outWindow._pos >= outWindow._windowSize) {\n outWindow._pos = 0;\n }\n }\n // Keep _streamPos in sync so flush() doesn't try to write these bytes\n // (they're already in our output buffer)\n outWindow._streamPos = outWindow._pos;\n\n // Update decoder's cumulative position so subsequent LZMA chunks have correct rep0 validation\n decoder._nowPos64 += uncompSize;\n\n // Update prevByte for literal decoder context in subsequent LZMA chunks\n decoder._prevByte = uncompData[uncompData.length - 1];\n\n offset += uncompSize;\n } else if (control >= 0x80) {\n // LZMA compressed chunk\n // Control byte format (bits 7-0):\n // Bit 7: always 1 for LZMA chunk\n // Bits 6-5: reset mode (00=nothing, 01=state, 10=state+props, 11=all)\n // Bits 4-0: high 5 bits of uncompressed size - 1\n\n // Control byte ranges (based on bits 6-5):\n // 0x80-0x9F (00): no reset - continue existing state (solid mode)\n // 0xA0-0xBF (01): reset state only\n // 0xC0-0xDF (10): reset state + new properties\n // 0xE0-0xFF (11): reset dictionary + state + new properties\n const resetState = control >= 0xa0;\n const newProps = control >= 0xc0;\n const dictReset = control >= 0xe0;\n const useSolidMode = !resetState;\n\n // Handle dictionary reset for control bytes 0xE0-0xFF\n if (dictReset) {\n outWindow._pos = 0;\n outWindow._streamPos = 0;\n }\n\n if (offset + 4 > input.length) {\n throw new Error('Truncated LZMA2 LZMA chunk header');\n }\n\n // Uncompressed size: 5 bits from control + 16 bits from next 2 bytes + 1\n const uncompHigh = control & 0x1f;\n const uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n const compSize = ((input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // If new properties, read 1-byte LZMA properties\n if (newProps) {\n if (offset >= input.length) {\n throw new Error('Truncated LZMA2 properties byte');\n }\n const propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n const lc = propsByte % 9;\n const remainder = Math.floor(propsByte / 9);\n const lp = remainder % 5;\n const pb = Math.floor(remainder / 5);\n\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 }\n\n if (!propsSet) {\n throw new Error('LZMA chunk without properties');\n }\n\n if (offset + compSize > input.length) {\n throw new Error('Truncated LZMA2 compressed data');\n }\n\n // Decode LZMA chunk\n const inStream = createInputStream(input, offset, compSize);\n const outStream = createOutputStream(uncompSize2); // Pre-allocate for memory efficiency\n\n // Set solid mode based on control byte - this preserves state across code() calls\n // For state reset WITHOUT dict reset (0xa0-0xdf), use resetProbabilities() to\n // reset probability tables while preserving _nowPos64 for dictionary references\n if (resetState && !dictReset) {\n decoder.resetProbabilities();\n decoder.setSolid(true); // Preserve _nowPos64 in code()\n } else {\n decoder.setSolid(useSolidMode);\n }\n\n // Decode the chunk\n const success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n const chunkOutput = outStream.toBuffer();\n if (outputBuffer) {\n chunkOutput.copy(outputBuffer, outputPos);\n outputPos += chunkOutput.length;\n } else {\n outputChunks?.push(chunkOutput);\n }\n\n offset += compSize;\n } else {\n throw new Error(`Invalid LZMA2 control byte: 0x${control.toString(16)}`);\n }\n }\n\n // Return pre-allocated buffer or concatenated chunks\n if (outputBuffer) {\n // Return only the used portion if we didn't fill the buffer\n return outputPos < outputBuffer.length ? outputBuffer.slice(0, outputPos) : outputBuffer;\n }\n return Buffer.concat(outputChunks);\n}\n\n/**\n * Create an LZMA2 decoder Transform stream\n *\n * Uses native lzma-native when available for better performance,\n * falls back to lzma-purejs buffering decoder for Node.js 0.8+ compatibility.\n */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n // Try native decoder first (available on Node.js 8+ with lzma-native installed)\n if (hasNativeLzma && properties && properties.length >= 1) {\n const dictSize = decodeDictionarySize(properties[0]);\n const nativeDecoder = createNativeLzma2Decoder(dictSize);\n if (nativeDecoder) {\n return nativeDecoder;\n }\n }\n\n // Fall back to buffering decoder with pure JS implementation\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["Module","_require","require","createRequire","url","allocBufferUnsafe","createBufferingDecoder","createNativeLzma2Decoder","hasNativeLzma","createInputStream","createOutputStream","LZMA","LzmaDecoder","Decoder","decodeDictionarySize","propByte","Error","base","shift","Math","floor","decodeLzma2","input","properties","unpackSize","length","dictSize","outputBuffer","outputPos","outputChunks","offset","decoder","setDictionarySize","outWindow","_outWindow","propsSet","control","_pos","_streamPos","_nowPos64","uncompSize","uncompData","slice","copy","push","i","_buffer","_windowSize","_prevByte","resetState","newProps","dictReset","useSolidMode","uncompHigh","uncompSize2","compSize","propsByte","lc","remainder","lp","pb","setLcLpPb","inStream","outStream","resetProbabilities","setSolid","success","code","chunkOutput","toBuffer","toString","Buffer","concat","createLzma2Decoder","nativeDecoder"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAE5B,MAAMC,WAAW,OAAOC,YAAY,cAAcF,OAAOG,aAAa,CAAC,YAAYC,GAAG,IAAIF;AAE1F,kFAAkF;AAClF,kEAAkE;AAClE,EAAE;AACF,8BAA8B;AAC9B,2DAA2D;AAC3D,EAAE;AACF,uBAAuB;AACvB,+BAA+B;AAC/B,sDAAsD;AACtD,yDAAyD;AACzD,0EAA0E;AAC1E,EAAE;AACF,4EAA4E;AAC5E,6DAA6D;AAC7D,+DAA+D;AAE/D,SAASG,iBAAiB,QAAQ,wBAAwB;AAE1D,OAAOC,4BAA4B,8BAA8B;AACjE,SAASC,wBAAwB,EAAEC,aAAa,QAAQ,kBAAkB;AAC1E,SAASC,iBAAiB,EAAEC,kBAAkB,QAAQ,eAAe;AAErE,sFAAsF;AACtF,4DAA4D;AAC5D,MAAM,EAAEC,IAAI,EAAE,GAAGV,SAAS;AAC1B,MAAMW,cAAcD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;IAEA,6DAA6D;IAC7D,OAAOpE,uBAAuBe,aAAaE,YAAYC;AACzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "7z-iterator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Extract contents from 7z archives using an iterator API. Pure JavaScript, works on Node.js 0.8+",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"extract",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"call-once-fn": "^1.1.6",
|
|
51
|
-
"extract-base-iterator": "^2.
|
|
51
|
+
"extract-base-iterator": "^2.6.1",
|
|
52
52
|
"fs-remove-compat": "^1.0.0",
|
|
53
53
|
"lodash.compact": "^3.0.1",
|
|
54
54
|
"mkdirp-classic": "^0.5.2",
|