/** * @param \PhpBinaryReader\BinaryReader $br * @param int $length * @return string * @throws \OutOfBoundsException * @throws InvalidDataException */ public function read(BinaryReader &$br, $length) { if (!is_int($length)) { throw new InvalidDataException('The length parameter must be an integer'); } if (!$br->canReadBytes($length)) { throw new \OutOfBoundsException('Cannot read string, it exceeds the boundary of the file'); } $str = $br->readFromHandle($length); return $str; }
/** * Returns an unsigned integer from the bit level * * @param \PhpBinaryReader\BinaryReader $br * @param int $length * @throws \OutOfBoundsException * @throws InvalidDataException * @return int */ public function read(BinaryReader &$br, $length) { if (!is_int($length)) { throw new InvalidDataException('The length parameter must be an integer'); } $bitmask = new BitMask(); $result = 0; $bits = $length; $shift = $br->getCurrentBit(); if ($shift != 0) { $bitsLeft = 8 - $shift; if ($bitsLeft < $bits) { $bits -= $bitsLeft; $result = $br->getNextByte() >> $shift << $bits; } elseif ($bitsLeft > $bits) { $br->setCurrentBit($br->getCurrentBit() + $bits); return $br->getNextByte() >> $shift & $bitmask->getMask($bits, BitMask::MASK_LO); } else { $br->setCurrentBit(0); return $br->getNextByte() >> $shift; } } if (!$br->canReadBytes($length / 8)) { throw new \OutOfBoundsException('Cannot read bits, it exceeds the boundary of the file'); } if ($bits >= 8) { $bytes = intval($bits / 8); if ($bytes == 1) { $bits -= 8; $result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << $bits; } elseif ($bytes == 2) { $bits -= 16; $result |= ($this->getSigned() ? $br->readInt16() : $br->readUInt16()) << $bits; } elseif ($bytes == 4) { $bits -= 32; $result |= ($this->getSigned() ? $br->readInt32() : $br->readUInt32()) << $bits; } else { while ($bits > 8) { $bits -= 8; $result |= ($this->getSigned() ? $br->readInt8() : $br->readUInt8()) << 8; } } } if ($bits != 0) { $code = $this->getSigned() ? 'c' : 'C'; $data = unpack($code, $br->readFromHandle(1)); $br->setNextByte($data[1]); $result |= $br->getNextByte() & $bitmask->getMask($bits, BitMask::MASK_LO); } $br->setCurrentBit($bits); return $result; }
/** * Returns an Unsigned 8-bit Integer (aka a single byte) * * @param \PhpBinaryReader\BinaryReader $br * @param null $length * @return int * @throws \OutOfBoundsException */ public function read(BinaryReader &$br, $length = null) { if (!$br->canReadBytes(1)) { throw new \OutOfBoundsException('Cannot read 8-bit int, it exceeds the boundary of the file'); } $segment = $br->readFromHandle(1); $data = unpack($this->endian, $segment); $data = $data[1]; if ($br->getCurrentBit() != 0) { $data = $this->bitReader($br, $data); } return $data; }
/** * Returns an Unsigned 16-bit Integer * * @param \PhpBinaryReader\BinaryReader $br * @param null $length * @return int * @throws \OutOfBoundsException */ public function read(BinaryReader &$br, $length = null) { if (!$br->canReadBytes(2)) { throw new \OutOfBoundsException('Cannot read 16-bit int, it exceeds the boundary of the file'); } $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle; $segment = $br->readFromHandle(2); $data = unpack($endian, $segment); $data = $data[1]; if ($br->getCurrentBit() != 0) { $data = $this->bitReader($br, $data); } return $data; }
/** * 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; }