From 579c50876101431cc9197be05ea471daf0dccd08 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 11 Jan 2018 13:19:59 +0000 Subject: [PATCH] Move Binary and BinaryStream to Composer library --- composer.json | 7 +- composer.lock | 35 +- src/pocketmine/utils/Binary.php | 562 -------------------------- src/pocketmine/utils/BinaryStream.php | 290 ------------- 4 files changed, 39 insertions(+), 855 deletions(-) delete mode 100644 src/pocketmine/utils/Binary.php delete mode 100644 src/pocketmine/utils/BinaryStream.php diff --git a/composer.json b/composer.json index 19170403d..e5f7b00d6 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "ext-zip": "*", "ext-zlib": ">=1.2.11", "pocketmine/raklib": "^0.10.0", - "pocketmine/pocketmine-spl": "^0.2.0" + "pocketmine/pocketmine-spl": "^0.2.0", + "pocketmine/pocketmine-binaryutils": "dev-master#4795001f4444725f97b3c14c1e295635ff0a98a4" }, "autoload": { "psr-0": { @@ -37,6 +38,10 @@ { "type": "vcs", "url": "https://github.com/pmmp/PocketMine-SPL" + }, + { + "type": "vcs", + "url": "https://github.com/pmmp/PocketMine-BinaryUtils" } ] } diff --git a/composer.lock b/composer.lock index 5e3ce1923..938135ccd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,38 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "2e9fcb8bb36b51385e5dc3ebf0336d07", + "content-hash": "13a106ee8e33d29b4fc1d9edf2828b7d", "packages": [ + { + "name": "pocketmine/pocketmine-binaryutils", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/pmmp/PocketMine-BinaryUtils.git", + "reference": "4795001f4444725f97b3c14c1e295635ff0a98a4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pmmp/PocketMine-BinaryUtils/zipball/4795001f4444725f97b3c14c1e295635ff0a98a4", + "reference": "4795001f4444725f97b3c14c1e295635ff0a98a4", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "pocketmine\\utils\\": "src/" + } + }, + "license": [ + "LGPL-3.0" + ], + "description": "Classes and methods for conveniently handling binary data", + "support": { + "source": "https://github.com/pmmp/PocketMine-BinaryUtils/tree/master", + "issues": "https://github.com/pmmp/PocketMine-BinaryUtils/issues" + }, + "time": "2018-01-11T12:51:47+00:00" + }, { "name": "pocketmine/pocketmine-spl", "version": "0.2.0", @@ -81,7 +111,8 @@ "minimum-stability": "stable", "stability-flags": { "php": 5, - "ext-pthreads": 20 + "ext-pthreads": 20, + "pocketmine/pocketmine-binaryutils": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php deleted file mode 100644 index 57e58afda..000000000 --- a/src/pocketmine/utils/Binary.php +++ /dev/null @@ -1,562 +0,0 @@ -> 56; - } - - public static function unsignByte(int $value) : int{ - return $value & 0xff; - } - - public static function signShort(int $value) : int{ - return $value << 48 >> 48; - } - - public static function unsignShort(int $value) : int{ - return $value & 0xffff; - } - - public static function signInt(int $value) : int{ - return $value << 32 >> 32; - } - - public static function unsignInt(int $value) : int{ - return $value & 0xffffffff; - } - - - public static function flipShortEndianness(int $value) : int{ - return self::readLShort(self::writeShort($value)); - } - - public static function flipIntEndianness(int $value) : int{ - return self::readLInt(self::writeInt($value)); - } - - public static function flipLongEndianness(int $value) : int{ - return self::readLLong(self::writeLong($value)); - } - - /** - * Reads a byte boolean - * - * @param string $b - * @return bool - */ - public static function readBool(string $b) : bool{ - return $b !== "\x00"; - } - - /** - * Writes a byte boolean - * - * @param bool $b - * @return string - */ - public static function writeBool(bool $b) : string{ - return $b ? "\x01" : "\x00"; - } - - /** - * Reads an unsigned byte (0 - 255) - * - * @param string $c - * @return int - */ - public static function readByte(string $c) : int{ - return ord($c{0}); - } - - /** - * Reads a signed byte (-128 - 127) - * - * @param string $c - * @return int - */ - public static function readSignedByte(string $c) : int{ - return self::signByte(ord($c{0})); - } - - /** - * Writes an unsigned/signed byte - * - * @param int $c - * @return string - */ - public static function writeByte(int $c) : string{ - return chr($c); - } - - /** - * Reads a 16-bit unsigned big-endian number - * - * @param string $str - * @return int - */ - public static function readShort(string $str) : int{ - return unpack("n", $str)[1]; - } - - /** - * Reads a 16-bit signed big-endian number - * - * @param $str - * - * @return int - */ - public static function readSignedShort(string $str) : int{ - return self::signShort(unpack("n", $str)[1]); - } - - /** - * Writes a 16-bit signed/unsigned big-endian number - * - * @param int $value - * - * @return string - */ - public static function writeShort(int $value) : string{ - return pack("n", $value); - } - - /** - * Reads a 16-bit unsigned little-endian number - * - * @param string $str - * - * @return int - */ - public static function readLShort(string $str) : int{ - return unpack("v", $str)[1]; - } - - /** - * Reads a 16-bit signed little-endian number - * - * @param string $str - * @return int - */ - public static function readSignedLShort(string $str) : int{ - return self::signShort(unpack("v", $str)[1]); - } - - /** - * Writes a 16-bit signed/unsigned little-endian number - * - * @param int $value - * @return string - */ - public static function writeLShort(int $value) : string{ - return pack("v", $value); - } - - /** - * Reads a 3-byte big-endian number - * - * @param string $str - * @return int - */ - public static function readTriad(string $str) : int{ - return unpack("N", "\x00" . $str)[1]; - } - - /** - * Writes a 3-byte big-endian number - * - * @param int $value - * @return string - */ - public static function writeTriad(int $value) : string{ - return substr(pack("N", $value), 1); - } - - /** - * Reads a 3-byte little-endian number - * - * @param string $str - * @return int - */ - public static function readLTriad(string $str) : int{ - return unpack("V", $str . "\x00")[1]; - } - - /** - * Writes a 3-byte little-endian number - * - * @param int $value - * @return string - */ - public static function writeLTriad(int $value) : string{ - return substr(pack("V", $value), 0, -1); - } - - /** - * Reads a 4-byte signed integer - * - * @param string $str - * @return int - */ - public static function readInt(string $str) : int{ - return self::signInt(unpack("N", $str)[1]); - } - - /** - * Writes a 4-byte integer - * - * @param int $value - * @return string - */ - public static function writeInt(int $value) : string{ - return pack("N", $value); - } - - /** - * Reads a 4-byte signed little-endian integer - * - * @param string $str - * @return int - */ - public static function readLInt(string $str) : int{ - return self::signInt(unpack("V", $str)[1]); - } - - /** - * Writes a 4-byte signed little-endian integer - * - * @param int $value - * @return string - */ - public static function writeLInt(int $value) : string{ - return pack("V", $value); - } - - /** - * Reads a 4-byte floating-point number - * - * @param string $str - * @return float - */ - public static function readFloat(string $str) : float{ - return unpack("G", $str)[1]; - } - - /** - * Reads a 4-byte floating-point number, rounded to the specified number of decimal places. - * - * @param string $str - * @param int $accuracy - * - * @return float - */ - public static function readRoundedFloat(string $str, int $accuracy) : float{ - return round(self::readFloat($str), $accuracy); - } - - /** - * Writes a 4-byte floating-point number. - * - * @param float $value - * @return string - */ - public static function writeFloat(float $value) : string{ - return pack("G", $value); - } - - /** - * Reads a 4-byte little-endian floating-point number. - * - * @param string $str - * @return float - */ - public static function readLFloat(string $str) : float{ - return unpack("g", $str)[1]; - } - - /** - * Reads a 4-byte little-endian floating-point number rounded to the specified number of decimal places. - * - * @param string $str - * @param int $accuracy - * - * @return float - */ - public static function readRoundedLFloat(string $str, int $accuracy) : float{ - return round(self::readLFloat($str), $accuracy); - } - - /** - * Writes a 4-byte little-endian floating-point number. - * - * @param float $value - * @return string - */ - public static function writeLFloat(float $value) : string{ - return pack("g", $value); - } - - /** - * Returns a printable floating-point number. - * - * @param float $value - * @return string - */ - public static function printFloat(float $value) : string{ - return preg_replace("/(\\.\\d+?)0+$/", "$1", sprintf("%F", $value)); - } - - /** - * Reads an 8-byte floating-point number. - * - * @param string $str - * @return float - */ - public static function readDouble(string $str) : float{ - return unpack("E", $str)[1]; - } - - /** - * Writes an 8-byte floating-point number. - * - * @param float $value - * @return string - */ - public static function writeDouble(float $value) : string{ - return pack("E", $value); - } - - /** - * Reads an 8-byte little-endian floating-point number. - * - * @param string $str - * @return float - */ - public static function readLDouble(string $str) : float{ - return unpack("e", $str)[1]; - } - - /** - * Writes an 8-byte floating-point little-endian number. - * @param float $value - * @return string - */ - public static function writeLDouble(float $value) : string{ - return pack("e", $value); - } - - /** - * Reads an 8-byte integer. - * - * @param string $str - * @return int - */ - public static function readLong(string $str) : int{ - return unpack("J", $str)[1]; - } - - /** - * Writes an 8-byte integer. - * - * @param int $value - * @return string - */ - public static function writeLong(int $value) : string{ - return pack("J", $value); - } - - /** - * Reads an 8-byte little-endian integer. - * - * @param string $str - * @return int - */ - public static function readLLong(string $str) : int{ - return unpack("P", $str)[1]; - } - - /** - * Writes an 8-byte little-endian integer. - * - * @param int $value - * @return string - */ - public static function writeLLong(int $value) : string{ - return pack("P", $value); - } - - - /** - * Reads a 32-bit zigzag-encoded variable-length integer. - * - * @param string $buffer - * @param int &$offset - * - * @return int - */ - public static function readVarInt(string $buffer, int &$offset) : int{ - $raw = self::readUnsignedVarInt($buffer, $offset); - $temp = ((($raw << 63) >> 63) ^ $raw) >> 1; - return $temp ^ ($raw & (1 << 63)); - } - - /** - * Reads a 32-bit variable-length unsigned integer. - * - * @param string $buffer - * @param int &$offset - * - * @return int - * - * @throws \InvalidArgumentException if the var-int did not end after 5 bytes - */ - public static function readUnsignedVarInt(string $buffer, int &$offset) : int{ - $value = 0; - for($i = 0; $i <= 35; $i += 7){ - $b = ord($buffer{$offset++}); - $value |= (($b & 0x7f) << $i); - - if(($b & 0x80) === 0){ - return $value; - }elseif(!isset($buffer{$offset})){ - throw new \UnexpectedValueException("Expected more bytes, none left to read"); - } - } - - throw new \InvalidArgumentException("VarInt did not terminate after 5 bytes!"); - } - - /** - * Writes a 32-bit integer as a zigzag-encoded variable-length integer. - * - * @param int $v - * @return string - */ - public static function writeVarInt(int $v) : string{ - $v = ($v << 32 >> 32); - return self::writeUnsignedVarInt(($v << 1) ^ ($v >> 31)); - } - - /** - * Writes a 32-bit unsigned integer as a variable-length integer. - * - * @param int $value - * @return string up to 5 bytes - */ - public static function writeUnsignedVarInt(int $value) : string{ - $buf = ""; - $value &= 0xffffffff; - for($i = 0; $i < 5; ++$i){ - if(($value >> 7) !== 0){ - $buf .= chr($value | 0x80); - }else{ - $buf .= chr($value & 0x7f); - return $buf; - } - - $value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator - } - - throw new \InvalidArgumentException("Value too large to be encoded as a VarInt"); - } - - - /** - * Reads a 64-bit zigzag-encoded variable-length integer. - * - * @param string $buffer - * @param int &$offset - * - * @return int - */ - public static function readVarLong(string $buffer, int &$offset) : int{ - $raw = self::readUnsignedVarLong($buffer, $offset); - $temp = ((($raw << 63) >> 63) ^ $raw) >> 1; - return $temp ^ ($raw & (1 << 63)); - } - - /** - * Reads a 64-bit unsigned variable-length integer. - * - * @param string $buffer - * @param int &$offset - * - * @return int - */ - public static function readUnsignedVarLong(string $buffer, int &$offset) : int{ - $value = 0; - for($i = 0; $i <= 63; $i += 7){ - $b = ord($buffer{$offset++}); - $value |= (($b & 0x7f) << $i); - - if(($b & 0x80) === 0){ - return $value; - }elseif(!isset($buffer{$offset})){ - throw new \UnexpectedValueException("Expected more bytes, none left to read"); - } - } - - throw new \InvalidArgumentException("VarLong did not terminate after 10 bytes!"); - } - - /** - * Writes a 64-bit integer as a zigzag-encoded variable-length long. - * - * @param int $v - * @return string - */ - public static function writeVarLong(int $v) : string{ - return self::writeUnsignedVarLong(($v << 1) ^ ($v >> 63)); - } - - /** - * Writes a 64-bit unsigned integer as a variable-length long. - * @param int $value - * - * @return string - */ - public static function writeUnsignedVarLong(int $value) : string{ - $buf = ""; - for($i = 0; $i < 10; ++$i){ - if(($value >> 7) !== 0){ - $buf .= chr($value | 0x80); //Let chr() take the last byte of this, it's faster than adding another & 0x7f. - }else{ - $buf .= chr($value & 0x7f); - return $buf; - } - - $value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator - } - - throw new \InvalidArgumentException("Value too large to be encoded as a VarLong"); - } -} diff --git a/src/pocketmine/utils/BinaryStream.php b/src/pocketmine/utils/BinaryStream.php deleted file mode 100644 index 27c1d1637..000000000 --- a/src/pocketmine/utils/BinaryStream.php +++ /dev/null @@ -1,290 +0,0 @@ - - -class BinaryStream{ - - /** @var int */ - public $offset; - /** @var string */ - public $buffer; - - public function __construct(string $buffer = "", int $offset = 0){ - $this->buffer = $buffer; - $this->offset = $offset; - } - - public function reset(){ - $this->buffer = ""; - $this->offset = 0; - } - - public function setBuffer(string $buffer = "", int $offset = 0){ - $this->buffer = $buffer; - $this->offset = $offset; - } - - public function getOffset() : int{ - return $this->offset; - } - - public function getBuffer() : string{ - return $this->buffer; - } - - /** - * @param int|bool $len - * - * @return string - */ - public function get($len) : string{ - if($len === true){ - $str = substr($this->buffer, $this->offset); - $this->offset = strlen($this->buffer); - return $str; - }elseif($len < 0){ - $this->offset = strlen($this->buffer) - 1; - return ""; - }elseif($len === 0){ - return ""; - } - - return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len); - } - - public function getRemaining() : string{ - $str = substr($this->buffer, $this->offset); - $this->offset = strlen($this->buffer); - return $str; - } - - public function put(string $str){ - $this->buffer .= $str; - } - - - public function getBool() : bool{ - return $this->get(1) !== "\x00"; - } - - public function putBool(bool $v){ - $this->buffer .= ($v ? "\x01" : "\x00"); - } - - - public function getByte() : int{ - return ord($this->buffer{$this->offset++}); - } - - public function putByte(int $v){ - $this->buffer .= chr($v); - } - - - public function getShort() : int{ - return Binary::readShort($this->get(2)); - } - - public function getSignedShort() : int{ - return Binary::readSignedShort($this->get(2)); - } - - public function putShort(int $v){ - $this->buffer .= Binary::writeShort($v); - } - - public function getLShort() : int{ - return Binary::readLShort($this->get(2)); - } - - public function getSignedLShort() : int{ - return Binary::readSignedLShort($this->get(2)); - } - - public function putLShort(int $v){ - $this->buffer .= Binary::writeLShort($v); - } - - - public function getTriad() : int{ - return Binary::readTriad($this->get(3)); - } - - public function putTriad(int $v){ - $this->buffer .= Binary::writeTriad($v); - } - - public function getLTriad() : int{ - return Binary::readLTriad($this->get(3)); - } - - public function putLTriad(int $v){ - $this->buffer .= Binary::writeLTriad($v); - } - - - public function getInt() : int{ - return Binary::readInt($this->get(4)); - } - - public function putInt(int $v){ - $this->buffer .= Binary::writeInt($v); - } - - public function getLInt() : int{ - return Binary::readLInt($this->get(4)); - } - - public function putLInt(int $v){ - $this->buffer .= Binary::writeLInt($v); - } - - - public function getFloat() : float{ - return Binary::readFloat($this->get(4)); - } - - public function getRoundedFloat(int $accuracy) : float{ - return Binary::readRoundedFloat($this->get(4), $accuracy); - } - - public function putFloat(float $v){ - $this->buffer .= Binary::writeFloat($v); - } - - public function getLFloat() : float{ - return Binary::readLFloat($this->get(4)); - } - - public function getRoundedLFloat(int $accuracy) : float{ - return Binary::readRoundedLFloat($this->get(4), $accuracy); - } - - public function putLFloat(float $v){ - $this->buffer .= Binary::writeLFloat($v); - } - - - /** - * @return int - */ - public function getLong() : int{ - return Binary::readLong($this->get(8)); - } - - /** - * @param int $v - */ - public function putLong(int $v){ - $this->buffer .= Binary::writeLong($v); - } - - /** - * @return int - */ - public function getLLong() : int{ - return Binary::readLLong($this->get(8)); - } - - /** - * @param int $v - */ - public function putLLong(int $v){ - $this->buffer .= Binary::writeLLong($v); - } - - /** - * Reads a 32-bit variable-length unsigned integer from the buffer and returns it. - * @return int - */ - public function getUnsignedVarInt() : int{ - return Binary::readUnsignedVarInt($this->buffer, $this->offset); - } - - /** - * Writes a 32-bit variable-length unsigned integer to the end of the buffer. - * @param int $v - */ - public function putUnsignedVarInt(int $v){ - $this->put(Binary::writeUnsignedVarInt($v)); - } - - /** - * Reads a 32-bit zigzag-encoded variable-length integer from the buffer and returns it. - * @return int - */ - public function getVarInt() : int{ - return Binary::readVarInt($this->buffer, $this->offset); - } - - /** - * Writes a 32-bit zigzag-encoded variable-length integer to the end of the buffer. - * @param int $v - */ - public function putVarInt(int $v){ - $this->put(Binary::writeVarInt($v)); - } - - /** - * Reads a 64-bit variable-length integer from the buffer and returns it. - * @return int - */ - public function getUnsignedVarLong() : int{ - return Binary::readUnsignedVarLong($this->buffer, $this->offset); - } - - /** - * Writes a 64-bit variable-length integer to the end of the buffer. - * @param int $v - */ - public function putUnsignedVarLong(int $v){ - $this->buffer .= Binary::writeUnsignedVarLong($v); - } - - /** - * Reads a 64-bit zigzag-encoded variable-length integer from the buffer and returns it. - * @return int - */ - public function getVarLong() : int{ - return Binary::readVarLong($this->buffer, $this->offset); - } - - /** - * Writes a 64-bit zigzag-encoded variable-length integer to the end of the buffer. - * @param int - */ - public function putVarLong(int $v){ - $this->buffer .= Binary::writeVarLong($v); - } - - /** - * Returns whether the offset has reached the end of the buffer. - * @return bool - */ - public function feof() : bool{ - return !isset($this->buffer{$this->offset}); - } -}