SWF file format (structure)

Source: Internet
Author: User

This is useful when you develop your own auxiliary tools.

The overall structure of the SWF file is composed of header + body.

The file starts with a [File Header]
Its structure is as follows:

[Copy to clipboard] [-] Code:
Byte Name Description
1 signature "F" indicates the non-encrypted format, and "C" indicates the encrypted format
1 signature "W" has no special significance
1 signature "S" has no special significance
1 version number, indicating the corresponding Player version
4 filelength: the length of the entire file.
N framesize rect struct, indicating the screen size. The specific structure and length are discussed based on data changes.
2 framerate frame rate. The default value is 12.
2 framecount: the number of frames at the root of the file.
The above is the simplest tag in SWF. A complete SWF file is composed of many independent tags. Each tag includes a header and a data body. The header has two types: Short tag and long tag.

The short tag is composed of 2 bytes. The first 10 bits represent the tag type, and the last 6 bits represent the tag length.

The long tag is composed of 6 bytes. The first 10 bits indicate the tag type. The last 6 bits are fixed to 1, and the last 4 bits represent the tag length.

The length of a tag is different from that of a filelength header. It is the length after the tag header is removed.

(In addition) Only unencrypted SWF files can be parsed directly using the above method. The encrypted files need to be processed in another step. The specific method involves copyright issues. I will not describe it here.

This document may be of little significance to the Flash design, but if someone is using it, I will continue to analyze it. If someone is also doing this investigation, please join me to discuss it.

Section 2nd

The preceding section describes that the SWF file consists of one head and one body.
The header structure is parsed, and a brief description of the header part of a tag is provided.

The rest below is the body of the SWF file.

The entire file body consists of a large number of tags. By analyzing the head part of the tag, you can immediately know the type code and length of the tag.

If you cannot identify the tag type, you can skip the tag directly using the tag length.

This method ensures the compatibility of the version. Even if a new tag is generated, the player of the old version can still parse the entire SWF file without making any error. A big deal is that it cannot provide new functions.

The following is an image concept of the SWF file structure.

[Copy to clipboard] [-] Code:
(File Header) (file body)
|
(TAG 1) (TAG 2) (TAG 3 )(......)
|
(TAG header) (TAG body)
|
(Tag type code) (tag length)
In this way, do you have a basic understanding of the structure of SWF files?

In the next section, I will analyze several necessary SWF tags, including backgroundcolor tag, showframe tag, and end tag.

Section 3rd
Setbackgroundcolor tag

This tag is the first tag directly following the head of the file, which must exist in the file.
Its structure is as follows:

[Copy to clipboard] [-] Code:
Length (BIT) Name Description
16 header tag header. The short tag type code is 9.
24 backgroundcolor RGB type, three bytes on the right, indicating red, green, and blue respectively
Showframe tag

This is the last 2nd tags of a file, which must exist.
The structure is as follows:

[Copy to clipboard] [-] Code:
Length (BIT) Name Description
16 header tag header. The short tag type code is 1.
End tag

End tag, which is the last tag of the file.

[Copy to clipboard] [-] Code:
Length (BIT) Name Description
16 header tag header. The short tag type code is 0.
In the next section, I will talk about the meaning of character ID and depth and the basic tag of textfield.
(Also) thanks to AOL for its supplement and explanation.

Section 4th
A textfield is a text box. There are three types of text boxes: static, dynamic, and input. As a tag, there are only two types of static and dynamic types. The input type is only a special form of dynamic.

The composition of dynamic text boxes is discussed here.
It consists of three tags, two defined tags and one control tag.
They are:
Define tag definefont2 defineedittext (check the Player version if Player 7.0 is different)
Control tag placeobject2

Definefont2 defines a font information. defineedittext references the defined font and defines the displayed text information. placeobject references the defined text information and controls the display of the text.

The reference between them is based on character ID.

Character ID is a number marker starting from 1. If there is a missing character in the middle, all the character IDs starting from the missing character are ignored and repeated, the tag that appears later will overwrite the tag that appears first.

Definefont2 uses a character ID to identify itself, and so does defineedittext and placeobject2. However, not all tags have character IDs.
In addition, although placeobject2 also has a character ID, it is not used to identify itself, but to call it.

The deep depth is only available in placeobject2 among the three tags.

This is the relationship between the three tags. The structure of the three tags will be described in the next section. (Sorry, I say a little bit every time !)

Section 5th

Definefont2
This tag defines a font or a set of static profile words for defineedittext.

Almost all text information can be set in this tag. Therefore, this is also a complicated tag.
Its structure is as follows:

