Test Failed
Push — main ( c8394f...8477f1 )
by Rafael
66:21
created

Datamatrix::lookAheadTest()   F

Complexity

Conditions 36
Paths 12651

Size

Total Lines 121
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 86
nc 12651
nop 3
dl 0
loc 121
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
//============================================================+
4
// File name   : datamatrix.php
5
// Version     : 1.0.008
6
// Begin       : 2010-06-07
7
// Last Update : 2014-05-06
8
// Author      : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - [email protected]
9
// License     : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
10
// -------------------------------------------------------------------
11
// Copyright (C) 2010-2014  Nicola Asuni - Tecnick.com LTD
12
//
13
// This file is part of TCPDF software library.
14
//
15
// TCPDF is free software: you can redistribute it and/or modify it
16
// under the terms of the GNU Lesser General Public License as
17
// published by the Free Software Foundation, either version 3 of the
18
// License, or (at your option) any later version.
19
//
20
// TCPDF is distributed in the hope that it will be useful, but
21
// WITHOUT ANY WARRANTY; without even the implied warranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23
// See the GNU Lesser General Public License for more details.
24
//
25
// You should have received a copy of the GNU Lesser General Public License
26
// along with TCPDF.  If not, see <http://www.gnu.org/licenses/>.
27
//
28
// See LICENSE.TXT file for more information.
29
// -------------------------------------------------------------------
30
//
31
// DESCRIPTION :
32
//
33
// Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
34
// DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
35
//============================================================+
36
37
/**
38
* @file
39
* Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
40
* DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
41
*
42
* @package com.tecnick.tcpdf
43
* @author Nicola Asuni
44
* @version 1.0.008
45
*/
46
47
// custom definitions
48
if (!defined('DATAMATRIXDEFS')) {
49
50
    /**
51
     * Indicate that definitions for this class are set
52
     */
53
    define('DATAMATRIXDEFS', true);
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
*/
64
define('ENC_ASCII', 0);
65
66
/**
67
* C40 encoding: Upper-case alphanumeric (3/2 bytes per CW)
68
*/
69
define('ENC_C40', 1);
70
71
/**
72
* TEXT encoding: Lower-case alphanumeric (3/2 bytes per CW)
73
*/
74
define('ENC_TXT', 2);
75
76
/**
77
* X12 encoding: ANSI X12 (3/2 byte per CW)
78
*/
79
define('ENC_X12', 3);
80
81
/**
82
* EDIFACT encoding: ASCII character 32 to 94 (4/3 bytes per CW)
83
*/
84
define('ENC_EDF', 4);
85
86
/**
87
* BASE 256 encoding: ASCII character 0 to 255 (1 byte per CW)
88
*/
89
define('ENC_BASE256', 5);
90
91
/**
92
* ASCII extended encoding: ASCII character 128 to 255 (1/2 byte per CW)
93
*/
94
define('ENC_ASCII_EXT', 6);
95
96
/**
97
* ASCII number encoding: ASCII digits (2 bytes per CW)
98
*/
99
define('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
*/
110
class 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 string $code Code to represent using Datamatrix.
233
     * @public
234
     */
235
    public function __construct($code)
236
    {
237
        $barcode_array = array();
238
        if ((is_null($code)) or ($code == '\0') or ($code == '')) {
239
            return false;
240
        }
241
        // get data codewords
242
        $cw = $this->getHighLevelEncoding($code);
243
        // number of data codewords
244
        $nd = count($cw);
245
        // check size
246
        if ($nd > 1558) {
247
            return false;
248
        }
249
        // get minimum required matrix size.
250
        foreach ($this->symbattr as $params) {
251
            if ($params[11] >= $nd) {
252
                break;
253
            }
254
        }
255
        if ($params[11] < $nd) {
256
            // too much data
257
            return false;
258
        } elseif ($params[11] > $nd) {
259
            // add padding
260
            if ((($params[11] - $nd) > 1) and ($cw[($nd - 1)] != 254)) {
261
                if ($this->last_enc == ENC_EDF) {
262
                    // switch to ASCII encoding
263
                    $cw[] = 124;
264
                    ++$nd;
265
                } elseif (($this->last_enc != ENC_ASCII) and ($this->last_enc != ENC_BASE256)) {
266
                    // switch to ASCII encoding
267
                    $cw[] = 254;
268
                    ++$nd;
269
                }
270
            }
271
            if ($params[11] > $nd) {
272
                // add first pad
273
                $cw[] = 129;
274
                ++$nd;
275
                // add remaining pads
276
                for ($i = $nd; $i < $params[11]; ++$i) {
277
                    $cw[] = $this->get253StateCodeword(129, $i);
278
                }
279
            }
280
        }
281
        // add error correction codewords
282
        $cw = $this->getErrorCorrection($cw, $params[13], $params[14], $params[15]);
283
        // initialize empty arrays
284
        $grid = array_fill(0, ($params[2] * $params[3]), 0);
285
        // get placement map
286
        $places = $this->getPlacementMap($params[2], $params[3]);
287
        // fill the grid with data
288
        $grid = array();
289
        $i = 0;
290
        // region data row max index
291
        $rdri = ($params[4] - 1);
292
        // region data column max index
293
        $rdci = ($params[5] - 1);
294
        // for each vertical region
295
        for ($vr = 0; $vr < $params[9]; ++$vr) {
296
            // for each row on region
297
            for ($r = 0; $r < $params[4]; ++$r) {
298
                // get row
299
                $row = (($vr * $params[4]) + $r);
300
                // for each horizontal region
301
                for ($hr = 0; $hr < $params[8]; ++$hr) {
302
                    // for each column on region
303
                    for ($c = 0; $c < $params[5]; ++$c) {
304
                        // get column
305
                        $col = (($hr * $params[5]) + $c);
306
                        // braw bits by case
307
                        if ($r == 0) {
308
                            // top finder pattern
309
                            if ($c % 2) {
310
                                $grid[$row][$col] = 0;
311
                            } else {
312
                                $grid[$row][$col] = 1;
313
                            }
314
                        } elseif ($r == $rdri) {
315
                            // bottom finder pattern
316
                            $grid[$row][$col] = 1;
317
                        } elseif ($c == 0) {
318
                            // left finder pattern
319
                            $grid[$row][$col] = 1;
320
                        } elseif ($c == $rdci) {
321
                            // right finder pattern
322
                            if ($r % 2) {
323
                                $grid[$row][$col] = 1;
324
                            } else {
325
                                $grid[$row][$col] = 0;
326
                            }
327
                        } else { // data bit
328
                            if ($places[$i] < 2) {
329
                                $grid[$row][$col] = $places[$i];
330
                            } else {
331
                                // codeword ID
332
                                $cw_id = (floor($places[$i] / 10) - 1);
333
                                // codeword BIT mask
334
                                $cw_bit = pow(2, (8 - ($places[$i] % 10)));
335
                                $grid[$row][$col] = (($cw[$cw_id] & $cw_bit) == 0) ? 0 : 1;
336
                            }
337
                            ++$i;
338
                        }
339
                    }
340
                }
341
            }
342
        }
343
        $this->barcode_array['num_rows'] = $params[0];
344
        $this->barcode_array['num_cols'] = $params[1];
345
        $this->barcode_array['bcode'] = $grid;
346
    }
347
348
    /**
349
     * Returns a barcode array which is readable by TCPDF
350
     * @return array barcode array readable by TCPDF;
351
     * @public
352
     */
353
    public function getBarcodeArray()
354
    {
355
        return $this->barcode_array;
356
    }
357
358
    /**
359
     * Product of two numbers in a Power-of-Two Galois Field
360
     * @param int $a first number to multiply.
361
     * @param int $b second number to multiply.
362
     * @param array $log Log table.
363
     * @param array $alog Anti-Log table.
364
     * @param int $gf Number of Factors of the Reed-Solomon polynomial.
365
     * @return int product
366
     * @protected
367
     */
368
    protected function getGFProduct($a, $b, $log, $alog, $gf)
369
    {
370
        if (($a == 0) or ($b == 0)) {
371
            return 0;
372
        }
373
        return ($alog[($log[$a] + $log[$b]) % ($gf - 1)]);
374
    }
375
376
    /**
377
     * Add error correction codewords to data codewords array (ANNEX E).
378
     * @param array $wd Array of datacodewords.
379
     * @param int $nb Number of blocks.
380
     * @param int $nd Number of data codewords per block.
381
     * @param int $nc Number of correction codewords per block.
382
     * @param int $gf numner of fields on log/antilog table (power of 2).
383
     * @param int $pp The value of its prime modulus polynomial (301 for ECC200).
384
     * @return array data codewords + error codewords
385
     * @protected
386
     */
387
    protected function getErrorCorrection($wd, $nb, $nd, $nc, $gf = 256, $pp = 301)
388
    {
389
        // generate the log ($log) and antilog ($alog) tables
390
        $log[0] = 0;
391
        $alog[0] = 1;
392
        for ($i = 1; $i < $gf; ++$i) {
393
            $alog[$i] = ($alog[($i - 1)] * 2);
394
            if ($alog[$i] >= $gf) {
395
                $alog[$i] ^= $pp;
396
            }
397
            $log[$alog[$i]] = $i;
398
        }
399
        ksort($log);
400
        // generate the polynomial coefficients (c)
401
        $c = array_fill(0, ($nc + 1), 0);
402
        $c[0] = 1;
403
        for ($i = 1; $i <= $nc; ++$i) {
404
            $c[$i] = $c[($i - 1)];
405
            for ($j = ($i - 1); $j >= 1; --$j) {
406
                $c[$j] = $c[($j - 1)] ^ $this->getGFProduct($c[$j], $alog[$i], $log, $alog, $gf);
407
            }
408
            $c[0] = $this->getGFProduct($c[0], $alog[$i], $log, $alog, $gf);
409
        }
410
        ksort($c);
411
        // total number of data codewords
412
        $num_wd = ($nb * $nd);
413
        // total number of error codewords
414
        $num_we = ($nb * $nc);
415
        // for each block
416
        for ($b = 0; $b < $nb; ++$b) {
417
            // create interleaved data block
418
            $block = array();
419
            for ($n = $b; $n < $num_wd; $n += $nb) {
420
                $block[] = $wd[$n];
421
            }
422
            // initialize error codewords
423
            $we = array_fill(0, ($nc + 1), 0);
424
            // calculate error correction codewords for this block
425
            for ($i = 0; $i < $nd; ++$i) {
426
                $k = ($we[0] ^ $block[$i]);
427
                for ($j = 0; $j < $nc; ++$j) {
428
                    $we[$j] = ($we[($j + 1)] ^ $this->getGFProduct($k, $c[($nc - $j - 1)], $log, $alog, $gf));
429
                }
430
            }
431
            // add error codewords at the end of data codewords
432
            $j = 0;
433
            for ($i = $b; $i < $num_we; $i += $nb) {
434
                $wd[($num_wd + $i)] = $we[$j];
435
                ++$j;
436
            }
437
        }
438
        // reorder codewords
439
        ksort($wd);
440
        return $wd;
441
    }
442
443
    /**
444
     * Return the 253-state codeword
445
     * @param int $cwpad Pad codeword.
446
     * @param int $cwpos Number of data codewords from the beginning of encoded data.
447
     * @return int pad codeword
448
     * @protected
449
     */
450
    protected function get253StateCodeword($cwpad, $cwpos)
451
    {
452
        $pad = ($cwpad + (((149 * $cwpos) % 253) + 1));
453
        if ($pad > 254) {
454
            $pad -= 254;
455
        }
456
        return $pad;
457
    }
458
459
    /**
460
     * Return the 255-state codeword
461
     * @param int $cwpad Pad codeword.
462
     * @param int $cwpos Number of data codewords from the beginning of encoded data.
463
     * @return int pad codeword
464
     * @protected
465
     */
466
    protected function get255StateCodeword($cwpad, $cwpos)
467
    {
468
        $pad = ($cwpad + (((149 * $cwpos) % 255) + 1));
469
        if ($pad > 255) {
470
            $pad -= 256;
471
        }
472
        return $pad;
473
    }
474
475
    /**
476
     * Returns true if the char belongs to the selected mode
477
     * @param int $chr Character (byte) to check.
478
     * @param int $mode Current encoding mode.
479
     * @return boolean true if the char is of the selected mode.
480
     * @protected
481
     */
482
    protected function isCharMode($chr, $mode)
483
    {
484
        $status = false;
485
        switch ($mode) {
486
            case ENC_ASCII: { // ASCII character 0 to 127
487
                $status = (($chr >= 0) and ($chr <= 127));
488
                break;
489
            }
490
            case ENC_C40: { // Upper-case alphanumeric
491
                $status = (($chr == 32) or (($chr >= 48) and ($chr <= 57)) or (($chr >= 65) and ($chr <= 90)));
492
                break;
493
            }
494
            case ENC_TXT: { // Lower-case alphanumeric
495
                $status = (($chr == 32) or (($chr >= 48) and ($chr <= 57)) or (($chr >= 97) and ($chr <= 122)));
496
                break;
497
            }
498
            case ENC_X12: { // ANSI X12
499
                $status = (($chr == 13) or ($chr == 42) or ($chr == 62));
500
                break;
501
            }
502
            case ENC_EDF: { // ASCII character 32 to 94
503
                $status = (($chr >= 32) and ($chr <= 94));
504
                break;
505
            }
506
            case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
507
                $status = (($chr == 232) or ($chr == 233) or ($chr == 234) or ($chr == 241));
508
                break;
509
            }
510
            case ENC_ASCII_EXT: { // ASCII character 128 to 255
511
                $status = (($chr >= 128) and ($chr <= 255));
512
                break;
513
            }
514
            case ENC_ASCII_NUM: { // ASCII digits
515
                $status = (($chr >= 48) and ($chr <= 57));
516
                break;
517
            }
518
        }
519
        return $status;
520
    }
521
522
    /**
523
     * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
524
     * @param string $data data to encode
525
     * @param int $pos current position
526
     * @param int $mode current encoding mode
527
     * @return int encoding mode
528
     * @protected
529
     */
530
    protected function lookAheadTest($data, $pos, $mode)
531
    {
532
        $data_length = strlen($data);
533
        if ($pos >= $data_length) {
534
            return $mode;
535
        }
536
        $charscount = 0; // count processed chars
537
        // STEP J
538
        if ($mode == ENC_ASCII) {
539
            $numch = array(0, 1, 1, 1, 1, 1.25);
540
        } else {
541
            $numch = array(1, 2, 2, 2, 2, 2.25);
542
            $numch[$mode] = 0;
543
        }
544
        while (true) {
545
            // STEP K
546
            if (($pos + $charscount) == $data_length) {
547
                if ($numch[ENC_ASCII] <= ceil(min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
548
                    return ENC_ASCII;
549
                }
550
                if ($numch[ENC_BASE256] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
551
                    return ENC_BASE256;
552
                }
553
                if ($numch[ENC_EDF] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256]))) {
554
                    return ENC_EDF;
555
                }
556
                if ($numch[ENC_TXT] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
557
                    return ENC_TXT;
558
                }
559
                if ($numch[ENC_X12] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
560
                    return ENC_X12;
561
                }
562
                return ENC_C40;
563
            }
564
            // get char
565
            $chr = ord($data[$pos + $charscount]);
566
            $charscount++;
567
            // STEP L
568
            if ($this->isCharMode($chr, ENC_ASCII_NUM)) {
569
                $numch[ENC_ASCII] += (1 / 2);
570
            } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
571
                $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
572
                $numch[ENC_ASCII] += 2;
573
            } else {
574
                $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
575
                $numch[ENC_ASCII] += 1;
576
            }
