GenericGF.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common\Reedsolomon;
  18. /**
  19. * <p>This class contains utility methods for performing mathematical operations over
  20. * the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
  21. *
  22. * <p>Throughout this package, elements of the GF are represented as an {@code int}
  23. * for convenience and speed (but at the cost of memory).
  24. * </p>
  25. *
  26. * @author Sean Owen
  27. * @author David Olivier
  28. */
  29. final class GenericGF {
  30. public static $AZTEC_DATA_12;
  31. public static $AZTEC_DATA_10;
  32. public static $AZTEC_DATA_6;
  33. public static $AZTEC_PARAM;
  34. public static $QR_CODE_FIELD_256;
  35. public static $DATA_MATRIX_FIELD_256;
  36. public static $AZTEC_DATA_8;
  37. public static $MAXICODE_FIELD_64;
  38. private $expTable;
  39. private $logTable;
  40. private $zero;
  41. private $one;
  42. private $size;
  43. private $primitive;
  44. private $generatorBase;
  45. public static function Init(){
  46. self::$AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
  47. self::$AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
  48. self::$AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
  49. self::$AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
  50. self::$QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  51. self::$DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
  52. self::$AZTEC_DATA_8 = self::$DATA_MATRIX_FIELD_256;
  53. self::$MAXICODE_FIELD_64 = self::$AZTEC_DATA_6;
  54. }
  55. /**
  56. * Create a representation of GF(size) using the given primitive polynomial.
  57. *
  58. * @param primitive irreducible polynomial whose coefficients are represented by
  59. * the bits of an int, where the least-significant bit represents the constant
  60. * coefficient
  61. * @param size the size of the field
  62. * @param b the factor b in the generator polynomial can be 0- or 1-based
  63. * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  64. * In most cases it should be 1, but for QR code it is 0.
  65. */
  66. public function __construct($primitive, $size, $b) {
  67. $this->primitive = $primitive;
  68. $this->size = $size;
  69. $this->generatorBase = $b;
  70. $this->expTable = array();
  71. $this->logTable =array();
  72. $x = 1;
  73. for ($i = 0; $i < $size; $i++) {
  74. $this->expTable[$i] = $x;
  75. $x *= 2; // we're assuming the generator alpha is 2
  76. if ($x >= $size) {
  77. $x ^= $primitive;
  78. $x &= $size-1;
  79. }
  80. }
  81. for ($i = 0; $i < $size-1; $i++) {
  82. $this->logTable[$this->expTable[$i]] = $i;
  83. }
  84. // logTable[0] == 0 but this should never be used
  85. $this->zero = new GenericGFPoly($this, array(0));
  86. $this->one = new GenericGFPoly($this, array(1));
  87. }
  88. function getZero() {
  89. return $this->zero;
  90. }
  91. function getOne() {
  92. return $this->one;
  93. }
  94. /**
  95. * @return the monomial representing coefficient * x^degree
  96. */
  97. function buildMonomial($degree, $coefficient) {
  98. if ($degree < 0) {
  99. throw new \InvalidArgumentException();
  100. }
  101. if ($coefficient == 0) {
  102. return $this->zero;
  103. }
  104. $coefficients = fill_array(0,$degree+1,0);//new int[degree + 1];
  105. $coefficients[0] = $coefficient;
  106. return new GenericGFPoly($this, $coefficients);
  107. }
  108. /**
  109. * Implements both addition and subtraction -- they are the same in GF(size).
  110. *
  111. * @return sum/difference of a and b
  112. */
  113. static function addOrSubtract($a, $b) {
  114. return $a ^ $b;
  115. }
  116. /**
  117. * @return 2 to the power of a in GF(size)
  118. */
  119. function exp($a) {
  120. return $this->expTable[$a];
  121. }
  122. /**
  123. * @return base 2 log of a in GF(size)
  124. */
  125. function log($a) {
  126. if ($a == 0) {
  127. throw new \InvalidArgumentException();
  128. }
  129. return $this->logTable[$a];
  130. }
  131. /**
  132. * @return multiplicative inverse of a
  133. */
  134. function inverse($a) {
  135. if ($a == 0) {
  136. throw new Exception();
  137. }
  138. return $this->expTable[$this->size - $this->logTable[$a] - 1];
  139. }
  140. /**
  141. * @return product of a and b in GF(size)
  142. */
  143. function multiply($a, $b) {
  144. if ($a == 0 || $b == 0) {
  145. return 0;
  146. }
  147. return $this->expTable[($this->logTable[$a] + $this->logTable[$b]) % ($this->size - 1)];
  148. }
  149. public function getSize() {
  150. return $this->size;
  151. }
  152. public function getGeneratorBase() {
  153. return $this->generatorBase;
  154. }
  155. // @Override
  156. public function toString() {
  157. return "GF(0x" . dechex(intval($this->primitive)) . ',' . $this->size . ')';
  158. }
  159. }
  160. GenericGF::Init();