@patrickdk77/archive-defuse 0.0.0-stage → 0.1.1

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.
Files changed (122) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +378 -2
  3. package/dist/aes7z.d.ts +16 -0
  4. package/dist/aes7z.js +131 -0
  5. package/dist/alone.d.ts +21 -0
  6. package/dist/alone.js +65 -0
  7. package/dist/bcj2.d.ts +21 -0
  8. package/dist/bcj2.js +140 -0
  9. package/dist/bzip2.d.ts +21 -0
  10. package/dist/bzip2.js +688 -0
  11. package/dist/cab.d.ts +63 -0
  12. package/dist/cab.js +493 -0
  13. package/dist/checks.d.ts +23 -0
  14. package/dist/checks.js +123 -0
  15. package/dist/cli.d.ts +2 -0
  16. package/dist/cli.js +364 -0
  17. package/dist/codecs.d.ts +39 -0
  18. package/dist/codecs.js +124 -0
  19. package/dist/coders.d.ts +58 -0
  20. package/dist/coders.js +392 -0
  21. package/dist/crc32.d.ts +8 -0
  22. package/dist/crc32.js +59 -0
  23. package/dist/detect.d.ts +10 -0
  24. package/dist/detect.js +95 -0
  25. package/dist/encoder.d.ts +44 -0
  26. package/dist/encoder.js +114 -0
  27. package/dist/engine.d.ts +44 -0
  28. package/dist/engine.js +753 -0
  29. package/dist/errors.d.ts +64 -0
  30. package/dist/errors.js +98 -0
  31. package/dist/filters.d.ts +34 -0
  32. package/dist/filters.js +385 -0
  33. package/dist/findings.d.ts +26 -0
  34. package/dist/findings.js +176 -0
  35. package/dist/formats.d.ts +24 -0
  36. package/dist/formats.js +106 -0
  37. package/dist/index.d.ts +9 -0
  38. package/dist/index.js +53 -0
  39. package/dist/index.mjs +3 -0
  40. package/dist/inflate64.d.ts +6 -0
  41. package/dist/inflate64.js +388 -0
  42. package/dist/io.d.ts +78 -0
  43. package/dist/io.js +379 -0
  44. package/dist/lzma-formats.d.ts +75 -0
  45. package/dist/lzma-formats.js +373 -0
  46. package/dist/lzma-sniff.d.ts +20 -0
  47. package/dist/lzma-sniff.js +73 -0
  48. package/dist/lzma-writers.d.ts +35 -0
  49. package/dist/lzma-writers.js +276 -0
  50. package/dist/lzma1/LICENSE +201 -0
  51. package/dist/lzma1/NOTICE +36 -0
  52. package/dist/lzma1/crc32.d.ts +4 -0
  53. package/dist/lzma1/crc32.js +19 -0
  54. package/dist/lzma1/hash234.d.ts +22 -0
  55. package/dist/lzma1/hash234.js +75 -0
  56. package/dist/lzma1/hc4.d.ts +30 -0
  57. package/dist/lzma1/hc4.js +142 -0
  58. package/dist/lzma1/header.d.ts +32 -0
  59. package/dist/lzma1/header.js +73 -0
  60. package/dist/lzma1/lz-decoder.d.ts +41 -0
  61. package/dist/lzma1/lz-decoder.js +128 -0
  62. package/dist/lzma1/lz-encoder.d.ts +87 -0
  63. package/dist/lzma1/lz-encoder.js +166 -0
  64. package/dist/lzma1/lzma-coder.d.ts +107 -0
  65. package/dist/lzma1/lzma-coder.js +162 -0
  66. package/dist/lzma1/lzma-decoder.d.ts +36 -0
  67. package/dist/lzma1/lzma-decoder.js +484 -0
  68. package/dist/lzma1/lzma-encoder-fast.d.ts +13 -0
  69. package/dist/lzma1/lzma-encoder-fast.js +114 -0
  70. package/dist/lzma1/lzma-encoder.d.ts +105 -0
  71. package/dist/lzma1/lzma-encoder.js +497 -0
  72. package/dist/lzma1/lzma2-decoder.d.ts +85 -0
  73. package/dist/lzma1/lzma2-decoder.js +249 -0
  74. package/dist/lzma1/range-coder.d.ts +18 -0
  75. package/dist/lzma1/range-coder.js +23 -0
  76. package/dist/lzma1/range-decoder.d.ts +41 -0
  77. package/dist/lzma1/range-decoder.js +78 -0
  78. package/dist/lzma1/range-encoder.d.ts +53 -0
  79. package/dist/lzma1/range-encoder.js +269 -0
  80. package/dist/lzma1-stream.d.ts +60 -0
  81. package/dist/lzma1-stream.js +202 -0
  82. package/dist/model.d.ts +141 -0
  83. package/dist/model.js +16 -0
  84. package/dist/names.d.ts +13 -0
  85. package/dist/names.js +66 -0
  86. package/dist/pack.d.ts +79 -0
  87. package/dist/pack.js +590 -0
  88. package/dist/plugins.d.ts +13 -0
  89. package/dist/plugins.js +56 -0
  90. package/dist/rules.d.ts +25 -0
  91. package/dist/rules.js +403 -0
  92. package/dist/score.d.ts +17 -0
  93. package/dist/score.js +58 -0
  94. package/dist/seven.d.ts +75 -0
  95. package/dist/seven.js +492 -0
  96. package/dist/sevenzip.d.ts +133 -0
  97. package/dist/sevenzip.js +529 -0
  98. package/dist/single.d.ts +103 -0
  99. package/dist/single.js +453 -0
  100. package/dist/sniff.d.ts +20 -0
  101. package/dist/sniff.js +224 -0
  102. package/dist/stream-meter.d.ts +39 -0
  103. package/dist/stream-meter.js +54 -0
  104. package/dist/tar.d.ts +102 -0
  105. package/dist/tar.js +765 -0
  106. package/dist/types.d.ts +311 -0
  107. package/dist/types.js +133 -0
  108. package/dist/unpack.d.ts +67 -0
  109. package/dist/unpack.js +194 -0
  110. package/dist/xz.d.ts +25 -0
  111. package/dist/xz.js +333 -0
  112. package/dist/zip-lzma.d.ts +15 -0
  113. package/dist/zip-lzma.js +137 -0
  114. package/dist/zip-writer.d.ts +51 -0
  115. package/dist/zip-writer.js +305 -0
  116. package/dist/zip.d.ts +99 -0
  117. package/dist/zip.js +901 -0
  118. package/dist/zipcrypt.d.ts +47 -0
  119. package/dist/zipcrypt.js +233 -0
  120. package/dist/zstd.d.ts +25 -0
  121. package/dist/zstd.js +160 -0
  122. package/package.json +94 -4
