From 975cdd309dbe3d1ab9dff64f70cd3d11cfae4bfd Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Thu, 30 Jun 2022 11:02:30 +0200 Subject: [PATCH] NBT: implement NBT::helper::readInt64Array --- src/lib/nbt.cpp | 15 +++++++++++++-- src/test/nbt_helpers.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index dab1a2f..2ee8499 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -141,8 +141,19 @@ namespace NBT { } ErrorOr> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - //TODO: implement - return ErrorOr>({0}); + // get size prefix + ErrorOr size = readInt32(data, dataSize, currentPosition); + if (size.isError) return ErrorOr>(true, size.errorCode); + + // get content + if (currentPosition+4+(size.value*8) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN_ERROR); + std::vector result = std::vector(); + for (int i=0; i nextInt64 = readInt64(data, dataSize, currentPosition+4+(i*8)); + if (nextInt64.isError) return ErrorOr>(true, nextInt64.errorCode); + result.push_back(nextInt64.value); + } + return ErrorOr>(result); } } diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index d62883a..145b82d 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -184,5 +184,29 @@ int main(){ std::cout << "Passed int32[] NBT helper test" << std::endl; + // int64 "array" ################################################### + // read successfully + currentPosition = 44; + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).value == std::vector({2966230773313449776, 3544952156018063160, 4123673537695186954, 4413034230074983236, 4991755612779596620})); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == false); + // read empty + currentPosition = 112; + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).value == std::vector()); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == false); + // read overrun + currentPosition = 20; + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + // read with size partially out of bounds + currentPosition = 114; + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + // read out of bounds + currentPosition = 200; + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + + std::cout << "Passed int64[] NBT helper test" << std::endl; + return 0; }