diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 811aaa1..ad5d404 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -424,32 +424,27 @@ namespace NBT { } } - ErrorOr nextTagType(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize <= currentPosition) { - return ErrorOr(true, ErrorCodes::OVERRUN); - } else { - return ErrorOr(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 nextTagTotalSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - ErrorOr nextTag = nextTagType(data, dataSize, currentPosition); - if (nextTag.isError) { - return ErrorOr(true, nextTag.errorCode); + uint8_t nextTag; + if (dataSize <= currentPosition) { + return ErrorOr(true, ErrorCodes::OVERRUN); + } else { + nextTag = data[currentPosition]; } // deal with compound tags separately - if (nextTag.value == TagType::COMPOUND) return ErrorOr(false, ErrorCodes::NOT_YET_KNOWN); + if (nextTag == TagType::COMPOUND) return ErrorOr(false, ErrorCodes::NOT_YET_KNOWN); // deal with end tag before trying to access the name - if (nextTag.value == TagType::END) return ErrorOr(1); + if (nextTag == TagType::END) return ErrorOr(1); // get name size - ErrorOr nameSize = (uint16_t) helper::readInt16(data, dataSize, currentPosition+1); + ErrorOr nameSize = helper::readInt16(data, dataSize, currentPosition+1); if (nameSize.isError) { return ErrorOr(true, nameSize.errorCode); } - switch (nextTag.value) { + switch (nextTag) { case TagType::INT8: // type byte + name size + data byte -> 4 bytes return ErrorOr((uint64_t) nameSize.value+4); @@ -566,14 +561,18 @@ namespace NBT { } ErrorOr nextTagDataSize(uint8_t data[], uint64_t dataSize, uint64_t currentPosition){ - ErrorOr nextTag = nexttagType(data, dataSize, currentPosition); - if (nextTag.isError) { - return ErrorOr(true, nextTag.errorCode); + + uint8_t nextTag; + if (dataSize <= currentPosition) { + return ErrorOr(true, ErrorCodes::OVERRUN); + } else { + nextTag = data[currentPosition]; } + // deal with compound tags separately - if (nextTag.value == TagType::COMPOUND) return ErrorOr(true, ErrorCodes::NOT_YET_KNOWN); + if (nextTag == TagType::COMPOUND) return ErrorOr(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(true, ErrorCodes::UNKNOWN);