aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/tcpdf/include/barcodes
diff options
context:
space:
mode:
Diffstat (limited to 'inc/3rdparty/libraries/tcpdf/include/barcodes')
-rw-r--r--inc/3rdparty/libraries/tcpdf/include/barcodes/datamatrix.php1176
-rw-r--r--inc/3rdparty/libraries/tcpdf/include/barcodes/pdf417.php996
-rw-r--r--inc/3rdparty/libraries/tcpdf/include/barcodes/qrcode.php2866
3 files changed, 5038 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/tcpdf/include/barcodes/datamatrix.php b/inc/3rdparty/libraries/tcpdf/include/barcodes/datamatrix.php
new file mode 100644
index 00000000..c1067299
--- /dev/null
+++ b/inc/3rdparty/libraries/tcpdf/include/barcodes/datamatrix.php
@@ -0,0 +1,1176 @@
1<?php
2//============================================================+
3// File name : datamatrix.php
4// Version : 1.0.008
5// Begin : 2010-06-07
6// Last Update : 2014-05-06
7// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
8// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9// -------------------------------------------------------------------
10// Copyright (C) 2010-2014 Nicola Asuni - Tecnick.com LTD
11//
12// This file is part of TCPDF software library.
13//
14// TCPDF is free software: you can redistribute it and/or modify it
15// under the terms of the GNU Lesser General Public License as
16// published by the Free Software Foundation, either version 3 of the
17// License, or (at your option) any later version.
18//
19// TCPDF is distributed in the hope that it will be useful, but
20// WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22// See the GNU Lesser General Public License for more details.
23//
24// You should have received a copy of the GNU Lesser General Public License
25// along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
26//
27// See LICENSE.TXT file for more information.
28// -------------------------------------------------------------------
29//
30// DESCRIPTION :
31//
32// Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
33// DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
34//============================================================+
35
36/**
37* @file
38* Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
39* DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
40*
41* @package com.tecnick.tcpdf
42* @author Nicola Asuni
43* @version 1.0.008
44*/
45
46// custom definitions
47if (!defined('DATAMATRIXDEFS')) {
48
49 /**
50 * Indicate that definitions for this class are set
51 */
52 define('DATAMATRIXDEFS', true);
53
54 // -----------------------------------------------------
55
56} // end of custom definitions
57
58// #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
59
60
61/**
62* ASCII encoding: ASCII character 0 to 127 (1 byte per CW)
63*/
64define('ENC_ASCII', 0);
65
66/**
67* C40 encoding: Upper-case alphanumeric (3/2 bytes per CW)
68*/
69define('ENC_C40', 1);
70
71/**
72* TEXT encoding: Lower-case alphanumeric (3/2 bytes per CW)
73*/
74define('ENC_TXT', 2);
75
76/**
77* X12 encoding: ANSI X12 (3/2 byte per CW)
78*/
79define('ENC_X12', 3);
80
81/**
82* EDIFACT encoding: ASCII character 32 to 94 (4/3 bytes per CW)
83*/
84define('ENC_EDF', 4);
85
86/**
87* BASE 256 encoding: ASCII character 0 to 255 (1 byte per CW)
88*/
89define('ENC_BASE256', 5);
90
91/**
92* ASCII extended encoding: ASCII character 128 to 255 (1/2 byte per CW)
93*/
94define('ENC_ASCII_EXT', 6);
95
96/**
97* ASCII number encoding: ASCII digits (2 bytes per CW)
98*/
99define('ENC_ASCII_NUM', 7);
100
101/**
102* @class Datamatrix
103* Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
104* DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
105*
106* @package com.tecnick.tcpdf
107* @author Nicola Asuni
108* @version 1.0.004
109*/
110class Datamatrix {
111
112 /**
113 * Barcode array to be returned which is readable by TCPDF.
114 * @protected
115 */
116 protected $barcode_array = array();
117
118 /**
119 * Store last used encoding for data codewords.
120 * @protected
121 */
122 protected $last_enc = ENC_ASCII;
123
124 /**
125 * Table of Data Matrix ECC 200 Symbol Attributes:<ul>
126 * <li>total matrix rows (including finder pattern)</li>
127 * <li>total matrix cols (including finder pattern)</li>
128 * <li>total matrix rows (without finder pattern)</li>
129 * <li>total matrix cols (without finder pattern)</li>
130 * <li>region data rows (with finder pattern)</li>
131 * <li>region data col (with finder pattern)</li>
132 * <li>region data rows (without finder pattern)</li>
133 * <li>region data col (without finder pattern)</li>
134 * <li>horizontal regions</li>
135 * <li>vertical regions</li>
136 * <li>regions</li>
137 * <li>data codewords</li>
138 * <li>error codewords</li>
139 * <li>blocks</li>
140 * <li>data codewords per block</li>
141 * <li>error codewords per block</li>
142 * </ul>
143 * @protected
144 */
145 protected $symbattr = array(
146 // square form ---------------------------------------------------------------------------------------
147 array(0x00a,0x00a,0x008,0x008,0x00a,0x00a,0x008,0x008,0x001,0x001,0x001,0x003,0x005,0x001,0x003,0x005), // 10x10
148 array(0x00c,0x00c,0x00a,0x00a,0x00c,0x00c,0x00a,0x00a,0x001,0x001,0x001,0x005,0x007,0x001,0x005,0x007), // 12x12
149 array(0x00e,0x00e,0x00c,0x00c,0x00e,0x00e,0x00c,0x00c,0x001,0x001,0x001,0x008,0x00a,0x001,0x008,0x00a), // 14x14
150 array(0x010,0x010,0x00e,0x00e,0x010,0x010,0x00e,0x00e,0x001,0x001,0x001,0x00c,0x00c,0x001,0x00c,0x00c), // 16x16
151 array(0x012,0x012,0x010,0x010,0x012,0x012,0x010,0x010,0x001,0x001,0x001,0x012,0x00e,0x001,0x012,0x00e), // 18x18
152 array(0x014,0x014,0x012,0x012,0x014,0x014,0x012,0x012,0x001,0x001,0x001,0x016,0x012,0x001,0x016,0x012), // 20x20
153 array(0x016,0x016,0x014,0x014,0x016,0x016,0x014,0x014,0x001,0x001,0x001,0x01e,0x014,0x001,0x01e,0x014), // 22x22
154 array(0x018,0x018,0x016,0x016,0x018,0x018,0x016,0x016,0x001,0x001,0x001,0x024,0x018,0x001,0x024,0x018), // 24x24
155 array(0x01a,0x01a,0x018,0x018,0x01a,0x01a,0x018,0x018,0x001,0x001,0x001,0x02c,0x01c,0x001,0x02c,0x01c), // 26x26
156 array(0x020,0x020,0x01c,0x01c,0x010,0x010,0x00e,0x00e,0x002,0x002,0x004,0x03e,0x024,0x001,0x03e,0x024), // 32x32
157 array(0x024,0x024,0x020,0x020,0x012,0x012,0x010,0x010,0x002,0x002,0x004,0x056,0x02a,0x001,0x056,0x02a), // 36x36
158 array(0x028,0x028,0x024,0x024,0x014,0x014,0x012,0x012,0x002,0x002,0x004,0x072,0x030,0x001,0x072,0x030), // 40x40
159 array(0x02c,0x02c,0x028,0x028,0x016,0x016,0x014,0x014,0x002,0x002,0x004,0x090,0x038,0x001,0x090,0x038), // 44x44
160 array(0x030,0x030,0x02c,0x02c,0x018,0x018,0x016,0x016,0x002,0x002,0x004,0x0ae,0x044,0x001,0x0ae,0x044), // 48x48
161 array(0x034,0x034,0x030,0x030,0x01a,0x01a,0x018,0x018,0x002,0x002,0x004,0x0cc,0x054,0x002,0x066,0x02a), // 52x52
162 array(0x040,0x040,0x038,0x038,0x010,0x010,0x00e,0x00e,0x004,0x004,0x010,0x118,0x070,0x002,0x08c,0x038), // 64x64
163 array(0x048,0x048,0x040,0x040,0x012,0x012,0x010,0x010,0x004,0x004,0x010,0x170,0x090,0x004,0x05c,0x024), // 72x72
164 array(0x050,0x050,0x048,0x048,0x014,0x014,0x012,0x012,0x004,0x004,0x010,0x1c8,0x0c0,0x004,0x072,0x030), // 80x80
165 array(0x058,0x058,0x050,0x050,0x016,0x016,0x014,0x014,0x004,0x004,0x010,0x240,0x0e0,0x004,0x090,0x038), // 88x88
166 array(0x060,0x060,0x058,0x058,0x018,0x018,0x016,0x016,0x004,0x004,0x010,0x2b8,0x110,0x004,0x0ae,0x044), // 96x96
167 array(0x068,0x068,0x060,0x060,0x01a,0x01a,0x018,0x018,0x004,0x004,0x010,0x330,0x150,0x006,0x088,0x038), // 104x104
168 array(0x078,0x078,0x06c,0x06c,0x014,0x014,0x012,0x012,0x006,0x006,0x024,0x41a,0x198,0x006,0x0af,0x044), // 120x120
169 array(0x084,0x084,0x078,0x078,0x016,0x016,0x014,0x014,0x006,0x006,0x024,0x518,0x1f0,0x008,0x0a3,0x03e), // 132x132
170 array(0x090,0x090,0x084,0x084,0x018,0x018,0x016,0x016,0x006,0x006,0x024,0x616,0x26c,0x00a,0x09c,0x03e), // 144x144
171 // rectangular form (currently unused) ---------------------------------------------------------------------------
172 array(0x008,0x012,0x006,0x010,0x008,0x012,0x006,0x010,0x001,0x001,0x001,0x005,0x007,0x001,0x005,0x007), // 8x18
173 array(0x008,0x020,0x006,0x01c,0x008,0x010,0x006,0x00e,0x001,0x002,0x002,0x00a,0x00b,0x001,0x00a,0x00b), // 8x32
174 array(0x00c,0x01a,0x00a,0x018,0x00c,0x01a,0x00a,0x018,0x001,0x001,0x001,0x010,0x00e,0x001,0x010,0x00e), // 12x26
175 array(0x00c,0x024,0x00a,0x020,0x00c,0x012,0x00a,0x010,0x001,0x002,0x002,0x00c,0x012,0x001,0x00c,0x012), // 12x36
176 array(0x010,0x024,0x00e,0x020,0x010,0x012,0x00e,0x010,0x001,0x002,0x002,0x020,0x018,0x001,0x020,0x018), // 16x36
177 array(0x010,0x030,0x00e,0x02c,0x010,0x018,0x00e,0x016,0x001,0x002,0x002,0x031,0x01c,0x001,0x031,0x01c) // 16x48
178 );
179
180 /**
181 * Map encodation modes whit character sets.
182 * @protected
183 */
184 protected $chset_id = array(ENC_C40 => 'C40', ENC_TXT => 'TXT', ENC_X12 =>'X12');
185
186 /**
187 * Basic set of characters for each encodation mode.
188 * @protected
189 */
190 protected $chset = array(
191 'C40' => array( // Basic set for C40 ----------------------------------------------------------------------------
192 'S1'=>0x00,'S2'=>0x01,'S3'=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
193 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x41=>0x0e,0x42=>0x0f,0x43=>0x10,0x44=>0x11,0x45=>0x12,0x46=>0x13, //
194 0x47=>0x14,0x48=>0x15,0x49=>0x16,0x4a=>0x17,0x4b=>0x18,0x4c=>0x19,0x4d=>0x1a,0x4e=>0x1b,0x4f=>0x1c,0x50=>0x1d, //
195 0x51=>0x1e,0x52=>0x1f,0x53=>0x20,0x54=>0x21,0x55=>0x22,0x56=>0x23,0x57=>0x24,0x58=>0x25,0x59=>0x26,0x5a=>0x27),//
196 'TXT' => array( // Basic set for TEXT ---------------------------------------------------------------------------
197 'S1'=>0x00,'S2'=>0x01,'S3'=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
198 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x61=>0x0e,0x62=>0x0f,0x63=>0x10,0x64=>0x11,0x65=>0x12,0x66=>0x13, //
199 0x67=>0x14,0x68=>0x15,0x69=>0x16,0x6a=>0x17,0x6b=>0x18,0x6c=>0x19,0x6d=>0x1a,0x6e=>0x1b,0x6f=>0x1c,0x70=>0x1d, //
200 0x71=>0x1e,0x72=>0x1f,0x73=>0x20,0x74=>0x21,0x75=>0x22,0x76=>0x23,0x77=>0x24,0x78=>0x25,0x79=>0x26,0x7a=>0x27),//
201 'SH1' => array( // Shift 1 set ----------------------------------------------------------------------------------
202 0x00=>0x00,0x01=>0x01,0x02=>0x02,0x03=>0x03,0x04=>0x04,0x05=>0x05,0x06=>0x06,0x07=>0x07,0x08=>0x08,0x09=>0x09, //
203 0x0a=>0x0a,0x0b=>0x0b,0x0c=>0x0c,0x0d=>0x0d,0x0e=>0x0e,0x0f=>0x0f,0x10=>0x10,0x11=>0x11,0x12=>0x12,0x13=>0x13, //
204 0x14=>0x14,0x15=>0x15,0x16=>0x16,0x17=>0x17,0x18=>0x18,0x19=>0x19,0x1a=>0x1a,0x1b=>0x1b,0x1c=>0x1c,0x1d=>0x1d, //
205 0x1e=>0x1e,0x1f=>0x1f), //
206 'SH2' => array( // Shift 2 set ----------------------------------------------------------------------------------
207 0x21=>0x00,0x22=>0x01,0x23=>0x02,0x24=>0x03,0x25=>0x04,0x26=>0x05,0x27=>0x06,0x28=>0x07,0x29=>0x08,0x2a=>0x09, //
208 0x2b=>0x0a,0x2c=>0x0b,0x2d=>0x0c,0x2e=>0x0d,0x2f=>0x0e,0x3a=>0x0f,0x3b=>0x10,0x3c=>0x11,0x3d=>0x12,0x3e=>0x13, //
209 0x3f=>0x14,0x40=>0x15,0x5b=>0x16,0x5c=>0x17,0x5d=>0x18,0x5e=>0x19,0x5f=>0x1a,'F1'=>0x1b,'US'=>0x1e), //
210 'S3C' => array( // Shift 3 set for C40 --------------------------------------------------------------------------
211 0x60=>0x00,0x61=>0x01,0x62=>0x02,0x63=>0x03,0x64=>0x04,0x65=>0x05,0x66=>0x06,0x67=>0x07,0x68=>0x08,0x69=>0x09, //
212 0x6a=>0x0a,0x6b=>0x0b,0x6c=>0x0c,0x6d=>0x0d,0x6e=>0x0e,0x6f=>0x0f,0x70=>0x10,0x71=>0x11,0x72=>0x12,0x73=>0x13, //
213 0x74=>0x14,0x75=>0x15,0x76=>0x16,0x77=>0x17,0x78=>0x18,0x79=>0x19,0x7a=>0x1a,0x7b=>0x1b,0x7c=>0x1c,0x7d=>0x1d, //
214 0x7e=>0x1e,0x7f=>0x1f),
215 'S3T' => array( // Shift 3 set for TEXT -------------------------------------------------------------------------
216 0x60=>0x00,0x41=>0x01,0x42=>0x02,0x43=>0x03,0x44=>0x04,0x45=>0x05,0x46=>0x06,0x47=>0x07,0x48=>0x08,0x49=>0x09, //
217 0x4a=>0x0a,0x4b=>0x0b,0x4c=>0x0c,0x4d=>0x0d,0x4e=>0x0e,0x4f=>0x0f,0x50=>0x10,0x51=>0x11,0x52=>0x12,0x53=>0x13, //
218 0x54=>0x14,0x55=>0x15,0x56=>0x16,0x57=>0x17,0x58=>0x18,0x59=>0x19,0x5a=>0x1a,0x7b=>0x1b,0x7c=>0x1c,0x7d=>0x1d, //
219 0x7e=>0x1e,0x7f=>0x1f), //
220 'X12' => array( // Set for X12 ----------------------------------------------------------------------------------
221 0x0d=>0x00,0x2a=>0x01,0x3e=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
222 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x41=>0x0e,0x42=>0x0f,0x43=>0x10,0x44=>0x11,0x45=>0x12,0x46=>0x13, //
223 0x47=>0x14,0x48=>0x15,0x49=>0x16,0x4a=>0x17,0x4b=>0x18,0x4c=>0x19,0x4d=>0x1a,0x4e=>0x1b,0x4f=>0x1c,0x50=>0x1d, //
224 0x51=>0x1e,0x52=>0x1f,0x53=>0x20,0x54=>0x21,0x55=>0x22,0x56=>0x23,0x57=>0x24,0x58=>0x25,0x59=>0x26,0x5a=>0x27) //
225 );
226
227// -----------------------------------------------------------------------------
228
229 /**
230 * This is the class constructor.
231 * Creates a datamatrix object
232 * @param $code (string) Code to represent using Datamatrix.
233 * @public
234 */
235 public function __construct($code) {
236 $barcode_array = array();
237 if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
238 return false;
239 }
240 // get data codewords
241 $cw = $this->getHighLevelEncoding($code);
242 // number of data codewords
243 $nd = count($cw);
244 // check size
245 if ($nd > 1558) {
246 return false;
247 }
248 // get minimum required matrix size.
249 foreach ($this->symbattr as $params) {
250 if ($params[11] >= $nd) {
251 break;
252 }
253 }
254 if ($params[11] < $nd) {
255 // too much data
256 return false;
257 } elseif ($params[11] > $nd) {
258 // add padding
259 if ((($params[11] - $nd) > 1) AND ($cw[($nd - 1)] != 254)) {
260 if ($this->last_enc == ENC_EDF) {
261 // switch to ASCII encoding
262 $cw[] = 124;
263 ++$nd;
264 } elseif (($this->last_enc != ENC_ASCII) AND ($this->last_enc != ENC_BASE256)) {
265 // switch to ASCII encoding
266 $cw[] = 254;
267 ++$nd;
268 }
269 }
270 if ($params[11] > $nd) {
271 // add first pad
272 $cw[] = 129;
273 ++$nd;
274 // add remaining pads
275 for ($i = $nd; $i < $params[11]; ++$i) {
276 $cw[] = $this->get253StateCodeword(129, $i);
277 }
278 }
279 }
280 // add error correction codewords
281 $cw = $this->getErrorCorrection($cw, $params[13], $params[14], $params[15]);
282 // initialize empty arrays
283 $grid = array_fill(0, ($params[2] * $params[3]), 0);
284 // get placement map
285 $places = $this->getPlacementMap($params[2], $params[3]);
286 // fill the grid with data
287 $grid = array();
288 $i = 0;
289 // region data row max index
290 $rdri = ($params[4] - 1);
291 // region data column max index
292 $rdci = ($params[5] - 1);
293 // for each vertical region
294 for ($vr = 0; $vr < $params[9]; ++$vr) {
295 // for each row on region
296 for ($r = 0; $r < $params[4]; ++$r) {
297 // get row
298 $row = (($vr * $params[4]) + $r);
299 // for each horizontal region
300 for ($hr = 0; $hr < $params[8]; ++$hr) {
301 // for each column on region
302 for ($c = 0; $c < $params[5]; ++$c) {
303 // get column
304 $col = (($hr * $params[5]) + $c);
305 // braw bits by case
306 if ($r == 0) {
307 // top finder pattern
308 if ($c % 2) {
309 $grid[$row][$col] = 0;
310 } else {
311 $grid[$row][$col] = 1;
312 }
313 } elseif ($r == $rdri) {
314 // bottom finder pattern
315 $grid[$row][$col] = 1;
316 } elseif ($c == 0) {
317 // left finder pattern
318 $grid[$row][$col] = 1;
319 } elseif ($c == $rdci) {
320 // right finder pattern
321 if ($r % 2) {
322 $grid[$row][$col] = 1;
323 } else {
324 $grid[$row][$col] = 0;
325 }
326 } else { // data bit
327 if ($places[$i] < 2) {
328 $grid[$row][$col] = $places[$i];
329 } else {
330 // codeword ID
331 $cw_id = (floor($places[$i] / 10) - 1);
332 // codeword BIT mask
333 $cw_bit = pow(2, (8 - ($places[$i] % 10)));
334 $grid[$row][$col] = (($cw[$cw_id] & $cw_bit) == 0) ? 0 : 1;
335 }
336 ++$i;
337 }
338 }
339 }
340 }
341 }
342 $this->barcode_array['num_rows'] = $params[0];
343 $this->barcode_array['num_cols'] = $params[1];
344 $this->barcode_array['bcode'] = $grid;
345 }
346
347 /**
348 * Returns a barcode array which is readable by TCPDF
349 * @return array barcode array readable by TCPDF;
350 * @public
351 */
352 public function getBarcodeArray() {
353 return $this->barcode_array;
354 }
355
356 /**
357 * Product of two numbers in a Power-of-Two Galois Field
358 * @param $a (int) first number to multiply.
359 * @param $b (int) second number to multiply.
360 * @param $log (array) Log table.
361 * @param $alog (array) Anti-Log table.
362 * @param $gf (array) Number of Factors of the Reed-Solomon polynomial.
363 * @return int product
364 * @protected
365 */
366 protected function getGFProduct($a, $b, $log, $alog, $gf) {
367 if (($a == 0) OR ($b == 0)) {
368 return 0;
369 }
370 return ($alog[($log[$a] + $log[$b]) % ($gf - 1)]);
371 }
372
373 /**
374 * Add error correction codewords to data codewords array (ANNEX E).
375 * @param $wd (array) Array of datacodewords.
376 * @param $nb (int) Number of blocks.
377 * @param $nd (int) Number of data codewords per block.
378 * @param $nc (int) Number of correction codewords per block.
379 * @param $gf (int) numner of fields on log/antilog table (power of 2).
380 * @param $pp (int) The value of its prime modulus polynomial (301 for ECC200).
381 * @return array data codewords + error codewords
382 * @protected
383 */
384 protected function getErrorCorrection($wd, $nb, $nd, $nc, $gf=256, $pp=301) {
385 // generate the log ($log) and antilog ($alog) tables
386 $log[0] = 0;
387 $alog[0] = 1;
388 for ($i = 1; $i < $gf; ++$i) {
389 $alog[$i] = ($alog[($i - 1)] * 2);
390 if ($alog[$i] >= $gf) {
391 $alog[$i] ^= $pp;
392 }
393 $log[$alog[$i]] = $i;
394 }
395 ksort($log);
396 // generate the polynomial coefficients (c)
397 $c = array_fill(0, ($nc + 1), 0);
398 $c[0] = 1;
399 for ($i = 1; $i <= $nc; ++$i) {
400 $c[$i] = $c[($i-1)];
401 for ($j = ($i - 1); $j >= 1; --$j) {
402 $c[$j] = $c[($j - 1)] ^ $this->getGFProduct($c[$j], $alog[$i], $log, $alog, $gf);
403 }
404 $c[0] = $this->getGFProduct($c[0], $alog[$i], $log, $alog, $gf);
405 }
406 ksort($c);
407 // total number of data codewords
408 $num_wd = ($nb * $nd);
409 // total number of error codewords
410 $num_we = ($nb * $nc);
411 // for each block
412 for ($b = 0; $b < $nb; ++$b) {
413 // create interleaved data block
414 $block = array();
415 for ($n = $b; $n < $num_wd; $n += $nb) {
416 $block[] = $wd[$n];
417 }
418 // initialize error codewords
419 $we = array_fill(0, ($nc + 1), 0);
420 // calculate error correction codewords for this block
421 for ($i = 0; $i < $nd; ++$i) {
422 $k = ($we[0] ^ $block[$i]);
423 for ($j = 0; $j < $nc; ++$j) {
424 $we[$j] = ($we[($j + 1)] ^ $this->getGFProduct($k, $c[($nc - $j - 1)], $log, $alog, $gf));
425 }
426 }
427 // add error codewords at the end of data codewords
428 $j = 0;
429 for ($i = $b; $i < $num_we; $i += $nb) {
430 $wd[($num_wd + $i)] = $we[$j];
431 ++$j;
432 }
433 }
434 // reorder codewords
435 ksort($wd);
436 return $wd;
437 }
438
439 /**
440 * Return the 253-state codeword
441 * @param $cwpad (int) Pad codeword.
442 * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
443 * @return pad codeword
444 * @protected
445 */
446 protected function get253StateCodeword($cwpad, $cwpos) {
447 $pad = ($cwpad + (((149 * $cwpos) % 253) + 1));
448 if ($pad > 254) {
449 $pad -= 254;
450 }
451 return $pad;
452 }
453
454 /**
455 * Return the 255-state codeword
456 * @param $cwpad (int) Pad codeword.
457 * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
458 * @return pad codeword
459 * @protected
460 */
461 protected function get255StateCodeword($cwpad, $cwpos) {
462 $pad = ($cwpad + (((149 * $cwpos) % 255) + 1));
463 if ($pad > 255) {
464 $pad -= 256;
465 }
466 return $pad;
467 }
468
469 /**
470 * Returns true if the char belongs to the selected mode
471 * @param $chr (int) Character (byte) to check.
472 * @param $mode (int) Current encoding mode.
473 * @return boolean true if the char is of the selected mode.
474 * @protected
475 */
476 protected function isCharMode($chr, $mode) {
477 $status = false;
478 switch ($mode) {
479 case ENC_ASCII: { // ASCII character 0 to 127
480 $status = (($chr >= 0) AND ($chr <= 127));
481 break;
482 }
483 case ENC_C40: { // Upper-case alphanumeric
484 $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 65) AND ($chr <= 90)));
485 break;
486 }
487 case ENC_TXT: { // Lower-case alphanumeric
488 $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 97) AND ($chr <= 122)));
489 break;
490 }
491 case ENC_X12: { // ANSI X12
492 $status = (($chr == 13) OR ($chr == 42) OR ($chr == 62));
493 break;
494 }
495 case ENC_EDF: { // ASCII character 32 to 94
496 $status = (($chr >= 32) AND ($chr <= 94));
497 break;
498 }
499 case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
500 $status = (($chr == 232) OR ($chr == 233) OR ($chr == 234) OR ($chr == 241));
501 break;
502 }
503 case ENC_ASCII_EXT: { // ASCII character 128 to 255
504 $status = (($chr >= 128) AND ($chr <= 255));
505 break;
506 }
507 case ENC_ASCII_NUM: { // ASCII digits
508 $status = (($chr >= 48) AND ($chr <= 57));
509 break;
510 }
511 }
512 return $status;
513 }
514
515 /**
516 * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
517 * @param $data (string) data to encode
518 * @param $pos (int) current position
519 * @param $mode (int) current encoding mode
520 * @return int encoding mode
521 * @protected
522 */
523 protected function lookAheadTest($data, $pos, $mode) {
524 $data_length = strlen($data);
525 if ($pos >= $data_length) {
526 return $mode;
527 }
528 $charscount = 0; // count processed chars
529 // STEP J
530 if ($mode == ENC_ASCII) {
531 $numch = array(0, 1, 1, 1, 1, 1.25);
532 } else {
533 $numch = array(1, 2, 2, 2, 2, 2.25);
534 $numch[$mode] = 0;
535 }
536 while (true) {
537 // STEP K
538 if (($pos + $charscount) == $data_length) {
539 if ($numch[ENC_ASCII] <= ceil(min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
540 return ENC_ASCII;
541 }
542 if ($numch[ENC_BASE256] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
543 return ENC_BASE256;
544 }
545 if ($numch[ENC_EDF] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256]))) {
546 return ENC_EDF;
547 }
548 if ($numch[ENC_TXT] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
549 return ENC_TXT;
550 }
551 if ($numch[ENC_X12] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
552 return ENC_X12;
553 }
554 return ENC_C40;
555 }
556 // get char
557 $chr = ord($data[$pos + $charscount]);
558 $charscount++;
559 // STEP L
560 if ($this->isCharMode($chr, ENC_ASCII_NUM)) {
561 $numch[ENC_ASCII] += (1 / 2);
562 } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
563 $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
564 $numch[ENC_ASCII] += 2;
565 } else {
566 $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
567 $numch[ENC_ASCII] += 1;
568 }
569 // STEP M
570 if ($this->isCharMode($chr, ENC_C40)) {
571 $numch[ENC_C40] += (2 / 3);
572 } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
573 $numch[ENC_C40] += (8 / 3);
574 } else {
575 $numch[ENC_C40] += (4 / 3);
576 }
577 // STEP N
578 if ($this->isCharMode($chr, ENC_TXT)) {
579 $numch[ENC_TXT] += (2 / 3);
580 } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
581 $numch[ENC_TXT] += (8 / 3);
582 } else {
583 $numch[ENC_TXT] += (4 / 3);
584 }
585 // STEP O
586 if ($this->isCharMode($chr, ENC_X12) OR $this->isCharMode($chr, ENC_C40)) {
587 $numch[ENC_X12] += (2 / 3);
588 } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
589 $numch[ENC_X12] += (13 / 3);
590 } else {
591 $numch[ENC_X12] += (10 / 3);
592 }
593 // STEP P
594 if ($this->isCharMode($chr, ENC_EDF)) {
595 $numch[ENC_EDF] += (3 / 4);
596 } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
597 $numch[ENC_EDF] += (17 / 4);
598 } else {
599 $numch[ENC_EDF] += (13 / 4);
600 }
601 // STEP Q
602 if ($this->isCharMode($chr, ENC_BASE256)) {
603 $numch[ENC_BASE256] += 4;
604 } else {
605 $numch[ENC_BASE256] += 1;
606 }
607 // STEP R
608 if ($charscount >= 4) {
609 if (($numch[ENC_ASCII] + 1) <= min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
610 return ENC_ASCII;
611 }
612 if ((($numch[ENC_BASE256] + 1) <= $numch[ENC_ASCII])
613 OR (($numch[ENC_BASE256] + 1) < min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
614 return ENC_BASE256;
615 }
616 if (($numch[ENC_EDF] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256])) {
617 return ENC_EDF;
618 }
619 if (($numch[ENC_TXT] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
620 return ENC_TXT;
621 }
622 if (($numch[ENC_X12] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
623 return ENC_X12;
624 }
625 if (($numch[ENC_C40] + 1) < min($numch[ENC_ASCII], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
626 if ($numch[ENC_C40] < $numch[ENC_X12]) {
627 return ENC_C40;
628 }
629 if ($numch[ENC_C40] == $numch[ENC_X12]) {
630 $k = ($pos + $charscount + 1);
631 while ($k < $data_length) {
632 $tmpchr = ord($data{$k});
633 if ($this->isCharMode($tmpchr, ENC_X12)) {
634 return ENC_X12;
635 } elseif (!($this->isCharMode($tmpchr, ENC_X12) OR $this->isCharMode($tmpchr, ENC_C40))) {
636 break;
637 }
638 ++$k;
639 }
640 return ENC_C40;
641 }
642 }
643 }
644 } // end of while
645 }
646
647 /**
648 * Get the switching codeword to a new encoding mode (latch codeword)
649 * @param $mode (int) New encoding mode.
650 * @return (int) Switch codeword.
651 * @protected
652 */
653 protected function getSwitchEncodingCodeword($mode) {
654 switch ($mode) {
655 case ENC_ASCII: { // ASCII character 0 to 127
656 $cw = 254;
657 if ($this->last_enc == ENC_EDF) {
658 $cw = 124;
659 }
660 break;
661 }
662 case ENC_C40: { // Upper-case alphanumeric
663 $cw = 230;
664 break;
665 }
666 case ENC_TXT: { // Lower-case alphanumeric
667 $cw = 239;
668 break;
669 }
670 case ENC_X12: { // ANSI X12
671 $cw = 238;
672 break;
673 }
674 case ENC_EDF: { // ASCII character 32 to 94
675 $cw = 240;
676 break;
677 }
678 case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
679 $cw = 231;
680 break;
681 }
682 }
683 return $cw;
684 }
685
686 /**
687 * Choose the minimum matrix size and return the max number of data codewords.
688 * @param $numcw (int) Number of current codewords.
689 * @return number of data codewords in matrix
690 * @protected
691 */
692 protected function getMaxDataCodewords($numcw) {
693 foreach ($this->symbattr as $key => $matrix) {
694 if ($matrix[11] >= $numcw) {
695 return $matrix[11];
696 }
697 }
698 return 0;
699 }
700
701 /**
702 * Get high level encoding using the minimum symbol data characters for ECC 200
703 * @param $data (string) data to encode
704 * @return array of codewords
705 * @protected
706 */
707 protected function getHighLevelEncoding($data) {
708 // STEP A. Start in ASCII encodation.
709 $enc = ENC_ASCII; // current encoding mode
710 $pos = 0; // current position
711 $cw = array(); // array of codewords to be returned
712 $cw_num = 0; // number of data codewords
713 $data_lenght = strlen($data); // number of chars
714 while ($pos < $data_lenght) {
715 // set last used encoding
716 $this->last_enc = $enc;
717 switch ($enc) {
718 case ENC_ASCII: { // STEP B. While in ASCII encodation
719 if (($data_lenght > 1) AND ($pos < ($data_lenght - 1)) AND ($this->isCharMode(ord($data[$pos]), ENC_ASCII_NUM) AND $this->isCharMode(ord($data[$pos + 1]), ENC_ASCII_NUM))) {
720 // 1. If the next data sequence is at least 2 consecutive digits, encode the next two digits as a double digit in ASCII mode.
721 $cw[] = (intval(substr($data, $pos, 2)) + 130);
722 ++$cw_num;
723 $pos += 2;
724 } else {
725 // 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
726 $newenc = $this->lookAheadTest($data, $pos, $enc);
727 if ($newenc != $enc) {
728 // switch to new encoding
729 $enc = $newenc;
730 $cw[] = $this->getSwitchEncodingCodeword($enc);
731 ++$cw_num;
732 } else {
733 // get new byte
734 $chr = ord($data[$pos]);
735 ++$pos;
736 if ($this->isCharMode($chr, ENC_ASCII_EXT)) {
737 // 3. If the next data character is extended ASCII (greater than 127) encode it in ASCII mode first using the Upper Shift (value 235) character.
738 $cw[] = 235;
739 $cw[] = ($chr - 127);
740 $cw_num += 2;
741 } else {
742 // 4. Otherwise process the next data character in ASCII encodation.
743 $cw[] = ($chr + 1);
744 ++$cw_num;
745 }
746 }
747 }
748 break;
749 }
750 case ENC_C40 : // Upper-case alphanumeric
751 case ENC_TXT : // Lower-case alphanumeric
752 case ENC_X12 : { // ANSI X12
753 $temp_cw = array();
754 $p = 0;
755 $epos = $pos;
756 // get charset ID
757 $set_id = $this->chset_id[$enc];
758 // get basic charset for current encoding
759 $charset = $this->chset[$set_id];
760 do {
761 // 2. process the next character in C40 encodation.
762 $chr = ord($data[$epos]);
763 ++$epos;
764 // check for extended character
765 if ($chr & 0x80) {
766 if ($enc == ENC_X12) {
767 return false;
768 }
769 $chr = ($chr & 0x7f);
770 $temp_cw[] = 1; // shift 2
771 $temp_cw[] = 30; // upper shift
772 $p += 2;
773 }
774 if (isset($charset[$chr])) {
775 $temp_cw[] = $charset[$chr];
776 ++$p;
777 } else {
778 if (isset($this->chset['SH1'][$chr])) {
779 $temp_cw[] = 0; // shift 1
780 $shiftset = $this->chset['SH1'];
781 } elseif (isset($chr, $this->chset['SH2'][$chr])) {
782 $temp_cw[] = 1; // shift 2
783 $shiftset = $this->chset['SH2'];
784 } elseif (($enc == ENC_C40) AND isset($this->chset['S3C'][$chr])) {
785 $temp_cw[] = 2; // shift 3
786 $shiftset = $this->chset['S3C'];
787 } elseif (($enc == ENC_TXT) AND isset($this->chset['S3T'][$chr])) {
788 $temp_cw[] = 2; // shift 3
789 $shiftset = $this->chset['S3T'];
790 } else {
791 return false;
792 }
793 $temp_cw[] = $shiftset[$chr];
794 $p += 2;
795 }
796 if ($p >= 3) {
797 $c1 = array_shift($temp_cw);
798 $c2 = array_shift($temp_cw);
799 $c3 = array_shift($temp_cw);
800 $p -= 3;
801 $tmp = ((1600 * $c1) + (40 * $c2) + $c3 + 1);
802 $cw[] = ($tmp >> 8);
803 $cw[] = ($tmp % 256);
804 $cw_num += 2;
805 $pos = $epos;
806 // 1. If the C40 encoding is at the point of starting a new double symbol character and if the look-ahead test (starting at step J) indicates another mode, switch to that mode.
807 $newenc = $this->lookAheadTest($data, $pos, $enc);
808 if ($newenc != $enc) {
809 // switch to new encoding
810 $enc = $newenc;
811 if ($enc != ENC_ASCII) {
812 // set unlatch character
813 $cw[] = $this->getSwitchEncodingCodeword(ENC_ASCII);
814 ++$cw_num;
815 }
816 $cw[] = $this->getSwitchEncodingCodeword($enc);
817 ++$cw_num;
818 $pos -= $p;
819 $p = 0;
820 break;
821 }
822 }
823 } while (($p > 0) AND ($epos < $data_lenght));
824 // process last data (if any)
825 if ($p > 0) {
826 // get remaining number of data symbols
827 $cwr = ($this->getMaxDataCodewords($cw_num) - $cw_num);
828 if (($cwr == 1) AND ($p == 1)) {
829 // d. If one symbol character remains and one C40 value (data character) remains to be encoded
830 $c1 = array_shift($temp_cw);
831 --$p;
832 $cw[] = ($chr + 1);
833 ++$cw_num;
834 $pos = $epos;
835 $enc = ENC_ASCII;
836 $this->last_enc = $enc;
837 } elseif (($cwr == 2) AND ($p == 1)) {
838 // c. If two symbol characters remain and only one C40 value (data character) remains to be encoded
839 $c1 = array_shift($temp_cw);
840 --$p;
841 $cw[] = 254;
842 $cw[] = ($chr + 1);
843 $cw_num += 2;
844 $pos = $epos;
845 $enc = ENC_ASCII;
846 $this->last_enc = $enc;
847 } elseif (($cwr == 2) AND ($p == 2)) {
848 // b. If two symbol characters remain and two C40 values remain to be encoded
849 $c1 = array_shift($temp_cw);
850 $c2 = array_shift($temp_cw);
851 $p -= 2;
852 $tmp = ((1600 * $c1) + (40 * $c2) + 1);
853 $cw[] = ($tmp >> 8);
854 $cw[] = ($tmp % 256);
855 $cw_num += 2;
856 $pos = $epos;
857 $enc = ENC_ASCII;
858 $this->last_enc = $enc;
859 } else {
860 // switch to ASCII encoding
861 if ($enc != ENC_ASCII) {
862 $enc = ENC_ASCII;
863 $this->last_enc = $enc;
864 $cw[] = $this->getSwitchEncodingCodeword($enc);
865 ++$cw_num;
866 $pos = ($epos - $p);
867 }
868 }
869 }
870 break;
871 }
872 case ENC_EDF: { // F. While in EDIFACT (EDF) encodation
873 // initialize temporary array with 0 lenght
874 $temp_cw = array();
875 $epos = $pos;
876 $field_lenght = 0;
877 $newenc = $enc;
878 do {
879 // 2. process the next character in EDIFACT encodation.
880 $chr = ord($data[$epos]);
881 if ($this->isCharMode($chr, ENC_EDF)) {
882 ++$epos;
883 $temp_cw[] = $chr;
884 ++$field_lenght;
885 }
886 if (($field_lenght == 4) OR ($epos == $data_lenght) OR !$this->isCharMode($chr, ENC_EDF)) {
887 if (($epos == $data_lenght) AND ($field_lenght < 3)) {
888 $enc = ENC_ASCII;
889 $cw[] = $this->getSwitchEncodingCodeword($enc);
890 ++$cw_num;
891 break;
892 }
893 if ($field_lenght < 4) {
894 // set unlatch character
895 $temp_cw[] = 0x1f;
896 ++$field_lenght;
897 // fill empty characters
898 for ($i = $field_lenght; $i < 4; ++$i) {
899 $temp_cw[] = 0;
900 }
901 $enc = ENC_ASCII;
902 $this->last_enc = $enc;
903 }
904 // encodes four data characters in three codewords
905 $tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
906 if ($tcw > 0) {
907 $cw[] = $tcw;
908 $cw_num++;
909 }
910 $tcw= (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
911 if ($tcw > 0) {
912 $cw[] = $tcw;
913 $cw_num++;
914 }
915 $tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
916 if ($tcw > 0) {
917 $cw[] = $tcw;
918 $cw_num++;
919 }
920 $temp_cw = array();
921 $pos = $epos;
922 $field_lenght = 0;
923 if ($enc == ENC_ASCII) {
924 break; // exit from EDIFACT mode
925 }
926 }
927 } while ($epos < $data_lenght);
928 break;
929 }
930 case ENC_BASE256: { // G. While in Base 256 (B256) encodation
931 // initialize temporary array with 0 lenght
932 $temp_cw = array();
933 $field_lenght = 0;
934 while (($pos < $data_lenght) AND ($field_lenght <= 1555)) {
935 $newenc = $this->lookAheadTest($data, $pos, $enc);
936 if ($newenc != $enc) {
937 // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
938 $enc = $newenc;
939 break; // exit from B256 mode
940 } else {
941 // 2. Otherwise, process the next character in Base 256 encodation.
942 $chr = ord($data[$pos]);
943 ++$pos;
944 $temp_cw[] = $chr;
945 ++$field_lenght;
946 }
947 }
948 // set field lenght
949 if ($field_lenght <= 249) {
950 $cw[] = $this->get255StateCodeword($field_lenght, ($cw_num + 1));
951 ++$cw_num;
952 } else {
953 $cw[] = $this->get255StateCodeword((floor($field_lenght / 250) + 249), ($cw_num + 1));
954 $cw[] = $this->get255StateCodeword(($field_lenght % 250), ($cw_num + 2));
955 $cw_num += 2;
956 }
957 if (!empty($temp_cw)) {
958 // add B256 field
959 foreach ($temp_cw as $p => $cht) {
960 $cw[] = $this->get255StateCodeword($cht, ($cw_num + $p + 1));
961 }
962 }
963 break;
964 }
965 } // end of switch enc
966 } // end of while
967 return $cw;
968 }
969
970 /**
971 * Places "chr+bit" with appropriate wrapping within array[].
972 * (Annex F - ECC 200 symbol character placement)
973 * @param $marr (array) Array of symbols.
974 * @param $nrow (int) Number of rows.
975 * @param $ncol (int) Number of columns.
976 * @param $row (int) Row number.
977 * @param $col (int) Column number.
978 * @param $chr (int) Char byte.
979 * @param $bit (int) Bit.
980 * @return array
981 * @protected
982 */
983 protected function placeModule($marr, $nrow, $ncol, $row, $col, $chr, $bit) {
984 if ($row < 0) {
985 $row += $nrow;
986 $col += (4 - (($nrow + 4) % 8));
987 }
988 if ($col < 0) {
989 $col += $ncol;
990 $row += (4 - (($ncol + 4) % 8));
991 }
992 $marr[(($row * $ncol) + $col)] = ((10 * $chr) + $bit);
993 return $marr;
994 }
995
996 /**
997 * Places the 8 bits of a utah-shaped symbol character.
998 * (Annex F - ECC 200 symbol character placement)
999 * @param $marr (array) Array of symbols.
1000 * @param $nrow (int) Number of rows.
1001 * @param $ncol (int) Number of columns.
1002 * @param $row (int) Row number.
1003 * @param $col (int) Column number.
1004 * @param $chr (int) Char byte.
1005 * @return array
1006 * @protected
1007 */
1008 protected function placeUtah($marr, $nrow, $ncol, $row, $col, $chr) {
1009 $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-2, $chr, 1);
1010 $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-1, $chr, 2);
1011 $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-2, $chr, 3);
1012 $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-1, $chr, 4);
1013 $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col, $chr, 5);
1014 $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-2, $chr, 6);
1015 $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-1, $chr, 7);
1016 $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
1017 return $marr;
1018 }
1019
1020 /**
1021 * Places the 8 bits of the first special corner case.
1022 * (Annex F - ECC 200 symbol character placement)
1023 * @param $marr (array) Array of symbols.
1024 * @param $nrow (int) Number of rows.
1025 * @param $ncol (int) Number of columns.
1026 * @param $chr (int) Char byte.
1027 * @return array
1028 * @protected
1029 */
1030 protected function placeCornerA($marr, $nrow, $ncol, $chr) {
1031 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
1032 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 1, $chr, 2);
1033 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 2, $chr, 3);
1034 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
1035 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
1036 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
1037 $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
1038 $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
1039 return $marr;
1040 }
1041
1042 /**
1043 * Places the 8 bits of the second special corner case.
1044 * (Annex F - ECC 200 symbol character placement)
1045 * @param $marr (array) Array of symbols.
1046 * @param $nrow (int) Number of rows.
1047 * @param $ncol (int) Number of columns.
1048 * @param $chr (int) Char byte.
1049 * @return array
1050 * @protected
1051 */
1052 protected function placeCornerB($marr, $nrow, $ncol, $chr) {
1053 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
1054 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
1055 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
1056 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-4, $chr, 4);
1057 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 5);
1058 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 6);
1059 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 7);
1060 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
1061 return $marr;
1062 }
1063
1064 /**
1065 * Places the 8 bits of the third special corner case.
1066 * (Annex F - ECC 200 symbol character placement)
1067 * @param $marr (array) Array of symbols.
1068 * @param $nrow (int) Number of rows.
1069 * @param $ncol (int) Number of columns.
1070 * @param $chr (int) Char byte.
1071 * @return array
1072 * @protected
1073 */
1074 protected function placeCornerC($marr, $nrow, $ncol, $chr) {
1075 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
1076 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
1077 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
1078 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
1079 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
1080 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
1081 $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
1082 $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
1083 return $marr;
1084 }
1085
1086 /**
1087 * Places the 8 bits of the fourth special corner case.
1088 * (Annex F - ECC 200 symbol character placement)
1089 * @param $marr (array) Array of symbols.
1090 * @param $nrow (int) Number of rows.
1091 * @param $ncol (int) Number of columns.
1092 * @param $chr (int) Char byte.
1093 * @return array
1094 * @protected
1095 */
1096 protected function placeCornerD($marr, $nrow, $ncol, $chr) {
1097 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
1098 $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, $ncol-1, $chr, 2);
1099 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 3);
1100 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
1101 $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
1102 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-3, $chr, 6);
1103 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-2, $chr, 7);
1104 $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
1105 return $marr;
1106 }
1107
1108 /**
1109 * Build a placement map.
1110 * (Annex F - ECC 200 symbol character placement)
1111 * @param $nrow (int) Number of rows.
1112 * @param $ncol (int) Number of columns.
1113 * @return array
1114 * @protected
1115 */
1116 protected function getPlacementMap($nrow, $ncol) {
1117 // initialize array with zeros
1118 $marr = array_fill(0, ($nrow * $ncol), 0);
1119 // set starting values
1120 $chr = 1;
1121 $row = 4;
1122 $col = 0;
1123 do {
1124 // repeatedly first check for one of the special corner cases, then
1125 if (($row == $nrow) AND ($col == 0)) {
1126 $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr);
1127 ++$chr;
1128 }
1129 if (($row == ($nrow - 2)) AND ($col == 0) AND ($ncol % 4)) {
1130 $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr);
1131 ++$chr;
1132 }
1133 if (($row == ($nrow - 2)) AND ($col == 0) AND (($ncol % 8) == 4)) {
1134 $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr);
1135 ++$chr;
1136 }
1137 if (($row == ($nrow + 4)) AND ($col == 2) AND (!($ncol % 8))) {
1138 $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr);
1139 ++$chr;
1140 }
1141 // sweep upward diagonally, inserting successive characters,
1142 do {
1143 if (($row < $nrow) AND ($col >= 0) AND (!$marr[(($row * $ncol) + $col)])) {
1144 $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
1145 ++$chr;
1146 }
1147 $row -= 2;
1148 $col += 2;
1149 } while (($row >= 0) AND ($col < $ncol));
1150 ++$row;
1151 $col += 3;
1152 // & then sweep downward diagonally, inserting successive characters,...
1153 do {
1154 if (($row >= 0) AND ($col < $ncol) AND (!$marr[(($row * $ncol) + $col)])) {
1155 $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
1156 ++$chr;
1157 }
1158 $row += 2;
1159 $col -= 2;
1160 } while (($row < $nrow) AND ($col >= 0));
1161 $row += 3;
1162 ++$col;
1163 // ... until the entire array is scanned
1164 } while (($row < $nrow) OR ($col < $ncol));
1165 // lastly, if the lower righthand corner is untouched, fill in fixed pattern
1166 if (!$marr[(($nrow * $ncol) - 1)]) {
1167 $marr[(($nrow * $ncol) - 1)] = 1;
1168 $marr[(($nrow * $ncol) - $ncol - 2)] = 1;
1169 }
1170 return $marr;
1171 }
1172
1173} // end DataMatrix class
1174//============================================================+
1175// END OF FILE
1176//============================================================+
diff --git a/inc/3rdparty/libraries/tcpdf/include/barcodes/pdf417.php b/inc/3rdparty/libraries/tcpdf/include/barcodes/pdf417.php
new file mode 100644
index 00000000..07b36b90
--- /dev/null
+++ b/inc/3rdparty/libraries/tcpdf/include/barcodes/pdf417.php
@@ -0,0 +1,996 @@
1<?php
2//============================================================+
3// File name : pdf417.php
4// Version : 1.0.005
5// Begin : 2010-06-03
6// Last Update : 2014-04-25
7// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
8// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9// -------------------------------------------------------------------
10// Copyright (C) 2010-2013 Nicola Asuni - Tecnick.com LTD
11//
12// This file is part of TCPDF software library.
13//
14// TCPDF is free software: you can redistribute it and/or modify it
15// under the terms of the GNU Lesser General Public License as
16// published by the Free Software Foundation, either version 3 of the
17// License, or (at your option) any later version.
18//
19// TCPDF is distributed in the hope that it will be useful, but
20// WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22// See the GNU Lesser General Public License for more details.
23//
24// You should have received a copy of the GNU Lesser General Public License
25// along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
26//
27// See LICENSE.TXT file for more information.
28// -------------------------------------------------------------------
29//
30// DESCRIPTION :
31//
32// Class to create PDF417 barcode arrays for TCPDF class.
33// PDF417 (ISO/IEC 15438:2006) is a 2-dimensional stacked bar code created by Symbol Technologies in 1991.
34// It is one of the most popular 2D codes because of its ability to be read with slightly modified handheld laser or linear CCD scanners.
35// TECHNICAL DATA / FEATURES OF PDF417:
36// Encodable Character Set: All 128 ASCII Characters (including extended)
37// Code Type: Continuous, Multi-Row
38// Symbol Height: 3 - 90 Rows
39// Symbol Width: 90X - 583X
40// Bidirectional Decoding: Yes
41// Error Correction Characters: 2 - 512
42// Maximum Data Characters: 1850 text, 2710 digits, 1108 bytes
43//
44//============================================================+
45
46/**
47 * @file
48 * Class to create PDF417 barcode arrays for TCPDF class.
49 * PDF417 (ISO/IEC 15438:2006) is a 2-dimensional stacked bar code created by Symbol Technologies in 1991.
50 * (requires PHP bcmath extension)
51 * @package com.tecnick.tcpdf
52 * @author Nicola Asuni
53 * @version 1.0.005
54 */
55
56// definitions
57if (!defined('PDF417DEFS')) {
58
59 /**
60 * Indicate that definitions for this class are set
61 */
62 define('PDF417DEFS', true);
63
64 // -----------------------------------------------------
65
66 /**
67 * Row height respect X dimension of single module
68 */
69 define('ROWHEIGHT', 4);
70
71 /**
72 * Horizontal quiet zone in modules
73 */
74 define('QUIETH', 2);
75
76 /**
77 * Vertical quiet zone in modules
78 */
79 define('QUIETV', 2);
80
81} // end of definitions
82
83// #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
84
85/**
86 * @class PDF417
87 * Class to create PDF417 barcode arrays for TCPDF class.
88 * PDF417 (ISO/IEC 15438:2006) is a 2-dimensional stacked bar code created by Symbol Technologies in 1991.
89 * @package com.tecnick.tcpdf
90 * @author Nicola Asuni
91 * @version 1.0.003
92 */
93class PDF417 {
94
95 /**
96 * Barcode array to be returned which is readable by TCPDF.
97 * @protected
98 */
99 protected $barcode_array = array();
100
101 /**
102 * Start pattern.
103 * @protected
104 */
105 protected $start_pattern = '11111111010101000';
106
107 /**
108 * Stop pattern.
109 * @protected
110 */
111 protected $stop_pattern = '111111101000101001';
112
113 /**
114 * Array of text Compaction Sub-Modes (values 0xFB - 0xFF are used for submode changers).
115 * @protected
116 */
117 protected $textsubmodes = array(
118 array(0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x20,0xFD,0xFE,0xFF), // Alpha
119 array(0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x20,0xFD,0xFE,0xFF), // Lower
120 array(0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x26,0x0d,0x09,0x2c,0x3a,0x23,0x2d,0x2e,0x24,0x2f,0x2b,0x25,0x2a,0x3d,0x5e,0xFB,0x20,0xFD,0xFE,0xFF), // Mixed
121 array(0x3b,0x3c,0x3e,0x40,0x5b,0x5c,0x5d,0x5f,0x60,0x7e,0x21,0x0d,0x09,0x2c,0x3a,0x0a,0x2d,0x2e,0x24,0x2f,0x22,0x7c,0x2a,0x28,0x29,0x3f,0x7b,0x7d,0x27,0xFF) // Puntuaction
122 );
123
124 /**
125 * Array of switching codes for Text Compaction Sub-Modes.
126 * @protected
127 */
128 protected $textlatch = array(
129 '01' => array(27), '02' => array(28), '03' => array(28,25), //
130 '10' => array(28,28), '12' => array(28), '13' => array(28,25), //
131 '20' => array(28), '21' => array(27), '23' => array(25), //
132 '30' => array(29), '31' => array(29,27), '32' => array(29,28) //
133 );
134
135 /**
136 * Clusters of codewords (0, 3, 6)<br/>
137 * Values are hex equivalents of binary representation of bars (1 = bar, 0 = space).<br/>
138 * The codewords numbered from 900 to 928 have special meaning, some enable to switch between modes in order to optimise the code:<ul>
139 * <li>900 : Switch to "Text" mode</li>
140 * <li>901 : Switch to "Byte" mode</li>
141 * <li>902 : Switch to "Numeric" mode</li>
142 * <li>903 - 912 : Reserved</li>
143 * <li>913 : Switch to "Octet" only for the next codeword</li>
144 * <li>914 - 920 : Reserved</li>
145 * <li>921 : Initialization</li>
146 * <li>922 : Terminator codeword for Macro PDF control block</li>
147 * <li>923 : Sequence tag to identify the beginning of optional fields in the Macro PDF control block</li>
148 * <li>924 : Switch to "Byte" mode (If the total number of byte is multiple of 6)</li>
149 * <li>925 : Identifier for a user defined Extended Channel Interpretation (ECI)</li>
150 * <li>926 : Identifier for a general purpose ECI format</li>
151 * <li>927 : Identifier for an ECI of a character set or code page</li>
152 * <li>928 : Macro marker codeword to indicate the beginning of a Macro PDF Control Block</li>
153 * </ul>
154 * @protected
155 */
156 protected $clusters = array(
157 array( // cluster 0 -----------------------------------------------------------------------
158 0x1d5c0,0x1eaf0,0x1f57c,0x1d4e0,0x1ea78,0x1f53e,0x1a8c0,0x1d470,0x1a860,0x15040, // 10
159 0x1a830,0x15020,0x1adc0,0x1d6f0,0x1eb7c,0x1ace0,0x1d678,0x1eb3e,0x158c0,0x1ac70, // 20
160 0x15860,0x15dc0,0x1aef0,0x1d77c,0x15ce0,0x1ae78,0x1d73e,0x15c70,0x1ae3c,0x15ef0, // 30
161 0x1af7c,0x15e78,0x1af3e,0x15f7c,0x1f5fa,0x1d2e0,0x1e978,0x1f4be,0x1a4c0,0x1d270, // 40
162 0x1e93c,0x1a460,0x1d238,0x14840,0x1a430,0x1d21c,0x14820,0x1a418,0x14810,0x1a6e0, // 50
163 0x1d378,0x1e9be,0x14cc0,0x1a670,0x1d33c,0x14c60,0x1a638,0x1d31e,0x14c30,0x1a61c, // 60
164 0x14ee0,0x1a778,0x1d3be,0x14e70,0x1a73c,0x14e38,0x1a71e,0x14f78,0x1a7be,0x14f3c, // 70
165 0x14f1e,0x1a2c0,0x1d170,0x1e8bc,0x1a260,0x1d138,0x1e89e,0x14440,0x1a230,0x1d11c, // 80
166 0x14420,0x1a218,0x14410,0x14408,0x146c0,0x1a370,0x1d1bc,0x14660,0x1a338,0x1d19e, // 90
167 0x14630,0x1a31c,0x14618,0x1460c,0x14770,0x1a3bc,0x14738,0x1a39e,0x1471c,0x147bc, // 100
168 0x1a160,0x1d0b8,0x1e85e,0x14240,0x1a130,0x1d09c,0x14220,0x1a118,0x1d08e,0x14210, // 110
169 0x1a10c,0x14208,0x1a106,0x14360,0x1a1b8,0x1d0de,0x14330,0x1a19c,0x14318,0x1a18e, // 120
170 0x1430c,0x14306,0x1a1de,0x1438e,0x14140,0x1a0b0,0x1d05c,0x14120,0x1a098,0x1d04e, // 130
171 0x14110,0x1a08c,0x14108,0x1a086,0x14104,0x141b0,0x14198,0x1418c,0x140a0,0x1d02e, // 140
172 0x1a04c,0x1a046,0x14082,0x1cae0,0x1e578,0x1f2be,0x194c0,0x1ca70,0x1e53c,0x19460, // 150
173 0x1ca38,0x1e51e,0x12840,0x19430,0x12820,0x196e0,0x1cb78,0x1e5be,0x12cc0,0x19670, // 160
174 0x1cb3c,0x12c60,0x19638,0x12c30,0x12c18,0x12ee0,0x19778,0x1cbbe,0x12e70,0x1973c, // 170
175 0x12e38,0x12e1c,0x12f78,0x197be,0x12f3c,0x12fbe,0x1dac0,0x1ed70,0x1f6bc,0x1da60, // 180
176 0x1ed38,0x1f69e,0x1b440,0x1da30,0x1ed1c,0x1b420,0x1da18,0x1ed0e,0x1b410,0x1da0c, // 190
177 0x192c0,0x1c970,0x1e4bc,0x1b6c0,0x19260,0x1c938,0x1e49e,0x1b660,0x1db38,0x1ed9e, // 200
178 0x16c40,0x12420,0x19218,0x1c90e,0x16c20,0x1b618,0x16c10,0x126c0,0x19370,0x1c9bc, // 210
179 0x16ec0,0x12660,0x19338,0x1c99e,0x16e60,0x1b738,0x1db9e,0x16e30,0x12618,0x16e18, // 220
180 0x12770,0x193bc,0x16f70,0x12738,0x1939e,0x16f38,0x1b79e,0x16f1c,0x127bc,0x16fbc, // 230
181 0x1279e,0x16f9e,0x1d960,0x1ecb8,0x1f65e,0x1b240,0x1d930,0x1ec9c,0x1b220,0x1d918, // 240
182 0x1ec8e,0x1b210,0x1d90c,0x1b208,0x1b204,0x19160,0x1c8b8,0x1e45e,0x1b360,0x19130, // 250
183 0x1c89c,0x16640,0x12220,0x1d99c,0x1c88e,0x16620,0x12210,0x1910c,0x16610,0x1b30c, // 260
184 0x19106,0x12204,0x12360,0x191b8,0x1c8de,0x16760,0x12330,0x1919c,0x16730,0x1b39c, // 270
185 0x1918e,0x16718,0x1230c,0x12306,0x123b8,0x191de,0x167b8,0x1239c,0x1679c,0x1238e, // 280
186 0x1678e,0x167de,0x1b140,0x1d8b0,0x1ec5c,0x1b120,0x1d898,0x1ec4e,0x1b110,0x1d88c, // 290
187 0x1b108,0x1d886,0x1b104,0x1b102,0x12140,0x190b0,0x1c85c,0x16340,0x12120,0x19098, // 300
188 0x1c84e,0x16320,0x1b198,0x1d8ce,0x16310,0x12108,0x19086,0x16308,0x1b186,0x16304, // 310
189 0x121b0,0x190dc,0x163b0,0x12198,0x190ce,0x16398,0x1b1ce,0x1638c,0x12186,0x16386, // 320
190 0x163dc,0x163ce,0x1b0a0,0x1d858,0x1ec2e,0x1b090,0x1d84c,0x1b088,0x1d846,0x1b084, // 330
191 0x1b082,0x120a0,0x19058,0x1c82e,0x161a0,0x12090,0x1904c,0x16190,0x1b0cc,0x19046, // 340
192 0x16188,0x12084,0x16184,0x12082,0x120d8,0x161d8,0x161cc,0x161c6,0x1d82c,0x1d826, // 350
193 0x1b042,0x1902c,0x12048,0x160c8,0x160c4,0x160c2,0x18ac0,0x1c570,0x1e2bc,0x18a60, // 360
194 0x1c538,0x11440,0x18a30,0x1c51c,0x11420,0x18a18,0x11410,0x11408,0x116c0,0x18b70, // 370
195 0x1c5bc,0x11660,0x18b38,0x1c59e,0x11630,0x18b1c,0x11618,0x1160c,0x11770,0x18bbc, // 380
196 0x11738,0x18b9e,0x1171c,0x117bc,0x1179e,0x1cd60,0x1e6b8,0x1f35e,0x19a40,0x1cd30, // 390
197 0x1e69c,0x19a20,0x1cd18,0x1e68e,0x19a10,0x1cd0c,0x19a08,0x1cd06,0x18960,0x1c4b8, // 400
198 0x1e25e,0x19b60,0x18930,0x1c49c,0x13640,0x11220,0x1cd9c,0x1c48e,0x13620,0x19b18, // 410
199 0x1890c,0x13610,0x11208,0x13608,0x11360,0x189b8,0x1c4de,0x13760,0x11330,0x1cdde, // 420
200 0x13730,0x19b9c,0x1898e,0x13718,0x1130c,0x1370c,0x113b8,0x189de,0x137b8,0x1139c, // 430
201 0x1379c,0x1138e,0x113de,0x137de,0x1dd40,0x1eeb0,0x1f75c,0x1dd20,0x1ee98,0x1f74e, // 440
202 0x1dd10,0x1ee8c,0x1dd08,0x1ee86,0x1dd04,0x19940,0x1ccb0,0x1e65c,0x1bb40,0x19920, // 450
203 0x1eedc,0x1e64e,0x1bb20,0x1dd98,0x1eece,0x1bb10,0x19908,0x1cc86,0x1bb08,0x1dd86, // 460
204 0x19902,0x11140,0x188b0,0x1c45c,0x13340,0x11120,0x18898,0x1c44e,0x17740,0x13320, // 470
205 0x19998,0x1ccce,0x17720,0x1bb98,0x1ddce,0x18886,0x17710,0x13308,0x19986,0x17708, // 480
206 0x11102,0x111b0,0x188dc,0x133b0,0x11198,0x188ce,0x177b0,0x13398,0x199ce,0x17798, // 490
207 0x1bbce,0x11186,0x13386,0x111dc,0x133dc,0x111ce,0x177dc,0x133ce,0x1dca0,0x1ee58, // 500
208 0x1f72e,0x1dc90,0x1ee4c,0x1dc88,0x1ee46,0x1dc84,0x1dc82,0x198a0,0x1cc58,0x1e62e, // 510
209 0x1b9a0,0x19890,0x1ee6e,0x1b990,0x1dccc,0x1cc46,0x1b988,0x19884,0x1b984,0x19882, // 520
210 0x1b982,0x110a0,0x18858,0x1c42e,0x131a0,0x11090,0x1884c,0x173a0,0x13190,0x198cc, // 530
211 0x18846,0x17390,0x1b9cc,0x11084,0x17388,0x13184,0x11082,0x13182,0x110d8,0x1886e, // 540
212 0x131d8,0x110cc,0x173d8,0x131cc,0x110c6,0x173cc,0x131c6,0x110ee,0x173ee,0x1dc50, // 550
213 0x1ee2c,0x1dc48,0x1ee26,0x1dc44,0x1dc42,0x19850,0x1cc2c,0x1b8d0,0x19848,0x1cc26, // 560
214 0x1b8c8,0x1dc66,0x1b8c4,0x19842,0x1b8c2,0x11050,0x1882c,0x130d0,0x11048,0x18826, // 570
215 0x171d0,0x130c8,0x19866,0x171c8,0x1b8e6,0x11042,0x171c4,0x130c2,0x171c2,0x130ec, // 580
216 0x171ec,0x171e6,0x1ee16,0x1dc22,0x1cc16,0x19824,0x19822,0x11028,0x13068,0x170e8, // 590
217 0x11022,0x13062,0x18560,0x10a40,0x18530,0x10a20,0x18518,0x1c28e,0x10a10,0x1850c, // 600
218 0x10a08,0x18506,0x10b60,0x185b8,0x1c2de,0x10b30,0x1859c,0x10b18,0x1858e,0x10b0c, // 610
219 0x10b06,0x10bb8,0x185de,0x10b9c,0x10b8e,0x10bde,0x18d40,0x1c6b0,0x1e35c,0x18d20, // 620
220 0x1c698,0x18d10,0x1c68c,0x18d08,0x1c686,0x18d04,0x10940,0x184b0,0x1c25c,0x11b40, // 630
221 0x10920,0x1c6dc,0x1c24e,0x11b20,0x18d98,0x1c6ce,0x11b10,0x10908,0x18486,0x11b08, // 640
222 0x18d86,0x10902,0x109b0,0x184dc,0x11bb0,0x10998,0x184ce,0x11b98,0x18dce,0x11b8c, // 650
223 0x10986,0x109dc,0x11bdc,0x109ce,0x11bce,0x1cea0,0x1e758,0x1f3ae,0x1ce90,0x1e74c, // 660
224 0x1ce88,0x1e746,0x1ce84,0x1ce82,0x18ca0,0x1c658,0x19da0,0x18c90,0x1c64c,0x19d90, // 670
225 0x1cecc,0x1c646,0x19d88,0x18c84,0x19d84,0x18c82,0x19d82,0x108a0,0x18458,0x119a0, // 680
226 0x10890,0x1c66e,0x13ba0,0x11990,0x18ccc,0x18446,0x13b90,0x19dcc,0x10884,0x13b88, // 690
227 0x11984,0x10882,0x11982,0x108d8,0x1846e,0x119d8,0x108cc,0x13bd8,0x119cc,0x108c6, // 700
228 0x13bcc,0x119c6,0x108ee,0x119ee,0x13bee,0x1ef50,0x1f7ac,0x1ef48,0x1f7a6,0x1ef44, // 710
229 0x1ef42,0x1ce50,0x1e72c,0x1ded0,0x1ef6c,0x1e726,0x1dec8,0x1ef66,0x1dec4,0x1ce42, // 720
230 0x1dec2,0x18c50,0x1c62c,0x19cd0,0x18c48,0x1c626,0x1bdd0,0x19cc8,0x1ce66,0x1bdc8, // 730
231 0x1dee6,0x18c42,0x1bdc4,0x19cc2,0x1bdc2,0x10850,0x1842c,0x118d0,0x10848,0x18426, // 740
232 0x139d0,0x118c8,0x18c66,0x17bd0,0x139c8,0x19ce6,0x10842,0x17bc8,0x1bde6,0x118c2, // 750
233 0x17bc4,0x1086c,0x118ec,0x10866,0x139ec,0x118e6,0x17bec,0x139e6,0x17be6,0x1ef28, // 760
234 0x1f796,0x1ef24,0x1ef22,0x1ce28,0x1e716,0x1de68,0x1ef36,0x1de64,0x1ce22,0x1de62, // 770
235 0x18c28,0x1c616,0x19c68,0x18c24,0x1bce8,0x19c64,0x18c22,0x1bce4,0x19c62,0x1bce2, // 780
236 0x10828,0x18416,0x11868,0x18c36,0x138e8,0x11864,0x10822,0x179e8,0x138e4,0x11862, // 790
237 0x179e4,0x138e2,0x179e2,0x11876,0x179f6,0x1ef12,0x1de34,0x1de32,0x19c34,0x1bc74, // 800
238 0x1bc72,0x11834,0x13874,0x178f4,0x178f2,0x10540,0x10520,0x18298,0x10510,0x10508, // 810
239 0x10504,0x105b0,0x10598,0x1058c,0x10586,0x105dc,0x105ce,0x186a0,0x18690,0x1c34c, // 820
240 0x18688,0x1c346,0x18684,0x18682,0x104a0,0x18258,0x10da0,0x186d8,0x1824c,0x10d90, // 830
241 0x186cc,0x10d88,0x186c6,0x10d84,0x10482,0x10d82,0x104d8,0x1826e,0x10dd8,0x186ee, // 840
242 0x10dcc,0x104c6,0x10dc6,0x104ee,0x10dee,0x1c750,0x1c748,0x1c744,0x1c742,0x18650, // 850
243 0x18ed0,0x1c76c,0x1c326,0x18ec8,0x1c766,0x18ec4,0x18642,0x18ec2,0x10450,0x10cd0, // 860
244 0x10448,0x18226,0x11dd0,0x10cc8,0x10444,0x11dc8,0x10cc4,0x10442,0x11dc4,0x10cc2, // 870
245 0x1046c,0x10cec,0x10466,0x11dec,0x10ce6,0x11de6,0x1e7a8,0x1e7a4,0x1e7a2,0x1c728, // 880
246 0x1cf68,0x1e7b6,0x1cf64,0x1c722,0x1cf62,0x18628,0x1c316,0x18e68,0x1c736,0x19ee8, // 890
247 0x18e64,0x18622,0x19ee4,0x18e62,0x19ee2,0x10428,0x18216,0x10c68,0x18636,0x11ce8, // 900
248 0x10c64,0x10422,0x13de8,0x11ce4,0x10c62,0x13de4,0x11ce2,0x10436,0x10c76,0x11cf6, // 910
249 0x13df6,0x1f7d4,0x1f7d2,0x1e794,0x1efb4,0x1e792,0x1efb2,0x1c714,0x1cf34,0x1c712, // 920
250 0x1df74,0x1cf32,0x1df72,0x18614,0x18e34,0x18612,0x19e74,0x18e32,0x1bef4), // 929
251 array( // cluster 3 -----------------------------------------------------------------------
252 0x1f560,0x1fab8,0x1ea40,0x1f530,0x1fa9c,0x1ea20,0x1f518,0x1fa8e,0x1ea10,0x1f50c, // 10
253 0x1ea08,0x1f506,0x1ea04,0x1eb60,0x1f5b8,0x1fade,0x1d640,0x1eb30,0x1f59c,0x1d620, // 20
254 0x1eb18,0x1f58e,0x1d610,0x1eb0c,0x1d608,0x1eb06,0x1d604,0x1d760,0x1ebb8,0x1f5de, // 30
255 0x1ae40,0x1d730,0x1eb9c,0x1ae20,0x1d718,0x1eb8e,0x1ae10,0x1d70c,0x1ae08,0x1d706, // 40
256 0x1ae04,0x1af60,0x1d7b8,0x1ebde,0x15e40,0x1af30,0x1d79c,0x15e20,0x1af18,0x1d78e, // 50
257 0x15e10,0x1af0c,0x15e08,0x1af06,0x15f60,0x1afb8,0x1d7de,0x15f30,0x1af9c,0x15f18, // 60
258 0x1af8e,0x15f0c,0x15fb8,0x1afde,0x15f9c,0x15f8e,0x1e940,0x1f4b0,0x1fa5c,0x1e920, // 70
259 0x1f498,0x1fa4e,0x1e910,0x1f48c,0x1e908,0x1f486,0x1e904,0x1e902,0x1d340,0x1e9b0, // 80
260 0x1f4dc,0x1d320,0x1e998,0x1f4ce,0x1d310,0x1e98c,0x1d308,0x1e986,0x1d304,0x1d302, // 90
261 0x1a740,0x1d3b0,0x1e9dc,0x1a720,0x1d398,0x1e9ce,0x1a710,0x1d38c,0x1a708,0x1d386, // 100
262 0x1a704,0x1a702,0x14f40,0x1a7b0,0x1d3dc,0x14f20,0x1a798,0x1d3ce,0x14f10,0x1a78c, // 110
263 0x14f08,0x1a786,0x14f04,0x14fb0,0x1a7dc,0x14f98,0x1a7ce,0x14f8c,0x14f86,0x14fdc, // 120
264 0x14fce,0x1e8a0,0x1f458,0x1fa2e,0x1e890,0x1f44c,0x1e888,0x1f446,0x1e884,0x1e882, // 130
265 0x1d1a0,0x1e8d8,0x1f46e,0x1d190,0x1e8cc,0x1d188,0x1e8c6,0x1d184,0x1d182,0x1a3a0, // 140
266 0x1d1d8,0x1e8ee,0x1a390,0x1d1cc,0x1a388,0x1d1c6,0x1a384,0x1a382,0x147a0,0x1a3d8, // 150
267 0x1d1ee,0x14790,0x1a3cc,0x14788,0x1a3c6,0x14784,0x14782,0x147d8,0x1a3ee,0x147cc, // 160
268 0x147c6,0x147ee,0x1e850,0x1f42c,0x1e848,0x1f426,0x1e844,0x1e842,0x1d0d0,0x1e86c, // 170
269 0x1d0c8,0x1e866,0x1d0c4,0x1d0c2,0x1a1d0,0x1d0ec,0x1a1c8,0x1d0e6,0x1a1c4,0x1a1c2, // 180
270 0x143d0,0x1a1ec,0x143c8,0x1a1e6,0x143c4,0x143c2,0x143ec,0x143e6,0x1e828,0x1f416, // 190
271 0x1e824,0x1e822,0x1d068,0x1e836,0x1d064,0x1d062,0x1a0e8,0x1d076,0x1a0e4,0x1a0e2, // 200
272 0x141e8,0x1a0f6,0x141e4,0x141e2,0x1e814,0x1e812,0x1d034,0x1d032,0x1a074,0x1a072, // 210
273 0x1e540,0x1f2b0,0x1f95c,0x1e520,0x1f298,0x1f94e,0x1e510,0x1f28c,0x1e508,0x1f286, // 220
274 0x1e504,0x1e502,0x1cb40,0x1e5b0,0x1f2dc,0x1cb20,0x1e598,0x1f2ce,0x1cb10,0x1e58c, // 230
275 0x1cb08,0x1e586,0x1cb04,0x1cb02,0x19740,0x1cbb0,0x1e5dc,0x19720,0x1cb98,0x1e5ce, // 240
276 0x19710,0x1cb8c,0x19708,0x1cb86,0x19704,0x19702,0x12f40,0x197b0,0x1cbdc,0x12f20, // 250
277 0x19798,0x1cbce,0x12f10,0x1978c,0x12f08,0x19786,0x12f04,0x12fb0,0x197dc,0x12f98, // 260
278 0x197ce,0x12f8c,0x12f86,0x12fdc,0x12fce,0x1f6a0,0x1fb58,0x16bf0,0x1f690,0x1fb4c, // 270
279 0x169f8,0x1f688,0x1fb46,0x168fc,0x1f684,0x1f682,0x1e4a0,0x1f258,0x1f92e,0x1eda0, // 280
280 0x1e490,0x1fb6e,0x1ed90,0x1f6cc,0x1f246,0x1ed88,0x1e484,0x1ed84,0x1e482,0x1ed82, // 290
281 0x1c9a0,0x1e4d8,0x1f26e,0x1dba0,0x1c990,0x1e4cc,0x1db90,0x1edcc,0x1e4c6,0x1db88, // 300
282 0x1c984,0x1db84,0x1c982,0x1db82,0x193a0,0x1c9d8,0x1e4ee,0x1b7a0,0x19390,0x1c9cc, // 310
283 0x1b790,0x1dbcc,0x1c9c6,0x1b788,0x19384,0x1b784,0x19382,0x1b782,0x127a0,0x193d8, // 320
284 0x1c9ee,0x16fa0,0x12790,0x193cc,0x16f90,0x1b7cc,0x193c6,0x16f88,0x12784,0x16f84, // 330
285 0x12782,0x127d8,0x193ee,0x16fd8,0x127cc,0x16fcc,0x127c6,0x16fc6,0x127ee,0x1f650, // 340
286 0x1fb2c,0x165f8,0x1f648,0x1fb26,0x164fc,0x1f644,0x1647e,0x1f642,0x1e450,0x1f22c, // 350
287 0x1ecd0,0x1e448,0x1f226,0x1ecc8,0x1f666,0x1ecc4,0x1e442,0x1ecc2,0x1c8d0,0x1e46c, // 360
288 0x1d9d0,0x1c8c8,0x1e466,0x1d9c8,0x1ece6,0x1d9c4,0x1c8c2,0x1d9c2,0x191d0,0x1c8ec, // 370
289 0x1b3d0,0x191c8,0x1c8e6,0x1b3c8,0x1d9e6,0x1b3c4,0x191c2,0x1b3c2,0x123d0,0x191ec, // 380
290 0x167d0,0x123c8,0x191e6,0x167c8,0x1b3e6,0x167c4,0x123c2,0x167c2,0x123ec,0x167ec, // 390
291 0x123e6,0x167e6,0x1f628,0x1fb16,0x162fc,0x1f624,0x1627e,0x1f622,0x1e428,0x1f216, // 400
292 0x1ec68,0x1f636,0x1ec64,0x1e422,0x1ec62,0x1c868,0x1e436,0x1d8e8,0x1c864,0x1d8e4, // 410
293 0x1c862,0x1d8e2,0x190e8,0x1c876,0x1b1e8,0x1d8f6,0x1b1e4,0x190e2,0x1b1e2,0x121e8, // 420
294 0x190f6,0x163e8,0x121e4,0x163e4,0x121e2,0x163e2,0x121f6,0x163f6,0x1f614,0x1617e, // 430
295 0x1f612,0x1e414,0x1ec34,0x1e412,0x1ec32,0x1c834,0x1d874,0x1c832,0x1d872,0x19074, // 440
296 0x1b0f4,0x19072,0x1b0f2,0x120f4,0x161f4,0x120f2,0x161f2,0x1f60a,0x1e40a,0x1ec1a, // 450
297 0x1c81a,0x1d83a,0x1903a,0x1b07a,0x1e2a0,0x1f158,0x1f8ae,0x1e290,0x1f14c,0x1e288, // 460
298 0x1f146,0x1e284,0x1e282,0x1c5a0,0x1e2d8,0x1f16e,0x1c590,0x1e2cc,0x1c588,0x1e2c6, // 470
299 0x1c584,0x1c582,0x18ba0,0x1c5d8,0x1e2ee,0x18b90,0x1c5cc,0x18b88,0x1c5c6,0x18b84, // 480
300 0x18b82,0x117a0,0x18bd8,0x1c5ee,0x11790,0x18bcc,0x11788,0x18bc6,0x11784,0x11782, // 490
301 0x117d8,0x18bee,0x117cc,0x117c6,0x117ee,0x1f350,0x1f9ac,0x135f8,0x1f348,0x1f9a6, // 500
302 0x134fc,0x1f344,0x1347e,0x1f342,0x1e250,0x1f12c,0x1e6d0,0x1e248,0x1f126,0x1e6c8, // 510
303 0x1f366,0x1e6c4,0x1e242,0x1e6c2,0x1c4d0,0x1e26c,0x1cdd0,0x1c4c8,0x1e266,0x1cdc8, // 520
304 0x1e6e6,0x1cdc4,0x1c4c2,0x1cdc2,0x189d0,0x1c4ec,0x19bd0,0x189c8,0x1c4e6,0x19bc8, // 530
305 0x1cde6,0x19bc4,0x189c2,0x19bc2,0x113d0,0x189ec,0x137d0,0x113c8,0x189e6,0x137c8, // 540
306 0x19be6,0x137c4,0x113c2,0x137c2,0x113ec,0x137ec,0x113e6,0x137e6,0x1fba8,0x175f0, // 550
307 0x1bafc,0x1fba4,0x174f8,0x1ba7e,0x1fba2,0x1747c,0x1743e,0x1f328,0x1f996,0x132fc, // 560
308 0x1f768,0x1fbb6,0x176fc,0x1327e,0x1f764,0x1f322,0x1767e,0x1f762,0x1e228,0x1f116, // 570
309 0x1e668,0x1e224,0x1eee8,0x1f776,0x1e222,0x1eee4,0x1e662,0x1eee2,0x1c468,0x1e236, // 580
310 0x1cce8,0x1c464,0x1dde8,0x1cce4,0x1c462,0x1dde4,0x1cce2,0x1dde2,0x188e8,0x1c476, // 590
311 0x199e8,0x188e4,0x1bbe8,0x199e4,0x188e2,0x1bbe4,0x199e2,0x1bbe2,0x111e8,0x188f6, // 600
312 0x133e8,0x111e4,0x177e8,0x133e4,0x111e2,0x177e4,0x133e2,0x177e2,0x111f6,0x133f6, // 610
313 0x1fb94,0x172f8,0x1b97e,0x1fb92,0x1727c,0x1723e,0x1f314,0x1317e,0x1f734,0x1f312, // 620
314 0x1737e,0x1f732,0x1e214,0x1e634,0x1e212,0x1ee74,0x1e632,0x1ee72,0x1c434,0x1cc74, // 630
315 0x1c432,0x1dcf4,0x1cc72,0x1dcf2,0x18874,0x198f4,0x18872,0x1b9f4,0x198f2,0x1b9f2, // 640
316 0x110f4,0x131f4,0x110f2,0x173f4,0x131f2,0x173f2,0x1fb8a,0x1717c,0x1713e,0x1f30a, // 650
317 0x1f71a,0x1e20a,0x1e61a,0x1ee3a,0x1c41a,0x1cc3a,0x1dc7a,0x1883a,0x1987a,0x1b8fa, // 660
318 0x1107a,0x130fa,0x171fa,0x170be,0x1e150,0x1f0ac,0x1e148,0x1f0a6,0x1e144,0x1e142, // 670
319 0x1c2d0,0x1e16c,0x1c2c8,0x1e166,0x1c2c4,0x1c2c2,0x185d0,0x1c2ec,0x185c8,0x1c2e6, // 680
320 0x185c4,0x185c2,0x10bd0,0x185ec,0x10bc8,0x185e6,0x10bc4,0x10bc2,0x10bec,0x10be6, // 690
321 0x1f1a8,0x1f8d6,0x11afc,0x1f1a4,0x11a7e,0x1f1a2,0x1e128,0x1f096,0x1e368,0x1e124, // 700
322 0x1e364,0x1e122,0x1e362,0x1c268,0x1e136,0x1c6e8,0x1c264,0x1c6e4,0x1c262,0x1c6e2, // 710
323 0x184e8,0x1c276,0x18de8,0x184e4,0x18de4,0x184e2,0x18de2,0x109e8,0x184f6,0x11be8, // 720
324 0x109e4,0x11be4,0x109e2,0x11be2,0x109f6,0x11bf6,0x1f9d4,0x13af8,0x19d7e,0x1f9d2, // 730
325 0x13a7c,0x13a3e,0x1f194,0x1197e,0x1f3b4,0x1f192,0x13b7e,0x1f3b2,0x1e114,0x1e334, // 740
326 0x1e112,0x1e774,0x1e332,0x1e772,0x1c234,0x1c674,0x1c232,0x1cef4,0x1c672,0x1cef2, // 750
327 0x18474,0x18cf4,0x18472,0x19df4,0x18cf2,0x19df2,0x108f4,0x119f4,0x108f2,0x13bf4, // 760
328 0x119f2,0x13bf2,0x17af0,0x1bd7c,0x17a78,0x1bd3e,0x17a3c,0x17a1e,0x1f9ca,0x1397c, // 770
329 0x1fbda,0x17b7c,0x1393e,0x17b3e,0x1f18a,0x1f39a,0x1f7ba,0x1e10a,0x1e31a,0x1e73a, // 780
330 0x1ef7a,0x1c21a,0x1c63a,0x1ce7a,0x1defa,0x1843a,0x18c7a,0x19cfa,0x1bdfa,0x1087a, // 790
331 0x118fa,0x139fa,0x17978,0x1bcbe,0x1793c,0x1791e,0x138be,0x179be,0x178bc,0x1789e, // 800
332 0x1785e,0x1e0a8,0x1e0a4,0x1e0a2,0x1c168,0x1e0b6,0x1c164,0x1c162,0x182e8,0x1c176, // 810
333 0x182e4,0x182e2,0x105e8,0x182f6,0x105e4,0x105e2,0x105f6,0x1f0d4,0x10d7e,0x1f0d2, // 820
334 0x1e094,0x1e1b4,0x1e092,0x1e1b2,0x1c134,0x1c374,0x1c132,0x1c372,0x18274,0x186f4, // 830
335 0x18272,0x186f2,0x104f4,0x10df4,0x104f2,0x10df2,0x1f8ea,0x11d7c,0x11d3e,0x1f0ca, // 840
336 0x1f1da,0x1e08a,0x1e19a,0x1e3ba,0x1c11a,0x1c33a,0x1c77a,0x1823a,0x1867a,0x18efa, // 850
337 0x1047a,0x10cfa,0x11dfa,0x13d78,0x19ebe,0x13d3c,0x13d1e,0x11cbe,0x13dbe,0x17d70, // 860
338 0x1bebc,0x17d38,0x1be9e,0x17d1c,0x17d0e,0x13cbc,0x17dbc,0x13c9e,0x17d9e,0x17cb8, // 870
339 0x1be5e,0x17c9c,0x17c8e,0x13c5e,0x17cde,0x17c5c,0x17c4e,0x17c2e,0x1c0b4,0x1c0b2, // 880
340 0x18174,0x18172,0x102f4,0x102f2,0x1e0da,0x1c09a,0x1c1ba,0x1813a,0x1837a,0x1027a, // 890
341 0x106fa,0x10ebe,0x11ebc,0x11e9e,0x13eb8,0x19f5e,0x13e9c,0x13e8e,0x11e5e,0x13ede, // 900
342 0x17eb0,0x1bf5c,0x17e98,0x1bf4e,0x17e8c,0x17e86,0x13e5c,0x17edc,0x13e4e,0x17ece, // 910
343 0x17e58,0x1bf2e,0x17e4c,0x17e46,0x13e2e,0x17e6e,0x17e2c,0x17e26,0x10f5e,0x11f5c, // 920
344 0x11f4e,0x13f58,0x19fae,0x13f4c,0x13f46,0x11f2e,0x13f6e,0x13f2c,0x13f26), // 929
345 array( // cluster 6 -----------------------------------------------------------------------
346 0x1abe0,0x1d5f8,0x153c0,0x1a9f0,0x1d4fc,0x151e0,0x1a8f8,0x1d47e,0x150f0,0x1a87c, // 10
347 0x15078,0x1fad0,0x15be0,0x1adf8,0x1fac8,0x159f0,0x1acfc,0x1fac4,0x158f8,0x1ac7e, // 20
348 0x1fac2,0x1587c,0x1f5d0,0x1faec,0x15df8,0x1f5c8,0x1fae6,0x15cfc,0x1f5c4,0x15c7e, // 30
349 0x1f5c2,0x1ebd0,0x1f5ec,0x1ebc8,0x1f5e6,0x1ebc4,0x1ebc2,0x1d7d0,0x1ebec,0x1d7c8, // 40
350 0x1ebe6,0x1d7c4,0x1d7c2,0x1afd0,0x1d7ec,0x1afc8,0x1d7e6,0x1afc4,0x14bc0,0x1a5f0, // 50
351 0x1d2fc,0x149e0,0x1a4f8,0x1d27e,0x148f0,0x1a47c,0x14878,0x1a43e,0x1483c,0x1fa68, // 60
352 0x14df0,0x1a6fc,0x1fa64,0x14cf8,0x1a67e,0x1fa62,0x14c7c,0x14c3e,0x1f4e8,0x1fa76, // 70
353 0x14efc,0x1f4e4,0x14e7e,0x1f4e2,0x1e9e8,0x1f4f6,0x1e9e4,0x1e9e2,0x1d3e8,0x1e9f6, // 80
354 0x1d3e4,0x1d3e2,0x1a7e8,0x1d3f6,0x1a7e4,0x1a7e2,0x145e0,0x1a2f8,0x1d17e,0x144f0, // 90
355 0x1a27c,0x14478,0x1a23e,0x1443c,0x1441e,0x1fa34,0x146f8,0x1a37e,0x1fa32,0x1467c, // 100
356 0x1463e,0x1f474,0x1477e,0x1f472,0x1e8f4,0x1e8f2,0x1d1f4,0x1d1f2,0x1a3f4,0x1a3f2, // 110
357 0x142f0,0x1a17c,0x14278,0x1a13e,0x1423c,0x1421e,0x1fa1a,0x1437c,0x1433e,0x1f43a, // 120
358 0x1e87a,0x1d0fa,0x14178,0x1a0be,0x1413c,0x1411e,0x141be,0x140bc,0x1409e,0x12bc0, // 130
359 0x195f0,0x1cafc,0x129e0,0x194f8,0x1ca7e,0x128f0,0x1947c,0x12878,0x1943e,0x1283c, // 140
360 0x1f968,0x12df0,0x196fc,0x1f964,0x12cf8,0x1967e,0x1f962,0x12c7c,0x12c3e,0x1f2e8, // 150
361 0x1f976,0x12efc,0x1f2e4,0x12e7e,0x1f2e2,0x1e5e8,0x1f2f6,0x1e5e4,0x1e5e2,0x1cbe8, // 160
362 0x1e5f6,0x1cbe4,0x1cbe2,0x197e8,0x1cbf6,0x197e4,0x197e2,0x1b5e0,0x1daf8,0x1ed7e, // 170
363 0x169c0,0x1b4f0,0x1da7c,0x168e0,0x1b478,0x1da3e,0x16870,0x1b43c,0x16838,0x1b41e, // 180
364 0x1681c,0x125e0,0x192f8,0x1c97e,0x16de0,0x124f0,0x1927c,0x16cf0,0x1b67c,0x1923e, // 190
365 0x16c78,0x1243c,0x16c3c,0x1241e,0x16c1e,0x1f934,0x126f8,0x1937e,0x1fb74,0x1f932, // 200
366 0x16ef8,0x1267c,0x1fb72,0x16e7c,0x1263e,0x16e3e,0x1f274,0x1277e,0x1f6f4,0x1f272, // 210
367 0x16f7e,0x1f6f2,0x1e4f4,0x1edf4,0x1e4f2,0x1edf2,0x1c9f4,0x1dbf4,0x1c9f2,0x1dbf2, // 220
368 0x193f4,0x193f2,0x165c0,0x1b2f0,0x1d97c,0x164e0,0x1b278,0x1d93e,0x16470,0x1b23c, // 230
369 0x16438,0x1b21e,0x1641c,0x1640e,0x122f0,0x1917c,0x166f0,0x12278,0x1913e,0x16678, // 240
370 0x1b33e,0x1663c,0x1221e,0x1661e,0x1f91a,0x1237c,0x1fb3a,0x1677c,0x1233e,0x1673e, // 250
371 0x1f23a,0x1f67a,0x1e47a,0x1ecfa,0x1c8fa,0x1d9fa,0x191fa,0x162e0,0x1b178,0x1d8be, // 260
372 0x16270,0x1b13c,0x16238,0x1b11e,0x1621c,0x1620e,0x12178,0x190be,0x16378,0x1213c, // 270
373 0x1633c,0x1211e,0x1631e,0x121be,0x163be,0x16170,0x1b0bc,0x16138,0x1b09e,0x1611c, // 280
374 0x1610e,0x120bc,0x161bc,0x1209e,0x1619e,0x160b8,0x1b05e,0x1609c,0x1608e,0x1205e, // 290
375 0x160de,0x1605c,0x1604e,0x115e0,0x18af8,0x1c57e,0x114f0,0x18a7c,0x11478,0x18a3e, // 300
376 0x1143c,0x1141e,0x1f8b4,0x116f8,0x18b7e,0x1f8b2,0x1167c,0x1163e,0x1f174,0x1177e, // 310
377 0x1f172,0x1e2f4,0x1e2f2,0x1c5f4,0x1c5f2,0x18bf4,0x18bf2,0x135c0,0x19af0,0x1cd7c, // 320
378 0x134e0,0x19a78,0x1cd3e,0x13470,0x19a3c,0x13438,0x19a1e,0x1341c,0x1340e,0x112f0, // 330
379 0x1897c,0x136f0,0x11278,0x1893e,0x13678,0x19b3e,0x1363c,0x1121e,0x1361e,0x1f89a, // 340
380 0x1137c,0x1f9ba,0x1377c,0x1133e,0x1373e,0x1f13a,0x1f37a,0x1e27a,0x1e6fa,0x1c4fa, // 350
381 0x1cdfa,0x189fa,0x1bae0,0x1dd78,0x1eebe,0x174c0,0x1ba70,0x1dd3c,0x17460,0x1ba38, // 360
382 0x1dd1e,0x17430,0x1ba1c,0x17418,0x1ba0e,0x1740c,0x132e0,0x19978,0x1ccbe,0x176e0, // 370
383 0x13270,0x1993c,0x17670,0x1bb3c,0x1991e,0x17638,0x1321c,0x1761c,0x1320e,0x1760e, // 380
384 0x11178,0x188be,0x13378,0x1113c,0x17778,0x1333c,0x1111e,0x1773c,0x1331e,0x1771e, // 390
385 0x111be,0x133be,0x177be,0x172c0,0x1b970,0x1dcbc,0x17260,0x1b938,0x1dc9e,0x17230, // 400
386 0x1b91c,0x17218,0x1b90e,0x1720c,0x17206,0x13170,0x198bc,0x17370,0x13138,0x1989e, // 410
387 0x17338,0x1b99e,0x1731c,0x1310e,0x1730e,0x110bc,0x131bc,0x1109e,0x173bc,0x1319e, // 420
388 0x1739e,0x17160,0x1b8b8,0x1dc5e,0x17130,0x1b89c,0x17118,0x1b88e,0x1710c,0x17106, // 430
389 0x130b8,0x1985e,0x171b8,0x1309c,0x1719c,0x1308e,0x1718e,0x1105e,0x130de,0x171de, // 440
390 0x170b0,0x1b85c,0x17098,0x1b84e,0x1708c,0x17086,0x1305c,0x170dc,0x1304e,0x170ce, // 450
391 0x17058,0x1b82e,0x1704c,0x17046,0x1302e,0x1706e,0x1702c,0x17026,0x10af0,0x1857c, // 460
392 0x10a78,0x1853e,0x10a3c,0x10a1e,0x10b7c,0x10b3e,0x1f0ba,0x1e17a,0x1c2fa,0x185fa, // 470
393 0x11ae0,0x18d78,0x1c6be,0x11a70,0x18d3c,0x11a38,0x18d1e,0x11a1c,0x11a0e,0x10978, // 480
394 0x184be,0x11b78,0x1093c,0x11b3c,0x1091e,0x11b1e,0x109be,0x11bbe,0x13ac0,0x19d70, // 490
395 0x1cebc,0x13a60,0x19d38,0x1ce9e,0x13a30,0x19d1c,0x13a18,0x19d0e,0x13a0c,0x13a06, // 500
396 0x11970,0x18cbc,0x13b70,0x11938,0x18c9e,0x13b38,0x1191c,0x13b1c,0x1190e,0x13b0e, // 510
397 0x108bc,0x119bc,0x1089e,0x13bbc,0x1199e,0x13b9e,0x1bd60,0x1deb8,0x1ef5e,0x17a40, // 520
398 0x1bd30,0x1de9c,0x17a20,0x1bd18,0x1de8e,0x17a10,0x1bd0c,0x17a08,0x1bd06,0x17a04, // 530
399 0x13960,0x19cb8,0x1ce5e,0x17b60,0x13930,0x19c9c,0x17b30,0x1bd9c,0x19c8e,0x17b18, // 540
400 0x1390c,0x17b0c,0x13906,0x17b06,0x118b8,0x18c5e,0x139b8,0x1189c,0x17bb8,0x1399c, // 550
401 0x1188e,0x17b9c,0x1398e,0x17b8e,0x1085e,0x118de,0x139de,0x17bde,0x17940,0x1bcb0, // 560
402 0x1de5c,0x17920,0x1bc98,0x1de4e,0x17910,0x1bc8c,0x17908,0x1bc86,0x17904,0x17902, // 570
403 0x138b0,0x19c5c,0x179b0,0x13898,0x19c4e,0x17998,0x1bcce,0x1798c,0x13886,0x17986, // 580
404 0x1185c,0x138dc,0x1184e,0x179dc,0x138ce,0x179ce,0x178a0,0x1bc58,0x1de2e,0x17890, // 590
405 0x1bc4c,0x17888,0x1bc46,0x17884,0x17882,0x13858,0x19c2e,0x178d8,0x1384c,0x178cc, // 600
406 0x13846,0x178c6,0x1182e,0x1386e,0x178ee,0x17850,0x1bc2c,0x17848,0x1bc26,0x17844, // 610
407 0x17842,0x1382c,0x1786c,0x13826,0x17866,0x17828,0x1bc16,0x17824,0x17822,0x13816, // 620
408 0x17836,0x10578,0x182be,0x1053c,0x1051e,0x105be,0x10d70,0x186bc,0x10d38,0x1869e, // 630
409 0x10d1c,0x10d0e,0x104bc,0x10dbc,0x1049e,0x10d9e,0x11d60,0x18eb8,0x1c75e,0x11d30, // 640
410 0x18e9c,0x11d18,0x18e8e,0x11d0c,0x11d06,0x10cb8,0x1865e,0x11db8,0x10c9c,0x11d9c, // 650
411 0x10c8e,0x11d8e,0x1045e,0x10cde,0x11dde,0x13d40,0x19eb0,0x1cf5c,0x13d20,0x19e98, // 660
412 0x1cf4e,0x13d10,0x19e8c,0x13d08,0x19e86,0x13d04,0x13d02,0x11cb0,0x18e5c,0x13db0, // 670
413 0x11c98,0x18e4e,0x13d98,0x19ece,0x13d8c,0x11c86,0x13d86,0x10c5c,0x11cdc,0x10c4e, // 680
414 0x13ddc,0x11cce,0x13dce,0x1bea0,0x1df58,0x1efae,0x1be90,0x1df4c,0x1be88,0x1df46, // 690
415 0x1be84,0x1be82,0x13ca0,0x19e58,0x1cf2e,0x17da0,0x13c90,0x19e4c,0x17d90,0x1becc, // 700
416 0x19e46,0x17d88,0x13c84,0x17d84,0x13c82,0x17d82,0x11c58,0x18e2e,0x13cd8,0x11c4c, // 710
417 0x17dd8,0x13ccc,0x11c46,0x17dcc,0x13cc6,0x17dc6,0x10c2e,0x11c6e,0x13cee,0x17dee, // 720
418 0x1be50,0x1df2c,0x1be48,0x1df26,0x1be44,0x1be42,0x13c50,0x19e2c,0x17cd0,0x13c48, // 730
419 0x19e26,0x17cc8,0x1be66,0x17cc4,0x13c42,0x17cc2,0x11c2c,0x13c6c,0x11c26,0x17cec, // 740
420 0x13c66,0x17ce6,0x1be28,0x1df16,0x1be24,0x1be22,0x13c28,0x19e16,0x17c68,0x13c24, // 750
421 0x17c64,0x13c22,0x17c62,0x11c16,0x13c36,0x17c76,0x1be14,0x1be12,0x13c14,0x17c34, // 760
422 0x13c12,0x17c32,0x102bc,0x1029e,0x106b8,0x1835e,0x1069c,0x1068e,0x1025e,0x106de, // 770
423 0x10eb0,0x1875c,0x10e98,0x1874e,0x10e8c,0x10e86,0x1065c,0x10edc,0x1064e,0x10ece, // 780
424 0x11ea0,0x18f58,0x1c7ae,0x11e90,0x18f4c,0x11e88,0x18f46,0x11e84,0x11e82,0x10e58, // 790
425 0x1872e,0x11ed8,0x18f6e,0x11ecc,0x10e46,0x11ec6,0x1062e,0x10e6e,0x11eee,0x19f50, // 800
426 0x1cfac,0x19f48,0x1cfa6,0x19f44,0x19f42,0x11e50,0x18f2c,0x13ed0,0x19f6c,0x18f26, // 810
427 0x13ec8,0x11e44,0x13ec4,0x11e42,0x13ec2,0x10e2c,0x11e6c,0x10e26,0x13eec,0x11e66, // 820
428 0x13ee6,0x1dfa8,0x1efd6,0x1dfa4,0x1dfa2,0x19f28,0x1cf96,0x1bf68,0x19f24,0x1bf64, // 830
429 0x19f22,0x1bf62,0x11e28,0x18f16,0x13e68,0x11e24,0x17ee8,0x13e64,0x11e22,0x17ee4, // 840
430 0x13e62,0x17ee2,0x10e16,0x11e36,0x13e76,0x17ef6,0x1df94,0x1df92,0x19f14,0x1bf34, // 850
431 0x19f12,0x1bf32,0x11e14,0x13e34,0x11e12,0x17e74,0x13e32,0x17e72,0x1df8a,0x19f0a, // 860
432 0x1bf1a,0x11e0a,0x13e1a,0x17e3a,0x1035c,0x1034e,0x10758,0x183ae,0x1074c,0x10746, // 870
433 0x1032e,0x1076e,0x10f50,0x187ac,0x10f48,0x187a6,0x10f44,0x10f42,0x1072c,0x10f6c, // 880
434 0x10726,0x10f66,0x18fa8,0x1c7d6,0x18fa4,0x18fa2,0x10f28,0x18796,0x11f68,0x18fb6, // 890
435 0x11f64,0x10f22,0x11f62,0x10716,0x10f36,0x11f76,0x1cfd4,0x1cfd2,0x18f94,0x19fb4, // 900
436 0x18f92,0x19fb2,0x10f14,0x11f34,0x10f12,0x13f74,0x11f32,0x13f72,0x1cfca,0x18f8a, // 910
437 0x19f9a,0x10f0a,0x11f1a,0x13f3a,0x103ac,0x103a6,0x107a8,0x183d6,0x107a4,0x107a2, // 920
438 0x10396,0x107b6,0x187d4,0x187d2,0x10794,0x10fb4,0x10792,0x10fb2,0x1c7ea) // 929
439 ); // end of $clusters array
440
441 /**
442 * Array of factors of the Reed-Solomon polynomial equations used for error correction; one sub array for each correction level (0-8).
443 * @protected
444 */
445 protected $rsfactors = array(
446 array( // ECL 0 (2 factors) -------------------------------------------------------------------------------
447 0x01b,0x395), // 2
448 array( // ECL 1 (4 factors) -------------------------------------------------------------------------------
449 0x20a,0x238,0x2d3,0x329), // 4
450 array( // ECL 2 (8 factors) -------------------------------------------------------------------------------
451 0x0ed,0x134,0x1b4,0x11c,0x286,0x28d,0x1ac,0x17b), // 8
452 array( // ECL 3 (16 factors) ------------------------------------------------------------------------------
453 0x112,0x232,0x0e8,0x2f3,0x257,0x20c,0x321,0x084,0x127,0x074,0x1ba,0x1ac,0x127,0x02a,0x0b0,0x041),// 16
454 array( // ECL 4 (32 factors) ------------------------------------------------------------------------------
455 0x169,0x23f,0x39a,0x20d,0x0b0,0x24a,0x280,0x141,0x218,0x2e6,0x2a5,0x2e6,0x2af,0x11c,0x0c1,0x205, // 16
456 0x111,0x1ee,0x107,0x093,0x251,0x320,0x23b,0x140,0x323,0x085,0x0e7,0x186,0x2ad,0x14a,0x03f,0x19a),// 32
457 array( // ECL 5 (64 factors) ------------------------------------------------------------------------------
458 0x21b,0x1a6,0x006,0x05d,0x35e,0x303,0x1c5,0x06a,0x262,0x11f,0x06b,0x1f9,0x2dd,0x36d,0x17d,0x264, // 16
459 0x2d3,0x1dc,0x1ce,0x0ac,0x1ae,0x261,0x35a,0x336,0x21f,0x178,0x1ff,0x190,0x2a0,0x2fa,0x11b,0x0b8, // 32
460 0x1b8,0x023,0x207,0x01f,0x1cc,0x252,0x0e1,0x217,0x205,0x160,0x25d,0x09e,0x28b,0x0c9,0x1e8,0x1f6, // 48
461 0x288,0x2dd,0x2cd,0x053,0x194,0x061,0x118,0x303,0x348,0x275,0x004,0x17d,0x34b,0x26f,0x108,0x21f),// 64
462 array( // ECL 6 (128 factors) -----------------------------------------------------------------------------
463 0x209,0x136,0x360,0x223,0x35a,0x244,0x128,0x17b,0x035,0x30b,0x381,0x1bc,0x190,0x39d,0x2ed,0x19f, // 16
464 0x336,0x05d,0x0d9,0x0d0,0x3a0,0x0f4,0x247,0x26c,0x0f6,0x094,0x1bf,0x277,0x124,0x38c,0x1ea,0x2c0, // 32
465 0x204,0x102,0x1c9,0x38b,0x252,0x2d3,0x2a2,0x124,0x110,0x060,0x2ac,0x1b0,0x2ae,0x25e,0x35c,0x239, // 48
466 0x0c1,0x0db,0x081,0x0ba,0x0ec,0x11f,0x0c0,0x307,0x116,0x0ad,0x028,0x17b,0x2c8,0x1cf,0x286,0x308, // 64
467 0x0ab,0x1eb,0x129,0x2fb,0x09c,0x2dc,0x05f,0x10e,0x1bf,0x05a,0x1fb,0x030,0x0e4,0x335,0x328,0x382, // 80
468 0x310,0x297,0x273,0x17a,0x17e,0x106,0x17c,0x25a,0x2f2,0x150,0x059,0x266,0x057,0x1b0,0x29e,0x268, // 96
469 0x09d,0x176,0x0f2,0x2d6,0x258,0x10d,0x177,0x382,0x34d,0x1c6,0x162,0x082,0x32e,0x24b,0x324,0x022, // 112
470 0x0d3,0x14a,0x21b,0x129,0x33b,0x361,0x025,0x205,0x342,0x13b,0x226,0x056,0x321,0x004,0x06c,0x21b),// 128
471 array( // ECL 7 (256 factors) -----------------------------------------------------------------------------
472 0x20c,0x37e,0x04b,0x2fe,0x372,0x359,0x04a,0x0cc,0x052,0x24a,0x2c4,0x0fa,0x389,0x312,0x08a,0x2d0, // 16
473 0x35a,0x0c2,0x137,0x391,0x113,0x0be,0x177,0x352,0x1b6,0x2dd,0x0c2,0x118,0x0c9,0x118,0x33c,0x2f5, // 32
474 0x2c6,0x32e,0x397,0x059,0x044,0x239,0x00b,0x0cc,0x31c,0x25d,0x21c,0x391,0x321,0x2bc,0x31f,0x089, // 48
475 0x1b7,0x1a2,0x250,0x29c,0x161,0x35b,0x172,0x2b6,0x145,0x0f0,0x0d8,0x101,0x11c,0x225,0x0d1,0x374, // 64
476 0x13b,0x046,0x149,0x319,0x1ea,0x112,0x36d,0x0a2,0x2ed,0x32c,0x2ac,0x1cd,0x14e,0x178,0x351,0x209, // 80
477 0x133,0x123,0x323,0x2c8,0x013,0x166,0x18f,0x38c,0x067,0x1ff,0x033,0x008,0x205,0x0e1,0x121,0x1d6, // 96
478 0x27d,0x2db,0x042,0x0ff,0x395,0x10d,0x1cf,0x33e,0x2da,0x1b1,0x350,0x249,0x088,0x21a,0x38a,0x05a, // 112
479 0x002,0x122,0x2e7,0x0c7,0x28f,0x387,0x149,0x031,0x322,0x244,0x163,0x24c,0x0bc,0x1ce,0x00a,0x086, // 128
480 0x274,0x140,0x1df,0x082,0x2e3,0x047,0x107,0x13e,0x176,0x259,0x0c0,0x25d,0x08e,0x2a1,0x2af,0x0ea, // 144
481 0x2d2,0x180,0x0b1,0x2f0,0x25f,0x280,0x1c7,0x0c1,0x2b1,0x2c3,0x325,0x281,0x030,0x03c,0x2dc,0x26d, // 160
482 0x37f,0x220,0x105,0x354,0x28f,0x135,0x2b9,0x2f3,0x2f4,0x03c,0x0e7,0x305,0x1b2,0x1a5,0x2d6,0x210, // 176
483 0x1f7,0x076,0x031,0x31b,0x020,0x090,0x1f4,0x0ee,0x344,0x18a,0x118,0x236,0x13f,0x009,0x287,0x226, // 192
484 0x049,0x392,0x156,0x07e,0x020,0x2a9,0x14b,0x318,0x26c,0x03c,0x261,0x1b9,0x0b4,0x317,0x37d,0x2f2, // 208
485 0x25d,0x17f,0x0e4,0x2ed,0x2f8,0x0d5,0x036,0x129,0x086,0x036,0x342,0x12b,0x39a,0x0bf,0x38e,0x214, // 224
486 0x261,0x33d,0x0bd,0x014,0x0a7,0x01d,0x368,0x1c1,0x053,0x192,0x029,0x290,0x1f9,0x243,0x1e1,0x0ad, // 240
487 0x194,0x0fb,0x2b0,0x05f,0x1f1,0x22b,0x282,0x21f,0x133,0x09f,0x39c,0x22e,0x288,0x037,0x1f1,0x00a),// 256
488 array( // ECL 8 (512 factors) -----------------------------------------------------------------------------
489 0x160,0x04d,0x175,0x1f8,0x023,0x257,0x1ac,0x0cf,0x199,0x23e,0x076,0x1f2,0x11d,0x17c,0x15e,0x1ec, // 16
490 0x0c5,0x109,0x398,0x09b,0x392,0x12b,0x0e5,0x283,0x126,0x367,0x132,0x058,0x057,0x0c1,0x160,0x30d, // 32
491 0x34e,0x04b,0x147,0x208,0x1b3,0x21f,0x0cb,0x29a,0x0f9,0x15a,0x30d,0x26d,0x280,0x10c,0x31a,0x216, // 48
492 0x21b,0x30d,0x198,0x186,0x284,0x066,0x1dc,0x1f3,0x122,0x278,0x221,0x025,0x35a,0x394,0x228,0x029, // 64
493 0x21e,0x121,0x07a,0x110,0x17f,0x320,0x1e5,0x062,0x2f0,0x1d8,0x2f9,0x06b,0x310,0x35c,0x292,0x2e5, // 80
494 0x122,0x0cc,0x2a9,0x197,0x357,0x055,0x063,0x03e,0x1e2,0x0b4,0x014,0x129,0x1c3,0x251,0x391,0x08e, // 96
495 0x328,0x2ac,0x11f,0x218,0x231,0x04c,0x28d,0x383,0x2d9,0x237,0x2e8,0x186,0x201,0x0c0,0x204,0x102, // 112
496 0x0f0,0x206,0x31a,0x18b,0x300,0x350,0x033,0x262,0x180,0x0a8,0x0be,0x33a,0x148,0x254,0x312,0x12f, // 128
497 0x23a,0x17d,0x19f,0x281,0x09c,0x0ed,0x097,0x1ad,0x213,0x0cf,0x2a4,0x2c6,0x059,0x0a8,0x130,0x192, // 144
498 0x028,0x2c4,0x23f,0x0a2,0x360,0x0e5,0x041,0x35d,0x349,0x200,0x0a4,0x1dd,0x0dd,0x05c,0x166,0x311, // 160
499 0x120,0x165,0x352,0x344,0x33b,0x2e0,0x2c3,0x05e,0x008,0x1ee,0x072,0x209,0x002,0x1f3,0x353,0x21f, // 176
500 0x098,0x2d9,0x303,0x05f,0x0f8,0x169,0x242,0x143,0x358,0x31d,0x121,0x033,0x2ac,0x1d2,0x215,0x334, // 192
501 0x29d,0x02d,0x386,0x1c4,0x0a7,0x156,0x0f4,0x0ad,0x023,0x1cf,0x28b,0x033,0x2bb,0x24f,0x1c4,0x242, // 208
502 0x025,0x07c,0x12a,0x14c,0x228,0x02b,0x1ab,0x077,0x296,0x309,0x1db,0x352,0x2fc,0x16c,0x242,0x38f, // 224
503 0x11b,0x2c7,0x1d8,0x1a4,0x0f5,0x120,0x252,0x18a,0x1ff,0x147,0x24d,0x309,0x2bb,0x2b0,0x02b,0x198, // 240
504 0x34a,0x17f,0x2d1,0x209,0x230,0x284,0x2ca,0x22f,0x03e,0x091,0x369,0x297,0x2c9,0x09f,0x2a0,0x2d9, // 256
505 0x270,0x03b,0x0c1,0x1a1,0x09e,0x0d1,0x233,0x234,0x157,0x2b5,0x06d,0x260,0x233,0x16d,0x0b5,0x304, // 272
506 0x2a5,0x136,0x0f8,0x161,0x2c4,0x19a,0x243,0x366,0x269,0x349,0x278,0x35c,0x121,0x218,0x023,0x309, // 288
507 0x26a,0x24a,0x1a8,0x341,0x04d,0x255,0x15a,0x10d,0x2f5,0x278,0x2b7,0x2ef,0x14b,0x0f7,0x0b8,0x02d, // 304
508 0x313,0x2a8,0x012,0x042,0x197,0x171,0x036,0x1ec,0x0e4,0x265,0x33e,0x39a,0x1b5,0x207,0x284,0x389, // 320
509 0x315,0x1a4,0x131,0x1b9,0x0cf,0x12c,0x37c,0x33b,0x08d,0x219,0x17d,0x296,0x201,0x038,0x0fc,0x155, // 336
510 0x0f2,0x31d,0x346,0x345,0x2d0,0x0e0,0x133,0x277,0x03d,0x057,0x230,0x136,0x2f4,0x299,0x18d,0x328, // 352
511 0x353,0x135,0x1d9,0x31b,0x17a,0x01f,0x287,0x393,0x1cb,0x326,0x24e,0x2db,0x1a9,0x0d8,0x224,0x0f9, // 368
512 0x141,0x371,0x2bb,0x217,0x2a1,0x30e,0x0d2,0x32f,0x389,0x12f,0x34b,0x39a,0x119,0x049,0x1d5,0x317, // 384
513 0x294,0x0a2,0x1f2,0x134,0x09b,0x1a6,0x38b,0x331,0x0bb,0x03e,0x010,0x1a9,0x217,0x150,0x11e,0x1b5, // 400
514 0x177,0x111,0x262,0x128,0x0b7,0x39b,0x074,0x29b,0x2ef,0x161,0x03e,0x16e,0x2b3,0x17b,0x2af,0x34a, // 416
515 0x025,0x165,0x2d0,0x2e6,0x14a,0x005,0x027,0x39b,0x137,0x1a8,0x0f2,0x2ed,0x141,0x036,0x29d,0x13c, // 432
516 0x156,0x12b,0x216,0x069,0x29b,0x1e8,0x280,0x2a0,0x240,0x21c,0x13c,0x1e6,0x2d1,0x262,0x02e,0x290, // 448
517 0x1bf,0x0ab,0x268,0x1d0,0x0be,0x213,0x129,0x141,0x2fa,0x2f0,0x215,0x0af,0x086,0x00e,0x17d,0x1b1, // 464
518 0x2cd,0x02d,0x06f,0x014,0x254,0x11c,0x2e0,0x08a,0x286,0x19b,0x36d,0x29d,0x08d,0x397,0x02d,0x30c, // 480
519 0x197,0x0a4,0x14c,0x383,0x0a5,0x2d6,0x258,0x145,0x1f2,0x28f,0x165,0x2f0,0x300,0x0df,0x351,0x287, // 496
520 0x03f,0x136,0x35f,0x0fb,0x16e,0x130,0x11a,0x2e2,0x2a3,0x19a,0x185,0x0f4,0x01f,0x079,0x12f,0x107) // 512
521 );
522
523 /**
524 * This is the class constructor.
525 * Creates a PDF417 object
526 * @param $code (string) code to represent using PDF417
527 * @param $ecl (int) error correction level (0-8); default -1 = automatic correction level
528 * @param $aspectratio (float) the width to height of the symbol (excluding quiet zones)
529 * @param $macro (array) information for macro block
530 * @public
531 */
532 public function __construct($code, $ecl=-1, $aspectratio=2, $macro=array()) {
533 $barcode_array = array();
534 if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
535 return false;
536 }
537 // get the input sequence array
538 $sequence = $this->getInputSequences($code);
539 $codewords = array(); // array of code-words
540 foreach($sequence as $seq) {
541 $cw = $this->getCompaction($seq[0], $seq[1], true);
542 $codewords = array_merge($codewords, $cw);
543 }
544 if ($codewords[0] == 900) {
545 // Text Alpha is the default mode, so remove the first code
546 array_shift($codewords);
547 }
548 // count number of codewords
549 $numcw = count($codewords);
550 if ($numcw > 925) {
551 // reached maximum data codeword capacity
552 return false;
553 }
554 // build macro control block codewords
555 if (!empty($macro)) {
556 $macrocw = array();
557 // beginning of macro control block
558 $macrocw[] = 928;
559 // segment index
560 $cw = $this->getCompaction(902, sprintf('%05d', $macro['segment_index']), false);
561 $macrocw = array_merge($macrocw, $cw);
562 // file ID
563 $cw = $this->getCompaction(900, $macro['file_id'], false);
564 $macrocw = array_merge($macrocw, $cw);
565 // optional fields
566 $optmodes = array(900,902,902,900,900,902,902);
567 $optsize = array(-1,2,4,-1,-1,-1,2);
568 foreach ($optmodes as $k => $omode) {
569 if (isset($macro['option_'.$k])) {
570 $macrocw[] = 923;
571 $macrocw[] = $k;
572 if ($optsize[$k] == 2) {
573 $macro['option_'.$k] = sprintf('%05d', $macro['option_'.$k]);
574 } elseif ($optsize[$k] == 4) {
575 $macro['option_'.$k] = sprintf('%010d', $macro['option_'.$k]);
576 }
577 $cw = $this->getCompaction($omode, $macro['option_'.$k], false);
578 $macrocw = array_merge($macrocw, $cw);
579 }
580 }
581 if ($macro['segment_index'] == ($macro['segment_total'] - 1)) {
582 // end of control block
583 $macrocw[] = 922;
584 }
585 // update total codewords
586 $numcw += count($macrocw);
587 }
588 // set error correction level
589 $ecl = $this->getErrorCorrectionLevel($ecl, $numcw);
590 // number of codewords for error correction
591 $errsize = (2 << $ecl);
592 // calculate number of columns (number of codewords per row) and rows
593 $nce = ($numcw + $errsize + 1);
594 $cols = round((sqrt(4761 + (68 * $aspectratio * ROWHEIGHT * $nce)) - 69) / 34);
595 // adjust cols
596 if ($cols < 1) {
597 $cols = 1;
598 } elseif ($cols > 30) {
599 $cols = 30;
600 }
601 $rows = ceil($nce / $cols);
602 $size = ($cols * $rows);
603 // adjust rows
604 if (($rows < 3) OR ($rows > 90)) {
605 if ($rows < 3) {
606 $rows = 3;
607 } elseif ($rows > 90) {
608 $rows = 90;
609 }
610 $cols = ceil($size / $rows);
611 $size = ($cols * $rows);
612 }
613 if ($size > 928) {
614 // set dimensions to get maximum capacity
615 if (abs($aspectratio - (17 * 29 / 32)) < abs($aspectratio - (17 * 16 / 58))) {
616 $cols = 29;
617 $rows = 32;
618 } else {
619 $cols = 16;
620 $rows = 58;
621 }
622 $size = 928;
623 }
624 // calculate padding
625 $pad = ($size - $nce);
626 if ($pad > 0) {
627 if (($size - $rows) == $nce) {
628 --$rows;
629 $size -= $rows;
630 } else {
631 // add pading
632 $codewords = array_merge($codewords, array_fill(0, $pad, 900));
633 }
634 }
635 if (!empty($macro)) {
636 // add macro section
637 $codewords = array_merge($codewords, $macrocw);
638 }
639 // Symbol Lenght Descriptor (number of data codewords including Symbol Lenght Descriptor and pad codewords)
640 $sld = $size - $errsize;
641 // add symbol length description
642 array_unshift($codewords, $sld);
643 // calculate error correction
644 $ecw = $this->getErrorCorrection($codewords, $ecl);
645 // add error correction codewords
646 $codewords = array_merge($codewords, $ecw);
647 // add horizontal quiet zones to start and stop patterns
648 $pstart = str_repeat('0', QUIETH).$this->start_pattern;
649 $pstop = $this->stop_pattern.str_repeat('0', QUIETH);
650 $barcode_array['num_rows'] = ($rows * ROWHEIGHT) + (2 * QUIETV);
651 $barcode_array['num_cols'] = (($cols + 2) * 17) + 35 + (2 * QUIETH);
652 $barcode_array['bcode'] = array();
653 // build rows for vertical quiet zone
654 if (QUIETV > 0) {
655 $empty_row = array_fill(0, $barcode_array['num_cols'], 0);
656 for ($i = 0; $i < QUIETV; ++$i) {
657 // add vertical quiet rows
658 $barcode_array['bcode'][] = $empty_row;
659 }
660 }
661 $k = 0; // codeword index
662 $cid = 0; // initial cluster
663 // for each row
664 for ($r = 0; $r < $rows; ++$r) {
665 // row start code
666 $row = $pstart;
667 switch ($cid) {
668 case 0: {
669 $L = ((30 * intval($r / 3)) + intval(($rows - 1) / 3));
670 break;
671 }
672 case 1: {
673 $L = ((30 * intval($r / 3)) + ($ecl * 3) + (($rows - 1) % 3));
674 break;
675 }
676 case 2: {
677 $L = ((30 * intval($r / 3)) + ($cols - 1));
678 break;
679 }
680 }
681 // left row indicator
682 $row .= sprintf('%17b', $this->clusters[$cid][$L]);
683 // for each column
684 for ($c = 0; $c < $cols; ++$c) {
685 $row .= sprintf('%17b', $this->clusters[$cid][$codewords[$k]]);
686 ++$k;
687 }
688 switch ($cid) {
689 case 0: {
690 $L = ((30 * intval($r / 3)) + ($cols - 1));
691 break;
692 }
693 case 1: {
694 $L = ((30 * intval($r / 3)) + intval(($rows - 1) / 3));
695 break;
696 }
697 case 2: {
698 $L = ((30 * intval($r / 3)) + ($ecl * 3) + (($rows - 1) % 3));
699 break;
700 }
701 }
702 // right row indicator
703 $row .= sprintf('%17b', $this->clusters[$cid][$L]);
704 // row stop code
705 $row .= $pstop;
706 // convert the string to array
707 $arow = preg_split('//', $row, -1, PREG_SPLIT_NO_EMPTY);
708 // duplicate row to get the desired height
709 for ($h = 0; $h < ROWHEIGHT; ++$h) {
710 $barcode_array['bcode'][] = $arow;
711 }
712 ++$cid;
713 if ($cid > 2) {
714 $cid = 0;
715 }
716 }
717 if (QUIETV > 0) {
718 for ($i = 0; $i < QUIETV; ++$i) {
719 // add vertical quiet rows
720 $barcode_array['bcode'][] = $empty_row;
721 }
722 }
723 $this->barcode_array = $barcode_array;
724 }
725
726 /**
727 * Returns a barcode array which is readable by TCPDF
728 * @return array barcode array readable by TCPDF;
729 * @public
730 */
731 public function getBarcodeArray() {
732 return $this->barcode_array;
733 }
734
735 /**
736 * Returns the error correction level (0-8) to be used
737 * @param $ecl (int) error correction level
738 * @param $numcw (int) number of data codewords
739 * @return int error correction level
740 * @protected
741 */
742 protected function getErrorCorrectionLevel($ecl, $numcw) {
743 // get maximum correction level
744 $maxecl = 8; // starting error level
745 $maxerrsize = (928 - $numcw); // available codewords for error
746 while ($maxecl > 0) {
747 $errsize = (2 << $ecl);
748 if ($maxerrsize >= $errsize) {
749 break;
750 }
751 --$maxecl;
752 }
753 // check for automatic levels
754 if (($ecl < 0) OR ($ecl > 8)) {
755 if ($numcw < 41) {
756 $ecl = 2;
757 } elseif ($numcw < 161) {
758 $ecl = 3;
759 } elseif ($numcw < 321) {
760 $ecl = 4;
761 } elseif ($numcw < 864) {
762 $ecl = 5;
763 } else {
764 $ecl = $maxecl;
765 }
766 }
767 if ($ecl > $maxecl) {
768 $ecl = $maxecl;
769 }
770 return $ecl;
771 }
772
773 /**
774 * Returns the error correction codewords
775 * @param $cw (array) array of codewords including Symbol Lenght Descriptor and pad
776 * @param $ecl (int) error correction level 0-8
777 * @return array of error correction codewords
778 * @protected
779 */
780 protected function getErrorCorrection($cw, $ecl) {
781 // get error correction coefficients
782 $ecc = $this->rsfactors[$ecl];
783 // number of error correction factors
784 $eclsize = (2 << $ecl);
785 // maximum index for $rsfactors[$ecl]
786 $eclmaxid = ($eclsize - 1);
787 // initialize array of error correction codewords
788 $ecw = array_fill(0, $eclsize, 0);
789 // for each data codeword
790 foreach($cw as $k => $d) {
791 $t1 = ($d + $ecw[$eclmaxid]) % 929;
792 for ($j = $eclmaxid; $j > 0; --$j) {
793 $t2 = ($t1 * $ecc[$j]) % 929;
794 $t3 = 929 - $t2;
795 $ecw[$j] = ($ecw[($j - 1)] + $t3) % 929;
796 }
797 $t2 = ($t1 * $ecc[0]) % 929;
798 $t3 = 929 - $t2;
799 $ecw[0] = $t3 % 929;
800 }
801 foreach($ecw as $j => $e) {
802 if ($e != 0) {
803 $ecw[$j] = 929 - $e;
804 }
805 }
806 $ecw = array_reverse($ecw);
807 return $ecw;
808 }
809
810 /**
811 * Create array of sequences from input
812 * @param $code (string) code
813 * @return bidimensional array containing characters and classification
814 * @protected
815 */
816 protected function getInputSequences($code) {
817 $sequence_array = array(); // array to be returned
818 $numseq = array();
819 // get numeric sequences
820 preg_match_all('/([0-9]{13,44})/', $code, $numseq, PREG_OFFSET_CAPTURE);
821 $numseq[1][] = array('', strlen($code));
822 $offset = 0;
823 foreach($numseq[1] as $seq) {
824 $seqlen = strlen($seq[0]);
825 if ($seq[1] > 0) {
826 // extract text sequence before the number sequence
827 $prevseq = substr($code, $offset, ($seq[1] - $offset));
828 $textseq = array();
829 // get text sequences
830 preg_match_all('/([\x09\x0a\x0d\x20-\x7e]{5,})/', $prevseq, $textseq, PREG_OFFSET_CAPTURE);
831 $textseq[1][] = array('', strlen($prevseq));
832 $txtoffset = 0;
833 foreach($textseq[1] as $txtseq) {
834 $txtseqlen = strlen($txtseq[0]);
835 if ($txtseq[1] > 0) {
836 // extract byte sequence before the text sequence
837 $prevtxtseq = substr($prevseq, $txtoffset, ($txtseq[1] - $txtoffset));
838 if (strlen($prevtxtseq) > 0) {
839 // add BYTE sequence
840 if ((strlen($prevtxtseq) == 1) AND ((count($sequence_array) > 0) AND ($sequence_array[(count($sequence_array) - 1)][0] == 900))) {
841 $sequence_array[] = array(913, $prevtxtseq);
842 } elseif ((strlen($prevtxtseq) % 6) == 0) {
843 $sequence_array[] = array(924, $prevtxtseq);
844 } else {
845 $sequence_array[] = array(901, $prevtxtseq);
846 }
847 }
848 }
849 if ($txtseqlen > 0) {
850 // add numeric sequence
851 $sequence_array[] = array(900, $txtseq[0]);
852 }
853 $txtoffset = $txtseq[1] + $txtseqlen;
854 }
855 }
856 if ($seqlen > 0) {
857 // add numeric sequence
858 $sequence_array[] = array(902, $seq[0]);
859 }
860 $offset = $seq[1] + $seqlen;
861 }
862 return $sequence_array;
863 }
864
865 /**
866 * Compact data by mode.
867 * @param $mode (int) compaction mode number
868 * @param $code (string) data to compact
869 * @param $addmode (boolean) if true add the mode codeword at first position
870 * @return array of codewords
871 * @protected
872 */
873 protected function getCompaction($mode, $code, $addmode=true) {
874 $cw = array(); // array of codewords to return
875 switch($mode) {
876 case 900: { // Text Compaction mode latch
877 $submode = 0; // default Alpha sub-mode
878 $txtarr = array(); // array of characters and sub-mode switching characters
879 $codelen = strlen($code);
880 for ($i = 0; $i < $codelen; ++$i) {
881 $chval = ord($code{$i});
882 if (($k = array_search($chval, $this->textsubmodes[$submode])) !== false) {
883 // we are on the same sub-mode
884 $txtarr[] = $k;
885 } else {
886 // the sub-mode is changed
887 for ($s = 0; $s < 4; ++$s) {
888 // search new sub-mode
889 if (($s != $submode) AND (($k = array_search($chval, $this->textsubmodes[$s])) !== false)) {
890 // $s is the new submode
891 if (((($i + 1) == $codelen) OR ((($i + 1) < $codelen) AND (array_search(ord($code{($i + 1)}), $this->textsubmodes[$submode]) !== false))) AND (($s == 3) OR (($s == 0) AND ($submode == 1)))) {
892 // shift (temporary change only for this char)
893 if ($s == 3) {
894 // shift to puntuaction
895 $txtarr[] = 29;
896 } else {
897 // shift from lower to alpha
898 $txtarr[] = 27;
899 }
900 } else {
901 // latch
902 $txtarr = array_merge($txtarr, $this->textlatch[''.$submode.$s]);
903 // set new submode
904 $submode = $s;
905 }
906 // add characted code to array
907 $txtarr[] = $k;
908 break;
909 }
910 }
911 }
912 }
913 $txtarrlen = count($txtarr);
914 if (($txtarrlen % 2) != 0) {
915 // add padding
916 $txtarr[] = 29;
917 ++$txtarrlen;
918 }
919 // calculate codewords
920 for ($i = 0; $i < $txtarrlen; $i += 2) {
921 $cw[] = (30 * $txtarr[$i]) + $txtarr[($i + 1)];
922 }
923 break;
924 }
925 case 901:
926 case 924: { // Byte Compaction mode latch
927 while (($codelen = strlen($code)) > 0) {
928 if ($codelen > 6) {
929 $rest = substr($code, 6);
930 $code = substr($code, 0, 6);
931 $sublen = 6;
932 } else {
933 $rest = '';
934 $sublen = strlen($code);
935 }
936 if ($sublen == 6) {
937 $t = bcmul(''.ord($code[0]), '1099511627776');
938 $t = bcadd($t, bcmul(''.ord($code[1]), '4294967296'));
939 $t = bcadd($t, bcmul(''.ord($code[2]), '16777216'));
940 $t = bcadd($t, bcmul(''.ord($code[3]), '65536'));
941 $t = bcadd($t, bcmul(''.ord($code[4]), '256'));
942 $t = bcadd($t, ''.ord($code[5]));
943 // tmp array for the 6 bytes block
944 $cw6 = array();
945 do {
946 $d = bcmod($t, '900');
947 $t = bcdiv($t, '900');
948 // prepend the value to the beginning of the array
949 array_unshift($cw6, $d);
950 } while ($t != '0');
951 // append the result array at the end
952 $cw = array_merge($cw, $cw6);
953 } else {
954 for ($i = 0; $i < $sublen; ++$i) {
955 $cw[] = ord($code{$i});
956 }
957 }
958 $code = $rest;
959 }
960 break;
961 }
962 case 902: { // Numeric Compaction mode latch
963 while (($codelen = strlen($code)) > 0) {
964 if ($codelen > 44) {
965 $rest = substr($code, 44);
966 $code = substr($code, 0, 44);
967 } else {
968 $rest = '';
969 }
970 $t = '1'.$code;
971 do {
972 $d = bcmod($t, '900');
973 $t = bcdiv($t, '900');
974 array_unshift($cw, $d);
975 } while ($t != '0');
976 $code = $rest;
977 }
978 break;
979 }
980 case 913: { // Byte Compaction mode shift
981 $cw[] = ord($code);
982 break;
983 }
984 }
985 if ($addmode) {
986 // add the compaction mode codeword at the beginning
987 array_unshift($cw, $mode);
988 }
989 return $cw;
990 }
991
992} // end PDF417 class
993
994//============================================================+
995// END OF FILE
996//============================================================+
diff --git a/inc/3rdparty/libraries/tcpdf/include/barcodes/qrcode.php b/inc/3rdparty/libraries/tcpdf/include/barcodes/qrcode.php
new file mode 100644
index 00000000..3127fe68
--- /dev/null
+++ b/inc/3rdparty/libraries/tcpdf/include/barcodes/qrcode.php
@@ -0,0 +1,2866 @@
1<?php
2//============================================================+
3// File name : qrcode.php
4// Version : 1.0.010
5// Begin : 2010-03-22
6// Last Update : 2012-07-25
7// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
8// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9// -------------------------------------------------------------------
10// Copyright (C) 2010-2012 Nicola Asuni - Tecnick.com LTD
11//
12// This file is part of TCPDF software library.
13//
14// TCPDF is free software: you can redistribute it and/or modify it
15// under the terms of the GNU Lesser General Public License as
16// published by the Free Software Foundation, either version 3 of the
17// License, or (at your option) any later version.
18//
19// TCPDF is distributed in the hope that it will be useful, but
20// WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22// See the GNU Lesser General Public License for more details.
23//
24// You should have received a copy of the GNU Lesser General Public License
25// along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
26//
27// See LICENSE.TXT file for more information.
28// -------------------------------------------------------------------
29//
30// DESCRIPTION :
31//
32// Class to create QR-code arrays for TCPDF class.
33// QR Code symbol is a 2D barcode that can be scanned by
34// handy terminals such as a mobile phone with CCD.
35// The capacity of QR Code is up to 7000 digits or 4000
36// characters, and has high robustness.
37// This class supports QR Code model 2, described in
38// JIS (Japanese Industrial Standards) X0510:2004
39// or ISO/IEC 18004.
40// Currently the following features are not supported:
41// ECI and FNC1 mode, Micro QR Code, QR Code model 1,
42// Structured mode.
43//
44// This class is derived from the following projects:
45// ---------------------------------------------------------
46// "PHP QR Code encoder"
47// License: GNU-LGPLv3
48// Copyright (C) 2010 by Dominik Dzienia <deltalab at poczta dot fm>
49// http://phpqrcode.sourceforge.net/
50// https://sourceforge.net/projects/phpqrcode/
51//
52// The "PHP QR Code encoder" is based on
53// "C libqrencode library" (ver. 3.1.1)
54// License: GNU-LGPL 2.1
55// Copyright (C) 2006-2010 by Kentaro Fukuchi
56// http://megaui.net/fukuchi/works/qrencode/index.en.html
57//
58// Reed-Solomon code encoder is written by Phil Karn, KA9Q.
59// Copyright (C) 2002-2006 Phil Karn, KA9Q
60//
61// QR Code is registered trademark of DENSO WAVE INCORPORATED
62// http://www.denso-wave.com/qrcode/index-e.html
63// ---------------------------------------------------------
64//============================================================+
65
66/**
67 * @file
68 * Class to create QR-code arrays for TCPDF class.
69 * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
70 * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
71 * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
72 * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
73 *
74 * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
75 * Please read comments on this class source file for full copyright and license information.
76 *
77 * @package com.tecnick.tcpdf
78 * @author Nicola Asuni
79 * @version 1.0.010
80 */
81
82// definitions
83if (!defined('QRCODEDEFS')) {
84
85 /**
86 * Indicate that definitions for this class are set
87 */
88 define('QRCODEDEFS', true);
89
90 // -----------------------------------------------------
91
92 // Encoding modes (characters which can be encoded in QRcode)
93
94 /**
95 * Encoding mode
96 */
97 define('QR_MODE_NL', -1);
98
99 /**
100 * Encoding mode numeric (0-9). 3 characters are encoded to 10bit length. In theory, 7089 characters or less can be stored in a QRcode.
101 */
102 define('QR_MODE_NM', 0);
103
104 /**
105 * Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length. In theory, 4296 characters or less can be stored in a QRcode.
106 */
107 define('QR_MODE_AN', 1);
108
109 /**
110 * Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
111 */
112 define('QR_MODE_8B', 2);
113
114 /**
115 * Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length. In theory, 1817 characters or less can be stored in a QRcode.
116 */
117 define('QR_MODE_KJ', 3);
118
119 /**
120 * Encoding mode STRUCTURED (currently unsupported)
121 */
122 define('QR_MODE_ST', 4);
123
124 // -----------------------------------------------------
125
126 // Levels of error correction.
127 // QRcode has a function of an error correcting for miss reading that white is black.
128 // Error correcting is defined in 4 level as below.
129
130 /**
131 * Error correction level L : About 7% or less errors can be corrected.
132 */
133 define('QR_ECLEVEL_L', 0);
134
135 /**
136 * Error correction level M : About 15% or less errors can be corrected.
137 */
138 define('QR_ECLEVEL_M', 1);
139
140 /**
141 * Error correction level Q : About 25% or less errors can be corrected.
142 */
143 define('QR_ECLEVEL_Q', 2);
144
145 /**
146 * Error correction level H : About 30% or less errors can be corrected.
147 */
148 define('QR_ECLEVEL_H', 3);
149
150 // -----------------------------------------------------
151
152 // Version. Size of QRcode is defined as version.
153 // Version is from 1 to 40.
154 // Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
155 // So version 40 is 177*177 matrix.
156
157 /**
158 * Maximum QR Code version.
159 */
160 define('QRSPEC_VERSION_MAX', 40);
161
162 /**
163 * Maximum matrix size for maximum version (version 40 is 177*177 matrix).
164 */
165 define('QRSPEC_WIDTH_MAX', 177);
166
167 // -----------------------------------------------------
168
169 /**
170 * Matrix index to get width from $capacity array.
171 */
172 define('QRCAP_WIDTH', 0);
173
174 /**
175 * Matrix index to get number of words from $capacity array.
176 */
177 define('QRCAP_WORDS', 1);
178
179 /**
180 * Matrix index to get remainder from $capacity array.
181 */
182 define('QRCAP_REMINDER', 2);
183
184 /**
185 * Matrix index to get error correction level from $capacity array.
186 */
187 define('QRCAP_EC', 3);
188
189 // -----------------------------------------------------
190
191 // Structure (currently usupported)
192
193 /**
194 * Number of header bits for structured mode
195 */
196 define('STRUCTURE_HEADER_BITS', 20);
197
198 /**
199 * Max number of symbols for structured mode
200 */
201 define('MAX_STRUCTURED_SYMBOLS', 16);
202
203 // -----------------------------------------------------
204
205 // Masks
206
207 /**
208 * Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
209 */
210 define('N1', 3);
211
212 /**
213 * Down point base value for case 2 mask pattern (module block of same color)
214 */
215 define('N2', 3);
216
217 /**
218 * Down point base value for case 3 mask pattern (1:1:3:1:1(dark:bright:dark:bright:dark)pattern in a line or a column)
219 */
220 define('N3', 40);
221
222 /**
223 * Down point base value for case 4 mask pattern (ration of dark modules in whole)
224 */
225 define('N4', 10);
226
227 // -----------------------------------------------------
228
229 // Optimization settings
230
231 /**
232 * if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
233 */
234 define('QR_FIND_BEST_MASK', true);
235
236 /**
237 * if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
238 */
239 define('QR_FIND_FROM_RANDOM', 2);
240
241 /**
242 * when QR_FIND_BEST_MASK === false
243 */
244 define('QR_DEFAULT_MASK', 2);
245
246 // -----------------------------------------------------
247
248} // end of definitions
249
250// #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
251
252// for compatibility with PHP4
253if (!function_exists('str_split')) {
254 /**
255 * Convert a string to an array (needed for PHP4 compatibility)
256 * @param $string (string) The input string.
257 * @param $split_length (int) Maximum length of the chunk.
258 * @return If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string , the entire string is returned as the first (and only) array element.
259 */
260 function str_split($string, $split_length=1) {
261 if ((strlen($string) > $split_length) OR (!$split_length)) {
262 do {
263 $c = strlen($string);
264 $parts[] = substr($string, 0, $split_length);
265 $string = substr($string, $split_length);
266 } while ($string !== false);
267 } else {
268 $parts = array($string);
269 }
270 return $parts;
271 }
272}
273
274// #####################################################
275
276/**
277 * @class QRcode
278 * Class to create QR-code arrays for TCPDF class.
279 * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
280 * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
281 * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
282 * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
283 *
284 * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
285 * Please read comments on this class source file for full copyright and license information.
286 *
287 * @package com.tecnick.tcpdf
288 * @author Nicola Asuni
289 * @version 1.0.010
290 */
291class QRcode {
292
293 /**
294 * Barcode array to be returned which is readable by TCPDF.
295 * @protected
296 */
297 protected $barcode_array = array();
298
299 /**
300 * QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix.
301 * @protected
302 */
303 protected $version = 0;
304
305 /**
306 * Levels of error correction. See definitions for possible values.
307 * @protected
308 */
309 protected $level = QR_ECLEVEL_L;
310
311 /**
312 * Encoding mode.
313 * @protected
314 */
315 protected $hint = QR_MODE_8B;
316
317 /**
318 * Boolean flag, if true the input string will be converted to uppercase.
319 * @protected
320 */
321 protected $casesensitive = true;
322
323 /**
324 * Structured QR code (not supported yet).
325 * @protected
326 */
327 protected $structured = 0;
328
329 /**
330 * Mask data.
331 * @protected
332 */
333 protected $data;
334
335 // FrameFiller
336
337 /**
338 * Width.
339 * @protected
340 */
341 protected $width;
342
343 /**
344 * Frame.
345 * @protected
346 */
347 protected $frame;
348
349 /**
350 * X position of bit.
351 * @protected
352 */
353 protected $x;
354
355 /**
356 * Y position of bit.
357 * @protected
358 */
359 protected $y;
360
361 /**
362 * Direction.
363 * @protected
364 */
365 protected $dir;
366
367 /**
368 * Single bit value.
369 * @protected
370 */
371 protected $bit;
372
373 // ---- QRrawcode ----
374
375 /**
376 * Data code.
377 * @protected
378 */
379 protected $datacode = array();
380
381 /**
382 * Error correction code.
383 * @protected
384 */
385 protected $ecccode = array();
386
387 /**
388 * Blocks.
389 * @protected
390 */
391 protected $blocks;
392
393 /**
394 * Reed-Solomon blocks.
395 * @protected
396 */
397 protected $rsblocks = array(); //of RSblock
398
399 /**
400 * Counter.
401 * @protected
402 */
403 protected $count;
404
405 /**
406 * Data length.
407 * @protected
408 */
409 protected $dataLength;
410
411 /**
412 * Error correction length.
413 * @protected
414 */
415 protected $eccLength;
416
417 /**
418 * Value b1.
419 * @protected
420 */
421 protected $b1;
422
423 // ---- QRmask ----
424
425 /**
426 * Run length.
427 * @protected
428 */
429 protected $runLength = array();
430
431 // ---- QRsplit ----
432
433 /**
434 * Input data string.
435 * @protected
436 */
437 protected $dataStr = '';
438
439 /**
440 * Input items.
441 * @protected
442 */
443 protected $items;
444
445 // Reed-Solomon items
446
447 /**
448 * Reed-Solomon items.
449 * @protected
450 */
451 protected $rsitems = array();
452
453 /**
454 * Array of frames.
455 * @protected
456 */
457 protected $frames = array();
458
459 /**
460 * Alphabet-numeric convesion table.
461 * @protected
462 */
463 protected $anTable = array(
464 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
465 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
466 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, //
467 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, //
468 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
469 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, //
470 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
471 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 //
472 );
473
474 /**
475 * Array Table of the capacity of symbols.
476 * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
477 * @protected
478 */
479 protected $capacity = array(
480 array( 0, 0, 0, array( 0, 0, 0, 0)), //
481 array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
482 array( 25, 44, 7, array( 10, 16, 22, 28)), //
483 array( 29, 70, 7, array( 15, 26, 36, 44)), //
484 array( 33, 100, 7, array( 20, 36, 52, 64)), //
485 array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
486 array( 41, 172, 7, array( 36, 64, 96, 112)), //
487 array( 45, 196, 0, array( 40, 72, 108, 130)), //
488 array( 49, 242, 0, array( 48, 88, 132, 156)), //
489 array( 53, 292, 0, array( 60, 110, 160, 192)), //
490 array( 57, 346, 0, array( 72, 130, 192, 224)), // 10
491 array( 61, 404, 0, array( 80, 150, 224, 264)), //
492 array( 65, 466, 0, array( 96, 176, 260, 308)), //
493 array( 69, 532, 0, array( 104, 198, 288, 352)), //
494 array( 73, 581, 3, array( 120, 216, 320, 384)), //
495 array( 77, 655, 3, array( 132, 240, 360, 432)), // 15
496 array( 81, 733, 3, array( 144, 280, 408, 480)), //
497 array( 85, 815, 3, array( 168, 308, 448, 532)), //
498 array( 89, 901, 3, array( 180, 338, 504, 588)), //
499 array( 93, 991, 3, array( 196, 364, 546, 650)), //
500 array( 97, 1085, 3, array( 224, 416, 600, 700)), // 20
501 array(101, 1156, 4, array( 224, 442, 644, 750)), //
502 array(105, 1258, 4, array( 252, 476, 690, 816)), //
503 array(109, 1364, 4, array( 270, 504, 750, 900)), //
504 array(113, 1474, 4, array( 300, 560, 810, 960)), //
505 array(117, 1588, 4, array( 312, 588, 870, 1050)), // 25
506 array(121, 1706, 4, array( 336, 644, 952, 1110)), //
507 array(125, 1828, 4, array( 360, 700, 1020, 1200)), //
508 array(129, 1921, 3, array( 390, 728, 1050, 1260)), //
509 array(133, 2051, 3, array( 420, 784, 1140, 1350)), //
510 array(137, 2185, 3, array( 450, 812, 1200, 1440)), // 30
511 array(141, 2323, 3, array( 480, 868, 1290, 1530)), //
512 array(145, 2465, 3, array( 510, 924, 1350, 1620)), //
513 array(149, 2611, 3, array( 540, 980, 1440, 1710)), //
514 array(153, 2761, 3, array( 570, 1036, 1530, 1800)), //
515 array(157, 2876, 0, array( 570, 1064, 1590, 1890)), // 35
516 array(161, 3034, 0, array( 600, 1120, 1680, 1980)), //
517 array(165, 3196, 0, array( 630, 1204, 1770, 2100)), //
518 array(169, 3362, 0, array( 660, 1260, 1860, 2220)), //
519 array(173, 3532, 0, array( 720, 1316, 1950, 2310)), //
520 array(177, 3706, 0, array( 750, 1372, 2040, 2430)) // 40
521 );
522
523 /**
524 * Array Length indicator.
525 * @protected
526 */
527 protected $lengthTableBits = array(
528 array(10, 12, 14),
529 array( 9, 11, 13),
530 array( 8, 16, 16),
531 array( 8, 10, 12)
532 );
533
534 /**
535 * Array Table of the error correction code (Reed-Solomon block).
536 * See Table 12-16 (pp.30-36), JIS X0510:2004.
537 * @protected
538 */
539 protected $eccTable = array(
540 array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)), //
541 array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
542 array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), //
543 array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)), //
544 array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)), //
545 array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
546 array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)), //
547 array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)), //
548 array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)), //
549 array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)), //
550 array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), // 10
551 array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)), //
552 array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)), //
553 array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)), //
554 array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)), //
555 array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), // 15
556 array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)), //
557 array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)), //
558 array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)), //
559 array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)), //
560 array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), // 20
561 array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)), //
562 array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)), //
563 array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)), //
564 array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)), //
565 array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), // 25
566 array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)), //
567 array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)), //
568 array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)), //
569 array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)), //
570 array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30
571 array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)), //
572 array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)), //
573 array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)), //
574 array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)), //
575 array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), // 35
576 array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)), //
577 array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)), //
578 array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)), //
579 array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)), //
580 array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)) // 40
581 );
582
583 /**
584 * Array Positions of alignment patterns.
585 * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them.
586 * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
587 * @protected
588 */
589 protected $alignmentPattern = array(
590 array( 0, 0),
591 array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
592 array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
593 array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15
594 array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20
595 array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25
596 array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30
597 array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35
598 array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) // 35-40
599 );
600
601 /**
602 * Array Version information pattern (BCH coded).
603 * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
604 * size: [QRSPEC_VERSION_MAX - 6]
605 * @protected
606 */
607 protected $versionPattern = array(
608 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, //
609 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, //
610 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, //
611 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, //
612 0x27541, 0x28c69
613 );
614
615 /**
616 * Array Format information
617 * @protected
618 */
619 protected $formatInfo = array(
620 array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), //
621 array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), //
622 array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), //
623 array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b) //
624 );
625
626
627 // -------------------------------------------------
628 // -------------------------------------------------
629
630
631 /**
632 * This is the class constructor.
633 * Creates a QRcode object
634 * @param $code (string) code to represent using QRcode
635 * @param $eclevel (string) error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul>
636 * @public
637 * @since 1.0.000
638 */
639 public function __construct($code, $eclevel = 'L') {
640 $barcode_array = array();
641 if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
642 return false;
643 }
644 // set error correction level
645 $this->level = array_search($eclevel, array('L', 'M', 'Q', 'H'));
646 if ($this->level === false) {
647 $this->level = QR_ECLEVEL_L;
648 }
649 if (($this->hint != QR_MODE_8B) AND ($this->hint != QR_MODE_KJ)) {
650 return false;
651 }
652 if (($this->version < 0) OR ($this->version > QRSPEC_VERSION_MAX)) {
653 return false;
654 }
655 $this->items = array();
656 $this->encodeString($code);
657 if (is_null($this->data)) {
658 return false;
659 }
660 $qrTab = $this->binarize($this->data);
661 $size = count($qrTab);
662 $barcode_array['num_rows'] = $size;
663 $barcode_array['num_cols'] = $size;
664 $barcode_array['bcode'] = array();
665 foreach ($qrTab as $line) {
666 $arrAdd = array();
667 foreach (str_split($line) as $char) {
668 $arrAdd[] = ($char=='1')?1:0;
669 }
670 $barcode_array['bcode'][] = $arrAdd;
671 }
672 $this->barcode_array = $barcode_array;
673 }
674
675 /**
676 * Returns a barcode array which is readable by TCPDF
677 * @return array barcode array readable by TCPDF;
678 * @public
679 */
680 public function getBarcodeArray() {
681 return $this->barcode_array;
682 }
683
684 /**
685 * Convert the frame in binary form
686 * @param $frame (array) array to binarize
687 * @return array frame in binary form
688 */
689 protected function binarize($frame) {
690 $len = count($frame);
691 // the frame is square (width = height)
692 foreach ($frame as &$frameLine) {
693 for ($i=0; $i<$len; $i++) {
694 $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
695 }
696 }
697 return $frame;
698 }
699
700 /**
701 * Encode the input string to QR code
702 * @param $string (string) input string to encode
703 */
704 protected function encodeString($string) {
705 $this->dataStr = $string;
706 if (!$this->casesensitive) {
707 $this->toUpper();
708 }
709 $ret = $this->splitString();
710 if ($ret < 0) {
711 return NULL;
712 }
713 $this->encodeMask(-1);
714 }
715
716 /**
717 * Encode mask
718 * @param $mask (int) masking mode
719 */
720 protected function encodeMask($mask) {
721 $spec = array(0, 0, 0, 0, 0);
722 $this->datacode = $this->getByteStream($this->items);
723 if (is_null($this->datacode)) {
724 return NULL;
725 }
726 $spec = $this->getEccSpec($this->version, $this->level, $spec);
727 $this->b1 = $this->rsBlockNum1($spec);
728 $this->dataLength = $this->rsDataLength($spec);
729 $this->eccLength = $this->rsEccLength($spec);
730 $this->ecccode = array_fill(0, $this->eccLength, 0);
731 $this->blocks = $this->rsBlockNum($spec);
732 $ret = $this->init($spec);
733 if ($ret < 0) {
734 return NULL;
735 }
736 $this->count = 0;
737 $this->width = $this->getWidth($this->version);
738 $this->frame = $this->newFrame($this->version);
739 $this->x = $this->width - 1;
740 $this->y = $this->width - 1;
741 $this->dir = -1;
742 $this->bit = -1;
743 // inteleaved data and ecc codes
744 for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
745 $code = $this->getCode();
746 $bit = 0x80;
747 for ($j=0; $j<8; $j++) {
748 $addr = $this->getNextPosition();
749 $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
750 $bit = $bit >> 1;
751 }
752 }
753 // remainder bits
754 $j = $this->getRemainder($this->version);
755 for ($i=0; $i<$j; $i++) {
756 $addr = $this->getNextPosition();
757 $this->setFrameAt($addr, 0x02);
758 }
759 // masking
760 $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
761 if ($mask < 0) {
762 if (QR_FIND_BEST_MASK) {
763 $masked = $this->mask($this->width, $this->frame, $this->level);
764 } else {
765 $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
766 }
767 } else {
768 $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
769 }
770 if ($masked == NULL) {
771 return NULL;
772 }
773 $this->data = $masked;
774 }
775
776 // - - - - - - - - - - - - - - - - - - - - - - - - -
777
778 // FrameFiller
779
780 /**
781 * Set frame value at specified position
782 * @param $at (array) x,y position
783 * @param $val (int) value of the character to set
784 */
785 protected function setFrameAt($at, $val) {
786 $this->frame[$at['y']][$at['x']] = chr($val);
787 }
788
789 /**
790 * Get frame value at specified position
791 * @param $at (array) x,y position
792 * @return value at specified position
793 */
794 protected function getFrameAt($at) {
795 return ord($this->frame[$at['y']][$at['x']]);
796 }
797
798 /**
799 * Return the next frame position
800 * @return array of x,y coordinates
801 */
802 protected function getNextPosition() {
803 do {
804 if ($this->bit == -1) {
805 $this->bit = 0;
806 return array('x'=>$this->x, 'y'=>$this->y);
807 }
808 $x = $this->x;
809 $y = $this->y;
810 $w = $this->width;
811 if ($this->bit == 0) {
812 $x--;
813 $this->bit++;
814 } else {
815 $x++;
816 $y += $this->dir;
817 $this->bit--;
818 }
819 if ($this->dir < 0) {
820 if ($y < 0) {
821 $y = 0;
822 $x -= 2;
823 $this->dir = 1;
824 if ($x == 6) {
825 $x--;
826 $y = 9;
827 }
828 }
829 } else {
830 if ($y == $w) {
831 $y = $w - 1;
832 $x -= 2;
833 $this->dir = -1;
834 if ($x == 6) {
835 $x--;
836 $y -= 8;
837 }
838 }
839 }
840 if (($x < 0) OR ($y < 0)) {
841 return NULL;
842 }
843 $this->x = $x;
844 $this->y = $y;
845 } while(ord($this->frame[$y][$x]) & 0x80);
846 return array('x'=>$x, 'y'=>$y);
847 }
848
849 // - - - - - - - - - - - - - - - - - - - - - - - - -
850
851 // QRrawcode
852
853 /**
854 * Initialize code.
855 * @param $spec (array) array of ECC specification
856 * @return 0 in case of success, -1 in case of error
857 */
858 protected function init($spec) {
859 $dl = $this->rsDataCodes1($spec);
860 $el = $this->rsEccCodes1($spec);
861 $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
862 $blockNo = 0;
863 $dataPos = 0;
864 $eccPos = 0;
865 $endfor = $this->rsBlockNum1($spec);
866 for ($i=0; $i < $endfor; ++$i) {
867 $ecc = array_slice($this->ecccode, $eccPos);
868 $this->rsblocks[$blockNo] = array();
869 $this->rsblocks[$blockNo]['dataLength'] = $dl;
870 $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
871 $this->rsblocks[$blockNo]['eccLength'] = $el;
872 $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
873 $this->rsblocks[$blockNo]['ecc'] = $ecc;
874 $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
875 $dataPos += $dl;
876 $eccPos += $el;
877 $blockNo++;
878 }
879 if ($this->rsBlockNum2($spec) == 0) {
880 return 0;
881 }
882 $dl = $this->rsDataCodes2($spec);
883 $el = $this->rsEccCodes2($spec);
884 $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
885 if ($rs == NULL) {
886 return -1;
887 }
888 $endfor = $this->rsBlockNum2($spec);
889 for ($i=0; $i < $endfor; ++$i) {
890 $ecc = array_slice($this->ecccode, $eccPos);
891 $this->rsblocks[$blockNo] = array();
892 $this->rsblocks[$blockNo]['dataLength'] = $dl;
893 $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
894 $this->rsblocks[$blockNo]['eccLength'] = $el;
895 $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
896 $this->rsblocks[$blockNo]['ecc'] = $ecc;
897 $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
898 $dataPos += $dl;
899 $eccPos += $el;
900 $blockNo++;
901 }
902 return 0;
903 }
904
905 /**
906 * Return Reed-Solomon block code.
907 * @return array rsblocks
908 */
909 protected function getCode() {
910 if ($this->count < $this->dataLength) {
911 $row = $this->count % $this->blocks;
912 $col = $this->count / $this->blocks;
913 if ($col >= $this->rsblocks[0]['dataLength']) {
914 $row += $this->b1;
915 }
916 $ret = $this->rsblocks[$row]['data'][$col];
917 } elseif ($this->count < $this->dataLength + $this->eccLength) {
918 $row = ($this->count - $this->dataLength) % $this->blocks;
919 $col = ($this->count - $this->dataLength) / $this->blocks;
920 $ret = $this->rsblocks[$row]['ecc'][$col];
921 } else {
922 return 0;
923 }
924 $this->count++;
925 return $ret;
926 }
927
928 // - - - - - - - - - - - - - - - - - - - - - - - - -
929
930 // QRmask
931
932 /**
933 * Write Format Information on frame and returns the number of black bits
934 * @param $width (int) frame width
935 * @param $frame (array) frame
936 * @param $mask (array) masking mode
937 * @param $level (int) error correction level
938 * @return int blacks
939 */
940 protected function writeFormatInformation($width, &$frame, $mask, $level) {
941 $blacks = 0;
942 $format = $this->getFormatInfo($mask, $level);
943 for ($i=0; $i<8; ++$i) {
944 if ($format & 1) {
945 $blacks += 2;
946 $v = 0x85;
947 } else {
948 $v = 0x84;
949 }
950 $frame[8][$width - 1 - $i] = chr($v);
951 if ($i < 6) {
952 $frame[$i][8] = chr($v);
953 } else {
954 $frame[$i + 1][8] = chr($v);
955 }
956 $format = $format >> 1;
957 }
958 for ($i=0; $i<7; ++$i) {
959 if ($format & 1) {
960 $blacks += 2;
961 $v = 0x85;
962 } else {
963 $v = 0x84;
964 }
965 $frame[$width - 7 + $i][8] = chr($v);
966 if ($i == 0) {
967 $frame[8][7] = chr($v);
968 } else {
969 $frame[8][6 - $i] = chr($v);
970 }
971 $format = $format >> 1;
972 }
973 return $blacks;
974 }
975
976 /**
977 * mask0
978 * @param $x (int) X position
979 * @param $y (int) Y position
980 * @return int mask
981 */
982 protected function mask0($x, $y) {
983 return ($x + $y) & 1;
984 }
985
986 /**
987 * mask1
988 * @param $x (int) X position
989 * @param $y (int) Y position
990 * @return int mask
991 */
992 protected function mask1($x, $y) {
993 return ($y & 1);
994 }
995
996 /**
997 * mask2
998 * @param $x (int) X position
999 * @param $y (int) Y position
1000 * @return int mask
1001 */
1002 protected function mask2($x, $y) {
1003 return ($x % 3);
1004 }
1005
1006 /**
1007 * mask3
1008 * @param $x (int) X position
1009 * @param $y (int) Y position
1010 * @return int mask
1011 */
1012 protected function mask3($x, $y) {
1013 return ($x + $y) % 3;
1014 }
1015
1016 /**
1017 * mask4
1018 * @param $x (int) X position
1019 * @param $y (int) Y position
1020 * @return int mask
1021 */
1022 protected function mask4($x, $y) {
1023 return (((int)($y / 2)) + ((int)($x / 3))) & 1;
1024 }
1025
1026 /**
1027 * mask5
1028 * @param $x (int) X position
1029 * @param $y (int) Y position
1030 * @return int mask
1031 */
1032 protected function mask5($x, $y) {
1033 return (($x * $y) & 1) + ($x * $y) % 3;
1034 }
1035
1036 /**
1037 * mask6
1038 * @param $x (int) X position
1039 * @param $y (int) Y position
1040 * @return int mask
1041 */
1042 protected function mask6($x, $y) {
1043 return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
1044 }
1045
1046 /**
1047 * mask7
1048 * @param $x (int) X position
1049 * @param $y (int) Y position
1050 * @return int mask
1051 */
1052 protected function mask7($x, $y) {
1053 return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
1054 }
1055
1056 /**
1057 * Return bitmask
1058 * @param $maskNo (int) mask number
1059 * @param $width (int) width
1060 * @param $frame (array) frame
1061 * @return array bitmask
1062 */
1063 protected function generateMaskNo($maskNo, $width, $frame) {
1064 $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
1065 for ($y=0; $y<$width; ++$y) {
1066 for ($x=0; $x<$width; ++$x) {
1067 if (ord($frame[$y][$x]) & 0x80) {
1068 $bitMask[$y][$x] = 0;
1069 } else {
1070 $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
1071 $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
1072 }
1073 }
1074 }
1075 return $bitMask;
1076 }
1077
1078 /**
1079 * makeMaskNo
1080 * @param $maskNo (int)
1081 * @param $width (int)
1082 * @param $s (int)
1083 * @param $d (int)
1084 * @param $maskGenOnly (boolean)
1085 * @return int b
1086 */
1087 protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
1088 $b = 0;
1089 $bitMask = array();
1090 $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
1091 if ($maskGenOnly) {
1092 return;
1093 }
1094 $d = $s;
1095 for ($y=0; $y<$width; ++$y) {
1096 for ($x=0; $x<$width; ++$x) {
1097 if ($bitMask[$y][$x] == 1) {
1098 $d[$y][$x] = chr(ord($s[$y][$x]) ^ ((int)($bitMask[$y][$x])));
1099 }
1100 $b += (int)(ord($d[$y][$x]) & 1);
1101 }
1102 }
1103 return $b;
1104 }
1105
1106 /**
1107 * makeMask
1108 * @param $width (int)
1109 * @param $frame (array)
1110 * @param $maskNo (int)
1111 * @param $level (int)
1112 * @return array mask
1113 */
1114 protected function makeMask($width, $frame, $maskNo, $level) {
1115 $masked = array_fill(0, $width, str_repeat("\0", $width));
1116 $this->makeMaskNo($maskNo, $width, $frame, $masked);
1117 $this->writeFormatInformation($width, $masked, $maskNo, $level);
1118 return $masked;
1119 }
1120
1121 /**
1122 * calcN1N3
1123 * @param $length (int)
1124 * @return int demerit
1125 */
1126 protected function calcN1N3($length) {
1127 $demerit = 0;
1128 for ($i=0; $i<$length; ++$i) {
1129 if ($this->runLength[$i] >= 5) {
1130 $demerit += (N1 + ($this->runLength[$i] - 5));
1131 }
1132 if ($i & 1) {
1133 if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
1134 $fact = (int)($this->runLength[$i] / 3);
1135 if (($this->runLength[$i-2] == $fact)
1136 AND ($this->runLength[$i-1] == $fact)
1137 AND ($this->runLength[$i+1] == $fact)
1138 AND ($this->runLength[$i+2] == $fact)) {
1139 if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
1140 $demerit += N3;
1141 } elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
1142 $demerit += N3;
1143 }
1144 }
1145 }
1146 }
1147 }
1148 return $demerit;
1149 }
1150
1151 /**
1152 * evaluateSymbol
1153 * @param $width (int)
1154 * @param $frame (array)
1155 * @return int demerit
1156 */
1157 protected function evaluateSymbol($width, $frame) {
1158 $head = 0;
1159 $demerit = 0;
1160 for ($y=0; $y<$width; ++$y) {
1161 $head = 0;
1162 $this->runLength[0] = 1;
1163 $frameY = $frame[$y];
1164 if ($y > 0) {
1165 $frameYM = $frame[$y-1];
1166 }
1167 for ($x=0; $x<$width; ++$x) {
1168 if (($x > 0) AND ($y > 0)) {
1169 $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
1170 $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
1171 if (($b22 | ($w22 ^ 1)) & 1) {
1172 $demerit += N2;
1173 }
1174 }
1175 if (($x == 0) AND (ord($frameY[$x]) & 1)) {
1176 $this->runLength[0] = -1;
1177 $head = 1;
1178 $this->runLength[$head] = 1;
1179 } elseif ($x > 0) {
1180 if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
1181 $head++;
1182 $this->runLength[$head] = 1;
1183 } else {
1184 $this->runLength[$head]++;
1185 }
1186 }
1187 }
1188 $demerit += $this->calcN1N3($head+1);
1189 }
1190 for ($x=0; $x<$width; ++$x) {
1191 $head = 0;
1192 $this->runLength[0] = 1;
1193 for ($y=0; $y<$width; ++$y) {
1194 if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
1195 $this->runLength[0] = -1;
1196 $head = 1;
1197 $this->runLength[$head] = 1;
1198 } elseif ($y > 0) {
1199 if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
1200 $head++;
1201 $this->runLength[$head] = 1;
1202 } else {
1203 $this->runLength[$head]++;
1204 }
1205 }
1206 }
1207 $demerit += $this->calcN1N3($head+1);
1208 }
1209 return $demerit;
1210 }
1211
1212 /**
1213 * mask
1214 * @param $width (int)
1215 * @param $frame (array)
1216 * @param $level (int)
1217 * @return array best mask
1218 */
1219 protected function mask($width, $frame, $level) {
1220 $minDemerit = PHP_INT_MAX;
1221 $bestMaskNum = 0;
1222 $bestMask = array();
1223 $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
1224 if (QR_FIND_FROM_RANDOM !== false) {
1225 $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
1226 for ($i = 0; $i < $howManuOut; ++$i) {
1227 $remPos = rand (0, count($checked_masks)-1);
1228 unset($checked_masks[$remPos]);
1229 $checked_masks = array_values($checked_masks);
1230 }
1231 }
1232 $bestMask = $frame;
1233 foreach ($checked_masks as $i) {
1234 $mask = array_fill(0, $width, str_repeat("\0", $width));
1235 $demerit = 0;
1236 $blacks = 0;
1237 $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
1238 $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
1239 $blacks = (int)(100 * $blacks / ($width * $width));
1240 $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
1241 $demerit += $this->evaluateSymbol($width, $mask);
1242 if ($demerit < $minDemerit) {
1243 $minDemerit = $demerit;
1244 $bestMask = $mask;
1245 $bestMaskNum = $i;
1246 }
1247 }
1248 return $bestMask;
1249 }
1250
1251 // - - - - - - - - - - - - - - - - - - - - - - - - -
1252
1253 // QRsplit
1254
1255 /**
1256 * Return true if the character at specified position is a number
1257 * @param $str (string) string
1258 * @param $pos (int) characted position
1259 * @return boolean true of false
1260 */
1261 protected function isdigitat($str, $pos) {
1262 if ($pos >= strlen($str)) {
1263 return false;
1264 }
1265 return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
1266 }
1267
1268 /**
1269 * Return true if the character at specified position is an alphanumeric character
1270 * @param $str (string) string
1271 * @param $pos (int) characted position
1272 * @return boolean true of false
1273 */
1274 protected function isalnumat($str, $pos) {
1275 if ($pos >= strlen($str)) {
1276 return false;
1277 }
1278 return ($this->lookAnTable(ord($str[$pos])) >= 0);
1279 }
1280
1281 /**
1282 * identifyMode
1283 * @param $pos (int)
1284 * @return int mode
1285 */
1286 protected function identifyMode($pos) {
1287 if ($pos >= strlen($this->dataStr)) {
1288 return QR_MODE_NL;
1289 }
1290 $c = $this->dataStr[$pos];
1291 if ($this->isdigitat($this->dataStr, $pos)) {
1292 return QR_MODE_NM;
1293 } elseif ($this->isalnumat($this->dataStr, $pos)) {
1294 return QR_MODE_AN;
1295 } elseif ($this->hint == QR_MODE_KJ) {
1296 if ($pos+1 < strlen($this->dataStr)) {
1297 $d = $this->dataStr[$pos+1];
1298 $word = (ord($c) << 8) | ord($d);
1299 if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
1300 return QR_MODE_KJ;
1301 }
1302 }
1303 }
1304 return QR_MODE_8B;
1305 }
1306
1307 /**
1308 * eatNum
1309 * @return int run
1310 */
1311 protected function eatNum() {
1312 $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1313 $p = 0;
1314 while($this->isdigitat($this->dataStr, $p)) {
1315 $p++;
1316 }
1317 $run = $p;
1318 $mode = $this->identifyMode($p);
1319 if ($mode == QR_MODE_8B) {
1320 $dif = $this->estimateBitsModeNum($run) + 4 + $ln
1321 + $this->estimateBitsMode8(1) // + 4 + l8
1322 - $this->estimateBitsMode8($run + 1); // - 4 - l8
1323 if ($dif > 0) {
1324 return $this->eat8();
1325 }
1326 }
1327 if ($mode == QR_MODE_AN) {
1328 $dif = $this->estimateBitsModeNum($run) + 4 + $ln
1329 + $this->estimateBitsModeAn(1) // + 4 + la
1330 - $this->estimateBitsModeAn($run + 1);// - 4 - la
1331 if ($dif > 0) {
1332 return $this->eatAn();
1333 }
1334 }
1335 $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
1336 return $run;
1337 }
1338
1339 /**
1340 * eatAn
1341 * @return int run
1342 */
1343 protected function eatAn() {
1344 $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
1345 $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1346 $p =1 ;
1347 while($this->isalnumat($this->dataStr, $p)) {
1348 if ($this->isdigitat($this->dataStr, $p)) {
1349 $q = $p;
1350 while($this->isdigitat($this->dataStr, $q)) {
1351 $q++;
1352 }
1353 $dif = $this->estimateBitsModeAn($p) // + 4 + la
1354 + $this->estimateBitsModeNum($q - $p) + 4 + $ln
1355 - $this->estimateBitsModeAn($q); // - 4 - la
1356 if ($dif < 0) {
1357 break;
1358 } else {
1359 $p = $q;
1360 }
1361 } else {
1362 $p++;
1363 }
1364 }
1365 $run = $p;
1366 if (!$this->isalnumat($this->dataStr, $p)) {
1367 $dif = $this->estimateBitsModeAn($run) + 4 + $la
1368 + $this->estimateBitsMode8(1) // + 4 + l8
1369 - $this->estimateBitsMode8($run + 1); // - 4 - l8
1370 if ($dif > 0) {
1371 return $this->eat8();
1372 }
1373 }
1374 $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
1375 return $run;
1376 }
1377
1378 /**
1379 * eatKanji
1380 * @return int run
1381 */
1382 protected function eatKanji() {
1383 $p = 0;
1384 while($this->identifyMode($p) == QR_MODE_KJ) {
1385 $p += 2;
1386 }
1387 $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
1388 return $run;
1389 }
1390
1391 /**
1392 * eat8
1393 * @return int run
1394 */
1395 protected function eat8() {
1396 $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
1397 $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1398 $p = 1;
1399 $dataStrLen = strlen($this->dataStr);
1400 while($p < $dataStrLen) {
1401 $mode = $this->identifyMode($p);
1402 if ($mode == QR_MODE_KJ) {
1403 break;
1404 }
1405 if ($mode == QR_MODE_NM) {
1406 $q = $p;
1407 while($this->isdigitat($this->dataStr, $q)) {
1408 $q++;
1409 }
1410 $dif = $this->estimateBitsMode8($p) // + 4 + l8
1411 + $this->estimateBitsModeNum($q - $p) + 4 + $ln
1412 - $this->estimateBitsMode8($q); // - 4 - l8
1413 if ($dif < 0) {
1414 break;
1415 } else {
1416 $p = $q;
1417 }
1418 } elseif ($mode == QR_MODE_AN) {
1419 $q = $p;
1420 while($this->isalnumat($this->dataStr, $q)) {
1421 $q++;
1422 }
1423 $dif = $this->estimateBitsMode8($p) // + 4 + l8
1424 + $this->estimateBitsModeAn($q - $p) + 4 + $la
1425 - $this->estimateBitsMode8($q); // - 4 - l8
1426 if ($dif < 0) {
1427 break;
1428 } else {
1429 $p = $q;
1430 }
1431 } else {
1432 $p++;
1433 }
1434 }
1435 $run = $p;
1436 $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
1437 return $run;
1438 }
1439
1440 /**
1441 * splitString
1442 * @return (int)
1443 */
1444 protected function splitString() {
1445 while (strlen($this->dataStr) > 0) {
1446 $mode = $this->identifyMode(0);
1447 switch ($mode) {
1448 case QR_MODE_NM: {
1449 $length = $this->eatNum();
1450 break;
1451 }
1452 case QR_MODE_AN: {
1453 $length = $this->eatAn();
1454 break;
1455 }
1456 case QR_MODE_KJ: {
1457 if ($hint == QR_MODE_KJ) {
1458 $length = $this->eatKanji();
1459 } else {
1460 $length = $this->eat8();
1461 }
1462 break;
1463 }
1464 default: {
1465 $length = $this->eat8();
1466 break;
1467 }
1468 }
1469 if ($length == 0) {
1470 return 0;
1471 }
1472 if ($length < 0) {
1473 return -1;
1474 }
1475 $this->dataStr = substr($this->dataStr, $length);
1476 }
1477 return 0;
1478 }
1479
1480 /**
1481 * toUpper
1482 */
1483 protected function toUpper() {
1484 $stringLen = strlen($this->dataStr);
1485 $p = 0;
1486 while ($p < $stringLen) {
1487 $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
1488 if ($mode == QR_MODE_KJ) {
1489 $p += 2;
1490 } else {
1491 if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
1492 $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
1493 }
1494 $p++;
1495 }
1496 }
1497 return $this->dataStr;
1498 }
1499
1500 // - - - - - - - - - - - - - - - - - - - - - - - - -
1501
1502 // QRinputItem
1503
1504 /**
1505 * newInputItem
1506 * @param $mode (int)
1507 * @param $size (int)
1508 * @param $data (array)
1509 * @param $bstream (array)
1510 * @return array input item
1511 */
1512 protected function newInputItem($mode, $size, $data, $bstream=null) {
1513 $setData = array_slice($data, 0, $size);
1514 if (count($setData) < $size) {
1515 $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
1516 }
1517 if (!$this->check($mode, $size, $setData)) {
1518 return NULL;
1519 }
1520 $inputitem = array();
1521 $inputitem['mode'] = $mode;
1522 $inputitem['size'] = $size;
1523 $inputitem['data'] = $setData;
1524 $inputitem['bstream'] = $bstream;
1525 return $inputitem;
1526 }
1527
1528 /**
1529 * encodeModeNum
1530 * @param $inputitem (array)
1531 * @param $version (int)
1532 * @return array input item
1533 */
1534 protected function encodeModeNum($inputitem, $version) {
1535 $words = (int)($inputitem['size'] / 3);
1536 $inputitem['bstream'] = array();
1537 $val = 0x1;
1538 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
1539 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
1540 for ($i=0; $i < $words; ++$i) {
1541 $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
1542 $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
1543 $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
1544 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
1545 }
1546 if ($inputitem['size'] - $words * 3 == 1) {
1547 $val = ord($inputitem['data'][$words*3]) - ord('0');
1548 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
1549 } elseif (($inputitem['size'] - ($words * 3)) == 2) {
1550 $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
1551 $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
1552 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
1553 }
1554 return $inputitem;
1555 }
1556
1557 /**
1558 * encodeModeAn
1559 * @param $inputitem (array)
1560 * @param $version (int)
1561 * @return array input item
1562 */
1563 protected function encodeModeAn($inputitem, $version) {
1564 $words = (int)($inputitem['size'] / 2);
1565 $inputitem['bstream'] = array();
1566 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
1567 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
1568 for ($i=0; $i < $words; ++$i) {
1569 $val = (int)($this->lookAnTable(ord($inputitem['data'][$i*2])) * 45);
1570 $val += (int)($this->lookAnTable(ord($inputitem['data'][($i*2)+1])));
1571 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
1572 }
1573 if ($inputitem['size'] & 1) {
1574 $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
1575 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
1576 }
1577 return $inputitem;
1578 }
1579
1580 /**
1581 * encodeMode8
1582 * @param $inputitem (array)
1583 * @param $version (int)
1584 * @return array input item
1585 */
1586 protected function encodeMode8($inputitem, $version) {
1587 $inputitem['bstream'] = array();
1588 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
1589 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
1590 for ($i=0; $i < $inputitem['size']; ++$i) {
1591 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
1592 }
1593 return $inputitem;
1594 }
1595
1596 /**
1597 * encodeModeKanji
1598 * @param $inputitem (array)
1599 * @param $version (int)
1600 * @return array input item
1601 */
1602 protected function encodeModeKanji($inputitem, $version) {
1603 $inputitem['bstream'] = array();
1604 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
1605 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
1606 for ($i=0; $i<$inputitem['size']; $i+=2) {
1607 $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
1608 if ($val <= 0x9ffc) {
1609 $val -= 0x8140;
1610 } else {
1611 $val -= 0xc140;
1612 }
1613 $h = ($val >> 8) * 0xc0;
1614 $val = ($val & 0xff) + $h;
1615 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
1616 }
1617 return $inputitem;
1618 }
1619
1620 /**
1621 * encodeModeStructure
1622 * @param $inputitem (array)
1623 * @return array input item
1624 */
1625 protected function encodeModeStructure($inputitem) {
1626 $inputitem['bstream'] = array();
1627 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
1628 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
1629 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
1630 $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
1631 return $inputitem;
1632 }
1633
1634 /**
1635 * encodeBitStream
1636 * @param $inputitem (array)
1637 * @param $version (int)
1638 * @return array input item
1639 */
1640 protected function encodeBitStream($inputitem, $version) {
1641 $inputitem['bstream'] = array();
1642 $words = $this->maximumWords($inputitem['mode'], $version);
1643 if ($inputitem['size'] > $words) {
1644 $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
1645 $st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words));
1646 $st1 = $this->encodeBitStream($st1, $version);
1647 $st2 = $this->encodeBitStream($st2, $version);
1648 $inputitem['bstream'] = array();
1649 $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
1650 $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
1651 } else {
1652 switch($inputitem['mode']) {
1653 case QR_MODE_NM: {
1654 $inputitem = $this->encodeModeNum($inputitem, $version);
1655 break;
1656 }
1657 case QR_MODE_AN: {
1658 $inputitem = $this->encodeModeAn($inputitem, $version);
1659 break;
1660 }
1661 case QR_MODE_8B: {
1662 $inputitem = $this->encodeMode8($inputitem, $version);
1663 break;
1664 }
1665 case QR_MODE_KJ: {
1666 $inputitem = $this->encodeModeKanji($inputitem, $version);
1667 break;
1668 }
1669 case QR_MODE_ST: {
1670 $inputitem = $this->encodeModeStructure($inputitem);
1671 break;
1672 }
1673 default: {
1674 break;
1675 }
1676 }
1677 }
1678 return $inputitem;
1679 }
1680
1681 // - - - - - - - - - - - - - - - - - - - - - - - - -
1682
1683 // QRinput
1684
1685 /**
1686 * Append data to an input object.
1687 * The data is copied and appended to the input object.
1688 * @param $items (arrray) input items
1689 * @param $mode (int) encoding mode.
1690 * @param $size (int) size of data (byte).
1691 * @param $data (array) array of input data.
1692 * @return items
1693 *
1694 */
1695 protected function appendNewInputItem($items, $mode, $size, $data) {
1696 $newitem = $this->newInputItem($mode, $size, $data);
1697 if (!empty($newitem)) {
1698 $items[] = $newitem;
1699 }
1700 return $items;
1701 }
1702
1703 /**
1704 * insertStructuredAppendHeader
1705 * @param $items (array)
1706 * @param $size (int)
1707 * @param $index (int)
1708 * @param $parity (int)
1709 * @return array items
1710 */
1711 protected function insertStructuredAppendHeader($items, $size, $index, $parity) {
1712 if ($size > MAX_STRUCTURED_SYMBOLS) {
1713 return -1;
1714 }
1715 if (($index <= 0) OR ($index > MAX_STRUCTURED_SYMBOLS)) {
1716 return -1;
1717 }
1718 $buf = array($size, $index, $parity);
1719 $entry = $this->newInputItem(QR_MODE_ST, 3, buf);
1720 array_unshift($items, $entry);
1721 return $items;
1722 }
1723
1724 /**
1725 * calcParity
1726 * @param $items (array)
1727 * @return int parity
1728 */
1729 protected function calcParity($items) {
1730 $parity = 0;
1731 foreach ($items as $item) {
1732 if ($item['mode'] != QR_MODE_ST) {
1733 for ($i=$item['size']-1; $i>=0; --$i) {
1734 $parity ^= $item['data'][$i];
1735 }
1736 }
1737 }
1738 return $parity;
1739 }
1740
1741 /**
1742 * checkModeNum
1743 * @param $size (int)
1744 * @param $data (array)
1745 * @return boolean true or false
1746 */
1747 protected function checkModeNum($size, $data) {
1748 for ($i=0; $i<$size; ++$i) {
1749 if ((ord($data[$i]) < ord('0')) OR (ord($data[$i]) > ord('9'))){
1750 return false;
1751 }
1752 }
1753 return true;
1754 }
1755
1756 /**
1757 * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
1758 * @param $c (int) character value
1759 * @return value
1760 */
1761 protected function lookAnTable($c) {
1762 return (($c > 127)?-1:$this->anTable[$c]);
1763 }
1764
1765 /**
1766 * checkModeAn
1767 * @param $size (int)
1768 * @param $data (array)
1769 * @return boolean true or false
1770 */
1771 protected function checkModeAn($size, $data) {
1772 for ($i=0; $i<$size; ++$i) {
1773 if ($this->lookAnTable(ord($data[$i])) == -1) {
1774 return false;
1775 }
1776 }
1777 return true;
1778 }
1779
1780 /**
1781 * estimateBitsModeNum
1782 * @param $size (int)
1783 * @return int number of bits
1784 */
1785 protected function estimateBitsModeNum($size) {
1786 $w = (int)($size / 3);
1787 $bits = ($w * 10);
1788 switch($size - ($w * 3)) {
1789 case 1: {
1790 $bits += 4;
1791 break;
1792 }
1793 case 2: {
1794 $bits += 7;
1795 break;
1796 }
1797 }
1798 return $bits;
1799 }
1800
1801 /**
1802 * estimateBitsModeAn
1803 * @param $size (int)
1804 * @return int number of bits
1805 */
1806 protected function estimateBitsModeAn($size) {
1807 $bits = (int)($size * 5.5); // (size / 2 ) * 11
1808 if ($size & 1) {
1809 $bits += 6;
1810 }
1811 return $bits;
1812 }
1813
1814 /**
1815 * estimateBitsMode8
1816 * @param $size (int)
1817 * @return int number of bits
1818 */
1819 protected function estimateBitsMode8($size) {
1820 return (int)($size * 8);
1821 }
1822
1823 /**
1824 * estimateBitsModeKanji
1825 * @param $size (int)
1826 * @return int number of bits
1827 */
1828 protected function estimateBitsModeKanji($size) {
1829 return (int)($size * 6.5); // (size / 2 ) * 13
1830 }
1831
1832 /**
1833 * checkModeKanji
1834 * @param $size (int)
1835 * @param $data (array)
1836 * @return boolean true or false
1837 */
1838 protected function checkModeKanji($size, $data) {
1839 if ($size & 1) {
1840 return false;
1841 }
1842 for ($i=0; $i<$size; $i+=2) {
1843 $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
1844 if (($val < 0x8140) OR (($val > 0x9ffc) AND ($val < 0xe040)) OR ($val > 0xebbf)) {
1845 return false;
1846 }
1847 }
1848 return true;
1849 }
1850
1851 /**
1852 * Validate the input data.
1853 * @param $mode (int) encoding mode.
1854 * @param $size (int) size of data (byte).
1855 * @param $data (array) data to validate
1856 * @return boolean true in case of valid data, false otherwise
1857 */
1858 protected function check($mode, $size, $data) {
1859 if ($size <= 0) {
1860 return false;
1861 }
1862 switch($mode) {
1863 case QR_MODE_NM: {
1864 return $this->checkModeNum($size, $data);
1865 }
1866 case QR_MODE_AN: {
1867 return $this->checkModeAn($size, $data);
1868 }
1869 case QR_MODE_KJ: {
1870 return $this->checkModeKanji($size, $data);
1871 }
1872 case QR_MODE_8B: {
1873 return true;
1874 }
1875 case QR_MODE_ST: {
1876 return true;
1877 }
1878 default: {
1879 break;
1880 }
1881 }
1882 return false;
1883 }
1884
1885 /**
1886 * estimateBitStreamSize
1887 * @param $items (array)
1888 * @param $version (int)
1889 * @return int bits
1890 */
1891 protected function estimateBitStreamSize($items, $version) {
1892 $bits = 0;
1893 if ($version == 0) {
1894 $version = 1;
1895 }
1896 foreach ($items as $item) {
1897 switch($item['mode']) {
1898 case QR_MODE_NM: {
1899 $bits = $this->estimateBitsModeNum($item['size']);
1900 break;
1901 }
1902 case QR_MODE_AN: {
1903 $bits = $this->estimateBitsModeAn($item['size']);
1904 break;
1905 }
1906 case QR_MODE_8B: {
1907 $bits = $this->estimateBitsMode8($item['size']);
1908 break;
1909 }
1910 case QR_MODE_KJ: {
1911 $bits = $this->estimateBitsModeKanji($item['size']);
1912 break;
1913 }
1914 case QR_MODE_ST: {
1915 return STRUCTURE_HEADER_BITS;
1916 }
1917 default: {
1918 return 0;
1919 }
1920 }
1921 $l = $this->lengthIndicator($item['mode'], $version);
1922 $m = 1 << $l;
1923 $num = (int)(($item['size'] + $m - 1) / $m);
1924 $bits += $num * (4 + $l);
1925 }
1926 return $bits;
1927 }
1928
1929 /**
1930 * estimateVersion
1931 * @param $items (array)
1932 * @return int version
1933 */
1934 protected function estimateVersion($items) {
1935 $version = 0;
1936 $prev = 0;
1937 do {
1938 $prev = $version;
1939 $bits = $this->estimateBitStreamSize($items, $prev);
1940 $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
1941 if ($version < 0) {
1942 return -1;
1943 }
1944 } while ($version > $prev);
1945 return $version;
1946 }
1947
1948 /**
1949 * lengthOfCode
1950 * @param $mode (int)
1951 * @param $version (int)
1952 * @param $bits (int)
1953 * @return int size
1954 */
1955 protected function lengthOfCode($mode, $version, $bits) {
1956 $payload = $bits - 4 - $this->lengthIndicator($mode, $version);
1957 switch($mode) {
1958 case QR_MODE_NM: {
1959 $chunks = (int)($payload / 10);
1960 $remain = $payload - $chunks * 10;
1961 $size = $chunks * 3;
1962 if ($remain >= 7) {
1963 $size += 2;
1964 } elseif ($remain >= 4) {
1965 $size += 1;
1966 }
1967 break;
1968 }
1969 case QR_MODE_AN: {
1970 $chunks = (int)($payload / 11);
1971 $remain = $payload - $chunks * 11;
1972 $size = $chunks * 2;
1973 if ($remain >= 6) {
1974 ++$size;
1975 }
1976 break;
1977 }
1978 case QR_MODE_8B: {
1979 $size = (int)($payload / 8);
1980 break;
1981 }
1982 case QR_MODE_KJ: {
1983 $size = (int)(($payload / 13) * 2);
1984 break;
1985 }
1986 case QR_MODE_ST: {
1987 $size = (int)($payload / 8);
1988 break;
1989 }
1990 default: {
1991 $size = 0;
1992 break;
1993 }
1994 }
1995 $maxsize = $this->maximumWords($mode, $version);
1996 if ($size < 0) {
1997 $size = 0;
1998 }
1999 if ($size > $maxsize) {
2000 $size = $maxsize;
2001 }
2002 return $size;
2003 }
2004
2005 /**
2006 * createBitStream
2007 * @param $items (array)
2008 * @return array of items and total bits
2009 */
2010 protected function createBitStream($items) {
2011 $total = 0;
2012 foreach ($items as $key => $item) {
2013 $items[$key] = $this->encodeBitStream($item, $this->version);
2014 $bits = count($items[$key]['bstream']);
2015 $total += $bits;
2016 }
2017 return array($items, $total);
2018 }
2019
2020 /**
2021 * convertData
2022 * @param $items (array)
2023 * @return array items
2024 */
2025 protected function convertData($items) {
2026 $ver = $this->estimateVersion($items);
2027 if ($ver > $this->version) {
2028 $this->version = $ver;
2029 }
2030 while (true) {
2031 $cbs = $this->createBitStream($items);
2032 $items = $cbs[0];
2033 $bits = $cbs[1];
2034 if ($bits < 0) {
2035 return -1;
2036 }
2037 $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
2038 if ($ver < 0) {
2039 return -1;
2040 } elseif ($ver > $this->version) {
2041 $this->version = $ver;
2042 } else {
2043 break;
2044 }
2045 }
2046 return $items;
2047 }
2048
2049 /**
2050 * Append Padding Bit to bitstream
2051 * @param $bstream (array)
2052 * @return array bitstream
2053 */
2054 protected function appendPaddingBit($bstream) {
2055 if (is_null($bstream)) {
2056 return null;
2057 }
2058 $bits = count($bstream);
2059 $maxwords = $this->getDataLength($this->version, $this->level);
2060 $maxbits = $maxwords * 8;
2061 if ($maxbits == $bits) {
2062 return $bstream;
2063 }
2064 if ($maxbits - $bits < 5) {
2065 return $this->appendNum($bstream, $maxbits - $bits, 0);
2066 }
2067 $bits += 4;
2068 $words = (int)(($bits + 7) / 8);
2069 $padding = array();
2070 $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
2071 $padlen = $maxwords - $words;
2072 if ($padlen > 0) {
2073 $padbuf = array();
2074 for ($i=0; $i<$padlen; ++$i) {
2075 $padbuf[$i] = ($i&1)?0x11:0xec;
2076 }
2077 $padding = $this->appendBytes($padding, $padlen, $padbuf);
2078 }
2079 return $this->appendBitstream($bstream, $padding);
2080 }
2081
2082 /**
2083 * mergeBitStream
2084 * @param $items (array) items
2085 * @return array bitstream
2086 */
2087 protected function mergeBitStream($items) {
2088 $items = $this->convertData($items);
2089 if (!is_array($items)) {
2090 return null;
2091 }
2092 $bstream = array();
2093 foreach ($items as $item) {
2094 $bstream = $this->appendBitstream($bstream, $item['bstream']);
2095 }
2096 return $bstream;
2097 }
2098
2099 /**
2100 * Returns a stream of bits.
2101 * @param $items (int)
2102 * @return array padded merged byte stream
2103 */
2104 protected function getBitStream($items) {
2105 $bstream = $this->mergeBitStream($items);
2106 return $this->appendPaddingBit($bstream);
2107 }
2108
2109 /**
2110 * Pack all bit streams padding bits into a byte array.
2111 * @param $items (int)
2112 * @return array padded merged byte stream
2113 */
2114 protected function getByteStream($items) {
2115 $bstream = $this->getBitStream($items);
2116 return $this->bitstreamToByte($bstream);
2117 }
2118
2119 // - - - - - - - - - - - - - - - - - - - - - - - - -
2120
2121 // QRbitstream
2122
2123 /**
2124 * Return an array with zeros
2125 * @param $setLength (int) array size
2126 * @return array
2127 */
2128 protected function allocate($setLength) {
2129 return array_fill(0, $setLength, 0);
2130 }
2131
2132 /**
2133 * Return new bitstream from number
2134 * @param $bits (int) number of bits
2135 * @param $num (int) number
2136 * @return array bitstream
2137 */
2138 protected function newFromNum($bits, $num) {
2139 $bstream = $this->allocate($bits);
2140 $mask = 1 << ($bits - 1);
2141 for ($i=0; $i<$bits; ++$i) {
2142 if ($num & $mask) {
2143 $bstream[$i] = 1;
2144 } else {
2145 $bstream[$i] = 0;
2146 }
2147 $mask = $mask >> 1;
2148 }
2149 return $bstream;
2150 }
2151
2152 /**
2153 * Return new bitstream from bytes
2154 * @param $size (int) size
2155 * @param $data (array) bytes
2156 * @return array bitstream
2157 */
2158 protected function newFromBytes($size, $data) {
2159 $bstream = $this->allocate($size * 8);
2160 $p=0;
2161 for ($i=0; $i<$size; ++$i) {
2162 $mask = 0x80;
2163 for ($j=0; $j<8; ++$j) {
2164 if ($data[$i] & $mask) {
2165 $bstream[$p] = 1;
2166 } else {
2167 $bstream[$p] = 0;
2168 }
2169 $p++;
2170 $mask = $mask >> 1;
2171 }
2172 }
2173 return $bstream;
2174 }
2175
2176 /**
2177 * Append one bitstream to another
2178 * @param $bitstream (array) original bitstream
2179 * @param $append (array) bitstream to append
2180 * @return array bitstream
2181 */
2182 protected function appendBitstream($bitstream, $append) {
2183 if ((!is_array($append)) OR (count($append) == 0)) {
2184 return $bitstream;
2185 }
2186 if (count($bitstream) == 0) {
2187 return $append;
2188 }
2189 return array_values(array_merge($bitstream, $append));
2190 }
2191
2192 /**
2193 * Append one bitstream created from number to another
2194 * @param $bitstream (array) original bitstream
2195 * @param $bits (int) number of bits
2196 * @param $num (int) number
2197 * @return array bitstream
2198 */
2199 protected function appendNum($bitstream, $bits, $num) {
2200 if ($bits == 0) {
2201 return 0;
2202 }
2203 $b = $this->newFromNum($bits, $num);
2204 return $this->appendBitstream($bitstream, $b);
2205 }
2206
2207 /**
2208 * Append one bitstream created from bytes to another
2209 * @param $bitstream (array) original bitstream
2210 * @param $size (int) size
2211 * @param $data (array) bytes
2212 * @return array bitstream
2213 */
2214 protected function appendBytes($bitstream, $size, $data) {
2215 if ($size == 0) {
2216 return 0;
2217 }
2218 $b = $this->newFromBytes($size, $data);
2219 return $this->appendBitstream($bitstream, $b);
2220 }
2221
2222 /**
2223 * Convert bitstream to bytes
2224 * @param $bstream (array) original bitstream
2225 * @return array of bytes
2226 */
2227 protected function bitstreamToByte($bstream) {
2228 if (is_null($bstream)) {
2229 return null;
2230 }
2231 $size = count($bstream);
2232 if ($size == 0) {
2233 return array();
2234 }
2235 $data = array_fill(0, (int)(($size + 7) / 8), 0);
2236 $bytes = (int)($size / 8);
2237 $p = 0;
2238 for ($i=0; $i<$bytes; $i++) {
2239 $v = 0;
2240 for ($j=0; $j<8; $j++) {
2241 $v = $v << 1;
2242 $v |= $bstream[$p];
2243 $p++;
2244 }
2245 $data[$i] = $v;
2246 }
2247 if ($size & 7) {
2248 $v = 0;
2249 for ($j=0; $j<($size & 7); $j++) {
2250 $v = $v << 1;
2251 $v |= $bstream[$p];
2252 $p++;
2253 }
2254 $data[$bytes] = $v;
2255 }
2256 return $data;
2257 }
2258
2259 // - - - - - - - - - - - - - - - - - - - - - - - - -
2260
2261 // QRspec
2262
2263 /**
2264 * Replace a value on the array at the specified position
2265 * @param $srctab (array)
2266 * @param $x (int) X position
2267 * @param $y (int) Y position
2268 * @param $repl (string) value to replace
2269 * @param $replLen (int) length of the repl string
2270 * @return array srctab
2271 */
2272 protected function qrstrset($srctab, $x, $y, $repl, $replLen=false) {
2273 $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
2274 return $srctab;
2275 }
2276
2277 /**
2278 * Return maximum data code length (bytes) for the version.
2279 * @param $version (int) version
2280 * @param $level (int) error correction level
2281 * @return int maximum size (bytes)
2282 */
2283 protected function getDataLength($version, $level) {
2284 return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level];
2285 }
2286
2287 /**
2288 * Return maximum error correction code length (bytes) for the version.
2289 * @param $version (int) version
2290 * @param $level (int) error correction level
2291 * @return int ECC size (bytes)
2292 */
2293 protected function getECCLength($version, $level){
2294 return $this->capacity[$version][QRCAP_EC][$level];
2295 }
2296
2297 /**
2298 * Return the width of the symbol for the version.
2299 * @param $version (int) version
2300 * @return int width
2301 */
2302 protected function getWidth($version) {
2303 return $this->capacity[$version][QRCAP_WIDTH];
2304 }
2305
2306 /**
2307 * Return the numer of remainder bits.
2308 * @param $version (int) version
2309 * @return int number of remainder bits
2310 */
2311 protected function getRemainder($version) {
2312 return $this->capacity[$version][QRCAP_REMINDER];
2313 }
2314
2315 /**
2316 * Return a version number that satisfies the input code length.
2317 * @param $size (int) input code length (bytes)
2318 * @param $level (int) error correction level
2319 * @return int version number
2320 */
2321 protected function getMinimumVersion($size, $level) {
2322 for ($i = 1; $i <= QRSPEC_VERSION_MAX; ++$i) {
2323 $words = ($this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level]);
2324 if ($words >= $size) {
2325 return $i;
2326 }
2327 }
2328 // the size of input data is greater than QR capacity, try to lover the error correction mode
2329 return -1;
2330 }
2331
2332 /**
2333 * Return the size of length indicator for the mode and version.
2334 * @param $mode (int) encoding mode
2335 * @param $version (int) version
2336 * @return int the size of the appropriate length indicator (bits).
2337 */
2338 protected function lengthIndicator($mode, $version) {
2339 if ($mode == QR_MODE_ST) {
2340 return 0;
2341 }
2342 if ($version <= 9) {
2343 $l = 0;
2344 } elseif ($version <= 26) {
2345 $l = 1;
2346 } else {
2347 $l = 2;
2348 }
2349 return $this->lengthTableBits[$mode][$l];
2350 }
2351
2352 /**
2353 * Return the maximum length for the mode and version.
2354 * @param $mode (int) encoding mode
2355 * @param $version (int) version
2356 * @return int the maximum length (bytes)
2357 */
2358 protected function maximumWords($mode, $version) {
2359 if ($mode == QR_MODE_ST) {
2360 return 3;
2361 }
2362 if ($version <= 9) {
2363 $l = 0;
2364 } else if ($version <= 26) {
2365 $l = 1;
2366 } else {
2367 $l = 2;
2368 }
2369 $bits = $this->lengthTableBits[$mode][$l];
2370 $words = (1 << $bits) - 1;
2371 if ($mode == QR_MODE_KJ) {
2372 $words *= 2; // the number of bytes is required
2373 }
2374 return $words;
2375 }
2376
2377 /**
2378 * Return an array of ECC specification.
2379 * @param $version (int) version
2380 * @param $level (int) error correction level
2381 * @param $spec (array) an array of ECC specification contains as following: {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code}
2382 * @return array spec
2383 */
2384 protected function getEccSpec($version, $level, $spec) {
2385 if (count($spec) < 5) {
2386 $spec = array(0, 0, 0, 0, 0);
2387 }
2388 $b1 = $this->eccTable[$version][$level][0];
2389 $b2 = $this->eccTable[$version][$level][1];
2390 $data = $this->getDataLength($version, $level);
2391 $ecc = $this->getECCLength($version, $level);
2392 if ($b2 == 0) {
2393 $spec[0] = $b1;
2394 $spec[1] = (int)($data / $b1);
2395 $spec[2] = (int)($ecc / $b1);
2396 $spec[3] = 0;
2397 $spec[4] = 0;
2398 } else {
2399 $spec[0] = $b1;
2400 $spec[1] = (int)($data / ($b1 + $b2));
2401 $spec[2] = (int)($ecc / ($b1 + $b2));
2402 $spec[3] = $b2;
2403 $spec[4] = $spec[1] + 1;
2404 }
2405 return $spec;
2406 }
2407
2408 /**
2409 * Put an alignment marker.
2410 * @param $frame (array) frame
2411 * @param $ox (int) X center coordinate of the pattern
2412 * @param $oy (int) Y center coordinate of the pattern
2413 * @return array frame
2414 */
2415 protected function putAlignmentMarker($frame, $ox, $oy) {
2416 $finder = array(
2417 "\xa1\xa1\xa1\xa1\xa1",
2418 "\xa1\xa0\xa0\xa0\xa1",
2419 "\xa1\xa0\xa1\xa0\xa1",
2420 "\xa1\xa0\xa0\xa0\xa1",
2421 "\xa1\xa1\xa1\xa1\xa1"
2422 );
2423 $yStart = $oy - 2;
2424 $xStart = $ox - 2;
2425 for ($y=0; $y < 5; $y++) {
2426 $frame = $this->qrstrset($frame, $xStart, $yStart+$y, $finder[$y]);
2427 }
2428 return $frame;
2429 }
2430
2431 /**
2432 * Put an alignment pattern.
2433 * @param $version (int) version
2434 * @param $frame (array) frame
2435 * @param $width (int) width
2436 * @return array frame
2437 */
2438 protected function putAlignmentPattern($version, $frame, $width) {
2439 if ($version < 2) {
2440 return $frame;
2441 }
2442 $d = $this->alignmentPattern[$version][1] - $this->alignmentPattern[$version][0];
2443 if ($d < 0) {
2444 $w = 2;
2445 } else {
2446 $w = (int)(($width - $this->alignmentPattern[$version][0]) / $d + 2);
2447 }
2448 if ($w * $w - 3 == 1) {
2449 $x = $this->alignmentPattern[$version][0];
2450 $y = $this->alignmentPattern[$version][0];
2451 $frame = $this->putAlignmentMarker($frame, $x, $y);
2452 return $frame;
2453 }
2454 $cx = $this->alignmentPattern[$version][0];
2455 $wo = $w - 1;
2456 for ($x=1; $x < $wo; ++$x) {
2457 $frame = $this->putAlignmentMarker($frame, 6, $cx);
2458 $frame = $this->putAlignmentMarker($frame, $cx, 6);
2459 $cx += $d;
2460 }
2461 $cy = $this->alignmentPattern[$version][0];
2462 for ($y=0; $y < $wo; ++$y) {
2463 $cx = $this->alignmentPattern[$version][0];
2464 for ($x=0; $x < $wo; ++$x) {
2465 $frame = $this->putAlignmentMarker($frame, $cx, $cy);
2466 $cx += $d;
2467 }
2468 $cy += $d;
2469 }
2470 return $frame;
2471 }
2472
2473 /**
2474 * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits.
2475 * @param $version (int) version
2476 * @return BCH encoded version information pattern
2477 */
2478 protected function getVersionPattern($version) {
2479 if (($version < 7) OR ($version > QRSPEC_VERSION_MAX)) {
2480 return 0;
2481 }
2482 return $this->versionPattern[($version - 7)];
2483 }
2484
2485 /**
2486 * Return BCH encoded format information pattern.
2487 * @param $mask (array)
2488 * @param $level (int) error correction level
2489 * @return BCH encoded format information pattern
2490 */
2491 protected function getFormatInfo($mask, $level) {
2492 if (($mask < 0) OR ($mask > 7)) {
2493 return 0;
2494 }
2495 if (($level < 0) OR ($level > 3)) {
2496 return 0;
2497 }
2498 return $this->formatInfo[$level][$mask];
2499 }
2500
2501 /**
2502 * Put a finder pattern.
2503 * @param $frame (array) frame
2504 * @param $ox (int) X center coordinate of the pattern
2505 * @param $oy (int) Y center coordinate of the pattern
2506 * @return array frame
2507 */
2508 protected function putFinderPattern($frame, $ox, $oy) {
2509 $finder = array(
2510 "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
2511 "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
2512 "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2513 "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2514 "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2515 "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
2516 "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
2517 );
2518 for ($y=0; $y < 7; $y++) {
2519 $frame = $this->qrstrset($frame, $ox, ($oy + $y), $finder[$y]);
2520 }
2521 return $frame;
2522 }
2523
2524 /**
2525 * Return a copy of initialized frame.
2526 * @param $version (int) version
2527 * @return Array of unsigned char.
2528 */
2529 protected function createFrame($version) {
2530 $width = $this->capacity[$version][QRCAP_WIDTH];
2531 $frameLine = str_repeat ("\0", $width);
2532 $frame = array_fill(0, $width, $frameLine);
2533 // Finder pattern
2534 $frame = $this->putFinderPattern($frame, 0, 0);
2535 $frame = $this->putFinderPattern($frame, $width - 7, 0);
2536 $frame = $this->putFinderPattern($frame, 0, $width - 7);
2537 // Separator
2538 $yOffset = $width - 7;
2539 for ($y=0; $y < 7; ++$y) {
2540 $frame[$y][7] = "\xc0";
2541 $frame[$y][$width - 8] = "\xc0";
2542 $frame[$yOffset][7] = "\xc0";
2543 ++$yOffset;
2544 }
2545 $setPattern = str_repeat("\xc0", 8);
2546 $frame = $this->qrstrset($frame, 0, 7, $setPattern);
2547 $frame = $this->qrstrset($frame, $width-8, 7, $setPattern);
2548 $frame = $this->qrstrset($frame, 0, $width - 8, $setPattern);
2549 // Format info
2550 $setPattern = str_repeat("\x84", 9);
2551 $frame = $this->qrstrset($frame, 0, 8, $setPattern);
2552 $frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8);
2553 $yOffset = $width - 8;
2554 for ($y=0; $y < 8; ++$y,++$yOffset) {
2555 $frame[$y][8] = "\x84";
2556 $frame[$yOffset][8] = "\x84";
2557 }
2558 // Timing pattern
2559 $wo = $width - 15;
2560 for ($i=1; $i < $wo; ++$i) {
2561 $frame[6][7+$i] = chr(0x90 | ($i & 1));
2562 $frame[7+$i][6] = chr(0x90 | ($i & 1));
2563 }
2564 // Alignment pattern
2565 $frame = $this->putAlignmentPattern($version, $frame, $width);
2566 // Version information
2567 if ($version >= 7) {
2568 $vinf = $this->getVersionPattern($version);
2569 $v = $vinf;
2570 for ($x=0; $x<6; ++$x) {
2571 for ($y=0; $y<3; ++$y) {
2572 $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
2573 $v = $v >> 1;
2574 }
2575 }
2576 $v = $vinf;
2577 for ($y=0; $y<6; ++$y) {
2578 for ($x=0; $x<3; ++$x) {
2579 $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
2580 $v = $v >> 1;
2581 }
2582 }
2583 }
2584 // and a little bit...
2585 $frame[$width - 8][8] = "\x81";
2586 return $frame;
2587 }
2588
2589 /**
2590 * Set new frame for the specified version.
2591 * @param $version (int) version
2592 * @return Array of unsigned char.
2593 */
2594 protected function newFrame($version) {
2595 if (($version < 1) OR ($version > QRSPEC_VERSION_MAX)) {
2596 return NULL;
2597 }
2598 if (!isset($this->frames[$version])) {
2599 $this->frames[$version] = $this->createFrame($version);
2600 }
2601 if (is_null($this->frames[$version])) {
2602 return NULL;
2603 }
2604 return $this->frames[$version];
2605 }
2606
2607 /**
2608 * Return block number 0
2609 * @param $spec (array)
2610 * @return int value
2611 */
2612 protected function rsBlockNum($spec) {
2613 return ($spec[0] + $spec[3]);
2614 }
2615
2616 /**
2617 * Return block number 1
2618 * @param $spec (array)
2619 * @return int value
2620 */
2621 protected function rsBlockNum1($spec) {
2622 return $spec[0];
2623 }
2624
2625 /**
2626 * Return data codes 1
2627 * @param $spec (array)
2628 * @return int value
2629 */
2630 protected function rsDataCodes1($spec) {
2631 return $spec[1];
2632 }
2633
2634 /**
2635 * Return ecc codes 1
2636 * @param $spec (array)
2637 * @return int value
2638 */
2639 protected function rsEccCodes1($spec) {
2640 return $spec[2];
2641 }
2642
2643 /**
2644 * Return block number 2
2645 * @param $spec (array)
2646 * @return int value
2647 */
2648 protected function rsBlockNum2($spec) {
2649 return $spec[3];
2650 }
2651
2652 /**
2653 * Return data codes 2
2654 * @param $spec (array)
2655 * @return int value
2656 */
2657 protected function rsDataCodes2($spec) {
2658 return $spec[4];
2659 }
2660
2661 /**
2662 * Return ecc codes 2
2663 * @param $spec (array)
2664 * @return int value
2665 */
2666 protected function rsEccCodes2($spec) {
2667 return $spec[2];
2668 }
2669
2670 /**
2671 * Return data length
2672 * @param $spec (array)
2673 * @return int value
2674 */
2675 protected function rsDataLength($spec) {
2676 return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
2677 }
2678
2679 /**
2680 * Return ecc length
2681 * @param $spec (array)
2682 * @return int value
2683 */
2684 protected function rsEccLength($spec) {
2685 return ($spec[0] + $spec[3]) * $spec[2];
2686 }
2687
2688 // - - - - - - - - - - - - - - - - - - - - - - - - -
2689
2690 // QRrs
2691
2692 /**
2693 * Initialize a Reed-Solomon codec and add it to existing rsitems
2694 * @param $symsize (int) symbol size, bits
2695 * @param $gfpoly (int) Field generator polynomial coefficients
2696 * @param $fcr (int) first root of RS code generator polynomial, index form
2697 * @param $prim (int) primitive element to generate polynomial roots
2698 * @param $nroots (int) RS code generator polynomial degree (number of roots)
2699 * @param $pad (int) padding bytes at front of shortened block
2700 * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
2701 */
2702 protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
2703 foreach ($this->rsitems as $rs) {
2704 if (($rs['pad'] != $pad) OR ($rs['nroots'] != $nroots) OR ($rs['mm'] != $symsize)
2705 OR ($rs['gfpoly'] != $gfpoly) OR ($rs['fcr'] != $fcr) OR ($rs['prim'] != $prim)) {
2706 continue;
2707 }
2708 return $rs;
2709 }
2710 $rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
2711 array_unshift($this->rsitems, $rs);
2712 return $rs;
2713 }
2714
2715 // - - - - - - - - - - - - - - - - - - - - - - - - -
2716
2717 // QRrsItem
2718
2719 /**
2720 * modnn
2721 * @param $rs (array) RS values
2722 * @param $x (int) X position
2723 * @return int X osition
2724 */
2725 protected function modnn($rs, $x) {
2726 while ($x >= $rs['nn']) {
2727 $x -= $rs['nn'];
2728 $x = ($x >> $rs['mm']) + ($x & $rs['nn']);
2729 }
2730 return $x;
2731 }
2732
2733 /**
2734 * Initialize a Reed-Solomon codec and returns an array of values.
2735 * @param $symsize (int) symbol size, bits
2736 * @param $gfpoly (int) Field generator polynomial coefficients
2737 * @param $fcr (int) first root of RS code generator polynomial, index form
2738 * @param $prim (int) primitive element to generate polynomial roots
2739 * @param $nroots (int) RS code generator polynomial degree (number of roots)
2740 * @param $pad (int) padding bytes at front of shortened block
2741 * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
2742 */
2743 protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
2744 // Based on Reed solomon encoder by Phil Karn, KA9Q (GNU-LGPLv2)
2745 $rs = null;
2746 // Check parameter ranges
2747 if (($symsize < 0) OR ($symsize > 8)) {
2748 return $rs;
2749 }
2750 if (($fcr < 0) OR ($fcr >= (1<<$symsize))) {
2751 return $rs;
2752 }
2753 if (($prim <= 0) OR ($prim >= (1<<$symsize))) {
2754 return $rs;
2755 }
2756 if (($nroots < 0) OR ($nroots >= (1<<$symsize))) {
2757 return $rs;
2758 }
2759 if (($pad < 0) OR ($pad >= ((1<<$symsize) -1 - $nroots))) {
2760 return $rs;
2761 }
2762 $rs = array();
2763 $rs['mm'] = $symsize;
2764 $rs['nn'] = (1 << $symsize) - 1;
2765 $rs['pad'] = $pad;
2766 $rs['alpha_to'] = array_fill(0, ($rs['nn'] + 1), 0);
2767 $rs['index_of'] = array_fill(0, ($rs['nn'] + 1), 0);
2768 // PHP style macro replacement ;)
2769 $NN =& $rs['nn'];
2770 $A0 =& $NN;
2771 // Generate Galois field lookup tables
2772 $rs['index_of'][0] = $A0; // log(zero) = -inf
2773 $rs['alpha_to'][$A0] = 0; // alpha**-inf = 0
2774 $sr = 1;
2775 for ($i=0; $i<$rs['nn']; ++$i) {
2776 $rs['index_of'][$sr] = $i;
2777 $rs['alpha_to'][$i] = $sr;
2778 $sr <<= 1;
2779 if ($sr & (1 << $symsize)) {
2780 $sr ^= $gfpoly;
2781 }
2782 $sr &= $rs['nn'];
2783 }
2784 if ($sr != 1) {
2785 // field generator polynomial is not primitive!
2786 return NULL;
2787 }
2788 // Form RS code generator polynomial from its roots
2789 $rs['genpoly'] = array_fill(0, ($nroots + 1), 0);
2790 $rs['fcr'] = $fcr;
2791 $rs['prim'] = $prim;
2792 $rs['nroots'] = $nroots;
2793 $rs['gfpoly'] = $gfpoly;
2794 // Find prim-th root of 1, used in decoding
2795 for ($iprim=1; ($iprim % $prim) != 0; $iprim += $rs['nn']) {
2796 ; // intentional empty-body loop!
2797 }
2798 $rs['iprim'] = (int)($iprim / $prim);
2799 $rs['genpoly'][0] = 1;
2800 for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
2801 $rs['genpoly'][$i+1] = 1;
2802 // Multiply rs->genpoly[] by @**(root + x)
2803 for ($j = $i; $j > 0; --$j) {
2804 if ($rs['genpoly'][$j] != 0) {
2805 $rs['genpoly'][$j] = $rs['genpoly'][$j-1] ^ $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][$j]] + $root)];
2806 } else {
2807 $rs['genpoly'][$j] = $rs['genpoly'][$j-1];
2808 }
2809 }
2810 // rs->genpoly[0] can never be zero
2811 $rs['genpoly'][0] = $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][0]] + $root)];
2812 }
2813 // convert rs->genpoly[] to index form for quicker encoding
2814 for ($i = 0; $i <= $nroots; ++$i) {
2815 $rs['genpoly'][$i] = $rs['index_of'][$rs['genpoly'][$i]];
2816 }
2817 return $rs;
2818 }
2819
2820 /**
2821 * Encode a Reed-Solomon codec and returns the parity array
2822 * @param $rs (array) RS values
2823 * @param $data (array) data
2824 * @param $parity (array) parity
2825 * @return parity array
2826 */
2827 protected function encode_rs_char($rs, $data, $parity) {
2828 $MM =& $rs['mm']; // bits per symbol
2829 $NN =& $rs['nn']; // the total number of symbols in a RS block
2830 $ALPHA_TO =& $rs['alpha_to']; // the address of an array of NN elements to convert Galois field elements in index (log) form to polynomial form
2831 $INDEX_OF =& $rs['index_of']; // the address of an array of NN elements to convert Galois field elements in polynomial form to index (log) form
2832 $GENPOLY =& $rs['genpoly']; // an array of NROOTS+1 elements containing the generator polynomial in index form
2833 $NROOTS =& $rs['nroots']; // the number of roots in the RS code generator polynomial, which is the same as the number of parity symbols in a block
2834 $FCR =& $rs['fcr']; // first consecutive root, index form
2835 $PRIM =& $rs['prim']; // primitive element, index form
2836 $IPRIM =& $rs['iprim']; // prim-th root of 1, index form
2837 $PAD =& $rs['pad']; // the number of pad symbols in a block
2838 $A0 =& $NN;
2839 $parity = array_fill(0, $NROOTS, 0);
2840 for ($i=0; $i < ($NN - $NROOTS - $PAD); $i++) {
2841 $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
2842 if ($feedback != $A0) {
2843 // feedback term is non-zero
2844 // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
2845 // always be for the polynomials constructed by init_rs()
2846 $feedback = $this->modnn($rs, $NN - $GENPOLY[$NROOTS] + $feedback);
2847 for ($j=1; $j < $NROOTS; ++$j) {
2848 $parity[$j] ^= $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[($NROOTS - $j)])];
2849 }
2850 }
2851 // Shift
2852 array_shift($parity);
2853 if ($feedback != $A0) {
2854 array_push($parity, $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[0])]);
2855 } else {
2856 array_push($parity, 0);
2857 }
2858 }
2859 return $parity;
2860 }
2861
2862} // end QRcode class
2863
2864//============================================================+
2865// END OF FILE
2866//============================================================+