Using AS3 's ByteArray to parse the size of a SWF

Source: Internet
Author: User

AS3 's ByteArray can be used to manipulate binary. Using it, we get the size of the loaded SWF.

First, to understand the file structure of the SWF, you can download the official PDF to see.

Open a SWF with UltraEdit32, you will see that the first byte is 43 or 46, which is the 16 binary, the corresponding character is C or F, where c is compressed, and f means uncompressed.

The second byte and the third byte are fixed 57 53.

Then 1 bytes to indicate the flash version, such as 08, is Flash8, 0A is Flash10.

The next 4 bytes indicate the size of the Flash file (uncompressed size)

There is a need to pause for a moment. We know from the beginning that this file is compressed, and if it is already compressed, the byte after the file size needs to be decompressed, that is, using bytearray.uncompress (). If it is uncompressed, skip this step.

Next will be a RECT structure. The number of bytes it occupies is not fixed. It consists of 5 parts, the first one is Len, and the next 4 numbers are xmin,xmax,ymin,ymax. These 4 numbers occupy the same number of digits, all of which are Len.

Len is determined by the first 5 binary binary of the rect.

For example, if we see a rect starting with the binary is this 0F A0 00 00 .....

First take the first byte 70, the binary is 0111 0000, the first 5 bits is (01110) 2 = (14) 10, is 10 of 14, that is, the following 4 number Xmin,xmax,ymin,ymax each number accounted for 14 bits, so 14*4=56;

And the first byte has 3 bits left, 56-3=53. That is, in addition to the first byte, we also need Math.ceil (53/8) = 7 Bytes, why do we have to take up the whole? Because of the byte-aligned relationship. That is, you use 53 bit with 56 bit of space is the same, not enough 56 will be enough to fill 56.

OK, that's how it works.

Private Function Calc_size (Byte:bytearray): void {  var char:string = String.fromCharCode (Byte.readbyte ());//C or F Byte.readbyte (); W Byte.readbyte (); S Byte.readbyte (); Version Byte.readunsignedint (); File size, if (char = = "C") {var tmpbyte:bytearray = new ByteArray (); byte.readbytes (tmpbyte); byte = Tmpbyte;byte. Position = 0;byte.uncompress (); } var bit:uint = Byte.readunsignedbyte (); var bitlen:uint = bit >> 3; var bits:uint = Bitlen * 4;bits = Math.ceil ((bits-3)/8); var num:int = bit & 0x07; var readed:int = 3; var sizearr:array = []; var sizeid:int = 0; var ava:int = 0; var avanum:uint; var readcount:int = 0;  do {if (Ava = = 0) {Ava = 8;readcount ++; avanum = Byte.readunsignedbyte (); } &nbs P if (readed < Bitlen) {var offset:int = bitlen-readed; if (Offset > ava) {num = (num << ava) | avanum; r eaded + = Ava;ava = 0; } else {num = (num << offset) | (Avanum >> (Ava-offset)); avanum = Avanum -(Avanum >> (Ava-offset));  ava-= offset;readed + offset; }  } else {Sizearr[sizeid] = Num;si ZeId + +; if (Sizeid = = 4) {break;} num = 0;readed = 0; }}while (true); SRCW = sizearr[1]/20;srch = sizearr[3]/20; }

We can note that the final size has been divided by 20, because the number of units we have calculated is twip,1 pixel = twips. So dividing by 20 is the pixel we know.

Using AS3 's ByteArray to parse the size of a SWF

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.