@patrickdk77/archive-defuse 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +345 -2
  3. package/dist/aes7z.d.ts +16 -0
  4. package/dist/aes7z.js +131 -0
  5. package/dist/alone.d.ts +21 -0
  6. package/dist/alone.js +65 -0
  7. package/dist/bcj2.d.ts +21 -0
  8. package/dist/bcj2.js +140 -0
  9. package/dist/bzip2.d.ts +21 -0
  10. package/dist/bzip2.js +688 -0
  11. package/dist/cab.d.ts +63 -0
  12. package/dist/cab.js +493 -0
  13. package/dist/checks.d.ts +23 -0
  14. package/dist/checks.js +123 -0
  15. package/dist/cli.d.ts +2 -0
  16. package/dist/cli.js +364 -0
  17. package/dist/codecs.d.ts +39 -0
  18. package/dist/codecs.js +124 -0
  19. package/dist/coders.d.ts +58 -0
  20. package/dist/coders.js +392 -0
  21. package/dist/crc32.d.ts +8 -0
  22. package/dist/crc32.js +59 -0
  23. package/dist/encoder.d.ts +44 -0
  24. package/dist/encoder.js +114 -0
  25. package/dist/engine.d.ts +44 -0
  26. package/dist/engine.js +753 -0
  27. package/dist/errors.d.ts +64 -0
  28. package/dist/errors.js +98 -0
  29. package/dist/filters.d.ts +34 -0
  30. package/dist/filters.js +385 -0
  31. package/dist/findings.d.ts +26 -0
  32. package/dist/findings.js +176 -0
  33. package/dist/formats.d.ts +14 -0
  34. package/dist/formats.js +97 -0
  35. package/dist/index.d.ts +8 -0
  36. package/dist/index.js +50 -0
  37. package/dist/index.mjs +3 -0
  38. package/dist/inflate64.d.ts +6 -0
  39. package/dist/inflate64.js +388 -0
  40. package/dist/io.d.ts +78 -0
  41. package/dist/io.js +379 -0
  42. package/dist/lzma-formats.d.ts +75 -0
  43. package/dist/lzma-formats.js +373 -0
  44. package/dist/lzma-sniff.d.ts +20 -0
  45. package/dist/lzma-sniff.js +73 -0
  46. package/dist/lzma-writers.d.ts +35 -0
  47. package/dist/lzma-writers.js +276 -0
  48. package/dist/lzma1/LICENSE +201 -0
  49. package/dist/lzma1/NOTICE +36 -0
  50. package/dist/lzma1/crc32.d.ts +4 -0
  51. package/dist/lzma1/crc32.js +19 -0
  52. package/dist/lzma1/hash234.d.ts +22 -0
  53. package/dist/lzma1/hash234.js +75 -0
  54. package/dist/lzma1/hc4.d.ts +30 -0
  55. package/dist/lzma1/hc4.js +142 -0
  56. package/dist/lzma1/header.d.ts +32 -0
  57. package/dist/lzma1/header.js +73 -0
  58. package/dist/lzma1/lz-decoder.d.ts +41 -0
  59. package/dist/lzma1/lz-decoder.js +128 -0
  60. package/dist/lzma1/lz-encoder.d.ts +87 -0
  61. package/dist/lzma1/lz-encoder.js +166 -0
  62. package/dist/lzma1/lzma-coder.d.ts +107 -0
  63. package/dist/lzma1/lzma-coder.js +162 -0
  64. package/dist/lzma1/lzma-decoder.d.ts +36 -0
  65. package/dist/lzma1/lzma-decoder.js +484 -0
  66. package/dist/lzma1/lzma-encoder-fast.d.ts +13 -0
  67. package/dist/lzma1/lzma-encoder-fast.js +114 -0
  68. package/dist/lzma1/lzma-encoder.d.ts +105 -0
  69. package/dist/lzma1/lzma-encoder.js +497 -0
  70. package/dist/lzma1/lzma2-decoder.d.ts +85 -0
  71. package/dist/lzma1/lzma2-decoder.js +249 -0
  72. package/dist/lzma1/range-coder.d.ts +18 -0
  73. package/dist/lzma1/range-coder.js +23 -0
  74. package/dist/lzma1/range-decoder.d.ts +41 -0
  75. package/dist/lzma1/range-decoder.js +78 -0
  76. package/dist/lzma1/range-encoder.d.ts +53 -0
  77. package/dist/lzma1/range-encoder.js +269 -0
  78. package/dist/lzma1-stream.d.ts +60 -0
  79. package/dist/lzma1-stream.js +202 -0
  80. package/dist/model.d.ts +141 -0
  81. package/dist/model.js +16 -0
  82. package/dist/names.d.ts +13 -0
  83. package/dist/names.js +66 -0
  84. package/dist/pack.d.ts +72 -0
  85. package/dist/pack.js +514 -0
  86. package/dist/plugins.d.ts +13 -0
  87. package/dist/plugins.js +55 -0
  88. package/dist/rules.d.ts +25 -0
  89. package/dist/rules.js +403 -0
  90. package/dist/score.d.ts +17 -0
  91. package/dist/score.js +58 -0
  92. package/dist/seven.d.ts +75 -0
  93. package/dist/seven.js +492 -0
  94. package/dist/sevenzip.d.ts +133 -0
  95. package/dist/sevenzip.js +529 -0
  96. package/dist/single.d.ts +103 -0
  97. package/dist/single.js +453 -0
  98. package/dist/sniff.d.ts +20 -0
  99. package/dist/sniff.js +224 -0
  100. package/dist/stream-meter.d.ts +39 -0
  101. package/dist/stream-meter.js +54 -0
  102. package/dist/tar.d.ts +102 -0
  103. package/dist/tar.js +765 -0
  104. package/dist/types.d.ts +284 -0
  105. package/dist/types.js +133 -0
  106. package/dist/unpack.d.ts +67 -0
  107. package/dist/unpack.js +194 -0
  108. package/dist/xz.d.ts +25 -0
  109. package/dist/xz.js +333 -0
  110. package/dist/zip-lzma.d.ts +15 -0
  111. package/dist/zip-lzma.js +137 -0
  112. package/dist/zip-writer.d.ts +41 -0
  113. package/dist/zip-writer.js +263 -0
  114. package/dist/zip.d.ts +99 -0
  115. package/dist/zip.js +901 -0
  116. package/dist/zipcrypt.d.ts +47 -0
  117. package/dist/zipcrypt.js +233 -0
  118. package/dist/zstd.d.ts +25 -0
  119. package/dist/zstd.js +160 -0
  120. package/package.json +94 -4
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
3
+ /**
4
+ * Decoder for raw LZMA2 data, the chunked format inside `.xz` blocks and
5
+ * 7z LZMA2 entries. LZMA2 has no header of its own. The container stores
6
+ * the dictionary size.
7
+ *
8
+ * Each chunk starts with a control byte:
9
+ *
10
+ * | control | chunk |
11
+ * | --------- | ------------------------------------------------------ |
12
+ * | 0x00 | end of the data |
13
+ * | 0x01 | uncompressed, dictionary reset |
14
+ * | 0x02 | uncompressed, no reset |
15
+ * | 0x80-0x9F | LZMA, no reset |
16
+ * | 0xA0-0xBF | LZMA, state reset |
17
+ * | 0xC0-0xDF | LZMA, state reset and new properties |
18
+ * | 0xE0-0xFF | LZMA, dictionary reset, state reset and new properties |
19
+ *
20
+ * The sizes follow as big-endian values minus one, then the data:
21
+ *
22
+ * | bytes | uncompressed chunk | LZMA chunk |
23
+ * | ----- | ------------------ | ------------------------------------------ |
24
+ * | 1-2 | size | uncompressed size, bits 0-15; bits 16-20 |
25
+ * | | | are the low 5 bits of the control byte |
26
+ * | 3-4 | | compressed size |
27
+ * | 5 | | `(pb * 5 + lp) * 9 + lc`, 0xC0 and up |
28
+ *
29
+ * The first chunk has to reset the dictionary, and after a dictionary reset
30
+ * the next LZMA chunk has to set properties. Every LZMA chunk starts a new
31
+ * range coder, and LZMA2 doesn't use the end marker of the LZMA layer.
32
+ *
33
+ * Input may arrive in pieces. The decoder copies uncompressed chunks as
34
+ * they arrive and decodes an LZMA chunk once its compressed data, at most
35
+ * 64 KiB, is complete.
36
+ *
37
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
38
+ */
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.Lzma2Decoder = void 0;
41
+ const lz_decoder_1 = require("./lz-decoder");
42
+ const lzma_decoder_1 = require("./lzma-decoder");
43
+ const range_decoder_1 = require("./range-decoder");
44
+ const COMPRESSED_SIZE_MAX = 1 << 16;
45
+ /**
46
+ * Upper bound of input bytes a single symbol can consume. The value of
47
+ * `INPUT_MARGIN` in lzma1's alone-decoder.ts, which lzma-defuse does not carry.
48
+ */
49
+ const INPUT_MARGIN = 64;
50
+ /** Largest chunk header: control byte, two sizes and the properties. */
51
+ const CHUNK_HEADER_SIZE_MAX = 6;
52
+ class Lzma2Decoder {
53
+ lz;
54
+ rc = new range_decoder_1.RangeDecoderFromBuffer();
55
+ lzma = null;
56
+ uncompressedSize = 0;
57
+ compressedSize = 0;
58
+ isLzmaChunk = false;
59
+ needDictReset = true;
60
+ needProps = true;
61
+ endReached = false;
62
+ /**
63
+ * Bytes decoded since the last dictionary reset. LZMA's position bits
64
+ * come from it. XZ for Java reads it from the dictionary
65
+ * (`lz.getPos()`); `LzmaDecoder` counts it in `outPos`.
66
+ */
67
+ outPos = 0;
68
+ /** Header of the next chunk, collected until it is complete. */
69
+ header = new Uint8Array(CHUNK_HEADER_SIZE_MAX);
70
+ headerPos = 0;
71
+ /**
72
+ * Compressed data of the current LZMA chunk, collected until it is
73
+ * complete. `INPUT_MARGIN` bytes after the data leave room for corrupt
74
+ * input, which can make the decoder read past it.
75
+ */
76
+ inBuf = new Uint8Array(COMPRESSED_SIZE_MAX + INPUT_MARGIN);
77
+ inPos = 0;
78
+ /**
79
+ * @param dictSize Dictionary size in bytes
80
+ * @param sink Receives decompressed data. The chunk is only valid during
81
+ * the call.
82
+ */
83
+ constructor(dictSize, sink) {
84
+ this.lz = new lz_decoder_1.LzDecoder(dictSize, sink);
85
+ }
86
+ /**
87
+ * Approximate memory needed to decode, in KiB, as XZ for Java's
88
+ * `LZMA2InputStream.getMemoryUsage` computes it.
89
+ */
90
+ static getMemoryUsage(dictSize) {
91
+ // The base state is around 30-40 KiB (probabilities etc.), the range
92
+ // decoder needs COMPRESSED_SIZE_MAX bytes for buffering, and the LZ
93
+ // decoder needs a dictionary buffer.
94
+ return 40 + COMPRESSED_SIZE_MAX / 1024 + Math.floor(getDictSize(dictSize) / 1024);
95
+ }
96
+ /** Decodes as much of `input` as possible. */
97
+ write(input) {
98
+ let pos = 0;
99
+ while (pos < input.length && !this.endReached) {
100
+ if (this.uncompressedSize === 0) {
101
+ pos = this.readChunkHeader(input, pos);
102
+ continue;
103
+ }
104
+ if (!this.isLzmaChunk) {
105
+ const copySize = Math.min(this.uncompressedSize, input.length - pos);
106
+ this.lz.copyUncompressed(input, pos, copySize);
107
+ pos += copySize;
108
+ this.outPos += copySize;
109
+ this.uncompressedSize -= copySize;
110
+ }
111
+ else {
112
+ const copySize = Math.min(this.compressedSize - this.inPos, input.length - pos);
113
+ this.inBuf.set(input.subarray(pos, pos + copySize), this.inPos);
114
+ pos += copySize;
115
+ this.inPos += copySize;
116
+ if (this.inPos === this.compressedSize) {
117
+ this.decodeLzmaChunk();
118
+ }
119
+ }
120
+ this.lz.flush();
121
+ }
122
+ }
123
+ /** Throws if the data ended before its end marker. */
124
+ end() {
125
+ if (!this.endReached) {
126
+ throw new Error('Truncated input');
127
+ }
128
+ }
129
+ /** Collects the chunk header and decodes it once it is complete. */
130
+ readChunkHeader(input, start) {
131
+ const header = this.header;
132
+ let pos = start;
133
+ header[this.headerPos++] = input[pos++];
134
+ const size = chunkHeaderSize(header[0]);
135
+ const copySize = Math.min(size - this.headerPos, input.length - pos);
136
+ header.set(input.subarray(pos, pos + copySize), this.headerPos);
137
+ this.headerPos += copySize;
138
+ if (this.headerPos === size) {
139
+ this.headerPos = 0;
140
+ this.decodeChunkHeader();
141
+ }
142
+ return pos + copySize;
143
+ }
144
+ decodeChunkHeader() {
145
+ const header = this.header;
146
+ const control = header[0];
147
+ if (control === 0x00) {
148
+ this.endReached = true;
149
+ return;
150
+ }
151
+ if (control >= 0xe0 || control === 0x01) {
152
+ this.needProps = true;
153
+ this.needDictReset = false;
154
+ this.lz.reset();
155
+ this.outPos = 0;
156
+ }
157
+ else if (this.needDictReset) {
158
+ throw new Error("Corrupted input: the first LZMA2 chunk doesn't reset the dictionary");
159
+ }
160
+ if (control >= 0x80) {
161
+ this.isLzmaChunk = true;
162
+ this.uncompressedSize = (control & 0x1f) << 16;
163
+ this.uncompressedSize += readUnsignedShort(header, 1) + 1;
164
+ // XZ for Java reads the compressed data here; `write` collects
165
+ // it instead.
166
+ this.compressedSize = readUnsignedShort(header, 3) + 1;
167
+ this.inPos = 0;
168
+ if (control >= 0xc0) {
169
+ this.needProps = false;
170
+ this.decodeProps(header[5]);
171
+ }
172
+ else if (this.needProps) {
173
+ throw new Error('Corrupted input: LZMA2 chunk without properties');
174
+ }
175
+ else if (control >= 0xa0) {
176
+ this.currentLzma().reset();
177
+ }
178
+ }
179
+ else if (control > 0x02) {
180
+ throw new Error('Corrupted input: invalid LZMA2 control byte');
181
+ }
182
+ else {
183
+ this.isLzmaChunk = false;
184
+ this.uncompressedSize = readUnsignedShort(header, 1) + 1;
185
+ }
186
+ }
187
+ decodeProps(byte) {
188
+ let props = byte;
189
+ if (props > (4 * 5 + 4) * 9 + 8) {
190
+ throw new Error('Corrupted input: invalid LZMA2 properties');
191
+ }
192
+ const pb = (props / (9 * 5)) | 0;
193
+ props -= pb * 9 * 5;
194
+ const lp = (props / 9) | 0;
195
+ const lc = props - lp * 9;
196
+ if (lc + lp > 4) {
197
+ throw new Error('Corrupted input: invalid LZMA2 properties');
198
+ }
199
+ this.lzma = new lzma_decoder_1.LzmaDecoder(this.lz, this.rc, lc, lp, pb);
200
+ }
201
+ /** The LZMA decoder of the last properties, which a chunk without properties continues. */
202
+ currentLzma() {
203
+ if (this.lzma === null)
204
+ throw new Error('Corrupted input: LZMA2 chunk without properties');
205
+ return this.lzma;
206
+ }
207
+ decodeLzmaChunk() {
208
+ const lzma = this.currentLzma();
209
+ const outLimit = this.outPos + this.uncompressedSize;
210
+ this.rc.prepareInputBuffer(this.inBuf, this.compressedSize);
211
+ lzma.outPos = this.outPos;
212
+ lzma.decode(outLimit, this.compressedSize);
213
+ // Decoding stops before the chunk's size when it reads past the
214
+ // chunk's data or decodes an end marker. XZ for Java throws in both
215
+ // cases. A match that would cross the chunk's end throws in
216
+ // `decode`, which XZ for Java detects with `lz.hasPending()`.
217
+ if (lzma.outPos !== outLimit || !this.rc.isFinished()) {
218
+ throw new Error("Corrupted input: LZMA2 chunk doesn't match its sizes");
219
+ }
220
+ this.outPos = outLimit;
221
+ this.uncompressedSize = 0;
222
+ }
223
+ }
224
+ exports.Lzma2Decoder = Lzma2Decoder;
225
+ /** Size of the chunk header that starts with `control`. */
226
+ function chunkHeaderSize(control) {
227
+ if (control >= 0xc0) {
228
+ return 6;
229
+ }
230
+ if (control >= 0x80) {
231
+ return 5;
232
+ }
233
+ if (control === 0x01 || control === 0x02) {
234
+ return 3;
235
+ }
236
+ // The end marker, or an invalid control byte, which throws
237
+ return 1;
238
+ }
239
+ function readUnsignedShort(bytes, pos) {
240
+ return (bytes[pos] << 8) | bytes[pos + 1];
241
+ }
242
+ /**
243
+ * Dictionary size rounded up to a multiple of 16, as XZ for Java allocates
244
+ * it, so the memory needed matches its numbers.
245
+ */
246
+ function getDictSize(dictSize) {
247
+ // Not `(dictSize + 15) & ~15`, which turns sizes of 2 GiB and up negative.
248
+ return Math.ceil(dictSize / 16) * 16;
249
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Constants shared by the range encoder and decoder.
3
+ *
4
+ * LZMA's entropy coder is a binary range coder: every coded bit has an
5
+ * adaptive probability (an 11-bit integer, the chance that the bit is 0)
6
+ * that is updated after each use.
7
+ */
8
+ /** When the range drops below this value, one byte is shifted in or out. */
9
+ export declare const TOP_VALUE: number;
10
+ export declare const BIT_MODEL_TOTAL_BITS = 11;
11
+ export declare const BIT_MODEL_TOTAL: number;
12
+ /** Initial probability: 0 and 1 are equally likely. */
13
+ export declare const PROB_INIT: number;
14
+ /** Adaptation speed of the probabilities. */
15
+ export declare const MOVE_BITS = 5;
16
+ /** Probability array. Each entry is an 11-bit probability of a 0 bit. */
17
+ export type Probs = Uint16Array;
18
+ export declare function initProbs(probs: Probs): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
3
+ /**
4
+ * Constants shared by the range encoder and decoder.
5
+ *
6
+ * LZMA's entropy coder is a binary range coder: every coded bit has an
7
+ * adaptive probability (an 11-bit integer, the chance that the bit is 0)
8
+ * that is updated after each use.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.MOVE_BITS = exports.PROB_INIT = exports.BIT_MODEL_TOTAL = exports.BIT_MODEL_TOTAL_BITS = exports.TOP_VALUE = void 0;
12
+ exports.initProbs = initProbs;
13
+ /** When the range drops below this value, one byte is shifted in or out. */
14
+ exports.TOP_VALUE = 1 << 24;
15
+ exports.BIT_MODEL_TOTAL_BITS = 11;
16
+ exports.BIT_MODEL_TOTAL = 1 << exports.BIT_MODEL_TOTAL_BITS;
17
+ /** Initial probability: 0 and 1 are equally likely. */
18
+ exports.PROB_INIT = exports.BIT_MODEL_TOTAL / 2;
19
+ /** Adaptation speed of the probabilities. */
20
+ exports.MOVE_BITS = 5;
21
+ function initProbs(probs) {
22
+ probs.fill(exports.PROB_INIT);
23
+ }
@@ -0,0 +1,41 @@
1
+ /** Number of bytes the range decoder reads on initialization. */
2
+ export declare const RANGE_DECODER_INIT_SIZE = 5;
3
+ /**
4
+ * Range decoder reading from an in-memory byte array.
5
+ *
6
+ * `range` and `code` are unsigned 32-bit values stored in regular numbers,
7
+ * so plain `<` comparisons are unsigned.
8
+ *
9
+ * The LZMA decoder loop (`LzmaDecoder.decode`) does the decoding steps
10
+ * inline with the state in local variables, which is why the state fields
11
+ * are public. `range-coder_test.ts` has the reference implementations.
12
+ */
13
+ export declare class RangeDecoder {
14
+ range: number;
15
+ code: number;
16
+ input: Uint8Array;
17
+ /** Position of the next unread input byte. */
18
+ pos: number;
19
+ /** Reads the initial bytes from `input` starting at `pos`. */
20
+ init(input: Uint8Array, pos: number): void;
21
+ /** Continues decoding from a different buffer (used when streaming). */
22
+ setInput(input: Uint8Array, pos: number): void;
23
+ /** True when the encoder's flush bytes have been consumed exactly. */
24
+ isFinished(): boolean;
25
+ private readByte;
26
+ }
27
+ /**
28
+ * Range decoder for one LZMA2 chunk. Every LZMA chunk starts a new range
29
+ * coder, and its compressed data is complete in the buffer before decoding
30
+ * starts, so the data ends at a known position.
31
+ *
32
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
33
+ */
34
+ export declare class RangeDecoderFromBuffer extends RangeDecoder {
35
+ /** Position after the chunk's compressed data. */
36
+ private end;
37
+ /** Starts decoding the `len` bytes of chunk data at the start of `input`. */
38
+ prepareInputBuffer(input: Uint8Array, len: number): void;
39
+ /** True when the chunk's data has been consumed exactly. */
40
+ isFinished(): boolean;
41
+ }
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RangeDecoderFromBuffer = exports.RangeDecoder = exports.RANGE_DECODER_INIT_SIZE = void 0;
4
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
5
+ /** Number of bytes the range decoder reads on initialization. */
6
+ exports.RANGE_DECODER_INIT_SIZE = 5;
7
+ /**
8
+ * Range decoder reading from an in-memory byte array.
9
+ *
10
+ * `range` and `code` are unsigned 32-bit values stored in regular numbers,
11
+ * so plain `<` comparisons are unsigned.
12
+ *
13
+ * The LZMA decoder loop (`LzmaDecoder.decode`) does the decoding steps
14
+ * inline with the state in local variables, which is why the state fields
15
+ * are public. `range-coder_test.ts` has the reference implementations.
16
+ */
17
+ class RangeDecoder {
18
+ range = 0;
19
+ code = 0;
20
+ input = new Uint8Array(0);
21
+ /** Position of the next unread input byte. */
22
+ pos = 0;
23
+ /** Reads the initial bytes from `input` starting at `pos`. */
24
+ init(input, pos) {
25
+ this.setInput(input, pos);
26
+ if (this.readByte() !== 0x00) {
27
+ throw new Error('Corrupted input: invalid range coder header');
28
+ }
29
+ this.range = 0xffffffff;
30
+ this.code = 0;
31
+ for (let i = 1; i < exports.RANGE_DECODER_INIT_SIZE; ++i) {
32
+ this.code = ((this.code << 8) | this.readByte()) >>> 0;
33
+ }
34
+ if (this.code === this.range) {
35
+ throw new Error('Corrupted input: invalid range coder header');
36
+ }
37
+ }
38
+ /** Continues decoding from a different buffer (used when streaming). */
39
+ setInput(input, pos) {
40
+ this.input = input;
41
+ this.pos = pos;
42
+ }
43
+ /** True when the encoder's flush bytes have been consumed exactly. */
44
+ isFinished() {
45
+ return this.code === 0;
46
+ }
47
+ readByte() {
48
+ if (this.pos >= this.input.length) {
49
+ throw new Error('Truncated input');
50
+ }
51
+ return this.input[this.pos++];
52
+ }
53
+ }
54
+ exports.RangeDecoder = RangeDecoder;
55
+ /**
56
+ * Range decoder for one LZMA2 chunk. Every LZMA chunk starts a new range
57
+ * coder, and its compressed data is complete in the buffer before decoding
58
+ * starts, so the data ends at a known position.
59
+ *
60
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
61
+ */
62
+ class RangeDecoderFromBuffer extends RangeDecoder {
63
+ /** Position after the chunk's compressed data. */
64
+ end = 0;
65
+ /** Starts decoding the `len` bytes of chunk data at the start of `input`. */
66
+ prepareInputBuffer(input, len) {
67
+ if (len < exports.RANGE_DECODER_INIT_SIZE) {
68
+ throw new Error('Corrupted input: LZMA2 chunk is too short');
69
+ }
70
+ this.init(input, 0);
71
+ this.end = len;
72
+ }
73
+ /** True when the chunk's data has been consumed exactly. */
74
+ isFinished() {
75
+ return this.pos === this.end && this.code === 0;
76
+ }
77
+ }
78
+ exports.RangeDecoderFromBuffer = RangeDecoderFromBuffer;
@@ -0,0 +1,53 @@
1
+ import { type Probs } from './range-coder';
2
+ export declare function getBitPrice(prob: number, bit: number): number;
3
+ /** Price of a `bits`-bit symbol coded MSB-first with the bit tree at `offset`. */
4
+ export declare function getBitTreePrice(probs: Probs, offset: number, bits: number, symbolIn: number): number;
5
+ /** Price of a `bits`-bit symbol coded LSB-first with the bit tree at `offset`. */
6
+ export declare function getReverseBitTreePrice(probs: Probs, offset: number, bits: number, symbolIn: number): number;
7
+ /** Price of bits coded with a fixed 50% probability. */
8
+ export declare function getDirectBitsPrice(count: number): number;
9
+ /**
10
+ * Range encoder writing to its own growable buffer.
11
+ *
12
+ * `low` needs 33 bits (32 bits plus a carry), so it is kept as a regular
13
+ * number instead of an int32. Doubles represent integers up to 2^53 exactly.
14
+ *
15
+ * Performance: like `RangeDecoder`, the methods that encode several bits
16
+ * keep `range` and `low` in local variables for the whole symbol.
17
+ */
18
+ export declare class RangeEncoder {
19
+ private low;
20
+ private range;
21
+ private cache;
22
+ private cacheSize;
23
+ private buf;
24
+ private size;
25
+ constructor(initialCapacity: number);
26
+ /** Appends bytes that are not range coded, e.g. a header. */
27
+ writeBytes(bytes: Uint8Array): void;
28
+ /** Returns a copy of the output written so far and empties the buffer. */
29
+ take(): Uint8Array;
30
+ /** Flushes all pending bytes and returns the rest of the output. */
31
+ finish(): Uint8Array;
32
+ encodeBit(probs: Probs, index: number, bit: number): void;
33
+ /** Encodes a `bits`-bit symbol MSB-first with the bit tree at `offset`. */
34
+ encodeBitTree(probs: Probs, offset: number, bits: number, symbol: number): void;
35
+ /** Encodes a `bits`-bit symbol LSB-first with the bit tree at `offset`. */
36
+ encodeReverseBitTree(probs: Probs, offset: number, bits: number, symbolIn: number): void;
37
+ /** Encodes a literal byte with the 0x100-leaf bit tree at `offset`. */
38
+ encodeLiteral(probs: Probs, offset: number, symbol: number): void;
39
+ /**
40
+ * Encodes a literal after a match. The bits of `matchByte` (the byte at
41
+ * rep0) select separate probabilities until the first bit that differs.
42
+ */
43
+ encodeMatchedLiteral(probs: Probs, offset: number, symbolIn: number, matchByteIn: number): void;
44
+ /** Encodes the `count` low bits of `value` with a fixed 50% probability. */
45
+ encodeDirectBits(value: number, countIn: number): void;
46
+ /**
47
+ * Moves the top byte of `low` to the output and returns the new `low`.
48
+ * A run of 0xFF bytes is held back (`cache` + `cacheSize`) until it is
49
+ * known whether a carry will propagate into it.
50
+ */
51
+ private shiftLow;
52
+ private ensureCapacity;
53
+ }