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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "ZXByteArray.h"
  17. #import "ZXQRCodeDataBlock.h"
  18. #import "ZXQRCodeErrorCorrectionLevel.h"
  19. #import "ZXQRCodeVersion.h"
  20. @implementation ZXQRCodeDataBlock
  21. - (id)initWithNumDataCodewords:(int)numDataCodewords codewords:(ZXByteArray *)codewords {
  22. if (self = [super init]) {
  23. _numDataCodewords = numDataCodewords;
  24. _codewords = codewords;
  25. }
  26. return self;
  27. }
  28. + (NSArray *)dataBlocks:(ZXByteArray *)rawCodewords version:(ZXQRCodeVersion *)version ecLevel:(ZXQRCodeErrorCorrectionLevel *)ecLevel {
  29. if (rawCodewords.length != version.totalCodewords) {
  30. [NSException raise:NSInvalidArgumentException format:@"Invalid codewords count"];
  31. }
  32. // Figure out the number and size of data blocks used by this version and
  33. // error correction level
  34. ZXQRCodeECBlocks *ecBlocks = [version ecBlocksForLevel:ecLevel];
  35. // First count the total number of data blocks
  36. int totalBlocks = 0;
  37. NSArray *ecBlockArray = ecBlocks.ecBlocks;
  38. for (ZXQRCodeECB *ecBlock in ecBlockArray) {
  39. totalBlocks += ecBlock.count;
  40. }
  41. // Now establish DataBlocks of the appropriate size and number of data codewords
  42. NSMutableArray *result = [NSMutableArray arrayWithCapacity:totalBlocks];
  43. for (ZXQRCodeECB *ecBlock in ecBlockArray) {
  44. for (int i = 0; i < ecBlock.count; i++) {
  45. int numDataCodewords = ecBlock.dataCodewords;
  46. int numBlockCodewords = ecBlocks.ecCodewordsPerBlock + numDataCodewords;
  47. [result addObject:[[ZXQRCodeDataBlock alloc] initWithNumDataCodewords:numDataCodewords codewords:[[ZXByteArray alloc] initWithLength:numBlockCodewords]]];
  48. }
  49. }
  50. // All blocks have the same amount of data, except that the last n
  51. // (where n may be 0) have 1 more byte. Figure out where these start.
  52. int shorterBlocksTotalCodewords = [(ZXQRCodeDataBlock *)result[0] codewords].length;
  53. int longerBlocksStartAt = (int)[result count] - 1;
  54. while (longerBlocksStartAt >= 0) {
  55. int numCodewords = [(ZXQRCodeDataBlock *)result[longerBlocksStartAt] codewords].length;
  56. if (numCodewords == shorterBlocksTotalCodewords) {
  57. break;
  58. }
  59. longerBlocksStartAt--;
  60. }
  61. longerBlocksStartAt++;
  62. int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ecCodewordsPerBlock;
  63. // The last elements of result may be 1 element longer;
  64. // first fill out as many elements as all of them have
  65. int rawCodewordsOffset = 0;
  66. int numResultBlocks = (int)[result count];
  67. for (int i = 0; i < shorterBlocksNumDataCodewords; i++) {
  68. for (int j = 0; j < numResultBlocks; j++) {
  69. [(ZXQRCodeDataBlock *)result[j] codewords].array[i] = rawCodewords.array[rawCodewordsOffset++];
  70. }
  71. }
  72. // Fill out the last data block in the longer ones
  73. for (int j = longerBlocksStartAt; j < numResultBlocks; j++) {
  74. [(ZXQRCodeDataBlock *)result[j] codewords].array[shorterBlocksNumDataCodewords] = rawCodewords.array[rawCodewordsOffset++];
  75. }
  76. // Now add in error correction blocks
  77. int max = (int)[(ZXQRCodeDataBlock *)result[0] codewords].length;
  78. for (int i = shorterBlocksNumDataCodewords; i < max; i++) {
  79. for (int j = 0; j < numResultBlocks; j++) {
  80. int iOffset = j < longerBlocksStartAt ? i : i + 1;
  81. [(ZXQRCodeDataBlock *)result[j] codewords].array[iOffset] = rawCodewords.array[rawCodewordsOffset++];
  82. }
  83. }
  84. return result;
  85. }
  86. @end