Skip to content

Commit ad7f838

Browse files
committed
Merge pull request #4 from mdurrant/develop
Feature/redesign
2 parents bcb4f54 + 78c5c47 commit ad7f838

21 files changed

Lines changed: 1443 additions & 358 deletions

README.md

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,80 @@ PhpBinaryReader
44
[![Code Coverage](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/?branch=master)
55
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/?branch=master)
66

7-
The primary purpose of this binary reader is to accept a string of file contents and provide a familiar set of methods
8-
to read various data types. This is class is rough, early in development and no warranties are provided. Please see the
9-
license file for usage guidelines.
7+
Why?
8+
---
9+
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
10+
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
11+
well do our best to make it as painless as possible.
1012

11-
Contributing
13+
The purpose of this binary reader is to accept a string of file contents and provide a set of methods inspired by .NET
14+
to traverse it.
15+
16+
Note on Endians
1217
---
13-
Contributions must follow the PSR2 coding standards.
18+
The reader is designed to work on little endian machines, which is going to apply to most scenarios as all x86 and x86-64
19+
machines are little endian. If you have somehow found yourself on a big endian machine, you need to inform the class or
20+
you may not be able to properly read signed integers in the file you're parsing.
21+
```
22+
$fileData = file_get_contents('somefile.bin');
23+
$br = new BinaryReader($fileData);
24+
$br->setMachineByteOrder(Endian::ENDIAN_BIG);
25+
...
26+
```
1427

1528
Example Usage
1629
---
1730
```
1831
$fileData = file_get_contents('somefile.bin');
19-
$br = new BinaryReader($fileData, 'little');
32+
$br = new BinaryReader($fileData, Endian::ENDIAN_LITTLE);
2033
$magic = $br->readUInt32();
2134
$offset = $br->readUInt16();
2235
$length = $br->readUInt16();
36+
...
2337
```
2438

2539
Methods
2640
---
27-
**__construct($str, $endian)** a string must be provided to use this class, an endian is optional (big|little), it will default to big if not provided
41+
**__construct($str, $endian)** a string 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.
42+
43+
**readUInt8()** returns a single 8 bit byte as an unsigned integer
44+
45+
**readInt8()** returns a single 8 bit byte as a nsigned integer
46+
47+
**readUInt16()** returns a 16-bit short as an unsigned integer
48+
49+
**readInt16()** returns a 16-bit short as signed integer
2850

29-
**readUInt8()** will return a single 8 bit byte as an unsigned integer
51+
**readUInt32()** returns a 32-bit unsigned integer
3052

31-
**readUInt16()** will return a 16-bit short as an unsigned integer
53+
**readInt32()** returns a 32-bit signed integer
3254

33-
**readUInt32()** will return a 32-bit unsigned integer
55+
**readUBits($length)** returns a variable length of bits (unsigned)
3456

35-
**readBits($count)** will return up to 32 bits, allows reading data at the bit level
57+
**readBits($length)** returns a variable length of bits (signed)
3658

37-
**align($move)** will align the pointer back to 0 bits, it will move it to the next byte if $move = true
59+
**readString($length)** returns a variable length string
3860

39-
**readString($len)** expects a length parameter, it will read the number of characters and return a string
61+
**readAlignedString($length)** aligns the pointer to 0 bits and returns a variable length string
4062

41-
**isEof()** will return true if the pointer is on the last byte of the file
63+
**align()** aligns the pointer back to 0 bits
64+
65+
**isEof()** returns true if the pointer is on the last byte of the file
66+
67+
**getPosition()** returns the current byte position in the file
68+
69+
**setPosition($position)** sets the current byte position
70+
71+
**getCurrentBit()** returns the current bit position in the file
72+
73+
**setCurrentBit($currentBit)** sets the current bit position
74+
75+
Contributing
76+
---
77+
Contributions must follow the PSR2 coding standards and must not degrade 100% coverage.
4278

4379
Acknowledgements
4480
---
45-
Significant portions of the work is based on Graylin Kim's Python bit/byte reader in _sc2reader: https://github.com/GraylinKim/sc2reader
81+
Significant portions of the work is based on Graylin Kim's Python bit/byte reader in `sc2reader`_
82+
83+
.. _sc2reader: https://github.com/GraylinKim/sc2reader

0 commit comments

Comments
 (0)