Giter Club home page Giter Club logo

php-binary-reader's Introduction

PhpBinaryReader

Build Status Code Coverage Scrutinizer Code Quality

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.

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.

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.

$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.

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

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

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

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

readUInt32() returns a 32-bit unsigned integer

readInt32() returns a 32-bit signed integer

readUInt64() returns a 64-bit unsigned integer

readInt64() returns a 64-bit signed integer

readSingle() returns a 4-bytes floating-point

readDouble() returns a 8-bytes signed double

readUBits($length) returns a variable length of bits (unsigned)

readBits($length) returns a variable length of bits (signed)

readBytes($length) returns a variable length of bytes

readString($length) returns a variable length string

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

align() aligns the pointer back to 0 bits

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

getPosition() returns the current byte position in the file

setPosition($position) sets the current byte position

getCurrentBit() returns the current bit position in the file

setCurrentBit($currentBit) sets the current bit position

Contributing

Contributions must follow the PSR2 coding standards and must not degrade 100% coverage.

Acknowledgements

Significant portions of the work is based on Graylin Kim's Python bit/byte reader in sc2reader_

.. _sc2reader: https://github.com/GraylinKim/sc2reader

php-binary-reader's People

Contributors

mdurrant avatar jwpage avatar nsknewbie avatar arjunkishore avatar gingerbeardman avatar handsomematt avatar scrutinizer-auto-fixer avatar klermonte avatar

Stargazers

Taha Amin Ghafuri [T@G] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.