lzma1 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/lzma.js CHANGED
@@ -1,381 +1,57 @@
1
- // const CRC32_TABLE = (() => {
2
- // const crcTable = [];
3
- // for (let i = 0, r; i < 256; ++i, r = i) {
4
- // r = i;
5
- // for (let j = 0; j < 8; ++j) {
6
- // if ((r & 1) != 0) {
7
- // r >>>= 1;
8
- // r ^= -306674912;
9
- // } else {
10
- // r >>>= 1;
11
- // }
12
- // }
13
- // crcTable[i] = r;
14
- // }
15
- // return crcTable;
16
- // })();
17
- // dprint-ignore
18
- const CRC32_TABLE = [
19
- 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
20
- 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
21
- 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
22
- 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
23
- 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
24
- 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
25
- 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
26
- 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
27
- 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
28
- 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
29
- 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
30
- 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
31
- 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
32
- 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
33
- 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
34
- 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
35
- 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
36
- 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
37
- 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
38
- 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
39
- 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
40
- 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
41
- 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
42
- 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
43
- 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
44
- 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
45
- 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
46
- 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
47
- 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
48
- 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
49
- 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
50
- 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
51
- 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
52
- 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
53
- 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
54
- 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
55
- 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
56
- 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
57
- 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
58
- 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
59
- 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
60
- 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
61
- 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
62
- ];
1
+ import { DecoderChunker, EncoderChunker, } from "./chunker.js";
2
+ import { Decoder } from "./decoder.js";
3
+ import { Encoder, } from "./encoder.js";
4
+ import { LzInWindow } from "./lz-in-window.js";
5
+ import { computeHashSize, computeWindowReservSize, ensureCyclicBuffer, isDictionarySizeBelowThreshold, setCutValue, setMatchMaxLen, } from "./match-finder-config.js";
6
+ import { _MAX_UINT32, add64, arraycopy, compare64, CRC32_TABLE, createBitTree, DICTIONARY_SIZE_THRESHOLD, fromInt64, G_FAST_POS, getBitPrice, getLenToPosState, INFINITY_PRICE, initArray, lowBits64, N1_LONG_LIT, P0_LONG_LIT, P1_LONG_LIT, PROB_PRICES, shr64, stateUpdateChar, sub64, } from "./utils.js";
7
+ /**
8
+ * Compression modes
9
+ */
10
+ export const MODES = {
11
+ 1: { searchDepth: 0x10, filterStrength: 0x40, modeIndex: 0x00 },
12
+ 2: { searchDepth: 0x14, filterStrength: 0x40, modeIndex: 0x00 },
13
+ 3: { searchDepth: 0x13, filterStrength: 0x40, modeIndex: 0x01 },
14
+ 4: { searchDepth: 0x14, filterStrength: 0x40, modeIndex: 0x01 },
15
+ 5: { searchDepth: 0x15, filterStrength: 0x80, modeIndex: 0x01 },
16
+ 6: { searchDepth: 0x16, filterStrength: 0x80, modeIndex: 0x01 },
17
+ 7: { searchDepth: 0x17, filterStrength: 0x80, modeIndex: 0x01 },
18
+ 8: { searchDepth: 0x18, filterStrength: 0xFF, modeIndex: 0x01 },
19
+ 9: { searchDepth: 0x19, filterStrength: 0xFF, modeIndex: 0x01 },
20
+ };
63
21
  export class LZMA {
64
- #MAX_UINT32 = 4294967296;
65
- #_MAX_UINT32 = 0xFFFFFFFF;
66
- #MAX_INT32 = 2147483647;
67
- #MIN_INT32 = -2147483648;
68
- #MAX_COMPRESSION_SIZE = 9223372036854775807;
69
- #kIfinityPrice = 268435455;
70
- #dictionarySizeThreshold = 0x3FFFFFFF;
71
- #N1_LONG_LIT;
72
- #MIN_VALUE;
73
- #P0_LONG_LIT = [0, 0];
74
- #P1_LONG_LIT = [1, 0];
75
- CompressionModes = {
76
- 1: { searchDepth: 0x10, filterStrength: 0x40, modeIndex: 0x00 },
77
- 2: { searchDepth: 0x14, filterStrength: 0x40, modeIndex: 0x00 },
78
- 3: { searchDepth: 0x13, filterStrength: 0x40, modeIndex: 0x01 },
79
- 4: { searchDepth: 0x14, filterStrength: 0x40, modeIndex: 0x01 },
80
- 5: { searchDepth: 0x15, filterStrength: 0x80, modeIndex: 0x01 },
81
- 6: { searchDepth: 0x16, filterStrength: 0x80, modeIndex: 0x01 },
82
- 7: { searchDepth: 0x17, filterStrength: 0x80, modeIndex: 0x01 },
83
- 8: { searchDepth: 0x18, filterStrength: 0xFF, modeIndex: 0x01 },
84
- 9: { searchDepth: 0x19, filterStrength: 0xFF, modeIndex: 0x01 },
85
- };
86
22
  #encoder;
87
23
  #decoder;
88
- #probPrices;
89
- #gFastPos;
24
+ #lzInWindow = null;
90
25
  #compressor;
91
26
  #decompressor;
92
27
  constructor() {
93
- this.#N1_LONG_LIT = [4294967295, -this.#MAX_UINT32]; // N1_longLit from old.ts
94
- this.#MIN_VALUE = [0, -9223372036854775808]; // MIN_VALUE from old.ts
95
- this.#encoder = this.#initEncoder();
96
- this.#decoder = this.#initDecoder();
97
- this.#probPrices = this.#createProbPrices();
98
- this.#gFastPos = this.#createFastPos();
28
+ this.#encoder = new Encoder();
29
+ this.#decoder = new Decoder();
99
30
  this.#compressor = this.#initCompressor();
100
31
  this.#decompressor = this.#initDecompressor();
101
32
  }
102
- #initEncoder() {
103
- return {
104
- _state: 0,
105
- _previousByte: 0,
106
- _distTableSize: 0,
107
- _longestMatchWasFound: 0,
108
- _optimumEndIndex: 0,
109
- _optimumCurrentIndex: 0,
110
- _additionalOffset: 0,
111
- _dictionarySize: 0,
112
- _matchFinder: null,
113
- _dictionarySizePrev: 0,
114
- _numFastBytes: 0,
115
- _numLiteralContextBits: 0,
116
- _numLiteralPosStateBits: 0,
117
- _posStateBits: 0,
118
- _posStateMask: 0,
119
- _needReleaseMFStream: 0,
120
- _inStream: { buf: [], pos: 0, count: 0 },
121
- _finished: 0,
122
- nowPos64: [0, 0],
123
- _repDistances: this.#initArray(4),
124
- _optimum: [],
125
- _rangeEncoder: {
126
- stream: {
127
- buf: [],
128
- count: 0,
129
- },
130
- rrange: 0,
131
- cache: 0,
132
- low: [0, 0],
133
- cacheSize: 0,
134
- position: [0, 0],
135
- },
136
- _isMatch: this.#initArray(0xC0),
137
- _isRep: this.#initArray(0x0C),
138
- _isRepG0: this.#initArray(0x0C),
139
- _isRepG1: this.#initArray(0x0C),
140
- _isRepG2: this.#initArray(0x0C),
141
- _isRep0Long: this.#initArray(0xC0),
142
- _posSlotEncoder: [],
143
- _posEncoders: this.#initArray(0x72),
144
- _posAlignEncoder: this.#createBitTreeEncoder(0x04),
145
- _lenEncoder: this.#createLenPriceTableEncoder(),
146
- _repMatchLenEncoder: this.#createLenPriceTableEncoder(),
147
- _literalEncoder: {},
148
- _matchDistances: [],
149
- _posSlotPrices: [],
150
- _distancesPrices: [],
151
- _alignPrices: this.#initArray(0x10),
152
- _matchPriceCount: 0,
153
- _alignPriceCount: 0,
154
- reps: this.#initArray(0x04),
155
- repLens: this.#initArray(0x04),
156
- processedInSize: [this.#P0_LONG_LIT],
157
- processedOutSize: [this.#P0_LONG_LIT],
158
- finished: [0x00],
159
- properties: this.#initArray(0x05),
160
- tempPrices: this.#initArray(0x80),
161
- _longestMatchLength: 0x00,
162
- _matchFinderType: 0x01,
163
- _numDistancePairs: 0x00,
164
- _numFastBytesPrev: -0x01,
165
- backRes: 0x00,
166
- };
167
- }
168
- #initDecoder() {
169
- const decoder = {
170
- posStateMask: 0,
171
- dictSize: 0,
172
- dictSizeCheck: 0,
173
- state: 0,
174
- rep0: 0,
175
- rep1: 0,
176
- rep2: 0,
177
- rep3: 0,
178
- outSize: [0, 0],
179
- nowPos64: [0, 0],
180
- prevByte: 0,
181
- alive: 0,
182
- encoder: null,
183
- decoder: {},
184
- inBytesProcessed: [0, 0],
185
- outWin: {},
186
- rangeDecoder: {},
187
- matchDecoders: this.#initArray(0xC0),
188
- repDecoders: this.#initArray(0x0C),
189
- repG0Decoders: this.#initArray(0x0C),
190
- repG1Decoders: this.#initArray(0x0C),
191
- repG2Decoders: this.#initArray(0x0C),
192
- rep0LongDecoders: this.#initArray(0xC0),
193
- posSlotDecoders: this.#initArray(4),
194
- posDecoders: this.#initArray(0x72),
195
- posAlignDecoder: this.#createBitTreeDecoder(0x04),
196
- lenDecoder: this.#createLenDecoder(),
197
- repLenDecoder: this.#createLenDecoder(),
198
- literalDecoder: {},
199
- };
200
- for (let i = 0; i < 4; ++i) {
201
- decoder.posSlotDecoders[i] = this.#createBitTreeDecoder(0x06);
202
- }
203
- return decoder;
204
- }
205
33
  #initCompressor() {
34
+ const encoderChunker = new EncoderChunker(this);
206
35
  return {
207
- chunker: {
208
- alive: 0x00,
209
- encoder: null,
210
- decoder: null,
211
- inBytesProcessed: [0x00, 0x00],
212
- },
36
+ chunker: encoderChunker,
213
37
  output: {
214
- buf: this.#initArray(0x20),
215
- count: 0x00,
38
+ buf: initArray(32),
39
+ count: 0,
40
+ write: () => { },
216
41
  },
217
42
  };
218
43
  }
219
44
  #initDecompressor() {
45
+ const decoderChunker = new DecoderChunker(this.#decoder);
220
46
  return {
221
- chunker: {
222
- alive: 0x00,
223
- encoder: null,
224
- decoder: this.#decoder,
225
- inBytesProcessed: [0x00, 0x00],
226
- },
47
+ chunker: decoderChunker,
227
48
  output: {
228
- buf: this.#initArray(0x20),
229
- count: 0x00,
49
+ buf: initArray(0x20),
50
+ count: 0,
51
+ write: () => { },
230
52
  },
231
53
  };
232
54
  }
233
- #createProbPrices() {
234
- const probPrices = [];
235
- for (let i = 8; i >= 0; --i) {
236
- let start = 1 << (9 - i - 1);
237
- let end = 1 << (9 - i);
238
- for (let j = start; j < end; ++j) {
239
- probPrices[j] = (i << 6) + ((end - j) << 6 >>> (9 - i - 1));
240
- }
241
- }
242
- return probPrices;
243
- }
244
- #createFastPos() {
245
- const gFastPos = [0x00, 0x01];
246
- let c = 2;
247
- for (let slotFast = 2; slotFast < 22; ++slotFast) {
248
- let k = 1 << ((slotFast >> 1) - 1);
249
- for (let j = 0; j < k; ++j, ++c) {
250
- gFastPos[c] = slotFast;
251
- }
252
- }
253
- return gFastPos;
254
- }
255
- #initArray(len) {
256
- const array = [];
257
- // This is MUCH faster than "new Array(len)" in newer versions of v8
258
- // (starting with Node.js 0.11.15, which uses v8 3.28.73).
259
- array[len - 1] = undefined;
260
- return array;
261
- }
262
- #add(a, b) {
263
- return this.#create(a[0] + b[0], a[1] + b[1]);
264
- }
265
- #and(a, b) {
266
- const highBits = ~~Math.max(Math.min(a[1] / this.#MAX_UINT32, this.#MAX_INT32), this.#MIN_INT32) & ~~Math.max(Math.min(b[1] / this.#MAX_UINT32, this.#MAX_INT32), this.#MIN_INT32);
267
- const lowBits = this.#lowBits_0(a) & this.#lowBits_0(b);
268
- let high = highBits * this.#MAX_UINT32;
269
- let low = lowBits;
270
- if (lowBits < 0) {
271
- low += this.#MAX_UINT32;
272
- }
273
- return [low, high];
274
- }
275
- #compare(a, b) {
276
- if (a[0] == b[0] && a[1] == b[1]) {
277
- return 0;
278
- }
279
- const nega = a[1] < 0;
280
- const negb = b[1] < 0;
281
- if (nega && !negb) {
282
- return -1;
283
- }
284
- if (!nega && negb) {
285
- return 1;
286
- }
287
- if (this.#sub(a, b)[1] < 0) {
288
- return -1;
289
- }
290
- return 1;
291
- }
292
- #create(valueLow, valueHigh) {
293
- let diffHigh, diffLow;
294
- valueHigh %= 1.8446744073709552E19;
295
- valueLow %= 1.8446744073709552E19;
296
- diffHigh = valueHigh % this.#MAX_UINT32;
297
- diffLow = Math.floor(valueLow / this.#MAX_UINT32) * this.#MAX_UINT32;
298
- valueHigh = valueHigh - diffHigh + diffLow;
299
- valueLow = valueLow - diffLow + diffHigh;
300
- while (valueLow < 0) {
301
- valueLow += this.#MAX_UINT32;
302
- valueHigh -= this.#MAX_UINT32;
303
- }
304
- while (valueLow > 4294967295) {
305
- valueLow -= this.#MAX_UINT32;
306
- valueHigh += this.#MAX_UINT32;
307
- }
308
- valueHigh = valueHigh % 1.8446744073709552E19;
309
- while (valueHigh > 9223372032559808512) {
310
- valueHigh -= 1.8446744073709552E19;
311
- }
312
- while (valueHigh < -9223372036854775808) {
313
- valueHigh += 1.8446744073709552E19;
314
- }
315
- return [valueLow, valueHigh];
316
- }
317
- #eq(a, b) {
318
- return a[0] == b[0] && a[1] == b[1];
319
- }
320
- #fromInt(value) {
321
- if (value >= 0) {
322
- return [value, 0];
323
- }
324
- else {
325
- return [value + this.#MAX_UINT32, -this.#MAX_UINT32];
326
- }
327
- }
328
- #lowBits_0(a) {
329
- if (a[0] >= 0x80000000) {
330
- return ~~Math.max(Math.min(a[0] - this.#MAX_UINT32, this.#MAX_INT32), this.#MIN_INT32);
331
- }
332
- return ~~Math.max(Math.min(a[0], this.#MAX_INT32), this.#MIN_INT32);
333
- }
334
- #pwrAsDouble(n) {
335
- if (n <= 0x1E) {
336
- return 1 << n;
337
- }
338
- return this.#pwrAsDouble(0x1E) * this.#pwrAsDouble(n - 0x1E);
339
- }
340
- #shl(a, n) {
341
- let diff, newHigh, newLow, twoToN;
342
- n &= 0x3F;
343
- if (this.#eq(a, this.#MIN_VALUE)) {
344
- if (!n) {
345
- return a;
346
- }
347
- return this.#P0_LONG_LIT;
348
- }
349
- if (a[1] < 0) {
350
- throw new Error("Neg");
351
- }
352
- twoToN = this.#pwrAsDouble(n);
353
- newHigh = a[1] * twoToN % 1.8446744073709552E19;
354
- newLow = a[0] * twoToN;
355
- diff = newLow - newLow % this.#MAX_UINT32;
356
- newHigh += diff;
357
- newLow -= diff;
358
- if (newHigh >= this.#MAX_COMPRESSION_SIZE) {
359
- newHigh -= 1.8446744073709552E19;
360
- }
361
- return [newLow, newHigh];
362
- }
363
- #shr(a, n) {
364
- n &= 0x3F;
365
- let shiftFact = this.#pwrAsDouble(n);
366
- return this.#create(Math.floor(a[0] / shiftFact), a[1] / shiftFact);
367
- }
368
- #shru(a, n) {
369
- let sr = this.#shr(a, n);
370
- n &= 0x3F;
371
- if (a[1] < 0) {
372
- sr = this.#add(sr, this.#shl([2, 0], 0x3F - n));
373
- }
374
- return sr;
375
- }
376
- #sub(a, b) {
377
- return this.#create(a[0] - b[0], a[1] - b[1]);
378
- }
379
55
  #read(inputStream) {
