FOSS-VG/src/lib/nbt.cpp

114 lines
4.1 KiB
C++
Raw Normal View History

2022-06-27 11:46:13 +02:00
// Copyright 2022, FOSS-VG Developers and Contributers
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#include <bit>
2022-06-27 04:50:32 +02:00
#include <cstdint>
#include <vector>
2022-06-27 04:50:32 +02:00
#include "nbt.h++"
#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?
}
}
2022-06-27 04:50:32 +02:00
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);
return ErrorOr<int8_t>((int8_t) data[currentPosition]);
2022-06-27 04:50:32 +02:00
}
ErrorOr<int16_t> readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<int16_t>((int16_t) 0);
2022-06-27 04:50:32 +02:00
}
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<int32_t>((int32_t) 0);
2022-06-27 04:50:32 +02:00
}
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<int64_t>((int64_t) 0);
2022-06-27 04:50:32 +02:00
}
//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) {
2022-06-27 04:50:32 +02:00
//TODO: implement assuming standard single-precision IEEE754 float
// Alternatively, maybe calculate a floating point number by using
// the stored value as math instructions?
return ErrorOr<float>(0.0f);
}
//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) {
2022-06-27 04:50:32 +02:00
//TODO: implement assuming standard double-precision IEEE754 float
// Alternatively, maybe calculate a floating point number by using
// the stored value as math instructions?
return ErrorOr<double>(0.0);
}
ErrorOr<std::vector<int8_t>> readInt8Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<std::vector<int8_t>>({0});
}
// Maybe use a struct that holds decoded (de-Java-fied) string
// data, decoded size, and original size? Original size is needed
// so the parser knows where to continue.
//ErrorOr<> readString(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
// return ErrorOr<>("");
//}
ErrorOr<std::vector<int32_t>> readInt32Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<std::vector<int32_t>>({0});
}
ErrorOr<std::vector<int64_t>> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
2022-06-27 04:50:32 +02:00
//TODO: implement
return ErrorOr<std::vector<int64_t>>({0});
}
}
bool validateRawNBTData(uint8_t data[], uint64_t dataSize){
2022-06-27 04:50:32 +02:00
//state machine?
//TODO: implement
return false;
}
}