<?php

require_once('BitStream.php');

class 
FlashSWF_TagBlock {
        private static 
$_tagNameTable =
      array(
=> 'End',
        
=> 'ShowFrame',
        
=> 'DefineShape',
        
=> 'DefineBitsJPEG',
        
=> 'JPEGTables',
        
=> 'SetBackgroundColor',
        
12 =>'DoAction',
        
26 =>'PlaceObject2'
        
);
        private static 
$_tagClassTable =
      array(
        
=> 'DefineBitsJPEG',
        );
    private 
$_tag;
    private 
$_length;
    private 
$_rawdata;
    static function 
Factory(&$ibs) {
            
$tag_and_length $ibs->getBytesLE(2); // important
        
$tag $tag_and_length >> 6;
        
$length $tag_and_length 0x3f;
        if (
$length == 0x3f) {
            
$length $ibs->getBytesLE(4);
        }
        
$rawdata $ibs->getStrings($length);
        
$classname __CLASS__;
        
$tagclass = @ self::$_tagClassTable[$tag];
        if (! empty(
$tagclass)) {
              require_once(
"FlashSWF/TagBlock/$tagclass.php");
              
$classname $classname '_' $tagclass;
        }
        
$block = new $classname($tag$length$rawdata);
        return 
$block;
    }
    function 
__construct($tag$length$rawdata) {
        
$this->_tag $tag;
        
$this->_length $length;
        
$this->_rawdata $rawdata;
    }
    function 
getTagName() {
        
$tag $this->_tag;
        if (isset(
self::$_tagNameTable[$tag])) {
                return 
self::$_tagNameTable[$tag] . "($tag)";
        }
        return 
$tag;
    }
    function 
getTagId() {
            return 
$this->_tag;
    }
    function 
getLength() {
            return 
$this->_length;
    }
    function 
getData() { // must be override
            
return $this->_rawdata;
    }
    function 
save() {
      
$data $this->getData();
      
$length strlen($data);
    }
    function 
dump() {
      
$tagname $this->getTagName();
      
$length $this->getLength();
      print 
"tag=$tagname length=$length\n";
    }
}