577
            // STEP M
578
            if ($this->isCharMode($chr, ENC_C40)) {
579
                $numch[ENC_C40] += (2 / 3);
580
            } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
581
                $numch[ENC_C40] += (8 / 3);
582
            } else {
583
                $numch[ENC_C40] += (4 / 3);
584
            }
585
            // STEP N
586
            if ($this->isCharMode($chr, ENC_TXT)) {
587
                $numch[ENC_TXT] += (2 / 3);
588
            } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
589
                $numch[ENC_TXT] += (8 / 3);
590
            } else {
591
                $numch[ENC_TXT] += (4 / 3);
592
            }
593
            // STEP O
594
            if ($this->isCharMode($chr, ENC_X12) or $this->isCharMode($chr, ENC_C40)) {
595
                $numch[ENC_X12] += (2 / 3);
596
            } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
597
                $numch[ENC_X12] += (13 / 3);
598
            } else {
599
                $numch[ENC_X12] += (10 / 3);
600
            }
601
            // STEP P
602
            if ($this->isCharMode($chr, ENC_EDF)) {
603
                $numch[ENC_EDF] += (3 / 4);
604
            } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
605
                $numch[ENC_EDF] += (17 / 4);
606
            } else {
607
                $numch[ENC_EDF] += (13 / 4);
608
            }