@@ -0,0 +1,484 @@
1
+ "use strict";
2
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
3
+ /**
4
+ * LZMA decoder: turns range-coded symbols back into literals and matches.
5
+ *
6
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov. The main
7
+ * loop is structured like `LzmaDec_DecodeReal` of the LZMA SDK.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.LzmaDecoder = void 0;
11
+ const lzma_coder_1 = require("./lzma-coder");
12
+ const range_coder_1 = require("./range-coder");
13
+ /** Distance value that marks the end of the stream. */
14
+ const END_MARKER_DIST = -1;
15
+ class LzmaDecoder extends lzma_coder_1.LzmaCoder {
16
+ lz;
17
+ rc;
18
+ /** Number of bytes decoded so far. */
19
+ outPos = 0;
20
+ constructor(lz, rc, lc, lp, pb, probs) {
21
+ super(lc, lp, pb, probs);
22
+ this.lz = lz;
23
+ this.rc = rc;
24
+ this.reset();
25
+ }
26
+ /**
27
+ * Decodes symbols until `outLimit` bytes have been produced or the input
28
+ * position passes `inLimit`.
29
+ *
30
+ * Performance: this is the hot loop of decompression. The range decoder
31
+ * state, the coder state and the reps live in local variables, and the
32
+ * bit decoding step (`decodeBit` in `range-coder_test.ts`) is repeated inline:
33
+ *
34
+ * prob = probs[i];
35
+ * bound = (range >>> 11) * prob;
36
+ * if (code < bound) { range = bound; probs[i] = prob + ((2048 - prob) >>> 5); bit 0 }
37
+ * else { range -= bound; code -= bound; probs[i] = prob - (prob >>> 5); bit 1 }
38
+ * if (range < 2^24) { shift in the next input byte }
39
+ *
40
+ * With the state in object fields, engines store and reload it around
41
+ * every probability update, which costs 10-15% on V8.
42
+ *
43
+ * @returns `true` if the end marker was decoded
44
+ */
45
+ decode(outLimit, inLimit) {
46
+ const probs = this.probs;
47
+ const lz = this.lz;
48
+ const reps = this.reps;
49
+ const posMask = this.posMask;
50
+ const rc = this.rc;
51
+ const input = rc.input;
52
+ const inputEnd = input.length;
53
+ let range = rc.range;
54
+ let code = rc.code;
55
+ let inPos = rc.pos;
56
+ let state = this.state;
57
+ let outPos = this.outPos;
58
+ let rep0 = reps[0];
59
+ let rep1 = reps[1];
60
+ let rep2 = reps[2];
61
+ let rep3 = reps[3];
62
+ let endMarker = false;
63
+ let i = 0;
64
+ let prob = 0;
65
+ let bound = 0;
66
+ while (outPos < outLimit && inPos <= inLimit) {
67
+ const posState = outPos & posMask;
68
+ // -- Literal or match? --
69
+ i = lzma_coder_1.IS_MATCH + (state << 4) + posState;
70
+ prob = probs[i];
71
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
72
+ if (code < bound) {
73
+ range = bound;
74
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
75
+ if (range < range_coder_1.TOP_VALUE) {
76
+ if (inPos >= inputEnd)
77
+ throw truncated();
78
+ range = (range << 8) >>> 0;
79
+ code = ((code << 8) | input[inPos++]) >>> 0;
80
+ }
81
+ // -- Literal: 8 bits, MSB first --
82
+ const offset = this.literalOffset(lz.getByte(0), outPos);
83
+ let symbol = 1;
84
+ if ((0, lzma_coder_1.isLiteralState)(state)) {
85
+ do {
86
+ i = offset + symbol;
87
+ prob = probs[i];
88
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
89
+ if (code < bound) {
90
+ range = bound;
91
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
92
+ symbol <<= 1;
93
+ }
94
+ else {
95
+ range -= bound;
96
+ code -= bound;
97
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
98
+ symbol = (symbol << 1) | 1;
99
+ }
100
+ if (range < range_coder_1.TOP_VALUE) {
101
+ if (inPos >= inputEnd)
102
+ throw truncated();
103
+ range = (range << 8) >>> 0;
104
+ code = ((code << 8) | input[inPos++]) >>> 0;
105
+ }
106
+ } while (symbol < 0x100);
107
+ }
108
+ else {
109
+ // After a match, the bits of the byte at rep0 select the
110
+ // probabilities until the first bit that differs.
111
+ let matchByte = lz.getByte(rep0);
112
+ let matchOffset = 0x100;
113
+ do {
114
+ matchByte <<= 1;
115
+ const matchBit = matchByte & matchOffset;
116
+ i = offset + matchOffset + matchBit + symbol;
117
+ prob = probs[i];
118
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
119
+ if (code < bound) {
120
+ range = bound;
121
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
122
+ symbol <<= 1;
123
+ matchOffset &= ~matchBit;
124
+ }
125
+ else {
126
+ range -= bound;
127
+ code -= bound;
128
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
129
+ symbol = (symbol << 1) | 1;
130
+ matchOffset &= matchBit;
131
+ }
132
+ if (range < range_coder_1.TOP_VALUE) {
133
+ if (inPos >= inputEnd)
134
+ throw truncated();
135
+ range = (range << 8) >>> 0;
136
+ code = ((code << 8) | input[inPos++]) >>> 0;
137
+ }
138
+ } while (symbol < 0x100);
139
+ }
140
+ lz.putByte(symbol & 0xff);
141
+ state = (0, lzma_coder_1.stateAfterLiteral)(state);
142
+ ++outPos;
143
+ continue;
144
+ }
145
+ range -= bound;
146
+ code -= bound;
147
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
148
+ if (range < range_coder_1.TOP_VALUE) {
149
+ if (inPos >= inputEnd)
150
+ throw truncated();
151
+ range = (range << 8) >>> 0;
152
+ code = ((code << 8) | input[inPos++]) >>> 0;
153
+ }
154
+ // -- Match or rep match? --
155
+ let lenCoder;
156
+ i = lzma_coder_1.IS_REP + state;
157
+ prob = probs[i];
158
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
159
+ if (code < bound) {
160
+ range = bound;
161
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
162
+ if (range < range_coder_1.TOP_VALUE) {
163
+ if (inPos >= inputEnd)
164
+ throw truncated();
165
+ range = (range << 8) >>> 0;
166
+ code = ((code << 8) | input[inPos++]) >>> 0;
167
+ }
168
+ // Match: the distance follows the length.
169
+ state = (0, lzma_coder_1.stateAfterMatch)(state);
170
+ rep3 = rep2;
171
+ rep2 = rep1;
172
+ rep1 = rep0;
173
+ lenCoder = lzma_coder_1.MATCH_LEN;
174
+ }
175
+ else {
176
+ range -= bound;
177
+ code -= bound;
178
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
179
+ if (range < range_coder_1.TOP_VALUE) {
180
+ if (inPos >= inputEnd)
181
+ throw truncated();
182
+ range = (range << 8) >>> 0;
183
+ code = ((code << 8) | input[inPos++]) >>> 0;
184
+ }
185
+ // Rep match: one of the four recent distances.
186
+ i = lzma_coder_1.IS_REP0 + state;
187
+ prob = probs[i];
188
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
189
+ if (code < bound) {
190
+ range = bound;
191
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
192
+ if (range < range_coder_1.TOP_VALUE) {
193
+ if (inPos >= inputEnd)
194
+ throw truncated();
195
+ range = (range << 8) >>> 0;
196
+ code = ((code << 8) | input[inPos++]) >>> 0;
197
+ }
198
+ // rep0: a single byte ("short rep") or a longer match?
199
+ i = lzma_coder_1.IS_REP0_LONG + (state << 4) + posState;
200
+ prob = probs[i];
201
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
202
+ if (code < bound) {
203
+ range = bound;
204
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
205
+ if (range < range_coder_1.TOP_VALUE) {
206
+ if (inPos >= inputEnd)
207
+ throw truncated();
208
+ range = (range << 8) >>> 0;
209
+ code = ((code << 8) | input[inPos++]) >>> 0;
210
+ }
211
+ state = (0, lzma_coder_1.stateAfterShortRep)(state);
212
+ if (outPos >= outLimit)
213
+ throw exceedsSize();
214
+ lz.repeat(rep0, 1);
215
+ ++outPos;
216
+ continue;
217
+ }
218
+ range -= bound;
219
+ code -= bound;
220
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
221
+ if (range < range_coder_1.TOP_VALUE) {
222
+ if (inPos >= inputEnd)
223
+ throw truncated();
224
+ range = (range << 8) >>> 0;
225
+ code = ((code << 8) | input[inPos++]) >>> 0;
226
+ }
227
+ }
228
+ else {
229
+ range -= bound;
230
+ code -= bound;
231
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
232
+ if (range < range_coder_1.TOP_VALUE) {
233
+ if (inPos >= inputEnd)
234
+ throw truncated();
235
+ range = (range << 8) >>> 0;
236
+ code = ((code << 8) | input[inPos++]) >>> 0;
237
+ }
238
+ let dist;
239
+ // rep1, or rep2/rep3?
240
+ i = lzma_coder_1.IS_REP1 + state;
241
+ prob = probs[i];
242
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
243
+ if (code < bound) {
244
+ range = bound;
245
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
246
+ dist = rep1;
247
+ }
248
+ else {
249
+ range -= bound;
250
+ code -= bound;
251
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
252
+ if (range < range_coder_1.TOP_VALUE) {
253
+ if (inPos >= inputEnd)
254
+ throw truncated();
255
+ range = (range << 8) >>> 0;
256
+ code = ((code << 8) | input[inPos++]) >>> 0;
257
+ }
258
+ // rep2 or rep3?
259
+ i = lzma_coder_1.IS_REP2 + state;
260
+ prob = probs[i];
261
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
262
+ if (code < bound) {
263
+ range = bound;
264
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
265
+ dist = rep2;
266
+ }
267
+ else {
268
+ range -= bound;
269
+ code -= bound;
270
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
271
+ dist = rep3;
272
+ rep3 = rep2;
273
+ }
274
+ rep2 = rep1;
275
+ }
276
+ if (range < range_coder_1.TOP_VALUE) {
277
+ if (inPos >= inputEnd)
278
+ throw truncated();
279
+ range = (range << 8) >>> 0;
280
+ code = ((code << 8) | input[inPos++]) >>> 0;
281
+ }
282
+ rep1 = rep0;
283
+ rep0 = dist;
284
+ }
285
+ state = (0, lzma_coder_1.stateAfterLongRep)(state);
286
+ lenCoder = lzma_coder_1.REP_LEN;
287
+ }
288
+ // -- Length: choice bits select the low, mid or high tree --
289
+ let treeOffset;
290
+ let treeBits;
291
+ let len;
292
+ i = lenCoder + lzma_coder_1.LEN_CHOICE;
293
+ prob = probs[i];
294
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
295
+ if (code < bound) {
296
+ range = bound;
297
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
298
+ treeOffset = lenCoder + lzma_coder_1.LEN_LOW + posState * lzma_coder_1.LOW_SYMBOLS;
299
+ treeBits = 3;
300
+ len = lzma_coder_1.MATCH_LEN_MIN;
301
+ }
302
+ else {
303
+ range -= bound;
304
+ code -= bound;
305
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
306
+ if (range < range_coder_1.TOP_VALUE) {
307
+ if (inPos >= inputEnd)
308
+ throw truncated();
309
+ range = (range << 8) >>> 0;
310
+ code = ((code << 8) | input[inPos++]) >>> 0;
311
+ }
312
+ i = lenCoder + lzma_coder_1.LEN_CHOICE2;
313
+ prob = probs[i];
314
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
315
+ if (code < bound) {
316
+ range = bound;
317
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
318
+ treeOffset = lenCoder + lzma_coder_1.LEN_MID + posState * lzma_coder_1.MID_SYMBOLS;
319
+ treeBits = 3;
320
+ len = lzma_coder_1.MATCH_LEN_MIN + lzma_coder_1.LOW_SYMBOLS;
321
+ }
322
+ else {
323
+ range -= bound;
324
+ code -= bound;
325
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
326
+ treeOffset = lenCoder + lzma_coder_1.LEN_HIGH;
327
+ treeBits = 8;
328
+ len = lzma_coder_1.MATCH_LEN_MIN + lzma_coder_1.LOW_SYMBOLS + lzma_coder_1.MID_SYMBOLS;
329
+ }
330
+ }
331
+ if (range < range_coder_1.TOP_VALUE) {
332
+ if (inPos >= inputEnd)
333
+ throw truncated();
334
+ range = (range << 8) >>> 0;
335
+ code = ((code << 8) | input[inPos++]) >>> 0;
336
+ }
337
+ {
338
+ const end = 1 << treeBits;
339
+ let symbol = 1;
340
+ do {
341
+ i = treeOffset + symbol;
342
+ prob = probs[i];
343
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
344
+ if (code < bound) {
345
+ range = bound;
346
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
347
+ symbol <<= 1;
348
+ }
349
+ else {
350
+ range -= bound;
351
+ code -= bound;
352
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
353
+ symbol = (symbol << 1) | 1;
354
+ }
355
+ if (range < range_coder_1.TOP_VALUE) {
356
+ if (inPos >= inputEnd)
357
+ throw truncated();
358
+ range = (range << 8) >>> 0;
359
+ code = ((code << 8) | input[inPos++]) >>> 0;
360
+ }
361
+ } while (symbol < end);
362
+ len += symbol - end;
363
+ }
364
+ // -- Distance (normal matches only) --
365
+ if (lenCoder === lzma_coder_1.MATCH_LEN) {
366
+ // Distance slot: 6 bits, MSB first
367
+ const slotTree = lzma_coder_1.DIST_SLOTS + ((0, lzma_coder_1.getDistState)(len) << lzma_coder_1.DIST_SLOT_BITS);
368
+ let distSlot = 1;
369
+ do {
370
+ i = slotTree + distSlot;
371
+ prob = probs[i];
372
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
373
+ if (code < bound) {
374
+ range = bound;
375
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
376
+ distSlot <<= 1;
377
+ }
378
+ else {
379
+ range -= bound;
380
+ code -= bound;
381
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
382
+ distSlot = (distSlot << 1) | 1;
383
+ }
384
+ if (range < range_coder_1.TOP_VALUE) {
385
+ if (inPos >= inputEnd)
386
+ throw truncated();
387
+ range = (range << 8) >>> 0;
388
+ code = ((code << 8) | input[inPos++]) >>> 0;
389
+ }
390
+ } while (distSlot < 1 << lzma_coder_1.DIST_SLOT_BITS);
391
+ distSlot -= 1 << lzma_coder_1.DIST_SLOT_BITS;
392
+ if (distSlot < lzma_coder_1.DIST_MODEL_START) {
393
+ rep0 = distSlot;
394
+ }
395
+ else {
396
+ // The slot gives the top two bits; the footer bits follow.
397
+ const footerBits = (distSlot >>> 1) - 1;
398
+ let dist = (2 | (distSlot & 1)) << footerBits;
399
+ let reverseTree;
400
+ let reverseBits;
401
+ if (distSlot < lzma_coder_1.DIST_MODEL_END) {
402
+ reverseTree = (0, lzma_coder_1.distSpecialOffset)(distSlot, dist);
403
+ reverseBits = footerBits;
404
+ }
405
+ else {
406
+ // Direct bits with a fixed 50% probability, then 4
407
+ // align bits with probabilities.
408
+ let direct = 0;
409
+ for (let k = footerBits - lzma_coder_1.ALIGN_BITS; k > 0; --k) {
410
+ range >>>= 1;
411
+ let bit = 0;
412
+ if (code >= range) {
413
+ code -= range;
414
+ bit = 1;
415
+ }
416
+ direct = ((direct << 1) | bit) >>> 0;
417
+ if (range < range_coder_1.TOP_VALUE) {
418
+ if (inPos >= inputEnd)
419
+ throw truncated();
420
+ range = (range << 8) >>> 0;
421
+ code = ((code << 8) | input[inPos++]) >>> 0;
422
+ }
423
+ }
424
+ dist |= direct << lzma_coder_1.ALIGN_BITS;
425
+ reverseTree = lzma_coder_1.DIST_ALIGN;
426
+ reverseBits = lzma_coder_1.ALIGN_BITS;
427
+ }
428
+ // Footer or align bits, LSB first
429
+ let symbol = 1;
430
+ for (let k = 0; k < reverseBits; ++k) {
431
+ i = reverseTree + symbol;
432
+ prob = probs[i];
433
+ bound = (range >>> range_coder_1.BIT_MODEL_TOTAL_BITS) * prob;
434
+ if (code < bound) {
435
+ range = bound;
436
+ probs[i] = prob + ((range_coder_1.BIT_MODEL_TOTAL - prob) >>> range_coder_1.MOVE_BITS);
437
+ symbol <<= 1;
438
+ }
439
+ else {
440
+ range -= bound;
441
+ code -= bound;
442
+ probs[i] = prob - (prob >>> range_coder_1.MOVE_BITS);
443
+ symbol = (symbol << 1) | 1;
444
+ dist |= 1 << k;
445
+ }
446
+ if (range < range_coder_1.TOP_VALUE) {
447
+ if (inPos >= inputEnd)
448
+ throw truncated();
449
+ range = (range << 8) >>> 0;
450
+ code = ((code << 8) | input[inPos++]) >>> 0;
451
+ }
452
+ }
453
+ // As int32, the end marker distance 0xFFFFFFFF is -1.
454
+ rep0 = dist | 0;
455
+ if (rep0 === END_MARKER_DIST) {
456
+ endMarker = true;
457
+ break;
458
+ }
459
+ }
460
+ }
461
+ if (len > outLimit - outPos)
462
+ throw exceedsSize();
463
+ lz.repeat(rep0, len);
464
+ outPos += len;
465
+ }
466
+ rc.range = range;
467
+ rc.code = code;
468
+ rc.pos = inPos;
469
+ this.state = state;
470
+ this.outPos = outPos;
471
+ reps[0] = rep0;
472
+ reps[1] = rep1;
473
+ reps[2] = rep2;
474
+ reps[3] = rep3;
475
+ return endMarker;
476
+ }
477
+ }
478
+ exports.LzmaDecoder = LzmaDecoder;
479
+ function truncated() {
480
+ return new Error('Truncated input');
481
+ }
482
+ function exceedsSize() {
483
+ return new Error('Corrupted input: data exceeds the declared size');
484
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Fast LZMA encoder: picks symbols with simple heuristics, similar to a
3
+ * lazy-matching LZ77 encoder.
4
+ *
5
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
6
+ */
7
+ import { LzmaEncoder } from './lzma-encoder';
8
+ export declare const FAST_EXTRA_SIZE_BEFORE = 1;
9
+ export declare const FAST_EXTRA_SIZE_AFTER: number;
10
+ export declare class LzmaEncoderFast extends LzmaEncoder {
11
+ private matches;
12
+ protected getNextSymbol(): number;
13
+ }
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
3
+ /**
4
+ * Fast LZMA encoder: picks symbols with simple heuristics, similar to a
5
+ * lazy-matching LZ77 encoder.
6
+ *
7
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.LzmaEncoderFast = exports.FAST_EXTRA_SIZE_AFTER = exports.FAST_EXTRA_SIZE_BEFORE = void 0;
11
+ const lzma_coder_1 = require("./lzma-coder");
12
+ const lzma_encoder_1 = require("./lzma-encoder");
13
+ exports.FAST_EXTRA_SIZE_BEFORE = 1;
14
+ exports.FAST_EXTRA_SIZE_AFTER = lzma_coder_1.MATCH_LEN_MAX - 1;
15
+ /** True if `smallDist` is so much shorter that it's worth a byte of length. */
16
+ function changePair(smallDist, bigDist) {
17
+ return smallDist < bigDist >>> 7;
18
+ }
19
+ class LzmaEncoderFast extends lzma_encoder_1.LzmaEncoder {
20
+ matches = null;
21
+ getNextSymbol() {
22
+ // Get the matches for the next byte unless they were already read
23
+ // during the previous call.
24
+ if (this.readAhead === -1) {
25
+ this.matches = this.getMatches();
26
+ }
27
+ this.back = -1;
28
+ // Not enough bytes left for a match: encode a literal.
29
+ const avail = Math.min(this.lz.getAvail(), lzma_coder_1.MATCH_LEN_MAX);
30
+ if (avail < lzma_coder_1.MATCH_LEN_MIN) {
31
+ return 1;
32
+ }
33
+ // Look for a match at one of the four recent distances.
34
+ let bestRepLen = 0;
35
+ let bestRepIndex = 0;
36
+ for (let rep = 0; rep < lzma_coder_1.REPS; ++rep) {
37
+ const len = this.lz.getMatchLen(0, this.reps[rep], avail);
38
+ if (len < lzma_coder_1.MATCH_LEN_MIN)
39
+ continue;
40
+ // Long enough: take it.
41
+ if (len >= this.niceLen) {
42
+ this.back = rep;
43
+ this.skip(len - 1);
44
+ return len;
45
+ }
46
+ if (len > bestRepLen) {
47
+ bestRepIndex = rep;
48
+ bestRepLen = len;
49
+ }
50
+ }
51
+ let mainLen = 0;
52
+ let mainDist = 0;
53
+ let matches = this.matches;
54
+ if (matches === null)
55
+ throw new Error('No matches were read');
56
+ if (matches.count > 0) {
57
+ mainLen = matches.len[matches.count - 1];
58
+ mainDist = matches.dist[matches.count - 1];
59
+ if (mainLen >= this.niceLen) {
60
+ this.back = mainDist + lzma_coder_1.REPS;
61
+ this.skip(mainLen - 1);
62
+ return mainLen;
63
+ }
64
+ // Prefer a one byte shorter match if its distance is much smaller.
65
+ while (matches.count > 1 && mainLen === matches.len[matches.count - 2] + 1) {
66
+ if (!changePair(matches.dist[matches.count - 2], mainDist))
67
+ break;
68
+ --matches.count;
69
+ mainLen = matches.len[matches.count - 1];
70
+ mainDist = matches.dist[matches.count - 1];
71
+ }
72
+ // A 2-byte match with a big distance costs more than two literals.
73
+ if (mainLen === lzma_coder_1.MATCH_LEN_MIN && mainDist >= 0x80) {
74
+ mainLen = 1;
75
+ }
76
+ }
77
+ // Prefer a rep match unless the normal match is clearly longer.
78
+ if (bestRepLen >= lzma_coder_1.MATCH_LEN_MIN) {
79
+ if (bestRepLen + 1 >= mainLen || (bestRepLen + 2 >= mainLen && mainDist >= 1 << 9) || (bestRepLen + 3 >= mainLen && mainDist >= 1 << 15)) {
80
+ this.back = bestRepIndex;
81
+ this.skip(bestRepLen - 1);
82
+ return bestRepLen;
83
+ }
84
+ }
85
+ if (mainLen < lzma_coder_1.MATCH_LEN_MIN || avail <= lzma_coder_1.MATCH_LEN_MIN) {
86
+ return 1;
87
+ }
88
+ // Lazy matching: if the next position has a better match, encode
89
+ // the current byte as a literal.
90
+ this.matches = this.getMatches();
91
+ matches = this.matches;
92
+ if (matches.count > 0) {
93
+ const newLen = matches.len[matches.count - 1];
94
+ const newDist = matches.dist[matches.count - 1];
95
+ if ((newLen >= mainLen && newDist < mainDist) ||
96
+ (newLen === mainLen + 1 && !changePair(mainDist, newDist)) ||
97
+ newLen > mainLen + 1 ||
98
+ (newLen + 1 >= mainLen && mainLen >= lzma_coder_1.MATCH_LEN_MIN + 1 && changePair(newDist, mainDist))) {
99
+ return 1;
100
+ }
101
+ }
102
+ // A rep match of almost the same length is cheaper.
103
+ const limit = Math.max(mainLen - 1, lzma_coder_1.MATCH_LEN_MIN);
104
+ for (let rep = 0; rep < lzma_coder_1.REPS; ++rep) {
105
+ if (this.lz.getMatchLen(0, this.reps[rep], limit) === limit) {
106
+ return 1;
107
+ }
108
+ }
109
+ this.back = mainDist + lzma_coder_1.REPS;
110
+ this.skip(mainLen - 2);
111
+ return mainLen;
112
+ }
113
+ }
114
+ exports.LzmaEncoderFast = LzmaEncoderFast;