lzma1 0.0.2 → 0.0.3

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