asSWFbit Library - a ActionScript based SWF Decompiler / Compiler

ActionScript 3 / Last updating 06.12.2010

asSWFbit_Library is a ActionScript based SWF Decompiler / Compiler for SWF files.
The library should help you to check, test and manipulate SWF files in any way you wanted.
The idea behind asSWFbit is to make it possible to provide a way fix small errors in a SWF file without needing a flash editor or the FLA files.
I hope you understand you should this only do for your own SWF files !

Please keep in Mind that English is not my native language, so I try my best to make a clear documentation in English.
When you find errors or something like that, please send me a short email what I can change or which part is mistakable.

Content

Road map

This is only a small Road map for the asSWFbit Library. Because I do this in my free time I will make no promise for any time lines or any other ETA for parts of the project.

Read Support (planned Q1/2011)

  • SWF Header Information (Signature, Version, FileLength, Width, Height, FrameRate, FrameCount)
  • FileAttributes (UseDirectBlit, UseGPU, HasMetadata, ActionScript3, UseNetwork)
  • MetaData (CreatorTool, CreateDate, MetadataDate, ModifyDate, RAW)
  • BackgroundColor
  • ScriptLimits
  • ...
  • ActionScript Parser

Modify Support (planned Q3/2011)

  • SWF Header
  • FileAttributes
  • MetaData
  • ...
  • ActionScript

Write Support (planned Q4/2011)

  • SWF Header
  • FileAttributes
  • MetaData
  • ...
  • ActionScript

All items which are line-through are already implemented in the lasted version.
Other items are planned but not released in any version yet.

Manifestations about asSWFbit

The License for asSWFbit is the BSD License, the License for the examples / documentation is the Creative Commons (by-sa) License. Please also check http://code.google.com/p/asswfbit/ for more Information's.

Download from asSWFbit 0.2

asSWFbit is hosted on Google Code on this page you can download the lasted version of asSWFbit and submit errors and improvements.

Errors, Improvements and Support

First of all there is no direct Support for this project, please understand I do this in my free time and it takes a lot of time to write such a documentation.
So I have no time or the resources to answer direct support requests. Support requests to the contact email of the web page will be ignore and not answered.
When you find errors or have a idea of a improvement, please submit them over the Google Code web page http://code.google.com/p/asswfbit/.

Installation from asSWFbit Library

asSWFbit Library 0.2 is available as package, copy the corresponding package folder "de" to the same place as the FLA file.

Read Examples

This examples shows how you can load and parse a flash file with the asSWFbit library.
The FLAs for these examples are also include in the example folder of the asSWFbit library.

Read SWF Header Information

The asSWFbit library has the option only to parse and read the SWF Header of a flash file.
When you only need these Information you should include the switch for a faster parsing of the SWF file.
The SWF Header include the following Information:

  • Signature (FWS or CWS)
  • Flash Version
  • Uncompress file length
  • Frame size (Dimension)
  • Frame rate (Frames per seconds)
  • Frame Count (Number of Frames)
ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library("Headers"); // Set Mode to Header only (faster) /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information to text fields */ if (SWF_data.parse(file)) { txt_signature.text = SWF_data.infos.Header.Signature; txt_compress.text = SWF_data.infos.Header.Compress; txt_version.text = SWF_data.infos.Header.Version; txt_filelength.text = SWF_data.infos.Header.FileLength; txt_width.text = SWF_data.infos.Header.Width; txt_height.text = SWF_data.infos.Header.Height; txt_framerate.text = SWF_data.infos.Header.FrameRate; txt_framecount.text = SWF_data.infos.Header.FrameCount; }

In this example you see it is very easy to check for a valid SWF file and read out the SWF Header Information of this SWF file.
The Library can be used on any ByteArray which has loaded a SWF file, so you can also use the URLLoader or the FileReference Object to load a SWF file into a ByteArray.

Read FileAttributes

