diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 25cb694..d8c08ff 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -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 readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize<=currentPosition) return ErrorOr(true, ErrorCodes::RANGE_ERROR); + if (dataSize(true, ErrorCodes::RANGE_ERROR); return ErrorOr((int8_t) data[currentPosition]); } ErrorOr readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - //TODO: implement - return ErrorOr((int16_t) 0); + if (dataSize(true, ErrorCodes::RANGE_ERROR); + return ErrorOr((int16_t) ((static_cast(data[currentPosition]) << 8) | static_cast(data[currentPosition+1]))); } ErrorOr readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - //TODO: implement - return ErrorOr((int32_t) 0); + if (dataSize(true, ErrorCodes::RANGE_ERROR); + return ErrorOr((int32_t) ((static_cast(data[currentPosition]) << 24) | (static_cast(data[currentPosition+1]) << 16) | (static_cast(data[currentPosition+2]) << 8) | static_cast(data[currentPosition+3]))); } ErrorOr readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - //TODO: implement - return ErrorOr((int64_t) 0); + if (dataSize(true, ErrorCodes::RANGE_ERROR); + return ErrorOr((int64_t) ((static_cast(data[currentPosition]) << 56) | (static_cast(data[currentPosition+1]) << 48) | (static_cast(data[currentPosition+2]) << 40) | (static_cast(data[currentPosition+3]) << 32) | (static_cast(data[currentPosition]) << 24) | (static_cast(data[currentPosition+1]) << 16) | (static_cast(data[currentPosition+2]) << 8) | static_cast(data[currentPosition+3]))); } //FIXME: we just assume that float is a single-precision IEEE754 diff --git a/src/lib/nbt.h++ b/src/lib/nbt.h++ index 450b4b9..cb600fc 100644 --- a/src/lib/nbt.h++ +++ b/src/lib/nbt.h++ @@ -42,27 +42,18 @@ namespace NBT { namespace helper { ErrorOr readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr 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 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 readFloat64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr> readInt8Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - //ErrorOr<> readString(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr> readInt32Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); - ErrorOr> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); }