380
56
  if (inputStream.pos >= inputStream.count) {
381
57
  return -1;
@@ -392,28 +68,6 @@ export class LZMA {
392
68
  }
393
69
  return value & 0xFF;
394
70
  }
395
- #read_0(off, len) {
396
- const encoder = this.#compressor.chunker.encoder;
397
- const stream = encoder._matchFinder._stream;
398
- const buffer = encoder._matchFinder._bufferBase;
399
- if (stream.pos >= stream.count) {
400
- return -1;
401
- }
402
- let srcBuf;
403
- if (stream.buf instanceof Uint8Array) {
404
- srcBuf = Array.from(stream.buf);
405
- }
406
- else if (stream.buf instanceof ArrayBuffer) {
407
- srcBuf = Array.from(new Uint8Array(stream.buf));
408
- }
409
- else {
410
- srcBuf = stream.buf;
411
- }
412
- len = Math.min(len, stream.count - stream.pos);
413
- this.#arraycopy(srcBuf, stream.pos, buffer, off, len);
414
- stream.pos += len;
415
- return len;
416
- }
417
71
  #toByteArray(output) {
418
72
  const data = output.buf.slice(0, output.count);
419
73
  return data;
@@ -421,7 +75,6 @@ export class LZMA {
421
75
  #write(buffer, b) {
422
76
  if (!buffer)
423
77
  return;
424
- // Ensure buffer has enough capacity
425
78
  if (buffer.count >= buffer.buf.length) {
426
79
  const newSize = Math.max(buffer.buf.length * 2, buffer.count + 1);
427
80
  const newBuf = new Array(newSize);
@@ -433,7 +86,6 @@ export class LZMA {
433
86
  buffer.buf[buffer.count++] = b << 24 >> 24;
434
87
  }
435
88
  #write_0(buffer, buf, off, len) {
436
- // Ensure buffer has enough capacity
437
89
  const requiredSize = buffer.count + len;
438
90
  if (requiredSize > buffer.buf.length) {
439
91
  const newSize = Math.max(buffer.buf.length * 2, requiredSize);
@@ -443,7 +95,7 @@ export class LZMA {
443
95
  }
444
96
  buffer.buf = newBuf;
445
97
  }
446
- this.#arraycopy(buf, off, buffer.buf, buffer.count, len);
98
+ arraycopy(buf, off, buffer.buf, buffer.count, len);
447
99
  buffer.count += len;
448
100
  }
449
101
  #getChars(inputString, srcBegin, srcEnd, dst, dstBegin) {
@@ -451,44 +103,12 @@ export class LZMA {
451
103
  dst[dstBegin++] = inputString.charCodeAt(srcIdx);
452
104
  }
453
105
  }
454
- #arraycopy(src, srcOfs, dest, destOfs, len) {
455
- // Bounds checking
456
- if (srcOfs < 0
457
- || destOfs < 0
458
- || len < 0
459
- || srcOfs + len > src.length
460
- || destOfs + len > dest.length) {
461
- return;
462
- }
463
- if (src === dest
464
- && srcOfs < destOfs
465
- && destOfs < srcOfs + len) {
466
- // Overlapping regions - copy backwards
467
- for (let i = len - 1; i >= 0; i--) {
468
- dest[destOfs + i] = src[srcOfs + i];
469
- }
470
- }
471
- else {
472
- // Non-overlapping or forward copy
473
- for (let i = 0; i < len; i++) {
474
- dest[destOfs + i] = src[srcOfs + i];
475
- }
476
- }
477
- }
478
106
  #configure(mode) {
479
- this.#SetDictionarySize_0(0x1 << mode.searchDepth);
480
- this.#encoder._numFastBytes = mode.filterStrength;
481
- this.#SetMatchFinder(mode.modeIndex);
482
- // lc is always 3
483
- // lp is always 0
484
- // pb is always 2
485
- this.#encoder._numLiteralContextBits = 0x3;
486
- this.#encoder._numLiteralPosStateBits = 0x0;
487
- this.#encoder._posStateBits = 0x2;
488
- this.#encoder._posStateMask = 0x3;
107
+ this.#encoder.initialize();
108
+ this.#encoder.configure(mode);
489
109
  }
490
110
  #initCompression(input, len, mode) {
491
- if (this.#compare(len, this.#N1_LONG_LIT) < 0) {
111
+ if (compare64(len, N1_LONG_LIT) < 0) {
492
112
  throw new Error("invalid length " + len);
493
113
  }
494
114
  this.#compressor.length_0 = len;
@@ -496,43 +116,34 @@ export class LZMA {
496
116
  this.#configure(mode);
497
117
  this.writeHeaderProperties();
498
118
  for (let i = 0; i < 64; i += 8) {
499
- this.#write(this.#compressor.output, this.#lowBits_0(this.#shr(len, i)) & 0xFF);
119
+ this.#write(this.#compressor.output, lowBits64(shr64(len, i)) & 0xFF);
500
120
  }
501
- // Initialize encoder stream and properties
502
- this.#encoder._needReleaseMFStream = 0x00;
121
+ this.#encoder._needReleaseMFStream = 0;
503
122
  this.#encoder._inStream = input;
504
- this.#encoder._finished = 0x00;
505
- // Create and configure encoder
123
+ this.#encoder._finished = 0;
506
124
  this.#Create_2();
507
125
  this.#encoder._rangeEncoder.stream = this.#compressor.output;
508
- this.#Init_4();
509
- // Initialize pricing tables
510
- this.#FillDistancesPrices(this.#encoder);
511
- this.#FillAlignPrices(this.#encoder);
512
- // Configure length encoders
513
- this.#encoder._lenEncoder.tableSize = this.#encoder._numFastBytes + 1 - 2;
514
- this.#LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
515
- this.#encoder._repMatchLenEncoder.tableSize = this.#encoder._numFastBytes + 1 - 2;
516
- this.#LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
517
- // Reset position counter
518
- this.#encoder.nowPos64 = this.#P0_LONG_LIT;
519
- // Create new chunker with configured encoder
520
- this.#compressor.chunker = {
521
- encoder: this.#encoder,
522
- decoder: null,
523
- alive: 1,
524
- };
126
+ this.#encoder.init();
127
+ this.#encoder.fillDistancesPrices();
128
+ this.#encoder.fillAlignPrices();
129
+ this.#encoder._lenEncoder.setTableSize(this.#encoder._numFastBytes + 1 - 2);
130
+ this.#encoder._lenEncoder.updateTables(1 << this.#encoder._posStateBits);
131
+ this.#encoder._repMatchLenEncoder.setTableSize(this.#encoder._numFastBytes + 1 - 2);
132
+ this.#encoder._repMatchLenEncoder.updateTables(1 << this.#encoder._posStateBits);
133
+ this.#encoder.nowPos64 = P0_LONG_LIT;
134
+ this.#compressor.chunker.encoder = this.#encoder;
135
+ this.#compressor.chunker.alive = 1;
525
136
  }
526
137
  #byteArrayCompressor(data, mode) {
527
- // Initialize output buffer with estimated size for compression
528
138
  const inputSize = data instanceof ArrayBuffer ? data.byteLength : data.length;
529
139
  const estimatedOutputSize = Math.max(32, Math.ceil(inputSize * 1.2));
530
140
  this.#compressor.output = {
531
- buf: this.#initArray(estimatedOutputSize),
141
+ buf: initArray(estimatedOutputSize),
532
142
  count: 0,
143
+ write: () => { },
533
144
  };
534
145
  const inputBuffer = {
535
- pos: 0x00,
146
+ pos: 0,
536
147
  buf: data instanceof ArrayBuffer
537
148
  ? new Uint8Array(data)
538
149
  : data,
@@ -540,7 +151,7 @@ export class LZMA {
540
151
  ? new Uint8Array(data).length
541
152
  : data.length,
542
153
  };
543
- this.#initCompression(inputBuffer, this.#fromInt(data instanceof ArrayBuffer ? data.byteLength : data.length), mode);
154
+ this.#initCompression(inputBuffer, fromInt64(data instanceof ArrayBuffer ? data.byteLength : data.length), mode);
544
155
  }
545
156
  #initDecompression(input) {
546
157
  let hex_length = "", properties = [], r, tmp_length;
@@ -551,7 +162,10 @@ export class LZMA {
551
162
  }
552
163
  properties[i] = r << 24 >> 24;
553
164
  }
554
- if (!this.#SetDecoderProperties(properties)) {
165
+ const isDecoderInitialized = !this.#decoder.setDecoderProperties(properties)
166
+ ? 1
167
+ : 0;
168
+ if (isDecoderInitialized) {
555
169
  throw new Error("corrupted input");
556
170
  }
557
171
  for (let i = 0; i < 64; i += 8) {
@@ -569,8 +183,7 @@ export class LZMA {
569
183
  * length is all f"s).
570
184
  */
571
185
  if (/^0+$|^f+$/i.test(hex_length)) {
572
- // The length is unknown, so set to -1.
573
- this.#compressor.length_0 = this.#N1_LONG_LIT;
186
+ this.#compressor.length_0 = N1_LONG_LIT;
574
187
  }
575
188
  else {
576
189
  /**
@@ -578,25 +191,24 @@ export class LZMA {
578
191
  * you can always set the length to -1 (N1_longLit) which means unknown.
579
192
  */
580
193
  tmp_length = parseInt(hex_length, 0x10);
581
- // If the length is too long to handle, just set it to unknown.
582
- if (tmp_length > this.#_MAX_UINT32) {
583
- this.#compressor.length_0 = this.#N1_LONG_LIT;
194
+ if (tmp_length > _MAX_UINT32) {
195
+ this.#compressor.length_0 = N1_LONG_LIT;
584
196
  }
585
197
  else {
586
- this.#compressor.length_0 = this.#fromInt(tmp_length);
198
+ this.#compressor.length_0 = fromInt64(tmp_length);
587
199
  }
588
200
  }
589
201
  this.#decompressor.chunker = this.#CodeInChunks(input, this.#compressor.length_0);
590
202
  }
591
203
  #byteArrayDecompressor(data) {
592
- // Calculate initial buffer size for decompression
593
204
  const inputDataSize = data instanceof ArrayBuffer ? data.byteLength : data.length;
594
205
  const minBufferSize = 0x20; // 32 bytes minimum
595
206
  const estimatedOutputSize = inputDataSize * 2; // Estimate 2x expansion for decompression
596
207
  const initialBufferSize = Math.max(minBufferSize, estimatedOutputSize);
597
208
  this.#decompressor.output = {
598
- buf: this.#initArray(initialBufferSize),
209
+ buf: initArray(initialBufferSize),
599
210
  count: 0,
211
+ write: () => { },
600
212
  };
601
213
  const inputBuffer = {
602
214
  buf: data,
@@ -611,52 +223,11 @@ export class LZMA {
611
223
  this.#encoder._matchFinder._keepSizeAfter = keepSizeAfter;
612
224
  blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
613
225
  if (this.#encoder._matchFinder._bufferBase == null || this.#encoder._matchFinder._blockSize != blockSize) {
614
- this.#encoder._matchFinder._bufferBase = this.#initArray(blockSize);
226
+ this.#encoder._matchFinder._bufferBase = initArray(blockSize);
615
227
  this.#encoder._matchFinder._blockSize = blockSize;
616
228
  }
617
229
  this.#encoder._matchFinder._pointerToLastSafePosition = this.#encoder._matchFinder._blockSize - keepSizeAfter;
618
230
  }
619
- #GetIndexByte(index) {
620
- const byte = this.#compressor.chunker.encoder._matchFinder._bufferBase[this.#compressor.chunker.encoder._matchFinder._bufferOffset
621
- + this.#compressor.chunker.encoder._matchFinder._pos
622
- + index];
623
- return byte;
624
- }
625
- #GetMatchLen(index, distance, limit) {
626
- const encoder = this.#compressor.chunker.encoder;
627
- if (encoder._matchFinder._streamEndWasReached) {
628
- if (encoder._matchFinder._pos + index + limit
629
- > encoder._matchFinder._streamPos) {
630
- limit = encoder._matchFinder._streamPos
631
- - (encoder._matchFinder._pos + index);
632
- }
633
- }
634
- ++distance;
635
- let i, pby = encoder._matchFinder._bufferOffset
636
- + encoder._matchFinder._pos
637
- + index;
638
- for (i = 0; i < limit
639
- && encoder._matchFinder._bufferBase[pby + i]
640
- == encoder._matchFinder._bufferBase[pby + i - distance]; ++i)
641
- ;
642
- return i;
643
- }
644
- #GetNumAvailableBytes() {
645
- const encoder = this.#compressor.chunker.encoder;
646
- return encoder._matchFinder._streamPos - encoder._matchFinder._pos;
647
- }
648
- #MoveBlock() {
649
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
650
- let offset = matchFinder._bufferOffset + matchFinder._pos - matchFinder._keepSizeBefore;
651
- if (offset > 0) {
652
- --offset;
653
- }
654
- let numBytes = matchFinder._bufferOffset + matchFinder._streamPos - offset;
655
- for (let i = 0; i < numBytes; ++i) {
656
- matchFinder._bufferBase[i] = matchFinder._bufferBase[offset + i];
657
- }
658
- matchFinder._bufferOffset -= offset;
659
- }
660
231
  #MovePos_1() {
