|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBinaryReader\Type; |
|
4
|
|
|
|
|
5
|
|
|
use PhpBinaryReader\BinaryReader; |
|
6
|
|
|
use PhpBinaryReader\BitMask; |
|
7
|
|
|
use PhpBinaryReader\Endian; |
|
8
|
|
|
|
|
9
|
|
|
class Int64 implements TypeInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $endianBig = 'N'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $endianLittle = 'V'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns an Unsigned 64-bit Integer |
|
23
|
|
|
* |
|
24
|
|
|
* @param \PhpBinaryReader\BinaryReader $br |
|
25
|
|
|
* @param null $length |
|
26
|
|
|
* @return int |
|
27
|
|
|
* @throws \OutOfBoundsException |
|
28
|
|
|
*/ |
|
29
|
20 |
|
public function read(BinaryReader &$br, $length = null) |
|
30
|
|
|
{ |
|
31
|
20 |
|
if (!$br->canReadBytes(8)) { |
|
32
|
4 |
|
throw new \OutOfBoundsException('Cannot read 64-bit int, it exceeds the boundary of the file'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
16 |
|
$endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle; |
|
36
|
16 |
|
$firstSegment = $br->readFromHandle(4); |
|
37
|
16 |
|
$secondSegment = $br->readFromHandle(4); |
|
38
|
|
|
|
|
39
|
16 |
|
$firstHalf = unpack($endian, $firstSegment)[1]; |
|
40
|
16 |
|
$secondHalf = unpack($endian, $secondSegment)[1]; |
|
41
|
|
|
|
|
42
|
16 |
|
if ($br->getEndian() == Endian::ENDIAN_BIG) |
|
43
|
16 |
|
$value = bcadd($secondHalf, bcmul($firstHalf, "4294967296")); |
|
44
|
|
|
else |
|
45
|
10 |
|
$value = bcadd($firstHalf, bcmul($secondHalf, "4294967296")); |
|
46
|
|
|
|
|
47
|
16 |
|
if ($br->getCurrentBit() != 0) { |
|
48
|
4 |
|
$data = $this->bitReader($br, $data); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
12 |
|
return $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a Signed 64-Bit Integer |
|
56
|
|
|
* |
|
57
|
|
|
* @param \PhpBinaryReader\BinaryReader $br |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function readSigned(&$br) |
|
61
|
|
|
{ |
|
62
|
8 |
|
$value = $this->read($br); |
|
63
|
8 |
|
if(bccomp($value, bcpow(2, 63)) >= 0) $value = bcsub($value, bcpow(2, 64)); |
|
64
|
|
|
|
|
65
|
8 |
|
return $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \PhpBinaryReader\BinaryReader $br |
|
70
|
|
|
* @param int $data |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
private function bitReader(&$br, $data) |
|
74
|
|
|
{ |
|
75
|
|
|
$bitmask = new BitMask(); |
|
76
|
|
|
$loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO); |
|
77
|
|
|
$hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI); |
|
78
|
|
|
$hiBits = ($br->getNextByte() & $hiMask) << 56; |
|
79
|
|
|
$miBits = ($data & 0xFFFFFFFFFFFFFF00) >> (8 - $br->getCurrentBit()); |
|
80
|
|
|
$loBits = ($data & $loMask); |
|
81
|
|
|
$br->setNextByte($data & 0xFF); |
|
82
|
|
|
|
|
83
|
|
|
return $hiBits | $miBits | $loBits; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $endianBig |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function setEndianBig($endianBig) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->endianBig = $endianBig; |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getEndianBig() |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->endianBig; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $endianLittle |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function setEndianLittle($endianLittle) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->endianLittle = $endianLittle; |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getEndianLittle() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->endianLittle; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.