lzma1 0.1.2 → 0.3.0

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