lib/nbt: Finish implementing containedDataLength, rename nextTagTotalSize->totalTagSize and nextTagDataLength->containedDataLength

BodgeMaster-unfinished
BodgeMaster 2022-08-13 15:56:59 +02:00
parent 0c92bdf8fd
commit 149285c357
3 changed files with 13 additions and 6 deletions

View File

@ -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<uint64_t> nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
ErrorOr<uint64_t> totalTagSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
uint8_t nextTag;
if (dataSize <= currentPosition) {
return ErrorOr<uint64_t>(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<int32_t> 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<int32_t> 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>((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);

View File

@ -67,8 +67,8 @@ namespace NBT {
void writeInt64Array(std::vector<uint8_t>* destination, std::vector<int64_t> data);
void writeInt64Array(std::vector<uint8_t>* destination, int64_t data[], uint32_t dataSize);
ErrorOr<uint64_t> nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int32_t> nextTagDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<uint64_t> totalTagSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int32_t> containedDataLength(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
}
namespace TagType {

View File

@ -37,8 +37,8 @@ int main() {
0x33, 0x30, 0x30, 0x6e, 0x75, 0x6f, 0x70, 0x09,
0x00, 0x03
};
ErrorOr<uint64_t> totalSize = NBT::helper::nextTagTotalSize(endTestData, 34, 0);
ErrorOr<int32_t> dataLength = NBT::helper::nextTagDataLength(endTestData, 34, 0);
ErrorOr<uint64_t> totalSize = NBT::helper::totalTagSize(endTestData, 34, 0);
ErrorOr<int32_t> dataLength = NBT::helper::containedDataLength(endTestData, 34, 0);
ASSERT(!totalSize.isError);
ASSERT(totalSize.value == 1);