lzma1 0.0.4 → 0.5.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/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # lzma1
2
2
 
3
- This is a simplified [fork][fork-link] of [Nathan Rugg's][fork-author] package.
3
+ This is a [fork][fork-link] of [Nathan Rugg's][fork-author] package that adds
4
+ types and makes the logic more structured and readable.
4
5
 
5
6
  [fork-link]: https://github.com/LZMA-JS/LZMA-JS
6
7
  [fork-author]: https://github.com/nmrugg
@@ -64,8 +65,16 @@ const decompressed = decompress(result);
64
65
  // data === decompressed
65
66
  ```
66
67
 
68
+ ## LZMA header
69
+
70
+ More [information][header_link] about the LZMA header.
71
+
72
+ ![lzma](./docs/lzma.svg)
73
+
67
74
  ## Related
68
75
 
69
76
  - <https://github.com/cscott/lzma-purejs>
70
77
  - <https://github.com/glinscott/lzmajs>
71
78
  - <https://github.com/mauron85/lzma-purejs/tree/master>
79
+
80
+ [header_link]: https://docs.fileformat.com/compression/lzma/#lzma-header
package/dist/lzma.d.ts CHANGED
@@ -47,6 +47,7 @@ export declare class LZMA {
47
47
  readonly modeIndex: 1;
48
48
  };
49
49
  };
50
+ readonly bitMaskForRange = -16777216;
50
51
  constructor();
51
52
  GetLenToPosState(len: number): number;
52
53
  StateUpdateChar(index: number): number;
package/dist/lzma.js CHANGED
@@ -55,10 +55,16 @@ const CRC32_TABLE = [
55
55
  0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
56
56
  ];
57
57
  export class LZMA {
58
- #MAX_UINT32 = 4_294_967_296;
59
- #MAX_INT32 = 2_147_483_647;
60
- #MIN_INT32 = -2_147_483_648;
61
- #MAX_COMPRESSION_SIZE = 9_223_372_036_854_775_807;
58
+ #MAX_UINT32 = 0x100000000; // 2^32
59
+ #_MAX_UINT32 = 0xFFFFFFFF; // 2^32 - 1
60
+ #MAX_INT32 = 0x7FFFFFFF; // 2^31 - 1
61
+ #MIN_INT32 = -0x80000000; // -2^31
62
+ #MAX_UINT64 = 0x10000000000000000; // 2^64
63
+ #MAX_INT64 = 0x7FFFFFFFFFFFFFFF; // 2^63
64
+ #MIN_INT64 = -0x8000000000000000; // -2^63
65
+ #MAX_COMPRESSION_SIZE = 0x7FFFFFFFFFFFFFFF; // 2^63 - 1
66
+ #kIfinityPrice = 0xFFFFFFF; // 2^28 - 1
67
+ #dictionarySizeThreshold = 0x3FFFFFFF;
62
68
  #N1_LONG_LIT;
63
69
  #MIN_VALUE;
64
70
  #P0_LONG_LIT = [0, 0];
@@ -80,9 +86,10 @@ export class LZMA {
80
86
  #gFastPos;
81
87
  #compressor;
82
88
  #decompressor;
89
+ bitMaskForRange = -16777216;
83
90
  constructor() {
84
- this.#N1_LONG_LIT = [4294967295, -this.#MAX_UINT32];
85
- this.#MIN_VALUE = [0, -9223372036854775808];
91
+ this.#N1_LONG_LIT = [this.#_MAX_UINT32, -this.#MAX_UINT32];
92
+ this.#MIN_VALUE = [0, this.#MIN_INT64];
86
93
  this.#encoder = this.#initEncoder();
87
94
  this.#decoder = this.#initDecoder();
88
95
  this.#probPrices = this.#createProbPrices();
@@ -146,8 +153,8 @@ export class LZMA {
146
153
  m_PosSlotDecoder: this.#initArray(4),
147
154
  m_PosDecoders: this.#initArray(114),
148
155
  m_PosAlignDecoder: this.#createBitTreeDecoder(4),
149
- m_LenDecoder: this.#createLenDecoder({}),
150
- m_RepLenDecoder: this.#createLenDecoder({}),
156
+ m_LenDecoder: this.#createLenDecoder(),
157
+ m_RepLenDecoder: this.#createLenDecoder(),
151
158
  m_LiteralDecoder: {},
152
159
  };
153
160
  for (let i = 0; i < 4; ++i) {
@@ -161,7 +168,7 @@ export class LZMA {
161
168
  alive: 0,
162
169
  encoder: null,
163
170
  decoder: null,
164
- inBytesProcessed: [],
171
+ inBytesProcessed: [0, 0],
165
172
  },
166
173
  output: {
167
174
  buf: this.#initArray(32),
@@ -175,7 +182,7 @@ export class LZMA {
175
182
  alive: 0,
176
183
  encoder: null,
177
184
  decoder: null,
178
- inBytesProcessed: [],
185
+ inBytesProcessed: [0, 0],
179
186
  },
180
187
  output: {
181
188
  buf: this.#initArray(32),
@@ -244,8 +251,8 @@ export class LZMA {
244
251
  }
245
252
  #create(valueLow, valueHigh) {
246
253
  let diffHigh, diffLow;
247
- valueHigh %= 1.8446744073709552E19;
248
- valueLow %= 1.8446744073709552E19;
254
+ valueHigh %= this.#MAX_UINT64;
255
+ valueLow %= this.#MAX_UINT64;
249
256
  diffHigh = valueHigh % this.#MAX_UINT32;
250
257
  diffLow = Math.floor(valueLow / this.#MAX_UINT32) * this.#MAX_UINT32;
251
258
  valueHigh = valueHigh - diffHigh + diffLow;
@@ -254,16 +261,16 @@ export class LZMA {
254
261
  valueLow += this.#MAX_UINT32;
255
262
  valueHigh -= this.#MAX_UINT32;
256
263
  }
257
- while (valueLow > 4294967295) {
264
+ while (valueLow > this.#MAX_UINT32) {
258
265
  valueLow -= this.#MAX_UINT32;
259
266
  valueHigh += this.#MAX_UINT32;
260
267
  }
261
- valueHigh = valueHigh % 1.8446744073709552E19;
262
- while (valueHigh > 9223372032559808512) {
263
- valueHigh -= 1.8446744073709552E19;
268
+ valueHigh = valueHigh % this.#MAX_UINT64;
269
+ while (valueHigh > this.#MAX_INT64) {
270
+ valueHigh -= this.#MAX_UINT64;
264
271
  }
265
- while (valueHigh < -9223372036854775808) {
266
- valueHigh += 1.8446744073709552E19;
272
+ while (valueHigh < this.#MIN_INT64) {
273
+ valueHigh += this.#MAX_UINT64;
267
274
  }
268
275
  return [valueLow, valueHigh];
269
276
  }
@@ -303,13 +310,13 @@ export class LZMA {
303
310
  throw new Error("Neg");
304
311
  }
305
312
  twoToN = this.#pwrAsDouble(n);
306
- newHigh = a[1] * twoToN % 1.8446744073709552E19;
313
+ newHigh = a[1] * twoToN % this.#MAX_UINT64;
307
314
  newLow = a[0] * twoToN;
308
315
  diff = newLow - newLow % this.#MAX_UINT32;
309
316
  newHigh += diff;
310
317
  newLow -= diff;
311
318
  if (newHigh >= this.#MAX_COMPRESSION_SIZE) {
312
- newHigh -= 1.8446744073709552E19;
319
+ newHigh -= this.#MAX_UINT64;
313
320
  }
314
321
  return [newLow, newHigh];
315
322
  }
@@ -351,12 +358,12 @@ export class LZMA {
351
358
  data.length = output.count;
352
359
  return data;
353
360
  }
354
- #write(obj, b) {
355
- obj.buf[obj.count++] = b << 24 >> 24;
361
+ #write(buffer, b) {
362
+ buffer.buf[buffer.count++] = b & 0xFF;
356
363
  }
357
- #write_0(obj, buf, off, len) {
358
- this.#arraycopy(buf, off, obj.buf, obj.count, len);
359
- obj.count += len;
364
+ #write_0(buffer, buf, off, len) {
365
+ this.#arraycopy(buf, off, buffer.buf, buffer.count, len);
366
+ buffer.count += len;
360
367
  }
361
368
  #getChars(inputString, srcBegin, srcEnd, dst, dstBegin) {
362
369
  for (let srcIdx = srcBegin; srcIdx < srcEnd; ++srcIdx) {
@@ -409,9 +416,9 @@ export class LZMA {
409
416
  this.#FillAlignPrices(this.#encoder);
410
417
  // Configure length encoders
411
418
  this.#encoder._lenEncoder._tableSize = this.#encoder._numFastBytes + 1 - 2;
412
- this.#UpdateTables(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
419
+ this.#LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(this.#encoder._lenEncoder, 1 << this.#encoder._posStateBits);
413
420
  this.#encoder._repMatchLenEncoder._tableSize = this.#encoder._numFastBytes + 1 - 2;
414
- this.#UpdateTables(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
421
+ this.#LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(this.#encoder._repMatchLenEncoder, 1 << this.#encoder._posStateBits);
415
422
  // Reset position counter
416
423
  this.#encoder.nowPos64 = this.#P0_LONG_LIT;
417
424
  // Create new chunker with configured encoder
@@ -444,7 +451,7 @@ export class LZMA {
444
451
  if (r == -1) {
445
452
  throw new Error("truncated input");
446
453
  }
447
- properties[i] = r << 24 >> 24;
454
+ properties[i] = r & 0xFF;
448
455
  }
449
456
  if (!this.#SetDecoderProperties(properties)) {
450
457
  throw new Error("corrupted input");
@@ -474,7 +481,7 @@ export class LZMA {
474
481
  */
475
482
  tmp_length = parseInt(hex_length, 16);
476
483
  // If the length is too long to handle, just set it to unknown.
477
- if (tmp_length > 4294967295) {
484
+ if (tmp_length > this.#_MAX_UINT32) {
478
485
  this.#compressor.length_0 = this.#N1_LONG_LIT;
479
486
  }
480
487
  else {
@@ -513,26 +520,27 @@ export class LZMA {
513
520
  + index];
514
521
  }
515
522
  #GetMatchLen(index, distance, limit) {
516
- if (this.#compressor.chunker.encoder._matchFinder._streamEndWasReached) {
517
- if (this.#compressor.chunker.encoder._matchFinder._pos + index + limit
518
- > this.#compressor.chunker.encoder._matchFinder._streamPos) {
519
- limit = this.#compressor.chunker.encoder._matchFinder._streamPos
520
- - (this.#compressor.chunker.encoder._matchFinder._pos + index);
523
+ const encoder = this.#compressor.chunker.encoder;
524
+ if (encoder._matchFinder._streamEndWasReached) {
525
+ if (encoder._matchFinder._pos + index + limit
526
+ > encoder._matchFinder._streamPos) {
527
+ limit = encoder._matchFinder._streamPos
528
+ - (encoder._matchFinder._pos + index);
521
529
  }
522
530
  }
523
531
  ++distance;
524
- let i, pby = this.#compressor.chunker.encoder._matchFinder._bufferOffset
525
- + this.#compressor.chunker.encoder._matchFinder._pos
532
+ let i, pby = encoder._matchFinder._bufferOffset
533
+ + encoder._matchFinder._pos
526
534
  + index;
527
535
  for (i = 0; i < limit
528
- && this.#compressor.chunker.encoder._matchFinder._bufferBase[pby + i]
529
- == this.#compressor.chunker.encoder._matchFinder._bufferBase[pby + i - distance]; ++i)
536
+ && encoder._matchFinder._bufferBase[pby + i]
537
+ == encoder._matchFinder._bufferBase[pby + i - distance]; ++i)
530
538
  ;
531
539
  return i;
532
540
  }
533
541
  #GetNumAvailableBytes() {
534
- return (this.#compressor.chunker.encoder._matchFinder._streamPos
535
- - this.#compressor.chunker.encoder._matchFinder._pos);
542
+ const encoder = this.#compressor.chunker.encoder;
543
+ return encoder._matchFinder._streamPos - encoder._matchFinder._pos;
536
544
  }
537
545
  #MoveBlock() {
538
546
  const matchFinder = this.#compressor.chunker.encoder._matchFinder;
@@ -592,7 +600,7 @@ export class LZMA {
592
600
  this.#compressor.chunker.encoder._matchFinder._streamPos -= subValue;
593
601
  }
594
602
  #Create_3(keepAddBufferBefore, keepAddBufferAfter) {
595
- if (this.#encoder._dictionarySize < 1073741567) {
603
+ if (this.#encoder._dictionarySize < this.#dictionarySizeThreshold) {
596
604
  this.#encoder._matchFinder._cutValue = 16 + (this.#encoder._numFastBytes >> 1);
597
605
  const windowReservSize = ~~((this.#encoder._dictionarySize
598
606
  + keepAddBufferBefore
@@ -796,7 +804,7 @@ export class LZMA {
796
804
  }
797
805
  #Skip(num) {
798
806
  const matchFinder = this.#compressor.chunker.encoder._matchFinder;
799
- var count, cur, curMatch, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, pby1, ptr0, ptr1, temp;
807
+ let count, cur, curMatch, cyclicPos, delta, hash2Value, hash3Value, hashValue, len, len0, len1, lenLimit, matchMinPos, pby1, ptr0, ptr1, temp;
800
808
  do {
801
809
  if (matchFinder._pos + matchFinder._matchMaxLen <= matchFinder._streamPos) {
802
810
  lenLimit = matchFinder._matchMaxLen;
@@ -886,11 +894,11 @@ export class LZMA {
886
894
  outputWindow._pos += 1;
887
895
  pos += 1;
888
896
  if (outputWindow._pos >= outputWindow._windowSize) {
889
- this.#Flush_0(outputWindow);
897
+ this.#Flush_0();
890
898
  }
891
899
  }
892
900
  }
893
- #Create_5(m_OutWindow, windowSize) {
901
+ #OutWindow_Create(m_OutWindow, windowSize) {
894
902
  if (m_OutWindow._buffer == null || m_OutWindow._windowSize != windowSize) {
895
903
  m_OutWindow._buffer = this.#initArray(windowSize);
896
904
  }
@@ -898,35 +906,35 @@ export class LZMA {
898
906
  m_OutWindow._pos = 0;
899
907
  m_OutWindow._streamPos = 0;
900
908
  }
901
- #Flush_0(obj) {
902
- var size = obj._pos - obj._streamPos;
909
+ #Flush_0() {
910
+ let size = this.#decoder.m_OutWindow._pos - this.#decoder.m_OutWindow._streamPos;
903
911
  if (!size) {
904
912
  return;
905
913
  }
906
- this.#write_0(obj._stream, obj._buffer, obj._streamPos, size);
907
- if (obj._pos >= obj._windowSize) {
908
- obj._pos = 0;
914
+ this.#write_0(this.#decoder.m_OutWindow._stream, this.#decoder.m_OutWindow._buffer, this.#decoder.m_OutWindow._streamPos, size);
915
+ if (this.#decoder.m_OutWindow._pos >= this.#decoder.m_OutWindow._windowSize) {
916
+ this.#decoder.m_OutWindow._pos = 0;
909
917
  }
910
- obj._streamPos = obj._pos;
918
+ this.#decoder.m_OutWindow._streamPos = this.#decoder.m_OutWindow._pos;
911
919
  }
912
920
  #GetByte(distance) {
913
921
  const outputWindow = this.#decompressor.chunker.decoder.m_OutWindow;
914
- var pos = outputWindow._pos - distance - 1;
922
+ let pos = outputWindow._pos - distance - 1;
915
923
  if (pos < 0) {
916
924
  pos += outputWindow._windowSize;
917
925
  }
918
926
  return outputWindow._buffer[pos];
919
927
  }
920
- #PutByte(obj, b) {
921
- obj._buffer[obj._pos] = b;
922
- obj._pos += 1;
923
- if (obj._pos >= obj._windowSize) {
924
- this.#Flush_0(obj);
928
+ #PutByte(b) {
929
+ this.#decoder.m_OutWindow._buffer[this.#decoder.m_OutWindow._pos] = b;
930
+ this.#decoder.m_OutWindow._pos += 1;
931
+ if (this.#decoder.m_OutWindow._pos >= this.#decoder.m_OutWindow._windowSize) {
932
+ this.#Flush_0();
925
933
  }
926
934
  }
927
- #ReleaseStream(obj) {
928
- this.#Flush_0(obj);
929
- obj._stream = null;
935
+ #OutWindow_ReleaseStream() {
936
+ this.#Flush_0();
937
+ this.#decoder.m_OutWindow._stream = null;
930
938
  }
931
939
  GetLenToPosState(len) {
932
940
  len -= 2;
@@ -969,8 +977,8 @@ export class LZMA {
969
977
  if (result
970
978
  || this.#compare(decoder.outSize, this.#P0_LONG_LIT) >= 0
971
979
  && this.#compare(decoder.nowPos64, decoder.outSize) >= 0) {
972
- this.#Flush_0(decoder.m_OutWindow);
973
- this.#ReleaseStream(decoder.m_OutWindow);
980
+ this.#Flush_0();
981
+ this.#OutWindow_ReleaseStream();
974
982
  decoder.m_RangeDecoder.Stream = null;
975
983
  this.#decompressor.chunker.alive = 0;
976
984
  }
@@ -993,7 +1001,7 @@ export class LZMA {
993
1001
  }
994
1002
  #CodeInChunks(inStream, outSize) {
995
1003
  this.#decoder.m_RangeDecoder.Stream = inStream;
996
- this.#ReleaseStream(this.#decoder.m_OutWindow);
1004
+ this.#OutWindow_ReleaseStream();
997
1005
  this.#decoder.m_OutWindow._stream = this.#decompressor.output;
998
1006
  this.#Init_1();
999
1007
  this.#decoder.state = 0;
@@ -1022,7 +1030,7 @@ export class LZMA {
1022
1030
  else {
1023
1031
  decoder.prevByte = this.#DecodeWithMatchByte(decoder2, this.#GetByte(decoder.rep0));
1024
1032
  }
1025
- this.#PutByte(decoder.m_OutWindow, decoder.prevByte);
1033
+ this.#PutByte(decoder.prevByte);
1026
1034
  decoder.state = this.StateUpdateChar(decoder.state);
1027
1035
  decoder.nowPos64 = this.#add(decoder.nowPos64, this.#P1_LONG_LIT);
1028
1036
  }
@@ -1065,7 +1073,7 @@ export class LZMA {
1065
1073
  decoder.rep1 = decoder.rep0;
1066
1074
  len = 2 + this.#Decode(decoder.m_LenDecoder, posState);
1067
1075
  decoder.state = decoder.state < 7 ? 7 : 10;
1068
- positionSlot = this.#Decode_0(decoder.m_PosSlotDecoder[this.GetLenToPosState(len)]);
1076
+ positionSlot = this.#RangeCoder_BitTreeDecoder_Decoder(decoder.m_PosSlotDecoder[this.GetLenToPosState(len)]);
1069
1077
  if (positionSlot >= 4) {
1070
1078
  numDirectBits = (positionSlot >> 1) - 1;
1071
1079
  decoder.rep0 = (2 | positionSlot & 1) << numDirectBits;
@@ -1117,7 +1125,7 @@ export class LZMA {
1117
1125
  this.#Init_8();
1118
1126
  }
1119
1127
  #SetDecoderProperties(properties) {
1120
- var dictionarySize, i, lc, lp, pb, remainder, val;
1128
+ let dictionarySize, i, lc, lp, pb, remainder, val;
1121
1129
  if (properties.length < 5) {
1122
1130
  return 0;
1123
1131
  }
@@ -1143,7 +1151,7 @@ export class LZMA {
1143
1151
  if (this.#decoder.m_DictionarySize != dictionarySize) {
1144
1152
  this.#decoder.m_DictionarySize = dictionarySize;
1145
1153
  this.#decoder.m_DictionarySizeCheck = Math.max(this.#decoder.m_DictionarySize, 1);
1146
- this.#Create_5(this.#decoder.m_OutWindow, Math.max(this.#decoder.m_DictionarySizeCheck, 4096));
1154
+ this.#OutWindow_Create(this.#decoder.m_OutWindow, Math.max(this.#decoder.m_DictionarySizeCheck, 4096));
1147
1155
  }
1148
1156
  return 1;
1149
1157
  }
@@ -1152,7 +1160,7 @@ export class LZMA {
1152
1160
  return 0;
1153
1161
  }
1154
1162
  this.#Create_0(lp, lc);
1155
- var numPosStates = 1 << pb;
1163
+ let numPosStates = 1 << pb;
1156
1164
  this.#Create(this.#decoder.m_LenDecoder, numPosStates);
1157
1165
  this.#Create(this.#decoder.m_RepLenDecoder, numPosStates);
1158
1166
  this.#decoder.m_PosStateMask = numPosStates - 1;
@@ -1164,34 +1172,36 @@ export class LZMA {
1164
1172
  decoder.m_MidCoder[decoder.m_NumPosStates] = this.#createBitTreeDecoder(3);
1165
1173
  }
1166
1174
  }
1167
- #Decode(obj, posState) {
1168
- if (!this.#decodeBit(obj.m_Choice, 0)) {
1169
- return this.#Decode_0(obj.m_LowCoder[posState]);
1175
+ #Decode(decoder, posState) {
1176
+ if (!this.#decodeBit(decoder.m_Choice, 0)) {
1177
+ return this.#RangeCoder_BitTreeDecoder_Decoder(decoder.m_LowCoder[posState]);
1170
1178
  }
1171
1179
  let symbol = 8;
1172
- if (!this.#decodeBit(obj.m_Choice, 1)) {
1173
- symbol += this.#Decode_0(obj.m_MidCoder[posState]);
1180
+ if (!this.#decodeBit(decoder.m_Choice, 1)) {
1181
+ symbol += this.#RangeCoder_BitTreeDecoder_Decoder(decoder.m_MidCoder[posState]);
1174
1182
  }
1175
1183
  else {
1176
- symbol += 8 + this.#Decode_0(obj.m_HighCoder);
1184
+ symbol += 8 + this.#RangeCoder_BitTreeDecoder_Decoder(decoder.m_HighCoder);
1177
1185
  }
1178
1186
  return symbol;
1179
1187
  }
1180
- #createLenDecoder(obj) {
1181
- obj.m_Choice = this.#initArray(2);
1182
- obj.m_LowCoder = this.#initArray(16);
1183
- obj.m_MidCoder = this.#initArray(16);
1184
- obj.m_HighCoder = this.#createBitTreeDecoder(8);
1185
- obj.m_NumPosStates = 0;
1186
- return obj;
1188
+ #createLenDecoder() {
1189
+ const decoder = {
1190
+ m_Choice: this.#initArray(2),
1191
+ m_LowCoder: this.#initArray(16),
1192
+ m_MidCoder: this.#initArray(16),
1193
+ m_HighCoder: this.#createBitTreeDecoder(8),
1194
+ m_NumPosStates: 0,
1195
+ };
1196
+ return decoder;
1187
1197
  }
1188
- #Init(obj) {
1189
- this.InitBitModels(obj.m_Choice);
1190
- for (let posState = 0; posState < obj.m_NumPosStates; ++posState) {
1191
- this.InitBitModels(obj.m_LowCoder[posState].Models);
1192
- this.InitBitModels(obj.m_MidCoder[posState].Models);
1198
+ #Init(decoder) {
1199
+ this.InitBitModels(decoder.m_Choice);
1200
+ for (let posState = 0; posState < decoder.m_NumPosStates; ++posState) {
1201
+ this.InitBitModels(decoder.m_LowCoder[posState].Models);
1202
+ this.InitBitModels(decoder.m_MidCoder[posState].Models);
1193
1203
  }
1194
- this.InitBitModels(obj.m_HighCoder.Models);
1204
+ this.InitBitModels(decoder.m_HighCoder.Models);
1195
1205
  }
1196
1206
  #Create_0(numPosBits, numPrevBits) {
1197
1207
  let i, numStates;
@@ -1206,7 +1216,7 @@ export class LZMA {
1206
1216
  numStates = 1 << this.#decoder.m_LiteralDecoder.m_NumPrevBits + this.#decoder.m_LiteralDecoder.m_NumPosBits;
1207
1217
  this.#decoder.m_LiteralDecoder.m_Coders = this.#initArray(numStates);
1208
1218
  for (i = 0; i < numStates; ++i) {
1209
- this.#decoder.m_LiteralDecoder.m_Coders[i] = this.#createLiteralDecoderEncoder2({});
1219
+ this.#decoder.m_LiteralDecoder.m_Coders[i] = this.#createLiteralDecoderEncoder2();
1210
1220
  }
1211
1221
  }
1212
1222
  #GetDecoder(pos, prevByte) {
@@ -1218,68 +1228,69 @@ export class LZMA {
1218
1228
  // Return decoder at calculated index
1219
1229
  return literalDecoder.m_Coders[index];
1220
1230
  }
1221
- #Init_0(obj) {
1231
+ #Init_0(decoder) {
1222
1232
  let i, numStates;
1223
- numStates = 1 << obj.m_NumPrevBits + obj.m_NumPosBits;
1233
+ numStates = 1 << decoder.m_NumPrevBits + decoder.m_NumPosBits;
1224
1234
  for (i = 0; i < numStates; ++i) {
1225
- this.InitBitModels(obj.m_Coders[i].m_Decoders);
1235
+ this.InitBitModels(decoder.m_Coders[i].m_Decoders);
1226
1236
  }
1227
1237
  }
1228
- #DecodeNormal(rangeDecoder) {
1229
- const _rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
1230
- var symbol = 1;
1238
+ #DecodeNormal(decoder) {
1239
+ let symbol = 1;
1231
1240
  do {
1232
- symbol = symbol << 1 | this.#decodeBit(rangeDecoder.m_Decoders, symbol);
1241
+ symbol = symbol << 1 | this.#decodeBit(decoder.m_Decoders, symbol);
1233
1242
  } while (symbol < 256);
1234
- return symbol << 24 >> 24;
1243
+ return symbol & 0xFF;
1235
1244
  }
1236
- #DecodeWithMatchByte(obj, matchByte) {
1245
+ #DecodeWithMatchByte(encoder, matchByte) {
1237
1246
  let bit, matchBit, symbol = 1;
1238
1247
  do {
1239
1248
  matchBit = matchByte >> 7 & 1;
1240
1249
  matchByte <<= 1;
1241
- bit = this.#decodeBit(obj.m_Decoders, (1 + matchBit << 8) + symbol);
1250
+ bit = this.#decodeBit(encoder.m_Decoders, (1 + matchBit << 8) + symbol);
1242
1251
  symbol = symbol << 1 | bit;
1243
1252
  if (matchBit != bit) {
1244
1253
  while (symbol < 256) {
1245
- symbol = symbol << 1 | this.#decodeBit(obj.m_Decoders, symbol);
1254
+ symbol = symbol << 1 | this.#decodeBit(encoder.m_Decoders, symbol);
1246
1255
  }
1247
1256
  break;
1248
1257
  }
1249
1258
  } while (symbol < 256);
1250
- return symbol << 24 >> 24;
1259
+ return symbol & 0xFF;
1251
1260
  }
1252
- #createLiteralDecoderEncoder2(obj) {
1253
- obj.m_Decoders = this.#initArray(768);
1254
- return obj;
1261
+ #createLiteralDecoderEncoder2() {
1262
+ const literalDecoder = {
1263
+ m_Decoders: this.#initArray(0x300),
1264
+ };
1265
+ return literalDecoder;
1255
1266
  }
1256
1267
  #Backward(cur) {
1257
- const obj = this.#compressor.chunker.encoder;
1268
+ const encoder = this.#compressor.chunker.encoder;
1258
1269
  let backCur, backMem, posMem, posPrev;
1259
- obj._optimumEndIndex = cur;
1260
- posMem = obj._optimum[cur].PosPrev;
1261
- backMem = obj._optimum[cur].BackPrev;
1270
+ encoder._optimumEndIndex = cur;
1271
+ posMem = encoder._optimum[cur].PosPrev;
1272
+ backMem = encoder._optimum[cur].BackPrev;
1262
1273
  do {
1263
- if (obj._optimum[cur].Prev1IsChar) {
1264
- this.#MakeAsChar(obj._optimum[posMem]);
1265
- obj._optimum[posMem].PosPrev = posMem - 1;
1266
- if (obj._optimum[cur].Prev2) {
1267
- obj._optimum[posMem - 1].Prev1IsChar = 0;
1268
- obj._optimum[posMem - 1].PosPrev = obj._optimum[cur].PosPrev2;
1269
- obj._optimum[posMem - 1].BackPrev = obj._optimum[cur].BackPrev2;
1274
+ if (encoder._optimum[cur].Prev1IsChar) {
1275
+ this.#MakeAsChar(encoder._optimum[posMem]);
1276
+ encoder._optimum[posMem].PosPrev = posMem - 1;
1277
+ if (encoder._optimum[cur].Prev2) {
1278
+ encoder._optimum[posMem - 1].Prev1IsChar = 0;
1279
+ encoder._optimum[posMem - 1].PosPrev = encoder._optimum[cur].PosPrev2;
1280
+ encoder._optimum[posMem - 1].BackPrev = encoder._optimum[cur].BackPrev2;
1270
1281
  }
1271
1282
  }
1272
1283
  posPrev = posMem;
1273
1284
  backCur = backMem;
1274
- backMem = obj._optimum[posPrev].BackPrev;
1275
- posMem = obj._optimum[posPrev].PosPrev;
1276
- obj._optimum[posPrev].BackPrev = backCur;
1277
- obj._optimum[posPrev].PosPrev = cur;
1285
+ backMem = encoder._optimum[posPrev].BackPrev;
1286
+ posMem = encoder._optimum[posPrev].PosPrev;
1287
+ encoder._optimum[posPrev].BackPrev = backCur;
1288
+ encoder._optimum[posPrev].PosPrev = cur;
1278
1289
  cur = posPrev;
1279
1290
  } while (cur > 0);
1280
- obj.backRes = obj._optimum[0].BackPrev;
1281
- obj._optimumCurrentIndex = obj._optimum[0].PosPrev;
1282
- return obj._optimumCurrentIndex;
1291
+ encoder.backRes = encoder._optimum[0].BackPrev;
1292
+ encoder._optimumCurrentIndex = encoder._optimum[0].PosPrev;
1293
+ return encoder._optimumCurrentIndex;
1283
1294
  }
1284
1295
  #BaseInit() {
1285
1296
  this.#encoder._state = 0;
@@ -1315,7 +1326,7 @@ export class LZMA {
1315
1326
  this.#Encode_3(this.#compressor.chunker.encoder._isMatch, (this.#compressor.chunker.encoder._state << 4) + posState, 0);
1316
1327
  this.#compressor.chunker.encoder._state = this.StateUpdateChar(this.#compressor.chunker.encoder._state);
1317
1328
  curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1318
- this.#Encode_1(this.#GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
1329
+ this.#Encode_1(this.#LZMA_Encoder_GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte), curByte);
1319
1330
  this.#compressor.chunker.encoder._previousByte = curByte;
1320
1331
  this.#compressor.chunker.encoder._additionalOffset -= 1;
1321
1332
  this.#compressor.chunker.encoder.nowPos64 = this.#add(this.#compressor.chunker.encoder.nowPos64, this.#P1_LONG_LIT);
@@ -1333,7 +1344,7 @@ export class LZMA {
1333
1344
  if (len == 1 && pos == -1) {
1334
1345
  this.#Encode_3(this.#compressor.chunker.encoder._isMatch, complexState, 0);
1335
1346
  curByte = this.#GetIndexByte(-this.#compressor.chunker.encoder._additionalOffset);
1336
- subCoder = this.#GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
1347
+ subCoder = this.#LZMA_Encoder_GetSubCoder(this.#lowBits_0(this.#compressor.chunker.encoder.nowPos64), this.#compressor.chunker.encoder._previousByte);
1337
1348
  if (this.#compressor.chunker.encoder._state < 7) {
1338
1349
  this.#Encode_1(subCoder, curByte);
1339
1350
  }
@@ -1456,7 +1467,7 @@ export class LZMA {
1456
1467
  this.#SetType(binTree, numHashBytes);
1457
1468
  this.#encoder._matchFinder = binTree;
1458
1469
  }
1459
- this.#Create_1();
1470
+ this.#LZMA_Encoder_LiteralEncoder_Create();
1460
1471
  if (this.#encoder._dictionarySize == this.#encoder._dictionarySizePrev
1461
1472
  && this.#encoder._numFastBytesPrev == this.#encoder._numFastBytes) {
1462
1473
  return;
@@ -1491,7 +1502,7 @@ export class LZMA {
1491
1502
  encoder = obj._posSlotEncoder[lenToPosState];
1492
1503
  st = lenToPosState << 6;
1493
1504
  for (posSlot = 0; posSlot < obj._distTableSize; posSlot += 1) {
1494
- obj._posSlotPrices[st + posSlot] = this.#GetPrice_1(encoder, posSlot);
1505
+ obj._posSlotPrices[st + posSlot] = this.#RangeCoder_Encoder_GetPrice_1(encoder, posSlot);
1495
1506
  }
1496
1507
  for (posSlot = 14; posSlot < obj._distTableSize; posSlot += 1) {
1497
1508
  obj._posSlotPrices[st + posSlot] += (posSlot >> 1) - 1 - 4 << 6;
@@ -1567,7 +1578,7 @@ export class LZMA {
1567
1578
  }
1568
1579
  encoder._optimum[0].State = encoder._state;
1569
1580
  posState = position & encoder._posStateMask;
1570
- encoder._optimum[1].Price = this.#probPrices[encoder._isMatch[(encoder._state << 4) + posState] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
1581
+ encoder._optimum[1].Price = this.#probPrices[encoder._isMatch[(encoder._state << 4) + posState] >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, encoder._previousByte), encoder._state >= 7, matchByte, currentByte);
1571
1582
  this.#MakeAsChar(encoder._optimum[1]);
1572
1583
  matchPrice = this.#probPrices[2_048
1573
1584
  - encoder._isMatch[(encoder._state << 4) + posState]
@@ -1594,7 +1605,7 @@ export class LZMA {
1594
1605
  encoder._optimum[0].Backs3 = encoder.reps[3];
1595
1606
  len = lenEnd;
1596
1607
  do {
1597
- encoder._optimum[len].Price = 268_435_455;
1608
+ encoder._optimum[len].Price = this.#kIfinityPrice;
1598
1609
  len -= 1;
1599
1610
  } while (len >= 2);
1600
1611
  for (let i = 0; i < 4; ++i) {
@@ -1604,7 +1615,7 @@ export class LZMA {
1604
1615
  }
1605
1616
  price_4 = repMatchPrice + this.#GetPureRepPrice(i, encoder._state, posState);
1606
1617
  do {
1607
- curAndLenPrice = price_4 + this.#GetPrice(encoder._repMatchLenEncoder, repLen - 2, posState);
1618
+ curAndLenPrice = price_4 + this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, repLen - 2, posState);
1608
1619
  optimum = encoder._optimum[repLen];
1609
1620
  if (curAndLenPrice < optimum.Price) {
1610
1621
  optimum.Price = curAndLenPrice;
@@ -1624,7 +1635,7 @@ export class LZMA {
1624
1635
  }
1625
1636
  for (;; len += 1) {
1626
1637
  distance = encoder._matchDistances[offs + 1];
1627
- curAndLenPrice = normalMatchPrice + this.#GetPosLenPrice(distance, len, posState);
1638
+ curAndLenPrice = normalMatchPrice + this.#LZMA_Encoder_GetPosLenPrice(distance, len, posState);
1628
1639
  optimum = encoder._optimum[len];
1629
1640
  if (curAndLenPrice < optimum.Price) {
1630
1641
  optimum.Price = curAndLenPrice;
@@ -1743,7 +1754,7 @@ export class LZMA {
1743
1754
  posState = position & encoder._posStateMask;
1744
1755
  curAnd1Price = curPrice
1745
1756
  + this.#probPrices[encoder._isMatch[(state << 4) + posState] >>> 2]
1746
- + this.#GetPrice_0(this.#GetSubCoder(position, this.#GetIndexByte(-2)), state >= 7, matchByte, currentByte);
1757
+ + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position, this.#GetIndexByte(-2)), state >= 7, matchByte, currentByte);
1747
1758
  nextOptimum = encoder._optimum[cur + 1];
1748
1759
  nextIsChar = 0;
1749
1760
  if (curAnd1Price < nextOptimum.Price) {
@@ -1786,9 +1797,9 @@ export class LZMA {
1786
1797
  nextRepMatchPrice = curAnd1Price + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1787
1798
  offset = cur + 1 + lenTest2;
1788
1799
  while (lenEnd < offset) {
1789
- encoder._optimum[lenEnd += 1].Price = 268_435_455;
1800
+ encoder._optimum[lenEnd += 1].Price = this.#kIfinityPrice;
1790
1801
  }
1791
- curAndLenPrice = nextRepMatchPrice + (price = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1802
+ curAndLenPrice = nextRepMatchPrice + (price = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1792
1803
  price + this.#GetPureRepPrice(0, state2, posStateNext));
1793
1804
  optimum = encoder._optimum[offset];
1794
1805
  if (curAndLenPrice < optimum.Price) {
@@ -1809,9 +1820,9 @@ export class LZMA {
1809
1820
  lenTestTemp = lenTest;
1810
1821
  do {
1811
1822
  while (lenEnd < cur + lenTest) {
1812
- encoder._optimum[lenEnd += 1].Price = 268_435_455;
1823
+ encoder._optimum[lenEnd += 1].Price = this.#kIfinityPrice;
1813
1824
  }
1814
- curAndLenPrice = repMatchPrice + (price_0 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1825
+ curAndLenPrice = repMatchPrice + (price_0 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1815
1826
  price_0 + this.#GetPureRepPrice(repIndex, state, posState));
1816
1827
  optimum = encoder._optimum[cur + lenTest];
1817
1828
  if (curAndLenPrice < optimum.Price) {
@@ -1831,18 +1842,18 @@ export class LZMA {
1831
1842
  if (lenTest2 >= 2) {
1832
1843
  state2 = state < 7 ? 8 : 11;
1833
1844
  posStateNext = position + lenTest & encoder._posStateMask;
1834
- curAndLenCharPrice = repMatchPrice + (price_1 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1845
+ curAndLenCharPrice = repMatchPrice + (price_1 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest - 2, posState),
1835
1846
  price_1 + this.#GetPureRepPrice(repIndex, state, posState))
1836
- + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#GetIndexByte(lenTest - 1));
1847
+ + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - 1 - (encoder.reps[repIndex] + 1)), this.#GetIndexByte(lenTest - 1));
1837
1848
  state2 = this.StateUpdateChar(state2);
1838
1849
  posStateNext = position + lenTest + 1 & encoder._posStateMask;
1839
1850
  nextMatchPrice = curAndLenCharPrice + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext] >>> 2];
1840
1851
  nextRepMatchPrice = nextMatchPrice + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1841
1852
  offset = cur + 1 + lenTest + lenTest2;
1842
1853
  while (lenEnd < cur + offset) {
1843
- encoder._optimum[lenEnd += 1].Price = 268_435_455;
1854
+ encoder._optimum[lenEnd += 1].Price = this.#kIfinityPrice;
1844
1855
  }
1845
- curAndLenPrice = nextRepMatchPrice + (price_2 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1856
+ curAndLenPrice = nextRepMatchPrice + (price_2 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1846
1857
  price_2 + this.#GetPureRepPrice(0, state2, posStateNext));
1847
1858
  optimum = encoder._optimum[cur + offset];
1848
1859
  if (curAndLenPrice < optimum.Price) {
@@ -1866,7 +1877,7 @@ export class LZMA {
1866
1877
  if (newLen >= startLen) {
1867
1878
  normalMatchPrice = matchPrice + this.#probPrices[encoder._isRep[state] >>> 2];
1868
1879
  while (lenEnd < cur + newLen) {
1869
- encoder._optimum[lenEnd += 1].Price = 268_435_455;
1880
+ encoder._optimum[lenEnd += 1].Price = this.#kIfinityPrice;
1870
1881
  }
1871
1882
  offs = 0;
1872
1883
  while (startLen > encoder._matchDistances[offs]) {
@@ -1874,7 +1885,7 @@ export class LZMA {
1874
1885
  }
1875
1886
  for (lenTest = startLen;; lenTest += 1) {
1876
1887
  curBack = encoder._matchDistances[offs + 1];
1877
- curAndLenPrice = normalMatchPrice + this.#GetPosLenPrice(curBack, lenTest, posState);
1888
+ curAndLenPrice = normalMatchPrice + this.#LZMA_Encoder_GetPosLenPrice(curBack, lenTest, posState);
1878
1889
  optimum = encoder._optimum[cur + lenTest];
1879
1890
  if (curAndLenPrice < optimum.Price) {
1880
1891
  optimum.Price = curAndLenPrice;
@@ -1889,7 +1900,7 @@ export class LZMA {
1889
1900
  if (lenTest2 >= 2) {
1890
1901
  state2 = state < 7 ? 7 : 10;
1891
1902
  posStateNext = position + lenTest & encoder._posStateMask;
1892
- curAndLenCharPrice = curAndLenPrice + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#GetPrice_0(this.#GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - (curBack + 1) - 1), this.#GetIndexByte(lenTest - 1));
1903
+ curAndLenCharPrice = curAndLenPrice + this.#probPrices[encoder._isMatch[(state2 << 4) + posStateNext] >>> 2] + this.#RangeCoder_Encoder_GetPrice_0(this.#LZMA_Encoder_GetSubCoder(position + lenTest, this.#GetIndexByte(lenTest - 1 - 1)), 1, this.#GetIndexByte(lenTest - (curBack + 1) - 1), this.#GetIndexByte(lenTest - 1));
1893
1904
  state2 = this.StateUpdateChar(state2);
1894
1905
  posStateNext = position + lenTest + 1 & encoder._posStateMask;
1895
1906
  nextMatchPrice = curAndLenCharPrice + this.#probPrices[2_048 - encoder._isMatch[(state2 << 4) + posStateNext]
@@ -1897,9 +1908,9 @@ export class LZMA {
1897
1908
  nextRepMatchPrice = nextMatchPrice + this.#probPrices[2_048 - encoder._isRep[state2] >>> 2];
1898
1909
  offset = lenTest + 1 + lenTest2;
1899
1910
  while (lenEnd < cur + offset) {
1900
- encoder._optimum[lenEnd += 1].Price = 268_435_455;
1911
+ encoder._optimum[lenEnd += 1].Price = this.#kIfinityPrice;
1901
1912
  }
1902
- curAndLenPrice = nextRepMatchPrice + (price_3 = this.#GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1913
+ curAndLenPrice = nextRepMatchPrice + (price_3 = this.#RangeCoder_Encoder_GetPrice(encoder._repMatchLenEncoder, lenTest2 - 2, posStateNext),
1903
1914
  price_3 + this.#GetPureRepPrice(0, state2, posStateNext));
1904
1915
  optimum = encoder._optimum[cur + offset];
1905
1916
  if (curAndLenPrice < optimum.Price) {
@@ -1922,19 +1933,20 @@ export class LZMA {
1922
1933
  }
1923
1934
  }
1924
1935
  }
1925
- #GetPosLenPrice(pos, len, posState) {
1936
+ #LZMA_Encoder_GetPosLenPrice(pos, len, posState) {
1937
+ const encoder = this.#compressor.chunker.encoder;
1926
1938
  let price, lenToPosState = this.GetLenToPosState(len);
1927
1939
  if (pos < 128) {
1928
- price = this.#compressor.chunker.encoder._distancesPrices[lenToPosState * 128 + pos];
1940
+ price = encoder._distancesPrices[lenToPosState * 128 + pos];
1929
1941
  }
1930
1942
  else {
1931
- price = this.#compressor.chunker.encoder._posSlotPrices[(lenToPosState << 6) + this.GetPosSlot2(pos)]
1932
- + this.#compressor.chunker.encoder._alignPrices[pos & 15];
1943
+ const position = (lenToPosState << 6) + this.GetPosSlot2(pos);
1944
+ price = encoder._posSlotPrices[position] + encoder._alignPrices[pos & 15];
1933
1945
  }
1934
- return price + this.#GetPrice(this.#compressor.chunker.encoder._lenEncoder, len - 2, posState);
1946
+ return price + this.#RangeCoder_Encoder_GetPrice(encoder._lenEncoder, len - 2, posState);
1935
1947
  }
1936
1948
  #GetPureRepPrice(repIndex, state, posState) {
1937
- var price;
1949
+ let price;
1938
1950
  if (!repIndex) {
1939
1951
  price = this.#probPrices[this.#compressor.chunker.encoder._isRepG0[state] >>> 2];
1940
1952
  price += this.#probPrices[2_048 - this.#compressor.chunker.encoder._isRep0Long[(state << 4) + posState] >>> 2];
@@ -1985,23 +1997,16 @@ export class LZMA {
1985
1997
  }
1986
1998
  }
1987
1999
  #ReadMatchDistances() {
1988
- var lenRes = 0;
1989
- this.#compressor.chunker.encoder._numDistancePairs = this.#GetMatches();
1990
- if (this.#compressor.chunker.encoder._numDistancePairs > 0) {
1991
- lenRes = this
1992
- .#compressor
1993
- .chunker
1994
- .encoder
1995
- ._matchDistances[this.#compressor.chunker.encoder._numDistancePairs - 2];
1996
- if (lenRes == this.#compressor.chunker.encoder._numFastBytes) {
1997
- lenRes += this.#GetMatchLen(lenRes - 1, this
1998
- .#compressor
1999
- .chunker
2000
- .encoder
2001
- ._matchDistances[this.#compressor.chunker.encoder._numDistancePairs - 1], 273 - lenRes);
2002
- }
2003
- }
2004
- this.#compressor.chunker.encoder._additionalOffset += 1;
2000
+ let lenRes = 0;
2001
+ const encoder = this.#compressor.chunker.encoder;
2002
+ encoder._numDistancePairs = this.#GetMatches();
2003
+ if (encoder._numDistancePairs > 0) {
2004
+ lenRes = encoder._matchDistances[encoder._numDistancePairs - 2];
2005
+ if (lenRes == encoder._numFastBytes) {
2006
+ lenRes += this.#GetMatchLen(lenRes - 1, encoder._matchDistances[encoder._numDistancePairs - 1], 273 - lenRes);
2007
+ }
2008
+ }
2009
+ encoder._additionalOffset += 1;
2005
2010
  return lenRes;
2006
2011
  }
2007
2012
  #ReleaseMFStream() {
@@ -2016,7 +2021,8 @@ export class LZMA {
2016
2021
  }
2017
2022
  #SetDictionarySize_0(dictionarySize) {
2018
2023
  this.#encoder._dictionarySize = dictionarySize;
2019
- for (var dicLogSize = 0; dictionarySize > (1 << dicLogSize); ++dicLogSize)
2024
+ let dicLogSize = 0;
2025
+ for (; dictionarySize > (1 << dicLogSize); ++dicLogSize)
2020
2026
  ;
2021
2027
  this.#encoder._distTableSize = dicLogSize * 2;
2022
2028
  }
@@ -2049,7 +2055,7 @@ export class LZMA {
2049
2055
  this.#Encode_3(encoder._isRep, encoder._state, 0);
2050
2056
  encoder._state = encoder._state < 7 ? 7 : 10;
2051
2057
  this.#Encode_0(encoder._lenEncoder, 0, positionState);
2052
- var lenToPosState = this.GetLenToPosState(2);
2058
+ let lenToPosState = this.GetLenToPosState(2);
2053
2059
  this.#Encode_2(encoder._posSlotEncoder[lenToPosState], 63);
2054
2060
  this.#EncodeDirectBits(67108863, 26);
2055
2061
  this.#ReverseEncode(15);
@@ -2072,21 +2078,21 @@ export class LZMA {
2072
2078
  }
2073
2079
  return this.#gFastPos[pos >> 26] + 52;
2074
2080
  }
2075
- #Encode(obj, symbol, posState) {
2081
+ #Encode(encoder, symbol, posState) {
2076
2082
  if (symbol < 8) {
2077
- this.#Encode_3(obj._choice, 0, 0);
2078
- this.#Encode_2(obj._lowCoder[posState], symbol);
2083
+ this.#Encode_3(encoder._choice, 0, 0);
2084
+ this.#Encode_2(encoder._lowCoder[posState], symbol);
2079
2085
  }
2080
2086
  else {
2081
2087
  symbol -= 8;
2082
- this.#Encode_3(obj._choice, 0, 1);
2088
+ this.#Encode_3(encoder._choice, 0, 1);
2083
2089
  if (symbol < 8) {
2084
- this.#Encode_3(obj._choice, 1, 0);
2085
- this.#Encode_2(obj._midCoder[posState], symbol);
2090
+ this.#Encode_3(encoder._choice, 1, 0);
2091
+ this.#Encode_2(encoder._midCoder[posState], symbol);
2086
2092
  }
2087
2093
  else {
2088
- this.#Encode_3(obj._choice, 1, 1);
2089
- this.#Encode_2(obj._highCoder, symbol - 8);
2094
+ this.#Encode_3(encoder._choice, 1, 1);
2095
+ this.#Encode_2(encoder._highCoder, symbol - 8);
2090
2096
  }
2091
2097
  }
2092
2098
  }
@@ -2110,34 +2116,34 @@ export class LZMA {
2110
2116
  }
2111
2117
  this.InitBitModels(obj._highCoder.Models);
2112
2118
  }
2113
- #SetPrices(obj, posState, numSymbols, prices, st) {
2119
+ #SetPrices(encoder, posState, numSymbols, prices, st) {
2114
2120
  let a0, a1, b0, b1, i;
2115
- a0 = this.#probPrices[obj._choice[0] >>> 2];
2116
- a1 = this.#probPrices[2_048 - obj._choice[0] >>> 2];
2117
- b0 = a1 + this.#probPrices[obj._choice[1] >>> 2];
2118
- b1 = a1 + this.#probPrices[2_048 - obj._choice[1] >>> 2];
2121
+ a0 = this.#probPrices[encoder._choice[0] >>> 2];
2122
+ a1 = this.#probPrices[2_048 - encoder._choice[0] >>> 2];
2123
+ b0 = a1 + this.#probPrices[encoder._choice[1] >>> 2];
2124
+ b1 = a1 + this.#probPrices[2_048 - encoder._choice[1] >>> 2];
2119
2125
  i = 0;
2120
2126
  for (i = 0; i < 8; ++i) {
2121
2127
  if (i >= numSymbols) {
2122
2128
  return;
2123
2129
  }
2124
- prices[st + i] = a0 + this.#GetPrice_1(obj._lowCoder[posState], i);
2130
+ prices[st + i] = a0 + this.#RangeCoder_Encoder_GetPrice_1(encoder._lowCoder[posState], i);
2125
2131
  }
2126
2132
  for (; i < 16; ++i) {
2127
2133
  if (i >= numSymbols) {
2128
2134
  return;
2129
2135
  }
2130
- prices[st + i] = b0 + this.#GetPrice_1(obj._midCoder[posState], i - 8);
2136
+ prices[st + i] = b0 + this.#RangeCoder_Encoder_GetPrice_1(encoder._midCoder[posState], i - 8);
2131
2137
  }
2132
2138
  for (; i < numSymbols; ++i) {
2133
- prices[st + i] = b1 + this.#GetPrice_1(obj._highCoder, i - 8 - 8);
2139
+ prices[st + i] = b1 + this.#RangeCoder_Encoder_GetPrice_1(encoder._highCoder, i - 8 - 8);
2134
2140
  }
2135
2141
  }
2136
- #Encode_0(obj, symbol, posState) {
2137
- this.#Encode(obj, symbol, posState);
2138
- if ((obj._counters[posState] -= 1) == 0) {
2139
- this.#SetPrices(obj, posState, obj._tableSize, obj._prices, posState * 272);
2140
- obj._counters[posState] = obj._tableSize;
2142
+ #Encode_0(encoder, symbol, posState) {
2143
+ this.#Encode(encoder, symbol, posState);
2144
+ if ((encoder._counters[posState] -= 1) == 0) {
2145
+ this.#SetPrices(encoder, posState, encoder._tableSize, encoder._prices, posState * 272);
2146
+ encoder._counters[posState] = encoder._tableSize;
2141
2147
  }
2142
2148
  }
2143
2149
  #createLenPriceTableEncoder() {
@@ -2146,16 +2152,16 @@ export class LZMA {
2146
2152
  encoder._counters = [];
2147
2153
  return encoder;
2148
2154
  }
2149
- #GetPrice(obj, symbol, posState) {
2150
- return obj._prices[posState * 272 + symbol];
2155
+ #RangeCoder_Encoder_GetPrice(encoder, symbol, posState) {
2156
+ return encoder._prices[posState * 272 + symbol];
2151
2157
  }
2152
- #UpdateTables(obj, numPosStates) {
2158
+ #LZMA_LenPriceTableEncoder_UpdateTablesUpdateTables(encoder, numPosStates) {
2153
2159
  for (let posState = 0; posState < numPosStates; ++posState) {
2154
- this.#SetPrices(obj, posState, obj._tableSize, obj._prices, posState * 272);
2155
- obj._counters[posState] = obj._tableSize;
2160
+ this.#SetPrices(encoder, posState, encoder._tableSize, encoder._prices, posState * 272);
2161
+ encoder._counters[posState] = encoder._tableSize;
2156
2162
  }
2157
2163
  }
2158
- #Create_1() {
2164
+ #LZMA_Encoder_LiteralEncoder_Create() {
2159
2165
  let i, numStates;
2160
2166
  if (this.#encoder._literalEncoder.m_Coders != null
2161
2167
  && this.#encoder._literalEncoder.m_NumPrevBits == this.#encoder._numLiteralContextBits
@@ -2168,10 +2174,10 @@ export class LZMA {
2168
2174
  numStates = 1 << this.#encoder._literalEncoder.m_NumPrevBits + this.#encoder._literalEncoder.m_NumPosBits;
2169
2175
  this.#encoder._literalEncoder.m_Coders = this.#initArray(numStates);
2170
2176
  for (i = 0; i < numStates; ++i) {
2171
- this.#encoder._literalEncoder.m_Coders[i] = this.#createLiteralEncoderEncoder2({});
2177
+ this.#encoder._literalEncoder.m_Coders[i] = this.#createLiteralEncoderEncoder2();
2172
2178
  }
2173
2179
  }
2174
- #GetSubCoder(pos, prevByte) {
2180
+ #LZMA_Encoder_GetSubCoder(pos, prevByte) {
2175
2181
  const literalEncoder = this.#compressor.chunker.encoder._literalEncoder;
2176
2182
  // Calculate position mask bits
2177
2183
  const posBits = pos & literalEncoder.m_PosMask;
@@ -2190,15 +2196,15 @@ export class LZMA {
2190
2196
  this.InitBitModels(this.#encoder._literalEncoder.m_Coders[i].m_Encoders);
2191
2197
  }
2192
2198
  }
2193
- #Encode_1(obj, symbol) {
2194
- var bit, context = 1;
2199
+ #Encode_1(encoder, symbol) {
2200
+ let bit, context = 1;
2195
2201
  for (let i = 7; i >= 0; --i) {
2196
2202
  bit = symbol >> i & 1;
2197
- this.#Encode_3(obj.m_Encoders, context, bit);
2203
+ this.#Encode_3(encoder.m_Encoders, context, bit);
2198
2204
  context = context << 1 | bit;
2199
2205
  }
2200
2206
  }
2201
- #EncodeMatched(obj, matchByte, symbol) {
2207
+ #EncodeMatched(encoder, matchByte, symbol) {
2202
2208
  let bit, matchBit, state, same = true, context = 1;
2203
2209
  for (let i = 7; i >= 0; --i) {
2204
2210
  bit = symbol >> i & 1;
@@ -2208,21 +2214,23 @@ export class LZMA {
2208
2214
  state += 1 + matchBit << 8;
2209
2215
  same = matchBit === bit;
2210
2216
  }
2211
- this.#Encode_3(obj.m_Encoders, state, bit);
2217
+ this.#Encode_3(encoder.m_Encoders, state, bit);
2212
2218
  context = context << 1 | bit;
2213
2219
  }
2214
2220
  }
2215
- #createLiteralEncoderEncoder2(obj) {
2216
- obj.m_Encoders = this.#initArray(768);
2217
- return obj;
2221
+ #createLiteralEncoderEncoder2() {
2222
+ const encoder = {
2223
+ m_Encoders: this.#initArray(768),
2224
+ };
2225
+ return encoder;
2218
2226
  }
2219
- #GetPrice_0(obj, matchMode, matchByte, symbol) {
2227
+ #RangeCoder_Encoder_GetPrice_0(encoder, matchMode, matchByte, symbol) {
2220
2228
  let bit, context = 1, i = 7, matchBit, price = 0;
2221
2229
  if (matchMode) {
2222
2230
  for (; i >= 0; --i) {
2223
2231
  matchBit = matchByte >> i & 1;
2224
2232
  bit = symbol >> i & 1;
2225
- price += this.GetPrice(obj.m_Encoders[(1 + matchBit << 8) + context], bit);
2233
+ price += this.GetPrice(encoder.m_Encoders[(1 + matchBit << 8) + context], bit);
2226
2234
  context = context << 1 | bit;
2227
2235
  if (matchBit != bit) {
2228
2236
  --i;
@@ -2232,18 +2240,18 @@ export class LZMA {
2232
2240
  }
2233
2241
  for (; i >= 0; --i) {
2234
2242
  bit = symbol >> i & 1;
2235
- price += this.GetPrice(obj.m_Encoders[context], bit);
2243
+ price += this.GetPrice(encoder.m_Encoders[context], bit);
2236
2244
  context = context << 1 | bit;
2237
2245
  }
2238
2246
  return price;
2239
2247
  }
2240
- #MakeAsChar(obj) {
2241
- obj.BackPrev = -1;
2242
- obj.Prev1IsChar = 0;
2248
+ #MakeAsChar(optimum) {
2249
+ optimum.BackPrev = -1;
2250
+ optimum.Prev1IsChar = 0;
2243
2251
  }
2244
- #MakeAsShortRep(obj) {
2245
- obj.BackPrev = 0;
2246
- obj.Prev1IsChar = 0;
2252
+ #MakeAsShortRep(optimum) {
2253
+ optimum.BackPrev = 0;
2254
+ optimum.Prev1IsChar = 0;
2247
2255
  }
2248
2256
  #createBitTreeDecoder(numBitLevels) {
2249
2257
  return {
@@ -2251,8 +2259,7 @@ export class LZMA {
2251
2259
  Models: this.#initArray(1 << numBitLevels),
2252
2260
  };
2253
2261
  }
2254
- // BitTreeDecoder.Decoder
2255
- #Decode_0(rangeDecoder) {
2262
+ #RangeCoder_BitTreeDecoder_Decoder(rangeDecoder) {
2256
2263
  const _rangeDecoder = this.#decompressor.chunker.decoder.m_RangeDecoder;
2257
2264
  let bitIndex, m = 1;
2258
2265
  for (bitIndex = rangeDecoder.NumBitLevels; bitIndex != 0; bitIndex -= 1) {
@@ -2288,28 +2295,28 @@ export class LZMA {
2288
2295
  Models: this.#initArray(1 << numBitLevels),
2289
2296
  };
2290
2297
  }
2291
- #Encode_2(obj, symbol) {
2292
- var bit, bitIndex, m = 1;
2293
- for (bitIndex = obj.NumBitLevels; bitIndex != 0;) {
2298
+ #Encode_2(encoder, symbol) {
2299
+ let bit, bitIndex, m = 1;
2300
+ for (bitIndex = encoder.NumBitLevels; bitIndex != 0;) {
2294
2301
  bitIndex -= 1;
2295
2302
  bit = symbol >>> bitIndex & 1;
2296
- this.#Encode_3(obj.Models, m, bit);
2303
+ this.#Encode_3(encoder.Models, m, bit);
2297
2304
  m = m << 1 | bit;
2298
2305
  }
2299
2306
  }
2300
- #GetPrice_1(obj, symbol) {
2301
- var bit, bitIndex, m = 1, price = 0;
2302
- for (bitIndex = obj.NumBitLevels; bitIndex != 0;) {
2307
+ #RangeCoder_Encoder_GetPrice_1(encoder, symbol) {
2308
+ let bit, bitIndex, m = 1, price = 0;
2309
+ for (bitIndex = encoder.NumBitLevels; bitIndex != 0;) {
2303
2310
  bitIndex -= 1;
2304
2311
  bit = symbol >>> bitIndex & 1;
2305
- price += this.GetPrice(obj.Models[m], bit);
2312
+ price += this.GetPrice(encoder.Models[m], bit);
2306
2313
  m = (m << 1) + bit;
2307
2314
  }
2308
2315
  return price;
2309
2316
  }
2310
2317
  #ReverseEncode(symbol) {
2311
2318
  const posAlignEncoder = this.#compressor.chunker.encoder._posAlignEncoder;
2312
- var bit, m = 1;
2319
+ let bit, m = 1;
2313
2320
  for (let i = 0; i < posAlignEncoder.NumBitLevels; ++i) {
2314
2321
  bit = symbol & 1;
2315
2322
  this.#Encode_3(posAlignEncoder.Models, m, bit);
@@ -2326,18 +2333,18 @@ export class LZMA {
2326
2333
  symbol >>= 1;
2327
2334
  }
2328
2335
  }
2329
- #ReverseGetPrice(obj, symbol) {
2336
+ #ReverseGetPrice(encoder, symbol) {
2330
2337
  let bit, m = 1, price = 0;
2331
- for (let i = obj.NumBitLevels; i != 0; i -= 1) {
2338
+ for (let i = encoder.NumBitLevels; i != 0; i -= 1) {
2332
2339
  bit = symbol & 1;
2333
2340
  symbol >>>= 1;
2334
- price += this.GetPrice(obj.Models[m], bit);
2341
+ price += this.GetPrice(encoder.Models[m], bit);
2335
2342
  m = m << 1 | bit;
2336
2343
  }
2337
2344
  return price;
2338
2345
  }
2339
2346
  ReverseGetPrice(Models, startIndex, NumBitLevels, symbol) {
2340
- var bit, m = 1, price = 0;
2347
+ let bit, m = 1, price = 0;
2341
2348
  for (let i = NumBitLevels; i != 0; i -= 1) {
2342
2349
  bit = symbol & 1;
2343
2350
  symbol >>>= 1;
@@ -2353,7 +2360,7 @@ export class LZMA {
2353
2360
  if ((rangeDecoder.Code ^ this.#MIN_INT32) < (newBound ^ this.#MIN_INT32)) {
2354
2361
  rangeDecoder.Range = newBound;
2355
2362
  probs[index] = prob + (2_048 - prob >>> 5) << 16 >> 16;
2356
- if (!(rangeDecoder.Range & -16777216)) {
2363
+ if (!(rangeDecoder.Range & this.bitMaskForRange)) {
2357
2364
  rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2358
2365
  rangeDecoder.Range <<= 8;
2359
2366
  }
@@ -2363,7 +2370,7 @@ export class LZMA {
2363
2370
  rangeDecoder.Range -= newBound;
2364
2371
  rangeDecoder.Code -= newBound;
2365
2372
  probs[index] = prob - (prob >>> 5) << 16 >> 16;
2366
- if (!(rangeDecoder.Range & -16777216)) {
2373
+ if (!(rangeDecoder.Range & this.bitMaskForRange)) {
2367
2374
  rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2368
2375
  rangeDecoder.Range <<= 8;
2369
2376
  }
@@ -2378,7 +2385,7 @@ export class LZMA {
2378
2385
  let t = rangeDecoder.Code - rangeDecoder.Range >>> 31;
2379
2386
  rangeDecoder.Code -= rangeDecoder.Range & t - 1;
2380
2387
  result = result << 1 | 1 - t;
2381
- if (!(rangeDecoder.Range & -16777216)) {
2388
+ if (!(rangeDecoder.Range & this.bitMaskForRange)) {
2382
2389
  rangeDecoder.Code = rangeDecoder.Code << 8 | this.#read(rangeDecoder.Stream);
2383
2390
  rangeDecoder.Range <<= 8;
2384
2391
  }
@@ -2400,18 +2407,18 @@ export class LZMA {
2400
2407
  }
2401
2408
  #Encode_3(probs, index, symbol) {
2402
2409
  const rangeEncoder = this.#compressor.chunker.encoder._rangeEncoder;
2403
- var newBound, prob = probs[index];
2410
+ let newBound, prob = probs[index];
2404
2411
  newBound = (rangeEncoder.Range >>> 11) * prob;
2405
2412
  if (!symbol) {
2406
2413
  rangeEncoder.Range = newBound;
2407
2414
  probs[index] = prob + (2_048 - prob >>> 5) << 16 >> 16;
2408
2415
  }
2409
2416
  else {
2410
- rangeEncoder.Low = this.#add(rangeEncoder.Low, this.#and(this.#fromInt(newBound), [4294967295, 0]));
2417
+ rangeEncoder.Low = this.#add(rangeEncoder.Low, this.#and(this.#fromInt(newBound), [this.#_MAX_UINT32, 0]));
2411
2418
  rangeEncoder.Range -= newBound;
2412
2419
  probs[index] = prob - (prob >>> 5) << 16 >> 16;
2413
2420
  }
2414
- if (!(rangeEncoder.Range & -16777216)) {
2421
+ if (!(rangeEncoder.Range & this.bitMaskForRange)) {
2415
2422
  rangeEncoder.Range <<= 8;
2416
2423
  this.#ShiftLow();
2417
2424
  }
@@ -2423,7 +2430,7 @@ export class LZMA {
2423
2430
  if ((valueToEncode >>> i & 1) == 1) {
2424
2431
  rangeEncoder.Low = this.#add(rangeEncoder.Low, this.#fromInt(rangeEncoder.Range));
2425
2432
  }
2426
- if (!(rangeEncoder.Range & -16777216)) {
2433
+ if (!(rangeEncoder.Range & this.bitMaskForRange)) {
2427
2434
  rangeEncoder.Range <<= 8;
2428
2435
  this.#ShiftLow();
2429
2436
  }
@@ -2547,16 +2554,16 @@ export class LZMA {
2547
2554
  for (let i = 0; i < l; ++i) {
2548
2555
  ch = chars[i];
2549
2556
  if (ch >= 1 && ch <= 127) {
2550
- data[elen++] = ch << 24 >> 24;
2557
+ data[elen++] = ch & 0xFF;
2551
2558
  }
2552
2559
  else if (!ch || ch >= 128 && ch <= 2047) {
2553
- data[elen++] = (192 | ch >> 6 & 31) << 24 >> 24;
2554
- data[elen++] = (128 | ch & 63) << 24 >> 24;
2560
+ data[elen++] = (192 | ch >> 6 & 31) & 0xFF;
2561
+ data[elen++] = (128 | ch & 63) & 0xFF;
2555
2562
  }
2556
2563
  else {
2557
- data[elen++] = (224 | ch >> 12 & 15) << 24 >> 24;
2558
- data[elen++] = (128 | ch >> 6 & 63) << 24 >> 24;
2559
- data[elen++] = (128 | ch & 63) << 24 >> 24;
2564
+ data[elen++] = (224 | ch >> 12 & 15) & 0xFF;
2565
+ data[elen++] = (128 | ch >> 6 & 63) & 0xFF;
2566
+ data[elen++] = (128 | ch & 63) & 0xFF;
2560
2567
  }
2561
2568
  }
2562
2569
  return data;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "lzma1",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.5.0",
5
5
  "license": "Apache-2.0",
6
6
  "authors": [
7
7
  "Filip Seman <filip.seman@pm.me>",
@@ -27,14 +27,12 @@
27
27
  "fmt": "dprint fmt",
28
28
  "fmt:check": "dprint check",
29
29
  "typecheck": "tsc --noEmit",
30
- "version": "git checkout develop && npm test",
31
- "postversion": "echo 'Now run npm run build && npm publish'",
32
30
  "test": "TS_NODE_TRANSPILE_ONLY=true node --no-warnings --test --test-reporter=spec --loader=ts-node/esm ./*.test.ts",
33
31
  "test:watch": "TS_NODE_TRANSPILE_ONLY=true node --no-warnings --test --watch --test-reporter=spec --loader=ts-node/esm ./*.test.ts"
34
32
  },
35
33
  "dependencies": {},
36
34
  "devDependencies": {
37
- "@types/node": ">=18.8",
35
+ "@types/node": "^22.10.0",
38
36
  "dprint": "~0.47.0",
39
37
  "ts-node": "~10.9.0",
40
38
  "typescript": "^5.7.0"