From 2024e9ecdfd26cebcdb7e58971d5bf974cd244b3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 10 Jun 2017 18:33:54 +0100 Subject: [PATCH] Fixed extremely stupid zero-length bug in BinaryStream pls don't kill me :cry: --- .../network/mcpe/protocol/BatchPacket.php | 2 +- .../mcpe/protocol/BlockEntityDataPacket.php | 2 +- .../mcpe/protocol/CommandStepPacket.php | 2 +- .../network/mcpe/protocol/UnknownPacket.php | 2 +- .../mcpe/protocol/UpdateTradePacket.php | 2 +- src/pocketmine/utils/BinaryStream.php | 23 +++++++++++++++---- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/pocketmine/network/mcpe/protocol/BatchPacket.php b/src/pocketmine/network/mcpe/protocol/BatchPacket.php index 8d510b8e6..06124eafd 100644 --- a/src/pocketmine/network/mcpe/protocol/BatchPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BatchPacket.php @@ -46,7 +46,7 @@ class BatchPacket extends DataPacket{ } public function decode(){ - $this->payload = $this->get(0); + $this->payload = $this->getRemaining(); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php index 29f2a2d26..2b138307a 100644 --- a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php @@ -38,7 +38,7 @@ class BlockEntityDataPacket extends DataPacket{ public function decode(){ $this->getBlockPosition($this->x, $this->y, $this->z); - $this->namedtag = $this->get(0); + $this->namedtag = $this->getRemaining(); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php index ca701c429..5192050dc 100644 --- a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php +++ b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php @@ -49,7 +49,7 @@ class CommandStepPacket extends DataPacket{ $this->inputJson = json_decode($this->getString()); $this->outputJson = json_decode($this->getString()); - $this->get(0); //TODO: read command origin data + $this->getRemaining(); //TODO: read command origin data } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php index f8d914a49..d11517c80 100644 --- a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php @@ -45,7 +45,7 @@ class UnknownPacket extends DataPacket{ public function decode(){ $this->offset -= 1; //Rewind one byte so we can read the PID - $this->payload = $this->get(0); + $this->payload = $this->getRemaining(); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php b/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php index 4086090cd..b2d700466 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php @@ -53,7 +53,7 @@ class UpdateTradePacket extends DataPacket{ $this->traderEid = $this->getEntityUniqueId(); $this->playerEid = $this->getEntityUniqueId(); $this->displayName = $this->getString(); - $this->offers = $this->get(0); + $this->offers = $this->getRemaining(); } public function encode(){ diff --git a/src/pocketmine/utils/BinaryStream.php b/src/pocketmine/utils/BinaryStream.php index 31619b2ce..623ec6360 100644 --- a/src/pocketmine/utils/BinaryStream.php +++ b/src/pocketmine/utils/BinaryStream.php @@ -57,19 +57,32 @@ class BinaryStream{ return $this->buffer; } - public function get(int $len) : string{ - if($len < 0){ - $this->offset = strlen($this->buffer) - 1; - return ""; - }elseif($len === 0){ + /** + * @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; }