source-class-Com.Tecnick.Barcode.Type.Square.QrCode.Init

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  1: <?php
  2: /**
  3:  * Init.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 12:  *
 13:  * This file is part of tc-lib-barcode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Barcode\Type\Square\QrCode;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\QrCode\Data;
 20: use \Com\Tecnick\Barcode\Type\Square\QrCode\Spec;
 21: 
 22: /**
 23:  * Com\Tecnick\Barcode\Type\Square\QrCode\Init
 24:  *
 25:  * @since       2015-02-21
 26:  * @category    Library
 27:  * @package     Barcode
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 32:  */
 33: abstract class Init extends \Com\Tecnick\Barcode\Type\Square\QrCode\Mask
 34: {
 35:     /**
 36:      * Initialize code
 37:      *
 38:      * @param array $spec Array of ECC specification
 39:      */
 40:     protected function init($spec)
 41:     {
 42:         $dlv = $this->spc->rsDataCodes1($spec);
 43:         $elv = $this->spc->rsEccCodes1($spec);
 44:         $rsv = $this->initRs(8, 0x11d, 0, 1, $elv, 255 - $dlv - $elv);
 45:         $blockNo = 0;
 46:         $dataPos = 0;
 47:         $eccPos = 0;
 48:         $endfor = $this->spc->rsBlockNum1($spec);
 49:         $this->initLoop($endfor, $dlv, $elv, $rsv, $eccPos, $blockNo, $dataPos, $ecc);
 50:         if ($this->spc->rsBlockNum2($spec) == 0) {
 51:             return;
 52:         }
 53:         $dlv = $this->spc->rsDataCodes2($spec);
 54:         $elv = $this->spc->rsEccCodes2($spec);
 55:         $rsv = $this->initRs(8, 0x11d, 0, 1, $elv, 255 - $dlv - $elv);
 56:         if ($rsv == null) {
 57:             throw new BarcodeException('Empty RS');
 58:         }
 59:         $endfor = $this->spc->rsBlockNum2($spec);
 60:         $this->initLoop($endfor, $dlv, $elv, $rsv, $eccPos, $blockNo, $dataPos, $ecc);
 61:     }
 62: 
 63:     /**
 64:      * Internal loop for init
 65:      *
 66:      * @param int $endfor
 67:      * @param int $dlv
 68:      * @param int $elv
 69:      * @param int $rsv
 70:      * @param int $eccPos
 71:      * @param int $blockNo
 72:      * @param int $dataPos
 73:      * @param int $ecc
 74:      */
 75:     protected function initLoop($endfor, $dlv, $elv, $rsv, &$eccPos, &$blockNo, &$dataPos, &$ecc)
 76:     {
 77:         for ($idx = 0; $idx < $endfor; ++$idx) {
 78:             $ecc = array_slice($this->ecccode, $eccPos);
 79:             $this->rsblocks[$blockNo] = array();
 80:             $this->rsblocks[$blockNo]['dataLength'] = $dlv;
 81:             $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
 82:             $this->rsblocks[$blockNo]['eccLength'] = $elv;
 83:             $ecc = $this->encodeRsChar($rsv, $this->rsblocks[$blockNo]['data'], $ecc);
 84:             $this->rsblocks[$blockNo]['ecc'] = $ecc;
 85:             $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
 86:             $dataPos += $dlv;
 87:             $eccPos += $elv;
 88:             $blockNo++;
 89:         }
 90:     }
 91: 
 92:     /**
 93:      * Initialize a Reed-Solomon codec and add it to existing rsitems
 94:      *
 95:      * @param int $symsize Symbol size, bits
 96:      * @param int $gfpoly  Field generator polynomial coefficients
 97:      * @param int $fcr     First root of RS code generator polynomial, index form
 98:      * @param int $prim    Primitive element to generate polynomial roots
 99:      * @param int $nroots  RS code generator polynomial degree (number of roots)
100:      * @param int $pad     Padding bytes at front of shortened block
101:      *
102:      * @return array Array of RS values:
103:      *          mm = Bits per symbol;
104:      *          nn = Symbols per block;
105:      *          alpha_to = log lookup table array;
106:      *          index_of = Antilog lookup table array;
107:      *          genpoly = Generator polynomial array;
108:      *          nroots = Number of generator;
109:      *          roots = number of parity symbols;
110:      *          fcr = First consecutive root, index form;
111:      *          prim = Primitive element, index form;
112:      *          iprim = prim-th root of 1, index form;
113:      *          pad = Padding bytes in shortened block;
114:      *          gfpoly.
115:      */
116:     protected function initRs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
117:     {
118:         foreach ($this->rsitems as $rsv) {
119:             if (($rsv['pad'] != $pad)
120:                 || ($rsv['nroots'] != $nroots)
121:                 || ($rsv['mm'] != $symsize)
122:                 || ($rsv['gfpoly'] != $gfpoly)
123:                 || ($rsv['fcr'] != $fcr)
124:                 || ($rsv['prim'] != $prim)) {
125:                 continue;
126:             }
127:             return $rsv;
128:         }
129:         $rsv = $this->initRsChar($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
130:         array_unshift($this->rsitems, $rsv);
131:         return $rsv;
132:     }
133: 
134:     /**
135:      * modnn
136:      *
137:      * @param array $rsv  RS values
138:      * @param int   $xpos X position
139:      *
140:      * @return int X position
141:      */
142:     protected function modnn($rsv, $xpos)
143:     {
144:         while ($xpos >= $rsv['nn']) {
145:             $xpos -= $rsv['nn'];
146:             $xpos = (($xpos >> $rsv['mm']) + ($xpos & $rsv['nn']));
147:         }
148:         return $xpos;
149:     }
150: 
151:     /**
152:      * Check the params for the initRsChar and throws an exception in case of error.
153:      *
154:      * @param int $symsize Symbol size, bits
155:      * @param int $fcr     First root of RS code generator polynomial, index form
156:      * @param int $prim    Primitive element to generate polynomial roots
157:      *
158:      * @throws BarcodeException in case of error
159:      */
160:     protected function checkRsCharParamsA($symsize, $fcr, $prim)
161:     {
162:         $shfsymsize = (1 << $symsize);
163:         if (($symsize < 0)
164:             || ($symsize > 8)
165:             || ($fcr < 0)
166:             || ($fcr >= $shfsymsize)
167:             || ($prim <= 0)
168:             || ($prim >= $shfsymsize)
169:         ) {
170:             throw new BarcodeException('Invalid parameters');
171:         }
172:     }
173: 
174:     /**
175:      * Check the params for the initRsChar and throws an exception in case of error.
176:      *
177:      * @param int $symsize Symbol size, bits
178:      * @param int $nroots  RS code generator polynomial degree (number of roots)
179:      * @param int $pad     Padding bytes at front of shortened block
180:      *
181:      * @throws BarcodeException in case of error
182:      */
183:     protected function checkRsCharParamsB($symsize, $nroots, $pad)
184:     {
185:         $shfsymsize = (1 << $symsize);
186:         if (($nroots < 0)
187:             || ($nroots >= $shfsymsize)
188:             || ($pad < 0)
189:             || ($pad >= ($shfsymsize - 1 - $nroots))
190:         ) {
191:             throw new BarcodeException('Invalid parameters');
192:         }
193:     }
194:     /**
195:      * Initialize a Reed-Solomon codec and returns an array of values.
196:      *
197:      * @param int $symsize Symbol size, bits
198:      * @param int $gfpoly  Field generator polynomial coefficients
199:      * @param int $fcr     First root of RS code generator polynomial, index form
200:      * @param int $prim    Primitive element to generate polynomial roots
201:      * @param int $nroots  RS code generator polynomial degree (number of roots)
202:      * @param int $pad     Padding bytes at front of shortened block
203:      *
204:      * @return array Array of RS values:
205:      *          mm = Bits per symbol;
206:      *          nn = Symbols per block;
207:      *          alpha_to = log lookup table array;
208:      *          index_of = Antilog lookup table array;
209:      *          genpoly = Generator polynomial array;
210:      *          nroots = Number of generator;
211:      *          roots = number of parity symbols;
212:      *          fcr = First consecutive root, index form;
213:      *          prim = Primitive element, index form;
214:      *          iprim = prim-th root of 1, index form;
215:      *          pad = Padding bytes in shortened block;
216:      *          gfpoly.
217:      */
218:     protected function initRsChar($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
219:     {
220:         $this->checkRsCharParamsA($symsize, $fcr, $prim);
221:         $this->checkRsCharParamsB($symsize, $nroots, $pad);
222:         $rsv = array();
223:         $rsv['mm'] = $symsize;
224:         $rsv['nn'] = ((1 << $symsize) - 1);
225:         $rsv['pad'] = $pad;
226:         $rsv['alpha_to'] = array_fill(0, ($rsv['nn'] + 1), 0);
227:         $rsv['index_of'] = array_fill(0, ($rsv['nn'] + 1), 0);
228:         // PHP style macro replacement
229:         $nnv =& $rsv['nn'];
230:         $azv =& $nnv;
231:         // Generate Galois field lookup tables
232:         $rsv['index_of'][0] = $azv; // log(zero) = -inf
233:         $rsv['alpha_to'][$azv] = 0; // alpha**-inf = 0
234:         $srv = 1;
235:         for ($idx = 0; $idx <$rsv['nn']; ++$idx) {
236:             $rsv['index_of'][$srv] = $idx;
237:             $rsv['alpha_to'][$idx] = $srv;
238:             $srv <<= 1;
239:             if ($srv & (1 << $symsize)) {
240:                 $srv ^= $gfpoly;
241:             }
242:             $srv &= $rsv['nn'];
243:         }
244:         if ($srv != 1) {
245:             throw new BarcodeException('field generator polynomial is not primitive!');
246:         }
247:         // form RS code generator polynomial from its roots
248:         $rsv['genpoly'] = array_fill(0, ($nroots + 1), 0);
249:         $rsv['fcr'] = $fcr;
250:         $rsv['prim'] = $prim;
251:         $rsv['nroots'] = $nroots;
252:         $rsv['gfpoly'] = $gfpoly;
253:         // find prim-th root of 1, used in decoding
254:         for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rsv['nn']) {
255:             ; // intentional empty-body loop!
256:         }
257:         $rsv['iprim'] = (int)($iprim / $prim);
258:         $rsv['genpoly'][0] = 1;
259:         for ($idx = 0, $root = ($fcr * $prim); $idx < $nroots; ++$idx, $root += $prim) {
260:             $rsv['genpoly'][($idx + 1)] = 1;
261:             // multiply rs->genpoly[] by  @**(root + x)
262:             for ($jdx = $idx; $jdx > 0; --$jdx) {
263:                 if ($rsv['genpoly'][$jdx] != 0) {
264:                     $rsv['genpoly'][$jdx] = ($rsv['genpoly'][($jdx - 1)]
265:                         ^ $rsv['alpha_to'][$this->modnn($rsv, $rsv['index_of'][$rsv['genpoly'][$jdx]] + $root)]);
266:                 } else {
267:                     $rsv['genpoly'][$jdx] = $rsv['genpoly'][($jdx - 1)];
268:                 }
269:             }
270:             // rs->genpoly[0] can never be zero
271:             $rsv['genpoly'][0] = $rsv['alpha_to'][$this->modnn($rsv, $rsv['index_of'][$rsv['genpoly'][0]] + $root)];
272:         }
273:         // convert rs->genpoly[] to index form for quicker encoding
274:         for ($idx = 0; $idx <= $nroots; ++$idx) {
275:             $rsv['genpoly'][$idx] = $rsv['index_of'][$rsv['genpoly'][$idx]];
276:         }
277:         return $rsv;
278:     }
279: 
280:     /**
281:      * Encode a Reed-Solomon codec and returns the parity array
282:      *
283:      * @param array $rsv    RS values
284:      * @param array $data   Data
285:      * @param array $parity Parity
286:      *
287:      * @return array Parity array
288:      */
289:     protected function encodeRsChar($rsv, $data, $parity)
290:     {
291:         // the total number of symbols in a RS block
292:         $nnv =& $rsv['nn'];
293:         // the address of an array of NN elements to convert Galois field elements
294:         // in index (log) form to polynomial form
295:         $alphato =& $rsv['alpha_to'];
296:         // the address of an array of NN elements to convert Galois field elements
297:         // in polynomial form to index (log) form
298:         $indexof =& $rsv['index_of'];
299:         // an array of NROOTS+1 elements containing the generator polynomial in index form
300:         $genpoly =& $rsv['genpoly'];
301:         // the number of roots in the RS code generator polynomial,
302:         // which is the same as the number of parity symbols in a block
303:         $nroots =& $rsv['nroots'];
304:         // the number of pad symbols in a block
305:         $pad =& $rsv['pad'];
306:         $azv =& $nnv;
307:         $parity = array_fill(0, $nroots, 0);
308:         for ($idx = 0; $idx < ($nnv - $nroots - $pad); ++$idx) {
309:             $feedback = $indexof[$data[$idx] ^ $parity[0]];
310:             if ($feedback != $azv) {
311:                 // feedback term is non-zero
312:                 // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
313:                 // always be for the polynomials constructed by initRs()
314:                 $feedback = $this->modnn($rsv, ($nnv - $genpoly[$nroots] + $feedback));
315:                 for ($jdx = 1; $jdx < $nroots; ++$jdx) {
316:                     $parity[$jdx] ^= $alphato[$this->modnn($rsv, $feedback + $genpoly[($nroots - $jdx)])];
317:                 }
318:             }
319:             // Shift
320:             array_shift($parity);
321:             if ($feedback != $azv) {
322:                 array_push($parity, $alphato[$this->modnn($rsv, $feedback + $genpoly[0])]);
323:             } else {
324:                 array_push($parity, 0);
325:             }
326:         }
327:         return $parity;
328:     }
329: }
330: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy