Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZXGenericGF.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2012 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. #import "ZXGenericGF.h"
  17. #import "ZXGenericGFPoly.h"
  18. #import "ZXIntArray.h"
  19. @interface ZXGenericGF ()
  20. @property (nonatomic, assign, readonly) int32_t *expTable;
  21. @property (nonatomic, assign, readonly) int32_t *logTable;
  22. @property (nonatomic, assign, readonly) int primitive;
  23. @end
  24. @implementation ZXGenericGF {
  25. ZXGenericGFPoly *_one;
  26. ZXGenericGFPoly *_zero;
  27. }
  28. - (id)initWithPrimitive:(int)primitive size:(int)size b:(int)b {
  29. if (self = [super init]) {
  30. _primitive = primitive;
  31. _size = size;
  32. _generatorBase = b;
  33. _expTable = (int32_t *)calloc(self.size, sizeof(int32_t));
  34. _logTable = (int32_t *)calloc(self.size, sizeof(int32_t));
  35. int32_t x = 1;
  36. for (int i = 0; i < self.size; i++) {
  37. _expTable[i] = x;
  38. x <<= 1; // we're assuming the generator alpha is 2
  39. if (x >= self.size) {
  40. x ^= (int32_t)self.primitive;
  41. x &= (int32_t)self.size - 1;
  42. }
  43. }
  44. for (int32_t i = 0; i < (int32_t)self.size-1; i++) {
  45. _logTable[_expTable[i]] = i;
  46. }
  47. // logTable[0] == 0 but this should never be used
  48. _zero = [[ZXGenericGFPoly alloc] initWithField:self coefficients:[[ZXIntArray alloc] initWithLength:1]];
  49. _one = [[ZXGenericGFPoly alloc] initWithField:self coefficients:[[ZXIntArray alloc] initWithInts:1, -1]];
  50. }
  51. return self;
  52. }
  53. + (ZXGenericGF *)AztecData12 {
  54. static ZXGenericGF *AztecData12 = nil;
  55. static dispatch_once_t onceToken;
  56. dispatch_once(&onceToken, ^{
  57. AztecData12 = [[ZXGenericGF alloc] initWithPrimitive:0x1069 size:4096 b:1]; // x^12 + x^6 + x^5 + x^3 + 1
  58. });
  59. return AztecData12;
  60. }
  61. + (ZXGenericGF *)AztecData10 {
  62. static ZXGenericGF *AztecData10 = nil;
  63. static dispatch_once_t onceToken;
  64. dispatch_once(&onceToken, ^{
  65. AztecData10 = [[ZXGenericGF alloc] initWithPrimitive:0x409 size:1024 b:1]; // x^10 + x^3 + 1
  66. });
  67. return AztecData10;
  68. }
  69. + (ZXGenericGF *)AztecData6 {
  70. static ZXGenericGF *AztecData6 = nil;
  71. static dispatch_once_t onceToken;
  72. dispatch_once(&onceToken, ^{
  73. AztecData6 = [[ZXGenericGF alloc] initWithPrimitive:0x43 size:64 b:1]; // x^6 + x + 1
  74. });
  75. return AztecData6;
  76. }
  77. + (ZXGenericGF *)AztecParam {
  78. static ZXGenericGF *AztecParam = nil;
  79. static dispatch_once_t onceToken;
  80. dispatch_once(&onceToken, ^{
  81. AztecParam = [[ZXGenericGF alloc] initWithPrimitive:0x13 size:16 b:1]; // x^4 + x + 1
  82. });
  83. return AztecParam;
  84. }
  85. + (ZXGenericGF *)QrCodeField256 {
  86. static ZXGenericGF *QrCodeField256 = nil;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. QrCodeField256 = [[ZXGenericGF alloc] initWithPrimitive:0x011D size:256 b:0]; // x^8 + x^4 + x^3 + x^2 + 1
  90. });
  91. return QrCodeField256;
  92. }
  93. + (ZXGenericGF *)DataMatrixField256 {
  94. static ZXGenericGF *DataMatrixField256 = nil;
  95. static dispatch_once_t onceToken;
  96. dispatch_once(&onceToken, ^{
  97. DataMatrixField256 = [[ZXGenericGF alloc] initWithPrimitive:0x012D size:256 b:1]; // x^8 + x^5 + x^3 + x^2 + 1
  98. });
  99. return DataMatrixField256;
  100. }
  101. + (ZXGenericGF *)AztecData8 {
  102. return [self DataMatrixField256];
  103. }
  104. + (ZXGenericGF *)MaxiCodeField64 {
  105. return [self AztecData6];
  106. }
  107. - (ZXGenericGFPoly *)buildMonomial:(int)degree coefficient:(int32_t)coefficient {
  108. if (degree < 0) {
  109. [NSException raise:NSInvalidArgumentException format:@"Degree must be greater than 0."];
  110. }
  111. if (coefficient == 0) {
  112. return self.zero;
  113. }
  114. ZXIntArray *coefficients = [[ZXIntArray alloc] initWithLength:degree + 1];
  115. coefficients.array[0] = coefficient;
  116. return [[ZXGenericGFPoly alloc] initWithField:self coefficients:coefficients];
  117. }
  118. + (int32_t)addOrSubtract:(int32_t)a b:(int32_t)b {
  119. return a ^ b;
  120. }
  121. - (int32_t)exp:(int)a {
  122. return _expTable[a];
  123. }
  124. - (int32_t)log:(int)a {
  125. if (a == 0) {
  126. [NSException raise:NSInvalidArgumentException format:@"Argument must be non-zero."];
  127. }
  128. return _logTable[a];
  129. }
  130. - (int32_t)inverse:(int)a {
  131. if (a == 0) {
  132. [NSException raise:NSInvalidArgumentException format:@"Argument must be non-zero."];
  133. }
  134. return _expTable[_size - _logTable[a] - 1];
  135. }
  136. - (int32_t)multiply:(int)a b:(int)b {
  137. if (a == 0 || b == 0) {
  138. return 0;
  139. }
  140. return _expTable[(_logTable[a] + _logTable[b]) % (_size - 1)];
  141. }
  142. - (BOOL)isEqual:(ZXGenericGF *)object {
  143. return self.primitive == object.primitive && self.size == object.size;
  144. }
  145. - (NSString *)description {
  146. return [NSString stringWithFormat:@"GF(0x%X,%d)", self.primitive, self.size];
  147. }
  148. @end