Skip to content

Commit 080b90f

Browse files
committed
Merge pull request #5 from mdurrant/develop
Adding byte method, refactoring out static methods
2 parents ad7f838 + bea2f9b commit 080b90f

18 files changed

Lines changed: 550 additions & 175 deletions

src/BinaryReader.php

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpBinaryReader\Exception\InvalidDataException;
66
use PhpBinaryReader\Type\Bit;
7+
use PhpBinaryReader\Type\Byte;
78
use PhpBinaryReader\Type\Int8;
89
use PhpBinaryReader\Type\Int16;
910
use PhpBinaryReader\Type\Int32;
@@ -46,6 +47,36 @@ class BinaryReader
4647
*/
4748
private $endian;
4849

50+
/**
51+
* @var \PhpBinaryReader\Type\Byte
52+
*/
53+
private $byteReader;
54+
55+
/**
56+
* @var \PhpBinaryReader\Type\Bit
57+
*/
58+
private $bitReader;
59+
60+
/**
61+
* @var \PhpBinaryReader\Type\String
62+
*/
63+
private $stringReader;
64+
65+
/**
66+
* @var \PhpBinaryReader\Type\Int8
67+
*/
68+
private $int8Reader;
69+
70+
/**
71+
* @var \PhpBinaryReader\Type\Int16
72+
*/
73+
private $int16Reader;
74+
75+
/**
76+
* @var \PhpBinaryReader\Type\Int32
77+
*/
78+
private $int32Reader;
79+
4980
/**
5081
* @param string $str
5182
* @param int|string $endian
@@ -89,7 +120,7 @@ public function align()
89120
*/
90121
public function readBits($count)
91122
{
92-
return Bit::readSigned($this, $count);
123+
return $this->getBitReader()->readSigned($this, $count);
93124
}
94125

95126
/**
@@ -98,55 +129,64 @@ public function readBits($count)
98129
*/
99130
public function readUBits($count)
100131
{
101-
return Bit::read($this, $count);
132+
return $this->getBitReader()->read($this, $count);
133+
}
134+
135+
/**
136+
* @param int $count
137+
* @return int
138+
*/
139+
public function readBytes($count)
140+
{
141+
return $this->getByteReader()->read($this, $count);
102142
}
103143

104144
/**
105145
* @return int
106146
*/
107147
public function readInt8()
108148
{
109-
return Int8::readSigned($this);
149+
return $this->getInt8Reader()->readSigned($this);
110150
}
111151

112152
/**
113153
* @return int
114154
*/
115155
public function readUInt8()
116156
{
117-
return Int8::read($this);
157+
return $this->getInt8Reader()->read($this);
118158
}
119159

120160
/**
121161
* @return int
122162
*/
123163
public function readInt16()
124164
{
125-
return Int16::readSigned($this);
165+
return $this->getInt16Reader()->readSigned($this);
126166
}
127167

128168
/**
129169
* @return string
130170
*/
131171
public function readUInt16()
132172
{
133-
return Int16::read($this);
173+
return $this->getInt16Reader()->read($this);
134174
}
135175

136176
/**
137177
* @return int
138178
*/
139179
public function readInt32()
140180
{
141-
return Int32::readSigned($this);
181+
return $this->getInt32Reader()->readSigned($this);
142182
}
143183

144184
/**
145185
* @return int
146186
*/
147187
public function readUInt32()
148188
{
149-
return Int32::read($this);
189+
return $this->getInt32Reader()->read($this);
150190
}
151191

152192
/**
@@ -155,7 +195,7 @@ public function readUInt32()
155195
*/
156196
public function readString($length)
157197
{
158-
return String::read($this, $length);
198+
return $this->getStringReader()->read($this, $length);
159199
}
160200

161201
/**
@@ -164,7 +204,7 @@ public function readString($length)
164204
*/
165205
public function readAlignedString($length)
166206
{
167-
return String::readAligned($this, $length);
207+
return $this->getStringReader()->readAligned($this, $length);
168208
}
169209

