lib/nbt: remove a function used to get the next tag type which introduced unnecessary complexity

BodgeMaster-unfinished
BodgeMaster 2022-08-11 06:56:06 +02:00
parent 4363432025
commit 68fbf3ae20
1 changed files with 18 additions and 19 deletions

View File

@ -424,32 +424,27 @@ namespace NBT {
}
}
ErrorOr<uint8_t> nextTagType(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
if (dataSize <= currentPosition) {
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
} else {
return ErrorOr<uint8_t>(data[currentPosition]);
}
}
//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<uint64_t> nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
ErrorOr<uint8_t> nextTag = nextTagType(data, dataSize, currentPosition);
if (nextTag.isError) {
return ErrorOr<uint64_t>(true, nextTag.errorCode);
uint8_t nextTag;
if (dataSize <= currentPosition) {
return ErrorOr<uint64_t>(true, ErrorCodes::OVERRUN);
} else {
nextTag = data[currentPosition];
}
// deal with compound tags separately
if (nextTag.value == TagType::COMPOUND) return ErrorOr<uint64_t>(false, ErrorCodes::NOT_YET_KNOWN);
if (nextTag == TagType::COMPOUND) return ErrorOr<uint64_t>(false, ErrorCodes::NOT_YET_KNOWN);
// deal with end tag before trying to access the name
if (nextTag.value == TagType::END) return ErrorOr<uint64_t>(1);
if (nextTag == TagType::END) return ErrorOr<uint64_t>(1);
// get name size
ErrorOr<uint16_t> nameSize = (uint16_t) helper::readInt16(data, dataSize, currentPosition+1);
ErrorOr<int16_t> nameSize = helper::readInt16(data, dataSize, currentPosition+1);
if (nameSize.isError) {
return ErrorOr<uint64_t>(true, nameSize.errorCode);
}
switch (nextTag.value) {
switch (nextTag) {
case TagType::INT8:
// type byte + name size + data byte -> 4 bytes
return ErrorOr<uint64_t>((uint64_t) nameSize.value+4);
@ -566,14 +561,18 @@ namespace NBT {
}
ErrorOr<uint32_t> nextTagDataSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition){
ErrorOr<uint8_t> nextTag = nexttagType(data, dataSize, currentPosition);
if (nextTag.isError) {
return ErrorOr<int64_t>(true, nextTag.errorCode);
uint8_t nextTag;
if (dataSize <= currentPosition) {
return ErrorOr<uint32_t>(true, ErrorCodes::OVERRUN);
} else {
nextTag = data[currentPosition];
}
// deal with compound tags separately
if (nextTag.value == TagType::COMPOUND) return ErrorOr<uint64_t>(true, ErrorCodes::NOT_YET_KNOWN);
if (nextTag == TagType::COMPOUND) return ErrorOr<uint32_t>(true, ErrorCodes::NOT_YET_KNOWN);
// deal with end tag before trying to access the name
if (nextTag.value == TagType::END) return 0;
if (nextTag == TagType::END) return 0;
//TODO: implement for all the remaining types
// fall-through in case of unknown tag or parsing error
return ErrorOr<uint32_t>(true, ErrorCodes::UNKNOWN);