123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
-
-
- namespace Zxing\Common;
-
-
- final class BitSource
- {
-
- private $bytes;
- private $byteOffset = 0;
- private $bitOffset = 0;
-
-
-
- public function __construct($bytes)
- {
- $this->bytes = $bytes;
- }
-
-
-
- public function getBitOffset()
- {
- return $this->bitOffset;
- }
-
-
-
- public function getByteOffset()
- {
- return $this->byteOffset;
- }
-
-
-
- public function readBits($numBits)
- {
- if ($numBits < 1 || $numBits > 32 || $numBits > $this->available()) {
- throw new \InvalidArgumentException(strval($numBits));
- }
-
- $result = 0;
-
-
- if ($this->bitOffset > 0) {
- $bitsLeft = 8 - $this->bitOffset;
- $toRead = $numBits < $bitsLeft ? $numBits : $bitsLeft;
- $bitsToNotRead = $bitsLeft - $toRead;
- $mask = (0xFF >> (8 - $toRead)) << $bitsToNotRead;
- $result = ($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead;
- $numBits -= $toRead;
- $this->bitOffset += $toRead;
- if ($this->bitOffset == 8) {
- $this->bitOffset = 0;
- $this->byteOffset++;
- }
- }
-
-
- if ($numBits > 0) {
- while ($numBits >= 8) {
- $result = ($result << 8) | ($this->bytes[$this->byteOffset] & 0xFF);
- $this->byteOffset++;
- $numBits -= 8;
- }
-
-
- if ($numBits > 0) {
- $bitsToNotRead = 8 - $numBits;
- $mask = (0xFF >> $bitsToNotRead) << $bitsToNotRead;
- $result = ($result << $numBits) | (($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead);
- $this->bitOffset += $numBits;
- }
- }
-
- return $result;
- }
-
-
-
- public function available()
- {
- return 8 * (count($this->bytes) - $this->byteOffset) - $this->bitOffset;
- }
- }
|