170210
/**
@@ -295,4 +335,76 @@ public function getCurrentBit()
295335
{
296336
return $this->currentBit;
297337
}
338+
339+
/**
340+
* @return \PhpBinaryReader\Type\Bit
341+
*/
342+
public function getBitReader()
343+
{
344+
if (!$this->bitReader instanceof Bit) {
345+
$this->bitReader = new Bit();
346+
}
347+
348+
return $this->bitReader;
349+
}
350+
351+
/**
352+
* @return \PhpBinaryReader\Type\Byte
353+
*/
354+
public function getByteReader()
355+
{
356+
if (!$this->byteReader instanceof Byte) {
357+
$this->byteReader = new Byte();
358+
}
359+
360+
return $this->byteReader;
361+
}
362+
363+
/**
364+
* @return \PhpBinaryReader\Type\Int8
365+
*/
366+
public function getInt8Reader()
367+
{
368+
if (!$this->int8Reader instanceof Int8) {
369+
$this->int8Reader = new Int8();
370+
}
371+
372+
return $this->int8Reader;
373+
}
374+
375+
/**
376+
* @return \PhpBinaryReader\Type\Int16
377+
*/
378+
public function getInt16Reader()
379+
{
380+
if (!$this->int16Reader instanceof Int16) {
381+
$this->int16Reader = new Int16();
382+
}
383+
384+
return $this->int16Reader;
385+
}
386+
387+
/**
388+
* @return \PhpBinaryReader\Type\Int32
389+
*/
390+
public function getInt32Reader()
391+
{
392+
if (!$this->int32Reader instanceof Int32) {
393+
$this->int32Reader = new Int32();
394+
}
395+
396+
return $this->int32Reader;
397+
}
398+
399+
/**
400+
* @return \PhpBinaryReader\Type\String
401+
*/
402+
public function getStringReader()
403+
{
404+
if (!$this->stringReader instanceof String) {
405+
$this->stringReader = new String();
406+
}
407+
408+
return $this->stringReader;
409+
}
298410
}