609
            // STEP Q
610
            if ($this->isCharMode($chr, ENC_BASE256)) {
611
                $numch[ENC_BASE256] += 4;
612
            } else {
613
                $numch[ENC_BASE256] += 1;
614
            }
615
            // STEP R
616
            if ($charscount >= 4) {
617
                if (($numch[ENC_ASCII] + 1) <= min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
618
                    return ENC_ASCII;
619
                }
620
                if (
621
                    (($numch[ENC_BASE256] + 1) <= $numch[ENC_ASCII])
622
                    or (($numch[ENC_BASE256] + 1) < min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))
623
                ) {
624
                    return ENC_BASE256;
625
                }
626
                if (($numch[ENC_EDF] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256])) {
627
                    return ENC_EDF;
628
                }
629
                if (($numch[ENC_TXT] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
630
                    return ENC_TXT;
631
                }
632
                if (($numch[ENC_X12] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
633
                    return ENC_X12;
634
                }
635
                if (($numch[ENC_C40] + 1) < min($numch[ENC_ASCII], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
636
                    if ($numch[ENC_C40] < $numch[ENC_X12]) {
637
                        return ENC_C40;
638
                    }
639
                    if ($numch[ENC_C40] == $numch[ENC_X12]) {
640
                        $k = ($pos + $charscount + 1);
641
                        while ($k < $data_length) {
642
                            $tmpchr = ord($data[$k]);
643
                            if ($this->isCharMode($tmpchr, ENC_X12)) {
644
                                return ENC_X12;
645
                            } elseif (!($this->isCharMode($tmpchr, ENC_X12) or $this->isCharMode($tmpchr, ENC_C40))) {
646
                                break;
647
                            }
648
                            ++$k;
649
                        }
650
                        return ENC_C40;
651
                    }
652
                }
653
            }
654
        } // end of while
655
    }
