swf2js 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.
- package/LICENSE-GPL +680 -0
- package/README.md +208 -2
- package/package.json +24 -4
- package/src/cli.js +272 -0
- package/src/decoder/button-decoder.js +136 -0
- package/src/decoder/shape-decoder.js +786 -0
- package/src/decoder/symbol-table.js +36 -0
- package/src/decoder/tag-decoder.js +1190 -0
- package/src/decoder/tag-types.js +258 -0
- package/src/emitter/asset-encoder.js +708 -0
- package/src/emitter/bundle-emitter.js +596 -0
- package/src/emitter/embedded-swf.js +246 -0
- package/src/emitter/font-encoder.js +365 -0
- package/src/emitter/timeline-data-codec.js +58 -0
- package/src/model/swf-document.js +59 -0
- package/src/model/transpile-result.js +32 -0
- package/src/parser/bit-reader.js +237 -0
- package/src/parser/decompressor.js +91 -0
- package/src/parser/swf-parser.js +164 -0
- package/src/pretty-printer/pretty-printer.js +137 -0
- package/src/runtime/avm1-runtime.js +4907 -0
- package/src/runtime/avm2-runtime.js +1591 -0
- package/src/runtime/embedded-movie-runtime.js +208 -0
- package/src/runtime/index.js +17 -0
- package/src/runtime/movie-runtime.js +306 -0
- package/src/runtime/swflib.js +14050 -0
- package/src/transpiler/actionscript.js +390 -0
- package/src/transpiler/avm1/avm1-ir.js +205 -0
- package/src/transpiler/avm1/avm1-opcodes.js +149 -0
- package/src/transpiler/avm1/avm1-rewrite.js +291 -0
- package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
- package/src/transpiler/avm2/abc-reader.js +416 -0
- package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
- package/src/transpiler/cfg-structurer.js +876 -0
- package/src/transpiler/timeline/display-list.js +643 -0
- package/src/transpiler/timeline/timeline-codegen.js +6354 -0
- package/src/util/diagnostics.js +50 -0
- package/src/util/logger.js +40 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core SWF document model — shared typedefs used across all pipeline stages.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Rect
|
|
7
|
+
* @property {number} xMin - in twips (1/20 px)
|
|
8
|
+
* @property {number} xMax - in twips
|
|
9
|
+
* @property {number} yMin - in twips
|
|
10
|
+
* @property {number} yMax - in twips
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} Matrix
|
|
15
|
+
* @property {number} a
|
|
16
|
+
* @property {number} b
|
|
17
|
+
* @property {number} c
|
|
18
|
+
* @property {number} d
|
|
19
|
+
* @property {number} tx
|
|
20
|
+
* @property {number} ty
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {Object} ColorTransform
|
|
25
|
+
* @property {number} rMult
|
|
26
|
+
* @property {number} gMult
|
|
27
|
+
* @property {number} bMult
|
|
28
|
+
* @property {number} aMult
|
|
29
|
+
* @property {number} rAdd
|
|
30
|
+
* @property {number} gAdd
|
|
31
|
+
* @property {number} bAdd
|
|
32
|
+
* @property {number} aAdd
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {Object} SwfHeader
|
|
37
|
+
* @property {"FWS"|"CWS"|"ZWS"} signature
|
|
38
|
+
* @property {number} version
|
|
39
|
+
* @property {number} fileLength
|
|
40
|
+
* @property {Rect} frameSize
|
|
41
|
+
* @property {number} frameRate - frames per second (8.8 fixed-point decoded)
|
|
42
|
+
* @property {number} frameCount
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @typedef {Object} RawTag
|
|
47
|
+
* @property {number} code
|
|
48
|
+
* @property {number} length
|
|
49
|
+
* @property {Uint8Array} data
|
|
50
|
+
* @property {number} offset - byte offset in decompressed body
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @typedef {Object} SwfDocument
|
|
55
|
+
* @property {SwfHeader} header
|
|
56
|
+
* @property {RawTag[]} tags
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Transpiler output model — the result passed from Transpile stage to Emit stage.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Diagnostic
|
|
7
|
+
* @property {"warning"|"error"} level
|
|
8
|
+
* @property {"parse"|"decode"|"transpile"|"emit"} stage
|
|
9
|
+
* @property {string} message
|
|
10
|
+
* @property {Record<string, unknown>} [context]
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} AssetEntry
|
|
15
|
+
* @property {number} id - SWF character ID
|
|
16
|
+
* @property {"bitmap"|"audio"} type
|
|
17
|
+
* @property {string} mimeType
|
|
18
|
+
* @property {Uint8Array} data
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {Object} TranspileResult
|
|
23
|
+
* @property {string[]} jsFragments - ordered JS code blocks to concatenate into bundle.js
|
|
24
|
+
* @property {AssetEntry[]} assets - binary assets (bitmaps, audio)
|
|
25
|
+
* @property {Diagnostic[]} diagnostics
|
|
26
|
+
* @property {string|null} backgroundColor - CSS hex color from SetBackgroundColor tag
|
|
27
|
+
* @property {number} frameRate - frames per second
|
|
28
|
+
* @property {Rect} stageSize - stage dimensions in twips
|
|
29
|
+
* @property {boolean} usesWebGL - true when bitmap-heavy heuristic triggered
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview BitReader — bit-level and byte-level reading over a Uint8Array.
|
|
3
|
+
* Handles the SWF format's variable-width bit fields (RECT, Matrix, etc.)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const utf8Decoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
7
|
+
|
|
8
|
+
export class BitReader {
|
|
9
|
+
/**
|
|
10
|
+
* @param {Uint8Array} bytes
|
|
11
|
+
*/
|
|
12
|
+
constructor(bytes) {
|
|
13
|
+
this._bytes = bytes;
|
|
14
|
+
this._bytePos = 0;
|
|
15
|
+
this._bitBuf = 0; // current byte being read bit-by-bit
|
|
16
|
+
this._bitsLeft = 0; // how many bits remain in _bitBuf
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @returns {number} current byte position */
|
|
20
|
+
get bytePos() { return this._bytePos; }
|
|
21
|
+
|
|
22
|
+
/** @returns {number} total byte length */
|
|
23
|
+
get length() { return this._bytes.length; }
|
|
24
|
+
|
|
25
|
+
/** @returns {boolean} */
|
|
26
|
+
get eof() { return this._bytePos >= this._bytes.length && this._bitsLeft === 0; }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Align to the next byte boundary (discard remaining bits in current byte).
|
|
30
|
+
*/
|
|
31
|
+
align() {
|
|
32
|
+
this._bitBuf = 0;
|
|
33
|
+
this._bitsLeft = 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read n unsigned bits (UB[n]).
|
|
38
|
+
* @param {number} n
|
|
39
|
+
* @returns {number}
|
|
40
|
+
*/
|
|
41
|
+
readUB(n) {
|
|
42
|
+
let result = 0;
|
|
43
|
+
for (let i = 0; i < n; i++) {
|
|
44
|
+
if (this._bitsLeft === 0) {
|
|
45
|
+
if (this._bytePos >= this._bytes.length) throw new Error('BitReader: unexpected end of data');
|
|
46
|
+
this._bitBuf = this._bytes[this._bytePos++];
|
|
47
|
+
this._bitsLeft = 8;
|
|
48
|
+
}
|
|
49
|
+
result = (result << 1) | ((this._bitBuf >> (this._bitsLeft - 1)) & 1);
|
|
50
|
+
this._bitsLeft--;
|
|
51
|
+
}
|
|
52
|
+
return result >>> 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read n signed bits (SB[n]).
|
|
57
|
+
* @param {number} n
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
readSB(n) {
|
|
61
|
+
const val = this.readUB(n);
|
|
62
|
+
// sign-extend
|
|
63
|
+
if (n > 0 && (val & (1 << (n - 1)))) {
|
|
64
|
+
return val - (1 << n);
|
|
65
|
+
}
|
|
66
|
+
return val;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read n signed fixed-point 16.16 bits (FB[n]).
|
|
71
|
+
* @param {number} n
|
|
72
|
+
* @returns {number}
|
|
73
|
+
*/
|
|
74
|
+
readFB(n) {
|
|
75
|
+
return this.readSB(n) / 65536;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Read 1 unsigned byte (UI8).
|
|
80
|
+
* @returns {number}
|
|
81
|
+
*/
|
|
82
|
+
readUI8() {
|
|
83
|
+
this.align();
|
|
84
|
+
if (this._bytePos >= this._bytes.length) throw new Error('BitReader: unexpected end of data');
|
|
85
|
+
return this._bytes[this._bytePos++];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read 2 bytes little-endian unsigned (UI16).
|
|
90
|
+
* @returns {number}
|
|
91
|
+
*/
|
|
92
|
+
readUI16() {
|
|
93
|
+
this.align();
|
|
94
|
+
if (this._bytePos + 2 > this._bytes.length) throw new Error('BitReader: unexpected end of data');
|
|
95
|
+
const val = this._bytes[this._bytePos] | (this._bytes[this._bytePos + 1] << 8);
|
|
96
|
+
this._bytePos += 2;
|
|
97
|
+
return val >>> 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Read 2 bytes little-endian signed (SI16).
|
|
102
|
+
* @returns {number}
|
|
103
|
+
*/
|
|
104
|
+
readSI16() {
|
|
105
|
+
const val = this.readUI16();
|
|
106
|
+
return val >= 0x8000 ? val - 0x10000 : val;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Read 4 bytes little-endian unsigned (UI32).
|
|
111
|
+
* @returns {number}
|
|
112
|
+
*/
|
|
113
|
+
readUI32() {
|
|
114
|
+
this.align();
|
|
115
|
+
if (this._bytePos + 4 > this._bytes.length) throw new Error('BitReader: unexpected end of data');
|
|
116
|
+
const val = (this._bytes[this._bytePos]
|
|
117
|
+
| (this._bytes[this._bytePos + 1] << 8)
|
|
118
|
+
| (this._bytes[this._bytePos + 2] << 16)
|
|
119
|
+
| (this._bytes[this._bytePos + 3] << 24)) >>> 0;
|
|
120
|
+
this._bytePos += 4;
|
|
121
|
+
return val;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Read n raw bytes and return them as a new Uint8Array.
|
|
126
|
+
* @param {number} n
|
|
127
|
+
* @returns {Uint8Array}
|
|
128
|
+
*/
|
|
129
|
+
readBytes(n) {
|
|
130
|
+
this.align();
|
|
131
|
+
if (this._bytePos + n > this._bytes.length) throw new Error('BitReader: unexpected end of data');
|
|
132
|
+
const slice = this._bytes.slice(this._bytePos, this._bytePos + n);
|
|
133
|
+
this._bytePos += n;
|
|
134
|
+
return slice;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Read a null-terminated string (STRING).
|
|
139
|
+
* @param {boolean} [utf8=false] - Decode as UTF-8 for SWF 6 or later.
|
|
140
|
+
* @returns {string}
|
|
141
|
+
*/
|
|
142
|
+
readString(utf8 = false) {
|
|
143
|
+
this.align();
|
|
144
|
+
if (utf8) {
|
|
145
|
+
const start = this._bytePos;
|
|
146
|
+
const terminator = this._bytes.indexOf(0, start);
|
|
147
|
+
const end = terminator < 0 ? this._bytes.length : terminator;
|
|
148
|
+
this._bytePos = terminator < 0 ? end : end + 1;
|
|
149
|
+
return utf8Decoder.decode(this._bytes.subarray(start, end));
|
|
150
|
+
}
|
|
151
|
+
let str = '';
|
|
152
|
+
while (this._bytePos < this._bytes.length) {
|
|
153
|
+
const ch = this._bytes[this._bytePos++];
|
|
154
|
+
if (ch === 0) break;
|
|
155
|
+
str += String.fromCharCode(ch);
|
|
156
|
+
}
|
|
157
|
+
return str;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Seek to an absolute byte position (clears bit buffer).
|
|
162
|
+
* @param {number} pos
|
|
163
|
+
*/
|
|
164
|
+
seek(pos) {
|
|
165
|
+
this._bytePos = pos;
|
|
166
|
+
this._bitBuf = 0;
|
|
167
|
+
this._bitsLeft = 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Read a SWF RECT structure.
|
|
172
|
+
* @returns {import('../model/swf-document.js').Rect}
|
|
173
|
+
*/
|
|
174
|
+
readRect() {
|
|
175
|
+
const nbits = this.readUB(5);
|
|
176
|
+
return {
|
|
177
|
+
xMin: this.readSB(nbits),
|
|
178
|
+
xMax: this.readSB(nbits),
|
|
179
|
+
yMin: this.readSB(nbits),
|
|
180
|
+
yMax: this.readSB(nbits),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Read a SWF MATRIX structure.
|
|
186
|
+
* @returns {import('../model/swf-document.js').Matrix}
|
|
187
|
+
*/
|
|
188
|
+
readMatrix() {
|
|
189
|
+
let a = 1, d = 1, b = 0, c = 0, tx = 0, ty = 0;
|
|
190
|
+
|
|
191
|
+
const hasScale = this.readUB(1);
|
|
192
|
+
if (hasScale) {
|
|
193
|
+
const nbits = this.readUB(5);
|
|
194
|
+
a = this.readFB(nbits);
|
|
195
|
+
d = this.readFB(nbits);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const hasRotate = this.readUB(1);
|
|
199
|
+
if (hasRotate) {
|
|
200
|
+
const nbits = this.readUB(5);
|
|
201
|
+
b = this.readFB(nbits);
|
|
202
|
+
c = this.readFB(nbits);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const nTranslateBits = this.readUB(5);
|
|
206
|
+
tx = this.readSB(nTranslateBits);
|
|
207
|
+
ty = this.readSB(nTranslateBits);
|
|
208
|
+
|
|
209
|
+
this.align();
|
|
210
|
+
return { a, b, c, d, tx, ty };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Read a SWF RGBA color (4 bytes).
|
|
215
|
+
* @returns {{ r: number, g: number, b: number, a: number }}
|
|
216
|
+
*/
|
|
217
|
+
readRGBA() {
|
|
218
|
+
return {
|
|
219
|
+
r: this.readUI8(),
|
|
220
|
+
g: this.readUI8(),
|
|
221
|
+
b: this.readUI8(),
|
|
222
|
+
a: this.readUI8(),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Read a SWF RGB color (3 bytes).
|
|
228
|
+
* @returns {{ r: number, g: number, b: number }}
|
|
229
|
+
*/
|
|
230
|
+
readRGB() {
|
|
231
|
+
return {
|
|
232
|
+
r: this.readUI8(),
|
|
233
|
+
g: this.readUI8(),
|
|
234
|
+
b: this.readUI8(),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Decompressor — handles CWS (zlib) and ZWS (LZMA) SWF bodies.
|
|
3
|
+
* Uses Node's built-in zlib for CWS; lzma-js (pure JS) for ZWS.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { inflateSync } from 'zlib';
|
|
7
|
+
import { createRequire } from 'module';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Decompress a CWS (zlib) SWF body.
|
|
11
|
+
* The first 8 bytes of the SWF are uncompressed header; only bytes 8+ are compressed.
|
|
12
|
+
* @param {Uint8Array} fullBytes - the entire SWF file bytes
|
|
13
|
+
* @returns {Uint8Array} decompressed body (bytes 8 onwards, uncompressed)
|
|
14
|
+
*/
|
|
15
|
+
export function decompressCWS(fullBytes) {
|
|
16
|
+
const compressed = fullBytes.slice(8);
|
|
17
|
+
const decompressed = inflateSync(Buffer.from(compressed));
|
|
18
|
+
return new Uint8Array(decompressed);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Decompress a ZWS (LZMA) SWF body.
|
|
23
|
+
* @param {Uint8Array} fullBytes - the entire SWF file bytes
|
|
24
|
+
* @param {number} fileLength - uncompressed file length from SWF header
|
|
25
|
+
* @returns {Promise<Uint8Array>} decompressed body (bytes 8 onwards)
|
|
26
|
+
*/
|
|
27
|
+
export async function decompressZWS(fullBytes, fileLength) {
|
|
28
|
+
// Dynamically load lzma-js via createRequire (it's a legacy CJS-style global script)
|
|
29
|
+
let LZMA;
|
|
30
|
+
try {
|
|
31
|
+
const require = createRequire(import.meta.url);
|
|
32
|
+
const lzmaModule = require('lzma-js/src/lzma.js');
|
|
33
|
+
// lzma-js attaches to a global LZMA var — access it via the module's scope
|
|
34
|
+
// The file uses a global LZMA object; we need to execute it in a scope that captures it
|
|
35
|
+
LZMA = lzmaModule;
|
|
36
|
+
if (!LZMA || typeof LZMA.decompressFile !== 'function') {
|
|
37
|
+
// Fallback: execute the file and read the global
|
|
38
|
+
const { readFileSync } = await import('fs');
|
|
39
|
+
const src = readFileSync(require.resolve('lzma-js/src/lzma.js'), 'utf8');
|
|
40
|
+
const fn = new Function('return (function(){ var LZMA = {}; ' + src.replace('var LZMA = LZMA || {};', '') + '; return LZMA; })()');
|
|
41
|
+
LZMA = fn();
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
'ZWS (LZMA) compressed SWF detected but lzma-js is not installed.\n' +
|
|
46
|
+
'Run: npm install lzma-js\n' + err.message
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ZWS layout after the 8-byte SWF header:
|
|
51
|
+
// 4 bytes: compressed body length (UI32 LE)
|
|
52
|
+
// 5 bytes: LZMA properties (1 byte props + 4 bytes dict size)
|
|
53
|
+
// rest: raw LZMA compressed stream (no uncompressed-size field)
|
|
54
|
+
//
|
|
55
|
+
// lzma-js decodeHeader expects a 13-byte header:
|
|
56
|
+
// 1 byte props + 4 bytes dict size + 8 bytes uncompressed size
|
|
57
|
+
// We build that by appending the known uncompressed size (from SWF fileLength).
|
|
58
|
+
|
|
59
|
+
const uncompressedSize = fileLength - 8; // SWF header is 8 bytes
|
|
60
|
+
|
|
61
|
+
// Extract the 5-byte LZMA properties from bytes 12-16
|
|
62
|
+
const lzmaProps = fullBytes.slice(12, 17);
|
|
63
|
+
// Compressed stream starts at byte 17
|
|
64
|
+
const compressedStream = fullBytes.slice(17);
|
|
65
|
+
|
|
66
|
+
// Build the 13-byte lzma-js header
|
|
67
|
+
const header = new Uint8Array(13);
|
|
68
|
+
header.set(lzmaProps, 0); // 5 bytes: props + dict size
|
|
69
|
+
// 8-byte uncompressed size, little-endian
|
|
70
|
+
header[5] = uncompressedSize & 0xFF;
|
|
71
|
+
header[6] = (uncompressedSize >> 8) & 0xFF;
|
|
72
|
+
header[7] = (uncompressedSize >> 16) & 0xFF;
|
|
73
|
+
header[8] = (uncompressedSize >> 24) & 0xFF;
|
|
74
|
+
header[9] = 0; header[10] = 0; header[11] = 0; header[12] = 0;
|
|
75
|
+
|
|
76
|
+
// Concatenate header + compressed stream
|
|
77
|
+
const lzmaData = new Uint8Array(header.length + compressedStream.length);
|
|
78
|
+
lzmaData.set(header, 0);
|
|
79
|
+
lzmaData.set(compressedStream, header.length);
|
|
80
|
+
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
try {
|
|
83
|
+
const inStream = { data: Array.from(lzmaData), offset: 0, readByte() { return this.data[this.offset++]; } };
|
|
84
|
+
const outStream = { data: [], writeByte(b) { this.data.push(b); } };
|
|
85
|
+
LZMA.decompressFile(inStream, outStream);
|
|
86
|
+
resolve(new Uint8Array(outStream.data));
|
|
87
|
+
} catch (err) {
|
|
88
|
+
reject(new Error('LZMA decompression failed: ' + err.message));
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview SWF_Parser — reads SWF binary bytes and produces a SwfDocument.
|
|
3
|
+
* Handles FWS (uncompressed), CWS (zlib), and ZWS (LZMA) compression.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BitReader } from './bit-reader.js';
|
|
7
|
+
import { decompressCWS, decompressZWS } from './decompressor.js';
|
|
8
|
+
import { DiagnosticsCollector } from '../util/diagnostics.js';
|
|
9
|
+
|
|
10
|
+
/** @import { SwfDocument, SwfHeader, RawTag } from '../model/swf-document.js' */
|
|
11
|
+
|
|
12
|
+
const SWF_MIN_VERSION = 1;
|
|
13
|
+
const SWF_MAX_VERSION = 45;
|
|
14
|
+
const TAG_END = 0;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parse a SWF binary and return a SwfDocument.
|
|
18
|
+
* Never throws — errors are recorded in the returned diagnostics.
|
|
19
|
+
*
|
|
20
|
+
* @param {Uint8Array} bytes
|
|
21
|
+
* @param {DiagnosticsCollector} [diagnostics]
|
|
22
|
+
* @returns {Promise<{ doc: SwfDocument, diagnostics: DiagnosticsCollector }>}
|
|
23
|
+
*/
|
|
24
|
+
export async function parse(bytes, diagnostics) {
|
|
25
|
+
const diag = diagnostics ?? new DiagnosticsCollector();
|
|
26
|
+
|
|
27
|
+
if (bytes.length < 8) {
|
|
28
|
+
diag.error('SWF file too short to contain a valid header', 'parse', { byteLength: bytes.length });
|
|
29
|
+
return { doc: emptyDoc(), diagnostics: diag };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// --- Detect signature ---
|
|
33
|
+
const sig = String.fromCharCode(bytes[0], bytes[1], bytes[2]);
|
|
34
|
+
|
|
35
|
+
if (sig !== 'FWS' && sig !== 'CWS' && sig !== 'ZWS') {
|
|
36
|
+
diag.error(`Unknown SWF signature: ${JSON.stringify(sig)}`, 'parse', { sig });
|
|
37
|
+
return { doc: emptyDoc(), diagnostics: diag };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const version = bytes[3];
|
|
41
|
+
// fileLength is UI32 LE at bytes 4-7 (total uncompressed file length)
|
|
42
|
+
const fileLength = bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24);
|
|
43
|
+
|
|
44
|
+
if (version < SWF_MIN_VERSION || version > SWF_MAX_VERSION) {
|
|
45
|
+
diag.warn(`SWF version ${version} is outside the expected range (${SWF_MIN_VERSION}–${SWF_MAX_VERSION})`, 'parse', { version });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// --- Decompress body ---
|
|
49
|
+
let body; // Uint8Array of decompressed bytes starting at offset 8
|
|
50
|
+
|
|
51
|
+
if (sig === 'FWS') {
|
|
52
|
+
body = bytes.slice(8);
|
|
53
|
+
} else if (sig === 'CWS') {
|
|
54
|
+
try {
|
|
55
|
+
body = decompressCWS(bytes);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
diag.error(`zlib decompression failed: ${err.message}`, 'parse');
|
|
58
|
+
return { doc: emptyDoc(), diagnostics: diag };
|
|
59
|
+
}
|
|
60
|
+
} else { // ZWS
|
|
61
|
+
try {
|
|
62
|
+
body = await decompressZWS(bytes, fileLength);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
diag.error(`LZMA decompression failed: ${err.message}`, 'parse');
|
|
65
|
+
return { doc: emptyDoc(), diagnostics: diag };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// --- Parse header fields from decompressed body ---
|
|
70
|
+
const reader = new BitReader(body);
|
|
71
|
+
|
|
72
|
+
let frameSize;
|
|
73
|
+
try {
|
|
74
|
+
frameSize = reader.readRect();
|
|
75
|
+
reader.align();
|
|
76
|
+
} catch (err) {
|
|
77
|
+
diag.error(`Failed to read frame size RECT: ${err.message}`, 'parse');
|
|
78
|
+
return { doc: emptyDoc(), diagnostics: diag };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const frameRateRaw = reader.readUI16(); // 8.8 fixed-point
|
|
82
|
+
const frameRate = frameRateRaw / 256;
|
|
83
|
+
const frameCount = reader.readUI16();
|
|
84
|
+
|
|
85
|
+
/** @type {SwfHeader} */
|
|
86
|
+
const header = {
|
|
87
|
+
signature: /** @type {"FWS"|"CWS"|"ZWS"} */ (sig),
|
|
88
|
+
version,
|
|
89
|
+
fileLength,
|
|
90
|
+
frameSize,
|
|
91
|
+
frameRate,
|
|
92
|
+
frameCount,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// --- Parse tags ---
|
|
96
|
+
/** @type {RawTag[]} */
|
|
97
|
+
const tags = [];
|
|
98
|
+
|
|
99
|
+
while (reader.bytePos < body.length) {
|
|
100
|
+
const tagStart = reader.bytePos;
|
|
101
|
+
|
|
102
|
+
let tagCodeAndLength;
|
|
103
|
+
try {
|
|
104
|
+
tagCodeAndLength = reader.readUI16();
|
|
105
|
+
} catch (_) {
|
|
106
|
+
break; // reached end of body
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const code = (tagCodeAndLength >> 6) & 0x3FF;
|
|
110
|
+
let length = tagCodeAndLength & 0x3F;
|
|
111
|
+
|
|
112
|
+
// Long-form tag: sentinel length 0x3F means UI32 follows
|
|
113
|
+
if (length === 0x3F) {
|
|
114
|
+
try {
|
|
115
|
+
length = reader.readUI32();
|
|
116
|
+
} catch (err) {
|
|
117
|
+
diag.warn(`Could not read long-form tag length at offset ${tagStart}`, 'parse', { offset: tagStart });
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// End tag
|
|
123
|
+
if (code === TAG_END) break;
|
|
124
|
+
|
|
125
|
+
// Boundary check
|
|
126
|
+
const dataStart = reader.bytePos;
|
|
127
|
+
const dataEnd = dataStart + length;
|
|
128
|
+
|
|
129
|
+
if (dataEnd > body.length) {
|
|
130
|
+
diag.warn(
|
|
131
|
+
`Tag at offset ${tagStart} (code ${code}) length ${length} exceeds remaining bytes; skipping`,
|
|
132
|
+
'parse',
|
|
133
|
+
{ offset: tagStart, code, length }
|
|
134
|
+
);
|
|
135
|
+
// Skip to end of available data
|
|
136
|
+
reader.seek(body.length);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const data = body.slice(dataStart, dataEnd);
|
|
141
|
+
tags.push({ code, length, data, offset: tagStart });
|
|
142
|
+
reader.seek(dataEnd);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
doc: { header, tags },
|
|
147
|
+
diagnostics: diag,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** @returns {SwfDocument} */
|
|
152
|
+
function emptyDoc() {
|
|
153
|
+
return {
|
|
154
|
+
header: {
|
|
155
|
+
signature: 'FWS',
|
|
156
|
+
version: 0,
|
|
157
|
+
fileLength: 0,
|
|
158
|
+
frameSize: { xMin: 0, xMax: 0, yMin: 0, yMax: 0 },
|
|
159
|
+
frameRate: 0,
|
|
160
|
+
frameCount: 0,
|
|
161
|
+
},
|
|
162
|
+
tags: [],
|
|
163
|
+
};
|
|
164
|
+
}
|