GF256.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace FastReport.Barcode.QRCode
  18. {
  19. /*/// <summary> <p>This class contains utility methods for performing mathematical operations over
  20. /// the Galois Field GF(256). Operations use a given primitive polynomial in calculations.</p>
  21. ///
  22. /// <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>
  23. /// for convenience and speed (but at the cost of memory).
  24. /// Only the bottom 8 bits are really used.</p>
  25. ///
  26. /// </summary>
  27. /// <author> Sean Owen
  28. /// </author>
  29. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  30. /// </author>*/
  31. internal sealed class GF256
  32. {
  33. internal GF256Poly Zero
  34. {
  35. get
  36. {
  37. return zero;
  38. }
  39. }
  40. internal GF256Poly One
  41. {
  42. get
  43. {
  44. return one;
  45. }
  46. }
  47. //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'"
  48. public static readonly GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1
  49. //UPGRADE_NOTE: Final was removed from the declaration of 'expTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  50. private int[] expTable;
  51. //UPGRADE_NOTE: Final was removed from the declaration of 'logTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  52. private int[] logTable;
  53. //UPGRADE_NOTE: Final was removed from the declaration of 'zero '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  54. private GF256Poly zero;
  55. //UPGRADE_NOTE: Final was removed from the declaration of 'one '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  56. private GF256Poly one;
  57. /*/// <summary> Create a representation of GF(256) using the given primitive polynomial.
  58. ///
  59. /// </summary>
  60. /// <param name="primitive">irreducible polynomial whose coefficients are represented by
  61. /// the bits of an int, where the least-significant bit represents the constant
  62. /// coefficient
  63. /// </param>*/
  64. private GF256(int primitive)
  65. {
  66. expTable = new int[256];
  67. logTable = new int[256];
  68. int x = 1;
  69. for (int i = 0; i < 256; i++)
  70. {
  71. expTable[i] = x;
  72. x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
  73. if (x >= 0x100)
  74. {
  75. x ^= primitive;
  76. }
  77. }
  78. for (int i = 0; i < 255; i++)
  79. {
  80. logTable[expTable[i]] = i;
  81. }
  82. // logTable[0] == 0 but this should never be used
  83. zero = new GF256Poly(this, new int[]{0});
  84. one = new GF256Poly(this, new int[]{1});
  85. }
  86. /*/// <returns> the monomial representing coefficient * x^degree
  87. /// </returns>*/
  88. internal GF256Poly buildMonomial(int degree, int coefficient)
  89. {
  90. if (degree < 0)
  91. {
  92. throw new System.ArgumentException();
  93. }
  94. if (coefficient == 0)
  95. {
  96. return zero;
  97. }
  98. int[] coefficients = new int[degree + 1];
  99. coefficients[0] = coefficient;
  100. return new GF256Poly(this, coefficients);
  101. }
  102. /*/// <summary> Implements both addition and subtraction -- they are the same in GF(256).
  103. ///
  104. /// </summary>
  105. /// <returns> sum/difference of a and b
  106. /// </returns>*/
  107. internal static int addOrSubtract(int a, int b)
  108. {
  109. return a ^ b;
  110. }
  111. /*/// <returns> 2 to the power of a in GF(256)
  112. /// </returns>*/
  113. internal int exp(int a)
  114. {
  115. return expTable[a];
  116. }
  117. /*/// <returns> base 2 log of a in GF(256)
  118. /// </returns>*/
  119. internal int log(int a)
  120. {
  121. if (a == 0)
  122. {
  123. throw new System.ArgumentException();
  124. }
  125. return logTable[a];
  126. }
  127. /*/// <returns> multiplicative inverse of a
  128. /// </returns>*/
  129. internal int inverse(int a)
  130. {
  131. if (a == 0)
  132. {
  133. throw new System.ArithmeticException();
  134. }
  135. return expTable[255 - logTable[a]];
  136. }
  137. /*/// <param name="a">
  138. /// </param>
  139. /// <param name="b">
  140. /// </param>
  141. /// <returns> product of a and b in GF(256)
  142. /// </returns>*/
  143. internal int multiply(int a, int b)
  144. {
  145. if (a == 0 || b == 0)
  146. {
  147. return 0;
  148. }
  149. if (a == 1)
  150. {
  151. return b;
  152. }
  153. if (b == 1)
  154. {
  155. return a;
  156. }
  157. return expTable[(logTable[a] + logTable[b]) % 255];
  158. }
  159. }
  160. }