1 | <?php |
||
10 | class BitReader extends StringReader |
||
11 | { |
||
12 | /** |
||
13 | * The maximum size of the buffer in bits. |
||
14 | * |
||
15 | * @var int |
||
16 | */ |
||
17 | const BUFFER_BITS = 32; |
||
18 | |||
19 | /** |
||
20 | * Pre-calculated mask to make sure we get. |
||
21 | * |
||
22 | * @var int[] |
||
23 | */ |
||
24 | protected static $UNSIGNED_MASK = array( |
||
25 | 0x0000, |
||
26 | 0x0001, 0x0003, 0x0007, 0x000f, |
||
27 | 0x001f, 0x003f, 0x007f, 0x00ff, |
||
28 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, |
||
29 | 0x1fff, 0x3fff, 0x7fff, 0xffff, |
||
30 | ); |
||
31 | |||
32 | /** |
||
33 | * The current bit buffer. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $buffer; |
||
38 | |||
39 | /** |
||
40 | * The number of bits in the buffer. |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $bufferSize; |
||
45 | |||
46 | /** |
||
47 | * Initializes the instance. |
||
48 | * |
||
49 | * @param string $string The raw string containing the data to be read. |
||
50 | */ |
||
51 | 23 | public function __construct($string) |
|
57 | |||
58 | /** |
||
59 | * Make sure that there are at least $n (<=16) bits in the buffer. |
||
60 | * If less than $n bits are there, read a 16-bit little-endian word from the byte array. |
||
61 | * |
||
62 | * @param int $n The minimum number of bits that should be available in the remaining bit buffer. |
||
63 | * |
||
64 | * @return int Return the number of remaining bits in the buffer. |
||
65 | */ |
||
66 | 23 | public function ensure($n) |
|
67 | { |
||
68 | 23 | while ($this->bufferSize < $n) { |
|
69 | 23 | if (($this->length - $this->position) < 2) { |
|
70 | 20 | $this->position = $this->length; |
|
71 | 20 | break; |
|
72 | } |
||
73 | 23 | $word = $this->readUInt16(); |
|
74 | 23 | $this->buffer |= $word << (static::BUFFER_BITS - 16 - $this->bufferSize); |
|
75 | 23 | $this->bufferSize += 16; |
|
76 | 23 | } |
|
77 | |||
78 | 23 | return $this->bufferSize; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Peek n bits, may raise an EOF Exception if there are not enough bits and $suppressException is false. |
||
83 | * |
||
84 | * @param int $n The number of bits to peek. |
||
85 | * @param bool $suppressException Set to true to avoid raising an exception if there are not enough bits. |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | 23 | public function peek($n, $suppressException = false) |
|
90 | { |
||
91 | 23 | if ($this->ensure($n) < $n) { |
|
92 | 20 | if (!$suppressException) { |
|
93 | throw new Exception('EOF'); |
||
94 | } |
||
95 | 20 | } |
|
96 | |||
97 | 23 | return (($this->buffer >> (static::BUFFER_BITS - $n))) & static::$UNSIGNED_MASK[$n]; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Read no more than 16 bits (represented as a little endian integer). |
||
102 | * |
||
103 | * @param int $n The number of bits to retrieve. |
||
104 | * |
||
105 | * @return int |
||
106 | */ |
||
107 | 23 | public function readLE($n) |
|
115 | |||
116 | /** |
||
117 | * Reset the bits buffer and flush $n bytes. |
||
118 | * |
||
119 | * @param int $n The number of bytes to skip. |
||
120 | * |
||
121 | * @throws Exception Throws an Exception in case of errors. |
||
122 | */ |
||
123 | public function skip($n) |
||
129 | |||
130 | /** |
||
131 | * Read a number of bytes and insert them in an array at a specified position. |
||
132 | * |
||
133 | * @param array $buffer The buffer where the data should be inserted. |
||
134 | * @param int $offset The offset at which the data should be inserted. |
||
135 | * @param int $length The number of bytes to read. |
||
136 | * |
||
137 | * @return array Return the $buffer array with filled-in bytes. |
||
138 | * |
||
139 | * @throws Exception Throws an Exception in case of errors. |
||
140 | */ |
||
141 | public function readFully($buffer, $offset, $length) |
||
159 | } |
||
160 |