1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QRrawcode |
4
|
|
|
* @author Tinymeng <[email protected]> |
5
|
|
|
* @date: 2019/9/26 18:24 |
6
|
|
|
*/ |
7
|
|
|
namespace tinymeng\code\Gateways\qrcode; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
class QRrawcode |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public $version; |
14
|
|
|
public $datacode = array(); |
15
|
|
|
public $ecccode = array(); |
16
|
|
|
public $blocks; |
17
|
|
|
public $rsblocks = array(); //of RSblock |
18
|
|
|
public $count; |
19
|
|
|
public $dataLength; |
20
|
|
|
public $eccLength; |
21
|
|
|
public $b1; |
22
|
|
|
|
23
|
|
|
//---------------------------------------------------------------------- |
24
|
|
|
public function __construct(QRinput $input) |
25
|
|
|
{ |
26
|
|
|
$spec = array(0,0,0,0,0); |
27
|
|
|
|
28
|
|
|
$this->datacode = $input->getByteStream(); |
|
|
|
|
29
|
|
|
if(is_null($this->datacode)) { |
|
|
|
|
30
|
|
|
throw new Exception('null imput string'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec); |
34
|
|
|
|
35
|
|
|
$this->version = $input->getVersion(); |
36
|
|
|
$this->b1 = QRspec::rsBlockNum1($spec); |
37
|
|
|
$this->dataLength = QRspec::rsDataLength($spec); |
38
|
|
|
$this->eccLength = QRspec::rsEccLength($spec); |
39
|
|
|
$this->ecccode = array_fill(0, $this->eccLength, 0); |
40
|
|
|
$this->blocks = QRspec::rsBlockNum($spec); |
41
|
|
|
|
42
|
|
|
$ret = $this->init($spec); |
43
|
|
|
if($ret < 0) { |
44
|
|
|
throw new Exception('block alloc error'); |
45
|
|
|
return null; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->count = 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//---------------------------------------------------------------------- |
52
|
|
|
public function init(array $spec) |
53
|
|
|
{ |
54
|
|
|
$dl = QRspec::rsDataCodes1($spec); |
55
|
|
|
$el = QRspec::rsEccCodes1($spec); |
56
|
|
|
$rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$blockNo = 0; |
60
|
|
|
$dataPos = 0; |
61
|
|
|
$eccPos = 0; |
62
|
|
|
for($i=0; $i<QRspec::rsBlockNum1($spec); $i++) { |
63
|
|
|
$ecc = array_slice($this->ecccode,$eccPos); |
64
|
|
|
$this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs); |
|
|
|
|
65
|
|
|
$this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc); |
66
|
|
|
|
67
|
|
|
$dataPos += $dl; |
68
|
|
|
$eccPos += $el; |
69
|
|
|
$blockNo++; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if(QRspec::rsBlockNum2($spec) == 0) |
73
|
|
|
return 0; |
74
|
|
|
|
75
|
|
|
$dl = QRspec::rsDataCodes2($spec); |
76
|
|
|
$el = QRspec::rsEccCodes2($spec); |
77
|
|
|
$rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el); |
78
|
|
|
|
79
|
|
|
if($rs == NULL) return -1; |
80
|
|
|
|
81
|
|
|
for($i=0; $i<QRspec::rsBlockNum2($spec); $i++) { |
82
|
|
|
$ecc = array_slice($this->ecccode,$eccPos); |
83
|
|
|
$this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs); |
84
|
|
|
$this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc); |
85
|
|
|
|
86
|
|
|
$dataPos += $dl; |
87
|
|
|
$eccPos += $el; |
88
|
|
|
$blockNo++; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
//---------------------------------------------------------------------- |
95
|
|
|
public function getCode() |
96
|
|
|
{ |
97
|
|
|
$ret; |
|
|
|
|
98
|
|
|
|
99
|
|
|
if($this->count < $this->dataLength) { |
100
|
|
|
$row = $this->count % $this->blocks; |
101
|
|
|
$col = $this->count / $this->blocks; |
102
|
|
|
if($col >= $this->rsblocks[0]->dataLength) { |
103
|
|
|
$row += $this->b1; |
104
|
|
|
} |
105
|
|
|
$ret = $this->rsblocks[$row]->data[$col]; |
106
|
|
|
} else if($this->count < $this->dataLength + $this->eccLength) { |
107
|
|
|
$row = ($this->count - $this->dataLength) % $this->blocks; |
108
|
|
|
$col = ($this->count - $this->dataLength) / $this->blocks; |
109
|
|
|
$ret = $this->rsblocks[$row]->ecc[$col]; |
110
|
|
|
} else { |
111
|
|
|
return 0; |
112
|
|
|
} |
113
|
|
|
$this->count++; |
114
|
|
|
|
115
|
|
|
return $ret; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.