BitSource.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common;
  18. /**
  19. * <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
  20. * number of bits read is not often a multiple of 8.</p>
  21. *
  22. * <p>This class is thread-safe but not reentrant -- unless the caller modifies the bytes array
  23. * it passed in, in which case all bets are off.</p>
  24. *
  25. * @author Sean Owen
  26. */
  27. final class BitSource
  28. {
  29. private $bytes;
  30. private $byteOffset = 0;
  31. private $bitOffset = 0;
  32. /**
  33. * @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
  34. * Bits are read within a byte from most-significant to least-significant bit.
  35. */
  36. public function __construct($bytes)
  37. {
  38. $this->bytes = $bytes;
  39. }
  40. /**
  41. * @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
  42. */
  43. public function getBitOffset()
  44. {
  45. return $this->bitOffset;
  46. }
  47. /**
  48. * @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
  49. */
  50. public function getByteOffset()
  51. {
  52. return $this->byteOffset;
  53. }
  54. /**
  55. * @param numBits number of bits to read
  56. *
  57. * @return int representing the bits read. The bits will appear as the least-significant
  58. * bits of the int
  59. * @throws InvalidArgumentException if numBits isn't in [1,32] or more than is available
  60. */
  61. public function readBits($numBits)
  62. {
  63. if ($numBits < 1 || $numBits > 32 || $numBits > $this->available()) {
  64. throw new \InvalidArgumentException(strval($numBits));
  65. }
  66. $result = 0;
  67. // First, read remainder from current byte
  68. if ($this->bitOffset > 0) {
  69. $bitsLeft = 8 - $this->bitOffset;
  70. $toRead = $numBits < $bitsLeft ? $numBits : $bitsLeft;
  71. $bitsToNotRead = $bitsLeft - $toRead;
  72. $mask = (0xFF >> (8 - $toRead)) << $bitsToNotRead;
  73. $result = ($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead;
  74. $numBits -= $toRead;
  75. $this->bitOffset += $toRead;
  76. if ($this->bitOffset == 8) {
  77. $this->bitOffset = 0;
  78. $this->byteOffset++;
  79. }
  80. }
  81. // Next read whole bytes
  82. if ($numBits > 0) {
  83. while ($numBits >= 8) {
  84. $result = ($result << 8) | ($this->bytes[$this->byteOffset] & 0xFF);
  85. $this->byteOffset++;
  86. $numBits -= 8;
  87. }
  88. // Finally read a partial byte
  89. if ($numBits > 0) {
  90. $bitsToNotRead = 8 - $numBits;
  91. $mask = (0xFF >> $bitsToNotRead) << $bitsToNotRead;
  92. $result = ($result << $numBits) | (($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead);
  93. $this->bitOffset += $numBits;
  94. }
  95. }
  96. return $result;
  97. }
  98. /**
  99. * @return number of bits that can be read successfully
  100. */
  101. public function available()
  102. {
  103. return 8 * (count($this->bytes) - $this->byteOffset) - $this->bitOffset;
  104. }
  105. }