lzma1 0.1.2 → 0.2.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.
@@ -0,0 +1,132 @@
1
+ import { arraycopy } from "./utils.js";
2
+ /**
3
+ * LzInWindow - Input Window helper for LZMA encoding
4
+ *
5
+ * This class manages the input window operations for LZMA encoding,
6
+ * including buffer management, position tracking, and input stream reading.
7
+ */
8
+ export class LzInWindow {
9
+ matchFinder;
10
+ constructor(matchFinder) {
11
+ this.matchFinder = matchFinder;
12
+ }
13
+ /**
14
+ * Get a byte at the specified index relative to current position
15
+ */
16
+ getIndexByte(index) {
17
+ const byte = this.matchFinder._bufferBase[this.matchFinder._bufferOffset + this.matchFinder._pos + index];
18
+ return byte;
19
+ }
20
+ /**
21
+ * Calculate match length between current position and a previous position
22
+ */
23
+ getMatchLen(index, distance, limit) {
24
+ if (this.matchFinder._streamEndWasReached) {
25
+ if (this.matchFinder._pos + index + limit > this.matchFinder._streamPos) {
26
+ limit = this.matchFinder._streamPos - (this.matchFinder._pos + index);
27
+ }
28
+ }
29
+ ++distance;
30
+ let i;
31
+ const pby = this.matchFinder._bufferOffset + this.matchFinder._pos + index;
32
+ for (i = 0; i < limit
33
+ && this.matchFinder._bufferBase[pby + i]
34
+ == this.matchFinder._bufferBase[pby + i - distance]; ++i)
35
+ ;
36
+ return i;
37
+ }
38
+ /**
39
+ * Get number of available bytes in the input window
40
+ */
41
+ getNumAvailableBytes() {
42
+ return this.matchFinder._streamPos - this.matchFinder._pos;
43
+ }
44
+ /**
45
+ * Move buffer block when reaching buffer boundaries
46
+ */
47
+ moveBlock() {
48
+ let offset = this.matchFinder._bufferOffset + this.matchFinder._pos - this.matchFinder._keepSizeBefore;
49
+ if (offset > 0) {
50
+ --offset;
51
+ }
52
+ const numBytes = this.matchFinder._bufferOffset + this.matchFinder._streamPos - offset;
53
+ for (let i = 0; i < numBytes; ++i) {
54
+ this.matchFinder._bufferBase[i] = this.matchFinder._bufferBase[offset + i];
55
+ }
56
+ this.matchFinder._bufferOffset -= offset;
57
+ }
58
+ /**
59
+ * Move position by one and handle buffer management
60
+ */
61
+ movePos() {
62
+ this.matchFinder._pos += 1;
63
+ if (this.matchFinder._pos > this.matchFinder._posLimit) {
64
+ const pointerToPosition = this.matchFinder._bufferOffset + this.matchFinder._pos;
65
+ if (pointerToPosition > this.matchFinder._pointerToLastSafePosition) {
66
+ this.moveBlock();
67
+ }
68
+ this.readBlock();
69
+ }
70
+ }
71
+ /**
72
+ * Read a block of data from the input stream
73
+ */
74
+ readBlock() {
75
+ if (this.matchFinder._streamEndWasReached) {
76
+ return;
77
+ }
78
+ while (true) {
79
+ const size = -this.matchFinder._bufferOffset + this.matchFinder._blockSize - this.matchFinder._streamPos;
80
+ if (!size) {
81
+ return;
82
+ }
83
+ const bytesRead = this.readFromStream(this.matchFinder._bufferOffset + this.matchFinder._streamPos, size);
84
+ if (bytesRead == -1) {
85
+ this.matchFinder._posLimit = this.matchFinder._streamPos;
86
+ const pointerToPosition = this.matchFinder._bufferOffset + this.matchFinder._posLimit;
87
+ if (pointerToPosition > this.matchFinder._pointerToLastSafePosition) {
88
+ this.matchFinder._posLimit = this.matchFinder._pointerToLastSafePosition - this.matchFinder._bufferOffset;
89
+ }
90
+ this.matchFinder._streamEndWasReached = 1;
91
+ return;
92
+ }
93
+ this.matchFinder._streamPos += bytesRead;
94
+ if (this.matchFinder._streamPos >= this.matchFinder._pos + this.matchFinder._keepSizeAfter) {
95
+ this.matchFinder._posLimit = this.matchFinder._streamPos - this.matchFinder._keepSizeAfter;
96
+ }
97
+ }
98
+ }
99
+ /**
100
+ * Reduce all position offsets by the specified value
101
+ */
102
+ reduceOffsets(subValue) {
103
+ this.matchFinder._bufferOffset += subValue;
104
+ this.matchFinder._posLimit -= subValue;
105
+ this.matchFinder._pos -= subValue;
106
+ this.matchFinder._streamPos -= subValue;
107
+ }
108
+ /**
109
+ * Read data from the input stream into the buffer
110
+ */
111
+ readFromStream(off, len) {
112
+ const stream = this.matchFinder._stream;
113
+ const buffer = this.matchFinder._bufferBase;
114
+ if (stream.pos >= stream.count) {
115
+ return -1;
116
+ }
117
+ let srcBuf;
118
+ if (stream.buf instanceof Uint8Array) {
119
+ srcBuf = Array.from(stream.buf);
120
+ }
121
+ else if (stream.buf instanceof ArrayBuffer) {
122
+ srcBuf = Array.from(new Uint8Array(stream.buf));
123
+ }
124
+ else {
125
+ srcBuf = stream.buf;
126
+ }
127
+ len = Math.min(len, stream.count - stream.pos);
128
+ arraycopy(srcBuf, stream.pos, buffer, off, len);
129
+ stream.pos += len;
130
+ return len;
131
+ }
132
+ }
@@ -0,0 +1,35 @@
1
+ import type { Writer } from "./streams.js";
2
+ export declare class LzOutWindow {
3
+ buffer: number[] | null;
4
+ pos: number;
5
+ streamPos: number;
6
+ stream: Writer | null;
7
+ windowSize: number;
8
+ private w;
9
+ private buf;
10
+ constructor(writer?: Writer | null, windowSize?: number);
11
+ /**
12
+ * Copy a block of data from a previous position (LZ77-style)
13
+ */
14
+ copyBlock(distance: number, length: number): void;
15
+ /**
16
+ * Put a single byte into the window
17
+ */
18
+ putByte(byte: number): void;
19
+ /**
20
+ * Get a byte from a relative position
21
+ */
22
+ getByte(relativePos: number): number;
23
+ /**
24
+ * Flush buffered data to output writer
25
+ */
26
+ flush(): void;
27
+ /**
28
+ * Check if the window is empty
29
+ */
30
+ isEmpty(): boolean;
31
+ /**
32
+ * Reset the window
33
+ */
34
+ reset(): void;
35
+ }
@@ -0,0 +1,89 @@
1
+ export class LzOutWindow {
2
+ buffer = null;
3
+ pos = 0;
4
+ streamPos = 0;
5
+ stream = null;
6
+ windowSize = 0;
7
+ // Private Go-style properties
8
+ w = null;
9
+ buf = [];
10
+ constructor(writer = null, windowSize = 4096) {
11
+ this.w = writer;
12
+ this.stream = writer;
13
+ this.windowSize = windowSize;
14
+ this.buf = new Array(windowSize);
15
+ this.buffer = this.buf;
16
+ this.pos = 0;
17
+ this.streamPos = 0;
18
+ }
19
+ /**
20
+ * Copy a block of data from a previous position (LZ77-style)
21
+ */
22
+ copyBlock(distance, length) {
23
+ if (!this.buffer)
24
+ return;
25
+ for (let i = 0; i < length; i++) {
26
+ // Get byte from previous position
27
+ let sourcePos = this.pos - distance - 1;
28
+ if (sourcePos < 0) {
29
+ sourcePos += this.windowSize;
30
+ }
31
+ const byte = this.buffer[sourcePos];
32
+ this.putByte(byte);
33
+ }
34
+ }
35
+ /**
36
+ * Put a single byte into the window
37
+ */
38
+ putByte(byte) {
39
+ if (!this.buffer)
40
+ return;
41
+ this.buffer[this.pos] = byte;
42
+ this.pos++;
43
+ this.streamPos++;
44
+ if (this.pos >= this.windowSize) {
45
+ this.flush();
46
+ }
47
+ }
48
+ /**
49
+ * Get a byte from a relative position
50
+ */
51
+ getByte(relativePos) {
52
+ if (!this.buffer)
53
+ return 0;
54
+ let pos = this.pos + relativePos;
55
+ if (pos < 0) {
56
+ pos += this.windowSize;
57
+ }
58
+ else if (pos >= this.windowSize) {
59
+ pos -= this.windowSize;
60
+ }
61
+ return this.buffer[pos];
62
+ }
63
+ /**
64
+ * Flush buffered data to output writer
65
+ */
66
+ flush() {
67
+ if (this.w && this.buffer && this.pos > 0) {
68
+ const dataToWrite = this.buffer.slice(0, this.pos);
69
+ this.w.write(dataToWrite);
70
+ this.pos = 0;
71
+ }
72
+ }
73
+ /**
74
+ * Check if the window is empty
75
+ */
76
+ isEmpty() {
77
+ return this.streamPos === 0;
78
+ }
79
+ /**
80
+ * Reset the window
81
+ */
82
+ reset() {
83
+ this.pos = 0;
84
+ this.streamPos = 0;
85
+ if (this.buffer) {
86
+ this.buffer.fill(0);
87
+ }
88
+ }
89
+ }
package/lib/lzma.d.ts CHANGED
@@ -1,66 +1,107 @@
1
+ import type { BaseStream, BufferWithCount } from "./streams.js";
2
+ import { type BitTree, type LiteralDecoderEncoder2 } from "./utils.js";
3
+ /**
4
+ * Range coder interface
5
+ */
6
+ interface RangeCoder {
7
+ stream: BaseStream | BufferWithCount | null;
8
+ }
9
+ /**
10
+ * Range decoder
11
+ */
12
+ export interface RangeDecoder extends RangeCoder {
13
+ code: number;
14
+ rrange: number;
15
+ stream: BaseStream | null;
16
+ init?(): void;
17
+ decodeBit?(probs: number[], index: number): 0 | 1;
18
+ }
19
+ /**
20
+ * Literal coder interface
21
+ */
22
+ export interface LiteralCoder {
23
+ coders: LiteralDecoderEncoder2[];
24
+ numPrevBits: number;
25
+ numPosBits: number;
26
+ posMask: number;
27
+ init?(): void;
28
+ }
29
+ /**
30
+ * Length coder
31
+ */
32
+ export interface LenCoder {
33
+ choice: number[];
34
+ lowCoder: BitTree[];
35
+ midCoder: BitTree[];
36
+ highCoder: BitTree;
37
+ }
38
+ /**
39
+ * LZMA compression mode levels (1-9)
40
+ * Higher values provide better compression but are slower
41
+ */
42
+ export type CompressionMode = keyof typeof MODES;
43
+ /**
44
+ * Compression modes
45
+ */
46
+ export declare const MODES: {
47
+ readonly 1: {
48
+ readonly searchDepth: 16;
49
+ readonly filterStrength: 64;
50
+ readonly modeIndex: 0;
51
+ };
52
+ readonly 2: {
53
+ readonly searchDepth: 20;
54
+ readonly filterStrength: 64;
55
+ readonly modeIndex: 0;
56
+ };
57
+ readonly 3: {
58
+ readonly searchDepth: 19;
59
+ readonly filterStrength: 64;
60
+ readonly modeIndex: 1;
61
+ };
62
+ readonly 4: {
63
+ readonly searchDepth: 20;
64
+ readonly filterStrength: 64;
65
+ readonly modeIndex: 1;
66
+ };
67
+ readonly 5: {
68
+ readonly searchDepth: 21;
69
+ readonly filterStrength: 128;
70
+ readonly modeIndex: 1;
71
+ };
72
+ readonly 6: {
73
+ readonly searchDepth: 22;
74
+ readonly filterStrength: 128;
75
+ readonly modeIndex: 1;
76
+ };
77
+ readonly 7: {
78
+ readonly searchDepth: 23;
79
+ readonly filterStrength: 128;
80
+ readonly modeIndex: 1;
81
+ };
82
+ readonly 8: {
83
+ readonly searchDepth: 24;
84
+ readonly filterStrength: 255;
85
+ readonly modeIndex: 1;
86
+ };
87
+ readonly 9: {
88
+ readonly searchDepth: 25;
89
+ readonly filterStrength: 255;
90
+ readonly modeIndex: 1;
91
+ };
92
+ };
1
93
  export declare class LZMA {
2
94
  #private;
3
- readonly CompressionModes: {
4
- readonly 1: {
5
- readonly searchDepth: 16;
6
- readonly filterStrength: 64;
7
- readonly modeIndex: 0;
8
- };
9
- readonly 2: {
10
- readonly searchDepth: 20;
11
- readonly filterStrength: 64;
12
- readonly modeIndex: 0;
13
- };
14
- readonly 3: {
15
- readonly searchDepth: 19;
16
- readonly filterStrength: 64;
17
- readonly modeIndex: 1;
18
- };
19
- readonly 4: {
20
- readonly searchDepth: 20;
21
- readonly filterStrength: 64;
22
- readonly modeIndex: 1;
23
- };
24
- readonly 5: {
25
- readonly searchDepth: 21;
26
- readonly filterStrength: 128;
27
- readonly modeIndex: 1;
28
- };
29
- readonly 6: {
30
- readonly searchDepth: 22;
31
- readonly filterStrength: 128;
32
- readonly modeIndex: 1;
33
- };
34
- readonly 7: {
35
- readonly searchDepth: 23;
36
- readonly filterStrength: 128;
37
- readonly modeIndex: 1;
38
- };
39
- readonly 8: {
40
- readonly searchDepth: 24;
41
- readonly filterStrength: 255;
42
- readonly modeIndex: 1;
43
- };
44
- readonly 9: {
45
- readonly searchDepth: 25;
46
- readonly filterStrength: 255;
47
- readonly modeIndex: 1;
48
- };
49
- };
50
95
  constructor();
51
- GetLenToPosState(len: number): number;
52
- StateUpdateChar(index: number): number;
53
96
  writeHeaderProperties(): void;
54
- GetPosSlot(pos: number): number;
55
97
  GetPosSlot2(pos: number): number;
56
- reverseDecode(Models: number[], startIndex: number, NumBitLevels: number): number;
57
98
  ReverseEncode(startIndex: number, NumBitLevels: number, symbol: number): void;
58
- ReverseGetPrice(Models: number[], startIndex: number, NumBitLevels: number, symbol: number): number;
59
- InitBitModels(probs: number[]): void;
60
- GetPrice(Prob: number, symbol: number): number;
61
99
  encodeString(inputString: string): number[];
62
- compress(data: Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): Int8Array;
63
- compressString(data: string, mode?: keyof typeof this.CompressionModes): Int8Array;
100
+ compress(data: Uint8Array | ArrayBuffer, mode?: CompressionMode): Int8Array;
101
+ compressString(data: string, mode?: CompressionMode): Int8Array;
64
102
  decompress(bytearray: Uint8Array | ArrayBuffer): number[];
65
103
  decompressString(bytearray: Uint8Array | ArrayBuffer): string;
104
+ codeOneBlock(): void;
105
+ releaseStreams(): void;
66
106
  }
107
+ export {};