not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function GF256( primitive)
  22. {
  23. this.expTable = new Array(256);
  24. this.logTable = new Array(256);
  25. var x = 1;
  26. for (var i = 0; i < 256; i++)
  27. {
  28. this.expTable[i] = x;
  29. x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
  30. if (x >= 0x100)
  31. {
  32. x ^= primitive;
  33. }
  34. }
  35. for (var i = 0; i < 255; i++)
  36. {
  37. this.logTable[this.expTable[i]] = i;
  38. }
  39. // logTable[0] == 0 but this should never be used
  40. var at0=new Array(1);at0[0]=0;
  41. this.zero = new GF256Poly(this, new Array(at0));
  42. var at1=new Array(1);at1[0]=1;
  43. this.one = new GF256Poly(this, new Array(at1));
  44. this.__defineGetter__("Zero", function()
  45. {
  46. return this.zero;
  47. });
  48. this.__defineGetter__("One", function()
  49. {
  50. return this.one;
  51. });
  52. this.buildMonomial=function( degree, coefficient)
  53. {
  54. if (degree < 0)
  55. {
  56. throw "System.ArgumentException";
  57. }
  58. if (coefficient == 0)
  59. {
  60. return this.zero;
  61. }
  62. var coefficients = new Array(degree + 1);
  63. for(var i=0;i<coefficients.length;i++)coefficients[i]=0;
  64. coefficients[0] = coefficient;
  65. return new GF256Poly(this, coefficients);
  66. }
  67. this.exp=function( a)
  68. {
  69. return this.expTable[a];
  70. }
  71. this.log=function( a)
  72. {
  73. if (a == 0)
  74. {
  75. throw "System.ArgumentException";
  76. }
  77. return this.logTable[a];
  78. }
  79. this.inverse=function( a)
  80. {
  81. if (a == 0)
  82. {
  83. throw "System.ArithmeticException";
  84. }
  85. return this.expTable[255 - this.logTable[a]];
  86. }
  87. this.multiply=function( a, b)
  88. {
  89. if (a == 0 || b == 0)
  90. {
  91. return 0;
  92. }
  93. if (a == 1)
  94. {
  95. return b;
  96. }
  97. if (b == 1)
  98. {
  99. return a;
  100. }
  101. return this.expTable[(this.logTable[a] + this.logTable[b]) % 255];
  102. }
  103. }
  104. GF256.QR_CODE_FIELD = new GF256(0x011D);
  105. GF256.DATA_MATRIX_FIELD = new GF256(0x012D);
  106. GF256.addOrSubtract=function( a, b)
  107. {
  108. return a ^ b;
  109. }