Issues (4542)

config/qrcode/qrrscode.php (4 issues)

Severity
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 = []; // log lookup table
36
        public $index_of = []; // Antilog lookup table
37
        public $genpoly = []; // 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
            } // Can't have more roots than symbol values!
78
            if ($pad < 0 || $pad >= ((1 << $symsize) - 1 - $nroots)) {
79
                return $rs;
80
            } // Too much padding
81
82
            $rs = new self();
83
            $rs->mm = $symsize;
84
            $rs->nn = (1 << $symsize) - 1;
85
            $rs->pad = $pad;
86
87
            $rs->alpha_to = array_fill(0, $rs->nn + 1, 0);
88
            $rs->index_of = array_fill(0, $rs->nn + 1, 0);
89
90
            // PHP style macro replacement ;)
91
            $NN = &$rs->nn;
92
            $A0 = &$NN;
93
94
            // Generate Galois field lookup tables
95
            $rs->index_of[0] = $A0; // log(zero) = -inf
96
            $rs->alpha_to[$A0] = 0; // alpha**-inf = 0
97
            $sr = 1;
98
99
            for ($i = 0; $i < $rs->nn; $i++) {
100
                $rs->index_of[$sr] = $i;
101
                $rs->alpha_to[$i] = $sr;
102
                $sr <<= 1;
103
                if ($sr & (1 << $symsize)) {
104
                    $sr ^= $gfpoly;
105
                }
106
                $sr &= $rs->nn;
107
            }
108
109
            if ($sr != 1) {
110
                // field generator polynomial is not primitive!
111
                $rs = null;
112
113
                return $rs;
114
            }
115
116
            /* Form RS code generator polynomial from its roots */
117
            $rs->genpoly = array_fill(0, $nroots + 1, 0);
118
119
            $rs->fcr = $fcr;
120
            $rs->prim = $prim;
121
            $rs->nroots = $nroots;
122
            $rs->gfpoly = $gfpoly;
123
124
            /* Find prim-th root of 1, used in decoding */
125
            for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rs->nn); // intentional empty-body loop!
126
127
            $rs->iprim = (int) ($iprim / $prim);
128
            $rs->genpoly[0] = 1;
129
130
            for ($i = 0, $root = $fcr * $prim; $i < $nroots; $i++, $root += $prim) {
131
                $rs->genpoly[$i + 1] = 1;
132
133
                // Multiply rs->genpoly[] by  @**(root + x)
134
                for ($j = $i; $j > 0; $j--) {
135
                    if ($rs->genpoly[$j] != 0) {
136
                        $rs->genpoly[$j] = $rs->genpoly[$j - 1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
137
                    } else {
138
                        $rs->genpoly[$j] = $rs->genpoly[$j - 1];
139
                    }
140
                }
141
                // rs->genpoly[0] can never be zero
142
                $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
143
            }
144
145
            // convert rs->genpoly[] to index form for quicker encoding
146
            for ($i = 0; $i <= $nroots; $i++) {
147
                $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
148
            }
149
150
            return $rs;
151
        }
152
153
        //----------------------------------------------------------------------
154
        public function encode_rs_char($data, &$parity)
155
        {
156
            $MM = &$this->mm;
0 ignored issues
show
The assignment to $MM is dead and can be removed.
Loading history...
157
            $NN = &$this->nn;
158
            $ALPHA_TO = &$this->alpha_to;
159
            $INDEX_OF = &$this->index_of;
160
            $GENPOLY = &$this->genpoly;
161
            $NROOTS = &$this->nroots;
162
            $FCR = &$this->fcr;
0 ignored issues
show
The assignment to $FCR is dead and can be removed.
Loading history...
163
            $PRIM = &$this->prim;
0 ignored issues
show
The assignment to $PRIM is dead and can be removed.
Loading history...
164
            $IPRIM = &$this->iprim;
0 ignored issues
show
The assignment to $IPRIM is dead and can be removed.
Loading history...
165
            $PAD = &$this->pad;
166
            $A0 = &$NN;
167
168
            $parity = array_fill(0, $NROOTS, 0);
169
170
            for ($i = 0; $i < ($NN - $NROOTS - $PAD); $i++) {
171
                $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
172
                if ($feedback != $A0) {
173
                    // feedback term is non-zero
174
175
                    // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
176
                    // always be for the polynomials constructed by init_rs()
177
                    $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
178
179
                    for ($j = 1; $j < $NROOTS; $j++) {
180
                        $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS - $j])];
181
                    }
182
                }
183
184
                // Shift
185
                array_shift($parity);
186
                if ($feedback != $A0) {
187
                    array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
188
                } else {
189
                    array_push($parity, 0);
190
                }
191
            }
192
        }
193
    }
194
195
    //##########################################################################
196
197
    class QRrs
198
    {
199
        public static $items = [];
200
201
        //----------------------------------------------------------------------
202
        public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
203
        {
204
            foreach (self::$items as $rs) {
205
                if ($rs->pad != $pad) {
206
                    continue;
207
                }
208
                if ($rs->nroots != $nroots) {
209
                    continue;
210
                }
211
                if ($rs->mm != $symsize) {
212
                    continue;
213
                }
214
                if ($rs->gfpoly != $gfpoly) {
215
                    continue;
216
                }
217
                if ($rs->fcr != $fcr) {
218
                    continue;
219
                }
220
                if ($rs->prim != $prim) {
221
                    continue;
222
                }
223
224
                return $rs;
225
            }
226
227
            $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
228
            array_unshift(self::$items, $rs);
229
230
            return $rs;
231
        }
232
    }
233