661
232
  const matchFinder = this.#compressor.chunker.encoder._matchFinder;
662
233
  let pointerToPostion;
@@ -664,77 +235,34 @@ export class LZMA {
664
235
  if (matchFinder._pos > matchFinder._posLimit) {
665
236
  pointerToPostion = matchFinder._bufferOffset + matchFinder._pos;
666
237
  if (pointerToPostion > matchFinder._pointerToLastSafePosition) {
667
- this.#MoveBlock();
238
+ this.#lzInWindow.moveBlock();
668
239
  }
669
- this.#ReadBlock();
240
+ this.#lzInWindow.readBlock();
670
241
  }
671
242
  }
672
- #ReadBlock() {
673
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
674
- let bytesRead, pointerToPostion, size;
675
- if (matchFinder._streamEndWasReached) {
676
- return;
677
- }
678
- while (1) {
679
- size = -matchFinder._bufferOffset + matchFinder._blockSize - matchFinder._streamPos;
680
- if (!size) {
681
- return;
682
- }
683
- bytesRead = this.#read_0(matchFinder._bufferOffset + matchFinder._streamPos, size);
684
- if (bytesRead == -1) {
685
- matchFinder._posLimit = matchFinder._streamPos;
686
- pointerToPostion = matchFinder._bufferOffset + matchFinder._posLimit;
687
- if (pointerToPostion > matchFinder._pointerToLastSafePosition) {
688
- matchFinder._posLimit = matchFinder._pointerToLastSafePosition - matchFinder._bufferOffset;
689
- }
690
- matchFinder._streamEndWasReached = 1;
691
- return;
692
- }
693
- matchFinder._streamPos += bytesRead;
694
- if (matchFinder._streamPos >= matchFinder._pos + matchFinder._keepSizeAfter) {
695
- matchFinder._posLimit = matchFinder._streamPos - matchFinder._keepSizeAfter;
696
- }
697
- }
698
- }
699
- #ReduceOffsets(subValue) {
700
- this.#compressor.chunker.encoder._matchFinder._bufferOffset += subValue;
701
- this.#compressor.chunker.encoder._matchFinder._posLimit -= subValue;
702
- this.#compressor.chunker.encoder._matchFinder._pos -= subValue;
703
- this.#compressor.chunker.encoder._matchFinder._streamPos -= subValue;
704
- }
705
243
  #Create_3(keepAddBufferBefore, keepAddBufferAfter) {
706
244
  const dictionarySize = this.#encoder._dictionarySize;
707
245
  const numFastBytes = this.#encoder._numFastBytes;
