QRrsItem.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * QRrsItem.php
  4. *
  5. * Created by arielferrandini
  6. */
  7. namespace PHPQRCode;
  8. class QRrsItem {
  9. public $mm; // Bits per symbol
  10. public $nn; // Symbols per block (= (1<<mm)-1)
  11. public $alpha_to = array(); // log lookup table
  12. public $index_of = array(); // Antilog lookup table
  13. public $genpoly = array(); // Generator polynomial
  14. public $nroots; // Number of generator roots = number of parity symbols
  15. public $fcr; // First consecutive root, index form
  16. public $prim; // Primitive element, index form
  17. public $iprim; // prim-th root of 1, index form
  18. public $pad; // Padding bytes in shortened block
  19. public $gfpoly;
  20. //----------------------------------------------------------------------
  21. public function modnn($x)
  22. {
  23. while ($x >= $this->nn) {
  24. $x -= $this->nn;
  25. $x = ($x >> $this->mm) + ($x & $this->nn);
  26. }
  27. return $x;
  28. }
  29. //----------------------------------------------------------------------
  30. public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
  31. {
  32. // Common code for intializing a Reed-Solomon control block (char or int symbols)
  33. // Copyright 2004 Phil Karn, KA9Q
  34. // May be used under the terms of the GNU Lesser General Public License (LGPL)
  35. $rs = null;
  36. // Check parameter ranges
  37. if($symsize < 0 || $symsize > 8) return $rs;
  38. if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs;
  39. if($prim <= 0 || $prim >= (1<<$symsize)) return $rs;
  40. if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values!
  41. if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding
  42. $rs = new QRrsItem();
  43. $rs->mm = $symsize;
  44. $rs->nn = (1<<$symsize)-1;
  45. $rs->pad = $pad;
  46. $rs->alpha_to = array_fill(0, $rs->nn+1, 0);
  47. $rs->index_of = array_fill(0, $rs->nn+1, 0);
  48. // PHP style macro replacement ;)
  49. $NN =& $rs->nn;
  50. $A0 =& $NN;
  51. // Generate Galois field lookup tables
  52. $rs->index_of[0] = $A0; // log(zero) = -inf
  53. $rs->alpha_to[$A0] = 0; // alpha**-inf = 0
  54. $sr = 1;
  55. for($i=0; $i<$rs->nn; $i++) {
  56. $rs->index_of[$sr] = $i;
  57. $rs->alpha_to[$i] = $sr;
  58. $sr <<= 1;
  59. if($sr & (1<<$symsize)) {
  60. $sr ^= $gfpoly;
  61. }
  62. $sr &= $rs->nn;
  63. }
  64. if($sr != 1){
  65. // field generator polynomial is not primitive!
  66. $rs = NULL;
  67. return $rs;
  68. }
  69. /* Form RS code generator polynomial from its roots */
  70. $rs->genpoly = array_fill(0, $nroots+1, 0);
  71. $rs->fcr = $fcr;
  72. $rs->prim = $prim;
  73. $rs->nroots = $nroots;
  74. $rs->gfpoly = $gfpoly;
  75. /* Find prim-th root of 1, used in decoding */
  76. for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn)
  77. ; // intentional empty-body loop!
  78. $rs->iprim = (int)($iprim / $prim);
  79. $rs->genpoly[0] = 1;
  80. for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
  81. $rs->genpoly[$i+1] = 1;
  82. // Multiply rs->genpoly[] by @**(root + x)
  83. for ($j = $i; $j > 0; $j--) {
  84. if ($rs->genpoly[$j] != 0) {
  85. $rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  86. } else {
  87. $rs->genpoly[$j] = $rs->genpoly[$j-1];
  88. }
  89. }
  90. // rs->genpoly[0] can never be zero
  91. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  92. }
  93. // convert rs->genpoly[] to index form for quicker encoding
  94. for ($i = 0; $i <= $nroots; $i++)
  95. $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  96. return $rs;
  97. }
  98. //----------------------------------------------------------------------
  99. public function encode_rs_char($data, &$parity)
  100. {
  101. $MM =& $this->mm;
  102. $NN =& $this->nn;
  103. $ALPHA_TO =& $this->alpha_to;
  104. $INDEX_OF =& $this->index_of;
  105. $GENPOLY =& $this->genpoly;
  106. $NROOTS =& $this->nroots;
  107. $FCR =& $this->fcr;
  108. $PRIM =& $this->prim;
  109. $IPRIM =& $this->iprim;
  110. $PAD =& $this->pad;
  111. $A0 =& $NN;
  112. $parity = array_fill(0, $NROOTS, 0);
  113. for($i=0; $i< ($NN-$NROOTS-$PAD); $i++) {
  114. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  115. if($feedback != $A0) {
  116. // feedback term is non-zero
  117. // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
  118. // always be for the polynomials constructed by init_rs()
  119. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  120. for($j=1;$j<$NROOTS;$j++) {
  121. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])];
  122. }
  123. }
  124. // Shift
  125. array_shift($parity);
  126. if($feedback != $A0) {
  127. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  128. } else {
  129. array_push($parity, 0);
  130. }
  131. }
  132. }
  133. }