2 * Licensed under the Apache License, Version 2.0 (the "License");
\r
3 * you may not use this file except in compliance with the License.
\r
4 * You may obtain a copy of the License at
\r
6 * http://www.apache.org/licenses/LICENSE-2.0
\r
8 * Unless required by applicable law or agreed to in writing, software
\r
9 * distributed under the License is distributed on an "AS IS" BASIS,
\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
11 * See the License for the specific language governing permissions and
\r
12 * limitations under the License.
\r
16 namespace com.google.zxing.common.reedsolomon
\r
19 /// <summary> <p>This class contains utility methods for performing mathematical operations over
\r
20 /// the Galois Field GF(256). Operations use the primitive polynomial
\r
21 /// x^8 + x^4 + x^3 + x^2 + 1 in calculations.</p>
\r
23 /// <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>
\r
24 /// for convenience and speed (but at the cost of memory).
\r
25 /// Only the bottom 8 bits are really used.</p>
\r
28 /// <author> srowen@google.com (Sean Owen)
\r
30 public sealed class GF256
\r
32 public static GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1
\r
33 public static GF256 DATA_MATRIX_FIELD = new GF256(0x012D); // x^8 + x^5 + x^3 + x^2 + 1
\r
35 private int[] expTable;
\r
36 private int[] logTable;
\r
37 private GF256Poly zero;
\r
38 private GF256Poly one;
\r
41 * Create a representation of GF(256) using the given primitive polynomial.
\r
43 * @param primitive irreducible polynomial whose coefficients are represented by
\r
44 * the bits of an int, where the least-significant bit represents the constant
\r
47 private GF256(int primitive) {
\r
48 expTable = new int[256];
\r
49 logTable = new int[256];
\r
51 for (int i = 0; i < 256; i++) {
\r
53 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
\r
58 for (int i = 0; i < 255; i++) {
\r
59 logTable[expTable[i]] = i;
\r
61 // logTable[0] == 0 but this should never be used
\r
62 zero = new GF256Poly(this, new int[]{0});
\r
63 one = new GF256Poly(this, new int[]{1});
\r
66 public GF256Poly getZero() {
\r
70 public GF256Poly getOne()
\r
76 * @return the monomial representing coefficient * x^degree
\r
78 public GF256Poly buildMonomial(int degree, int coefficient)
\r
81 throw new ArgumentException();
\r
83 if (coefficient == 0) {
\r
86 int[] coefficients = new int[degree + 1];
\r
87 coefficients[0] = coefficient;
\r
88 return new GF256Poly(this, coefficients);
\r
92 * Implements both addition and subtraction -- they are the same in GF(256).
\r
94 * @return sum/difference of a and b
\r
96 public static int addOrSubtract(int a, int b) {
\r
101 * @return 2 to the power of a in GF(256)
\r
103 public int exp(int a)
\r
105 return expTable[a];
\r
109 * @return base 2 log of a in GF(256)
\r
111 public int log(int a)
\r
114 throw new ArgumentException();
\r
116 return logTable[a];
\r
120 * @return multiplicative inverse of a
\r
122 public int inverse(int a)
\r
125 throw new ArithmeticException();
\r
127 return expTable[255 - logTable[a]];
\r
133 * @return product of a and b in GF(256)
\r
135 public int multiply(int a, int b)
\r
137 if (a == 0 || b == 0) {
\r
146 return expTable[(logTable[a] + logTable[b]) % 255];
\r