Skip to content

Commit

Permalink
Merge pull request #5 from mdurrant/develop
Browse files Browse the repository at this point in the history
Adding byte method, refactoring out static methods
  • Loading branch information
mdurrant committed Jul 16, 2014
2 parents ad7f838 + bea2f9b commit 080b90f
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 175 deletions.
132 changes: 122 additions & 10 deletions src/BinaryReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpBinaryReader\Exception\InvalidDataException;
use PhpBinaryReader\Type\Bit;
use PhpBinaryReader\Type\Byte;
use PhpBinaryReader\Type\Int8;
use PhpBinaryReader\Type\Int16;
use PhpBinaryReader\Type\Int32;
Expand Down Expand Up @@ -46,6 +47,36 @@ class BinaryReader
*/
private $endian;

/**
* @var \PhpBinaryReader\Type\Byte
*/
private $byteReader;

/**
* @var \PhpBinaryReader\Type\Bit
*/
private $bitReader;

/**
* @var \PhpBinaryReader\Type\String
*/
private $stringReader;

/**
* @var \PhpBinaryReader\Type\Int8
*/
private $int8Reader;

/**
* @var \PhpBinaryReader\Type\Int16
*/
private $int16Reader;

/**
* @var \PhpBinaryReader\Type\Int32
*/
private $int32Reader;

/**
* @param string $str
* @param int|string $endian
Expand Down Expand Up @@ -89,7 +120,7 @@ public function align()
*/
public function readBits($count)
{
return Bit::readSigned($this, $count);
return $this->getBitReader()->readSigned($this, $count);
}

/**
Expand All @@ -98,55 +129,64 @@ public function readBits($count)
*/
public function readUBits($count)
{
return Bit::read($this, $count);
return $this->getBitReader()->read($this, $count);
}

/**
* @param int $count
* @return int
*/
public function readBytes($count)
{
return $this->getByteReader()->read($this, $count);
}

/**
* @return int
*/
public function readInt8()
{
return Int8::readSigned($this);
return $this->getInt8Reader()->readSigned($this);
}

/**
* @return int
*/
public function readUInt8()
{
return Int8::read($this);
return $this->getInt8Reader()->read($this);
}

/**
* @return int
*/
public function readInt16()
{
return Int16::readSigned($this);
return $this->getInt16Reader()->readSigned($this);
}

/**
* @return string
*/
public function readUInt16()
{
return Int16::read($this);
return $this->getInt16Reader()->read($this);
}

/**
* @return int
*/
public function readInt32()
{
return Int32::readSigned($this);
return $this->getInt32Reader()->readSigned($this);
}

/**
* @return int
*/
public function readUInt32()
{
return Int32::read($this);
return $this->getInt32Reader()->read($this);
}

/**
Expand All @@ -155,7 +195,7 @@ public function readUInt32()
*/
public function readString($length)
{
return String::read($this, $length);
return $this->getStringReader()->read($this, $length);
}

/**
Expand All @@ -164,7 +204,7 @@ public function readString($length)
*/
public function readAlignedString($length)
{
return String::readAligned($this, $length);
return $this->getStringReader()->readAligned($this, $length);
}

/**
Expand Down Expand Up @@ -295,4 +335,76 @@ public function getCurrentBit()
{
return $this->currentBit;
}

/**
* @return \PhpBinaryReader\Type\Bit
*/
public function getBitReader()
{
if (!$this->bitReader instanceof Bit) {
$this->bitReader = new Bit();
}

return $this->bitReader;
}

/**
* @return \PhpBinaryReader\Type\Byte
*/
public function getByteReader()
{
if (!$this->byteReader instanceof Byte) {
$this->byteReader = new Byte();
}

return $this->byteReader;
}

/**
* @return \PhpBinaryReader\Type\Int8
*/
public function getInt8Reader()
{
if (!$this->int8Reader instanceof Int8) {
$this->int8Reader = new Int8();
}

return $this->int8Reader;
}

/**
* @return \PhpBinaryReader\Type\Int16
*/
public function getInt16Reader()
{
if (!$this->int16Reader instanceof Int16) {
$this->int16Reader = new Int16();
}

return $this->int16Reader;
}

