Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Feb 1, 2024
1 parent 7bbadf8 commit fd0262e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
11 changes: 5 additions & 6 deletions src/BaseNbtSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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){
Expand Down
51 changes: 23 additions & 28 deletions src/JsonNbtParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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){
Expand Down Expand Up @@ -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'");
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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");
}

/**
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -214,27 +210,27 @@ 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);
$foundEnd = true;

}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);
Expand All @@ -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));
Expand Down Expand Up @@ -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){
Expand All @@ -328,29 +323,29 @@ 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;
}
}
}

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;
Expand Down

0 comments on commit fd0262e

Please sign in to comment.