1 | <?php |
||||
2 | /* |
||||
3 | * PHP QR Code encoder |
||||
4 | * |
||||
5 | * Input encoding class |
||||
6 | * |
||||
7 | * Based on libqrencode C library distributed under LGPL 2.1 |
||||
8 | * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <[email protected]> |
||||
9 | * |
||||
10 | * PHP QR Code is distributed under LGPL 3 |
||||
11 | * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm> |
||||
12 | * |
||||
13 | * This library is free software; you can redistribute it and/or |
||||
14 | * modify it under the terms of the GNU Lesser General Public |
||||
15 | * License as published by the Free Software Foundation; either |
||||
16 | * version 3 of the License, or any later version. |
||||
17 | * |
||||
18 | * This library is distributed in the hope that it will be useful, |
||||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
21 | * Lesser General Public License for more details. |
||||
22 | * |
||||
23 | * You should have received a copy of the GNU Lesser General Public |
||||
24 | * License along with this library; if not, write to the Free Software |
||||
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||||
26 | */ |
||||
27 | |||||
28 | define('STRUCTURE_HEADER_BITS', 20); |
||||
29 | define('MAX_STRUCTURED_SYMBOLS', 16); |
||||
30 | |||||
31 | class QRinputItem { |
||||
32 | |||||
33 | public $mode; |
||||
34 | public $size; |
||||
35 | public $data; |
||||
36 | public $bstream; |
||||
37 | |||||
38 | public function __construct($mode, $size, $data, $bstream = null) |
||||
39 | { |
||||
40 | $setData = array_slice($data, 0, $size); |
||||
41 | |||||
42 | if (count($setData) < $size) { |
||||
43 | $setData = array_merge($setData, array_fill(0, $size - count($setData), 0)); |
||||
44 | } |
||||
45 | |||||
46 | if (!QRinput::check($mode, $size, $setData)) { |
||||
47 | throw new Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',', $setData)); |
||||
48 | return null; |
||||
0 ignored issues
–
show
|
|||||
49 | } |
||||
50 | |||||
51 | $this->mode = $mode; |
||||
52 | $this->size = $size; |
||||
53 | $this->data = $setData; |
||||
54 | $this->bstream = $bstream; |
||||
55 | } |
||||
56 | |||||
57 | //---------------------------------------------------------------------- |
||||
58 | public function encodeModeNum($version) |
||||
59 | { |
||||
60 | try { |
||||
61 | |||||
62 | $words = (int) ($this->size / 3); |
||||
63 | $bs = new QRbitstream(); |
||||
64 | |||||
65 | $val = 0x1; |
||||
66 | $bs->appendNum(4, $val); |
||||
67 | $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size); |
||||
68 | |||||
69 | for ($i = 0; $i < $words; $i++) { |
||||
70 | $val = (ord($this->data[$i * 3]) - ord('0')) * 100; |
||||
71 | $val += (ord($this->data[$i * 3 + 1]) - ord('0')) * 10; |
||||
72 | $val += (ord($this->data[$i * 3 + 2]) - ord('0')); |
||||
73 | $bs->appendNum(10, $val); |
||||
74 | } |
||||
75 | |||||
76 | if ($this->size - $words * 3 == 1) { |
||||
77 | $val = ord($this->data[$words * 3]) - ord('0'); |
||||
78 | $bs->appendNum(4, $val); |
||||
79 | } else if ($this->size - $words * 3 == 2) { |
||||
80 | $val = (ord($this->data[$words * 3]) - ord('0')) * 10; |
||||
81 | $val += (ord($this->data[$words * 3 + 1]) - ord('0')); |
||||
82 | $bs->appendNum(7, $val); |
||||
83 | } |
||||
84 | |||||
85 | $this->bstream = $bs; |
||||
86 | return 0; |
||||
87 | |||||
88 | } catch (Exception $e) { |
||||
89 | return -1; |
||||
90 | } |
||||
91 | } |
||||
92 | |||||
93 | //---------------------------------------------------------------------- |
||||
94 | public function encodeModeAn($version) |
||||
95 | { |
||||
96 | try { |
||||
97 | $words = (int) ($this->size / 2); |
||||
98 | $bs = new QRbitstream(); |
||||
99 | |||||
100 | $bs->appendNum(4, 0x02); |
||||
101 | $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size); |
||||
102 | |||||
103 | for ($i = 0; $i < $words; $i++) { |
||||
104 | $val = (int) QRinput::lookAnTable(ord($this->data[$i * 2])) * 45; |
||||
105 | $val += (int) QRinput::lookAnTable(ord($this->data[$i * 2 + 1])); |
||||
106 | |||||
107 | $bs->appendNum(11, $val); |
||||
108 | } |
||||
109 | |||||
110 | if ($this->size & 1) { |
||||
111 | $val = QRinput::lookAnTable(ord($this->data[$words * 2])); |
||||
112 | $bs->appendNum(6, $val); |
||||
113 | } |
||||
114 | |||||
115 | $this->bstream = $bs; |
||||
116 | return 0; |
||||
117 | |||||
118 | } catch (Exception $e) { |
||||
119 | return -1; |
||||
120 | } |
||||
121 | } |
||||
122 | |||||
123 | //---------------------------------------------------------------------- |
||||
124 | public function encodeMode8($version) |
||||
125 | { |
||||
126 | try { |
||||
127 | $bs = new QRbitstream(); |
||||
128 | |||||
129 | $bs->appendNum(4, 0x4); |
||||
130 | $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size); |
||||
131 | |||||
132 | for ($i = 0; $i < $this->size; $i++) { |
||||
133 | $bs->appendNum(8, ord($this->data[$i])); |
||||
134 | } |
||||
135 | |||||
136 | $this->bstream = $bs; |
||||
137 | return 0; |
||||
138 | |||||
139 | } catch (Exception $e) { |
||||
140 | return -1; |
||||
141 | } |
||||
142 | } |
||||
143 | |||||
144 | //---------------------------------------------------------------------- |
||||
145 | public function encodeModeKanji($version) |
||||
146 | { |
||||
147 | try { |
||||
148 | |||||
149 | $bs = new QRbitrtream(); |
||||
0 ignored issues
–
show
The type
QRbitrtream was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
150 | |||||
151 | $bs->appendNum(4, 0x8); |
||||
152 | $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int) ($this->size / 2)); |
||||
153 | |||||
154 | for ($i = 0; $i < $this->size; $i += 2) { |
||||
155 | $val = (ord($this->data[$i]) << 8) | ord($this->data[$i + 1]); |
||||
156 | if ($val <= 0x9ffc) { |
||||
157 | $val -= 0x8140; |
||||
158 | } else { |
||||
159 | $val -= 0xc140; |
||||
160 | } |
||||
161 | |||||
162 | $h = ($val >> 8) * 0xc0; |
||||
163 | $val = ($val & 0xff) + $h; |
||||
164 | |||||
165 | $bs->appendNum(13, $val); |
||||
166 | } |
||||
167 | |||||
168 | $this->bstream = $bs; |
||||
169 | return 0; |
||||
170 | |||||
171 | } catch (Exception $e) { |
||||
172 | return -1; |
||||
173 | } |
||||
174 | } |
||||
175 | |||||
176 | //---------------------------------------------------------------------- |
||||
177 | public function encodeModeStructure() |
||||
178 | { |
||||
179 | try { |
||||
180 | $bs = new QRbitstream(); |
||||
181 | |||||
182 | $bs->appendNum(4, 0x03); |
||||
183 | $bs->appendNum(4, ord($this->data[1]) - 1); |
||||
184 | $bs->appendNum(4, ord($this->data[0]) - 1); |
||||
185 | $bs->appendNum(8, ord($this->data[2])); |
||||
186 | |||||
187 | $this->bstream = $bs; |
||||
188 | return 0; |
||||
189 | |||||
190 | } catch (Exception $e) { |
||||
191 | return -1; |
||||
192 | } |
||||
193 | } |
||||
194 | |||||
195 | //---------------------------------------------------------------------- |
||||
196 | public function estimateBitStreamSizeOfEntry($version) |
||||
197 | { |
||||
198 | $bits = 0; |
||||
199 | |||||
200 | if ($version == 0) { |
||||
201 | $version = 1; |
||||
202 | } |
||||
203 | |||||
204 | switch ($this->mode) { |
||||
205 | case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size); break; |
||||
206 | case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size); break; |
||||
207 | case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size); break; |
||||
208 | case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size); break; |
||||
0 ignored issues
–
show
The method
QRinput::estimateBitsModeKanji() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
209 | case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS; |
||||
210 | default: |
||||
211 | return 0; |
||||
212 | } |
||||
213 | |||||
214 | $l = QRspec::lengthIndicator($this->mode, $version); |
||||
215 | $m = 1 << $l; |
||||
216 | $num = (int) (($this->size + $m - 1) / $m); |
||||
217 | |||||
218 | $bits += $num * (4 + $l); |
||||
219 | |||||
220 | return $bits; |
||||
221 | } |
||||
222 | |||||
223 | //---------------------------------------------------------------------- |
||||
224 | public function encodeBitStream($version) |
||||
225 | { |
||||
226 | try { |
||||
227 | |||||
228 | unset($this->bstream); |
||||
229 | $words = QRspec::maximumWords($this->mode, $version); |
||||
230 | |||||
231 | if ($this->size > $words) { |
||||
232 | |||||
233 | $st1 = new QRinputItem($this->mode, $words, $this->data); |
||||
234 | $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words)); |
||||
235 | |||||
236 | $st1->encodeBitStream($version); |
||||
237 | $st2->encodeBitStream($version); |
||||
238 | |||||
239 | $this->bstream = new QRbitstream(); |
||||
240 | $this->bstream->append($st1->bstream); |
||||
241 | $this->bstream->append($st2->bstream); |
||||
242 | |||||
243 | unset($st1); |
||||
244 | unset($st2); |
||||
245 | |||||
246 | } else { |
||||
247 | |||||
248 | $ret = 0; |
||||
249 | |||||
250 | switch ($this->mode) { |
||||
251 | case QR_MODE_NUM: $ret = $this->encodeModeNum($version); break; |
||||
252 | case QR_MODE_AN: $ret = $this->encodeModeAn($version); break; |
||||
253 | case QR_MODE_8: $ret = $this->encodeMode8($version); break; |
||||
254 | case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version); break; |
||||
255 | case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure(); break; |
||||
256 | |||||
257 | default: |
||||
258 | break; |
||||
259 | } |
||||
260 | |||||
261 | if ($ret < 0) { |
||||
262 | return -1; |
||||
263 | } |
||||
264 | } |
||||
265 | |||||
266 | return $this->bstream->size(); |
||||
267 | |||||
268 | } catch (Exception $e) { |
||||
269 | return -1; |
||||
270 | } |
||||
271 | } |
||||
272 | }; |
||||
273 | |||||
274 | //########################################################################## |
||||
275 | |||||
276 | class QRinput { |
||||
277 | |||||
278 | public $items; |
||||
279 | |||||
280 | private $version; |
||||
281 | private $level; |
||||
282 | |||||
283 | //---------------------------------------------------------------------- |
||||
284 | public function __construct($version = 0, $level = QR_ECLEVEL_L) |
||||
285 | { |
||||
286 | if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) { |
||||
287 | throw new Exception('Invalid version no'); |
||||
288 | return null; |
||||
0 ignored issues
–
show
return null is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
289 | } |
||||
290 | |||||
291 | $this->version = $version; |
||||
292 | $this->level = $level; |
||||
293 | } |
||||
294 | |||||
295 | //---------------------------------------------------------------------- |
||||
296 | public function getVersion() |
||||
297 | { |
||||
298 | return $this->version; |
||||
299 | } |
||||
300 | |||||
301 | //---------------------------------------------------------------------- |
||||
302 | public function setVersion($version) |
||||
303 | { |
||||
304 | if ($version < 0 || $version > QRSPEC_VERSION_MAX) { |
||||
305 | throw new Exception('Invalid version no'); |
||||
306 | return -1; |
||||
0 ignored issues
–
show
return -1 is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
307 | } |
||||
308 | |||||
309 | $this->version = $version; |
||||
310 | |||||
311 | return 0; |
||||
312 | } |
||||
313 | |||||
314 | //---------------------------------------------------------------------- |
||||
315 | public function getErrorCorrectionLevel() |
||||
316 | { |
||||
317 | return $this->level; |
||||
318 | } |
||||
319 | |||||
320 | //---------------------------------------------------------------------- |
||||
321 | public function setErrorCorrectionLevel($level) |
||||
322 | { |
||||
323 | if ($level > QR_ECLEVEL_H) { |
||||
324 | throw new Exception('Invalid ECLEVEL'); |
||||
325 | return -1; |
||||
0 ignored issues
–
show
return -1 is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
326 | } |
||||
327 | |||||
328 | $this->level = $level; |
||||
329 | |||||
330 | return 0; |
||||
331 | } |
||||
332 | |||||
333 | //---------------------------------------------------------------------- |
||||
334 | public function appendEntry(QRinputItem $entry) |
||||
335 | { |
||||
336 | $this->items[] = $entry; |
||||
337 | } |
||||
338 | |||||
339 | //---------------------------------------------------------------------- |
||||
340 | public function append($mode, $size, $data) |
||||
341 | { |
||||
342 | try { |
||||
343 | $entry = new QRinputItem($mode, $size, $data); |
||||
344 | $this->items[] = $entry; |
||||
345 | return 0; |
||||
346 | } catch (Exception $e) { |
||||
347 | return -1; |
||||
348 | } |
||||
349 | } |
||||
350 | |||||
351 | //---------------------------------------------------------------------- |
||||
352 | |||||
353 | public function insertStructuredAppendHeader($size, $index, $parity) |
||||
354 | { |
||||
355 | if ($size > MAX_STRUCTURED_SYMBOLS) { |
||||
356 | throw new Exception('insertStructuredAppendHeader wrong size'); |
||||
357 | } |
||||
358 | |||||
359 | if ($index <= 0 || $index > MAX_STRUCTURED_SYMBOLS) { |
||||
360 | throw new Exception('insertStructuredAppendHeader wrong index'); |
||||
361 | } |
||||
362 | |||||
363 | $buf = array($size, $index, $parity); |
||||
0 ignored issues
–
show
|
|||||
364 | |||||
365 | try { |
||||
366 | $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf); |
||||
0 ignored issues
–
show
|
|||||
367 | array_unshift($this->items, $entry); |
||||
368 | return 0; |
||||
369 | } catch (Exception $e) { |
||||
370 | return -1; |
||||
371 | } |
||||
372 | } |
||||
373 | |||||
374 | //---------------------------------------------------------------------- |
||||
375 | public function calcParity() |
||||
376 | { |
||||
377 | $parity = 0; |
||||
378 | |||||
379 | foreach ($this->items as $item) { |
||||
380 | if ($item->mode != QR_MODE_STRUCTURE) { |
||||
381 | for ($i = $item->size - 1; $i >= 0; $i--) { |
||||
382 | $parity ^= $item->data[$i]; |
||||
383 | } |
||||
384 | } |
||||
385 | } |
||||
386 | |||||
387 | return $parity; |
||||
388 | } |
||||
389 | |||||
390 | //---------------------------------------------------------------------- |
||||
391 | public static function checkModeNum($size, $data) |
||||
392 | { |
||||
393 | for ($i = 0; $i < $size; $i++) { |
||||
394 | if ((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))) { |
||||
395 | return false; |
||||
396 | } |
||||
397 | } |
||||
398 | |||||
399 | return true; |
||||
400 | } |
||||
401 | |||||
402 | //---------------------------------------------------------------------- |
||||
403 | public static function estimateBitsModeNum($size) |
||||
404 | { |
||||
405 | $w = (int) $size / 3; |
||||
406 | $bits = $w * 10; |
||||
407 | |||||
408 | switch ($size - $w * 3) { |
||||
409 | case 1: |
||||
410 | $bits += 4; |
||||
411 | break; |
||||
412 | case 2: |
||||
413 | $bits += 7; |
||||
414 | break; |
||||
415 | default: |
||||
416 | break; |
||||
417 | } |
||||
418 | |||||
419 | return $bits; |
||||
420 | } |
||||
421 | |||||
422 | //---------------------------------------------------------------------- |
||||
423 | public static $anTable = array( |
||||
424 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
425 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
426 | 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, |
||||
427 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, |
||||
428 | -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
||||
429 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, |
||||
430 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
431 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
||||
432 | ); |
||||
433 | |||||
434 | //---------------------------------------------------------------------- |
||||
435 | public static function lookAnTable($c) |
||||
436 | { |
||||
437 | return (($c > 127) ?-1 : self::$anTable[$c]); |
||||
438 | } |
||||
439 | |||||
440 | //---------------------------------------------------------------------- |
||||
441 | public static function checkModeAn($size, $data) |
||||
442 | { |
||||
443 | for ($i = 0; $i < $size; $i++) { |
||||
444 | if (self::lookAnTable(ord($data[$i])) == -1) { |
||||
445 | return false; |
||||
446 | } |
||||
447 | } |
||||
448 | |||||
449 | return true; |
||||
450 | } |
||||
451 | |||||
452 | //---------------------------------------------------------------------- |
||||
453 | public static function estimateBitsModeAn($size) |
||||
454 | { |
||||
455 | $w = (int) ($size / 2); |
||||
456 | $bits = $w * 11; |
||||
457 | |||||
458 | if ($size & 1) { |
||||
459 | $bits += 6; |
||||
460 | } |
||||
461 | |||||
462 | return $bits; |
||||
463 | } |
||||
464 | |||||
465 | //---------------------------------------------------------------------- |
||||
466 | public static function estimateBitsMode8($size) |
||||
467 | { |
||||
468 | return $size * 8; |
||||
469 | } |
||||
470 | |||||
471 | //---------------------------------------------------------------------- |
||||
472 | public function estimateBitsModeKanji($size) |
||||
473 | { |
||||
474 | return (int) (($size / 2) * 13); |
||||
475 | } |
||||
476 | |||||
477 | //---------------------------------------------------------------------- |
||||
478 | public static function checkModeKanji($size, $data) |
||||
479 | { |
||||
480 | if ($size & 1) { |
||||
481 | return false; |
||||
482 | } |
||||
483 | |||||
484 | for ($i = 0; $i < $size; $i += 2) { |
||||
485 | $val = (ord($data[$i]) << 8) | ord($data[$i + 1]); |
||||
486 | if ($val < 0x8140 |
||||
487 | || ($val > 0x9ffc && $val < 0xe040) |
||||
488 | || $val > 0xebbf) { |
||||
489 | return false; |
||||
490 | } |
||||
491 | } |
||||
492 | |||||
493 | return true; |
||||
494 | } |
||||
495 | |||||
496 | /*********************************************************************** |
||||
497 | * Validation |
||||
498 | **********************************************************************/ |
||||
499 | |||||
500 | public static function check($mode, $size, $data) |
||||
501 | { |
||||
502 | if ($size <= 0) { |
||||
503 | return false; |
||||
504 | } |
||||
505 | |||||
506 | switch ($mode) { |
||||
507 | case QR_MODE_NUM: return self::checkModeNum($size, $data); break; |
||||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other ![]() |
|||||
508 | case QR_MODE_AN: return self::checkModeAn($size, $data); break; |
||||
509 | case QR_MODE_KANJI: return self::checkModeKanji($size, $data); break; |
||||
510 | case QR_MODE_8: return true; break; |
||||
511 | case QR_MODE_STRUCTURE: return true; break; |
||||
512 | |||||
513 | default: |
||||
514 | break; |
||||
515 | } |
||||
516 | |||||
517 | return false; |
||||
518 | } |
||||
519 | |||||
520 | |||||
521 | //---------------------------------------------------------------------- |
||||
522 | public function estimateBitStreamSize($version) |
||||
523 | { |
||||
524 | $bits = 0; |
||||
525 | |||||
526 | foreach ($this->items as $item) { |
||||
527 | $bits += $item->estimateBitStreamSizeOfEntry($version); |
||||
528 | } |
||||
529 | |||||
530 | return $bits; |
||||
531 | } |
||||
532 | |||||
533 | //---------------------------------------------------------------------- |
||||
534 | public function estimateVersion() |
||||
535 | { |
||||
536 | $version = 0; |
||||
537 | $prev = 0; |
||||
0 ignored issues
–
show
|
|||||
538 | do { |
||||
539 | $prev = $version; |
||||
540 | $bits = $this->estimateBitStreamSize($prev); |
||||
541 | $version = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level); |
||||
542 | if ($version < 0) { |
||||
543 | return -1; |
||||
544 | } |
||||
545 | } while ($version > $prev); |
||||
546 | |||||
547 | return $version; |
||||
548 | } |
||||
549 | |||||
550 | //---------------------------------------------------------------------- |
||||
551 | public static function lengthOfCode($mode, $version, $bits) |
||||
552 | { |
||||
553 | $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version); |
||||
554 | switch ($mode) { |
||||
555 | case QR_MODE_NUM: |
||||
556 | $chunks = (int) ($payload / 10); |
||||
557 | $remain = $payload - $chunks * 10; |
||||
558 | $size = $chunks * 3; |
||||
559 | if ($remain >= 7) { |
||||
560 | $size += 2; |
||||
561 | } else if ($remain >= 4) { |
||||
562 | $size += 1; |
||||
563 | } |
||||
564 | break; |
||||
565 | case QR_MODE_AN: |
||||
566 | $chunks = (int) ($payload / 11); |
||||
567 | $remain = $payload - $chunks * 11; |
||||
568 | $size = $chunks * 2; |
||||
569 | if ($remain >= 6) { |
||||
570 | $size++; |
||||
571 | } |
||||
572 | break; |
||||
573 | case QR_MODE_8: |
||||
574 | $size = (int) ($payload / 8); |
||||
575 | break; |
||||
576 | case QR_MODE_KANJI: |
||||
577 | $size = (int) (($payload / 13) * 2); |
||||
578 | break; |
||||
579 | case QR_MODE_STRUCTURE: |
||||
580 | $size = (int) ($payload / 8); |
||||
581 | break; |
||||
582 | default: |
||||
583 | $size = 0; |
||||
584 | break; |
||||
585 | } |
||||
586 | |||||
587 | $maxsize = QRspec::maximumWords($mode, $version); |
||||
588 | if ($size < 0) { |
||||
589 | $size = 0; |
||||
590 | } |
||||
591 | if ($size > $maxsize) { |
||||
592 | $size = $maxsize; |
||||
593 | } |
||||
594 | |||||
595 | return $size; |
||||
596 | } |
||||
597 | |||||
598 | //---------------------------------------------------------------------- |
||||
599 | public function createBitStream() |
||||
600 | { |
||||
601 | $total = 0; |
||||
602 | |||||
603 | foreach ($this->items as $item) { |
||||
604 | $bits = $item->encodeBitStream($this->version); |
||||
605 | |||||
606 | if ($bits < 0) { |
||||
607 | return -1; |
||||
608 | } |
||||
609 | |||||
610 | $total += $bits; |
||||
611 | } |
||||
612 | |||||
613 | return $total; |
||||
614 | } |
||||
615 | |||||
616 | //---------------------------------------------------------------------- |
||||
617 | public function convertData() |
||||
618 | { |
||||
619 | $ver = $this->estimateVersion(); |
||||
620 | if ($ver > $this->getVersion()) { |
||||
621 | $this->setVersion($ver); |
||||
622 | } |
||||
623 | |||||
624 | for (;;) { |
||||
625 | $bits = $this->createBitStream(); |
||||
626 | |||||
627 | if ($bits < 0) { |
||||
628 | return -1; |
||||
629 | } |
||||
630 | |||||
631 | $ver = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level); |
||||
632 | if ($ver < 0) { |
||||
633 | throw new Exception('WRONG VERSION'); |
||||
634 | return -1; |
||||
0 ignored issues
–
show
return -1 is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
635 | } else if ($ver > $this->getVersion()) { |
||||
636 | $this->setVersion($ver); |
||||
637 | } else { |
||||
638 | break; |
||||
639 | } |
||||
640 | } |
||||
641 | |||||
642 | return 0; |
||||
643 | } |
||||
644 | |||||
645 | //---------------------------------------------------------------------- |
||||
646 | public function appendPaddingBit(&$bstream) |
||||
647 | { |
||||
648 | $bits = $bstream->size(); |
||||
649 | $maxwords = QRspec::getDataLength($this->version, $this->level); |
||||
650 | $maxbits = $maxwords * 8; |
||||
651 | |||||
652 | if ($maxbits == $bits) { |
||||
653 | return 0; |
||||
654 | } |
||||
655 | |||||
656 | if ($maxbits - $bits < 5) { |
||||
657 | return $bstream->appendNum($maxbits - $bits, 0); |
||||
658 | } |
||||
659 | |||||
660 | $bits += 4; |
||||
661 | $words = (int) (($bits + 7) / 8); |
||||
662 | |||||
663 | $padding = new QRbitstream(); |
||||
664 | $ret = $padding->appendNum($words * 8 - $bits + 4, 0); |
||||
665 | |||||
666 | if ($ret < 0) { |
||||
667 | return $ret; |
||||
668 | } |
||||
669 | |||||
670 | $padlen = $maxwords - $words; |
||||
671 | |||||
672 | if ($padlen > 0) { |
||||
673 | |||||
674 | $padbuf = array(); |
||||
675 | for ($i = 0; $i < $padlen; $i++) { |
||||
676 | $padbuf[$i] = ($i & 1) ? 0x11 : 0xec; |
||||
677 | } |
||||
678 | |||||
679 | $ret = $padding->appendBytes($padlen, $padbuf); |
||||
680 | |||||
681 | if ($ret < 0) { |
||||
682 | return $ret; |
||||
683 | } |
||||
684 | |||||
685 | } |
||||
686 | |||||
687 | $ret = $bstream->append($padding); |
||||
688 | |||||
689 | return $ret; |
||||
690 | } |
||||
691 | |||||
692 | //---------------------------------------------------------------------- |
||||
693 | public function mergeBitStream() |
||||
694 | { |
||||
695 | if ($this->convertData() < 0) { |
||||
696 | return null; |
||||
697 | } |
||||
698 | |||||
699 | $bstream = new QRbitstream(); |
||||
700 | |||||
701 | foreach ($this->items as $item) { |
||||
702 | $ret = $bstream->append($item->bstream); |
||||
703 | if ($ret < 0) { |
||||
704 | return null; |
||||
705 | } |
||||
706 | } |
||||
707 | |||||
708 | return $bstream; |
||||
709 | } |
||||
710 | |||||
711 | //---------------------------------------------------------------------- |
||||
712 | public function getBitStream() |
||||
713 | { |
||||
714 | |||||
715 | $bstream = $this->mergeBitStream(); |
||||
716 | |||||
717 | if ($bstream == null) { |
||||
718 | return null; |
||||
719 | } |
||||
720 | |||||
721 | $ret = $this->appendPaddingBit($bstream); |
||||
722 | if ($ret < 0) { |
||||
723 | return null; |
||||
724 | } |
||||
725 | |||||
726 | return $bstream; |
||||
727 | } |
||||
728 | |||||
729 | //---------------------------------------------------------------------- |
||||
730 | public function getByteStream() |
||||
731 | { |
||||
732 | $bstream = $this->getBitStream(); |
||||
0 ignored issues
–
show
Are you sure the assignment to
$bstream is correct as $this->getBitStream() targeting QRinput::getBitStream() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
733 | if ($bstream == null) { |
||||
0 ignored issues
–
show
|
|||||
734 | return null; |
||||
735 | } |
||||
736 | |||||
737 | return $bstream->toByte(); |
||||
738 | } |
||||
739 | } |
||||
740 | |||||
741 | |||||
742 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.