708
- if (dictionarySize < this.#dictionarySizeThreshold) {
709
- this.#encoder._matchFinder._cutValue = 0x10 + (numFastBytes >> 1);
710
- const windowReservSize = ~~((dictionarySize
711
- + keepAddBufferBefore
712
- + numFastBytes
713
- + keepAddBufferAfter) / 2) + 0x100;
246
+ if (isDictionarySizeBelowThreshold(dictionarySize)) {
247
+ this.#encoder._matchFinder._cutValue = setCutValue(numFastBytes);
248
+ const windowReservSize = computeWindowReservSize(dictionarySize, keepAddBufferBefore, numFastBytes, keepAddBufferAfter);
714
249
  this.#Create_4(dictionarySize + keepAddBufferBefore, numFastBytes + keepAddBufferAfter, windowReservSize);
715
- this.#encoder._matchFinder._matchMaxLen = numFastBytes;
716
- let cyclicBufferSize = dictionarySize + 1;
717
- if (this.#encoder._matchFinder._cyclicBufferSize != cyclicBufferSize) {
718
- this.#encoder._matchFinder._son = this.#initArray((this.#encoder._matchFinder._cyclicBufferSize = cyclicBufferSize) * 2);
719
- }
720
- let hs = 0x10000;
250
+ this.#encoder._matchFinder._matchMaxLen = setMatchMaxLen(numFastBytes);
251
+ ensureCyclicBuffer(this.#encoder._matchFinder, dictionarySize);
252
+ const { hashMask, hashSizeSum } = computeHashSize(dictionarySize, this.#encoder._matchFinder.HASH_ARRAY);
721
253
  if (this.#encoder._matchFinder.HASH_ARRAY) {
722
- hs = dictionarySize - 1;
723
- hs |= hs >> 1;
724
- hs |= hs >> 2;
725
- hs |= hs >> 4;
726
- hs |= hs >> 0x08;
727
- hs >>= 1;
728
- hs |= 0xFFFF;
729
- if (hs > 0x1000000) {
730
- hs >>= 1;
254
+ this.#encoder._matchFinder._hashMask = hashMask;
255
+ const finalHashSizeSum = hashSizeSum + this.#encoder._matchFinder.kFixHashSize;
256
+ if (finalHashSizeSum !== this.#encoder._matchFinder._hashSizeSum) {
257
+ this.#encoder._matchFinder._hashSizeSum = finalHashSizeSum;
258
+ this.#encoder._matchFinder._hash = initArray(finalHashSizeSum);
731
259
  }
732
- this.#encoder._matchFinder._hashMask = hs;
733
- hs += 1;
734
- hs += this.#encoder._matchFinder.kFixHashSize;
735
260
  }
736
- if (hs != this.#encoder._matchFinder._hashSizeSum) {
737
- this.#encoder._matchFinder._hash = this.#initArray(this.#encoder._matchFinder._hashSizeSum = hs);
261
+ else {
262
+ if (hashSizeSum !== this.#encoder._matchFinder._hashSizeSum) {
263
+ this.#encoder._matchFinder._hashSizeSum = hashSizeSum;
264
+ this.#encoder._matchFinder._hash = initArray(hashSizeSum);
265
+ }
738
266
  }
739
267
  }
740
268
  }
@@ -859,9 +387,9 @@ export class LZMA {
859
387
  this.#compressor.chunker.encoder._matchFinder._pos = 0;
860
388
  this.#compressor.chunker.encoder._matchFinder._streamPos = 0;
861
389
  this.#compressor.chunker.encoder._matchFinder._streamEndWasReached = 0;
862
- this.#ReadBlock();
390
+ this.#lzInWindow.readBlock();
863
391
  this.#compressor.chunker.encoder._matchFinder._cyclicBufferPos = 0;
864
- this.#ReduceOffsets(-1);
392
+ this.#lzInWindow.reduceOffsets(-1);
865
393
  }
866
394
  #MovePos_0() {
867
395
  let subValue;
@@ -870,11 +398,11 @@ export class LZMA {
870
398
  matchFinder._cyclicBufferPos = 0;
871
399
  }
872
400
  this.#MovePos_1();
873
- if (matchFinder._pos == this.#dictionarySizeThreshold) {
401
+ if (matchFinder._pos == DICTIONARY_SIZE_THRESHOLD) {
874
402
  subValue = matchFinder._pos - matchFinder._cyclicBufferSize;
875
403
  this.#NormalizeLinks(matchFinder._cyclicBufferSize * 2, subValue);
876
404
  this.#NormalizeLinks(matchFinder._hashSizeSum, subValue);
877
- this.#ReduceOffsets(subValue);
405
+ this.#lzInWindow.reduceOffsets(subValue);
878
406
  }
879
407
  }
880
408
  /**
@@ -982,122 +510,11 @@ export class LZMA {
982
510
  this.#MovePos_0();
983
511
  } while ((num -= 1) != 0);
984
512
  }
985
- #CopyBlock(len) {
986
- const outputWindow = this.#decompressor.chunker.decoder.outWin;
987
- const distance = this.#decompressor.chunker.decoder.rep0;
988
- let pos = outputWindow._pos - distance - 1;
989
- if (pos < 0) {
990
- pos += outputWindow._windowSize;
991
- }
992
- for (; len != 0; len -= 1) {
993
- if (pos >= outputWindow._windowSize) {
994
- pos = 0;
995
- }
996
- outputWindow._buffer[outputWindow._pos] = outputWindow._buffer[pos];
997
- outputWindow._pos += 1;
998
- pos += 1;
999
- if (outputWindow._pos >= outputWindow._windowSize) {
1000
- this.#Flush_0();
1001
- }
1002
- }
1003
- }
1004
- #OutWindow_Create(windowSize) {
1005
- const outWin = this.#decoder.outWin;
1006
- if (outWin._buffer == null || outWin._windowSize != windowSize) {
1007
- outWin._buffer = this.#initArray(windowSize);
1008
- }
1009
- outWin._windowSize = windowSize;
1010
- outWin._pos = 0;
1011
- outWin._streamPos = 0;
1012
- }
1013
- #Flush_0() {
1014
- let size = this.#decoder.outWin._pos - this.#decoder.outWin._streamPos;
1015
- if (!size) {
1016
- return;
1017
- }
1018
- this.#write_0(this.#decoder.outWin._stream, this.#decoder.outWin._buffer, this.#decoder.outWin._streamPos, size);
1019
- if (this.#decoder.outWin._pos >= this.#decoder.outWin._windowSize) {
1020
- this.#decoder.outWin._pos = 0;
1021
- }
1022
- this.#decoder.outWin._streamPos = this.#decoder.outWin._pos;
1023
- }
1024
- #GetByte(distance) {
1025
- const outputWindow = this.#decompressor.chunker.decoder.outWin;
1026
- let pos = outputWindow._pos - distance - 1;
1027
- if (pos < 0) {
1028
- pos += outputWindow._windowSize;
1029
- }
1030
- return outputWindow._buffer[pos];
1031
- }
1032
- #PutByte(b) {
1033
- this.#decoder.outWin._buffer[this.#decoder.outWin._pos] = b;
1034
- this.#decoder.outWin._pos += 1;
1035
- if (this.#decoder.outWin._pos >= this.#decoder.outWin._windowSize) {
1036
- this.#Flush_0();
1037
- }
1038
- }
1039
- #OutWindow_ReleaseStream() {
1040
- this.#Flush_0();
1041
- this.#decoder.outWin._stream = null;
1042
- }
1043
- GetLenToPosState(len) {
1044
- len -= 2;
1045
- if (len < 4) {
1046
- return len;
1047
- }
1048
- return 3;
1049
- }
1050
- StateUpdateChar(index) {
1051
- if (index < 4) {
1052
- return 0;
1053
- }
1054
- if (index < 10) {
1055
- return index - 3;
1056
- }
1057
- return index - 6;
1058
- }
1059
- #processChunkDecode() {
1060
- if (!this.#decompressor.chunker.alive) {
1061
- throw new Error("Bad state");
1062
- }
1063
- if (this.#decompressor.chunker.encoder) {
1064
- throw new Error("No encoding");
1065
- }
1066
- const result = this.#CodeOneChunk();
1067
- if (result === -1) {
1068
- throw new Error("Corrupted input");
1069
- }
1070
- const decoder = this.#decompressor.chunker.decoder;
1071
- this.#decompressor.chunker.inBytesProcessed = decoder.nowPos64;
1072
- const isOutputComplete = (this.#compare(decoder.outSize, this.#P0_LONG_LIT) >= 0)
1073
- && (this.#compare(decoder.nowPos64, decoder.outSize) >= 0);
1074
- if (result || isOutputComplete) {
1075
- this.#Flush_0();
1076
- this.#OutWindow_ReleaseStream();
1077
- decoder.rangeDecoder.stream = null;
1078
- this.#decompressor.chunker.alive = 0;
1079
- }
1080
- return this.#decompressor.chunker.alive;
1081
- }
1082
- #processChunkEncode() {
1083
- if (!this.#compressor.chunker.alive) {
1084
- throw new Error("bad state");
1085
- }
1086
- if (!this.#compressor.chunker.encoder) {
1087
- throw new Error("No decoding");
1088
- }
1089
- this.#CodeOneBlock();
1090
- this.#compressor.chunker.inBytesProcessed = this.#compressor.chunker.encoder.processedInSize[0];
1091
- if (this.#compressor.chunker.encoder.finished[0]) {
1092
- this.#ReleaseStreams();
1093
- this.#compressor.chunker.alive = 0;
1094
- }
1095
- return this.#compressor.chunker.alive;
1096
- }
1097
513
  #CodeInChunks(inStream, outSize) {
1098
514
  this.#decoder.rangeDecoder.stream = inStream;
1099
- this.#OutWindow_ReleaseStream();
1100
- this.#decoder.outWin._stream = this.#decompressor.output;
515
+ this.#decoder.flush();
516
+ this.#decoder.outWindow.stream = null;
517
+ this.#decoder.outWindow.stream = this.#decompressor.output;
1101
518
  this.#Init_1();
1102
519
  this.#decoder.state = 0;
1103
520
  this.#decoder.rep0 = 0;
@@ -1105,259 +522,15 @@ export class LZMA {
1105
522
  this.#decoder.rep2 = 0;
1106
523
  this.#decoder.rep3 = 0;
1107
524
  this.#decoder.outSize = outSize;
1108
- this.#decoder.nowPos64 = this.#P0_LONG_LIT;
525
+ this.#decoder.nowPos64 = P0_LONG_LIT;
1109
526
  this.#decoder.prevByte = 0;
1110
- this.#decoder.decoder = this.#decoder;
1111
- this.#decoder.encoder = null;
1112
- this.#decoder.alive = 1;
1113
- return this.#decoder;
1114
- }
1115
- #CodeOneChunk() {
1116
- const decoder = this.#decompressor.chunker.decoder;
1117
- let decoder2, distance, len, numDirectBits, positionSlot;
1118
- let posState = this.#lowBits_0(decoder.nowPos64)
1119
- & decoder.posStateMask;
1120
- if (!this.#decodeBit(decoder.matchDecoders, (decoder.state << 4) + posState)) {
1121
- decoder2 = this.#GetDecoder(this.#lowBits_0(decoder.nowPos64), decoder.prevByte);
1122
- if (decoder.state < 7) {
1123
- decoder.prevByte = this.#DecodeNormal(decoder2);
1124
- }
1125
- else {
1126
- decoder.prevByte = this.#DecodeWithMatchByte(decoder2, this.#GetByte(decoder.rep0));
1127
- }
1128
- this.#PutByte(decoder.prevByte);
1129
- decoder.state = this.StateUpdateChar(decoder.state);
1130
- decoder.nowPos64 = this.#add(decoder.nowPos64, this.#P1_LONG_LIT);
1131
- }
1132
- else {
1133
- if (this.#decodeBit(decoder.repDecoders, decoder.state)) {
1134
- len = 0;
1135
- if (!this.#decodeBit(decoder.repG0Decoders, decoder.state)) {
1136
- if (!this.#decodeBit(decoder.rep0LongDecoders, (decoder.state << 4) + posState)) {
1137
- decoder.state = decoder.state < 7
1138
- ? 9
1139
- : 11;
1140
- len = 1;
1141
- }
1142
- }
1143
- else {
1144
- if (!this.#decodeBit(decoder.repG1Decoders, decoder.state)) {
1145
- distance = decoder.rep1;
1146
- }
1147
- else {
1148
- if (!this.#decodeBit(decoder.repG2Decoders, decoder.state)) {
1149
- distance = decoder.rep2;
1150
- }
1151
- else {
1152
- distance = decoder.rep3;
1153
- decoder.rep3 = decoder.rep2;
1154
- }
1155
- decoder.rep2 = decoder.rep1;
1156
- }
1157
- decoder.rep1 = decoder.rep0;
1158
- decoder.rep0 = distance;
1159
- }
1160
- if (!len) {
1161
- len = this.#Decode(decoder.repLenDecoder, posState) + 2;
1162
- decoder.state = decoder.state < 7 ? 0x08 : 11;
1163
- }
1164
- }
1165
- else {
1166
- decoder.rep3 = decoder.rep2;
1167
- decoder.rep2 = decoder.rep1;
1168
- decoder.rep1 = decoder.rep0;
1169
- len = 2 + this.#Decode(decoder.lenDecoder, posState);
1170
- decoder.state = decoder.state < 7 ? 7 : 10;
1171
- positionSlot = this.#RangeCoder_BitTreeDecoder_Decoder(decoder.posSlotDecoders[this.GetLenToPosState(len)]);
1172
- if (positionSlot >= 4) {
1173
- numDirectBits = (positionSlot >> 1) - 1;
1174
- decoder.rep0 = (2 | (positionSlot & 1)) << numDirectBits;
1175
- if (positionSlot < 14) {
1176
- decoder.rep0 += this.reverseDecode(decoder.posDecoders, decoder.rep0 - positionSlot - 1, numDirectBits);
1177
- }
1178
- else {
1179
- decoder.rep0 += this.#DecodeDirectBits(numDirectBits - 4) << 4;
1180
- decoder.rep0 += this.#ReverseDecode();
1181
- if (decoder.rep0 < 0) {
1182
- if (decoder.rep0 == -1) {
1183
- return 1;
1184
- }
1185
- return -1;
1186
- }
1187
- }
1188
- }
1189
- else {
1190
- decoder.rep0 = positionSlot;
1191
- }
1192
- }
1193
- if (this.#compare(this.#fromInt(decoder.rep0), decoder.nowPos64) >= 0
1194
- || decoder.rep0 >= decoder.dictSizeCheck) {
1195
- return -1;
1196
- }
1197
- this.#CopyBlock(len);
1198
- decoder.nowPos64 = this.#add(decoder.nowPos64, this.#fromInt(len));
1199
- decoder.prevByte = this.#GetByte(0);
1200
- }
1201
- return 0;
527
+ // Create and return decoder chunker
528
+ const decoderChunker = new DecoderChunker(this.#decoder);
529
+ decoderChunker.alive = 1;
530
+ return decoderChunker;
1202
531
  }
1203
532
  #Init_1() {
1204
- this.#decoder.outWin._streamPos = 0;
1205
- this.#decoder.outWin._pos = 0;
1206
- this.InitBitModels(this.#decoder.matchDecoders);
1207
- this.InitBitModels(this.#decoder.rep0LongDecoders);
1208
- this.InitBitModels(this.#decoder.repDecoders);
1209
- this.InitBitModels(this.#decoder.repG0Decoders);
1210
- this.InitBitModels(this.#decoder.repG1Decoders);
1211
- this.InitBitModels(this.#decoder.repG2Decoders);
1212
- this.InitBitModels(this.#decoder.posDecoders);
1213
- this.#Init_0(this.#decoder.literalDecoder);
1214
- for (let i = 0; i < 4; ++i) {
1215
- this.InitBitModels(this.#decoder.posSlotDecoders[i].models);
1216
- }
1217
- this.#Init(this.#decoder.lenDecoder);
1218
- this.#Init(this.#decoder.repLenDecoder);
1219
- this.InitBitModels(this.#decoder.posAlignDecoder.models);
1220
- this.#Init_8();
1221
- }
1222
- #SetDecoderProperties(properties) {
1223
- let dictionarySize, i, lc, lp, pb, remainder, val;
1224
- if (properties.length < 5) {
1225
- return 0;
1226
- }
1227
- val = properties[0] & 0xFF;
1228
- lc = val % 9;
1229
- remainder = ~~(val / 9);
1230
- lp = remainder % 5;
1231
- pb = ~~(remainder / 5);
1232
- dictionarySize = 0;
1233
- for (i = 0; i < 4; ++i) {
1234
- dictionarySize += (properties[1 + i] & 0xFF) << (i * 0x08);
1235
- }
1236
- // NOTE: If the input is bad, it might call for an insanely large dictionary size, which would crash the script.
1237
- if (dictionarySize > 0x5F5E0FF || !this.#SetLcLpPb(lc, lp, pb)) {
1238
- return 0;
1239
- }
1240
- return this.#SetDictionarySize(dictionarySize);
1241
- }
1242
- #SetDictionarySize(dictionarySize) {
1243
- if (dictionarySize < 0) {
1244
- return 0;
1245
- }
1246
- if (this.#decoder.dictSize != dictionarySize) {
1247
- this.#decoder.dictSize = dictionarySize;
1248
- this.#decoder.dictSizeCheck = Math.max(this.#decoder.dictSize, 1);
1249
- this.#OutWindow_Create(Math.max(this.#decoder.dictSizeCheck, 0x1000));
1250
- }
1251
- return 1;
1252
- }
1253
- #SetLcLpPb(lc, lp, pb) {
1254
- if (lc > 0x08 || lp > 4 || pb > 4) {
1255
- return 0;
1256
- }
1257
- this.#Create_0(lp, lc);
1258
- let numPosStates = 0x01 << pb;
1259
- this.#Create(this.#decoder.lenDecoder, numPosStates);
1260
- this.#Create(this.#decoder.repLenDecoder, numPosStates);
1261
- this.#decoder.posStateMask = numPosStates - 1;
1262
- return 1;
1263
- }
1264
- #Create(decoder, numPosStates) {
1265
- for (; decoder.numPosStates < numPosStates; decoder.numPosStates += 1) {
1266
- decoder.lowCoder[decoder.numPosStates] = this.#createBitTreeDecoder(3);
1267
- decoder.midCoder[decoder.numPosStates] = this.#createBitTreeDecoder(3);
1268
- }
1269
- }
1270
- #Decode(decoder, posState) {
1271
- if (!this.#decodeBit(decoder.choice, 0)) {
1272
- return this.#RangeCoder_BitTreeDecoder_Decoder(decoder.lowCoder[posState]);
1273
- }
1274
- let symbol = 0x08;
1275
- if (!this.#decodeBit(decoder.choice, 1)) {
1276
- symbol += this.#RangeCoder_BitTreeDecoder_Decoder(decoder.midCoder[posState]);
1277
- }
1278
- else {
1279
- symbol += 0x08 + this.#RangeCoder_BitTreeDecoder_Decoder(decoder.highCoder);
1280
- }
1281
- return symbol;
1282
- }
1283
- #createLenDecoder() {
1284
- const decoder = {
1285
- choice: this.#initArray(2),
1286
- lowCoder: [],
1287
- midCoder: [],
1288
- highCoder: this.#createBitTreeDecoder(0x08),
1289
- numPosStates: 0x00,
1290
- };
1291
- return decoder;
1292
- }
1293
- #Init(decoder) {
1294
- this.InitBitModels(decoder.choice);
1295
- for (let posState = 0; posState < decoder.numPosStates; ++posState) {
1296
- this.InitBitModels(decoder.lowCoder[posState].models);
1297
- this.InitBitModels(decoder.midCoder[posState].models);
1298
- }
1299
- this.InitBitModels(decoder.highCoder.models);
1300
- }
1301
- #Create_0(numPosBits, numPrevBits) {
1302
- let i, numStates;
1303
- if (this.#decoder.literalDecoder.coders !== null
1304
- && this.#decoder.literalDecoder.numPrevBits == numPrevBits
1305
- && this.#decoder.literalDecoder.numPosBits == numPosBits) {
1306
- return;
1307
- }
1308
- this.#decoder.literalDecoder.numPosBits = numPosBits;
1309
- this.#decoder.literalDecoder.posMask = (1 << numPosBits) - 1;
1310
- this.#decoder.literalDecoder.numPrevBits = numPrevBits;
1311
- numStates = 1 << (this.#decoder.literalDecoder.numPrevBits + this.#decoder.literalDecoder.numPosBits);
1312
- this.#decoder.literalDecoder.coders = [];
1313
- for (i = 0; i < numStates; ++i) {
1314
- this.#decoder.literalDecoder.coders[i] = this.#createLiteralDecoderEncoder2();
1315
- }
1316
- }
1317
- #GetDecoder(pos, prevByte) {
1318
- const literalDecoder = this.#decompressor.chunker.decoder.literalDecoder;
1319
- // Calculate index based on position and previous byte
1320
- const positionMask = pos & literalDecoder.posMask;
1321
- const prevBitsMask = (prevByte & 0xFF) >>> (8 - literalDecoder.numPrevBits);
1322
- const index = (positionMask << literalDecoder.numPrevBits) + prevBitsMask;
1323
- // Return decoder at calculated index
1324
- return literalDecoder.coders[index];
1325
- }
1326
- #Init_0(decoder) {
1327
- let i, numStates;
1328
- numStates = 1 << (decoder.numPrevBits + decoder.numPosBits);
1329
- for (i = 0; i < numStates; ++i) {
1330
- this.InitBitModels(decoder.coders[i].decoders);
1331
- }
1332
- }
1333
- #DecodeNormal(decoder) {
1334
- let symbol = 1;
1335
- do {
1336
- symbol = symbol << 1 | this.#decodeBit(decoder.decoders, symbol);
1337
- } while (symbol < 0x100);
1338
- return symbol << 24 >> 24;
1339
- }
1340
- #DecodeWithMatchByte(encoder, matchByte) {
1341
- let bit, matchBit, symbol = 1;
1342
- do {
1343
- matchBit = (matchByte >> 7) & 1;
1344
- matchByte <<= 1;
1345
- bit = this.#decodeBit(encoder.decoders, ((1 + matchBit) << 8) + symbol);
1346
- symbol = symbol << 1 | bit;
1347
- if (matchBit != bit) {
1348
- while (symbol < 0x100) {
1349
- symbol = symbol << 1 | this.#decodeBit(encoder.decoders, symbol);
1350
- }
1351
- break;
1352
- }
1353
- } while (symbol < 0x100);
1354
- return symbol << 24 >> 24;
1355
- }
1356
- #createLiteralDecoderEncoder2() {
1357
- const literalDecoder = {
1358
- decoders: this.#initArray(0x300),
1359
- };
1360
- return literalDecoder;
533
+ this.#decoder.init();
1361
534
  }
1362
535
  #Backward(cur) {
1363
536
  const encoder = this.#compressor.chunker.encoder;
@@ -1387,17 +560,10 @@ export class LZMA {
1387
560
  encoder._optimumCurrentIndex = encoder._optimum[0].posPrev;
1388
561
  return encoder._optimumCurrentIndex;
1389
562
  }
1390
- #BaseInit() {
1391
- this.#encoder._state = 0;
1392
- this.#encoder._previousByte = 0;
1393
- for (let i = 0; i < 4; ++i) {
1394
- this.#encoder._repDistances[i] = 0;
1395
- }
1396
- }
1397
563
  #CodeOneBlock() {
1398
564
  let baseVal, complexState, curByte, distance, footerBits, len, lenToPosState, matchByte, pos, posReduced, posSlot, posState, progressPosValuePrev, subCoder;
1399
- this.#compressor.chunker.encoder.processedInSize[0] = this.#P0_LONG_LIT;
1400
- this.#compressor.chunker.encoder.processedOutSize[0] = this.#P0_LONG_LIT;
565
+ this.#compressor.chunker.encoder.processedInSize[0] = P0_LONG_LIT;
566
+ this.#compressor.chunker.encoder.processedOutSize[0] = P0_LONG_LIT;
1401
567
  this.#compressor.chunker.encoder.finished[0] = 1;
1402
568
  progressPosValuePrev = this.#compressor.chunker.encoder.nowPos64;
1403
569
  if (this.#compressor.chunker.encoder._inStream) {
@@ -1410,74 +576,74 @@ export class LZMA {
1410
576
  return;
1411
577
  }
1412
578
  this.#compressor.chunker.encoder._finished = 1;
1413
- if (this.#eq(this.#compressor.chunker.encoder.nowPos64, this.#P0_LONG_LIT)) {
1414
- if (!this.#GetNumAvailableBytes()) {
1415
- this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
579
+ if (compare64(this.#compressor.chunker.encoder.nowPos64, P0_LONG_LIT) === 0) {
580
+ if (!this.#lzInWindow.getNumAvailableBytes()) {
581
+ this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
1416
582
  return;
1417
583
  }
1418
584
  this.#ReadMatchDistances();
1419
- posState = this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
1420
- this.#Encode_3(this.#compressor.chunker.encoder._isMatch, (this.#compressor.chunker.encoder._state << 4) + posState, 0);
1421
- this.#compressor.chunker.encoder._state = this.StateUpdateChar(this.#compressor.chunker.encoder._state);
1422
- curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1423
- this.#Encode_1(this.#LZMA_Encoder_GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
585
+ posState = lowBits64(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
586
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, (this.#compressor.chunker.encoder._state << 4) + posState, 0);
587
+ this.#compressor.chunker.encoder._state = stateUpdateChar(this.#compressor.chunker.encoder._state);
588
+ curByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
589
+ this.#compressor.chunker.encoder.encodeLiteral(this.#LZMA_Encoder_GetSubCoder(lowBits64(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
1424
590
  this.#compressor.chunker.encoder._previousByte = curByte;
1425
591
  this.#compressor.chunker.encoder._additionalOffset -= 1;
1426
- this.#compressor.chunker.encoder.nowPos64 = this.#add(this.#compressor.chunker.encoder.nowPos64, this.#P1_LONG_LIT);
592
+ this.#compressor.chunker.encoder.nowPos64 = add64(this.#compressor.chunker.encoder.nowPos64, P1_LONG_LIT);
1427
593
  }
1428
- if (!this.#GetNumAvailableBytes()) {
1429
- this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
594
+ if (!this.#lzInWindow.getNumAvailableBytes()) {
595
+ this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
1430
596
  return;
1431
597
  }
1432
598
  while (1) {
1433
- len = this.#GetOptimum(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
599
+ len = this.#GetOptimum(lowBits64(this.#compressor.chunker.encoder.nowPos64));
1434
600
  pos = this.#compressor.chunker.encoder.backRes;
1435
- posState = this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
601
+ posState = lowBits64(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
1436
602
  complexState = (this.#compressor.chunker.encoder._state << 4) + posState;
1437
603
  if (len == 1 && pos == -1) {
1438
- this.#Encode_3(this.#compressor.chunker.encoder._isMatch, complexState, 0);
1439
- curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1440
- subCoder = this.#LZMA_Encoder_GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
604
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, complexState, 0);
605
+ curByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
606
+ subCoder = this.#LZMA_Encoder_GetSubCoder(lowBits64(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
1441
607
  if (this.#compressor.chunker.encoder._state < 7) {
1442
- this.#Encode_1(subCoder, curByte);
608
+ this.#compressor.chunker.encoder.encodeLiteral(subCoder, curByte);
1443
609
  }
1444
610
  else {
1445
- matchByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._repDistances[0]
611
+ matchByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._repDistances[0]
1446
612
  - 1
1447
613
  - this.#compressor.chunker.encoder._additionalOffset);
1448
- this.#EncodeMatched(subCoder, matchByte, curByte);
614
+ this.#compressor.chunker.encoder.encodeMatched(subCoder, matchByte, curByte);
1449
615
  }
1450
616
  this.#compressor.chunker.encoder._previousByte = curByte;
1451
- this.#compressor.chunker.encoder._state = this.StateUpdateChar(this.#compressor.chunker.encoder._state);
617
+ this.#compressor.chunker.encoder._state = stateUpdateChar(this.#compressor.chunker.encoder._state);
1452
618
  }
1453
619
  else {
1454
- this.#Encode_3(this.#compressor.chunker.encoder._isMatch, complexState, 1);
620
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, complexState, 1);
1455
621
  if (pos < 4) {
1456
- this.#Encode_3(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 1);
622
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 1);
1457
623
  if (!pos) {
1458
- this.#Encode_3(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 0);
624
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 0);
1459
625
  if (len == 1) {
1460
- this.#Encode_3(this.#compressor.chunker.encoder._isRep0Long, complexState, 0);
626
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep0Long, complexState, 0);
1461
627
  }
1462
628
  else {
1463
- this.#Encode_3(this.#compressor.chunker.encoder._isRep0Long, complexState, 1);
629
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep0Long, complexState, 1);
1464
630
  }
1465
631
  }
1466
632
  else {
1467
- this.#Encode_3(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 1);
633
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 1);
1468
634
  if (pos == 1) {
1469
- this.#Encode_3(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 0);
635
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 0);
1470
636
  }
1471
637
  else {
1472
- this.#Encode_3(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 1);
1473
- this.#Encode_3(this.#compressor.chunker.encoder._isRepG2, this.#compressor.chunker.encoder._state, pos - 2);
638
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 1);
639
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG2, this.#compressor.chunker.encoder._state, pos - 2);
1474
640
  }
1475
641
  }
1476
642
  if (len == 1) {
1477
643
  this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 9 : 11;
1478
644
  }
1479
645
  else {
1480
- this.#Encode_0(this.#compressor.chunker.encoder._repMatchLenEncoder, len - 2, posState);
646
+ this.#compressor.chunker.encoder.encodeLength(this.#compressor.chunker.encoder._repMatchLenEncoder, len - 2, posState);
1481
647
  this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7
1482
648
  ? 0x08
1483
649
  : 11;
@@ -1492,23 +658,23 @@ export class LZMA {
1492
658
  }
1493
659
  }
1494
660
  else {
1495
- this.#Encode_3(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 0x00);
661
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 0);
1496
662
  this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 7 : 10;
1497
- this.#Encode_0(this.#compressor.chunker.encoder._lenEncoder, len - 0x02, posState);
663
+ this.#compressor.chunker.encoder.encodeLength(this.#compressor.chunker.encoder._lenEncoder, len - 0x02, posState);
1498
664
  pos -= 0x04;
1499
- posSlot = this.GetPosSlot(pos);
1500
- lenToPosState = this.GetLenToPosState(len);
1501
- this.#Encode_2(this.#compressor.chunker.encoder._posSlotEncoder[lenToPosState], posSlot);
665
+ posSlot = this.#compressor.chunker.encoder.getPosSlot(pos);
666
+ lenToPosState = getLenToPosState(len);
667
+ this.#compressor.chunker.encoder.encodeBitTree(this.#compressor.chunker.encoder._posSlotEncoder[lenToPosState], posSlot);
1502
668
  if (posSlot >= 0x04) {
1503
669
  footerBits = (posSlot >> 0x01) - 0x01;
1504
670
  baseVal = (0x02 | (posSlot & 0x01)) << footerBits;
1505
671
  posReduced = pos - baseVal;
1506
672
  if (posSlot < 0x0E) {
1507
- this.ReverseEncode(baseVal - posSlot - 0x01, footerBits, posReduced);
673
+ this.#compressor.chunker.encoder.reverseEncodeRange(baseVal - posSlot - 0x01, footerBits, posReduced);
1508
674
  }
1509
675
  else {
1510
- this.#EncodeDirectBits(posReduced >> 0x04, footerBits - 4);
1511
- this.#ReverseEncode(posReduced & 0x0F);
676
+ this.#compressor.chunker.encoder.encodeDirectBits(posReduced >> 0x04, footerBits - 4);
677
+ this.#compressor.chunker.encoder.reverseEncode(posReduced & 0x0F);
1512
678
  this.#compressor.chunker.encoder._alignPriceCount += 1;
1513
679
  }
1514
680
  }
@@ -1520,24 +686,24 @@ export class LZMA {
1520
686
  encoder2._repDistances[0] = distance;
1521
687
  encoder2._matchPriceCount += 0x01;
1522
688
  }
1523
- this.#compressor.chunker.encoder._previousByte = this.#GetIndexByte(len - 1 - this.#compressor.chunker.encoder._additionalOffset);
689
+ this.#compressor.chunker.encoder._previousByte = this.#lzInWindow.getIndexByte(len - 1 - this.#compressor.chunker.encoder._additionalOffset);
1524
690
  }
1525
691
  this.#compressor.chunker.encoder._additionalOffset -= len;
1526
- this.#compressor.chunker.encoder.nowPos64 = this.#add(this.#compressor.chunker.encoder.nowPos64, this.#fromInt(len));
692
+ this.#compressor.chunker.encoder.nowPos64 = add64(this.#compressor.chunker.encoder.nowPos64, fromInt64(len));
1527
693
  if (!this.#compressor.chunker.encoder._additionalOffset) {
1528
- if (this.#compressor.chunker.encoder._matchPriceCount >= 128) {
1529
- this.#FillDistancesPrices(this.#compressor.chunker.encoder);
694
+ if (this.#compressor.chunker.encoder._matchPriceCount >= 0x80) {
695
+ this.#compressor.chunker.encoder.fillDistancesPrices();
1530
696
  }
1531
- if (this.#compressor.chunker.encoder._alignPriceCount >= 16) {
697
+ if (this.#compressor.chunker.encoder._alignPriceCount >= 0x10) {
1532
698
  this.#FillAlignPrices(this.#compressor.chunker.encoder);
1533
699
  }
1534
700
  this.#compressor.chunker.encoder.processedInSize[0] = this.#compressor.chunker.encoder.nowPos64;
1535
701
  this.#compressor.chunker.encoder.processedOutSize[0] = this.#GetProcessedSizeAdd();
1536
- if (!this.#GetNumAvailableBytes()) {
1537
- this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
702
+ if (!this.#lzInWindow.getNumAvailableBytes()) {
703
+ this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
1538
704
  return;
1539
705
  }
1540
- if (this.#compare(this.#sub(this.#compressor.chunker.encoder.nowPos64, progressPosValuePrev), [0x1000, 0]) >= 0) {
706
+ if (compare64(sub64(this.#compressor.chunker.encoder.nowPos64, progressPosValuePrev), [0x1000, 0]) >= 0) {
1541
707
  this.#compressor.chunker.encoder._finished = 0;
1542
708
  this.#compressor.chunker.encoder.finished[0] = 0;
1543
709
  return;
@@ -1555,8 +721,9 @@ export class LZMA {
1555
721
  }
1556
722
  this.#SetType(binTree, numHashBytes);
1557
723
  this.#encoder._matchFinder = binTree;
724
+ this.#lzInWindow = new LzInWindow(binTree);
1558
725
  }
1559
- this.#LZMA_Encoder_LiteralEncoder_Create();
726
+ this.#encoder.createLiteralEncoder();
1560
727
  if (this.#encoder._dictionarySize == this.#encoder._dictionarySizePrev
1561
728
  && this.#encoder._numFastBytesPrev == this.#encoder._numFastBytes) {
1562
729
  return;
@@ -1570,7 +737,7 @@ export class LZMA {
1570
737
  this.#encoder._optimum[i] = {};
1571
738
  }
1572
739
  for (let i = 0; i < 4; ++i) {
1573
- this.#encoder._posSlotEncoder[i] = this.#createBitTreeEncoder(6);
740
+ this.#encoder._posSlotEncoder[i] = createBitTree(6);
1574
741
  }
1575
742
  }
1576
743
  #FillAlignPrices(encoder) {
@@ -1579,38 +746,11 @@ export class LZMA {
1579
746
  }
1580
747
  encoder._alignPriceCount = 0;
1581
748
  }
1582
- #FillDistancesPrices(encoder) {
1583
- let baseVal, bitTreeEncoder, footerBits, posSlot, st, st2;
1584
- for (let i = 4; i < 128; ++i) {
1585
- posSlot = this.GetPosSlot(i);
1586
- footerBits = (posSlot >> 1) - 1;
1587
- baseVal = (2 | (posSlot & 1)) << footerBits;
1588
- encoder.tempPrices[i] = this.ReverseGetPrice(encoder._posEncoders, baseVal - posSlot - 1, footerBits, i - baseVal);
1589
- }
1590
- for (let lenToPosState = 0; lenToPosState < 4; ++lenToPosState) {
1591
- bitTreeEncoder = encoder._posSlotEncoder[lenToPosState];
1592
- st = lenToPosState << 6;
1593
- for (posSlot = 0; posSlot < encoder._distTableSize; posSlot += 1) {
1594
- encoder._posSlotPrices[st + posSlot] = this.#RangeCoder_Encoder_GetPrice_1(bitTreeEncoder, posSlot);
1595
- }
1596
- for (posSlot = 14; posSlot < encoder._distTableSize; posSlot += 1) {
1597
- encoder._posSlotPrices[st + posSlot] += (posSlot >> 1) - 1 - 4 << 6;
1598
- }
1599
- st2 = lenToPosState * 128;
1600
- for (let i = 0; i < 4; ++i) {
1601
- encoder._distancesPrices[st2 + i] = encoder._posSlotPrices[st + i];
1602
- }
1603
- for (let i = 4; i < 128; ++i) {
1604
- encoder._distancesPrices[st2 + i] = encoder._posSlotPrices[st + this.GetPosSlot(i)] + encoder.tempPrices[i];
1605
- }
1606
- }
1607
- encoder._matchPriceCount = 0;
1608
- }
1609
749
  #Flush(nowPos) {
1610
750
  this.#ReleaseMFStream();
1611
- this.#WriteEndMarker(nowPos & this.#compressor.chunker.encoder._posStateMask);
751
+ this.#compressor.chunker.encoder.writeEndMarker(nowPos & this.#compressor.chunker.encoder._posStateMask);
1612
752
  for (let i = 0; i < 5; ++i) {
1613
- this.#ShiftLow();
753
+ this.#compressor.chunker.encoder.shiftLow();
1614
754
  }
1615
755
  }
1616
756
  #GetOptimum(position) {
@@ -1631,7 +771,7 @@ export class LZMA {
1631
771
  lenMain = this.#ReadMatchDistances();
1632
772
  }
1633
773
  numDistancePairs = encoder._numDistancePairs;
1634
- numAvailableBytes = this.#GetNumAvailableBytes() + 1;
774
+ numAvailableBytes = this.#lzInWindow.getNumAvailableBytes() + 1;
1635
775
  if (numAvailableBytes < 2) {
1636
776
  encoder.backRes = -1;
1637
777
  return 1;
@@ -1642,7 +782,7 @@ export class LZMA {
1642
782
  repMaxIndex = 0;
1643
783
  for (let i = 0; i < 4; ++i) {
1644
784
  encoder.reps[i] = encoder._repDistances[i];
1645
- encoder.repLens[i] = this.#GetMatchLen(-1, encoder.reps[i], 0x0111);
785
+ encoder.repLens[i] = this.#lzInWindow.getMatchLen(-1, encoder.reps[i], 0x0111);
1646
786
  if (encoder.repLens[i] > encoder.repLens[repMaxIndex]) {
1647
787
  repMaxIndex = i;
1648
788
  }
@@ -1658,19 +798,19 @@ export class LZMA {
1658
798
  this.#MovePos(lenMain - 1);
1659
799
  return lenMain;
1660
800
  }
1661
- currentByte = this.#GetIndexByte(-1);
1662
- matchByte = this.#GetIndexByte(-encoder._repDistances[0] - 1 - 1);
801
+ currentByte = this.#lzInWindow.getIndexByte(-1);
802
+ matchByte = this.#lzInWindow.getIndexByte(-encoder._repDistances[0] - 1 - 1);
1663
803
  if (lenMain < 2 && currentByte != matchByte && encoder.repLens[repMaxIndex] < 2) {
1664
804
  encoder.backRes = -1;
1665
805
  return 1;
1666
806
  }
1667
807
  encoder._optimum[0].state = encoder._state;
1668
808
  posState = position & encoder._posStateMask;
1669
- encoder._optimum[1].price = this.#probPrices[(encoder._isMatch[(encoder._state << 4) + posState]) >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
809
+ encoder._optimum[1].price = PROB_PRICES[(encoder._isMatch[(encoder._state << 4) + posState]) >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
1670
810
  this.#MakeAsChar(encoder._optimum[1]);
1671
- matchPrice = this.#probPrices[(2048 - encoder._isMatch[(encoder._state << 4) + posState])
811
+ matchPrice = PROB_PRICES[(2048 - encoder._isMatch[(encoder._state << 4) + posState])
1672
812
  >>> 2];
1673
- repMatchPrice = matchPrice + this.#probPrices[(2048 - encoder._isRep[encoder._state]) >>> 2];
813
+ repMatchPrice = matchPrice + PROB_PRICES[(2048 - encoder._isRep[encoder._state]) >>> 2];
1674
814
  if (matchByte == currentByte) {
1675
815
  shortRepPrice = repMatchPrice + this.#GetRepLen1Price(posState);
1676
816
  if (shortRepPrice < encoder._optimum[1].price) {
@@ -1692,7 +832,7 @@ export class LZMA {
1692
832
  encoder._optimum[0].backs3 = encoder.reps[3];
1693
833
  len = lenEnd;
1694
834
  do {
1695
- encoder._optimum[len].price = this.#kIfinityPrice;
835
+ encoder._optimum[len].price = INFINITY_PRICE;
1696
836
  len -= 1;
1697
837
  } while (len >= 2);
1698
838
  for (let i = 0; i < 4; ++i) {
@@ -1702,7 +842,7 @@ export class LZMA {
1702
842
  }
1703
843
  price_4 = repMatchPrice + this.#GetPureRepPrice(i, encoder._state, posState);
1704
844
  do {
1705
- curAndLenPrice = price_4 + this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, repLen - 2, posState);
845
+ curAndLenPrice = price_4 + encoder._repMatchLenEncoder.getPrice(repLen - 2, posState);
1706
846
  optimum = encoder._optimum[repLen];
1707
847
  if (curAndLenPrice < optimum.price) {
1708
848
  optimum.price = curAndLenPrice;
@@ -1713,7 +853,7 @@ export class LZMA {
1713
853
  } while ((repLen -= 1) >= 2);
1714
854
  }
1715
855
  normalMatchPrice = matchPrice
1716
- + this.#probPrices[(encoder._isRep[encoder._state]) >>> 2];
856
+ + PROB_PRICES[(encoder._isRep[encoder._state]) >>> 2];
1717
857
  len = encoder.repLens[0] >= 2 ? encoder.repLens[0] + 1 : 2;
1718
858
  if (len <= lenMain) {
1719
859
  offs = 0;
@@ -1767,7 +907,7 @@ export class LZMA {
1767
907
  else {
1768
908
  state = encoder._optimum[posPrev].state;
1769
909
  }
1770
- state = this.StateUpdateChar(state);
910
+ state = stateUpdateChar(state);
1771
911
  }
1772
912
  else {
1773
913
  state = encoder._optimum[posPrev].state;
@@ -1777,7 +917,7 @@ export class LZMA {
1777
917
  state = state < 7 ? 9 : 11;
1778
918
  }
1779
919
  else {
1780
- state = this.StateUpdateChar(state);
920
+ state = stateUpdateChar(state);
1781
921
  }
1782
922
  }
1783
923
  else {
@@ -1836,26 +976,26 @@ export class LZMA {
1836
976
  encoder._optimum[cur].backs2 = encoder.reps[2];
1837
977
  encoder._optimum[cur].backs3 = encoder.reps[3];
1838
978
  curPrice = encoder._optimum[cur].price;
1839
- currentByte = this.#GetIndexByte(-0x01);
1840
- matchByte = this.#GetIndexByte(-encoder.reps[0] - 1 - 1);
979
+ currentByte = this.#lzInWindow.getIndexByte(-0x01);
980
+ matchByte = this.#lzInWindow.getIndexByte(-encoder.reps[0] - 1 - 1);
1841
981
  posState = position & encoder._posStateMask;
1842
982
  curAnd1Price = curPrice
1843
- + this.#probPrices[(encoder._isMatch[(state << 0x04) + posState]) >>> 2]
1844
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, this.#GetIndexByte(-2)), state >= 7, matchByte, currentByte);
983
+ + PROB_PRICES[(encoder._isMatch[(state << 0x04) + posState]) >>> 2]
984
+ + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, this.#lzInWindow.getIndexByte(-2)), state >= 7, matchByte, currentByte);
1845
985
  nextOptimum = encoder._optimum[cur + 1];
1846
986
  nextIsChar = 0;
1847
987
  if (curAnd1Price < nextOptimum.price) {
1848
988
  nextOptimum.price = curAnd1Price;
1849
989
  nextOptimum.posPrev = cur;
1850
990
  nextOptimum.backPrev = -0x01;
1851
- nextOptimum.prev1IsChar = 0x00;
991
+ nextOptimum.prev1IsChar = 0;
1852
992
  nextIsChar = 1;
1853
993
  }
1854
- matchPrice = curPrice + this.#probPrices[(2048 - encoder._isMatch[(state << 4) + posState]) >>> 2];
1855
- repMatchPrice = matchPrice + this.#probPrices[(2048 - encoder._isRep[state]) >>> 2];
994
+ matchPrice = curPrice + PROB_PRICES[(2048 - encoder._isMatch[(state << 4) + posState]) >>> 2];
995
+ repMatchPrice = matchPrice + PROB_PRICES[(2048 - encoder._isRep[state]) >>> 2];
1856
996
  if (matchByte == currentByte && !(nextOptimum.posPrev < cur && !nextOptimum.backPrev)) {
1857
997
  shortRepPrice = repMatchPrice
1858
- + (this.#probPrices[(encoder._isRepG0[state]) >>> 0x02] + this.#probPrices[(encoder._isRep0Long[(state << 0x04) + posState]) >>> 0x02]);
998
+ + (PROB_PRICES[(encoder._isRepG0[state]) >>> 0x02] + PROB_PRICES[(encoder._isRep0Long[(state << 0x04) + posState]) >>> 0x02]);
1859
999
  if (shortRepPrice <= nextOptimum.price) {
1860
1000
  nextOptimum.price = shortRepPrice;
1861
1001
  nextOptimum.posPrev = cur;
@@ -1864,12 +1004,12 @@ export class LZMA {
1864
1004
  nextIsChar = 1;
1865
1005
  }
1866
1006
  }
1867
- numAvailableBytesFull = this.#GetNumAvailableBytes() + 1;
1007
+ numAvailableBytesFull = this.#lzInWindow.getNumAvailableBytes() + 1;
1868
1008
  numAvailableBytesFull = 0xFFF - cur < numAvailableBytesFull
1869
1009
  ? 0xFFF - cur
1870
1010
  : numAvailableBytesFull;
1871
1011
  numAvailableBytes = numAvailableBytesFull;
1872
- if (numAvailableBytes < 0x02) {
1012
+ if (numAvailableBytes < 2) {
1873
1013
  continue;
1874
1014
  }
1875
1015
  if (numAvailableBytes > encoder._numFastBytes) {
@@ -1877,19 +1017,19 @@ export class LZMA {
1877
1017
  }
1878
1018
  if (!nextIsChar && matchByte != currentByte) {
1879
1019
  t = Math.min(numAvailableBytesFull - 1, encoder._numFastBytes);
1880
- lenTest2 = this.#GetMatchLen(0x00, encoder.reps[0], t);
1881
- if (lenTest2 >= 0x02) {
1882
- state2 = this.StateUpdateChar(state);
1020
+ lenTest2 = this.#lzInWindow.getMatchLen(0, encoder.reps[0], t);
1021
+ if (lenTest2 >= 2) {
1022
+ state2 = stateUpdateChar(state);
1883
1023
  posStateNext = position + 1 & encoder._posStateMask;
1884
1024
  nextRepMatchPrice = curAnd1Price
1885
- + this.#probPrices[(0x800 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1886
- + this.#probPrices[(0x800 - encoder._isRep[state2]) >>> 2];
1025
+ + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1026
+ + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1887
1027
  offset = cur + 1 + lenTest2;
1888
1028
  while (lenEnd < offset) {
1889
- encoder._optimum[lenEnd += 1].price = this.#kIfinityPrice;
1029
+ encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1890
1030
  }
1891
- curAndLenPrice = nextRepMatchPrice + (price = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 0x02, posStateNext),
1892
- price + this.#GetPureRepPrice(0x00, state2, posStateNext));
1031
+ curAndLenPrice = nextRepMatchPrice + (price = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext),
1032
+ price + this.#GetPureRepPrice(0, state2, posStateNext));
1893
1033
  optimum = encoder._optimum[offset];
1894
1034
  if (curAndLenPrice < optimum.price) {
1895
1035
  optimum.price = curAndLenPrice;
@@ -1902,16 +1042,16 @@ export class LZMA {
1902
1042
  }
1903
1043
  startLen = 0x02;
1904
1044
  for (repIndex = 0; repIndex < 4; ++repIndex) {
1905
- lenTest = this.#GetMatchLen(-0x01, encoder.reps[repIndex], numAvailableBytes);
1045
+ lenTest = this.#lzInWindow.getMatchLen(-0x01, encoder.reps[repIndex], numAvailableBytes);
1906
1046
  if (lenTest < 2) {
1907
1047
  continue;
1908
1048
  }
1909
1049
  lenTestTemp = lenTest;
1910
1050
  do {
1911
1051
  while (lenEnd < cur + lenTest) {
1912
- encoder._optimum[lenEnd += 1].price = this.#kIfinityPrice;
1052
+ encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1913
1053
  }
1914
- curAndLenPrice = repMatchPrice + (price_0 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1054
+ curAndLenPrice = repMatchPrice + (price_0 = encoder._repMatchLenEncoder.getPrice(lenTest - 2, posState),
1915
1055
  price_0 + this.#GetPureRepPrice(repIndex, state, posState));
1916
1056
  optimum = encoder._optimum[cur + lenTest];
1917
1057
  if (curAndLenPrice < optimum.price) {
@@ -1927,23 +1067,23 @@ export class LZMA {
1927
1067
  }
1928
1068
  if (lenTest < numAvailableBytesFull) {
1929
1069
  t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1930
- lenTest2 = this.#GetMatchLen(lenTest, encoder.reps[repIndex], t);
1070
+ lenTest2 = this.#lzInWindow.getMatchLen(lenTest, encoder.reps[repIndex], t);
1931
1071
  if (lenTest2 >= 2) {
1932
1072
  state2 = state < 7 ? 0x08 : 11;
1933
1073
  posStateNext = position + lenTest & encoder._posStateMask;
1934
1074
  curAndLenCharPrice = repMatchPrice
1935
- + (price_1 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState), price_1 + this.#GetPureRepPrice(repIndex, state, posState))
1936
- + this.#probPrices[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1937
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), true, this.#GetIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#GetIndexByte(lenTest - 1));
1938
- state2 = this.StateUpdateChar(state2);
1075
+ + (price_1 = encoder._repMatchLenEncoder.getPrice(lenTest - 2, posState), price_1 + this.#GetPureRepPrice(repIndex, state, posState))
1076
+ + PROB_PRICES[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1077
+ + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#lzInWindow.getIndexByte(lenTest - 1 - 1)), true, this.#lzInWindow.getIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#lzInWindow.getIndexByte(lenTest - 1));
1078
+ state2 = stateUpdateChar(state2);
1939
1079
  posStateNext = position + lenTest + 1 & encoder._posStateMask;
1940
- nextMatchPrice = curAndLenCharPrice + this.#probPrices[(0x800 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1941
- nextRepMatchPrice = nextMatchPrice + this.#probPrices[(0x800 - encoder._isRep[state2]) >>> 2];
1080
+ nextMatchPrice = curAndLenCharPrice + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1081
+ nextRepMatchPrice = nextMatchPrice + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1942
1082
  offset = lenTest + 1 + lenTest2;
1943
1083
  while (lenEnd < cur + offset) {
1944
- encoder._optimum[lenEnd += 1].price = this.#kIfinityPrice;
1084
+ encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1945
1085
  }
1946
- curAndLenPrice = nextRepMatchPrice + (price_2 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext), price_2 + this.#GetPureRepPrice(0, state2, posStateNext));
1086
+ curAndLenPrice = nextRepMatchPrice + (price_2 = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext), price_2 + this.#GetPureRepPrice(0, state2, posStateNext));
1947
1087
  optimum = encoder._optimum[cur + offset];
1948
1088
  if (curAndLenPrice < optimum.price) {
1949
1089
  optimum.price = curAndLenPrice;
@@ -1964,9 +1104,9 @@ export class LZMA {
1964
1104
  numDistancePairs += 2;
1965
1105
  }
1966
1106
  if (newLen >= startLen) {
1967
- normalMatchPrice = matchPrice + this.#probPrices[(encoder._isRep[state]) >>> 2];
1107
+ normalMatchPrice = matchPrice + PROB_PRICES[(encoder._isRep[state]) >>> 2];
1968
1108
  while (lenEnd < cur + newLen) {
1969
- encoder._optimum[lenEnd += 1].price = this.#kIfinityPrice;
1109
+ encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1970
1110
  }
1971
1111
  offs = 0;
1972
1112
  while (startLen > encoder._matchDistances[offs]) {
@@ -1985,22 +1125,22 @@ export class LZMA {
1985
1125
  if (lenTest == encoder._matchDistances[offs]) {
1986
1126
  if (lenTest < numAvailableBytesFull) {
1987
1127
  t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1988
- lenTest2 = this.#GetMatchLen(lenTest, curBack, t);
1128
+ lenTest2 = this.#lzInWindow.getMatchLen(lenTest, curBack, t);
1989
1129
  if (lenTest2 >= 2) {
1990
1130
  state2 = state < 7 ? 7 : 10;
1991
1131
  posStateNext = position + lenTest & encoder._posStateMask;
1992
1132
  curAndLenCharPrice = curAndLenPrice
1993
- + this.#probPrices[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1994
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), true, this.#GetIndexByte(lenTest - (curBack + 1) - 1), this.#GetIndexByte(lenTest - 1));
1995
- state2 = this.StateUpdateChar(state2);
1133
+ + PROB_PRICES[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1134
+ + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#lzInWindow.getIndexByte(lenTest - 1 - 1)), true, this.#lzInWindow.getIndexByte(lenTest - (curBack + 1) - 1), this.#lzInWindow.getIndexByte(lenTest - 1));
1135
+ state2 = stateUpdateChar(state2);
1996
1136
  posStateNext = position + lenTest + 1 & encoder._posStateMask;
1997
- nextMatchPrice = curAndLenCharPrice + this.#probPrices[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1998
- nextRepMatchPrice = nextMatchPrice + this.#probPrices[(2048 - encoder._isRep[state2]) >>> 2];
1137
+ nextMatchPrice = curAndLenCharPrice + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1138
+ nextRepMatchPrice = nextMatchPrice + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1999
1139
  offset = lenTest + 1 + lenTest2;
2000
1140
  while (lenEnd < cur + offset) {
2001
- encoder._optimum[lenEnd += 1].price = this.#kIfinityPrice;
1141
+ encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
2002
1142
  }
2003
- curAndLenPrice = nextRepMatchPrice + (price_3 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext), price_3 + this.#GetPureRepPrice(0, state2, posStateNext));
1143
+ curAndLenPrice = nextRepMatchPrice + (price_3 = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext), price_3 + this.#GetPureRepPrice(0, state2, posStateNext));
2004
1144
  optimum = encoder._optimum[cur + offset];
2005
1145
  if (curAndLenPrice < optimum.price) {
2006
1146
  optimum.price = curAndLenPrice;
@@ -2026,7 +1166,7 @@ export class LZMA {
2026
1166
  }
2027
1167
  #LZMA_Encoder_GetPosLenPrice(pos, len, posState) {
2028
1168
  const encoder = this.#compressor.chunker.encoder;
2029
- let price, lenToPosState = this.GetLenToPosState(len);
1169
+ let price, lenToPosState = getLenToPosState(len);
2030
1170
  if (pos < 128) {
2031
1171
  price = encoder._distancesPrices[lenToPosState * 128 + pos];
2032
1172
  }
@@ -2034,55 +1174,33 @@ export class LZMA {
2034
1174
  const position = (lenToPosState << 6) + this.GetPosSlot2(pos);
2035
1175
  price = encoder._posSlotPrices[position] + encoder._alignPrices[pos & 15];
2036
1176
  }
2037
- return price + this.#RangeCoder_Encoder_GetPrice(encoder._lenEncoder, len - 2, posState);
1177
+ return price + encoder._lenEncoder.getPrice(len - 2, posState);
2038
1178
  }
2039
1179
  #GetPureRepPrice(repIndex, state, posState) {
2040
1180
  const encoder = this.#compressor.chunker.encoder;
2041
1181
  let price;
2042
1182
  if (!repIndex) {
2043
- price = this.#probPrices[(encoder._isRepG0[state]) >>> 2];
2044
- price += this.#probPrices[0x800 - this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
1183
+ price = PROB_PRICES[(encoder._isRepG0[state]) >>> 2];
1184
+ price += PROB_PRICES[0x800 - this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
2045
1185
  }
2046
1186
  else {
2047
- price = this.#probPrices[(0x800 - this.#compressor.chunker.encoder._isRepG0[state]) >>> 2];
1187
+ price = PROB_PRICES[(0x800 - this.#compressor.chunker.encoder._isRepG0[state]) >>> 2];
2048
1188
  if (repIndex == 1) {
2049
- price += this.#probPrices[(this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
1189
+ price += PROB_PRICES[(this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
2050
1190
  }
2051
1191
  else {
2052
- price += this.#probPrices[(0x800 - this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
2053
- price += this.GetPrice(this.#compressor.chunker.encoder._isRepG2[state], repIndex - 2);
1192
+ price += PROB_PRICES[(0x800 - this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
1193
+ price += getBitPrice(this.#compressor.chunker.encoder._isRepG2[state], repIndex - 2);
2054
1194
  }
2055
1195
  }
2056
1196
  return price;
2057
1197
  }
2058
1198
  #GetRepLen1Price(posState) {
2059
1199
  const encoder = this.#compressor.chunker.encoder;
2060
- const repG0Price = this.#probPrices[(encoder._isRepG0[encoder._state]) >>> 2];
2061
- const rep0LongPrice = this.#probPrices[encoder._isRep0Long[(encoder._state << 4) + posState] >>> 2];
1200
+ const repG0Price = PROB_PRICES[(encoder._isRepG0[encoder._state]) >>> 2];
1201
+ const rep0LongPrice = PROB_PRICES[encoder._isRep0Long[(encoder._state << 4) + posState] >>> 2];
2062
1202
  return repG0Price + rep0LongPrice;
2063
1203
  }
2064
- #Init_4() {
2065
- this.#BaseInit();
2066
- this.#Init_9();
2067
- this.InitBitModels(this.#encoder._isMatch);
2068
- this.InitBitModels(this.#encoder._isRep0Long);
2069
- this.InitBitModels(this.#encoder._isRep);
2070
- this.InitBitModels(this.#encoder._isRepG0);
2071
- this.InitBitModels(this.#encoder._isRepG1);
2072
- this.InitBitModels(this.#encoder._isRepG2);
2073
- this.InitBitModels(this.#encoder._posEncoders);
2074
- this.#Init_3();
2075
- for (let i = 0; i < 4; ++i) {
2076
- this.InitBitModels(this.#encoder._posSlotEncoder[i].models);
2077
- }
2078
- this.#Init_2(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
2079
- this.#Init_2(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
2080
- this.InitBitModels(this.#encoder._posAlignEncoder.models);
2081
- this.#encoder._longestMatchWasFound = 0;
2082
- this.#encoder._optimumEndIndex = 0;
2083
- this.#encoder._optimumCurrentIndex = 0;
2084
- this.#encoder._additionalOffset = 0;
2085
- }
2086
1204
  #MovePos(num) {
2087
1205
  if (num > 0) {
2088
1206
  this.#Skip(num);
@@ -2096,7 +1214,7 @@ export class LZMA {
2096
1214
  if (encoder._numDistancePairs > 0) {
2097
1215
  lenRes = encoder._matchDistances[encoder._numDistancePairs - 2];
2098
1216
  if (lenRes == encoder._numFastBytes) {
2099
- lenRes += this.#GetMatchLen(lenRes - 1, encoder._matchDistances[encoder._numDistancePairs - 1], 0x0111 - lenRes);
1217
+ lenRes += this.#lzInWindow.getMatchLen(lenRes - 1, encoder._matchDistances[encoder._numDistancePairs - 1], 0x0111 - lenRes);
2100
1218
  }
2101
1219
  }
2102
1220
  encoder._additionalOffset += 1;
@@ -2113,21 +1231,6 @@ export class LZMA {
2113
1231
  this.#ReleaseMFStream();
2114
1232
  this.#compressor.chunker.encoder._rangeEncoder.stream = null;
2115
1233
  }
2116
- #SetDictionarySize_0(dictionarySize) {
2117
- this.#encoder._dictionarySize = dictionarySize;
2118
- let dicLogSize = 0;
2119
- for (; dictionarySize > (1 << dicLogSize); ++dicLogSize)
2120
- ;
2121
- this.#encoder._distTableSize = dicLogSize * 2;
2122
- }
2123
- #SetMatchFinder(matchFinderIndex) {
2124
- const matchFinderIndexPrev = this.#encoder._matchFinderType;
2125
- this.#encoder._matchFinderType = matchFinderIndex;
2126
- if (this.#encoder._matchFinder && matchFinderIndexPrev != this.#encoder._matchFinderType) {
2127
- this.#encoder._dictionarySizePrev = -1;
2128
- this.#encoder._matchFinder = null;
2129
- }
2130
- }
2131
1234
  writeHeaderProperties() {
2132
1235
  const HEADER_SIZE = 0x5; // Total header size in bytes
2133
1236
  // First byte combines posStateBits, literalPosStateBits and literalContextBits
@@ -2138,184 +1241,20 @@ export class LZMA {
2138
1241
  // Shift dictionary size right by appropriate number of bits and mask to byte
2139
1242
  this.#encoder.properties[1 + byteIndex] = (this.#encoder._dictionarySize >> (0x08 * byteIndex)) & 0xFF;
2140
1243
  }
2141
- // Write the 5-byte header to output
2142
- this.#write_0(this.#compressor.output, this.#encoder.properties, 0, // Starting from index 0
2143
- HEADER_SIZE);
2144
- }
2145
- #WriteEndMarker(positionState) {
2146
- const encoder = this.#compressor.chunker.encoder;
2147
- this.#Encode_3(encoder._isMatch, (encoder._state << 4) + positionState, 1);
2148
- this.#Encode_3(encoder._isRep, encoder._state, 0);
2149
- encoder._state = encoder._state < 7 ? 7 : 10;
2150
- this.#Encode_0(encoder._lenEncoder, 0, positionState);
2151
- let lenToPosState = this.GetLenToPosState(2);
2152
- this.#Encode_2(encoder._posSlotEncoder[lenToPosState], 63);
2153
- this.#EncodeDirectBits(67108863, 26);
2154
- this.#ReverseEncode(15);
2155
- }
2156
- GetPosSlot(pos) {
2157
- if (pos < 2048) {
2158
- return this.#gFastPos[pos];
2159
- }
2160
- if (pos < 2097152) {
2161
- return this.#gFastPos[pos >> 10] + 20;
2162
- }
2163
- return this.#gFastPos[pos >> 20] + 40;
1244
+ this.#write_0(this.#compressor.output, this.#encoder.properties, 0, HEADER_SIZE);
2164
1245
  }
2165
1246
  GetPosSlot2(pos) {
2166
- if (pos < 131072) {
2167
- return this.#gFastPos[pos >> 6] + 12;
2168
- }
2169
- if (pos < 134217728) {
2170
- return this.#gFastPos[pos >> 16] + 32;
2171
- }
2172
- return this.#gFastPos[pos >> 26] + 52;
2173
- }
2174
- #Encode(encoder, symbol, posState) {
2175
- if (symbol < 8) {
2176
- this.#Encode_3(encoder.choice, 0, 0);
2177
- this.#Encode_2(encoder.lowCoder[posState], symbol);
2178
- }
2179
- else {
2180
- symbol -= 8;
2181
- this.#Encode_3(encoder.choice, 0, 1);
2182
- if (symbol < 8) {
2183
- this.#Encode_3(encoder.choice, 1, 0);
2184
- this.#Encode_2(encoder.midCoder[posState], symbol);
2185
- }
2186
- else {
2187
- this.#Encode_3(encoder.choice, 1, 1);
2188
- this.#Encode_2(encoder.highCoder, symbol - 8);
2189
- }
1247
+ if (pos < 0x20000) {
1248
+ return G_FAST_POS[pos >> 6] + 12;
2190
1249
  }
2191
- }
2192
- #createLenEncoder() {
2193
- const encoder = {};
2194
- encoder.choice = this.#initArray(2);
2195
- encoder.lowCoder = [];
2196
- encoder.midCoder = [];
2197
- encoder.highCoder = this.#createBitTreeEncoder(8);
2198
- for (let posState = 0; posState < 16; ++posState) {
2199
- encoder.lowCoder[posState] = this.#createBitTreeEncoder(3);
2200
- encoder.midCoder[posState] = this.#createBitTreeEncoder(3);
2201
- }
2202
- return encoder;
2203
- }
2204
- #Init_2(encoder, numPosStates) {
2205
- this.InitBitModels(encoder.choice);
2206
- for (let posState = 0; posState < numPosStates; ++posState) {
2207
- this.InitBitModels(encoder.lowCoder[posState].models);
2208
- this.InitBitModels(encoder.midCoder[posState].models);
2209
- }
2210
- this.InitBitModels(encoder.highCoder.models);
2211
- }
2212
- #SetPrices(encoder, posState, numSymbols, prices, st) {
2213
- let a0 = this.#probPrices[encoder.choice[0] >>> 2];
2214
- let a1 = this.#probPrices[2048 - encoder.choice[0] >>> 2];
2215
- let b0 = a1 + this.#probPrices[encoder.choice[1] >>> 2];
2216
- let b1 = a1 + this.#probPrices[2048 - encoder.choice[1] >>> 2];
2217
- let i = 0;
2218
- for (i = 0; i < 8; ++i) {
2219
- if (i >= numSymbols) {
2220
- return;
2221
- }
2222
- prices[st + i] = a0 + this.#RangeCoder_Encoder_GetPrice_1(encoder.lowCoder[posState], i);
2223
- }
2224
- for (; i < 16; ++i) {
2225
- if (i >= numSymbols) {
2226
- return;
2227
- }
2228
- prices[st + i] = b0 + this.#RangeCoder_Encoder_GetPrice_1(encoder.midCoder[posState], i - 8);
2229
- }
2230
- for (; i < numSymbols; ++i) {
2231
- prices[st + i] = b1 + this.#RangeCoder_Encoder_GetPrice_1(encoder.highCoder, i - 8 - 8);
2232
- }
2233
- }
2234
- #Encode_0(encoder, symbol, posState) {
2235
- this.#Encode(encoder, symbol, posState);
2236
- if ((encoder.counters[posState] -= 1) == 0) {
2237
- this.#SetPrices(encoder, posState, encoder.tableSize, encoder.prices, posState * 272);
2238
- encoder.counters[posState] = encoder.tableSize;
2239
- }
2240
- }
2241
- #createLenPriceTableEncoder() {
2242
- const encoder = this.#createLenEncoder();
2243
- encoder.prices = [];
2244
- encoder.counters = [];
2245
- return encoder;
2246
- }
2247
- #RangeCoder_Encoder_GetPrice(encoder, symbol, posState) {
2248
- return encoder.prices[posState * 272 + symbol];
2249
- }
2250
- #LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(encoder, numPosStates) {
2251
- for (let posState = 0; posState < numPosStates; ++posState) {
2252
- this.#SetPrices(encoder, posState, encoder.tableSize, encoder.prices, posState * 272);
2253
- encoder.counters[posState] = encoder.tableSize;
2254
- }
2255
- }
2256
- #LZMA_Encoder_LiteralEncoder_Create() {
2257
- const encoder = this.#encoder._literalEncoder;
2258
- let i, numStates;
2259
- if (encoder.coders != null
2260
- && encoder.numPrevBits == this.#encoder._numLiteralContextBits
2261
- && encoder.numPosBits == this.#encoder._numLiteralPosStateBits) {
2262
- return;
2263
- }
2264
- encoder.numPosBits = this.#encoder._numLiteralPosStateBits;
2265
- encoder.posMask = (1 << this.#encoder._numLiteralPosStateBits) - 1;
2266
- encoder.numPrevBits = this.#encoder._numLiteralContextBits;
2267
- numStates = 1 << (encoder.numPrevBits + encoder.numPosBits);
2268
- encoder.coders = [];
2269
- for (i = 0; i < numStates; ++i) {
2270
- encoder.coders[i] = this.#createLiteralEncoderEncoder2();
1250
+ if (pos < 0x8000000) {
1251
+ return G_FAST_POS[pos >> 16] + 32;
2271
1252
  }
1253
+ return G_FAST_POS[pos >> 26] + 52;
2272
1254
  }
2273
1255
  #LZMA_Encoder_GetSubCoder(pos, prevByte) {
2274
- const literalEncoder = this.#compressor.chunker.encoder._literalEncoder;
2275
- // Calculate position mask bits
2276
- const posBits = pos & literalEncoder.posMask;
2277
- const posShifted = posBits << literalEncoder.numPrevBits;
2278
- // Calculate previous byte bits
2279
- const prevByteShift = 0x08 - literalEncoder.numPrevBits;
2280
- const prevByteBits = (prevByte & 0xFF) >>> prevByteShift;
2281
- // Combine position and prevByte bits to get final index
2282
- const coderIndex = posShifted + prevByteBits;
2283
- return literalEncoder.coders[coderIndex];
2284
- }
2285
- #Init_3() {
2286
- const totalStates = 1 << (this.#encoder._literalEncoder.numPrevBits
2287
- + this.#encoder._literalEncoder.numPosBits);
2288
- for (let i = 0; i < totalStates; ++i) {
2289
- this.InitBitModels(this.#encoder._literalEncoder.coders[i].decoders);
2290
- }
2291
- }
2292
- #Encode_1(encoder, symbol) {
2293
- let bit, context = 1;
2294
- for (let i = 7; i >= 0; --i) {
2295
- bit = (symbol >> i) & 1;
2296
- this.#Encode_3(encoder.decoders, context, bit);
2297
- context = context << 1 | bit;
2298
- }
2299
- }
2300
- #EncodeMatched(encoder, matchByte, symbol) {
2301
- let bit, matchBit, state, same = true, context = 1;
2302
- for (let i = 7; i >= 0; --i) {
2303
- bit = (symbol >> i) & 1;
2304
- state = context;
2305
- if (same) {
2306
- matchBit = (matchByte >> i) & 1;
2307
- state += (1 + matchBit) << 8;
2308
- same = matchBit === bit;
2309
- }
2310
- this.#Encode_3(encoder.decoders, state, bit);
2311
- context = context << 1 | bit;
2312
- }
2313
- }
2314
- #createLiteralEncoderEncoder2() {
2315
- const encoder = {
2316
- decoders: this.#initArray(0x300),
2317
- };
2318
- return encoder;
1256
+ const subCoder = this.#compressor.chunker.encoder._literalEncoder.getSubCoder(pos, prevByte);
1257
+ return { decoders: subCoder.decoders };
2319
1258
  }
2320
1259
  #RangeCoder_Encoder_GetPrice_0(encoder, matchMode, matchByte, symbol) {
2321
1260
  let bit, context = 1, i = 7, matchBit, price = 0;
@@ -2323,7 +1262,7 @@ export class LZMA {
2323
1262
  for (; i >= 0; --i) {
2324
1263
  matchBit = (matchByte >> i) & 1;
2325
1264
  bit = (symbol >> i) & 1;
2326
- price += this.GetPrice(encoder.decoders[((1 + matchBit) << 8) + context], bit);
1265
+ price += getBitPrice(encoder.decoders[((1 + matchBit) << 8) + context], bit);
2327
1266
  context = context << 1 | bit;
2328
1267
  if (matchBit != bit) {
2329
1268
  --i;
@@ -2333,7 +1272,7 @@ export class LZMA {
2333
1272
  }
2334
1273
  for (; i >= 0; --i) {
2335
1274
  bit = symbol >> i & 1;
2336
- price += this.GetPrice(encoder.decoders[context], bit);
1275
+ price += getBitPrice(encoder.decoders[context], bit);
2337
1276
  context = context << 1 | bit;
2338
1277
  }
2339
1278
  return price;
@@ -2346,80 +1285,11 @@ export class LZMA {
2346
1285
  optimum.backPrev = 0;
2347
1286
  optimum.prev1IsChar = 0;
2348
1287
  }
2349
- #createBitTreeDecoder(numBitLevels) {
2350
- return {
2351
- numBitLevels: numBitLevels,
2352
- models: this.#initArray(1 << numBitLevels),
2353
- };
2354
- }
2355
- #RangeCoder_BitTreeDecoder_Decoder(rangeDecoder) {
2356
- let bitIndex, m = 1;
2357
- for (bitIndex = rangeDecoder.numBitLevels; bitIndex != 0; bitIndex -= 1) {
2358
- m = (m << 1) + this.#decodeBit(rangeDecoder.models, m);
2359
- }
2360
- return m - (1 << rangeDecoder.numBitLevels);
2361
- }
2362
- #ReverseDecode() {
2363
- const positionAlignmentDecoder = this.#decompressor.chunker.decoder.posAlignDecoder;
2364
- let symbol = 0;
2365
- for (let m = 1, bitIndex = 0, bit; bitIndex < positionAlignmentDecoder.numBitLevels; ++bitIndex) {
2366
- bit = this.#decodeBit(positionAlignmentDecoder.models, m);
2367
- m <<= 1;
2368
- m += bit;
2369
- symbol |= bit << bitIndex;
2370
- }
2371
- return symbol;
2372
- }
2373
- reverseDecode(Models, startIndex, NumBitLevels) {
2374
- let symbol = 0;
2375
- for (let bitIndex = 0, m = 1, bit; bitIndex < NumBitLevels; ++bitIndex) {
2376
- bit = this.#decodeBit(Models, startIndex + m);
2377
- m <<= 1;
2378
- m += bit;
2379
- symbol |= bit << bitIndex;
2380
- }
2381
- return symbol;
2382
- }
2383
- #createBitTreeEncoder(numBitLevels) {
2384
- return {
2385
- numBitLevels: numBitLevels,
2386
- models: this.#initArray(1 << numBitLevels),
2387
- };
2388
- }
2389
- #Encode_2(encoder, symbol) {
2390
- let bit, bitIndex, m = 1;
2391
- for (bitIndex = encoder.numBitLevels; bitIndex != 0;) {
2392
- bitIndex -= 1;
2393
- bit = symbol >>> bitIndex & 1;
2394
- this.#Encode_3(encoder.models, m, bit);
2395
- m = m << 1 | bit;
2396
- }
2397
- }
2398
- #RangeCoder_Encoder_GetPrice_1(encoder, symbol) {
2399
- let bit, bitIndex, m = 1, price = 0;
2400
- for (bitIndex = encoder.numBitLevels; bitIndex != 0;) {
2401
- bitIndex -= 1;
2402
- bit = symbol >>> bitIndex & 1;
2403
- price += this.GetPrice(encoder.models[m], bit);
2404
- m = (m << 1) + bit;
2405
- }
2406
- return price;
2407
- }
2408
- #ReverseEncode(symbol) {
2409
- const posAlignEncoder = this.#compressor.chunker.encoder._posAlignEncoder;
2410
- let bit, m = 1;
2411
- for (let i = 0; i < posAlignEncoder.numBitLevels; ++i) {
2412
- bit = symbol & 1;
2413
- this.#Encode_3(posAlignEncoder.models, m, bit);
2414
- m = m << 1 | bit;
2415
- symbol >>= 1;
2416
- }
2417
- }
2418
1288
  ReverseEncode(startIndex, NumBitLevels, symbol) {
2419
1289
  let bit, m = 1;
2420
1290
  for (let i = 0; i < NumBitLevels; ++i) {
2421
1291
  bit = symbol & 1;
2422
- this.#Encode_3(this.#compressor.chunker.encoder._posEncoders, startIndex + m, bit);
1292
+ this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._posEncoders, startIndex + m, bit);
2423
1293
  m = m << 1 | bit;
2424
1294
  symbol >>= 1;
2425
1295
  }
@@ -2429,132 +1299,14 @@ export class LZMA {
2429
1299
  for (let i = encoder.numBitLevels; i != 0; i -= 1) {
2430
1300
  bit = symbol & 1;
2431
1301
  symbol >>>= 1;
2432
- price += this.GetPrice(encoder.models[m], bit);
1302
+ price += getBitPrice(encoder.models[m], bit);
2433
1303
  m = m << 1 | bit;
2434
1304
  }
2435
1305
  return price;
2436
1306
  }
2437
- ReverseGetPrice(Models, startIndex, NumBitLevels, symbol) {
2438
- let bit, m = 1, price = 0;
2439
- for (let i = NumBitLevels; i != 0; i -= 1) {
2440
- bit = symbol & 1;
2441
- symbol >>>= 1;
2442
- price += this.#probPrices[((Models[startIndex + m] - bit ^ -bit) & 2047) >>> 2];
2443
- m = m << 1 | bit;
2444
- }
2445
- return price;
2446
- }
2447
- #decodeBit(probs, index) {
2448
- const rangeDecoder = this.#decompressor.chunker.decoder.rangeDecoder;
2449
- let newBound, prob = probs[index];
2450
- newBound = (rangeDecoder.rrange >>> 11) * prob;
2451
- if ((rangeDecoder.code ^ this.#MIN_INT32) < (newBound ^ this.#MIN_INT32)) {
2452
- rangeDecoder.rrange = newBound;
2453
- probs[index] = prob + (2048 - prob >>> 5) << 16 >> 16;
2454
- if (!(rangeDecoder.rrange & -16777216)) {
2455
- rangeDecoder.code = rangeDecoder.code << 8 | this.#read(rangeDecoder.stream);
2456
- rangeDecoder.rrange <<= 8;
2457
- }
2458
- return 0;
2459
- }
2460
- else {
2461
- rangeDecoder.rrange -= newBound;
2462
- rangeDecoder.code -= newBound;
2463
- probs[index] = prob - (prob >>> 5) << 16 >> 16;
2464
- if (!(rangeDecoder.rrange & -16777216)) {
2465
- rangeDecoder.code = rangeDecoder.code << 8 | this.#read(rangeDecoder.stream);
2466
- rangeDecoder.rrange <<= 8;
2467
- }
2468
- return 1;
2469
- }
2470
- }
2471
- #DecodeDirectBits(numTotalBits) {
2472
- const rangeDecoder = this.#decompressor.chunker.decoder.rangeDecoder;
2473
- let result = 0;
2474
- for (let i = numTotalBits; i != 0; i -= 1) {
2475
- rangeDecoder.rrange >>>= 1;
2476
- let t = (rangeDecoder.code - rangeDecoder.rrange) >>> 31;
2477
- rangeDecoder.code -= rangeDecoder.rrange & (t - 1);
2478
- result = result << 1 | 1 - t;
2479
- if (!(rangeDecoder.rrange & -16777216)) {
2480
- rangeDecoder.code = rangeDecoder.code << 8 | this.#read(rangeDecoder.stream);
2481
- rangeDecoder.rrange <<= 8;
2482
- }
2483
- }
2484
- return result;
2485
- }
2486
- #Init_8() {
2487
- this.#decoder.rangeDecoder.code = 0;
2488
- this.#decoder.rangeDecoder.rrange = -1;
2489
- for (let i = 0; i < 5; ++i) {
2490
- this.#decoder.rangeDecoder.code = this.#decoder.rangeDecoder.code << 8
2491
- | this.#read(this.#decoder.rangeDecoder.stream);
2492
- }
2493
- }
2494
- InitBitModels(probs) {
2495
- for (let i = probs.length - 1; i >= 0; --i) {
2496
- probs[i] = 1024;
2497
- }
2498
- }
2499
- #Encode_3(probs, index, symbol) {
2500
- const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2501
- let newBound, prob = probs[index];
2502
- newBound = (rangeEncoder.rrange >>> 11) * prob;
2503
- if (!symbol) {
2504
- rangeEncoder.rrange = newBound;
2505
- probs[index] = prob + (2048 - prob >>> 5) << 16 >> 16;
2506
- }
2507
- else {
2508
- rangeEncoder.low = this.#add(rangeEncoder.low, this.#and(this.#fromInt(newBound), [4294967295, 0]));
2509
- rangeEncoder.rrange -= newBound;
2510
- probs[index] = prob - (prob >>> 5) << 16 >> 16;
2511
- }
2512
- if (!(rangeEncoder.rrange & -16777216)) {
2513
- rangeEncoder.rrange <<= 8;
2514
- this.#ShiftLow();
2515
- }
2516
- }
2517
- #EncodeDirectBits(valueToEncode, numTotalBits) {
2518
- const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2519
- for (let i = numTotalBits - 1; i >= 0; i -= 1) {
2520
- rangeEncoder.rrange >>>= 1;
2521
- if ((valueToEncode >>> i & 1) == 1) {
2522
- rangeEncoder.low = this.#add(rangeEncoder.low, this.#fromInt(rangeEncoder.rrange));
2523
- }
2524
- if (!(rangeEncoder.rrange & -16777216)) {
2525
- rangeEncoder.rrange <<= 8;
2526
- this.#ShiftLow();
2527
- }
2528
- }
2529
- }
2530
1307
  #GetProcessedSizeAdd() {
2531
- const processedCacheSize = this.#add(this.#fromInt(this.#compressor.chunker.encoder._rangeEncoder.cacheSize), this.#compressor.chunker.encoder._rangeEncoder.position);
2532
- return this.#add(processedCacheSize, [4, 0]);
2533
- }
2534
- #Init_9() {
2535
- this.#encoder._rangeEncoder.position = this.#P0_LONG_LIT;
2536
- this.#encoder._rangeEncoder.low = this.#P0_LONG_LIT;
2537
- this.#encoder._rangeEncoder.rrange = -1;
2538
- this.#encoder._rangeEncoder.cacheSize = 1;
2539
- this.#encoder._rangeEncoder.cache = 0;
2540
- }
2541
- #ShiftLow() {
2542
- const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2543
- const LowHi = this.#lowBits_0(this.#shru(rangeEncoder.low, 32));
2544
- if (LowHi != 0 || this.#compare(rangeEncoder.low, [4278190080, 0]) < 0) {
2545
- rangeEncoder.position = this.#add(rangeEncoder.position, this.#fromInt(rangeEncoder.cacheSize));
2546
- let temp = rangeEncoder.cache;
2547
- do {
2548
- this.#write(rangeEncoder.stream, temp + LowHi);
2549
- temp = 255;
2550
- } while ((rangeEncoder.cacheSize -= 1) != 0);
2551
- rangeEncoder.cache = this.#lowBits_0(rangeEncoder.low) >>> 24;
2552
- }
2553
- rangeEncoder.cacheSize += 1;
2554
- rangeEncoder.low = this.#shl(this.#and(rangeEncoder.low, [16777215, 0]), 8);
2555
- }
2556
- GetPrice(Prob, symbol) {
2557
- return this.#probPrices[((Prob - symbol ^ -symbol) & 2047) >>> 2];
1308
+ const processedCacheSize = add64(fromInt64(this.#compressor.chunker.encoder._rangeEncoder.cacheSize), this.#compressor.chunker.encoder._rangeEncoder.position);
1309
+ return add64(processedCacheSize, [4, 0]);
2558
1310
  }
2559
1311
  #decodeString(utf) {
2560
1312
  let j = 0, x, y, z, l = utf.length, buf = [], charCodes = [];
@@ -2638,25 +1390,25 @@ export class LZMA {
2638
1390
  elen = 0;
2639
1391
  for (let i = 0; i < l; ++i) {
2640
1392
  ch = chars[i];
2641
- if (ch >= 1 && ch <= 127) {
1393
+ if (ch >= 1 && ch <= 0x7F) { // 127
2642
1394
  data[elen++] = ch << 24 >> 24;
2643
1395
  }
2644
- else if (!ch || ch >= 128 && ch <= 2047) {
2645
- data[elen++] = (192 | ch >> 6 & 31) << 24 >> 24;
2646
- data[elen++] = (128 | ch & 63) << 24 >> 24;
1396
+ else if (!ch || ch >= 0x80 && ch <= 0x7FF) {
1397
+ data[elen++] = (0xC0 | ch >> 6 & 0x1F) << 24 >> 24;
1398
+ data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
2647
1399
  }
2648
1400
  else {
2649
- data[elen++] = (224 | ch >> 12 & 15) << 24 >> 24;
2650
- data[elen++] = (128 | ch >> 6 & 63) << 24 >> 24;
2651
- data[elen++] = (128 | ch & 63) << 24 >> 24;
1401
+ data[elen++] = (0xE0 | ch >> 12 & 0x0F) << 24 >> 24;
1402
+ data[elen++] = (0x80 | ch >> 6 & 0x3F) << 24 >> 24;
1403
+ data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
2652
1404
  }
2653
1405
  }
2654
1406
  return data;
2655
1407
  }
2656
1408
  compress(data, mode = 5) {
2657
- const compressionMode = this.CompressionModes[mode];
1409
+ const compressionMode = MODES[mode];
2658
1410
  this.#byteArrayCompressor(data, compressionMode);
2659
- while (this.#processChunkEncode())
1411
+ while (this.#compressor.chunker.processChunk())
2660
1412
  ;
2661
1413
  const result = this.#toByteArray(this.#compressor.output);
2662
1414
  return new Int8Array(result);
@@ -2667,13 +1419,13 @@ export class LZMA {
2667
1419
  }
2668
1420
  decompress(bytearray) {
2669
1421
  this.#byteArrayDecompressor(bytearray);
2670
- while (this.#processChunkDecode())
1422
+ while (this.#decompressor.chunker.processChunk())
2671
1423
  ;
2672
1424
  return this.#toByteArray(this.#decompressor.output);
2673
1425
  }
2674
1426
  decompressString(bytearray) {
2675
1427
  this.#byteArrayDecompressor(bytearray);
2676
- while (this.#processChunkDecode())
1428
+ while (this.#decompressor.chunker.processChunk())
2677
1429
  ;
2678
1430
  const decodedByteArray = this.#toByteArray(this.#decompressor.output);
2679
1431
  const result = this.#decodeString(decodedByteArray);
@@ -2685,4 +1437,11 @@ export class LZMA {
2685
1437
  return String.fromCharCode(...result);
2686
1438
  }
2687
1439
  }
1440
+ // Public methods for chunker access
1441
+ codeOneBlock() {
1442
+ this.#CodeOneBlock();
1443
+ }
1444
+ releaseStreams() {
1445
+ this.#ReleaseStreams();
1446
+ }
2688
1447
  }