Here's the full script I using to parse a SWF file:
$fp = @fopen($filename,"rb");
if ($fp)
{
#--------------------------------------------
#-------------------------------------------------------------
$first_three = fread($fp,3);
if( $first_three && $first_three != 'CWS')
{
if ($debug) echo "DEBUG: " . $filename . " is not a valid/supported SWF file<br>" ;
$valid = 0 ;
}
else
{
#---- parse for compression -----------
#--------------------------------------
if( substr( $first_three,0,1 ) == "C"){ $compressed = true; }
else{ $compressed = false; }
if( $debug ){ echo "DEBUG: Read MAGIC signature: " . $first_three. "<br>"; } // bytes 1-3
#--------------------------------------
#---- parse for Version ---------------
#--------------------------------------
$version = fread( $fp,1 );
$version = ord( $version );
$version = $version;
#--------------------------------------
#---- Size ----------------------------
#--------------------------------------
$lg = 0 ;
for( $i = 0; $i < 4; $i++ )
{
$t = fread($fp,1); $test_file_string .= $t;
$t = ord( $t ) ;
if( $debug ){ echo "DEBUG: Partial SIZE read [$t]: " . ( $t << ( 8$i ) ) . "<br>"; }
$lg += ( $t << ( 8 $i ) );
}
$size = $lg ;
if( $debug ){ echo "DEBUG: Total SIZE: " . $size . "<br>"; } //bytes 5-8
#--------------------------------------
#---- to uncompress or not to uncompress? ----
#---------------------------------------------
$buffer = fread($fp, $size ) ;
$test_file_string .= $buffer;
if( $compressed)
{
$buffer = gzuncompress($buffer, $size); // First decompress GZ stream
}
#--------------------------------------------
$b = substr( $buffer,0,7 ); #<<--read next 7 bytes of string
$b = ord( $b );
$buffer = substr( $buffer,1 );
#---- Frame rate ----------------------
#--------------------------------------
$fps = Array() ;
for( $i=0; $i<2; $i++ )
{
$t = substr($buffer,0,1 ); #take the first byte in front
$t = ord($t) ;
$buffer = substr($buffer,1) ; #take one off the front (advance it)
$fps[] = $t ;
}
if ($debug) echo "DEBUG: Frame rate: " . $fps[1] . "." . $fps[0] . "<br>" ;
# the result here comes to 12!! This is correct as the SWF file has a frame rate of 12
# Somehow some 0c0c is getting switched to c0 (first byte being ignored??, second byte inverted?)
#---------------------------------------
#---- Frames --------------------------
#--------------------------------------
$frames = 0 ;
for( $i=0; $i<2; $i++ )
{
$t = substr( $buffer,0,1 );
$t = ord( $t );
$buffer = substr( $buffer,1 );
$frames += ( $t<< (8$i) );
}
if( $debug ){ echo "DEBUG: Frames: " . $frames . "<br>"; }
# the result here comes to 3!! This is correct as the SWF file has 3 frames in it
#* Somehow some C3 CC is getting switched to 03 (first byte being ignored??, second byte inverted?)
#---------------------------------------
fclose($fp) ;
}//endElse
}
else
{
$valid = 0 ;
if( $debug ) echo "DEBUG: Failed to open " . $fname . "<br>" ;
}
[/PHPNET]
I've denoted the important comments w/ #***.
The results are completely correct as the SWF file has a frame rate of 12 and 3 frames. I'm just not getting how they are switching the bytes around.
Thanks for your help