From edcf40d5a561b47439d4046530f2fb2178982b24 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Thu, 30 Jun 2022 10:45:12 +0200 Subject: [PATCH] NBT: implement NBT::helper::readInt32Array --- 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 93b6ca9..dab1a2f 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -125,8 +125,19 @@ namespace NBT { //} ErrorOr> readInt32Array(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*4) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN_ERROR); + std::vector result = std::vector(); + for (int i=0; i nextInt32 = readInt32(data, dataSize, currentPosition+4+(i*4)); + if (nextInt32.isError) return ErrorOr>(true, nextInt32.errorCode); + result.push_back(nextInt32.value); + } + return ErrorOr>(result); } ErrorOr> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index e3e9286..d62883a 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -160,5 +160,29 @@ int main(){ std::cout << "Passed int8[] NBT helper test" << std::endl; + // int32 "array" ################################################### + // read successfully + currentPosition = 68; + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).value == std::vector({1027489600, 1094861636, 1162233672, 1229605708, 1296977744, 1364349780, 1431721816, 1499093852, 1566465888, 1633837924})); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == false); + // read empty + currentPosition = 112; + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).value == std::vector()); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == false); + // read overrun + currentPosition = 20; + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + // read with size partially out of bounds + currentPosition = 114; + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + // read out of bounds + currentPosition = 200; + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + + std::cout << "Passed int32[] NBT helper test" << std::endl; + return 0; }