1 | <?php |
||
2 | /* |
||
3 | * PHP QR Code encoder |
||
4 | * |
||
5 | * Reed-Solomon error correction support |
||
6 | * |
||
7 | * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q |
||
8 | * (libfec is released under the GNU Lesser General Public License.) |
||
9 | * |
||
10 | * Based on libqrencode C library distributed under LGPL 2.1 |
||
11 | * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <[email protected]> |
||
12 | * |
||
13 | * PHP QR Code is distributed under LGPL 3 |
||
14 | * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm> |
||
15 | * |
||
16 | * This library is free software; you can redistribute it and/or |
||
17 | * modify it under the terms of the GNU Lesser General Public |
||
18 | * License as published by the Free Software Foundation; either |
||
19 | * version 3 of the License, or any later version. |
||
20 | * |
||
21 | * This library is distributed in the hope that it will be useful, |
||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
24 | * Lesser General Public License for more details. |
||
25 | * |
||
26 | * You should have received a copy of the GNU Lesser General Public |
||
27 | * License along with this library; if not, write to the Free Software |
||
28 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
29 | */ |
||
30 | |||
31 | class QRrsItem { |
||
32 | |||
33 | public $mm; // Bits per symbol |
||
34 | public $nn; // Symbols per block (= (1<<mm)-1) |
||
35 | public $alpha_to = array(); // log lookup table |
||
36 | public $index_of = array(); // Antilog lookup table |
||
37 | public $genpoly = array(); // Generator polynomial |
||
38 | public $nroots; // Number of generator roots = number of parity symbols |
||
39 | public $fcr; // First consecutive root, index form |
||
40 | public $prim; // Primitive element, index form |
||
41 | public $iprim; // prim-th root of 1, index form |
||
42 | public $pad; // Padding bytes in shortened block |
||
43 | public $gfpoly; |
||
44 | |||
45 | //---------------------------------------------------------------------- |
||
46 | public function modnn($x) |
||
47 | { |
||
48 | while ($x >= $this->nn) { |
||
49 | $x -= $this->nn; |
||
50 | $x = ($x >> $this->mm) + ($x & $this->nn); |
||
51 | } |
||
52 | |||
53 | return $x; |
||
54 | } |
||
55 | |||
56 | //---------------------------------------------------------------------- |
||
57 | public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
||
58 | { |
||
59 | // Common code for intializing a Reed-Solomon control block (char or int symbols) |
||
60 | // Copyright 2004 Phil Karn, KA9Q |
||
61 | // May be used under the terms of the GNU Lesser General Public License (LGPL) |
||
62 | |||
63 | $rs = null; |
||
64 | |||
65 | // Check parameter ranges |
||
66 | if ($symsize < 0 || $symsize > 8) { |
||
67 | return $rs; |
||
68 | } |
||
69 | if ($fcr < 0 || $fcr >= (1 << $symsize)) { |
||
70 | return $rs; |
||
71 | } |
||
72 | if ($prim <= 0 || $prim >= (1 << $symsize)) { |
||
73 | return $rs; |
||
74 | } |
||
75 | if ($nroots < 0 || $nroots >= (1 << $symsize)) { |
||
76 | return $rs; |
||
77 | } |
||
78 | // Can't have more roots than symbol values! |
||
79 | if ($pad < 0 || $pad >= ((1 << $symsize) - 1 - $nroots)) { |
||
80 | return $rs; |
||
81 | } |
||
82 | // Too much padding |
||
83 | |||
84 | $rs = new QRrsItem(); |
||
85 | $rs->mm = $symsize; |
||
86 | $rs->nn = (1 << $symsize) - 1; |
||
87 | $rs->pad = $pad; |
||
88 | |||
89 | $rs->alpha_to = array_fill(0, $rs->nn + 1, 0); |
||
90 | $rs->index_of = array_fill(0, $rs->nn + 1, 0); |
||
91 | |||
92 | // PHP style macro replacement ;) |
||
93 | $NN = & $rs->nn; |
||
94 | $A0 = & $NN; |
||
95 | |||
96 | // Generate Galois field lookup tables |
||
97 | $rs->index_of[0] = $A0; // log(zero) = -inf |
||
98 | $rs->alpha_to[$A0] = 0; // alpha**-inf = 0 |
||
99 | $sr = 1; |
||
100 | |||
101 | for ($i = 0; $i < $rs->nn; $i++) { |
||
102 | $rs->index_of[$sr] = $i; |
||
103 | $rs->alpha_to[$i] = $sr; |
||
104 | $sr <<= 1; |
||
105 | if ($sr & (1 << $symsize)) { |
||
106 | $sr ^= $gfpoly; |
||
107 | } |
||
108 | $sr &= $rs->nn; |
||
109 | } |
||
110 | |||
111 | if ($sr != 1) { |
||
112 | // field generator polynomial is not primitive! |
||
113 | $rs = null; |
||
114 | return $rs; |
||
115 | } |
||
116 | |||
117 | /* Form RS code generator polynomial from its roots */ |
||
118 | $rs->genpoly = array_fill(0, $nroots + 1, 0); |
||
119 | |||
120 | $rs->fcr = $fcr; |
||
121 | $rs->prim = $prim; |
||
122 | $rs->nroots = $nroots; |
||
123 | $rs->gfpoly = $gfpoly; |
||
124 | |||
125 | /* Find prim-th root of 1, used in decoding */ |
||
126 | for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rs->nn) |
||
127 | ; // intentional empty-body loop! |
||
128 | |||
129 | $rs->iprim = (int) ($iprim / $prim); |
||
130 | $rs->genpoly[0] = 1; |
||
131 | |||
132 | for ($i = 0, $root = $fcr * $prim; $i < $nroots; $i++, $root += $prim) { |
||
133 | $rs->genpoly[$i + 1] = 1; |
||
134 | |||
135 | // Multiply rs->genpoly[] by @**(root + x) |
||
136 | for ($j = $i; $j > 0; $j--) { |
||
137 | if ($rs->genpoly[$j] != 0) { |
||
138 | $rs->genpoly[$j] = $rs->genpoly[$j - 1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)]; |
||
139 | } else { |
||
140 | $rs->genpoly[$j] = $rs->genpoly[$j - 1]; |
||
141 | } |
||
142 | } |
||
143 | // rs->genpoly[0] can never be zero |
||
144 | $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)]; |
||
145 | } |
||
146 | |||
147 | // convert rs->genpoly[] to index form for quicker encoding |
||
148 | for ($i = 0; $i <= $nroots; $i++) { |
||
149 | $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]]; |
||
150 | } |
||
151 | |||
152 | return $rs; |
||
153 | } |
||
154 | |||
155 | //---------------------------------------------------------------------- |
||
156 | public function encode_rs_char($data, &$parity) |
||
157 | { |
||
158 | $MM = & $this->mm; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
159 | $NN = & $this->nn; |
||
160 | $ALPHA_TO = & $this->alpha_to; |
||
161 | $INDEX_OF = & $this->index_of; |
||
162 | $GENPOLY = & $this->genpoly; |
||
163 | $NROOTS = & $this->nroots; |
||
164 | $FCR = & $this->fcr; |
||
0 ignored issues
–
show
|
|||
165 | $PRIM = & $this->prim; |
||
0 ignored issues
–
show
|
|||
166 | $IPRIM = & $this->iprim; |
||
0 ignored issues
–
show
|
|||
167 | $PAD = & $this->pad; |
||
168 | $A0 = & $NN; |
||
169 | |||
170 | $parity = array_fill(0, $NROOTS, 0); |
||
171 | |||
172 | for ($i = 0; $i < ($NN - $NROOTS - $PAD); $i++) { |
||
173 | |||
174 | $feedback = $INDEX_OF[$data[$i] ^ $parity[0]]; |
||
175 | if ($feedback != $A0) { |
||
176 | // feedback term is non-zero |
||
177 | |||
178 | // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must |
||
179 | // always be for the polynomials constructed by init_rs() |
||
180 | $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback); |
||
181 | |||
182 | for ($j = 1; $j < $NROOTS; $j++) { |
||
183 | $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS - $j])]; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // Shift |
||
188 | array_shift($parity); |
||
189 | if ($feedback != $A0) { |
||
190 | array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]); |
||
191 | } else { |
||
192 | array_push($parity, 0); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | //########################################################################## |
||
199 | |||
200 | class QRrs { |
||
201 | |||
202 | public static $items = array(); |
||
203 | |||
204 | //---------------------------------------------------------------------- |
||
205 | public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
||
206 | { |
||
207 | foreach (self::$items as $rs) { |
||
208 | if ($rs->pad != $pad) { |
||
209 | continue; |
||
210 | } |
||
211 | if ($rs->nroots != $nroots) { |
||
212 | continue; |
||
213 | } |
||
214 | if ($rs->mm != $symsize) { |
||
215 | continue; |
||
216 | } |
||
217 | if ($rs->gfpoly != $gfpoly) { |
||
218 | continue; |
||
219 | } |
||
220 | if ($rs->fcr != $fcr) { |
||
221 | continue; |
||
222 | } |
||
223 | if ($rs->prim != $prim) { |
||
224 | continue; |
||
225 | } |
||
226 | |||
227 | return $rs; |
||
228 | } |
||
229 | |||
230 | $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad); |
||
231 | array_unshift(self::$items, $rs); |
||
232 | |||
233 | return $rs; |
||
234 | } |
||
235 | } |