BitSource.php 3.3 KB

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