/**
* @return \PhpBinaryReader\Type\Int32
*/
public function getInt32Reader()
{
if (!$this->int32Reader instanceof Int32) {
$this->int32Reader = new Int32();
}

return $this->int32Reader;
}

/**
* @return \PhpBinaryReader\Type\String
*/
public function getStringReader()
{
if (!$this->stringReader instanceof String) {
$this->stringReader = new String();
}

return $this->stringReader;
}
}
12 changes: 6 additions & 6 deletions src/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BitMask
/**
* @var array
*/
private static $bitMasks = [
private $bitMasks = [
[0x00, 0xFF],
[0x01, 0x7F],
[0x03, 0x3F],
Expand All @@ -27,9 +27,9 @@ class BitMask
/**
* @return array
*/
public static function getBitMasks()
public function getBitMasks()
{
return self::$bitMasks;
return $this->bitMasks;
}

/**
Expand All @@ -38,14 +38,14 @@ public static function getBitMasks()
* @return mixed
* @throws Exception\InvalidDataException
*/
public static function getMask($bit, $type)
public function getMask($bit, $type)
{
$bit = (int) $bit >= 0 && (int) $bit <= 8 ? $bit : 0;

if ($type == self::MASK_LO) {
return self::getBitMasks()[$bit][self::MASK_LO];
return $this->getBitMasks()[$bit][self::MASK_LO];
} elseif ($type == self::MASK_HI) {
return self::getBitMasks()[$bit][self::MASK_HI];
return $this->getBitMasks()[$bit][self::MASK_HI];
} else {
throw new InvalidDataException('You can only request a lo or hi bit mask using this method');
}
Expand Down
4 changes: 3 additions & 1 deletion src/Endian.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class Endian
{
const ENDIAN_BIG = 1;
const ENDIAN_LITTLE = 2;
const BIG = 1;
const LITTLE = 2;

/**
* Converts the endianess of a number from big to little or vise-versa
*
* @param int $value
* @return int
*/
public static function convert($value)
public function convert($value)
{
$data = dechex($value);

Expand Down
43 changes: 30 additions & 13 deletions src/Type/Bit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Bit implements TypeInterface
/**
* @var bool
*/
private static $signed = false;
private $signed = false;

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

$bitmask = new BitMask();
$result = 0;
$bits = $length;
$shift = $br->getCurrentBit();
Expand All @@ -45,7 +46,7 @@ public static function read(BinaryReader &$br, $length)
} elseif ($bitsLeft > $bits) {
$br->setCurrentBit($br->getCurrentBit() + $bits);

return ($br->getNextByte() >> $shift) & BitMask::getMask($bits, BitMask::MASK_LO);
return ($br->getNextByte() >> $shift) & $bitmask->getMask($bits, BitMask::MASK_LO);
} else {
$br->setCurrentBit(0);

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

if ($bytes == 1) {
$bits -= 8;
$result |= (self::$signed ? $br->readInt8() : $br->readUInt8()) << $bits;
$result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << $bits;
} elseif ($bytes == 2) {
$bits -= 16;
$result |= (self::$signed ? $br->readInt16() : $br->readUInt16()) << $bits;
$result |= ($this->getSigned() ? $br->readInt16() : $br->readUInt16()) << $bits;
} elseif ($bytes == 4) {
$bits -= 32;
$result |= (self::$signed ? $br->readInt32() : $br->readUInt32()) << $bits;
$result |= ($this->getSigned() ? $br->readInt32() : $br->readUInt32()) << $bits;
} else {
while ($bits > 8) {
$bits -= 8;
$result |= (self::$signed ? $br->readInt8() : $br->readUInt8()) << 8;
$result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << 8;
}
}
}

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

$br->setCurrentBit($bits);
Expand All @@ -93,12 +94,28 @@ public static function read(BinaryReader &$br, $length)
* @param int $length
* @return int
*/
public static function readSigned(&$br, $length)
public function readSigned(&$br, $length)
{
self::$signed = true;
$value = self::read($br, $length);
self::$signed = false;
$this->setSigned(true);
$value = $this->read($br, $length);
$this->setSigned(false);

return $value;
}

/**
* @param boolean $signed
*/
public function setSigned($signed)
{
$this->signed = $signed;
}

/**
* @return boolean
*/
public function getSigned()
{
return $this->signed;
}
}

0 comments on commit 080b90f

Please sign in to comment.