wapmorgan / binary-stream
A handy tool for working with binary data
Installs: 204 329
Dependents: 5
Suggesters: 0
Security: 0
Stars: 51
Watchers: 2
Forks: 7
Open Issues: 1
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.8.35
README
BinaryStream - a handy tool for working with binary data and the best replacement for pack()/unpack() with big list of features.
If you are looking for a convenient tool that would allow read and write binary data (whether existing or created by you format), you choose the correct library.
BinaryStream - is a powerful tool for reading and writing binary files. It supports a variety of high-level data types and sometimes lets you forget that you are working with unstructured binary data.
With BinaryStream you can handle network packets, binary files, system protocols and other low-level data.
Features
- Minimal supported PHP version is 5.3.0 for all features.
- The library supports all major data types and allows both read and write the data.
- Supports both direct order of bytes (big endian) and reverse (little). You can switch between them while reading a file.
- Supports multiple dimensions of integers (8, 16, 32 and 64) and also rare (24, 40, 48 and 56).
- Supports multiple dimensions of fractional numbers (32 and 64).
- You can read both individual bytes and individual bits.
- For ease of navigation through the file, you can command BinaryStream remember some positions in the file, and later return to them again.
- Supports data groups: save configuration once and then read similar data groups with only it's name.
- Supports configuration files to switch between file formats and versions.
- Unlike standard php functions, it can work with fractional numbers written in both the direct order of bytes (Big-Endian) and the reverse one (Little-Endian).
Why it's objectively better pack/unpack?
- 64 bit int's/float's
- selection of byte-order of float's
- rare, but possible int's size (24, 40, 48, 56)
- other features like data groups and configurations ...
And that's all with PHP 5.3.
Manual
Simple usage
The easiest way to use BinaryStream - this:
use wapmorgan\BinaryStream\BinaryStream; $stream = new BinaryStream('filename.ext'); $text = $s->readString(20);
This example reads 20 bytes at the beginning of the file as a string.
A more complex example, where the data were located in the following order:
- integer (int, 32 bit)
- float (float, 32 bit)
- flag byte (where each bit has its own value, 8 bits): first bit determines whether there after this byte written another data, 5-bit empty, and the last 2 bits of the data type:
0b00
- after this data recorded 1 character (char, 8 bits)0b01
- after this data recorded 10 characters (string, 10 bytes)0b10
- after this data time in unixtime format packaged in long integer (long, 64 bits)0b11
- not used at this moment.
In order to read these data and those that depend on the flags, this example is suitable:
use wapmorgan\BinaryStream\BinaryStream; $stream = new BinaryStream('filename.ext'); $int = $stream->readInteger(32); $float = $stream->readFloat(32); $flags = $stream->readBits([ 'additional_data' => 1, '_' => 5, // pointless data 'data_type' => 2, ]); if ($flags['additional_data']) { if ($flags['data_type'] == 0b00) $char = $stream->readChar(); else if ($flags['data_type'] == 0b01) $string = $stream->readString(10); else if ($flags['data_type'] == 0b10) $time = date('r', $stream->readInteger(64)); }
In this example, we read the basic data and the additional, based on the value of flags.
But it is unlikely to be so few data. For added convenience, you can use a group reading function. The previous example can be rewritten as follows:
use wapmorgan\BinaryStream\BinaryStream; $stream = new BinaryStream('filename.ext'); $data = $stream->readGroup([ 'i:int' => 32, 'f:float' => 32, 'additional_data' => 1, '_' => 5, 'data_type' => 2, ]); if ($data['additional_data']) { if ($data['data_type'] == 0b00) $data['char'] = $stream->readChar(); else if ($data['data_type'] == 0b01) $data['string'] = $stream->readString(10); else if ($data['data_type'] == 0b10) $data['time'] = date('r', $stream->readInteger(64)); }
If you are reading a file in which such groups of data are repeated, you can save a group with a name, and then simply refer to it to read the next data. Let us introduce one more value for data_type: 0b11
- means that this is the last group of data in the file. An example would be:
use wapmorgan\BinaryStream\BinaryStream; $stream = new BinaryStream('filename.ext'); $stream->saveGroup('Data', [ 'i:int' => 32, 'f:float' => 32, 'additional_data' => 1, '_' => 5, 'data_type' => 2, ]); do { $data = $stream->readGroup('Data'); // Some operations with data } while ($data['data_type'] != 0b11);
And now imagine that we have moved to a new file format that is different from the previous one and has a certain mark in the beginning of the file, which will help to distinguish the new from the old format. For example, a new label is a sequence of characters 'A', 'S', 'C'
. We need to check the label and if it is present, parse the file according to another scheme, and if it does not, use the old version of the processor. An example to illustrate this:
use wapmorgan\BinaryStream\BinaryStream; $stream = new BinaryStream('filename.ext'); if ($stream->compare(3, 'ASC')) { // parse here new format } else { $stream->saveGroup('DataOld', [ 'i:int' => 32, 'f:float' => 32, 'additional_data' => 1, '_' => 5, 'data_type' => 2, ]); do { $data = $stream->readGroup('DataOld'); // Some operations with data } while ($data['data_type'] != 0b11); }
Installation
Installation via composer:
composer require wapmorgan/binary-stream
Reference
Data types
All used data types are presented in the following table:
API
-
Creating an object is possible in several ways:
new BinaryStream($filename | $socket | $stream)
-
Reading data is possible using specialized methods for each data type:
- bit:
-
readBit(): boolean
Example:
$flag = $s->readBit();
-
readBits(array $listOfBits): array of boolean and integers
.Example:
$flags = $s->readBits(['a' => 2, '_' => 5, 'b' => 3]);
If size of field (an array element value is1
, then this field will havetrue/false
, if larger 1, thenN
consecutive bits will be combined in aninteger
.)
-
- char:
-
readChar(): string(1)
Example:
$char = $s->readChar();
-
readChars($count): array of string(1)
Example:
$chars = $s->readChars(4);
-
- integer
-
readInteger($sizeInBits = 32): integer
Example:
$int = $s->readInteger(32);
It supports the following dimensions: 8, 16, 32, 64 and 24, 40, 48, 56 bits.
-
- float:
-
readFloat($sizeInBits = 32): float
Example:
$float = $s->readFloat(32);
It supports the following dimensions: 32, 64 bits.
-
- string:
-
readString($length): string($length)
Example:
$string = $s->readString(10);
-
- bit:
Groups of fields
You can save list of fields definitions with a specific name and use it's name when you need to read the same block few times. A group is defined by group configuration - list of fields, their type and size. To compose group configuration create an array: keys define type and name of fields, values - their size:
-
key is a name of field and may contain type of field. To specify type prepend name with a type letter and a colon. Type letters:
b
- biti
- integerf
- floatc
- chars
- string If type is not defined, field will be treated as abit
-field. Example:flag
-bit
-field,s:name
-string
-field
-
value is a size or a dimension of field:
- If field has
integer
,float
orbit
type, it defines size of field in term of bits. - If field has
char
orstring
type, it defines size in term of bytes. Example:
'flags' => 16, // bits-field (16 bits = 2 bytes) 's:name' => 10, // string-field (80 bits = 10 bytes)
- If field has
So full example of group configuration:
$group = [ 'flags' => 16, 'i:date' => 32, 'f:summ' => 32, 's:name' => 10, ];
Navigation
- Caret moving: To change the position of the cursor in the file use the following methods.
-
Current position testing:
isEnd(): boolean
Returns true if cursor is at the end of file.
-
Remembering the positions in file:
Auxiliary
-
Comparation of bytes:
compare($length, $bytes)
Compares
$length
bytes from current position with$bytes
. Carrent position will not be changed. Returns true or false. -
Endianness: By default,
BinaryStream
treatsint
's andlong
's in little-endian format. To change the reading order of bytes usesetEndian($endian)
method with one ofBinaryStream
constants:
Configurations
Advanced usage. Writing
If you are the one who needs to write data to binary files, you can use additional methods to do so.
Firstly, you need to open a file in one of the modes that allow writing of a file (by default, files are opened in read-only mode). For this when you create an object BinaryStream specify in second argument one of the following modes:
After you have correctly opened the file, you can use the following methods, named by analogy with the other designed for reading.