src/BitMask.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BitMask
1212
/**
1313
* @var array
1414
*/
15-
private static $bitMasks = [
15+
private $bitMasks = [
1616
[0x00, 0xFF],
1717
[0x01, 0x7F],
1818
[0x03, 0x3F],
@@ -27,9 +27,9 @@ class BitMask
2727
/**
2828
* @return array
2929
*/
30-
public static function getBitMasks()
30+
public function getBitMasks()
3131
{
32-
return self::$bitMasks;
32+
return $this->bitMasks;
3333
}
3434

3535
/**
@@ -38,14 +38,14 @@ public static function getBitMasks()
3838
* @return mixed
3939
* @throws Exception\InvalidDataException
4040
*/
41-
public static function getMask($bit, $type)
41+
public function getMask($bit, $type)
4242
{
4343
$bit = (int) $bit >= 0 && (int) $bit <= 8 ? $bit : 0;
4444

4545
if ($type == self::MASK_LO) {
46-
return self::getBitMasks()[$bit][self::MASK_LO];
46+
return $this->getBitMasks()[$bit][self::MASK_LO];
4747
} elseif ($type == self::MASK_HI) {
48-
return self::getBitMasks()[$bit][self::MASK_HI];
48+
return $this->getBitMasks()[$bit][self::MASK_HI];
4949
} else {
5050
throw new InvalidDataException('You can only request a lo or hi bit mask using this method');
5151
}

src/Endian.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ class Endian
66
{
77
const ENDIAN_BIG = 1;
88
const ENDIAN_LITTLE = 2;
9+
const BIG = 1;
10+
const LITTLE = 2;
911

1012
/**
1113
* Converts the endianess of a number from big to little or vise-versa
1214
*
1315
* @param int $value
1416
* @return int
1517
*/
16-
public static function convert($value)
18+
public function convert($value)
1719
{
1820
$data = dechex($value);
1921

src/Type/Bit.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Bit implements TypeInterface
1111
/**
1212
* @var bool
1313
*/
14-
private static $signed = false;
14+
private $signed = false;
1515

1616
/**
1717
* Returns an unsigned integer from the bit level
@@ -22,7 +22,7 @@ class Bit implements TypeInterface
2222
* @throws InvalidDataException
2323
* @return int
2424
*/
25-
public static function read(BinaryReader &$br, $length)
25+
public function read(BinaryReader &$br, $length)
2626
{
2727
if (!is_int($length)) {
2828
throw new InvalidDataException('The length parameter must be an integer');
@@ -32,6 +32,7 @@ public static function read(BinaryReader &$br, $length)
3232
throw new \OutOfBoundsException('Cannot read bits, it exceeds the boundary of the file');
3333
}
3434

35+
$bitmask = new BitMask();
3536
$result = 0;
3637
$bits = $length;
3738
$shift = $br->getCurrentBit();
@@ -45,7 +46,7 @@ public static function read(BinaryReader &$br, $length)
4546
} elseif ($bitsLeft > $bits) {
4647
$br->setCurrentBit($br->getCurrentBit() + $bits);
4748

48-
return ($br->getNextByte() >> $shift) & BitMask::getMask($bits, BitMask::MASK_LO);
49+
return ($br->getNextByte() >> $shift) & $bitmask->getMask($bits, BitMask::MASK_LO);
4950
} else {
5051
$br->setCurrentBit(0);
5152

@@ -58,27 +59,27 @@ public static function read(BinaryReader &$br, $length)
5859

5960
if ($bytes == 1) {
6061
$bits -= 8;
61-
$result |= (self::$signed ? $br->readInt8() : $br->readUInt8()) << $bits;
62+
$result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << $bits;
6263
} elseif ($bytes == 2) {
6364
$bits -= 16;
64-
$result |= (self::$signed ? $br->readInt16() : $br->readUInt16()) << $bits;
65+
$result |= ($this->getSigned() ? $br->readInt16() : $br->readUInt16()) << $bits;
6566
} elseif ($bytes == 4) {
6667
$bits -= 32;
67-
$result |= (self::$signed ? $br->readInt32() : $br->readUInt32()) << $bits;
68+
$result |= ($this->getSigned() ? $br->readInt32() : $br->readUInt32()) << $bits;
6869
} else {
6970
while ($bits > 8) {
7071
$bits -= 8;
71-
$result |= (self::$signed ? $br->readInt8() : $br->readUInt8()) << 8;
72+
$result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << 8;
7273
}
7374
}
7475
}
7576

7677
if ($bits != 0) {
77-
$code = self::$signed ? 'c' : 'C';
78+
$code = $this->getSigned() ? 'c' : 'C';
7879
$data = unpack($code, substr($br->getInputString(), $br->getPosition(), 1));
7980
$br->setNextByte($data[1]);
8081
$br->setPosition($br->getPosition() + 1);
81-
$result |= $br->getNextByte() & BitMask::getMask($bits, BitMask::MASK_LO);
82+
$result |= $br->getNextByte() & $bitmask->getMask($bits, BitMask::MASK_LO);
8283
}
8384

8485
$br->setCurrentBit($bits);
@@ -93,12 +94,28 @@ public static function read(BinaryReader &$br, $length)
9394
* @param int $length
9495
* @return int
9596
*/
96-
public static function readSigned(&$br, $length)
97+
public function readSigned(&$br, $length)
9798
{
98-
self::$signed = true;
99-
$value = self::read($br, $length);
100-
self::$signed = false;
99+
$this->setSigned(true);
100+
$value = $this->read($br, $length);
101+
$this->setSigned(false);
101102

102103
return $value;
103104
}
105+
106+
/**
107+
* @param boolean $signed
108+
*/
109+
public function setSigned($signed)
110+
{
111+
$this->signed = $signed;
112+
}
113+
114+
/**
115+
* @return boolean
116+
*/
117+
public function getSigned()
118+
{
119+
return $this->signed;
120+
}
104121
}

0 commit comments

Comments
 (0)