lzma1 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/lzma.js CHANGED
@@ -1,9 +1,6 @@
1
- import { DecoderChunker, EncoderChunker, } from "./chunker.js";
2
1
  import { Decoder } from "./decoder.js";
3
- import { Encoder, } from "./encoder.js";
4
- import { LzInWindow } from "./lz-in-window.js";
5
- import { computeHashSize, computeWindowReservSize, ensureCyclicBuffer, isDictionarySizeBelowThreshold, setCutValue, setMatchMaxLen, } from "./match-finder-config.js";
6
- import { _MAX_UINT32, add64, arraycopy, compare64, CRC32_TABLE, createBitTree, DICTIONARY_SIZE_THRESHOLD, fromInt64, G_FAST_POS, getBitPrice, getLenToPosState, INFINITY_PRICE, initArray, lowBits64, N1_LONG_LIT, P0_LONG_LIT, P1_LONG_LIT, PROB_PRICES, shr64, stateUpdateChar, sub64, } from "./utils.js";
2
+ import { Encoder } from "./encoder.js";
3
+ import { InputBuffer, OutputBuffer, } from "./streams.js";
7
4
  /**
8
5
  * Compression modes
9
6
  */
@@ -19,1344 +16,88 @@ export const MODES = {
19
16
  9: { searchDepth: 0x19, filterStrength: 0xFF, modeIndex: 0x01 },
20
17
  };
