Example #1
0
 /**
  * @param  \PhpBinaryReader\BinaryReader $br
  * @param  int                           $data
  * @return int
  */
 private function bitReader(&$br, $data)
 {
     $bitmask = new BitMask();
     $loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO);
     $hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI);
     $hiBits = $br->getNextByte() & $hiMask;
     $loBits = $data & $loMask;
     $br->setNextByte($data);
     return $hiBits | $loBits;
 }
Example #2
0
 /**
  * Returns an variable number of bytes
  *
  * @param  \PhpBinaryReader\BinaryReader $br
  * @param  int|null                      $length
  * @return string
  * @throws \OutOfBoundsException
  * @throws InvalidDataException
  */
 public function read(BinaryReader &$br, $length = null)
 {
     if (!is_int($length)) {
         throw new InvalidDataException('The length parameter must be an integer');
     }
     $br->align();
     if (!$br->canReadBytes($length)) {
         throw new \OutOfBoundsException('Cannot read bytes, it exceeds the boundary of the file');
     }
     $segment = $br->readFromHandle($length);
     return $segment;
 }
Example #3
0
 /**
  * @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;
 }
Example #4
0
 /**
  * @param  \PhpBinaryReader\BinaryReader $br
  * @param  int                           $length
  * @return string
  */
 public function readAligned(BinaryReader &$br, $length)
 {
     $br->align();
     return $this->read($br, $length);
 }
 /**
  * @param \PhpBinaryReader\BinaryReader $brBig
  * @param \PhpBinaryReader\BinaryReader $brLittle
  *
  * @expectedException \OutOfBoundsException
  * @dataProvider binaryReaders
  */
 public function testOutOfBoundsExceptionIsThrownWithLittleEndian(BinaryReader $brBig, BinaryReader $brLittle)
 {
     $brLittle->readBits(360);
     $this->single->read($brLittle);
 }
Example #6
0
 /**
  * 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;
 }