Compare commits

...

2 Commits

2 changed files with 19 additions and 5 deletions

View File

@ -62,12 +62,26 @@ namespace NBT {
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);
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) {
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

View File

@ -98,15 +98,15 @@ int main(){
// int64 ###########################################################
// read successfully
currentPosition = 1;
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2242829044932683046L);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2242829044932683046);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
// begin of data
currentPosition = 0;
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2170488872094606374L);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2170488872094606373);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
// end of data
currentPosition = 2;
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2315169217770759719L);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2315169217770759719);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
// partially out of bounds
currentPosition = 3;