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 | 248 | public function __construct($data, $number_type = self::NUMBER_TYPE_LITTLEENDIAN) |
|
87 | |||
88 | /** |
||
89 | * Return all the data |
||
90 | * |
||
91 | * @return string The data |
||
92 | */ |
||
93 | 1 | public function getData() |
|
98 | |||
99 | /** |
||
100 | * Return data currently in the buffer |
||
101 | * |
||
102 | * @return string The data currently in the buffer |
||
103 | */ |
||
104 | 166 | public function getBuffer() |
|
109 | |||
110 | /** |
||
111 | * Returns the number of bytes in the buffer |
||
112 | * |
||
113 | * @return int Length of the buffer |
||
114 | */ |
||
115 | 158 | public function getLength() |
|
120 | |||
121 | /** |
||
122 | * Read from the buffer |
||
123 | * |
||
124 | * @param int $length |
||
125 | * |
||
126 | * @return string |
||
127 | * @throws \GameQ\Exception\Protocol |
||
128 | */ |
||
129 | 237 | public function read($length = 1) |
|
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() |
|
160 | |||
161 | /** |
||
162 | * Look at the buffer, but don't remove |
||
163 | * |
||
164 | * @param int $length |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 109 | public function lookAhead($length = 1) |
|
173 | |||
174 | /** |
||
175 | * Skip forward in the buffer |
||
176 | * |
||
177 | * @param int $length |
||
178 | */ |
||
179 | 68 | public function skip($length = 1) |
|
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) |
|
196 | |||
197 | /** |
||
198 | * Get the current pointer position |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | 1 | public function getPosition() |
|
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 | 167 | public function readString($delim = "\x00") |
|
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 | 11 | public function readPascalString($offset = 0, $read_offset = false) |
|
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 | 17 | public function readStringMulti($delims, &$delimfound = null) |
|
274 | { |
||
275 | |||
276 | // Get position of delimiters |
||
277 | 17 | $pos = []; |
|
278 | 17 | foreach ($delims as $delim) { |
|
279 | 17 | if ($p = strpos($this->data, $delim, min($this->index, $this->length))) { |
|
280 | 17 | $pos[] = $p; |
|
281 | 17 | } |
|
282 | 17 | } |
|
283 | |||
284 | // If none are found then return whole buffer |
||
285 | 17 | if (empty($pos)) { |
|
286 | 17 | return $this->read(strlen($this->data) - $this->index); |
|
287 | } |
||
288 | |||
289 | // Read the string and remove the delimiter |
||
290 | 17 | sort($pos); |
|
291 | 17 | $string = $this->read($pos[0] - $this->index); |
|
292 | 17 | $delimfound = $this->read(); |
|
293 | |||
294 | 17 | return $string; |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Read an 8-bit unsigned integer |
||
299 | * |
||
300 | * @return int |
||
301 | * @throws \GameQ\Exception\Protocol |
||
302 | */ |
||
303 | 131 | public function readInt8() |
|
310 | |||
311 | /** |
||
312 | * Read and 8-bit signed integer |
||
313 | * |
||
314 | * @return int |
||
315 | * @throws \GameQ\Exception\Protocol |
||
316 | */ |
||
317 | 5 | public function readInt8Signed() |
|
324 | |||
325 | /** |
||
326 | * Read a 16-bit unsigned integer |
||
327 | * |
||
328 | * @return int |
||
329 | * @throws \GameQ\Exception\Protocol |
||
330 | */ |
||
331 | 107 | public function readInt16() |
|
352 | |||
353 | /** |
||
354 | * Read a 16-bit signed integer |
||
355 | * |
||
356 | * @return int |
||
357 | * @throws \GameQ\Exception\Protocol |
||
358 | */ |
||
359 | 102 | public function readInt16Signed() |
|
376 | |||
377 | /** |
||
378 | * Read a 32-bit unsigned integer |
||
379 | * |
||
380 | * @return int |
||
381 | * @throws \GameQ\Exception\Protocol |
||
382 | */ |
||
383 | 137 | public function readInt32() |
|
405 | |||
406 | /** |
||
407 | * Read a 32-bit signed integer |
||
408 | * |
||
409 | * @return int |
||
410 | * @throws \GameQ\Exception\Protocol |
||
411 | */ |
||
412 | 105 | public function readInt32Signed() |
|
429 | |||
430 | /** |
||
431 | * Read a 64-bit unsigned integer |
||
432 | * |
||
433 | * @return int |
||
434 | * @throws \GameQ\Exception\Protocol |
||
435 | */ |
||
436 | 93 | public function readInt64() |
|
477 | |||
478 | /** |
||
479 | * Read a 32-bit float |
||
480 | * |
||
481 | * @return float |
||
482 | * @throws \GameQ\Exception\Protocol |
||
483 | */ |
||
484 | 80 | public function readFloat32() |
|
501 | } |
||
502 |