| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class BitBuffer |
| 5 |
* |
| 6 |
* @created 25.11.2015 |
| 7 |
* @author Smiley <smiley@chillerlan.net> |
| 8 |
* @copyright 2015 Smiley |
| 9 |
* @license MIT |
| 10 |
*/ |
| 11 |
namespace WCPOS\Vendor\chillerlan\QRCode\Common; |
| 12 |
|
| 13 |
use WCPOS\Vendor\chillerlan\QRCode\QRCodeException; |
| 14 |
use function count, floor, min; |
| 15 |
/** |
| 16 |
* Holds the raw binary data |
| 17 |
*/ |
| 18 |
final class BitBuffer |
| 19 |
{ |
| 20 |
/** |
| 21 |
* The buffer content |
| 22 |
* |
| 23 |
* @var int[] |
| 24 |
*/ |
| 25 |
private array $buffer; |
| 26 |
/** |
| 27 |
* Length of the content (bits) |
| 28 |
*/ |
| 29 |
private int $length; |
| 30 |
/** |
| 31 |
* Read count (bytes) |
| 32 |
*/ |
| 33 |
private int $bytesRead = 0; |
| 34 |
/** |
| 35 |
* Read count (bits) |
| 36 |
*/ |
| 37 |
private int $bitsRead = 0; |
| 38 |
/** |
| 39 |
* BitBuffer constructor. |
| 40 |
* |
| 41 |
* @param int[] $bytes |
| 42 |
*/ |
| 43 |
public function __construct(array $bytes = []) |
| 44 |
{ |
| 45 |
$this->buffer = $bytes; |
| 46 |
$this->length = count($this->buffer); |
| 47 |
} |
| 48 |
/** |
| 49 |
* appends a sequence of bits |
| 50 |
*/ |
| 51 |
public function put(int $bits, int $length) : self |
| 52 |
{ |
| 53 |
for ($i = 0; $i < $length; $i++) { |
| 54 |
$this->putBit(($bits >> $length - $i - 1 & 1) === 1); |
| 55 |
} |
| 56 |
return $this; |
| 57 |
} |
| 58 |
/** |
| 59 |
* appends a single bit |
| 60 |
*/ |
| 61 |
public function putBit(bool $bit) : self |
| 62 |
{ |
| 63 |
$bufIndex = (int) floor($this->length / 8); |
| 64 |
if (count($this->buffer) <= $bufIndex) { |
| 65 |
$this->buffer[] = 0; |
| 66 |
} |
| 67 |
if ($bit === \true) { |
| 68 |
$this->buffer[$bufIndex] |= 0x80 >> $this->length % 8; |
| 69 |
} |
| 70 |
$this->length++; |
| 71 |
return $this; |
| 72 |
} |
| 73 |
/** |
| 74 |
* returns the current buffer length |
| 75 |
*/ |
| 76 |
public function getLength() : int |
| 77 |
{ |
| 78 |
return $this->length; |
| 79 |
} |
| 80 |
/** |
| 81 |
* returns the buffer content |
| 82 |
* |
| 83 |
* to debug: array_map(fn($v) => sprintf('%08b', $v), $bitBuffer->getBuffer()) |
| 84 |
*/ |
| 85 |
public function getBuffer() : array |
| 86 |
{ |
| 87 |
return $this->buffer; |
| 88 |
} |
| 89 |
/** |
| 90 |
* @return int number of bits that can be read successfully |
| 91 |
*/ |
| 92 |
public function available() : int |
| 93 |
{ |
| 94 |
return 8 * ($this->length - $this->bytesRead) - $this->bitsRead; |
| 95 |
} |
| 96 |
/** |
| 97 |
* @author Sean Owen, ZXing |
| 98 |
* |
| 99 |
* @param int $numBits number of bits to read |
| 100 |
* |
| 101 |
* @return int representing the bits read. The bits will appear as the least-significant bits of the int |
| 102 |
* @throws \chillerlan\QRCode\QRCodeException if numBits isn't in [1,32] or more than is available |
| 103 |
*/ |
| 104 |
public function read(int $numBits) : int |
| 105 |
{ |
| 106 |
if ($numBits < 1 || $numBits > $this->available()) { |
| 107 |
throw new QRCodeException('invalid $numBits: ' . $numBits); |
| 108 |
} |
| 109 |
$result = 0; |
| 110 |
// First, read remainder from current byte |
| 111 |
if ($this->bitsRead > 0) { |
| 112 |
$bitsLeft = 8 - $this->bitsRead; |
| 113 |
$toRead = min($numBits, $bitsLeft); |
| 114 |
$bitsToNotRead = $bitsLeft - $toRead; |
| 115 |
$mask = 0xff >> 8 - $toRead << $bitsToNotRead; |
| 116 |
$result = ($this->buffer[$this->bytesRead] & $mask) >> $bitsToNotRead; |
| 117 |
$numBits -= $toRead; |
| 118 |
$this->bitsRead += $toRead; |
| 119 |
if ($this->bitsRead === 8) { |
| 120 |
$this->bitsRead = 0; |
| 121 |
$this->bytesRead++; |
| 122 |
} |
| 123 |
} |
| 124 |
// Next read whole bytes |
| 125 |
if ($numBits > 0) { |
| 126 |
while ($numBits >= 8) { |
| 127 |
$result = $result << 8 | $this->buffer[$this->bytesRead] & 0xff; |
| 128 |
$this->bytesRead++; |
| 129 |
$numBits -= 8; |
| 130 |
} |
| 131 |
// Finally read a partial byte |
| 132 |
if ($numBits > 0) { |
| 133 |
$bitsToNotRead = 8 - $numBits; |
| 134 |
$mask = 0xff >> $bitsToNotRead << $bitsToNotRead; |
| 135 |
$result = $result << $numBits | ($this->buffer[$this->bytesRead] & $mask) >> $bitsToNotRead; |
| 136 |
$this->bitsRead += $numBits; |
| 137 |
} |
| 138 |
} |
| 139 |
return $result; |
| 140 |
} |
| 141 |
/** |
| 142 |
* Clears the buffer and resets the stats |
| 143 |
*/ |
| 144 |
public function clear() : self |
| 145 |
{ |
| 146 |
$this->buffer = []; |
| 147 |
$this->length = 0; |
| 148 |
return $this->rewind(); |
| 149 |
} |
| 150 |
/** |
| 151 |
* Resets the read-counters |
| 152 |
*/ |
| 153 |
public function rewind() : self |
| 154 |
{ |
| 155 |
$this->bytesRead = 0; |
| 156 |
$this->bitsRead = 0; |
| 157 |
return $this; |
| 158 |
} |
| 159 |
} |
| 160 |
|