NBT: implement the integer helper functions
parent
12b4a8bb55
commit
7c12a92b2a
|
@ -21,46 +21,53 @@
|
|||
#include "error.h++"
|
||||
|
||||
// This is just an example for how to find out if the system is big endian
|
||||
// or little endian. Do not use this.
|
||||
int endianness_example() {
|
||||
if constexpr (std::endian::native == std::endian::big)
|
||||
{
|
||||
// Big-endian system
|
||||
return 0;
|
||||
}
|
||||
else if constexpr (std::endian::native == std::endian::little)
|
||||
{
|
||||
// Little-endian system
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Something else
|
||||
return 2;
|
||||
// How did we even end up here?
|
||||
}
|
||||
}
|
||||
// or little endian.
|
||||
//int endianness_example() {
|
||||
// if constexpr (std::endian::native == std::endian::big)
|
||||
// {
|
||||
// // Big-endian system
|
||||
// return 0;
|
||||
// }
|
||||
// else if constexpr (std::endian::native == std::endian::little)
|
||||
// {
|
||||
// // Little-endian system
|
||||
// return 1;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // Something else
|
||||
// return 2;
|
||||
// // How did we even end up here?
|
||||
// }
|
||||
//}
|
||||
|
||||
// There is supposedly also a function htobe64 and be64toh to deal with
|
||||
// converting 64 bit integers between host order and big endian.
|
||||
|
||||
// Though converting between host order and BE may not be necessary if the
|
||||
// raw NBT data is to be used as instructions for rebuilding the data in memory.
|
||||
// Doing the opposite may just be very painful.
|
||||
|
||||
namespace NBT {
|
||||
namespace helper {
|
||||
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
|
||||
if (dataSize<=currentPosition) return ErrorOr<int8_t>(true, ErrorCodes::RANGE_ERROR);
|
||||
if (dataSize<currentPosition+1) return ErrorOr<int8_t>(true, ErrorCodes::RANGE_ERROR);
|
||||
return ErrorOr<int8_t>((int8_t) data[currentPosition]);
|
||||
}
|
||||
|
||||
ErrorOr<int16_t> readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
|
||||
//TODO: implement
|
||||
return ErrorOr<int16_t>((int16_t) 0);
|
||||
if (dataSize<currentPosition+2) return ErrorOr<int16_t>(true, ErrorCodes::RANGE_ERROR);
|
||||
return ErrorOr<int16_t>((int16_t) ((static_cast<int16_t>(data[currentPosition]) << 8) | static_cast<int16_t>(data[currentPosition+1])));
|
||||
}
|
||||
|
||||
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
|
||||
//TODO: implement
|
||||
return ErrorOr<int32_t>((int32_t) 0);
|
||||
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])));
|
||||
}
|
||||
|
||||
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
|
||||
//TODO: implement
|
||||
return ErrorOr<int64_t>((int64_t) 0);
|
||||
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])));
|
||||
}
|
||||
|
||||
//FIXME: we just assume that float is a single-precision IEEE754
|
||||
|
|
|
@ -42,27 +42,18 @@
|
|||
namespace NBT {
|
||||
namespace helper {
|
||||
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<int16_t> readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
//FIXME: we just assume that float is a single-precision IEEE754
|
||||
// floating point number
|
||||
ErrorOr<float> readFloat32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
//FIXME: we just assume that double is a double-precision IEEE754
|
||||
// floating point number
|
||||
ErrorOr<double> readFloat64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<std::vector<int8_t>> readInt8Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
//ErrorOr<> readString(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<std::vector<int32_t>> readInt32Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
|
||||
ErrorOr<std::vector<int64_t>> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue