Parsing SWF file header and tag Structure

Source: Internet
Author: User
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, comctrls;
Type
Tform1 = Class (tform)
Edit1: tedit;
Button1: tbutton;
Opendialog1: topendialog;
Edit2: tedit;
Edit3: tedit;
Edit4: tedit;
Edit5: tedit;
Edit6: tedit;
Edit7: tedit;
Edit8: tedit;
Listview1: tlistview;
Button2: tbutton;
Edit9: tedit;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Type tbuff = record
F: byte;
W: byte;
S: byte;
Version: byte;
File_length: Cardinal;
End;
VaR Buff: tbuff;
DT: array of byte;
Procedure tform1.button1click (Sender: tobject );
VaR
Filename: string;
Fileid: Cardinal;
Fileinfo: ofstruct;
Nbits: integer;
Vbitpos: integer;
Vbyte: integer;
Nbytesread: Cardinal;
Xmin, xmax, ymin, Ymax: longword;
ID, length, _ from, _ to: word;
_ Tlist: tlistitem;
Function getbitval: longword;
VaR vvsam: longword;
Begin
Vvsam: = dt [vbyte] * $1000000 + dt [vbyte + 1] * $10000 + dt [vbyte + 2] * $100 + dt [vbyte + 3]; // fetch number
Result: = longword (vvsam SHL vbitpos) SHR (32-nbits); // shift value
INC (vbitpos, nbits); // shift
INC (vbyte, vbitpos Div 8); // shift byte to new byte
Vbitpos: = vbitpos mod 8; // set a new bit
End;
Begin
If opendialog1.execute then
Filename: = opendialog1.filename;
Fileid: = openfile (pchar (filename), fileinfo, of_read );
Readfile (fileid, buff, 8, nbytesread, nil );
Setlength (DT, Buff. file_length );
Readfile (fileid, DT [0], Buff. file_length, nbytesread, nil); // eight bytes have been read into the buff, not included in DT
Setlength (DT, nbytesread );
Edit1.text: = 'f = '+ inttostr (buff. F );
Edit2.text: = 'W = '+ inttostr (buff. W );
Edit3.text: ='s = '+ inttostr (buff. s );
Edit4.text: = 'version = '+ inttostr (buff. version );
Edit5.text: = 'file _ length = '+ inttostr (buff. file_length );
Nbits: = byte (dt [0] SHR 3 );
Edit8.text: = 'nbits = '+ inttostr (nbits );
Vbyte: = 0;
Vbitpos: = 5;
Xmin: = getbitval;
Xmax: = getbitval Div 20;
Ymin: = getbitval;
Ymax: = getbitval Div 20;
Edit9.text: = ('xmin = '+ inttostr (xmin) +', '+
'Xmax = '+ inttostr (xmax) +', '+
'Ymin = '+ inttostr (ymin) +', '+
'Ymax = '+ inttostr (Ymax ));
If vbitpos> 0 then Inc (vbyte );
Edit6.text: = 'rate = '+ inttostr (dt [vbyte + 1]);
Edit7.text: = 'count = '+ inttostr (Word (@ dt [vbyte + 2]) ^ ));
// ------------------------------------------------------------------------- SWF File Header
_ From: = vbyte + 4;
While (_ from <= nBytesRead-1) Do begin
_ Tlist: = listview1.items. Add; // generate an object. Only objects that exist can be assigned a value.
ID: = word (@ dt [_ from]) ^ );
Length: = ID and $ 3f;
If length = $ 3f then length: = longword (@ dt [_ from + 2]) ^) + 4;
INC (length, 2 );
ID: = id shr 6;
_ To: = _ from + length;

_ Tlist. Caption: = inttostr (ID );
_ Tlist. subitems. Add (inttostr (length ));
_ Tlist. subitems. Add (inttostr (_ from ));
_ Tlist. subitems. Add (inttostr (_ ));
_ From: = _;
End;
// ------------------------------------------------------------------------ Tag = Length + tag header (2 or 6 );
End;

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.