NBT: fix bug in readInt64, improve readability

BodgeMaster-unfinished
BodgeMaster 2022-06-29 00:05:02 +02:00
parent ed429633e5
commit d43ef67ed1
1 changed files with 16 additions and 2 deletions

View File

@ -62,12 +62,26 @@ namespace NBT {
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
if (dataSize<currentPosition+4) return ErrorOr<int32_t>(true, ErrorCodes::RANGE_ERROR); if (dataSize<currentPosition+4) return ErrorOr<int32_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int32_t>((int32_t) ((static_cast<int32_t>(data[currentPosition]) << 24) | (static_cast<int32_t>(data[currentPosition+1]) << 16) | (static_cast<int32_t>(data[currentPosition+2]) << 8) | static_cast<int32_t>(data[currentPosition+3]))); return ErrorOr<int32_t>((int32_t) (
(static_cast<int32_t>(data[currentPosition ]) << 24) |
(static_cast<int32_t>(data[currentPosition+1]) << 16) |
(static_cast<int32_t>(data[currentPosition+2]) << 8) |
static_cast<int32_t>(data[currentPosition+3])
));
} }
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
if (dataSize<currentPosition+8) return ErrorOr<int64_t>(true, ErrorCodes::RANGE_ERROR); if (dataSize<currentPosition+8) return ErrorOr<int64_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int64_t>((int64_t) ((static_cast<int64_t>(data[currentPosition]) << 56) | (static_cast<int64_t>(data[currentPosition+1]) << 48) | (static_cast<int64_t>(data[currentPosition+2]) << 40) | (static_cast<int64_t>(data[currentPosition+3]) << 32) | (static_cast<int64_t>(data[currentPosition]) << 24) | (static_cast<int64_t>(data[currentPosition+1]) << 16) | (static_cast<int64_t>(data[currentPosition+2]) << 8) | static_cast<int64_t>(data[currentPosition+3]))); return ErrorOr<int64_t>((int64_t) (
(static_cast<int64_t>(data[currentPosition ]) << 56) |
(static_cast<int64_t>(data[currentPosition+1]) << 48) |
(static_cast<int64_t>(data[currentPosition+2]) << 40) |
(static_cast<int64_t>(data[currentPosition+3]) << 32) |
(static_cast<int64_t>(data[currentPosition+4]) << 24) |
(static_cast<int64_t>(data[currentPosition+5]) << 16) |
(static_cast<int64_t>(data[currentPosition+6]) << 8) |
static_cast<int64_t>(data[currentPosition+7])
));
} }
//FIXME: we just assume that float is a single-precision IEEE754 //FIXME: we just assume that float is a single-precision IEEE754