@patrickdk77/archive-defuse 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +345 -2
- package/dist/aes7z.d.ts +16 -0
- package/dist/aes7z.js +131 -0
- package/dist/alone.d.ts +21 -0
- package/dist/alone.js +65 -0
- package/dist/bcj2.d.ts +21 -0
- package/dist/bcj2.js +140 -0
- package/dist/bzip2.d.ts +21 -0
- package/dist/bzip2.js +688 -0
- package/dist/cab.d.ts +63 -0
- package/dist/cab.js +493 -0
- package/dist/checks.d.ts +23 -0
- package/dist/checks.js +123 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +364 -0
- package/dist/codecs.d.ts +39 -0
- package/dist/codecs.js +124 -0
- package/dist/coders.d.ts +58 -0
- package/dist/coders.js +392 -0
- package/dist/crc32.d.ts +8 -0
- package/dist/crc32.js +59 -0
- package/dist/encoder.d.ts +44 -0
- package/dist/encoder.js +114 -0
- package/dist/engine.d.ts +44 -0
- package/dist/engine.js +753 -0
- package/dist/errors.d.ts +64 -0
- package/dist/errors.js +98 -0
- package/dist/filters.d.ts +34 -0
- package/dist/filters.js +385 -0
- package/dist/findings.d.ts +26 -0
- package/dist/findings.js +176 -0
- package/dist/formats.d.ts +14 -0
- package/dist/formats.js +97 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +50 -0
- package/dist/index.mjs +3 -0
- package/dist/inflate64.d.ts +6 -0
- package/dist/inflate64.js +388 -0
- package/dist/io.d.ts +78 -0
- package/dist/io.js +379 -0
- package/dist/lzma-formats.d.ts +75 -0
- package/dist/lzma-formats.js +373 -0
- package/dist/lzma-sniff.d.ts +20 -0
- package/dist/lzma-sniff.js +73 -0
- package/dist/lzma-writers.d.ts +35 -0
- package/dist/lzma-writers.js +276 -0
- package/dist/lzma1/LICENSE +201 -0
- package/dist/lzma1/NOTICE +36 -0
- package/dist/lzma1/crc32.d.ts +4 -0
- package/dist/lzma1/crc32.js +19 -0
- package/dist/lzma1/hash234.d.ts +22 -0
- package/dist/lzma1/hash234.js +75 -0
- package/dist/lzma1/hc4.d.ts +30 -0
- package/dist/lzma1/hc4.js +142 -0
- package/dist/lzma1/header.d.ts +32 -0
- package/dist/lzma1/header.js +73 -0
- package/dist/lzma1/lz-decoder.d.ts +41 -0
- package/dist/lzma1/lz-decoder.js +128 -0
- package/dist/lzma1/lz-encoder.d.ts +87 -0
- package/dist/lzma1/lz-encoder.js +166 -0
- package/dist/lzma1/lzma-coder.d.ts +107 -0
- package/dist/lzma1/lzma-coder.js +162 -0
- package/dist/lzma1/lzma-decoder.d.ts +36 -0
- package/dist/lzma1/lzma-decoder.js +484 -0
- package/dist/lzma1/lzma-encoder-fast.d.ts +13 -0
- package/dist/lzma1/lzma-encoder-fast.js +114 -0
- package/dist/lzma1/lzma-encoder.d.ts +105 -0
- package/dist/lzma1/lzma-encoder.js +497 -0
- package/dist/lzma1/lzma2-decoder.d.ts +85 -0
- package/dist/lzma1/lzma2-decoder.js +249 -0
- package/dist/lzma1/range-coder.d.ts +18 -0
- package/dist/lzma1/range-coder.js +23 -0
- package/dist/lzma1/range-decoder.d.ts +41 -0
- package/dist/lzma1/range-decoder.js +78 -0
- package/dist/lzma1/range-encoder.d.ts +53 -0
- package/dist/lzma1/range-encoder.js +269 -0
- package/dist/lzma1-stream.d.ts +60 -0
- package/dist/lzma1-stream.js +202 -0
- package/dist/model.d.ts +141 -0
- package/dist/model.js +16 -0
- package/dist/names.d.ts +13 -0
- package/dist/names.js +66 -0
- package/dist/pack.d.ts +72 -0
- package/dist/pack.js +514 -0
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +55 -0
- package/dist/rules.d.ts +25 -0
- package/dist/rules.js +403 -0
- package/dist/score.d.ts +17 -0
- package/dist/score.js +58 -0
- package/dist/seven.d.ts +75 -0
- package/dist/seven.js +492 -0
- package/dist/sevenzip.d.ts +133 -0
- package/dist/sevenzip.js +529 -0
- package/dist/single.d.ts +103 -0
- package/dist/single.js +453 -0
- package/dist/sniff.d.ts +20 -0
- package/dist/sniff.js +224 -0
- package/dist/stream-meter.d.ts +39 -0
- package/dist/stream-meter.js +54 -0
- package/dist/tar.d.ts +102 -0
- package/dist/tar.js +765 -0
- package/dist/types.d.ts +284 -0
- package/dist/types.js +133 -0
- package/dist/unpack.d.ts +67 -0
- package/dist/unpack.js +194 -0
- package/dist/xz.d.ts +25 -0
- package/dist/xz.js +333 -0
- package/dist/zip-lzma.d.ts +15 -0
- package/dist/zip-lzma.js +137 -0
- package/dist/zip-writer.d.ts +41 -0
- package/dist/zip-writer.js +263 -0
- package/dist/zip.d.ts +99 -0
- package/dist/zip.js +901 -0
- package/dist/zipcrypt.d.ts +47 -0
- package/dist/zipcrypt.js +233 -0
- package/dist/zstd.d.ts +25 -0
- package/dist/zstd.js +160 -0
- package/package.json +94 -4
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.inflate64 = inflate64;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const WINDOW = 65536;
|
|
6
|
+
const MAXBITS = 15;
|
|
7
|
+
const FAST = 9;
|
|
8
|
+
/** Base and extra bits of literal/length codes 257..285. */
|
|
9
|
+
const LBASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3];
|
|
10
|
+
const LEXT = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16];
|
|
11
|
+
/** Base and extra bits of distance codes 0..31. */
|
|
12
|
+
const DBASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 32769, 49153];
|
|
13
|
+
const DEXT = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14];
|
|
14
|
+
const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
15
|
+
/** A canonical Huffman code: counts per length, symbols by (length, value), and a 9-bit lookup for short codes. */
|
|
16
|
+
class Huffman {
|
|
17
|
+
count = new Uint16Array(MAXBITS + 1);
|
|
18
|
+
symbol = new Uint16Array(288);
|
|
19
|
+
/** (symbol << 4) | length for codes of at most FAST bits, 0 otherwise. */
|
|
20
|
+
fast = new Int32Array(1 << FAST);
|
|
21
|
+
maxLen = 0;
|
|
22
|
+
}
|
|
23
|
+
const offs = new Uint16Array(MAXBITS + 2);
|
|
24
|
+
/** Builds `h` from `n` lengths at `lens[at]`. Returns an error message, or undefined. Work is bounded by 288 + 512. */
|
|
25
|
+
function build(h, lens, at, n, kind) {
|
|
26
|
+
const count = h.count;
|
|
27
|
+
count.fill(0);
|
|
28
|
+
for (let i = 0; i < n; i++)
|
|
29
|
+
count[lens[at + i]]++;
|
|
30
|
+
let max = MAXBITS;
|
|
31
|
+
while (max >= 1 && count[max] === 0)
|
|
32
|
+
max--;
|
|
33
|
+
h.maxLen = max;
|
|
34
|
+
h.fast.fill(0);
|
|
35
|
+
if (max === 0)
|
|
36
|
+
return kind === 'dists' ? undefined : 'no codes';
|
|
37
|
+
let left = 1;
|
|
38
|
+
for (let len = 1; len <= MAXBITS; len++) {
|
|
39
|
+
left = (left << 1) - count[len];
|
|
40
|
+
if (left < 0)
|
|
41
|
+
return 'over-subscribed code';
|
|
42
|
+
}
|
|
43
|
+
if (left > 0 && (kind === 'codes' || max !== 1))
|
|
44
|
+
return 'incomplete code';
|
|
45
|
+
count[0] = 0;
|
|
46
|
+
offs[1] = 0;
|
|
47
|
+
for (let len = 1; len < MAXBITS; len++)
|
|
48
|
+
offs[len + 1] = offs[len] + count[len];
|
|
49
|
+
for (let i = 0; i < n; i++) {
|
|
50
|
+
const len = lens[at + i];
|
|
51
|
+
if (len !== 0)
|
|
52
|
+
h.symbol[offs[len]++] = i;
|
|
53
|
+
}
|
|
54
|
+
// Canonical codes in symbol order: the first code of each length, then reversed into the LSB-first lookup.
|
|
55
|
+
let code = 0;
|
|
56
|
+
let index = 0;
|
|
57
|
+
for (let len = 1; len <= FAST && len <= max; len++) {
|
|
58
|
+
for (let k = 0; k < count[len]; k++) {
|
|
59
|
+
let rev = 0;
|
|
60
|
+
for (let b = 0; b < len; b++)
|
|
61
|
+
rev |= ((code >>> b) & 1) << (len - 1 - b);
|
|
62
|
+
const entry = (h.symbol[index + k] << 4) | len;
|
|
63
|
+
for (let j = rev; j < 1 << FAST; j += 1 << len)
|
|
64
|
+
h.fast[j] = entry;
|
|
65
|
+
code++;
|
|
66
|
+
}
|
|
67
|
+
index += count[len];
|
|
68
|
+
code <<= 1;
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
/** Result of a decode or bit read that needs input the current chunk does not have. */
|
|
73
|
+
const NEED = -2;
|
|
74
|
+
/** Result of decoding bits that are no code. */
|
|
75
|
+
const INVALID = -1;
|
|
76
|
+
const fixedLen = new Huffman();
|
|
77
|
+
const fixedDist = new Huffman();
|
|
78
|
+
{
|
|
79
|
+
const l = new Uint8Array(320);
|
|
80
|
+
l.fill(8, 0, 144);
|
|
81
|
+
l.fill(9, 144, 256);
|
|
82
|
+
l.fill(7, 256, 280);
|
|
83
|
+
l.fill(8, 280, 288);
|
|
84
|
+
l.fill(5, 288, 320);
|
|
85
|
+
build(fixedLen, l, 0, 288, 'lens');
|
|
86
|
+
build(fixedDist, l, 288, 32, 'dists');
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The decoder proper, as a synchronous generator. It yields an output chunk, or null when it needs the next input
|
|
90
|
+
* chunk, which the caller passes back through next(); undefined there means the input has ended.
|
|
91
|
+
*/
|
|
92
|
+
function* decoder(consumed, deadline) {
|
|
93
|
+
let buf = new Uint8Array(0);
|
|
94
|
+
let pos = 0;
|
|
95
|
+
let taken = 0;
|
|
96
|
+
let hold = 0;
|
|
97
|
+
let bits = 0;
|
|
98
|
+
const win = new Uint8Array(WINDOW);
|
|
99
|
+
let wpos = 0;
|
|
100
|
+
let total = 0;
|
|
101
|
+
const lens = new Uint8Array(286 + 32);
|
|
102
|
+
const lenCode = new Huffman();
|
|
103
|
+
const distCode = new Huffman();
|
|
104
|
+
const clCode = new Huffman();
|
|
105
|
+
/** Takes bytes from the current chunk until `n` bits are held or the chunk runs out. n is at most 16. */
|
|
106
|
+
const fill = (n) => {
|
|
107
|
+
while (bits < n && pos < buf.length) {
|
|
108
|
+
hold |= buf[pos++] << bits;
|
|
109
|
+
bits += 8;
|
|
110
|
+
taken++;
|
|
111
|
+
}
|
|
112
|
+
return bits >= n;
|
|
113
|
+
};
|
|
114
|
+
const drop = (n) => {
|
|
115
|
+
hold >>>= n;
|
|
116
|
+
bits -= n;
|
|
117
|
+
};
|
|
118
|
+
const used = () => {
|
|
119
|
+
consumed.bytes = taken - (bits >>> 3);
|
|
120
|
+
};
|
|
121
|
+
/** Decodes one symbol from the bits held, or returns NEED or INVALID. Consumes nothing unless it succeeds. */
|
|
122
|
+
const decode = (h) => {
|
|
123
|
+
fill(MAXBITS);
|
|
124
|
+
if (bits >= FAST) {
|
|
125
|
+
const e = h.fast[hold & ((1 << FAST) - 1)];
|
|
126
|
+
if (e !== 0) {
|
|
127
|
+
drop(e & 15);
|
|
128
|
+
return e >>> 4;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
let code = 0;
|
|
132
|
+
let first = 0;
|
|
133
|
+
let index = 0;
|
|
134
|
+
for (let len = 1; len <= h.maxLen; len++) {
|
|
135
|
+
if (bits < len)
|
|
136
|
+
return NEED;
|
|
137
|
+
code |= (hold >>> (len - 1)) & 1;
|
|
138
|
+
const count = h.count[len];
|
|
139
|
+
if (code - count < first) {
|
|
140
|
+
drop(len);
|
|
141
|
+
return h.symbol[index + code - first];
|
|
142
|
+
}
|
|
143
|
+
index += count;
|
|
144
|
+
first = (first + count) << 1;
|
|
145
|
+
code <<= 1;
|
|
146
|
+
}
|
|
147
|
+
return INVALID;
|
|
148
|
+
};
|
|
149
|
+
// Pulls the next chunk from the caller. Used as `yield* more()`, which happens once per input chunk.
|
|
150
|
+
function* more() {
|
|
151
|
+
used();
|
|
152
|
+
const next = yield null;
|
|
153
|
+
if (next === undefined)
|
|
154
|
+
throw new errors_1.DataError('Deflate64 stream ends before its final block', true);
|
|
155
|
+
buf = next;
|
|
156
|
+
pos = 0;
|
|
157
|
+
}
|
|
158
|
+
let last = false;
|
|
159
|
+
while (!last) {
|
|
160
|
+
(0, errors_1.checkTime)(deadline);
|
|
161
|
+
while (!fill(3))
|
|
162
|
+
yield* more();
|
|
163
|
+
last = (hold & 1) === 1;
|
|
164
|
+
const type = (hold >>> 1) & 3;
|
|
165
|
+
drop(3);
|
|
166
|
+
if (type === 0) {
|
|
167
|
+
drop(bits & 7);
|
|
168
|
+
while (!fill(16))
|
|
169
|
+
yield* more();
|
|
170
|
+
const len = hold & 0xffff;
|
|
171
|
+
drop(16);
|
|
172
|
+
while (!fill(16))
|
|
173
|
+
yield* more();
|
|
174
|
+
const nlen = hold & 0xffff;
|
|
175
|
+
drop(16);
|
|
176
|
+
if (len !== (~nlen & 0xffff))
|
|
177
|
+
throw new errors_1.DataError('Deflate64 stored block lengths do not match');
|
|
178
|
+
let left = len;
|
|
179
|
+
// Whole bytes still held come first; bits is a multiple of 8 here.
|
|
180
|
+
while (left > 0 && bits > 0) {
|
|
181
|
+
win[wpos++] = hold & 0xff;
|
|
182
|
+
drop(8);
|
|
183
|
+
left--;
|
|
184
|
+
total++;
|
|
185
|
+
if (wpos === WINDOW) {
|
|
186
|
+
used();
|
|
187
|
+
yield win.slice(0, WINDOW);
|
|
188
|
+
(0, errors_1.checkTime)(deadline);
|
|
189
|
+
wpos = 0;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
while (left > 0) {
|
|
193
|
+
if (pos === buf.length)
|
|
194
|
+
yield* more();
|
|
195
|
+
const n = Math.min(left, buf.length - pos, WINDOW - wpos);
|
|
196
|
+
win.set(buf.subarray(pos, pos + n), wpos);
|
|
197
|
+
pos += n;
|
|
198
|
+
taken += n;
|
|
199
|
+
wpos += n;
|
|
200
|
+
total += n;
|
|
201
|
+
left -= n;
|
|
202
|
+
if (wpos === WINDOW) {
|
|
203
|
+
used();
|
|
204
|
+
yield win.slice(0, WINDOW);
|
|
205
|
+
(0, errors_1.checkTime)(deadline);
|
|
206
|
+
wpos = 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (type === 3)
|
|
212
|
+
throw new errors_1.DataError('Deflate64 block type 3 is invalid');
|
|
213
|
+
let lc = fixedLen;
|
|
214
|
+
let dc = fixedDist;
|
|
215
|
+
if (type === 2) {
|
|
216
|
+
while (!fill(14))
|
|
217
|
+
yield* more();
|
|
218
|
+
const nlen = (hold & 31) + 257;
|
|
219
|
+
const ndist = ((hold >>> 5) & 31) + 1;
|
|
220
|
+
const ncode = ((hold >>> 10) & 15) + 4;
|
|
221
|
+
drop(14);
|
|
222
|
+
if (nlen > 286)
|
|
223
|
+
throw new errors_1.DataError('Deflate64 block has too many literal/length codes');
|
|
224
|
+
lens.fill(0, 0, 19);
|
|
225
|
+
for (let i = 0; i < ncode; i++) {
|
|
226
|
+
while (!fill(3))
|
|
227
|
+
yield* more();
|
|
228
|
+
lens[CL_ORDER[i]] = hold & 7;
|
|
229
|
+
drop(3);
|
|
230
|
+
}
|
|
231
|
+
const clErr = build(clCode, lens, 0, 19, 'codes');
|
|
232
|
+
if (clErr !== undefined)
|
|
233
|
+
throw new errors_1.DataError(`Deflate64 code-length code is invalid: ${clErr}`);
|
|
234
|
+
const want = nlen + ndist;
|
|
235
|
+
let have = 0;
|
|
236
|
+
// lens is reused: the code-length code is already built from its first 19 entries.
|
|
237
|
+
while (have < want) {
|
|
238
|
+
let sym = decode(clCode);
|
|
239
|
+
while (sym === NEED) {
|
|
240
|
+
yield* more();
|
|
241
|
+
sym = decode(clCode);
|
|
242
|
+
}
|
|
243
|
+
if (sym === INVALID)
|
|
244
|
+
throw new errors_1.DataError('Deflate64 code-length code has no meaning');
|
|
245
|
+
if (sym < 16) {
|
|
246
|
+
lens[have++] = sym;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
let rep;
|
|
250
|
+
let value = 0;
|
|
251
|
+
if (sym === 16) {
|
|
252
|
+
if (have === 0)
|
|
253
|
+
throw new errors_1.DataError('Deflate64 repeat code with no previous length');
|
|
254
|
+
value = lens[have - 1];
|
|
255
|
+
while (!fill(2))
|
|
256
|
+
yield* more();
|
|
257
|
+
rep = 3 + (hold & 3);
|
|
258
|
+
drop(2);
|
|
259
|
+
}
|
|
260
|
+
else if (sym === 17) {
|
|
261
|
+
while (!fill(3))
|
|
262
|
+
yield* more();
|
|
263
|
+
rep = 3 + (hold & 7);
|
|
264
|
+
drop(3);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
while (!fill(7))
|
|
268
|
+
yield* more();
|
|
269
|
+
rep = 11 + (hold & 127);
|
|
270
|
+
drop(7);
|
|
271
|
+
}
|
|
272
|
+
if (have + rep > want)
|
|
273
|
+
throw new errors_1.DataError('Deflate64 code lengths run past HLIT + HDIST');
|
|
274
|
+
lens.fill(value, have, have + rep);
|
|
275
|
+
have += rep;
|
|
276
|
+
}
|
|
277
|
+
if (lens[256] === 0)
|
|
278
|
+
throw new errors_1.DataError('Deflate64 block has no end-of-block code');
|
|
279
|
+
const lErr = build(lenCode, lens, 0, nlen, 'lens');
|
|
280
|
+
if (lErr !== undefined)
|
|
281
|
+
throw new errors_1.DataError(`Deflate64 literal/length code is invalid: ${lErr}`);
|
|
282
|
+
const dErr = build(distCode, lens, nlen, ndist, 'dists');
|
|
283
|
+
if (dErr !== undefined)
|
|
284
|
+
throw new errors_1.DataError(`Deflate64 distance code is invalid: ${dErr}`);
|
|
285
|
+
lc = lenCode;
|
|
286
|
+
dc = distCode;
|
|
287
|
+
}
|
|
288
|
+
for (;;) {
|
|
289
|
+
let sym = decode(lc);
|
|
290
|
+
while (sym === NEED) {
|
|
291
|
+
yield* more();
|
|
292
|
+
sym = decode(lc);
|
|
293
|
+
}
|
|
294
|
+
if (sym === INVALID)
|
|
295
|
+
throw new errors_1.DataError('Deflate64 literal/length code has no meaning');
|
|
296
|
+
if (sym < 256) {
|
|
297
|
+
win[wpos++] = sym;
|
|
298
|
+
total++;
|
|
299
|
+
if (wpos === WINDOW) {
|
|
300
|
+
used();
|
|
301
|
+
yield win.slice(0, WINDOW);
|
|
302
|
+
(0, errors_1.checkTime)(deadline);
|
|
303
|
+
wpos = 0;
|
|
304
|
+
}
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (sym === 256)
|
|
308
|
+
break;
|
|
309
|
+
sym -= 257;
|
|
310
|
+
if (sym >= 29)
|
|
311
|
+
throw new errors_1.DataError('Deflate64 literal/length code 286 or 287 is invalid');
|
|
312
|
+
let length = LBASE[sym];
|
|
313
|
+
const lx = LEXT[sym];
|
|
314
|
+
if (lx > 0) {
|
|
315
|
+
while (!fill(lx))
|
|
316
|
+
yield* more();
|
|
317
|
+
length += hold & ((1 << lx) - 1);
|
|
318
|
+
drop(lx);
|
|
319
|
+
}
|
|
320
|
+
let dsym = decode(dc);
|
|
321
|
+
while (dsym === NEED) {
|
|
322
|
+
yield* more();
|
|
323
|
+
dsym = decode(dc);
|
|
324
|
+
}
|
|
325
|
+
if (dsym === INVALID)
|
|
326
|
+
throw new errors_1.DataError('Deflate64 distance code has no meaning');
|
|
327
|
+
let dist = DBASE[dsym];
|
|
328
|
+
const dx = DEXT[dsym];
|
|
329
|
+
if (dx > 0) {
|
|
330
|
+
while (!fill(dx))
|
|
331
|
+
yield* more();
|
|
332
|
+
dist += hold & ((1 << dx) - 1);
|
|
333
|
+
drop(dx);
|
|
334
|
+
}
|
|
335
|
+
if (dist > total)
|
|
336
|
+
throw new errors_1.DataError('Deflate64 distance reaches before the start of the output');
|
|
337
|
+
total += length;
|
|
338
|
+
// dist <= WINDOW, so the source byte is always still in the window. Byte by byte, so an overlapping match repeats.
|
|
339
|
+
let from = (wpos - dist) & (WINDOW - 1);
|
|
340
|
+
while (length > 0) {
|
|
341
|
+
win[wpos++] = win[from];
|
|
342
|
+
from = (from + 1) & (WINDOW - 1);
|
|
343
|
+
length--;
|
|
344
|
+
if (wpos === WINDOW) {
|
|
345
|
+
used();
|
|
346
|
+
yield win.slice(0, WINDOW);
|
|
347
|
+
(0, errors_1.checkTime)(deadline);
|
|
348
|
+
wpos = 0;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
used();
|
|
354
|
+
if (wpos > 0)
|
|
355
|
+
yield win.slice(0, wpos);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Decodes a raw Deflate64 stream. Stops after the final block; consumed.bytes then counts the input bytes the stream
|
|
359
|
+
* used, so bytes after it are the caller's. Output comes in chunks of at most 64 KiB.
|
|
360
|
+
*/
|
|
361
|
+
async function* inflate64(chunks, consumed, deadline) {
|
|
362
|
+
const count = consumed ?? { bytes: 0 };
|
|
363
|
+
count.bytes = 0;
|
|
364
|
+
const it = chunks[Symbol.asyncIterator]();
|
|
365
|
+
const core = decoder(count, deadline);
|
|
366
|
+
let finished = false;
|
|
367
|
+
try {
|
|
368
|
+
let r = core.next(undefined);
|
|
369
|
+
while (r.done !== true) {
|
|
370
|
+
if (r.value === null) {
|
|
371
|
+
let next = await it.next();
|
|
372
|
+
while (next.done !== true && next.value.length === 0)
|
|
373
|
+
next = await it.next();
|
|
374
|
+
r = core.next(next.done === true ? undefined : next.value);
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
yield r.value;
|
|
378
|
+
r = core.next(undefined);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
finished = true;
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
core.return(undefined);
|
|
385
|
+
if (!finished)
|
|
386
|
+
await it.return?.();
|
|
387
|
+
}
|
|
388
|
+
}
|
package/dist/io.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Writable } from 'node:stream';
|
|
2
|
+
import type { ByteSink, ByteSource } from './types';
|
|
3
|
+
/** A source over bytes already in memory. Reads return views, not copies. */
|
|
4
|
+
export declare function bufferSource(bytes: Uint8Array): ByteSource;
|
|
5
|
+
/** A source over a local file, read at offsets through one file handle. */
|
|
6
|
+
export declare function fileSource(filePath: string): ByteSource;
|
|
7
|
+
/**
|
|
8
|
+
* Random-access reader with a small block cache, so the ZIP reader can ask for small overlapping windows cheaply. Its
|
|
9
|
+
* reads span at most a few blocks: a header, a name and its extra field, or the end of the file.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Reader {
|
|
12
|
+
readonly source: ByteSource;
|
|
13
|
+
readonly size: number;
|
|
14
|
+
private readonly blockSize;
|
|
15
|
+
private readonly maxBlocks;
|
|
16
|
+
/** Blocks by index, oldest first. */
|
|
17
|
+
private readonly cache;
|
|
18
|
+
constructor(source: ByteSource, size: number, blockSize?: number, maxBlocks?: number);
|
|
19
|
+
private block;
|
|
20
|
+
/** Reads up to `length` bytes at `offset`; shorter only at the end of the source. */
|
|
21
|
+
read(offset: number, length: number): Promise<Uint8Array>;
|
|
22
|
+
/** Yields the range in chunks without caching them. */
|
|
23
|
+
chunks(offset: number, length: number, chunkSize?: number): AsyncGenerator<Uint8Array>;
|
|
24
|
+
}
|
|
25
|
+
/** Collects output in memory. */
|
|
26
|
+
export declare function bufferSink(): ByteSink & {
|
|
27
|
+
result(): Uint8Array;
|
|
28
|
+
size(): number;
|
|
29
|
+
};
|
|
30
|
+
/** Writes to a local file. The file is created on the first write. */
|
|
31
|
+
export declare function fileSink(filePath: string): ByteSink;
|
|
32
|
+
/**
|
|
33
|
+
* Wraps any Node Writable, waiting for 'drain' when it applies backpressure. The stream's first error, or its
|
|
34
|
+
* closing before the end, fails the pending write and every later one.
|
|
35
|
+
*/
|
|
36
|
+
export declare function writableSink(stream: Writable): ByteSink;
|
|
37
|
+
/** A private temporary directory, removed by cleanup(). It also keeps the run's first I/O error. */
|
|
38
|
+
export declare class TempDir {
|
|
39
|
+
private readonly base;
|
|
40
|
+
private dir?;
|
|
41
|
+
private seq;
|
|
42
|
+
/** The first error from a temporary file, which the engine throws instead of a verdict on the file. */
|
|
43
|
+
ioError?: unknown;
|
|
44
|
+
constructor(base?: string);
|
|
45
|
+
file(suffix?: string): Promise<string>;
|
|
46
|
+
cleanup(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A sink that keeps what it is given in memory up to `threshold` bytes and in a temporary file past that, with the MD5
|
|
50
|
+
* of the bytes, which tells the output check that a file inside is the one a plugin returned.
|
|
51
|
+
*/
|
|
52
|
+
export declare class SpillSink implements ByteSink {
|
|
53
|
+
private readonly temp;
|
|
54
|
+
private readonly threshold;
|
|
55
|
+
private chunks;
|
|
56
|
+
private size;
|
|
57
|
+
private filePath?;
|
|
58
|
+
private handle?;
|
|
59
|
+
private readonly md5;
|
|
60
|
+
private hex?;
|
|
61
|
+
spilled: boolean;
|
|
62
|
+
constructor(temp: TempDir, threshold: number);
|
|
63
|
+
write(chunk: Uint8Array): Promise<void>;
|
|
64
|
+
close(): Promise<void>;
|
|
65
|
+
/** Closes the sink and deletes its temporary file, once nothing will read what it holds again. */
|
|
66
|
+
dispose(): Promise<void>;
|
|
67
|
+
get length(): number;
|
|
68
|
+
digest(): string;
|
|
69
|
+
/** A source over what was written. Call after close(). */
|
|
70
|
+
source(): ByteSource;
|
|
71
|
+
/** What was written, in chunks. Call after close(). */
|
|
72
|
+
read(chunkSize?: number): AsyncGenerator<Uint8Array>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Copies a whole source, calling `checkTime` before each chunk. A read that returns more or fewer bytes than it asked
|
|
76
|
+
* for fails the copy, which would otherwise shift every byte after it.
|
|
77
|
+
*/
|
|
78
|
+
export declare function copyToSink(source: ByteSource, sink: ByteSink, checkTime?: () => void): Promise<void>;
|