New C# port from Suraj Supekar
[zxing.git] / csharp / common / reedsolomon / GF256.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\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
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\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
15 */\r
16 using System;\r
17 namespace com.google.zxing.common.reedsolomon\r
18 {\r
19         \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
22         /// \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
26         /// \r
27         /// </summary>\r
28         /// <author>  Sean Owen\r
29         /// </author>\r
30         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
31         /// </author>\r
32         public sealed class GF256\r
33         {\r
34                 internal GF256Poly Zero\r
35                 {\r
36                         get\r
37                         {\r
38                                 return zero;\r
39                         }\r
40                         \r
41                 }\r
42                 internal GF256Poly One\r
43                 {\r
44                         get\r
45                         {\r
46                                 return one;\r
47                         }\r
48                         \r
49                 }\r
50                 \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
55                 \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
64                 \r
65                 /// <summary> Create a representation of GF(256) using the given primitive polynomial.\r
66                 /// \r
67                 /// </summary>\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
70                 /// coefficient\r
71                 /// </param>\r
72                 private GF256(int primitive)\r
73                 {\r
74                         expTable = new int[256];\r
75                         logTable = new int[256];\r
76                         int x = 1;\r
77                         for (int i = 0; i < 256; i++)\r
78                         {\r
79                                 expTable[i] = x;\r
80                                 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2\r
81                                 if (x >= 0x100)\r
82                                 {\r
83                                         x ^= primitive;\r
84                                 }\r
85                         }\r
86                         for (int i = 0; i < 255; i++)\r
87                         {\r
88                                 logTable[expTable[i]] = i;\r
89                         }\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
93                 }\r
94                 \r
95                 /// <returns> the monomial representing coefficient * x^degree\r
96                 /// </returns>\r
97                 internal GF256Poly buildMonomial(int degree, int coefficient)\r
98                 {\r
99                         if (degree < 0)\r
100                         {\r
101                                 throw new System.ArgumentException();\r
102                         }\r
103                         if (coefficient == 0)\r
104                         {\r
105                                 return zero;\r
106                         }\r
107                         int[] coefficients = new int[degree + 1];\r
108                         coefficients[0] = coefficient;\r
109                         return new GF256Poly(this, coefficients);\r
110                 }\r
111                 \r
112                 /// <summary> Implements both addition and subtraction -- they are the same in GF(256).\r
113                 /// \r
114                 /// </summary>\r
115                 /// <returns> sum/difference of a and b\r
116                 /// </returns>\r
117                 internal static int addOrSubtract(int a, int b)\r
118                 {\r
119                         return a ^ b;\r
120                 }\r
121                 \r
122                 /// <returns> 2 to the power of a in GF(256)\r
123                 /// </returns>\r
124                 internal int exp(int a)\r
125                 {\r
126                         return expTable[a];\r
127                 }\r
128                 \r
129                 /// <returns> base 2 log of a in GF(256)\r
130                 /// </returns>\r
131                 internal int log(int a)\r
132                 {\r
133                         if (a == 0)\r
134                         {\r
135                                 throw new System.ArgumentException();\r
136                         }\r
137                         return logTable[a];\r
138                 }\r
139                 \r
140                 /// <returns> multiplicative inverse of a\r
141                 /// </returns>\r
142                 internal int inverse(int a)\r
143                 {\r
144                         if (a == 0)\r
145                         {\r
146                                 throw new System.ArithmeticException();\r
147                         }\r
148                         return expTable[255 - logTable[a]];\r
149                 }\r
150                 \r
151                 /// <param name="a">\r
152                 /// </param>\r
153                 /// <param name="b">\r
154                 /// </param>\r
155                 /// <returns> product of a and b in GF(256)\r
156                 /// </returns>\r
157                 internal int multiply(int a, int b)\r
158                 {\r
159                         if (a == 0 || b == 0)\r
160                         {\r
161                                 return 0;\r
162                         }\r
163                         if (a == 1)\r
164                         {\r
165                                 return b;\r
166                         }\r
167                         if (b == 1)\r
168                         {\r
169                                 return a;\r
170                         }\r
171                         return expTable[(logTable[a] + logTable[b]) % 255];\r
172                 }\r
173         }\r
174 }