21
18
  export class LZMA {
22
- #encoder;
23
- #decoder;
24
- #lzInWindow = null;
25
- #compressor;
26
- #decompressor;
27
- constructor() {
28
- this.#encoder = new Encoder();
29
- this.#decoder = new Decoder();
30
- this.#compressor = this.#initCompressor();
31
- this.#decompressor = this.#initDecompressor();
32
- }
33
- #initCompressor() {
34
- const encoderChunker = new EncoderChunker(this);
35
- return {
36
- chunker: encoderChunker,
37
- output: {
38
- buf: initArray(32),
39
- count: 0,
40
- write: () => { },
41
- },
42
- };
43
- }
44
- #initDecompressor() {
45
- const decoderChunker = new DecoderChunker(this.#decoder);
46
- return {
47
- chunker: decoderChunker,
48
- output: {
49
- buf: initArray(0x20),
50
- count: 0,
51
- write: () => { },
52
- },
53
- };
54
- }
55
- #read(inputStream) {
56
- if (inputStream.pos >= inputStream.count) {
57
- return -1;
58
- }
59
- let value;
60
- if (inputStream.buf instanceof ArrayBuffer) {
61
- value = new Uint8Array(inputStream.buf)[inputStream.pos++];
62
- }
63
- else if (inputStream.buf instanceof Uint8Array) {
64
- value = inputStream.buf[inputStream.pos++];
65
- }
66
- else {
67
- value = inputStream.buf[inputStream.pos++];
68
- }
69
- return value & 0xFF;
70
- }
71
- #toByteArray(output) {
72
- const data = output.buf.slice(0, output.count);
73
- return data;
74
- }
75
- #write(buffer, b) {
76
- if (!buffer)
77
- return;
78
- if (buffer.count >= buffer.buf.length) {
79
- const newSize = Math.max(buffer.buf.length * 2, buffer.count + 1);
80
- const newBuf = new Array(newSize);
81
- for (let i = 0; i < buffer.count; i++) {
82
- newBuf[i] = buffer.buf[i];
83
- }
84
- buffer.buf = newBuf;
85
- }
86
- buffer.buf[buffer.count++] = b << 24 >> 24;
87
- }
88
- #write_0(buffer, buf, off, len) {
89
- const requiredSize = buffer.count + len;
90
- if (requiredSize > buffer.buf.length) {
91
- const newSize = Math.max(buffer.buf.length * 2, requiredSize);
92
- const newBuf = new Array(newSize);
93
- for (let i = 0; i < buffer.count; i++) {
94
- newBuf[i] = buffer.buf[i];
95
- }
96
- buffer.buf = newBuf;
97
- }
98
- arraycopy(buf, off, buffer.buf, buffer.count, len);
99
- buffer.count += len;
100
- }
101
- #getChars(inputString, srcBegin, srcEnd, dst, dstBegin) {
102
- for (let srcIdx = srcBegin; srcIdx < srcEnd; ++srcIdx) {
103
- dst[dstBegin++] = inputString.charCodeAt(srcIdx);
104
- }
105
- }
106
- #configure(mode) {
107
- this.#encoder.initialize();
108
- this.#encoder.configure(mode);
109
- }
110
- #initCompression(input, len, mode) {
111
- if (compare64(len, N1_LONG_LIT) < 0) {
112
- throw new Error("invalid length " + len);
113
- }
114
- this.#compressor.length_0 = len;
115
- this.#Encoder();
116
- this.#configure(mode);
117
- this.writeHeaderProperties();
118
- for (let i = 0; i < 64; i += 8) {
119
- this.#write(this.#compressor.output, lowBits64(shr64(len, i)) & 0xFF);
120
- }
121
- this.#encoder._needReleaseMFStream = 0;
122
- this.#encoder._inStream = input;
123
- this.#encoder._finished = 0;
124
- this.#Create_2();
125
- this.#encoder._rangeEncoder.stream = this.#compressor.output;
126
- this.#encoder.init();
127
- this.#encoder.fillDistancesPrices();
128
- this.#encoder.fillAlignPrices();
129
- this.#encoder._lenEncoder.setTableSize(this.#encoder._numFastBytes + 1 - 2);
130
- this.#encoder._lenEncoder.updateTables(1 << this.#encoder._posStateBits);
131
- this.#encoder._repMatchLenEncoder.setTableSize(this.#encoder._numFastBytes + 1 - 2);
132
- this.#encoder._repMatchLenEncoder.updateTables(1 << this.#encoder._posStateBits);
133
- this.#encoder.nowPos64 = P0_LONG_LIT;
134
- this.#compressor.chunker.encoder = this.#encoder;
135
- this.#compressor.chunker.alive = 1;
136
- }
137
- #byteArrayCompressor(data, mode) {
138
- const inputSize = data instanceof ArrayBuffer ? data.byteLength : data.length;
139
- const estimatedOutputSize = Math.max(32, Math.ceil(inputSize * 1.2));
140
- this.#compressor.output = {
141
- buf: initArray(estimatedOutputSize),
142
- count: 0,
143
- write: () => { },
144
- };
145
- const inputBuffer = {
146
- pos: 0,
147
- buf: data instanceof ArrayBuffer
148
- ? new Uint8Array(data)
149
- : data,
150
- count: data instanceof ArrayBuffer
151
- ? new Uint8Array(data).length
152
- : data.length,
153
- };
154
- this.#initCompression(inputBuffer, fromInt64(data instanceof ArrayBuffer ? data.byteLength : data.length), mode);
155
- }
156
- #initDecompression(input) {
157
- let hex_length = "", properties = [], r, tmp_length;
158
- for (let i = 0; i < 5; ++i) {
159
- r = this.#read(input);
160
- if (r == -1) {
161
- throw new Error("truncated input");
162
- }
163
- properties[i] = r << 24 >> 24;
164
- }
165
- const isDecoderInitialized = !this.#decoder.setDecoderProperties(properties)
166
- ? 1
167
- : 0;
168
- if (isDecoderInitialized) {
169
- throw new Error("corrupted input");
170
- }
171
- for (let i = 0; i < 64; i += 8) {
172
- r = this.#read(input);
173
- if (r == -1) {
174
- throw new Error("truncated input");
175
- }
176
- r = r.toString(0x10);
177
- if (r.length == 1)
178
- r = "0" + r;
179
- hex_length = r + "" + hex_length;
180
- }
181
- /**
182
- * Was the length set in the header (if it was compressed from a stream, the
183
- * length is all f"s).
184
- */
185
- if (/^0+$|^f+$/i.test(hex_length)) {
186
- this.#compressor.length_0 = N1_LONG_LIT;
187
- }
188
- else {
189
- /**
190
- * NOTE: If there is a problem with the decoder because of the length,
191
- * you can always set the length to -1 (N1_longLit) which means unknown.
192
- */
193
- tmp_length = parseInt(hex_length, 0x10);
194
- if (tmp_length > _MAX_UINT32) {
195
- this.#compressor.length_0 = N1_LONG_LIT;
196
- }
197
- else {
198
- this.#compressor.length_0 = fromInt64(tmp_length);
199
- }
200
- }
201
- this.#decompressor.chunker = this.#CodeInChunks(input, this.#compressor.length_0);
202
- }
203
- #byteArrayDecompressor(data) {
204
- const inputDataSize = data instanceof ArrayBuffer ? data.byteLength : data.length;
205
- const minBufferSize = 0x20; // 32 bytes minimum
206
- const estimatedOutputSize = inputDataSize * 2; // Estimate 2x expansion for decompression
207
- const initialBufferSize = Math.max(minBufferSize, estimatedOutputSize);
208
- this.#decompressor.output = {
209
- buf: initArray(initialBufferSize),
210
- count: 0,
211
- write: () => { },
212
- };
213
- const inputBuffer = {
214
- buf: data,
215
- pos: 0,
216
- count: data instanceof ArrayBuffer ? data.byteLength : data.length,
217
- };
218
- this.#initDecompression(inputBuffer);
219
- }
220
- #Create_4(keepSizeBefore, keepSizeAfter, keepSizeReserv) {
221
- let blockSize;
222
- this.#encoder._matchFinder._keepSizeBefore = keepSizeBefore;
223
- this.#encoder._matchFinder._keepSizeAfter = keepSizeAfter;
224
- blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
225
- if (this.#encoder._matchFinder._bufferBase == null || this.#encoder._matchFinder._blockSize != blockSize) {
226
- this.#encoder._matchFinder._bufferBase = initArray(blockSize);
227
- this.#encoder._matchFinder._blockSize = blockSize;
228
- }
229
- this.#encoder._matchFinder._pointerToLastSafePosition = this.#encoder._matchFinder._blockSize - keepSizeAfter;
230
- }
231
- #MovePos_1() {
232
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
233
- let pointerToPostion;
234
- matchFinder._pos += 1;
235
- if (matchFinder._pos > matchFinder._posLimit) {
236
- pointerToPostion = matchFinder._bufferOffset + matchFinder._pos;
237
- if (pointerToPostion > matchFinder._pointerToLastSafePosition) {
238
- this.#lzInWindow.moveBlock();
239
- }
240
- this.#lzInWindow.readBlock();
241
- }
242
- }
243
- #Create_3(keepAddBufferBefore, keepAddBufferAfter) {
244
- const dictionarySize = this.#encoder._dictionarySize;
245
- const numFastBytes = this.#encoder._numFastBytes;
246
- if (isDictionarySizeBelowThreshold(dictionarySize)) {
247
- this.#encoder._matchFinder._cutValue = setCutValue(numFastBytes);
248
- const windowReservSize = computeWindowReservSize(dictionarySize, keepAddBufferBefore, numFastBytes, keepAddBufferAfter);
249
- this.#Create_4(dictionarySize + keepAddBufferBefore, numFastBytes + keepAddBufferAfter, windowReservSize);
250
- this.#encoder._matchFinder._matchMaxLen = setMatchMaxLen(numFastBytes);
251
- ensureCyclicBuffer(this.#encoder._matchFinder, dictionarySize);
252
- const { hashMask, hashSizeSum } = computeHashSize(dictionarySize, this.#encoder._matchFinder.HASH_ARRAY);
253
- if (this.#encoder._matchFinder.HASH_ARRAY) {
254
- this.#encoder._matchFinder._hashMask = hashMask;
255
- const finalHashSizeSum = hashSizeSum + this.#encoder._matchFinder.kFixHashSize;
256
- if (finalHashSizeSum !== this.#encoder._matchFinder._hashSizeSum) {
257
- this.#encoder._matchFinder._hashSizeSum = finalHashSizeSum;
258
- this.#encoder._matchFinder._hash = initArray(finalHashSizeSum);
259
- }
260
- }
261
- else {
262
- if (hashSizeSum !== this.#encoder._matchFinder._hashSizeSum) {
263
- this.#encoder._matchFinder._hashSizeSum = hashSizeSum;
264
- this.#encoder._matchFinder._hash = initArray(hashSizeSum);
265
- }
266
- }
267
- }
268
- }
269
- #GetMatches() {
270
- let count, cur, curMatch, curMatch2, curMatch3, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, maxLen, offset, pby1, ptr0, ptr1, temp;
271
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
272
- const distances = this.#compressor.chunker.encoder._matchDistances;
273
- if (matchFinder._pos + matchFinder._matchMaxLen <= matchFinder._streamPos) {
274
- lenLimit = matchFinder._matchMaxLen;
275
- }
276
- else {
277
- lenLimit = matchFinder._streamPos - matchFinder._pos;
278
- if (lenLimit < matchFinder.kMinMatchCheck) {
279
- this.#MovePos_0();
280
- return 0;
281
- }
282
- }
283
- offset = 0;
284
- matchMinPos = matchFinder._pos > matchFinder._cyclicBufferSize
285
- ? matchFinder._pos - matchFinder._cyclicBufferSize
286
- : 0;
287
- cur = matchFinder._bufferOffset + matchFinder._pos;
288
- maxLen = 1;
289
- hash2Value = 0;
290
- hash3Value = 0;
291
- if (matchFinder.HASH_ARRAY) {
292
- temp = CRC32_TABLE[matchFinder._bufferBase[cur] & 0xFF] ^ (matchFinder._bufferBase[cur + 1] & 0xFF);
293
- hash2Value = temp & 0x3FF;
294
- temp ^= (matchFinder._bufferBase[cur + 2] & 0xFF) << 0x08;
295
- hash3Value = temp & 0xFFFF;
296
- hashValue = (temp ^ (CRC32_TABLE[matchFinder._bufferBase[cur + 3] & 0xFF] << 5)) & matchFinder._hashMask;
297
- }
298
- else {
299
- hashValue = (matchFinder._bufferBase[cur] & 0xFF) ^ ((matchFinder._bufferBase[cur + 1] & 0xFF) << 0x08);
300
- }
301
- curMatch = matchFinder._hash[matchFinder.kFixHashSize + hashValue] || 0;
302
- if (matchFinder.HASH_ARRAY) {
303
- curMatch2 = matchFinder._hash[hash2Value] || 0;
304
- curMatch3 = matchFinder._hash[0x400 + hash3Value] || 0;
305
- matchFinder._hash[hash2Value] = matchFinder._pos;
306
- matchFinder._hash[0x400 + hash3Value] = matchFinder._pos;
307
- if (curMatch2 > matchMinPos) {
308
- if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch2] == matchFinder._bufferBase[cur]) {
309
- distances[offset++] = maxLen = 2;
310
- distances[offset++] = matchFinder._pos - curMatch2 - 1;
311
- }
312
- }
313
- if (curMatch3 > matchMinPos) {
314
- if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch3] == matchFinder._bufferBase[cur]) {
315
- if (curMatch3 == curMatch2) {
316
- offset -= 2;
317
- }
318
- distances[offset++] = maxLen = 3;
319
- distances[offset++] = matchFinder._pos - curMatch3 - 1;
320
- curMatch2 = curMatch3;
321
- }
322
- }
323
- if (offset != 0 && curMatch2 == curMatch) {
324
- offset -= 2;
325
- maxLen = 1;
326
- }
327
- }
328
- matchFinder._hash[matchFinder.kFixHashSize + hashValue] = matchFinder._pos;
329
- ptr0 = (matchFinder._cyclicBufferPos << 1) + 1;
330
- ptr1 = matchFinder._cyclicBufferPos << 1;
331
- len0 = len1 = matchFinder.kNumHashDirectBytes;
332
- if (matchFinder.kNumHashDirectBytes != 0) {
333
- if (curMatch > matchMinPos) {
334
- if (matchFinder._bufferBase[matchFinder._bufferOffset + curMatch + matchFinder.kNumHashDirectBytes] != matchFinder._bufferBase[cur + matchFinder.kNumHashDirectBytes]) {
335
- distances[offset++] = maxLen = matchFinder.kNumHashDirectBytes;
336
- distances[offset++] = matchFinder._pos - curMatch - 1;
337
- }
338
- }
339
- }
340
- count = matchFinder._cutValue;
341
- while (1) {
342
- if (curMatch <= matchMinPos || count == 0) {
343
- count -= 1;
344
- matchFinder._son[ptr0] = matchFinder._son[ptr1] = 0;
345
- break;
346
- }
347
- delta = matchFinder._pos - curMatch;
348
- cyclicPos = (delta <= matchFinder._cyclicBufferPos
349
- ? matchFinder._cyclicBufferPos - delta
350
- : matchFinder._cyclicBufferPos - delta + matchFinder._cyclicBufferSize) << 1;
351
- pby1 = matchFinder._bufferOffset + curMatch;
352
- len = len0 < len1 ? len0 : len1;
353
- if (matchFinder._bufferBase[pby1 + len] == matchFinder._bufferBase[cur + len]) {
354
- while ((len += 1) != lenLimit) {
355
- if (matchFinder._bufferBase[pby1 + len] != matchFinder._bufferBase[cur + len]) {
356
- break;
357
- }
358
- }
359
- if (maxLen < len) {
360
- distances[offset++] = maxLen = len;
361
- distances[offset++] = delta - 1;
362
- if (len == lenLimit) {
363
- matchFinder._son[ptr1] = matchFinder._son[cyclicPos];
364
- matchFinder._son[ptr0] = matchFinder._son[cyclicPos + 1];
365
- break;
366
- }
367
- }
368
- }
369
- if ((matchFinder._bufferBase[pby1 + len] & 0xFF) < (matchFinder._bufferBase[cur + len] & 0xFF)) {
370
- matchFinder._son[ptr1] = curMatch;
371
- ptr1 = cyclicPos + 1;
372
- curMatch = matchFinder._son[ptr1];
373
- len1 = len;
374
- }
375
- else {
376
- matchFinder._son[ptr0] = curMatch;
377
- ptr0 = cyclicPos;
378
- curMatch = matchFinder._son[ptr0];
379
- len0 = len;
380
- }
381
- }
382
- this.#MovePos_0();
383
- return offset;
384
- }
385
- #Init_5() {
386
- this.#compressor.chunker.encoder._matchFinder._bufferOffset = 0;
387
- this.#compressor.chunker.encoder._matchFinder._pos = 0;
388
- this.#compressor.chunker.encoder._matchFinder._streamPos = 0;
389
- this.#compressor.chunker.encoder._matchFinder._streamEndWasReached = 0;
390
- this.#lzInWindow.readBlock();
391
- this.#compressor.chunker.encoder._matchFinder._cyclicBufferPos = 0;
392
- this.#lzInWindow.reduceOffsets(-1);
393
- }
394
- #MovePos_0() {
395
- let subValue;
396
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
397
- if ((matchFinder._cyclicBufferPos += 1) >= matchFinder._cyclicBufferSize) {
398
- matchFinder._cyclicBufferPos = 0;
399
- }
400
- this.#MovePos_1();
401
- if (matchFinder._pos == DICTIONARY_SIZE_THRESHOLD) {
402
- subValue = matchFinder._pos - matchFinder._cyclicBufferSize;
403
- this.#NormalizeLinks(matchFinder._cyclicBufferSize * 2, subValue);
404
- this.#NormalizeLinks(matchFinder._hashSizeSum, subValue);
405
- this.#lzInWindow.reduceOffsets(subValue);
406
- }
407
- }
408
- /**
409
- * This is only called after reading one whole gigabyte.
410
- */
411
- #NormalizeLinks(numItems, subValue) {
412
- const items = this.#compressor.chunker.encoder._matchFinder._son;
413
- for (let i = 0, value; i < numItems; ++i) {
414
- value = items[i] || 0;
415
- if (value <= subValue) {
416
- value = 0;
417
- }
418
- else {
419
- value -= subValue;
420
- }
421
- items[i] = value;
422
- }
423
- }
424
- #SetType(binTree, numHashBytes) {
425
- binTree.HASH_ARRAY = numHashBytes > 2;
426
- if (binTree.HASH_ARRAY) {
427
- binTree.kNumHashDirectBytes = 0;
428
- binTree.kMinMatchCheck = 4;
429
- binTree.kFixHashSize = 66560;
430
- }
431
- else {
432
- binTree.kNumHashDirectBytes = 2;
433
- binTree.kMinMatchCheck = 3;
434
- binTree.kFixHashSize = 0;
435
- }
436
- }
437
- #Skip(num) {
438
- const matchFinder = this.#compressor.chunker.encoder._matchFinder;
439
- let count, cur, curMatch, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, pby1, ptr0, ptr1, temp;
440
- do {
441
- if (matchFinder._pos + matchFinder._matchMaxLen <= matchFinder._streamPos) {
442
- lenLimit = matchFinder._matchMaxLen;
443
- }
444
- else {
445
- lenLimit = matchFinder._streamPos - matchFinder._pos;
446
- if (lenLimit < matchFinder.kMinMatchCheck) {
447
- this.#MovePos_0();
448
- continue;
449
- }
450
- }
451
- matchMinPos = matchFinder._pos > matchFinder._cyclicBufferSize
452
- ? matchFinder._pos - matchFinder._cyclicBufferSize
453
- : 0;
454
- cur = matchFinder._bufferOffset + matchFinder._pos;
455
- if (matchFinder.HASH_ARRAY) {
456
- temp = CRC32_TABLE[matchFinder._bufferBase[cur] & 0xFF] ^ (matchFinder._bufferBase[cur + 1] & 0xFF);
457
- hash2Value = temp & 0x3FF;
458
- matchFinder._hash[hash2Value] = matchFinder._pos;
459
- temp ^= (matchFinder._bufferBase[cur + 2] & 0xFF) << 0x08;
460
- hash3Value = temp & 0xFFFF;
461
- matchFinder._hash[0x400 + hash3Value] = matchFinder._pos;
462
- hashValue = (temp ^ (CRC32_TABLE[matchFinder._bufferBase[cur + 3] & 0xFF] << 5)) & matchFinder._hashMask;
463
- }
464
- else {
465
- hashValue = (matchFinder._bufferBase[cur] & 0xFF) ^ ((matchFinder._bufferBase[cur + 1] & 0xFF) << 0x08);
466
- }
467
- curMatch = matchFinder._hash[matchFinder.kFixHashSize + hashValue];
468
- matchFinder._hash[matchFinder.kFixHashSize + hashValue] = matchFinder._pos;
469
- ptr0 = (matchFinder._cyclicBufferPos << 1) + 1;
470
- ptr1 = matchFinder._cyclicBufferPos << 1;
471
- len0 = len1 = matchFinder.kNumHashDirectBytes;
472
- count = matchFinder._cutValue;
473
- while (1) {
474
- if (curMatch <= matchMinPos || count == 0) {
475
- count -= 1;
476
- matchFinder._son[ptr0] = matchFinder._son[ptr1] = 0;
477
- break;
478
- }
479
- delta = matchFinder._pos - curMatch;
480
- cyclicPos = (delta <= matchFinder._cyclicBufferPos
481
- ? matchFinder._cyclicBufferPos - delta
482
- : matchFinder._cyclicBufferPos - delta + matchFinder._cyclicBufferSize) << 1;
483
- pby1 = matchFinder._bufferOffset + curMatch;
484
- len = len0 < len1 ? len0 : len1;
485
- if (matchFinder._bufferBase[pby1 + len] == matchFinder._bufferBase[cur + len]) {
486
- while ((len += 1) != lenLimit) {
487
- if (matchFinder._bufferBase[pby1 + len] != matchFinder._bufferBase[cur + len]) {
488
- break;
489
- }
490
- }
491
- if (len == lenLimit) {
492
- matchFinder._son[ptr1] = matchFinder._son[cyclicPos];
493
- matchFinder._son[ptr0] = matchFinder._son[cyclicPos + 1];
494
- break;
495
- }
496
- }
497
- if ((matchFinder._bufferBase[pby1 + len] & 0xFF) < (matchFinder._bufferBase[cur + len] & 0xFF)) {
498
- matchFinder._son[ptr1] = curMatch;
499
- ptr1 = cyclicPos + 1;
500
- curMatch = matchFinder._son[ptr1];
501
- len1 = len;
502
- }
503
- else {
504
- matchFinder._son[ptr0] = curMatch;
505
- ptr0 = cyclicPos;
506
- curMatch = matchFinder._son[ptr0];
507
- len0 = len;
508
- }
509
- }
510
- this.#MovePos_0();
511
- } while ((num -= 1) != 0);
512
- }
513
- #CodeInChunks(inStream, outSize) {
514
- this.#decoder.rangeDecoder.stream = inStream;
515
- this.#decoder.flush();
516
- this.#decoder.outWindow.stream = null;
517
- this.#decoder.outWindow.stream = this.#decompressor.output;
518
- this.#Init_1();
519
- this.#decoder.state = 0;
520
- this.#decoder.rep0 = 0;
521
- this.#decoder.rep1 = 0;
522
- this.#decoder.rep2 = 0;
523
- this.#decoder.rep3 = 0;
524
- this.#decoder.outSize = outSize;
525
- this.#decoder.nowPos64 = P0_LONG_LIT;
526
- this.#decoder.prevByte = 0;
527
- // Create and return decoder chunker
528
- const decoderChunker = new DecoderChunker(this.#decoder);
529
- decoderChunker.alive = 1;
530
- return decoderChunker;
531
- }
532
- #Init_1() {
533
- this.#decoder.init();
534
- }
535
- #Backward(cur) {
536
- const encoder = this.#compressor.chunker.encoder;
537
- let backCur, backMem, posMem, posPrev;
538
- encoder._optimumEndIndex = cur;
539
- posMem = encoder._optimum[cur].posPrev;
540
- backMem = encoder._optimum[cur].backPrev;
541
- do {
542
- if (encoder._optimum[cur].prev1IsChar) {
543
- this.#MakeAsChar(encoder._optimum[posMem]);
544
- encoder._optimum[posMem].posPrev = posMem - 1;
545
- if (encoder._optimum[cur].prev2) {
546
- encoder._optimum[posMem - 1].prev1IsChar = 0;
547
- encoder._optimum[posMem - 1].posPrev = encoder._optimum[cur].posPrev2;
548
- encoder._optimum[posMem - 1].backPrev = encoder._optimum[cur].backPrev2;
549
- }
550
- }
551
- posPrev = posMem;
552
- backCur = backMem;
553
- backMem = encoder._optimum[posPrev].backPrev;
554
- posMem = encoder._optimum[posPrev].posPrev;
555
- encoder._optimum[posPrev].backPrev = backCur;
556
- encoder._optimum[posPrev].posPrev = cur;
557
- cur = posPrev;
558
- } while (cur > 0);
559
- encoder.backRes = encoder._optimum[0].backPrev;
560
- encoder._optimumCurrentIndex = encoder._optimum[0].posPrev;
561
- return encoder._optimumCurrentIndex;
562
- }
563
- #CodeOneBlock() {
564
- let baseVal, complexState, curByte, distance, footerBits, len, lenToPosState, matchByte, pos, posReduced, posSlot, posState, progressPosValuePrev, subCoder;
565
- this.#compressor.chunker.encoder.processedInSize[0] = P0_LONG_LIT;
566
- this.#compressor.chunker.encoder.processedOutSize[0] = P0_LONG_LIT;
567
- this.#compressor.chunker.encoder.finished[0] = 1;
568
- progressPosValuePrev = this.#compressor.chunker.encoder.nowPos64;
569
- if (this.#compressor.chunker.encoder._inStream) {
570
- this.#compressor.chunker.encoder._matchFinder._stream = this.#compressor.chunker.encoder._inStream;
571
- this.#Init_5();
572
- this.#compressor.chunker.encoder._needReleaseMFStream = 1;
573
- this.#compressor.chunker.encoder._inStream = null;
574
- }
575
- if (this.#compressor.chunker.encoder._finished) {
576
- return;
577
- }
578
- this.#compressor.chunker.encoder._finished = 1;
579
- if (compare64(this.#compressor.chunker.encoder.nowPos64, P0_LONG_LIT) === 0) {
580
- if (!this.#lzInWindow.getNumAvailableBytes()) {
581
- this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
582
- return;
583
- }
584
- this.#ReadMatchDistances();
585
- posState = lowBits64(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
586
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, (this.#compressor.chunker.encoder._state << 4) + posState, 0);
587
- this.#compressor.chunker.encoder._state = stateUpdateChar(this.#compressor.chunker.encoder._state);
588
- curByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
589
- this.#compressor.chunker.encoder.encodeLiteral(this.#LZMA_Encoder_GetSubCoder(lowBits64(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
590
- this.#compressor.chunker.encoder._previousByte = curByte;
591
- this.#compressor.chunker.encoder._additionalOffset -= 1;
592
- this.#compressor.chunker.encoder.nowPos64 = add64(this.#compressor.chunker.encoder.nowPos64, P1_LONG_LIT);
593
- }
594
- if (!this.#lzInWindow.getNumAvailableBytes()) {
595
- this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
596
- return;
597
- }
598
- while (1) {
599
- len = this.#GetOptimum(lowBits64(this.#compressor.chunker.encoder.nowPos64));
600
- pos = this.#compressor.chunker.encoder.backRes;
601
- posState = lowBits64(this.#compressor.chunker.encoder.nowPos64) & this.#compressor.chunker.encoder._posStateMask;
602
- complexState = (this.#compressor.chunker.encoder._state << 4) + posState;
603
- if (len == 1 && pos == -1) {
604
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, complexState, 0);
605
- curByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
606
- subCoder = this.#LZMA_Encoder_GetSubCoder(lowBits64(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
607
- if (this.#compressor.chunker.encoder._state < 7) {
608
- this.#compressor.chunker.encoder.encodeLiteral(subCoder, curByte);
609
- }
610
- else {
611
- matchByte = this.#lzInWindow.getIndexByte(-this.#compressor.chunker.encoder._repDistances[0]
612
- - 1
613
- - this.#compressor.chunker.encoder._additionalOffset);
614
- this.#compressor.chunker.encoder.encodeMatched(subCoder, matchByte, curByte);
615
- }
616
- this.#compressor.chunker.encoder._previousByte = curByte;
617
- this.#compressor.chunker.encoder._state = stateUpdateChar(this.#compressor.chunker.encoder._state);
618
- }
619
- else {
620
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isMatch, complexState, 1);
621
- if (pos < 4) {
622
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 1);
623
- if (!pos) {
624
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 0);
625
- if (len == 1) {
626
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep0Long, complexState, 0);
627
- }
628
- else {
629
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep0Long, complexState, 1);
630
- }
631
- }
632
- else {
633
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG0, this.#compressor.chunker.encoder._state, 1);
634
- if (pos == 1) {
635
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 0);
636
- }
637
- else {
638
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG1, this.#compressor.chunker.encoder._state, 1);
639
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRepG2, this.#compressor.chunker.encoder._state, pos - 2);
640
- }
641
- }
642
- if (len == 1) {
643
- this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 9 : 11;
644
- }
645
- else {
646
- this.#compressor.chunker.encoder.encodeLength(this.#compressor.chunker.encoder._repMatchLenEncoder, len - 2, posState);
647
- this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7
648
- ? 0x08
649
- : 11;
650
- }
651
- distance = this.#compressor.chunker.encoder._repDistances[pos];
652
- if (pos != 0) {
653
- const encoder = this.#compressor.chunker.encoder;
654
- for (let i = pos; i >= 1; --i) {
655
- encoder._repDistances[i] = encoder._repDistances[i - 1];
656
- }
657
- encoder._repDistances[0] = distance;
658
- }
659
- }
660
- else {
661
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._isRep, this.#compressor.chunker.encoder._state, 0);
662
- this.#compressor.chunker.encoder._state = this.#compressor.chunker.encoder._state < 7 ? 7 : 10;
663
- this.#compressor.chunker.encoder.encodeLength(this.#compressor.chunker.encoder._lenEncoder, len - 0x02, posState);
664
- pos -= 0x04;
665
- posSlot = this.#compressor.chunker.encoder.getPosSlot(pos);
666
- lenToPosState = getLenToPosState(len);
667
- this.#compressor.chunker.encoder.encodeBitTree(this.#compressor.chunker.encoder._posSlotEncoder[lenToPosState], posSlot);
668
- if (posSlot >= 0x04) {
669
- footerBits = (posSlot >> 0x01) - 0x01;
670
- baseVal = (0x02 | (posSlot & 0x01)) << footerBits;
671
- posReduced = pos - baseVal;
672
- if (posSlot < 0x0E) {
673
- this.#compressor.chunker.encoder.reverseEncodeRange(baseVal - posSlot - 0x01, footerBits, posReduced);
674
- }
675
- else {
676
- this.#compressor.chunker.encoder.encodeDirectBits(posReduced >> 0x04, footerBits - 4);
677
- this.#compressor.chunker.encoder.reverseEncode(posReduced & 0x0F);
678
- this.#compressor.chunker.encoder._alignPriceCount += 1;
679
- }
680
- }
681
- distance = pos;
682
- const encoder2 = this.#compressor.chunker.encoder;
683
- for (let i = 3; i >= 1; --i) {
684
- encoder2._repDistances[i] = encoder2._repDistances[i - 1];
685
- }
686
- encoder2._repDistances[0] = distance;
687
- encoder2._matchPriceCount += 0x01;
688
- }
689
- this.#compressor.chunker.encoder._previousByte = this.#lzInWindow.getIndexByte(len - 1 - this.#compressor.chunker.encoder._additionalOffset);
690
- }
691
- this.#compressor.chunker.encoder._additionalOffset -= len;
692
- this.#compressor.chunker.encoder.nowPos64 = add64(this.#compressor.chunker.encoder.nowPos64, fromInt64(len));
693
- if (!this.#compressor.chunker.encoder._additionalOffset) {
694
- if (this.#compressor.chunker.encoder._matchPriceCount >= 0x80) {
695
- this.#compressor.chunker.encoder.fillDistancesPrices();
696
- }
697
- if (this.#compressor.chunker.encoder._alignPriceCount >= 0x10) {
698
- this.#FillAlignPrices(this.#compressor.chunker.encoder);
699
- }
700
- this.#compressor.chunker.encoder.processedInSize[0] = this.#compressor.chunker.encoder.nowPos64;
701
- this.#compressor.chunker.encoder.processedOutSize[0] = this.#GetProcessedSizeAdd();
702
- if (!this.#lzInWindow.getNumAvailableBytes()) {
703
- this.#Flush(lowBits64(this.#compressor.chunker.encoder.nowPos64));
704
- return;
705
- }
706
- if (compare64(sub64(this.#compressor.chunker.encoder.nowPos64, progressPosValuePrev), [0x1000, 0]) >= 0) {
707
- this.#compressor.chunker.encoder._finished = 0;
708
- this.#compressor.chunker.encoder.finished[0] = 0;
709
- return;
710
- }
711
- }
712
- }
713
- }
714
- #Create_2() {
715
- let binTree, numHashBytes;
716
- if (!this.#encoder._matchFinder) {
717
- binTree = {};
718
- numHashBytes = 4;
719
- if (!this.#encoder._matchFinderType) {
720
- numHashBytes = 2;
721
- }
722
- this.#SetType(binTree, numHashBytes);
723
- this.#encoder._matchFinder = binTree;
724
- this.#lzInWindow = new LzInWindow(binTree);
725
- }
726
- this.#encoder.createLiteralEncoder();
727
- if (this.#encoder._dictionarySize == this.#encoder._dictionarySizePrev
728
- && this.#encoder._numFastBytesPrev == this.#encoder._numFastBytes) {
729
- return;
730
- }
731
- this.#Create_3(0x1000, 0x0112);
732
- this.#encoder._dictionarySizePrev = this.#encoder._dictionarySize;
733
- this.#encoder._numFastBytesPrev = this.#encoder._numFastBytes;
19
+ #encoder = new Encoder();
20
+ #decoder = new Decoder();
21
+ compress(data, mode = 5) {
22
+ const inputData = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
23
+ const output = new OutputBuffer(Math.max(32, Math.ceil(inputData.length * 1.2)));
24
+ const input = new InputBuffer(inputData);
25
+ this.#encoder.compress(input, output, MODES[mode]);
26
+ const result = output.toArray();
27
+ return new Int8Array(result.buffer, result.byteOffset, result.byteLength);
734
28
  }
