@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/alone.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readAlone = readAlone;
4
+ const errors_1 = require("./errors");
5
+ const header_1 = require("./lzma1/header");
6
+ const lzma1_stream_1 = require("./lzma1-stream");
7
+ const types_1 = require("./types");
8
+ /** Bytes of input decoded per step. LZMA decodes to at most about 7,000 times its input. */
9
+ const SLICE = 1024;
10
+ /**
11
+ * Decodes an .lzma file: a 13-byte header with the properties, the dictionary size and the decoded size or all ones
12
+ * for unknown, then raw LZMA data. An end marker before the declared size is a size mismatch; data that goes on past
13
+ * it, or a stream without its end marker when the size is unknown, does not decode.
14
+ */
15
+ async function* readAlone(env, info) {
16
+ const { reader, size, meter } = env;
17
+ const head = await reader.read(0, header_1.HEADER_SIZE);
18
+ if (head.length < header_1.HEADER_SIZE)
19
+ throw new errors_1.DataError('the file ends in its header', true);
20
+ let header;
21
+ try {
22
+ header = (0, header_1.decodeHeader)(head);
23
+ }
24
+ catch (e) {
25
+ throw new errors_1.DataError(String(e.message).replace(/^(Corrupted|Unsupported) input: /, ''));
26
+ }
27
+ const declared = header.uncompressedSize === header_1.UNKNOWN_SIZE ? undefined : header.uncompressedSize;
28
+ if (declared !== undefined && declared > env.limits.decompressedBytes) {
29
+ throw new errors_1.StopError(types_1.ArchiveCategory.Limit, types_1.ArchiveDetail.DecompressedSize, 'Decompressed size limit exceeded', { limit: env.limits.decompressedBytes, declared });
30
+ }
31
+ const need = (0, lzma1_stream_1.effectiveDictSize)(header.dictSize, declared);
32
+ if (need > env.limits.dictionaryBytes)
33
+ throw new errors_1.StopError(types_1.ArchiveCategory.Limit, types_1.ArchiveDetail.DictionarySize, 'Dictionary size limit exceeded', { limit: env.limits.dictionaryBytes, dictionary: need });
34
+ const out = [];
35
+ const stream = new lzma1_stream_1.Lzma1Stream(header, declared, chunk => out.push(chunk.slice()), true);
36
+ let pos = header_1.HEADER_SIZE;
37
+ while (!stream.done && pos < size) {
38
+ const chunk = await reader.read(pos, 65536);
39
+ // Once the data ends, the rest of the chunk goes to the stream for the end-marker check.
40
+ for (let p = 0; p < chunk.length; p += SLICE) {
41
+ if (!stream.done)
42
+ meter.packed(Math.min(SLICE, chunk.length - p));
43
+ stream.write(chunk.subarray(p, p + SLICE));
44
+ for (const c of out.splice(0)) {
45
+ meter.add(c.length);
46
+ yield c;
47
+ }
48
+ }
49
+ pos += chunk.length;
50
+ }
51
+ // The end-marker check needs a few bytes past the end of the data.
52
+ if (stream.done && pos < size)
53
+ stream.write(await reader.read(pos, 256));
54
+ stream.end();
55
+ for (const c of out.splice(0)) {
56
+ meter.add(c.length);
57
+ yield c;
58
+ }
59
+ if (declared !== undefined && stream.decoded < declared) {
60
+ info.notes.push({ category: types_1.ArchiveCategory.Structure, detail: types_1.ArchiveDetail.SizeMismatch, data: { reason: 'an end marker comes before the declared size', declared, actual: stream.decoded } });
61
+ }
62
+ const end = header_1.HEADER_SIZE + stream.consumed;
63
+ if (end < size)
64
+ info.trailingAt = end;
65
+ }
package/dist/bcj2.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ /** Reads an async stream of chunks byte by byte or a chunk at a time. */
2
+ export declare class PullReader {
3
+ private readonly it;
4
+ private buf;
5
+ private pos;
6
+ private ended;
7
+ constructor(source: AsyncIterable<Uint8Array>);
8
+ /** The unread part of the current chunk, after fetching one if it is used up. Empty at the end of the stream. */
9
+ peek(): Promise<Uint8Array>;
10
+ skip(n: number): void;
11
+ /** The next byte. Throws at the end of the stream. */
12
+ byte(): Promise<number>;
13
+ /** Stops reading the stream. */
14
+ close(): Promise<void>;
15
+ }
16
+ /**
17
+ * Decodes 7-Zip's BCJ2 filter, which moves the targets of x86 calls and jumps into streams of their own. The main
18
+ * stream holds the code, the call and jump streams the big-endian absolute targets, and a range coder stream says
19
+ * which E8, E9 and Jcc opcodes were converted. This follows Bcj2_Decode of the LZMA SDK 9.20 (public domain).
20
+ */
21
+ export declare function decodeBcj2(main: AsyncIterable<Uint8Array>, call: AsyncIterable<Uint8Array>, jump: AsyncIterable<Uint8Array>, rcStream: AsyncIterable<Uint8Array>, outSize: number): AsyncGenerator<Uint8Array>;
package/dist/bcj2.js ADDED
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PullReader = void 0;
4
+ exports.decodeBcj2 = decodeBcj2;
5
+ const errors_1 = require("./errors");
6
+ /** Reads an async stream of chunks byte by byte or a chunk at a time. */
7
+ class PullReader {
8
+ it;
9
+ buf = new Uint8Array(0);
10
+ pos = 0;
11
+ ended = false;
12
+ constructor(source) {
13
+ this.it = source[Symbol.asyncIterator]();
14
+ }
15
+ /** The unread part of the current chunk, after fetching one if it is used up. Empty at the end of the stream. */
16
+ async peek() {
17
+ while (this.pos >= this.buf.length && !this.ended) {
18
+ const r = await this.it.next();
19
+ if (r.done)
20
+ this.ended = true;
21
+ else {
22
+ this.buf = r.value;
23
+ this.pos = 0;
24
+ }
25
+ }
26
+ return this.buf.subarray(this.pos);
27
+ }
28
+ skip(n) {
29
+ this.pos += n;
30
+ }
31
+ /** The next byte. Throws at the end of the stream. */
32
+ async byte() {
33
+ const b = await this.peek();
34
+ if (b.length === 0)
35
+ throw new errors_1.DataError('a BCJ2 stream ends early', true);
36
+ this.pos++;
37
+ return b[0];
38
+ }
39
+ /** Stops reading the stream. */
40
+ async close() {
41
+ await this.it.return?.();
42
+ }
43
+ }
44
+ exports.PullReader = PullReader;
45
+ const TOP = 1 << 24;
46
+ const isJ = (b0, b1) => (b1 & 0xfe) === 0xe8 || (b0 === 0x0f && (b1 & 0xf0) === 0x80);
47
+ /**
48
+ * Decodes 7-Zip's BCJ2 filter, which moves the targets of x86 calls and jumps into streams of their own. The main
49
+ * stream holds the code, the call and jump streams the big-endian absolute targets, and a range coder stream says
50
+ * which E8, E9 and Jcc opcodes were converted. This follows Bcj2_Decode of the LZMA SDK 9.20 (public domain).
51
+ */
52
+ async function* decodeBcj2(main, call, jump, rcStream, outSize) {
53
+ const m = new PullReader(main);
54
+ const c = new PullReader(call);
55
+ const j = new PullReader(jump);
56
+ const r = new PullReader(rcStream);
57
+ try {
58
+ const probs = new Uint16Array(256 + 2).fill(1024);
59
+ let range = 0xffffffff;
60
+ let code = 0;
61
+ for (let i = 0; i < 5; i++)
62
+ code = ((code << 8) | (await r.byte())) >>> 0;
63
+ let prevByte = 0;
64
+ let outPos = 0;
65
+ const out = new Uint8Array(1 << 16);
66
+ let o = 0;
67
+ const flush = () => {
68
+ const chunk = out.slice(0, o);
69
+ o = 0;
70
+ return chunk;
71
+ };
72
+ while (outPos < outSize) {
73
+ // Copy from the main stream up to and including the next opcode that may have been converted.
74
+ const chunk = await m.peek();
75
+ if (chunk.length === 0)
76
+ break;
77
+ let i = 0;
78
+ let found = false;
79
+ const limit = Math.min(chunk.length, outSize - outPos);
80
+ while (i < limit) {
81
+ const b = chunk[i++];
82
+ out[o++] = b;
83
+ outPos++;
84
+ if (o === out.length)
85
+ yield flush();
86
+ if (isJ(prevByte, b)) {
87
+ found = true;
88
+ break;
89
+ }
90
+ prevByte = b;
91
+ }
92
+ m.skip(i);
93
+ if (!found || outPos === outSize)
94
+ continue;
95
+ const b = chunk[i - 1];
96
+ const index = b === 0xe8 ? prevByte : b === 0xe9 ? 256 : 257;
97
+ const prob = probs[index];
98
+ const bound = (range >>> 11) * prob;
99
+ let converted;
100
+ if (code < bound) {
101
+ range = bound;
102
+ probs[index] = prob + ((2048 - prob) >>> 5);
103
+ converted = false;
104
+ }
105
+ else {
106
+ range = (range - bound) >>> 0;
107
+ code = (code - bound) >>> 0;
108
+ probs[index] = prob - (prob >>> 5);
109
+ converted = true;
110
+ }
111
+ if (range < TOP) {
112
+ range = (range << 8) >>> 0;
113
+ code = ((code << 8) | (await r.byte())) >>> 0;
114
+ }
115
+ if (!converted) {
116
+ prevByte = b;
117
+ continue;
118
+ }
119
+ const from = b === 0xe8 ? c : j;
120
+ let src = 0;
121
+ for (let k = 0; k < 4; k++)
122
+ src = ((src << 8) | (await from.byte())) >>> 0;
123
+ const dest = (src - (outPos + 4)) >>> 0;
124
+ for (let k = 0; k < 4 && outPos < outSize; k++) {
125
+ out[o++] = (dest >>> (8 * k)) & 0xff;
126
+ outPos++;
127
+ if (o === out.length)
128
+ yield flush();
129
+ }
130
+ prevByte = dest >>> 24;
131
+ }
132
+ if (o > 0)
133
+ yield flush();
134
+ if (outPos !== outSize)
135
+ throw new errors_1.DataError('the BCJ2 main stream ends early', true);
136
+ }
137
+ finally {
138
+ await Promise.all([m.close(), c.close(), j.close(), r.close()]);
139
+ }
140
+ }
@@ -0,0 +1,21 @@
1
+ import type { Consumed } from './codecs';
2
+ /** Where each stream of a bzip2 file sits, and what follows the last. */
3
+ export interface Bzip2Info {
4
+ streams: Array<{
5
+ start: number;
6
+ end: number;
7
+ level: number;
8
+ }>;
9
+ /** Bytes after the last stream. */
10
+ trailing: number;
11
+ /** True when every trailing byte is zero. */
12
+ trailingZeros: boolean;
13
+ }
14
+ /**
15
+ * Decodes bzip2 data. With `multi`, streams that follow each other decode as one, the way bzip2 reads them, and
16
+ * `info` records where each sits and what follows the last. Without it the data must be one stream and nothing
17
+ * after it, as in a ZIP entry. Corrupt or cut-short data throws DataError.
18
+ */
19
+ export declare function bunzip2(chunks: AsyncIterable<Uint8Array>, info: Bzip2Info, multi: boolean, consumed?: Consumed): AsyncGenerator<Uint8Array>;
20
+ /** Compresses `chunks` as one bzip2 stream with blocks of `level` hundred thousand bytes. */
21
+ export declare function bzip2(chunks: AsyncIterable<Uint8Array>, level?: number): AsyncGenerator<Uint8Array>;