@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.
- package/LICENSE +21 -0
- package/README.md +378 -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/detect.d.ts +10 -0
- package/dist/detect.js +95 -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 +24 -0
- package/dist/formats.js +106 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +53 -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 +79 -0
- package/dist/pack.js +590 -0
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +56 -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 +311 -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 +51 -0
- package/dist/zip-writer.js +305 -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
package/dist/encoder.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Lzma2ChunkWriter = exports.LZMA2_DICT_PROP = exports.LZMA2_DICT_SIZE = exports.RawLzmaEncoder = exports.PROPS_BYTE = exports.PB = exports.LP = exports.LC = void 0;
|
|
4
|
+
exports.fitDictSize = fitDictSize;
|
|
5
|
+
const hc4_1 = require("./lzma1/hc4");
|
|
6
|
+
const lzma_coder_1 = require("./lzma1/lzma-coder");
|
|
7
|
+
const lzma_encoder_fast_1 = require("./lzma1/lzma-encoder-fast");
|
|
8
|
+
const range_encoder_1 = require("./lzma1/range-encoder");
|
|
9
|
+
/** lc, lp and pb of everything this package encodes: lzma1's defaults, and xz's. */
|
|
10
|
+
exports.LC = 3;
|
|
11
|
+
exports.LP = 0;
|
|
12
|
+
exports.PB = 2;
|
|
13
|
+
/** The properties byte of an LZMA header or 7z coder for LC, LP and PB. */
|
|
14
|
+
exports.PROPS_BYTE = (exports.PB * 5 + exports.LP) * 9 + exports.LC;
|
|
15
|
+
/** lzma1's preset 1: the fast encoder with the hash chain match finder and a 1 MiB dictionary. */
|
|
16
|
+
const DICT_SIZE = 1 << 20;
|
|
17
|
+
const NICE_LEN = 128;
|
|
18
|
+
const DEPTH = 8;
|
|
19
|
+
/**
|
|
20
|
+
* The smallest dictionary that covers `size` bytes, at most 1 MiB and at least 4 KiB, rounded up to 2^n or 2^n +
|
|
21
|
+
* 2^(n-1) as decoders of the .lzma format expect.
|
|
22
|
+
*/
|
|
23
|
+
function fitDictSize(size) {
|
|
24
|
+
for (let n = 12; n < 20; ++n) {
|
|
25
|
+
if (2 ** n >= size)
|
|
26
|
+
return 2 ** n;
|
|
27
|
+
if (2 ** n + 2 ** (n - 1) >= size)
|
|
28
|
+
return 2 ** n + 2 ** (n - 1);
|
|
29
|
+
}
|
|
30
|
+
return DICT_SIZE;
|
|
31
|
+
}
|
|
32
|
+
function makeEncoder(rc, dictSize, inputSize) {
|
|
33
|
+
const lz = new hc4_1.HC4({
|
|
34
|
+
dictSize,
|
|
35
|
+
extraSizeBefore: lzma_encoder_fast_1.FAST_EXTRA_SIZE_BEFORE,
|
|
36
|
+
extraSizeAfter: lzma_encoder_fast_1.FAST_EXTRA_SIZE_AFTER,
|
|
37
|
+
niceLen: NICE_LEN,
|
|
38
|
+
matchLenMax: lzma_coder_1.MATCH_LEN_MAX,
|
|
39
|
+
depthLimit: DEPTH,
|
|
40
|
+
inputSize,
|
|
41
|
+
});
|
|
42
|
+
return new lzma_encoder_fast_1.LzmaEncoderFast(rc, lz, { lc: exports.LC, lp: exports.LP, pb: exports.PB, dictSize, niceLen: NICE_LEN });
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Encodes raw LZMA data of a known size, without an end marker, with lzma1's encoder at its preset 1. The output is
|
|
46
|
+
* the range coder's bytes, as a 7z LZMA coder and the data of an .lzma file hold them.
|
|
47
|
+
*/
|
|
48
|
+
class RawLzmaEncoder {
|
|
49
|
+
rc;
|
|
50
|
+
lzma;
|
|
51
|
+
dictSize;
|
|
52
|
+
/** `size` is the input's whole length, or undefined to encode pieces with restart(). */
|
|
53
|
+
constructor(size, dictSize = size === undefined ? DICT_SIZE : fitDictSize(size)) {
|
|
54
|
+
this.dictSize = dictSize;
|
|
55
|
+
this.rc = new range_encoder_1.RangeEncoder(size === undefined ? 1 << 16 : Math.min(size >>> 1, 1 << 20) + 64);
|
|
56
|
+
this.lzma = makeEncoder(this.rc, dictSize, size);
|
|
57
|
+
}
|
|
58
|
+
/** Encodes `input` and returns the bytes ready so far. */
|
|
59
|
+
write(input) {
|
|
60
|
+
const lz = this.lzma.lz;
|
|
61
|
+
for (let off = 0; off < input.length;) {
|
|
62
|
+
off += lz.fillWindow(input, off, input.length - off);
|
|
63
|
+
this.lzma.encode();
|
|
64
|
+
}
|
|
65
|
+
return this.rc.take();
|
|
66
|
+
}
|
|
67
|
+
/** Encodes what is left and returns the last bytes. */
|
|
68
|
+
finish() {
|
|
69
|
+
this.lzma.lz.setFinishing();
|
|
70
|
+
this.lzma.encode();
|
|
71
|
+
return this.rc.finish();
|
|
72
|
+
}
|
|
73
|
+
/** Starts an independent input with the same encoder, whose tables need no new allocation. */
|
|
74
|
+
restart() {
|
|
75
|
+
this.rc = new range_encoder_1.RangeEncoder(1 << 16);
|
|
76
|
+
this.lzma.restart(this.rc);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.RawLzmaEncoder = RawLzmaEncoder;
|
|
80
|
+
/** The largest data of one LZMA2 chunk, compressed or stored. */
|
|
81
|
+
const CHUNK_MAX = 1 << 16;
|
|
82
|
+
/** Dictionary of the LZMA2 chunks below: every chunk resets it, and no chunk holds more than this. */
|
|
83
|
+
exports.LZMA2_DICT_SIZE = CHUNK_MAX;
|
|
84
|
+
/** The LZMA2 properties byte for LZMA2_DICT_SIZE: (2 | (p & 1)) << (p / 2 + 11) is 64 KiB for 8. */
|
|
85
|
+
exports.LZMA2_DICT_PROP = 8;
|
|
86
|
+
/**
|
|
87
|
+
* Writes raw LZMA2 data as independent chunks of at most 64 KiB, each compressed by the LZMA1 encoder above, or
|
|
88
|
+
* stored when that does not make it smaller. Every chunk resets the dictionary and the state, which LZMA2 allows, so
|
|
89
|
+
* the encoder needs no knowledge of LZMA2. `forceCompressed` keeps a chunk compressed even when that is larger, so the
|
|
90
|
+
* stored bytes of the content do not show at the start of a file, where other readers look for their own signatures.
|
|
91
|
+
*/
|
|
92
|
+
class Lzma2ChunkWriter {
|
|
93
|
+
encoder = new RawLzmaEncoder(undefined, exports.LZMA2_DICT_SIZE);
|
|
94
|
+
used = false;
|
|
95
|
+
/** The chunks for one piece of at most 64 KiB. With `store`, a piece not forced compressed is stored as it is. */
|
|
96
|
+
chunk(piece, forceCompressed, store = false) {
|
|
97
|
+
if (piece.length === 0 || piece.length > CHUNK_MAX)
|
|
98
|
+
throw new RangeError('An LZMA2 piece must hold 1 to 65536 bytes');
|
|
99
|
+
const u = piece.length - 1;
|
|
100
|
+
if (store && !forceCompressed)
|
|
101
|
+
return Buffer.concat([Uint8Array.of(0x01, (u >>> 8) & 0xff, u & 0xff), piece]);
|
|
102
|
+
if (this.used)
|
|
103
|
+
this.encoder.restart();
|
|
104
|
+
this.used = true;
|
|
105
|
+
const packed = Buffer.concat([this.encoder.write(piece), this.encoder.finish()]);
|
|
106
|
+
if (packed.length <= CHUNK_MAX && (packed.length < piece.length || forceCompressed)) {
|
|
107
|
+
const c = packed.length - 1;
|
|
108
|
+
const head = Uint8Array.of(0xe0 | (u >>> 16), (u >>> 8) & 0xff, u & 0xff, (c >>> 8) & 0xff, c & 0xff, exports.PROPS_BYTE);
|
|
109
|
+
return Buffer.concat([head, packed]);
|
|
110
|
+
}
|
|
111
|
+
return Buffer.concat([Uint8Array.of(0x01, (u >>> 8) & 0xff, u & 0xff), piece]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.Lzma2ChunkWriter = Lzma2ChunkWriter;
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Entry, Meter } from './model';
|
|
2
|
+
import { type ArchiveDisarmResult, type ArchiveInspection, type ArchiveOptions, type ByteSink, type ByteSource, type PluginContext } from './types';
|
|
3
|
+
export declare const DEFAULT_LIMITS: {
|
|
4
|
+
entries: number;
|
|
5
|
+
decompressedBytes: number;
|
|
6
|
+
totalDecompressedBytes: number;
|
|
7
|
+
compressionRatio: number;
|
|
8
|
+
nestingDepth: number;
|
|
9
|
+
dictionaryBytes: number;
|
|
10
|
+
};
|
|
11
|
+
/** The key under which archive-defuse hands its run's options to a plugin. No other package can name it. */
|
|
12
|
+
export declare const RUN_OPTIONS: unique symbol;
|
|
13
|
+
/** A plugin context as archive-defuse makes it. */
|
|
14
|
+
export type RunContext = PluginContext & {
|
|
15
|
+
[RUN_OPTIONS]?: ArchiveOptions;
|
|
16
|
+
};
|
|
17
|
+
/** The passwords to try on an encrypted entry, in order, with the empty password last. */
|
|
18
|
+
export declare const passwordsOf: (options: ArchiveOptions) => string[];
|
|
19
|
+
/** The limits of a run, with the defaults filled in. */
|
|
20
|
+
export declare const limitsOf: (options: ArchiveOptions) => typeof DEFAULT_LIMITS & {
|
|
21
|
+
fileSize?: number;
|
|
22
|
+
timeMs?: number;
|
|
23
|
+
};
|
|
24
|
+
/** A meter for one entry: its own decoded size, the run's total, the ratio, and the deadline. */
|
|
25
|
+
export declare function makeMeter(e: Entry, limits: typeof DEFAULT_LIMITS, deadline: number | undefined, totals: {
|
|
26
|
+
decoded: number;
|
|
27
|
+
}): Meter & {
|
|
28
|
+
total: number;
|
|
29
|
+
};
|
|
30
|
+
/** Inspects an archive without changing it. The source is closed when the run ends. */
|
|
31
|
+
export declare function inspectArchiveSource(source: ByteSource, options?: ArchiveOptions): Promise<ArchiveInspection>;
|
|
32
|
+
/**
|
|
33
|
+
* Writes a defused copy of `source` to `sink`. Nothing reaches the sink unless the output passes inspection. The
|
|
34
|
+
* source is closed when the run ends.
|
|
35
|
+
*/
|
|
36
|
+
export declare function disarmArchiveSource(source: ByteSource, sink: ByteSink, options?: ArchiveOptions): Promise<ArchiveDisarmResult>;
|
|
37
|
+
/** Internal entry that carries the nesting depth for archives inside other files, and the deadline they share with the upload. */
|
|
38
|
+
export declare function disarmAtDepth(source: ByteSource, sink: ByteSink, options: ArchiveOptions, depth: number, deadline?: number | undefined): Promise<ArchiveDisarmResult>;
|
|
39
|
+
/** Inspects archive bytes. */
|
|
40
|
+
export declare function inspectArchive(bytes: Uint8Array, options?: ArchiveOptions): Promise<ArchiveInspection>;
|
|
41
|
+
/** Defuses archive bytes. `bytes` holds the output unless the archive was rejected. */
|
|
42
|
+
export declare function disarmArchive(bytes: Uint8Array, options?: ArchiveOptions): Promise<ArchiveDisarmResult & {
|
|
43
|
+
bytes?: Uint8Array;
|
|
44
|
+
}>;
|