2017 © Pedro Peláez
 

library php-binary-reader

Lightweight binary reader for PHP

image

mdurrant/php-binary-reader

Lightweight binary reader for PHP

  • Wednesday, April 19, 2017
  • by mdurrant
  • Repository
  • 7 Watchers
  • 40 Stars
  • 4,940 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 8 Forks
  • 3 Open issues
  • 12 Versions
  • 12 % Grown

The README.md

PhpBinaryReader

Build Status Code Coverage Scrutinizer Code Quality, (*1)

Why?

You probably wouldn't be here if you hadn't run into a scenario where you needed to leverage PHP to read a stream of binary data. The honest truth is PHP really stinks at this stuff, but as long as we're going to be using it we may as well do our best to make it as painless as possible., (*2)

The purpose of this binary reader is to accept a string of file contents or file resource and provide a set of methods inspired by .NET to traverse it., (*3)

Note on Endians

The reader is designed to work on little endian machines, which is going to apply to most scenarios as all x86 and x86-64 machines are little endian. If you have somehow found yourself on a big endian machine, you need to inform the class or you may not be able to properly read signed integers in the file you're parsing., (*4)

$fileData = file_get_contents('somefile.bin');
$br = new BinaryReader($fileData);
$br->setMachineByteOrder(Endian::ENDIAN_BIG);
...

Example Usage

$fileData = file_get_contents('somefile.bin');
$br = new BinaryReader($fileData, Endian::ENDIAN_LITTLE);
// or
$fileResource = fopen('somefile.bin', 'rb');
$br = new BinaryReader($fileResource, Endian::ENDIAN_LITTLE);

$magic = $br->readUInt32();
$offset = $br->readUInt16();
$length = $br->readUInt16();
...

Methods

__construct($input, $endian) a string or file resource must be provided to use this class, an endian is optional (string [big|little], or use the constants in the Endian class), it will default to little if not provided., (*5)

readUInt8() returns a single 8 bit byte as an unsigned integer, (*6)

readInt8() returns a single 8 bit byte as a signed integer, (*7)

readUInt16() returns a 16-bit short as an unsigned integer, (*8)

readInt16() returns a 16-bit short as a signed integer, (*9)

readUInt32() returns a 32-bit unsigned integer, (*10)

readInt32() returns a 32-bit signed integer, (*11)

readUInt64() returns a 64-bit unsigned integer, (*12)

readInt64() returns a 64-bit signed integer, (*13)

readSingle() returns a 4-bytes floating-point, (*14)

readUBits($length) returns a variable length of bits (unsigned), (*15)

readBits($length) returns a variable length of bits (signed), (*16)

readBytes($length) returns a variable length of bytes, (*17)

readString($length) returns a variable length string, (*18)

readAlignedString($length) aligns the pointer to 0 bits and returns a variable length string, (*19)

align() aligns the pointer back to 0 bits, (*20)

isEof() returns true if the pointer is on the last byte of the file, (*21)

getPosition() returns the current byte position in the file, (*22)

setPosition($position) sets the current byte position, (*23)

getCurrentBit() returns the current bit position in the file, (*24)

setCurrentBit($currentBit) sets the current bit position, (*25)

Contributing

Contributions must follow the PSR2 coding standards and must not degrade 100% coverage., (*26)

Acknowledgements

Significant portions of the work is based on Graylin Kim's Python bit/byte reader in sc2reader_, (*27)

.. _sc2reader: https://github.com/GraylinKim/sc2reader, (*28)

The Versions

19/04 2017

dev-master

9999999-dev

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

08/12 2016

1.0.9

1.0.9.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

14/06 2016

1.0.8

1.0.8.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

16/03 2016

1.0.7.1

1.0.7.1

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

16/03 2016

1.0.7

1.0.7.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

22/08 2014

1.0.6

1.0.6.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

10/08 2014

dev-develop

dev-develop

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

16/07 2014

1.0.5

1.0.5.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

14/07 2014

1.0.4

1.0.4.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

12/07 2014

1.0.3

1.0.3.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

12/07 2014

1.0.2

1.0.2.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant

10/07 2014

1.0.1

1.0.1.0

Lightweight binary reader for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matt Durrant