BitSource.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. private $bytes;
  29. private $byteOffset = 0;
  30. private $bitOffset = 0;
  31. /**
  32. * @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
  33. * Bits are read within a byte from most-significant to least-significant bit.
  34. */
  35. public function __construct($bytes) {
  36. $this->bytes = $bytes;
  37. }
  38. /**
  39. * @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
  40. */
  41. public function getBitOffset() {
  42. return $this->bitOffset;
  43. }
  44. /**
  45. * @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
  46. */
  47. public function getByteOffset() {
  48. return $this->byteOffset;
  49. }
  50. /**
  51. * @param numBits number of bits to read
  52. * @return int representing the bits read. The bits will appear as the least-significant
  53. * bits of the int
  54. * @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
  55. */
  56. public function readBits($numBits) {
  57. if ($numBits < 1 || $numBits > 32 || $numBits > $this->available()) {
  58. throw new \InvalidArgumentException(strval($numBits));
  59. }
  60. $result = 0;
  61. // First, read remainder from current byte
  62. if ($this->bitOffset > 0) {
  63. $bitsLeft = 8 - $this->bitOffset;
  64. $toRead = $numBits < $bitsLeft ? $numBits : $bitsLeft;
  65. $bitsToNotRead = $bitsLeft - $toRead;
  66. $mask = (0xFF >> (8 - $toRead)) << $bitsToNotRead;
  67. $result = ($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead;
  68. $numBits -= $toRead;
  69. $this->bitOffset += $toRead;
  70. if ($this->bitOffset == 8) {
  71. $this->bitOffset = 0;
  72. $this->byteOffset++;
  73. }
  74. }
  75. // Next read whole bytes
  76. if ($numBits > 0) {
  77. while ($numBits >= 8) {
  78. $result = ($result << 8) | ($this->bytes[$this->byteOffset] & 0xFF);
  79. $this->byteOffset++;
  80. $numBits -= 8;
  81. }
  82. // Finally read a partial byte
  83. if ($numBits > 0) {
  84. $bitsToNotRead = 8 - $numBits;
  85. $mask = (0xFF >> $bitsToNotRead) << $bitsToNotRead;
  86. $result = ($result << $numBits) | (($this->bytes[$this->byteOffset] & $mask) >> $bitsToNotRead);
  87. $this->bitOffset += $numBits;
  88. }
  89. }
  90. return $result;
  91. }
  92. /**
  93. * @return number of bits that can be read successfully
  94. */
  95. public function available() {
  96. return 8 * (count($this->bytes) - $this->byteOffset) - $this->bitOffset;
  97. }
  98. }