qrrscode.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Reed-Solomon error correction support
  6. *
  7. * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
  8. * (libfec is released under the GNU Lesser General Public License.)
  9. *
  10. * Based on libqrencode C library distributed under LGPL 2.1
  11. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  12. *
  13. * PHP QR Code is distributed under LGPL 3
  14. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 3 of the License, or any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with this library; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. class QRrsItem {
  31. public $mm; // Bits per symbol
  32. public $nn; // Symbols per block (= (1<<mm)-1)
  33. public $alpha_to = array(); // log lookup table
  34. public $index_of = array(); // Antilog lookup table
  35. public $genpoly = array(); // Generator polynomial
  36. public $nroots; // Number of generator roots = number of parity symbols
  37. public $fcr; // First consecutive root, index form
  38. public $prim; // Primitive element, index form
  39. public $iprim; // prim-th root of 1, index form
  40. public $pad; // Padding bytes in shortened block
  41. public $gfpoly;
  42. //----------------------------------------------------------------------
  43. public function modnn($x)
  44. {
  45. while ($x >= $this->nn) {
  46. $x -= $this->nn;
  47. $x = ($x >> $this->mm) + ($x & $this->nn);
  48. }
  49. return $x;
  50. }
  51. //----------------------------------------------------------------------
  52. public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
  53. {
  54. // Common code for intializing a Reed-Solomon control block (char or int symbols)
  55. // Copyright 2004 Phil Karn, KA9Q
  56. // May be used under the terms of the GNU Lesser General Public License (LGPL)
  57. $rs = null;
  58. // Check parameter ranges
  59. if($symsize < 0 || $symsize > 8) return $rs;
  60. if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs;
  61. if($prim <= 0 || $prim >= (1<<$symsize)) return $rs;
  62. if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values!
  63. if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding
  64. $rs = new QRrsItem();
  65. $rs->mm = $symsize;
  66. $rs->nn = (1<<$symsize)-1;
  67. $rs->pad = $pad;
  68. $rs->alpha_to = array_fill(0, $rs->nn+1, 0);
  69. $rs->index_of = array_fill(0, $rs->nn+1, 0);
  70. // PHP style macro replacement ;)
  71. $NN =& $rs->nn;
  72. $A0 =& $NN;
  73. // Generate Galois field lookup tables
  74. $rs->index_of[0] = $A0; // log(zero) = -inf
  75. $rs->alpha_to[$A0] = 0; // alpha**-inf = 0
  76. $sr = 1;
  77. for($i=0; $i<$rs->nn; $i++) {
  78. $rs->index_of[$sr] = $i;
  79. $rs->alpha_to[$i] = $sr;
  80. $sr <<= 1;
  81. if($sr & (1<<$symsize)) {
  82. $sr ^= $gfpoly;
  83. }
  84. $sr &= $rs->nn;
  85. }
  86. if($sr != 1){
  87. // field generator polynomial is not primitive!
  88. $rs = NULL;
  89. return $rs;
  90. }
  91. /* Form RS code generator polynomial from its roots */
  92. $rs->genpoly = array_fill(0, $nroots+1, 0);
  93. $rs->fcr = $fcr;
  94. $rs->prim = $prim;
  95. $rs->nroots = $nroots;
  96. $rs->gfpoly = $gfpoly;
  97. /* Find prim-th root of 1, used in decoding */
  98. for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn)
  99. ; // intentional empty-body loop!
  100. $rs->iprim = (int)($iprim / $prim);
  101. $rs->genpoly[0] = 1;
  102. for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
  103. $rs->genpoly[$i+1] = 1;
  104. // Multiply rs->genpoly[] by @**(root + x)
  105. for ($j = $i; $j > 0; $j--) {
  106. if ($rs->genpoly[$j] != 0) {
  107. $rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  108. } else {
  109. $rs->genpoly[$j] = $rs->genpoly[$j-1];
  110. }
  111. }
  112. // rs->genpoly[0] can never be zero
  113. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  114. }
  115. // convert rs->genpoly[] to index form for quicker encoding
  116. for ($i = 0; $i <= $nroots; $i++)
  117. $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  118. return $rs;
  119. }
  120. //----------------------------------------------------------------------
  121. public function encode_rs_char($data, &$parity)
  122. {
  123. $MM =& $this->mm;
  124. $NN =& $this->nn;
  125. $ALPHA_TO =& $this->alpha_to;
  126. $INDEX_OF =& $this->index_of;
  127. $GENPOLY =& $this->genpoly;
  128. $NROOTS =& $this->nroots;
  129. $FCR =& $this->fcr;
  130. $PRIM =& $this->prim;
  131. $IPRIM =& $this->iprim;
  132. $PAD =& $this->pad;
  133. $A0 =& $NN;
  134. $parity = array_fill(0, $NROOTS, 0);
  135. for($i=0; $i< ($NN-$NROOTS-$PAD); $i++) {
  136. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  137. if($feedback != $A0) {
  138. // feedback term is non-zero
  139. // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
  140. // always be for the polynomials constructed by init_rs()
  141. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  142. for($j=1;$j<$NROOTS;$j++) {
  143. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])];
  144. }
  145. }
  146. // Shift
  147. array_shift($parity);
  148. if($feedback != $A0) {
  149. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  150. } else {
  151. array_push($parity, 0);
  152. }
  153. }
  154. }
  155. }
  156. //##########################################################################
  157. class QRrs {
  158. public static $items = array();
  159. //----------------------------------------------------------------------
  160. public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
  161. {
  162. foreach(self::$items as $rs) {
  163. if($rs->pad != $pad) continue;
  164. if($rs->nroots != $nroots) continue;
  165. if($rs->mm != $symsize) continue;
  166. if($rs->gfpoly != $gfpoly) continue;
  167. if($rs->fcr != $fcr) continue;
  168. if($rs->prim != $prim) continue;
  169. return $rs;
  170. }
  171. $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  172. array_unshift(self::$items, $rs);
  173. return $rs;
  174. }
  175. }