From 6d6b0dae5a8f73a5f97070c056decf30a119c67e Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Mon, 27 Jun 2022 14:22:16 +0200 Subject: [PATCH 1/6] add warranty disclaimer --- COPYING | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/COPYING b/COPYING index 9903bef..4598712 100644 --- a/COPYING +++ b/COPYING @@ -5,6 +5,10 @@ the GNU AGPL version 3. A copy of the license has been provided to you as `LICENSE.md` in the base directory of the repository or source tree. It is also available at `https://www.gnu.org/licenses/agpl-3.0.en.html`. +This software is distributed WITHOUT ANY WARRANTY (without even the +implied warranty of merchantability or fitness for any particular purpose) +as specified by the AGPL. + People who have contributed to this project: Jan Danielzick (aka. BodgeMaster) Milan Suman (aka. Shwoomple) From 9b58d165c0ea6acb668f3339c3eaad9b897b698d Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Mon, 27 Jun 2022 18:15:01 +0200 Subject: [PATCH 2/6] We have a suitable string type for UTF-8 now. --- src/lib/nbt.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 9fb6838..b2596c1 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -86,7 +86,6 @@ namespace NBT { return ErrorOr>({0}); } - //TODO: find suitable string type // 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. From ab920a114bb8aa9b5ca1ea88dade6b58d0144684 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 28 Jun 2022 14:25:32 +0200 Subject: [PATCH 3/6] comply with code style guidelines, also minor fix to them --- code_style.md | 3 ++- src/lib/nbt.cpp | 4 ++-- src/lib/nbt.h++ | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/code_style.md b/code_style.md index 3fb7125..55addec 100644 --- a/code_style.md +++ b/code_style.md @@ -42,7 +42,8 @@ all uppercase letters and underscores for their names. Variables and functions start with a lowercase letter, classes and structs with an uppercase letter. -Avoid abbreviations unless they are well known and universally used acronyms. +Avoid abbreviations unless they are well known acronyms and/or +universally used. Use explicitly sized data types where possible. For example, use `int32_t` instead of `int`. diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index b2596c1..3935af6 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -43,7 +43,7 @@ int endianness_example() { namespace NBT { namespace helpers { - ErrorOr readByte(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { + ErrorOr readInt8(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement return ErrorOr(0); } @@ -81,7 +81,7 @@ namespace NBT { return ErrorOr(0.0); } - ErrorOr> readByteArray(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { + ErrorOr> readInt8Array(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement return ErrorOr>({0}); } diff --git a/src/lib/nbt.h++ b/src/lib/nbt.h++ index 9c21abe..2c9d175 100644 --- a/src/lib/nbt.h++ +++ b/src/lib/nbt.h++ @@ -19,16 +19,16 @@ // NBT tags have a type, optionally a name which consists of the name size and the name string, optionally content type, and optionally a payload which can consist of optionally content type, optionally a content size, // and the stored content. The format in which they are stored is as follows: . All numbers are stored in big endian representation. // All tag types: -// generic representation: Tag(Byte:tag_type, String:name, uint16:name_size, byte[]:content, int32:size) +// generic representation: Tag(uint8:tag_type, String:name, uint16:name_size, byte[]:content, int32:size) // None (compound end): Tag( 0, "", 0, None, 0) => used to determine the end of a compound tag, only the type gets stored -// byte: Tag( 1, String:name, uint16:name_size, byte:content, 1) => a single signed byte, size not stored +// int8: Tag( 1, String:name, uint16:name_size, int8:content, 1) => a single signed byte, size not stored // int16: Tag( 2, String:name, uint16:name_size, int16:content, 2) => 16 bit signed integer, size not stored // int32: Tag( 3, String:name, uint16:name_size, int32:content, 4) => 32 bit signed integer, size not stored // int64: Tag( 4, String:name, uint16:name_size, int64:content, 8) => 64 bit signed integer, size not stored // float32: Tag( 5, String:name, uint16:name_size, float32:content,4) => 32 bit IEEE754 floating point number, size not stored // float64: Tag( 6, String:name, uint16:name_size, float64:content,8) => 64 bit IEEE754 floating point number, size not stored -// byte[]: Tag( 7, String:name, uint16:name_size, byte[]:content, int32:size) => content stored prefixed with size -// String: Tag( 8, String:name, uint16:name_size, byte[]:content, uint16:size) => Java style modified UTF-8 string, content stored prefixed with size +// int8[]: Tag( 7, String:name, uint16:name_size, int8[]:content, int32:size) => content stored prefixed with size +// String: Tag( 8, String:name, uint16:name_size, String:content, uint16:size) => Java style modified UTF-8 string, content stored prefixed with size // Tag[] (list): Tag( 9, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags of the same type with tag type and name information omitted prefixed by (in order) content type and size // Tag[] (compound): Tag(10, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags, last tag is always an end tag, size not stored // int32[]: Tag(11, String:name, uint16:name_size, int32[]:content,int32:size) => list of 32 bit signed integers prefixed with its size, endianness not verified at this point From 081035db328e651e7b0f2163b977618a0b153620 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 28 Jun 2022 15:19:47 +0200 Subject: [PATCH 4/6] expose helpers in the header, rename NBT::helpers to NBT::helper --- src/lib/nbt.cpp | 2 +- src/lib/nbt.h++ | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 3935af6..4642077 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -42,7 +42,7 @@ int endianness_example() { } namespace NBT { - namespace helpers { + namespace helper { ErrorOr readInt8(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement return ErrorOr(0); diff --git a/src/lib/nbt.h++ b/src/lib/nbt.h++ index 2c9d175..1ca5cfb 100644 --- a/src/lib/nbt.h++ +++ b/src/lib/nbt.h++ @@ -40,5 +40,31 @@ #include "error.h++" 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); + } + bool validateRawNBTData(uint8_t* data[], int length); } From 83d606a2c79e3a8d83904a905bed944ce0e6eacd Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 28 Jun 2022 16:01:39 +0200 Subject: [PATCH 5/6] Error: Add error codes to ErrorOr<> and add constructors This allows us to handle functions that can fail in multiple different ways --- src/lib/error.h++ | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/lib/error.h++ b/src/lib/error.h++ index e60af52..a675363 100644 --- a/src/lib/error.h++ +++ b/src/lib/error.h++ @@ -15,22 +15,59 @@ #pragma once +#include + template struct ErrorOr { bool isError; + uint8_t errorCode; T value; ErrorOr(); ErrorOr(T); + ErrorOr(bool); + ErrorOr(bool, uint8_t); + ErrorOr(bool, uint8_t, T); }; template ErrorOr::ErrorOr() { this->isError = false; + this->errorCode = 0; } template ErrorOr::ErrorOr(T value) { this->isError = false; + this->errorCode = 0; this->value = value; } + +template +ErrorOr::ErrorOr(bool isError) { + this->isError = isError; + this->errorCode = 0; +} + +template +ErrorOr::ErrorOr(bool isError, uint8_t errorCode) { + this->isError = isError; + this->errorCode = errorCode; +} + +template +ErrorOr::ErrorOr(bool isError, uint8_t errorCode, T value) { + this->isError = isError; + this->errorCode = errorCode; + this->value = value; +} + +namespace ErrorCodes { + // These are all arbitrary values used to assign error codes to different + // kinds of errors. + // Using them is optional as ErrorOr<> accepts any uint8_t value as + // error code, but they are useful for readability. + + // IndexOutOfRangeException equivalent + const uint8_t RANGE_ERROR = 1; +} From 8594f4cbb0d34c2f9d4ac624c180ba0b765eb304 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 28 Jun 2022 16:13:33 +0200 Subject: [PATCH 6/6] NBT: Fix ambiguity between int types and bool when calling the ErrorOr<> constructor --- src/lib/nbt.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 4642077..d6893f2 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -45,22 +45,22 @@ namespace NBT { namespace helper { ErrorOr readInt8(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement - return ErrorOr(0); + return ErrorOr((int8_t) 0); } ErrorOr readInt16(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement - return ErrorOr(0); + return ErrorOr((int16_t) 0); } ErrorOr readInt32(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement - return ErrorOr(0); + return ErrorOr((int32_t) 0); } ErrorOr readInt64(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) { //TODO: implement - return ErrorOr(0); + return ErrorOr((int64_t) 0); } //FIXME: we just assume that float is a single-precision IEEE754