@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.
Files changed (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +345 -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/encoder.d.ts +44 -0
  24. package/dist/encoder.js +114 -0
  25. package/dist/engine.d.ts +44 -0
  26. package/dist/engine.js +753 -0
  27. package/dist/errors.d.ts +64 -0
  28. package/dist/errors.js +98 -0
  29. package/dist/filters.d.ts +34 -0
  30. package/dist/filters.js +385 -0
  31. package/dist/findings.d.ts +26 -0
  32. package/dist/findings.js +176 -0
  33. package/dist/formats.d.ts +14 -0
  34. package/dist/formats.js +97 -0
  35. package/dist/index.d.ts +8 -0
  36. package/dist/index.js +50 -0
  37. package/dist/index.mjs +3 -0
  38. package/dist/inflate64.d.ts +6 -0
  39. package/dist/inflate64.js +388 -0
  40. package/dist/io.d.ts +78 -0
  41. package/dist/io.js +379 -0
  42. package/dist/lzma-formats.d.ts +75 -0
  43. package/dist/lzma-formats.js +373 -0
  44. package/dist/lzma-sniff.d.ts +20 -0
  45. package/dist/lzma-sniff.js +73 -0
  46. package/dist/lzma-writers.d.ts +35 -0
  47. package/dist/lzma-writers.js +276 -0
  48. package/dist/lzma1/LICENSE +201 -0
  49. package/dist/lzma1/NOTICE +36 -0
  50. package/dist/lzma1/crc32.d.ts +4 -0
  51. package/dist/lzma1/crc32.js +19 -0
  52. package/dist/lzma1/hash234.d.ts +22 -0
  53. package/dist/lzma1/hash234.js +75 -0
  54. package/dist/lzma1/hc4.d.ts +30 -0
  55. package/dist/lzma1/hc4.js +142 -0
  56. package/dist/lzma1/header.d.ts +32 -0
  57. package/dist/lzma1/header.js +73 -0
  58. package/dist/lzma1/lz-decoder.d.ts +41 -0
  59. package/dist/lzma1/lz-decoder.js +128 -0
  60. package/dist/lzma1/lz-encoder.d.ts +87 -0
  61. package/dist/lzma1/lz-encoder.js +166 -0
  62. package/dist/lzma1/lzma-coder.d.ts +107 -0
  63. package/dist/lzma1/lzma-coder.js +162 -0
  64. package/dist/lzma1/lzma-decoder.d.ts +36 -0
  65. package/dist/lzma1/lzma-decoder.js +484 -0
  66. package/dist/lzma1/lzma-encoder-fast.d.ts +13 -0
  67. package/dist/lzma1/lzma-encoder-fast.js +114 -0
  68. package/dist/lzma1/lzma-encoder.d.ts +105 -0
  69. package/dist/lzma1/lzma-encoder.js +497 -0
  70. package/dist/lzma1/lzma2-decoder.d.ts +85 -0
  71. package/dist/lzma1/lzma2-decoder.js +249 -0
  72. package/dist/lzma1/range-coder.d.ts +18 -0
  73. package/dist/lzma1/range-coder.js +23 -0
  74. package/dist/lzma1/range-decoder.d.ts +41 -0
  75. package/dist/lzma1/range-decoder.js +78 -0
  76. package/dist/lzma1/range-encoder.d.ts +53 -0
  77. package/dist/lzma1/range-encoder.js +269 -0
  78. package/dist/lzma1-stream.d.ts +60 -0
  79. package/dist/lzma1-stream.js +202 -0
  80. package/dist/model.d.ts +141 -0
  81. package/dist/model.js +16 -0
  82. package/dist/names.d.ts +13 -0
  83. package/dist/names.js +66 -0
  84. package/dist/pack.d.ts +72 -0
  85. package/dist/pack.js +514 -0
  86. package/dist/plugins.d.ts +13 -0
  87. package/dist/plugins.js +55 -0
  88. package/dist/rules.d.ts +25 -0
  89. package/dist/rules.js +403 -0
  90. package/dist/score.d.ts +17 -0
  91. package/dist/score.js +58 -0
  92. package/dist/seven.d.ts +75 -0
  93. package/dist/seven.js +492 -0
  94. package/dist/sevenzip.d.ts +133 -0
  95. package/dist/sevenzip.js +529 -0
  96. package/dist/single.d.ts +103 -0
  97. package/dist/single.js +453 -0
  98. package/dist/sniff.d.ts +20 -0
  99. package/dist/sniff.js +224 -0
  100. package/dist/stream-meter.d.ts +39 -0
  101. package/dist/stream-meter.js +54 -0
  102. package/dist/tar.d.ts +102 -0
  103. package/dist/tar.js +765 -0
  104. package/dist/types.d.ts +284 -0
  105. package/dist/types.js +133 -0
  106. package/dist/unpack.d.ts +67 -0
  107. package/dist/unpack.js +194 -0
  108. package/dist/xz.d.ts +25 -0
  109. package/dist/xz.js +333 -0
  110. package/dist/zip-lzma.d.ts +15 -0
  111. package/dist/zip-lzma.js +137 -0
  112. package/dist/zip-writer.d.ts +41 -0
  113. package/dist/zip-writer.js +263 -0
  114. package/dist/zip.d.ts +99 -0
  115. package/dist/zip.js +901 -0
  116. package/dist/zipcrypt.d.ts +47 -0
  117. package/dist/zipcrypt.js +233 -0
  118. package/dist/zstd.d.ts +25 -0
  119. package/dist/zstd.js +160 -0
  120. package/package.json +94 -4
@@ -0,0 +1,105 @@
1
+ /**
2
+ * LZMA encoder base: codes the symbols chosen by a subclass and keeps the
3
+ * price tables the subclasses use to choose between symbols.
4
+ *
5
+ * Subclasses implement `getNextSymbol()`:
6
+ * - `LzmaEncoderFast` uses simple heuristics (levels 1-3).
7
+ * - `LzmaEncoderNormal` searches for the cheapest symbol sequence (levels 4-9).
8
+ *
9
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
10
+ */
11
+ import type { LzEncoder, Matches } from './lz-encoder';
12
+ import { LzmaCoder } from './lzma-coder';
13
+ import { type Probs } from './range-coder';
14
+ import { type RangeEncoder } from './range-encoder';
15
+ /** Returns the distance slot: roughly 2 * log2(dist), plus the next bit. */
16
+ export declare function getDistSlot(dist: number): number;
17
+ export interface LzmaEncoderConfig {
18
+ lc: number;
19
+ lp: number;
20
+ pb: number;
21
+ dictSize: number;
22
+ niceLen: number;
23
+ }
24
+ export declare abstract class LzmaEncoder extends LzmaCoder {
25
+ private rc;
26
+ readonly lz: LzEncoder;
27
+ readonly niceLen: number;
28
+ readonly matchLenEncoder: LengthEncoder;
29
+ readonly repLenEncoder: LengthEncoder;
30
+ private distPriceCount;
31
+ private alignPriceCount;
32
+ /** Whether a distance or align bits were encoded since the last reset. */
33
+ private distModelsUsed;
34
+ private alignModelUsed;
35
+ private readonly distSlotPricesSize;
36
+ /** Price of each distance slot per distance state, `[distState << 6 | slot]`. */
37
+ private readonly distSlotPrices;
38
+ /** Price of each distance below `FULL_DISTANCES` per distance state, `[distState << 7 | dist]`. */
39
+ private readonly fullDistPrices;
40
+ private readonly alignPrices;
41
+ /**
42
+ * Symbol chosen by `getNextSymbol()`:
43
+ * -1 = literal, 0-3 = rep match, 4+ = match with distance `back - REPS`.
44
+ */
45
+ protected back: number;
46
+ /** Bytes the match finder is ahead of the encoder. */
47
+ protected readAhead: number;
48
+ constructor(rc: RangeEncoder, lz: LzEncoder, config: LzmaEncoderConfig);
49
+ /** Prepares for a new, independent input written to `rc`. */
50
+ restart(rc: RangeEncoder): void;
51
+ /**
52
+ * Chooses the next symbol: sets `back` and returns its length.
53
+ */
54
+ protected abstract getNextSymbol(): number;
55
+ reset(): void;
56
+ /** Encodes as much of the buffered input as the lookahead allows. */
57
+ encode(): void;
58
+ /** Encodes the end marker: a match with distance 0xFFFFFFFF. */
59
+ encodeEndMarker(): void;
60
+ /** The first symbol is always a literal with no previous byte. */
61
+ private encodeInit;
62
+ private encodeSymbol;
63
+ private encodeLiteral;
64
+ private encodeMatch;
65
+ private encodeRepMatch;
66
+ protected getMatches(): Matches;
67
+ protected skip(len: number): void;
68
+ protected getLiteralPrice(curByte: number, matchByte: number, prevByte: number, pos: number, state: number): number;
69
+ protected getAnyMatchPrice(state: number, posState: number): number;
70
+ protected getNormalMatchPrice(anyMatchPrice: number, state: number): number;
71
+ protected getAnyRepPrice(anyMatchPrice: number, state: number): number;
72
+ protected getShortRepPrice(anyRepPrice: number, state: number, posState: number): number;
73
+ protected getLongRepPrice(anyRepPrice: number, rep: number, state: number, posState: number): number;
74
+ protected getLongRepAndLenPrice(rep: number, len: number, state: number, posState: number): number;
75
+ protected getMatchAndLenPrice(normalMatchPrice: number, dist: number, len: number, posState: number): number;
76
+ /** Price of coding `dist` in distance state `distState`. */
77
+ protected getDistPrice(distState: number, dist: number): number;
78
+ /** Refreshes the price tables that are due for an update. */
79
+ protected updatePrices(): void;
80
+ private updateDistPrices;
81
+ private updateAlignPrices;
82
+ }
83
+ /**
84
+ * Length encoder with cached prices. Prices of each position state are
85
+ * recalculated after every `LEN_PRICE_UPDATE_INTERVAL` encoded lengths.
86
+ */
87
+ export declare class LengthEncoder {
88
+ private readonly probs;
89
+ /** Offset of this length coder in `probs`. */
90
+ private readonly coder;
91
+ private readonly counters;
92
+ /** Whether a length was encoded since the last reset. */
93
+ private used;
94
+ /** Prices per position state, `[posState * lenSymbols + len - MATCH_LEN_MIN]`. */
95
+ readonly prices: Uint32Array;
96
+ readonly lenSymbols: number;
97
+ constructor(probs: Probs, coder: number, pb: number, niceLen: number);
98
+ reset(): void;
99
+ encode(rc: RangeEncoder, lenIn: number, posState: number): void;
100
+ getPrice(len: number, posState: number): number;
101
+ /** Offset in `prices` such that `prices[offset + len]` is the price of `len`. */
102
+ priceOffset(posState: number): number;
103
+ updatePrices(): void;
104
+ private updatePosStatePrices;
105
+ }
@@ -0,0 +1,497 @@
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
+ * LZMA encoder base: codes the symbols chosen by a subclass and keeps the
5
+ * price tables the subclasses use to choose between symbols.
6
+ *
7
+ * Subclasses implement `getNextSymbol()`:
8
+ * - `LzmaEncoderFast` uses simple heuristics (levels 1-3).
9
+ * - `LzmaEncoderNormal` searches for the cheapest symbol sequence (levels 4-9).
10
+ *
11
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.LengthEncoder = exports.LzmaEncoder = void 0;
15
+ exports.getDistSlot = getDistSlot;
16
+ const lzma_coder_1 = require("./lzma-coder");
17
+ const range_coder_1 = require("./range-coder");
18
+ const range_encoder_1 = require("./range-encoder");
19
+ const DIST_PRICE_UPDATE_INTERVAL = lzma_coder_1.FULL_DISTANCES;
20
+ const ALIGN_PRICE_UPDATE_INTERVAL = lzma_coder_1.ALIGN_SIZE;
21
+ const LEN_PRICE_UPDATE_INTERVAL = 32;
22
+ /** Returns the distance slot: roughly 2 * log2(dist), plus the next bit. */
23
+ function getDistSlot(dist) {
24
+ if (dist <= lzma_coder_1.DIST_MODEL_START && dist >= 0) {
25
+ return dist;
26
+ }
27
+ const i = 31 - Math.clz32(dist);
28
+ return (i << 1) + ((dist >>> (i - 1)) & 1);
29
+ }
30
+ class LzmaEncoder extends lzma_coder_1.LzmaCoder {
31
+ rc;
32
+ lz;
33
+ niceLen;
34
+ matchLenEncoder;
35
+ repLenEncoder;
36
+ distPriceCount = 0;
37
+ alignPriceCount = 0;
38
+ /** Whether a distance or align bits were encoded since the last reset. */
39
+ distModelsUsed = false;
40
+ alignModelUsed = false;
41
+ distSlotPricesSize;
42
+ /** Price of each distance slot per distance state, `[distState << 6 | slot]`. */
43
+ distSlotPrices = new Uint32Array(lzma_coder_1.DIST_STATES << lzma_coder_1.DIST_SLOT_BITS);
44
+ /** Price of each distance below `FULL_DISTANCES` per distance state, `[distState << 7 | dist]`. */
45
+ fullDistPrices = new Uint32Array(lzma_coder_1.DIST_STATES * lzma_coder_1.FULL_DISTANCES);
46
+ alignPrices = new Uint32Array(lzma_coder_1.ALIGN_SIZE);
47
+ /**
48
+ * Symbol chosen by `getNextSymbol()`:
49
+ * -1 = literal, 0-3 = rep match, 4+ = match with distance `back - REPS`.
50
+ */
51
+ back = 0;
52
+ /** Bytes the match finder is ahead of the encoder. */
53
+ readAhead = -1;
54
+ constructor(rc, lz, config) {
55
+ super(config.lc, config.lp, config.pb);
56
+ this.rc = rc;
57
+ this.lz = lz;
58
+ this.niceLen = config.niceLen;
59
+ this.matchLenEncoder = new LengthEncoder(this.probs, lzma_coder_1.MATCH_LEN, config.pb, config.niceLen);
60
+ this.repLenEncoder = new LengthEncoder(this.probs, lzma_coder_1.REP_LEN, config.pb, config.niceLen);
61
+ this.distSlotPricesSize = getDistSlot(config.dictSize - 1) + 1;
62
+ this.reset();
63
+ }
64
+ /** Prepares for a new, independent input written to `rc`. */
65
+ restart(rc) {
66
+ this.rc = rc;
67
+ this.lz.reset();
68
+ this.reset();
69
+ }
70
+ reset() {
71
+ super.reset();
72
+ this.matchLenEncoder.reset();
73
+ this.repLenEncoder.reset();
74
+ this.distPriceCount = 0;
75
+ this.alignPriceCount = 0;
76
+ this.distModelsUsed = false;
77
+ this.alignModelUsed = false;
78
+ this.readAhead = -1;
79
+ }
80
+ /** Encodes as much of the buffered input as the lookahead allows. */
81
+ encode() {
82
+ if (!this.lz.isStarted() && !this.encodeInit()) {
83
+ return;
84
+ }
85
+ while (this.encodeSymbol()) {
86
+ // Each call encodes one symbol.
87
+ }
88
+ }
89
+ /** Encodes the end marker: a match with distance 0xFFFFFFFF. */
90
+ encodeEndMarker() {
91
+ const posState = (this.lz.getPos() - this.readAhead) & this.posMask;
92
+ this.rc.encodeBit(this.probs, lzma_coder_1.IS_MATCH + (this.state << 4) + posState, 1);
93
+ this.rc.encodeBit(this.probs, lzma_coder_1.IS_REP + this.state, 0);
94
+ this.encodeMatch(-1, lzma_coder_1.MATCH_LEN_MIN, posState);
95
+ }
96
+ /** The first symbol is always a literal with no previous byte. */
97
+ encodeInit() {
98
+ if (!this.lz.hasEnoughData(0)) {
99
+ return false;
100
+ }
101
+ this.skip(1);
102
+ this.rc.encodeBit(this.probs, lzma_coder_1.IS_MATCH + (this.state << 4), 0);
103
+ this.encodeLiteral(this.literalOffset(0, 0));
104
+ --this.readAhead;
105
+ return true;
106
+ }
107
+ encodeSymbol() {
108
+ if (!this.lz.hasEnoughData(this.readAhead + 1)) {
109
+ return false;
110
+ }
111
+ const len = this.getNextSymbol();
112
+ const lz = this.lz;
113
+ const rc = this.rc;
114
+ const probs = this.probs;
115
+ const pos = lz.getPos() - this.readAhead;
116
+ const posState = pos & this.posMask;
117
+ const back = this.back;
118
+ if (back === -1) {
119
+ rc.encodeBit(probs, lzma_coder_1.IS_MATCH + (this.state << 4) + posState, 0);
120
+ this.encodeLiteral(this.literalOffset(lz.getByte(1 + this.readAhead), pos));
121
+ }
122
+ else {
123
+ rc.encodeBit(probs, lzma_coder_1.IS_MATCH + (this.state << 4) + posState, 1);
124
+ if (back < lzma_coder_1.REPS) {
125
+ rc.encodeBit(probs, lzma_coder_1.IS_REP + this.state, 1);
126
+ this.encodeRepMatch(back, len, posState);
127
+ }
128
+ else {
129
+ rc.encodeBit(probs, lzma_coder_1.IS_REP + this.state, 0);
130
+ this.encodeMatch(back - lzma_coder_1.REPS, len, posState);
131
+ }
132
+ }
133
+ this.readAhead -= len;
134
+ return true;
135
+ }
136
+ encodeLiteral(offset) {
137
+ const symbol = this.lz.getByte(this.readAhead);
138
+ if ((0, lzma_coder_1.isLiteralState)(this.state)) {
139
+ this.rc.encodeLiteral(this.probs, offset, symbol);
140
+ }
141
+ else {
142
+ const matchByte = this.lz.getByte(this.reps[0] + 1 + this.readAhead);
143
+ this.rc.encodeMatchedLiteral(this.probs, offset, symbol, matchByte);
144
+ }
145
+ this.state = (0, lzma_coder_1.stateAfterLiteral)(this.state);
146
+ }
147
+ encodeMatch(dist, len, posState) {
148
+ const rc = this.rc;
149
+ const probs = this.probs;
150
+ const reps = this.reps;
151
+ this.state = (0, lzma_coder_1.stateAfterMatch)(this.state);
152
+ this.matchLenEncoder.encode(rc, len, posState);
153
+ const distSlot = getDistSlot(dist);
154
+ rc.encodeBitTree(probs, lzma_coder_1.DIST_SLOTS + ((0, lzma_coder_1.getDistState)(len) << lzma_coder_1.DIST_SLOT_BITS), lzma_coder_1.DIST_SLOT_BITS, distSlot);
155
+ this.distModelsUsed = true;
156
+ if (distSlot >= lzma_coder_1.DIST_MODEL_START) {
157
+ const footerBits = (distSlot >>> 1) - 1;
158
+ const base = (2 | (distSlot & 1)) << footerBits;
159
+ const distReduced = dist - base;
160
+ if (distSlot < lzma_coder_1.DIST_MODEL_END) {
161
+ rc.encodeReverseBitTree(probs, (0, lzma_coder_1.distSpecialOffset)(distSlot, base), footerBits, distReduced);
162
+ }
163
+ else {
164
+ rc.encodeDirectBits(distReduced >>> lzma_coder_1.ALIGN_BITS, footerBits - lzma_coder_1.ALIGN_BITS);
165
+ rc.encodeReverseBitTree(probs, lzma_coder_1.DIST_ALIGN, lzma_coder_1.ALIGN_BITS, distReduced & lzma_coder_1.ALIGN_MASK);
166
+ this.alignModelUsed = true;
167
+ --this.alignPriceCount;
168
+ }
169
+ }
170
+ reps[3] = reps[2];
171
+ reps[2] = reps[1];
172
+ reps[1] = reps[0];
173
+ reps[0] = dist;
174
+ --this.distPriceCount;
175
+ }
176
+ encodeRepMatch(rep, len, posState) {
177
+ const rc = this.rc;
178
+ const probs = this.probs;
179
+ const reps = this.reps;
180
+ const state = this.state;
181
+ if (rep === 0) {
182
+ rc.encodeBit(probs, lzma_coder_1.IS_REP0 + state, 0);
183
+ rc.encodeBit(probs, lzma_coder_1.IS_REP0_LONG + (state << 4) + posState, len === 1 ? 0 : 1);
184
+ }
185
+ else {
186
+ const dist = reps[rep];
187
+ rc.encodeBit(probs, lzma_coder_1.IS_REP0 + state, 1);
188
+ if (rep === 1) {
189
+ rc.encodeBit(probs, lzma_coder_1.IS_REP1 + state, 0);
190
+ }
191
+ else {
192
+ rc.encodeBit(probs, lzma_coder_1.IS_REP1 + state, 1);
193
+ rc.encodeBit(probs, lzma_coder_1.IS_REP2 + state, rep - 2);
194
+ if (rep === 3) {
195
+ reps[3] = reps[2];
196
+ }
197
+ reps[2] = reps[1];
198
+ }
199
+ reps[1] = reps[0];
200
+ reps[0] = dist;
201
+ }
202
+ if (len === 1) {
203
+ this.state = (0, lzma_coder_1.stateAfterShortRep)(state);
204
+ }
205
+ else {
206
+ this.repLenEncoder.encode(rc, len, posState);
207
+ this.state = (0, lzma_coder_1.stateAfterLongRep)(state);
208
+ }
209
+ }
210
+ // -- Match finder access --
211
+ getMatches() {
212
+ ++this.readAhead;
213
+ return this.lz.getMatches();
214
+ }
215
+ skip(len) {
216
+ this.readAhead += len;
217
+ this.lz.skip(len);
218
+ }
219
+ // -- Prices --
220
+ getLiteralPrice(curByte, matchByte, prevByte, pos, state) {
221
+ const probs = this.probs;
222
+ const offset = this.literalOffset(prevByte, pos);
223
+ const price = (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_MATCH + (state << 4) + (pos & this.posMask)], 0);
224
+ return price + ((0, lzma_coder_1.isLiteralState)(state) ? getLiteralNormalPrice(probs, offset, curByte) : getLiteralMatchedPrice(probs, offset, curByte, matchByte));
225
+ }
226
+ getAnyMatchPrice(state, posState) {
227
+ return (0, range_encoder_1.getBitPrice)(this.probs[lzma_coder_1.IS_MATCH + (state << 4) + posState], 1);
228
+ }
229
+ getNormalMatchPrice(anyMatchPrice, state) {
230
+ return anyMatchPrice + (0, range_encoder_1.getBitPrice)(this.probs[lzma_coder_1.IS_REP + state], 0);
231
+ }
232
+ getAnyRepPrice(anyMatchPrice, state) {
233
+ return anyMatchPrice + (0, range_encoder_1.getBitPrice)(this.probs[lzma_coder_1.IS_REP + state], 1);
234
+ }
235
+ getShortRepPrice(anyRepPrice, state, posState) {
236
+ const probs = this.probs;
237
+ return anyRepPrice + (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP0 + state], 0) + (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP0_LONG + (state << 4) + posState], 0);
238
+ }
239
+ getLongRepPrice(anyRepPrice, rep, state, posState) {
240
+ const probs = this.probs;
241
+ let price = anyRepPrice;
242
+ if (rep === 0) {
243
+ price += (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP0 + state], 0) + (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP0_LONG + (state << 4) + posState], 1);
244
+ }
245
+ else {
246
+ price += (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP0 + state], 1);
247
+ if (rep === 1) {
248
+ price += (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP1 + state], 0);
249
+ }
250
+ else {
251
+ price += (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP1 + state], 1) + (0, range_encoder_1.getBitPrice)(probs[lzma_coder_1.IS_REP2 + state], rep - 2);
252
+ }
253
+ }
254
+ return price;
255
+ }
256
+ getLongRepAndLenPrice(rep, len, state, posState) {
257
+ const anyMatchPrice = this.getAnyMatchPrice(state, posState);
258
+ const anyRepPrice = this.getAnyRepPrice(anyMatchPrice, state);
259
+ const longRepPrice = this.getLongRepPrice(anyRepPrice, rep, state, posState);
260
+ return longRepPrice + this.repLenEncoder.getPrice(len, posState);
261
+ }
262
+ getMatchAndLenPrice(normalMatchPrice, dist, len, posState) {
263
+ return normalMatchPrice + this.matchLenEncoder.getPrice(len, posState) + this.getDistPrice((0, lzma_coder_1.getDistState)(len), dist);
264
+ }
265
+ /** Price of coding `dist` in distance state `distState`. */
266
+ getDistPrice(distState, dist) {
267
+ if (dist < lzma_coder_1.FULL_DISTANCES) {
268
+ return this.fullDistPrices[distState * lzma_coder_1.FULL_DISTANCES + dist];
269
+ }
270
+ // distSlotPrices includes the price of the direct bits.
271
+ return this.distSlotPrices[(distState << lzma_coder_1.DIST_SLOT_BITS) + getDistSlot(dist)] + this.alignPrices[dist & lzma_coder_1.ALIGN_MASK];
272
+ }
273
+ /** Refreshes the price tables that are due for an update. */
274
+ updatePrices() {
275
+ if (this.distPriceCount <= 0) {
276
+ this.updateDistPrices();
277
+ }
278
+ if (this.alignPriceCount <= 0) {
279
+ this.updateAlignPrices();
280
+ }
281
+ this.matchLenEncoder.updatePrices();
282
+ this.repLenEncoder.updatePrices();
283
+ }
284
+ updateDistPrices() {
285
+ this.distPriceCount = DIST_PRICE_UPDATE_INTERVAL;
286
+ if (this.distModelsUsed) {
287
+ computeDistPrices(this.probs, this.distSlotPricesSize, this.distSlotPrices, this.fullDistPrices);
288
+ }
289
+ else {
290
+ // The models still have their initial probabilities.
291
+ const initial = getInitialPrices();
292
+ this.distSlotPrices.set(initial.distSlots);
293
+ this.fullDistPrices.set(initial.fullDists);
294
+ }
295
+ }
296
+ updateAlignPrices() {
297
+ this.alignPriceCount = ALIGN_PRICE_UPDATE_INTERVAL;
298
+ if (this.alignModelUsed) {
299
+ computeAlignPrices(this.probs, this.alignPrices);
300
+ }
301
+ else {
302
+ this.alignPrices.set(getInitialPrices().align);
303
+ }
304
+ }
305
+ }
306
+ exports.LzmaEncoder = LzmaEncoder;
307
+ /**
308
+ * Prices of the distance slots `[distState << 6 | slot]` (including the
309
+ * direct bits) and of the distances below `FULL_DISTANCES`
310
+ * `[distState * FULL_DISTANCES + dist]`.
311
+ */
312
+ function computeDistPrices(probs, slotCount, slotPrices, fullPrices) {
313
+ for (let distState = 0; distState < lzma_coder_1.DIST_STATES; ++distState) {
314
+ const slots = distState << lzma_coder_1.DIST_SLOT_BITS;
315
+ const tree = lzma_coder_1.DIST_SLOTS + slots;
316
+ for (let distSlot = 0; distSlot < slotCount; ++distSlot) {
317
+ slotPrices[slots + distSlot] = (0, range_encoder_1.getBitTreePrice)(probs, tree, lzma_coder_1.DIST_SLOT_BITS, distSlot);
318
+ }
319
+ for (let distSlot = lzma_coder_1.DIST_MODEL_END; distSlot < slotCount; ++distSlot) {
320
+ const count = (distSlot >>> 1) - 1 - lzma_coder_1.ALIGN_BITS;
321
+ slotPrices[slots + distSlot] += (0, range_encoder_1.getDirectBitsPrice)(count);
322
+ }
323
+ for (let dist = 0; dist < lzma_coder_1.DIST_MODEL_START; ++dist) {
324
+ fullPrices[distState * lzma_coder_1.FULL_DISTANCES + dist] = slotPrices[slots + dist];
325
+ }
326
+ }
327
+ let dist = lzma_coder_1.DIST_MODEL_START;
328
+ for (let distSlot = lzma_coder_1.DIST_MODEL_START; distSlot < lzma_coder_1.DIST_MODEL_END; ++distSlot) {
329
+ const footerBits = (distSlot >>> 1) - 1;
330
+ const base = (2 | (distSlot & 1)) << footerBits;
331
+ const tree = (0, lzma_coder_1.distSpecialOffset)(distSlot, base);
332
+ for (let i = 0; i < 1 << footerBits; ++i) {
333
+ const price = (0, range_encoder_1.getReverseBitTreePrice)(probs, tree, footerBits, dist - base);
334
+ for (let distState = 0; distState < lzma_coder_1.DIST_STATES; ++distState) {
335
+ fullPrices[distState * lzma_coder_1.FULL_DISTANCES + dist] = slotPrices[(distState << lzma_coder_1.DIST_SLOT_BITS) + distSlot] + price;
336
+ }
337
+ ++dist;
338
+ }
339
+ }
340
+ }
341
+ function computeAlignPrices(probs, alignPrices) {
342
+ for (let i = 0; i < lzma_coder_1.ALIGN_SIZE; ++i) {
343
+ alignPrices[i] = (0, range_encoder_1.getReverseBitTreePrice)(probs, lzma_coder_1.DIST_ALIGN, lzma_coder_1.ALIGN_BITS, i);
344
+ }
345
+ }
346
+ /**
347
+ * Prices of `count` lengths for `posState` with the length coder at
348
+ * `coder`, written to `prices` from `start`.
349
+ */
350
+ function computeLengthPrices(probs, coder, posState, prices, start, count) {
351
+ const end = start + count;
352
+ let i = 0;
353
+ let choice0Price = (0, range_encoder_1.getBitPrice)(probs[coder + lzma_coder_1.LEN_CHOICE], 0);
354
+ const low = coder + lzma_coder_1.LEN_LOW + posState * lzma_coder_1.LOW_SYMBOLS;
355
+ for (; i < lzma_coder_1.LOW_SYMBOLS; ++i) {
356
+ prices[start + i] = choice0Price + (0, range_encoder_1.getBitTreePrice)(probs, low, 3, i);
357
+ }
358
+ choice0Price = (0, range_encoder_1.getBitPrice)(probs[coder + lzma_coder_1.LEN_CHOICE], 1);
359
+ let choice1Price = (0, range_encoder_1.getBitPrice)(probs[coder + lzma_coder_1.LEN_CHOICE2], 0);
360
+ const mid = coder + lzma_coder_1.LEN_MID + posState * lzma_coder_1.MID_SYMBOLS;
361
+ for (; i < lzma_coder_1.LOW_SYMBOLS + lzma_coder_1.MID_SYMBOLS; ++i) {
362
+ prices[start + i] = choice0Price + choice1Price + (0, range_encoder_1.getBitTreePrice)(probs, mid, 3, i - lzma_coder_1.LOW_SYMBOLS);
363
+ }
364
+ choice1Price = (0, range_encoder_1.getBitPrice)(probs[coder + lzma_coder_1.LEN_CHOICE2], 1);
365
+ for (; start + i < end; ++i) {
366
+ prices[start + i] = choice0Price + choice1Price + (0, range_encoder_1.getBitTreePrice)(probs, coder + lzma_coder_1.LEN_HIGH, 8, i - lzma_coder_1.LOW_SYMBOLS - lzma_coder_1.MID_SYMBOLS);
367
+ }
368
+ }
369
+ let initialPrices;
370
+ /**
371
+ * Price tables for models that still have their initial probabilities.
372
+ * They are the same for every encoder, so they are computed once. Copying
373
+ * them saves about a quarter of the time of compressing a small input.
374
+ */
375
+ function getInitialPrices() {
376
+ if (initialPrices === undefined) {
377
+ const probs = new Uint16Array(lzma_coder_1.LITERAL);
378
+ (0, range_coder_1.initProbs)(probs);
379
+ const distSlots = new Uint32Array(lzma_coder_1.DIST_STATES << lzma_coder_1.DIST_SLOT_BITS);
380
+ const fullDists = new Uint32Array(lzma_coder_1.DIST_STATES * lzma_coder_1.FULL_DISTANCES);
381
+ computeDistPrices(probs, lzma_coder_1.DIST_SLOT_COUNT, distSlots, fullDists);
382
+ const align = new Uint32Array(lzma_coder_1.ALIGN_SIZE);
383
+ computeAlignPrices(probs, align);
384
+ const lengths = new Uint32Array(lzma_coder_1.MATCH_LEN_MAX - lzma_coder_1.MATCH_LEN_MIN + 1);
385
+ computeLengthPrices(probs, lzma_coder_1.MATCH_LEN, 0, lengths, 0, lengths.length);
386
+ initialPrices = { distSlots, fullDists, align, lengths };
387
+ }
388
+ return initialPrices;
389
+ }
390
+ function getLiteralNormalPrice(probs, offset, symbolIn) {
391
+ let symbol = symbolIn;
392
+ let price = 0;
393
+ symbol |= 0x100;
394
+ do {
395
+ price += (0, range_encoder_1.getBitPrice)(probs[offset + (symbol >>> 8)], (symbol >>> 7) & 1);
396
+ symbol <<= 1;
397
+ } while (symbol < 0x10000);
398
+ return price;
399
+ }
400
+ function getLiteralMatchedPrice(probs, offset, symbolIn, matchByteIn) {
401
+ let symbol = symbolIn;
402
+ let matchByte = matchByteIn;
403
+ let price = 0;
404
+ let matchOffset = 0x100;
405
+ symbol |= 0x100;
406
+ do {
407
+ matchByte <<= 1;
408
+ const matchBit = matchByte & matchOffset;
409
+ price += (0, range_encoder_1.getBitPrice)(probs[offset + matchOffset + matchBit + (symbol >>> 8)], (symbol >>> 7) & 1);
410
+ symbol <<= 1;
411
+ matchOffset &= ~(matchByte ^ symbol);
412
+ } while (symbol < 0x10000);
413
+ return price;
414
+ }
415
+ /**
416
+ * Length encoder with cached prices. Prices of each position state are
417
+ * recalculated after every `LEN_PRICE_UPDATE_INTERVAL` encoded lengths.
418
+ */
419
+ class LengthEncoder {
420
+ probs;
421
+ /** Offset of this length coder in `probs`. */
422
+ coder;
423
+ counters;
424
+ /** Whether a length was encoded since the last reset. */
425
+ used = false;
426
+ /** Prices per position state, `[posState * lenSymbols + len - MATCH_LEN_MIN]`. */
427
+ prices;
428
+ lenSymbols;
429
+ constructor(probs, coder, pb, niceLen) {
430
+ this.probs = probs;
431
+ this.coder = coder;
432
+ const posStates = 1 << pb;
433
+ this.counters = new Int32Array(posStates);
434
+ // Always allocate at least LOW_SYMBOLS + MID_SYMBOLS entries to keep
435
+ // updatePrices() simple.
436
+ this.lenSymbols = Math.max(niceLen - lzma_coder_1.MATCH_LEN_MIN + 1, lzma_coder_1.LOW_SYMBOLS + lzma_coder_1.MID_SYMBOLS);
437
+ this.prices = new Uint32Array(posStates * this.lenSymbols);
438
+ }
439
+ reset() {
440
+ // Zero counters force a price update before the prices are needed.
441
+ this.counters.fill(0);
442
+ this.used = false;
443
+ }
444
+ encode(rc, lenIn, posState) {
445
+ let len = lenIn;
446
+ const probs = this.probs;
447
+ const coder = this.coder;
448
+ len -= lzma_coder_1.MATCH_LEN_MIN;
449
+ if (len < lzma_coder_1.LOW_SYMBOLS) {
450
+ rc.encodeBit(probs, coder + lzma_coder_1.LEN_CHOICE, 0);
451
+ rc.encodeBitTree(probs, coder + lzma_coder_1.LEN_LOW + posState * lzma_coder_1.LOW_SYMBOLS, 3, len);
452
+ }
453
+ else {
454
+ rc.encodeBit(probs, coder + lzma_coder_1.LEN_CHOICE, 1);
455
+ len -= lzma_coder_1.LOW_SYMBOLS;
456
+ if (len < lzma_coder_1.MID_SYMBOLS) {
457
+ rc.encodeBit(probs, coder + lzma_coder_1.LEN_CHOICE2, 0);
458
+ rc.encodeBitTree(probs, coder + lzma_coder_1.LEN_MID + posState * lzma_coder_1.MID_SYMBOLS, 3, len);
459
+ }
460
+ else {
461
+ rc.encodeBit(probs, coder + lzma_coder_1.LEN_CHOICE2, 1);
462
+ rc.encodeBitTree(probs, coder + lzma_coder_1.LEN_HIGH, 8, len - lzma_coder_1.MID_SYMBOLS);
463
+ }
464
+ }
465
+ --this.counters[posState];
466
+ this.used = true;
467
+ }
468
+ getPrice(len, posState) {
469
+ return this.prices[posState * this.lenSymbols + len - lzma_coder_1.MATCH_LEN_MIN];
470
+ }
471
+ /** Offset in `prices` such that `prices[offset + len]` is the price of `len`. */
472
+ priceOffset(posState) {
473
+ return posState * this.lenSymbols - lzma_coder_1.MATCH_LEN_MIN;
474
+ }
475
+ updatePrices() {
476
+ for (let posState = 0; posState < this.counters.length; ++posState) {
477
+ if (this.counters[posState] <= 0) {
478
+ this.counters[posState] = LEN_PRICE_UPDATE_INTERVAL;
479
+ this.updatePosStatePrices(posState);
480
+ }
481
+ }
482
+ }
483
+ updatePosStatePrices(posState) {
484
+ const start = posState * this.lenSymbols;
485
+ if (this.used) {
486
+ computeLengthPrices(this.probs, this.coder, posState, this.prices, start, this.lenSymbols);
487
+ }
488
+ else {
489
+ // The model still has its initial probabilities.
490
+ const initial = getInitialPrices().lengths;
491
+ for (let i = 0; i < this.lenSymbols; ++i) {
492
+ this.prices[start + i] = initial[i];
493
+ }
494
+ }
495
+ }
496
+ }
497
+ exports.LengthEncoder = LengthEncoder;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Decoder for raw LZMA2 data, the chunked format inside `.xz` blocks and
3
+ * 7z LZMA2 entries. LZMA2 has no header of its own. The container stores
4
+ * the dictionary size.
5
+ *
6
+ * Each chunk starts with a control byte:
7
+ *
8
+ * | control | chunk |
9
+ * | --------- | ------------------------------------------------------ |
10
+ * | 0x00 | end of the data |
11
+ * | 0x01 | uncompressed, dictionary reset |
12
+ * | 0x02 | uncompressed, no reset |
13
+ * | 0x80-0x9F | LZMA, no reset |
14
+ * | 0xA0-0xBF | LZMA, state reset |
15
+ * | 0xC0-0xDF | LZMA, state reset and new properties |
16
+ * | 0xE0-0xFF | LZMA, dictionary reset, state reset and new properties |
17
+ *
18
+ * The sizes follow as big-endian values minus one, then the data:
19
+ *
20
+ * | bytes | uncompressed chunk | LZMA chunk |
21
+ * | ----- | ------------------ | ------------------------------------------ |
22
+ * | 1-2 | size | uncompressed size, bits 0-15; bits 16-20 |
23
+ * | | | are the low 5 bits of the control byte |
24
+ * | 3-4 | | compressed size |
25
+ * | 5 | | `(pb * 5 + lp) * 9 + lc`, 0xC0 and up |
26
+ *
27
+ * The first chunk has to reset the dictionary, and after a dictionary reset
28
+ * the next LZMA chunk has to set properties. Every LZMA chunk starts a new
29
+ * range coder, and LZMA2 doesn't use the end marker of the LZMA layer.
30
+ *
31
+ * Input may arrive in pieces. The decoder copies uncompressed chunks as
32
+ * they arrive and decodes an LZMA chunk once its compressed data, at most
33
+ * 64 KiB, is complete.
34
+ *
35
+ * Ported from XZ for Java (0BSD) by Lasse Collin and Igor Pavlov.
36
+ */
37
+ export declare class Lzma2Decoder {
38
+ private readonly lz;
39
+ private readonly rc;
40
+ private lzma;
41
+ private uncompressedSize;
42
+ private compressedSize;
43
+ private isLzmaChunk;
44
+ private needDictReset;
45
+ private needProps;
46
+ private endReached;
47
+ /**
48
+ * Bytes decoded since the last dictionary reset. LZMA's position bits
49
+ * come from it. XZ for Java reads it from the dictionary
50
+ * (`lz.getPos()`); `LzmaDecoder` counts it in `outPos`.
51
+ */
52
+ private outPos;
53
+ /** Header of the next chunk, collected until it is complete. */
54
+ private readonly header;
55
+ private headerPos;
56
+ /**
57
+ * Compressed data of the current LZMA chunk, collected until it is
58
+ * complete. `INPUT_MARGIN` bytes after the data leave room for corrupt
59
+ * input, which can make the decoder read past it.
60
+ */
61
+ private readonly inBuf;
62
+ private inPos;
63
+ /**
64
+ * @param dictSize Dictionary size in bytes
65
+ * @param sink Receives decompressed data. The chunk is only valid during
66
+ * the call.
67
+ */
68
+ constructor(dictSize: number, sink: (chunk: Uint8Array) => void);
69
+ /**
70
+ * Approximate memory needed to decode, in KiB, as XZ for Java's
71
+ * `LZMA2InputStream.getMemoryUsage` computes it.
72
+ */
73
+ static getMemoryUsage(dictSize: number): number;
74
+ /** Decodes as much of `input` as possible. */
75
+ write(input: Uint8Array): void;
76
+ /** Throws if the data ended before its end marker. */
77
+ end(): void;
78
+ /** Collects the chunk header and decodes it once it is complete. */
79
+ private readChunkHeader;
80
+ private decodeChunkHeader;
81
+ private decodeProps;
82
+ /** The LZMA decoder of the last properties, which a chunk without properties continues. */
83
+ private currentLzma;
84
+ private decodeLzmaChunk;
85
+ }