656
657
    /**
658
     * Get the switching codeword to a new encoding mode (latch codeword)
659
     * @param int $mode New encoding mode.
660
     * @return int Switch codeword.
661
     * @protected
662
     */
663
    protected function getSwitchEncodingCodeword($mode)
664
    {
665
        switch ($mode) {
666
            case ENC_ASCII: { // ASCII character 0 to 127
667
                $cw = 254;
668
                if ($this->last_enc == ENC_EDF) {
669
                    $cw = 124;
670
                }
671
                break;
672
            }
673
            case ENC_C40: { // Upper-case alphanumeric
674
                $cw = 230;
675
                break;
676
            }
677
            case ENC_TXT: { // Lower-case alphanumeric
678
                $cw = 239;
679
                break;
680
            }
681
            case ENC_X12: { // ANSI X12
682
                $cw = 238;
683
                break;
684
            }
685
            case ENC_EDF: { // ASCII character 32 to 94
686
                $cw = 240;
687
                break;
688
            }
689
            case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
690
                $cw = 231;
691
                break;
692
            }
693
        }
694
        return $cw;
695
    }
696
697
    /**
698
     * Choose the minimum matrix size and return the max number of data codewords.
699
     * @param int $numcw Number of current codewords.
700
     * @return int number of data codewords in matrix
701
     * @protected
702
     */
