上文把文件头解析完成了,接下来就是解析标签
swf文件把所有的资源都打包在标签内部
如字体,位图,嵌入的2进制,代码等等

tag有两种类型,1种为短tag,1种为长tag
短tag的长度是小于63个字节的

短标签格式
RECORDHEADER (short)
Field               Type    Comment
TagCodeAndLength    UI16    Upper 10 bits: tag type Lower 6 bits: tag length
高10位表示标签类型,低6位表示标签长度

长标签格式 长标签的TagCodeAndLength为0x3F (=63)
长标签使用Length来表示标签的长度
RECORDHEADER (long)
Field               Type    Comment
TagCodeAndLength    UI16    Tag type and length of 0x3F Packed together as in short header
Length              SI32    Length of tag

跟着标签头后面就是标签指定长度的数据
然后根据不同的标签类型来解析数据就可以了

每个tag读取结束后,后面的又重新字节对齐了,不用考虑剩余几个字节的问题

/**
* 读取tag类型,不移动position 
* @param bytes
* @return 
*/
private function readTagType(bytes:ByteArray):uint
{
    var result:uint = bytes.readUnsignedShort();
    result = result >> 6;
    
    bytes.position -= 2;
    
    return result;
}

/**
* 读取tag长度 不包括头本身占的长度 
* @param bytes
* @return 

*/     
private function readTagLength(bytes:ByteArray):uint{
    var tagLength:uint = (bytes.readUnsignedShort() & 0x3F);
    if (tagLength == 0x3F){
        tagLength = bytes.readUnsignedInt();
    };
    if (tagLength < 0){
        throw (Error("SWFReader:无效的tag长度"));
    };
    
    return tagLength;
}

/**
* 读取tag列表 
* @param bytes

*/     
private function readTags(bytes:ByteArray):Array
{
    var result:Array = [];
    
    var tagType:uint;
    var tagLength:uint;
    var start:int;
    var tag:Tag;
    var cl:Class;
    
    while(bytes.bytesAvailable > 0)
    {
        start = bytes.position;
        tagType = readTagType(bytes);
        tagLength = readTagLength(bytes);
        
        cl = TagTypes.getTagClassByTagType(tagType);
        if (cl == null)
        {
            trace("Tag Not Found, Type=" + tagType, tagLength);
            bytes.position += tagLength;// 跳过长度
            continue;
        }
        tag = new cl();
        tag.tagType = tagType;
        if( tagLength > 0)
            bytes.readBytes(tag.data, 0, tagLength);
        
        tag.data.endian = Endian.LITTLE_ENDIAN;
        tag.data.position = 0;
        
        // 调用解析方法
        tag.parse();
        
        result.push(tag);
        
        if (tagType == TagTypes.TAG_END)
            break;
    }
    
    return result;
}

// 所有的tag类型列表
package swf.file.tags
{
    import com.the9.nbu.swf.file.tags.actions.*;
    import com.the9.nbu.swf.file.tags.binary.*;
    import com.the9.nbu.swf.file.tags.bitmaps.*;
    import com.the9.nbu.swf.file.tags.buttons.*;
    import com.the9.nbu.swf.file.tags.control.*;
    import com.the9.nbu.swf.file.tags.displaylist.*;
    import com.the9.nbu.swf.file.tags.font.*;
    import com.the9.nbu.swf.file.tags.shapemorphing.*;
    import com.the9.nbu.swf.file.tags.shapes.*;
    import com.the9.nbu.swf.file.tags.sounds.*;
    import com.the9.nbu.swf.file.tags.sprite.*;
    import com.the9.nbu.swf.file.tags.text.*;
    import com.the9.nbu.swf.file.tags.video.*;
    import com.the9.nbu.swf.file.tags.unknown.*;
    
    /**
     * TagTypes
     * @author 任冬 
     * $Id: TagTypes.as 499 2010-11-19 10:02:56Z rendong $
     * @version 1.0
     */
    public class TagTypes
    {
        // Flash 1 tags
        public static const TAG_END:uint = 0;
        public static const TAG_SHOWFRAME:uint = 1;
        public static const TAG_DEFINESHAPE:uint = 2;
        public static const TAG_FREECHARACTER:uint = 3;
        public static const TAG_PLACEOBJECT:uint = 4;
        public static const TAG_REMOVEOBJECT:uint = 5;
        public static const TAG_DEFINEBITS:uint = 6;
        public static const TAG_DEFINEBUTTON:uint = 7;
        public static const TAG_JPEGTABLES:uint = 8;
        public static const TAG_SETBACKGROUNDCOLOR:uint = 9;
        public static const TAG_DEFINEFONT:uint = 10;
        public static const TAG_DEFINETEXT:uint = 11;
        public static const TAG_DOACTION:uint = 12;
        public static const TAG_DEFINEFONTINFO:uint = 13;
        public static const TAG_DEFINESOUND:uint = 14;
        public static const TAG_STARTSOUND:uint = 15;
        public static const TAG_STOPSOUND:uint = 16;
        public static const TAG_DEFINEBUTTONSOUND:uint = 17;
        public static const TAG_SOUNDSTREAMHEAD:uint = 18;
        public static const TAG_SOUNDSTREAMBLOCK:uint = 19;
        // Flash 2 tags
        public static const TAG_DEFINEBITSLOSSLESS:uint = 20;
        public static const TAG_DEFINEBITSJPEG2:uint = 21;
        public static const TAG_DEFINESHAPE2:uint = 22;
        public static const TAG_DEFINEBUTTONCXFORM:uint = 23;
        public static const TAG_PROTECT:uint = 24;
        public static const TAG_PATHSAREPOSTSCRIPT:uint = 25;
        // Flash 3 tags
        public static const TAG_PLACEOBJECT2:uint = 26;
        
        public static const TAG_REMOVEOBJECT2:uint = 28;
        public static const TAG_SYNCFRAME:uint = 29;
        
        public static const TAG_FREEALL:uint = 31;
        public static const TAG_DEFINESHAPE3:uint = 32;
        public static const TAG_DEFINETEXT2:uint = 33;
        public static const TAG_DEFINEBUTTON2:uint = 34;
        public static const TAG_DEFINEBITSJPEG3:uint = 35;
        public static const TAG_DEFINEBITSLOSSLESS2:uint = 36;
        // Flash 4 tags
        public static const TAG_DEFINEEDITTEXT:uint = 37;
        public static const TAG_DEFINEVIDEO:uint = 38;
        public static const TAG_DEFINESPRITE:uint = 39;
        public static const TAG_NAMECHARACTER:uint = 40;
        public static const TAG_PRODUCTINFO:uint = 41;
        public static const TAG_DEFINETEXTFORMAT:uint = 42;
        public static const TAG_FRAMELABEL:uint = 43;
        // Flash 5 tags
        public static const TAG_DEFINEBEHAVIOR:uint = 44;
        public static const TAG_SOUNDSTREAMHEAD2:uint = 45;
        public static const TAG_DEFINEMORPHSHAPE:uint = 46;
        public static const TAG_FRAMETAG:uint = 47;
        public static const TAG_DEFINEFONT2:uint = 48;
        public static const TAG_GENCOMMAND:uint = 49;
        public static const TAG_DEFINECOMMANDOBJ:uint = 50;
        public static const TAG_CHARACTERSET:uint = 51;
        public static const TAG_FONTREF:uint = 52;
        public static const TAG_DEFINEFUNCTION:uint = 53;
        public static const TAG_PLACEFUNCTION:uint = 54;
        public static const TAG_GENTAGOBJECT:uint = 55;
        public static const TAG_EXPORTASSETS:uint = 56;
        public static const TAG_IMPORTASSETS:uint = 57;
        public static const TAG_ENABLEDEBUGGER:uint = 58;
        // Flash 6 tags
        public static const TAG_DOINITACTION:uint = 59;
        public static const TAG_DEFINEVIDEOSTREAM:uint = 60;
        public static const TAG_VIDEOFRAME:uint = 61;
        public static const TAG_DEFINEFONTINFO2:uint = 62;
        public static const TAG_DEBUGID:uint = 63;
        public static const TAG_ENABLEDEBUGGER2:uint = 64;
        public static const TAG_SCRIPTLIMITS:uint = 65;
        // Flash 7 tags
        public static const TAG_SETTABINDEX:uint = 66;
        // Flash 8 tags
        
        
        public static const TAG_FILEATTRIBUTES:uint = 69;
        public static const TAG_PLACEOBJECT3:uint = 70;
        public static const TAG_IMPORTASSETS2:uint = 71;
        public static const TAG_DOABC:uint = 72;
        public static const TAG_DEFINEFONTALIGNZONES:uint = 73;
        public static const TAG_CSMTEXTSETTINGS:uint = 74;
        public static const TAG_DEFINEFONT3:uint = 75;
        public static const TAG_SYMBOLCLASS:uint = 76;
        public static const TAG_METADATA:uint = 77;
        public static const TAG_SCALINGGRID:uint = 78;
        
        
        
        public static const TAG_DOABC2:uint = 82;
        public static const TAG_DEFINESHAPE4:uint = 83;
        public static const TAG_DEFINEMORPHSHAPE2:uint = 84;
        
        // Flash 9 tags
        public static const TAG_DEFINESCENEANDFRAMELABELDATA:uint = 86;
        public static const TAG_DEFINEBINARYDATA:uint = 87;
        public static const TAG_DEFINEFONTNAME:uint = 88;
        public static const TAG_STARTSOUND2:uint = 89;
        public static const TAG_DEFINEBITSJPEG4:uint = 90;
        // Flash 10 tags
        public static const TAG_DEFINEFONT4:uint = 91;

/**
         * tagType => tag 名称 
         */     
        private static const TAG_NAMES:Array =["End","ShowFrame","DefineShape","FreeCharacter","PlaceObject","RemoveObject","DefineBits","DefineButton","JPEGTables","SetBackgroundColor","DefineFont","DefineText","DoAction","DefineFontInfo","DefineSound","StartSound","StopSound","DefineButtonSound","SoundStreamHead","SoundStreamBlock","DefineBitsLossless","DefineBitsJPEG2","DefineShape2","DefineButtonCxform","Protect","PathsArePostScript","PlaceObject2","27 (invalid)","RemoveObject2","SyncFrame","30 (invalid)","FreeAll","DefineShape3","DefineText2","DefineButton2","DefineBitsJPEG3","DefineBitsLossless2","DefineEditText","DefineVideo","DefineSprite","NameCharacter","ProductInfo","DefineTextFormat","FrameLabel","DefineBehavior","SoundStreamHead2","DefineMorphShape","FrameTag","DefineFont2","GenCommand","DefineCommandObj","CharacterSet","FontRef","DefineFunction","PlaceFunction","GenTagObject","ExportAssets","ImportAssets","EnableDebugger","DoInitAction","DefineVideoStream","VideoFrame","DefineFontInfo2","DebugID","EnableDebugger2","ScriptLimits","SetTabIndex","67 (invalid)","68 (invalid)","FileAttributes","PlaceObject3","ImportAssets2","DoABC","DefineFontAlignZones","CSMTextSettings","DefineFont3","SymbolClass","Metadata","ScalingGrid","79 (invalid)","80 (invalid)","81 (invalid)","DoABC2","DefineShape4","DefineMorphShape2","85 (invalid)","DefineSceneAndFrameLabelData","DefineBinaryData","DefineFontName","StartSound2","DefineBitsJPEG4","DefineFont4"];

/**
         * tagType => tag class
         */     
        private static const TAG_CLASS:Array = [EndTag,ShowFrameTag,DefineShapeTag,FreeCharacterTag,PlaceObjectTag,RemoveObjectTag,DefineBitsTag,DefineButtonTag,JPEGTablesTag,SetBackgroundColorTag,DefineFontTag,DefineTextTag,DoActionTag,DefineFontInfoTag,DefineSoundTag,StartSoundTag,StopSoundTag,DefineButtonSoundTag,SoundStreamHeadTag,SoundStreamBlockTag,DefineBitsLosslessTag,DefineBitsJPEG2Tag,DefineShape2Tag,DefineButtonCxformTag,ProtectTag,PathsArePostScriptTag,PlaceObject2Tag,null,RemoveObject2Tag,SyncFrameTag,null,FreeAllTag,DefineShape3Tag,DefineText2Tag,DefineButton2Tag,DefineBitsJPEG3Tag,DefineBitsLossless2Tag,DefineEditTextTag,DefineVideoTag,DefineSpriteTag,NameCharacterTag,ProductInfoTag,DefineTextFormatTag,FrameLabelTag,DefineBehaviorTag,SoundStreamHead2Tag,DefineMorphShapeTag,FrameTagTag,DefineFont2Tag,GenCommandTag,DefineCommandObjTag,CharacterSetTag,FontRefTag,DefineFunctionTag,PlaceFunctionTag,GenTagObjectTag,ExportAssetsTag,ImportAssetsTag,EnableDebuggerTag,DoInitActionTag,DefineVideoStreamTag,VideoFrameTag,DefineFontInfo2Tag,DebugIDTag,EnableDebugger2Tag,ScriptLimitsTag,SetTabIndexTag,null,null,FileAttributesTag,PlaceObject3Tag,ImportAssets2Tag,DoABCTag,DefineFontAlignZonesTag,CSMTextSettingsTag,DefineFont3Tag,SymbolClassTag,MetadataTag,ScalingGridTag,null,null,null,DoABCTag,DefineShape4Tag,DefineMorphShape2Tag,null,DefineSceneAndFrameLabelDataTag,DefineBinaryDataTag,DefineFontNameTag,StartSound2Tag,DefineBitsJPEG4Tag,DefineFont4Tag];
        
        
        /**
         * 根据编号获取tag名称 
         * @param tagType
         * @return 
         * 
         */     
        public static function getTagNameByTagType(tagType:int):String
        {
            return TAG_NAMES[tagType] || null;
        }
        
        /**
         * 根据变化获取tag class 
         * @param tagType
         * @return 
         * 
         */     
        public static function getTagClassByTagType(tagType:int):Class
        {
            return TAG_CLASS[tagType] || null;
        }
    }
}

