国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
8.1KB

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