-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWFReader.cpp
262 lines (182 loc) · 5.47 KB
/
SWFReader.cpp
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
/*
* File: SWFReader.cpp
* Author: brucewang
*
* Created on October 24, 2009, 8:44 PM
*/
#include "SWFReader.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int SWFReader::getNumberOfVariables() {
return this->iVariables;
}
void SWFReader::AddVariableInfo(
ulong lPosition,
ulong length,
int iOriginalOffset,
ushort usEditId,
int iRectInfoLength,
byte *bRectData,
int iOptionFlagSize,
byte *bOptionFlagData,
ushort usFlag,
char *szVariableName,
char *szInitialValue) {
VariableList[iVariables].lPosition = lPosition;
VariableList[iVariables].lOriginalLength = VariableList[iVariables].lNewLength = length;
VariableList[iVariables].iOriginalOffset = iOriginalOffset;
VariableList[iVariables].usEditId = usEditId;
VariableList[iVariables].usFlag = usFlag;
VariableList[iVariables].iRectInfoLength = iRectInfoLength;
memcpy(VariableList[iVariables].bRectData, bRectData, iRectInfoLength);
VariableList[iVariables].iOptionFlagSize = iOptionFlagSize;
memcpy(VariableList[iVariables].bOptionFlagData, bOptionFlagData, iOptionFlagSize);
strcpy(VariableList[iVariables].szVariableName, szVariableName);
strcpy(VariableList[iVariables].szInitialValue, szInitialValue);
iVariables++;
}
////////////////////////////////////////////////////////////////////////////////
// READ
////////////////////////////////////////////////////////////////////////////////
/*
* 2007-08-07 brucewang
*/
long SWFReader::GetPosition() {
return lDataOffset;
}
SWFReader::~SWFReader() {
if (bitValues) delete bitValues;
if (powers) delete powers;
}
SWFReader::SWFReader(byte *buffer) {
SwfData = buffer;
bitPosition = 0; // Current bit position within byte (only used for reading bit fields)
currentByte = 0; // Value of the current byte (only used for reading bit fields)
lDataOffset = 0L;
lFileSizePos = 4;
iVariables = 0;
// Setup bit values for later lookup
bitValues = new UInt32[32]; /// deletion is done at the destructor
for (byte power = 0; power < 32; power++) {
bitValues[power] = (UInt32) (1 << power);
}
// Setup power values for later lookup
powers = new Single[32]; /// deletion is done at the destructor
for (byte power = 0; power < 32; power++) {
powers[power] = (Single) pow(2.0f, (power - 16)*1.0f);
}
}
byte SWFReader::ReadByte() {
byte byteRead = 0;
// !!
// !!
memcpy(&byteRead, this->SwfData + lDataOffset, 1);
lDataOffset += 1;
this->bitPosition = 8; // So that ReadBit() knows that we've "used" this byte already
return (byte) byteRead;
}
bool SWFReader::ReadBit() {
bool result;
// Do we need another byte?
if (this->bitPosition > 7) {
this->currentByte = ReadByte();
this->bitPosition = 0; // Reset, since we haven't "used" this byte yet
}
// Read the current bit
result = ((this->currentByte & bitValues[(7 - bitPosition)]) != 0);
// Move to the next bit
this->bitPosition++;
return result;
}
// Read an unsigned 8-bit integer
byte SWFReader::ReadUI8() {
return ReadByte();
}
// You need to 'delete' the returned memory chunk
// after use.
//
// Read an array of unsigned 8-bit integers
byte* SWFReader::ReadUI8(int n) {
byte* result = new byte[n];
for (int index = 0; index < n; index++) {
result[index] = ReadUI8();
}
return result;
}
// Read a signed byte
sbyte SWFReader::ReadSI8() {
return (sbyte) ReadByte();
}
// Read an unsigned 16-bit integer
UInt16 SWFReader::ReadUI16() {
UInt16 result = 0;
result |= (UInt16) ReadByte();
result |= (UInt16) (ReadByte() << 8);
return result;
}
// Read a signed 16-bit integer
Int16 SWFReader::ReadSI16() {
return (Int16) ReadUI16();
}
// Read an unsigned 32-bit integer
UInt32 SWFReader::ReadUI32() {
UInt32 result = 0;
result |= (UInt32) ReadByte();
result |= (UInt32) (ReadByte() << 8);
result |= (UInt32) (ReadByte() << 16);
result |= (UInt32) (ReadByte() << 24);
return result;
}
// Read a signed 32-bit integer
Int32 SWFReader::ReadSI32() {
return (Int32) ReadUI32();
}
// Read a szstring
// TODO: Is StringBuilder worth it for these small strings?
szstring SWFReader::ReadSTRING() {
static char result[4096] = {0,};
memset(result,0,4096);
int index = 0;
byte byteTemp = 0;
// Grab characters until we hit 0x00
do {
byteTemp = ReadByte();
result[index] = (char) byteTemp;
index++;
} while (byteTemp != 0x00);
return result;
}
// Read an unsigned bit value
UInt32 SWFReader::ReadUB(int nBits) {
UInt32 result = 0;
// Is there anything to read?
if (nBits > 0) {
// Calculate value
for (int index = nBits - 1; index > -1; index--) {
if (ReadBit()) {
result |= bitValues[index];
}
}
}
return result;
}
// Read a signed bit value
Int32 SWFReader::ReadSB(int nBits) {
Int32 result = 0;
// Is there anything to read?
if (nBits > 0) {
// Is this a negative number (MSB will be set)?
if (ReadBit()) {
result -= (Int32) bitValues[nBits - 1];
}
// Calculate rest of value
for (int index = nBits - 2; index > -1; index--) {
if (ReadBit()) {
result |= (Int32) bitValues[index];
}
}
}
return result;
}