Menu

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

Download this file

888 lines (781 with data), 25.2 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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/* ***** 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
{
import decompiler.Logging.ILog;
import decompiler.Logging.StringWriter;
import decompiler.swfdump.Action;
import decompiler.swfdump.ActionDecoder;
import decompiler.swfdump.SWFDictionary;
import decompiler.swfdump.SWFFormatError;
import decompiler.swfdump.TagDecoder;
import decompiler.swfdump.TagValues;
import decompiler.swfdump.tags.DefineButton;
import decompiler.swfdump.tags.DoAction;
import decompiler.swfdump.tags.DoInitAction;
import decompiler.swfdump.tags.PlaceObject;
import decompiler.swfdump.tools.Disassembler;
import decompiler.swfdump.tools.SwfxPrinter;
import decompiler.swfdump.types.ActionList;
import decompiler.swfdump.types.ClipActionRecord;
import decompiler.swfdump.types.ClipActions;
import decompiler.tamarin.abcdump.Abc;
import decompiler.tamarin.abcdump.Rect;
import decompiler.tamarin.abcdump.Tag;
import flash.utils.ByteArray;
import flash.utils.Endian;
//import flash.utils.getDefinitionByName;
public class Swf
{
include 'tamarin/abcdump/SWF_Constants.as';
private var bitPos:int;
private var bitBuf:int;
private var offset:int;
public var data:ByteArray = new ByteArray();
private var act:Disassembler;
private var aDecoder:ActionDecoder;
//Added by SWF Investigator
//Basic Info
public var rect:Rect;
public var frameRate:uint;
public var frameCount:uint;
public var version:uint;
public var compressed:Boolean;
public var length:uint;
//File Attributes
public var useNetwork:Boolean;
public var hasMetadata:Boolean;
public var avm2:Boolean = false;
public var suppressXDomainCaching:Boolean;
public var useGPU:Boolean;
public var useDirectBlit:Boolean;
public var swfRelativeURLs:Boolean;
public var hasActions:Boolean = false;
//Data structures
public var abc:Array = [];
public var tags:Array = [];
public var classes:Array;
public var log:ILog;
private var verboseLog:Boolean = false;
private var dict:SWFDictionary;
public function Swf(swfData:ByteArray, logger:ILog, dumpVerbose:Boolean = false, decoderMode:Boolean = false)
{
this.data.clear();
this.data.writeBytes(swfData);
this.data.position = 0;
this.log = logger;
this.verboseLog = dumpVerbose;
//The TagDecoder will use this class as just a fancy wrapper for reading from a ByteArray
//Therefore, we shouldn't try to interpret the byteArray as a SWF.
if (decoderMode) {
return;
}
//Begin SWF Investigator changes
this.data.endian = Endian.LITTLE_ENDIAN;
this.dict = new SWFDictionary();
var header:String = data.readUTFBytes(3);
if (header == "CWS") {
this.compressed = true;
}
this.version = data.readUnsignedByte();
this.length = data.readUnsignedInt();
if (this.compressed) {
var udata:ByteArray = new ByteArray;
udata.endian = "littleEndian";
//data.position = 8;
data.readBytes(udata,0,data.length-data.position);
var csize:int = udata.length;
udata.uncompress();
this.log.print("decompressed swf "+csize+" -> "+udata.length + "\r");
udata.position = 0;
this.data.clear();
this.data.writeUTFBytes("FWS");
this.data.writeByte(this.version);
this.data.writeUnsignedInt(this.length);
this.data.writeBytes(udata);
this.data.position = 8;
}
//end SWF Investigator changes
decodeRect();
this.log.print("size "+ this.rect.toString() + "\r");
this.frameRate = (data.readUnsignedByte()<<8|data.readUnsignedByte());
this.log.print("frame rate " + this.frameRate + "\r");
this.frameCount = data.readUnsignedShort();
this.log.print("frame count " + this.frameCount + "\r");
decodeTags()
}
public function decodeTags():void
{
var type:int, h:int, length:int;
var offset:int;
var tDecoder:TagDecoder = new TagDecoder(this);
tDecoder.setDictionary(this.dict);
var tag:Tag;
while (data.position < data.length)
{
//Added by SWF Investigator
tag = new Tag();
tag.position = data.position;
var eat:int = 0;
type = (h = data.readUnsignedShort()) >> 6;
if (((length = h & 0x3F) == 0x3F)) {
tag.longRecordHeader = true;
length = data.readInt();
}
if (length < 0) {
throw new SWFFormatError("Negative length: " + length + " for tag at position: " + data.position);
}
var o:int = getOffset();
//Added by SWF Investigator
tag.type = type;
tag.tName = tagNames[type];
tag.size = length;
//var tValues:TagValues = new TagValues();
if (type == stagDoABC || type == stagDoABC2) {
this.avm2 = true;
data.position += tag.size;
} else {
try {
tag.theTag = tDecoder.decodeTag(tag.type, tag.size);
} catch (e:Error) {
//Don't care since not all tags are supported.
//Just skip to the end
if (tag.longRecordHeader) {
data.position = tag.position + tag.size + 6;
} else {
data.position = tag.position + tag.size + 2;
}
}
}
if (getOffset() - o != length) {
tag.SIErrorMessage = "offset mismatch after " + tag.tName + ": read " + (getOffset() - o) + ", expected " + length;
if (getOffset() - o < length)
{
eat = length - (getOffset() - o);
}
if (eat > 0) {
var trash:ByteArray = new ByteArray();
trash.length = eat;
read (trash);
}
}
if (this.tags[type] == null) {
this.tags[type] = [];
}
this.tags[type].push(tag);
//This is a swag to a certain extent
if (( type == TagValues.stagDoInitAction) || ( type == TagValues.stagDoAction) || ( type == TagValues.stagDefineButton)
|| (type == TagValues.stagDefineButton2) || (type == TagValues.stagPlaceObject2) || (type == TagValues.stagPlaceObject3)) {
this.hasActions = true;
}
//File Attributes
if (type == 69) {
this.useNetwork = tag.theTag.useNetwork;
this.avm2 = tag.theTag.actionScript3;
this.hasMetadata = tag.theTag.hasMetadata;
this.useDirectBlit = tag.theTag.useDirectBlit;
this.suppressXDomainCaching = tag.theTag.suppressCrossDomainCaching;
this.useGPU = tag.theTag.useGPU;
this.swfRelativeURLs = tag.theTag.swfRelativeUrls;
}
this.log.print(tagNames[type]+" "+length+"b "+int(100*length/data.length)+"%" + "\r");
//data.position += tag.size;
}
}
public function getTagName(i:int):String {
return tagNames[i];
}
/**
* Dumping Action tags
* The following functions are dedicated to dumping tags that relate to Actions or Metadata
*/
private function dumpDoABC(bLength:uint, logger:ILog, keep:Boolean = true, infoIdent:String = ""):void {
var data2:ByteArray = new ByteArray();
data2.endian = "littleEndian";
this.data.readBytes(data2,0,bLength);
data2.position = 0;
var abcInfo:Abc = new Abc(data2,logger, this.verboseLog, infoIdent);
if (keep) {
this.abc[this.abc.length] = abcInfo;
}
abcInfo.dump(" ");
logger.print("\r");
data2.clear();
}
public function dumpABC():void {
var t1:Tag;
//var data2:ByteArray;
if (this.tags[stagDoABC2] != null && this.tags[stagDoABC2].length > 0) {
for each (t1 in this.tags[stagDoABC2]) {
//var pos1:int = t1.position;
var bLength:uint = t1.size;
this.data.position = t1.position;
data.readUnsignedShort(); //consume tag
data.readInt(); //consume length
data.readInt();
this.log.print("\nabc name "+readString() + "\r");
bLength -= (data.position- t1.position - 6);
dumpDoABC(bLength, this.log);
}
}
if (this.tags[stagDoABC] != null && this.tags[stagDoABC].length > 0) {
for each (t1 in this.tags[stagDoABC]) {
this.data.position = t1.position;
data.readUnsignedShort();//consume tag
data.readInt();//consume length
dumpDoABC(t1.size, this.log);
}
}
this.data.position = 0;
}
public function dumpAllActions(logger:ILog = null):void {
if (logger == null) {logger = this.log;}
var actionTags:Array = [TagValues.stagDoAction,
TagValues.stagDoInitAction,
TagValues.stagDefineButton,
TagValues.stagDefineButton2,
TagValues.stagPlaceObject2,
TagValues.stagPlaceObject3];
var dis:Disassembler = new Disassembler(logger,null," ",true,1);
var tDecoder:TagDecoder = new TagDecoder(this);
tDecoder.setDictionary(this.dict);
for each (var i:int in actionTags) {
if (this.tags[i] != null && this.tags[i].length > 0) { // && this.tags[34][i] != null) {
for each (var t1:Tag in this.tags[i]) {
if (t1 != null) {
if (t1.longRecordHeader) {
data.position = t1.position + 6;
} else {
data.position = t1.position + 2;
}
var t:Tag;
if (i == TagValues.stagDefineButton2) {
var db:DefineButton;
if (t1.theTag != null && t1.theTag is DefineButton) {
db = t1.theTag;
} else {
t = tDecoder.decodeDefineButton2(t1.size);
db = t as DefineButton;
}
logger.print("\r<DefineButton2 id=\"" + db.id + "\">\r");
for (var j:int = 0; j < db.condActions.length; j++) {
logger.print("\r on(" + db.condActions[j].toString() + ") {\r");
var aList:ActionList = db.condActions[j].actionList;
aList.visitAll(dis);
logger.print("}\r");
}
logger.print("</DefineButton2>\r\r");
} else if (i == TagValues.stagDoInitAction) {
logger.print("<DoInitAction>\r");
if (t1.theTag && t1 is DoInitAction) {
t1.theTag.actionList.visitAll(dis);
} else {
t = tDecoder.decodeDoInitAction(t1.size);
(t as DoInitAction).actionList.visitAll(dis);
}
logger.print("</DoInitAction>\r\r");
} else if (i == TagValues.stagPlaceObject2) {
var p2:PlaceObject;
if (t1.theTag && t1.theTag is PlaceObject) {
p2 = t1.theTag;
} else {
t = tDecoder.decodePlaceObject23(2,t1.size);
p2 = t as PlaceObject;
}
if (p2.hasClipAction()) {
logger.print("<PlaceObject2 name=\"" + p2.name + "\">\r");
for each (var c2:ClipActionRecord in p2.clipActions.clipActionRecords) {
logger.print("\r onClipEvent(" + SwfxPrinter.printClipEventFlags(c2.eventFlags) +
(c2.hasKeyPress() ? "<" + c2.keyCode + ">" : "") +
") {\r");
c2.actionList.visitAll(dis);
logger.print(" }\r");
}
logger.print("</PlaceObject2>\r\r");
}
} else if (i == TagValues.stagPlaceObject3) {
var p3:PlaceObject;
if (t1.theTag && t1.theTag is PlaceObject) {
p3 = t1.theTag;
} else {
t = tDecoder.decodePlaceObject23(3,t1.size);
p3 = t as PlaceObject;
}
if (p3.hasClipAction()) {
logger.print("<PlaceObject3 name=\"" + p3.name + "\">\r");
for each (var c3:ClipActionRecord in p3.clipActions.clipActionRecords) {
logger.print("\r onClipEvent(" + SwfxPrinter.printClipEventFlags(c3.eventFlags) +
(c3.hasKeyPress() ? "<" + c3.keyCode + ">" : "") +
") {\r");
c3.actionList.visitAll(dis);
logger.print(" }\r");
}
logger.print("</PlaceObject3>\r\r");
}
} else if (i == TagValues.stagDefineButton) {
logger.print("<DefineButton>\r");
if (t1.theTag && t1.theTag is DefineButton) {
t1.theTag.condActions[0].actionList.visitAll(dis);
} else {
t = tDecoder.decodeDefineButton(t1.size);
(t as DefineButton).condActions[0].actionList.visitAll(dis);
}
logger.print("</DefineButton>\r\r");
} else if (i == TagValues.stagDoAction) {
logger.print("<DoAction>\r");
try {
if (t1.theTag && t1.theTag is DoAction) {
t = t1.theTag;
} else {
t = tDecoder.decodeDoAction(t1.size);
}
} catch (s:SWFFormatError) {
t = s.t;
logger.print("Error encountered: " + s.message + "\r\r");
}
(t as DoAction).actionList.visitAll(dis);
logger.print("</DoAction>\r\r");
}
} //end if t1 != null
} //end for each
}// end if length > 1
}//end for each
}
public function getMetadata():XML
{
if ( this.tags[77] )
{
var origPos:uint = this.data.position;
this.data.position = tags[77][0].position;
var metadata:XML = new XML(this.data.readUTFBytes(tags[77][0].size) );
this.data.position = origPos;
return metadata;
};
return null;
}
/**
* Tag Viewer functions
* These functions are for the Tag Viewer tab.
*/
/**
* @public
* Returns a byteArray of the selected tag's bytes
*
* @param tag The tag to retrieve
* @return The ByteArray containing the tag's bytes
*/
public function getTagData(tag:Tag):ByteArray
{
var ba:ByteArray = new ByteArray();
this.data.position = tag.position;
this.data.readBytes(ba, 0, tag.size);
this.data.position = 0;
ba.position = 0;
return ba;
}
/**
* @public
* Returns a string representing the indicated tag.
*
* @param tag The tag to inspect
* @param dumpFile Indicates whether the tag's binary data should be dumped
* @param fileName Indicates the fileName where the binary data will be stored
* @return A string representing the tag.
*/
public function printTag(tag:Tag,dumpFile:Boolean=false,fileName:String=""):String
{
var sw:StringWriter = new StringWriter();
var sPrinter:SwfxPrinter = new SwfxPrinter(sw);
var tagName:String;
var fName:String;
sPrinter.setDecoderDictionary(this.dict);
if (tag.type < 92) {
tagName = tagNames[tag.type];
} else {
tagName = "Obfuscated";
}
try {
//var classReference:Class = getDefinitionByName("decompiler.swfdump.tags." + tagName) as Class;
if (tag.longRecordHeader) {
data.position = tag.position + 6;
} else {
data.position = tag.position + 2;
}
if (tag.type == stagDoABC) {
sw.print("<DoABC>\r");
dumpDoABC(tag.size,sw, false, " ");
sw.print("</DoABC>\r");
} else if (tag.type == stagDoABC2) {
sw.print("<DoABC2>\r");
var bLength:uint = tag.size;
data.readInt();
sw.print(" abc name "+readString() + "\r");
bLength -= (data.position- tag.position - 6);
dumpDoABC(bLength, sw, false, " ");
sw.print("</DoABC2>\r");
} else if (tag.type == TagValues.stagEnd) {
sw.print("<end/>\r");
} else {
var params:Array = new Array();
if (tag.theTag == null) {
var tDecoder:TagDecoder = new TagDecoder(this);
tDecoder.setDictionary(this.dict);
tDecoder.setHandler(sPrinter);
params[0] = tag.size;
//var t:* = tDecoder["decode" + tagName].apply(tDecoder,params);
tag.theTag = tDecoder.decodeTag(tag.type, tag.size);
}
if (dumpFile) {
sPrinter.external = true;
sPrinter.externalPrefix = fileName;
}
/**
if (tag.theTag.SIErrorMessage != null && tag.theTag.SIErrorMessage.length > 0) {
sPrinter.error(tag.theTag.SIErrorMessage);
}
*/
if (tag.type == 74) {
//capitilization is different for this one tag
fName = "csmTextSettings";
} else {
fName = tagName.charAt(0).toLowerCase() + tagName.substring(1);
}
params[0] = tag.theTag;
sPrinter[fName].apply(sPrinter,params);
}
} catch (s:SWFFormatError) {
if (s.t == null) {
return ("");
} else {
sw.print("Error in parsing: " + s.message + "\r");
fName = tagName.charAt(0).toLowerCase() + tagName.substring(1);
params[0] = s.t;
sPrinter[fName].apply(sPrinter,params);
}
} catch (e:Error) {
//It is expected to fail since I don't have all the tags defined yet.
return ("");
}
return (sw.output);
}
/******
* SWF Reader section
* Functions below are dedicated to getting bytes from the byteArray
*
* @return The Pascal style string
*/
public function readLengthString():String //throws IOException
{
var length:int = readUI8();
//byte[] b = new byte[length];
var b:ByteArray = new ByteArray();
b.length = length;
readFully(b);
if (b.toString().charCodeAt(b.length -1) == 00) {
return (b.toString().substr(0,length-1));
}
return (b.toString());
/**
// [paul] Flash Authoring and the player null terminate the
// string, so ignore the last byte when constructing the String.
if (swfVersion >= 6)
{
return new String(b, 0, length - 1, "UTF8").intern();
}
else
{
// use platform encoding
return new String(b, 0, length - 1).intern();
}
*/
}
public function readString():String
{
var s:String = "";
var c:int;
while (c=data.readUnsignedByte()) {
s += String.fromCharCode(c);
offset++;
}
offset++;
return s;
}
public function syncBits():void
{
this.bitPos = 0;
}
private function decodeRect():void
{
syncBits();
//Mod by SWF Investigator
//var rect:Rect = new Rect();
this.rect = new Rect();
var nBits:int = readUBits(5)
this.rect.xMin = readSBits(nBits);
this.rect.xMax = readSBits(nBits);
this.rect.yMin = readSBits(nBits);
this.rect.yMax = readSBits(nBits);
//return this.rect;
return;
}
public function readSBits(numBits:int):int
{
if (numBits > 32)
throw new Error("Number of bits > 32");
var num:int = readUBits(numBits);
var shift:int = 32-numBits;
// sign extension
num = (num << shift) >> shift;
return num;
}
public function readBit():Boolean// throws IOException
{
return readUBits(1) != 0;
}
public function readUBits(numBits:int):uint
{
if (numBits == 0)
return 0
var bitsLeft:int = numBits;
var result:int = 0;
if (bitPos == 0) //no value in the buffer - read a byte
{
bitBuf = data.readUnsignedByte();
offset++;
bitPos = 8;
}
while (true)
{
var shift:int = bitsLeft - bitPos;
if (shift > 0)
{
// Consume the entire buffer
result |= bitBuf << shift;
bitsLeft -= bitPos;
// Get the next byte from the input stream
bitBuf = data.readUnsignedByte();
offset++;
bitPos = 8;
}
else
{
// Consume a portion of the buffer
result |= bitBuf >> -shift;
bitPos -= bitsLeft;
bitBuf &= 0xff >> (8 - bitPos); // mask off the consumed bits
// if (print) System.out.println(" read"+numBits+" " + result);
return result;
}
}
//Added by SWF Investigator
return (result);
}
/**
* Section stolen from SwfDecoder.java in Flex swfdump
*/
public function getOffset():uint
{
//return offset;
return data.position;
}
public function readFixed8():Number //throws IOException
{
var val:int = readUI16();
// FIXME: this doesn't consider sign of original 8.8 value
return (val / 256.0);
}
public function readUI8():int
{
if (data.position<data.length)
{
offset++;
return data.readUnsignedByte()&0xFF;
}
/**
else if (in)
{
offset++;
return data.readUnsignedByte();
}
*/
else
{
return -1;
}
}
public function readUI16():int
{
syncBits();
var i:int;
if (data.length-data.position >= 2)
{
i = data.readUnsignedByte() & 0xFF | (data.readUnsignedByte() & 0xFF) << 8;
//pos += 2;
offset += 2;
}
/**
else if (in != null)
{
i = super.read() | super.read()<<8;
offset += 2;
}*/
else
{
return -1;
}
return i;
}
public function readUI32():uint
{
var i:uint = data.readUnsignedInt() & 0xFFFFFFFF;
offset += 4;
return i;
}
public function readSI16():int
{
offset += 2;
return data.readShort();
}
public function readFully(b:ByteArray):void
{
var remain:int = b.length;
var off:int = 0;
var count:int;
while (remain > 0)
{
/**
count = read(b, off, remain);
if (count > 0)
{
off += count;
remain -= count;
}
else
{
//throw new SwfFormatException("couldn't read " + remain);
}
*/
data.readBytes(b,off,remain);
offset += remain;
remain = 0;
}
}
public function readSI32():int // throws IOException
{
syncBits();
var i:int;
if (data.length - data.position >= 4)
{
i = data.readUnsignedByte() & 0xFF | (data.readUnsignedByte() & 0xFF) << 8 | (data.readUnsignedByte() & 0xFF) << 16 | data.readUnsignedByte() << 24;
offset += 4;
//pos += 4;
}
else if (data.position < data.length)
{
//i = super.read() | super.read() << 8 | super.read() << 16 | super.read() << 24;
i = data.readUnsignedByte() | data.readUnsignedByte() << 8 | data.readUnsignedByte() << 16 | data.readUnsignedByte() << 24;
offset += 4;
}
else
{
i = -1;
}
return i;
}
//SI - Needs testing
public function readFloat():Number // throws IOException
{
var bits:Number = data.readFloat();
offset += 4;
return (bits);
//int bits = readSI32();
//return Float.intBitsToFloat( bits );
}
//SI - Needs testing
public function read64():Number //throws IOException
{
var ls:uint = readUI32() & 0xFFFFFFFF;
var hs:Number = readUI32();
hs = hs * 4294967296;
hs += ls;
//var n:Number = ((hs << 32) | ls);
return (hs);
}
public function read(ba:ByteArray):void {
for (var i:int = 0; i < ba.length; i++) {
ba[i] = readUI8();
}
}
//Added by SWF Investigator
//Logic taken from SWF file format spec
public function readEncodedU32():int
{
var result:uint = data.readUnsignedByte();
var temp:uint;
if (!(result & 0x00000080))
{
offset++;
return result;
}
temp = data.readUnsignedByte();
result = (result & 0x0000007f) | temp<<7;
if (!(result & 0x00004000))
{
offset += 2;
return result;
}
temp = data.readUnsignedByte();
result = (result & 0x00003fff) | temp<<14;
if (!(result & 0x00200000))
{
offset += 3;
return result;
}
temp = data.readUnsignedByte();
result = (result & 0x001fffff) | temp<<21;
if (!(result & 0x10000000))
{
offset += 4;
return result;
}
temp = data.readUnsignedByte();
result = (result & 0x0fffffff) | temp<<28;
offset += 5;
return result;
}
}
}
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.