[Copy to clipboard] [-] Code:
Length (BIT) Name Description
Headerlength header tag header, type code: 48
16 fontid character ID, Unique Identifier
1 fontflagshaslayout determines whether there is a variant mark based on the literal explanation.
1 fontflagsshiftjis encoded using shiftjis
1 fontflagssmalltext whether to display in small font
1 fontflagsansi whether ANSI encoding is used
1 fontflagswideoffsets whether to use a 32-bit offset
1 fontflagswidecodes: whether to use 16-bit Text Encoding
1. Whether the fontflagsitalic text is Italic
1. Whether the fontflagsbold text is bold
8. The languagecode language has the corresponding encoding table.
8 fontnamelen File Name Length
Fontnamelen * 8 fontname file name (UTF-8 encoded)
16 numglyphs contour word count
32/16 offsettable 32-bit based on fontflagswideoffsets; otherwise, 16-bit
32/16 codetableoffset same as above
* Numglyphs glyphshapetable profile information, which is a shape structure (a complex structure)
16/8 codetable is 16 bits based on fontflagswidecodes. Encoding table, which is a fixed-value UCS-2
16/0 fontascent: Based on fontflagshaslayout, the value is 16 bits. Otherwise, this field does not exist.
16/0 fontdescent: Based on fontflagshaslayout, the value is 16 bits; otherwise, this field does not exist.
16/0 fontleading: Based on fontflagshaslayout, the value is 16 bits. Otherwise, this field does not exist.
16/0 * numglyphs fontadvancetable: Based on fontflagshaslayout, the value is 16 bits. Otherwise, this field is unavailable.
Rect * numglyphs fontboundstable: Based on fontflagshaslayout, it is 16 bits; otherwise, this field does not exist.
16/0 kerningcount: Based on fontflagshaslayout, the value is 16 bits. Otherwise, this field does not exist.
Kerningrecord * kerningcount
Fontkerningtable is 16 bits based on fontflagshaslayout. Otherwise, this field is not found.
If you carefully read the above content, I admire you.

In fact, if you simply analyze the tag information of the dynamic text, you only need to analyze the fontname part above. Other information is only valid for the profile word, that is, static text.

As you can see, we should all understand that dynamic text only has one name, while static text contains its profile information (including its shape ).
This is the biggest difference between dynamic text and static text.

Next section describes the next tag defineedittext

This is not clear about the rect structure. It is a type of flash storage structure.

Attribute type description
Nbits = UB [5] number of digits of each attribute value in the rect Structure
Minimum value of xmin Sb [nbits] X axis
Xmax Sb [nbits] maximum value on the X axis
Minimum value of ymin Sb [nbits] Y axis
Ymax Sb [nbits] maximum value in the Y axis

Here, a length unit in the SWF file is involved: twip. 1 twip, that is, 1/20 of a pixel.

Nbits is generally the number of digits equal to the maximum attribute value. For example, a video has this attribute:

Attribute decimal binary
Xmin = 127 decimal = 1111111 binary
Xmax = 260 decimal = 10000100 binary
Ymin = 15 decimal = 1111 binary
Ymax = 514 decimal = 1000000010 binary

So the maximum attribute value is Ymax. It is 514, it has ten digits, and a sign with positive and negative numbers is eleven digits. So nbits should be 11.

At this time:
Attribute decimal binary
Nbits = 11 decimal = 1011
Xmin = 127 decimal = 00001111111 binary
Xmax = 260 decimal = 00010000100 binary
Ymin = 15 decimal = 00000001111 binary
Ymax = 514 decimal = 01000000010 binary

Read from Java:

// Readubits is the function for reading unsigned bit.
// Readsbits is a function used to read signed bit.
Public rectangle2d readrect ()
Throws ioexception {

Bytealign ();
Int nbits = (INT) readubits (5 );
Int xmin = (INT) readsbits (nbits );
Int xmax = (INT) readsbits (nbits );
Int ymin = (INT) readsbits (nbits );
Int Ymax = (INT) readsbits (nbits );
Return new rectangle2d. Double (xmin/twips, ymin/twips, (xmax-xmin)/twips, (Ymax-ymin)/twips );
}

Let me talk about it. You can add more content together!

This is the format of the SWF file. The SWF File Header occupies 21 bytes, And the endtag occupies three bytes. each of the other tags has a unified structure, and they are independent of each other. data of the tag cannot be accessed from this tag. the internal data reading method of each tag is based on the data offset. because it is independent, you can use a tool to modify, add, and delete tags in the SWF file.

// Add a tag for Java
Final tagcollection Tc = new tagcollection ();
TC. Add (tag1 );
TC. Add (tag2 );
...
Final tagenumeration TE = tc. Close ();

