|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBinaryReader\Type; |
|
4
|
|
|
|
|
5
|
|
|
use PhpBinaryReader\BinaryReader; |
|
6
|
|
|
|
|
7
|
|
|
class Single implements TypeInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Returns a 4-bytes floating-point |
|
11
|
|
|
* |
|
12
|
|
|
* @param \PhpBinaryReader\BinaryReader $br |
|
13
|
|
|
* @param null $length |
|
14
|
|
|
* |
|
15
|
|
|
* @return float |
|
16
|
|
|
* @throws \OutOfBoundsException |
|
17
|
|
|
*/ |
|
18
|
14 |
|
public function read(BinaryReader &$br, $length = null) |
|
19
|
|
|
{ |
|
20
|
14 |
|
if (!$br->canReadBytes(4)) { |
|
21
|
4 |
|
throw new \OutOfBoundsException('Cannot read 4-bytes floating-point, it exceeds the boundary of the file'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
10 |
|
$segment = $br->readFromHandle(4); |
|
25
|
|
|
|
|
26
|
10 |
|
if ($br->getCurrentBit() !== 0) { |
|
27
|
4 |
|
$data = unpack('N', $segment)[1]; |
|
28
|
4 |
|
$data = $this->bitReader($br, $data); |
|
29
|
|
|
|
|
30
|
4 |
|
$endian = $br->getMachineByteOrder() === $br->getEndian() ? 'N' : 'V'; |
|
|
|
|
|
|
31
|
4 |
|
$segment = pack($endian, $data); |
|
32
|
6 |
|
} elseif ($br->getMachineByteOrder() !== $br->getEndian()) { |
|
|
|
|
|
|
33
|
4 |
|
$segment = pack('N', unpack('V', $segment)[1]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
10 |
|
$value = unpack('f', $segment)[1]; |
|
37
|
|
|
|
|
38
|
10 |
|
return $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param \PhpBinaryReader\BinaryReader $br |
|
43
|
|
|
* @param int $data |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
4 |
|
private function bitReader(BinaryReader $br, $data) |
|
48
|
|
|
{ |
|
49
|
4 |
|
$mask = 0x7FFFFFFF >> ($br->getCurrentBit() - 1); |
|
50
|
4 |
|
$value = (($data >> (8 - $br->getCurrentBit())) & $mask) | ($br->getNextByte() << (24 + $br->getCurrentBit())); |
|
51
|
4 |
|
$br->setNextByte($data & 0xFF); |
|
52
|
|
|
|
|
53
|
4 |
|
return $value; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|