lib/nbt: Fix a bug in nextTagTotalSize and significantly improve readability by removing redundant code
parent
7a2c1d7d57
commit
027f324f03
|
@ -377,7 +377,7 @@ namespace NBT {
|
||||||
//
|
//
|
||||||
// Does not work for compound tags and lists. This is an intended
|
// Does not work for compound tags and lists. This is an intended
|
||||||
// feature as compound tags and lists need to be dealt with
|
// feature as compound tags and lists need to be dealt with
|
||||||
// separately to avoid unnecessarily complex code.
|
// separately to avoid unnecessarily long and complex code.
|
||||||
//
|
//
|
||||||
// Regardinng lists specifically: The size of some lists can can
|
// Regardinng lists specifically: The size of some lists can can
|
||||||
// be determined easily by looking at the contained data type and
|
// be determined easily by looking at the contained data type and
|
||||||
|
@ -397,81 +397,53 @@ namespace NBT {
|
||||||
if (nextTag == TagType::COMPOUND || nextTag == TagType::LIST) return ErrorOr<uint64_t>(false, ErrorCodes::NOT_YET_KNOWN);
|
if (nextTag == TagType::COMPOUND || nextTag == TagType::LIST) return ErrorOr<uint64_t>(false, ErrorCodes::NOT_YET_KNOWN);
|
||||||
// deal with end tag before trying to access the name
|
// deal with end tag before trying to access the name
|
||||||
if (nextTag == TagType::END) return ErrorOr<uint64_t>(1);
|
if (nextTag == TagType::END) return ErrorOr<uint64_t>(1);
|
||||||
// get name size
|
|
||||||
ErrorOr<int16_t> nameSize = helper::readInt16(data, dataSize, currentPosition+1);
|
ErrorOr<int16_t> nameSize = helper::readInt16(data, dataSize, currentPosition+1);
|
||||||
if (nameSize.isError) {
|
if (nameSize.isError) {
|
||||||
return ErrorOr<uint64_t>(true, nameSize.errorCode);
|
return ErrorOr<uint64_t>(true, nameSize.errorCode);
|
||||||
}
|
}
|
||||||
|
// add type byte and name size bytes
|
||||||
|
uint64_t prefixSize = (uint64_t) nameSize.value + 3;
|
||||||
switch (nextTag) {
|
switch (nextTag) {
|
||||||
case TagType::INT8:
|
case TagType::INT8:
|
||||||
// type byte + name size + data byte -> 4 bytes
|
return ErrorOr<uint64_t>(prefixSize+1);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+4);
|
|
||||||
case TagType::INT16:
|
case TagType::INT16:
|
||||||
// type byte + name size + 2 data bytes -> 5 bytes
|
return ErrorOr<uint64_t>(prefixSize+2);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+5);
|
|
||||||
case TagType::INT32:
|
case TagType::INT32:
|
||||||
// type byte + name size + 4 data bytes -> 7 bytes
|
return ErrorOr<uint64_t>(prefixSize+4);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+7);
|
|
||||||
case TagType::INT64:
|
case TagType::INT64:
|
||||||
// type byte + name size + 8 data bytes -> 11 bytes
|
return ErrorOr<uint64_t>(prefixSize+8);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+11);
|
|
||||||
case TagType::FLOAT:
|
case TagType::FLOAT:
|
||||||
// type byte + name size + 4 data bytes -> 7 bytes
|
return ErrorOr<uint64_t>(prefixSize+4);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+7);
|
|
||||||
case TagType::DOUBLE:
|
case TagType::DOUBLE:
|
||||||
// type byte + name size + 8 data bytes -> 11 bytes
|
return ErrorOr<uint64_t>(prefixSize+8);
|
||||||
return ErrorOr<uint64_t>((uint64_t) nameSize.value+11);
|
|
||||||
case TagType::INT8_ARRAY: {
|
case TagType::INT8_ARRAY: {
|
||||||
// type byte + name size + 4 size bytes -> 7 bytes
|
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize);
|
||||||
uint64_t totalSize = (uint64_t) nameSize.value+7;
|
if (arrayLength.isError) {
|
||||||
|
return ErrorOr<uint64_t>(true, arrayLength.errorCode);
|
||||||
// add size of actual data (1 byte per entry)
|
|
||||||
ErrorOr<int32_t> arraySize = helper::readInt32(data, dataSize, currentPosition+totalSize);
|
|
||||||
if (arraySize.isError) {
|
|
||||||
return ErrorOr<uint64_t>(true, arraySize.errorCode);
|
|
||||||
}
|
}
|
||||||
totalSize += (uint64_t) arraySize.value;
|
return ErrorOr<uint64_t>((uint64_t) arrayLength.value + prefixSize + 4);
|
||||||
|
|
||||||
return ErrorOr<uint64_t>(totalSize);
|
|
||||||
}
|
}
|
||||||
case TagType::STRING: {
|
case TagType::STRING: {
|
||||||
// type byte + name size + 2 size bytes -> 5 bytes
|
ErrorOr<int16_t> stringSize = helper::readInt16(data, dataSize, currentPosition+prefixSize);
|
||||||
uint64_t totalSize = (uint64_t) nameSize.value+5;
|
|
||||||
|
|
||||||
// add size of actual data
|
|
||||||
ErrorOr<int16_t> stringSize = helper::readInt16(data, dataSize, currentPosition+totalSize);
|
|
||||||
if (stringSize.isError) {
|
if (stringSize.isError) {
|
||||||
return ErrorOr<uint64_t>(true, stringSize.errorCode);
|
return ErrorOr<uint64_t>(true, stringSize.errorCode);
|
||||||
}
|
}
|
||||||
totalSize += (uint64_t) stringSize.value;
|
return ErrorOr<uint64_t>((uint64_t) stringSize.value + prefixSize + 2);
|
||||||
|
|
||||||
return ErrorOr<uint64_t>(totalSize);
|
|
||||||
}
|
}
|
||||||
case TagType::INT32_ARRAY: {
|
case TagType::INT32_ARRAY: {
|
||||||
// type byte + name size + 4 size bytes -> 7 bytes
|
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize);
|
||||||
uint64_t totalSize = (uint64_t) nameSize.value+7;
|
if (arrayLength.isError) {
|
||||||
|
return ErrorOr<uint64_t>(true, arrayLength.errorCode);
|
||||||
// add size of actual data (4 bytes per entry)
|
|
||||||
ErrorOr<int16_t> arraySize = helper::readInt16(data, dataSize, currentPosition+totalSize);
|
|
||||||
if (arraySize.isError) {
|
|
||||||
return ErrorOr<uint64_t>(true, arraySize.errorCode);
|
|
||||||
}
|
}
|
||||||
totalSize += (uint64_t) arraySize.value*4;
|
return ErrorOr<uint64_t>((uint64_t) arrayLength.value*4 + prefixSize + 4);
|
||||||
|
|
||||||
return ErrorOr<uint64_t>(totalSize);
|
|
||||||
}
|
}
|
||||||
case TagType::INT64_ARRAY: {
|
case TagType::INT64_ARRAY: {
|
||||||
// type byte + name size + 4 size bytes -> 7 bytes
|
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize);
|
||||||
uint64_t totalSize = (uint64_t) nameSize.value+7;
|
if (arrayLength.isError) {
|
||||||
|
return ErrorOr<uint64_t>(true, arrayLength.errorCode);
|
||||||
// add size of actual data (8 bytes per entry)
|
|
||||||
ErrorOr<int16_t> arraySize = helper::readInt16(data, dataSize, currentPosition+totalSize);
|
|
||||||
if (arraySize.isError) {
|
|
||||||
return ErrorOr<uint64_t>(true, arraySize.errorCode);
|
|
||||||
}
|
}
|
||||||
totalSize += (uint64_t) arraySize.value*8;
|
return ErrorOr<uint64_t>((uint64_t) arrayLength.value*8 + prefixSize + 4);
|
||||||
|
|
||||||
return ErrorOr<uint64_t>(totalSize);
|
|
||||||
}
|
}
|
||||||
// unknown tag or parsing error
|
// unknown tag or parsing error
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue