1 | <?php |
||
36 | class Buffer |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * Constants for the byte code types we need to read as |
||
41 | */ |
||
42 | const NUMBER_TYPE_BIGENDIAN = 'be', |
||
43 | NUMBER_TYPE_LITTLEENDIAN = 'le', |
||
44 | NUMBER_TYPE_MACHINE = 'm'; |
||
45 | |||
46 | /** |
||
47 | * The number type we use for reading integers. Defaults to little endian |
||
48 | * |
||
49 | * @type string |
||
50 | */ |
||
51 | private $number_type = self::NUMBER_TYPE_LITTLEENDIAN; |
||
52 | |||
53 | /** |
||
54 | * The original data |
||
55 | * |
||
56 | * @type string |
||
57 | */ |
||
58 | private $data; |
||
59 | |||
60 | /** |
||
61 | * The original data |
||
62 | * |
||
63 | * @type int |
||
64 | */ |
||
65 | private $length; |
||
66 | |||
67 | /** |
||
68 | * Position of pointer |
||
69 | * |
||
70 | * @type int |
||
71 | */ |
||
72 | private $index = 0; |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param string $data |
||
78 | * @param string $number_type |
||
79 | */ |
||
80 | 163 | public function __construct($data, $number_type = self::NUMBER_TYPE_LITTLEENDIAN) |
|
81 | { |
||
82 | |||
83 | 163 | $this->number_type = $number_type; |
|
84 | 163 | $this->data = $data; |
|
85 | 163 | $this->length = strlen($data); |
|
86 | 163 | } |
|
87 | |||
88 | /** |
||
89 | * Return all the data |
||
90 | * |
||
91 | * @return string The data |
||
92 | */ |
||
93 | 1 | public function getData() |
|
94 | { |
||
95 | |||
96 | 1 | return $this->data; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Return data currently in the buffer |
||
101 | * |
||
102 | * @return string The data currently in the buffer |
||
103 | */ |
||
104 | 95 | public function getBuffer() |
|
105 | { |
||
106 | |||
107 | 95 | return substr($this->data, $this->index); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns the number of bytes in the buffer |
||
112 | * |
||
113 | * @return int Length of the buffer |
||
114 | */ |
||
115 | 87 | public function getLength() |
|
116 | { |
||
117 | |||
118 | 87 | return max($this->length - $this->index, 0); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Read from the buffer |
||
123 | * |
||
124 | * @param int $length |
||
125 | * |
||
126 | * @return string |
||
127 | * @throws \GameQ\Exception\Protocol |
||
128 | */ |
||
129 | 158 | public function read($length = 1) |
|
130 | { |
||
131 | |||
132 | 158 | if (($length + $this->index) > $this->length) { |
|
133 | 5 | throw new Exception("Unable to read length={$length} from buffer. Bad protocol format or return?"); |
|
134 | } |
||
135 | |||
136 | 156 | $string = substr($this->data, $this->index, $length); |
|
137 | 156 | $this->index += $length; |
|
138 | |||
139 | 156 | return $string; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Read the last character from the buffer |
||
144 | * |
||
145 | * Unlike the other read functions, this function actually removes |
||
146 | * the character from the buffer. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | public function readLast() |
|
151 | { |
||
152 | |||
153 | 1 | $len = strlen($this->data); |
|
154 | 1 | $string = $this->data{strlen($this->data) - 1}; |
|
155 | 1 | $this->data = substr($this->data, 0, $len - 1); |
|
156 | 1 | $this->length -= 1; |
|
157 | |||
158 | 1 | return $string; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Look at the buffer, but don't remove |
||
163 | * |
||
164 | * @param int $length |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 67 | public function lookAhead($length = 1) |
|
169 | { |
||
170 | |||
171 | 67 | return substr($this->data, $this->index, $length); |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Skip forward in the buffer |
||
176 | * |
||
177 | * @param int $length |
||
178 | */ |
||
179 | 21 | public function skip($length = 1) |
|
180 | { |
||
181 | |||
182 | 21 | $this->index += $length; |
|
183 | 21 | } |
|
184 | |||
185 | /** |
||
186 | * Jump to a specific position in the buffer, |
||
187 | * will not jump past end of buffer |
||
188 | * |
||
189 | * @param $index |
||
190 | */ |
||
191 | 9 | public function jumpto($index) |
|
192 | { |
||
193 | |||
194 | 9 | $this->index = min($index, $this->length - 1); |
|
195 | 9 | } |
|
196 | |||
197 | /** |
||
198 | * Get the current pointer position |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | 1 | public function getPosition() |
|
203 | { |
||
204 | |||
205 | 1 | return $this->index; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Read from buffer until delimiter is reached |
||
210 | * |
||
211 | * If not found, return everything |
||
212 | * |
||
213 | * @param string $delim |
||
214 | * |
||
215 | * @return string |
||
216 | * @throws \GameQ\Exception\Protocol |
||
217 | */ |
||
218 | 91 | public function readString($delim = "\x00") |
|
219 | { |
||
220 | |||
221 | // Get position of delimiter |
||
222 | 91 | $len = strpos($this->data, $delim, min($this->index, $this->length)); |
|
223 | |||
224 | // If it is not found then return whole buffer |
||
225 | 91 | if ($len === false) { |
|
226 | 6 | return $this->read(strlen($this->data) - $this->index); |
|
227 | } |
||
228 | |||
229 | // Read the string and remove the delimiter |
||
230 | 91 | $string = $this->read($len - $this->index); |
|
231 | 91 | ++$this->index; |
|
232 | |||
233 | 91 | return $string; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Reads a pascal string from the buffer |
||
238 | * |
||
239 | * @param int $offset Number of bits to cut off the end |
||
240 | * @param bool $read_offset True if the data after the offset is to be read |
||
241 | * |
||
242 | * @return string |
||
243 | * @throws \GameQ\Exception\Protocol |
||
244 | */ |
||
245 | 7 | public function readPascalString($offset = 0, $read_offset = false) |
|
246 | { |
||
247 | |||
248 | // Get the proper offset |
||
249 | 7 | $len = $this->readInt8(); |
|
250 | 7 | $offset = max($len - $offset, 0); |
|
251 | |||
252 | // Read the data |
||
253 | 7 | if ($read_offset) { |
|
254 | 2 | return $this->read($offset); |
|
255 | } else { |
||
256 | 5 | return substr($this->read($len), 0, $offset); |
|
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Read from buffer until any of the delimiters is reached |
||
262 | * |
||
263 | * If not found, return everything |
||
264 | * |
||
265 | * @param $delims |
||
266 | * @param null $delimfound |
||
267 | * |
||
268 | * @return string |
||
269 | * @throws \GameQ\Exception\Protocol |
||
270 | * |
||
271 | * @todo: Check to see if this is even used anymore |
||
272 | */ |
||
273 | 4 | public function readStringMulti($delims, &$delimfound = null) |
|
274 | { |
||
275 | |||
276 | // Get position of delimiters |
||
277 | 4 | $pos = []; |
|
278 | 4 | foreach ($delims as $delim) { |
|
279 | 4 | if ($p = strpos($this->data, $delim, min($this->index, $this->length))) { |
|
280 | 4 | $pos[] = $p; |
|
281 | } |
||
282 | } |
||
283 | |||
284 | // If none are found then return whole buffer |
||
285 | 4 | if (empty($pos)) { |
|
286 | 4 | return $this->read(strlen($this->data) - $this->index); |
|
287 | } |
||
288 | |||
289 | // Read the string and remove the delimiter |
||
290 | 4 | sort($pos); |
|
291 | 4 | $string = $this->read($pos[0] - $this->index); |
|
292 | 4 | $delimfound = $this->read(); |
|
293 | |||
294 | 4 | return $string; |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Read an 8-bit unsigned integer |
||
299 | * |
||
300 | * @return int |
||
301 | * @throws \GameQ\Exception\Protocol |
||
302 | */ |
||
303 | 78 | public function readInt8() |
|
304 | { |
||
305 | |||
306 | 78 | $int = unpack('Cint', $this->read(1)); |
|
307 | |||
308 | 78 | return $int['int']; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Read and 8-bit signed integer |
||
313 | * |
||
314 | * @return int |
||
315 | * @throws \GameQ\Exception\Protocol |
||
316 | */ |
||
317 | 7 | public function readInt8Signed() |
|
324 | |||
325 | /** |
||
326 | * Read a 16-bit unsigned integer |
||
327 | * |
||
328 | * @return int |
||
329 | * @throws \GameQ\Exception\Protocol |
||
330 | */ |
||
331 | 68 | public function readInt16() |
|
332 | { |
||
333 | |||
334 | // Change the integer type we are looking up |
||
335 | 68 | switch ($this->number_type) { |
|
336 | 68 | case self::NUMBER_TYPE_BIGENDIAN: |
|
337 | 2 | $type = 'nint'; |
|
338 | 2 | break; |
|
339 | |||
340 | 66 | case self::NUMBER_TYPE_LITTLEENDIAN: |
|
341 | 64 | $type = 'vint'; |
|
342 | 64 | break; |
|
343 | |||
344 | default: |
||
345 | 2 | $type = 'Sint'; |
|
346 | } |
||
347 | |||
348 | 68 | $int = unpack($type, $this->read(2)); |
|
349 | |||
350 | 68 | return $int['int']; |
|
351 | } |
||
352 | |||
353 | /** |
||
354 | * Read a 16-bit signed integer |
||
355 | * |
||
356 | * @return int |
||
357 | * @throws \GameQ\Exception\Protocol |
||
358 | */ |
||
359 | 67 | public function readInt16Signed() |
|
360 | { |
||
361 | |||
362 | // Read the data into a string |
||
363 | 67 | $string = $this->read(2); |
|
364 | |||
365 | // For big endian we need to reverse the bytes |
||
366 | 67 | if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) { |
|
367 | 5 | $string = strrev($string); |
|
368 | } |
||
369 | |||
370 | 67 | $int = unpack('sint', $string); |
|
371 | |||
372 | 67 | unset($string); |
|
373 | |||
374 | 67 | return $int['int']; |
|
375 | } |
||
376 | |||
377 | /** |
||
378 | * Read a 32-bit unsigned integer |
||
379 | * |
||
380 | * @return int |
||
381 | * @throws \GameQ\Exception\Protocol |
||
382 | */ |
||
383 | 22 | public function readInt32() |
|
384 | { |
||
385 | |||
386 | // Change the integer type we are looking up |
||
387 | 22 | switch ($this->number_type) { |
|
388 | 22 | case self::NUMBER_TYPE_BIGENDIAN: |
|
389 | 5 | $type = 'Nint'; |
|
390 | 5 | break; |
|
391 | |||
392 | 17 | case self::NUMBER_TYPE_LITTLEENDIAN: |
|
393 | 15 | $type = 'Vint'; |
|
394 | 15 | break; |
|
395 | |||
396 | default: |
||
397 | 2 | $type = 'Lint'; |
|
398 | } |
||
399 | |||
400 | // Unpack the number |
||
401 | 22 | $int = unpack($type, $this->read(4)); |
|
402 | |||
403 | 22 | return $int['int']; |
|
404 | } |
||
405 | |||
406 | /** |
||
407 | * Read a 32-bit signed integer |
||
408 | * |
||
409 | * @return int |
||
410 | * @throws \GameQ\Exception\Protocol |
||
411 | */ |
||
412 | 72 | public function readInt32Signed() |
|
413 | { |
||
414 | |||
415 | // Read the data into a string |
||
416 | 72 | $string = $this->read(4); |
|
417 | |||
418 | // For big endian we need to reverse the bytes |
||
419 | 72 | if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) { |
|
420 | 5 | $string = strrev($string); |
|
421 | } |
||
422 | |||
423 | 72 | $int = unpack('lint', $string); |
|
424 | |||
425 | 72 | unset($string); |
|
426 | |||
427 | 72 | return $int['int']; |
|
428 | } |
||
429 | |||
430 | /** |
||
431 | * Read a 64-bit unsigned integer |
||
432 | * |
||
433 | * @return int |
||
434 | * @throws \GameQ\Exception\Protocol |
||
435 | */ |
||
436 | 63 | public function readInt64() |
|
477 | |||
478 | /** |
||
479 | * Read a 32-bit float |
||
480 | * |
||
481 | * @return float |
||
482 | * @throws \GameQ\Exception\Protocol |
||
483 | */ |
||
484 | 61 | public function readFloat32() |
|
485 | { |
||
486 | |||
487 | // Read the data into a string |
||
488 | 61 | $string = $this->read(4); |
|
489 | |||
490 | // For big endian we need to reverse the bytes |
||
491 | 61 | if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) { |
|
492 | 5 | $string = strrev($string); |
|
493 | } |
||
494 | |||
501 | } |
||
502 |