2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 namespace com.google.zxing.common.reedsolomon
\r
20 /// <summary> <p>This class contains utility methods for performing mathematical operations over
\r
21 /// the Galois Field GF(256). Operations use a given primitive polynomial 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> Sean Owen
\r
30 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
32 public sealed class GF256
\r
34 internal GF256Poly Zero
\r
42 internal GF256Poly One
\r
51 //UPGRADE_NOTE: Final was removed from the declaration of 'QR_CODE_FIELD '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
52 public static readonly GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1
\r
53 //UPGRADE_NOTE: Final was removed from the declaration of 'DATA_MATRIX_FIELD '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
54 public static readonly GF256 DATA_MATRIX_FIELD = new GF256(0x012D); // x^8 + x^5 + x^3 + x^2 + 1
\r
56 //UPGRADE_NOTE: Final was removed from the declaration of 'expTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
57 private int[] expTable;
\r
58 //UPGRADE_NOTE: Final was removed from the declaration of 'logTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
59 private int[] logTable;
\r
60 //UPGRADE_NOTE: Final was removed from the declaration of 'zero '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
61 private GF256Poly zero;
\r
62 //UPGRADE_NOTE: Final was removed from the declaration of 'one '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
63 private GF256Poly one;
\r
65 /// <summary> Create a representation of GF(256) using the given primitive polynomial.
\r
68 /// <param name="primitive">irreducible polynomial whose coefficients are represented by
\r
69 /// the bits of an int, where the least-significant bit represents the constant
\r
72 private GF256(int primitive)
\r
74 expTable = new int[256];
\r
75 logTable = new int[256];
\r
77 for (int i = 0; i < 256; i++)
\r
80 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
\r
86 for (int i = 0; i < 255; i++)
\r
88 logTable[expTable[i]] = i;
\r
90 // logTable[0] == 0 but this should never be used
\r
91 zero = new GF256Poly(this, new int[]{0});
\r
92 one = new GF256Poly(this, new int[]{1});
\r
95 /// <returns> the monomial representing coefficient * x^degree
\r
97 internal GF256Poly buildMonomial(int degree, int coefficient)
\r
101 throw new System.ArgumentException();
\r
103 if (coefficient == 0)
\r
107 int[] coefficients = new int[degree + 1];
\r
108 coefficients[0] = coefficient;
\r
109 return new GF256Poly(this, coefficients);
\r
112 /// <summary> Implements both addition and subtraction -- they are the same in GF(256).
\r
115 /// <returns> sum/difference of a and b
\r
117 internal static int addOrSubtract(int a, int b)
\r
122 /// <returns> 2 to the power of a in GF(256)
\r
124 internal int exp(int a)
\r
126 return expTable[a];
\r
129 /// <returns> base 2 log of a in GF(256)
\r
131 internal int log(int a)
\r
135 throw new System.ArgumentException();
\r
137 return logTable[a];
\r
140 /// <returns> multiplicative inverse of a
\r
142 internal int inverse(int a)
\r
146 throw new System.ArithmeticException();
\r
148 return expTable[255 - logTable[a]];
\r
151 /// <param name="a">
\r
153 /// <param name="b">
\r
155 /// <returns> product of a and b in GF(256)
\r
157 internal int multiply(int a, int b)
\r
159 if (a == 0 || b == 0)
\r
171 return expTable[(logTable[a] + logTable[b]) % 255];
\r