‍swf文件格式解析入门(tag解析)相关推荐

  1. FLV科普12 FLV脚本数据解析-Metadata Tag解析

    AMF(Action Message Format)是Flash与服务端通信的一种常见的二进制编码模式,其传输效率高,可以在HTTP层面上传输.现在很多Flash WebGame都采用这样的消息格式. ...

  2. swf文件格式解析入门(文件头解析)

    这里是使用as3语言来对swf文件做解析,其它语言可以参考. 一,准备工作 从Adobe官网下载一份swf文件格式说明文档 http://www.adobe.com/content/dam/Adobe ...

  3. swf文件格式解析(二)

    原文地址:swf文件格式解析(二)作者:爱与痛的边缘 上一篇教程可能写的有点乱 ,本回合开始之前先做一个概述吧,引用官方白皮书的原文 概述 SWF 文件是由一个文件头,和跟在后面的一系列的标签组成.标 ...

  4. Office 文件解析入门

    Office 文件解析入门 概述 - Office 解析相关协议 这里所讲的 Office 文件指的是微软 Office2007 及以后的 PPT/EXCEL/WORD 等文件格式,因为 Office ...

  5. Python3网络爬虫快速入门实战解析(一小时入门 Python 3 网络爬虫)

    Python3网络爬虫快速入门实战解析(一小时入门 Python 3 网络爬虫) https://blog.csdn.net/u012662731/article/details/78537432 出 ...

  6. SoapUI简介和入门实例解析

    SoapUI简介 SoapUI是一个开源测试工具,通过soap/http来检查.调用.实现Web Service的功能/负载/符合性测试.该工具既可作为一个单独的测试软件使用,也可利用插件集成到Ecl ...

  7. (2) 用java实现一个简易编译器1-词法解析入门

    转载地址 : http://blog.csdn.net/tyler_download/article/details/50668983/ 视频地址 : http://study.163.com/cou ...

  8. python3 爬虫实例_【实战练习】Python3网络爬虫快速入门实战解析(上)

    原标题:[实战练习]Python3网络爬虫快速入门实战解析(上) 摘要 使用python3学习网络爬虫,快速入门静态网站爬取和动态网站爬取 [ 前言 ] 强烈建议:请在电脑的陪同下,阅读本文.本文以实 ...

  9. AOSP6.0.1 launcher3入门篇-解析DeviceProject.java及相关文件

    上一篇文章(AOSP6.0.1 launcher3入门篇-解析launcher.java文件)描述了launcher3的加载过程,本篇文章记录hotseat停靠方向和位置.隐藏页指示器.Folder大 ...

