7z-iterator 0.2.9 → 0.2.11
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/dist/cjs/FileEntry.js +1 -1
- package/dist/cjs/FileEntry.js.map +1 -1
- package/dist/cjs/SevenZipIterator.js +1 -1
- package/dist/cjs/SevenZipIterator.js.map +1 -1
- package/dist/cjs/compat.js +1 -1
- package/dist/cjs/compat.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/lib/Lock.js +1 -1
- package/dist/cjs/lib/streamToSource.js +1 -1
- package/dist/cjs/lib/streamToSource.js.map +1 -1
- package/dist/cjs/nextEntry.js +1 -1
- package/dist/cjs/nextEntry.js.map +1 -1
- package/dist/cjs/sevenz/NumberCodec.js +1 -1
- package/dist/cjs/sevenz/NumberCodec.js.map +1 -1
- package/dist/cjs/sevenz/SevenZipParser.js +1 -1
- package/dist/cjs/sevenz/SevenZipParser.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Aes.js +1 -1
- package/dist/cjs/sevenz/codecs/Aes.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BZip2.js +1 -1
- package/dist/cjs/sevenz/codecs/Bcj.js +2 -2
- package/dist/cjs/sevenz/codecs/Bcj.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Bcj2.js +1 -1
- package/dist/cjs/sevenz/codecs/Bcj2.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjArm.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjArm.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjArm64.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjArm64.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjArmt.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjArmt.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjIa64.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjIa64.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjPpc.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjPpc.js.map +1 -1
- package/dist/cjs/sevenz/codecs/BcjSparc.js +1 -1
- package/dist/cjs/sevenz/codecs/BcjSparc.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Copy.js +1 -1
- package/dist/cjs/sevenz/codecs/Deflate.js +1 -1
- package/dist/cjs/sevenz/codecs/Delta.js +1 -1
- package/dist/cjs/sevenz/codecs/Delta.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Lzma.js +1 -1
- package/dist/cjs/sevenz/codecs/Lzma.js.map +1 -1
- package/dist/cjs/sevenz/codecs/Lzma2.js +1 -1
- package/dist/cjs/sevenz/codecs/Lzma2.js.map +1 -1
- package/dist/cjs/sevenz/codecs/createBufferingDecoder.js +1 -1
- package/dist/cjs/sevenz/codecs/createBufferingDecoder.js.map +1 -1
- package/dist/cjs/sevenz/codecs/index.js +1 -1
- package/dist/cjs/sevenz/codecs/index.js.map +1 -1
- package/dist/cjs/sevenz/codecs/lzmaCompat.d.cts +1 -1
- package/dist/cjs/sevenz/codecs/lzmaCompat.d.ts +1 -1
- package/dist/cjs/sevenz/codecs/lzmaCompat.js +1 -1
- package/dist/cjs/sevenz/codecs/lzmaCompat.js.map +1 -1
- package/dist/cjs/sevenz/codecs/streams.js +1 -1
- package/dist/cjs/sevenz/codecs/streams.js.map +1 -1
- package/dist/cjs/sevenz/constants.d.cts +8 -8
- package/dist/cjs/sevenz/constants.d.ts +8 -8
- package/dist/cjs/sevenz/constants.js +1 -1
- package/dist/cjs/sevenz/constants.js.map +1 -1
- package/dist/cjs/sevenz/headers.js +2 -2
- package/dist/cjs/sevenz/headers.js.map +1 -1
- package/dist/cjs/sevenz/index.js +1 -1
- package/dist/cjs/types.js +1 -1
- package/dist/esm/FileEntry.js +1 -1
- package/dist/esm/FileEntry.js.map +1 -1
- package/dist/esm/SevenZipIterator.js +6 -6
- package/dist/esm/SevenZipIterator.js.map +1 -1
- package/dist/esm/compat.js +2 -2
- package/dist/esm/compat.js.map +1 -1
- package/dist/esm/lib/streamToSource.js +12 -12
- package/dist/esm/lib/streamToSource.js.map +1 -1
- package/dist/esm/nextEntry.js +11 -11
- package/dist/esm/nextEntry.js.map +1 -1
- package/dist/esm/sevenz/NumberCodec.js +19 -19
- package/dist/esm/sevenz/NumberCodec.js.map +1 -1
- package/dist/esm/sevenz/SevenZipParser.js +191 -191
- package/dist/esm/sevenz/SevenZipParser.js.map +1 -1
- package/dist/esm/sevenz/codecs/Aes.js +29 -29
- package/dist/esm/sevenz/codecs/Aes.js.map +1 -1
- package/dist/esm/sevenz/codecs/Bcj.js +8 -8
- package/dist/esm/sevenz/codecs/Bcj.js.map +1 -1
- package/dist/esm/sevenz/codecs/Bcj2.js +35 -35
- package/dist/esm/sevenz/codecs/Bcj2.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjArm.js +4 -4
- package/dist/esm/sevenz/codecs/BcjArm.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjArm64.js +5 -5
- package/dist/esm/sevenz/codecs/BcjArm64.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjArmt.js +10 -10
- package/dist/esm/sevenz/codecs/BcjArmt.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjIa64.js +24 -24
- package/dist/esm/sevenz/codecs/BcjIa64.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjPpc.js +5 -5
- package/dist/esm/sevenz/codecs/BcjPpc.js.map +1 -1
- package/dist/esm/sevenz/codecs/BcjSparc.js +8 -8
- package/dist/esm/sevenz/codecs/BcjSparc.js.map +1 -1
- package/dist/esm/sevenz/codecs/Delta.js +6 -6
- package/dist/esm/sevenz/codecs/Delta.js.map +1 -1
- package/dist/esm/sevenz/codecs/Lzma.js +16 -16
- package/dist/esm/sevenz/codecs/Lzma.js.map +1 -1
- package/dist/esm/sevenz/codecs/Lzma2.js +35 -35
- package/dist/esm/sevenz/codecs/Lzma2.js.map +1 -1
- package/dist/esm/sevenz/codecs/createBufferingDecoder.js +3 -3
- package/dist/esm/sevenz/codecs/createBufferingDecoder.js.map +1 -1
- package/dist/esm/sevenz/codecs/index.js +6 -6
- package/dist/esm/sevenz/codecs/index.js.map +1 -1
- package/dist/esm/sevenz/codecs/lzmaCompat.d.ts +1 -1
- package/dist/esm/sevenz/codecs/lzmaCompat.js +5 -5
- package/dist/esm/sevenz/codecs/lzmaCompat.js.map +1 -1
- package/dist/esm/sevenz/codecs/streams.js +13 -13
- package/dist/esm/sevenz/codecs/streams.js.map +1 -1
- package/dist/esm/sevenz/constants.d.ts +8 -8
- package/dist/esm/sevenz/constants.js +9 -9
- package/dist/esm/sevenz/constants.js.map +1 -1
- package/dist/esm/sevenz/headers.js +134 -134
- package/dist/esm/sevenz/headers.js.map +1 -1
- package/package.json +20 -20
|
@@ -10,26 +10,26 @@
|
|
|
10
10
|
import { allocBuffer } from 'extract-base-iterator';
|
|
11
11
|
import createBufferingDecoder from './createBufferingDecoder.js';
|
|
12
12
|
// Range coder constants
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const kTopValue = 1 << 24;
|
|
14
|
+
const kNumBitModelTotalBits = 11;
|
|
15
|
+
const kBitModelTotal = 1 << kNumBitModelTotalBits;
|
|
16
|
+
const kNumMoveBits = 5;
|
|
17
17
|
// Number of probability models:
|
|
18
18
|
// Index 0: conditional jumps (0x0F 0x80-0x8F)
|
|
19
19
|
// Index 1: JMP (0xE9)
|
|
20
20
|
// Indices 2-257: CALL (0xE8), indexed by previous byte
|
|
21
|
-
|
|
21
|
+
const kNumProbs = 258;
|
|
22
22
|
/**
|
|
23
23
|
* Initialize range decoder
|
|
24
24
|
*/ function initRangeDecoder(stream) {
|
|
25
|
-
|
|
25
|
+
const rd = {
|
|
26
26
|
range: 0xffffffff,
|
|
27
27
|
code: 0,
|
|
28
28
|
stream: stream,
|
|
29
29
|
pos: 0
|
|
30
30
|
};
|
|
31
31
|
// Initialize code from first 5 bytes
|
|
32
|
-
for(
|
|
32
|
+
for(let i = 0; i < 5; i++){
|
|
33
33
|
rd.code = rd.code << 8 | (rd.pos < stream.length ? stream[rd.pos++] : 0);
|
|
34
34
|
}
|
|
35
35
|
return rd;
|
|
@@ -37,9 +37,9 @@ var kNumProbs = 258;
|
|
|
37
37
|
/**
|
|
38
38
|
* Decode a single bit using probability model
|
|
39
39
|
*/ function decodeBit(rd, prob, probIndex) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
const ttt = prob[probIndex];
|
|
41
|
+
const bound = (rd.range >>> kNumBitModelTotalBits) * ttt;
|
|
42
|
+
let symbol;
|
|
43
43
|
if (rd.code >>> 0 < bound >>> 0) {
|
|
44
44
|
rd.range = bound;
|
|
45
45
|
prob[probIndex] = ttt + (kBitModelTotal - ttt >>> kNumMoveBits) | 0;
|
|
@@ -69,52 +69,52 @@ var kNumProbs = 258;
|
|
|
69
69
|
// streams[1] = call stream (after LZMA)
|
|
70
70
|
// streams[2] = jump stream (after LZMA)
|
|
71
71
|
// streams[3] = range coder stream (uncompressed)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const mainStream = streams[0];
|
|
73
|
+
const callStream = streams[1];
|
|
74
|
+
const jumpStream = streams[2];
|
|
75
|
+
const rcStream = streams[3];
|
|
76
76
|
// Output buffer
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
const outSize = unpackSize || mainStream.length + callStream.length + jumpStream.length;
|
|
78
|
+
const output = allocBuffer(outSize);
|
|
79
|
+
let outPos = 0;
|
|
80
80
|
// Stream positions
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
let mainPos = 0;
|
|
82
|
+
let callPos = 0;
|
|
83
|
+
let jumpPos = 0;
|
|
84
84
|
// Initialize range decoder
|
|
85
|
-
|
|
85
|
+
const rd = initRangeDecoder(rcStream);
|
|
86
86
|
// Initialize probability models
|
|
87
|
-
|
|
88
|
-
for(
|
|
87
|
+
const probs = [];
|
|
88
|
+
for(let i = 0; i < kNumProbs; i++){
|
|
89
89
|
probs.push(kBitModelTotal >>> 1);
|
|
90
90
|
}
|
|
91
91
|
// Track previous byte for probability context
|
|
92
|
-
|
|
92
|
+
let prevByte = 0;
|
|
93
93
|
// Instruction pointer for address conversion
|
|
94
|
-
|
|
94
|
+
let ip = 0;
|
|
95
95
|
while(outPos < outSize && mainPos < mainStream.length){
|
|
96
|
-
|
|
96
|
+
const b = mainStream[mainPos++];
|
|
97
97
|
// Check for branch opcodes
|
|
98
98
|
if (b === 0xe8 || b === 0xe9) {
|
|
99
99
|
// CALL (0xE8) or JMP (0xE9)
|
|
100
100
|
// Use range decoder to check if this should be processed
|
|
101
101
|
// Probability index: E8 uses 2 + prevByte, E9 uses 1
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
const probIndex = b === 0xe8 ? 2 + prevByte : 1;
|
|
103
|
+
const isMatch = decodeBit(rd, probs, probIndex);
|
|
104
104
|
if (outPos >= outSize) break;
|
|
105
105
|
output[outPos++] = b;
|
|
106
106
|
ip++;
|
|
107
107
|
if (isMatch) {
|
|
108
108
|
// Read 4-byte address from appropriate stream
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
const addrStream = b === 0xe8 ? callStream : jumpStream;
|
|
110
|
+
const addrPos = b === 0xe8 ? callPos : jumpPos;
|
|
111
111
|
if (addrPos + 4 > addrStream.length) {
|
|
112
112
|
break;
|
|
113
113
|
}
|
|
114
114
|
// Check if we have room for 4 address bytes
|
|
115
115
|
if (outPos + 4 > outSize) break;
|
|
116
116
|
// Read as big-endian (BCJ2 stores addresses big-endian)
|
|
117
|
-
|
|
117
|
+
let addr = addrStream[addrPos] << 24 | addrStream[addrPos + 1] << 16 | addrStream[addrPos + 2] << 8 | addrStream[addrPos + 3];
|
|
118
118
|
if (b === 0xe8) {
|
|
119
119
|
callPos += 4;
|
|
120
120
|
} else {
|
|
@@ -137,13 +137,13 @@ var kNumProbs = 258;
|
|
|
137
137
|
if (outPos >= outSize) break;
|
|
138
138
|
output[outPos++] = b;
|
|
139
139
|
ip++;
|
|
140
|
-
|
|
140
|
+
const b2 = mainStream[mainPos];
|
|
141
141
|
if ((b2 & 0xf0) === 0x80) {
|
|
142
142
|
// Conditional jump
|
|
143
143
|
mainPos++;
|
|
144
144
|
// Probability index 0 for conditional jumps (since b2 != 0xE8 and b2 != 0xE9)
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
const probIndex2 = 0;
|
|
146
|
+
const isMatch2 = decodeBit(rd, probs, probIndex2);
|
|
147
147
|
if (outPos >= outSize) break;
|
|
148
148
|
output[outPos++] = b2;
|
|
149
149
|
ip++;
|
|
@@ -154,7 +154,7 @@ var kNumProbs = 258;
|
|
|
154
154
|
}
|
|
155
155
|
// Check if we have room for 4 address bytes
|
|
156
156
|
if (outPos + 4 > outSize) break;
|
|
157
|
-
|
|
157
|
+
let addr2 = jumpStream[jumpPos] << 24 | jumpStream[jumpPos + 1] << 16 | jumpStream[jumpPos + 2] << 8 | jumpStream[jumpPos + 3];
|
|
158
158
|
jumpPos += 4;
|
|
159
159
|
// Convert absolute to relative
|
|
160
160
|
addr2 = addr2 - (ip + 4) | 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Bcj2.ts"],"sourcesContent":["// BCJ2 (x86-64) filter codec - advanced branch/call/jump converter\n// BCJ2 uses 4 input streams and arithmetic (range) coding for better compression\n// Reference: LZMA SDK Bcj2.c\n//\n// Stream layout:\n// Stream 0: Main data (contains literals and branch opcode markers)\n// Stream 1: CALL addresses (for 0xE8 instructions)\n// Stream 2: JUMP addresses (for 0xE9 instructions)\n// Stream 3: Range coder data (probability decisions)\n\nimport { allocBuffer } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// Range coder constants\nvar kTopValue = 1 << 24;\nvar kNumBitModelTotalBits = 11;\nvar kBitModelTotal = 1 << kNumBitModelTotalBits;\nvar kNumMoveBits = 5;\n\n// Number of probability models:\n// Index 0: conditional jumps (0x0F 0x80-0x8F)\n// Index 1: JMP (0xE9)\n// Indices 2-257: CALL (0xE8), indexed by previous byte\nvar kNumProbs = 258;\n\n/**\n * Range decoder state\n */\ninterface RangeDecoder {\n range: number;\n code: number;\n stream: Buffer;\n pos: number;\n}\n\n/**\n * Initialize range decoder\n */\nfunction initRangeDecoder(stream: Buffer): RangeDecoder {\n var rd: RangeDecoder = {\n range: 0xffffffff,\n code: 0,\n stream: stream,\n pos: 0,\n };\n\n // Initialize code from first 5 bytes\n for (var i = 0; i < 5; i++) {\n rd.code = (rd.code << 8) | (rd.pos < stream.length ? stream[rd.pos++] : 0);\n }\n\n return rd;\n}\n\n/**\n * Decode a single bit using probability model\n */\nfunction decodeBit(rd: RangeDecoder, prob: number[], probIndex: number): number {\n var ttt = prob[probIndex];\n var bound = (rd.range >>> kNumBitModelTotalBits) * ttt;\n\n var symbol: number;\n if (rd.code >>> 0 < bound >>> 0) {\n rd.range = bound;\n prob[probIndex] = (ttt + ((kBitModelTotal - ttt) >>> kNumMoveBits)) | 0;\n symbol = 0;\n } else {\n rd.range = (rd.range - bound) >>> 0;\n rd.code = (rd.code - bound) >>> 0;\n prob[probIndex] = (ttt - (ttt >>> kNumMoveBits)) | 0;\n symbol = 1;\n }\n\n // Normalize\n if (rd.range < kTopValue) {\n rd.range = (rd.range << 8) >>> 0;\n rd.code = ((rd.code << 8) | (rd.pos < rd.stream.length ? rd.stream[rd.pos++] : 0)) >>> 0;\n }\n\n return symbol;\n}\n\n/**\n * BCJ2 multi-stream decoder\n * Takes 4 pre-decompressed streams and combines them\n */\nexport function decodeBcj2Multi(streams: Buffer[], _properties?: Buffer, unpackSize?: number): Buffer {\n if (streams.length !== 4) {\n throw new Error(`BCJ2 requires 4 input streams, got ${streams.length}`);\n }\n\n // Stream assignment (based on 7z bind pair convention):\n // streams[0] = main data (after LZMA2)\n // streams[1] = call stream (after LZMA)\n // streams[2] = jump stream (after LZMA)\n // streams[3] = range coder stream (uncompressed)\n var mainStream = streams[0];\n var callStream = streams[1];\n var jumpStream = streams[2];\n var rcStream = streams[3];\n\n // Output buffer\n var outSize = unpackSize || mainStream.length + callStream.length + jumpStream.length;\n var output = allocBuffer(outSize);\n var outPos = 0;\n\n // Stream positions\n var mainPos = 0;\n var callPos = 0;\n var jumpPos = 0;\n\n // Initialize range decoder\n var rd = initRangeDecoder(rcStream);\n\n // Initialize probability models\n var probs: number[] = [];\n for (var i = 0; i < kNumProbs; i++) {\n probs.push(kBitModelTotal >>> 1);\n }\n\n // Track previous byte for probability context\n var prevByte = 0;\n\n // Instruction pointer for address conversion\n var ip = 0;\n\n while (outPos < outSize && mainPos < mainStream.length) {\n var b = mainStream[mainPos++];\n\n // Check for branch opcodes\n if (b === 0xe8 || b === 0xe9) {\n // CALL (0xE8) or JMP (0xE9)\n // Use range decoder to check if this should be processed\n // Probability index: E8 uses 2 + prevByte, E9 uses 1\n var probIndex = b === 0xe8 ? 2 + prevByte : 1;\n var isMatch = decodeBit(rd, probs, probIndex);\n\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n\n if (isMatch) {\n // Read 4-byte address from appropriate stream\n var addrStream = b === 0xe8 ? callStream : jumpStream;\n var addrPos = b === 0xe8 ? callPos : jumpPos;\n\n if (addrPos + 4 > addrStream.length) {\n // Not enough data, copy remaining\n break;\n }\n\n // Check if we have room for 4 address bytes\n if (outPos + 4 > outSize) break;\n\n // Read as big-endian (BCJ2 stores addresses big-endian)\n var addr = (addrStream[addrPos] << 24) | (addrStream[addrPos + 1] << 16) | (addrStream[addrPos + 2] << 8) | addrStream[addrPos + 3];\n\n if (b === 0xe8) {\n callPos += 4;\n } else {\n jumpPos += 4;\n }\n\n // Convert absolute to relative address\n addr = (addr - (ip + 4)) | 0;\n\n // Write as little-endian\n output[outPos++] = addr & 0xff;\n output[outPos++] = (addr >>> 8) & 0xff;\n output[outPos++] = (addr >>> 16) & 0xff;\n output[outPos++] = (addr >>> 24) & 0xff;\n ip += 4;\n\n prevByte = (addr >>> 24) & 0xff;\n } else {\n prevByte = b;\n }\n } else if (b === 0x0f && mainPos < mainStream.length) {\n // Potential conditional jump (0x0F 0x8x)\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n\n var b2 = mainStream[mainPos];\n if ((b2 & 0xf0) === 0x80) {\n // Conditional jump\n mainPos++;\n // Probability index 0 for conditional jumps (since b2 != 0xE8 and b2 != 0xE9)\n var probIndex2 = 0;\n var isMatch2 = decodeBit(rd, probs, probIndex2);\n\n if (outPos >= outSize) break;\n output[outPos++] = b2;\n ip++;\n\n if (isMatch2) {\n // Read 4-byte address from jump stream\n if (jumpPos + 4 > jumpStream.length) {\n break;\n }\n\n // Check if we have room for 4 address bytes\n if (outPos + 4 > outSize) break;\n\n var addr2 = (jumpStream[jumpPos] << 24) | (jumpStream[jumpPos + 1] << 16) | (jumpStream[jumpPos + 2] << 8) | jumpStream[jumpPos + 3];\n jumpPos += 4;\n\n // Convert absolute to relative\n addr2 = (addr2 - (ip + 4)) | 0;\n\n // Write as little-endian\n output[outPos++] = addr2 & 0xff;\n output[outPos++] = (addr2 >>> 8) & 0xff;\n output[outPos++] = (addr2 >>> 16) & 0xff;\n output[outPos++] = (addr2 >>> 24) & 0xff;\n ip += 4;\n\n prevByte = (addr2 >>> 24) & 0xff;\n } else {\n prevByte = b2;\n }\n } else {\n prevByte = b;\n }\n } else {\n // Regular byte\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n prevByte = b;\n }\n }\n\n // Return only the used portion\n return outPos < output.length ? output.slice(0, outPos) : output;\n}\n\n/**\n * Single-buffer decode (for API compatibility)\n * Note: BCJ2 requires multi-stream, this throws\n */\nexport function decodeBcj2(_input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n throw new Error('BCJ2 requires multi-stream decoding - use decodeBcj2Multi');\n}\n\n/**\n * Create a BCJ2 decoder Transform stream\n * Note: BCJ2 requires multi-stream, this is for API compatibility\n */\nexport function createBcj2Decoder(_properties?: Buffer, _unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcj2, _properties, _unpackSize);\n}\n"],"names":["allocBuffer","createBufferingDecoder","kTopValue","kNumBitModelTotalBits","kBitModelTotal","kNumMoveBits","kNumProbs","initRangeDecoder","stream","rd","range","code","pos","i","length","decodeBit","prob","probIndex","ttt","bound","symbol","decodeBcj2Multi","streams","_properties","unpackSize","Error","mainStream","callStream","jumpStream","rcStream","outSize","output","outPos","mainPos","callPos","jumpPos","probs","push","prevByte","ip","b","isMatch","addrStream","addrPos","addr","b2","probIndex2","isMatch2","addr2","slice","decodeBcj2","_input","_unpackSize","createBcj2Decoder"],"mappings":"AAAA,mEAAmE;AACnE,iFAAiF;AACjF,6BAA6B;AAC7B,EAAE;AACF,iBAAiB;AACjB,sEAAsE;AACtE,qDAAqD;AACrD,qDAAqD;AACrD,uDAAuD;AAEvD,SAASA,WAAW,QAAQ,wBAAwB;AAEpD,OAAOC,4BAA4B,8BAA8B;AAEjE,wBAAwB;AACxB,IAAIC,YAAY,KAAK;AACrB,IAAIC,wBAAwB;AAC5B,IAAIC,iBAAiB,KAAKD;AAC1B,IAAIE,eAAe;AAEnB,gCAAgC;AAChC,8CAA8C;AAC9C,sBAAsB;AACtB,uDAAuD;AACvD,IAAIC,YAAY;AAYhB;;CAEC,GACD,SAASC,iBAAiBC,MAAc;IACtC,IAAIC,KAAmB;QACrBC,OAAO;QACPC,MAAM;QACNH,QAAQA;QACRI,KAAK;IACP;IAEA,qCAAqC;IACrC,IAAK,IAAIC,IAAI,GAAGA,IAAI,GAAGA,IAAK;QAC1BJ,GAAGE,IAAI,GAAG,AAACF,GAAGE,IAAI,IAAI,IAAMF,CAAAA,GAAGG,GAAG,GAAGJ,OAAOM,MAAM,GAAGN,MAAM,CAACC,GAAGG,GAAG,GAAG,GAAG,CAAA;IAC1E;IAEA,OAAOH;AACT;AAEA;;CA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AAU,CAACO,UAAU,EAAE;oBACpIA,WAAW;oBAEX,+BAA+B;oBAC/Ba,QAAQ,AAACA,QAAST,CAAAA,KAAK,CAAA,IAAM;oBAE7B,yBAAyB;oBACzBR,MAAM,CAACC,SAAS,GAAGgB,QAAQ;oBAC3BjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,IAAK;oBACnCjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,KAAM;oBACpCjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,KAAM;oBACpCT,MAAM;oBAEND,WAAW,AAACU,UAAU,KAAM;gBAC9B,OAAO;oBACLV,WAAWO;gBACb;YACF,OAAO;gBACLP,WAAWE;YACb;QACF,OAAO;YACL,eAAe;YACf,IAAIR,UAAUF,SAAS;YACvBC,MAAM,CAACC,SAAS,GAAGQ;YACnBD;YACAD,WAAWE;QACb;IACF;IAEA,+BAA+B;IAC/B,OAAOR,SAASD,OAAOjB,MAAM,GAAGiB,OAAOkB,KAAK,CAAC,GAAGjB,UAAUD;AAC5D;AAEA;;;CAGC,GACD,OAAO,SAASmB,WAAWC,MAAc,EAAE5B,WAAoB,EAAE6B,WAAoB;IACnF,MAAM,IAAI3B,MAAM;AAClB;AAEA;;;CAGC,GACD,OAAO,SAAS4B,kBAAkB9B,WAAoB,EAAE6B,WAAoB;IAC1E,OAAOnD,uBAAuBiD,YAAY3B,aAAa6B;AACzD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/Bcj2.ts"],"sourcesContent":["// BCJ2 (x86-64) filter codec - advanced branch/call/jump converter\n// BCJ2 uses 4 input streams and arithmetic (range) coding for better compression\n// Reference: LZMA SDK Bcj2.c\n//\n// Stream layout:\n// Stream 0: Main data (contains literals and branch opcode markers)\n// Stream 1: CALL addresses (for 0xE8 instructions)\n// Stream 2: JUMP addresses (for 0xE9 instructions)\n// Stream 3: Range coder data (probability decisions)\n\nimport { allocBuffer } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// Range coder constants\nconst kTopValue = 1 << 24;\nconst kNumBitModelTotalBits = 11;\nconst kBitModelTotal = 1 << kNumBitModelTotalBits;\nconst kNumMoveBits = 5;\n\n// Number of probability models:\n// Index 0: conditional jumps (0x0F 0x80-0x8F)\n// Index 1: JMP (0xE9)\n// Indices 2-257: CALL (0xE8), indexed by previous byte\nconst kNumProbs = 258;\n\n/**\n * Range decoder state\n */\ninterface RangeDecoder {\n range: number;\n code: number;\n stream: Buffer;\n pos: number;\n}\n\n/**\n * Initialize range decoder\n */\nfunction initRangeDecoder(stream: Buffer): RangeDecoder {\n const rd: RangeDecoder = {\n range: 0xffffffff,\n code: 0,\n stream: stream,\n pos: 0,\n };\n\n // Initialize code from first 5 bytes\n for (let i = 0; i < 5; i++) {\n rd.code = (rd.code << 8) | (rd.pos < stream.length ? stream[rd.pos++] : 0);\n }\n\n return rd;\n}\n\n/**\n * Decode a single bit using probability model\n */\nfunction decodeBit(rd: RangeDecoder, prob: number[], probIndex: number): number {\n const ttt = prob[probIndex];\n const bound = (rd.range >>> kNumBitModelTotalBits) * ttt;\n\n let symbol: number;\n if (rd.code >>> 0 < bound >>> 0) {\n rd.range = bound;\n prob[probIndex] = (ttt + ((kBitModelTotal - ttt) >>> kNumMoveBits)) | 0;\n symbol = 0;\n } else {\n rd.range = (rd.range - bound) >>> 0;\n rd.code = (rd.code - bound) >>> 0;\n prob[probIndex] = (ttt - (ttt >>> kNumMoveBits)) | 0;\n symbol = 1;\n }\n\n // Normalize\n if (rd.range < kTopValue) {\n rd.range = (rd.range << 8) >>> 0;\n rd.code = ((rd.code << 8) | (rd.pos < rd.stream.length ? rd.stream[rd.pos++] : 0)) >>> 0;\n }\n\n return symbol;\n}\n\n/**\n * BCJ2 multi-stream decoder\n * Takes 4 pre-decompressed streams and combines them\n */\nexport function decodeBcj2Multi(streams: Buffer[], _properties?: Buffer, unpackSize?: number): Buffer {\n if (streams.length !== 4) {\n throw new Error(`BCJ2 requires 4 input streams, got ${streams.length}`);\n }\n\n // Stream assignment (based on 7z bind pair convention):\n // streams[0] = main data (after LZMA2)\n // streams[1] = call stream (after LZMA)\n // streams[2] = jump stream (after LZMA)\n // streams[3] = range coder stream (uncompressed)\n const mainStream = streams[0];\n const callStream = streams[1];\n const jumpStream = streams[2];\n const rcStream = streams[3];\n\n // Output buffer\n const outSize = unpackSize || mainStream.length + callStream.length + jumpStream.length;\n const output = allocBuffer(outSize);\n let outPos = 0;\n\n // Stream positions\n let mainPos = 0;\n let callPos = 0;\n let jumpPos = 0;\n\n // Initialize range decoder\n const rd = initRangeDecoder(rcStream);\n\n // Initialize probability models\n const probs: number[] = [];\n for (let i = 0; i < kNumProbs; i++) {\n probs.push(kBitModelTotal >>> 1);\n }\n\n // Track previous byte for probability context\n let prevByte = 0;\n\n // Instruction pointer for address conversion\n let ip = 0;\n\n while (outPos < outSize && mainPos < mainStream.length) {\n const b = mainStream[mainPos++];\n\n // Check for branch opcodes\n if (b === 0xe8 || b === 0xe9) {\n // CALL (0xE8) or JMP (0xE9)\n // Use range decoder to check if this should be processed\n // Probability index: E8 uses 2 + prevByte, E9 uses 1\n const probIndex = b === 0xe8 ? 2 + prevByte : 1;\n const isMatch = decodeBit(rd, probs, probIndex);\n\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n\n if (isMatch) {\n // Read 4-byte address from appropriate stream\n const addrStream = b === 0xe8 ? callStream : jumpStream;\n const addrPos = b === 0xe8 ? callPos : jumpPos;\n\n if (addrPos + 4 > addrStream.length) {\n // Not enough data, copy remaining\n break;\n }\n\n // Check if we have room for 4 address bytes\n if (outPos + 4 > outSize) break;\n\n // Read as big-endian (BCJ2 stores addresses big-endian)\n let addr = (addrStream[addrPos] << 24) | (addrStream[addrPos + 1] << 16) | (addrStream[addrPos + 2] << 8) | addrStream[addrPos + 3];\n\n if (b === 0xe8) {\n callPos += 4;\n } else {\n jumpPos += 4;\n }\n\n // Convert absolute to relative address\n addr = (addr - (ip + 4)) | 0;\n\n // Write as little-endian\n output[outPos++] = addr & 0xff;\n output[outPos++] = (addr >>> 8) & 0xff;\n output[outPos++] = (addr >>> 16) & 0xff;\n output[outPos++] = (addr >>> 24) & 0xff;\n ip += 4;\n\n prevByte = (addr >>> 24) & 0xff;\n } else {\n prevByte = b;\n }\n } else if (b === 0x0f && mainPos < mainStream.length) {\n // Potential conditional jump (0x0F 0x8x)\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n\n const b2 = mainStream[mainPos];\n if ((b2 & 0xf0) === 0x80) {\n // Conditional jump\n mainPos++;\n // Probability index 0 for conditional jumps (since b2 != 0xE8 and b2 != 0xE9)\n const probIndex2 = 0;\n const isMatch2 = decodeBit(rd, probs, probIndex2);\n\n if (outPos >= outSize) break;\n output[outPos++] = b2;\n ip++;\n\n if (isMatch2) {\n // Read 4-byte address from jump stream\n if (jumpPos + 4 > jumpStream.length) {\n break;\n }\n\n // Check if we have room for 4 address bytes\n if (outPos + 4 > outSize) break;\n\n let addr2 = (jumpStream[jumpPos] << 24) | (jumpStream[jumpPos + 1] << 16) | (jumpStream[jumpPos + 2] << 8) | jumpStream[jumpPos + 3];\n jumpPos += 4;\n\n // Convert absolute to relative\n addr2 = (addr2 - (ip + 4)) | 0;\n\n // Write as little-endian\n output[outPos++] = addr2 & 0xff;\n output[outPos++] = (addr2 >>> 8) & 0xff;\n output[outPos++] = (addr2 >>> 16) & 0xff;\n output[outPos++] = (addr2 >>> 24) & 0xff;\n ip += 4;\n\n prevByte = (addr2 >>> 24) & 0xff;\n } else {\n prevByte = b2;\n }\n } else {\n prevByte = b;\n }\n } else {\n // Regular byte\n if (outPos >= outSize) break;\n output[outPos++] = b;\n ip++;\n prevByte = b;\n }\n }\n\n // Return only the used portion\n return outPos < output.length ? output.slice(0, outPos) : output;\n}\n\n/**\n * Single-buffer decode (for API compatibility)\n * Note: BCJ2 requires multi-stream, this throws\n */\nexport function decodeBcj2(_input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n throw new Error('BCJ2 requires multi-stream decoding - use decodeBcj2Multi');\n}\n\n/**\n * Create a BCJ2 decoder Transform stream\n * Note: BCJ2 requires multi-stream, this is for API compatibility\n */\nexport function createBcj2Decoder(_properties?: Buffer, _unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcj2, _properties, _unpackSize);\n}\n"],"names":["allocBuffer","createBufferingDecoder","kTopValue","kNumBitModelTotalBits","kBitModelTotal","kNumMoveBits","kNumProbs","initRangeDecoder","stream","rd","range","code","pos","i","length","decodeBit","prob","probIndex","ttt","bound","symbol","decodeBcj2Multi","streams","_properties","unpackSize","Error","mainStream","callStream","jumpStream","rcStream","outSize","output","outPos","mainPos","callPos","jumpPos","probs","push","prevByte","ip","b","isMatch","addrStream","addrPos","addr","b2","probIndex2","isMatch2","addr2","slice","decodeBcj2","_input","_unpackSize","createBcj2Decoder"],"mappings":"AAAA,mEAAmE;AACnE,iFAAiF;AACjF,6BAA6B;AAC7B,EAAE;AACF,iBAAiB;AACjB,sEAAsE;AACtE,qDAAqD;AACrD,qDAAqD;AACrD,uDAAuD;AAEvD,SAASA,WAAW,QAAQ,wBAAwB;AAEpD,OAAOC,4BAA4B,8BAA8B;AAEjE,wBAAwB;AACxB,MAAMC,YAAY,KAAK;AACvB,MAAMC,wBAAwB;AAC9B,MAAMC,iBAAiB,KAAKD;AAC5B,MAAME,eAAe;AAErB,gCAAgC;AAChC,8CAA8C;AAC9C,sBAAsB;AACtB,uDAAuD;AACvD,MAAMC,YAAY;AAYlB;;CAEC,GACD,SAASC,iBAAiBC,MAAc;IACtC,MAAMC,KAAmB;QACvBC,OAAO;QACPC,MAAM;QACNH,QAAQA;QACRI,KAAK;IACP;IAEA,qCAAqC;IACrC,IAAK,IAAIC,IAAI,GAAGA,IAAI,GAAGA,IAAK;QAC1BJ,GAAGE,IAAI,GAAG,AAACF,GAAGE,IAAI,IAAI,IAAMF,CAA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B,QAAQ,AAACpB,UAAU,CAACO,QAAQ,IAAI,KAAOP,UAAU,CAACO,UAAU,EAAE,IAAI,KAAOP,UAAU,CAACO,UAAU,EAAE,IAAI,IAAKP,UAAU,CAACO,UAAU,EAAE;oBACpIA,WAAW;oBAEX,+BAA+B;oBAC/Ba,QAAQ,AAACA,QAAST,CAAAA,KAAK,CAAA,IAAM;oBAE7B,yBAAyB;oBACzBR,MAAM,CAACC,SAAS,GAAGgB,QAAQ;oBAC3BjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,IAAK;oBACnCjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,KAAM;oBACpCjB,MAAM,CAACC,SAAS,GAAG,AAACgB,UAAU,KAAM;oBACpCT,MAAM;oBAEND,WAAW,AAACU,UAAU,KAAM;gBAC9B,OAAO;oBACLV,WAAWO;gBACb;YACF,OAAO;gBACLP,WAAWE;YACb;QACF,OAAO;YACL,eAAe;YACf,IAAIR,UAAUF,SAAS;YACvBC,MAAM,CAACC,SAAS,GAAGQ;YACnBD;YACAD,WAAWE;QACb;IACF;IAEA,+BAA+B;IAC/B,OAAOR,SAASD,OAAOjB,MAAM,GAAGiB,OAAOkB,KAAK,CAAC,GAAGjB,UAAUD;AAC5D;AAEA;;;CAGC,GACD,OAAO,SAASmB,WAAWC,MAAc,EAAE5B,WAAoB,EAAE6B,WAAoB;IACnF,MAAM,IAAI3B,MAAM;AAClB;AAEA;;;CAGC,GACD,OAAO,SAAS4B,kBAAkB9B,WAAoB,EAAE6B,WAAoB;IAC1E,OAAOnD,uBAAuBiD,YAAY3B,aAAa6B;AACzD"}
|
|
@@ -21,14 +21,14 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
21
21
|
* @param _unpackSize - Unused for ARM BCJ
|
|
22
22
|
* @returns Unfiltered data
|
|
23
23
|
*/ export function decodeBcjArm(input, _properties, _unpackSize) {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const output = bufferFrom(input); // Copy since we modify in place
|
|
25
|
+
let pos = 0;
|
|
26
26
|
// Process 4-byte aligned positions
|
|
27
27
|
while(pos + 4 <= output.length){
|
|
28
28
|
// Check for BL instruction: byte 3 is 0xEB
|
|
29
29
|
if (output[pos + 3] === 0xeb) {
|
|
30
30
|
// Read 24-bit address (little-endian in bytes 0-2)
|
|
31
|
-
|
|
31
|
+
let addr = output[pos] | output[pos + 1] << 8 | output[pos + 2] << 16;
|
|
32
32
|
// Sign-extend 24-bit to 32-bit
|
|
33
33
|
if (addr & 0x800000) {
|
|
34
34
|
addr |= 0xff000000;
|
|
@@ -36,7 +36,7 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
36
36
|
// Convert absolute to relative:
|
|
37
37
|
// Subtract current position (in words, so divide by 4)
|
|
38
38
|
// ARM PC is 2 words (8 bytes) ahead during execution
|
|
39
|
-
|
|
39
|
+
const relAddr = addr - (pos >>> 2);
|
|
40
40
|
// Write back lower 24 bits
|
|
41
41
|
output[pos] = relAddr & 0xff;
|
|
42
42
|
output[pos + 1] = relAddr >>> 8 & 0xff;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArm.ts"],"sourcesContent":["// BCJ (ARM 32-bit) filter codec - converts ARM branch instruction addresses\n// This filter makes ARM executables more compressible by LZMA\n//\n// ARM branch instructions (BL) use relative addressing. The filter converts\n// these to absolute addresses during compression, and back during decompression.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM BL instruction format:\n * - 4 bytes aligned\n * - Byte pattern: XX XX XX EB (where EB = 0xEB opcode for BL)\n * - Lower 24 bits are signed offset (in words, not bytes)\n *\n * @param input - ARM BCJ filtered data\n * @param _properties - Unused for ARM BCJ\n * @param _unpackSize - Unused for ARM BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArm.ts"],"sourcesContent":["// BCJ (ARM 32-bit) filter codec - converts ARM branch instruction addresses\n// This filter makes ARM executables more compressible by LZMA\n//\n// ARM branch instructions (BL) use relative addressing. The filter converts\n// these to absolute addresses during compression, and back during decompression.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM BL instruction format:\n * - 4 bytes aligned\n * - Byte pattern: XX XX XX EB (where EB = 0xEB opcode for BL)\n * - Lower 24 bits are signed offset (in words, not bytes)\n *\n * @param input - ARM BCJ filtered data\n * @param _properties - Unused for ARM BCJ\n * @param _unpackSize - Unused for ARM BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n // Check for BL instruction: byte 3 is 0xEB\n if (output[pos + 3] === 0xeb) {\n // Read 24-bit address (little-endian in bytes 0-2)\n let addr = output[pos] | (output[pos + 1] << 8) | (output[pos + 2] << 16);\n\n // Sign-extend 24-bit to 32-bit\n if (addr & 0x800000) {\n addr |= 0xff000000;\n }\n\n // Convert absolute to relative:\n // Subtract current position (in words, so divide by 4)\n // ARM PC is 2 words (8 bytes) ahead during execution\n const relAddr = addr - (pos >>> 2);\n\n // Write back lower 24 bits\n output[pos] = relAddr & 0xff;\n output[pos + 1] = (relAddr >>> 8) & 0xff;\n output[pos + 2] = (relAddr >>> 16) & 0xff;\n }\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create an ARM BCJ decoder Transform stream\n */\nexport function createBcjArmDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjArm, properties, unpackSize);\n}\n"],"names":["bufferFrom","createBufferingDecoder","decodeBcjArm","input","_properties","_unpackSize","output","pos","length","addr","relAddr","createBcjArmDecoder","properties","unpackSize"],"mappings":"AAAA,4EAA4E;AAC5E,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,iFAAiF;AACjF,EAAE;AACF,+DAA+D;AAE/D,SAASA,UAAU,QAAQ,wBAAwB;AAEnD,OAAOC,4BAA4B,8BAA8B;AAEjE;;;;;;;;;;;;;CAaC,GACD,OAAO,SAASC,aAAaC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACpF,MAAMC,SAASN,WAAWG,QAAQ,gCAAgC;IAClE,IAAII,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKD,OAAOE,MAAM,CAAE;QAC/B,2CAA2C;QAC3C,IAAIF,MAAM,CAACC,MAAM,EAAE,KAAK,MAAM;YAC5B,mDAAmD;YACnD,IAAIE,OAAOH,MAAM,CAACC,IAAI,GAAID,MAAM,CAACC,MAAM,EAAE,IAAI,IAAMD,MAAM,CAACC,MAAM,EAAE,IAAI;YAEtE,+BAA+B;YAC/B,IAAIE,OAAO,UAAU;gBACnBA,QAAQ;YACV;YAEA,gCAAgC;YAChC,uDAAuD;YACvD,qDAAqD;YACrD,MAAMC,UAAUD,OAAQF,CAAAA,QAAQ,CAAA;YAEhC,2BAA2B;YAC3BD,MAAM,CAACC,IAAI,GAAGG,UAAU;YACxBJ,MAAM,CAACC,MAAM,EAAE,GAAG,AAACG,YAAY,IAAK;YACpCJ,MAAM,CAACC,MAAM,EAAE,GAAG,AAACG,YAAY,KAAM;QACvC;QACAH,OAAO;IACT;IAEA,OAAOD;AACT;AAEA;;CAEC,GACD,OAAO,SAASK,oBAAoBC,UAAmB,EAAEC,UAAmB;IAC1E,OAAOZ,uBAAuBC,cAAcU,YAAYC;AAC1D"}
|
|
@@ -21,23 +21,23 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
21
21
|
* @param _unpackSize - Unused for ARM64 BCJ
|
|
22
22
|
* @returns Unfiltered data
|
|
23
23
|
*/ export function decodeBcjArm64(input, _properties, _unpackSize) {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const output = bufferFrom(input); // Copy since we modify in place
|
|
25
|
+
let pos = 0;
|
|
26
26
|
// Process 4-byte aligned positions
|
|
27
27
|
while(pos + 4 <= output.length){
|
|
28
28
|
// Read 32-bit value (little-endian)
|
|
29
|
-
|
|
29
|
+
let instr = output[pos] | output[pos + 1] << 8 | output[pos + 2] << 16 | output[pos + 3] << 24 >>> 0;
|
|
30
30
|
// Check for B/BL instruction: (instr & 0x7C000000) === 0x14000000
|
|
31
31
|
// This matches both B (0x14000000) and BL (0x94000000)
|
|
32
32
|
if ((instr & 0x7c000000) === 0x14000000) {
|
|
33
33
|
// Extract 26-bit offset
|
|
34
|
-
|
|
34
|
+
let addr = instr & 0x03ffffff;
|
|
35
35
|
// Sign-extend 26-bit to 32-bit
|
|
36
36
|
if (addr & 0x02000000) {
|
|
37
37
|
addr |= 0xfc000000;
|
|
38
38
|
}
|
|
39
39
|
// Convert absolute to relative: subtract current position (in words)
|
|
40
|
-
|
|
40
|
+
const relAddr = addr - (pos >>> 2);
|
|
41
41
|
// Clear old offset and write new one, preserve opcode
|
|
42
42
|
instr = instr & 0xfc000000 | relAddr & 0x03ffffff;
|
|
43
43
|
// Write back (little-endian)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArm64.ts"],"sourcesContent":["// BCJ (ARM64/AArch64) filter codec - converts ARM64 branch instruction addresses\n// This filter makes ARM64 executables more compressible by LZMA\n//\n// ARM64 uses 32-bit fixed-width instructions. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM64 BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM64 B/BL instruction format (little-endian):\n * - 4 bytes aligned\n * - B: opcode 0x14 (000101xx)\n * - BL: opcode 0x94 (100101xx)\n * - Bits 0-25 are 26-bit signed offset (in words)\n *\n * @param input - ARM64 BCJ filtered data\n * @param _properties - Unused for ARM64 BCJ\n * @param _unpackSize - Unused for ARM64 BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm64(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArm64.ts"],"sourcesContent":["// BCJ (ARM64/AArch64) filter codec - converts ARM64 branch instruction addresses\n// This filter makes ARM64 executables more compressible by LZMA\n//\n// ARM64 uses 32-bit fixed-width instructions. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM64 BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM64 B/BL instruction format (little-endian):\n * - 4 bytes aligned\n * - B: opcode 0x14 (000101xx)\n * - BL: opcode 0x94 (100101xx)\n * - Bits 0-25 are 26-bit signed offset (in words)\n *\n * @param input - ARM64 BCJ filtered data\n * @param _properties - Unused for ARM64 BCJ\n * @param _unpackSize - Unused for ARM64 BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArm64(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read 32-bit value (little-endian)\n let instr = output[pos] | (output[pos + 1] << 8) | (output[pos + 2] << 16) | ((output[pos + 3] << 24) >>> 0);\n\n // Check for B/BL instruction: (instr & 0x7C000000) === 0x14000000\n // This matches both B (0x14000000) and BL (0x94000000)\n if ((instr & 0x7c000000) === 0x14000000) {\n // Extract 26-bit offset\n let addr = instr & 0x03ffffff;\n\n // Sign-extend 26-bit to 32-bit\n if (addr & 0x02000000) {\n addr |= 0xfc000000;\n }\n\n // Convert absolute to relative: subtract current position (in words)\n const relAddr = addr - (pos >>> 2);\n\n // Clear old offset and write new one, preserve opcode\n instr = (instr & 0xfc000000) | (relAddr & 0x03ffffff);\n\n // Write back (little-endian)\n output[pos] = instr & 0xff;\n output[pos + 1] = (instr >>> 8) & 0xff;\n output[pos + 2] = (instr >>> 16) & 0xff;\n output[pos + 3] = (instr >>> 24) & 0xff;\n }\n\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create an ARM64 BCJ decoder Transform stream\n */\nexport function createBcjArm64Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjArm64, properties, unpackSize);\n}\n"],"names":["bufferFrom","createBufferingDecoder","decodeBcjArm64","input","_properties","_unpackSize","output","pos","length","instr","addr","relAddr","createBcjArm64Decoder","properties","unpackSize"],"mappings":"AAAA,iFAAiF;AACjF,gEAAgE;AAChE,EAAE;AACF,6FAA6F;AAC7F,EAAE;AACF,+DAA+D;AAE/D,SAASA,UAAU,QAAQ,wBAAwB;AAEnD,OAAOC,4BAA4B,8BAA8B;AAEjE;;;;;;;;;;;;;;CAcC,GACD,OAAO,SAASC,eAAeC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACtF,MAAMC,SAASN,WAAWG,QAAQ,gCAAgC;IAClE,IAAII,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKD,OAAOE,MAAM,CAAE;QAC/B,oCAAoC;QACpC,IAAIC,QAAQH,MAAM,CAACC,IAAI,GAAID,MAAM,CAACC,MAAM,EAAE,IAAI,IAAMD,MAAM,CAACC,MAAM,EAAE,IAAI,KAAO,AAACD,MAAM,CAACC,MAAM,EAAE,IAAI,OAAQ;QAE1G,kEAAkE;QAClE,uDAAuD;QACvD,IAAI,AAACE,CAAAA,QAAQ,UAAS,MAAO,YAAY;YACvC,wBAAwB;YACxB,IAAIC,OAAOD,QAAQ;YAEnB,+BAA+B;YAC/B,IAAIC,OAAO,YAAY;gBACrBA,QAAQ;YACV;YAEA,qEAAqE;YACrE,MAAMC,UAAUD,OAAQH,CAAAA,QAAQ,CAAA;YAEhC,sDAAsD;YACtDE,QAAQ,AAACA,QAAQ,aAAeE,UAAU;YAE1C,6BAA6B;YAC7BL,MAAM,CAACC,IAAI,GAAGE,QAAQ;YACtBH,MAAM,CAACC,MAAM,EAAE,GAAG,AAACE,UAAU,IAAK;YAClCH,MAAM,CAACC,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;YACnCH,MAAM,CAACC,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;QACrC;QAEAF,OAAO;IACT;IAEA,OAAOD;AACT;AAEA;;CAEC,GACD,OAAO,SAASM,sBAAsBC,UAAmB,EAAEC,UAAmB;IAC5E,OAAOb,uBAAuBC,gBAAgBW,YAAYC;AAC5D"}
|
|
@@ -20,23 +20,23 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
20
20
|
* @param _unpackSize - Unused for ARM Thumb BCJ
|
|
21
21
|
* @returns Unfiltered data
|
|
22
22
|
*/ export function decodeBcjArmt(input, _properties, _unpackSize) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const output = bufferFrom(input); // Copy since we modify in place
|
|
24
|
+
let pos = 0;
|
|
25
25
|
// Process 2-byte aligned positions
|
|
26
26
|
while(pos + 4 <= output.length){
|
|
27
27
|
// Read two 16-bit values (little-endian)
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
const w0 = output[pos] | output[pos + 1] << 8;
|
|
29
|
+
const w1 = output[pos + 2] | output[pos + 3] << 8;
|
|
30
30
|
// Check for BL instruction pair:
|
|
31
31
|
// First word: 0xF000-0xF7FF (1111 0xxx xxxx xxxx)
|
|
32
32
|
// Second word: 0xF800-0xFFFF (1111 1xxx xxxx xxxx)
|
|
33
33
|
if ((w0 & 0xf800) === 0xf000 && (w1 & 0xf800) === 0xf800) {
|
|
34
34
|
// Extract and combine the offset parts
|
|
35
35
|
// High 11 bits from w0, low 11 bits from w1
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
const hi = w0 & 0x7ff;
|
|
37
|
+
const lo = w1 & 0x7ff;
|
|
38
38
|
// Combine into 22-bit offset (in half-words)
|
|
39
|
-
|
|
39
|
+
let addr = hi << 11 | lo;
|
|
40
40
|
// Sign-extend 22-bit to 32-bit
|
|
41
41
|
if (addr & 0x200000) {
|
|
42
42
|
addr |= 0xffc00000;
|
|
@@ -44,10 +44,10 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
44
44
|
// Convert absolute to relative:
|
|
45
45
|
// Subtract current position (in half-words, so divide by 2)
|
|
46
46
|
// Thumb PC is 2 half-words (4 bytes) ahead
|
|
47
|
-
|
|
47
|
+
const relAddr = addr - (pos >>> 1);
|
|
48
48
|
// Write back
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
const newHi = relAddr >>> 11 & 0x7ff;
|
|
50
|
+
const newLo = relAddr & 0x7ff;
|
|
51
51
|
output[pos] = newHi & 0xff;
|
|
52
52
|
output[pos + 1] = 0xf0 | newHi >>> 8 & 0x07;
|
|
53
53
|
output[pos + 2] = newLo & 0xff;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArmt.ts"],"sourcesContent":["// BCJ (ARM Thumb) filter codec - converts ARM Thumb branch instruction addresses\n// This filter makes ARM Thumb executables more compressible by LZMA\n//\n// ARM Thumb uses 16-bit instructions, but BL (branch with link) spans two 16-bit words.\n// The filter converts relative addresses to absolute during compression.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM Thumb BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM Thumb BL instruction format (2 x 16-bit):\n * - First half-word: 1111 0xxx xxxx xxxx (high bits of offset)\n * - Second half-word: 1111 1xxx xxxx xxxx (low bits of offset)\n *\n * @param input - ARM Thumb BCJ filtered data\n * @param _properties - Unused for ARM Thumb BCJ\n * @param _unpackSize - Unused for ARM Thumb BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArmt(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjArmt.ts"],"sourcesContent":["// BCJ (ARM Thumb) filter codec - converts ARM Thumb branch instruction addresses\n// This filter makes ARM Thumb executables more compressible by LZMA\n//\n// ARM Thumb uses 16-bit instructions, but BL (branch with link) spans two 16-bit words.\n// The filter converts relative addresses to absolute during compression.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode ARM Thumb BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * ARM Thumb BL instruction format (2 x 16-bit):\n * - First half-word: 1111 0xxx xxxx xxxx (high bits of offset)\n * - Second half-word: 1111 1xxx xxxx xxxx (low bits of offset)\n *\n * @param input - ARM Thumb BCJ filtered data\n * @param _properties - Unused for ARM Thumb BCJ\n * @param _unpackSize - Unused for ARM Thumb BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjArmt(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 2-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read two 16-bit values (little-endian)\n const w0 = output[pos] | (output[pos + 1] << 8);\n const w1 = output[pos + 2] | (output[pos + 3] << 8);\n\n // Check for BL instruction pair:\n // First word: 0xF000-0xF7FF (1111 0xxx xxxx xxxx)\n // Second word: 0xF800-0xFFFF (1111 1xxx xxxx xxxx)\n if ((w0 & 0xf800) === 0xf000 && (w1 & 0xf800) === 0xf800) {\n // Extract and combine the offset parts\n // High 11 bits from w0, low 11 bits from w1\n const hi = w0 & 0x7ff;\n const lo = w1 & 0x7ff;\n\n // Combine into 22-bit offset (in half-words)\n let addr = (hi << 11) | lo;\n\n // Sign-extend 22-bit to 32-bit\n if (addr & 0x200000) {\n addr |= 0xffc00000;\n }\n\n // Convert absolute to relative:\n // Subtract current position (in half-words, so divide by 2)\n // Thumb PC is 2 half-words (4 bytes) ahead\n const relAddr = addr - (pos >>> 1);\n\n // Write back\n const newHi = (relAddr >>> 11) & 0x7ff;\n const newLo = relAddr & 0x7ff;\n\n output[pos] = newHi & 0xff;\n output[pos + 1] = 0xf0 | ((newHi >>> 8) & 0x07);\n output[pos + 2] = newLo & 0xff;\n output[pos + 3] = 0xf8 | ((newLo >>> 8) & 0x07);\n\n pos += 4;\n } else {\n pos += 2;\n }\n }\n\n return output;\n}\n\n/**\n * Create an ARM Thumb BCJ decoder Transform stream\n */\nexport function createBcjArmtDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjArmt, properties, unpackSize);\n}\n"],"names":["bufferFrom","createBufferingDecoder","decodeBcjArmt","input","_properties","_unpackSize","output","pos","length","w0","w1","hi","lo","addr","relAddr","newHi","newLo","createBcjArmtDecoder","properties","unpack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EAAE,GAAG,OAAQ,AAACS,UAAU,IAAK;YAE1CT,OAAO;QACT,OAAO;YACLA,OAAO;QACT;IACF;IAEA,OAAOD;AACT;AAEA;;CAEC,GACD,OAAO,SAASW,qBAAqBC,UAAmB,EAAEC,UAAmB;IAC3E,OAAOlB,uBAAuBC,eAAegB,YAAYC;AAC3D"}
|
|
@@ -9,7 +9,7 @@ import { bufferFrom } from 'extract-base-iterator';
|
|
|
9
9
|
import createBufferingDecoder from './createBufferingDecoder.js';
|
|
10
10
|
// IA64 branch instruction slot mask
|
|
11
11
|
// Each bundle has a 5-bit template and 3 x 41-bit instruction slots
|
|
12
|
-
|
|
12
|
+
const kBranchTable = [
|
|
13
13
|
0,
|
|
14
14
|
0,
|
|
15
15
|
0,
|
|
@@ -52,59 +52,59 @@ var kBranchTable = [
|
|
|
52
52
|
* @param _unpackSize - Unused for IA64 BCJ
|
|
53
53
|
* @returns Unfiltered data
|
|
54
54
|
*/ export function decodeBcjIa64(input, _properties, _unpackSize) {
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const output = bufferFrom(input); // Copy since we modify in place
|
|
56
|
+
let pos = 0;
|
|
57
57
|
// Process 16-byte aligned bundles
|
|
58
58
|
while(pos + 16 <= output.length){
|
|
59
59
|
// Get template (low 5 bits of first byte)
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
const template = output[pos] & 0x1f;
|
|
61
|
+
const mask = kBranchTable[template];
|
|
62
62
|
// Check each instruction slot (3 slots per bundle)
|
|
63
|
-
for(
|
|
63
|
+
for(let slot = 0; slot < 3; slot++){
|
|
64
64
|
if ((mask & 1 << slot) === 0) {
|
|
65
65
|
continue;
|
|
66
66
|
}
|
|
67
67
|
// Calculate bit position for this slot
|
|
68
68
|
// Slot 0: bits 5-45, Slot 1: bits 46-86, Slot 2: bits 87-127
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
const bitPos = 5 + slot * 41;
|
|
70
|
+
const bytePos = bitPos >>> 3;
|
|
71
|
+
const bitOffset = bitPos & 7;
|
|
72
72
|
// Read 8 bytes to get the instruction (may span bytes)
|
|
73
73
|
// We need at least 6 bytes for a 41-bit instruction
|
|
74
74
|
if (pos + bytePos + 6 > output.length) {
|
|
75
75
|
break;
|
|
76
76
|
}
|
|
77
77
|
// Extract instruction bytes
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
const instr0 = output[pos + bytePos];
|
|
79
|
+
const instr1 = output[pos + bytePos + 1];
|
|
80
|
+
const instr2 = output[pos + bytePos + 2];
|
|
81
|
+
const instr3 = output[pos + bytePos + 3];
|
|
82
|
+
const instr4 = output[pos + bytePos + 4];
|
|
83
|
+
const instr5 = output[pos + bytePos + 5];
|
|
84
84
|
// Build instruction value (we only need the immediate field)
|
|
85
85
|
// The immediate is in bits 13-32 of the instruction (20 bits)
|
|
86
86
|
// Plus bit 36 as the sign bit
|
|
87
87
|
// For decoding, we extract the address that was encoded and convert back
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
let instrLo = instr0 >>> bitOffset | instr1 << 8 - bitOffset | instr2 << 16 - bitOffset | instr3 << 24 - bitOffset;
|
|
89
|
+
let instrHi = instr4 >>> bitOffset | instr5 << 8 - bitOffset;
|
|
90
90
|
// Check opcode for branch (opcode 4 or 5 in bits 37-40)
|
|
91
|
-
|
|
91
|
+
const opcode = instrHi >>> 37 - 32 - bitOffset & 0xf;
|
|
92
92
|
if (opcode !== 4 && opcode !== 5) {
|
|
93
93
|
continue;
|
|
94
94
|
}
|
|
95
95
|
// Extract 21-bit immediate (bits 13-32 + sign bit 36)
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
const imm20 = instrLo >>> 13 & 0xfffff;
|
|
97
|
+
const sign = instrHi >>> 36 - 32 & 1;
|
|
98
98
|
// Combine into 21-bit signed value
|
|
99
|
-
|
|
99
|
+
let addr = imm20 | sign << 20;
|
|
100
100
|
if (sign) {
|
|
101
101
|
addr |= 0xffe00000; // Sign-extend
|
|
102
102
|
}
|
|
103
103
|
// Convert absolute to relative: subtract current position (in bundles)
|
|
104
|
-
|
|
104
|
+
const relAddr = addr - (pos >>> 4);
|
|
105
105
|
// Write back
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
const newImm20 = relAddr & 0xfffff;
|
|
107
|
+
const newSign = relAddr >>> 20 & 1;
|
|
108
108
|
// Clear old immediate and write new one
|
|
109
109
|
instrLo = instrLo & ~(0xfffff << 13) | newImm20 << 13;
|
|
110
110
|
instrHi = instrHi & ~(1 << 36 - 32) | newSign << 36 - 32;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjIa64.ts"],"sourcesContent":["// BCJ (IA64/Itanium) filter codec - converts IA64 branch instruction addresses\n// This filter makes IA64 executables more compressible by LZMA\n//\n// IA64 uses 128-bit instruction bundles with 3 instructions per bundle.\n// Branch instructions use 21-bit signed offsets (in bundles).\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// IA64 branch instruction slot mask\n// Each bundle has a 5-bit template and 3 x 41-bit instruction slots\
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjIa64.ts"],"sourcesContent":["// BCJ (IA64/Itanium) filter codec - converts IA64 branch instruction addresses\n// This filter makes IA64 executables more compressible by LZMA\n//\n// IA64 uses 128-bit instruction bundles with 3 instructions per bundle.\n// Branch instructions use 21-bit signed offsets (in bundles).\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n// IA64 branch instruction slot mask\n// Each bundle has a 5-bit template and 3 x 41-bit instruction slots\nconst kBranchTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0, 7, 7, 4, 4, 0, 0, 4, 4, 0, 0];\n\n/**\n * Decode IA64 BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * @param input - IA64 BCJ filtered data\n * @param _properties - Unused for IA64 BCJ\n * @param _unpackSize - Unused for IA64 BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjIa64(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 16-byte aligned bundles\n while (pos + 16 <= output.length) {\n // Get template (low 5 bits of first byte)\n const template = output[pos] & 0x1f;\n const mask = kBranchTable[template];\n\n // Check each instruction slot (3 slots per bundle)\n for (let slot = 0; slot < 3; slot++) {\n if ((mask & (1 << slot)) === 0) {\n continue;\n }\n\n // Calculate bit position for this slot\n // Slot 0: bits 5-45, Slot 1: bits 46-86, Slot 2: bits 87-127\n const bitPos = 5 + slot * 41;\n const bytePos = bitPos >>> 3;\n const bitOffset = bitPos & 7;\n\n // Read 8 bytes to get the instruction (may span bytes)\n // We need at least 6 bytes for a 41-bit instruction\n if (pos + bytePos + 6 > output.length) {\n break;\n }\n\n // Extract instruction bytes\n const instr0 = output[pos + bytePos];\n const instr1 = output[pos + bytePos + 1];\n const instr2 = output[pos + bytePos + 2];\n const instr3 = output[pos + bytePos + 3];\n const instr4 = output[pos + bytePos + 4];\n const instr5 = output[pos + bytePos + 5];\n\n // Build instruction value (we only need the immediate field)\n // The immediate is in bits 13-32 of the instruction (20 bits)\n // Plus bit 36 as the sign bit\n\n // For decoding, we extract the address that was encoded and convert back\n let instrLo = (instr0 >>> bitOffset) | (instr1 << (8 - bitOffset)) | (instr2 << (16 - bitOffset)) | (instr3 << (24 - bitOffset));\n\n let instrHi = (instr4 >>> bitOffset) | (instr5 << (8 - bitOffset));\n\n // Check opcode for branch (opcode 4 or 5 in bits 37-40)\n const opcode = (instrHi >>> (37 - 32 - bitOffset)) & 0xf;\n if (opcode !== 4 && opcode !== 5) {\n continue;\n }\n\n // Extract 21-bit immediate (bits 13-32 + sign bit 36)\n const imm20 = (instrLo >>> 13) & 0xfffff;\n const sign = (instrHi >>> (36 - 32)) & 1;\n\n // Combine into 21-bit signed value\n let addr = imm20 | (sign << 20);\n if (sign) {\n addr |= 0xffe00000; // Sign-extend\n }\n\n // Convert absolute to relative: subtract current position (in bundles)\n const relAddr = addr - (pos >>> 4);\n\n // Write back\n const newImm20 = relAddr & 0xfffff;\n const newSign = (relAddr >>> 20) & 1;\n\n // Clear old immediate and write new one\n instrLo = (instrLo & ~(0xfffff << 13)) | (newImm20 << 13);\n instrHi = (instrHi & ~(1 << (36 - 32))) | (newSign << (36 - 32));\n\n // Write back bytes\n output[pos + bytePos] = (output[pos + bytePos] & ((1 << bitOffset) - 1)) | ((instrLo & 0xff) << bitOffset);\n output[pos + bytePos + 1] = (instrLo >>> (8 - bitOffset)) & 0xff;\n output[pos + bytePos + 2] = (instrLo >>> (16 - bitOffset)) & 0xff;\n output[pos + bytePos + 3] = (instrLo >>> (24 - bitOffset)) & 0xff;\n output[pos + bytePos + 4] = ((instrLo >>> (32 - bitOffset)) & ((1 << bitOffset) - 1)) | ((instrHi & 0xff) << bitOffset);\n output[pos + bytePos + 5] = (output[pos + bytePos + 5] & ~((1 << bitOffset) - 1)) | ((instrHi >>> (8 - bitOffset)) & ((1 << bitOffset) - 1));\n }\n\n pos += 16;\n }\n\n return output;\n}\n\n/**\n * Create an IA64 BCJ decoder Transform stream\n */\nexport function createBcjIa64Decoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjIa64, properties, unpackSize);\n}\n"],"names":["bufferFrom","createBufferingDecoder","kBranchTable","decodeBcjIa64","input","_properties","_unpackSize","output","pos","length","template","mask","slot","bitPos","bytePos","bitOffset","instr0","instr1","instr2","instr3","instr4","instr5","instrLo","instrHi","opcode","imm20","sign","addr","relAddr","newImm20","newSign","createBcjIa64Decoder","properties","unpackSize"],"mappings":"AAAA,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,wEAAwE;AACxE,8DAA8D;AAC9D,EAAE;AACF,+DAA+D;AAE/D,SAASA,UAAU,QAAQ,wBAAwB;AAEnD,OAAOC,4BAA4B,8BAA8B;AAEjE,oCAAoC;AACpC,oEAAoE;AACpE,MAAMC,eAAe;IAAC;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;IAAG;CAAE;AAErH;;;;;;;;CAQC,GACD,OAAO,SAASC,cAAcC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACrF,MAAMC,SAASP,WAAWI,QAAQ,gCAAgC;IAClE,IAAII,MAAM;IAEV,kCAAkC;IAClC,MAAOA,MAAM,MAAMD,OAAOE,MAAM,CAAE;QAChC,0CAA0C;QAC1C,MAAMC,WAAWH,MAAM,CAACC,IAAI,GAAG;QAC/B,MAAMG,OAAOT,YAAY,CAACQ,SAAS;QAEnC,mDAAmD;QACnD,IAAK,IAAIE,OAAO,GAAGA,OAAO,GAAGA,OAAQ;YACnC,IAAI,AAACD,CAAAA,OAAQ,KAAKC,IAAI,MAAO,GAAG;gBAC9B;YACF;YAEA,uCAAuC;YACvC,6DAA6D;YAC7D,MAAMC,SAAS,IAAID,OAAO;YAC1B,MAAME,UAAUD,WAAW;YAC3B,MAAME,YAAYF,SAAS;YAE3B,uDAAuD;YACvD,oDAAoD;YACpD,IAAIL,MAAMM,UAAU,IAAIP,OAAOE,MAAM,EAAE;gBACrC;YACF;YAEA,4BAA4B;YAC5B,MAAMO,SAAST,MAAM,CAACC,MAAMM,QAAQ;YACpC,MAAM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GR,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,AAACQ,YAAa,IAAIP,YAAc;YAC5DR,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,AAACQ,YAAa,KAAKP,YAAc;YAC7DR,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,AAACQ,YAAa,KAAKP,YAAc;YAC7DR,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,AAAEQ,YAAa,KAAKP,YAAe,AAAC,CAAA,KAAKA,SAAQ,IAAK,IAAO,AAACQ,CAAAA,UAAU,IAAG,KAAMR;YAC7GR,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,AAACP,MAAM,CAACC,MAAMM,UAAU,EAAE,GAAG,CAAE,CAAA,AAAC,CAAA,KAAKC,SAAQ,IAAK,CAAA,IAAO,AAACQ,YAAa,IAAIR,YAAe,AAAC,CAAA,KAAKA,SAAQ,IAAK;QAC3I;QAEAP,OAAO;IACT;IAEA,OAAOD;AACT;AAEA;;CAEC,GACD,OAAO,SAASwB,qBAAqBC,UAAmB,EAAEC,UAAmB;IAC3E,OAAOhC,uBAAuBE,eAAe6B,YAAYC;AAC3D"}
|
|
@@ -20,22 +20,22 @@ import createBufferingDecoder from './createBufferingDecoder.js';
|
|
|
20
20
|
* @param _unpackSize - Unused for PowerPC BCJ
|
|
21
21
|
* @returns Unfiltered data
|
|
22
22
|
*/ export function decodeBcjPpc(input, _properties, _unpackSize) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const output = bufferFrom(input); // Copy since we modify in place
|
|
24
|
+
let pos = 0;
|
|
25
25
|
// Process 4-byte aligned positions
|
|
26
26
|
while(pos + 4 <= output.length){
|
|
27
27
|
// Read 32-bit value (big-endian)
|
|
28
|
-
|
|
28
|
+
let instr = output[pos] << 24 | output[pos + 1] << 16 | output[pos + 2] << 8 | output[pos + 3];
|
|
29
29
|
// Check for B/BL instruction: (instr & 0xFC000003) === 0x48000001
|
|
30
30
|
if ((instr & 0xfc000003) === 0x48000001) {
|
|
31
31
|
// Extract 26-bit offset (bits 2-27, the LI field)
|
|
32
|
-
|
|
32
|
+
let addr = instr & 0x03fffffc;
|
|
33
33
|
// Sign-extend 26-bit to 32-bit
|
|
34
34
|
if (addr & 0x02000000) {
|
|
35
35
|
addr |= 0xfc000000;
|
|
36
36
|
}
|
|
37
37
|
// Convert absolute to relative: subtract current position
|
|
38
|
-
|
|
38
|
+
const relAddr = addr - pos;
|
|
39
39
|
// Clear old offset and write new one
|
|
40
40
|
instr = instr & 0xfc000003 | relAddr & 0x03fffffc;
|
|
41
41
|
// Write back (big-endian)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjPpc.ts"],"sourcesContent":["// BCJ (PowerPC) filter codec - converts PowerPC branch instruction addresses\n// This filter makes PowerPC executables more compressible by LZMA\n//\n// PowerPC is big-endian. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode PowerPC BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * PowerPC B/BL instruction format (big-endian):\n * - 4 bytes aligned\n * - Opcode 0x48 in high byte with AA=0, LK=1 (0x48000001 mask 0xFC000003)\n * - Bits 6-29 are 24-bit signed offset (in words)\n *\n * @param input - PowerPC BCJ filtered data\n * @param _properties - Unused for PowerPC BCJ\n * @param _unpackSize - Unused for PowerPC BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjPpc(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/7z-iterator/src/sevenz/codecs/BcjPpc.ts"],"sourcesContent":["// BCJ (PowerPC) filter codec - converts PowerPC branch instruction addresses\n// This filter makes PowerPC executables more compressible by LZMA\n//\n// PowerPC is big-endian. Branch instructions use 26-bit signed offsets.\n//\n// Reference: https://github.com/kornelski/7z/blob/main/C/Bra.c\n\nimport { bufferFrom } from 'extract-base-iterator';\nimport type { Transform } from 'readable-stream';\nimport createBufferingDecoder from './createBufferingDecoder.ts';\n\n/**\n * Decode PowerPC BCJ filtered data\n * Reverses the BCJ transformation by converting absolute addresses back to relative\n *\n * PowerPC B/BL instruction format (big-endian):\n * - 4 bytes aligned\n * - Opcode 0x48 in high byte with AA=0, LK=1 (0x48000001 mask 0xFC000003)\n * - Bits 6-29 are 24-bit signed offset (in words)\n *\n * @param input - PowerPC BCJ filtered data\n * @param _properties - Unused for PowerPC BCJ\n * @param _unpackSize - Unused for PowerPC BCJ\n * @returns Unfiltered data\n */\nexport function decodeBcjPpc(input: Buffer, _properties?: Buffer, _unpackSize?: number): Buffer {\n const output = bufferFrom(input); // Copy since we modify in place\n let pos = 0;\n\n // Process 4-byte aligned positions\n while (pos + 4 <= output.length) {\n // Read 32-bit value (big-endian)\n let instr = (output[pos] << 24) | (output[pos + 1] << 16) | (output[pos + 2] << 8) | output[pos + 3];\n\n // Check for B/BL instruction: (instr & 0xFC000003) === 0x48000001\n if ((instr & 0xfc000003) === 0x48000001) {\n // Extract 26-bit offset (bits 2-27, the LI field)\n let addr = instr & 0x03fffffc;\n\n // Sign-extend 26-bit to 32-bit\n if (addr & 0x02000000) {\n addr |= 0xfc000000;\n }\n\n // Convert absolute to relative: subtract current position\n const relAddr = addr - pos;\n\n // Clear old offset and write new one\n instr = (instr & 0xfc000003) | (relAddr & 0x03fffffc);\n\n // Write back (big-endian)\n output[pos] = (instr >>> 24) & 0xff;\n output[pos + 1] = (instr >>> 16) & 0xff;\n output[pos + 2] = (instr >>> 8) & 0xff;\n output[pos + 3] = instr & 0xff;\n }\n pos += 4;\n }\n\n return output;\n}\n\n/**\n * Create a PowerPC BCJ decoder Transform stream\n */\nexport function createBcjPpcDecoder(properties?: Buffer, unpackSize?: number): Transform {\n return createBufferingDecoder(decodeBcjPpc, properties, unpackSize);\n}\n"],"names":["bufferFrom","createBufferingDecoder","decodeBcjPpc","input","_properties","_unpackSize","output","pos","length","instr","addr","relAddr","createBcjPpcDecoder","properties","unpackSize"],"mappings":"AAAA,6EAA6E;AAC7E,kEAAkE;AAClE,EAAE;AACF,wEAAwE;AACxE,EAAE;AACF,+DAA+D;AAE/D,SAASA,UAAU,QAAQ,wBAAwB;AAEnD,OAAOC,4BAA4B,8BAA8B;AAEjE;;;;;;;;;;;;;CAaC,GACD,OAAO,SAASC,aAAaC,KAAa,EAAEC,WAAoB,EAAEC,WAAoB;IACpF,MAAMC,SAASN,WAAWG,QAAQ,gCAAgC;IAClE,IAAII,MAAM;IAEV,mCAAmC;IACnC,MAAOA,MAAM,KAAKD,OAAOE,MAAM,CAAE;QAC/B,iCAAiC;QACjC,IAAIC,QAAQ,AAACH,MAAM,CAACC,IAAI,IAAI,KAAOD,MAAM,CAACC,MAAM,EAAE,IAAI,KAAOD,MAAM,CAACC,MAAM,EAAE,IAAI,IAAKD,MAAM,CAACC,MAAM,EAAE;QAEpG,kEAAkE;QAClE,IAAI,AAACE,CAAAA,QAAQ,UAAS,MAAO,YAAY;YACvC,kDAAkD;YAClD,IAAIC,OAAOD,QAAQ;YAEnB,+BAA+B;YAC/B,IAAIC,OAAO,YAAY;gBACrBA,QAAQ;YACV;YAEA,0DAA0D;YAC1D,MAAMC,UAAUD,OAAOH;YAEvB,qCAAqC;YACrCE,QAAQ,AAACA,QAAQ,aAAeE,UAAU;YAE1C,0BAA0B;YAC1BL,MAAM,CAACC,IAAI,GAAG,AAACE,UAAU,KAAM;YAC/BH,MAAM,CAACC,MAAM,EAAE,GAAG,AAACE,UAAU,KAAM;YACnCH,MAAM,CAACC,MAAM,EAAE,GAAG,AAACE,UAAU,IAAK;YAClCH,MAAM,CAACC,MAAM,EAAE,GAAGE,QAAQ;QAC5B;QACAF,OAAO;IACT;IAEA,OAAOD;A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