| @@ 9-121 (lines=113) @@ | ||
| 6 | use PhpBinaryReader\BitMask; |
|
| 7 | use PhpBinaryReader\Endian; |
|
| 8 | ||
| 9 | class Int16 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 16-bit Integer |
|
| 23 | * |
|
| 24 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 25 | * @param null $length |
|
| 26 | * @return int |
|
| 27 | * @throws \OutOfBoundsException |
|
| 28 | */ |
|
| 29 | public function read(BinaryReader &$br, $length = null) |
|
| 30 | { |
|
| 31 | if (!$br->canReadBytes(2)) { |
|
| 32 | throw new \OutOfBoundsException('Cannot read 16-bit int, it exceeds the boundary of the file'); |
|
| 33 | } |
|
| 34 | ||
| 35 | $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle; |
|
| 36 | $segment = $br->readFromHandle(2); |
|
| 37 | ||
| 38 | $data = unpack($endian, $segment); |
|
| 39 | $data = $data[1]; |
|
| 40 | ||
| 41 | if ($br->getCurrentBit() != 0) { |
|
| 42 | $data = $this->bitReader($br, $data); |
|
| 43 | } |
|
| 44 | ||
| 45 | return $data; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Returns a Signed 16-bit Integer |
|
| 50 | * |
|
| 51 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 52 | * @return int |
|
| 53 | */ |
|
| 54 | public function readSigned(&$br) |
|
| 55 | { |
|
| 56 | $this->setEndianBig('s'); |
|
| 57 | $this->setEndianLittle('s'); |
|
| 58 | ||
| 59 | $value = $this->read($br); |
|
| 60 | ||
| 61 | $this->setEndianBig('n'); |
|
| 62 | $this->setEndianLittle('v'); |
|
| 63 | ||
| 64 | $endian = new Endian(); |
|
| 65 | if ($br->getMachineByteOrder() != Endian::ENDIAN_LITTLE && $br->getEndian() == Endian::ENDIAN_LITTLE) { |
|
| 66 | return $endian->convert($value); |
|
| 67 | } else { |
|
| 68 | return $value; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 74 | * @param int $data |
|
| 75 | * @return int |
|
| 76 | */ |
|
| 77 | private function bitReader(&$br, $data) |
|
| 78 | { |
|
| 79 | $bitmask = new BitMask(); |
|
| 80 | $loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO); |
|
| 81 | $hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI); |
|
| 82 | $hiBits = ($br->getNextByte() & $hiMask) << 8; |
|
| 83 | $miBits = ($data & 0xFF00) >> (8 - $br->getCurrentBit()); |
|
| 84 | $loBits = ($data & $loMask); |
|
| 85 | $br->setNextByte($data & 0xFF); |
|
| 86 | ||
| 87 | return $hiBits | $miBits | $loBits; |
|
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * @param string $endianBig |
|
| 92 | */ |
|
| 93 | public function setEndianBig($endianBig) |
|
| 94 | { |
|
| 95 | $this->endianBig = $endianBig; |
|
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * @return string |
|
| 100 | */ |
|
| 101 | public function getEndianBig() |
|
| 102 | { |
|
| 103 | return $this->endianBig; |
|
| 104 | } |
|
| 105 | ||
| 106 | /** |
|
| 107 | * @param string $endianLittle |
|
| 108 | */ |
|
| 109 | public function setEndianLittle($endianLittle) |
|
| 110 | { |
|
| 111 | $this->endianLittle = $endianLittle; |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * @return string |
|
| 116 | */ |
|
| 117 | public function getEndianLittle() |
|
| 118 | { |
|
| 119 | return $this->endianLittle; |
|
| 120 | } |
|
| 121 | } |
|
| 122 | ||
| @@ 9-122 (lines=114) @@ | ||
| 6 | use PhpBinaryReader\BitMask; |
|
| 7 | use PhpBinaryReader\Endian; |
|
| 8 | ||
| 9 | class Int32 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 32-bit Integer |
|
| 23 | * |
|
| 24 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 25 | * @param null $length |
|
| 26 | * @return int |
|
| 27 | * @throws \OutOfBoundsException |
|
| 28 | */ |
|
| 29 | public function read(BinaryReader &$br, $length = null) |
|
| 30 | { |
|
| 31 | if (!$br->canReadBytes(4)) { |
|
| 32 | throw new \OutOfBoundsException('Cannot read 32-bit int, it exceeds the boundary of the file'); |
|
| 33 | } |
|
| 34 | ||
| 35 | $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle; |
|
| 36 | $segment = $br->readFromHandle(4); |
|
| 37 | ||
| 38 | $data = unpack($endian, $segment); |
|
| 39 | $data = $data[1]; |
|
| 40 | ||
| 41 | if ($br->getCurrentBit() != 0) { |
|
| 42 | $data = $this->bitReader($br, $data); |
|
| 43 | } |
|
| 44 | ||
| 45 | return $data; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Returns a Signed 32-Bit Integer |
|
| 50 | * |
|
| 51 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 52 | * @return int |
|
| 53 | */ |
|
| 54 | public function readSigned(&$br) |
|
| 55 | { |
|
| 56 | $this->setEndianBig('l'); |
|
| 57 | $this->setEndianLittle('l'); |
|
| 58 | ||
| 59 | $value = $this->read($br); |
|
| 60 | ||
| 61 | $this->setEndianBig('N'); |
|
| 62 | $this->setEndianLittle('V'); |
|
| 63 | ||
| 64 | if ($br->getMachineByteOrder() != Endian::ENDIAN_LITTLE && $br->getEndian() == Endian::ENDIAN_LITTLE) { |
|
| 65 | $endian = new Endian(); |
|
| 66 | ||
| 67 | return $endian->convert($value); |
|
| 68 | } else { |
|
| 69 | return $value; |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 75 | * @param int $data |
|
| 76 | * @return int |
|
| 77 | */ |
|
| 78 | private function bitReader(&$br, $data) |
|
| 79 | { |
|
| 80 | $bitmask = new BitMask(); |
|
| 81 | $loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO); |
|
| 82 | $hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI); |
|
| 83 | $hiBits = ($br->getNextByte() & $hiMask) << 24; |
|
| 84 | $miBits = ($data & 0xFFFFFF00) >> (8 - $br->getCurrentBit()); |
|
| 85 | $loBits = ($data & $loMask); |
|
| 86 | $br->setNextByte($data & 0xFF); |
|
| 87 | ||
| 88 | return $hiBits | $miBits | $loBits; |
|
| 89 | } |
|
| 90 | ||
| 91 | /** |
|
| 92 | * @param string $endianBig |
|
| 93 | */ |
|
| 94 | public function setEndianBig($endianBig) |
|
| 95 | { |
|
| 96 | $this->endianBig = $endianBig; |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * @return string |
|
| 101 | */ |
|
| 102 | public function getEndianBig() |
|
| 103 | { |
|
| 104 | return $this->endianBig; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * @param string $endianLittle |
|
| 109 | */ |
|
| 110 | public function setEndianLittle($endianLittle) |
|
| 111 | { |
|
| 112 | $this->endianLittle = $endianLittle; |
|
| 113 | } |
|
| 114 | ||
| 115 | /** |
|
| 116 | * @return string |
|
| 117 | */ |
|
| 118 | public function getEndianLittle() |
|
| 119 | { |
|
| 120 | return $this->endianLittle; |
|
| 121 | } |
|
| 122 | } |
|
| 123 | ||