Jump to content

Recommended Posts

Hi I have a file that is in binary and it has this structure:

 

header

[4] - ID (DAT2)

[4] - Int32 - Number of Files

 

Number of Files Loop (12 bytes a entry)

[4] - Offset: In .Dat

[4] - File Size

[4] - ID/Hash?

 

So the first 8 bytes contains two pieces of useful data, you can split up the 8 bytes into each of the 4 bytes. The rest of the file contains data unique data every 12 bytes which you can split up those 12 bytes into groups of 4 bytes to get specific information.

 

I'm trying to simply grab all of these bytes and ultimately use the information in the bytes like "offset" and "filesize" to read yet another binary file, and feed php function like fread() and fseek() with values like filesize and offset

 

For now though I am checking to see if this is the correct way to grab these bytes from teh file, as someone told me that it is not, and I am deeply confused. I count the # of bytes with strlen() because 1 char is 1 byte

 

 

<?php
//handle
$handle = fopen($Root.'/file.ext', 'rb') or die('cannot open file');

//get top part
$idBin = fread($handle, 4); //reads 4 bytes past pointer not to offset 4 (at 0 now)
$idHex = bin2hex($idBin);
fseek($handle, 4, SEEK_SET); //sets 4 bytes from begg. of the file
$int32Bin = fread($handle, 4); //reads bytes 4-8
$int32Hex = bin2hex($int32Bin);
fseek($handle, 0, SEEK_SET); //rewinds back to 0 bytes

//loop
$before = file_get_contents($Root.'/file.ext');
$length = strlen($before); //how many bytes in file period
$loopEnd = ($length/12) + 5;
for ($i=0; $i<=$loopEnd; $i++) {
    if ($i == 0) {
        //start at 8 bytes not 0
        $start = 8;
    } else {
        $start = ($i * 12) + 8;
    }
    
    if ($start < $length) {
            if (($start + 12) > $length) {
                $num = $length - $start;
            } else {
                $num = 12;
            }
        fseek($handle, $start, SEEK_SET);
        $raw = fread($handle, $num);
        $splitRaw = str_split($raw, 4); //split every 4 bytes
        $offsetBin = $splitRaw[0]; 
        $filesizeBin = $splitRaw[1];
        $hashBin = $splitRaw[2];
         $offsetHex = bin2hex($offsetBin);
         $filesizeHex = bin2hex($filesizeBin);
         $hashHex = bin2hex($hashBin);

        $rArray[] = array('bin' => array('offset' => $offsetBin, 'filesize' => $filesizeBin, 'hash' => $hashBin), 'hex' => array('offset' => $offsetHex, 'filesize' => $filesizeHex, 'hash' => $hashHex));
    } else {
        continue;
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/70271-help-with-reading-binary-data-from-file/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.