Skip to content

Commit

Permalink
better error handling for invalid NBT data, added ext-json to compose…
Browse files Browse the repository at this point in the history
…r.json
  • Loading branch information
KurtThiemann committed Sep 16, 2021
1 parent dd2e256 commit b32c375
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"require": {
"pocketmine/binaryutils": "^0.2.1",
"php-64bit": "*",
"ext-zlib": "*"
"ext-zlib": "*",
"ext-json": "*"
}
}
10 changes: 9 additions & 1 deletion src/IO/Reader/GZipCompressedStringReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Aternos\Nbt\IO\Reader;

use Exception;

class GZipCompressedStringReader extends StringReader
{
/**
* @throws Exception
*/
public function __construct(string $data, int $format)
{
parent::__construct(gzdecode($data), $format);
if(($uncompressed = @gzdecode($data)) === false) {
throw new Exception("Invalid GZip data");
}
parent::__construct($uncompressed, $format);
}
}
10 changes: 9 additions & 1 deletion src/IO/Reader/ZLibCompressedStringReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Aternos\Nbt\IO\Reader;

use Exception;

class ZLibCompressedStringReader extends StringReader
{
/**
* @throws Exception
*/
public function __construct(string $data, int $format)
{
parent::__construct(zlib_decode($data), $format);
if(($uncompressed = @zlib_decode($data)) === false) {
throw new Exception("Invalid ZLib data");
}
parent::__construct($uncompressed, $format);
}
}
10 changes: 9 additions & 1 deletion src/IO/Writer/GZipCompressedStringWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Aternos\Nbt\IO\Writer;

use Exception;

class GZipCompressedStringWriter extends StringWriter
{
/**
* @throws Exception
*/
public function getStringData(): string
{
return gzencode(parent::getStringData());
if(($compressed = @gzencode(parent::getStringData())) === false) {
throw new Exception("Failed to apply GZip compression");
}
return $compressed;
}
}
10 changes: 9 additions & 1 deletion src/IO/Writer/ZLibCompressedStringWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Aternos\Nbt\IO\Writer;

use Exception;

class ZLibCompressedStringWriter extends StringWriter
{
/**
* @throws Exception
*/
public function getStringData(): string
{
return zlib_encode(parent::getStringData(), ZLIB_ENCODING_DEFLATE);
if(($compressed = @zlib_encode(parent::getStringData(), ZLIB_ENCODING_DEFLATE)) === false) {
throw new Exception("Failed to apply ZLib compression");
}
return $compressed;
}
}
21 changes: 3 additions & 18 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Aternos\Nbt\NbtFormat;
use Aternos\Nbt\Serializer\NbtSerializer;
use Exception;
use JsonSerializable;

abstract class Tag implements \JsonSerializable
abstract class Tag implements JsonSerializable
{
public const TYPE = -1;

Expand Down Expand Up @@ -143,22 +144,6 @@ public function write(Writer $writer): Tag
return $this;
}

/**
* @param string $type
* @param string $value
* @return mixed
* @throws Exception
* @deprecated
*/
protected function unpack(string $type, string $value)
{
$unpacked = @unpack($type, $value);
if($unpacked === false) {
throw new Exception("Failed to unpack " . $type . " from " . bin2hex($value));
}
return $unpacked[1];
}

/**
* @param string $str
* @return string
Expand Down Expand Up @@ -195,7 +180,7 @@ abstract protected function getValueString(): string;
*/
public static function getTagClass(int $type): ?string
{
return static::TAGS[$type] ?: null;
return static::TAGS[$type] ?? null;
}

/**
Expand Down

0 comments on commit b32c375

Please sign in to comment.