lzma1 0.0.2 → 0.0.4

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