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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "ZXErrors.h"
  17. #import "ZXGenericGF.h"
  18. #import "ZXGenericGFPoly.h"
  19. #import "ZXIntArray.h"
  20. #import "ZXReedSolomonDecoder.h"
  21. @interface ZXReedSolomonDecoder ()
  22. @property (nonatomic, strong, readonly) ZXGenericGF *field;
  23. @end
  24. @implementation ZXReedSolomonDecoder
  25. - (id)initWithField:(ZXGenericGF *)field {
  26. if (self = [super init]) {
  27. _field = field;
  28. }
  29. return self;
  30. }
  31. - (BOOL)decode:(ZXIntArray *)received twoS:(int)twoS error:(NSError **)error {
  32. ZXGenericGFPoly *poly = [[ZXGenericGFPoly alloc] initWithField:self.field coefficients:received];
  33. ZXIntArray *syndromeCoefficients = [[ZXIntArray alloc] initWithLength:twoS];
  34. BOOL noError = YES;
  35. for (int i = 0; i < twoS; i++) {
  36. int eval = [poly evaluateAt:[self.field exp:i + self.field.generatorBase]];
  37. syndromeCoefficients.array[syndromeCoefficients.length - 1 - i] = eval;
  38. if (eval != 0) {
  39. noError = NO;
  40. }
  41. }
  42. if (noError) {
  43. return YES;
  44. }
  45. ZXGenericGFPoly *syndrome = [[ZXGenericGFPoly alloc] initWithField:self.field coefficients:syndromeCoefficients];
  46. NSArray *sigmaOmega = [self runEuclideanAlgorithm:[self.field buildMonomial:twoS coefficient:1] b:syndrome R:twoS error:error];
  47. if (!sigmaOmega) {
  48. return NO;
  49. }
  50. ZXGenericGFPoly *sigma = sigmaOmega[0];
  51. ZXGenericGFPoly *omega = sigmaOmega[1];
  52. ZXIntArray *errorLocations = [self findErrorLocations:sigma error:error];
  53. if (!errorLocations) {
  54. return NO;
  55. }
  56. ZXIntArray *errorMagnitudes = [self findErrorMagnitudes:omega errorLocations:errorLocations];
  57. for (int i = 0; i < errorLocations.length; i++) {
  58. int position = received.length - 1 - [self.field log:errorLocations.array[i]];
  59. if (position < 0) {
  60. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"Bad error location"};
  61. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXReedSolomonError userInfo:userInfo];
  62. return NO;
  63. }
  64. received.array[position] = [ZXGenericGF addOrSubtract:received.array[position] b:errorMagnitudes.array[i]];
  65. }
  66. return YES;
  67. }
  68. - (NSArray *)runEuclideanAlgorithm:(ZXGenericGFPoly *)a b:(ZXGenericGFPoly *)b R:(int)R error:(NSError **)error {
  69. if (a.degree < b.degree) {
  70. ZXGenericGFPoly *temp = a;
  71. a = b;
  72. b = temp;
  73. }
  74. ZXGenericGFPoly *rLast = a;
  75. ZXGenericGFPoly *r = b;
  76. ZXGenericGFPoly *tLast = self.field.zero;
  77. ZXGenericGFPoly *t = self.field.one;
  78. while ([r degree] >= R / 2) {
  79. ZXGenericGFPoly *rLastLast = rLast;
  80. ZXGenericGFPoly *tLastLast = tLast;
  81. rLast = r;
  82. tLast = t;
  83. if ([rLast zero]) {
  84. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"r_{i-1} was zero"};
  85. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXReedSolomonError userInfo:userInfo];
  86. return nil;
  87. }
  88. r = rLastLast;
  89. ZXGenericGFPoly *q = [self.field zero];
  90. int denominatorLeadingTerm = [rLast coefficient:[rLast degree]];
  91. int dltInverse = [self.field inverse:denominatorLeadingTerm];
  92. while ([r degree] >= [rLast degree] && ![r zero]) {
  93. int degreeDiff = [r degree] - [rLast degree];
  94. int scale = [self.field multiply:[r coefficient:[r degree]] b:dltInverse];
  95. q = [q addOrSubtract:[self.field buildMonomial:degreeDiff coefficient:scale]];
  96. r = [r addOrSubtract:[rLast multiplyByMonomial:degreeDiff coefficient:scale]];
  97. }
  98. t = [[q multiply:tLast] addOrSubtract:tLastLast];
  99. if (r.degree >= rLast.degree) {
  100. @throw [NSException exceptionWithName:@"IllegalStateException"
  101. reason:@"Division algorithm failed to reduce polynomial?"
  102. userInfo:nil];
  103. }
  104. }
  105. int sigmaTildeAtZero = [t coefficient:0];
  106. if (sigmaTildeAtZero == 0) {
  107. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"sigmaTilde(0) was zero"};
  108. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXReedSolomonError userInfo:userInfo];
  109. return nil;
  110. }
  111. int inverse = [self.field inverse:sigmaTildeAtZero];
  112. ZXGenericGFPoly *sigma = [t multiplyScalar:inverse];
  113. ZXGenericGFPoly *omega = [r multiplyScalar:inverse];
  114. return @[sigma, omega];
  115. }
  116. - (ZXIntArray *)findErrorLocations:(ZXGenericGFPoly *)errorLocator error:(NSError **)error {
  117. int numErrors = [errorLocator degree];
  118. if (numErrors == 1) {
  119. ZXIntArray *array = [[ZXIntArray alloc] initWithLength:1];
  120. array.array[0] = [errorLocator coefficient:1];
  121. return array;
  122. }
  123. ZXIntArray *result = [[ZXIntArray alloc] initWithLength:numErrors];
  124. int e = 0;
  125. for (int i = 1; i < [self.field size] && e < numErrors; i++) {
  126. if ([errorLocator evaluateAt:i] == 0) {
  127. result.array[e] = [self.field inverse:i];
  128. e++;
  129. }
  130. }
  131. if (e != numErrors) {
  132. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"Error locator degree does not match number of roots"};
  133. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXReedSolomonError userInfo:userInfo];
  134. return nil;
  135. }
  136. return result;
  137. }
  138. - (ZXIntArray *)findErrorMagnitudes:(ZXGenericGFPoly *)errorEvaluator errorLocations:(ZXIntArray *)errorLocations {
  139. int s = errorLocations.length;
  140. ZXIntArray *result = [[ZXIntArray alloc] initWithLength:s];
  141. ZXGenericGF *field = self.field;
  142. for (int i = 0; i < s; i++) {
  143. int xiInverse = [field inverse:errorLocations.array[i]];
  144. int denominator = 1;
  145. for (int j = 0; j < s; j++) {
  146. if (i != j) {
  147. //denominator = field.multiply(denominator,
  148. // GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
  149. // Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
  150. // Below is a funny-looking workaround from Steven Parkes
  151. int term = [field multiply:errorLocations.array[j] b:xiInverse];
  152. int termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1;
  153. denominator = [field multiply:denominator b:termPlus1];
  154. }
  155. }
  156. result.array[i] = [field multiply:[errorEvaluator evaluateAt:xiInverse] b:[field inverse:denominator]];
  157. if (field.generatorBase != 0) {
  158. result.array[i] = [field multiply:result.array[i] b:xiInverse];
  159. }
  160. }
  161. return result;
  162. }
  163. @end