From fd0262e21ee0185c1628fe61d53fcd57cc9eb5c4 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 1 Feb 2024 13:23:19 +0000 Subject: [PATCH] Use API in pmmp/ext-encoding@688806dd7bc194e74acfbff9b4486d9c94bc20ff --- src/BaseNbtSerializer.php | 11 ++++----- src/JsonNbtParser.php | 51 ++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/BaseNbtSerializer.php b/src/BaseNbtSerializer.php index e48f9cb..4bb2f38 100644 --- a/src/BaseNbtSerializer.php +++ b/src/BaseNbtSerializer.php @@ -61,14 +61,14 @@ private function readRoot(int $maxDepth) : TreeRoot{ */ public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : TreeRoot{ $this->buffer = new ByteBuffer($buffer); - $this->buffer->setOffset($offset); + $this->buffer->setReadOffset($offset); try{ $data = $this->readRoot($maxDepth); }catch(DataDecodeException $e){ throw new NbtDataException($e->getMessage(), 0, $e); } - $offset = $this->buffer->getOffset(); + $offset = $this->buffer->getReadOffset(); return $data; } @@ -85,10 +85,10 @@ public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : Tree */ public function readHeadless(string $buffer, int $rootType, int &$offset = 0, int $maxDepth = 0) : Tag{ $this->buffer = new ByteBuffer($buffer); - $this->buffer->setOffset($offset); + $this->buffer->setReadOffset($offset); $data = NBT::createTag($rootType, $this, new ReaderTracker($maxDepth)); - $offset = $this->buffer->getOffset(); + $offset = $this->buffer->getReadOffset(); return $data; } @@ -106,8 +106,7 @@ public function readMultiple(string $buffer, int $maxDepth = 0) : array{ $retval = []; - $strlen = strlen($buffer); - while($this->buffer->getOffset() < $strlen){ + while($this->buffer->getReadOffset() < $this->buffer->getUsedLength()){ try{ $retval[] = $this->readRoot($maxDepth); }catch(DataDecodeException $e){ diff --git a/src/JsonNbtParser.php b/src/JsonNbtParser.php index ecaf740..6dbd412 100644 --- a/src/JsonNbtParser.php +++ b/src/JsonNbtParser.php @@ -57,11 +57,11 @@ public static function parseJson(string $data) : CompoundTag{ } $ret = self::parseCompound($stream); //don't return directly, syntax needs to be validated }catch(NbtDataException $e){ - throw new NbtDataException($e->getMessage() . " at offset " . $stream->getOffset()); + throw new NbtDataException($e->getMessage() . " at offset " . $stream->getReadOffset()); }catch(DataDecodeException $e){ - throw new NbtDataException("Syntax error: " . $e->getMessage() . " at offset " . $stream->getOffset()); + throw new NbtDataException("Syntax error: " . $e->getMessage() . " at offset " . $stream->getReadOffset()); } - $leftover = $stream->getUnreadLength(); + $leftover = $stream->getUsedLength() - $stream->getReadOffset(); if($leftover > 0){ throw new NbtDataException("Syntax error: unexpected trailing characters after end of tag: " . $stream->readByteArray($leftover)); } @@ -77,7 +77,7 @@ private static function parseList(ByteBuffer $stream) : ListTag{ $retval = new ListTag(); if(self::skipWhitespace($stream, "]")){ - while($stream->getUnreadLength() > 0){ + while($stream->getReadOffset() < $stream->getUsedLength()){ try{ $value = self::readValue($stream); }catch(InvalidTagValueException $e){ @@ -107,7 +107,7 @@ private static function parseCompound(ByteBuffer $stream) : CompoundTag{ $retval = new CompoundTag(); if(self::skipWhitespace($stream, "}")){ - while($stream->getUnreadLength() > 0){ + while($stream->getReadOffset() < $stream->getUsedLength()){ $k = self::readKey($stream); if($retval->getTag($k) !== null){ throw new NbtDataException("Syntax error: duplicate compound leaf node '$k'"); @@ -134,7 +134,7 @@ private static function parseCompound(ByteBuffer $stream) : CompoundTag{ * @throws NbtDataException */ private static function skipWhitespace(ByteBuffer $stream, string $terminator) : bool{ - while($stream->getUnreadLength() > 0){ + while($stream->getReadOffset() < $stream->getUsedLength()){ $b = $stream->readByteArray(1); if($b === $terminator){ return false; @@ -143,7 +143,7 @@ private static function skipWhitespace(ByteBuffer $stream, string $terminator) : continue; } - $stream->setOffset($stream->getOffset() - 1); + $stream->setReadOffset($stream->getReadOffset() - 1); return true; } @@ -156,10 +156,9 @@ private static function skipWhitespace(ByteBuffer $stream, string $terminator) : * @throws NbtDataException */ private static function readBreak(ByteBuffer $stream, string $terminator) : bool{ - if($stream->getUnreadLength() <= 0){ + if($stream->getReadOffset() >= $stream->getUsedLength()){ throw new NbtDataException("Syntax error: unexpected end of stream, expected '$terminator'"); } - $offset = $stream->getOffset(); $c = $stream->readByteArray(1); if($c === ","){ return false; @@ -168,7 +167,7 @@ private static function readBreak(ByteBuffer $stream, string $terminator) : bool return true; } - throw new NbtDataException("Syntax error: unexpected '$c' end at offset $offset"); + throw new NbtDataException("Syntax error: unexpected '$c' end"); } /** @@ -180,15 +179,12 @@ private static function readValue(ByteBuffer $stream) : Tag{ $value = ""; $inQuotes = false; - $offset = $stream->getOffset(); - $foundEnd = false; /** @var Tag|null $retval */ $retval = null; - while($stream->getUnreadLength() > 0){ - $offset = $stream->getOffset(); + while($stream->getReadOffset() < $stream->getUsedLength()){ $c = $stream->readByteArray(1); if($inQuotes){ //anything is allowed inside quotes, except unescaped quotes @@ -203,7 +199,7 @@ private static function readValue(ByteBuffer $stream) : Tag{ } }else{ if($c === "," or $c === "}" or $c === "]"){ //end of parent tag - $stream->setOffset($stream->getOffset() - 1); //the caller needs to be able to read this character + $stream->setReadOffset($stream->getReadOffset() - 1); //the caller needs to be able to read this character $foundEnd = true; break; } @@ -214,19 +210,19 @@ private static function readValue(ByteBuffer $stream) : Tag{ } if($foundEnd){ //unexpected non-whitespace character after end of value - throw new NbtDataException("Syntax error: unexpected '$c' after end of value at offset $offset"); + throw new NbtDataException("Syntax error: unexpected '$c' after end of value"); } } if($c === '"'){ //start of quoted string if($value !== ""){ - throw new NbtDataException("Syntax error: unexpected quote at offset $offset"); + throw new NbtDataException("Syntax error: unexpected quote"); } $inQuotes = true; }elseif($c === "{"){ //start of compound tag if($value !== ""){ - throw new NbtDataException("Syntax error: unexpected compound start at offset $offset (enclose in double quotes for literal)"); + throw new NbtDataException("Syntax error: unexpected compound start (enclose in double quotes for literal)"); } $retval = self::parseCompound($stream); @@ -234,7 +230,7 @@ private static function readValue(ByteBuffer $stream) : Tag{ }elseif($c === "["){ //start of list tag - TODO: arrays if($value !== ""){ - throw new NbtDataException("Syntax error: unexpected list start at offset $offset (enclose in double quotes for literal)"); + throw new NbtDataException("Syntax error: unexpected list start (enclose in double quotes for literal)"); } $retval = self::parseList($stream); @@ -251,10 +247,10 @@ private static function readValue(ByteBuffer $stream) : Tag{ } if($value === ""){ - throw new NbtDataException("Syntax error: empty value at offset $offset"); + throw new NbtDataException("Syntax error: empty value"); } if(!$foundEnd){ - throw new NbtDataException("Syntax error: unexpected end of stream at offset $offset"); + throw new NbtDataException("Syntax error: unexpected end of stream"); } $last = strtolower(substr($value, -1)); @@ -299,12 +295,11 @@ private static function readValue(ByteBuffer $stream) : Tag{ */ private static function readKey(ByteBuffer $stream) : string{ $key = ""; - $offset = $stream->getOffset(); $inQuotes = false; $foundEnd = false; - while($stream->getUnreadLength() > 0){ + while($stream->getReadOffset() < $stream->getUsedLength()){ $c = $stream->readByteArray(1); if($inQuotes){ @@ -328,18 +323,18 @@ private static function readKey(ByteBuffer $stream) : string{ } if($foundEnd){ //unexpected non-whitespace character after end of value - throw new NbtDataException("Syntax error: unexpected '$c' after end of value at offset $offset"); + throw new NbtDataException("Syntax error: unexpected '$c' after end of value"); } } if($c === '"'){ //start of quoted string if($key !== ""){ - throw new NbtDataException("Syntax error: unexpected quote at offset $offset"); + throw new NbtDataException("Syntax error: unexpected quote"); } $inQuotes = true; }elseif($c === "{" or $c === "}" or $c === "[" or $c === "]" or $c === ","){ - throw new NbtDataException("Syntax error: unexpected '$c' at offset $offset (enclose in double quotes for literal)"); + throw new NbtDataException("Syntax error: unexpected '$c' (enclose in double quotes for literal)"); }else{ //any other character $key .= $c; } @@ -347,10 +342,10 @@ private static function readKey(ByteBuffer $stream) : string{ } if($key === ""){ - throw new NbtDataException("Syntax error: invalid empty key at offset $offset"); + throw new NbtDataException("Syntax error: invalid empty key"); } if(!$foundEnd){ - throw new NbtDataException("Syntax error: unexpected end of stream at offset $offset"); + throw new NbtDataException("Syntax error: unexpected end of stream"); } return $key;