I want to find a specific sequence of Bytes in a binary file using PHP. I represented this sequence in hexadecimal, to avoid typing too many 0s and 1s. The sequence to find is 0x4749524f
. This is the working solution i came up for now:
$mysequence = "4749524f";
$f = fopen($filename, "r") or die("Unable to open file!");
while(!feof($f)){
$seq = fread($f, 4);
if(bin2hex($seq) == $mysequence){
echo "found!";
break;
}
else if(!feof($f)) fseek($f, -3, SEEK_CUR);
}
What the algorithm does is simple:
- Read 4 Bytes
- Check if they are equals to the sequence
- If they are equals -> found! Stop the execution.
- If they are not equals and i am not at the end of the file, go back 3 Bytes into the file and repeat step 1.
Why do I go back 3 Bytes? Because if this is the content of the file:
0000 4749 524f 0000 01b0 0013
If i don't go back 3 Bytes, I will read 0000 4749
on the first iteration, 524f 0000
on the second, 01b0 0013
on the third and as you can see i missed the sequence.
Problem: It's slow like hell...The application will have to work with files up to 50MB big, so it will take forever to find this sequence.
Is there an optimized function in PHP that would do the job? Is there a faster (not dumb like mine) way to do this?