Menu

[r11]: / trunk / SWFInvestigator / src / decompiler / tamarin / abcdump / Abc.as  Maximize  Restore  History

Download this file

672 lines (608 with data), 20.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is [Open Source Virtual Machine.].
*
* The Initial Developer of the Original Code is
* Adobe System Incorporated.
* Portions created by the Initial Developer are Copyright (C) 2004-2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Adobe AS3 Team
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package decompiler.tamarin.abcdump
{
import decompiler.Logging.ILog;
import decompiler.tamarin.abcdump.Multiname;
import decompiler.tamarin.abcdump.Traits;
import decompiler.tamarin.abcdump.TypeName;
import flash.utils.ByteArray;
//Used by Swf.as
//Used by MethodInfo.as
public class Abc
{
include "ABC_Constants.as";
private var data:ByteArray
public var major:int;
public var minor:int;
public var ints:Array;
public var uints:Array;
public var doubles:Array;
public var strings:Array;
public var namespaces:Array;
public var nssets:Array;
public var names:Array;
public var defaults:Array = new Array(constantKinds.length);
public var methods:Array;
public var instances:Array;
public var classes:Array;
public var scripts:Array;
private var metadata:Array;
public var publicNs:Namespace = new Namespace("");
public var anyNs:Namespace = new Namespace("*");
private var magic:int;
//Added by SWF Investigator
public var totalSize:int;
public var opSizes:Array = new Array(256);
public var log:ILog;
public var verboseOut:Boolean = false;
private var infoIndent:String;
public function Abc(data:ByteArray, logger:ILog, verboseFlag:Boolean = false, infoSpace:String = "")
{
//Added by SWF Investigator
this.log = logger;
this.verboseOut = verboseFlag;
this.infoIndent = infoSpace;
data.position = 0;
this.data = data;
this.magic = data.readInt();
if (this.verboseOut) {
verbose(data.position-4, "ABC MAGIC. Version="+(magic>>16)+"."+(magic%65536));
}
this.log.print(infoIndent + "magic " + magic.toString(16) + "\r")
switch (magic) {
case (46<<16|14):
case (46<<16|15):
case (46<<16|16):
case (47<<16|12):
case (47<<16|13):
case (47<<16|14):
case (47<<16|15):
case (47<<16|16):
case (47<<16|17):
case (47<<16|18):
case (47<<16|19):
case (47<<16|20):
break;
default:
throw new Error("not an abc file. magic=" + magic.toString(16))
}
parseCpool();
defaults[CONSTANT_Utf8] = strings;
defaults[CONSTANT_Int] = ints;
defaults[CONSTANT_UInt] = uints;
defaults[CONSTANT_Double] = doubles;
defaults[CONSTANT_Int] = ints;
defaults[CONSTANT_False] = { 10:false };
defaults[CONSTANT_True] = { 11:true };
defaults[CONSTANT_Namespace] = namespaces;
defaults[CONSTANT_PrivateNs] = namespaces;
defaults[CONSTANT_PackageNs] = namespaces;
defaults[CONSTANT_PackageInternalNs] = namespaces;
defaults[CONSTANT_ProtectedNs] = namespaces;
defaults[CONSTANT_StaticProtectedNs] = namespaces;
defaults[CONSTANT_StaticProtectedNs2] = namespaces;
defaults[CONSTANT_Null] = { 12: null };
parseMethodInfos();
parseMetadataInfos();
parseInstanceInfos();
parseClassInfos();
parseScriptInfos();
parseMethodBodies();
//if (doExtractAbc==true)
// data.writeFile(nextAbcFname());
}
//Taken from EvalES4UI
private function verbose(start:uint, comment:String):void {
var b:ByteArray = data;
var s:String =("000000"+start.toString(16)).substr(-6,6)+" ";
for (var i:int=start;i<b.position;i++) {
var c:int = b[i];
s+= ("0"+c.toString(16)).substr(-2,2)+" ";
}
s+=" // "+comment;
this.log.print(s + "\r");
}
private function readU32():int
{
var result:int = data.readUnsignedByte();
if (!(result & 0x00000080))
return result;
result = result & 0x0000007f | data.readUnsignedByte()<<7;
if (!(result & 0x00004000))
return result;
result = result & 0x00003fff | data.readUnsignedByte()<<14;
if (!(result & 0x00200000))
return result;
result = result & 0x001fffff | data.readUnsignedByte()<<21;
if (!(result & 0x10000000))
return result;
return result & 0x0fffffff | data.readUnsignedByte()<<28;
}
private function parseCpool():void
{
var i:int, j:int;
var n:int;
var kind:int;
var start:int = data.position;
var p:uint = start;
// ints
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" int");}
ints = [0];
for (i=1; i < n; i++) {
p = data.position;
ints[i] = readU32();
if (this.verboseOut) {verbose(p, "int["+i+"] = "+ints[i]);}
}
// uints
p = data.position
n = readU32();
uints = [0];
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" int");}
for (i=1; i < n; i++) {
p = data.position;
uints[i] = uint(readU32());
if (this.verboseOut) {verbose(p, "uint["+i+"] = "+uints[i]);}
}
// doubles
p = data.position;
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" Number");}
doubles = [NaN];
for (i=1; i < n; i++) {
p = data.position;
doubles[i] = data.readDouble();
if (this.verboseOut) {verbose(p, "double["+i+"] = "+doubles[i]);}
}
this.log.print(this.infoIndent + "Cpool numbers size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r")
start = data.position
// strings
p = data.position;
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" String");}
strings = [""];
for (i=1; i < n; i++) {
p = data.position;
strings[i] = data.readUTFBytes(readU32());
if (this.verboseOut) {verbose(p, "String["+i+"] = \""+strings[i]+'"');}
}
this.log.print(this.infoIndent + "Cpool strings count "+ n +" size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
start = data.position;
// namespaces
p = data.position;
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" Namespace");}
namespaces = [publicNs];
for (i=1; i < n; i++) {
p = data.position;
switch (data.readByte())
{
case CONSTANT_Namespace:
case CONSTANT_PackageNs:
case CONSTANT_PackageInternalNs:
case CONSTANT_ProtectedNs:
case CONSTANT_StaticProtectedNs:
case CONSTANT_StaticProtectedNs2:
{
namespaces[i] = new Namespace(strings[readU32()])
// todo mark kind of namespace.
var s:String="";
switch(t) {
case CONSTANT_Namespace: s="Namespace"; break;
case CONSTANT_PackageNs: s="Package"; break;
case CONSTANT_PackageInternalNs: s="Internal Package"; break;
case CONSTANT_ProtectedNs: s="protected"; break;
case CONSTANT_StaticProtectedNs: s="static protected"; break;
case CONSTANT_StaticProtectedNs2: s="static protected(v2)"; break;
}
if (this.verboseOut) {verbose(p, "Namespace["+i+"] = "+s+" \""+namespaces[i]+'"');}
break;
}
case CONSTANT_PrivateNs:
readU32();
namespaces[i] = new Namespace(null, "private");
if (this.verboseOut) {verbose(p, "Namespace["+i+"] = \""+namespaces[i]+'"');}
break;
}
}
this.log.print(this.infoIndent + "Cpool namespaces count "+ n +" size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r")
start = data.position;
//SWF Investigator refactor
var count:uint;
// namespace sets
p = data.position;
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" namespace sets");}
nssets = [null];
for (i=1; i < n; i++)
{
p = data.position;
count = readU32();
if (this.verboseOut) {verbose(p, "nsset["+i+"] has "+count+" namespaces");}
var nsset:Array = nssets[i] = [];
for (j=0; j < count; j++) {
p = data.position;
nsset[j] = namespaces[readU32()];
if (this.verboseOut) {verbose(p, "nsset["+i+"]["+j+"] = \""+nsset[j]+"\"");}
}
}
this.log.print(this.infoIndent + "Cpool nssets count "+ n +" size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
start = data.position;
// multinames
p = data.position;
n = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+(n-1)+" multinames");}
names = [null];
namespaces[0] = anyNs;
strings[0] = "*"; // any name
for (i=1; i < n; i++) {
p = data.position;
switch (data.readByte())
{
case CONSTANT_Qname:
case CONSTANT_QnameA:
names[i] = new QName(namespaces[readU32()], strings[readU32()]);
break;
case CONSTANT_RTQname:
case CONSTANT_RTQnameA:
names[i] = new QName(strings[readU32()]);
break;
case CONSTANT_RTQnameL:
case CONSTANT_RTQnameLA:
names[i] = null;
break;
case CONSTANT_NameL:
case CONSTANT_NameLA:
names[i] = new QName(new Namespace(""), null);
break;
case CONSTANT_Multiname:
case CONSTANT_MultinameA:
var sName:String = strings[readU32()];
names[i] = new Multiname(nssets[readU32()], sName);
break;
case CONSTANT_MultinameL:
case CONSTANT_MultinameLA:
names[i] = new Multiname(nssets[readU32()], null);
break;
case CONSTANT_TypeName:
var name:* = names[readU32()];
count = readU32();
var types:Array = [];
for( var t:int=0; t < count; ++t )
types.push(names[readU32()]);
names[i] = new TypeName(name, types);
break;
default:
throw new Error("invalid kind " + data[data.position-1])
}
if (this.verboseOut) {verbose(p, "name["+i+"] = "+names[i]);}
}
this.log.print(this.infoIndent + "Cpool names count "+ n +" size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
start = data.position;
namespaces[0] = publicNs;
strings[0] = "*";
}
private function parseMethodInfos():void
{
var start:int = data.position;
var p:uint = data.position;
names[0] = new QName(publicNs,"*");
var method_count:int = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+method_count+" methods");}
methods = [];
for (var i:int=0; i < method_count; i++)
{
p = data.position;
var m:MethodInfo = methods[i] = new MethodInfo();
//Added by SWF Investigator
m.mDefPosition = this.data.position;
var k:int;
m.method_id = i;
var param_count:int = readU32();
m.returnType = names[readU32()];
m.paramTypes = [];
for (var j:int=0; j < param_count; j++)
m.paramTypes[j] = names[readU32()];
m.debugName = strings[readU32()];
m.flags = data.readByte();
if (m.flags & HAS_OPTIONAL)
{
// has_optional
var optional_count:int = readU32();
m.optionalValues = [];
for( k = param_count-optional_count; k < param_count; ++k)
{
var index:uint = readU32(); // optional value index
var kind:int = data.readByte(); // kind byte for each default value
if (index == 0)
{
// kind is ignored, default value is based on type
m.optionalValues[k] = undefined;
}
else
{
if (!defaults[kind])
this.log.errorPrint("ERROR kind="+kind+" method_id " + i + "\r");
else
m.optionalValues[k] = defaults[kind][index];
}
}
}
if (m.flags & HAS_ParamNames)
{
// has_paramnames
for( k = 0; k < param_count; ++k)
{
readU32();
}
}
if (this.verboseOut) {verbose(p, "method["+i+"] = "+m);}
}
this.log.print(this.infoIndent + "MethodInfo count " +method_count+ " size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r")
}
private function parseMetadataInfos():void
{
var p:uint = data.position;
var count:int = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+count+" metadata");}
metadata= [];
for (var i:int=0; i < count; i++)
{
// MetadataInfo
p = data.position;
var m:MetaData = metadata[i] = new MetaData();
m.name = strings[readU32()];
var values_count:int = readU32();
var names:Array = [];
//Refactor by SWF Investigator
var q:int;
for(q = 0; q < values_count; ++q)
names[q] = strings[readU32()] // name
for(q = 0; q < values_count; ++q)
m[names[q]] = strings[readU32()] // value
if (this.verboseOut) {verbose(p, "metadata["+i+"] = "+m);}
}
}
//Instances extend classes
private function parseInstanceInfos():void
{
var start:int = this.data.position;
var p:uint = this.data.position;
var count:int = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+count+" instance info");}
instances = [];
for (var i:int=0; i < count; i++)
{
p = data.position;
var t:Traits = instances[i] = new Traits();
//Added by SWF Investigator
t.start = this.data.position;
var nIndex:uint = readU32();
t.name = names[nIndex];
t.base = names[readU32()];
t.flags = this.data.readByte();
if (t.flags & 8)
t.protectedNs = namespaces[readU32()];
var interface_count:uint = readU32();
for (var j:int=0; j < interface_count; j++)
t.interfaces[j] = names[readU32()];
var m:MethodInfo = t.init = methods[readU32()];
m.name = t.name;
m.nIndex = nIndex;
m.kind = TRAIT_Method;
m.id = -1;
parseTraits(t);
if (this.verboseOut) {verbose(p, "instance["+i+"] = "+t);}
}
this.log.print(this.infoIndent + "InstanceInfo count " + count + " size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
}
private function parseTraits(t:Traits):void
{
var namecount:uint = readU32();
for (var i:int=0; i < namecount; i++)
{
//Added by Si
var nIndex:uint = readU32();
var name:* = names[nIndex];
var tag:int = data.readByte();
var kind:uint = tag & 0xf;
var member:*;
switch(kind) {
case TRAIT_Slot:
case TRAIT_Const:
case TRAIT_Class:
var slot:SlotInfo = member = new SlotInfo();
slot.id = readU32();
t.slots[slot.id] = slot;
if (kind==TRAIT_Slot || kind==TRAIT_Const)
{
slot.type = names[readU32()];
var index:uint=readU32();
if (index)
slot.value = defaults[data.readByte()][index];
}
else // (kind == TRAIT_Class)
{
slot.value = classes[readU32()];
}
break;
case TRAIT_Method:
case TRAIT_Getter:
case TRAIT_Setter:
var disp_id:uint = readU32();
var method:MethodInfo = member = methods[readU32()];
t.methods[disp_id] = method;
method.id = disp_id;
//print("\t",traitKinds[kind],name,disp_id,method,"// disp_id", disp_id)
break;
}
if (!member)
this.log.errorPrint("error trait kind "+kind + "\r");
member.kind = kind;
member.name = name;
member.nIndex = nIndex;
t.names[String(name)] = t.members[i] = member;
if ( (tag >> 4) & ATTR_metadata ) {
member.metadata = [];
for(var j:int=0, mdCount:int=readU32(); j < mdCount; ++j)
member.metadata[j] = metadata[readU32()];
}
}
}
private function parseClassInfos():void
{
var start:int = data.position;
var count:int = instances.length;
classes = [];
for (var i:int=0; i < count; i++)
{
var t:Traits = classes[i] = new Traits();
//Added by SWF Investigator
t.start = start;
var p:uint = data.position;
t.init = methods[readU32()];
t.base = "Class";
t.itraits = instances[i];
//SWF Investigator change
t.name = t.itraits.name + "$";
t.init.name = t.itraits.name + "$cinit";
t.init.kind = TRAIT_Method;
parseTraits(t);
if (this.verboseOut) {verbose(p, "class["+i+"] = "+t);}
}
this.log.print(this.infoIndent + "ClassInfo count " + count + " size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+"%\r");
}
private function parseScriptInfos():void
{
var start:int = data.position;
var p:uint = data.position;
var count:int = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+count+" script");}
scripts = [];
for (var i:int=0; i < count; i++)
{
p = data.position;
var t:Traits = new Traits();
scripts[i] = t;
t.name = "script" + i;
t.base = names[0]; // Object
t.init = methods[readU32()];
t.init.name = t.name + "$init";
t.init.kind = TRAIT_Method;
parseTraits(t);
if (this.verboseOut) {verbose(p, "script["+i+"] = "+t);}
}
this.log.print(this.infoIndent + "ScriptInfo size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
}
private function parseMethodBodies():void
{
var start:uint = data.position;
var p:uint = data.position;
var count:uint = readU32();
if (this.verboseOut) {verbose(p, "Pool of "+count+" method body");}
for (var i:int=0; i < count; i++)
{
p = data.position;
var m:MethodInfo = methods[readU32()];
m.max_stack = readU32();
m.local_count = readU32();
var initScopeDepth:uint = readU32();
var maxScopeDepth:uint = readU32();
m.max_scope = maxScopeDepth - initScopeDepth;
m.codePosition = data.position;
var code_length:uint = readU32();
m.code = new ByteArray();
m.code.endian = "littleEndian";
if (code_length > 0)
data.readBytes(m.code, 0, code_length);
var ex_count:uint = readU32();
for (var j:int = 0; j < ex_count; j++)
{
var from:uint = readU32();
var to:uint = readU32();
var target:uint = readU32();
var type:* = names[readU32()];
//print("magic " + magic.toString(16))
//if (magic >= (46<<16|16))
var name:* = names[readU32()];
}
parseTraits(m.activation = new Traits);
if (this.verboseOut) {verbose(p, "method "+i);}
}
this.log.print(this.infoIndent + "MethodBodies count " + count + " size "+(data.position-start)+" "+int(100*(data.position-start)/data.length)+" %\r");
}
public function dump(indent:String=""):void
{
//Same
for each (var t:Traits in scripts)
{
this.log.print("\r\r" + indent + "// " + indent+t.name + "\r");
t.dump(this,indent);
t.init.dump(this,indent);
}
//Classes & instances
for each (var m:MethodInfo in methods)
{
if (!m.dumped) {
m.dump(this,indent);
}
}
if (this.verboseOut) {
this.log.print("OPCODE\tSIZE\t% OF "+ this.totalSize + "\r");
var done:Array = [];
for (;;)
{
var max:int = -1;
var maxsize:int = 0;
for (var i:int=0; i < 256; i++)
{
if (opSizes[i] > maxsize && !done[i])
{
max = i;
maxsize = opSizes[i];
}
}
if (max == -1)
break;
done[max] = 1;
this.log.print(opNames[max]+"\t"+int(opSizes[max])+"\t"+int(100*opSizes[max]/this.totalSize)+"%\r")
}
}
}
}
}
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.