/zxing-objc/ZXingObjC/common/reedsolomon/ZXReedSolomonEncoder.m

https://gitlab.com/gavins-Zhang/shadowsocks-iOS · Objective C · 85 lines · 59 code · 11 blank · 15 comment · 9 complexity · 1b44ad7a336be56d8e8a6781b0b657fb MD5 · raw file

  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 "ZXReedSolomonEncoder.h"
  19. @interface ZXReedSolomonEncoder ()
  20. @property (nonatomic, strong) NSMutableArray *cachedGenerators;
  21. @property (nonatomic, strong) ZXGenericGF *field;
  22. @end
  23. @implementation ZXReedSolomonEncoder
  24. - (id)initWithField:(ZXGenericGF *)field {
  25. if (self = [super init]) {
  26. _field = field;
  27. int one = 1;
  28. _cachedGenerators = [NSMutableArray arrayWithObject:[[ZXGenericGFPoly alloc] initWithField:field coefficients:&one coefficientsLen:1]];
  29. }
  30. return self;
  31. }
  32. - (ZXGenericGFPoly *)buildGenerator:(int)degree {
  33. if (degree >= self.cachedGenerators.count) {
  34. ZXGenericGFPoly *lastGenerator = self.cachedGenerators[[self.cachedGenerators count] - 1];
  35. for (NSUInteger d = [self.cachedGenerators count]; d <= degree; d++) {
  36. int next[2] = { 1, [self.field exp:(int)d - 1 + self.field.generatorBase] };
  37. ZXGenericGFPoly *nextGenerator = [lastGenerator multiply:[[ZXGenericGFPoly alloc] initWithField:self.field coefficients:next coefficientsLen:2]];
  38. [self.cachedGenerators addObject:nextGenerator];
  39. lastGenerator = nextGenerator;
  40. }
  41. }
  42. return (ZXGenericGFPoly *)self.cachedGenerators[degree];
  43. }
  44. - (void)encode:(int *)toEncode toEncodeLen:(int)toEncodeLen ecBytes:(int)ecBytes {
  45. if (ecBytes == 0) {
  46. @throw [NSException exceptionWithName:NSInvalidArgumentException
  47. reason:@"No error correction bytes"
  48. userInfo:nil];
  49. }
  50. int dataBytes = toEncodeLen - ecBytes;
  51. if (dataBytes <= 0) {
  52. @throw [NSException exceptionWithName:NSInvalidArgumentException
  53. reason:@"No data bytes provided"
  54. userInfo:nil];
  55. }
  56. ZXGenericGFPoly *generator = [self buildGenerator:ecBytes];
  57. int infoCoefficients[dataBytes];
  58. for (int i = 0; i < dataBytes; i++) {
  59. infoCoefficients[i] = toEncode[i];
  60. }
  61. ZXGenericGFPoly *info = [[ZXGenericGFPoly alloc] initWithField:self.field coefficients:infoCoefficients coefficientsLen:dataBytes];
  62. info = [info multiplyByMonomial:ecBytes coefficient:1];
  63. ZXGenericGFPoly *remainder = [info divide:generator][1];
  64. int *coefficients = remainder.coefficients;
  65. int coefficientsLen = remainder.coefficientsLen;
  66. int numZeroCoefficients = ecBytes - coefficientsLen;
  67. for (int i = 0; i < numZeroCoefficients; i++) {
  68. toEncode[dataBytes + i] = 0;
  69. }
  70. for (int i = 0; i < coefficientsLen; i++) {
  71. toEncode[dataBytes + numZeroCoefficients + i] = coefficients[i];
  72. }
  73. }
  74. @end