| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- /**
- * This file is part of GameQ.
- *
- * GameQ is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * GameQ is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- */
- namespace GameQ;
- use GameQ\Exception\Protocol as Exception;
- /**
- * Class Buffer
- *
- * Read specific byte sequences from a provided string or Buffer
- *
- * @package GameQ
- *
- * @author Austin Bischoff <[email protected]>
- * @author Aidan Lister <[email protected]>
- * @author Tom Buskens <[email protected]>
- */
- class Buffer
- {
- /**
- * Constants for the byte code types we need to read as
- */
- const NUMBER_TYPE_BIGENDIAN = 'be',
- NUMBER_TYPE_LITTLEENDIAN = 'le',
- NUMBER_TYPE_MACHINE = 'm';
- /**
- * The number type we use for reading integers. Defaults to little endian
- *
- * @type string
- */
- private $number_type = self::NUMBER_TYPE_LITTLEENDIAN;
- /**
- * The original data
- *
- * @type string
- */
- private $data;
- /**
- * The original data
- *
- * @type int
- */
- private $length;
- /**
- * Position of pointer
- *
- * @type int
- */
- private $index = 0;
- /**
- * Constructor
- *
- * @param string $data
- * @param string $number_type
- */
- public function __construct($data, $number_type = self::NUMBER_TYPE_LITTLEENDIAN)
- {
- $this->number_type = $number_type;
- $this->data = $data;
- $this->length = strlen($data);
- }
- /**
- * Return all the data
- *
- * @return string The data
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * Return data currently in the buffer
- *
- * @return string The data currently in the buffer
- */
- public function getBuffer()
- {
- return substr($this->data, $this->index);
- }
- /**
- * Returns the number of bytes in the buffer
- *
- * @return int Length of the buffer
- */
- public function getLength()
- {
- return max($this->length - $this->index, 0);
- }
- /**
- * Read from the buffer
- *
- * @param int $length
- *
- * @return string
- * @throws \GameQ\Exception\Protocol
- */
- public function read($length = 1)
- {
- if (($length + $this->index) > $this->length) {
- throw new Exception("Unable to read length={$length} from buffer. Bad protocol format or return?");
- }
- $string = substr($this->data, $this->index, $length);
- $this->index += $length;
- return $string;
- }
- /**
- * Read the last character from the buffer
- *
- * Unlike the other read functions, this function actually removes
- * the character from the buffer.
- *
- * @return string
- */
- public function readLast()
- {
- $len = strlen($this->data);
- $string = $this->data[strlen($this->data) - 1];
- $this->data = substr($this->data, 0, $len - 1);
- $this->length -= 1;
- return $string;
- }
- /**
- * Look at the buffer, but don't remove
- *
- * @param int $length
- *
- * @return string
- */
- public function lookAhead($length = 1)
- {
- return substr($this->data, $this->index, $length);
- }
- /**
- * Skip forward in the buffer
- *
- * @param int $length
- */
- public function skip($length = 1)
- {
- $this->index += $length;
- }
- /**
- * Jump to a specific position in the buffer,
- * will not jump past end of buffer
- *
- * @param $index
- */
- public function jumpto($index)
- {
- $this->index = min($index, $this->length - 1);
- }
- /**
- * Get the current pointer position
- *
- * @return int
- */
- public function getPosition()
- {
- return $this->index;
- }
- /**
- * Read from buffer until delimiter is reached
- *
- * If not found, return everything
- *
- * @param string $delim
- *
- * @return string
- * @throws \GameQ\Exception\Protocol
- */
- public function readString($delim = "\x00")
- {
- // Get position of delimiter
- $len = strpos($this->data, $delim, min($this->index, $this->length));
- // If it is not found then return whole buffer
- if ($len === false) {
- return $this->read(strlen($this->data) - $this->index);
- }
- // Read the string and remove the delimiter
- $string = $this->read($len - $this->index);
- ++$this->index;
- return $string;
- }
- /**
- * Reads a pascal string from the buffer
- *
- * @param int $offset Number of bits to cut off the end
- * @param bool $read_offset True if the data after the offset is to be read
- *
- * @return string
- * @throws \GameQ\Exception\Protocol
- */
- public function readPascalString($offset = 0, $read_offset = false)
- {
- // Get the proper offset
- $len = $this->readInt8();
- $offset = max($len - $offset, 0);
- // Read the data
- if ($read_offset) {
- return $this->read($offset);
- } else {
- return substr($this->read($len), 0, $offset);
- }
- }
- /**
- * Read from buffer until any of the delimiters is reached
- *
- * If not found, return everything
- *
- * @param $delims
- * @param null|string &$delimfound
- *
- * @return string
- * @throws \GameQ\Exception\Protocol
- *
- * @todo: Check to see if this is even used anymore
- */
- public function readStringMulti($delims, &$delimfound = null)
- {
- // Get position of delimiters
- $pos = [];
- foreach ($delims as $delim) {
- if ($index = strpos($this->data, $delim, min($this->index, $this->length))) {
- $pos[] = $index;
- }
- }
- // If none are found then return whole buffer
- if (empty($pos)) {
- return $this->read(strlen($this->data) - $this->index);
- }
- // Read the string and remove the delimiter
- sort($pos);
- $string = $this->read($pos[0] - $this->index);
- $delimfound = $this->read();
- return $string;
- }
- /**
- * Read an 8-bit unsigned integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt8()
- {
- $int = unpack('Cint', $this->read(1));
- return $int['int'];
- }
- /**
- * Read and 8-bit signed integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt8Signed()
- {
- $int = unpack('cint', $this->read(1));
- return $int['int'];
- }
- /**
- * Read a 16-bit unsigned integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt16()
- {
- // Change the integer type we are looking up
- switch ($this->number_type) {
- case self::NUMBER_TYPE_BIGENDIAN:
- $type = 'nint';
- break;
- case self::NUMBER_TYPE_LITTLEENDIAN:
- $type = 'vint';
- break;
- default:
- $type = 'Sint';
- }
- $int = unpack($type, $this->read(2));
- return $int['int'];
- }
- /**
- * Read a 16-bit signed integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt16Signed()
- {
- // Read the data into a string
- $string = $this->read(2);
- // For big endian we need to reverse the bytes
- if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) {
- $string = strrev($string);
- }
- $int = unpack('sint', $string);
- unset($string);
- return $int['int'];
- }
- /**
- * Read a 32-bit unsigned integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt32($length = 4)
- {
- // Change the integer type we are looking up
- $littleEndian = null;
- switch ($this->number_type) {
- case self::NUMBER_TYPE_BIGENDIAN:
- $type = 'N';
- $littleEndian = false;
- break;
- case self::NUMBER_TYPE_LITTLEENDIAN:
- $type = 'V';
- $littleEndian = true;
- break;
- default:
- $type = 'L';
- }
- // read from the buffer and append/prepend empty bytes for shortened int32
- $corrected = $this->read($length);
- // Unpack the number
- $int = unpack($type . 'int', self::extendBinaryString($corrected, 4, $littleEndian));
- return $int['int'];
- }
- /**
- * Read a 32-bit signed integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt32Signed()
- {
- // Read the data into a string
- $string = $this->read(4);
- // For big endian we need to reverse the bytes
- if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) {
- $string = strrev($string);
- }
- $int = unpack('lint', $string);
- unset($string);
- return $int['int'];
- }
- /**
- * Read a 64-bit unsigned integer
- *
- * @return int
- * @throws \GameQ\Exception\Protocol
- */
- public function readInt64()
- {
- // We have the pack 64-bit codes available. See: http://php.net/manual/en/function.pack.php
- if (version_compare(PHP_VERSION, '5.6.3') >= 0 && PHP_INT_SIZE == 8) {
- // Change the integer type we are looking up
- switch ($this->number_type) {
- case self::NUMBER_TYPE_BIGENDIAN:
- $type = 'Jint';
- break;
- case self::NUMBER_TYPE_LITTLEENDIAN:
- $type = 'Pint';
- break;
- default:
- $type = 'Qint';
- }
- $int64 = unpack($type, $this->read(8));
- $int = $int64['int'];
- unset($int64);
- } else {
- if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) {
- $high = $this->readInt32();
- $low = $this->readInt32();
- } else {
- $low = $this->readInt32();
- $high = $this->readInt32();
- }
- // We have to determine the number via bitwise
- $int = ($high << 32) | $low;
- unset($low, $high);
- }
- return $int;
- }
- /**
- * Read a 32-bit float
- *
- * @return float
- * @throws \GameQ\Exception\Protocol
- */
- public function readFloat32()
- {
- // Read the data into a string
- $string = $this->read(4);
- // For big endian we need to reverse the bytes
- if ($this->number_type == self::NUMBER_TYPE_BIGENDIAN) {
- $string = strrev($string);
- }
- $float = unpack('ffloat', $string);
- unset($string);
- return $float['float'];
- }
- private static function extendBinaryString($input, $length = 4, $littleEndian = null)
- {
- if (is_null($littleEndian)) {
- $littleEndian = self::isLittleEndian();
- }
- $extension = str_repeat(pack($littleEndian ? 'V' : 'N', 0b0000), $length - strlen($input));
- if ($littleEndian) {
- return $input . $extension;
- } else {
- return $extension . $input;
- }
- }
- private static function isLittleEndian()
- {
- return 0x00FF === current(unpack('v', pack('S', 0x00FF)));
- }
- }
|