swf文件头和tag结构解析

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];    //取数
        result:=longword(vvsam shl vbitpos) shr (32-nbits);                           //移位取值
        inc(vbitpos,nbits);                                                           //移位
        inc(vbyte,vbitpos div 8);                                                     //移字节 定新字节
        vbitpos:=vbitpos mod 8;                                                       //定新位
    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);               //已有8个字节读入buff中,不算在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文件头
_from:=vbyte+4;
    while (_from<=nBytesRead-1) do  begin
       _tlist:=listview1.Items.Add;            //生成对象,对象存在才能给其赋值
       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(_to));
       _from:=_to;
    end;
//----------------------------------------------------------------------Tag=length+Tag头(2或者6);
end;
posted @ 2009-04-17 15:56  苔苔以苔苔以苔  阅读(635)  评论(0编辑  收藏  举报
猪先飞