Trying to read a binary value from a file
Posted: Fri Aug 14, 2009 9:53 pm
I am stumped. I am trying to read 2 binary values out of a FLV file header meta data section. I start by reading in 400 bytes of data into a string, and then locating the keywords that locate the values I want. The problem is that I cannot figure out how to read the value so that it becomes a number.
Here is the code I have tried so far.
First, I wrote a function to locate the desired value in the input string. According to the FLV header documentation, the value is an 8 byte double. I have tried all kinds of type casting, and only end up with zero as the result. I know that the correct value is stored in there, as other GUI applications can successfully pick out the correct value.
function getValue ($source, $keyword)
{
$loc = strpos($source, $keyword);
if ($loc > 0)
{
$value = substr($source, $loc + strlen($keyword), 8);
return $value;
}
else
return -1;
}
Then I open the file, read in 400 bytes and try to pick out the width and height values.
$fp = fopen($file, 'r');
if ($fp)
{
$header = fread($fp, 400);
if ($header != false)
{
$isFLV = ($header[0]=='F' && $header[1]=='L' && $header[2]=='V');
$isVideo = (hexdec(bin2hex($header[4])) & 0x01);
if ($isFLV && $isVideo)
{
// find and read the height and width from the file header
$width = getValue ($header, "width");
$height = getValue ($header, "height");
}
}
fclose($fp);
}
The $isFLV and $isVideo flags are being correctly set. And when I add "echo" statements to the GetValue function, as well as print out the full 400 byte header, I know that the values I am looking for are in there, and they are being correctly located by the GetValue function. However, nothing I have tried so far has resulted in converting the bit stream into a number.
When I display the "ord" of each of the 8 characters read for the values, I get the following output:
0 64 118 0 0 0 0 0
and
0 64 126 0 0 0 0 0
From other applications, I know that the first value is supposed to be 352 and the second one is 480.
I am reasonably sure that the problem is with my approach - that once the bytes have been read in as a string, that they are in a format that I cannot convert. But I sure don't know how else to approach the solution. Any help would be greatly appreciated. I have bruised my head and ego both on this problem.
Here is the code I have tried so far.
First, I wrote a function to locate the desired value in the input string. According to the FLV header documentation, the value is an 8 byte double. I have tried all kinds of type casting, and only end up with zero as the result. I know that the correct value is stored in there, as other GUI applications can successfully pick out the correct value.
function getValue ($source, $keyword)
{
$loc = strpos($source, $keyword);
if ($loc > 0)
{
$value = substr($source, $loc + strlen($keyword), 8);
return $value;
}
else
return -1;
}
Then I open the file, read in 400 bytes and try to pick out the width and height values.
$fp = fopen($file, 'r');
if ($fp)
{
$header = fread($fp, 400);
if ($header != false)
{
$isFLV = ($header[0]=='F' && $header[1]=='L' && $header[2]=='V');
$isVideo = (hexdec(bin2hex($header[4])) & 0x01);
if ($isFLV && $isVideo)
{
// find and read the height and width from the file header
$width = getValue ($header, "width");
$height = getValue ($header, "height");
}
}
fclose($fp);
}
The $isFLV and $isVideo flags are being correctly set. And when I add "echo" statements to the GetValue function, as well as print out the full 400 byte header, I know that the values I am looking for are in there, and they are being correctly located by the GetValue function. However, nothing I have tried so far has resulted in converting the bit stream into a number.
When I display the "ord" of each of the 8 characters read for the values, I get the following output:
0 64 118 0 0 0 0 0
and
0 64 126 0 0 0 0 0
From other applications, I know that the first value is supposed to be 352 and the second one is 480.
I am reasonably sure that the problem is with my approach - that once the bytes have been read in as a string, that they are in a format that I cannot convert. But I sure don't know how else to approach the solution. Any help would be greatly appreciated. I have bruised my head and ego both on this problem.