703
    protected function getMaxDataCodewords($numcw)
704
    {
705
        foreach ($this->symbattr as $key => $matrix) {
706
            if ($matrix[11] >= $numcw) {
707
                return $matrix[11];
708
            }
709
        }
710
        return 0;
711
    }
712
713
    /**
714
     * Get high level encoding using the minimum symbol data characters for ECC 200
715
     * @param string $data data to encode
716
     * @return array of codewords
717
     * @protected
718
     */
719
    protected function getHighLevelEncoding($data)
720
    {
721
        // STEP A. Start in ASCII encodation.
722
        $enc = ENC_ASCII; // current encoding mode
723
        $pos = 0; // current position
724
        $cw = array(); // array of codewords to be returned
725
        $cw_num = 0; // number of data codewords
726
        $data_length = strlen($data); // number of chars
727
        while ($pos < $data_length) {
728
            // set last used encoding
729
            $this->last_enc = $enc;
730
            switch ($enc) {
731
                case ENC_ASCII: { // STEP B. While in ASCII encodation
732
                    if (($data_length > 1) and ($pos < ($data_length - 1)) and ($this->isCharMode(ord($data[$pos]), ENC_ASCII_NUM) and $this->isCharMode(ord($data[$pos + 1]), ENC_ASCII_NUM))) {
733
                        // 1. If the next data sequence is at least 2 consecutive digits, encode the next two digits as a double digit in ASCII mode.
734
                        $cw[] = (intval(substr($data, $pos, 2)) + 130);
735
                        ++$cw_num;
736
                        $pos += 2;
737
                    } else {
738
                        // 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
739
                        $newenc = $this->lookAheadTest($data, $pos, $enc);
740
                        if ($newenc != $enc) {
741
                            // switch to new encoding
742
                            $enc = $newenc;
743
                            $cw[] = $this->getSwitchEncodingCodeword($enc);
744
                            ++$cw_num;
745
                        } else {
746
                            // get new byte
747
                            $chr = ord($data[$pos]);
748
                            ++$pos;
749
                            if ($this->isCharMode($chr, ENC_ASCII_EXT)) {
750
                                // 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.
751
                                $cw[] = 235;
752
                                $cw[] = ($chr - 127);
753
                                $cw_num += 2;
754
                            } else {
755
                                // 4. Otherwise process the next data character in ASCII encodation.
756
                                $cw[] = ($chr + 1);
757
                                ++$cw_num;
758
                            }
759
                        }
760
                    }
761
                    break;
762
                }
763
                case ENC_C40:   // Upper-case alphanumeric
764
                case ENC_TXT:   // Lower-case alphanumeric
765
                case ENC_X12 : { // ANSI X12
766
                    $temp_cw = array();
767
                    $p = 0;
768
                    $epos = $pos;
769
                    // get charset ID
770
                    $set_id = $this->chset_id[$enc];
771
                    // get basic charset for current encoding
772
                    $charset = $this->chset[$set_id];
773
                    do {
774
                        // 2. process the next character in C40 encodation.
775
                        $chr = ord($data[$epos]);
776
                        ++$epos;
777
                        // check for extended character
778
                        if ($chr & 0x80) {
779
                            if ($enc == ENC_X12) {
780
                                return false;
781
                            }
782
                            $chr = ($chr & 0x7f);
783
                            $temp_cw[] = 1; // shift 2
784
                            $temp_cw[] = 30; // upper shift
785
                            $p += 2;
786
                        }
787
                        if (isset($charset[$chr])) {
788
                            $temp_cw[] = $charset[$chr];
789
                            ++$p;
790
                        } else {
791
                            if (isset($this->chset['SH1'][$chr])) {
792
                                $temp_cw[] = 0; // shift 1
793
                                $shiftset = $this->chset['SH1'];
794
                            } elseif (isset($this->chset['SH2'][$chr])) {
795
                                $temp_cw[] = 1; // shift 2
796
                                $shiftset = $this->chset['SH2'];
797
                            } elseif (($enc == ENC_C40) and isset($this->chset['S3C'][$chr])) {
798
                                $temp_cw[] = 2; // shift 3
799
                                $shiftset = $this->chset['S3C'];
800
                            } elseif (($enc == ENC_TXT) and isset($this->chset['S3T'][$chr])) {
801
                                $temp_cw[] = 2; // shift 3
802
                                $shiftset = $this->chset['S3T'];
803
                            } else {
804
                                return false;
805
                            }
806
                            $temp_cw[] = $shiftset[$chr];
807
                            $p += 2;
808
                        }
809
                        if ($p >= 3) {
810
                            $c1 = array_shift($temp_cw);
811
                            $c2 = array_shift($temp_cw);
812
                            $c3 = array_shift($temp_cw);
813
                            $p -= 3;
814
                            $tmp = ((1600 * $c1) + (40 * $c2) + $c3 + 1);
815
                            $cw[] = ($tmp >> 8);
816
                            $cw[] = ($tmp % 256);
817
                            $cw_num += 2;
818
                            $pos = $epos;
819
                            // 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.
820
                            $newenc = $this->lookAheadTest($data, $pos, $enc);
821
                            if ($newenc != $enc) {
822
                                // switch to new encoding
823
                                $enc = $newenc;
824
                                if ($enc != ENC_ASCII) {
825
                                    // set unlatch character
826
                                    $cw[] = $this->getSwitchEncodingCodeword(ENC_ASCII);
827
                                    ++$cw_num;
828
                                }
829
                                $cw[] = $this->getSwitchEncodingCodeword($enc);
830
                                ++$cw_num;
831
                                $pos -= $p;
832
                                $p = 0;
833
                                break;
834
                            }
835
                        }
836
                    } while (($p > 0) and ($epos < $data_length));
837
                    // process last data (if any)
838
                    if ($p > 0) {
839
                        // get remaining number of data symbols
840
                        $cwr = ($this->getMaxDataCodewords($cw_num) - $cw_num);
841
                        if (($cwr == 1) and ($p == 1)) {
842
                            // d. If one symbol character remains and one C40 value (data character) remains to be encoded
843
                            $c1 = array_shift($temp_cw);
844
                            --$p;
845
                            $cw[] = ($chr + 1);
846
                            ++$cw_num;
847
                            $pos = $epos;
848
                            $enc = ENC_ASCII;
849
                            $this->last_enc = $enc;
850
                        } elseif (($cwr == 2) and ($p == 1)) {
851
                            // c. If two symbol characters remain and only one C40 value (data character) remains to be encoded
852
                            $c1 = array_shift($temp_cw);
853
                            --$p;
854
                            $cw[] = 254;
855
                            $cw[] = ($chr + 1);
856
                            $cw_num += 2;
857
                            $pos = $epos;
858
                            $enc = ENC_ASCII;
859
                            $this->last_enc = $enc;
860
                        } elseif (($cwr == 2) and ($p == 2)) {
861
                            // b. If two symbol characters remain and two C40 values remain to be encoded
862
                            $c1 = array_shift($temp_cw);
863
                            $c2 = array_shift($temp_cw);
864
                            $p -= 2;
865
                            $tmp = ((1600 * $c1) + (40 * $c2) + 1);
866
                            $cw[] = ($tmp >> 8);
867
                            $cw[] = ($tmp % 256);
868
                            $cw_num += 2;
869
                            $pos = $epos;
870
                            $enc = ENC_ASCII;
871
                            $this->last_enc = $enc;
872
                        } else {
873
                            // switch to ASCII encoding
874
                            if ($enc != ENC_ASCII) {
875
                                $enc = ENC_ASCII;
876
                                $this->last_enc = $enc;
877
                                $cw[] = $this->getSwitchEncodingCodeword($enc);
878
                                ++$cw_num;
879
                                $pos = ($epos - $p);
880
                            }
881
                        }
882
                    }
883
                    break;
884
                }
885
                case ENC_EDF: { // F. While in EDIFACT (EDF) encodation
886
                    // initialize temporary array with 0 length
887
                    $temp_cw = array();
888
                    $epos = $pos;
889
                    $field_length = 0;
890
                    $newenc = $enc;
891
                    do {
892
                        // 2. process the next character in EDIFACT encodation.
893
                        $chr = ord($data[$epos]);
894
                        if ($this->isCharMode($chr, ENC_EDF)) {
895
                            ++$epos;
896
                            $temp_cw[] = $chr;
897
                            ++$field_length;
898
                        }
899
                        if (($field_length == 4) or ($epos == $data_length) or !$this->isCharMode($chr, ENC_EDF)) {
900
                            if (($epos == $data_length) and ($field_length < 3)) {
901
                                $enc = ENC_ASCII;
902
                                $cw[] = $this->getSwitchEncodingCodeword($enc);
903
                                ++$cw_num;
904
                                break;
905
                            }
906
                            if ($field_length < 4) {
907
                                // set unlatch character
908
                                $temp_cw[] = 0x1f;
909
                                ++$field_length;
910
                                // fill empty characters
911
                                for ($i = $field_length; $i < 4; ++$i) {
912
                                    $temp_cw[] = 0;
913
                                }
914
                                $enc = ENC_ASCII;
915
                                $this->last_enc = $enc;
916
                            }
917
                            // encodes four data characters in three codewords
918
                            $tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
919
                            if ($tcw > 0) {
920
                                $cw[] = $tcw;
921
                                $cw_num++;
922
                            }
923
                            $tcw = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
924
                            if ($tcw > 0) {
925
                                $cw[] = $tcw;
926
                                $cw_num++;
927
                            }
928
                            $tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
929
                            if ($tcw > 0) {
930
                                $cw[] = $tcw;
931
                                $cw_num++;
932
                            }
933
                            $temp_cw = array();
934
                            $pos = $epos;
935
                            $field_length = 0;
936
                            if ($enc == ENC_ASCII) {
937
                                break; // exit from EDIFACT mode
938
                            }
939
                        }
940
                    } while ($epos < $data_length);
941
                    break;
942
                }
943
                case ENC_BASE256: { // G. While in Base 256 (B256) encodation
944
                    // initialize temporary array with 0 length
945
                    $temp_cw = array();
946
                    $field_length = 0;
947
                    while (($pos < $data_length) and ($field_length <= 1555)) {
948
                        $newenc = $this->lookAheadTest($data, $pos, $enc);
949
                        if ($newenc != $enc) {
950
                            // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
951
                            $enc = $newenc;
952
                            break; // exit from B256 mode
953
                        } else {
954
                            // 2. Otherwise, process the next character in Base 256 encodation.
955
                            $chr = ord($data[$pos]);
956
                            ++$pos;
957
                            $temp_cw[] = $chr;
958
                            ++$field_length;
959
                        }
960
                    }
961
                    // set field length
962
                    if ($field_length <= 249) {
963
                        $cw[] = $this->get255StateCodeword($field_length, ($cw_num + 1));
964
                        ++$cw_num;
965
                    } else {
966
                        $cw[] = $this->get255StateCodeword((floor($field_length / 250) + 249), ($cw_num + 1));
967
                        $cw[] = $this->get255StateCodeword(($field_length % 250), ($cw_num + 2));
968
                        $cw_num += 2;
969
                    }
970
                    if (!empty($temp_cw)) {
971
                        // add B256 field
972
                        foreach ($temp_cw as $p => $cht) {
973
                            $cw[] = $this->get255StateCodeword($cht, ($cw_num + $p + 1));
974
                        }
975
                    }
976
                    break;
977
                }
978
            } // end of switch enc
979
        } // end of while
980
        return $cw;
981
    }
