PHP Classes

PHP Binary Stream: Parse extract data from binary files

Recommend this page to a friend!
  Info   View files Example   View files View files (1)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 195 All time: 8,501 This week: 122Up
Version License PHP version Categories
binarystream 1.0.3BSD License5PHP 5, Files and Folders, Parsers
Description 

Author

This class can parse extract data from binary files.

It can read binary data from a given file and extract data blocks of several types and sizes.

Currently it supports data blocks of types like bits, characters, big and little endian short and long integers of 16, 32 and 64 bits, 32 and 64 bits float and double.

The class can also read from a configuration file group data type definitions associated to given names.

Innovation Award
PHP Programming Innovation award nominee
January 2017
Number 8


Prize: One subscription to the PDF edition of the PHP Architect magazine
Sometimes applications need to parse files that have information in a binary format.

PHP has functions to convert a binary stream of data into individual values, but for whole streams in a specific format, parsing data may be a complicated task.

This class simplifies that process by the means of functions that read files and extract values of some expected types.

It also provides means to parse groups of data that have a well known format, by using data group definitions.

Manuel Lemos
Picture of Sergey Vanyushin
  Performance   Level  
Name: Sergey Vanyushin is available for providing paid consulting. Contact Sergey Vanyushin .
Classes: 15 packages by
Country: Russian Federation Russian Federation
Age: 27
All time rank: 57616 in Russian Federation Russian Federation
Week rank: 76 Up6 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Example

#!/usr/bin/php
<?php
require __DIR__.'/../vendor/autoload.php';
if (!isset(
$argv[1])) die('Usage: '.__FILE__.' <mp3file>'.PHP_EOL);

$s = new wapmorgan\BinaryStream\BinaryStream($argv[1]);
$s->loadConfiguration(__DIR__.'/../conf/mp3.conf');
function
convertText($content) { return ($content[0] == 0x00) ? mb_convert_encoding(substr($content, 1), 'utf-8', 'ISO-8859-1') : substr($content, 1); }
if (
$s->compare(3, 'ID3')) {
   
$header = $s->readGroup('id3v2');
   
$header_size = null;
    for (
$i = 0; $i < 4; $i++) {
       
$header_size .= decbin(ord($header['size'][$i]));
    }
   
$header_size = bindec($header_size);
   
$group = ($header['version'] == 2) ? 'id3v232' : 'id3v234';
   
$tags_2 = array();
    while (!
$s->compare(3, "\00\00\00")) {
       
$frame = $s->readGroup($group);
       
$frame_content = $s->readString($frame['size']);
        switch (
$frame['id']) {
            case
'TIT2': case 'TT2': $tags_2['song'] = convertText($frame_content); break;
            case
'TALB': case 'TAL': $tags_2['album'] = convertText($frame_content); break;
            case
'TPE1': case 'TP1': $tags_2['artist'] = convertText($frame_content); break;
            case
'TYER': case 'TYE': $tags_2['year'] = convertText($frame_content); break;
            case
'COMM': case 'COM':
               
$frame_content = substr(convertText($frame_content), 3);
               
$tags_2['comment'] = strpos($frame_content, "\00") ? substr($frame_content, strpos($frame_content, "\00") + 1) : $frame_content;
                break;
            case
'TRCK': case 'TRK': $tags_2['track'] = convertText($frame_content); break;
            case
'TCON': case 'TCO': $tags_2['genre'] = convertText($frame_content); break;
        }
    }
   
var_dump($tags_2);
}

$s->go(-128);
if (
$s->compare(3, 'TAG')) {
   
$tags = $s->readGroup('id3v1');
   
var_dump(array_map(function ($item) { return trim($item); }, $tags));
}


  Files folder image Files  
File Role Description
Files folder imagebin (1 file)

  Files folder image Files  /  bin  
File Role Description
  Plain text file mp3reader Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:195
This week:0
All time:8,501
This week:122Up
User Ratings User Comments (3)
 All time
Utility:81%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:81%StarStarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:730
 
nice
6 years ago (muabshir)
80%StarStarStarStarStar
nice
6 years ago (muabshir)
80%StarStarStarStarStar
nice
6 years ago (muabshir)
80%StarStarStarStarStar