-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathswf-buffer.js
287 lines (237 loc) · 5.88 KB
/
swf-buffer.js
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
var RECORDHEADER_LENTH_FULL = 0x3f
// null-character
, EOS = 0x00
, styleCountExt = 0xFF;
function readStyleArray(buffer, next) {
var styleArrayCount = buffer.readUInt8()
, styles = [];
if (styleArrayCount === styleCountExt)
styleArrayCount = buffer.readUIntLE(16);
for (var i = 0; i < styleArrayCount; i++)
styles.push(next(buffer));
return styles;
}
function readFillStyle(buffer) {
var type = buffer.readUInt8()
, fillStyle = {
/**
* 0x00 = solid
* 0x10 = linear gradient fill
* 0x12 = radial gradient fill
* 0x13 = focal radial gradient fill (SWF 8 or later)
* 0x40 = repeating bitmap fill
* 0x41 = clipped bitmap fill
* 0x42 = non-smoothed repeating bitmap
* 0x43 = non-smoothed clipped bitmap
*/
fillStyleType : type
};
switch (type) {
case 0x00:
fillStyle.color = buffer.readRGBA();
break;
case 0x10, 0x12, 0x13:
console.log('Gradient');
break;
case 0x40, 0x41, 0x42, 0x43:
fillStyle.bitmapId = buffer.readUIntLE(16);
break;
}
return fillStyle;
}
function readLineStyle(buffer) {
return {
width: buffer.readUIntLE(16)/20,
color: buffer.readRGBA()
};
}
function readShapeRecords(buffer) {
var shapeRecords = []
, typeFlag = buffer.readBits(1)
, shapeRecord
, eos;
while ((eos = buffer.readBits(5))) {
if (0 === typeFlag) {
shaperecord = {
type: 'STYLECHANGERECORD'
};
}
}
return shapeRecords;
}
/**
*
* Constructor of SWFBuffer object
*
* @param {Buffer} buffer
* @return Instance of SWFBuffer
*/
function SWFBuffer( buffer ) {
if ( !Buffer.isBuffer( buffer ) ) {
throw new Error('Invalid buffer');
}
this.buffer = buffer;
this.pointer = 0;
this.position = 1;
this.current = 0;
this.length = buffer.length;
}
/**
* Reads unsigned 16 or 32 Little Endian Bits
* and advance pointer to next bits / 8 bytes
*
* @param {Number} bits
* @return {Number} Value read from buffer
*/
SWFBuffer.prototype.readUIntLE = function( bits ) {
var value = 0;
try {
value = this.buffer['readUInt' + bits + 'LE'](this.pointer);
this.pointer += bits / 8;
} catch ( e ) {
throw e;
}
return value;
};
/**
* Reads unsigned 8 bit from the buffer
*
* @return {Number} Value read from buffer
*/
SWFBuffer.prototype.readUInt8 = function() {
return this.buffer.readUInt8( this.pointer++ );
};
/**
* Reads 32-bit unsigned integers value encoded (1-5 bytes)
*
* @return {Number} 32-bit unsigned integer
*/
SWFBuffer.prototype.readEncodedU32 = function() {
var i = 5
, result = 0
, nb;
do
result += (nb = this.nextByte());
while((nb & 128) && --i);
return result;
};
/**
* Reads an encoded data from buffer and returns a
* string using the specified character set.
*
* @param {String} encoding - defaults to 'utf8'
* @returns {String} Decoded string
*/
SWFBuffer.prototype.readString = function(encoding) {
var init = this.pointer;
while(this.readUInt8() !== EOS);
return this.buffer.toString(encoding || 'utf8', init, this.pointer - 1);
};
/**
* Reads RGB value
*
* @return {Array} Array of RGB value
*/
SWFBuffer.prototype.readRGB = function() {
return [this.readUInt8(), this.readUInt8(), this.readUInt8()];
};
/**
* Reads RGBA value
*
* @return {Array} Array of RGBA value
*/
SWFBuffer.prototype.readRGBA = function() {
var rgba = this.readRGB();
rgba.push(this.readUInt8());
return rgba;
}
/**
* Reads ShapeWithStyle structure
* used by the DefineShape tag.
*
* @return ShapeWithStyle structure
*/
SWFBuffer.prototype.readShapeWithStyle = function() {
return {
fillStyles : readStyleArray(this, readFillStyle),
lineStyles : readStyleArray(this, readLineStyle),
numFillBits : this.readBits(4),
numLineBits : this.readBits(4),
shapeRecords: readShapeRecords(this)
}
};
/**
* Reads RECORDHEADER from next tag in the buffer
*
* @return {Object} Tag code and length
*/
SWFBuffer.prototype.readTagCodeAndLength = function() {
var n = this.readUIntLE(16)
, tagType = n >> 6
, tagLength = n & RECORDHEADER_LENTH_FULL;
if ( n === 0 )
return false;
if ( tagLength === RECORDHEADER_LENTH_FULL )
tagLength = this.readUIntLE(32);
return { code : tagType, length : tagLength };
};
/**
* Reads RECT format
*
* @return {Object} x, y, width and height of the RECT
*/
SWFBuffer.prototype.readRect = function() {
this.start();
var NBits = this.readBits(5)
, Xmin = this.readBits(NBits, true)/20
, Xmax = this.readBits(NBits, true)/20
, Ymin = this.readBits(NBits, true)/20
, Ymax = this.readBits(NBits, true)/20;
return {
x : Xmin,
y : Ymin,
width : (Xmax > Xmin ? Xmax - Xmin : Xmin - Xmax),
height : (Ymax > Ymin ? Ymax - Ymin : Ymin - Ymax)
};
}
/**
* Sets internal pointer to the specified position;
*
* @param {Number} pos
*/
SWFBuffer.prototype.seek = function( pos ) {
this.pointer = pos % this.buffer.length;
};
/**
* Resets position and sets current to next Byte in buffer
*/
SWFBuffer.prototype.start = function() {
this.current = this.nextByte();
this.position = 1;
};
/**
* Gets next Byte in the buffer and Increment internal pointer
*
* @return {Number} Next byte in buffer
*/
SWFBuffer.prototype.nextByte = function() {
return this.pointer > this.buffer.length ? null : this.buffer[ this.pointer++ ];
};
/**
* Reads b bits from current byte in buffer
*
* @param {Number} b
* @return {Number} Bits read from buffer
*/
SWFBuffer.prototype.readBits = function( b, signed ) {
var n = 0
, r = 0
, sign = signed && ++n && ((this.current >> (8-this.position++)) & 1) ? -1 : 1;
while( n++ < b ) {
if ( this.position > 8 ) this.start();
r = (r << 1 ) + ((this.current >> (8-this.position++)) & 1);
}
return sign * r;
};
/* Exposes class */
exports = module.exports = SWFBuffer;