Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Common/BitBuffer.php

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