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