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 | 221 | public function __construct($data, $number_type = self::NUMBER_TYPE_LITTLEENDIAN) |
|
81 | { |
||
82 | |||
83 | 221 | $this->number_type = $number_type; |
|
84 | 221 | $this->data = $data; |
|
85 | 221 | $this->length = strlen($data); |
|
86 | 221 | } |
|
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 | 140 | public function getBuffer() |
|
105 | { |
||
106 | |||
107 | 140 | 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 | 137 | 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 | 210 | 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 | 94 | public function lookAhead($length = 1) |
|
169 | { |
||
170 | |||
171 | 94 | return substr($this->data, $this->index, $length); |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Skip forward in the buffer |
||
176 | * |
||
177 | * @param int $length |
||
178 | */ |
||
179 | 58 | public function skip($length = 1) |
|
180 | { |
||
181 | |||
182 | 58 | $this->index += $length; |
|
183 | 58 | } |
|
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 | 140 | public function readString($delim = "\x00") |
|
219 | { |
||
220 | |||
221 | // Get position of delimiter |
||
222 | 140 | $len = strpos($this->data, $delim, min($this->index, $this->length)); |
|
223 | |||
224 | // If it is not found then return whole buffer |
||
225 | 140 | if ($len === false) { |
|
226 | 13 | return $this->read(strlen($this->data) - $this->index); |
|
227 | } |
||
228 | |||
229 | // Read the string and remove the delimiter |
||
230 | 140 | $string = $this->read($len - $this->index); |
|
231 | 140 | ++$this->index; |
|
232 | |||
233 | 140 | 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 | 10 | 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 | 11 | public function readStringMulti($delims, &$delimfound = null) |
|
296 | |||
297 | /** |
||
298 | * Read an 8-bit unsigned integer |
||
299 | * |
||
300 | * @return int |
||
301 | * @throws \GameQ\Exception\Protocol |
||
302 | */ |
||
303 | 115 | public function readInt8() |
|
304 | { |
||
310 | |||
311 | /** |
||
312 | * Read and 8-bit signed integer |
||
313 | * |
||
314 | * @return int |
||
315 | * @throws \GameQ\Exception\Protocol |
||
316 | */ |
||
317 | 6 | public function readInt8Signed() |
|
324 | |||
325 | /** |
||
326 | * Read a 16-bit unsigned integer |
||
327 | * |
||
328 | * @return int |
||
329 | * @throws \GameQ\Exception\Protocol |
||
330 | */ |
||
331 | 96 | public function readInt16() |
|
352 | |||
353 | /** |
||
354 | * Read a 16-bit signed integer |
||
355 | * |
||
356 | * @return int |
||
357 | * @throws \GameQ\Exception\Protocol |
||
358 | */ |
||
359 | 92 | public function readInt16Signed() |
|
376 | |||
377 | /** |
||
378 | * Read a 32-bit unsigned integer |
||
379 | * |
||
380 | * @return int |
||
381 | * @throws \GameQ\Exception\Protocol |
||
382 | */ |
||
383 | 42 | public function readInt32() |
|
405 | |||
406 | /** |
||
407 | * Read a 32-bit signed integer |
||
408 | * |
||
409 | * @return int |
||
410 | * @throws \GameQ\Exception\Protocol |
||
411 | */ |
||
412 | 94 | public function readInt32Signed() |
|
429 | |||
430 | /** |
||
431 | * Read a 64-bit unsigned integer |
||
432 | * |
||
433 | * @return int |
||
434 | * @throws \GameQ\Exception\Protocol |
||
435 | */ |
||
436 | 83 | public function readInt64() |
|
477 | |||
478 | /** |
||
479 | * Read a 32-bit float |
||
480 | * |
||
481 | * @return float |
||
482 | * @throws \GameQ\Exception\Protocol |
||
483 | */ |
||
484 | 78 | public function readFloat32() |
|
501 | } |
||
502 |