Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte manipulation in PHP

Tags:

php

byte

In PHP, if you have a variable with binary data, how do you get specific bytes from the data? For example, if I have some data that is 30 bytes long how do I get the first 8 bytes?

Right now, I'm treating it like a string, using the substr() function:

$data = //...
$first8Bytes = substr($data, 0, 8);

Is it safe to use substr with binary data?

Or are there other functions that I should be using?

like image 496
Michael Avatar asked Dec 31 '10 15:12

Michael


2 Answers

If the mbstring extension is installed and mbstring overloading is enabled, then using substr might give you trouble. Mbstring overloading will cause mb_substr to be automatically called every time substr is called (if mbstring is installed and mbstring overloading is disabled, then substr will correctly retrieve the bytes). The following code will use mb_substr if mbstring is installed and substr if it isn't. The "8bit" character encoding is used, which will treat each character as 1 byte and will ignore null terminators ('\0').

if (function_exists('mb_substr')) {
    $bytes = mb_substr($string, 0, 8, '8bit');
} else {
    $bytes = substr($string, 0, 8);
}

Thanks to ircmaxell

like image 54
Michael Avatar answered Oct 23 '22 03:10

Michael


Generally all string functions in PHP are safe to use with raw bytes. The issue that mostly crops up are null-bytes, but only for filesystem-functions: http://php.net/manual/en/security.filesystem.nullbytes.php

Your substr() is perfectly fine to use with binary strings. Some other functions like strtok and ereg however interface to C, where the "\0" character becomes an issue.

like image 11
mario Avatar answered Oct 23 '22 03:10

mario