982
983
    /**
984
     * Places "chr+bit" with appropriate wrapping within array[].
985
     * (Annex F - ECC 200 symbol character placement)
986
     * @param array $marr Array of symbols.
987
     * @param int $nrow Number of rows.
988
     * @param int $ncol Number of columns.
989
     * @param int $row Row number.
990
     * @param int $col Column number.
991
     * @param int $chr Char byte.
992
     * @param int $bit Bit.
993
     * @return array
994
     * @protected
995
     */
996
    protected function placeModule($marr, $nrow, $ncol, $row, $col, $chr, $bit)
997
    {
998
        if ($row < 0) {
999
            $row += $nrow;
1000
            $col += (4 - (($nrow + 4) % 8));
1001
        }
1002
        if ($col < 0) {
1003
            $col += $ncol;
1004
            $row += (4 - (($ncol + 4) % 8));
1005
        }
1006
        $marr[(($row * $ncol) + $col)] = ((10 * $chr) + $bit);
1007
        return $marr;
1008
    }
1009
1010
    /**
1011
     * Places the 8 bits of a utah-shaped symbol character.
1012
     * (Annex F - ECC 200 symbol character placement)
1013
     * @param array $marr Array of symbols.
1014
     * @param int $nrow Number of rows.
1015
     * @param int $ncol Number of columns.
1016
     * @param int $row Row number.
1017
     * @param int $col Column number.
1018
     * @param int $chr Char byte.
1019
     * @return array
1020
     * @protected
1021
     */
