lzma1 0.0.2 → 0.0.4

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/dist/lzma.js ADDED
@@ -0,0 +1,2604 @@
1
+ // @ts-nocheck: remove when resolved all types
2
+ // Computed CRC32 lookup table
3
+ // const CRC32_TABLE = new Uint32Array(256);
4
+ // for (let i = 0; i < CRC32_TABLE.length; i++) {
5
+ // let crc = i;
6
+ // for (let j = 0; j < 8; j++) {
7
+ // crc = (crc >>> 1) ^ (0xEDB88320 * (crc & 1));
8
+ // }
9
+ // CRC32_TABLE[i] = crc;
10
+ // }
11
+ // dprint-ignore
12
+ const CRC32_TABLE = [
13
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
14
+ 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
15
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
16
+ 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
17
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
18
+ 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
19
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
20
+ 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
21
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
22
+ 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
23
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
24
+ 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
25
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
26
+ 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
27
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
28
+ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
29
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
30
+ 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
31
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
32
+ 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
33
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
34
+ 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
35
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
36
+ 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
37
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
38
+ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
39
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
40
+ 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
41
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
42
+ 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
43
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
44
+ 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
45
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
46
+ 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
47
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
48
+ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
49
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
50
+ 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
51
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
52
+ 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
53
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
54
+ 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
55
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
56
+ ];
57
+ export class LZMA {
58
+ #MAX_UINT32 = 4_294_967_296;
59
+ #MAX_INT32 = 2_147_483_647;
60
+ #MIN_INT32 = -2_147_483_648;
61
+ #MAX_COMPRESSION_SIZE = 9_223_372_036_854_775_807;
62
+ #N1_LONG_LIT;
63
+ #MIN_VALUE;
64
+ #P0_LONG_LIT = [0, 0];
65
+ #P1_LONG_LIT = [1, 0];
66
+ CompressionModes = {
67
+ 1: { searchDepth: 16, filterStrength: 64, modeIndex: 0 },
68
+ 2: { searchDepth: 20, filterStrength: 64, modeIndex: 0 },
69
+ 3: { searchDepth: 19, filterStrength: 64, modeIndex: 1 },
70
+ 4: { searchDepth: 20, filterStrength: 64, modeIndex: 1 },
71
+ 5: { searchDepth: 21, filterStrength: 128, modeIndex: 1 },
72
+ 6: { searchDepth: 22, filterStrength: 128, modeIndex: 1 },
73
+ 7: { searchDepth: 23, filterStrength: 128, modeIndex: 1 },
74
+ 8: { searchDepth: 24, filterStrength: 255, modeIndex: 1 },
75
+ 9: { searchDepth: 25, filterStrength: 255, modeIndex: 1 },
76
+ };
77
+ #encoder;
78
+ #decoder;
79
+ #probPrices;
80
+ #gFastPos;
81
+ #compressor;
82
+ #decompressor;
83
+ constructor() {
84
+ this.#N1_LONG_LIT = [4294967295, -this.#MAX_UINT32];
85
+ this.#MIN_VALUE = [0, -9223372036854775808];
86
+ this.#encoder = this.#initEncoder();
87
+ this.#decoder = this.#initDecoder();
88
+ this.#probPrices = this.#createProbPrices();
89
+ this.#gFastPos = this.#createFastPos();
90
+ this.#compressor = this.#initCompressor();
91
+ this.#decompressor = this.#initDecompressor();
92
+ }
93
+ #initEncoder() {
94
+ return {
95
+ _repDistances: this.#initArray(4),
96
+ _optimum: [],
97
+ _rangeEncoder: {
98
+ Stream: {
99
+ buf: [],
100
+ count: 0,
101
+ },
102
+ Range: 0,
103
+ _cache: 0,
104
+ Low: [],
105
+ },
106
+ _isMatch: this.#initArray(192),
107
+ _isRep: this.#initArray(12),
108
+ _isRepG0: this.#initArray(12),
109
+ _isRepG1: this.#initArray(12),
110
+ _isRepG2: this.#initArray(12),
111
+ _isRep0Long: this.#initArray(192),
112
+ _posSlotEncoder: [],
113
+ _posEncoders: this.#initArray(114),
114
+ _posAlignEncoder: this.#createBitTreeEncoder(4),
115
+ _lenEncoder: this.#createLenPriceTableEncoder(),
116
+ _repMatchLenEncoder: this.#createLenPriceTableEncoder(),
117
+ _literalEncoder: {},
118
+ _matchDistances: [],
119
+ _posSlotPrices: [],
120
+ _distancesPrices: [],
121
+ _alignPrices: this.#initArray(16),
122
+ reps: this.#initArray(4),
123
+ repLens: this.#initArray(4),
124
+ processedInSize: [this.#P0_LONG_LIT],
125
+ processedOutSize: [this.#P0_LONG_LIT],
126
+ finished: [0],
127
+ properties: this.#initArray(5),
128
+ tempPrices: this.#initArray(128),
129
+ _longestMatchLength: 0,
130
+ _matchFinderType: 1,
131
+ _numDistancePairs: 0,
132
+ _numFastBytesPrev: -1,
133
+ backRes: 0,
134
+ };
135
+ }
136
+ #initDecoder() {
137
+ const decoder = {
138
+ m_OutWindow: {},
139
+ m_RangeDecoder: {},
140
+ m_IsMatchDecoders: this.#initArray(192),
141
+ m_IsRepDecoders: this.#initArray(12),
142
+ m_IsRepG0Decoders: this.#initArray(12),
143
+ m_IsRepG1Decoders: this.#initArray(12),
144
+ m_IsRepG2Decoders: this.#initArray(12),
145
+ m_IsRep0LongDecoders: this.#initArray(192),
146
+ m_PosSlotDecoder: this.#initArray(4),
147
+ m_PosDecoders: this.#initArray(114),
148
+ m_PosAlignDecoder: this.#createBitTreeDecoder(4),
149
+ m_LenDecoder: this.#createLenDecoder({}),
150
+ m_RepLenDecoder: this.#createLenDecoder({}),
151
+ m_LiteralDecoder: {},
152
+ };
153
+ for (let i = 0; i < 4; ++i) {
154
+ decoder.m_PosSlotDecoder[i] = this.#createBitTreeDecoder(6);
155
+ }
156
+ return decoder;
157
+ }
158
+ #initCompressor() {
159
+ return {
160
+ chunker: {
161
+ alive: 0,
162
+ encoder: null,
163
+ decoder: null,
164
+ inBytesProcessed: [],
165
+ },
166
+ output: {
167
+ buf: this.#initArray(32),
168
+ count: 0,
169
+ },
170
+ };
171
+ }
172
+ #initDecompressor() {
173
+ return {
174
+ chunker: {
175
+ alive: 0,
176
+ encoder: null,
177
+ decoder: null,
178
+ inBytesProcessed: [],
179
+ },
180
+ output: {
181
+ buf: this.#initArray(32),
182
+ count: 0,
183
+ },
184
+ };
185
+ }
186
+ #createProbPrices() {
187
+ const probPrices = [];
188
+ for (let i = 8; i >= 0; --i) {
189
+ let start = 1 << (9 - i - 1);
190
+ let end = 1 << (9 - i);
191
+ for (let j = start; j < end; ++j) {
192
+ probPrices[j] = (i << 6) + ((end - j) << 6 >>> (9 - i - 1));
193
+ }
194
+ }
195
+ return probPrices;
196
+ }
197
+ #createFastPos() {
198
+ const gFastPos = [0, 1];
199
+ let c = 2;
200
+ for (let slotFast = 2; slotFast < 22; ++slotFast) {
201
+ let k = 1 << ((slotFast >> 1) - 1);
202
+ for (let j = 0; j < k; ++j, ++c) {
203
+ gFastPos[c] = slotFast;
204
+ }
205
+ }
206
+ return gFastPos;
207
+ }
208
+ #initArray(len) {
209
+ const array = [];
210
+ // This is MUCH faster than "new Array(len)" in newer versions of v8
211
+ // (starting with Node.js 0.11.15, which uses v8 3.28.73).
212
+ array[len - 1] = undefined;
213
+ return array;
214
+ }
215
+ #add(a, b) {
216
+ return this.#create(a[0] + b[0], a[1] + b[1]);
217
+ }
218
+ #and(a, b) {
219
+ 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);
220
+ const lowBits = this.#lowBits_0(a) & this.#lowBits_0(b);
221
+ let high = highBits * this.#MAX_UINT32;
222
+ let low = lowBits;
223
+ if (lowBits < 0) {
224
+ low += this.#MAX_UINT32;
225
+ }
226
+ return [low, high];
227
+ }
228
+ #compare(a, b) {
229
+ if (a[0] == b[0] && a[1] == b[1]) {
230
+ return 0;
231
+ }
232
+ const nega = a[1] < 0;
233
+ const negb = b[1] < 0;
234
+ if (nega && !negb) {
235
+ return -1;
236
+ }
237
+ if (!nega && negb) {
238
+ return 1;
239
+ }
240
+ if (this.#sub(a, b)[1] < 0) {
241
+ return -1;
242
+ }
243
+ return 1;
244
+ }
245
+ #create(valueLow, valueHigh) {
246
+ let diffHigh, diffLow;
247
+ valueHigh %= 1.8446744073709552E19;
248
+ valueLow %= 1.8446744073709552E19;
249
+ diffHigh = valueHigh % this.#MAX_UINT32;
250
+ diffLow = Math.floor(valueLow / this.#MAX_UINT32) * this.#MAX_UINT32;
251
+ valueHigh = valueHigh - diffHigh + diffLow;
252
+ valueLow = valueLow - diffLow + diffHigh;
253
+ while (valueLow < 0) {
254
+ valueLow += this.#MAX_UINT32;
255
+ valueHigh -= this.#MAX_UINT32;
256
+ }
257
+ while (valueLow > 4294967295) {
258
+ valueLow -= this.#MAX_UINT32;
259
+ valueHigh += this.#MAX_UINT32;
260
+ }
261
+ valueHigh = valueHigh % 1.8446744073709552E19;
262
+ while (valueHigh > 9223372032559808512) {
263
+ valueHigh -= 1.8446744073709552E19;
264
+ }
265
+ while (valueHigh < -9223372036854775808) {
266
+ valueHigh += 1.8446744073709552E19;
267
+ }
268
+ return [valueLow, valueHigh];
269
+ }
270
+ #eq(a, b) {
271
+ return a[0] == b[0] && a[1] == b[1];
272
+ }
273
+ #fromInt(value) {
274
+ if (value >= 0) {
275
+ return [value, 0];
276
+ }
277
+ else {
278
+ return [value + this.#MAX_UINT32, -this.#MAX_UINT32];
279
+ }
280
+ }
281
+ #lowBits_0(a) {
282
+ if (a[0] >= 2147483648) {
283
+ return ~~Math.max(Math.min(a[0] - this.#MAX_UINT32, this.#MAX_INT32), this.#MIN_INT32);
284
+ }
285
+ return ~~Math.max(Math.min(a[0], this.#MAX_INT32), this.#MIN_INT32);
286
+ }
287
+ #pwrAsDouble(n) {
288
+ if (n <= 30) {
289
+ return 1 << n;
290
+ }
291
+ return this.#pwrAsDouble(30) * this.#pwrAsDouble(n - 30);
292
+ }
293
+ #shl(a, n) {
294
+ let diff, newHigh, newLow, twoToN;
295
+ n &= 63;
296
+ if (this.#eq(a, this.#MIN_VALUE)) {
297
+ if (!n) {
298
+ return a;
299
+ }
300
+ return this.#P0_LONG_LIT;
301
+ }
302
+ if (a[1] < 0) {
303
+ throw new Error("Neg");
304
+ }
305
+ twoToN = this.#pwrAsDouble(n);
306
+ newHigh = a[1] * twoToN % 1.8446744073709552E19;
307
+ newLow = a[0] * twoToN;
308
+ diff = newLow - newLow % this.#MAX_UINT32;
309
+ newHigh += diff;
310
+ newLow -= diff;
311
+ if (newHigh >= this.#MAX_COMPRESSION_SIZE) {
312
+ newHigh -= 1.8446744073709552E19;
313
+ }
314
+ return [newLow, newHigh];
315
+ }
316
+ #shr(a, n) {
317
+ n &= 63;
318
+ let shiftFact = this.#pwrAsDouble(n);
319
+ return this.#create(Math.floor(a[0] / shiftFact), a[1] / shiftFact);
320
+ }
321
+ #shru(a, n) {
322
+ let sr = this.#shr(a, n);
323
+ n &= 63;
324
+ if (a[1] < 0) {
325
+ sr = this.#add(sr, this.#shl([2, 0], 63 - n));
326
+ }
327
+ return sr;
328
+ }
329
+ #sub(a, b) {
330
+ return this.#create(a[0] - b[0], a[1] - b[1]);
331
+ }
332
+ #read(inputStream) {
333
+ if (inputStream.pos >= inputStream.count) {
334
+ return -1;
335
+ }
336
+ return inputStream.buf[inputStream.pos++] & 255;
337
+ }
338
+ #read_0(off, len) {
339
+ const stream = this.#compressor.chunker.encoder._matchFinder._stream;
340
+ const buffer = this.#compressor.chunker.encoder._matchFinder._bufferBase;
341
+ if (stream.pos >= stream.count) {
342
+ return -1;
343
+ }
344
+ len = Math.min(len, stream.count - stream.pos);
345
+ this.#arraycopy(stream.buf, stream.pos, buffer, off, len);
346
+ stream.pos += len;
347
+ return len;
348
+ }
349
+ #toByteArray(output) {
350
+ const data = output.buf;
351
+ data.length = output.count;
352
+ return data;
353
+ }
354
+ #write(obj, b) {
355
+ obj.buf[obj.count++] = b << 24 >> 24;
356
+ }
357
+ #write_0(obj, buf, off, len) {
358
+ this.#arraycopy(buf, off, obj.buf, obj.count, len);
359
+ obj.count += len;
360
+ }
361
+ #getChars(inputString, srcBegin, srcEnd, dst, dstBegin) {
362
+ for (let srcIdx = srcBegin; srcIdx < srcEnd; ++srcIdx) {
363
+ dst[dstBegin++] = inputString.charCodeAt(srcIdx);
364
+ }
365
+ }
366
+ #arraycopy(src, srcOfs, dest, destOfs, len) {
367
+ for (let i = 0; i < len; ++i) {
368
+ try {
369
+ dest[destOfs + i] = src[srcOfs + i];
370
+ }
371
+ catch (error) {
372
+ break;
373
+ }
374
+ }
375
+ }
376
+ #configure(mode) {
377
+ this.#SetDictionarySize_0(1 << mode.searchDepth);
378
+ this.#encoder._numFastBytes = mode.filterStrength;
379
+ this.#SetMatchFinder(mode.modeIndex);
380
+ // lc is always 3
381
+ // lp is always 0
382
+ // pb is always 2
383
+ this.#encoder._numLiteralContextBits = 3;
384
+ this.#encoder._numLiteralPosStateBits = 0;
385
+ this.#encoder._posStateBits = 2;
386
+ this.#encoder._posStateMask = 3;
387
+ }
388
+ #initCompression(input, len, mode) {
389
+ if (this.#compare(len, this.#N1_LONG_LIT) < 0) {
390
+ throw new Error("invalid length " + len);
391
+ }
392
+ this.#compressor.length_0 = len;
393
+ this.#Encoder();
394
+ this.#configure(mode);
395
+ this.writeHeaderProperties();
396
+ for (let i = 0; i < 64; i += 8) {
397
+ this.#write(this.#compressor.output, this.#lowBits_0(this.#shr(len, i)) & 255);
398
+ }
399
+ // Initialize encoder stream and properties
400
+ this.#encoder._needReleaseMFStream = 0;
401
+ this.#encoder._inStream = input;
402
+ this.#encoder._finished = 0;
403
+ // Create and configure encoder
404
+ this.#Create_2();
405
+ this.#encoder._rangeEncoder.Stream = this.#compressor.output;
406
+ this.#Init_4();
407
+ // Initialize pricing tables
408
+ this.#FillDistancesPrices(this.#encoder);
409
+ this.#FillAlignPrices(this.#encoder);
410
+ // Configure length encoders
411
+ this.#encoder._lenEncoder._tableSize = this.#encoder._numFastBytes + 1 - 2;
412
+ this.#UpdateTables(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
413
+ this.#encoder._repMatchLenEncoder._tableSize = this.#encoder._numFastBytes + 1 - 2;
414
+ this.#UpdateTables(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
415
+ // Reset position counter
416
+ this.#encoder.nowPos64 = this.#P0_LONG_LIT;
417
+ // Create new chunker with configured encoder
418
+ this.#compressor.chunker = {
419
+ encoder: this.#encoder,
420
+ decoder: null,
421
+ alive: 1,
422
+ };
423
+ }
424
+ #byteArrayCompressor(data, mode) {
425
+ this.#compressor.output = {
426
+ buf: this.#initArray(32),
427
+ count: 0,
428
+ };
429
+ const inputBuffer = {
430
+ buf: data instanceof ArrayBuffer
431
+ ? new Uint8Array(data)
432
+ : data,
433
+ pos: 0,
434
+ count: data instanceof ArrayBuffer
435
+ ? new Uint8Array(data).length
436
+ : data.length,
437
+ };
438
+ this.#initCompression(inputBuffer, this.#fromInt(data.length), mode);
439
+ }
440
+ #initDecompression(input) {
441
+ let hex_length = "", properties = [], r, tmp_length;
442
+ for (let i = 0; i < 5; ++i) {
443
+ r = this.#read(input);
444
+ if (r == -1) {
445
+ throw new Error("truncated input");
446
+ }
447
+ properties[i] = r << 24 >> 24;
448
+ }
449
+ if (!this.#SetDecoderProperties(properties)) {
450
+ throw new Error("corrupted input");
451
+ }
452
+ for (let i = 0; i < 64; i += 8) {
453
+ r = this.#read(input);
454
+ if (r == -1) {
455
+ throw new Error("truncated input");
456
+ }
457
+ r = r.toString(16);
458
+ if (r.length == 1)
459
+ r = "0" + r;
460
+ hex_length = r + "" + hex_length;
461
+ }
462
+ /**
463
+ * Was the length set in the header (if it was compressed from a stream, the
464
+ * length is all f"s).
465
+ */
466
+ if (/^0+$|^f+$/i.test(hex_length)) {
467
+ // The length is unknown, so set to -1.
468
+ this.#compressor.length_0 = this.#N1_LONG_LIT;
469
+ }
470
+ else {
471
+ /**
472
+ * NOTE: If there is a problem with the decoder because of the length,
473
+ * you can always set the length to -1 (N1_longLit) which means unknown.
474
+ */
475
+ tmp_length = parseInt(hex_length, 16);
476
+ // If the length is too long to handle, just set it to unknown.
477
+ if (tmp_length > 4294967295) {
478
+ this.#compressor.length_0 = this.#N1_LONG_LIT;
479
+ }
480
+ else {
481
+ this.#compressor.length_0 = this.#fromInt(tmp_length);
482
+ }
483
+ }
484
+ this.#decompressor.chunker = this.#CodeInChunks(input, this.#compressor.length_0);
485
+ }
486
+ #byteArrayDecompressor(data) {
487
+ this.#decompressor.output = {
488
+ buf: this.#initArray(32),
489
+ count: 0,
490
+ };
491
+ const inputBuffer = {
492
+ buf: data,
493
+ pos: 0,
494
+ count: data.length,
495
+ };
496
+ this.#initDecompression(inputBuffer);
497
+ }
498
+ #Create_4(keepSizeBefore, keepSizeAfter, keepSizeReserv) {
499
+ let blockSize;
500
+ this.#encoder._matchFinder._keepSizeBefore = keepSizeBefore;
501
+ this.#encoder._matchFinder._keepSizeAfter = keepSizeAfter;
502
+ blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
503
+ if (this.#encoder._matchFinder._bufferBase == null || this.#encoder._matchFinder._blockSize != blockSize) {
504
+ this.#encoder._matchFinder._bufferBase = null;
505
+ this.#encoder._matchFinder._blockSize = blockSize;
506
+ this.#encoder._matchFinder._bufferBase = this.#initArray(this.#encoder._matchFinder._blockSize);
507
+ }
508
+ this.#encoder._matchFinder._pointerToLastSafePosition = this.#encoder._matchFinder._blockSize - keepSizeAfter;
509
+ }
510
+ #GetIndexByte(index) {
511
+ return this.#compressor.chunker.encoder._matchFinder._bufferBase[this.#compressor.chunker.encoder._matchFinder._bufferOffset
512
+ + this.#compressor.chunker.encoder._matchFinder._pos
513
+ + index];
514
+ }
515
+ #GetMatchLen(index, distance, limit) {
516
+ if (this.#compressor.chunker.encoder._matchFinder._streamEndWasReached) {
517
+ if (this.#compressor.chunker.encoder._matchFinder._pos + index + limit
518
+ > this.#compressor.chunker.encoder._matchFinder._streamPos) {
519
+ limit = this.#compressor.chunker.encoder._matchFinder._streamPos
520
+ - (this.#compressor.chunker.encoder._matchFinder._pos + index);
521
+ }
522
+ }
523
+ ++distance;
524
+ let i, pby = this.#compressor.chunker.encoder._matchFinder._bufferOffset
525
+ + this.#compressor.chunker.encoder._matchFinder._pos
526
+ + index;
527
+ for (i = 0; i < limit
528
+ && this.#compressor.chunker.encoder._matchFinder._bufferBase[pby + i]
529
+ == this.#compressor.chunker.encoder._matchFinder._bufferBase[pby + i - distance]; ++i)
530
+ ;
531
+ return i;
532
+ }
533
+ #GetNumAvailableBytes() {
534
+ return (this.#compressor.chunker.encoder._matchFinder._streamPos
535
+ - this.#compressor.chunker.encoder._matchFinder._pos);
536
+ }
537
+ #MoveBlock() {
538
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
539
+ let offset = matchFinder._bufferOffset + matchFinder._pos - matchFinder._keepSizeBefore;
540
+ if (offset > 0) {
541
+ --offset;
542
+ }
543
+ let numBytes = matchFinder._bufferOffset + matchFinder._streamPos - offset;
544
+ for (let i = 0; i < numBytes; ++i) {
545
+ matchFinder._bufferBase[i] = matchFinder._bufferBase[offset + i];
546
+ }
547
+ matchFinder._bufferOffset -= offset;
548
+ }
549
+ #MovePos_1() {
550
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
551
+ let pointerToPostion;
552
+ matchFinder._pos += 1;
553
+ if (matchFinder._pos > matchFinder._posLimit) {
554
+ pointerToPostion = matchFinder._bufferOffset + matchFinder._pos;
555
+ if (pointerToPostion > matchFinder._pointerToLastSafePosition) {
556
+ this.#MoveBlock();
557
+ }
558
+ this.#ReadBlock();
559
+ }
560
+ }
561
+ #ReadBlock() {
562
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
563
+ let bytesRead, pointerToPostion, size;
564
+ if (matchFinder._streamEndWasReached) {
565
+ return;
566
+ }
567
+ while (1) {
568
+ size = -matchFinder._bufferOffset + matchFinder._blockSize - matchFinder._streamPos;
569
+ if (!size) {
570
+ return;
571
+ }
572
+ bytesRead = this.#read_0(matchFinder._bufferOffset + matchFinder._streamPos, size);
573
+ if (bytesRead == -1) {
574
+ matchFinder._posLimit = matchFinder._streamPos;
575
+ pointerToPostion = matchFinder._bufferOffset + matchFinder._posLimit;
576
+ if (pointerToPostion > matchFinder._pointerToLastSafePosition) {
577
+ matchFinder._posLimit = matchFinder._pointerToLastSafePosition - matchFinder._bufferOffset;
578
+ }
579
+ matchFinder._streamEndWasReached = 1;
580
+ return;
581
+ }
582
+ matchFinder._streamPos += bytesRead;
583
+ if (matchFinder._streamPos >= matchFinder._pos + matchFinder._keepSizeAfter) {
584
+ matchFinder._posLimit = matchFinder._streamPos - matchFinder._keepSizeAfter;
585
+ }
586
+ }
587
+ }
588
+ #ReduceOffsets(subValue) {
589
+ this.#compressor.chunker.encoder._matchFinder._bufferOffset += subValue;
590
+ this.#compressor.chunker.encoder._matchFinder._posLimit -= subValue;
591
+ this.#compressor.chunker.encoder._matchFinder._pos -= subValue;
592
+ this.#compressor.chunker.encoder._matchFinder._streamPos -= subValue;
593
+ }
594
+ #Create_3(keepAddBufferBefore, keepAddBufferAfter) {
595
+ if (this.#encoder._dictionarySize < 1073741567) {
596
+ this.#encoder._matchFinder._cutValue = 16 + (this.#encoder._numFastBytes >> 1);
597
+ const windowReservSize = ~~((this.#encoder._dictionarySize
598
+ + keepAddBufferBefore
599
+ + this.#encoder._numFastBytes
600
+ + keepAddBufferAfter) / 2) + 256;
601
+ this.#Create_4(this.#encoder._dictionarySize + keepAddBufferBefore, this.#encoder._numFastBytes + keepAddBufferAfter, windowReservSize);
602
+ this.#encoder._matchFinder._matchMaxLen = this.#encoder._numFastBytes;
603
+ let cyclicBufferSize = this.#encoder._dictionarySize + 1;
604
+ if (this.#encoder._matchFinder._cyclicBufferSize != cyclicBufferSize) {
605
+ this.#encoder._matchFinder._son = this.#initArray((this.#encoder._matchFinder._cyclicBufferSize = cyclicBufferSize) * 2);
606
+ }
607
+ let hs = 65536;
608
+ if (this.#encoder._matchFinder.HASH_ARRAY) {
609
+ hs = this.#encoder._dictionarySize - 1;
610
+ hs |= hs >> 1;
611
+ hs |= hs >> 2;
612
+ hs |= hs >> 4;
613
+ hs |= hs >> 8;
614
+ hs >>= 1;
615
+ hs |= 65535;
616
+ if (hs > 16777216) {
617
+ hs >>= 1;
618
+ }
619
+ this.#encoder._matchFinder._hashMask = hs;
620
+ hs += 1;
621
+ hs += this.#encoder._matchFinder.kFixHashSize;
622
+ }
623
+ if (hs != this.#encoder._matchFinder._hashSizeSum) {
624
+ this.#encoder._matchFinder._hash = this.#initArray(this.#encoder._matchFinder._hashSizeSum = hs);
625
+ }
626
+ }
627
+ }
628
+ #GetMatches() {
629
+ let count, cur, curMatch, curMatch2, curMatch3, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, maxLen, offset, pby1, ptr0, ptr1, temp;
630
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
631
+ const distances = this.#compressor.chunker.encoder._matchDistances;
632
+ if (matchFinder._pos + matchFinder._matchMaxLen <= matchFinder._streamPos) {
633
+ lenLimit = matchFinder._matchMaxLen;
634
+ }
635
+ else {
636
+ lenLimit = matchFinder._streamPos - matchFinder._pos;
637
+ if (lenLimit < matchFinder.kMinMatchCheck) {
638
+ this.#MovePos_0();
639
+ return 0;
640
+ }
641
+ }
642
+ offset = 0;
643
+ matchMinPos = matchFinder._pos > matchFinder._cyclicBufferSize
644
+ ? matchFinder._pos - matchFinder._cyclicBufferSize
645
+ : 0;
646
+ cur = matchFinder._bufferOffset + matchFinder._pos;
647
+ maxLen = 1;
648
+ hash2Value = 0;
649
+ hash3Value = 0;
650
+ if (matchFinder.HASH_ARRAY) {
651
+ temp = CRC32_TABLE[matchFinder._bufferBase[cur] & 255] ^ matchFinder._bufferBase[cur + 1] & 255;
652
+ hash2Value = temp & 1023;
653
+ temp ^= (matchFinder._bufferBase[cur + 2] & 255) << 8;
654
+ hash3Value = temp & 65535;
655
+ hashValue = (temp ^ CRC32_TABLE[matchFinder._bufferBase[cur + 3] & 255] << 5) & matchFinder._hashMask;
656
+ }
657
+ else {
658
+ hashValue = matchFinder._bufferBase[cur] & 255 ^ (matchFinder._bufferBase[cur + 1] & 255) << 8;
659
+ }
660
+ curMatch = matchFinder._hash[matchFinder.kFixHashSize + hashValue] || 0;
661
+ if (matchFinder.HASH_ARRAY) {
662
+ curMatch2 = matchFinder._hash[hash2Value] || 0;
663
+ curMatch3 = matchFinder._hash[1024 + hash3Value] || 0;
664
+ matchFinder._hash[hash2Value] = matchFinder._pos;
665
+ matchFinder._hash[1024 + hash3Value] = matchFinder._pos;
666
+ if (curMatch2 > matchMinPos) {
667
+ if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch2] == matchFinder._bufferBase[cur]) {
668
+ distances[offset++] = maxLen = 2;
669
+ distances[offset++] = matchFinder._pos - curMatch2 - 1;
670
+ }
671
+ }
672
+ if (curMatch3 > matchMinPos) {
673
+ if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch3] == matchFinder._bufferBase[cur]) {
674
+ if (curMatch3 == curMatch2) {
675
+ offset -= 2;
676
+ }
677
+ distances[offset++] = maxLen = 3;
678
+ distances[offset++] = matchFinder._pos - curMatch3 - 1;
679
+ curMatch2 = curMatch3;
680
+ }
681
+ }
682
+ if (offset != 0 && curMatch2 == curMatch) {
683
+ offset -= 2;
684
+ maxLen = 1;
685
+ }
686
+ }
687
+ matchFinder._hash[matchFinder.kFixHashSize + hashValue] = matchFinder._pos;
688
+ ptr0 = (matchFinder._cyclicBufferPos << 1) + 1;
689
+ ptr1 = matchFinder._cyclicBufferPos << 1;
690
+ len0 = len1 = matchFinder.kNumHashDirectBytes;
691
+ if (matchFinder.kNumHashDirectBytes != 0) {
692
+ if (curMatch > matchMinPos) {
693
+ if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch + matchFinder.kNumHashDirectBytes] != matchFinder._bufferBase[cur + matchFinder.kNumHashDirectBytes]) {
694
+ distances[offset++] = maxLen = matchFinder.kNumHashDirectBytes;
695
+ distances[offset++] = matchFinder._pos - curMatch - 1;
696
+ }
697
+ }
698
+ }
699
+ count = matchFinder._cutValue;
700
+ while (1) {
701
+ if (curMatch <= matchMinPos || count == 0) {
702
+ count -= 1;
703
+ matchFinder._son[ptr0] = matchFinder._son[ptr1] = 0;
704
+ break;
705
+ }
706
+ delta = matchFinder._pos - curMatch;
707
+ cyclicPos = (delta <= matchFinder._cyclicBufferPos
708
+ ? matchFinder._cyclicBufferPos - delta
709
+ : matchFinder._cyclicBufferPos - delta + matchFinder._cyclicBufferSize) << 1;
710
+ pby1 = matchFinder._bufferOffset + curMatch;
711
+ len = len0 < len1 ? len0 : len1;
712
+ if (matchFinder._bufferBase[pby1 + len] == matchFinder._bufferBase[cur + len]) {
713
+ while ((len += 1) != lenLimit) {
714
+ if (matchFinder._bufferBase[pby1 + len] != matchFinder._bufferBase[cur + len]) {
715
+ break;
716
+ }
717
+ }
718
+ if (maxLen < len) {
719
+ distances[offset++] = maxLen = len;
720
+ distances[offset++] = delta - 1;
721
+ if (len == lenLimit) {
722
+ matchFinder._son[ptr1] = matchFinder._son[cyclicPos];
723
+ matchFinder._son[ptr0] = matchFinder._son[cyclicPos + 1];
724
+ break;
725
+ }
726
+ }
727
+ }
728
+ if ((matchFinder._bufferBase[pby1 + len] & 255) < (matchFinder._bufferBase[cur + len] & 255)) {
729
+ matchFinder._son[ptr1] = curMatch;
730
+ ptr1 = cyclicPos + 1;
731
+ curMatch = matchFinder._son[ptr1];
732
+ len1 = len;
733
+ }
734
+ else {
735
+ matchFinder._son[ptr0] = curMatch;
736
+ ptr0 = cyclicPos;
737
+ curMatch = matchFinder._son[ptr0];
738
+ len0 = len;
739
+ }
740
+ }
741
+ this.#MovePos_0();
742
+ return offset;
743
+ }
744
+ #Init_5() {
745
+ this.#compressor.chunker.encoder._matchFinder._bufferOffset = 0;
746
+ this.#compressor.chunker.encoder._matchFinder._pos = 0;
747
+ this.#compressor.chunker.encoder._matchFinder._streamPos = 0;
748
+ this.#compressor.chunker.encoder._matchFinder._streamEndWasReached = 0;
749
+ this.#ReadBlock();
750
+ this.#compressor.chunker.encoder._matchFinder._cyclicBufferPos = 0;
751
+ this.#ReduceOffsets(-1);
752
+ }
753
+ #MovePos_0() {
754
+ let subValue;
755
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
756
+ if ((matchFinder._cyclicBufferPos += 1) >= matchFinder._cyclicBufferSize) {
757
+ matchFinder._cyclicBufferPos = 0;
758
+ }
759
+ this.#MovePos_1();
760
+ if (matchFinder._pos == 1073741823) {
761
+ subValue = matchFinder._pos - matchFinder._cyclicBufferSize;
762
+ this.#NormalizeLinks(matchFinder._cyclicBufferSize * 2, subValue);
763
+ this.#NormalizeLinks(matchFinder._hashSizeSum, subValue);
764
+ this.#ReduceOffsets(subValue);
765
+ }
766
+ }
767
+ /**
768
+ * This is only called after reading one whole gigabyte.
769
+ */
770
+ #NormalizeLinks(numItems, subValue) {
771
+ const items = this.#compressor.chunker.encoder._matchFinder._son;
772
+ let value;
773
+ for (let i = 0; i < numItems; ++i) {
774
+ value = items[i] || 0;
775
+ if (value <= subValue) {
776
+ value = 0;
777
+ }
778
+ else {
779
+ value -= subValue;
780
+ }
781
+ items[i] = value;
782
+ }
783
+ }
784
+ #SetType(binTree, numHashBytes) {
785
+ binTree.HASH_ARRAY = numHashBytes > 2;
786
+ if (binTree.HASH_ARRAY) {
787
+ binTree.kNumHashDirectBytes = 0;
788
+ binTree.kMinMatchCheck = 4;
789
+ binTree.kFixHashSize = 66560;
790
+ }
791
+ else {
792
+ binTree.kNumHashDirectBytes = 2;
793
+ binTree.kMinMatchCheck = 3;
794
+ binTree.kFixHashSize = 0;
795
+ }
796
+ }
797
+ #Skip(num) {
798
+ const matchFinder = this.#compressor.chunker.encoder._matchFinder;
799
+ var count, cur, curMatch, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, pby1, ptr0, ptr1, temp;
800
+ do {
801
+ if (matchFinder._pos + matchFinder._matchMaxLen <= matchFinder._streamPos) {
802
+ lenLimit = matchFinder._matchMaxLen;
803
+ }
804
+ else {
805
+ lenLimit = matchFinder._streamPos - matchFinder._pos;
806
+ if (lenLimit < matchFinder.kMinMatchCheck) {
807
+ this.#MovePos_0();
808
+ continue;
809
+ }
810
+ }
811
+ matchMinPos = matchFinder._pos > matchFinder._cyclicBufferSize
812
+ ? matchFinder._pos - matchFinder._cyclicBufferSize
813
+ : 0;
814
+ cur = matchFinder._bufferOffset + matchFinder._pos;
815
+ if (matchFinder.HASH_ARRAY) {
816
+ temp = CRC32_TABLE[matchFinder._bufferBase[cur] & 255] ^ matchFinder._bufferBase[cur + 1] & 255;
817
+ hash2Value = temp & 1023;
818
+ matchFinder._hash[hash2Value] = matchFinder._pos;
819
+ temp ^= (matchFinder._bufferBase[cur + 2] & 255) << 8;
820
+ hash3Value = temp & 65535;
821
+ matchFinder._hash[1024 + hash3Value] = matchFinder._pos;
822
+ hashValue = (temp ^ CRC32_TABLE[matchFinder._bufferBase[cur + 3] & 255] << 5)
823
+ & matchFinder._hashMask;
824
+ }
825
+ else {
826
+ hashValue = matchFinder._bufferBase[cur] & 255 ^ (matchFinder._bufferBase[cur + 1] & 255) << 8;
827
+ }
828
+ curMatch = matchFinder._hash[matchFinder.kFixHashSize + hashValue];
829
+ matchFinder._hash[matchFinder.kFixHashSize + hashValue] = matchFinder._pos;
830
+ ptr0 = (matchFinder._cyclicBufferPos << 1) + 1;
831
+ ptr1 = matchFinder._cyclicBufferPos << 1;
832
+ len0 = len1 = matchFinder.kNumHashDirectBytes;
833
+ count = matchFinder._cutValue;
834
+ while (1) {
835
+ if (curMatch <= matchMinPos || count == 0) {
836
+ count -= 1;
837
+ matchFinder._son[ptr0] = matchFinder._son[ptr1] = 0;
838
+ break;
839
+ }
840
+ delta = matchFinder._pos - curMatch;
841
+ cyclicPos = (delta <= matchFinder._cyclicBufferPos
842
+ ? matchFinder._cyclicBufferPos - delta
843
+ : matchFinder._cyclicBufferPos - delta + matchFinder._cyclicBufferSize) << 1;
844
+ pby1 = matchFinder._bufferOffset + curMatch;
845
+ len = len0 < len1 ? len0 : len1;
846
+ if (matchFinder._bufferBase[pby1 + len] == matchFinder._bufferBase[cur + len]) {
847
+ while ((len += 1) != lenLimit) {
848
+ if (matchFinder._bufferBase[pby1 + len] != matchFinder._bufferBase[cur + len]) {
849
+ break;
850
+ }
851
+ }
852
+ if (len == lenLimit) {
853
+ matchFinder._son[ptr1] = matchFinder._son[cyclicPos];
854
+ matchFinder._son[ptr0] = matchFinder._son[cyclicPos + 1];
855
+ break;
856
+ }
857
+ }
858
+ if ((matchFinder._bufferBase[pby1 + len] & 255) < (matchFinder._bufferBase[cur + len] & 255)) {
859
+ matchFinder._son[ptr1] = curMatch;
860
+ ptr1 = cyclicPos + 1;
861
+ curMatch = matchFinder._son[ptr1];
862
+ len1 = len;
863
+ }
864
+ else {
865
+ matchFinder._son[ptr0] = curMatch;
866
+ ptr0 = cyclicPos;
867
+ curMatch = matchFinder._son[ptr0];
868
+ len0 = len;
869
+ }
870
+ }
871
+ this.#MovePos_0();
872
+ } while ((num -= 1) != 0);
873
+ }
874
+ #CopyBlock(len) {
875
+ const outputWindow = this.#decompressor.chunker.decoder.m_OutWindow;
876
+ const distance = this.#decompressor.chunker.decoder.rep0;
877
+ let pos = outputWindow._pos - distance - 1;
878
+ if (pos < 0) {
879
+ pos += outputWindow._windowSize;
880
+ }
881
+ for (; len != 0; len -= 1) {
882
+ if (pos >= outputWindow._windowSize) {
883
+ pos = 0;
884
+ }
885
+ outputWindow._buffer[outputWindow._pos] = outputWindow._buffer[pos];
886
+ outputWindow._pos += 1;
887
+ pos += 1;
888
+ if (outputWindow._pos >= outputWindow._windowSize) {
889
+ this.#Flush_0(outputWindow);
890
+ }
891
+ }
892
+ }
893
+ #Create_5(m_OutWindow, windowSize) {
894
+ if (m_OutWindow._buffer == null || m_OutWindow._windowSize != windowSize) {
895
+ m_OutWindow._buffer = this.#initArray(windowSize);
896
+ }
897
+ m_OutWindow._windowSize = windowSize;
898
+ m_OutWindow._pos = 0;
899
+ m_OutWindow._streamPos = 0;
900
+ }
901
+ #Flush_0(obj) {
902
+ var size = obj._pos - obj._streamPos;
903
+ if (!size) {
904
+ return;
905
+ }
906
+ this.#write_0(obj._stream, obj._buffer, obj._streamPos, size);
907
+ if (obj._pos >= obj._windowSize) {
908
+ obj._pos = 0;
909
+ }
910
+ obj._streamPos = obj._pos;
911
+ }
912
+ #GetByte(distance) {
913
+ const outputWindow = this.#decompressor.chunker.decoder.m_OutWindow;
914
+ var pos = outputWindow._pos - distance - 1;
915
+ if (pos < 0) {
916
+ pos += outputWindow._windowSize;
917
+ }
918
+ return outputWindow._buffer[pos];
919
+ }
920
+ #PutByte(obj, b) {
921
+ obj._buffer[obj._pos] = b;
922
+ obj._pos += 1;
923
+ if (obj._pos >= obj._windowSize) {
924
+ this.#Flush_0(obj);
925
+ }
926
+ }
927
+ #ReleaseStream(obj) {
928
+ this.#Flush_0(obj);
929
+ obj._stream = null;
930
+ }
931
+ GetLenToPosState(len) {
932
+ len -= 2;
933
+ if (len < 4) {
934
+ return len;
935
+ }
936
+ return 3;
937
+ }
938
+ StateUpdateChar(index) {
939
+ if (index < 4) {
940
+ return 0;
941
+ }
942
+ if (index < 10) {
943
+ return index - 3;
944
+ }
945
+ return index - 6;
946
+ }
947
+ #Chunker_0() {
948
+ const chunker = {
949
+ encoder: this.#encoder,
950
+ decoder: null,
951
+ alive: 1,
952
+ };
953
+ return chunker;
954
+ }
955
+ #processChunkDecode() {
956
+ if (!this.#decompressor.chunker.alive) {
957
+ throw new Error("Bad state");
958
+ }
959
+ if (this.#decompressor.chunker.encoder) {
960
+ throw new Error("No encoding");
961
+ }
962
+ const result = this.#CodeOneChunk();
963
+ if (result === -1) {
964
+ throw new Error("Corrupted input");
965
+ }
966
+ const decoder = this.#decompressor.chunker.decoder;
967
+ this.#decompressor.chunker.inBytesProcessed = this.#N1_LONG_LIT;
968
+ this.#decompressor.chunker.inBytesProcessed = decoder.nowPos64;
969
+ if (result
970
+ || this.#compare(decoder.outSize, this.#P0_LONG_LIT) >= 0
971
+ && this.#compare(decoder.nowPos64, decoder.outSize) >= 0) {
972
+ this.#Flush_0(decoder.m_OutWindow);
973
+ this.#ReleaseStream(decoder.m_OutWindow);
974
+ decoder.m_RangeDecoder.Stream = null;
975
+ this.#decompressor.chunker.alive = 0;
976
+ }
977
+ return this.#decompressor.chunker.alive;
978
+ }
979
+ #processChunkEncode() {
980
+ if (!this.#compressor.chunker.alive) {
981
+ throw new Error("bad state");
982
+ }
983
+ if (!this.#compressor.chunker.encoder) {
984
+ throw new Error("No decoding");
985
+ }
986
+ this.#CodeOneBlock();
987
+ this.#compressor.chunker.inBytesProcessed = this.#compressor.chunker.encoder.processedInSize[0];
988
+ if (this.#compressor.chunker.encoder.finished[0]) {
989
+ this.#ReleaseStreams();
990
+ this.#compressor.chunker.alive = 0;
991
+ }
992
+ return this.#compressor.chunker.alive;
993
+ }
994
+ #CodeInChunks(inStream, outSize) {
995
+ this.#decoder.m_RangeDecoder.Stream = inStream;
996
+ this.#ReleaseStream(this.#decoder.m_OutWindow);
997
+ this.#decoder.m_OutWindow._stream = this.#decompressor.output;
998
+ this.#Init_1();
999
+ this.#decoder.state = 0;
1000
+ this.#decoder.rep0 = 0;
1001
+ this.#decoder.rep1 = 0;
1002
+ this.#decoder.rep2 = 0;
1003
+ this.#decoder.rep3 = 0;
1004
+ this.#decoder.outSize = outSize;
1005
+ this.#decoder.nowPos64 = this.#P0_LONG_LIT;
1006
+ this.#decoder.prevByte = 0;
1007
+ this.#decoder.decoder = this.#decoder;
1008
+ this.#decoder.encoder = null;
1009
+ this.#decoder.alive = 1;
1010
+ return this.#decoder;
1011
+ }
1012
+ #CodeOneChunk() {
1013
+ const decoder = this.#decompressor.chunker.decoder;
1014
+ let decoder2, distance, len, numDirectBits, positionSlot;
1015
+ let posState = this.#lowBits_0(decoder.nowPos64)
1016
+ & decoder.m_PosStateMask;
1017
+ if (!this.#decodeBit(decoder.m_IsMatchDecoders, (decoder.state << 4) + posState)) {
1018
+ decoder2 = this.#GetDecoder(this.#lowBits_0(decoder.nowPos64), decoder.prevByte);
1019
+ if (decoder.state < 7) {
1020
+ decoder.prevByte = this.#DecodeNormal(decoder2);
1021
+ }
1022
+ else {
1023
+ decoder.prevByte = this.#DecodeWithMatchByte(decoder2, this.#GetByte(decoder.rep0));
1024
+ }
1025
+ this.#PutByte(decoder.m_OutWindow, decoder.prevByte);
1026
+ decoder.state = this.StateUpdateChar(decoder.state);
1027
+ decoder.nowPos64 = this.#add(decoder.nowPos64, this.#P1_LONG_LIT);
1028
+ }
1029
+ else {
1030
+ if (this.#decodeBit(decoder.m_IsRepDecoders, decoder.state)) {
1031
+ len = 0;
1032
+ if (!this.#decodeBit(decoder.m_IsRepG0Decoders, decoder.state)) {
1033
+ if (!this.#decodeBit(decoder.m_IsRep0LongDecoders, (decoder.state << 4) + posState)) {
1034
+ decoder.state = decoder.state < 7
1035
+ ? 9
1036
+ : 11;
1037
+ len = 1;
1038
+ }
1039
+ }
1040
+ else {
1041
+ if (!this.#decodeBit(decoder.m_IsRepG1Decoders, decoder.state)) {
1042
+ distance = decoder.rep1;
1043
+ }
1044
+ else {
1045
+ if (!this.#decodeBit(decoder.m_IsRepG2Decoders, decoder.state)) {
1046
+ distance = decoder.rep2;
1047
+ }
1048
+ else {
1049
+ distance = decoder.rep3;
1050
+ decoder.rep3 = decoder.rep2;
1051
+ }
1052
+ decoder.rep2 = decoder.rep1;
1053
+ }
1054
+ decoder.rep1 = decoder.rep0;
1055
+ decoder.rep0 = distance;
1056
+ }
1057
+ if (!len) {
1058
+ len = this.#Decode(decoder.m_RepLenDecoder, posState) + 2;
1059
+ decoder.state = decoder.state < 7 ? 8 : 11;
1060
+ }
1061
+ }
1062
+ else {
1063
+ decoder.rep3 = decoder.rep2;
1064
+ decoder.rep2 = decoder.rep1;
1065
+ decoder.rep1 = decoder.rep0;
1066
+ len = 2 + this.#Decode(decoder.m_LenDecoder, posState);
1067
+ decoder.state = decoder.state < 7 ? 7 : 10;
1068
+ positionSlot = this.#Decode_0(decoder.m_PosSlotDecoder[this.GetLenToPosState(len)]);
1069
+ if (positionSlot >= 4) {
1070
+ numDirectBits = (positionSlot >> 1) - 1;
1071
+ decoder.rep0 = (2 | positionSlot & 1) << numDirectBits;
1072
+ if (positionSlot < 14) {
1073
+ decoder.rep0 += this.reverseDecode(decoder.m_PosDecoders, decoder.rep0 - positionSlot - 1, numDirectBits);
1074
+ }
1075
+ else {
1076
+ decoder.rep0 += this.#DecodeDirectBits(numDirectBits - 4) << 4;
1077
+ decoder.rep0 += this.#ReverseDecode();
1078
+ if (decoder.rep0 < 0) {
1079
+ if (decoder.rep0 == -1) {
1080
+ return 1;
1081
+ }
1082
+ return -1;
1083
+ }
1084
+ }
1085
+ }
1086
+ else {
1087
+ decoder.rep0 = positionSlot;
1088
+ }
1089
+ }
1090
+ if (this.#compare(this.#fromInt(decoder.rep0), decoder.nowPos64) >= 0
1091
+ || decoder.rep0 >= decoder.m_DictionarySizeCheck) {
1092
+ return -1;
1093
+ }
1094
+ this.#CopyBlock(len);
1095
+ decoder.nowPos64 = this.#add(decoder.nowPos64, this.#fromInt(len));
1096
+ decoder.prevByte = this.#GetByte(0);
1097
+ }
1098
+ return 0;
1099
+ }
1100
+ #Init_1() {
1101
+ this.#decoder.m_OutWindow._streamPos = 0;
1102
+ this.#decoder.m_OutWindow._pos = 0;
1103
+ this.InitBitModels(this.#decoder.m_IsMatchDecoders);
1104
+ this.InitBitModels(this.#decoder.m_IsRep0LongDecoders);
1105
+ this.InitBitModels(this.#decoder.m_IsRepDecoders);
1106
+ this.InitBitModels(this.#decoder.m_IsRepG0Decoders);
1107
+ this.InitBitModels(this.#decoder.m_IsRepG1Decoders);
1108
+ this.InitBitModels(this.#decoder.m_IsRepG2Decoders);
1109
+ this.InitBitModels(this.#decoder.m_PosDecoders);
1110
+ this.#Init_0(this.#decoder.m_LiteralDecoder);
1111
+ for (let i = 0; i < 4; ++i) {
1112
+ this.InitBitModels(this.#decoder.m_PosSlotDecoder[i].Models);
1113
+ }
1114
+ this.#Init(this.#decoder.m_LenDecoder);
1115
+ this.#Init(this.#decoder.m_RepLenDecoder);
1116
+ this.InitBitModels(this.#decoder.m_PosAlignDecoder.Models);
1117
+ this.#Init_8();
1118
+ }
1119
+ #SetDecoderProperties(properties) {
1120
+ var dictionarySize, i, lc, lp, pb, remainder, val;
1121
+ if (properties.length < 5) {
1122
+ return 0;
1123
+ }
1124
+ val = properties[0] & 255;
1125
+ lc = val % 9;
1126
+ remainder = ~~(val / 9);
1127
+ lp = remainder % 5;
1128
+ pb = ~~(remainder / 5);
1129
+ dictionarySize = 0;
1130
+ for (i = 0; i < 4; ++i) {
1131
+ dictionarySize += (properties[1 + i] & 255) << i * 8;
1132
+ }
1133
+ // NOTE: If the input is bad, it might call for an insanely large dictionary size, which would crash the script.
1134
+ if (dictionarySize > 99999999 || !this.#SetLcLpPb(lc, lp, pb)) {
1135
+ return 0;
1136
+ }
1137
+ return this.#SetDictionarySize(dictionarySize);
1138
+ }
1139
+ #SetDictionarySize(dictionarySize) {
1140
+ if (dictionarySize < 0) {
1141
+ return 0;
1142
+ }
1143
+ if (this.#decoder.m_DictionarySize != dictionarySize) {
1144
+ this.#decoder.m_DictionarySize = dictionarySize;
1145
+ this.#decoder.m_DictionarySizeCheck = Math.max(this.#decoder.m_DictionarySize, 1);
1146
+ this.#Create_5(this.#decoder.m_OutWindow, Math.max(this.#decoder.m_DictionarySizeCheck, 4096));
1147
+ }
1148
+ return 1;
1149
+ }
1150
+ #SetLcLpPb(lc, lp, pb) {
1151
+ if (lc > 8 || lp > 4 || pb > 4) {
1152
+ return 0;
1153
+ }
1154
+ this.#Create_0(lp, lc);
1155
+ var numPosStates = 1 << pb;
1156
+ this.#Create(this.#decoder.m_LenDecoder, numPosStates);
1157
+ this.#Create(this.#decoder.m_RepLenDecoder, numPosStates);
1158
+ this.#decoder.m_PosStateMask = numPosStates - 1;
1159
+ return 1;
1160
+ }
1161
+ #Create(decoder, numPosStates) {
1162
+ for (; decoder.m_NumPosStates < numPosStates; decoder.m_NumPosStates += 1) {
1163
+ decoder.m_LowCoder[decoder.m_NumPosStates] = this.#createBitTreeDecoder(3);
1164
+ decoder.m_MidCoder[decoder.m_NumPosStates] = this.#createBitTreeDecoder(3);
1165
+ }
1166
+ }
1167
+ #Decode(obj, posState) {
1168
+ if (!this.#decodeBit(obj.m_Choice, 0)) {
1169
+ return this.#Decode_0(obj.m_LowCoder[posState]);
1170
+ }
1171
+ let symbol = 8;
1172
+ if (!this.#decodeBit(obj.m_Choice, 1)) {
1173
+ symbol += this.#Decode_0(obj.m_MidCoder[posState]);
1174
+ }
1175
+ else {
1176
+ symbol += 8 + this.#Decode_0(obj.m_HighCoder);
1177
+ }
1178
+ return symbol;
1179
+ }
1180
+ #createLenDecoder(obj) {
1181
+ obj.m_Choice = this.#initArray(2);
1182
+ obj.m_LowCoder = this.#initArray(16);
1183
+ obj.m_MidCoder = this.#initArray(16);
1184
+ obj.m_HighCoder = this.#createBitTreeDecoder(8);
1185
+ obj.m_NumPosStates = 0;
1186
+ return obj;
1187
+ }
1188
+ #Init(obj) {
1189
+ this.InitBitModels(obj.m_Choice);
1190
+ for (let posState = 0; posState < obj.m_NumPosStates; ++posState) {
1191
+ this.InitBitModels(obj.m_LowCoder[posState].Models);
1192
+ this.InitBitModels(obj.m_MidCoder[posState].Models);
1193
+ }
1194
+ this.InitBitModels(obj.m_HighCoder.Models);
1195
+ }
1196
+ #Create_0(numPosBits, numPrevBits) {
1197
+ let i, numStates;
1198
+ if (this.#decoder.m_LiteralDecoder.m_Coders !== null
1199
+ && this.#decoder.m_LiteralDecoder.m_NumPrevBits == numPrevBits
1200
+ && this.#decoder.m_LiteralDecoder.m_NumPosBits == numPosBits) {
1201
+ return;
1202
+ }
1203
+ this.#decoder.m_LiteralDecoder.m_NumPosBits = numPosBits;
1204
+ this.#decoder.m_LiteralDecoder.m_PosMask = (1 << numPosBits) - 1;
1205
+ this.#decoder.m_LiteralDecoder.m_NumPrevBits = numPrevBits;
1206
+ numStates = 1 << this.#decoder.m_LiteralDecoder.m_NumPrevBits + this.#decoder.m_LiteralDecoder.m_NumPosBits;
1207
+ this.#decoder.m_LiteralDecoder.m_Coders = this.#initArray(numStates);
1208
+ for (i = 0; i < numStates; ++i) {
1209
+ this.#decoder.m_LiteralDecoder.m_Coders[i] = this.#createLiteralDecoderEncoder2({});
1210
+ }
1211
+ }
1212
+ #GetDecoder(pos, prevByte) {
1213
+ const literalDecoder = this.#decompressor.chunker.decoder.m_LiteralDecoder;
1214
+ // Calculate index based on position and previous byte
1215
+ const positionMask = pos & literalDecoder.m_PosMask;
1216
+ const prevBitsMask = (prevByte & 255) >>> (8 - literalDecoder.m_NumPrevBits);
1217
+ const index = (positionMask << literalDecoder.m_NumPrevBits) + prevBitsMask;
1218
+ // Return decoder at calculated index
1219
+ return literalDecoder.m_Coders[index];
1220
+ }
1221
+ #Init_0(obj) {
1222
+ let i, numStates;
1223
+ numStates = 1 << obj.m_NumPrevBits + obj.m_NumPosBits;
1224
+ for (i = 0; i < numStates; ++i) {
1225
+ this.InitBitModels(obj.m_Coders[i].m_Decoders);
1226
+ }
1227
+ }
1228
+ #DecodeNormal(rangeDecoder) {
1229
+ const _rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
1230
+ var symbol = 1;
1231
+ do {
1232
+ symbol = symbol << 1 | this.#decodeBit(rangeDecoder.m_Decoders, symbol);
1233
+ } while (symbol < 256);
1234
+ return symbol << 24 >> 24;
1235
+ }
1236
+ #DecodeWithMatchByte(obj, matchByte) {
1237
+ let bit, matchBit, symbol = 1;
1238
+ do {
1239
+ matchBit = matchByte >> 7 & 1;
1240
+ matchByte <<= 1;
1241
+ bit = this.#decodeBit(obj.m_Decoders, (1 + matchBit << 8) + symbol);
1242
+ symbol = symbol << 1 | bit;
1243
+ if (matchBit != bit) {
1244
+ while (symbol < 256) {
1245
+ symbol = symbol << 1 | this.#decodeBit(obj.m_Decoders, symbol);
1246
+ }
1247
+ break;
1248
+ }
1249
+ } while (symbol < 256);
1250
+ return symbol << 24 >> 24;
1251
+ }
1252
+ #createLiteralDecoderEncoder2(obj) {
1253
+ obj.m_Decoders = this.#initArray(768);
1254
+ return obj;
1255
+ }
1256
+ #Backward(cur) {
1257
+ const obj = this.#compressor.chunker.encoder;
1258
+ let backCur, backMem, posMem, posPrev;
1259
+ obj._optimumEndIndex = cur;
1260
+ posMem = obj._optimum[cur].PosPrev;
1261
+ backMem = obj._optimum[cur].BackPrev;
1262
+ do {
1263
+ if (obj._optimum[cur].Prev1IsChar) {
1264
+ this.#MakeAsChar(obj._optimum[posMem]);
1265
+ obj._optimum[posMem].PosPrev = posMem - 1;
1266
+ if (obj._optimum[cur].Prev2) {
1267
+ obj._optimum[posMem - 1].Prev1IsChar = 0;
1268
+ obj._optimum[posMem - 1].PosPrev = obj._optimum[cur].PosPrev2;
1269
+ obj._optimum[posMem - 1].BackPrev = obj._optimum[cur].BackPrev2;
1270
+ }
1271
+ }
1272
+ posPrev = posMem;
1273
+ backCur = backMem;
1274
+ backMem = obj._optimum[posPrev].BackPrev;
1275
+ posMem = obj._optimum[posPrev].PosPrev;
1276
+ obj._optimum[posPrev].BackPrev = backCur;
1277
+ obj._optimum[posPrev].PosPrev = cur;
1278
+ cur = posPrev;
1279
+ } while (cur > 0);
1280
+ obj.backRes = obj._optimum[0].BackPrev;
1281
+ obj._optimumCurrentIndex = obj._optimum[0].PosPrev;
1282
+ return obj._optimumCurrentIndex;
1283
+ }
1284
+ #BaseInit() {
1285
+ this.#encoder._state = 0;
1286
+ this.#encoder._previousByte = 0;
1287
+ for (let i = 0; i < 4; ++i) {
1288
+ this.#encoder._repDistances[i] = 0;
1289
+ }
1290
+ }
1291
+ #CodeOneBlock() {
1292
+ let baseVal, complexState, curByte, distance, footerBits, i, len, lenToPosState, matchByte, pos, posReduced, posSlot, posState, progressPosValuePrev, subCoder;
1293
+ this.#compressor.chunker.encoder.processedInSize[0] = this.#P0_LONG_LIT;
1294
+ this.#compressor.chunker.encoder.processedOutSize[0] = this.#P0_LONG_LIT;
1295
+ this.#compressor.chunker.encoder.finished[0] = 1;
1296
+ if (this.#compressor.chunker.encoder._inStream) {
1297
+ this.#compressor.chunker.encoder._matchFinder._stream = this.#compressor.chunker.encoder._inStream;
1298
+ this.#Init_5();
1299
+ this.#compressor.chunker.encoder._needReleaseMFStream = 1;
1300
+ this.#compressor.chunker.encoder._inStream = null;
1301
+ }
1302
+ if (this.#compressor.chunker.encoder._finished) {
1303
+ return;
1304
+ }
1305
+ this.#compressor.chunker.encoder._finished = 1;
1306
+ progressPosValuePrev = this.#compressor.chunker.encoder.nowPos64;
1307
+ if (this.#eq(this.#compressor.chunker.encoder.nowPos64, this.#P0_LONG_LIT)) {
1308
+ if (!this.#GetNumAvailableBytes()) {
1309
+ this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
1310
+ return;
1311
+ }
1312
+ this.#ReadMatchDistances();
1313
+ posState = this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64)
1314
+ & this.#compressor.chunker.encoder._posStateMask;
1315
+ this.#Encode_3(this.#compressor.chunker.encoder._isMatch, (this.#compressor.chunker.encoder._state << 4) + posState, 0);
1316
+ this.#compressor.chunker.encoder._state = this.StateUpdateChar(this.#compressor.chunker.encoder._state);
1317
+ curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1318
+ this.#Encode_1(this.#GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
1319
+ this.#compressor.chunker.encoder._previousByte = curByte;
1320
+ this.#compressor.chunker.encoder._additionalOffset -= 1;
1321
+ this.#compressor.chunker.encoder.nowPos64 = this.#add(this.#compressor.chunker.encoder.nowPos64, this.#P1_LONG_LIT);
1322
+ }
1323
+ if (!this.#GetNumAvailableBytes()) {
1324
+ this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
1325
+ return;
1326
+ }
1327
+ while (1) {
1328
+ len = this.#GetOptimum(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
1329
+ pos = this.#compressor.chunker.encoder.backRes;
1330
+ posState = this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64)
1331
+ & this.#compressor.chunker.encoder._posStateMask;
1332
+ complexState = (this.#compressor.chunker.encoder._state << 4) + posState;
1333
+ if (len == 1 && pos == -1) {
1334
+ this.#Encode_3(this.#compressor.chunker.encoder._isMatch, complexState, 0);
1335
+ curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1336
+ subCoder = this.#GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
1337
+ if (this.#compressor.chunker.encoder._state < 7) {
1338
+ this.#Encode_1(subCoder, curByte);
1339
+ }
1340
+ else {
1341
+ matchByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._repDistances[0]
1342
+ - 1
1343
+ - this.#compressor.chunker.encoder._additionalOffset);
1344
+ this.#EncodeMatched(subCoder, matchByte, curByte);
1345
+ }
1346
+ this.#compressor.chunker.encoder._previousByte = curByte;
1347
+ this.#compressor.chunker.encoder._state = this.StateUpdateChar(this.#compressor.chunker.encoder._state);
1348
+ }
1349
+ else {
1350
+ this.#Encode_3(this.#compressor.chunker.encoder._isMatch, complexState, 1);
1351
+ if (pos < 4) {
1352
+ this.#Encode_3(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 1);
1353
+ if (!pos) {
1354
+ this.#Encode_3(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 0);
1355
+ if (len == 1) {
1356
+ this.#Encode_3(this.#compressor.chunker.encoder._isRep0Long, complexState, 0);
1357
+ }
1358
+ else {
1359
+ this.#Encode_3(this.#compressor.chunker.encoder._isRep0Long, complexState, 1);
1360
+ }
1361
+ }
1362
+ else {
1363
+ this.#Encode_3(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 1);
1364
+ if (pos == 1) {
1365
+ this.#Encode_3(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 0);
1366
+ }
1367
+ else {
1368
+ this.#Encode_3(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 1);
1369
+ this.#Encode_3(this.#compressor.chunker.encoder._isRepG2, this.#compressor.chunker.encoder._state, pos - 2);
1370
+ }
1371
+ }
1372
+ if (len == 1) {
1373
+ this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 9 : 11;
1374
+ }
1375
+ else {
1376
+ this.#Encode_0(this.#compressor.chunker.encoder._repMatchLenEncoder, len - 2, posState);
1377
+ this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 8 : 11;
1378
+ }
1379
+ distance = this.#compressor.chunker.encoder._repDistances[pos];
1380
+ if (pos != 0) {
1381
+ for (let i = pos; i >= 1; --i) {
1382
+ this.#compressor.chunker.encoder._repDistances[i] = this
1383
+ .#compressor
1384
+ .chunker
1385
+ .encoder
1386
+ ._repDistances[i - 1];
1387
+ }
1388
+ this.#compressor.chunker.encoder._repDistances[0] = distance;
1389
+ }
1390
+ }
1391
+ else {
1392
+ this.#Encode_3(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 0);
1393
+ this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 7 : 10;
1394
+ this.#Encode_0(this.#compressor.chunker.encoder._lenEncoder, len - 2, posState);
1395
+ pos -= 4;
1396
+ posSlot = this.GetPosSlot(pos);
1397
+ lenToPosState = this.GetLenToPosState(len);
1398
+ this.#Encode_2(this.#compressor.chunker.encoder._posSlotEncoder[lenToPosState], posSlot);
1399
+ if (posSlot >= 4) {
1400
+ footerBits = (posSlot >> 1) - 1;
1401
+ baseVal = (2 | posSlot & 1) << footerBits;
1402
+ posReduced = pos - baseVal;
1403
+ if (posSlot < 14) {
1404
+ this.ReverseEncode(baseVal - posSlot - 1, footerBits, posReduced);
1405
+ }
1406
+ else {
1407
+ this.#EncodeDirectBits(posReduced >> 4, footerBits - 4);
1408
+ this.#ReverseEncode(posReduced & 15);
1409
+ this.#compressor.chunker.encoder._alignPriceCount += 1;
1410
+ }
1411
+ }
1412
+ distance = pos;
1413
+ for (let i = 3; i >= 1; --i) {
1414
+ this.#compressor.chunker.encoder._repDistances[i] = this
1415
+ .#compressor
1416
+ .chunker
1417
+ .encoder
1418
+ ._repDistances[i - 1];
1419
+ }
1420
+ this.#compressor.chunker.encoder._repDistances[0] = distance;
1421
+ this.#compressor.chunker.encoder._matchPriceCount += 1;
1422
+ }
1423
+ this.#compressor.chunker.encoder._previousByte = this.#GetIndexByte(len - 1 - this.#compressor.chunker.encoder._additionalOffset);
1424
+ }
1425
+ this.#compressor.chunker.encoder._additionalOffset -= len;
1426
+ this.#compressor.chunker.encoder.nowPos64 = this.#add(this.#compressor.chunker.encoder.nowPos64, this.#fromInt(len));
1427
+ if (!this.#compressor.chunker.encoder._additionalOffset) {
1428
+ if (this.#compressor.chunker.encoder._matchPriceCount >= 128) {
1429
+ this.#FillDistancesPrices(this.#compressor.chunker.encoder);
1430
+ }
1431
+ if (this.#compressor.chunker.encoder._alignPriceCount >= 16) {
1432
+ this.#FillAlignPrices(this.#compressor.chunker.encoder);
1433
+ }
1434
+ this.#compressor.chunker.encoder.processedInSize[0] = this.#compressor.chunker.encoder.nowPos64;
1435
+ this.#compressor.chunker.encoder.processedOutSize[0] = this.#GetProcessedSizeAdd();
1436
+ if (!this.#GetNumAvailableBytes()) {
1437
+ this.#Flush(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64));
1438
+ return;
1439
+ }
1440
+ if (this.#compare(this.#sub(this.#compressor.chunker.encoder.nowPos64, [4096, 0]), [4096, 0]) >= 0) {
1441
+ this.#compressor.chunker.encoder._finished = 0;
1442
+ this.#compressor.chunker.encoder.finished[0] = 0;
1443
+ return;
1444
+ }
1445
+ }
1446
+ }
1447
+ }
1448
+ #Create_2() {
1449
+ let binTree, numHashBytes;
1450
+ if (!this.#encoder._matchFinder) {
1451
+ binTree = {};
1452
+ numHashBytes = 4;
1453
+ if (!this.#encoder._matchFinderType) {
1454
+ numHashBytes = 2;
1455
+ }
1456
+ this.#SetType(binTree, numHashBytes);
1457
+ this.#encoder._matchFinder = binTree;
1458
+ }
1459
+ this.#Create_1();
1460
+ if (this.#encoder._dictionarySize == this.#encoder._dictionarySizePrev
1461
+ && this.#encoder._numFastBytesPrev == this.#encoder._numFastBytes) {
1462
+ return;
1463
+ }
1464
+ this.#Create_3(4_096, 274);
1465
+ this.#encoder._dictionarySizePrev = this.#encoder._dictionarySize;
1466
+ this.#encoder._numFastBytesPrev = this.#encoder._numFastBytes;
1467
+ }
1468
+ #Encoder() {
1469
+ for (let i = 0; i < 4_096; ++i) {
1470
+ this.#encoder._optimum[i] = {};
1471
+ }
1472
+ for (let i = 0; i < 4; ++i) {
1473
+ this.#encoder._posSlotEncoder[i] = this.#createBitTreeEncoder(6);
1474
+ }
1475
+ }
1476
+ #FillAlignPrices(encoder) {
1477
+ for (let i = 0; i < 16; ++i) {
1478
+ encoder._alignPrices[i] = this.#ReverseGetPrice(encoder._posAlignEncoder, i);
1479
+ }
1480
+ encoder._alignPriceCount = 0;
1481
+ }
1482
+ #FillDistancesPrices(obj) {
1483
+ let baseVal, encoder, footerBits, posSlot, st, st2;
1484
+ for (let i = 4; i < 128; ++i) {
1485
+ posSlot = this.GetPosSlot(i);
1486
+ footerBits = (posSlot >> 1) - 1;
1487
+ baseVal = (2 | posSlot & 1) << footerBits;
1488
+ obj.tempPrices[i] = this.ReverseGetPrice(obj._posEncoders, baseVal - posSlot - 1, footerBits, i - baseVal);
1489
+ }
1490
+ for (let lenToPosState = 0; lenToPosState < 4; ++lenToPosState) {
1491
+ encoder = obj._posSlotEncoder[lenToPosState];
1492
+ st = lenToPosState << 6;
1493
+ for (posSlot = 0; posSlot < obj._distTableSize; posSlot += 1) {
1494
+ obj._posSlotPrices[st + posSlot] = this.#GetPrice_1(encoder, posSlot);
1495
+ }
1496
+ for (posSlot = 14; posSlot < obj._distTableSize; posSlot += 1) {
1497
+ obj._posSlotPrices[st + posSlot] += (posSlot >> 1) - 1 - 4 << 6;
1498
+ }
1499
+ st2 = lenToPosState * 128;
1500
+ for (let i = 0; i < 4; ++i) {
1501
+ obj._distancesPrices[st2 + i] = obj._posSlotPrices[st + i];
1502
+ }
1503
+ for (let i = 4; i < 128; ++i) {
1504
+ obj._distancesPrices[st2 + i] = obj._posSlotPrices[st + this.GetPosSlot(i)] + obj.tempPrices[i];
1505
+ }
1506
+ }
1507
+ obj._matchPriceCount = 0;
1508
+ }
1509
+ #Flush(nowPos) {
1510
+ this.#ReleaseMFStream();
1511
+ this.#WriteEndMarker(nowPos & this.#compressor.chunker.encoder._posStateMask);
1512
+ for (let i = 0; i < 5; ++i) {
1513
+ this.#ShiftLow();
1514
+ }
1515
+ }
1516
+ #GetOptimum(position) {
1517
+ let cur, curAnd1Price, curAndLenCharPrice, curAndLenPrice, curBack, curPrice, currentByte, distance, len, lenEnd, lenMain, lenTest, lenTest2, lenTestTemp, matchByte, matchPrice, newLen, nextIsChar, nextMatchPrice, nextOptimum, nextRepMatchPrice, normalMatchPrice, numAvailableBytes, numAvailableBytesFull, numDistancePairs, offs, offset, opt, optimum, pos, posPrev, posState, posStateNext, price_4, repIndex, repLen, repMatchPrice, repMaxIndex, shortRepPrice, startLen, state, state2, t, price, price_0, price_1, price_2, price_3, lenRes;
1518
+ const encoder = this.#compressor.chunker.encoder;
1519
+ if (encoder._optimumEndIndex != encoder._optimumCurrentIndex) {
1520
+ lenRes = encoder._optimum[encoder._optimumCurrentIndex].PosPrev
1521
+ - encoder._optimumCurrentIndex;
1522
+ encoder.backRes = encoder._optimum[encoder._optimumCurrentIndex].BackPrev;
1523
+ encoder._optimumCurrentIndex = encoder._optimum[encoder._optimumCurrentIndex].PosPrev;
1524
+ return lenRes;
1525
+ }
1526
+ encoder._optimumCurrentIndex = encoder._optimumEndIndex = 0;
1527
+ if (encoder._longestMatchWasFound) {
1528
+ lenMain = encoder._longestMatchLength;
1529
+ encoder._longestMatchWasFound = 0;
1530
+ }
1531
+ else {
1532
+ lenMain = this.#ReadMatchDistances();
1533
+ }
1534
+ numDistancePairs = encoder._numDistancePairs;
1535
+ numAvailableBytes = this.#GetNumAvailableBytes() + 1;
1536
+ if (numAvailableBytes < 2) {
1537
+ encoder.backRes = -1;
1538
+ return 1;
1539
+ }
1540
+ if (numAvailableBytes > 273) {
1541
+ numAvailableBytes = 273;
1542
+ }
1543
+ repMaxIndex = 0;
1544
+ for (let i = 0; i < 4; ++i) {
1545
+ encoder.reps[i] = encoder._repDistances[i];
1546
+ encoder.repLens[i] = this.#GetMatchLen(-1, encoder.reps[i], 273);
1547
+ if (encoder.repLens[i] > encoder.repLens[repMaxIndex]) {
1548
+ repMaxIndex = i;
1549
+ }
1550
+ }
1551
+ if (encoder.repLens[repMaxIndex] >= encoder._numFastBytes) {
1552
+ encoder.backRes = repMaxIndex;
1553
+ lenRes = encoder.repLens[repMaxIndex];
1554
+ this.#MovePos(lenRes - 1);
1555
+ return lenRes;
1556
+ }
1557
+ if (lenMain >= encoder._numFastBytes) {
1558
+ encoder.backRes = this.#compressor.chunker.encoder._matchDistances[numDistancePairs - 1] + 4;
1559
+ this.#MovePos(lenMain - 1);
1560
+ return lenMain;
1561
+ }
1562
+ currentByte = this.#GetIndexByte(-1);
1563
+ matchByte = this.#GetIndexByte(-encoder._repDistances[0] - 1 - 1);
1564
+ if (lenMain < 2 && currentByte != matchByte && encoder.repLens[repMaxIndex] < 2) {
1565
+ encoder.backRes = -1;
1566
+ return 1;
1567
+ }
1568
+ encoder._optimum[0].State = encoder._state;
1569
+ posState = position & encoder._posStateMask;
1570
+ encoder._optimum[1].Price = this.#probPrices[encoder._isMatch[(encoder._state << 4) + posState] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
1571
+ this.#MakeAsChar(encoder._optimum[1]);
1572
+ matchPrice = this.#probPrices[2_048
1573
+ - encoder._isMatch[(encoder._state << 4) + posState]
1574
+ >>> 2];
1575
+ repMatchPrice = matchPrice + this.#probPrices[2_048 - encoder._isRep[encoder._state] >>> 2];
1576
+ if (matchByte == currentByte) {
1577
+ shortRepPrice = repMatchPrice + this.#GetRepLen1Price(posState);
1578
+ if (shortRepPrice < encoder._optimum[1].Price) {
1579
+ encoder._optimum[1].Price = shortRepPrice;
1580
+ this.#MakeAsShortRep(encoder._optimum[1]);
1581
+ }
1582
+ }
1583
+ lenEnd = lenMain >= encoder.repLens[repMaxIndex]
1584
+ ? lenMain
1585
+ : encoder.repLens[repMaxIndex];
1586
+ if (lenEnd < 2) {
1587
+ encoder.backRes = encoder._optimum[1].BackPrev;
1588
+ return 1;
1589
+ }
1590
+ encoder._optimum[1].PosPrev = 0;
1591
+ encoder._optimum[0].Backs0 = encoder.reps[0];
1592
+ encoder._optimum[0].Backs1 = encoder.reps[1];
1593
+ encoder._optimum[0].Backs2 = encoder.reps[2];
1594
+ encoder._optimum[0].Backs3 = encoder.reps[3];
1595
+ len = lenEnd;
1596
+ do {
1597
+ encoder._optimum[len].Price = 268_435_455;
1598
+ len -= 1;
1599
+ } while (len >= 2);
1600
+ for (let i = 0; i < 4; ++i) {
1601
+ repLen = encoder.repLens[i];
1602
+ if (repLen < 2) {
1603
+ continue;
1604
+ }
1605
+ price_4 = repMatchPrice + this.#GetPureRepPrice(i, encoder._state, posState);
1606
+ do {
1607
+ curAndLenPrice = price_4 + this.#GetPrice(encoder._repMatchLenEncoder, repLen - 2, posState);
1608
+ optimum = encoder._optimum[repLen];
1609
+ if (curAndLenPrice < optimum.Price) {
1610
+ optimum.Price = curAndLenPrice;
1611
+ optimum.PosPrev = 0;
1612
+ optimum.BackPrev = i;
1613
+ optimum.Prev1IsChar = 0;
1614
+ }
1615
+ } while ((repLen -= 1) >= 2);
1616
+ }
1617
+ normalMatchPrice = matchPrice
1618
+ + this.#probPrices[encoder._isRep[encoder._state] >>> 2];
1619
+ len = encoder.repLens[0] >= 2 ? encoder.repLens[0] + 1 : 2;
1620
+ if (len <= lenMain) {
1621
+ offs = 0;
1622
+ while (len > encoder._matchDistances[offs]) {
1623
+ offs += 2;
1624
+ }
1625
+ for (;; len += 1) {
1626
+ distance = encoder._matchDistances[offs + 1];
1627
+ curAndLenPrice = normalMatchPrice + this.#GetPosLenPrice(distance, len, posState);
1628
+ optimum = encoder._optimum[len];
1629
+ if (curAndLenPrice < optimum.Price) {
1630
+ optimum.Price = curAndLenPrice;
1631
+ optimum.PosPrev = 0;
1632
+ optimum.BackPrev = distance + 4;
1633
+ optimum.Prev1IsChar = 0;
1634
+ }
1635
+ if (len == encoder._matchDistances[offs]) {
1636
+ offs += 2;
1637
+ if (offs == numDistancePairs) {
1638
+ break;
1639
+ }
1640
+ }
1641
+ }
1642
+ }
1643
+ cur = 0;
1644
+ while (1) {
1645
+ ++cur;
1646
+ if (cur == lenEnd) {
1647
+ return this.#Backward(cur);
1648
+ }
1649
+ newLen = this.#ReadMatchDistances();
1650
+ numDistancePairs = encoder._numDistancePairs;
1651
+ if (newLen >= encoder._numFastBytes) {
1652
+ encoder._longestMatchLength = newLen;
1653
+ encoder._longestMatchWasFound = 1;
1654
+ return this.#Backward(cur);
1655
+ }
1656
+ position += 1;
1657
+ posPrev = encoder._optimum[cur].PosPrev;
1658
+ if (encoder._optimum[cur].Prev1IsChar) {
1659
+ posPrev -= 1;
1660
+ if (encoder._optimum[cur].Prev2) {
1661
+ state = encoder._optimum[encoder._optimum[cur].PosPrev2].State;
1662
+ if (encoder._optimum[cur].BackPrev2 < 4) {
1663
+ state = (state < 7) ? 8 : 11;
1664
+ }
1665
+ else {
1666
+ state = (state < 7) ? 7 : 10;
1667
+ }
1668
+ }
1669
+ else {
1670
+ state = encoder._optimum[posPrev].State;
1671
+ }
1672
+ state = this.StateUpdateChar(state);
1673
+ }
1674
+ else {
1675
+ state = encoder._optimum[posPrev].State;
1676
+ }
1677
+ if (posPrev == cur - 1) {
1678
+ if (!encoder._optimum[cur].BackPrev) {
1679
+ state = state < 7 ? 9 : 11;
1680
+ }
1681
+ else {
1682
+ state = this.StateUpdateChar(state);
1683
+ }
1684
+ }
1685
+ else {
1686
+ if (encoder._optimum[cur].Prev1IsChar
1687
+ && encoder._optimum[cur].Prev2) {
1688
+ posPrev = encoder._optimum[cur].PosPrev2;
1689
+ pos = encoder._optimum[cur].BackPrev2;
1690
+ state = state < 7 ? 8 : 11;
1691
+ }
1692
+ else {
1693
+ pos = encoder._optimum[cur].BackPrev;
1694
+ if (pos < 4) {
1695
+ state = state < 7 ? 8 : 11;
1696
+ }
1697
+ else {
1698
+ state = state < 7 ? 7 : 10;
1699
+ }
1700
+ }
1701
+ opt = encoder._optimum[posPrev];
1702
+ if (pos < 4) {
1703
+ if (!pos) {
1704
+ encoder.reps[0] = opt.Backs0;
1705
+ encoder.reps[1] = opt.Backs1;
1706
+ encoder.reps[2] = opt.Backs2;
1707
+ encoder.reps[3] = opt.Backs3;
1708
+ }
1709
+ else if (pos == 1) {
1710
+ encoder.reps[0] = opt.Backs1;
1711
+ encoder.reps[1] = opt.Backs0;
1712
+ encoder.reps[2] = opt.Backs2;
1713
+ encoder.reps[3] = opt.Backs3;
1714
+ }
1715
+ else if (pos == 2) {
1716
+ encoder.reps[0] = opt.Backs2;
1717
+ encoder.reps[1] = opt.Backs0;
1718
+ encoder.reps[2] = opt.Backs1;
1719
+ encoder.reps[3] = opt.Backs3;
1720
+ }
1721
+ else {
1722
+ encoder.reps[0] = opt.Backs3;
1723
+ encoder.reps[1] = opt.Backs0;
1724
+ encoder.reps[2] = opt.Backs1;
1725
+ encoder.reps[3] = opt.Backs2;
1726
+ }
1727
+ }
1728
+ else {
1729
+ encoder.reps[0] = pos - 4;
1730
+ encoder.reps[1] = opt.Backs0;
1731
+ encoder.reps[2] = opt.Backs1;
1732
+ encoder.reps[3] = opt.Backs2;
1733
+ }
1734
+ }
1735
+ encoder._optimum[cur].State = state;
1736
+ encoder._optimum[cur].Backs0 = encoder.reps[0];
1737
+ encoder._optimum[cur].Backs1 = encoder.reps[1];
1738
+ encoder._optimum[cur].Backs2 = encoder.reps[2];
1739
+ encoder._optimum[cur].Backs3 = encoder.reps[3];
1740
+ curPrice = encoder._optimum[cur].Price;
1741
+ currentByte = this.#GetIndexByte(-1);
1742
+ matchByte = this.#GetIndexByte(-encoder.reps[0] - 1 - 1);
1743
+ posState = position & encoder._posStateMask;
1744
+ curAnd1Price = curPrice
1745
+ + this.#probPrices[encoder._isMatch[(state << 4) + posState] >>> 2]
1746
+ + this.#GetPrice_0(this.#GetSubCoder(position, this.#GetIndexByte(-2)), state >= 7, matchByte, currentByte);
1747
+ nextOptimum = encoder._optimum[cur + 1];
1748
+ nextIsChar = 0;
1749
+ if (curAnd1Price < nextOptimum.Price) {
1750
+ nextOptimum.Price = curAnd1Price;
1751
+ nextOptimum.PosPrev = cur;
1752
+ nextOptimum.BackPrev = -1;
1753
+ nextOptimum.Prev1IsChar = 0;
1754
+ nextIsChar = 1;
1755
+ }
1756
+ matchPrice = curPrice + this.#probPrices[2_048 - encoder._isMatch[(state << 4) + posState] >>> 2];
1757
+ repMatchPrice = matchPrice + this.#probPrices[2_048 - encoder._isRep[state] >>> 2];
1758
+ if (matchByte == currentByte && !(nextOptimum.PosPrev < cur && !nextOptimum.BackPrev)) {
1759
+ shortRepPrice = repMatchPrice
1760
+ + (this.#probPrices[encoder._isRepG0[state] >>> 2] + this.#probPrices[encoder._isRep0Long[(state << 4) + posState] >>> 2]);
1761
+ if (shortRepPrice <= nextOptimum.Price) {
1762
+ nextOptimum.Price = shortRepPrice;
1763
+ nextOptimum.PosPrev = cur;
1764
+ nextOptimum.BackPrev = 0;
1765
+ nextOptimum.Prev1IsChar = 0;
1766
+ nextIsChar = 1;
1767
+ }
1768
+ }
1769
+ numAvailableBytesFull = this.#GetNumAvailableBytes() + 1;
1770
+ numAvailableBytesFull = 4_095 - cur < numAvailableBytesFull
1771
+ ? 4_095 - cur
1772
+ : numAvailableBytesFull;
1773
+ numAvailableBytes = numAvailableBytesFull;
1774
+ if (numAvailableBytes < 2) {
1775
+ continue;
1776
+ }
1777
+ if (numAvailableBytes > encoder._numFastBytes) {
1778
+ numAvailableBytes = encoder._numFastBytes;
1779
+ }
1780
+ if (!nextIsChar && matchByte != currentByte) {
1781
+ t = Math.min(numAvailableBytesFull - 1, encoder._numFastBytes);
1782
+ lenTest2 = this.#GetMatchLen(0, encoder.reps[0], t);
1783
+ if (lenTest2 >= 2) {
1784
+ state2 = this.StateUpdateChar(state);
1785
+ posStateNext = position + 1 & encoder._posStateMask;
1786
+ nextRepMatchPrice = curAnd1Price + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1787
+ offset = cur + 1 + lenTest2;
1788
+ while (lenEnd < offset) {
1789
+ encoder._optimum[lenEnd += 1].Price = 268_435_455;
1790
+ }
1791
+ curAndLenPrice = nextRepMatchPrice + (price = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1792
+ price + this.#GetPureRepPrice(0, state2, posStateNext));
1793
+ optimum = encoder._optimum[offset];
1794
+ if (curAndLenPrice < optimum.Price) {
1795
+ optimum.Price = curAndLenPrice;
1796
+ optimum.PosPrev = cur + 1;
1797
+ optimum.BackPrev = 0;
1798
+ optimum.Prev1IsChar = 1;
1799
+ optimum.Prev2 = 0;
1800
+ }
1801
+ }
1802
+ }
1803
+ startLen = 2;
1804
+ for (repIndex = 0; repIndex < 4; ++repIndex) {
1805
+ lenTest = this.#GetMatchLen(-1, encoder.reps[repIndex], numAvailableBytes);
1806
+ if (lenTest < 2) {
1807
+ continue;
1808
+ }
1809
+ lenTestTemp = lenTest;
1810
+ do {
1811
+ while (lenEnd < cur + lenTest) {
1812
+ encoder._optimum[lenEnd += 1].Price = 268_435_455;
1813
+ }
1814
+ curAndLenPrice = repMatchPrice + (price_0 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1815
+ price_0 + this.#GetPureRepPrice(repIndex, state, posState));
1816
+ optimum = encoder._optimum[cur + lenTest];
1817
+ if (curAndLenPrice < optimum.Price) {
1818
+ optimum.Price = curAndLenPrice;
1819
+ optimum.PosPrev = cur;
1820
+ optimum.BackPrev = repIndex;
1821
+ optimum.Prev1IsChar = 0;
1822
+ }
1823
+ } while ((lenTest -= 1) >= 2);
1824
+ lenTest = lenTestTemp;
1825
+ if (!repIndex) {
1826
+ startLen = lenTest + 1;
1827
+ }
1828
+ if (lenTest < numAvailableBytesFull) {
1829
+ t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1830
+ lenTest2 = this.#GetMatchLen(lenTest, encoder.reps[repIndex], t);
1831
+ if (lenTest2 >= 2) {
1832
+ state2 = state < 7 ? 8 : 11;
1833
+ posStateNext = position + lenTest & encoder._posStateMask;
1834
+ curAndLenCharPrice = repMatchPrice + (price_1 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1835
+ price_1 + this.#GetPureRepPrice(repIndex, state, posState))
1836
+ + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#GetIndexByte(lenTest - 1));
1837
+ state2 = this.StateUpdateChar(state2);
1838
+ posStateNext = position + lenTest + 1 & encoder._posStateMask;
1839
+ nextMatchPrice = curAndLenCharPrice + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext] >>> 2];
1840
+ nextRepMatchPrice = nextMatchPrice + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1841
+ offset = cur + 1 + lenTest + lenTest2;
1842
+ while (lenEnd < cur + offset) {
1843
+ encoder._optimum[lenEnd += 1].Price = 268_435_455;
1844
+ }
1845
+ curAndLenPrice = nextRepMatchPrice + (price_2 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1846
+ price_2 + this.#GetPureRepPrice(0, state2, posStateNext));
1847
+ optimum = encoder._optimum[cur + offset];
1848
+ if (curAndLenPrice < optimum.Price) {
1849
+ optimum.Price = curAndLenPrice;
1850
+ optimum.PosPrev = cur + lenTest + 1;
1851
+ optimum.BackPrev = 0;
1852
+ optimum.Prev1IsChar = 1;
1853
+ optimum.Prev2 = 1;
1854
+ optimum.PosPrev2 = cur;
1855
+ optimum.BackPrev2 = repIndex;
1856
+ }
1857
+ }
1858
+ }
1859
+ }
1860
+ if (newLen > numAvailableBytes) {
1861
+ newLen = numAvailableBytes;
1862
+ for (numDistancePairs = 0; newLen > encoder._matchDistances[numDistancePairs]; numDistancePairs += 2) { }
1863
+ encoder._matchDistances[numDistancePairs] = newLen;
1864
+ numDistancePairs += 2;
1865
+ }
1866
+ if (newLen >= startLen) {
1867
+ normalMatchPrice = matchPrice + this.#probPrices[encoder._isRep[state] >>> 2];
1868
+ while (lenEnd < cur + newLen) {
1869
+ encoder._optimum[lenEnd += 1].Price = 268_435_455;
1870
+ }
1871
+ offs = 0;
1872
+ while (startLen > encoder._matchDistances[offs]) {
1873
+ offs += 2;
1874
+ }
1875
+ for (lenTest = startLen;; lenTest += 1) {
1876
+ curBack = encoder._matchDistances[offs + 1];
1877
+ curAndLenPrice = normalMatchPrice + this.#GetPosLenPrice(curBack, lenTest, posState);
1878
+ optimum = encoder._optimum[cur + lenTest];
1879
+ if (curAndLenPrice < optimum.Price) {
1880
+ optimum.Price = curAndLenPrice;
1881
+ optimum.PosPrev = cur;
1882
+ optimum.BackPrev = curBack + 4;
1883
+ optimum.Prev1IsChar = 0;
1884
+ }
1885
+ if (lenTest == encoder._matchDistances[offs]) {
1886
+ if (lenTest < numAvailableBytesFull) {
1887
+ t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1888
+ lenTest2 = this.#GetMatchLen(lenTest, curBack, t);
1889
+ if (lenTest2 >= 2) {
1890
+ state2 = state < 7 ? 7 : 10;
1891
+ posStateNext = position + lenTest & encoder._posStateMask;
1892
+ curAndLenCharPrice = curAndLenPrice + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - (curBack + 1) - 1), this.#GetIndexByte(lenTest - 1));
1893
+ state2 = this.StateUpdateChar(state2);
1894
+ posStateNext = position + lenTest + 1 & encoder._posStateMask;
1895
+ nextMatchPrice = curAndLenCharPrice + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext]
1896
+ >>> 2];
1897
+ nextRepMatchPrice = nextMatchPrice + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1898
+ offset = lenTest + 1 + lenTest2;
1899
+ while (lenEnd < cur + offset) {
1900
+ encoder._optimum[lenEnd += 1].Price = 268_435_455;
1901
+ }
1902
+ curAndLenPrice = nextRepMatchPrice + (price_3 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1903
+ price_3 + this.#GetPureRepPrice(0, state2, posStateNext));
1904
+ optimum = encoder._optimum[cur + offset];
1905
+ if (curAndLenPrice < optimum.Price) {
1906
+ optimum.Price = curAndLenPrice;
1907
+ optimum.PosPrev = cur + lenTest + 1;
1908
+ optimum.BackPrev = 0;
1909
+ optimum.Prev1IsChar = 1;
1910
+ optimum.Prev2 = 1;
1911
+ optimum.PosPrev2 = cur;
1912
+ optimum.BackPrev2 = curBack + 4;
1913
+ }
1914
+ }
1915
+ }
1916
+ offs += 2;
1917
+ if (offs == numDistancePairs) {
1918
+ break;
1919
+ }
1920
+ }
1921
+ }
1922
+ }
1923
+ }
1924
+ }
1925
+ #GetPosLenPrice(pos, len, posState) {
1926
+ let price, lenToPosState = this.GetLenToPosState(len);
1927
+ if (pos < 128) {
1928
+ price = this.#compressor.chunker.encoder._distancesPrices[lenToPosState * 128 + pos];
1929
+ }
1930
+ else {
1931
+ price = this.#compressor.chunker.encoder._posSlotPrices[(lenToPosState << 6) + this.GetPosSlot2(pos)]
1932
+ + this.#compressor.chunker.encoder._alignPrices[pos & 15];
1933
+ }
1934
+ return price + this.#GetPrice(this.#compressor.chunker.encoder._lenEncoder, len - 2, posState);
1935
+ }
1936
+ #GetPureRepPrice(repIndex, state, posState) {
1937
+ var price;
1938
+ if (!repIndex) {
1939
+ price = this.#probPrices[this.#compressor.chunker.encoder._isRepG0[state] >>> 2];
1940
+ price += this.#probPrices[2_048 - this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
1941
+ }
1942
+ else {
1943
+ price = this.#probPrices[2_048 - this.#compressor.chunker.encoder._isRepG0[state] >>> 2];
1944
+ if (repIndex == 1) {
1945
+ price += this.#probPrices[this.#compressor.chunker.encoder._isRepG1[state] >>> 2];
1946
+ }
1947
+ else {
1948
+ price += this.#probPrices[2_048 - this.#compressor.chunker.encoder._isRepG1[state] >>> 2];
1949
+ price += this.GetPrice(this.#compressor.chunker.encoder._isRepG2[state], repIndex - 2);
1950
+ }
1951
+ }
1952
+ return price;
1953
+ }
1954
+ #GetRepLen1Price(posState) {
1955
+ const state = this.#compressor.chunker.encoder._state;
1956
+ return this.#probPrices[this.#compressor.chunker.encoder._isRepG0[state] >>> 2]
1957
+ + this.#probPrices[this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
1958
+ }
1959
+ #Init_4() {
1960
+ this.#BaseInit();
1961
+ this.#Init_9();
1962
+ this.InitBitModels(this.#encoder._isMatch);
1963
+ this.InitBitModels(this.#encoder._isRep0Long);
1964
+ this.InitBitModels(this.#encoder._isRep);
1965
+ this.InitBitModels(this.#encoder._isRepG0);
1966
+ this.InitBitModels(this.#encoder._isRepG1);
1967
+ this.InitBitModels(this.#encoder._isRepG2);
1968
+ this.InitBitModels(this.#encoder._posEncoders);
1969
+ this.#Init_3();
1970
+ for (let i = 0; i < 4; ++i) {
1971
+ this.InitBitModels(this.#encoder._posSlotEncoder[i].Models);
1972
+ }
1973
+ this.#Init_2(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
1974
+ this.#Init_2(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
1975
+ this.InitBitModels(this.#encoder._posAlignEncoder.Models);
1976
+ this.#encoder._longestMatchWasFound = 0;
1977
+ this.#encoder._optimumEndIndex = 0;
1978
+ this.#encoder._optimumCurrentIndex = 0;
1979
+ this.#encoder._additionalOffset = 0;
1980
+ }
1981
+ #MovePos(num) {
1982
+ if (num > 0) {
1983
+ this.#Skip(num);
1984
+ this.#compressor.chunker.encoder._additionalOffset += num;
1985
+ }
1986
+ }
1987
+ #ReadMatchDistances() {
1988
+ var lenRes = 0;
1989
+ this.#compressor.chunker.encoder._numDistancePairs = this.#GetMatches();
1990
+ if (this.#compressor.chunker.encoder._numDistancePairs > 0) {
1991
+ lenRes = this
1992
+ .#compressor
1993
+ .chunker
1994
+ .encoder
1995
+ ._matchDistances[this.#compressor.chunker.encoder._numDistancePairs - 2];
1996
+ if (lenRes == this.#compressor.chunker.encoder._numFastBytes) {
1997
+ lenRes += this.#GetMatchLen(lenRes - 1, this
1998
+ .#compressor
1999
+ .chunker
2000
+ .encoder
2001
+ ._matchDistances[this.#compressor.chunker.encoder._numDistancePairs - 1], 273 - lenRes);
2002
+ }
2003
+ }
2004
+ this.#compressor.chunker.encoder._additionalOffset += 1;
2005
+ return lenRes;
2006
+ }
2007
+ #ReleaseMFStream() {
2008
+ if (this.#compressor.chunker.encoder._matchFinder && this.#compressor.chunker.encoder._needReleaseMFStream) {
2009
+ this.#compressor.chunker.encoder._matchFinder._stream = null;
2010
+ this.#compressor.chunker.encoder._needReleaseMFStream = 0;
2011
+ }
2012
+ }
2013
+ #ReleaseStreams() {
2014
+ this.#ReleaseMFStream();
2015
+ this.#compressor.chunker.encoder._rangeEncoder.Stream = null;
2016
+ }
2017
+ #SetDictionarySize_0(dictionarySize) {
2018
+ this.#encoder._dictionarySize = dictionarySize;
2019
+ for (var dicLogSize = 0; dictionarySize > (1 << dicLogSize); ++dicLogSize)
2020
+ ;
2021
+ this.#encoder._distTableSize = dicLogSize * 2;
2022
+ }
2023
+ #SetMatchFinder(matchFinderIndex) {
2024
+ const matchFinderIndexPrev = this.#encoder._matchFinderType;
2025
+ this.#encoder._matchFinderType = matchFinderIndex;
2026
+ if (this.#encoder._matchFinder && matchFinderIndexPrev != this.#encoder._matchFinderType) {
2027
+ this.#encoder._dictionarySizePrev = -1;
2028
+ this.#encoder._matchFinder = null;
2029
+ }
2030
+ }
2031
+ writeHeaderProperties() {
2032
+ const HEADER_SIZE = 5; // Total header size in bytes
2033
+ // First byte combines posStateBits, literalPosStateBits and literalContextBits
2034
+ // Format: (posStateBits * 5 + literalPosStateBits) * 9 + literalContextBits
2035
+ this.#encoder.properties[0] = ((this.#encoder._posStateBits * 5 + this.#encoder._numLiteralPosStateBits) * 9
2036
+ + this.#encoder._numLiteralContextBits) & 0xFF; // Ensure byte-sized value
2037
+ // Next 4 bytes store dictionary size in little-endian format
2038
+ for (let byteIndex = 0; byteIndex < 4; byteIndex++) {
2039
+ // Shift dictionary size right by appropriate number of bits and mask to byte
2040
+ this.#encoder.properties[1 + byteIndex] = (this.#encoder._dictionarySize >> (8 * byteIndex)) & 0xFF;
2041
+ }
2042
+ // Write the 5-byte header to output
2043
+ this.#write_0(this.#compressor.output, this.#encoder.properties, 0, // Starting from index 0
2044
+ HEADER_SIZE);
2045
+ }
2046
+ #WriteEndMarker(positionState) {
2047
+ const encoder = this.#compressor.chunker.encoder;
2048
+ this.#Encode_3(encoder._isMatch, (encoder._state << 4) + positionState, 1);
2049
+ this.#Encode_3(encoder._isRep, encoder._state, 0);
2050
+ encoder._state = encoder._state < 7 ? 7 : 10;
2051
+ this.#Encode_0(encoder._lenEncoder, 0, positionState);
2052
+ var lenToPosState = this.GetLenToPosState(2);
2053
+ this.#Encode_2(encoder._posSlotEncoder[lenToPosState], 63);
2054
+ this.#EncodeDirectBits(67108863, 26);
2055
+ this.#ReverseEncode(15);
2056
+ }
2057
+ GetPosSlot(pos) {
2058
+ if (pos < 2_048) {
2059
+ return this.#gFastPos[pos];
2060
+ }
2061
+ if (pos < 2097152) {
2062
+ return this.#gFastPos[pos >> 10] + 20;
2063
+ }
2064
+ return this.#gFastPos[pos >> 20] + 40;
2065
+ }
2066
+ GetPosSlot2(pos) {
2067
+ if (pos < 131072) {
2068
+ return this.#gFastPos[pos >> 6] + 12;
2069
+ }
2070
+ if (pos < 134217728) {
2071
+ return this.#gFastPos[pos >> 16] + 32;
2072
+ }
2073
+ return this.#gFastPos[pos >> 26] + 52;
2074
+ }
2075
+ #Encode(obj, symbol, posState) {
2076
+ if (symbol < 8) {
2077
+ this.#Encode_3(obj._choice, 0, 0);
2078
+ this.#Encode_2(obj._lowCoder[posState], symbol);
2079
+ }
2080
+ else {
2081
+ symbol -= 8;
2082
+ this.#Encode_3(obj._choice, 0, 1);
2083
+ if (symbol < 8) {
2084
+ this.#Encode_3(obj._choice, 1, 0);
2085
+ this.#Encode_2(obj._midCoder[posState], symbol);
2086
+ }
2087
+ else {
2088
+ this.#Encode_3(obj._choice, 1, 1);
2089
+ this.#Encode_2(obj._highCoder, symbol - 8);
2090
+ }
2091
+ }
2092
+ }
2093
+ #createLenEncoder() {
2094
+ const encoder = {};
2095
+ encoder._choice = this.#initArray(2);
2096
+ encoder._lowCoder = this.#initArray(16);
2097
+ encoder._midCoder = this.#initArray(16);
2098
+ encoder._highCoder = this.#createBitTreeEncoder(8);
2099
+ for (let posState = 0; posState < 16; ++posState) {
2100
+ encoder._lowCoder[posState] = this.#createBitTreeEncoder(3);
2101
+ encoder._midCoder[posState] = this.#createBitTreeEncoder(3);
2102
+ }
2103
+ return encoder;
2104
+ }
2105
+ #Init_2(obj, numPosStates) {
2106
+ this.InitBitModels(obj._choice);
2107
+ for (let posState = 0; posState < numPosStates; ++posState) {
2108
+ this.InitBitModels(obj._lowCoder[posState].Models);
2109
+ this.InitBitModels(obj._midCoder[posState].Models);
2110
+ }
2111
+ this.InitBitModels(obj._highCoder.Models);
2112
+ }
2113
+ #SetPrices(obj, posState, numSymbols, prices, st) {
2114
+ let a0, a1, b0, b1, i;
2115
+ a0 = this.#probPrices[obj._choice[0] >>> 2];
2116
+ a1 = this.#probPrices[2_048 - obj._choice[0] >>> 2];
2117
+ b0 = a1 + this.#probPrices[obj._choice[1] >>> 2];
2118
+ b1 = a1 + this.#probPrices[2_048 - obj._choice[1] >>> 2];
2119
+ i = 0;
2120
+ for (i = 0; i < 8; ++i) {
2121
+ if (i >= numSymbols) {
2122
+ return;
2123
+ }
2124
+ prices[st + i] = a0 + this.#GetPrice_1(obj._lowCoder[posState], i);
2125
+ }
2126
+ for (; i < 16; ++i) {
2127
+ if (i >= numSymbols) {
2128
+ return;
2129
+ }
2130
+ prices[st + i] = b0 + this.#GetPrice_1(obj._midCoder[posState], i - 8);
2131
+ }
2132
+ for (; i < numSymbols; ++i) {
2133
+ prices[st + i] = b1 + this.#GetPrice_1(obj._highCoder, i - 8 - 8);
2134
+ }
2135
+ }
2136
+ #Encode_0(obj, symbol, posState) {
2137
+ this.#Encode(obj, symbol, posState);
2138
+ if ((obj._counters[posState] -= 1) == 0) {
2139
+ this.#SetPrices(obj, posState, obj._tableSize, obj._prices, posState * 272);
2140
+ obj._counters[posState] = obj._tableSize;
2141
+ }
2142
+ }
2143
+ #createLenPriceTableEncoder() {
2144
+ const encoder = this.#createLenEncoder();
2145
+ encoder._prices = [];
2146
+ encoder._counters = [];
2147
+ return encoder;
2148
+ }
2149
+ #GetPrice(obj, symbol, posState) {
2150
+ return obj._prices[posState * 272 + symbol];
2151
+ }
2152
+ #UpdateTables(obj, numPosStates) {
2153
+ for (let posState = 0; posState < numPosStates; ++posState) {
2154
+ this.#SetPrices(obj, posState, obj._tableSize, obj._prices, posState * 272);
2155
+ obj._counters[posState] = obj._tableSize;
2156
+ }
2157
+ }
2158
+ #Create_1() {
2159
+ let i, numStates;
2160
+ if (this.#encoder._literalEncoder.m_Coders != null
2161
+ && this.#encoder._literalEncoder.m_NumPrevBits == this.#encoder._numLiteralContextBits
2162
+ && this.#encoder._literalEncoder.m_NumPosBits == this.#encoder._numLiteralPosStateBits) {
2163
+ return;
2164
+ }
2165
+ this.#encoder._literalEncoder.m_NumPosBits = this.#encoder._numLiteralPosStateBits;
2166
+ this.#encoder._literalEncoder.m_PosMask = (1 << this.#encoder._numLiteralPosStateBits) - 1;
2167
+ this.#encoder._literalEncoder.m_NumPrevBits = this.#encoder._numLiteralContextBits;
2168
+ numStates = 1 << this.#encoder._literalEncoder.m_NumPrevBits + this.#encoder._literalEncoder.m_NumPosBits;
2169
+ this.#encoder._literalEncoder.m_Coders = this.#initArray(numStates);
2170
+ for (i = 0; i < numStates; ++i) {
2171
+ this.#encoder._literalEncoder.m_Coders[i] = this.#createLiteralEncoderEncoder2({});
2172
+ }
2173
+ }
2174
+ #GetSubCoder(pos, prevByte) {
2175
+ const literalEncoder = this.#compressor.chunker.encoder._literalEncoder;
2176
+ // Calculate position mask bits
2177
+ const posBits = pos & literalEncoder.m_PosMask;
2178
+ const posShifted = posBits << literalEncoder.m_NumPrevBits;
2179
+ // Calculate previous byte bits
2180
+ const prevByteShift = 8 - literalEncoder.m_NumPrevBits;
2181
+ const prevByteBits = (prevByte & 255) >>> prevByteShift;
2182
+ // Combine position and prevByte bits to get final index
2183
+ const coderIndex = posShifted + prevByteBits;
2184
+ return literalEncoder.m_Coders[coderIndex];
2185
+ }
2186
+ #Init_3() {
2187
+ const totalStates = 1
2188
+ << this.#encoder._literalEncoder.m_NumPrevBits + this.#encoder._literalEncoder.m_NumPosBits;
2189
+ for (let i = 0; i < totalStates; ++i) {
2190
+ this.InitBitModels(this.#encoder._literalEncoder.m_Coders[i].m_Encoders);
2191
+ }
2192
+ }
2193
+ #Encode_1(obj, symbol) {
2194
+ var bit, context = 1;
2195
+ for (let i = 7; i >= 0; --i) {
2196
+ bit = symbol >> i & 1;
2197
+ this.#Encode_3(obj.m_Encoders, context, bit);
2198
+ context = context << 1 | bit;
2199
+ }
2200
+ }
2201
+ #EncodeMatched(obj, matchByte, symbol) {
2202
+ let bit, matchBit, state, same = true, context = 1;
2203
+ for (let i = 7; i >= 0; --i) {
2204
+ bit = symbol >> i & 1;
2205
+ state = context;
2206
+ if (same) {
2207
+ matchBit = matchByte >> i & 1;
2208
+ state += 1 + matchBit << 8;
2209
+ same = matchBit === bit;
2210
+ }
2211
+ this.#Encode_3(obj.m_Encoders, state, bit);
2212
+ context = context << 1 | bit;
2213
+ }
2214
+ }
2215
+ #createLiteralEncoderEncoder2(obj) {
2216
+ obj.m_Encoders = this.#initArray(768);
2217
+ return obj;
2218
+ }
2219
+ #GetPrice_0(obj, matchMode, matchByte, symbol) {
2220
+ let bit, context = 1, i = 7, matchBit, price = 0;
2221
+ if (matchMode) {
2222
+ for (; i >= 0; --i) {
2223
+ matchBit = matchByte >> i & 1;
2224
+ bit = symbol >> i & 1;
2225
+ price += this.GetPrice(obj.m_Encoders[(1 + matchBit << 8) + context], bit);
2226
+ context = context << 1 | bit;
2227
+ if (matchBit != bit) {
2228
+ --i;
2229
+ break;
2230
+ }
2231
+ }
2232
+ }
2233
+ for (; i >= 0; --i) {
2234
+ bit = symbol >> i & 1;
2235
+ price += this.GetPrice(obj.m_Encoders[context], bit);
2236
+ context = context << 1 | bit;
2237
+ }
2238
+ return price;
2239
+ }
2240
+ #MakeAsChar(obj) {
2241
+ obj.BackPrev = -1;
2242
+ obj.Prev1IsChar = 0;
2243
+ }
2244
+ #MakeAsShortRep(obj) {
2245
+ obj.BackPrev = 0;
2246
+ obj.Prev1IsChar = 0;
2247
+ }
2248
+ #createBitTreeDecoder(numBitLevels) {
2249
+ return {
2250
+ NumBitLevels: numBitLevels,
2251
+ Models: this.#initArray(1 << numBitLevels),
2252
+ };
2253
+ }
2254
+ // BitTreeDecoder.Decoder
2255
+ #Decode_0(rangeDecoder) {
2256
+ const _rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
2257
+ let bitIndex, m = 1;
2258
+ for (bitIndex = rangeDecoder.NumBitLevels; bitIndex != 0; bitIndex -= 1) {
2259
+ m = (m << 1) + this.#decodeBit(rangeDecoder.Models, m);
2260
+ }
2261
+ return m - (1 << rangeDecoder.NumBitLevels);
2262
+ }
2263
+ #ReverseDecode() {
2264
+ const positionAlignmentDecoder = this.#decompressor.chunker.decoder.m_PosAlignDecoder;
2265
+ let symbol = 0;
2266
+ for (let m = 1, bitIndex = 0, bit; bitIndex < positionAlignmentDecoder.NumBitLevels; ++bitIndex) {
2267
+ bit = this.#decodeBit(positionAlignmentDecoder.Models, m);
2268
+ m <<= 1;
2269
+ m += bit;
2270
+ symbol |= bit << bitIndex;
2271
+ }
2272
+ return symbol;
2273
+ }
2274
+ reverseDecode(Models, startIndex, NumBitLevels) {
2275
+ const rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
2276
+ let symbol = 0;
2277
+ for (let bitIndex = 0, m = 1, bit; bitIndex < NumBitLevels; ++bitIndex) {
2278
+ bit = this.#decodeBit(Models, startIndex + m);
2279
+ m <<= 1;
2280
+ m += bit;
2281
+ symbol |= bit << bitIndex;
2282
+ }
2283
+ return symbol;
2284
+ }
2285
+ #createBitTreeEncoder(numBitLevels) {
2286
+ return {
2287
+ NumBitLevels: numBitLevels,
2288
+ Models: this.#initArray(1 << numBitLevels),
2289
+ };
2290
+ }
2291
+ #Encode_2(obj, symbol) {
2292
+ var bit, bitIndex, m = 1;
2293
+ for (bitIndex = obj.NumBitLevels; bitIndex != 0;) {
2294
+ bitIndex -= 1;
2295
+ bit = symbol >>> bitIndex & 1;
2296
+ this.#Encode_3(obj.Models, m, bit);
2297
+ m = m << 1 | bit;
2298
+ }
2299
+ }
2300
+ #GetPrice_1(obj, symbol) {
2301
+ var bit, bitIndex, m = 1, price = 0;
2302
+ for (bitIndex = obj.NumBitLevels; bitIndex != 0;) {
2303
+ bitIndex -= 1;
2304
+ bit = symbol >>> bitIndex & 1;
2305
+ price += this.GetPrice(obj.Models[m], bit);
2306
+ m = (m << 1) + bit;
2307
+ }
2308
+ return price;
2309
+ }
2310
+ #ReverseEncode(symbol) {
2311
+ const posAlignEncoder = this.#compressor.chunker.encoder._posAlignEncoder;
2312
+ var bit, m = 1;
2313
+ for (let i = 0; i < posAlignEncoder.NumBitLevels; ++i) {
2314
+ bit = symbol & 1;
2315
+ this.#Encode_3(posAlignEncoder.Models, m, bit);
2316
+ m = m << 1 | bit;
2317
+ symbol >>= 1;
2318
+ }
2319
+ }
2320
+ ReverseEncode(startIndex, NumBitLevels, symbol) {
2321
+ let bit, m = 1;
2322
+ for (let i = 0; i < NumBitLevels; ++i) {
2323
+ bit = symbol & 1;
2324
+ this.#Encode_3(this.#compressor.chunker.encoder._posEncoders, startIndex + m, bit);
2325
+ m = m << 1 | bit;
2326
+ symbol >>= 1;
2327
+ }
2328
+ }
2329
+ #ReverseGetPrice(obj, symbol) {
2330
+ let bit, m = 1, price = 0;
2331
+ for (let i = obj.NumBitLevels; i != 0; i -= 1) {
2332
+ bit = symbol & 1;
2333
+ symbol >>>= 1;
2334
+ price += this.GetPrice(obj.Models[m], bit);
2335
+ m = m << 1 | bit;
2336
+ }
2337
+ return price;
2338
+ }
2339
+ ReverseGetPrice(Models, startIndex, NumBitLevels, symbol) {
2340
+ var bit, m = 1, price = 0;
2341
+ for (let i = NumBitLevels; i != 0; i -= 1) {
2342
+ bit = symbol & 1;
2343
+ symbol >>>= 1;
2344
+ price += this.#probPrices[((Models[startIndex + m] - bit ^ -bit) & 2047) >>> 2];
2345
+ m = m << 1 | bit;
2346
+ }
2347
+ return price;
2348
+ }
2349
+ #decodeBit(probs, index) {
2350
+ const rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
2351
+ let newBound, prob = probs[index];
2352
+ newBound = (rangeDecoder.Range >>> 11) * prob;
2353
+ if ((rangeDecoder.Code ^ this.#MIN_INT32) < (newBound ^ this.#MIN_INT32)) {
2354
+ rangeDecoder.Range = newBound;
2355
+ probs[index] = prob + (2_048 - prob >>> 5) << 16 >> 16;
2356
+ if (!(rangeDecoder.Range & -16777216)) {
2357
+ rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2358
+ rangeDecoder.Range <<= 8;
2359
+ }
2360
+ return 0;
2361
+ }
2362
+ else {
2363
+ rangeDecoder.Range -= newBound;
2364
+ rangeDecoder.Code -= newBound;
2365
+ probs[index] = prob - (prob >>> 5) << 16 >> 16;
2366
+ if (!(rangeDecoder.Range & -16777216)) {
2367
+ rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2368
+ rangeDecoder.Range <<= 8;
2369
+ }
2370
+ return 1;
2371
+ }
2372
+ }
2373
+ #DecodeDirectBits(numTotalBits) {
2374
+ const rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
2375
+ let result = 0;
2376
+ for (let i = numTotalBits; i != 0; i -= 1) {
2377
+ rangeDecoder.Range >>>= 1;
2378
+ let t = rangeDecoder.Code - rangeDecoder.Range >>> 31;
2379
+ rangeDecoder.Code -= rangeDecoder.Range & t - 1;
2380
+ result = result << 1 | 1 - t;
2381
+ if (!(rangeDecoder.Range & -16777216)) {
2382
+ rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2383
+ rangeDecoder.Range <<= 8;
2384
+ }
2385
+ }
2386
+ return result;
2387
+ }
2388
+ #Init_8() {
2389
+ this.#decoder.m_RangeDecoder.Code = 0;
2390
+ this.#decoder.m_RangeDecoder.Range = -1;
2391
+ for (let i = 0; i < 5; ++i) {
2392
+ this.#decoder.m_RangeDecoder.Code = this.#decoder.m_RangeDecoder.Code << 8
2393
+ | this.#read(this.#decoder.m_RangeDecoder.Stream);
2394
+ }
2395
+ }
2396
+ InitBitModels(probs) {
2397
+ for (let i = probs.length - 1; i >= 0; --i) {
2398
+ probs[i] = 1024;
2399
+ }
2400
+ }
2401
+ #Encode_3(probs, index, symbol) {
2402
+ const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2403
+ var newBound, prob = probs[index];
2404
+ newBound = (rangeEncoder.Range >>> 11) * prob;
2405
+ if (!symbol) {
2406
+ rangeEncoder.Range = newBound;
2407
+ probs[index] = prob + (2_048 - prob >>> 5) << 16 >> 16;
2408
+ }
2409
+ else {
2410
+ rangeEncoder.Low = this.#add(rangeEncoder.Low, this.#and(this.#fromInt(newBound), [4294967295, 0]));
2411
+ rangeEncoder.Range -= newBound;
2412
+ probs[index] = prob - (prob >>> 5) << 16 >> 16;
2413
+ }
2414
+ if (!(rangeEncoder.Range & -16777216)) {
2415
+ rangeEncoder.Range <<= 8;
2416
+ this.#ShiftLow();
2417
+ }
2418
+ }
2419
+ #EncodeDirectBits(valueToEncode, numTotalBits) {
2420
+ const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2421
+ for (let i = numTotalBits - 1; i >= 0; i -= 1) {
2422
+ rangeEncoder.Range >>>= 1;
2423
+ if ((valueToEncode >>> i & 1) == 1) {
2424
+ rangeEncoder.Low = this.#add(rangeEncoder.Low, this.#fromInt(rangeEncoder.Range));
2425
+ }
2426
+ if (!(rangeEncoder.Range & -16777216)) {
2427
+ rangeEncoder.Range <<= 8;
2428
+ this.#ShiftLow();
2429
+ }
2430
+ }
2431
+ }
2432
+ #GetProcessedSizeAdd() {
2433
+ const processedCacheSize = this.#add(this.#fromInt(this.#compressor.chunker.encoder._rangeEncoder._cacheSize), this.#compressor.chunker.encoder._rangeEncoder._position);
2434
+ return this.#add(processedCacheSize, [4, 0]);
2435
+ }
2436
+ #Init_9() {
2437
+ this.#encoder._rangeEncoder._position = this.#P0_LONG_LIT;
2438
+ this.#encoder._rangeEncoder.Low = this.#P0_LONG_LIT;
2439
+ this.#encoder._rangeEncoder.Range = -1;
2440
+ this.#encoder._rangeEncoder._cacheSize = 1;
2441
+ this.#encoder._rangeEncoder._cache = 0;
2442
+ }
2443
+ #ShiftLow() {
2444
+ const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2445
+ const LowHi = this.#lowBits_0(this.#shru(rangeEncoder.Low, 32));
2446
+ if (LowHi != 0 || this.#compare(rangeEncoder.Low, [4278190080, 0]) < 0) {
2447
+ rangeEncoder._position = this.#add(rangeEncoder._position, this.#fromInt(rangeEncoder._cacheSize));
2448
+ let temp = rangeEncoder._cache;
2449
+ do {
2450
+ this.#write(rangeEncoder.Stream, temp + LowHi);
2451
+ temp = 255;
2452
+ } while ((rangeEncoder._cacheSize -= 1) != 0);
2453
+ rangeEncoder._cache = this.#lowBits_0(rangeEncoder.Low) >>> 24;
2454
+ }
2455
+ rangeEncoder._cacheSize += 1;
2456
+ rangeEncoder.Low = this.#shl(this.#and(rangeEncoder.Low, [16777215, 0]), 8);
2457
+ }
2458
+ GetPrice(Prob, symbol) {
2459
+ return this.#probPrices[((Prob - symbol ^ -symbol) & 2047) >>> 2];
2460
+ }
2461
+ #decode(utf) {
2462
+ let j = 0, x, y, z, l = utf.length, buf = [], charCodes = [];
2463
+ for (let i = 0; i < l; ++i, ++j) {
2464
+ x = utf[i] & 255;
2465
+ if (!(x & 128)) {
2466
+ if (!x) {
2467
+ // It appears that this is binary data, so it cannot be
2468
+ // converted to a string, so just send it back.
2469
+ return utf;
2470
+ }
2471
+ charCodes[j] = x;
2472
+ }
2473
+ else if ((x & 224) == 192) {
2474
+ if (i + 1 >= l) {
2475
+ // It appears that this is binary data, so it cannot be
2476
+ // converted to a string, so just send it back.
2477
+ return utf;
2478
+ }
2479
+ y = utf[++i] & 255;
2480
+ if ((y & 192) != 128) {
2481
+ // It appears that this is binary data, so it cannot be
2482
+ // converted to a string, so just send it back.
2483
+ return utf;
2484
+ }
2485
+ charCodes[j] = ((x & 31) << 6) | (y & 63);
2486
+ }
2487
+ else if ((x & 240) == 224) {
2488
+ if (i + 2 >= l) {
2489
+ // It appears that this is binary data, so it cannot be
2490
+ // converted to a string, so just send it back.
2491
+ return utf;
2492
+ }
2493
+ y = utf[++i] & 255;
2494
+ if ((y & 192) != 128) {
2495
+ // It appears that this is binary data, so it cannot be
2496
+ // converted to a string, so just send it back.
2497
+ return utf;
2498
+ }
2499
+ z = utf[++i] & 255;
2500
+ if ((z & 192) != 128) {
2501
+ // It appears that this is binary data, so it cannot be converted to
2502
+ // a string, so just send it back.
2503
+ return utf;
2504
+ }
2505
+ charCodes[j] = ((x & 15) << 12) | ((y & 63) << 6) | (z & 63);
2506
+ }
2507
+ else {
2508
+ // It appears that this is binary data, so it cannot be converted to
2509
+ // a string, so just send it back.
2510
+ return utf;
2511
+ }
2512
+ if (j == 16383) {
2513
+ buf.push(String.fromCharCode.apply(String, charCodes));
2514
+ j = -1;
2515
+ }
2516
+ }
2517
+ if (j > 0) {
2518
+ charCodes.length = j;
2519
+ buf.push(String.fromCharCode.apply(String, charCodes));
2520
+ }
2521
+ return buf.join("");
2522
+ }
2523
+ encode(inputString) {
2524
+ let ch, chars = [], elen = 0, l = inputString.length;
2525
+ // Be able to handle binary arrays and buffers.
2526
+ if (typeof inputString === "object") {
2527
+ return inputString;
2528
+ }
2529
+ else {
2530
+ this.#getChars(inputString, 0, l, chars, 0);
2531
+ }
2532
+ // Add extra spaces in the array to break up the unicode symbols.
2533
+ for (let i = 0; i < l; ++i) {
2534
+ ch = chars[i];
2535
+ if (ch >= 1 && ch <= 127) {
2536
+ ++elen;
2537
+ }
2538
+ else if (!ch || ch >= 128 && ch <= 2047) {
2539
+ elen += 2;
2540
+ }
2541
+ else {
2542
+ elen += 3;
2543
+ }
2544
+ }
2545
+ const data = [];
2546
+ elen = 0;
2547
+ for (let i = 0; i < l; ++i) {
2548
+ ch = chars[i];
2549
+ if (ch >= 1 && ch <= 127) {
2550
+ data[elen++] = ch << 24 >> 24;
2551
+ }
2552
+ else if (!ch || ch >= 128 && ch <= 2047) {
2553
+ data[elen++] = (192 | ch >> 6 & 31) << 24 >> 24;
2554
+ data[elen++] = (128 | ch & 63) << 24 >> 24;
2555
+ }
2556
+ else {
2557
+ data[elen++] = (224 | ch >> 12 & 15) << 24 >> 24;
2558
+ data[elen++] = (128 | ch >> 6 & 63) << 24 >> 24;
2559
+ data[elen++] = (128 | ch & 63) << 24 >> 24;
2560
+ }
2561
+ }
2562
+ return data;
2563
+ }
2564
+ compress(data, mode = 5) {
2565
+ const encodedData = this.encode(data);
2566
+ const compressionMode = this.CompressionModes[mode];
2567
+ this.#byteArrayCompressor(encodedData, compressionMode);
2568
+ while (this.#processChunkEncode())
2569
+ ;
2570
+ const compressedByteArray = this.#toByteArray(this.#compressor.output);
2571
+ return new Int8Array(compressedByteArray);
2572
+ }
2573
+ decompress(bytearray) {
2574
+ this.#byteArrayDecompressor(bytearray);
2575
+ while (this.#processChunkDecode())
2576
+ ;
2577
+ const decodedByteArray = this.#toByteArray(this.#decompressor.output);
2578
+ const decoded = this.#decode(decodedByteArray);
2579
+ return decoded instanceof Array
2580
+ ? new Int8Array(decoded)
2581
+ : decoded;
2582
+ }
2583
+ }
2584
+ /**
2585
+ * Compresses data using LZMA algorithm
2586
+ *
2587
+ * @param data Data to compress - can be string, Uint8Array or ArrayBuffer
2588
+ * @param mode Compression mode (1-9), defaults to 5
2589
+ * @returns Compressed data as Int8Array
2590
+ */
2591
+ export function compress(data, mode = 5) {
2592
+ const lzma = new LZMA();
2593
+ return lzma.compress(data, mode);
2594
+ }
2595
+ /**
2596
+ * Decompresses LZMA compressed data
2597
+ *
2598
+ * @param data Compressed data as Uint8Array or ArrayBuffer
2599
+ * @returns Decompressed data as string if input was string, or Int8Array if input was binary
2600
+ */
2601
+ export function decompress(data) {
2602
+ const lzma = new LZMA();
2603
+ return lzma.decompress(data);
2604
+ }