As I said in my previous blog about “How to play a swf file” yesterday, I am making a small tool to convert .swf file to an .exe file. In this tool I will wrap the .swf file in a dialog and play it. So it leads to a small problem that the width and height of dialog and ocx control containing .swf file should be adjusted to show the file correctly. To achieve this requirement I need some info about the width and height of swf which can be derived directly from .swf file. After some searching with Google for a while to look for a available source code helping me read info with c#, I decide to write own for myself because I realize it is not difficult to write my own and the other reason is the source code I found, does not work correctly.
First step, I need the SWF Specification Format which can be downloaded from adobe’s site. Just a moment going through the document I found what I need. That is the SWF Header.
All detailed information you can read more in the documentation of adobe. I just post here some important information which I really need during programming.
1. An FWS signature indicates an uncompressed SWF file; CWS indicates that the entire file after the first 8 bytes (that is, after the FileLength field) was compressed by using the ZLIB open standard. <– I did not read carefully at this point. I think it will compress data after the header not inclusive the header and as consequence I lost half hour to find out the problem.
2. In the SWF format, a twip is 1/20th of a logical pixel. A logical pixel is the same as a screen pixel when the file is played at 100%—that is, without scaling. <– Nothing special here, I just do not know what a twip is :).
3. 8.8 signed fixed-point numbers. The high 8 bits represent the number before the decimal point, and the low 8 bits represent the number after the decimal point <– Are you sure that you understand this statement? I am not. An example must be better and here is it. For example: The real value 7.5 is equivalent to: 0x07.0x80.This value is stored in the SWF file as: 80 07. Still not understand. Change them to binary and you’ll understand it.
I think I discussed all important information about SWF Header. And at last, a source code as result which is SWFFileHeader. It is a .cs file (not a .dll) so that you can use it anywhere you want, for example in Silverlight.
To read the version of ActionScript which used in swf file we need to dig deeper into the SWF File structure. As shown in documentation of Adobe, the SWF File structure looks like below
As you can see, after the file header is the “File Attribute Tag” which contains a property indicating the version of ActionScript in that SWF file
It’s easy to edit the code of our class to read this property. One thing we should know that the type RECORDHEADER for tag “File Attribute” is the short one. That means its size is 2 bytes therefore we just need to read 2 bytes to bypass this struct. Then extract from the next byte the value of ActionScript version
int nTemp = strmReader.ReadByte(); nTemp = strmReader.ReadByte(); nTemp = strmReader.ReadByte(); ActionScript3 = ((nTemp & 8 ) == 0)?false:true;
Now through file attributes we can only define the ActionScript is version 3.0 or version 1.0/2.0. The complete source code of this blog can be downloaded as “Read SWF Header”
Mirror links for SWF specification format
Mirror 1: http://www.mediafire.com/view/iiowu0eg9gx3auo/swf-file-format-spec.pdf
Mirror 2: https://app.box.com/s/9vo2s1oa23hkjdd0ms7z
Mirror 3: https://mega.co.nz/#!2kR1laIC!k4sKKnOlpDNpubfsFCLWDbgU2iFWOjA2d3RgxyJsywg