1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QRrsItem |
4
|
|
|
* @author Tinymeng <[email protected]> |
5
|
|
|
* @date: 2019/9/26 18:26 |
6
|
|
|
*/ |
7
|
|
|
namespace tinymeng\code\Gateways\qrcode; |
8
|
|
|
|
9
|
|
|
class QRrsItem { |
10
|
|
|
|
11
|
|
|
public $mm; // Bits per symbol |
12
|
|
|
public $nn; // Symbols per block (= (1<<mm)-1) |
13
|
|
|
public $alpha_to = array(); // log lookup table |
14
|
|
|
public $index_of = array(); // Antilog lookup table |
15
|
|
|
public $genpoly = array(); // Generator polynomial |
16
|
|
|
public $nroots; // Number of generator roots = number of parity symbols |
17
|
|
|
public $fcr; // First consecutive root, index form |
18
|
|
|
public $prim; // Primitive element, index form |
19
|
|
|
public $iprim; // prim-th root of 1, index form |
20
|
|
|
public $pad; // Padding bytes in shortened block |
21
|
|
|
public $gfpoly; |
22
|
|
|
|
23
|
|
|
//---------------------------------------------------------------------- |
24
|
|
|
public function modnn($x) |
25
|
|
|
{ |
26
|
|
|
while ($x >= $this->nn) { |
27
|
|
|
$x -= $this->nn; |
28
|
|
|
$x = ($x >> $this->mm) + ($x & $this->nn); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $x; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
//---------------------------------------------------------------------- |
35
|
|
|
public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
36
|
|
|
{ |
37
|
|
|
// Common code for intializing a Reed-Solomon control block (char or int symbols) |
38
|
|
|
// Copyright 2004 Phil Karn, KA9Q |
39
|
|
|
// May be used under the terms of the GNU Lesser General Public License (LGPL) |
40
|
|
|
|
41
|
|
|
$rs = null; |
42
|
|
|
|
43
|
|
|
// Check parameter ranges |
44
|
|
|
if($symsize < 0 || $symsize > 8) return $rs; |
45
|
|
|
if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs; |
46
|
|
|
if($prim <= 0 || $prim >= (1<<$symsize)) return $rs; |
47
|
|
|
if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values! |
48
|
|
|
if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding |
49
|
|
|
|
50
|
|
|
$rs = new QRrsItem(); |
51
|
|
|
$rs->mm = $symsize; |
52
|
|
|
$rs->nn = (1<<$symsize)-1; |
53
|
|
|
$rs->pad = $pad; |
54
|
|
|
|
55
|
|
|
$rs->alpha_to = array_fill(0, $rs->nn+1, 0); |
56
|
|
|
$rs->index_of = array_fill(0, $rs->nn+1, 0); |
57
|
|
|
|
58
|
|
|
// PHP style macro replacement ;) |
59
|
|
|
$NN =& $rs->nn; |
60
|
|
|
$A0 =& $NN; |
61
|
|
|
|
62
|
|
|
// Generate Galois field lookup tables |
63
|
|
|
$rs->index_of[0] = $A0; // log(zero) = -inf |
64
|
|
|
$rs->alpha_to[$A0] = 0; // alpha**-inf = 0 |
65
|
|
|
$sr = 1; |
66
|
|
|
|
67
|
|
|
for($i=0; $i<$rs->nn; $i++) { |
68
|
|
|
$rs->index_of[$sr] = $i; |
69
|
|
|
$rs->alpha_to[$i] = $sr; |
70
|
|
|
$sr <<= 1; |
71
|
|
|
if($sr & (1<<$symsize)) { |
72
|
|
|
$sr ^= $gfpoly; |
73
|
|
|
} |
74
|
|
|
$sr &= $rs->nn; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if($sr != 1){ |
78
|
|
|
// field generator polynomial is not primitive! |
79
|
|
|
$rs = NULL; |
80
|
|
|
return $rs; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* Form RS code generator polynomial from its roots */ |
84
|
|
|
$rs->genpoly = array_fill(0, $nroots+1, 0); |
85
|
|
|
|
86
|
|
|
$rs->fcr = $fcr; |
87
|
|
|
$rs->prim = $prim; |
88
|
|
|
$rs->nroots = $nroots; |
89
|
|
|
$rs->gfpoly = $gfpoly; |
90
|
|
|
|
91
|
|
|
/* Find prim-th root of 1, used in decoding */ |
92
|
|
|
for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn) |
93
|
|
|
; // intentional empty-body loop! |
94
|
|
|
|
95
|
|
|
$rs->iprim = (int)($iprim / $prim); |
96
|
|
|
$rs->genpoly[0] = 1; |
97
|
|
|
|
98
|
|
|
for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) { |
99
|
|
|
$rs->genpoly[$i+1] = 1; |
100
|
|
|
|
101
|
|
|
// Multiply rs->genpoly[] by @**(root + x) |
102
|
|
|
for ($j = $i; $j > 0; $j--) { |
103
|
|
|
if ($rs->genpoly[$j] != 0) { |
104
|
|
|
$rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)]; |
105
|
|
|
} else { |
106
|
|
|
$rs->genpoly[$j] = $rs->genpoly[$j-1]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
// rs->genpoly[0] can never be zero |
110
|
|
|
$rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// convert rs->genpoly[] to index form for quicker encoding |
114
|
|
|
for ($i = 0; $i <= $nroots; $i++) |
115
|
|
|
$rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]]; |
116
|
|
|
|
117
|
|
|
return $rs; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
//---------------------------------------------------------------------- |
121
|
|
|
public function encode_rs_char($data, &$parity) |
122
|
|
|
{ |
123
|
|
|
$MM =& $this->mm; |
|
|
|
|
124
|
|
|
$NN =& $this->nn; |
125
|
|
|
$ALPHA_TO =& $this->alpha_to; |
126
|
|
|
$INDEX_OF =& $this->index_of; |
127
|
|
|
$GENPOLY =& $this->genpoly; |
128
|
|
|
$NROOTS =& $this->nroots; |
129
|
|
|
$FCR =& $this->fcr; |
|
|
|
|
130
|
|
|
$PRIM =& $this->prim; |
|
|
|
|
131
|
|
|
$IPRIM =& $this->iprim; |
|
|
|
|
132
|
|
|
$PAD =& $this->pad; |
133
|
|
|
$A0 =& $NN; |
134
|
|
|
|
135
|
|
|
$parity = array_fill(0, $NROOTS, 0); |
136
|
|
|
|
137
|
|
|
for($i=0; $i< ($NN-$NROOTS-$PAD); $i++) { |
138
|
|
|
|
139
|
|
|
$feedback = $INDEX_OF[$data[$i] ^ $parity[0]]; |
140
|
|
|
if($feedback != $A0) { |
141
|
|
|
// feedback term is non-zero |
142
|
|
|
|
143
|
|
|
// This line is unnecessary when GENPOLY[NROOTS] is unity, as it must |
144
|
|
|
// always be for the polynomials constructed by init_rs() |
145
|
|
|
$feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback); |
146
|
|
|
|
147
|
|
|
for($j=1;$j<$NROOTS;$j++) { |
148
|
|
|
$parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Shift |
153
|
|
|
array_shift($parity); |
154
|
|
|
if($feedback != $A0) { |
155
|
|
|
array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]); |
156
|
|
|
} else { |
157
|
|
|
array_push($parity, 0); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|