Compare commits
2 Commits
7c12a92b2a
...
d43ef67ed1
Author | SHA1 | Date |
---|---|---|
BodgeMaster | d43ef67ed1 | |
BodgeMaster | ed429633e5 |
|
@ -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
|
||||||
|
|
|
@ -98,15 +98,15 @@ int main(){
|
||||||
// int64 ###########################################################
|
// int64 ###########################################################
|
||||||
// read successfully
|
// read successfully
|
||||||
currentPosition = 1;
|
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);
|
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
|
||||||
// begin of data
|
// begin of data
|
||||||
currentPosition = 0;
|
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);
|
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
|
||||||
// end of data
|
// end of data
|
||||||
currentPosition = 2;
|
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);
|
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
|
||||||
// partially out of bounds
|
// partially out of bounds
|
||||||
currentPosition = 3;
|
currentPosition = 3;
|
||||||
|
|
Loading…
Reference in New Issue