Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "ZXBitSource.h"
  17. #import "ZXByteArray.h"
  18. @interface ZXBitSource ()
  19. @property (nonatomic, strong, readonly) ZXByteArray *bytes;
  20. @property (nonatomic, assign) int byteOffset;
  21. @property (nonatomic, assign) int bitOffset;
  22. @end
  23. @implementation ZXBitSource
  24. - (id)initWithBytes:(ZXByteArray *)bytes {
  25. if (self = [super init]) {
  26. _bytes = bytes;
  27. }
  28. return self;
  29. }
  30. - (int)readBits:(int)numBits {
  31. if (numBits < 1 || numBits > 32 || numBits > self.available) {
  32. [NSException raise:NSInvalidArgumentException format:@"Invalid number of bits: %d", numBits];
  33. }
  34. int result = 0;
  35. // First, read remainder from current byte
  36. if (self.bitOffset > 0) {
  37. int bitsLeft = 8 - self.bitOffset;
  38. int toRead = numBits < bitsLeft ? numBits : bitsLeft;
  39. int bitsToNotRead = bitsLeft - toRead;
  40. int mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
  41. result = (self.bytes.array[self.byteOffset] & mask) >> bitsToNotRead;
  42. numBits -= toRead;
  43. self.bitOffset += toRead;
  44. if (self.bitOffset == 8) {
  45. self.bitOffset = 0;
  46. self.byteOffset++;
  47. }
  48. }
  49. // Next read whole bytes
  50. if (numBits > 0) {
  51. while (numBits >= 8) {
  52. result = (result << 8) | (self.bytes.array[self.byteOffset] & 0xFF);
  53. self.byteOffset++;
  54. numBits -= 8;
  55. }
  56. // Finally read a partial byte
  57. if (numBits > 0) {
  58. int bitsToNotRead = 8 - numBits;
  59. int mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
  60. result = (result << numBits) | ((self.bytes.array[self.byteOffset] & mask) >> bitsToNotRead);
  61. self.bitOffset += numBits;
  62. }
  63. }
  64. return result;
  65. }
  66. - (int)available {
  67. return 8 * (self.bytes.length - self.byteOffset) - self.bitOffset;
  68. }
  69. @end