1022
    protected function placeUtah($marr, $nrow, $ncol, $row, $col, $chr)
1023
    {
1024
        $marr = $this->placeModule($marr, $nrow, $ncol, $row - 2, $col - 2, $chr, 1);
1025
        $marr = $this->placeModule($marr, $nrow, $ncol, $row - 2, $col - 1, $chr, 2);
1026
        $marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col - 2, $chr, 3);
1027
        $marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col - 1, $chr, 4);
1028
        $marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col, $chr, 5);
1029
        $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col - 2, $chr, 6);
1030
        $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col - 1, $chr, 7);
1031
        $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
1032
        return $marr;
1033
    }
1034
1035
    /**
1036
     * Places the 8 bits of the first special corner case.
1037
     * (Annex F - ECC 200 symbol character placement)
1038
     * @param array $marr Array of symbols.
1039
     * @param int $nrow Number of rows.
1040
     * @param int $ncol Number of columns.
1041
     * @param int $chr Char byte.
1042
     * @return array
1043
     * @protected
1044
     */
1045
    protected function placeCornerA($marr, $nrow, $ncol, $chr)
1046
    {
1047
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 1);
1048
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 1, $chr, 2);
1049
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 2, $chr, 3);
1050
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
1051
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
1052
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 6);
1053
        $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol - 1, $chr, 7);
1054
        $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol - 1, $chr, 8);
1055
        return $marr;
1056
    }
