@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,276 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WRITABLE_CHECKS = exports.passesRatio = void 0;
4
+ exports.writeXz = writeXz;
5
+ exports.writeAlone = writeAlone;
6
+ exports.writeSevenZip = writeSevenZip;
7
+ const checks_1 = require("./checks");
8
+ const crc32_1 = require("./crc32");
9
+ const encoder_1 = require("./encoder");
10
+ const io_1 = require("./io");
11
+ const lzma_sniff_1 = require("./lzma-sniff");
12
+ const sevenzip_1 = require("./sevenzip");
13
+ /** Bytes at the start of a file other readers search for their signatures, such as PDF's header. */
14
+ const SIGNATURE_WINDOW = 1024;
15
+ /** A stream's ratio counts once it passes this many decoded bytes, as the readers count it. */
16
+ const RATIO_FLOOR = 1024 * 1024;
17
+ /** True when data compressed this far would pass the ratio limit, so the writer stores it instead. */
18
+ const passesRatio = (size, packed, ratioLimit) => size > RATIO_FLOOR && size > packed * ratioLimit;
19
+ exports.passesRatio = passesRatio;
20
+ async function* chunksOf(source, size, step, time) {
21
+ for (let pos = 0; pos < size; pos += step) {
22
+ time();
23
+ const b = await source.read(pos, Math.min(step, size - pos));
24
+ if (b.length !== Math.min(step, size - pos))
25
+ throw Object.assign(new Error('A temporary file returned fewer bytes than it holds'), { code: 'EIO' });
26
+ yield b;
27
+ }
28
+ }
29
+ /** The bytes of `content` in pieces of at most `step`, checked to add up to its size. */
30
+ async function* piecesOf(content, step, time) {
31
+ let n = 0;
32
+ for await (const c of content.chunks()) {
33
+ for (let at = 0; at < c.length; at += step) {
34
+ time();
35
+ const piece = c.subarray(at, Math.min(c.length, at + step));
36
+ n += piece.length;
37
+ if (n > content.size)
38
+ throw new Error('An entry holds more bytes than its size');
39
+ yield piece;
40
+ }
41
+ }
42
+ if (n !== content.size)
43
+ throw new Error('An entry holds fewer bytes than its size');
44
+ }
45
+ const le32 = (v) => {
46
+ const b = Buffer.alloc(4);
47
+ b.writeUInt32LE(v >>> 0, 0);
48
+ return b;
49
+ };
50
+ const le64 = (v) => {
51
+ const b = Buffer.alloc(8);
52
+ b.writeUInt32LE(v % 2 ** 32, 0);
53
+ b.writeUInt32LE(Math.floor(v / 2 ** 32), 4);
54
+ return b;
55
+ };
56
+ /** An xz variable-length integer. */
57
+ function vli(v) {
58
+ const out = [];
59
+ let n = v;
60
+ while (n >= 0x80) {
61
+ out.push((n % 0x80) | 0x80);
62
+ n = Math.floor(n / 0x80);
63
+ }
64
+ out.push(n);
65
+ return Uint8Array.from(out);
66
+ }
67
+ /** The check IDs this package writes, the ones it can compute. */
68
+ exports.WRITABLE_CHECKS = new Set([checks_1.CHECK_NONE, checks_1.CHECK_CRC32, checks_1.CHECK_CRC64, checks_1.CHECK_SHA256]);
69
+ /**
70
+ * Writes `content` as a single-stream xz file with one LZMA2 block, or no block when it is empty. The block's chunks
71
+ * come from Lzma2ChunkWriter. Chunks that would start within the first 1024 bytes of the file stay compressed.
72
+ */
73
+ async function writeXz(content, checkId, sink, time, store = false) {
74
+ const flags = Uint8Array.of(0, checkId);
75
+ let out = 0;
76
+ const put = async (b) => {
77
+ out += b.length;
78
+ await sink.write(b);
79
+ };
80
+ await put(Buffer.concat([Uint8Array.from(lzma_sniff_1.XZ_MAGIC), flags, le32((0, crc32_1.crc32)(flags))]));
81
+ const size = content ? await content.size() : 0;
82
+ const records = [];
83
+ if (content && size > 0) {
84
+ // One filter, no sizes: size byte, flags, LZMA2's ID, props size, props, padding, CRC32.
85
+ const blockHeader = Buffer.alloc(12);
86
+ blockHeader.set([2, 0x00, 0x21, 0x01, encoder_1.LZMA2_DICT_PROP]);
87
+ blockHeader.writeUInt32LE((0, crc32_1.crc32)(blockHeader.subarray(0, 8)), 8);
88
+ await put(blockHeader);
89
+ const writer = new encoder_1.Lzma2ChunkWriter();
90
+ const check = (0, checks_1.makeCheck)(checkId);
91
+ let compressed = 0;
92
+ let pos = 0;
93
+ while (pos < size) {
94
+ time();
95
+ const force = out < SIGNATURE_WINDOW;
96
+ const piece = await content.read(pos, Math.min(force ? 1 << 15 : 1 << 16, size - pos));
97
+ if (piece.length === 0)
98
+ throw Object.assign(new Error('A temporary file returned fewer bytes than it holds'), { code: 'EIO' });
99
+ check?.update(piece);
100
+ const chunk = writer.chunk(piece, force, store);
101
+ compressed += chunk.length;
102
+ await put(chunk);
103
+ pos += piece.length;
104
+ }
105
+ await put(Uint8Array.of(0x00));
106
+ compressed += 1;
107
+ const padding = (4 - ((12 + compressed) % 4)) % 4;
108
+ await put(Buffer.alloc(padding));
109
+ await put(check ? check.digest() : new Uint8Array(0));
110
+ records.push({ unpadded: 12 + compressed + (0, checks_1.checkSize)(checkId), uncompressed: size });
111
+ }
112
+ const indexBody = Buffer.concat([Uint8Array.of(0x00), vli(records.length), ...records.flatMap(r => [vli(r.unpadded), vli(r.uncompressed)])]);
113
+ const index = Buffer.concat([indexBody, Buffer.alloc((4 - (indexBody.length % 4)) % 4)]);
114
+ await put(Buffer.concat([index, le32((0, crc32_1.crc32)(index))]));
115
+ const footerBody = Buffer.concat([le32((index.length + 4) / 4 - 1), flags]);
116
+ await put(Buffer.concat([le32((0, crc32_1.crc32)(footerBody)), footerBody, Uint8Array.of(0x59, 0x5a)]));
117
+ }
118
+ /** Writes `content` as an .lzma file with its size in the header and no end marker. */
119
+ async function writeAlone(content, sink, time) {
120
+ const size = content ? await content.size() : 0;
121
+ const encoder = new encoder_1.RawLzmaEncoder(size);
122
+ const header = Buffer.alloc(13);
123
+ header[0] = encoder_1.PROPS_BYTE;
124
+ header.writeUInt32LE(encoder.dictSize, 1);
125
+ le64(size).copy(header, 5);
126
+ await sink.write(header);
127
+ if (content)
128
+ for await (const b of chunksOf(content, size, 1 << 16, time))
129
+ await sink.write(encoder.write(b));
130
+ await sink.write(encoder.finish());
131
+ }
132
+ /** A 7z number. */
133
+ function num(v) {
134
+ if (v < 0x80)
135
+ return Uint8Array.of(v);
136
+ const bytes = [];
137
+ let n = v;
138
+ for (let i = 0; i < 8; i++) {
139
+ bytes.push(n % 256);
140
+ n = Math.floor(n / 256);
141
+ }
142
+ // Fewest extra bytes such that what is left fits in the first byte's free bits.
143
+ for (let extra = 1; extra <= 8; extra++) {
144
+ const high = Math.floor(v / 2 ** (8 * extra));
145
+ if (extra === 8 || high < 2 ** (7 - extra)) {
146
+ const mask = (0xff << (8 - extra)) & 0xff;
147
+ return Uint8Array.from([mask | (extra === 8 ? 0 : high), ...bytes.slice(0, extra)]);
148
+ }
149
+ }
150
+ throw new RangeError('A 7z number too large to write');
151
+ }
152
+ function bitVector(bits) {
153
+ const out = new Uint8Array(Math.ceil(bits.length / 8));
154
+ bits.forEach((b, i) => {
155
+ if (b)
156
+ out[i >> 3] |= 0x80 >> (i & 7);
157
+ });
158
+ return out;
159
+ }
160
+ /** A property of the files info: its ID, size and body. */
161
+ const property = (id, body) => Buffer.concat([Uint8Array.of(id), num(body.length), body]);
162
+ /** Times or attributes that may be missing for some entries: the all-defined byte or the vector, then external 0. */
163
+ function definedValues(values) {
164
+ if (!values.some(v => v !== undefined))
165
+ return undefined;
166
+ const all = values.every(v => v !== undefined);
167
+ const head = all ? Uint8Array.of(1) : Buffer.concat([Uint8Array.of(0), bitVector(values.map(v => v !== undefined))]);
168
+ return Buffer.concat([head, Uint8Array.of(0), ...values.filter((v) => v !== undefined)]);
169
+ }
170
+ /**
171
+ * Writes a 7z archive: one LZMA folder per file with content, compressed by lzma1's encoder at its preset 1, a plain
172
+ * header with the names, empty files and directories, attributes and the times given, and CRCs of every file.
173
+ */
174
+ async function writeSevenZip(entries, sink, temp, memoryThreshold, time, ratioLimit = Number.POSITIVE_INFINITY) {
175
+ if (entries.length === 0) {
176
+ const start = Buffer.alloc(20);
177
+ await sink.write(Buffer.concat([Uint8Array.from(lzma_sniff_1.SEVENZIP_MAGIC), Uint8Array.of(0, 4), le32((0, crc32_1.crc32)(start)), start]));
178
+ return;
179
+ }
180
+ const packed = new io_1.SpillSink(temp, memoryThreshold);
181
+ try {
182
+ const folders = [];
183
+ try {
184
+ for (const e of entries) {
185
+ const size = e.content ? e.content.size : 0;
186
+ if (e.dir || !e.content || size === 0)
187
+ continue;
188
+ const encoder = new encoder_1.RawLzmaEncoder(size);
189
+ let crc = 0;
190
+ const one = new io_1.SpillSink(temp, memoryThreshold);
191
+ try {
192
+ for await (const b of piecesOf(e.content, 1 << 16, time)) {
193
+ crc = (0, crc32_1.crc32)(b, crc);
194
+ await one.write(encoder.write(b));
195
+ }
196
+ await one.write(encoder.finish());
197
+ await one.close();
198
+ // A file LZMA would compress past the ratio limit is stored, so the output reads under the same limit.
199
+ const stored = (0, exports.passesRatio)(size, one.length, ratioLimit);
200
+ if (stored)
201
+ for await (const b of piecesOf(e.content, 1 << 16, time))
202
+ await packed.write(b);
203
+ else
204
+ for await (const b of one.read())
205
+ await packed.write(b);
206
+ folders.push({ packSize: stored ? size : one.length, unpackSize: size, crc, dict: encoder.dictSize, stored });
207
+ }
208
+ finally {
209
+ await one.dispose();
210
+ }
211
+ }
212
+ }
213
+ finally {
214
+ await packed.close();
215
+ }
216
+ const parts = [Uint8Array.of(sevenzip_1.Nid.Header)];
217
+ if (folders.length) {
218
+ parts.push(Uint8Array.of(sevenzip_1.Nid.MainStreamsInfo, sevenzip_1.Nid.PackInfo), num(0), num(folders.length), Uint8Array.of(sevenzip_1.Nid.Size));
219
+ for (const f of folders)
220
+ parts.push(num(f.packSize));
221
+ parts.push(Uint8Array.of(sevenzip_1.Nid.End, sevenzip_1.Nid.UnpackInfo, sevenzip_1.Nid.Folder), num(folders.length), Uint8Array.of(0));
222
+ for (const f of folders) {
223
+ if (f.stored) {
224
+ // One coder: ID size 1 and no properties, Copy.
225
+ parts.push(num(1), Uint8Array.of(0x01, 0x00));
226
+ continue;
227
+ }
228
+ // One coder: ID size 3 with properties, LZMA, five bytes of properties.
229
+ const props = Buffer.concat([Uint8Array.of((encoder_1.PB * 5 + encoder_1.LP) * 9 + encoder_1.LC), le32(f.dict)]);
230
+ parts.push(num(1), Uint8Array.of(0x23, 0x03, 0x01, 0x01), num(props.length), props);
231
+ }
232
+ parts.push(Uint8Array.of(sevenzip_1.Nid.CodersUnpackSize));
233
+ for (const f of folders)
234
+ parts.push(num(f.unpackSize));
235
+ parts.push(Uint8Array.of(sevenzip_1.Nid.End, sevenzip_1.Nid.SubStreamsInfo, sevenzip_1.Nid.Crc, 1));
236
+ for (const f of folders)
237
+ parts.push(le32(f.crc));
238
+ parts.push(Uint8Array.of(sevenzip_1.Nid.End, sevenzip_1.Nid.End));
239
+ }
240
+ const sizes = entries.map(e => (e.dir || !e.content ? 0 : e.content.size));
241
+ const empty = entries.map((e, i) => e.dir || sizes[i] === 0);
242
+ const props = [];
243
+ if (empty.some(Boolean)) {
244
+ props.push(property(sevenzip_1.Nid.EmptyStream, bitVector(empty)));
245
+ const emptyFiles = entries.filter((_, i) => empty[i]).map(e => !e.dir);
246
+ if (emptyFiles.some(Boolean))
247
+ props.push(property(sevenzip_1.Nid.EmptyFile, bitVector(emptyFiles)));
248
+ }
249
+ if (entries.some(e => e.name !== undefined)) {
250
+ const names = Buffer.concat([Uint8Array.of(0), ...entries.map(e => Buffer.from(`${e.name ?? ''}\0`, 'utf16le'))]);
251
+ props.push(property(sevenzip_1.Nid.Name, names));
252
+ }
253
+ for (const [id, key] of [
254
+ [sevenzip_1.Nid.CTime, 'ctime'],
255
+ [sevenzip_1.Nid.ATime, 'atime'],
256
+ [sevenzip_1.Nid.MTime, 'mtime'],
257
+ ]) {
258
+ const body = definedValues(entries.map(e => e[key]));
259
+ if (body)
260
+ props.push(property(id, body));
261
+ }
262
+ const attribs = definedValues(entries.map(e => (e.attrib === undefined ? undefined : le32(e.attrib))));
263
+ if (attribs)
264
+ props.push(property(sevenzip_1.Nid.WinAttributes, attribs));
265
+ parts.push(Uint8Array.of(sevenzip_1.Nid.FilesInfo), num(entries.length), ...props, Uint8Array.of(sevenzip_1.Nid.End, sevenzip_1.Nid.End));
266
+ const header = Buffer.concat(parts);
267
+ const start = Buffer.concat([le64(packed.length), le64(header.length), le32((0, crc32_1.crc32)(header))]);
268
+ await sink.write(Buffer.concat([Uint8Array.from(lzma_sniff_1.SEVENZIP_MAGIC), Uint8Array.of(0, 4), le32((0, crc32_1.crc32)(start)), start]));
269
+ for await (const b of packed.read())
270
+ await sink.write(b);
271
+ await sink.write(header);
272
+ }
273
+ finally {
274
+ await packed.dispose();
275
+ }
276
+ }
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2023 Filip Seman
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,36 @@
1
+ The files in this directory come from lzma1 by Filip Seman, https://github.com/xseman/lzma1, under the Apache
2
+ License 2.0 in LICENSE here. They come from the fork on branch feat/lzma2-decoder at commit
3
+ 19f319c68ed898619393a579d126ce06e3d5d704, which is lzma1 0.4.0 plus raw LZMA1 and LZMA2 decoding. lzma1 ports them
4
+ from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
5
+
6
+ Copied: crc32.ts, hash234.ts, hc4.ts, header.ts, lz-decoder.ts, lz-encoder.ts, lzma-coder.ts, lzma-decoder.ts,
7
+ lzma-encoder.ts, lzma-encoder-fast.ts, lzma2-decoder.ts, range-coder.ts, range-decoder.ts and range-encoder.ts.
8
+
9
+ lzma-defuse changed them as follows, and the files then moved into archive-defuse unchanged.
10
+
11
+ In every file:
12
+ - Biome reformatted it to this package's style, with spaces for tabs and single quotes for double.
13
+ - Imports drop the ".js" suffix, since this package compiles to CommonJS.
14
+ - A first line names lzma1, its license and this NOTICE.
15
+
16
+ In lzma-decoder.ts and lzma-encoder.ts, the box-drawing characters around section comments are now "--", since this
17
+ package keeps its files ASCII.
18
+
19
+ In hc4.ts, lz-decoder.ts, lz-encoder.ts, lzma-encoder.ts, lzma2-decoder.ts and range-encoder.ts, each function that
20
+ assigned to its own parameter now copies the parameter into a local variable first. This package's lint rules forbid
21
+ the assignment. The arithmetic is the same.
22
+
23
+ In lzma-encoder-fast.ts:
24
+ - getNextSymbol() throws if no matches were read, where it used a non-null assertion.
25
+ - A chained assignment is now two assignments.
26
+
27
+ In lzma-encoder.ts, the empty body of the loop in encode() holds a comment.
28
+
29
+ In lzma2-decoder.ts:
30
+ - The file defines INPUT_MARGIN itself instead of importing it from alone-decoder.ts, which this package does not
31
+ copy. The value is still 64.
32
+ - A private method, currentLzma(), returns the LZMA decoder or throws "LZMA2 chunk without properties". It replaces
33
+ two non-null assertions.
34
+
35
+ crc32.ts, hash234.ts, header.ts, lzma-coder.ts, lzma-decoder.ts, range-coder.ts and range-decoder.ts have no changes
36
+ beyond formatting and imports.
@@ -0,0 +1,4 @@
1
+ /**
2
+ * CRC-32 (IEEE 802.3) lookup table. The match finders use it for hashing.
3
+ */
4
+ export declare const CRC32_TABLE: number[];
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CRC32_TABLE = void 0;
4
+ // Copied from lzma1 (Apache-2.0, Copyright Filip Seman) and changed for lzma-defuse. NOTICE in this folder lists the changes.
5
+ /**
6
+ * CRC-32 (IEEE 802.3) lookup table. The match finders use it for hashing.
7
+ */
8
+ exports.CRC32_TABLE = createCrc32Table();
9
+ function createCrc32Table() {
10
+ const table = [];
11
+ for (let i = 0; i < 256; ++i) {
12
+ let r = i;
13
+ for (let j = 0; j < 8; ++j) {
14
+ r = (r & 1) !== 0 ? (r >>> 1) ^ 0xedb88320 : r >>> 1;
15
+ }
16
+ table.push(r >>> 0);
17
+ }
18
+ return table;
19
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Hash tables of the last positions of 2-, 3- and 4-byte sequences.
3
+ *
4
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
5
+ */
6
+ export declare class Hash234 {
7
+ private readonly hash4Mask;
8
+ private readonly hash2Table;
9
+ private readonly hash3Table;
10
+ private readonly hash4Table;
11
+ private hash2Value;
12
+ private hash3Value;
13
+ private hash4Value;
14
+ constructor(dictSize: number);
15
+ calcHashes(buf: Uint8Array, off: number): void;
16
+ getHash2Pos(): number;
17
+ getHash3Pos(): number;
18
+ getHash4Pos(): number;
19
+ updateTables(pos: number): void;
20
+ clear(): void;
21
+ normalize(offset: number): void;
22
+ }
@@ -0,0 +1,75 @@
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
+ * Hash tables of the last positions of 2-, 3- and 4-byte sequences.
5
+ *
6
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Hash234 = void 0;
10
+ const crc32_1 = require("./crc32");
11
+ const lz_encoder_1 = require("./lz-encoder");
12
+ const HASH_2_SIZE = 1 << 10;
13
+ const HASH_2_MASK = HASH_2_SIZE - 1;
14
+ const HASH_3_SIZE = 1 << 16;
15
+ const HASH_3_MASK = HASH_3_SIZE - 1;
16
+ function getHash4Size(dictSize) {
17
+ let h = dictSize - 1;
18
+ h |= h >>> 1;
19
+ h |= h >>> 2;
20
+ h |= h >>> 4;
21
+ h |= h >>> 8;
22
+ h >>>= 1;
23
+ h |= 0xffff;
24
+ if (h > 1 << 24) {
25
+ h >>>= 1;
26
+ }
27
+ return h + 1;
28
+ }
29
+ class Hash234 {
30
+ hash4Mask;
31
+ hash2Table = new Int32Array(HASH_2_SIZE);
32
+ hash3Table = new Int32Array(HASH_3_SIZE);
33
+ hash4Table;
34
+ hash2Value = 0;
35
+ hash3Value = 0;
36
+ hash4Value = 0;
37
+ constructor(dictSize) {
38
+ const hash4Size = getHash4Size(dictSize);
39
+ this.hash4Table = new Int32Array(hash4Size);
40
+ this.hash4Mask = hash4Size - 1;
41
+ }
42
+ calcHashes(buf, off) {
43
+ let temp = crc32_1.CRC32_TABLE[buf[off]] ^ buf[off + 1];
44
+ this.hash2Value = temp & HASH_2_MASK;
45
+ temp ^= buf[off + 2] << 8;
46
+ this.hash3Value = temp & HASH_3_MASK;
47
+ temp ^= crc32_1.CRC32_TABLE[buf[off + 3]] << 5;
48
+ this.hash4Value = temp & this.hash4Mask;
49
+ }
50
+ getHash2Pos() {
51
+ return this.hash2Table[this.hash2Value];
52
+ }
53
+ getHash3Pos() {
54
+ return this.hash3Table[this.hash3Value];
55
+ }
56
+ getHash4Pos() {
57
+ return this.hash4Table[this.hash4Value];
58
+ }
59
+ updateTables(pos) {
60
+ this.hash2Table[this.hash2Value] = pos;
61
+ this.hash3Table[this.hash3Value] = pos;
62
+ this.hash4Table[this.hash4Value] = pos;
63
+ }
64
+ clear() {
65
+ this.hash2Table.fill(0);
66
+ this.hash3Table.fill(0);
67
+ this.hash4Table.fill(0);
68
+ }
69
+ normalize(offset) {
70
+ (0, lz_encoder_1.normalizePositions)(this.hash2Table, offset);
71
+ (0, lz_encoder_1.normalizePositions)(this.hash3Table, offset);
72
+ (0, lz_encoder_1.normalizePositions)(this.hash4Table, offset);
73
+ }
74
+ }
75
+ exports.Hash234 = Hash234;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Hash chain match finder with 2-, 3- and 4-byte hashing.
3
+ *
4
+ * Every position links to the previous position with the same 4-byte hash.
5
+ * Faster than the binary tree finder but finds fewer long matches.
6
+ *
7
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
8
+ */
9
+ import { LzEncoder, type LzEncoderConfig, Matches } from './lz-encoder';
10
+ export declare class HC4 extends LzEncoder {
11
+ private readonly hash;
12
+ /** Previous position with the same hash, per position in the cyclic buffer. */
13
+ private readonly chain;
14
+ private readonly matches;
15
+ private readonly depthLimit;
16
+ private readonly cyclicSize;
17
+ private cyclicPos;
18
+ /** Position counter stored in the hash tables and chain. */
19
+ private lzPos;
20
+ constructor(config: LzEncoderConfig);
21
+ /**
22
+ * Prepares for a new input without clearing the tables: positions
23
+ * continue after the previous input, so its entries are too far back
24
+ * (`delta >= cyclicSize`) and get ignored like empty ones.
25
+ */
26
+ reset(): void;
27
+ getMatches(): Matches;
28
+ skip(len: number): void;
29
+ private movePosition;
30
+ }