@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
package/dist/bzip2.js ADDED
@@ -0,0 +1,688 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bunzip2 = bunzip2;
4
+ exports.bzip2 = bzip2;
5
+ const errors_1 = require("./errors");
6
+ /** bzip2's CRC-32: ZIP's polynomial, most significant bit first. */
7
+ const CRC_TABLE = new Uint32Array(256);
8
+ for (let i = 0; i < 256; i++) {
9
+ let c = i << 24;
10
+ for (let k = 0; k < 8; k++)
11
+ c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : c << 1;
12
+ CRC_TABLE[i] = c >>> 0;
13
+ }
14
+ const BLOCK_MAGIC_HI = 0x314159;
15
+ const BLOCK_MAGIC_LO = 0x265359;
16
+ const END_MAGIC_HI = 0x177245;
17
+ const END_MAGIC_LO = 0x385090;
18
+ const MAX_GROUPS = 6;
19
+ const MAX_ALPHA = 258;
20
+ const MAX_CODE_LEN = 20;
21
+ const GROUP_SIZE = 50;
22
+ /** bzip2 1.0.8 reads at most this many selectors and skips the rest. */
23
+ const MAX_SELECTORS = 18002;
24
+ /**
25
+ * The table bzip2 0.9.0 used to randomise blocks of repetitive data, which later versions still read. Every byte of
26
+ * such a block, before the run-length decoding, has its low bit flipped where the table's countdown reaches 1.
27
+ */
28
+ const RAND_NUMS = [
29
+ 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, 419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
30
+ 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, 170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
31
+ 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, 161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
32
+ 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, 469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
33
+ 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, 652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
34
+ 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, 411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
35
+ 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, 944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
36
+ 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, 686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
37
+ 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, 707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
38
+ 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, 140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
39
+ 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, 896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
40
+ 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, 372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
41
+ 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, 936, 638,
42
+ ];
43
+ /** The largest compressed block: 900,001 symbols of 20 bits, with room for the tables. */
44
+ const MAX_BLOCK_BYTES = 3 * 1024 * 1024;
45
+ const OUT_CHUNK = 65536;
46
+ /** Reads bits, most significant first, from chunks that arrive as the decoder asks for them. */
47
+ class BitInput {
48
+ it;
49
+ buf = new Uint8Array(0);
50
+ pos = 0;
51
+ acc = 0;
52
+ n = 0;
53
+ /** Offset in the whole input of buf[0]. */
54
+ base = 0;
55
+ done = false;
56
+ constructor(it) {
57
+ this.it = it;
58
+ }
59
+ /** Makes sure `min` bytes are buffered, unless the input ends first. */
60
+ async fill(min) {
61
+ while (!this.done && this.buf.length - this.pos < min) {
62
+ const r = await this.it.next();
63
+ if (r.done) {
64
+ this.done = true;
65
+ break;
66
+ }
67
+ // Bytes already taken into the bit buffer stay, so peek() and offset still find them.
68
+ const keep = this.pos - Math.ceil(this.n / 8);
69
+ const rest = this.buf.subarray(keep);
70
+ const next = new Uint8Array(rest.length + r.value.length);
71
+ next.set(rest, 0);
72
+ next.set(r.value, rest.length);
73
+ this.base += keep;
74
+ this.buf = next;
75
+ this.pos -= keep;
76
+ }
77
+ }
78
+ bits(k) {
79
+ while (this.n < k) {
80
+ if (this.pos >= this.buf.length)
81
+ throw new errors_1.DataError('The bzip2 data ends early', true);
82
+ this.acc = (this.acc << 8) | this.buf[this.pos++];
83
+ this.n += 8;
84
+ }
85
+ this.n -= k;
86
+ return (this.acc >>> this.n) & ((1 << k) - 1);
87
+ }
88
+ bit() {
89
+ return this.bits(1);
90
+ }
91
+ /** Drops the rest of the current byte. */
92
+ align() {
93
+ this.n -= this.n % 8;
94
+ }
95
+ /** Offset in the whole input of the next byte not yet taken into the bit buffer, after align(). */
96
+ get offset() {
97
+ return this.base + this.pos - this.n / 8;
98
+ }
99
+ /** The next `count` buffered bytes after align(), without taking them. */
100
+ peek(count) {
101
+ const at = this.pos - this.n / 8;
102
+ return this.buf.subarray(at, at + count);
103
+ }
104
+ /** Counts the rest of the input, and whether it is all zero, taking it. */
105
+ async drain() {
106
+ let bytes = 0;
107
+ let zeros = true;
108
+ const tally = (b) => {
109
+ bytes += b.length;
110
+ if (zeros && b.some(x => x !== 0))
111
+ zeros = false;
112
+ };
113
+ tally(this.peek(this.buf.length));
114
+ this.pos = this.buf.length;
115
+ this.n = 0;
116
+ for (;;) {
117
+ const r = await this.it.next();
118
+ if (r.done)
119
+ break;
120
+ tally(r.value);
121
+ }
122
+ return { bytes, zeros };
123
+ }
124
+ }
125
+ function decodeTable(lengths, alphaSize) {
126
+ let minLen = 32;
127
+ let maxLen = 0;
128
+ for (let i = 0; i < alphaSize; i++) {
129
+ minLen = Math.min(minLen, lengths[i]);
130
+ maxLen = Math.max(maxLen, lengths[i]);
131
+ }
132
+ const perm = new Int32Array(MAX_ALPHA);
133
+ const base = new Int32Array(MAX_CODE_LEN + 3);
134
+ const limit = new Int32Array(MAX_CODE_LEN + 3);
135
+ let pp = 0;
136
+ for (let i = minLen; i <= maxLen; i++)
137
+ for (let j = 0; j < alphaSize; j++)
138
+ if (lengths[j] === i)
139
+ perm[pp++] = j;
140
+ for (let i = 0; i < alphaSize; i++)
141
+ base[lengths[i] + 1]++;
142
+ for (let i = 1; i < base.length; i++)
143
+ base[i] += base[i - 1];
144
+ let vec = 0;
145
+ for (let i = minLen; i <= maxLen; i++) {
146
+ vec += base[i + 1] - base[i];
147
+ limit[i] = vec - 1;
148
+ vec <<= 1;
149
+ }
150
+ for (let i = minLen + 1; i <= maxLen; i++)
151
+ base[i] = ((limit[i - 1] + 1) << 1) - base[i];
152
+ return { limit, base, perm, minLen };
153
+ }
154
+ /** Reads one block after its magic number. */
155
+ function readBlock(input, level, tt) {
156
+ const crc = ((input.bits(16) << 16) | input.bits(16)) >>> 0;
157
+ const randomised = input.bit() === 1;
158
+ const origPtr = input.bits(24);
159
+ const used = input.bits(16);
160
+ const seqToUnseq = new Uint8Array(256);
161
+ let nInUse = 0;
162
+ for (let i = 0; i < 16; i++) {
163
+ if (!(used & (0x8000 >> i)))
164
+ continue;
165
+ const bits = input.bits(16);
166
+ for (let j = 0; j < 16; j++)
167
+ if (bits & (0x8000 >> j))
168
+ seqToUnseq[nInUse++] = i * 16 + j;
169
+ }
170
+ if (nInUse === 0)
171
+ throw new errors_1.DataError('A bzip2 block uses no byte values');
172
+ const alphaSize = nInUse + 2;
173
+ const nGroups = input.bits(3);
174
+ if (nGroups < 2 || nGroups > MAX_GROUPS)
175
+ throw new errors_1.DataError('A bzip2 block has a bad number of Huffman tables');
176
+ const nSelectors = input.bits(15);
177
+ if (nSelectors < 1)
178
+ throw new errors_1.DataError('A bzip2 block has no selectors');
179
+ const selectors = new Uint8Array(Math.min(nSelectors, MAX_SELECTORS));
180
+ const mtfGroups = [0, 1, 2, 3, 4, 5];
181
+ for (let i = 0; i < nSelectors; i++) {
182
+ let j = 0;
183
+ while (input.bit()) {
184
+ j++;
185
+ if (j >= nGroups)
186
+ throw new errors_1.DataError('A bzip2 selector names a table that is not there');
187
+ }
188
+ if (i >= MAX_SELECTORS)
189
+ continue;
190
+ const v = mtfGroups[j];
191
+ for (let k = j; k > 0; k--)
192
+ mtfGroups[k] = mtfGroups[k - 1];
193
+ mtfGroups[0] = v;
194
+ selectors[i] = v;
195
+ }
196
+ const tables = [];
197
+ const lengths = new Uint8Array(MAX_ALPHA);
198
+ for (let t = 0; t < nGroups; t++) {
199
+ let curr = input.bits(5);
200
+ for (let i = 0; i < alphaSize; i++) {
201
+ for (;;) {
202
+ if (curr < 1 || curr > MAX_CODE_LEN)
203
+ throw new errors_1.DataError('A bzip2 Huffman code length is out of range');
204
+ if (!input.bit())
205
+ break;
206
+ curr += input.bit() ? -1 : 1;
207
+ }
208
+ lengths[i] = curr;
209
+ }
210
+ tables.push(decodeTable(lengths, alphaSize));
211
+ }
212
+ const eob = nInUse + 1;
213
+ const max = 100000 * level;
214
+ const counts = new Int32Array(256);
215
+ const mtf = new Uint8Array(256);
216
+ for (let i = 0; i < 256; i++)
217
+ mtf[i] = i;
218
+ let length = 0;
219
+ let group = -1;
220
+ let left = 0;
221
+ let table = tables[0];
222
+ const next = () => {
223
+ if (left === 0) {
224
+ group++;
225
+ if (group >= selectors.length)
226
+ throw new errors_1.DataError('A bzip2 block runs past its selectors');
227
+ table = tables[selectors[group]];
228
+ left = GROUP_SIZE;
229
+ }
230
+ left--;
231
+ let len = table.minLen;
232
+ let code = input.bits(len);
233
+ while (code > table.limit[len]) {
234
+ len++;
235
+ if (len > MAX_CODE_LEN)
236
+ throw new errors_1.DataError('A bzip2 Huffman code is not in its table');
237
+ code = (code << 1) | input.bit();
238
+ }
239
+ const idx = code - table.base[len];
240
+ if (idx < 0 || idx >= MAX_ALPHA)
241
+ throw new errors_1.DataError('A bzip2 Huffman code is not in its table');
242
+ return table.perm[idx];
243
+ };
244
+ let sym = next();
245
+ for (;;) {
246
+ if (sym === eob)
247
+ break;
248
+ if (sym <= 1) {
249
+ let run = -1;
250
+ let n = 1;
251
+ do {
252
+ if (n >= 2 * 1024 * 1024)
253
+ throw new errors_1.DataError('A bzip2 run is too long');
254
+ run += (sym + 1) * n;
255
+ n *= 2;
256
+ sym = next();
257
+ } while (sym <= 1);
258
+ run++;
259
+ const uc = seqToUnseq[mtf[0]];
260
+ if (length + run > max)
261
+ throw new errors_1.DataError('A bzip2 block is longer than its level allows');
262
+ counts[uc] += run;
263
+ tt.fill(uc, length, length + run);
264
+ length += run;
265
+ continue;
266
+ }
267
+ if (length >= max)
268
+ throw new errors_1.DataError('A bzip2 block is longer than its level allows');
269
+ const nn = sym - 1;
270
+ if (nn >= nInUse)
271
+ throw new errors_1.DataError('A bzip2 symbol names a byte value the block does not use');
272
+ const v = mtf[nn];
273
+ mtf.copyWithin(1, 0, nn);
274
+ mtf[0] = v;
275
+ const uc = seqToUnseq[v];
276
+ counts[uc]++;
277
+ tt[length++] = uc;
278
+ sym = next();
279
+ }
280
+ if (origPtr >= length)
281
+ throw new errors_1.DataError('A bzip2 block starts outside itself');
282
+ // The inverse transform links each position to the one that follows it, in the high bits of tt.
283
+ const cftab = new Int32Array(257);
284
+ for (let i = 0; i < 256; i++)
285
+ cftab[i + 1] = cftab[i] + counts[i];
286
+ for (let i = 0; i < length; i++) {
287
+ const uc = tt[i] & 0xff;
288
+ tt[cftab[uc]] |= i << 8;
289
+ cftab[uc]++;
290
+ }
291
+ return { crc, randomised, tt, length, origPtr };
292
+ }
293
+ /** Undoes the transform and the first run-length coding of a block, in chunks, checking its CRC at the end. */
294
+ function* blockBytes(b) {
295
+ const out = new Uint8Array(OUT_CHUNK);
296
+ let o = 0;
297
+ let crc = 0xffffffff;
298
+ let pos = b.tt[b.origPtr] >>> 8;
299
+ let last = -1;
300
+ let run = 0;
301
+ let toGo = 0;
302
+ let tablePos = 0;
303
+ for (let left = b.length; left > 0; left--) {
304
+ pos = b.tt[pos];
305
+ let ch = pos & 0xff;
306
+ pos >>>= 8;
307
+ if (b.randomised) {
308
+ if (toGo === 0) {
309
+ toGo = RAND_NUMS[tablePos];
310
+ tablePos = (tablePos + 1) % 512;
311
+ }
312
+ toGo--;
313
+ if (toGo === 1)
314
+ ch ^= 1;
315
+ }
316
+ if (run === 4) {
317
+ for (let n = 0; n < ch; n++) {
318
+ out[o++] = last;
319
+ crc = ((crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ last) & 0xff]) >>> 0;
320
+ if (o === OUT_CHUNK) {
321
+ yield out.slice();
322
+ o = 0;
323
+ }
324
+ }
325
+ run = 0;
326
+ continue;
327
+ }
328
+ if (ch === last)
329
+ run++;
330
+ else {
331
+ last = ch;
332
+ run = 1;
333
+ }
334
+ out[o++] = ch;
335
+ crc = ((crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ ch) & 0xff]) >>> 0;
336
+ if (o === OUT_CHUNK) {
337
+ yield out.slice();
338
+ o = 0;
339
+ }
340
+ }
341
+ if (o)
342
+ yield out.slice(0, o);
343
+ if (~crc >>> 0 !== b.crc)
344
+ throw new errors_1.DataError('A bzip2 block does not match its CRC');
345
+ }
346
+ const isStreamHeader = (b) => b.length >= 4 && b[0] === 0x42 && b[1] === 0x5a && b[2] === 0x68 && b[3] >= 0x31 && b[3] <= 0x39;
347
+ /**
348
+ * Decodes bzip2 data. With `multi`, streams that follow each other decode as one, the way bzip2 reads them, and
349
+ * `info` records where each sits and what follows the last. Without it the data must be one stream and nothing
350
+ * after it, as in a ZIP entry. Corrupt or cut-short data throws DataError.
351
+ */
352
+ async function* bunzip2(chunks, info, multi, consumed) {
353
+ const input = new BitInput(chunks[Symbol.asyncIterator]());
354
+ let tt;
355
+ for (;;) {
356
+ await input.fill(4);
357
+ const start = input.offset;
358
+ if (!isStreamHeader(input.peek(4))) {
359
+ if (info.streams.length === 0)
360
+ throw new errors_1.DataError('No bzip2 stream header');
361
+ break;
362
+ }
363
+ input.bits(24);
364
+ const level = input.bits(8) - 0x30;
365
+ if (!tt || tt.length < level * 100000)
366
+ tt = new Uint32Array(level * 100000);
367
+ let combined = 0;
368
+ for (;;) {
369
+ await input.fill(MAX_BLOCK_BYTES);
370
+ const hi = input.bits(24);
371
+ const lo = input.bits(24);
372
+ if (hi === BLOCK_MAGIC_HI && lo === BLOCK_MAGIC_LO) {
373
+ const block = readBlock(input, level, tt);
374
+ if (consumed)
375
+ consumed.bytes = Math.floor(input.offset);
376
+ for (const c of blockBytes(block))
377
+ yield c;
378
+ combined = (((combined << 1) | (combined >>> 31)) ^ block.crc) >>> 0;
379
+ continue;
380
+ }
381
+ if (hi !== END_MAGIC_HI || lo !== END_MAGIC_LO)
382
+ throw new errors_1.DataError('A bzip2 block starts with the wrong magic number');
383
+ const stored = ((input.bits(16) << 16) | input.bits(16)) >>> 0;
384
+ if (stored !== combined)
385
+ throw new errors_1.DataError('A bzip2 stream does not match its CRC');
386
+ input.align();
387
+ break;
388
+ }
389
+ info.streams.push({ start, end: input.offset, level });
390
+ if (consumed)
391
+ consumed.bytes = input.offset;
392
+ if (!multi)
393
+ break;
394
+ }
395
+ const rest = await input.drain();
396
+ info.trailing = rest.bytes;
397
+ info.trailingZeros = rest.zeros;
398
+ if (!multi && rest.bytes)
399
+ throw new errors_1.DataError('Data follows the bzip2 stream');
400
+ }
401
+ // ---------------------------------------------------------------------------------------------------------------
402
+ // The encoder.
403
+ /** Writes bits, most significant first. */
404
+ class BitOutput {
405
+ buf = new Uint8Array(1 << 16);
406
+ len = 0;
407
+ acc = 0;
408
+ n = 0;
409
+ put(k, v) {
410
+ // k is at most 24, so the accumulator never holds more than 31 bits.
411
+ this.acc = (this.acc << k) | (v & ((1 << k) - 1));
412
+ this.n += k;
413
+ while (this.n >= 8) {
414
+ this.n -= 8;
415
+ this.byte((this.acc >>> this.n) & 0xff);
416
+ }
417
+ }
418
+ put32(v) {
419
+ this.put(16, v >>> 16);
420
+ this.put(16, v & 0xffff);
421
+ }
422
+ byte(b) {
423
+ if (this.len === this.buf.length) {
424
+ const bigger = new Uint8Array(this.buf.length * 2);
425
+ bigger.set(this.buf);
426
+ this.buf = bigger;
427
+ }
428
+ this.buf[this.len++] = b;
429
+ }
430
+ /** The whole bytes written so far, which leaves the bits of a part byte for later. */
431
+ take() {
432
+ const out = this.buf.slice(0, this.len);
433
+ this.len = 0;
434
+ return out;
435
+ }
436
+ /** Pads the last byte with zero bits. */
437
+ flush() {
438
+ if (this.n > 0)
439
+ this.put(8 - this.n, 0);
440
+ }
441
+ }
442
+ /**
443
+ * Sorts the rotations of `block` by prefix doubling, and returns the last column and where the block itself landed.
444
+ * Rotations that are equal keep any order, which the decoder reads back the same.
445
+ */
446
+ function bwt(block) {
447
+ const n = block.length;
448
+ const sa = new Int32Array(n);
449
+ let rank = new Int32Array(n);
450
+ let next = new Int32Array(n);
451
+ const tmp = new Int32Array(n);
452
+ const cnt = new Int32Array(Math.max(256, n) + 1);
453
+ for (let i = 0; i < n; i++)
454
+ cnt[block[i] + 1]++;
455
+ for (let i = 1; i <= 256; i++)
456
+ cnt[i] += cnt[i - 1];
457
+ for (let i = 0; i < n; i++)
458
+ sa[cnt[block[i]]++] = i;
459
+ for (let i = 0; i < n; i++)
460
+ rank[i] = block[i];
461
+ let classes = 256;
462
+ for (let k = 1; k < n; k *= 2) {
463
+ // sa is in order of the first k bytes, so these start positions are in order of the k bytes after them.
464
+ for (let j = 0; j < n; j++)
465
+ tmp[j] = (sa[j] - k + n) % n;
466
+ cnt.fill(0, 0, classes + 1);
467
+ for (let j = 0; j < n; j++)
468
+ cnt[rank[tmp[j]] + 1]++;
469
+ for (let j = 1; j <= classes; j++)
470
+ cnt[j] += cnt[j - 1];
471
+ for (let j = 0; j < n; j++)
472
+ sa[cnt[rank[tmp[j]]]++] = tmp[j];
473
+ let c = 0;
474
+ next[sa[0]] = 0;
475
+ for (let j = 1; j < n; j++) {
476
+ const cur = sa[j];
477
+ const prev = sa[j - 1];
478
+ if (rank[cur] !== rank[prev] || rank[(cur + k) % n] !== rank[(prev + k) % n])
479
+ c++;
480
+ next[cur] = c;
481
+ }
482
+ [rank, next] = [next, rank];
483
+ classes = c + 1;
484
+ if (classes === n)
485
+ break;
486
+ }
487
+ const last = new Uint8Array(n);
488
+ let origPtr = 0;
489
+ for (let j = 0; j < n; j++) {
490
+ if (sa[j] === 0)
491
+ origPtr = j;
492
+ last[j] = block[(sa[j] + n - 1) % n];
493
+ }
494
+ return { last, origPtr };
495
+ }
496
+ /** Huffman code lengths of at most `maxLen` bits for every symbol, as bzip2 makes them: zero counts count as one. */
497
+ function codeLengths(freq, alphaSize, maxLen) {
498
+ const weight = Array.from({ length: alphaSize }, (_, i) => Math.max(1, freq[i]));
499
+ for (;;) {
500
+ // Nodes 0..alphaSize-1 are leaves. Each merge makes a new node from the two lightest.
501
+ const parent = new Int32Array(alphaSize * 2).fill(-1);
502
+ const w = [...weight];
503
+ const live = Array.from({ length: alphaSize }, (_, i) => i);
504
+ while (live.length > 1) {
505
+ live.sort((x, y) => w[x] - w[y] || x - y);
506
+ const [a, b] = live.splice(0, 2);
507
+ const node = w.length;
508
+ w.push(w[a] + w[b]);
509
+ parent[a] = node;
510
+ parent[b] = node;
511
+ live.push(node);
512
+ }
513
+ const lengths = new Uint8Array(alphaSize);
514
+ let longest = 0;
515
+ for (let i = 0; i < alphaSize; i++) {
516
+ let d = 0;
517
+ for (let p = parent[i]; p >= 0; p = parent[p])
518
+ d++;
519
+ lengths[i] = Math.max(1, d);
520
+ longest = Math.max(longest, lengths[i]);
521
+ }
522
+ if (longest <= maxLen)
523
+ return lengths;
524
+ for (let i = 0; i < alphaSize; i++)
525
+ weight[i] = 1 + Math.floor(weight[i] / 2);
526
+ }
527
+ }
528
+ /** Writes one block: the transform, move-to-front with zero runs, and one Huffman table used twice. */
529
+ function writeBlock(out, block, crc) {
530
+ const { last, origPtr } = bwt(block);
531
+ const inUse = new Uint8Array(256);
532
+ for (const b of last)
533
+ inUse[b] = 1;
534
+ const unseqToSeq = new Uint8Array(256);
535
+ let nInUse = 0;
536
+ for (let i = 0; i < 256; i++)
537
+ if (inUse[i])
538
+ unseqToSeq[i] = nInUse++;
539
+ const alphaSize = nInUse + 2;
540
+ const eob = nInUse + 1;
541
+ const mtfv = new Uint16Array(block.length + 1);
542
+ const freq = new Int32Array(alphaSize);
543
+ let wr = 0;
544
+ const emit = (s) => {
545
+ mtfv[wr++] = s;
546
+ freq[s]++;
547
+ };
548
+ const flushZeros = (z) => {
549
+ let zPend = z - 1;
550
+ for (;;) {
551
+ emit(zPend & 1 ? 1 : 0);
552
+ if (zPend < 2)
553
+ break;
554
+ zPend = (zPend - 2) >> 1;
555
+ }
556
+ };
557
+ const yy = new Uint8Array(nInUse);
558
+ for (let i = 0; i < nInUse; i++)
559
+ yy[i] = i;
560
+ let zeros = 0;
561
+ for (const b of last) {
562
+ const s = unseqToSeq[b];
563
+ if (yy[0] === s) {
564
+ zeros++;
565
+ continue;
566
+ }
567
+ if (zeros)
568
+ flushZeros(zeros);
569
+ zeros = 0;
570
+ let j = 1;
571
+ while (yy[j] !== s)
572
+ j++;
573
+ yy.copyWithin(1, 0, j);
574
+ yy[0] = s;
575
+ emit(j + 1);
576
+ }
577
+ if (zeros)
578
+ flushZeros(zeros);
579
+ emit(eob);
580
+ const lengths = codeLengths(freq, alphaSize, 17);
581
+ const codes = new Int32Array(alphaSize);
582
+ let vec = 0;
583
+ for (let n = 1; n <= 17; n++) {
584
+ for (let i = 0; i < alphaSize; i++)
585
+ if (lengths[i] === n)
586
+ codes[i] = vec++;
587
+ vec <<= 1;
588
+ }
589
+ out.put(24, BLOCK_MAGIC_HI);
590
+ out.put(24, BLOCK_MAGIC_LO);
591
+ out.put32(crc);
592
+ out.put(1, 0);
593
+ out.put(24, origPtr);
594
+ let used16 = 0;
595
+ for (let i = 0; i < 16; i++)
596
+ if (inUse.subarray(i * 16, i * 16 + 16).some(x => x === 1))
597
+ used16 |= 0x8000 >> i;
598
+ out.put(16, used16);
599
+ for (let i = 0; i < 16; i++) {
600
+ if (!(used16 & (0x8000 >> i)))
601
+ continue;
602
+ let bits = 0;
603
+ for (let j = 0; j < 16; j++)
604
+ if (inUse[i * 16 + j])
605
+ bits |= 0x8000 >> j;
606
+ out.put(16, bits);
607
+ }
608
+ const nSelectors = Math.ceil(wr / GROUP_SIZE);
609
+ out.put(3, 2);
610
+ out.put(15, nSelectors);
611
+ // Every selector picks the first table, which move-to-front codes as a single zero bit.
612
+ for (let i = 0; i < nSelectors; i++)
613
+ out.put(1, 0);
614
+ for (let t = 0; t < 2; t++) {
615
+ let curr = lengths[0];
616
+ out.put(5, curr);
617
+ for (let i = 0; i < alphaSize; i++) {
618
+ while (curr < lengths[i]) {
619
+ out.put(2, 2);
620
+ curr++;
621
+ }
622
+ while (curr > lengths[i]) {
623
+ out.put(2, 3);
624
+ curr--;
625
+ }
626
+ out.put(1, 0);
627
+ }
628
+ }
629
+ for (let i = 0; i < wr; i++)
630
+ out.put(lengths[mtfv[i]], codes[mtfv[i]]);
631
+ }
632
+ /** Compresses `chunks` as one bzip2 stream with blocks of `level` hundred thousand bytes. */
633
+ async function* bzip2(chunks, level = 9) {
634
+ const out = new BitOutput();
635
+ out.put(24, 0x425a68);
636
+ out.put(8, 0x30 + level);
637
+ // bzip2 leaves 19 bytes of room, as its own encoder does.
638
+ const max = level * 100000 - 19;
639
+ const block = new Uint8Array(max);
640
+ let len = 0;
641
+ let crc = 0xffffffff;
642
+ let combined = 0;
643
+ let runChar = -1;
644
+ let runLen = 0;
645
+ const endBlock = () => {
646
+ if (len === 0)
647
+ return;
648
+ const blockCrc = ~crc >>> 0;
649
+ writeBlock(out, block.subarray(0, len), blockCrc);
650
+ combined = (((combined << 1) | (combined >>> 31)) ^ blockCrc) >>> 0;
651
+ len = 0;
652
+ crc = 0xffffffff;
653
+ };
654
+ const flushRun = () => {
655
+ if (runLen === 0)
656
+ return;
657
+ if (len + 5 > max)
658
+ endBlock();
659
+ for (let i = 0; i < runLen; i++)
660
+ crc = ((crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ runChar) & 0xff]) >>> 0;
661
+ for (let i = 0; i < Math.min(runLen, 4); i++)
662
+ block[len++] = runChar;
663
+ if (runLen >= 4)
664
+ block[len++] = runLen - 4;
665
+ runLen = 0;
666
+ };
667
+ for await (const c of chunks) {
668
+ for (const b of c) {
669
+ if (b === runChar && runLen < 255) {
670
+ runLen++;
671
+ continue;
672
+ }
673
+ flushRun();
674
+ runChar = b;
675
+ runLen = 1;
676
+ }
677
+ const done = out.take();
678
+ if (done.length)
679
+ yield done;
680
+ }
681
+ flushRun();
682
+ endBlock();
683
+ out.put(24, END_MAGIC_HI);
684
+ out.put(24, END_MAGIC_LO);
685
+ out.put32(combined);
686
+ out.flush();
687
+ yield out.take();
688
+ }