Committed C# port from Mohamad
[zxing.git] / csharp / common / reedsolomon / GF256.cs
diff --git a/csharp/common/reedsolomon/GF256.cs b/csharp/common/reedsolomon/GF256.cs
new file mode 100755 (executable)
index 0000000..f1aaeb3
--- /dev/null
@@ -0,0 +1,151 @@
+/*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+\r
+using System;\r
+namespace com.google.zxing.common.reedsolomon\r
+{\r
+\r
+    /// <summary> <p>This class contains utility methods for performing mathematical operations over\r
+    /// the Galois Field GF(256). Operations use the primitive polynomial\r
+    /// x^8 + x^4 + x^3 + x^2 + 1 in calculations.</p>\r
+    /// \r
+    /// <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>\r
+    /// for convenience and speed (but at the cost of memory).\r
+    /// Only the bottom 8 bits are really used.</p>\r
+    /// \r
+    /// </summary>\r
+    /// <author>  srowen@google.com (Sean Owen)\r
+    /// </author>\r
+    public sealed class GF256\r
+    { \r
+          public static GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1\r
+          public static GF256 DATA_MATRIX_FIELD = new GF256(0x012D); // x^8 + x^5 + x^3 + x^2 + 1\r
+\r
+          private int[] expTable;\r
+          private int[] logTable;\r
+          private GF256Poly zero;\r
+          private GF256Poly one;\r
+\r
+          /**\r
+           * Create a representation of GF(256) using the given primitive polynomial.\r
+           *\r
+           * @param primitive irreducible polynomial whose coefficients are represented by\r
+           *  the bits of an int, where the least-significant bit represents the constant\r
+           *  coefficient\r
+           */\r
+          private GF256(int primitive) {\r
+            expTable = new int[256];\r
+            logTable = new int[256];\r
+            int x = 1;\r
+            for (int i = 0; i < 256; i++) {\r
+              expTable[i] = x;\r
+              x <<= 1; // x = x * 2; we're assuming the generator alpha is 2\r
+              if (x >= 0x100) {\r
+                x ^= primitive;\r
+              }\r
+            }\r
+            for (int i = 0; i < 255; i++) {\r
+              logTable[expTable[i]] = i;\r
+            }\r
+            // logTable[0] == 0 but this should never be used\r
+            zero = new GF256Poly(this, new int[]{0});\r
+            one = new GF256Poly(this, new int[]{1});\r
+          }\r
+\r
+          public GF256Poly getZero() {\r
+            return zero;\r
+          }\r
+\r
+          public GF256Poly getOne()\r
+          {\r
+            return one;\r
+          }\r
+\r
+          /**\r
+           * @return the monomial representing coefficient * x^degree\r
+           */\r
+          public GF256Poly buildMonomial(int degree, int coefficient)\r
+          {\r
+            if (degree < 0) {\r
+              throw new ArgumentException();\r
+            }\r
+            if (coefficient == 0) {\r
+              return zero;\r
+            }\r
+            int[] coefficients = new int[degree + 1];\r
+            coefficients[0] = coefficient;\r
+            return new GF256Poly(this, coefficients);\r
+          }\r
+\r
+          /**\r
+           * Implements both addition and subtraction -- they are the same in GF(256).\r
+           *\r
+           * @return sum/difference of a and b\r
+           */\r
+          public static int addOrSubtract(int a, int b) {\r
+            return a ^ b;\r
+          }\r
+\r
+          /**\r
+           * @return 2 to the power of a in GF(256)\r
+           */\r
+          public int exp(int a)\r
+          {\r
+            return expTable[a];\r
+          }\r
+\r
+          /**\r
+           * @return base 2 log of a in GF(256)\r
+           */\r
+          public int log(int a)\r
+          {\r
+            if (a == 0) {\r
+              throw new ArgumentException();\r
+            }\r
+            return logTable[a];\r
+          }\r
+\r
+          /**\r
+           * @return multiplicative inverse of a\r
+           */\r
+          public int inverse(int a)\r
+          {\r
+            if (a == 0) {\r
+              throw new ArithmeticException();\r
+            }\r
+            return expTable[255 - logTable[a]];\r
+          }\r
+\r
+          /**\r
+           * @param a\r
+           * @param b\r
+           * @return product of a and b in GF(256)\r
+           */\r
+          public int multiply(int a, int b)\r
+          {\r
+            if (a == 0 || b == 0) {\r
+              return 0;\r
+            }\r
+            if (a == 1) {\r
+              return b;\r
+            }\r
+            if (b == 1) {\r
+              return a;\r
+            }\r
+            return expTable[(logTable[a] + logTable[b]) % 255];\r
+          }\r
+    \r
+    \r
+    }\r
+}
\ No newline at end of file