最新文章

  1. Python:处理一些格式规范的文字
  2. 利用Azure communication service实现跟Teams同样等级的沟通协作应用
  3. Hibernate使用最新的MySQL8.+版本出现的问题!
  4. 为文本添加风格text-decoration
  5. iOS App后台保活
  6. 【Scala】scala ':' or newline expected \u200b
  7. delphi.指针.应用----应用重要 多看 多练
  8. POJ 2718 Smallest Difference(dfs,剪枝)
  9. [USACO10MAR]伟大的奶牛聚集
  10. [LeetCode] 83. Remove Duplicates from Sorted List
  11. 邮件中html格式转换,如何在宏中将邮件格式更改为html?
  12. 编写树莓派引脚驱动代码
  13. matlab 密集,matlab – 全局和parfor
  14. html5 聊天机器人,发挥你想象力,BotUI – 聊天机器人 JS 框架
  15. linux2.6内核驱动程序注册函数,于PCI9656设备驱动程序的Linux2.6内核研究
  16. 介绍几种在线文档编辑器
  17. 上海应用技术大学计算机专业分数线,上海应用技术大学历年分数线 2021上海应用技术大学录取分数线...
  18. 使用DOS的导出Oracle的dmp文件
  19. Pippo java微服务,轻量级web开发框架,原来Filter还能这么玩
  20. Futuremark 3DMark 2.17.7137,3DMark兼容性强大

热门文章

  1. 微信小程序实现考场座位号随机
  2. 深度学习DBN深度置信网络
  3. 经典算法——筛选法求素数(素数筛选)
  4. lavaral中文手册_Laravel-mix 中文文档
  5. 网络钓鱼常用手段大揭秘,超级科技教你如何防范
  6. 初识人脸识别---人脸识别研究报告(概述篇)
  7. web打印window.print
  8. Github每日精选(第99期): AirDrop 的开源跨平台Localsend
  9. uniapp 小程序、H5 点餐、外卖源码
  10. 【CES遇见人工智能】欧莱雅发微型可穿戴设备:可帮助保护你的皮肤