From 8c7be385ae3e4a297373df9e2d695ab56b933a48 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Mon, 4 Jul 2022 18:27:51 +0200 Subject: [PATCH] NBT: add write helpers for int32 and int64 --- src/lib/nbt.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ src/test/nbt_helpers.cpp | 34 ++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index fc3473f..603f768 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -208,10 +208,58 @@ namespace NBT { delete value; } + //FIXME: endian dependent implementation void writeInt32(std::vector* destination, int32_t data) { + int32_t* value = new int32_t; + uint8_t* valueAsBytes = reinterpret_cast(value); + *value = data; + #ifdef FOSSVG_BIG_ENDIAN + destination->push_back(*valueAsBytes); + destination->push_back(*(valueAsBytes+1)); + destination->push_back(*(valueAsBytes+2)); + destination->push_back(*(valueAsBytes+3)); + #else + #ifdef FOSSVG_LITTLE_ENDIAN + destination->push_back(*(valueAsBytes+3)); + destination->push_back(*(valueAsBytes+2)); + destination->push_back(*(valueAsBytes+1)); + destination->push_back(*valueAsBytes); + #else + #error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." + #endif + #endif + delete value; } + //FIXME: endian dependent implementation void writeInt64(std::vector* destination, int64_t data) { + int64_t* value = new int64_t; + uint8_t* valueAsBytes = reinterpret_cast(value); + *value = data; + #ifdef FOSSVG_BIG_ENDIAN + destination->push_back(*valueAsBytes); + destination->push_back(*(valueAsBytes+1)); + destination->push_back(*(valueAsBytes+2)); + destination->push_back(*(valueAsBytes+3)); + destination->push_back(*(valueAsBytes+4)); + destination->push_back(*(valueAsBytes+5)); + destination->push_back(*(valueAsBytes+6)); + destination->push_back(*(valueAsBytes+7)); + #else + #ifdef FOSSVG_LITTLE_ENDIAN + destination->push_back(*(valueAsBytes+7)); + destination->push_back(*(valueAsBytes+6)); + destination->push_back(*(valueAsBytes+5)); + destination->push_back(*(valueAsBytes+4)); + destination->push_back(*(valueAsBytes+3)); + destination->push_back(*(valueAsBytes+2)); + destination->push_back(*(valueAsBytes+1)); + destination->push_back(*valueAsBytes); + #else + #error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." + #endif + #endif + delete value; } //FIXME: we just assume that float is a single-precision IEEE754 diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index bac01b3..9b35b27 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -86,7 +86,7 @@ int main(){ delete writeInt16TestResult; ASSERT(dereferencedWriteInt16TestResult[0] == (uint8_t) 0xAB && dereferencedWriteInt16TestResult[1] == (uint8_t) 0xCD); - std::cout << "Passed writeInt8 NBT helper test" << std::endl; + std::cout << "Passed writeInt16 NBT helper test" << std::endl; // int32 ########################################################### // read successfully @@ -112,6 +112,19 @@ int main(){ std::cout << "Passed readInt32 NBT helper test" << std::endl; + std::vector* writeInt32TestResult = new std::vector(); + NBT::helper::writeInt32(writeInt32TestResult, (int32_t) 0x12345678); + std::vector dereferencedWriteInt32TestResult = *writeInt32TestResult; + delete writeInt32TestResult; + ASSERT( + dereferencedWriteInt32TestResult[0] == (uint8_t) 0x12 && + dereferencedWriteInt32TestResult[1] == (uint8_t) 0x34 && + dereferencedWriteInt32TestResult[2] == (uint8_t) 0x56 && + dereferencedWriteInt32TestResult[3] == (uint8_t) 0x78 + ); + + std::cout << "Passed writeInt32 NBT helper test" << std::endl; + // int64 ########################################################### // read successfully currentPosition = 1; @@ -134,7 +147,24 @@ int main(){ ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); - std::cout << "Passed int64 NBT helper test" << std::endl; + std::cout << "Passed readInt64 NBT helper test" << std::endl; + + std::vector* writeInt64TestResult = new std::vector(); + NBT::helper::writeInt64(writeInt64TestResult, (int64_t) 0x0123456789ABCDEF); + std::vector dereferencedWriteInt64TestResult = *writeInt64TestResult; + delete writeInt64TestResult; + ASSERT( + dereferencedWriteInt64TestResult[0] == (uint8_t) 0x01 && + dereferencedWriteInt64TestResult[1] == (uint8_t) 0x23 && + dereferencedWriteInt64TestResult[2] == (uint8_t) 0x45 && + dereferencedWriteInt64TestResult[3] == (uint8_t) 0x67 && + dereferencedWriteInt64TestResult[4] == (uint8_t) 0x89 && + dereferencedWriteInt64TestResult[5] == (uint8_t) 0xAB && + dereferencedWriteInt64TestResult[6] == (uint8_t) 0xCD && + dereferencedWriteInt64TestResult[7] == (uint8_t) 0xEF + ); + + std::cout << "Passed writeInt32 NBT helper test" << std::endl; //##################################################################