diff options
author | Tiger Wang <ziwei.tiger@outlook.com> | 2021-01-11 17:39:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-11 17:39:43 +0100 |
commit | eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 (patch) | |
tree | b07daae788f918b83eeb0bdbd51e49292f1c8d88 /src/WorldStorage/FastNBT.cpp | |
parent | Fixed switch-ups regarding some slab and stair recipes (#5099) (diff) | |
download | cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.gz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.bz2 cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.lz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.xz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.zst cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.zip |
Diffstat (limited to '')
-rw-r--r-- | src/WorldStorage/FastNBT.cpp | 78 |
1 files changed, 37 insertions, 41 deletions
diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index ec43d2f12..d5b9fd0f7 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -137,7 +137,7 @@ std::error_code make_error_code(eNBTParseError a_Err) noexcept #define NEEDBYTES(N, ERR) \ do { \ - if (m_Length - m_Pos < static_cast<size_t>(N)) \ + if (m_Data.size() - m_Pos < static_cast<size_t>(N)) \ { \ return ERR; \ } \ @@ -147,9 +147,8 @@ std::error_code make_error_code(eNBTParseError a_Err) noexcept -cParsedNBT::cParsedNBT(const char * a_Data, size_t a_Length) : +cParsedNBT::cParsedNBT(const ContiguousByteBufferView a_Data) : m_Data(a_Data), - m_Length(a_Length), m_Pos(0) { m_Error = Parse(); @@ -161,12 +160,12 @@ cParsedNBT::cParsedNBT(const char * a_Data, size_t a_Length) : eNBTParseError cParsedNBT::Parse(void) { - if (m_Length < 3) + if (m_Data.size() < 3) { // Data too short return eNBTParseError::npNeedBytes; } - if (m_Data[0] != TAG_Compound) + if (m_Data[0] != std::byte(TAG_Compound)) { // The top-level tag must be a Compound return eNBTParseError::npNoTopLevelCompound; @@ -190,7 +189,7 @@ eNBTParseError cParsedNBT::ReadString(size_t & a_StringStart, size_t & a_StringL { NEEDBYTES(2, eNBTParseError::npStringMissingLength); a_StringStart = m_Pos + 2; - a_StringLen = static_cast<size_t>(GetBEShort(m_Data + m_Pos)); + a_StringLen = static_cast<size_t>(GetBEShort(m_Data.data() + m_Pos)); NEEDBYTES(2 + a_StringLen, eNBTParseError::npStringInvalidLength); m_Pos += 2 + a_StringLen; return eNBTParseError::npSuccess; @@ -210,8 +209,8 @@ eNBTParseError cParsedNBT::ReadCompound(void) for (;;) { NEEDBYTES(1, eNBTParseError::npCompoundImbalancedTag); - const char TagTypeNum = m_Data[m_Pos]; - if ((TagTypeNum < TAG_Min) || (TagTypeNum > TAG_Max)) + const auto TagTypeNum = m_Data[m_Pos]; + if ((TagTypeNum < std::byte(TAG_Min)) || (TagTypeNum > std::byte(TAG_Max))) { return eNBTParseError::npUnknownTag; } @@ -248,10 +247,10 @@ eNBTParseError cParsedNBT::ReadList(eTagType a_ChildrenType) // Read the count: NEEDBYTES(4, eNBTParseError::npListMissingLength); - int Count = GetBEInt(m_Data + m_Pos); + int Count = GetBEInt(m_Data.data() + m_Pos); m_Pos += 4; auto MinChildSize = GetMinTagSize(a_ChildrenType); - if ((Count < 0) || (Count > static_cast<int>((m_Length - m_Pos) / MinChildSize))) + if ((Count < 0) || (Count > static_cast<int>((m_Data.size() - m_Pos) / MinChildSize))) { return eNBTParseError::npListInvalidLength; } @@ -312,7 +311,7 @@ eNBTParseError cParsedNBT::ReadTag(void) case TAG_ByteArray: { NEEDBYTES(4, eNBTParseError::npArrayMissingLength); - int len = GetBEInt(m_Data + m_Pos); + int len = GetBEInt(m_Data.data() + m_Pos); m_Pos += 4; if (len < 0) { @@ -344,7 +343,7 @@ eNBTParseError cParsedNBT::ReadTag(void) case TAG_IntArray: { NEEDBYTES(4, eNBTParseError::npArrayMissingLength); - int len = GetBEInt(m_Data + m_Pos); + int len = GetBEInt(m_Data.data() + m_Pos); m_Pos += 4; if (len < 0) { @@ -392,7 +391,7 @@ int cParsedNBT::FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLen { if ( (m_Tags[static_cast<size_t>(Child)].m_NameLength == a_NameLength) && - (memcmp(m_Data + m_Tags[static_cast<size_t>(Child)].m_NameStart, a_Name, a_NameLength) == 0) + (memcmp(m_Data.data() + m_Tags[static_cast<size_t>(Child)].m_NameStart, a_Name, a_NameLength) == 0) ) { return Child; @@ -470,9 +469,9 @@ cFastNBTWriter::cFastNBTWriter(const AString & a_RootTagName) : m_CurrentStack(0) { m_Stack[0].m_Type = TAG_Compound; - m_Result.reserve(100 * 1024); - m_Result.push_back(TAG_Compound); - WriteString(a_RootTagName.data(), static_cast<UInt16>(a_RootTagName.size())); + m_Result.reserve(100 KiB); + m_Result.push_back(std::byte(TAG_Compound)); + WriteString(a_RootTagName); } @@ -502,7 +501,7 @@ void cFastNBTWriter::EndCompound(void) ASSERT(m_CurrentStack > 0); ASSERT(IsStackTopCompound()); - m_Result.push_back(TAG_End); + m_Result.push_back(std::byte(TAG_End)); --m_CurrentStack; } @@ -520,8 +519,8 @@ void cFastNBTWriter::BeginList(const AString & a_Name, eTagType a_ChildrenType) TagCommon(a_Name, TAG_List); - m_Result.push_back(static_cast<char>(a_ChildrenType)); - m_Result.append(4, static_cast<char>(0)); + m_Result.push_back(std::byte(a_ChildrenType)); + m_Result.append(4, std::byte(0)); ++m_CurrentStack; m_Stack[m_CurrentStack].m_Type = TAG_List; @@ -540,7 +539,7 @@ void cFastNBTWriter::EndList(void) ASSERT(m_Stack[m_CurrentStack].m_Type == TAG_List); // Update the list count: - SetBEInt(const_cast<char *>(m_Result.c_str() + m_Stack[m_CurrentStack].m_Pos), m_Stack[m_CurrentStack].m_Count); + SetBEInt(m_Result.data() + m_Stack[m_CurrentStack].m_Pos, m_Stack[m_CurrentStack].m_Count); --m_CurrentStack; } @@ -552,7 +551,7 @@ void cFastNBTWriter::EndList(void) void cFastNBTWriter::AddByte(const AString & a_Name, unsigned char a_Value) { TagCommon(a_Name, TAG_Byte); - m_Result.push_back(static_cast<char>(a_Value)); + m_Result.push_back(std::byte(a_Value)); } @@ -563,7 +562,7 @@ void cFastNBTWriter::AddShort(const AString & a_Name, Int16 a_Value) { TagCommon(a_Name, TAG_Short); UInt16 Value = htons(static_cast<UInt16>(a_Value)); - m_Result.append(reinterpret_cast<const char *>(&Value), 2); + m_Result.append(reinterpret_cast<const std::byte *>(&Value), 2); } @@ -574,7 +573,7 @@ void cFastNBTWriter::AddInt(const AString & a_Name, Int32 a_Value) { TagCommon(a_Name, TAG_Int); UInt32 Value = htonl(static_cast<UInt32>(a_Value)); - m_Result.append(reinterpret_cast<const char *>(&Value), 4); + m_Result.append(reinterpret_cast<const std::byte *>(&Value), 4); } @@ -585,7 +584,7 @@ void cFastNBTWriter::AddLong(const AString & a_Name, Int64 a_Value) { TagCommon(a_Name, TAG_Long); UInt64 Value = HostToNetwork8(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 8); + m_Result.append(reinterpret_cast<const std::byte *>(&Value), 8); } @@ -596,7 +595,7 @@ void cFastNBTWriter::AddFloat(const AString & a_Name, float a_Value) { TagCommon(a_Name, TAG_Float); UInt32 Value = HostToNetwork4(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 4); + m_Result.append(reinterpret_cast<const std::byte *>(&Value), 4); } @@ -607,7 +606,7 @@ void cFastNBTWriter::AddDouble(const AString & a_Name, double a_Value) { TagCommon(a_Name, TAG_Double); UInt64 Value = HostToNetwork8(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 8); + m_Result.append(reinterpret_cast<const std::byte *>(&Value), 8); } @@ -618,8 +617,8 @@ void cFastNBTWriter::AddString(const AString & a_Name, const std::string_view a_ { TagCommon(a_Name, TAG_String); const UInt16 Length = htons(static_cast<UInt16>(a_Value.size())); - m_Result.append(reinterpret_cast<const char *>(&Length), sizeof(Length)); - m_Result.append(a_Value); + m_Result.append(reinterpret_cast<const std::byte *>(&Length), sizeof(Length)); + m_Result.append({ reinterpret_cast<const std::byte *>(a_Value.data()), a_Value.size() }); } @@ -630,8 +629,8 @@ void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, { TagCommon(a_Name, TAG_ByteArray); UInt32 len = htonl(static_cast<UInt32>(a_NumElements)); - m_Result.append(reinterpret_cast<const char *>(&len), 4); - m_Result.append(a_Value, a_NumElements); + m_Result.append(reinterpret_cast<const std::byte *>(&len), 4); + m_Result.append(reinterpret_cast<const std::byte *>(a_Value), a_NumElements); } @@ -648,11 +647,11 @@ void cFastNBTWriter::AddIntArray(const AString & a_Name, const Int32 * a_Value, { m_Result.reserve(size + 4 + (a_NumElements * 4)); } - m_Result.append(reinterpret_cast<const char *>(&len), 4); + m_Result.append(reinterpret_cast<const std::byte *>(&len), sizeof(len)); for (size_t i = 0; i < a_NumElements; i++) { UInt32 Element = htonl(static_cast<UInt32>(a_Value[i])); - m_Result.append(reinterpret_cast<const char *>(&Element), 4); + m_Result.append(reinterpret_cast<const std::byte *>(&Element), sizeof(Element)); } } @@ -663,20 +662,17 @@ void cFastNBTWriter::AddIntArray(const AString & a_Name, const Int32 * a_Value, void cFastNBTWriter::Finish(void) { ASSERT(m_CurrentStack == 0); - m_Result.push_back(TAG_End); + m_Result.push_back(std::byte(TAG_End)); } -void cFastNBTWriter::WriteString(const char * a_Data, UInt16 a_Length) +void cFastNBTWriter::WriteString(const std::string_view a_Data) { - UInt16 Len = htons(a_Length); - m_Result.append(reinterpret_cast<const char *>(&Len), 2); - m_Result.append(a_Data, a_Length); + // TODO check size <= short max + UInt16 Len = htons(static_cast<unsigned short>(a_Data.size())); + m_Result.append(reinterpret_cast<const std::byte *>(&Len), sizeof(Len)); + m_Result.append(reinterpret_cast<const std::byte *>(a_Data.data()), a_Data.size()); } - - - - |