lib/nbt: Capitalize NBT::Helper because I feel like it

Soda
BodgeMaster 2022-09-15 02:00:07 +02:00
parent 6149418f52
commit ad291ee77d
2 changed files with 35 additions and 35 deletions

View File

@ -36,7 +36,7 @@
#endif #endif
namespace NBT { namespace NBT {
namespace helper { namespace Helper {
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
if (currentPosition>=dataSize) return ErrorOr<int8_t>(true, ErrorCodes::OUT_OF_RANGE); if (currentPosition>=dataSize) return ErrorOr<int8_t>(true, ErrorCodes::OUT_OF_RANGE);
return ErrorOr<int8_t>((int8_t) data[currentPosition]); return ErrorOr<int8_t>((int8_t) data[currentPosition]);
@ -92,7 +92,7 @@ namespace NBT {
*(valueAsBytes+2) = data[currentPosition+1]; *(valueAsBytes+2) = data[currentPosition+1];
*(valueAsBytes+3) = data[currentPosition]; *(valueAsBytes+3) = data[currentPosition];
#else #else
#error "NBT::helper::readFloat: An implementation for your endianness is unavailable." #error "NBT::Helper::readFloat: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
float dereferencedValue = *value; float dereferencedValue = *value;
@ -126,7 +126,7 @@ namespace NBT {
*(valueAsBytes+6) = data[currentPosition+1]; *(valueAsBytes+6) = data[currentPosition+1];
*(valueAsBytes+7) = data[currentPosition]; *(valueAsBytes+7) = data[currentPosition];
#else #else
#error "NBT::helper::readDouble: An implementation for your endianness is unavailable." #error "NBT::Helper::readDouble: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
double dereferencedValue = *value; double dereferencedValue = *value;
@ -232,7 +232,7 @@ namespace NBT {
destination->push_back(*(valueAsBytes+1)); destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes); destination->push_back(*valueAsBytes);
#else #else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." #error "NBT::Helper::writeInt16: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
delete value; delete value;
@ -255,7 +255,7 @@ namespace NBT {
destination->push_back(*(valueAsBytes+1)); destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes); destination->push_back(*valueAsBytes);
#else #else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." #error "NBT::Helper::writeInt16: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
delete value; delete value;
@ -286,7 +286,7 @@ namespace NBT {
destination->push_back(*(valueAsBytes+1)); destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes); destination->push_back(*valueAsBytes);
#else #else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." #error "NBT::Helper::writeInt16: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
delete value; delete value;
@ -309,7 +309,7 @@ namespace NBT {
destination->push_back(*(valueAsBytes+1)); destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes); destination->push_back(*valueAsBytes);
#else #else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." #error "NBT::Helper::writeInt16: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
delete value; delete value;
@ -340,7 +340,7 @@ namespace NBT {
destination->push_back(*(valueAsBytes+1)); destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes); destination->push_back(*valueAsBytes);
#else #else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable." #error "NBT::Helper::writeInt16: An implementation for your endianness is unavailable."
#endif #endif
#endif #endif
delete value; delete value;
@ -363,7 +363,7 @@ namespace NBT {
void writeString(std::vector<uint8_t>* destination, tiny_utf8::string data) { void writeString(std::vector<uint8_t>* destination, tiny_utf8::string data) {
ErrorOr<std::vector<uint8_t>> exportedString = JavaCompat::exportJavaString(data); ErrorOr<std::vector<uint8_t>> exportedString = JavaCompat::exportJavaString(data);
if(exportedString.isError){ if(exportedString.isError){
std::cerr << "NBT::helpers::writeString encountered an error: " << (int) exportedString.errorCode << std::endl; std::cerr << "NBT::Helpers::writeString encountered an error: " << (int) exportedString.errorCode << std::endl;
std::abort(); std::abort();
} }
*destination = exportedString.value; *destination = exportedString.value;
@ -426,7 +426,7 @@ namespace NBT {
// deal with end tag before trying to access the name // deal with end tag before trying to access the name
if (nextTag == TagType::END) return ErrorOr<uint64_t>(1); if (nextTag == TagType::END) return ErrorOr<uint64_t>(1);
ErrorOr<int16_t> nameSize = helper::readInt16(data, dataSize, currentPosition+1); ErrorOr<int16_t> nameSize = Helper::readInt16(data, dataSize, currentPosition+1);
if (nameSize.isError) { if (nameSize.isError) {
return ErrorOr<uint64_t>(true, nameSize.errorCode); return ErrorOr<uint64_t>(true, nameSize.errorCode);
} }
@ -446,28 +446,28 @@ namespace NBT {
case TagType::DOUBLE: case TagType::DOUBLE:
return ErrorOr<uint64_t>(prefixSize+8); return ErrorOr<uint64_t>(prefixSize+8);
case TagType::INT8_ARRAY: { case TagType::INT8_ARRAY: {
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize); ErrorOr<int32_t> arrayLength = Helper::readInt32(data, dataSize, currentPosition+prefixSize);
if (arrayLength.isError) { if (arrayLength.isError) {
return ErrorOr<uint64_t>(true, arrayLength.errorCode); return ErrorOr<uint64_t>(true, arrayLength.errorCode);
} }
return ErrorOr<uint64_t>((uint64_t) arrayLength.value + prefixSize + 4); return ErrorOr<uint64_t>((uint64_t) arrayLength.value + prefixSize + 4);
} }
case TagType::STRING: { case TagType::STRING: {
ErrorOr<int16_t> stringSize = helper::readInt16(data, dataSize, currentPosition+prefixSize); ErrorOr<int16_t> stringSize = Helper::readInt16(data, dataSize, currentPosition+prefixSize);
if (stringSize.isError) { if (stringSize.isError) {
return ErrorOr<uint64_t>(true, stringSize.errorCode); return ErrorOr<uint64_t>(true, stringSize.errorCode);
} }
return ErrorOr<uint64_t>((uint64_t) stringSize.value + prefixSize + 2); return ErrorOr<uint64_t>((uint64_t) stringSize.value + prefixSize + 2);
} }
case TagType::INT32_ARRAY: { case TagType::INT32_ARRAY: {
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize); ErrorOr<int32_t> arrayLength = Helper::readInt32(data, dataSize, currentPosition+prefixSize);
if (arrayLength.isError) { if (arrayLength.isError) {
return ErrorOr<uint64_t>(true, arrayLength.errorCode); return ErrorOr<uint64_t>(true, arrayLength.errorCode);
} }
return ErrorOr<uint64_t>((uint64_t) arrayLength.value*4 + prefixSize + 4); return ErrorOr<uint64_t>((uint64_t) arrayLength.value*4 + prefixSize + 4);
} }
case TagType::INT64_ARRAY: { case TagType::INT64_ARRAY: {
ErrorOr<int32_t> arrayLength = helper::readInt32(data, dataSize, currentPosition+prefixSize); ErrorOr<int32_t> arrayLength = Helper::readInt32(data, dataSize, currentPosition+prefixSize);
if (arrayLength.isError) { if (arrayLength.isError) {
return ErrorOr<uint64_t>(true, arrayLength.errorCode); return ErrorOr<uint64_t>(true, arrayLength.errorCode);
} }
@ -509,7 +509,7 @@ namespace NBT {
return ErrorOr<int32_t>(1); return ErrorOr<int32_t>(1);
} }
ErrorOr<int16_t> nameSize = helper::readInt16(data, dataSize, currentPosition+1); ErrorOr<int16_t> nameSize = Helper::readInt16(data, dataSize, currentPosition+1);
if (nameSize.isError) { if (nameSize.isError) {
return ErrorOr<int32_t>(true, nameSize.errorCode); return ErrorOr<int32_t>(true, nameSize.errorCode);
} }
@ -517,10 +517,10 @@ namespace NBT {
uint64_t prefixSize = (uint64_t) nameSize.value + 3; uint64_t prefixSize = (uint64_t) nameSize.value + 3;
switch (nextTag) { switch (nextTag) {
case TagType::INT8_ARRAY: { case TagType::INT8_ARRAY: {
return helper::readInt32(data, dataSize, currentPosition+prefixSize); return Helper::readInt32(data, dataSize, currentPosition+prefixSize);
} }
case TagType::STRING: { case TagType::STRING: {
ErrorOr<int16_t> stringSize = helper::readInt16(data, dataSize, currentPosition+prefixSize); ErrorOr<int16_t> stringSize = Helper::readInt16(data, dataSize, currentPosition+prefixSize);
if (stringSize.isError) { if (stringSize.isError) {
return ErrorOr<int32_t>(true, stringSize.errorCode); return ErrorOr<int32_t>(true, stringSize.errorCode);
} }
@ -528,13 +528,13 @@ namespace NBT {
} }
case TagType::LIST: { case TagType::LIST: {
// add an additional byte for the contained data type // add an additional byte for the contained data type
return helper::readInt32(data, dataSize, currentPosition+prefixSize+1); return Helper::readInt32(data, dataSize, currentPosition+prefixSize+1);
} }
case TagType::INT32_ARRAY: { case TagType::INT32_ARRAY: {
return helper::readInt32(data, dataSize, currentPosition+prefixSize); return Helper::readInt32(data, dataSize, currentPosition+prefixSize);
} }
case TagType::INT64_ARRAY: { case TagType::INT64_ARRAY: {
return helper::readInt32(data, dataSize, currentPosition+prefixSize); return Helper::readInt32(data, dataSize, currentPosition+prefixSize);
} }
default: default:
// unknown tag or parsing error // unknown tag or parsing error
@ -562,7 +562,7 @@ namespace NBT {
uint8_t nameSizeSlice[] = {data[1], data[2]}; uint8_t nameSizeSlice[] = {data[1], data[2]};
ErrorOr<int16_t> readIntResult = helper::readInt16(nameSizeSlice, 2, 0); ErrorOr<int16_t> readIntResult = Helper::readInt16(nameSizeSlice, 2, 0);
if(!readIntResult.isError){ if(!readIntResult.isError){
this->nameSize = readIntResult.value; this->nameSize = readIntResult.value;
}else{ }else{
@ -574,7 +574,7 @@ namespace NBT {
nameSlice[i] = data[i+1]; nameSlice[i] = data[i+1];
} }
ErrorOr<tiny_utf8::string> readStringResult = helper::readString(nameSlice, this->nameSize, 0); ErrorOr<tiny_utf8::string> readStringResult = Helper::readString(nameSlice, this->nameSize, 0);
if(!readStringResult.isError){ if(!readStringResult.isError){
this->name = readStringResult.value; this->name = readStringResult.value;
}else{ }else{
@ -601,7 +601,7 @@ namespace NBT {
// headerless tags // headerless tags
// //
// add one byte to position to skip the type byte // add one byte to position to skip the type byte
ErrorOr<int32_t> elementCount = helper::readInt32(data, dataSize, initialPosition+1); ErrorOr<int32_t> elementCount = Helper::readInt32(data, dataSize, initialPosition+1);
if (elementCount.isError) { if (elementCount.isError) {
return false; return false;
} }
@ -634,7 +634,7 @@ namespace NBT {
} }
case TagType::INT8_ARRAY: { case TagType::INT8_ARRAY: {
for (int32_t i=0; i<elementCount.value; i++) { for (int32_t i=0; i<elementCount.value; i++) {
ErrorOr<std::vector<int8_t>> nextArray = helper::readInt8Array(data, dataSize, initialPosition+*processedDataSize); ErrorOr<std::vector<int8_t>> nextArray = Helper::readInt8Array(data, dataSize, initialPosition+*processedDataSize);
if (nextArray.isError) { if (nextArray.isError) {
return false; return false;
} }
@ -644,12 +644,12 @@ namespace NBT {
} }
case TagType::STRING: { case TagType::STRING: {
for (int32_t i=0; i<elementCount.value; i++) { for (int32_t i=0; i<elementCount.value; i++) {
ErrorOr<tiny_utf8::string> nextString = helper::readString(data, dataSize, initialPosition+*processedDataSize); ErrorOr<tiny_utf8::string> nextString = Helper::readString(data, dataSize, initialPosition+*processedDataSize);
if (nextString.isError) { if (nextString.isError) {
return false; return false;
} }
// this cannot be an error because it just got checked // this cannot be an error because it just got checked
int16_t nextStringSize = helper::readInt16(data, dataSize, initialPosition+*processedDataSize).value; int16_t nextStringSize = Helper::readInt16(data, dataSize, initialPosition+*processedDataSize).value;
*processedDataSize += (uint64_t) nextStringSize + 2; *processedDataSize += (uint64_t) nextStringSize + 2;
} }
return true; return true;
@ -685,7 +685,7 @@ namespace NBT {
} }
case TagType::INT32_ARRAY: { case TagType::INT32_ARRAY: {
for (int32_t i=0; i<elementCount.value; i++) { for (int32_t i=0; i<elementCount.value; i++) {
ErrorOr<std::vector<int32_t>> nextArray = helper::readInt32Array(data, dataSize, initialPosition+*processedDataSize); ErrorOr<std::vector<int32_t>> nextArray = Helper::readInt32Array(data, dataSize, initialPosition+*processedDataSize);
if (nextArray.isError) { if (nextArray.isError) {
return false; return false;
} }
@ -695,7 +695,7 @@ namespace NBT {
} }
case TagType::INT64_ARRAY: { case TagType::INT64_ARRAY: {
for (int32_t i=0; i<elementCount.value; i++) { for (int32_t i=0; i<elementCount.value; i++) {
ErrorOr<std::vector<int64_t>> nextArray = helper::readInt64Array(data, dataSize, initialPosition+*processedDataSize); ErrorOr<std::vector<int64_t>> nextArray = Helper::readInt64Array(data, dataSize, initialPosition+*processedDataSize);
if (nextArray.isError) { if (nextArray.isError) {
return false; return false;
} }
@ -737,11 +737,11 @@ namespace NBT {
uint64_t currentPosition = initialPosition; uint64_t currentPosition = initialPosition;
#define return if (processedDataSize!=nullptr) *processedDataSize = currentPosition-initialPosition; return #define return if (processedDataSize!=nullptr) *processedDataSize = currentPosition-initialPosition; return
while (currentPosition<dataSize) { while (currentPosition<dataSize) {
ErrorOr<uint64_t> nextTagSize = helper::totalTagSize(data, dataSize, currentPosition); ErrorOr<uint64_t> nextTagSize = Helper::totalTagSize(data, dataSize, currentPosition);
if (nextTagSize.isError) { if (nextTagSize.isError) {
if (nextTagSize.errorCode == ErrorCodes::NOT_YET_KNOWN) { if (nextTagSize.errorCode == ErrorCodes::NOT_YET_KNOWN) {
// attempt parsing the name // attempt parsing the name
ErrorOr<tiny_utf8::string> tagName = helper::readString(data, dataSize, currentPosition+1); ErrorOr<tiny_utf8::string> tagName = Helper::readString(data, dataSize, currentPosition+1);
if (tagName.isError) { if (tagName.isError) {
return false; return false;
} }
@ -750,7 +750,7 @@ namespace NBT {
// //
// there is no way this is an error bc it gets // there is no way this is an error bc it gets
// checked while trying to parse the string above // checked while trying to parse the string above
int16_t nameSize = helper::readInt16(data, dataSize, currentPosition+1).value; int16_t nameSize = Helper::readInt16(data, dataSize, currentPosition+1).value;
uint64_t* processedTagSize = new uint64_t; uint64_t* processedTagSize = new uint64_t;
*processedTagSize = 0; *processedTagSize = 0;
@ -791,7 +791,7 @@ namespace NBT {
// nameSize cannot be an error here bc it got checked in // nameSize cannot be an error here bc it got checked in
// nextTagSize() already // nextTagSize() already
int16_t nameSize = helper::readInt16(data, dataSize, currentPosition+1).value; int16_t nameSize = Helper::readInt16(data, dataSize, currentPosition+1).value;
// attempt parsing the name // attempt parsing the name
// //
@ -800,7 +800,7 @@ namespace NBT {
// being guarded against with // being guarded against with
// if (currentPosition + nextTagSize.value > dataSize) return false; // if (currentPosition + nextTagSize.value > dataSize) return false;
// It might, however, turn out to be a useful check in the future. // It might, however, turn out to be a useful check in the future.
ErrorOr<tiny_utf8::string> name = helper::readString(data, dataSize, currentPosition+1); ErrorOr<tiny_utf8::string> name = Helper::readString(data, dataSize, currentPosition+1);
if (name.isError) { if (name.isError) {
return false; return false;
} }
@ -825,7 +825,7 @@ namespace NBT {
// in the future. // in the future.
// //
// type byte + two name size bytes = 3 // type byte + two name size bytes = 3
ErrorOr<tiny_utf8::string> content = helper::readString(data, dataSize, currentPosition+nameSize+3); ErrorOr<tiny_utf8::string> content = Helper::readString(data, dataSize, currentPosition+nameSize+3);
if (content.isError) { if (content.isError) {
return false; return false;
} }

View File

@ -41,7 +41,7 @@
#include "error.hpp" #include "error.hpp"
namespace NBT { namespace NBT {
namespace helper { namespace Helper {
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition); 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<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<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);