<?php

require_once('BitStream.php'); // {Input|Output}BitStream
require_once('FlashSWF/TagBlock.php'); // 

class FlashSWF {
        private 
$_header = array();
    private 
$_block_list = array();
        function 
__construct() {
      return ;
    }
        function 
loadfile($filename) {
            
$swfdata file_get_contents($filename);
        if (
$swfdata === FALSE) {
          
// throw Exception("Can't open file($filename)");
                
return FALSE;
        }
            
$this->load($swfdata);
    }
        function 
load($swfdata) {
            
$ibs = new InputBitStream($swfdata);
        
$magic $ibs->getStrings(3);
        
$version $ibs->getByte();
        
$filelength $ibs->getBytesLE(4);
                
$this->_header['magic'] = $magic;
                
$this->_header['version'] = $version;
        
$this->_header['filelength'] = $filelength;
        if (
$magic == 'FWS') {
                ;
        } elseif (
$magic == 'CWS') {
                
$movie_and_body gzuncompress($ibs->getStrings(), $filelength);
            
$ibs = new InputBitStream($movie_and_body);
        } else {
                return ;
        }
        
$this->_header['movie_header'] = $this->get_movie_header($ibs);
        while(
true) {
          
$block FlashSWF_TagBlock::Factory($ibs);
          
$this->_block_list[] = $block;
          if (
$block->getTagId() == 0) {
            break;
          }
        }
    }
    function 
get_movie_header(&$ibs) {
      
$movie_header = array();
      
$movie_align $ibs->getBits(5);
      
$movie_header['align'] = $movie_align;
      
$movie_header['x_min'] = $ibs->getBits($movie_align);
      
$movie_header['x_max'] = $ibs->getBits($movie_align);
      
$movie_header['y_min'] = $ibs->getBits($movie_align);
      
$movie_header['y_max'] = $ibs->getBits($movie_align);
      
$ibs->align();
      
$movie_header['frame_rate_decimal'] = $ibs->getByte();
      
$movie_header['frame_rate_integer'] = $ibs->getByte();
      
$movie_header['frame_rate_count'] = $ibs->getBytesLE(2);
      return array(
$movie_header);
    }
        function 
savefile($filename) {
            
$swfdata $this->save();
        
file_put_contents($filename$swfdata);
    }
        function 
save() {
      
$header "";
      
$movie_header "";
      
$data "";
      return 
$header $movie_header $data;
    }
    function 
dump() {
                
var_dump($this->_header);
        foreach (
$this->_block_list as $block) {
          
$block->dump();
        }
    }
}