Beside the Header Information there are some more Information which can be read out for more Information.
FileAttributes are available for flash files of version 8 and above.
So when the flash version is 7 and below you will not see any FileAttributes.
The FileAttributes include the following information:

  • UseDirectBlit (Hardware Acceleration)
  • UseGPU (Graphic Card Acceleration)
  • HasMetadata (Metadata)
  • ActionScript3 (Define if the file use ActionScript 3)
  • UseNetwork (Local Sandbox Setting)
ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library(); /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information to text fields */ if (SWF_data.parse(file)) { ... if (SWF_data.infos.Header.Version >= 8) { // Exists only in flash files version 8 or higher txt_usedirectblit.text = SWF_data.infos.FileAttributes.UseDirectBlit; txt_usegpu.text = SWF_data.infos.FileAttributes.UseGPU; txt_hasmetadata.text = SWF_data.infos.FileAttributes.HasMetadata; txt_actionscript3.text = SWF_data.infos.FileAttributes.ActionScript3; txt_usenetwork.text = SWF_data.infos.FileAttributes.UseNetwork; } }

Here we use first the SWF Header Information to check if the flash file is version 8 or higher.
When yes we can read out and display the FileAttributes Information.

Read Metadata

The Metadata are sometime very helpfully. They can include a lot of Information. You can also define to include your own Information.
The asSWFbit Library parse some of the values automatically, but you have still access to the full raw xml information.
The most common / default information are the following:

  • CreatorTool
  • ModifyDate
  • CreateDate
  • Author / Creator
  • Description
ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library(); /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information to text fields */ if (SWF_data.parse(file)) { ... if (SWF_data.infos.Header.Version >= 8) { // Exists only in flash files version 8 or higher ... if (SWF_data.infos.FileAttributes.HasMetadata) { // FileAttributes tell us if there are Metadata txt_creatortool.text = SWF_data.infos.Metadata.Description.xmp.CreatorTool; txt_metadatadate.text = SWF_data.infos.Metadata.Description.xmp.MetadataDate; txt_modifydate.text = SWF_data.infos.Metadata.Description.xmp.ModifyDate; txt_createdate.text = SWF_data.infos.Metadata.Description.xmp.CreateDate; txt_creator.text = SWF_data.infos.Metadata.Description.dc.creator; txt_description.text = SWF_data.infos.Metadata.Description.dc.description; txt_title.text = SWF_data.infos.Metadata.Description.dc.title; txt_metadata.text = SWF_data.infos.Metadata.raw; } } }

Here you see that most of the xmp entry's and dc entry's are already parsed to a variable for a easy access.
The raw xml data are stored in "infos.Metedata.raw". Please keep in mind that this xml is working with namespace, so you need to defined namespace variables to access the different information in the raw data.

Read BackgroundColor and ScriptLimits

The BackgroundColor and ScriptLimits are sometime very intresting information. Please keep in mind that the SWF will only store ScriptLimits when there are different from the default Settings.

ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library(); /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information to text fields */ if (SWF_data.parse(file)) { ... if (SWF_data.infos.BackgroundColor) { txt_backgroundcolor.text = SWF_data.infos.BackgroundColor; } if (SWF_data.infos.ScriptLimits) { txt_maxrecursiondepth.text = SWF_data.infos.ScriptLimits.MaxRecursionDepth; txt_scripttimeoutseconds.text = SWF_data.infos.ScriptLimits.ScriptTimeoutSeconds; } }

The format of the BackgroundColor is in the RGB (Red Green Blue) Format: RRGGBB.

Read all TAGs

You can also read all TAGs of a SWF file to search for a special TAG or to get a general Overview over the SWF itself.
It is possible to access the raw content of each TAG, the parsed content is only available when the asSWFbit Library support the TAG.

ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library(); /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information to text fields */ if (SWF_data.parse(file)) { ... if (SWF_data.infos.TAGs) { txt_tags.text = "Found " + SWF_data.infos.TAGs.NumberOfTAGs + " TAGs...\n=========================================\n"; for (var id:uint = 0; id < SWF_data.infos.TAGs.NumberOfTAGs; id++) { if (SWF_data.infos.TAGs["ID" + id]) { txt_tags.appendText(id+'\t:\t' + SWF_data.infos.TAGs["ID" + id].name + " (size: " + SWF_data.infos.TAGs["ID" + id].size + ")\n"); } } } }

All TAGs from a SWF file are stored at infos.TAGs, so when this is existing then you can be sure that the SWF file has some TAGs inside.
Each TAG has a unique ID over which you can access additional information. For more Information about the structure of this, please look at the "TAGs" section.

Overview over asSWFbit Library

This is a detail overview, which show all functions, events and constructors of the asSWFbit Library.
Below this there are more details information for each function, event and constructor.

Overview over constructors

SyntaxDescription
asSWFbit_Library(Mode:String)Create a asSWFbit_Library-Object. With the mode you can disable or enable additional features.
"Headers" will only parse the Header Information and no additional TAGs or Information.

Overview over functions

SyntaxDescription
parse(_DATA_:ByteArray) : BooleanTry to load and parse the SWF file inside the ByteArray. Return true when there is a valid SWF file inside this ByteArray.

Overview over "infos" objects

The "infos" object is only available after parsing over the "parse" function and keep all Information about the SWF file.

PathDescription
infos.HeaderInclude all SWF Header information like Version, Filesize, Compress, Dimension
infos.FileAttributesInclude all the FileAttributes information like ActionScript3, Metadata.
infos.MetadataInclude all Metadata like Creator, CreatorTool, CreationDate.
infos.BackgroundColorInclude only the BackgroundColor in the RGB Format.
infos.ScriptLimitsInclude all ScriptLimits information like MaxRecursionDepth and ScriptTimeoutSeconds.
infos.TAGsInclude all TAGs from the SWF file in a special format.

Overview over "infos.TAGs" objects

The "infos.TAGs" object include all TAGs from the SWF file in this special format.

PathDescription
infos.TAGs.indexSmall search Index for the most commend SWF Information.
infos.TAGs.NumberOfTAGsInclude the Number of TAGs which was found in the SWF file.
infos.TAGs["ID..."]For each TAG there is a unique Object with a ID.
infos.TAGs["ID..."].typeInclude the Type ID of the TAG.
infos.TAGs["ID..."].nameInclude the name of the Type of the TAG.
infos.TAGs["ID..."].startStart Position of the TAG inside the DataArray
infos.TAGs["ID..."].endEnd Position of the TAG inside the DataArray
infos.TAGs["ID..."].sizeSize of the raw data, exclude type header
infos.TAGs["ID..."].posOverall Position of the TAG in the Object
infos.TAGs["ID..."].rawRaw data of the TAG, exclude type header
infos.TAGs["ID..."].contentParsed raw data, depend on the type which information are here.

Constructor

asSWFbit_Library(Mode:String)

This will create the asSWFbit Library object and assign it to a variable, this is needed to access this object later.
You can also pass a Mode, where you can define which information you need and which information you didn't need.

ActionScript Code:
/* asSWFbit Library Example from Markus Bordihn (http://markusbordihn.de) */ import de.markusbordihn.flash.as3.asSWFbit_Library; /* Example without any Mode (Default) */ var SWF_data = new asSWFbit_Library(); /* Example with Headers only Mode */ var SWF_data = new asSWFbit_Library("Headers");

In this example the variable "SWF_data" will be the asSWFbit Library Object.
You can pass modes such "Headers" to decompile only the header information of a SWF file, in the future other modes will be supported as well.

Method

parse(_DATA_:ByteArray) : Boolean

With the parse method you load and parse the SWF file into the Info Object.
The Method will return "true" when it found a valid SWF file, when the SWF file is invalid it will return "false".
So you can check of this method if you have loaded a valid SWF file and when yes, you can try to access the SWF information.

ActionScript Code:
/* Define and Load asSWFbit Library */ import de.markusbordihn.flash.as3.asSWFbit_Library; var SWF_data = new asSWFbit_Library(); /* Load SWF file into ByteArray */ var file:ByteArray = ...; // Could be any ByteArray which has load the SWF file /* Check for valid SWF file and parse Information */ if (SWF_data.parse(file)) { ... }

You should always check first, if you have a valid SWF file before you try to access the information.

Objects

The asSWFbit Library store all of the information into Objects so you can easy check if a object is existing and if it has any information stored.

infos.Header

The "Header" Object store all header information of the SWF file.

Structure:
infos.Header = { Signature: ... // Signature of the SWF file (CWS = compress / FWS = uncompress) Compress: ... // Indicate if this is a compress or uncompress SWF file Version: ... // Version of the flash file FileLength: ... // FileLength of the SWF (uncompress) file FrameSize: ... // Frame size in twips format { Xmin: ... // x minimum position Xmax: ... // x maximum position Ymin: ... // y minimum position Ymax: ... // y maximum position }, Width: ... // Calculated width of the Frame size in pixel Height: ... // Calculated height of the Frame size in pixel FrameRate: ... // Frames per second FrameCount: ... // Total number of frame }

The "Width" and the "Height" information are only calculated values of the "FrameSize" Object, there are not stored in the SWF file directly.

infos.FileAttributes

The "FileAttributes" Object store all file attributes information of the SWF file.
This Object is only available for SWF files equal or higher then version 8.

Structure:
infos.FileAttributes = { UseDirectBlit: ... // DirectBlit (Hardware Acceleration) will be used UseGPU: ... // GPU Acceleration will be used HasMetadata: ... // SWF file has Metadata ActionScript3: ... // SWF file use ActionScript 3 UseNetwork: ... // Local Sandbox can use Network }

The values of these field are "1" for "true" and "0" for false. Please keep in mind that Hardware Acceleration is only available for flash player 10 and above.

infos.Metadata

The "Metadata" are only available when the infos.FileAttributes.HasMetadata is true, which should be the case.

Structure:
infos.Metadata = { Description : { xmp : { CreatorTool: ... // The Tools which create this SWF file CreateDate: ... // Create date of this SWF file MetadataDate: ... // Create date of the Metadata ModifyDate: ... // Date of the last modification }, dc: { format: ... // Fileformat, should be only "application/x-shockwave-flash" description: ... // Description of the SWF file creator: ... // Name of the Creator of the SWF file title: ... // Title of the SWF file } }, raw: ... // Raw Metadata in XML Format }

The raw Metadata use namespaces to access the different variables, so you need to know how you can use such namespaces.

infos.BackgroundColor

Store the BackgroundColor of the SWF file in a RRGGBB format.

Structure:
infos.BackgroundColor = ... // Background Color in RRGGBB format

It could be that future versions are store the BackgroundColor in RRGGBB and split in R,G and B.

infos.ScriptLimits

Store the ScriptLimits Information of the SWF file.

Structure:
infos.ScriptLimits = { MaxRecursionDepth: ... // Maximum of recursion of the ActionScript ScriptTimeoutSeconds: ... // Display a warning message when the ActionScript is above this limit }

These Information are only available when the SWF file don't use the default values.

infos.TAGs

The infos.TAGs is a very important object, it store all TAGs from the SWF file in parse and raw format.
For each TAG there will be a unique identifier, so you can easy access any of these TAGs individual.

Structure:
infos.TAGs = { index: ... // Small Search Index for important information NumberOfTAGs: ... // Save Number of TAGs for a faster acce "ID[Position]": { ... // Example Entry for for a TAG each TAG has a unique ID type: ... // Tag Type (Number) name: ... // Parsed Tag Type Name (String) start: ... // Start Position of the DATA end: ... // End Position of the DATA size: ... // Size of the information (not include RECORDHEADER) pos: ... // General Position of all TAG raw: ... // Raw Content of the DATA content: ... // Parsed Information of the TAG when possible / available } }

You can always access the raw content of any TAG even when there is no parser implemented for this type of TAG.
The parsed Information depend on the Tag Type, so for e.g: in the "BackgroundColor" TAG you will only found the BackgroundColor information.
When you need to check all TAGs, you can create a loop which use the "NumberOfTAGs" information for the numbers of TAGs which needs to be checked.
In the Example section there is also a example of such a loop, to display all TAGs.