-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nsknewbie/feature/float-reader
implemented 4-bytes floating-point reader
- Loading branch information
Showing
13 changed files
with
230 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace PhpBinaryReader\Type; | ||
|
||
use PhpBinaryReader\BinaryReader; | ||
|
||
class Single implements TypeInterface | ||
{ | ||
/** | ||
* Returns a 4-bytes floating-point | ||
* | ||
* @param \PhpBinaryReader\BinaryReader $br | ||
* @param null $length | ||
* | ||
* @return float | ||
* @throws \OutOfBoundsException | ||
*/ | ||
public function read(BinaryReader &$br, $length = null) | ||
{ | ||
if (!$br->canReadBytes(4)) { | ||
throw new \OutOfBoundsException('Cannot read 4-bytes floating-point, it exceeds the boundary of the file'); | ||
} | ||
|
||
$segment = $br->readFromHandle(4); | ||
|
||
if ($br->getCurrentBit() !== 0) { | ||
$data = unpack('N', $segment)[1]; | ||
$data = $this->bitReader($br, $data); | ||
|
||
$endian = $br->getMachineByteOrder() === $br->getEndian() ? 'N' : 'V'; | ||
$segment = pack($endian, $data); | ||
} elseif ($br->getMachineByteOrder() !== $br->getEndian()) { | ||
$segment = pack('N', unpack('V', $segment)[1]); | ||
} | ||
|
||
$value = unpack('f', $segment)[1]; | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @param \PhpBinaryReader\BinaryReader $br | ||
* @param int $data | ||
* | ||
* @return int | ||
*/ | ||
private function bitReader(BinaryReader $br, $data) | ||
{ | ||
$mask = 0x7FFFFFFF >> ($br->getCurrentBit() - 1); | ||
$value = (($data >> (8 - $br->getCurrentBit())) & $mask) | ($br->getNextByte() << (24 + $br->getCurrentBit())); | ||
$br->setNextByte($data & 0xFF); | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.