You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
12
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
12
17
---
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
+
```
14
27
15
28
Example Usage
16
29
---
17
30
```
18
31
$fileData = file_get_contents('somefile.bin');
19
-
$br = new BinaryReader($fileData, 'little');
32
+
$br = new BinaryReader($fileData, Endian::ENDIAN_LITTLE);
20
33
$magic = $br->readUInt32();
21
34
$offset = $br->readUInt16();
22
35
$length = $br->readUInt16();
36
+
...
23
37
```
24
38
25
39
Methods
26
40
---
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
28
50
29
-
**readUInt8()**will return a single 8 bit byte as an unsigned integer
51
+
**readUInt32()**returns a 32-bit unsigned integer
30
52
31
-
**readUInt16()**will return a 16-bit short as an unsigned integer
53
+
**readInt32()**returns a 32-bit signed integer
32
54
33
-
**readUInt32()**will return a 32-bit unsigned integer
55
+
**readUBits($length)**returns a variable length of bits (unsigned)
34
56
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)
36
58
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
38
60
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
40
62
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.
42
78
43
79
Acknowledgements
44
80
---
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`_
0 commit comments