735
- #Encoder() {
736
- for (let i = 0; i < 0x1000; ++i) {
737
- this.#encoder._optimum[i] = {};
738
- }
739
- for (let i = 0; i < 4; ++i) {
740
- this.#encoder._posSlotEncoder[i] = createBitTree(6);
741
- }
29
+ compressString(data, mode = 5) {
30
+ return this.compress(new Uint8Array(this.#encodeString(data)), mode);
742
31
  }
743
- #FillAlignPrices(encoder) {
744
- for (let i = 0; i < 16; ++i) {
745
- encoder._alignPrices[i] = this.#ReverseGetPrice(encoder._posAlignEncoder, i);
746
- }
747
- encoder._alignPriceCount = 0;
32
+ decompress(bytearray) {
33
+ const inputData = bytearray instanceof ArrayBuffer ? new Uint8Array(bytearray) : bytearray;
34
+ const output = new OutputBuffer(Math.max(32, inputData.length * 2));
35
+ const input = new InputBuffer(inputData);
36
+ this.#decoder.decompress(input, output);
37
+ return output.toArray();
748
38
  }
749
- #Flush(nowPos) {
750
- this.#ReleaseMFStream();
751
- this.#compressor.chunker.encoder.writeEndMarker(nowPos & this.#compressor.chunker.encoder._posStateMask);
752
- for (let i = 0; i < 5; ++i) {
753
- this.#compressor.chunker.encoder.shiftLow();
39
+ decompressString(bytearray) {
40
+ const decodedByteArray = this.decompress(bytearray);
41
+ const result = this.#decodeUTF8(decodedByteArray);
42
+ if (typeof result === "string") {
43
+ return result;
754
44
  }
45
+ return String.fromCharCode(...result);
755
46
  }
756
- #GetOptimum(position) {
757
- let cur, curAnd1Price, curAndLenCharPrice, curAndLenPrice, curBack, curPrice, currentByte, distance, len, lenEnd, lenMain, lenTest, lenTest2, lenTestTemp, matchByte, matchPrice, newLen, nextIsChar, nextMatchPrice, nextOptimum, nextRepMatchPrice, normalMatchPrice, numAvailableBytes, numAvailableBytesFull, numDistancePairs, offs, offset, opt, optimum, pos, posPrev, posState, posStateNext, price_4, repIndex, repLen, repMatchPrice, repMaxIndex, shortRepPrice, startLen, state, state2, t, price, price_0, price_1, price_2, price_3, lenRes;
758
- const encoder = this.#compressor.chunker.encoder;
759
- if (encoder._optimumEndIndex != encoder._optimumCurrentIndex) {
760
- lenRes = encoder._optimum[encoder._optimumCurrentIndex].posPrev - encoder._optimumCurrentIndex;
761
- encoder.backRes = encoder._optimum[encoder._optimumCurrentIndex].backPrev;
762
- encoder._optimumCurrentIndex = encoder._optimum[encoder._optimumCurrentIndex].posPrev;
763
- return lenRes;
764
- }
765
- encoder._optimumCurrentIndex = encoder._optimumEndIndex = 0;
766
- if (encoder._longestMatchWasFound) {
767
- lenMain = encoder._longestMatchLength;
768
- encoder._longestMatchWasFound = 0;
769
- }
770
- else {
771
- lenMain = this.#ReadMatchDistances();
772
- }
773
- numDistancePairs = encoder._numDistancePairs;
774
- numAvailableBytes = this.#lzInWindow.getNumAvailableBytes() + 1;
775
- if (numAvailableBytes < 2) {
776
- encoder.backRes = -1;
777
- return 1;
778
- }
779
- if (numAvailableBytes > 0x0111) {
780
- numAvailableBytes = 0x0111;
781
- }
782
- repMaxIndex = 0;
783
- for (let i = 0; i < 4; ++i) {
784
- encoder.reps[i] = encoder._repDistances[i];
785
- encoder.repLens[i] = this.#lzInWindow.getMatchLen(-1, encoder.reps[i], 0x0111);
786
- if (encoder.repLens[i] > encoder.repLens[repMaxIndex]) {
787
- repMaxIndex = i;
788
- }
789
- }
790
- if (encoder.repLens[repMaxIndex] >= encoder._numFastBytes) {
791
- encoder.backRes = repMaxIndex;
792
- lenRes = encoder.repLens[repMaxIndex];
793
- this.#MovePos(lenRes - 1);
794
- return lenRes;
795
- }
796
- if (lenMain >= encoder._numFastBytes) {
797
- encoder.backRes = this.#compressor.chunker.encoder._matchDistances[numDistancePairs - 1] + 4;
798
- this.#MovePos(lenMain - 1);
799
- return lenMain;
800
- }
801
- currentByte = this.#lzInWindow.getIndexByte(-1);
802
- matchByte = this.#lzInWindow.getIndexByte(-encoder._repDistances[0] - 1 - 1);
803
- if (lenMain < 2 && currentByte != matchByte && encoder.repLens[repMaxIndex] < 2) {
804
- encoder.backRes = -1;
805
- return 1;
806
- }
807
- encoder._optimum[0].state = encoder._state;
808
- posState = position & encoder._posStateMask;
809
- encoder._optimum[1].price = PROB_PRICES[(encoder._isMatch[(encoder._state << 4) + posState]) >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
810
- this.#MakeAsChar(encoder._optimum[1]);
811
- matchPrice = PROB_PRICES[(2048 - encoder._isMatch[(encoder._state << 4) + posState])
812
- >>> 2];
813
- repMatchPrice = matchPrice + PROB_PRICES[(2048 - encoder._isRep[encoder._state]) >>> 2];
814
- if (matchByte == currentByte) {
815
- shortRepPrice = repMatchPrice + this.#GetRepLen1Price(posState);
816
- if (shortRepPrice < encoder._optimum[1].price) {
817
- encoder._optimum[1].price = shortRepPrice;
818
- this.#MakeAsShortRep(encoder._optimum[1]);
819
- }
820
- }
821
- lenEnd = lenMain >= encoder.repLens[repMaxIndex]
822
- ? lenMain
823
- : encoder.repLens[repMaxIndex];
824
- if (lenEnd < 2) {
825
- encoder.backRes = encoder._optimum[1].backPrev;
826
- return 1;
827
- }
828
- encoder._optimum[1].posPrev = 0;
829
- encoder._optimum[0].backs0 = encoder.reps[0];
830
- encoder._optimum[0].backs1 = encoder.reps[1];
831
- encoder._optimum[0].backs2 = encoder.reps[2];
832
- encoder._optimum[0].backs3 = encoder.reps[3];
833
- len = lenEnd;
834
- do {
835
- encoder._optimum[len].price = INFINITY_PRICE;
836
- len -= 1;
837
- } while (len >= 2);
838
- for (let i = 0; i < 4; ++i) {
839
- repLen = encoder.repLens[i];
840
- if (repLen < 2) {
841
- continue;
842
- }
843
- price_4 = repMatchPrice + this.#GetPureRepPrice(i, encoder._state, posState);
844
- do {
845
- curAndLenPrice = price_4 + encoder._repMatchLenEncoder.getPrice(repLen - 2, posState);
846
- optimum = encoder._optimum[repLen];
847
- if (curAndLenPrice < optimum.price) {
848
- optimum.price = curAndLenPrice;
849
- optimum.posPrev = 0;
850
- optimum.backPrev = i;
851
- optimum.prev1IsChar = 0;
852
- }
853
- } while ((repLen -= 1) >= 2);
854
- }
855
- normalMatchPrice = matchPrice
856
- + PROB_PRICES[(encoder._isRep[encoder._state]) >>> 2];
857
- len = encoder.repLens[0] >= 2 ? encoder.repLens[0] + 1 : 2;
858
- if (len <= lenMain) {
859
- offs = 0;
860
- while (len > encoder._matchDistances[offs]) {
861
- offs += 2;
862
- }
863
- for (;; len += 1) {
864
- distance = encoder._matchDistances[offs + 1];
865
- curAndLenPrice = normalMatchPrice + this.#LZMA_Encoder_GetPosLenPrice(distance, len, posState);
866
- optimum = encoder._optimum[len];
867
- if (curAndLenPrice < optimum.price) {
868
- optimum.price = curAndLenPrice;
869
- optimum.posPrev = 0;
870
- optimum.backPrev = distance + 4;
871
- optimum.prev1IsChar = 0;
872
- }
873
- if (len == encoder._matchDistances[offs]) {
874
- offs += 2;
875
- if (offs == numDistancePairs) {
876
- break;
877
- }
878
- }
879
- }
47
+ #encodeString(inputString) {
48
+ const l = inputString.length;
49
+ const chars = [];
50
+ for (let i = 0; i < l; ++i) {
51
+ chars[i] = inputString.charCodeAt(i);
880
52
  }
881
- cur = 0;
882
- while (1) {
883
- ++cur;
884
- if (cur == lenEnd) {
885
- return this.#Backward(cur);
886
- }
887
- newLen = this.#ReadMatchDistances();
888
- numDistancePairs = encoder._numDistancePairs;
889
- if (newLen >= encoder._numFastBytes) {
890
- encoder._longestMatchLength = newLen;
891
- encoder._longestMatchWasFound = 0x01;
892
- return this.#Backward(cur);
893
- }
894
- position += 0x01;
895
- posPrev = encoder._optimum[cur].posPrev;
896
- if (encoder._optimum[cur].prev1IsChar) {
897
- posPrev -= 0x01;
898
- if (encoder._optimum[cur].prev2) {
899
- state = encoder._optimum[encoder._optimum[cur].posPrev2].state;
900
- if (encoder._optimum[cur].backPrev2 < 0x04) {
901
- state = (state < 0x07) ? 0x08 : 0x0B;
902
- }
903
- else {
904
- state = (state < 0x07) ? 0x07 : 0x0A;
905
- }
906
- }
907
- else {
908
- state = encoder._optimum[posPrev].state;
909
- }
910
- state = stateUpdateChar(state);
911
- }
912
- else {
913
- state = encoder._optimum[posPrev].state;
914
- }
915
- if (posPrev == cur - 1) {
916
- if (!encoder._optimum[cur].backPrev) {
917
- state = state < 7 ? 9 : 11;
918
- }
919
- else {
920
- state = stateUpdateChar(state);
921
- }
922
- }
923
- else {
924
- if (encoder._optimum[cur].prev1IsChar
925
- && encoder._optimum[cur].prev2) {
926
- posPrev = encoder._optimum[cur].posPrev2;
927
- pos = encoder._optimum[cur].backPrev2;
928
- state = state < 0x07 ? 0x08 : 0x0B;
929
- }
930
- else {
931
- pos = encoder._optimum[cur].backPrev;
932
- if (pos < 4) {
933
- state = state < 0x07 ? 0x08 : 0x0B;
934
- }
935
- else {
936
- state = state < 0x07 ? 0x07 : 0x0A;
937
- }
938
- }
939
- opt = encoder._optimum[posPrev];
940
- if (pos < 4) {
941
- if (!pos) {
942
- encoder.reps[0] = opt.backs0;
943
- encoder.reps[1] = opt.backs1;
944
- encoder.reps[2] = opt.backs2;
945
- encoder.reps[3] = opt.backs3;
946
- }
947
- else if (pos == 1) {
948
- encoder.reps[0] = opt.backs1;
949
- encoder.reps[1] = opt.backs0;
950
- encoder.reps[2] = opt.backs2;
951
- encoder.reps[3] = opt.backs3;
952
- }
953
- else if (pos == 2) {
954
- encoder.reps[0] = opt.backs2;
955
- encoder.reps[1] = opt.backs0;
956
- encoder.reps[2] = opt.backs1;
957
- encoder.reps[3] = opt.backs3;
958
- }
959
- else {
960
- encoder.reps[0] = opt.backs3;
961
- encoder.reps[1] = opt.backs0;
962
- encoder.reps[2] = opt.backs1;
963
- encoder.reps[3] = opt.backs2;
964
- }
965
- }
966
- else {
967
- encoder.reps[0] = pos - 4;
968
- encoder.reps[1] = opt.backs0;
969
- encoder.reps[2] = opt.backs1;
970
- encoder.reps[3] = opt.backs2;
971
- }
972
- }
973
- encoder._optimum[cur].state = state;
974
- encoder._optimum[cur].backs0 = encoder.reps[0];
975
- encoder._optimum[cur].backs1 = encoder.reps[1];
976
- encoder._optimum[cur].backs2 = encoder.reps[2];
977
- encoder._optimum[cur].backs3 = encoder.reps[3];
978
- curPrice = encoder._optimum[cur].price;
979
- currentByte = this.#lzInWindow.getIndexByte(-0x01);
980
- matchByte = this.#lzInWindow.getIndexByte(-encoder.reps[0] - 1 - 1);
981
- posState = position & encoder._posStateMask;
982
- curAnd1Price = curPrice
983
- + PROB_PRICES[(encoder._isMatch[(state << 0x04) + posState]) >>> 2]
984
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, this.#lzInWindow.getIndexByte(-2)), state >= 7, matchByte, currentByte);
985
- nextOptimum = encoder._optimum[cur + 1];
986
- nextIsChar = 0;
987
- if (curAnd1Price < nextOptimum.price) {
988
- nextOptimum.price = curAnd1Price;
989
- nextOptimum.posPrev = cur;
990
- nextOptimum.backPrev = -0x01;
991
- nextOptimum.prev1IsChar = 0;
992
- nextIsChar = 1;
993
- }
994
- matchPrice = curPrice + PROB_PRICES[(2048 - encoder._isMatch[(state << 4) + posState]) >>> 2];
995
- repMatchPrice = matchPrice + PROB_PRICES[(2048 - encoder._isRep[state]) >>> 2];
996
- if (matchByte == currentByte && !(nextOptimum.posPrev < cur && !nextOptimum.backPrev)) {
997
- shortRepPrice = repMatchPrice
998
- + (PROB_PRICES[(encoder._isRepG0[state]) >>> 0x02] + PROB_PRICES[(encoder._isRep0Long[(state << 0x04) + posState]) >>> 0x02]);
999
- if (shortRepPrice <= nextOptimum.price) {
1000
- nextOptimum.price = shortRepPrice;
1001
- nextOptimum.posPrev = cur;
1002
- nextOptimum.backPrev = 0;
1003
- nextOptimum.prev1IsChar = 0;
1004
- nextIsChar = 1;
1005
- }
1006
- }
1007
- numAvailableBytesFull = this.#lzInWindow.getNumAvailableBytes() + 1;
1008
- numAvailableBytesFull = 0xFFF - cur < numAvailableBytesFull
1009
- ? 0xFFF - cur
1010
- : numAvailableBytesFull;
1011
- numAvailableBytes = numAvailableBytesFull;
1012
- if (numAvailableBytes < 2) {
1013
- continue;
1014
- }
1015
- if (numAvailableBytes > encoder._numFastBytes) {
1016
- numAvailableBytes = encoder._numFastBytes;
1017
- }
1018
- if (!nextIsChar && matchByte != currentByte) {
1019
- t = Math.min(numAvailableBytesFull - 1, encoder._numFastBytes);
1020
- lenTest2 = this.#lzInWindow.getMatchLen(0, encoder.reps[0], t);
1021
- if (lenTest2 >= 2) {
1022
- state2 = stateUpdateChar(state);
1023
- posStateNext = position + 1 & encoder._posStateMask;
1024
- nextRepMatchPrice = curAnd1Price
1025
- + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1026
- + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1027
- offset = cur + 1 + lenTest2;
1028
- while (lenEnd < offset) {
1029
- encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1030
- }
1031
- curAndLenPrice = nextRepMatchPrice + (price = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext),
1032
- price + this.#GetPureRepPrice(0, state2, posStateNext));
1033
- optimum = encoder._optimum[offset];
1034
- if (curAndLenPrice < optimum.price) {
1035
- optimum.price = curAndLenPrice;
1036
- optimum.posPrev = cur + 1;
1037
- optimum.backPrev = 0;
1038
- optimum.prev1IsChar = 1;
1039
- optimum.prev2 = 0;
1040
- }
1041
- }
1042
- }
1043
- startLen = 0x02;
1044
- for (repIndex = 0; repIndex < 4; ++repIndex) {
1045
- lenTest = this.#lzInWindow.getMatchLen(-0x01, encoder.reps[repIndex], numAvailableBytes);
1046
- if (lenTest < 2) {
1047
- continue;
1048
- }
1049
- lenTestTemp = lenTest;
1050
- do {
1051
- while (lenEnd < cur + lenTest) {
1052
- encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1053
- }
1054
- curAndLenPrice = repMatchPrice + (price_0 = encoder._repMatchLenEncoder.getPrice(lenTest - 2, posState),
1055
- price_0 + this.#GetPureRepPrice(repIndex, state, posState));
1056
- optimum = encoder._optimum[cur + lenTest];
1057
- if (curAndLenPrice < optimum.price) {
1058
- optimum.price = curAndLenPrice;
1059
- optimum.posPrev = cur;
1060
- optimum.backPrev = repIndex;
1061
- optimum.prev1IsChar = 0;
1062
- }
1063
- } while ((lenTest -= 1) >= 2);
1064
- lenTest = lenTestTemp;
1065
- if (!repIndex) {
1066
- startLen = lenTest + 1;
1067
- }
1068
- if (lenTest < numAvailableBytesFull) {
1069
- t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1070
- lenTest2 = this.#lzInWindow.getMatchLen(lenTest, encoder.reps[repIndex], t);
1071
- if (lenTest2 >= 2) {
1072
- state2 = state < 7 ? 0x08 : 11;
1073
- posStateNext = position + lenTest & encoder._posStateMask;
1074
- curAndLenCharPrice = repMatchPrice
1075
- + (price_1 = encoder._repMatchLenEncoder.getPrice(lenTest - 2, posState), price_1 + this.#GetPureRepPrice(repIndex, state, posState))
1076
- + PROB_PRICES[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1077
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#lzInWindow.getIndexByte(lenTest - 1 - 1)), true, this.#lzInWindow.getIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#lzInWindow.getIndexByte(lenTest - 1));
1078
- state2 = stateUpdateChar(state2);
1079
- posStateNext = position + lenTest + 1 & encoder._posStateMask;
1080
- nextMatchPrice = curAndLenCharPrice + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1081
- nextRepMatchPrice = nextMatchPrice + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1082
- offset = lenTest + 1 + lenTest2;
1083
- while (lenEnd < cur + offset) {
1084
- encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1085
- }
1086
- curAndLenPrice = nextRepMatchPrice + (price_2 = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext), price_2 + this.#GetPureRepPrice(0, state2, posStateNext));
1087
- optimum = encoder._optimum[cur + offset];
1088
- if (curAndLenPrice < optimum.price) {
1089
- optimum.price = curAndLenPrice;
1090
- optimum.posPrev = cur + lenTest + 1;
1091
- optimum.backPrev = 0;
1092
- optimum.prev1IsChar = 1;
1093
- optimum.prev2 = 1;
1094
- optimum.posPrev2 = cur;
1095
- optimum.backPrev2 = repIndex;
1096
- }
1097
- }
1098
- }
1099
- }
1100
- if (newLen > numAvailableBytes) {
1101
- newLen = numAvailableBytes;
1102
- for (numDistancePairs = 0; newLen > encoder._matchDistances[numDistancePairs]; numDistancePairs += 2) { }
1103
- encoder._matchDistances[numDistancePairs] = newLen;
1104
- numDistancePairs += 2;
1105
- }
1106
- if (newLen >= startLen) {
1107
- normalMatchPrice = matchPrice + PROB_PRICES[(encoder._isRep[state]) >>> 2];
1108
- while (lenEnd < cur + newLen) {
1109
- encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1110
- }
1111
- offs = 0;
1112
- while (startLen > encoder._matchDistances[offs]) {
1113
- offs += 2;
1114
- }
1115
- for (lenTest = startLen;; lenTest += 1) {
1116
- curBack = encoder._matchDistances[offs + 1];
1117
- curAndLenPrice = normalMatchPrice + this.#LZMA_Encoder_GetPosLenPrice(curBack, lenTest, posState);
1118
- optimum = encoder._optimum[cur + lenTest];
1119
- if (curAndLenPrice < optimum.price) {
1120
- optimum.price = curAndLenPrice;
1121
- optimum.posPrev = cur;
1122
- optimum.backPrev = curBack + 4;
1123
- optimum.prev1IsChar = 0;
1124
- }
1125
- if (lenTest == encoder._matchDistances[offs]) {
1126
- if (lenTest < numAvailableBytesFull) {
1127
- t = Math.min(numAvailableBytesFull - 1 - lenTest, encoder._numFastBytes);
1128
- lenTest2 = this.#lzInWindow.getMatchLen(lenTest, curBack, t);
1129
- if (lenTest2 >= 2) {
1130
- state2 = state < 7 ? 7 : 10;
1131
- posStateNext = position + lenTest & encoder._posStateMask;
1132
- curAndLenCharPrice = curAndLenPrice
1133
- + PROB_PRICES[(encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2]
1134
- + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#lzInWindow.getIndexByte(lenTest - 1 - 1)), true, this.#lzInWindow.getIndexByte(lenTest - (curBack + 1) - 1), this.#lzInWindow.getIndexByte(lenTest - 1));
1135
- state2 = stateUpdateChar(state2);
1136
- posStateNext = position + lenTest + 1 & encoder._posStateMask;
1137
- nextMatchPrice = curAndLenCharPrice + PROB_PRICES[(2048 - encoder._isMatch[(state2 << 4) + posStateNext]) >>> 2];
1138
- nextRepMatchPrice = nextMatchPrice + PROB_PRICES[(2048 - encoder._isRep[state2]) >>> 2];
1139
- offset = lenTest + 1 + lenTest2;
1140
- while (lenEnd < cur + offset) {
1141
- encoder._optimum[lenEnd += 1].price = INFINITY_PRICE;
1142
- }
1143
- curAndLenPrice = nextRepMatchPrice + (price_3 = encoder._repMatchLenEncoder.getPrice(lenTest2 - 2, posStateNext), price_3 + this.#GetPureRepPrice(0, state2, posStateNext));
1144
- optimum = encoder._optimum[cur + offset];
1145
- if (curAndLenPrice < optimum.price) {
1146
- optimum.price = curAndLenPrice;
1147
- optimum.posPrev = cur + lenTest + 1;
1148
- optimum.backPrev = 0;
1149
- optimum.prev1IsChar = 1;
1150
- optimum.prev2 = 1;
1151
- optimum.posPrev2 = cur;
1152
- optimum.backPrev2 = curBack + 4;
1153
- }
1154
- }
1155
- }
1156
- offs += 2;
1157
- if (offs == numDistancePairs) {
1158
- break;
1159
- }
1160
- }
1161
- }
53
+ const data = [];
54
+ let elen = 0;
55
+ for (let i = 0; i < l; ++i) {
56
+ const ch = chars[i];
57
+ if (ch >= 1 && ch <= 0x7F) {
58
+ data[elen++] = ch << 24 >> 24;
1162
59
  }
1163
- }
1164
- // Fallback return - should not be reached in normal execution
1165
- return 1;
1166
- }
1167
- #LZMA_Encoder_GetPosLenPrice(pos, len, posState) {
1168
- const encoder = this.#compressor.chunker.encoder;
1169
- let price, lenToPosState = getLenToPosState(len);
1170
- if (pos < 128) {
1171
- price = encoder._distancesPrices[lenToPosState * 128 + pos];
1172
- }
1173
- else {
1174
- const position = (lenToPosState << 6) + this.GetPosSlot2(pos);
1175
- price = encoder._posSlotPrices[position] + encoder._alignPrices[pos & 15];
1176
- }
1177
- return price + encoder._lenEncoder.getPrice(len - 2, posState);
1178
- }
1179
- #GetPureRepPrice(repIndex, state, posState) {
1180
- const encoder = this.#compressor.chunker.encoder;
1181
- let price;
1182
- if (!repIndex) {
1183
- price = PROB_PRICES[(encoder._isRepG0[state]) >>> 2];
1184
- price += PROB_PRICES[0x800 - this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
1185
- }
1186
- else {
1187
- price = PROB_PRICES[(0x800 - this.#compressor.chunker.encoder._isRepG0[state]) >>> 2];
1188
- if (repIndex == 1) {
1189
- price += PROB_PRICES[(this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
60
+ else if (!ch || ch >= 0x80 && ch <= 0x7FF) {
61
+ data[elen++] = (0xC0 | ch >> 6 & 0x1F) << 24 >> 24;
62
+ data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
1190
63
  }
1191
64
  else {
1192
- price += PROB_PRICES[(0x800 - this.#compressor.chunker.encoder._isRepG1[state]) >>> 2];
1193
- price += getBitPrice(this.#compressor.chunker.encoder._isRepG2[state], repIndex - 2);
1194
- }
1195
- }
1196
- return price;
1197
- }
1198
- #GetRepLen1Price(posState) {
1199
- const encoder = this.#compressor.chunker.encoder;
1200
- const repG0Price = PROB_PRICES[(encoder._isRepG0[encoder._state]) >>> 2];
1201
- const rep0LongPrice = PROB_PRICES[encoder._isRep0Long[(encoder._state << 4) + posState] >>> 2];
1202
- return repG0Price + rep0LongPrice;
1203
- }
1204
- #MovePos(num) {
1205
- if (num > 0) {
1206
- this.#Skip(num);
1207
- this.#compressor.chunker.encoder._additionalOffset += num;
1208
- }
1209
- }
1210
- #ReadMatchDistances() {
1211
- let lenRes = 0;
1212
- const encoder = this.#compressor.chunker.encoder;
1213
- encoder._numDistancePairs = this.#GetMatches();
1214
- if (encoder._numDistancePairs > 0) {
1215
- lenRes = encoder._matchDistances[encoder._numDistancePairs - 2];
1216
- if (lenRes == encoder._numFastBytes) {
1217
- lenRes += this.#lzInWindow.getMatchLen(lenRes - 1, encoder._matchDistances[encoder._numDistancePairs - 1], 0x0111 - lenRes);
1218
- }
1219
- }
1220
- encoder._additionalOffset += 1;
1221
- return lenRes;
1222
- }
1223
- #ReleaseMFStream() {
1224
- const encoder = this.#compressor.chunker.encoder;
1225
- if (encoder._matchFinder && encoder._needReleaseMFStream) {
1226
- encoder._matchFinder._stream = null;
1227
- encoder._needReleaseMFStream = 0;
1228
- }
1229
- }
1230
- #ReleaseStreams() {
1231
- this.#ReleaseMFStream();
1232
- this.#compressor.chunker.encoder._rangeEncoder.stream = null;
1233
- }
1234
- writeHeaderProperties() {
1235
- const HEADER_SIZE = 0x5; // Total header size in bytes
1236
- // First byte combines posStateBits, literalPosStateBits and literalContextBits
1237
- // Format: (posStateBits * 5 + literalPosStateBits) * 9 + literalContextBits
1238
- this.#encoder.properties[0] = ((this.#encoder._posStateBits * 5 + this.#encoder._numLiteralPosStateBits) * 9 + this.#encoder._numLiteralContextBits) & 0xFF; // Ensure byte-sized value
1239
- // Next 4 bytes store dictionary size in little-endian format
1240
- for (let byteIndex = 0; byteIndex < 4; byteIndex++) {
1241
- // Shift dictionary size right by appropriate number of bits and mask to byte
1242
- this.#encoder.properties[1 + byteIndex] = (this.#encoder._dictionarySize >> (0x08 * byteIndex)) & 0xFF;
1243
- }
1244
- this.#write_0(this.#compressor.output, this.#encoder.properties, 0, HEADER_SIZE);
1245
- }
1246
- GetPosSlot2(pos) {
1247
- if (pos < 0x20000) {
1248
- return G_FAST_POS[pos >> 6] + 12;
1249
- }
1250
- if (pos < 0x8000000) {
1251
- return G_FAST_POS[pos >> 16] + 32;
1252
- }
1253
- return G_FAST_POS[pos >> 26] + 52;
1254
- }
1255
- #LZMA_Encoder_GetSubCoder(pos, prevByte) {
1256
- const subCoder = this.#compressor.chunker.encoder._literalEncoder.getSubCoder(pos, prevByte);
1257
- return { decoders: subCoder.decoders };
1258
- }
1259
- #RangeCoder_Encoder_GetPrice_0(encoder, matchMode, matchByte, symbol) {
1260
- let bit, context = 1, i = 7, matchBit, price = 0;
1261
- if (matchMode) {
1262
- for (; i >= 0; --i) {
1263
- matchBit = (matchByte >> i) & 1;
1264
- bit = (symbol >> i) & 1;
1265
- price += getBitPrice(encoder.decoders[((1 + matchBit) << 8) + context], bit);
1266
- context = context << 1 | bit;
1267
- if (matchBit != bit) {
1268
- --i;
1269
- break;
1270
- }
65
+ data[elen++] = (0xE0 | ch >> 12 & 0x0F) << 24 >> 24;
66
+ data[elen++] = (0x80 | ch >> 6 & 0x3F) << 24 >> 24;
67
+ data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
1271
68
  }
1272
69
  }
1273
- for (; i >= 0; --i) {
1274
- bit = symbol >> i & 1;
1275
- price += getBitPrice(encoder.decoders[context], bit);
1276
- context = context << 1 | bit;
1277
- }
1278
- return price;
1279
- }
1280
- #MakeAsChar(optimum) {
1281
- optimum.backPrev = -1;
1282
- optimum.prev1IsChar = 0;
1283
- }
1284
- #MakeAsShortRep(optimum) {
1285
- optimum.backPrev = 0;
1286
- optimum.prev1IsChar = 0;
1287
- }
1288
- ReverseEncode(startIndex, NumBitLevels, symbol) {
1289
- let bit, m = 1;
1290
- for (let i = 0; i < NumBitLevels; ++i) {
1291
- bit = symbol & 1;
1292
- this.#compressor.chunker.encoder.encodeBit(this.#compressor.chunker.encoder._posEncoders, startIndex + m, bit);
1293
- m = m << 1 | bit;
1294
- symbol >>= 1;
1295
- }
1296
- }
1297
- #ReverseGetPrice(encoder, symbol) {
1298
- let bit, m = 1, price = 0;
1299
- for (let i = encoder.numBitLevels; i != 0; i -= 1) {
1300
- bit = symbol & 1;
1301
- symbol >>>= 1;
1302
- price += getBitPrice(encoder.models[m], bit);
1303
- m = m << 1 | bit;
1304
- }
1305
- return price;
1306
- }
1307
- #GetProcessedSizeAdd() {
1308
- const processedCacheSize = add64(fromInt64(this.#compressor.chunker.encoder._rangeEncoder.cacheSize), this.#compressor.chunker.encoder._rangeEncoder.position);
1309
- return add64(processedCacheSize, [4, 0]);
70
+ return data;
1310
71
  }
1311
- #decodeString(utf) {
72
+ #decodeUTF8(utf) {
1312
73
  let j = 0, x, y, z, l = utf.length, buf = [], charCodes = [];
1313
74
  for (let i = 0; i < l; ++i, ++j) {
1314
75
  x = utf[i] & 0xFF;
1315
76
  if (!(x & 0x80)) {
1316
- if (!x) {
1317
- // It appears that this is binary data, so it cannot be
1318
- // converted to a string, so just send it back.
77
+ if (!x)
1319
78
  return utf;
1320
- }
1321
79
  charCodes[j] = x;
1322
80
  }
1323
81
  else if ((x & 0xE0) == 0xC0) {
1324
- if (i + 1 >= l) {
1325
- // It appears that this is binary data, so it cannot be
1326
- // converted to a string, so just send it back.
82
+ if (i + 1 >= l)
1327
83
  return String.fromCharCode(...utf);
1328
- }
1329
84
  y = utf[++i] & 0xFF;
1330
- if ((y & 0xC0) != 0x80) {
1331
- // It appears that this is binary data, so it cannot be
1332
- // converted to a string, so just send it back.
85
+ if ((y & 0xC0) != 0x80)
1333
86
  return String.fromCharCode(...utf);
1334
- }
1335
87
  charCodes[j] = ((x & 0x1F) << 6) | (y & 0x3F);
1336
88
  }
1337
89
  else if ((x & 0xF0) == 0xE0) {
1338
- if (i + 2 >= l) {
1339
- // It appears that this is binary data, so it cannot be
1340
- // converted to a string, so just send it back.
90
+ if (i + 2 >= l)
1341
91
  return utf;
1342
- }
1343
92
  y = utf[++i] & 0xFF;
1344
- if ((y & 0xC0) != 0x80) {
1345
- // It appears that this is binary data, so it cannot be converted to
1346
- // a string, so just send it back.
93
+ if ((y & 0xC0) != 0x80)
1347
94
  return utf;
1348
- }
1349
95
  z = utf[++i] & 0xFF;
1350
- if ((z & 0xC0) != 0x80) {
1351
- // It appears that this is binary data, so it cannot be converted to
1352
- // a string, so just send it back.
96
+ if ((z & 0xC0) != 0x80)
1353
97
  return utf;
1354
- }
1355
98
  charCodes[j] = ((x & 0x0F) << 0x0C) | ((y & 0x3F) << 6) | (z & 0x3F);
1356
99
  }
1357
100
  else {
1358
- // It appears that this is binary data, so it cannot be converted to
1359
- // a string, so just send it back.
1360
101
  return utf;
1361
102
  }
1362
103
  if (j == 0x3FFF) {
@@ -1370,78 +111,4 @@ export class LZMA {
1370
111
  }
1371
112
  return buf.join("");
1372
113
  }
1373
- encodeString(inputString) {
1374
- let ch, chars = [], elen = 0, l = inputString.length;
1375
- this.#getChars(inputString, 0, l, chars, 0);
1376
- // Add extra spaces in the array to break up the unicode symbols.
1377
- for (let i = 0; i < l; ++i) {
1378
- ch = chars[i];
1379
- if (ch >= 1 && ch <= 127) {
1380
- ++elen;
1381
- }
1382
- else if (!ch || ch >= 128 && ch <= 2047) {
1383
- elen += 2;
1384
- }
1385
- else {
1386
- elen += 3;
1387
- }
1388
- }
1389
- const data = [];
1390
- elen = 0;
1391
- for (let i = 0; i < l; ++i) {
1392
- ch = chars[i];
1393
- if (ch >= 1 && ch <= 0x7F) { // 127
1394
- data[elen++] = ch << 24 >> 24;
1395
- }
1396
- else if (!ch || ch >= 0x80 && ch <= 0x7FF) {
1397
- data[elen++] = (0xC0 | ch >> 6 & 0x1F) << 24 >> 24;
1398
- data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
1399
- }
1400
- else {
1401
- data[elen++] = (0xE0 | ch >> 12 & 0x0F) << 24 >> 24;
1402
- data[elen++] = (0x80 | ch >> 6 & 0x3F) << 24 >> 24;
1403
- data[elen++] = (0x80 | ch & 0x3F) << 24 >> 24;
1404
- }
1405
- }
1406
- return data;
1407
- }
1408
- compress(data, mode = 5) {
1409
- const compressionMode = MODES[mode];
1410
- this.#byteArrayCompressor(data, compressionMode);
1411
- while (this.#compressor.chunker.processChunk())
1412
- ;
1413
- const result = this.#toByteArray(this.#compressor.output);
1414
- return new Int8Array(result);
1415
- }
1416
- compressString(data, mode = 5) {
1417
- const encodedData = this.encodeString(data);
1418
- return this.compress(new Uint8Array(encodedData), mode);
1419
- }
1420
- decompress(bytearray) {
1421
- this.#byteArrayDecompressor(bytearray);
1422
- while (this.#decompressor.chunker.processChunk())
1423
- ;
1424
- return this.#toByteArray(this.#decompressor.output);
1425
- }
1426
- decompressString(bytearray) {
1427
- this.#byteArrayDecompressor(bytearray);
1428
- while (this.#decompressor.chunker.processChunk())
1429
- ;
1430
- const decodedByteArray = this.#toByteArray(this.#decompressor.output);
1431
- const result = this.#decodeString(decodedByteArray);
1432
- if (typeof result === "string") {
1433
- return result;
1434
- }
1435
- else {
1436
- // If decoding failed and returned binary data, convert to string anyway
1437
- return String.fromCharCode(...result);
1438
- }
1439
- }
1440
- // Public methods for chunker access
1441
- codeOneBlock() {
1442
- this.#CodeOneBlock();
1443
- }
1444
- releaseStreams() {
1445
- this.#ReleaseStreams();
1446
- }
1447
114
  }