forked from CyberShadow/RABCDAsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swffile.d
315 lines (283 loc) · 7.73 KB
/
swffile.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2010, 2011, 2012 Vladimir Panteleev <[email protected]>
* This file is part of RABCDAsm.
*
* RABCDAsm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RABCDAsm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RABCDAsm. If not, see <http://www.gnu.org/licenses/>.
*/
module swffile;
import std.conv;
import std.exception;
import std.zlib;
import zlibx;
version (HAVE_LZMA) import lzma;
/**
* Implements a shallow representation of a .swf file.
* Loading and saving a .swf file using this class should produce
* output identical to the input (aside zlib compression differences).
*/
final class SWFFile
{
Header header;
Rect frameSize;
ushort frameRate, frameCount;
Tag[] tags;
align(1) struct Header
{
align(1):
char[3] signature;
ubyte ver;
uint fileLength;
static assert(Header.sizeof == 8);
}
align(1) struct LZMAHeader
{
align(1):
uint compressedLength;
ubyte compressionParameters;
uint dictionarySize;
static assert(LZMAHeader.sizeof == 9);
}
struct Rect
{
//int xMin, xMax, yMin, yMax;
ubyte[] bytes;
}
struct Tag
{
ushort type;
ubyte[] data;
uint length; // may be >data.length if file is truncated
bool forceLongLength;
}
static SWFFile read(ubyte[] data)
{
return (new SWFReader(data)).swf;
}
ubyte[] write()
{
return SWFWriter.write(this);
}
}
private final class SWFReader
{
ubyte[] buf;
size_t pos;
SWFFile swf;
this(ubyte[] data)
{
buf = data;
swf = new SWFFile();
readRaw((&swf.header)[0..1]);
enforce(swf.header.signature == "FWS" || swf.header.signature == "CWS" || swf.header.signature == "ZWS", "Invalid file signature");
if (swf.header.signature[0] == 'C')
buf = buf[0..swf.header.sizeof] ~ exactUncompress(buf[swf.header.sizeof..$], swf.header.fileLength-swf.header.sizeof);
else
if (swf.header.signature[0] == 'Z')
{
version (HAVE_LZMA)
{
SWFFile.LZMAHeader lzHeader;
readRaw((&lzHeader)[0..1]);
lzma.LZMAHeader lzInfo;
lzInfo.compressionParameters = lzHeader.compressionParameters;
lzInfo.dictionarySize = lzHeader.dictionarySize;
lzInfo.decompressedSize = swf.header.fileLength - swf.header.sizeof;
enforce(swf.header.sizeof + lzHeader.sizeof + lzHeader.compressedLength == buf.length, "Trailing data in LZMA-compressed SWF file");
buf = buf[0..swf.header.sizeof] ~ lzmaDecompress(lzInfo, buf[swf.header.sizeof + lzHeader.sizeof .. $]);
pos = swf.header.sizeof;
}
else
enforce(false, "This version was built without LZMA support");
}
enforce(swf.header.fileLength == buf.length, "Incorrect file length in file header");
swf.frameSize = readRect();
swf.frameRate = readU16();
swf.frameCount = readU16();
while (pos < buf.length)
swf.tags ~= readTag();
}
void readRaw(void[] raw)
{
raw[] = buf[pos..pos+raw.length];
pos += raw.length;
}
/// May read less than len on EOF
void[] readRaw(size_t len)
{
auto end = pos+len;
auto data = buf[pos..end<$?end:$];
pos = end;
return data;
}
version(LittleEndian) {} else static assert(0, "Big endian platforms not supported");
ushort readU16()
{
ushort r;
readRaw((&r)[0..1]);
return r;
}
uint readU32()
{
uint r;
readRaw((&r)[0..1]);
return r;
}
SWFFile.Rect readRect()
{
SWFFile.Rect r;
ubyte b = buf[pos];
uint nbits = b >> 3;
uint nbytes = ((5 + 4*nbits) + 7) / 8;
r.bytes = cast(ubyte[])readRaw(nbytes);
return r;
}
SWFFile.Tag readTag()
{
SWFFile.Tag t;
ushort u = readU16();
t.type = cast(ushort)(u >> 6);
uint length = u & 0x3F;
if (length == 0x3F)
{
length = readU32();
if (length < 0x3F)
t.forceLongLength = true;
}
t.length = length;
t.data = cast(ubyte[])readRaw(length);
return t;
}
}
enum TagType
{
End = 0,
ShowFrame = 1,
DefineShape = 2,
FreeCharacter = 3,
PlaceObject = 4,
RemoveObject = 5,
DefineBits = 6,
DefineButton = 7,
JPEGTables = 8,
SetBackgroundColor = 9,
DefineFont = 10,
DefineText = 11,
DoAction = 12,
DefineFontInfo = 13,
DefineSound = 14,
StartSound = 15,
DefineButtonSound = 17,
SoundStreamHead = 18,
SoundStreamBlock = 19,
DefineBitsLossless = 20,
DefineBitsJPEG2 = 21,
DefineShape2 = 22,
DefineButtonCxform = 23,
Protect = 24,
PathsArePostScript = 25,
PlaceObject2 = 26,
RemoveObject2 = 28,
DefineShape3 = 32,
DefineText2 = 33,
DefineButton2 = 34,
DefineBitsJPEG3 = 35,
DefineBitsLossless2 = 36,
DefineSprite = 39,
ProductInfo = 41,
FrameLabel = 43,
SoundStreamHead2 = 45,
DefineMorphShape = 46,
DefineFont2 = 48,
DefineEditText = 37,
ExportAssets = 56,
ImportAssets = 57,
EnableDebugger = 58,
DoInitAction = 59,
DefineVideoStream = 60,
VideoFrame = 61,
DefineFontInfo2 = 62,
DebugID = 63,
EnableDebugger2 = 64,
ScriptLimits = 65,
SetTabIndex = 66,
FileAttributes = 69,
PlaceObject3 = 70,
ImportAssets2 = 71,
DoABC = 72,
DefineFontAlignZones = 73,
CSMTextSettings = 74,
DefineFont3 = 75,
SymbolClass = 76,
Metadata = 77,
DefineScalingGrid = 78,
DoABC2 = 82,
DefineShape4 = 83,
DefineMorphShape2 = 84,
DefineSceneAndFrameLabelData = 86,
DefineBinaryData = 87,
DefineFontName = 88,
DefineFont4 = 91
}
private final class SWFWriter
{
static ubyte[] write(SWFFile swf)
{
ubyte[] buf;
buf ~= swf.frameSize.bytes;
buf ~= toArray(swf.frameRate);
buf ~= toArray(swf.frameCount);
foreach (ref tag; swf.tags)
{
ushort u = cast(ushort)(tag.type << 6);
if (tag.length < 0x3F && !tag.forceLongLength)
{
u |= tag.length;
buf ~= toArray(u);
}
else
{
u |= 0x3F;
buf ~= toArray(u);
uint l = to!uint(tag.length);
buf ~= toArray(l);
}
buf ~= tag.data;
}
swf.header.fileLength = to!uint(swf.header.sizeof + buf.length);
if (swf.header.signature[0] == 'C')
buf = cast(ubyte[])compress(buf, 9);
else
if (swf.header.signature[0] == 'Z')
{
version (HAVE_LZMA)
{
lzma.LZMAHeader lzInfo;
buf = lzmaCompress(buf, &lzInfo);
SWFFile.LZMAHeader lzHeader;
lzHeader.compressionParameters = lzInfo.compressionParameters;
lzHeader.dictionarySize = lzInfo.dictionarySize;
lzHeader.compressedLength = to!uint(buf.length);
buf = cast(ubyte[])(&lzHeader)[0..1] ~ buf;
}
else
enforce(false, "This version was built without LZMA support");
}
buf = toArray(swf.header) ~ buf;
return buf;
}
static ubyte[] toArray(T)(ref T v)
{
return cast(ubyte[])(&v)[0..1];
}
}