1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* PHP QR Code encoder |
4
|
|
|
* |
5
|
|
|
* Bitstream 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
|
|
|
class qrbitstream |
29
|
|
|
{ |
30
|
|
|
public $data = []; |
31
|
|
|
|
32
|
|
|
//---------------------------------------------------------------------- |
33
|
|
|
public function size() |
34
|
|
|
{ |
35
|
|
|
return count($this->data); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//---------------------------------------------------------------------- |
39
|
|
|
public function allocate($setLength) |
40
|
|
|
{ |
41
|
|
|
$this->data = array_fill(0, $setLength, 0); |
42
|
|
|
|
43
|
|
|
return 0; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//---------------------------------------------------------------------- |
47
|
|
|
public static function newFromNum($bits, $num) |
48
|
|
|
{ |
49
|
|
|
$bstream = new self(); |
50
|
|
|
$bstream->allocate($bits); |
51
|
|
|
|
52
|
|
|
$mask = 1 << ($bits - 1); |
53
|
|
|
for ($i = 0; $i < $bits; $i++) { |
54
|
|
|
if ($num & $mask) { |
55
|
|
|
$bstream->data[$i] = 1; |
56
|
|
|
} else { |
57
|
|
|
$bstream->data[$i] = 0; |
58
|
|
|
} |
59
|
|
|
$mask = $mask >> 1; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $bstream; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
//---------------------------------------------------------------------- |
66
|
|
|
public static function newFromBytes($size, $data) |
67
|
|
|
{ |
68
|
|
|
$bstream = new self(); |
69
|
|
|
$bstream->allocate($size * 8); |
70
|
|
|
$p = 0; |
71
|
|
|
|
72
|
|
|
for ($i = 0; $i < $size; $i++) { |
73
|
|
|
$mask = 0x80; |
74
|
|
|
for ($j = 0; $j < 8; $j++) { |
75
|
|
|
if ($data[$i] & $mask) { |
76
|
|
|
$bstream->data[$p] = 1; |
77
|
|
|
} else { |
78
|
|
|
$bstream->data[$p] = 0; |
79
|
|
|
} |
80
|
|
|
$p++; |
81
|
|
|
$mask = $mask >> 1; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $bstream; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//---------------------------------------------------------------------- |
89
|
|
|
public function append(self $arg) |
90
|
|
|
{ |
91
|
|
|
if (is_null($arg)) { |
92
|
|
|
return -1; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($arg->size() == 0) { |
96
|
|
|
return 0; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->size() == 0) { |
100
|
|
|
$this->data = $arg->data; |
101
|
|
|
|
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->data = array_values(array_merge($this->data, $arg->data)); |
106
|
|
|
|
107
|
|
|
return 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
//---------------------------------------------------------------------- |
111
|
|
|
public function appendNum($bits, $num) |
112
|
|
|
{ |
113
|
|
|
if ($bits == 0) { |
114
|
|
|
return 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$b = self::newFromNum($bits, $num); |
118
|
|
|
|
119
|
|
|
if (is_null($b)) { |
120
|
|
|
return -1; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$ret = $this->append($b); |
124
|
|
|
unset($b); |
125
|
|
|
|
126
|
|
|
return $ret; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
//---------------------------------------------------------------------- |
130
|
|
|
public function appendBytes($size, $data) |
131
|
|
|
{ |
132
|
|
|
if ($size == 0) { |
133
|
|
|
return 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$b = self::newFromBytes($size, $data); |
137
|
|
|
|
138
|
|
|
if (is_null($b)) { |
139
|
|
|
return -1; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$ret = $this->append($b); |
143
|
|
|
unset($b); |
144
|
|
|
|
145
|
|
|
return $ret; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
//---------------------------------------------------------------------- |
149
|
|
|
public function toByte() |
150
|
|
|
{ |
151
|
|
|
$size = $this->size(); |
152
|
|
|
|
153
|
|
|
if ($size == 0) { |
154
|
|
|
return []; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$data = array_fill(0, (int) (($size + 7) / 8), 0); |
158
|
|
|
$bytes = (int) ($size / 8); |
159
|
|
|
|
160
|
|
|
$p = 0; |
161
|
|
|
|
162
|
|
|
for ($i = 0; $i < $bytes; $i++) { |
163
|
|
|
$v = 0; |
164
|
|
|
for ($j = 0; $j < 8; $j++) { |
165
|
|
|
$v = $v << 1; |
166
|
|
|
$v |= $this->data[$p]; |
167
|
|
|
$p++; |
168
|
|
|
} |
169
|
|
|
$data[$i] = $v; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($size & 7) { |
173
|
|
|
$v = 0; |
174
|
|
|
for ($j = 0; $j < ($size & 7); $j++) { |
175
|
|
|
$v = $v << 1; |
176
|
|
|
$v |= $this->data[$p]; |
177
|
|
|
$p++; |
178
|
|
|
} |
179
|
|
|
$data[$bytes] = $v; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $data; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|