1057
1058
    /**
1059
     * Places the 8 bits of the second special corner case.
1060
     * (Annex F - ECC 200 symbol character placement)
1061
     * @param array $marr Array of symbols.
1062
     * @param int $nrow Number of rows.
1063
     * @param int $ncol Number of columns.
1064
     * @param int $chr Char byte.
1065
     * @return array
1066
     * @protected
1067
     */
1068
    protected function placeCornerB($marr, $nrow, $ncol, $chr)
1069
    {
1070
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 3, 0, $chr, 1);
1071
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 2, 0, $chr, 2);
1072
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 3);
1073
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 4, $chr, 4);
1074
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 3, $chr, 5);
1075
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 6);
1076
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 7);
1077
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 8);
1078
        return $marr;
1079
    }
1080
1081
    /**
1082
     * Places the 8 bits of the third special corner case.
1083
     * (Annex F - ECC 200 symbol character placement)
1084
     * @param array $marr Array of symbols.
1085
     * @param int $nrow Number of rows.
1086
     * @param int $ncol Number of columns.
1087
     * @param int $chr Char byte.
1088
     * @return array
1089
     * @protected
1090
     */
1091
    protected function placeCornerC($marr, $nrow, $ncol, $chr)
1092
    {
1093
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 3, 0, $chr, 1);
1094
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 2, 0, $chr, 2);
1095
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 3);
1096
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
1097
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
1098
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 6);
1099
        $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol - 1, $chr, 7);
1100
        $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol - 1, $chr, 8);
1101
        return $marr;
1102
    }
1103
1104
    /**
1105
     * Places the 8 bits of the fourth special corner case.
1106
     * (Annex F - ECC 200 symbol character placement)
1107
     * @param array $marr Array of symbols.
1108
     * @param int $nrow Number of rows.
1109
     * @param int $ncol Number of columns.
1110
     * @param int $chr Char byte.
1111
     * @return array
1112
     * @protected
1113
     */
1114
    protected function placeCornerD($marr, $nrow, $ncol, $chr)
1115
    {
1116
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 1);
1117
        $marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, $ncol - 1, $chr, 2);
1118
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 3, $chr, 3);
1119
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
1120
        $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
1121
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 3, $chr, 6);
1122
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 2, $chr, 7);
1123
        $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 8);
1124
        return $marr;
1125
    }
1126
1127
    /**
1128
     * Build a placement map.
1129
     * (Annex F - ECC 200 symbol character placement)
1130
     * @param int $nrow Number of rows.
1131
     * @param int $ncol Number of columns.
1132
     * @return array
1133
     * @protected
1134
     */
1135
    protected function getPlacementMap($nrow, $ncol)
1136
    {
1137
        // initialize array with zeros
1138
        $marr = array_fill(0, ($nrow * $ncol), 0);
1139
        // set starting values
1140
        $chr = 1;
1141
        $row = 4;
1142
        $col = 0;
1143
        do {
1144
            // repeatedly first check for one of the special corner cases, then
1145
            if (($row == $nrow) and ($col == 0)) {
1146
                $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr);
1147
                ++$chr;
1148
            }
1149
            if (($row == ($nrow - 2)) and ($col == 0) and ($ncol % 4)) {
1150
                $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr);
1151
                ++$chr;
1152
            }
1153
            if (($row == ($nrow - 2)) and ($col == 0) and (($ncol % 8) == 4)) {
1154
                $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr);
1155
                ++$chr;
1156
            }
1157
            if (($row == ($nrow + 4)) and ($col == 2) and (!($ncol % 8))) {
1158
                $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr);
1159
                ++$chr;
1160
            }
1161
            // sweep upward diagonally, inserting successive characters,
1162
            do {
1163
                if (($row < $nrow) and ($col >= 0) and (!$marr[(($row * $ncol) + $col)])) {
1164
                    $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
1165
                    ++$chr;
1166
                }
1167
                $row -= 2;
1168
                $col += 2;
1169
            } while (($row >= 0) and ($col < $ncol));
1170
            ++$row;
1171
            $col += 3;
1172
            // & then sweep downward diagonally, inserting successive characters,...
1173
            do {
1174
                if (($row >= 0) and ($col < $ncol) and (!$marr[(($row * $ncol) + $col)])) {
1175
                    $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
1176
                    ++$chr;
1177
                }
1178
                $row += 2;
1179
                $col -= 2;
1180
            } while (($row < $nrow) and ($col >= 0));
1181
            $row += 3;
1182
            ++$col;
1183
            // ... until the entire array is scanned
1184
        } while (($row < $nrow) or ($col < $ncol));
1185
        // lastly, if the lower righthand corner is untouched, fill in fixed pattern
1186
        if (!$marr[(($nrow * $ncol) - 1)]) {
1187
            $marr[(($nrow * $ncol) - 1)] = 1;
1188
            $marr[(($nrow * $ncol) - $ncol - 2)] = 1;
1189
        }
1190
        return $marr;
1191
    }
1192
} // end DataMatrix class
1193
//============================================================+
1194
// END OF FILE
1195
//============================================================+
1196