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.

ZXModulusGF.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "ZXIntArray.h"
  17. #import "ZXModulusGF.h"
  18. #import "ZXModulusPoly.h"
  19. #import "ZXPDF417Common.h"
  20. @interface ZXModulusGF ()
  21. @property (nonatomic, assign, readonly) int32_t *expTable;
  22. @property (nonatomic, assign, readonly) int32_t *logTable;
  23. @property (nonatomic, assign, readonly) int modulus;
  24. @end
  25. @implementation ZXModulusGF
  26. + (ZXModulusGF *)PDF417_GF {
  27. static dispatch_once_t pred = 0;
  28. __strong static id _mod = nil;
  29. dispatch_once(&pred, ^{
  30. @autoreleasepool {
  31. _mod = [[ZXModulusGF alloc] initWithModulus:ZX_PDF417_NUMBER_OF_CODEWORDS generator:3];
  32. }
  33. });
  34. return _mod;
  35. }
  36. - (id)initWithModulus:(int)modulus generator:(int)generator {
  37. if (self = [super init]) {
  38. _modulus = modulus;
  39. _expTable = (int32_t *)calloc(self.modulus, sizeof(int32_t));
  40. _logTable = (int32_t *)calloc(self.modulus, sizeof(int32_t));
  41. int32_t x = 1;
  42. for (int i = 0; i < modulus; i++) {
  43. _expTable[i] = x;
  44. x = (x * generator) % modulus;
  45. }
  46. for (int i = 0; i < self.size - 1; i++) {
  47. _logTable[_expTable[i]] = i;
  48. }
  49. // logTable[0] == 0 but this should never be used
  50. _zero = [[ZXModulusPoly alloc] initWithField:self coefficients:[[ZXIntArray alloc] initWithLength:1]];
  51. _one = [[ZXModulusPoly alloc] initWithField:self coefficients:[[ZXIntArray alloc] initWithInts:1, -1]];
  52. }
  53. return self;
  54. }
  55. - (ZXModulusPoly *)buildMonomial:(int)degree coefficient:(int)coefficient {
  56. if (degree < 0) {
  57. [NSException raise:NSInvalidArgumentException format:@"Degree must be greater than 0."];
  58. }
  59. if (coefficient == 0) {
  60. return self.zero;
  61. }
  62. ZXIntArray *coefficients = [[ZXIntArray alloc] initWithLength:degree + 1];
  63. coefficients.array[0] = coefficient;
  64. return [[ZXModulusPoly alloc] initWithField:self coefficients:coefficients];
  65. }
  66. - (int)add:(int)a b:(int)b {
  67. return (a + b) % self.modulus;
  68. }
  69. - (int)subtract:(int)a b:(int)b {
  70. return (self.modulus + a - b) % self.modulus;
  71. }
  72. - (int)exp:(int)a {
  73. return _expTable[a];
  74. }
  75. - (int)log:(int)a {
  76. if (a == 0) {
  77. [NSException raise:NSInvalidArgumentException format:@"Argument must be non-zero."];
  78. }
  79. return _logTable[a];
  80. }
  81. - (int)inverse:(int)a {
  82. if (a == 0) {
  83. [NSException raise:NSInvalidArgumentException format:@"Argument must be non-zero."];
  84. }
  85. return _expTable[_modulus - _logTable[a] - 1];
  86. }
  87. - (int)multiply:(int)a b:(int)b {
  88. if (a == 0 || b == 0) {
  89. return 0;
  90. }
  91. return _expTable[(_logTable[a] + _logTable[b]) % (_modulus - 1)];
  92. }
  93. - (int)size {
  94. return self.modulus;
  95. }
  96. @end