diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 3fb198e..c320291 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -336,15 +336,31 @@ namespace NBT { //} void writeInt32Array(std::vector* destination, std::vector data) { + writeInt32(destination, data.size()); + for(int32_t element: data){ + writeInt32(destination, element); + } } - void writeInt32Array(std::vector* destination, int32_t data[], uint64_t dataSize) { + void writeInt32Array(std::vector* destination, int32_t data[], uint32_t dataSize) { + writeInt32(destination, dataSize); + for(uint32_t i = 0; i* destination, std::vector data) { + writeInt32(destination, data.size()); + for(int64_t element: data){ + writeInt64(destination, element); + } } - void writeInt64Array(std::vector* destination, int64_t data[], uint64_t dataSize) { + void writeInt64Array(std::vector* destination, int64_t data[], uint32_t dataSize) { + writeInt32(destination, dataSize); + for(uint32_t i = 0; i* destination, int8_t data[], uint32_t dataSize); //void writeString(std::vector* destination, data); void writeInt32Array(std::vector* destination, std::vector data); - void writeInt32Array(std::vector* destination, int32_t data[], uint64_t dataSize); + void writeInt32Array(std::vector* destination, int32_t data[], uint32_t dataSize); void writeInt64Array(std::vector* destination, std::vector data); - void writeInt64Array(std::vector* destination, int64_t data[], uint64_t dataSize); + void writeInt64Array(std::vector* destination, int64_t data[], uint32_t dataSize); } bool validateRawNBTData(uint8_t data[], int length); diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index c2eabcc..3351794 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -256,7 +256,44 @@ int main(){ ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); - std::cout << "Passed int32Array NBT helper test" << std::endl; + std::cout << "Passed readInt32Array NBT helper test" << std::endl; + + std::vector* int32ArrayTestOutput = new std::vector(); + int32_t input32[] = {0x01234567, 0x01234567}; + NBT::helper::writeInt32Array(int32ArrayTestOutput, input32, 2); + ASSERT( + int32ArrayTestOutput->at(0) == 0x00 && + int32ArrayTestOutput->at(1) == 0x00 && + int32ArrayTestOutput->at(2) == 0x00 && + int32ArrayTestOutput->at(3) == 0x02 && + int32ArrayTestOutput->at(4) == 0x01 && + int32ArrayTestOutput->at(5) == 0x23 && + int32ArrayTestOutput->at(6) == 0x45 && + int32ArrayTestOutput->at(7) == 0x67 && + int32ArrayTestOutput->at(8) == 0x01 && + int32ArrayTestOutput->at(9) == 0x23 && + int32ArrayTestOutput->at(10) == 0x45 && + int32ArrayTestOutput->at(11) == 0x67 + ); + int32ArrayTestOutput->clear(); + NBT::helper::writeInt32Array(int32ArrayTestOutput, std::vector({0x01234567, 0x01234567})); + ASSERT( + int32ArrayTestOutput->at(0) == 0x00 && + int32ArrayTestOutput->at(1) == 0x00 && + int32ArrayTestOutput->at(2) == 0x00 && + int32ArrayTestOutput->at(3) == 0x02 && + int32ArrayTestOutput->at(4) == 0x01 && + int32ArrayTestOutput->at(5) == 0x23 && + int32ArrayTestOutput->at(6) == 0x45 && + int32ArrayTestOutput->at(7) == 0x67 && + int32ArrayTestOutput->at(8) == 0x01 && + int32ArrayTestOutput->at(9) == 0x23 && + int32ArrayTestOutput->at(10) == 0x45 && + int32ArrayTestOutput->at(11) == 0x67 + ); + delete int32ArrayTestOutput; + + std::cout << "Passed writeInt32Array NBT helper test" << std::endl; // int64 "array" ################################################### // read successfully @@ -280,7 +317,61 @@ int main(){ ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); - std::cout << "Passed int64Array NBT helper test" << std::endl; + std::cout << "Passed readInt64Array NBT helper test" << std::endl; + + std::vector* int64ArrayTestOutput = new std::vector(); + int64_t input64[] = {0x0123456789ABCDEF, 0x0123456789ABCDEF}; + NBT::helper::writeInt64Array(int64ArrayTestOutput, input64, 2); + ASSERT( + int64ArrayTestOutput->at(0) == 0x00 && + int64ArrayTestOutput->at(1) == 0x00 && + int64ArrayTestOutput->at(2) == 0x00 && + int64ArrayTestOutput->at(3) == 0x02 && + int64ArrayTestOutput->at(4) == 0x01 && + int64ArrayTestOutput->at(5) == 0x23 && + int64ArrayTestOutput->at(6) == 0x45 && + int64ArrayTestOutput->at(7) == 0x67 && + int64ArrayTestOutput->at(8) == 0x89 && + int64ArrayTestOutput->at(9) == 0xAB && + int64ArrayTestOutput->at(10) == 0xCD && + int64ArrayTestOutput->at(11) == 0xEF && + int64ArrayTestOutput->at(12) == 0x01 && + int64ArrayTestOutput->at(13) == 0x23 && + int64ArrayTestOutput->at(14) == 0x45 && + int64ArrayTestOutput->at(15) == 0x67 && + int64ArrayTestOutput->at(16) == 0x89 && + int64ArrayTestOutput->at(17) == 0xAB && + int64ArrayTestOutput->at(18) == 0xCD && + int64ArrayTestOutput->at(19) == 0xEF + ); + int64ArrayTestOutput->clear(); + NBT::helper::writeInt64Array(int64ArrayTestOutput, std::vector({0x0123456789ABCDEF, 0x0123456789ABCDEF})); + ASSERT( + int64ArrayTestOutput->at(0) == 0x00 && + int64ArrayTestOutput->at(1) == 0x00 && + int64ArrayTestOutput->at(2) == 0x00 && + int64ArrayTestOutput->at(3) == 0x02 && + int64ArrayTestOutput->at(4) == 0x01 && + int64ArrayTestOutput->at(5) == 0x23 && + int64ArrayTestOutput->at(6) == 0x45 && + int64ArrayTestOutput->at(7) == 0x67 && + int64ArrayTestOutput->at(8) == 0x89 && + int64ArrayTestOutput->at(9) == 0xAB && + int64ArrayTestOutput->at(10) == 0xCD && + int64ArrayTestOutput->at(11) == 0xEF && + int64ArrayTestOutput->at(12) == 0x01 && + int64ArrayTestOutput->at(13) == 0x23 && + int64ArrayTestOutput->at(14) == 0x45 && + int64ArrayTestOutput->at(15) == 0x67 && + int64ArrayTestOutput->at(16) == 0x89 && + int64ArrayTestOutput->at(17) == 0xAB && + int64ArrayTestOutput->at(18) == 0xCD && + int64ArrayTestOutput->at(19) == 0xEF && + true + ); + delete int64ArrayTestOutput; + + std::cout << "Passed writeInt32Array NBT helper test" << std::endl; // float32 ["float" in the current implementation :( ] ############# uint8_t dataForFloat32Test[] = {0xC7, 0x77, 0x77, 0x77};