Repeated sansunzw's:
There are two types of tags: defined tag and control tag.
The length of a tag can also be two types: long tag and short tag.]
Tags generally start with the tag header.
The short-type tag header has sixteen bits, and the high ten is the tag type. The low six bits define the length of the tag. because it is 6-bit, the short tag can only be 2 ^ 6 characters at most. The size of SwF cannot exceed 62 bytes.
The long-type tag header has 48 bits, and the 16-bit tag type is defined + the 32-bit tag length is defined. therefore, the maximum length of a long tag can be 2 ^ 32 = 4G bytes. this is more than the order of magnitude we usually use.

// This Java class about the SWF tag Header
// A lot of related code is not listed
Public class tagheader {

Int tagid;
Long length;

Public tagheader (INT tagid, long length ){
This. tagid = tagid;
This. Length = length;
}

Public void settag (INT tagid ){
This. tagid = tagid;
}

Public int gettag (){
Return tagid;
}

Public void setlength (long length ){
This. Length = length;
}

Public long getlength (){
Return length;
}
}

Definition and control tags are two types of SWF file tags. The content of the SWF video defined by the definition tag, such as the shape, text, bitmap, and sound. Each definition tag is assigned

A unique identifier ID is called the role ID. Flash Player puts these roles in a bucket, which is generally called its dictionary. Defining tags will not draw any image, and will not produce any animation.

Because these tasks are all done by the control tag. The control tag is used to retrieve the role from the dictionary, draw and exercise the role, and control the entire video process.

How are these tags sorted and stored? In general, tags can be sorted in any case, but they are not randomly arranged. It follows some rules.

1. A tag can only rely on the tag before it, not the tag after it.
2. A Role-defined tag must be referenced before the role-defined control tag.
3. the streaming media tag must be in sequence. Streaming Media Playback without sequence is also in no order.
4. The tag should be at the end of the SWF file.

(To be continued)

A swf file is composed of a file header and a file body, both of which are not necessarily long, the file header defines the version, compression, file size, scenario size, frame rate, and total number of frames of the SWF file. The content is not plaintext, the data is stored in numbers. The first table provided by the landlord details the information about the content, the first word 'C' indicates whether the SWF file is compressed. When you use a version later than flash6, the public setting will contain "compressed video, all SWF files start with 'C. A compressed video is all the content, including the file body, after the filelength part in the file header, processed by the standard compression algorithm. The algorithm is zlib. The advantage of compression is that SWF files become smaller.
The following is an example:
46 57 53 05 af 00 00 00 78 00 05 5f 00 00 0f A0
00 00 0C 01 00 43 02 FF 00 06 3f 03 15 00 00 00

46 57 53 is the ASC code of 'fws ', which is displayed in hexadecimal notation.
05 indicates that the SWF is Flash 5 compatible format. If it is 0C, it indicates that the SWF is flash 12 format. Hey, I don't know what the flash 12 will look like in the future.
AF 00 00 is the storage method of the hexadecimal number 'af'. Converting af to the 10th is 175, which is the total length of the SWF file.
78 00 05 5f 00 00 0f A0 00 indicates the scene size. The length is not fixed. 9 bytes are occupied here. Why are 9 bytes occupied? You can refer to the rect structure instructions, I will not introduce it here.
00 0C indicates that the frame rate is 12, and C indicates 12. No one has any objection.
01 00 indicates that the SWF file has one frame in total.
The file header ends here. The above structure is required and cannot be any less.
43 02 FF is a "Block". Here, the file body is composed of a large number of blocks. These blocks are independent of each other and work together with the display animation, for example, some blocks represent a rectangle, some blocks store a piece of music, and some blocks represent a frame. Blocks, which constitute the file body, but each block can be left out. The five bytes represent the base color, the red is FF, the green is ff, and the blue is ff, it is white. The block, not strictly speaking, is the tag mentioned by the landlord. Blocks are stacked together. There is no separator between the two blocks, because the length of each block is defined. For example, 43 02 indicates that the block occupies 3 bytes, add 43 02, which is five bytes, so the following: 00 06 is not the content of this block. Why does 43 02 indicate that this block represents the base color and occupies 3 bytes? Ah, it's a problem. It's about the file body. I will introduce it later.
The last one is introduced. 00 06 indicates that this file is protected and cannot be imported into other Flash files. This is also a block. Hey, the smallest block is composed of 2 bytes. Removing it will not be protected, it seems nonsense. Haha, don't forget to change the file length to 2. If you have a password, it is not 00 06.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.