Menu

[r11]: / trunk / SWFInvestigator / src / decompiler / swfdump / ActionFactory.as  Maximize  Restore  History

Download this file

271 lines (236 with data), 7.9 kB

  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
////////////////////////////////////////////////////////////////////////////////
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2003-2006 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package decompiler.swfdump
{
import decompiler.swfdump.Action;
import decompiler.swfdump.actions.Label;
import decompiler.swfdump.actions.Push;
import decompiler.swfdump.actions.StoreRegister;
import decompiler.swfdump.actions.StrictMode;
import decompiler.swfdump.actions.WaitForFrame;
import decompiler.swfdump.debug.LineRecord;
import decompiler.swfdump.debug.RegisterRecord;
import decompiler.swfdump.types.ActionList;
import decompiler.swfdump.types.PushNum;
final public class ActionFactory
{
public static const UNDEFINED:String = "undefined";
public static const STACKTOP:String = "stack";
/** flyweight action objects for 1-byte opcodes 0..7F */
private static var actionFlyweights:Vector.<Action> = new Vector.<Action>(0x80);
private static var pushCpoolFlyweights:Vector.<Push> = new Vector.<Push>(256);
private static var pushRegisterFlyweights:Vector.<Push> = new Vector.<Push>(256);
private static var storeRegisterFlyweights:Vector.<StoreRegister> = new Vector.<StoreRegister>(256);
private static var pushTrueFlyweight:Push = new Push( new Boolean(true));
private static var pushFalseFlyweight:Push = new Push(new Boolean(false));
private static var pushUndefinedFlyweight:Push = new Push(UNDEFINED);
private static var pushNullFlyweight:Push = new Push(null);
private static var pushFloat0Flyweight:Push = new Push(new PushNum("Float", 0));
private static var pushInteger0Flyweight:Push = new Push(0);
private static var pushDouble0Flyweight:Push = new Push(new PushNum("Double",0));
private static var callFlyweight:Action = new Action(ActionConstants.sactionCall);
private static var strictTrueFlyweight:StrictMode = new StrictMode(true);
private static var strictFalseFlyweight:StrictMode = new StrictMode(false);
public static function createAction(code:int):Action
{
return actionFlyweights[code];
}
public static function createPushCpool(index:int):Push
{
return (index < pushCpoolFlyweights.length)
? pushCpoolFlyweights[index]
: new Push(new PushNum("Short", index));
}
public static function createPushString(s:String):Push
{
return new Push(s);
}
public static function createPushFloat(fvalue:PushNum):Push
{
return fvalue.num == 0
? pushFloat0Flyweight
: new Push(new PushNum("Float",fvalue.num));
}
public static function createPushNull():Push
{
return pushNullFlyweight;
}
public static function createPushUndefined():Push
{
return pushUndefinedFlyweight;
}
public static function createPushRegister(regno:int):Push
{
return pushRegisterFlyweights[regno];
}
public static function createPushBoolean(b:Boolean):Push
{
return (b ? pushTrueFlyweight : pushFalseFlyweight);
}
public static function createPushDouble(dvalue:PushNum):Push
{
return dvalue.num == 0
? pushDouble0Flyweight
: new Push(new PushNum("Double", dvalue.num));
}
public static function createPushInt(ivalue:int):Push
{
return ivalue == 0
? pushInteger0Flyweight
: new Push(ivalue);
}
public static function createStoreRegister(register:int):StoreRegister
{
return storeRegisterFlyweights[register];
}
public static function createCall():Action
{
return callFlyweight;
}
public static function createStrictMode(mode:Boolean):StrictMode
{
return mode ? strictTrueFlyweight : strictFalseFlyweight;
}
private var startOffset:int;
private var startCount:int;
private var actions:Vector.<Action>;
private var labels:Vector.<Label>;
private var lines:Vector.<LineRecord>;
private var registers:Vector.<RegisterRecord>;
private var actionOffsets:Vector.<int>;
private var count:int;
private var skipRecords:Vector.<SkipEntry>;
public function ActionFactory(length:int, startOffset:int, startCount:int)
{
this.startOffset = startOffset;
this.startCount = startCount;
labels = new Vector.<Label>(length+1); // length+1 to handle labels after last action
lines = new Vector.<LineRecord>(length);
registers = new Vector.<RegisterRecord>(length);
actions = new Vector.<Action>(length);
actionOffsets = new Vector.<int>(length+1);
skipRecords = new Vector.<SkipEntry>();
var i:int;
for (i=0; i < 0x80; i++)
{
ActionFactory.actionFlyweights[i] = new Action(i);
}
for (i=0; i < 256; i++)
{
ActionFactory.pushRegisterFlyweights[i] = new Push(new PushNum("Byte",i));
ActionFactory.pushCpoolFlyweights[i] = new Push(new PushNum("Short", i));
ActionFactory.storeRegisterFlyweights[i] = new StoreRegister(i);
}
}
public function setLine(offset:int, line:LineRecord):void
{
var i:int = offset-startOffset;
if (lines[i] == null)
count++;
lines[i] = line;
}
public function setRegister(offset:int, record:RegisterRecord):void
{
var i:int = offset-startOffset;
if (registers[i] == null)
count++;
registers[i] = record;
}
public function setAction(offset:int, a:Action):void
{
var i:int = offset-startOffset;
if (actions[i] == null)
count++;
actions[i] = a;
}
public function getLabel(target:int):Label
{
var i:int = target-startOffset;
var label:Label = labels[i];
if (label == null)
{
labels[i] = label = new Label();
count++;
}
return label;
}
public function setActionOffset(actionCount:int, offset:int):void
{
actionOffsets[actionCount-startCount] = offset;
}
/**
* now that everything has been decoded, build a single actionlist
* with the labels and jump targets merged in.
* @param keepOffsets
* @return
*/
public function createActionList(keepOffsets:Boolean):ActionList
{
processSkipEntries();
var list:ActionList = new ActionList(keepOffsets);
list.grow(count);
var a:Action;
var length:int = actions.length;
var i:int;
if (keepOffsets)
{
for (i=0; i < length; i++)
{
var offset:int = startOffset+i;
if ((a=actions[i]) != null)
list.insert(offset, a);
if ((a=lines[i]) != null)
list.insert(offset, a);
if ((a=registers[i]) != null)
list.insert(offset, a);
if ((a=labels[i]) != null)
list.insert(offset, a);
}
if ((a=labels[length]) != null)
list.insert(startOffset+length, a);
}
else
{
for (i=0; i < length; i++)
{
if ((a=labels[i]) != null)
list.append(a);
if ((a=lines[i]) != null)
list.append(a);
if ((a=registers[i]) != null)
list.append(a);
if ((a=actions[i]) != null)
list.append(a);
}
if ((a=labels[length]) != null)
list.append(a);
}
return list;
}
/**
* postprocess skip records now that we now the offset of each encoded action
*/
private function processSkipEntries():void
{
var i:int;
for (i = 0; i < skipRecords.length; i++)
{
var skipRecord:SkipEntry = skipRecords[i];
var labelOffset:int = actionOffsets[skipRecord.skipTarget-startCount];
skipRecord.action.skipTarget = getLabel(labelOffset);
}
}
public function addSkipEntry(a:WaitForFrame, sTarget:int):void
{
skipRecords.push(new SkipEntry(a, sTarget));
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.