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.

ZXAztecBinaryShiftToken.m 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2014 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 "ZXAztecBinaryShiftToken.h"
  17. #import "ZXBitArray.h"
  18. #import "ZXByteArray.h"
  19. @interface ZXAztecBinaryShiftToken ()
  20. @property (nonatomic, assign, readonly) int16_t binaryShiftStart;
  21. @property (nonatomic, assign, readonly) int16_t binaryShiftByteCount;
  22. @end
  23. @implementation ZXAztecBinaryShiftToken
  24. - (id)initWithPrevious:(ZXAztecToken *)previous binaryShiftStart:(int)binaryShiftStart binaryShiftByteCount:(int)binaryShiftByteCount {
  25. if (self = [super initWithPrevious:previous]) {
  26. _binaryShiftStart = (int16_t) binaryShiftStart;
  27. _binaryShiftByteCount = (int16_t) binaryShiftByteCount;
  28. }
  29. return self;
  30. }
  31. - (void)appendTo:(ZXBitArray *)bitArray text:(ZXByteArray *)text {
  32. for (int i = 0; i < self.binaryShiftByteCount; i++) {
  33. if (i == 0 || (i == 31 && self.binaryShiftByteCount <= 62)) {
  34. // We need a header before the first character, and before
  35. // character 31 when the total byte code is <= 62
  36. [bitArray appendBits:31 numBits:5]; // BINARY_SHIFT
  37. if (self.binaryShiftByteCount > 62) {
  38. [bitArray appendBits:self.binaryShiftByteCount - 31 numBits:16];
  39. } else if (i == 0) {
  40. // 1 <= binaryShiftByteCode <= 62
  41. [bitArray appendBits:MIN(self.binaryShiftByteCount, 31) numBits:5];
  42. } else {
  43. // 32 <= binaryShiftCount <= 62 and i == 31
  44. [bitArray appendBits:self.binaryShiftByteCount - 31 numBits:5];
  45. }
  46. }
  47. [bitArray appendBits:text.array[self.binaryShiftStart + i] numBits:8];
  48. }
  49. }
  50. @end