diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index be63d68..26ad964 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -375,6 +375,8 @@ namespace NBT { // one that is applicable to the situation (for example replace // OUT_OF_RANGE with OVERRUN where appropriate) // + // The total size in bytes + // // Does not work for compound tags and lists. This is an intended // feature as compound tags and lists need to be dealt with // separately to avoid unnecessarily long and complex code. @@ -386,7 +388,7 @@ namespace NBT { // contents requires special attention anyway due the tag headers // of contained tags being absent so they may as well get their // own function for this as well. - ErrorOr nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { + ErrorOr totalTagSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { uint8_t nextTag; if (dataSize <= currentPosition) { return ErrorOr(true, ErrorCodes::OVERRUN); @@ -454,7 +456,10 @@ namespace NBT { //FIXME: instead of blindly passing the error code upwards, choose // one that is applicable to the situation (for example replace // OUT_OF_RANGE with OVERRUN where appropriate) - ErrorOr nextTagDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition){ + // + // Length is the number of stored elements, not to be confused with size + // which is the size in bytes. + ErrorOr containedDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition){ uint8_t nextTag; if (dataSize <= currentPosition) { @@ -496,6 +501,8 @@ namespace NBT { return ErrorOr((int32_t) stringSize.value); } case TagType::LIST: { + // add an additional byte for the contained data type + return helper::readInt32(data, dataSize, currentPosition+prefixSize+1); } case TagType::INT32_ARRAY: { return helper::readInt32(data, dataSize, currentPosition+prefixSize); diff --git a/src/lib/nbt.hpp b/src/lib/nbt.hpp index c7174d8..3da58c9 100644 --- a/src/lib/nbt.hpp +++ b/src/lib/nbt.hpp @@ -67,8 +67,8 @@ namespace NBT { void writeInt64Array(std::vector* destination, std::vector data); void writeInt64Array(std::vector* destination, int64_t data[], uint32_t dataSize); - ErrorOr nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr nextTagDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); + ErrorOr totalTagSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); + ErrorOr containedDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); } namespace TagType { diff --git a/src/test/nbt_size_helpers.cpp b/src/test/nbt_size_helpers.cpp index 01a8fea..94c6f73 100644 --- a/src/test/nbt_size_helpers.cpp +++ b/src/test/nbt_size_helpers.cpp @@ -37,8 +37,8 @@ int main() { 0x33, 0x30, 0x30, 0x6e, 0x75, 0x6f, 0x70, 0x09, 0x00, 0x03 }; - ErrorOr totalSize = NBT::helper::nextTagTotalSize(endTestData, 34, 0); - ErrorOr dataLength = NBT::helper::nextTagDataLength(endTestData, 34, 0); + ErrorOr totalSize = NBT::helper::totalTagSize(endTestData, 34, 0); + ErrorOr dataLength = NBT::helper::containedDataLength(endTestData, 34, 0); ASSERT(!totalSize.isError); ASSERT(totalSize.value == 1);