7z-iterator 0.1.6 → 0.1.7
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/assets/lzma-purejs/LICENSE +11 -0
- package/assets/lzma-purejs/index.js +19 -0
- package/assets/lzma-purejs/lib/LZ/OutWindow.js +78 -0
- package/assets/lzma-purejs/lib/LZ.js +6 -0
- package/assets/lzma-purejs/lib/LZMA/Base.js +48 -0
- package/assets/lzma-purejs/lib/LZMA/Decoder.js +296 -0
- package/assets/lzma-purejs/lib/LZMA.js +6 -0
- package/assets/lzma-purejs/lib/RangeCoder/BitTreeDecoder.js +41 -0
- package/assets/lzma-purejs/lib/RangeCoder/Decoder.js +58 -0
- package/assets/lzma-purejs/lib/RangeCoder/Encoder.js +106 -0
- package/assets/lzma-purejs/lib/RangeCoder.js +10 -0
- package/assets/lzma-purejs/lib/Stream.js +41 -0
- package/assets/lzma-purejs/lib/Util.js +114 -0
- package/assets/lzma-purejs/lib/makeBuffer.js +14 -0
- package/assets/lzma-purejs/package.json +8 -0
- package/dist/cjs/sevenz/codecs/Lzma.js +6 -5
- package/dist/cjs/sevenz/codecs/Lzma.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Lzma2.js +7 -19
- package/dist/cjs/sevenz/codecs/Lzma2.js.map +1 -1
- package/dist/esm/sevenz/codecs/Lzma.js +6 -5
- package/dist/esm/sevenz/codecs/Lzma.js.map +1 -1
- package/dist/esm/sevenz/codecs/Lzma2.js +10 -8
- package/dist/esm/sevenz/codecs/Lzma2.js.map +1 -1
- package/package.json +2 -5
- package/patches/lzma-purejs+0.9.3.patch +0 -196
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/* very simple input/output stream interface */
|
|
4
|
+
var Stream = function() {
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// input streams //////////////
|
|
8
|
+
Stream.prototype.readByte = function() {
|
|
9
|
+
throw new Error("abstract method readByte() not implemented");
|
|
10
|
+
};
|
|
11
|
+
Stream.prototype.read = function(buffer, bufOffset, length) {
|
|
12
|
+
var bytesRead = 0;
|
|
13
|
+
while (bytesRead < length) {
|
|
14
|
+
var c = this.readByte();
|
|
15
|
+
if (c < 0) {
|
|
16
|
+
return (bytesRead===0) ? -1 : bytesRead;
|
|
17
|
+
}
|
|
18
|
+
buffer[bufOffset++] = c;
|
|
19
|
+
bytesRead++;
|
|
20
|
+
}
|
|
21
|
+
return bytesRead;
|
|
22
|
+
};
|
|
23
|
+
Stream.prototype.seek = function(new_pos) {
|
|
24
|
+
throw new Error("abstract method seek() not implemented");
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// output streams ///////////
|
|
28
|
+
Stream.prototype.writeByte = function(_byte) {
|
|
29
|
+
throw new Error("abstract method writeByte() not implemented");
|
|
30
|
+
};
|
|
31
|
+
Stream.prototype.write = function(buffer, bufOffset, length) {
|
|
32
|
+
var i;
|
|
33
|
+
for (i=0; i<length; i++) {
|
|
34
|
+
this.writeByte(buffer[bufOffset++]);
|
|
35
|
+
}
|
|
36
|
+
return length;
|
|
37
|
+
};
|
|
38
|
+
Stream.prototype.flush = function() {
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
module.exports = Stream;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var LZMA = require('./LZMA');
|
|
3
|
+
var Stream = require('./Stream');
|
|
4
|
+
|
|
5
|
+
var coerceInputStream = function(input) {
|
|
6
|
+
if ('readByte' in input) { return input; }
|
|
7
|
+
var inputStream = new Stream();
|
|
8
|
+
inputStream.pos = 0;
|
|
9
|
+
inputStream.size = input.length;
|
|
10
|
+
inputStream.readByte = function() {
|
|
11
|
+
return this.eof() ? -1 : input[this.pos++];
|
|
12
|
+
};
|
|
13
|
+
inputStream.read = function(buffer, bufOffset, length) {
|
|
14
|
+
var bytesRead = 0;
|
|
15
|
+
while (bytesRead < length && this.pos < input.length) {
|
|
16
|
+
buffer[bufOffset++] = input[this.pos++];
|
|
17
|
+
bytesRead++;
|
|
18
|
+
}
|
|
19
|
+
return bytesRead;
|
|
20
|
+
};
|
|
21
|
+
inputStream.seek = function(pos) { this.pos = pos; };
|
|
22
|
+
inputStream.eof = function() { return this.pos >= input.length; };
|
|
23
|
+
return inputStream;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
var coerceOutputStream = function(output) {
|
|
27
|
+
var outputStream = new Stream();
|
|
28
|
+
var resizeOk = true;
|
|
29
|
+
if (output) {
|
|
30
|
+
if (typeof(output)==='number') {
|
|
31
|
+
outputStream.buffer = Buffer.alloc(output);
|
|
32
|
+
resizeOk = false;
|
|
33
|
+
} else if ('writeByte' in output) {
|
|
34
|
+
return output;
|
|
35
|
+
} else {
|
|
36
|
+
outputStream.buffer = output;
|
|
37
|
+
resizeOk = false;
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
outputStream.buffer = Buffer.alloc(16384);
|
|
41
|
+
}
|
|
42
|
+
outputStream.pos = 0;
|
|
43
|
+
outputStream.writeByte = function(_byte) {
|
|
44
|
+
if (resizeOk && this.pos >= this.buffer.length) {
|
|
45
|
+
var newBuffer = Buffer.alloc(this.buffer.length*2);
|
|
46
|
+
this.buffer.copy(newBuffer);
|
|
47
|
+
this.buffer = newBuffer;
|
|
48
|
+
}
|
|
49
|
+
this.buffer[this.pos++] = _byte;
|
|
50
|
+
};
|
|
51
|
+
outputStream.getBuffer = function() {
|
|
52
|
+
if (this.pos !== this.buffer.length) {
|
|
53
|
+
if (!resizeOk)
|
|
54
|
+
throw new TypeError('outputsize does not match decoded input');
|
|
55
|
+
var newBuffer = Buffer.alloc(this.pos);
|
|
56
|
+
this.buffer.copy(newBuffer, 0, 0, this.pos);
|
|
57
|
+
this.buffer = newBuffer;
|
|
58
|
+
}
|
|
59
|
+
return this.buffer;
|
|
60
|
+
};
|
|
61
|
+
outputStream._coerced = true;
|
|
62
|
+
return outputStream;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
var Util = {};
|
|
66
|
+
|
|
67
|
+
Util.decompress = function(properties, inStream, outStream, outSize){
|
|
68
|
+
var decoder = new LZMA.Decoder();
|
|
69
|
+
if (!decoder.setDecoderProperties(properties)) {
|
|
70
|
+
throw new Error("Incorrect stream properties");
|
|
71
|
+
}
|
|
72
|
+
if (!decoder.code(inStream, outStream, outSize)) {
|
|
73
|
+
throw new Error("Error in data stream");
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
Util.decompressFile = function(inStream, outStream){
|
|
79
|
+
var decoder = new LZMA.Decoder(), i, mult;
|
|
80
|
+
inStream = coerceInputStream(inStream);
|
|
81
|
+
if (!decoder.setDecoderPropertiesFromStream(inStream)) {
|
|
82
|
+
throw new Error("Incorrect stream properties");
|
|
83
|
+
}
|
|
84
|
+
var outSizeLo = 0;
|
|
85
|
+
for (i=0, mult=1; i<4; i++, mult*=256) {
|
|
86
|
+
outSizeLo += (inStream.readByte() * mult);
|
|
87
|
+
}
|
|
88
|
+
var outSizeHi = 0;
|
|
89
|
+
for (i=0, mult=1; i<4; i++, mult*=256) {
|
|
90
|
+
outSizeHi += (inStream.readByte() * mult);
|
|
91
|
+
}
|
|
92
|
+
var outSize = outSizeLo + (outSizeHi * 0x100000000);
|
|
93
|
+
if (outSizeLo === 0xFFFFFFFF && outSizeHi === 0xFFFFFFFF) {
|
|
94
|
+
outSize = -1;
|
|
95
|
+
} else if (outSizeHi >= 0x200000) {
|
|
96
|
+
outSize = -1;
|
|
97
|
+
}
|
|
98
|
+
if (outSize >= 0 && !outStream) { outStream = outSize; }
|
|
99
|
+
outStream = coerceOutputStream(outStream);
|
|
100
|
+
if (!decoder.code(inStream, outStream, outSize)) {
|
|
101
|
+
throw new Error("Error in data stream");
|
|
102
|
+
}
|
|
103
|
+
return ('getBuffer' in outStream) ? outStream.getBuffer() : true;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
Util.compress = function(inStream, outStream, props, progress){
|
|
107
|
+
throw new Error("Compression not supported - decoder only");
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
Util.compressFile = function(inStream, outStream, props, progress) {
|
|
111
|
+
throw new Error("Compression not supported - decoder only");
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
module.exports = Util;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Node 0.8 compatible buffer allocation
|
|
4
|
+
var makeBuffer = function(len) {
|
|
5
|
+
if (Buffer.alloc) {
|
|
6
|
+
return Buffer.alloc(len);
|
|
7
|
+
}
|
|
8
|
+
// Node 0.8 fallback
|
|
9
|
+
var buf = new Buffer(len);
|
|
10
|
+
buf.fill(0);
|
|
11
|
+
return buf;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
module.exports = makeBuffer;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// LZMA codec - uses lzma-purejs for raw LZMA decompression
|
|
2
|
-
// LZMA properties in 7z are 5 bytes: 1 byte lc/lp/pb + 4 bytes dictionary size (little-endian)
|
|
3
|
-
// Import lzma-purejs - provides raw LZMA decoder
|
|
4
1
|
"use strict";
|
|
5
2
|
Object.defineProperty(exports, "__esModule", {
|
|
6
3
|
value: true
|
|
@@ -19,7 +16,7 @@ _export(exports, {
|
|
|
19
16
|
return decodeLzma;
|
|
20
17
|
}
|
|
21
18
|
});
|
|
22
|
-
var
|
|
19
|
+
var _module = /*#__PURE__*/ _interop_require_default(require("module"));
|
|
23
20
|
var _createBufferingDecoderts = /*#__PURE__*/ _interop_require_default(require("./createBufferingDecoder.js"));
|
|
24
21
|
var _streamsts = require("./streams.js");
|
|
25
22
|
function _interop_require_default(obj) {
|
|
@@ -27,7 +24,11 @@ function _interop_require_default(obj) {
|
|
|
27
24
|
default: obj
|
|
28
25
|
};
|
|
29
26
|
}
|
|
30
|
-
var
|
|
27
|
+
var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require;
|
|
28
|
+
// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
29
|
+
// Path accounts for build output in dist/esm/sevenz/codecs/
|
|
30
|
+
var LZMA = _require('../../../../assets/lzma-purejs').LZMA;
|
|
31
|
+
var LzmaDecoder = LZMA.Decoder;
|
|
31
32
|
function decodeLzma(input, properties, unpackSize) {
|
|
32
33
|
if (!properties || properties.length < 5) {
|
|
33
34
|
throw new Error('LZMA requires 5-byte properties');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma.ts"],"sourcesContent":["// LZMA codec - uses lzma-purejs for raw LZMA decompression\n// LZMA properties in 7z are 5 bytes: 1 byte lc/lp/pb + 4 bytes dictionary size (little-endian)\n\
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA codec - uses vendored lzma-purejs for raw LZMA decompression\n// LZMA properties in 7z are 5 bytes: 1 byte lc/lp/pb + 4 bytes dictionary size (little-endian)\n\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.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 LZMA compressed data to buffer\n *\n * @param input - LZMA compressed data\n * @param properties - Properties buffer (5 bytes: lc/lp/pb + dict size)\n * @param unpackSize - Expected output size (optional, -1 for unknown)\n * @returns Decompressed data\n */\nexport function decodeLzma(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 5) {\n throw new Error('LZMA requires 5-byte properties');\n }\n\n var decoder = new LzmaDecoder();\n\n // setDecoderProperties expects array-like with 5 bytes\n if (!decoder.setDecoderProperties(properties)) {\n throw new Error('Invalid LZMA properties');\n }\n\n var inStream = createInputStream(input, 0, input.length);\n\n // Use -1 for unknown size (decoder will use end marker)\n var size = typeof unpackSize === 'number' ? unpackSize : -1;\n\n // Pre-allocate output stream if size is known (memory optimization)\n var outStream = createOutputStream(size > 0 ? size : undefined);\n\n var success = decoder.code(inStream, outStream, size);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n return outStream.toBuffer();\n}\n\n/**\n * Create an LZMA decoder Transform stream\n */\nexport function createLzmaDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeLzma, properties, unpackSize);\n}\n"],"names":["createLzmaDecoder","decodeLzma","_require","require","Module","createRequire","LZMA","LzmaDecoder","Decoder","input","properties","unpackSize","length","Error","decoder","setDecoderProperties","inStream","createInputStream","size","outStream","createOutputStream","undefined","success","code","toBuffer","createBufferingDecoder"],"mappings":";;;;;;;;;;;QAuDgBA;eAAAA;;QA/BAC;eAAAA;;;6DAxBG;+EAQgB;yBACmB;;;;;;AAPtD,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAS1F,sFAAsF;AACtF,4DAA4D;AAC5D,IAAM,AAAEG,OAASJ,SAAS,kCAAlBI;AACR,IAAMC,cAAcD,KAAKE,OAAO;AAUzB,SAASP,WAAWQ,KAAa,EAAEC,UAAmB,EAAEC,UAAmB;IAChF,IAAI,CAACD,cAAcA,WAAWE,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAIC,UAAU,IAAIP;IAElB,uDAAuD;IACvD,IAAI,CAACO,QAAQC,oBAAoB,CAACL,aAAa;QAC7C,MAAM,IAAIG,MAAM;IAClB;IAEA,IAAIG,WAAWC,IAAAA,4BAAiB,EAACR,OAAO,GAAGA,MAAMG,MAAM;IAEvD,wDAAwD;IACxD,IAAIM,OAAO,OAAOP,eAAe,WAAWA,aAAa,CAAC;IAE1D,oEAAoE;IACpE,IAAIQ,YAAYC,IAAAA,6BAAkB,EAACF,OAAO,IAAIA,OAAOG;IAErD,IAAIC,UAAUR,QAAQS,IAAI,CAACP,UAAUG,WAAWD;IAChD,IAAI,CAACI,SAAS;QACZ,MAAM,IAAIT,MAAM;IAClB;IAEA,OAAOM,UAAUK,QAAQ;AAC3B;AAKO,SAASxB,kBAAkBU,UAAmB,EAAEC,UAAmB;IACxE,OAAOc,IAAAA,iCAAsB,EAACxB,YAAYS,YAAYC;AACxD"}
|
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
// LZMA2 codec - wrapper around lzma-purejs for LZMA2 decompression
|
|
2
|
-
// LZMA2 is a container format that wraps LZMA chunks with framing
|
|
3
|
-
//
|
|
4
|
-
// LZMA2 format specification:
|
|
5
|
-
// https://github.com/ulikunitz/xz/blob/master/doc/LZMA2.md
|
|
6
|
-
//
|
|
7
|
-
// Control byte values:
|
|
8
|
-
// 0x00 = End of stream
|
|
9
|
-
// 0x01 = Uncompressed chunk, dictionary reset
|
|
10
|
-
// 0x02 = Uncompressed chunk, no dictionary reset
|
|
11
|
-
// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)
|
|
12
|
-
//
|
|
13
|
-
// Note: lzma-purejs is patched via patch-package to support LZMA2 state preservation.
|
|
14
|
-
// The patch adds setSolid(true/false) method to control whether state is preserved
|
|
15
|
-
// across code() calls.
|
|
16
|
-
// Import lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
17
1
|
"use strict";
|
|
18
2
|
Object.defineProperty(exports, "__esModule", {
|
|
19
3
|
value: true
|
|
@@ -32,8 +16,8 @@ _export(exports, {
|
|
|
32
16
|
return decodeLzma2;
|
|
33
17
|
}
|
|
34
18
|
});
|
|
19
|
+
var _module = /*#__PURE__*/ _interop_require_default(require("module"));
|
|
35
20
|
var _extractbaseiterator = require("extract-base-iterator");
|
|
36
|
-
var _lzmapurejs = /*#__PURE__*/ _interop_require_default(require("lzma-purejs"));
|
|
37
21
|
var _createBufferingDecoderts = /*#__PURE__*/ _interop_require_default(require("./createBufferingDecoder.js"));
|
|
38
22
|
var _streamsts = require("./streams.js");
|
|
39
23
|
function _interop_require_default(obj) {
|
|
@@ -41,7 +25,11 @@ function _interop_require_default(obj) {
|
|
|
41
25
|
default: obj
|
|
42
26
|
};
|
|
43
27
|
}
|
|
44
|
-
var
|
|
28
|
+
var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require;
|
|
29
|
+
// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
30
|
+
// Path accounts for build output in dist/esm/sevenz/codecs/
|
|
31
|
+
var LZMA = _require('../../../../assets/lzma-purejs').LZMA;
|
|
32
|
+
var LzmaDecoder = LZMA.Decoder;
|
|
45
33
|
/**
|
|
46
34
|
* Decode LZMA2 dictionary size from properties byte
|
|
47
35
|
* Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar
|
|
@@ -80,7 +68,7 @@ function decodeLzma2(input, properties, unpackSize) {
|
|
|
80
68
|
}
|
|
81
69
|
var offset = 0;
|
|
82
70
|
// LZMA decoder instance - reused across chunks
|
|
83
|
-
// The decoder
|
|
71
|
+
// The vendored decoder supports setSolid() for LZMA2 state preservation
|
|
84
72
|
// The decoder also has _nowPos64 which tracks cumulative position for rep0 validation
|
|
85
73
|
// and _prevByte which is used for literal decoder context selection
|
|
86
74
|
var decoder = new LzmaDecoder();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["// LZMA2 codec - wrapper around lzma-purejs for LZMA2 decompression\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// Note: lzma-purejs is patched via patch-package to support LZMA2 state preservation.\n// The patch adds setSolid(true/false) method to control whether state is preserved\n// across code() calls.\n\n// Import lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport lzmajs from 'lzma-purejs';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\nvar LzmaDecoder = lzmajs.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 var base = 2 | (propByte & 1);\n var 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 var 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 var outputBuffer: Buffer | null = null;\n var outputPos = 0;\n var outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n var offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The decoder is patched via patch-package to support 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 var 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 var outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n var propsSet = false;\n\n while (offset < input.length) {\n var 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 var 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 var 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 (var 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 var resetState = control >= 0xa0;\n var newProps = control >= 0xc0;\n var dictReset = control >= 0xe0;\n var 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 var uncompHigh = control & 0x1f;\n var uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n var 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 var propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n var lc = propsByte % 9;\n var remainder = Math.floor(propsByte / 9);\n var lp = remainder % 5;\n var 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 var inStream = createInputStream(input, offset, compSize);\n var 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 var success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n var 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 */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["createLzma2Decoder","decodeLzma2","LzmaDecoder","lzmajs","LZMA","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","createBufferingDecoder"],"mappings":"AAAA,mEAAmE;AACnE,kEAAkE;AAClE,EAAE;AACF,8BAA8B;AAC9B,2DAA2D;AAC3D,EAAE;AACF,uBAAuB;AACvB,+BAA+B;AAC/B,sDAAsD;AACtD,yDAAyD;AACzD,0EAA0E;AAC1E,EAAE;AACF,sFAAsF;AACtF,mFAAmF;AACnF,uBAAuB;AAEvB,6EAA6E;;;;;;;;;;;;QA6P7DA;eAAAA;;QAnNAC;eAAAA;;;mCAzCkB;iEACf;+EAEgB;yBACmB;;;;;;AAEtD,IAAIC,cAAcC,mBAAM,CAACC,IAAI,CAACC,OAAO;AAErC;;;;;;;;;;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;IAC9D,IAAIE,OAAO,IAAKF,WAAW;IAC3B,IAAIG,QAAQC,KAAKC,KAAK,CAACL,WAAW,KAAK;IACvC,OAAOE,QAAQC;AACjB;AAUO,SAAST,YAAYY,KAAa,EAAEC,UAAmB,EAAEC,UAAmB;IACjF,IAAI,CAACD,cAAcA,WAAWE,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIR,MAAM;IAClB;IAEA,IAAIS,WAAWX,qBAAqBQ,UAAU,CAAC,EAAE;IAEjD,mEAAmE;IACnE,iDAAiD;IACjD,IAAII,eAA8B;IAClC,IAAIC,YAAY;IAChB,IAAIC,e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AArBoB,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;AAKO,SAASpB,mBAAmBc,UAAmB,EAAEC,UAAmB;IACzE,OAAOmD,IAAAA,iCAAsB,EAACjE,aAAaa,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 - wrapper around vendored lzma-purejs for LZMA2 decompression\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// Note: vendored lzma-purejs includes LZMA2 state preservation support.\n// The setSolid(true/false) method controls whether state is preserved across code() calls.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.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 var base = 2 | (propByte & 1);\n var 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 var 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 var outputBuffer: Buffer | null = null;\n var outputPos = 0;\n var outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n var 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 var 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 var outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n var propsSet = false;\n\n while (offset < input.length) {\n var 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 var 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 var 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 (var 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 var resetState = control >= 0xa0;\n var newProps = control >= 0xc0;\n var dictReset = control >= 0xe0;\n var 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 var uncompHigh = control & 0x1f;\n var uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n var 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 var propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n var lc = propsByte % 9;\n var remainder = Math.floor(propsByte / 9);\n var lp = remainder % 5;\n var 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 var inStream = createInputStream(input, offset, compSize);\n var 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 var success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n var 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 */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\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","createBufferingDecoder"],"mappings":";;;;;;;;;;;QAiRgBA;eAAAA;;QAnNAC;eAAAA;;;6DA9DG;mCAmBe;+EAEC;yBACmB;;;;;;AApBtD,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAsB1F,sFAAsF;AACtF,4DAA4D;AAC5D,IAAM,AAAEG,OAASJ,SAAS,kCAAlBI;AACR,IAAMC,cAAcD,KAAKE,OAAO;AAEhC;;;;;;;;;;CAUC,GACD,SAASC,qBAAqBC,QAAgB;IAC5C,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AiB,EAAC1C,OAAOS,QAAQyB;YAChD,IAAIS,YAAYC,IAAAA,6BAAkB,EAACX,cAAc,qCAAqC;YAEtF,kFAAkF;YAClFvB,QAAQmC,QAAQ,CAACd;YAEjB,mBAAmB;YACnB,IAAIe,UAAUpC,QAAQqC,IAAI,CAACN,UAAUE,WAAWV;YAChD,IAAI,CAACa,SAAS;gBACZ,MAAM,IAAInD,MAAM;YAClB;YAEA,IAAIqD,cAAcL,UAAUM,QAAQ;YACpC,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;AAKO,SAASvB,mBAAmBiB,UAAmB,EAAEC,UAAmB;IACzE,OAAOmD,IAAAA,iCAAsB,EAACpE,aAAagB,YAAYC;AACzD"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// Import lzma-purejs - provides raw LZMA decoder
|
|
4
|
-
import lzmajs from 'lzma-purejs';
|
|
1
|
+
import Module from 'module';
|
|
2
|
+
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
|
|
5
3
|
import createBufferingDecoder from './createBufferingDecoder.js';
|
|
6
4
|
import { createInputStream, createOutputStream } from './streams.js';
|
|
7
|
-
|
|
5
|
+
// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
6
|
+
// Path accounts for build output in dist/esm/sevenz/codecs/
|
|
7
|
+
const { LZMA } = _require('../../../../assets/lzma-purejs');
|
|
8
|
+
const LzmaDecoder = LZMA.Decoder;
|
|
8
9
|
/**
|
|
9
10
|
* Decode LZMA compressed data to buffer
|
|
10
11
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma.ts"],"sourcesContent":["// LZMA codec - uses lzma-purejs for raw LZMA decompression\n// LZMA properties in 7z are 5 bytes: 1 byte lc/lp/pb + 4 bytes dictionary size (little-endian)\n\
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma.ts"],"sourcesContent":["import Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\n// LZMA codec - uses vendored lzma-purejs for raw LZMA decompression\n// LZMA properties in 7z are 5 bytes: 1 byte lc/lp/pb + 4 bytes dictionary size (little-endian)\n\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.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 LZMA compressed data to buffer\n *\n * @param input - LZMA compressed data\n * @param properties - Properties buffer (5 bytes: lc/lp/pb + dict size)\n * @param unpackSize - Expected output size (optional, -1 for unknown)\n * @returns Decompressed data\n */\nexport function decodeLzma(input: Buffer, properties?: Buffer, unpackSize?: number): Buffer {\n if (!properties || properties.length < 5) {\n throw new Error('LZMA requires 5-byte properties');\n }\n\n var decoder = new LzmaDecoder();\n\n // setDecoderProperties expects array-like with 5 bytes\n if (!decoder.setDecoderProperties(properties)) {\n throw new Error('Invalid LZMA properties');\n }\n\n var inStream = createInputStream(input, 0, input.length);\n\n // Use -1 for unknown size (decoder will use end marker)\n var size = typeof unpackSize === 'number' ? unpackSize : -1;\n\n // Pre-allocate output stream if size is known (memory optimization)\n var outStream = createOutputStream(size > 0 ? size : undefined);\n\n var success = decoder.code(inStream, outStream, size);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n return outStream.toBuffer();\n}\n\n/**\n * Create an LZMA decoder Transform stream\n */\nexport function createLzmaDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeLzma, properties, unpackSize);\n}\n"],"names":["Module","_require","require","createRequire","url","createBufferingDecoder","createInputStream","createOutputStream","LZMA","LzmaDecoder","Decoder","decodeLzma","input","properties","unpackSize","length","Error","decoder","setDecoderProperties","inStream","size","outStream","undefined","success","code","toBuffer","createLzmaDecoder"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAE5B,MAAMC,WAAW,OAAOC,YAAY,cAAcF,OAAOG,aAAa,CAAC,YAAYC,GAAG,IAAIF;AAM1F,OAAOG,4BAA4B,8BAA8B;AACjE,SAASC,iBAAiB,EAAEC,kBAAkB,QAAQ,eAAe;AAErE,sFAAsF;AACtF,4DAA4D;AAC5D,MAAM,EAAEC,IAAI,EAAE,GAAGP,SAAS;AAC1B,MAAMQ,cAAcD,KAAKE,OAAO;AAEhC;;;;;;;CAOC,GACD,OAAO,SAASC,WAAWC,KAAa,EAAEC,UAAmB,EAAEC,UAAmB;IAChF,IAAI,CAACD,cAAcA,WAAWE,MAAM,GAAG,GAAG;QACxC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAIC,UAAU,IAAIR;IAElB,uDAAuD;IACvD,IAAI,CAACQ,QAAQC,oBAAoB,CAACL,aAAa;QAC7C,MAAM,IAAIG,MAAM;IAClB;IAEA,IAAIG,WAAWb,kBAAkBM,OAAO,GAAGA,MAAMG,MAAM;IAEvD,wDAAwD;IACxD,IAAIK,OAAO,OAAON,eAAe,WAAWA,aAAa,CAAC;IAE1D,oEAAoE;IACpE,IAAIO,YAAYd,mBAAmBa,OAAO,IAAIA,OAAOE;IAErD,IAAIC,UAAUN,QAAQO,IAAI,CAACL,UAAUE,WAAWD;IAChD,IAAI,CAACG,SAAS;QACZ,MAAM,IAAIP,MAAM;IAClB;IAEA,OAAOK,UAAUI,QAAQ;AAC3B;AAEA;;CAEC,GACD,OAAO,SAASC,kBAAkBb,UAAmB,EAAEC,UAAmB;IACxE,OAAOT,uBAAuBM,YAAYE,YAAYC;AACxD"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import Module from 'module';
|
|
2
|
+
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
|
|
3
|
+
// LZMA2 codec - wrapper around vendored lzma-purejs for LZMA2 decompression
|
|
2
4
|
// LZMA2 is a container format that wraps LZMA chunks with framing
|
|
3
5
|
//
|
|
4
6
|
// LZMA2 format specification:
|
|
@@ -10,15 +12,15 @@
|
|
|
10
12
|
// 0x02 = Uncompressed chunk, no dictionary reset
|
|
11
13
|
// 0x80-0xFF = LZMA compressed chunk (bits encode reset flags and size)
|
|
12
14
|
//
|
|
13
|
-
// Note: lzma-purejs
|
|
14
|
-
// The
|
|
15
|
-
// across code() calls.
|
|
16
|
-
// Import lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
15
|
+
// Note: vendored lzma-purejs includes LZMA2 state preservation support.
|
|
16
|
+
// The setSolid(true/false) method controls whether state is preserved across code() calls.
|
|
17
17
|
import { allocBufferUnsafe } from 'extract-base-iterator';
|
|
18
|
-
import lzmajs from 'lzma-purejs';
|
|
19
18
|
import createBufferingDecoder from './createBufferingDecoder.js';
|
|
20
19
|
import { createInputStream, createOutputStream } from './streams.js';
|
|
21
|
-
|
|
20
|
+
// Import vendored lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)
|
|
21
|
+
// Path accounts for build output in dist/esm/sevenz/codecs/
|
|
22
|
+
const { LZMA } = _require('../../../../assets/lzma-purejs');
|
|
23
|
+
const LzmaDecoder = LZMA.Decoder;
|
|
22
24
|
/**
|
|
23
25
|
* Decode LZMA2 dictionary size from properties byte
|
|
24
26
|
* Properties byte encodes dictionary size as: 2^(dictByte/2 + 12) or similar
|
|
@@ -64,7 +66,7 @@ var LzmaDecoder = lzmajs.LZMA.Decoder;
|
|
|
64
66
|
}
|
|
65
67
|
var offset = 0;
|
|
66
68
|
// LZMA decoder instance - reused across chunks
|
|
67
|
-
// The decoder
|
|
69
|
+
// The vendored decoder supports setSolid() for LZMA2 state preservation
|
|
68
70
|
// The decoder also has _nowPos64 which tracks cumulative position for rep0 validation
|
|
69
71
|
// and _prevByte which is used for literal decoder context selection
|
|
70
72
|
var decoder = new LzmaDecoder();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Lzma2.ts"],"sourcesContent":["// LZMA2 codec - wrapper around lzma-purejs for LZMA2 decompression\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// Note: lzma-purejs is patched via patch-package to support LZMA2 state preservation.\n// The patch adds setSolid(true/false) method to control whether state is preserved\n// across code() calls.\n\n// Import lzma-purejs - provides raw LZMA decoder (patched for LZMA2 support)\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport lzmajs from 'lzma-purejs';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\nimport { createInputStream, createOutputStream } from './streams.ts';\n\nvar LzmaDecoder = lzmajs.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 var base = 2 | (propByte & 1);\n var 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 var 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 var outputBuffer: Buffer | null = null;\n var outputPos = 0;\n var outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n var offset = 0;\n\n // LZMA decoder instance - reused across chunks\n // The decoder is patched via patch-package to support 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 var 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 var outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n var propsSet = false;\n\n while (offset < input.length) {\n var 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 var 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 var 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 (var 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 var resetState = control >= 0xa0;\n var newProps = control >= 0xc0;\n var dictReset = control >= 0xe0;\n var 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 var uncompHigh = control & 0x1f;\n var uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n var 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 var propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n var lc = propsByte % 9;\n var remainder = Math.floor(propsByte / 9);\n var lp = remainder % 5;\n var 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 var inStream = createInputStream(input, offset, compSize);\n var 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 var success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n var 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 */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["allocBufferUnsafe","lzmajs","createBufferingDecoder","createInputStream","createOutputStream","LzmaDecoder","LZMA","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"],"mappings":"AAAA,mEAAmE;AACnE,kEAAkE;AAClE,EAAE;AACF,8BAA8B;AAC9B,2DAA2D;AAC3D,EAAE;AACF,uBAAuB;AACvB,+BAA+B;AAC/B,sDAAsD;AACtD,yDAAyD;AACzD,0EAA0E;AAC1E,EAAE;AACF,sFAAsF;AACtF,mFAAmF;AACnF,uBAAuB;AAEvB,6EAA6E;AAC7E,SAASA,iBAAiB,QAAQ,wBAAwB;AAC1D,OAAOC,YAAY,c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,EAAEC,IAAI;gBACvE;gBACAzB,WAAW;YACb;YAEA,IAAI,CAACA,UAAU;gBACb,MAAM,IAAInB,MAAM;YAClB;YAEA,IAAIc,SAASyB,WAAWjC,MAAMG,MAAM,EAAE;gBACpC,MAAM,IAAIT,MAAM;YAClB;YAEA,oBAAoB;YACpB,IAAI8C,WAAWrD,kBAAkBa,OAAOQ,QAAQyB;YAChD,IAAIQ,YAAYrD,mBAAmB4C,cAAc,qCAAqC;YAEtF,kFAAkF;YAClFvB,QAAQiC,QAAQ,CAACZ;YAEjB,mBAAmB;YACnB,IAAIa,UAAUlC,QAAQmC,IAAI,CAACJ,UAAUC,WAAWT;YAChD,IAAI,CAACW,SAAS;gBACZ,MAAM,IAAIjD,MAAM;YAClB;YAEA,IAAImD,cAAcJ,UAAUK,QAAQ;YACpC,IAAIzC,cAAc;gBAChBwC,YAAYxB,IAAI,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;;CAEC,GACD,OAAO,SAAS2C,mBAAmBjD,UAAmB,EAAEC,UAAmB;IACzE,OAAOhB,uBAAuBa,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 - wrapper around vendored lzma-purejs for LZMA2 decompression\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// Note: vendored lzma-purejs includes LZMA2 state preservation support.\n// The setSolid(true/false) method controls whether state is preserved across code() calls.\n\nimport { allocBufferUnsafe } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.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 var base = 2 | (propByte & 1);\n var 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 var 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 var outputBuffer: Buffer | null = null;\n var outputPos = 0;\n var outputChunks: Buffer[] = [];\n\n if (unpackSize && unpackSize > 0) {\n outputBuffer = allocBufferUnsafe(unpackSize);\n }\n\n var 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 var 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 var outWindow = (decoder as unknown as { _outWindow: OutWindowType })._outWindow;\n\n // Track current LZMA properties (lc, lp, pb)\n var propsSet = false;\n\n while (offset < input.length) {\n var 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 var 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 var 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 (var 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 var resetState = control >= 0xa0;\n var newProps = control >= 0xc0;\n var dictReset = control >= 0xe0;\n var 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 var uncompHigh = control & 0x1f;\n var uncompSize2 = ((uncompHigh << 16) | (input[offset] << 8) | input[offset + 1]) + 1;\n offset += 2;\n\n // Compressed size: 16 bits + 1\n var 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 var propsByte = input[offset++];\n\n // Properties byte: pb * 45 + lp * 9 + lc\n // where pb, lp, lc are LZMA parameters\n var lc = propsByte % 9;\n var remainder = Math.floor(propsByte / 9);\n var lp = remainder % 5;\n var 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 var inStream = createInputStream(input, offset, compSize);\n var 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 var success = decoder.code(inStream, outStream, uncompSize2);\n if (!success) {\n throw new Error('LZMA decompression failed');\n }\n\n var 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 */\nexport function createLzma2Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeLzma2, properties, unpackSize);\n}\n"],"names":["Module","_require","require","createRequire","url","allocBufferUnsafe","createBufferingDecoder","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"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAE5B,MAAMC,WAAW,OAAOC,YAAY,cAAcF,OAAOG,aAAa,CAAC,YAAYC,GAAG,IAAIF;AAE1F,4EAA4E;AAC5E,kEAAkE;AAClE,EAAE;AACF,8BAA8B;AAC9B,2DAA2D;AAC3D,EAAE;AACF,uBAAuB;AACvB,+BAA+B;AAC/B,sDAAsD;AACtD,yDAAyD;AACzD,0EAA0E;AAC1E,EAAE;AACF,wEAAwE;AACxE,2FAA2F;AAE3F,SAASG,iBAAiB,QAAQ,wBAAwB;AAE1D,OAAOC,4BAA4B,8BAA8B;AACjE,SAASC,iBAAiB,EAAEC,kBAAkB,QAAQ,eAAe;AAErE,sFAAsF;AACtF,4DAA4D;AAC5D,MAAM,EAAEC,IAAI,EAAE,GAAGR,SAAS;AAC1B,MAAMS,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,IAAIE,OAAO,IAAKF,WAAW;IAC3B,IAAIG,QAAQC,KAAKC,KAAK,CAACL,WAAW,KAA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AACW,SAAS;gBACZ,MAAM,IAAIjD,MAAM;YAClB;YAEA,IAAImD,cAAcJ,UAAUK,QAAQ;YACpC,IAAIzC,cAAc;gBAChBwC,YAAYxB,IAAI,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;;CAEC,GACD,OAAO,SAAS2C,mBAAmBjD,UAAmB,EAAEC,UAAmB;IACzE,OAAOhB,uBAAuBa,aAAaE,YAAYC;AACzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "7z-iterator",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
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",
|
|
@@ -35,12 +35,11 @@
|
|
|
35
35
|
"types": "dist/cjs/index.d.ts",
|
|
36
36
|
"files": [
|
|
37
37
|
"dist",
|
|
38
|
-
"
|
|
38
|
+
"assets"
|
|
39
39
|
],
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsds build",
|
|
42
42
|
"format": "biome check --write --unsafe",
|
|
43
|
-
"postinstall": "patch-package",
|
|
44
43
|
"prepublishOnly": "tsds validate",
|
|
45
44
|
"test": "tsds test:node --no-timeouts",
|
|
46
45
|
"test:engines": "nvu engines tsds test:node --no-timeouts",
|
|
@@ -50,12 +49,10 @@
|
|
|
50
49
|
"call-once-fn": "*",
|
|
51
50
|
"extract-base-iterator": "*",
|
|
52
51
|
"lodash.compact": "*",
|
|
53
|
-
"lzma-purejs": "^0.9.3",
|
|
54
52
|
"mkdirp-classic": "*",
|
|
55
53
|
"on-one": "*",
|
|
56
54
|
"os-shim": "*",
|
|
57
55
|
"pako": "^1.0.11",
|
|
58
|
-
"patch-package": "*",
|
|
59
56
|
"queue-cb": "*",
|
|
60
57
|
"readable-stream": "^2.3.7",
|
|
61
58
|
"rimraf2": "*",
|