From d6cb6e0423fe38478c69f5d249882a01d727f0ed Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 7 May 2014 12:30:30 +0200 Subject: Add repair cost to cItem, add custom name to NBTChunkSerializer and fix anvil bugs. --- src/WorldStorage/NBTChunkSerializer.cpp | 37 +++++++++++++++++++++++++++------ src/WorldStorage/WSSAnvil.cpp | 34 ++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 12 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index fd356c7de..b696a8f69 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -88,23 +88,48 @@ void cNBTChunkSerializer::Finish(void) void cNBTChunkSerializer::AddItem(const cItem & a_Item, int a_Slot, const AString & a_CompoundName) { m_Writer.BeginCompound(a_CompoundName); - m_Writer.AddShort("id", (short)(a_Item.m_ItemType)); - m_Writer.AddShort("Damage", a_Item.m_ItemDamage); - m_Writer.AddByte ("Count", a_Item.m_ItemCount); + m_Writer.AddShort("id", (short)(a_Item.m_ItemType)); + m_Writer.AddShort("Damage", a_Item.m_ItemDamage); + m_Writer.AddByte ("Count", a_Item.m_ItemCount); if (a_Slot >= 0) { m_Writer.AddByte ("Slot", (unsigned char)a_Slot); } - // Write the enchantments: - if (!a_Item.m_Enchantments.IsEmpty() || ((a_Item.m_ItemType == E_ITEM_FIREWORK_ROCKET) || (a_Item.m_ItemType == E_ITEM_FIREWORK_STAR))) + // Write the tag compound (for enchantment, firework, custom name and repair cost): + if ( + (!a_Item.m_Enchantments.IsEmpty()) || + ((a_Item.m_ItemType == E_ITEM_FIREWORK_ROCKET) || (a_Item.m_ItemType == E_ITEM_FIREWORK_STAR)) || + (a_Item.m_RepairCost > 0) || + (a_Item.m_CustomName != "") || + (a_Item.m_Lore != "") + ) { m_Writer.BeginCompound("tag"); + if (a_Item.m_RepairCost > 0) + { + m_Writer.AddInt("RepairCost", (Int32)a_Item.m_RepairCost); + } + + if ((a_Item.m_CustomName != "") || (a_Item.m_Lore != "")) + { + m_Writer.BeginCompound("display"); + if (a_Item.m_CustomName != "") + { + m_Writer.AddString("Name", a_Item.m_CustomName); + } + if (a_Item.m_Lore != "") + { + m_Writer.AddString("Lore", a_Item.m_Lore); + } + m_Writer.EndCompound(); + } + if ((a_Item.m_ItemType == E_ITEM_FIREWORK_ROCKET) || (a_Item.m_ItemType == E_ITEM_FIREWORK_STAR)) { cFireworkItem::WriteToNBTCompound(a_Item.m_FireworkItem, m_Writer, (ENUM_ITEM_ID)a_Item.m_ItemType); } - + if (!a_Item.m_Enchantments.IsEmpty()) { const char * TagName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench"; diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index f33178173..b1c6238ab 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -645,18 +645,16 @@ bool cWSSAnvil::LoadItemFromNBT(cItem & a_Item, const cParsedNBT & a_NBT, int a_ } int Damage = a_NBT.FindChildByName(a_TagIdx, "Damage"); - if ((Damage < 0) || (a_NBT.GetType(Damage) != TAG_Short)) + if ((Damage > 0) && (a_NBT.GetType(Damage) == TAG_Short)) { - return false; + a_Item.m_ItemDamage = a_NBT.GetShort(Damage); } - a_Item.m_ItemDamage = a_NBT.GetShort(Damage); int Count = a_NBT.FindChildByName(a_TagIdx, "Count"); - if ((Count < 0) || (a_NBT.GetType(Count) != TAG_Byte)) + if ((Count > 0) && (a_NBT.GetType(Count) == TAG_Byte)) { - return false; + a_Item.m_ItemCount = a_NBT.GetByte(Count); } - a_Item.m_ItemCount = a_NBT.GetByte(Count); // Find the "tag" tag, used for enchantments and other extra data int TagTag = a_NBT.FindChildByName(a_TagIdx, "tag"); @@ -666,6 +664,29 @@ bool cWSSAnvil::LoadItemFromNBT(cItem & a_Item, const cParsedNBT & a_NBT, int a_ return true; } + // Load repair cost: + int RepairCost = a_NBT.FindChildByName(TagTag, "RepairCost"); + if ((RepairCost > 0) && (a_NBT.GetType(RepairCost) == TAG_Int)) + { + a_Item.m_RepairCost = (UInt16)a_NBT.GetInt(RepairCost); + } + + // Load display name: + int DisplayTag = a_NBT.FindChildByName(TagTag, "display"); + if (DisplayTag > 0) + { + int DisplayName = a_NBT.FindChildByName(DisplayTag, "Name"); + if ((DisplayName > 0) && (a_NBT.GetType(DisplayName) == TAG_String)) + { + a_Item.m_CustomName = a_NBT.GetString(DisplayName); + } + int Lore = a_NBT.FindChildByName(DisplayTag, "Lore"); + if ((Lore > 0) && (a_NBT.GetType(Lore) == TAG_String)) + { + a_Item.m_Lore = a_NBT.GetString(Lore); + } + } + // Load enchantments: const char * EnchName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench"; int EnchTag = a_NBT.FindChildByName(TagTag, EnchName); @@ -674,6 +695,7 @@ bool cWSSAnvil::LoadItemFromNBT(cItem & a_Item, const cParsedNBT & a_NBT, int a_ EnchantmentSerializer::ParseFromNBT(a_Item.m_Enchantments, a_NBT, EnchTag); } + // Load firework data: int FireworksTag = a_NBT.FindChildByName(TagTag, ((a_Item.m_ItemType == E_ITEM_FIREWORK_STAR) ? "Fireworks" : "Explosion")); if (EnchTag > 0) { -- cgit v1.2.3 From 80bcc43c7bf1661ceeab4567143e174979ccbd59 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 7 May 2014 12:43:21 +0200 Subject: Fix MagmaCube save. --- src/WorldStorage/NBTChunkSerializer.cpp | 2 +- src/WorldStorage/WSSAnvil.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index fd356c7de..ff0241091 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -490,7 +490,7 @@ void cNBTChunkSerializer::AddMonsterEntity(cMonster * a_Monster) } case cMonster::mtMagmaCube: { - m_Writer.AddByte("Size", ((const cMagmaCube *)a_Monster)->GetSize()); + m_Writer.AddInt("Size", ((const cMagmaCube *)a_Monster)->GetSize()); break; } case cMonster::mtSheep: diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index f33178173..00dff54f0 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -1974,7 +1974,10 @@ void cWSSAnvil::LoadMagmaCubeFromNBT(cEntityList & a_Entities, const cParsedNBT { int SizeIdx = a_NBT.FindChildByName(a_TagIdx, "Size"); - if (SizeIdx < 0) { return; } + if (SizeIdx < 0) + { + return; + } int Size = a_NBT.GetInt(SizeIdx); @@ -2132,7 +2135,10 @@ void cWSSAnvil::LoadSlimeFromNBT(cEntityList & a_Entities, const cParsedNBT & a_ { int SizeIdx = a_NBT.FindChildByName(a_TagIdx, "Size"); - if (SizeIdx < 0) { return; } + if (SizeIdx < 0) + { + return; + } int Size = a_NBT.GetInt(SizeIdx); -- cgit v1.2.3 From fbb6404cc8793f5caef374e1eda243fa2d159eb5 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 7 May 2014 20:43:37 +0200 Subject: Change m_RepairCost to int. --- src/WorldStorage/NBTChunkSerializer.cpp | 2 +- src/WorldStorage/WSSAnvil.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index b696a8f69..6b317663a 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -108,7 +108,7 @@ void cNBTChunkSerializer::AddItem(const cItem & a_Item, int a_Slot, const AStrin m_Writer.BeginCompound("tag"); if (a_Item.m_RepairCost > 0) { - m_Writer.AddInt("RepairCost", (Int32)a_Item.m_RepairCost); + m_Writer.AddInt("RepairCost", a_Item.m_RepairCost); } if ((a_Item.m_CustomName != "") || (a_Item.m_Lore != "")) diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index b1c6238ab..875093910 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -668,7 +668,7 @@ bool cWSSAnvil::LoadItemFromNBT(cItem & a_Item, const cParsedNBT & a_NBT, int a_ int RepairCost = a_NBT.FindChildByName(TagTag, "RepairCost"); if ((RepairCost > 0) && (a_NBT.GetType(RepairCost) == TAG_Int)) { - a_Item.m_RepairCost = (UInt16)a_NBT.GetInt(RepairCost); + a_Item.m_RepairCost = a_NBT.GetInt(RepairCost); } // Load display name: -- cgit v1.2.3 From 99af4453ef832c4053fa26e6ed9f64804bce110a Mon Sep 17 00:00:00 2001 From: worktycho Date: Fri, 9 May 2014 17:30:47 +0100 Subject: Check the height and width values read. Fixes CID 55831 --- src/WorldStorage/MapSerializer.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/MapSerializer.cpp b/src/WorldStorage/MapSerializer.cpp index df72d1cc9..012fc52f3 100644 --- a/src/WorldStorage/MapSerializer.cpp +++ b/src/WorldStorage/MapSerializer.cpp @@ -152,6 +152,10 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT) if (CurrLine >= 0) { unsigned int Width = a_NBT.GetShort(CurrLine); + if (Width != 128) + { + return false; + } m_Map->m_Width = Width; } @@ -159,6 +163,10 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT) if (CurrLine >= 0) { unsigned int Height = a_NBT.GetShort(CurrLine); + if (Height >= 256) + { + return false; + } m_Map->m_Height = Height; } -- cgit v1.2.3 From fb58ef55beddc73500029ae6c0fe08400de550d2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 8 May 2014 20:16:35 +0200 Subject: Fixed MSVC 64-bit build warnings. --- src/WorldStorage/FastNBT.cpp | 30 +++++++++++++++------------- src/WorldStorage/FastNBT.h | 8 ++++---- src/WorldStorage/SchematicFileSerializer.cpp | 4 ++-- src/WorldStorage/WSSAnvil.cpp | 8 ++++---- src/WorldStorage/WSSCompact.cpp | 24 +++++++++++----------- 5 files changed, 38 insertions(+), 36 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index ac9a21205..52e998f88 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -38,7 +38,7 @@ -cParsedNBT::cParsedNBT(const char * a_Data, int a_Length) : +cParsedNBT::cParsedNBT(const char * a_Data, size_t a_Length) : m_Data(a_Data), m_Length(a_Length), m_Pos(0) @@ -99,8 +99,10 @@ bool cParsedNBT::ReadString(int & a_StringStart, int & a_StringLen) bool cParsedNBT::ReadCompound(void) { + ASSERT(m_Tags.size() > 0); + // Reads the latest tag as a compound - int ParentIdx = m_Tags.size() - 1; + int ParentIdx = (int)m_Tags.size() - 1; int PrevSibling = -1; for (;;) { @@ -114,13 +116,13 @@ bool cParsedNBT::ReadCompound(void) m_Tags.push_back(cFastNBTTag(TagType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; + m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; + m_Tags[ParentIdx].m_FirstChild = (int)m_Tags.size() - 1; } - PrevSibling = m_Tags.size() - 1; + PrevSibling = (int)m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadString(m_Tags.back().m_NameStart, m_Tags.back().m_NameLength)); RETURN_FALSE_IF_FALSE(ReadTag()); } // while (true) @@ -146,20 +148,20 @@ bool cParsedNBT::ReadList(eTagType a_ChildrenType) } // Read items: - int ParentIdx = m_Tags.size() - 1; + int ParentIdx = (int)m_Tags.size() - 1; int PrevSibling = -1; for (int i = 0; i < Count; i++) { m_Tags.push_back(cFastNBTTag(a_ChildrenType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; + m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; + m_Tags[ParentIdx].m_FirstChild = (int)m_Tags.size() - 1; } - PrevSibling = m_Tags.size() - 1; + PrevSibling = (int)m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadTag()); } // for (i) m_Tags[ParentIdx].m_LastChild = PrevSibling; @@ -336,7 +338,7 @@ cFastNBTWriter::cFastNBTWriter(const AString & a_RootTagName) : m_Stack[0].m_Type = TAG_Compound; m_Result.reserve(100 * 1024); m_Result.push_back(TAG_Compound); - WriteString(a_RootTagName.data(), a_RootTagName.size()); + WriteString(a_RootTagName.data(), (UInt16)a_RootTagName.size()); } @@ -389,7 +391,7 @@ void cFastNBTWriter::BeginList(const AString & a_Name, eTagType a_ChildrenType) ++m_CurrentStack; m_Stack[m_CurrentStack].m_Type = TAG_List; - m_Stack[m_CurrentStack].m_Pos = m_Result.size() - 4; + m_Stack[m_CurrentStack].m_Pos = (int)m_Result.size() - 4; m_Stack[m_CurrentStack].m_Count = 0; m_Stack[m_CurrentStack].m_ItemType = a_ChildrenType; } @@ -493,7 +495,7 @@ void cFastNBTWriter::AddString(const AString & a_Name, const AString & a_Value) void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, size_t a_NumElements) { TagCommon(a_Name, TAG_ByteArray); - Int32 len = htonl(a_NumElements); + u_long len = htonl((u_long)a_NumElements); m_Result.append((const char *)&len, 4); m_Result.append(a_Value, a_NumElements); } @@ -505,7 +507,7 @@ void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, void cFastNBTWriter::AddIntArray(const AString & a_Name, const int * a_Value, size_t a_NumElements) { TagCommon(a_Name, TAG_IntArray); - Int32 len = htonl(a_NumElements); + u_long len = htonl((u_long)a_NumElements); size_t cap = m_Result.capacity(); size_t size = m_Result.length(); if ((cap - size) < (4 + a_NumElements * 4)) @@ -534,7 +536,7 @@ void cFastNBTWriter::Finish(void) -void cFastNBTWriter::WriteString(const char * a_Data, short a_Length) +void cFastNBTWriter::WriteString(const char * a_Data, UInt16 a_Length) { Int16 Len = htons(a_Length); m_Result.append((const char *)&Len, 2); diff --git a/src/WorldStorage/FastNBT.h b/src/WorldStorage/FastNBT.h index bcf93228f..7b0af4927 100644 --- a/src/WorldStorage/FastNBT.h +++ b/src/WorldStorage/FastNBT.h @@ -114,7 +114,7 @@ Each primitive tag also stores the length of the contained data, in bytes. class cParsedNBT { public: - cParsedNBT(const char * a_Data, int a_Length); + cParsedNBT(const char * a_Data, size_t a_Length); bool IsValid(void) const {return m_IsValid; } @@ -251,7 +251,7 @@ public: protected: const char * m_Data; - int m_Length; + size_t m_Length; std::vector m_Tags; bool m_IsValid; // True if parsing succeeded @@ -319,7 +319,7 @@ protected: bool IsStackTopCompound(void) const { return (m_Stack[m_CurrentStack].m_Type == TAG_Compound); } - void WriteString(const char * a_Data, short a_Length); + void WriteString(const char * a_Data, UInt16 a_Length); inline void TagCommon(const AString & a_Name, eTagType a_Type) { @@ -330,7 +330,7 @@ protected: { // Compound: add the type and name: m_Result.push_back((char)a_Type); - WriteString(a_Name.c_str(), (short)a_Name.length()); + WriteString(a_Name.c_str(), (UInt16)a_Name.length()); } else { diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp index 9d594a084..2e356b172 100644 --- a/src/WorldStorage/SchematicFileSerializer.cpp +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -230,7 +230,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP } // Copy the block types and metas: - int NumBytes = a_BlockArea.GetBlockCount(); + int NumBytes = (int)a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) { LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", @@ -242,7 +242,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP if (AreMetasPresent) { - int NumBytes = a_BlockArea.GetBlockCount(); + int NumBytes = (int)a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) { LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index a98ed81f7..c6b0472d0 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -96,7 +96,7 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor) : gzFile gz = gzopen((FILE_IO_PREFIX + fnam).c_str(), "wb"); if (gz != NULL) { - gzwrite(gz, Writer.GetResult().data(), Writer.GetResult().size()); + gzwrite(gz, Writer.GetResult().data(), (unsigned)Writer.GetResult().size()); } gzclose(gz); } @@ -252,7 +252,7 @@ bool cWSSAnvil::LoadChunkFromData(const cChunkCoords & a_Chunk, const AString & strm.next_out = (Bytef *)Uncompressed; strm.avail_out = sizeof(Uncompressed); strm.next_in = (Bytef *)a_Data.data(); - strm.avail_in = a_Data.size(); + strm.avail_in = (uInt)a_Data.size(); int res = inflate(&strm, Z_FINISH); inflateEnd(&strm); if (res != Z_STREAM_END) @@ -2682,7 +2682,7 @@ bool cWSSAnvil::cMCAFile::SetChunkData(const cChunkCoords & a_Chunk, const AStri // Store the chunk data: m_File.Seek(ChunkSector * 4096); - unsigned ChunkSize = htonl(a_Data.size() + 1); + u_long ChunkSize = htonl((u_long)a_Data.size() + 1); if (m_File.Write(&ChunkSize, 4) != 4) { LOGWARNING("Cannot save chunk [%d, %d], writing(1) data to file \"%s\" failed", a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ, GetFileName().c_str()); @@ -2706,7 +2706,7 @@ bool cWSSAnvil::cMCAFile::SetChunkData(const cChunkCoords & a_Chunk, const AStri m_File.Write(Padding, 4096 - (BytesWritten % 4096)); // Store the header: - ChunkSize = (a_Data.size() + MCA_CHUNK_HEADER_LENGTH + 4095) / 4096; // Round data size *up* to nearest 4KB sector, make it a sector number + ChunkSize = ((u_long)a_Data.size() + MCA_CHUNK_HEADER_LENGTH + 4095) / 4096; // Round data size *up* to nearest 4KB sector, make it a sector number ASSERT(ChunkSize < 256); m_Header[LocalX + 32 * LocalZ] = htonl((ChunkSector << 8) | ChunkSize); if (m_File.Seek(0) < 0) diff --git a/src/WorldStorage/WSSCompact.cpp b/src/WorldStorage/WSSCompact.cpp index 359bac4a8..6d06b8fe3 100644 --- a/src/WorldStorage/WSSCompact.cpp +++ b/src/WorldStorage/WSSCompact.cpp @@ -601,7 +601,7 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() // Decompress the data: AString UncompressedData; { - int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, UncompressedSize); + int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, (size_t)UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -681,7 +681,7 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() // Re-compress data AString CompressedData; { - int errorcode = CompressString(Converted.data(), Converted.size(), CompressedData,m_CompressionFactor); + int errorcode = CompressString(Converted.data(), Converted.size(), CompressedData, m_CompressionFactor); if (errorcode != Z_OK) { LOGERROR("Error %d compressing data for chunk [%d, %d]", @@ -693,9 +693,9 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() } // Save into file's cache - Header->m_UncompressedSize = Converted.size(); - Header->m_CompressedSize = CompressedData.size(); - NewDataContents.append( CompressedData ); + Header->m_UncompressedSize = (int)Converted.size(); + Header->m_CompressedSize = (int)CompressedData.size(); + NewDataContents.append(CompressedData); } // Done converting @@ -731,7 +731,7 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() Offset += Header->m_CompressedSize; // Crude data integrity check: - const int ExpectedSize = (16*256*16)*2 + (16*256*16)/2; // For version 2 + const int ExpectedSize = (16 * 256 * 16) * 2 + (16 * 256 * 16) / 2; // For version 2 if (UncompressedSize < ExpectedSize) { LOGWARNING("Chunk [%d, %d] has too short decompressed data (%d bytes out of %d needed), erasing", @@ -745,7 +745,7 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() // Decompress the data: AString UncompressedData; { - int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, UncompressedSize); + int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, (size_t)UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -829,9 +829,9 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() } // Save into file's cache - Header->m_UncompressedSize = Converted.size(); - Header->m_CompressedSize = CompressedData.size(); - NewDataContents.append( CompressedData ); + Header->m_UncompressedSize = (int)Converted.size(); + Header->m_CompressedSize = (int)CompressedData.size(); + NewDataContents.append(CompressedData); } // Done converting @@ -861,7 +861,7 @@ bool cWSSCompact::LoadChunkFromData(const cChunkCoords & a_Chunk, int a_Uncompre // Decompress the data: AString UncompressedData; - int errorcode = UncompressString(a_Data.data(), a_Data.size(), UncompressedData, a_UncompressedSize); + int errorcode = UncompressString(a_Data.data(), a_Data.size(), UncompressedData, (size_t)a_UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -873,7 +873,7 @@ bool cWSSCompact::LoadChunkFromData(const cChunkCoords & a_Chunk, int a_Uncompre if (a_UncompressedSize != (int)UncompressedData.size()) { - LOGWARNING("Uncompressed data size differs (exp %d bytes, got " SIZE_T_FMT ") for chunk [%d, %d]", + LOGWARNING("Uncompressed data size differs (exp %d bytes, got " SIZE_T_FMT ") for chunk [%d, %d]", a_UncompressedSize, UncompressedData.size(), a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ ); -- cgit v1.2.3 From c4cd201f184ab3a6212dd0d1fbb9e0e40e755352 Mon Sep 17 00:00:00 2001 From: worktycho Date: Fri, 9 May 2014 17:39:13 +0100 Subject: Range Check on schematic size Fixes CID 55830 --- src/WorldStorage/SchematicFileSerializer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp index 9d594a084..115b2e869 100644 --- a/src/WorldStorage/SchematicFileSerializer.cpp +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -192,7 +192,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP int SizeX = a_NBT.GetShort(TSizeX); int SizeY = a_NBT.GetShort(TSizeY); int SizeZ = a_NBT.GetShort(TSizeZ); - if ((SizeX < 1) || (SizeY < 1) || (SizeZ < 1)) + if ((SizeX < 1) || (SizeX > 65535) || (SizeY < 1) || (SizeY > 256) || (SizeZ < 1) || (SizeZ > 65535)) { LOG("Dimensions are invalid in the schematic file: %d, %d, %d", SizeX, SizeY, SizeZ); return false; -- cgit v1.2.3 From 5b54dc3eacca565531446d9c8cc9be7be084670c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 19:33:22 +0200 Subject: Build fixes after the last size_t batch. --- src/WorldStorage/FastNBT.cpp | 4 ++-- src/WorldStorage/FastNBT.h | 14 +++++++------- src/WorldStorage/FireworksSerializer.cpp | 4 ++-- src/WorldStorage/SchematicFileSerializer.cpp | 4 ++-- src/WorldStorage/WSSAnvil.cpp | 2 +- src/WorldStorage/WSSAnvil.h | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index 52e998f88..c4a6a7a5b 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -79,11 +79,11 @@ bool cParsedNBT::Parse(void) -bool cParsedNBT::ReadString(int & a_StringStart, int & a_StringLen) +bool cParsedNBT::ReadString(size_t & a_StringStart, size_t & a_StringLen) { NEEDBYTES(2); a_StringStart = m_Pos + 2; - a_StringLen = GetBEShort(m_Data + m_Pos); + a_StringLen = (size_t)GetBEShort(m_Data + m_Pos); if (a_StringLen < 0) { // Invalid string length diff --git a/src/WorldStorage/FastNBT.h b/src/WorldStorage/FastNBT.h index 7b0af4927..fe28005ac 100644 --- a/src/WorldStorage/FastNBT.h +++ b/src/WorldStorage/FastNBT.h @@ -61,10 +61,10 @@ public: // The following members are indices into the data stream. m_DataLength == 0 if no data available // They must not be pointers, because the datastream may be copied into another AString object in the meantime. - int m_NameStart; - int m_NameLength; - int m_DataStart; - int m_DataLength; + size_t m_NameStart; + size_t m_NameLength; + size_t m_DataStart; + size_t m_DataLength; // The following members are indices into the array returned; -1 if not valid // They must not be pointers, because pointers would not survive std::vector reallocation @@ -135,7 +135,7 @@ public: /** Returns the length of the tag's data, in bytes. Not valid for Compound or List tags! */ - int GetDataLength (int a_Tag) const + size_t GetDataLength (int a_Tag) const { ASSERT(m_Tags[(size_t)a_Tag].m_Type != TAG_List); ASSERT(m_Tags[(size_t)a_Tag].m_Type != TAG_Compound); @@ -256,10 +256,10 @@ protected: bool m_IsValid; // True if parsing succeeded // Used while parsing: - int m_Pos; + size_t m_Pos; bool Parse(void); - bool ReadString(int & a_StringStart, int & a_StringLen); // Reads a simple string (2 bytes length + data), sets the string descriptors + bool ReadString(size_t & a_StringStart, size_t & a_StringLen); // Reads a simple string (2 bytes length + data), sets the string descriptors bool ReadCompound(void); // Reads the latest tag as a compound bool ReadList(eTagType a_ChildrenType); // Reads the latest tag as a list of items of type a_ChildrenType bool ReadTag(void); // Reads the latest tag, depending on its m_Type setting diff --git a/src/WorldStorage/FireworksSerializer.cpp b/src/WorldStorage/FireworksSerializer.cpp index e0cd69634..263c77a1c 100644 --- a/src/WorldStorage/FireworksSerializer.cpp +++ b/src/WorldStorage/FireworksSerializer.cpp @@ -96,7 +96,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB if (ExplosionName == "Colors") { // Divide by four as data length returned in bytes - int DataLength = a_NBT.GetDataLength(explosiontag); + size_t DataLength = a_NBT.GetDataLength(explosiontag); // round to the next highest multiple of four DataLength -= DataLength % 4; if (DataLength == 0) @@ -112,7 +112,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB } else if (ExplosionName == "FadeColors") { - int DataLength = a_NBT.GetDataLength(explosiontag) / 4; + size_t DataLength = a_NBT.GetDataLength(explosiontag) / 4; // round to the next highest multiple of four DataLength -= DataLength % 4; if (DataLength == 0) diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp index 2e356b172..5cbff376b 100644 --- a/src/WorldStorage/SchematicFileSerializer.cpp +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -230,7 +230,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP } // Copy the block types and metas: - int NumBytes = (int)a_BlockArea.GetBlockCount(); + size_t NumBytes = a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) { LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", @@ -242,7 +242,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP if (AreMetasPresent) { - int NumBytes = (int)a_BlockArea.GetBlockCount(); + size_t NumBytes = a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) { LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index c6b0472d0..52cea462c 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -1078,7 +1078,7 @@ void cWSSAnvil::LoadCommandBlockFromNBT(cBlockEntityList & a_BlockEntities, cons -void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength) +void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, size_t a_IDTagLength) { if (strncmp(a_IDTag, "Boat", a_IDTagLength) == 0) { diff --git a/src/WorldStorage/WSSAnvil.h b/src/WorldStorage/WSSAnvil.h index 1773ee882..1493f5f33 100644 --- a/src/WorldStorage/WSSAnvil.h +++ b/src/WorldStorage/WSSAnvil.h @@ -145,7 +145,7 @@ protected: void LoadMobHeadFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadCommandBlockFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); - void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength); + void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, size_t a_IDTagLength); void LoadBoatFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadEnderCrystalFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx); -- cgit v1.2.3 From d67ea97a266412c2ca1d45d5e907f3072e0b53a2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 19:57:59 +0200 Subject: More size_t fixes. --- src/WorldStorage/WSSAnvil.cpp | 10 +++++----- src/WorldStorage/WSSAnvil.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 52cea462c..d310c9124 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -405,7 +405,7 @@ bool cWSSAnvil::LoadChunkFromNBT(const cChunkCoords & a_Chunk, const cParsedNBT -void cWSSAnvil::CopyNBTData(const cParsedNBT & a_NBT, int a_Tag, const AString & a_ChildName, char * a_Destination, int a_Length) +void cWSSAnvil::CopyNBTData(const cParsedNBT & a_NBT, int a_Tag, const AString & a_ChildName, char * a_Destination, size_t a_Length) { int Child = a_NBT.FindChildByName(a_Tag, a_ChildName); if ((Child >= 0) && (a_NBT.GetType(Child) == TAG_ByteArray) && (a_NBT.GetDataLength(Child) == a_Length)) @@ -440,8 +440,8 @@ bool cWSSAnvil::SaveChunkToNBT(const cChunkCoords & a_Chunk, cFastNBTWriter & a_ // Save blockdata: a_Writer.BeginList("Sections", TAG_Compound); - int SliceSizeBlock = cChunkDef::Width * cChunkDef::Width * 16; - int SliceSizeNibble = SliceSizeBlock / 2; + size_t SliceSizeBlock = cChunkDef::Width * cChunkDef::Width * 16; + size_t SliceSizeNibble = SliceSizeBlock / 2; const char * BlockTypes = (const char *)(Serializer.m_BlockTypes); const char * BlockMetas = (const char *)(Serializer.m_BlockMetas); #ifdef DEBUG_SKYLIGHT @@ -2630,14 +2630,14 @@ bool cWSSAnvil::cMCAFile::GetChunkData(const cChunkCoords & a_Chunk, AString & a unsigned ChunkLocation = ntohl(m_Header[LocalX + 32 * LocalZ]); unsigned ChunkOffset = ChunkLocation >> 8; - m_File.Seek(ChunkOffset * 4096); + m_File.Seek((int)ChunkOffset * 4096); int ChunkSize = 0; if (m_File.Read(&ChunkSize, 4) != 4) { return false; } - ChunkSize = ntohl(ChunkSize); + ChunkSize = ntohl((u_long)ChunkSize); char CompressionType = 0; if (m_File.Read(&CompressionType, 1) != 1) { diff --git a/src/WorldStorage/WSSAnvil.h b/src/WorldStorage/WSSAnvil.h index 1493f5f33..7542a828a 100644 --- a/src/WorldStorage/WSSAnvil.h +++ b/src/WorldStorage/WSSAnvil.h @@ -221,7 +221,7 @@ protected: cMCAFile * LoadMCAFile(const cChunkCoords & a_Chunk); /// Copies a_Length bytes of data from the specified NBT Tag's Child into the a_Destination buffer - void CopyNBTData(const cParsedNBT & a_NBT, int a_Tag, const AString & a_ChildName, char * a_Destination, int a_Length); + void CopyNBTData(const cParsedNBT & a_NBT, int a_Tag, const AString & a_ChildName, char * a_Destination, size_t a_Length); // cWSSchema overrides: virtual bool LoadChunk(const cChunkCoords & a_Chunk) override; -- cgit v1.2.3 From 294b7ca24a09b61cbac0bd43d9b287af21848c24 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 20:05:00 +0200 Subject: Even more size_t fixes. --- src/WorldStorage/FastNBT.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index c4a6a7a5b..a047d67c7 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -29,7 +29,7 @@ // cParsedNBT: #define NEEDBYTES(N) \ - if (m_Length - m_Pos < N) \ + if (m_Length - m_Pos < (size_t)N) \ { \ return false; \ } @@ -84,9 +84,9 @@ bool cParsedNBT::ReadString(size_t & a_StringStart, size_t & a_StringLen) NEEDBYTES(2); a_StringStart = m_Pos + 2; a_StringLen = (size_t)GetBEShort(m_Data + m_Pos); - if (a_StringLen < 0) + if (a_StringLen > 0xffff) { - // Invalid string length + // Suspicious string length return false; } m_Pos += 2 + a_StringLen; @@ -281,7 +281,7 @@ int cParsedNBT::FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLen for (int Child = m_Tags[a_Tag].m_FirstChild; Child != -1; Child = m_Tags[Child].m_NextSibling) { if ( - (m_Tags[Child].m_NameLength == (int)a_NameLength) && + (m_Tags[Child].m_NameLength == a_NameLength) && (memcmp(m_Data + m_Tags[Child].m_NameStart, a_Name, a_NameLength) == 0) ) { -- cgit v1.2.3 From 1a5a1233010a8072f26620866893cc7a3540a550 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 20:16:29 +0200 Subject: Fixed message formatting. --- src/WorldStorage/SchematicFileSerializer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp index 5cbff376b..d3a708f92 100644 --- a/src/WorldStorage/SchematicFileSerializer.cpp +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -234,7 +234,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) { LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", - NumBytes, a_NBT.GetDataLength(TBlockTypes) + (int)NumBytes, (int)a_NBT.GetDataLength(TBlockTypes) ); NumBytes = a_NBT.GetDataLength(TBlockTypes); } @@ -246,7 +246,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) { LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", - NumBytes, a_NBT.GetDataLength(TBlockMetas) + (int)NumBytes, (int)a_NBT.GetDataLength(TBlockMetas) ); NumBytes = a_NBT.GetDataLength(TBlockMetas); } -- cgit v1.2.3 From a2e18f0e01edf5a8e61ac0de1356b98d6a5af1de Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 20:32:27 +0200 Subject: Fixed size_t in FireworksSerializer. --- src/WorldStorage/FireworksSerializer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/FireworksSerializer.cpp b/src/WorldStorage/FireworksSerializer.cpp index 263c77a1c..181cfde0d 100644 --- a/src/WorldStorage/FireworksSerializer.cpp +++ b/src/WorldStorage/FireworksSerializer.cpp @@ -105,7 +105,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB } const char * ColourData = (a_NBT.GetData(explosiontag)); - for (int i = 0; i < DataLength; i += 4 /* Size of network int*/) + for (size_t i = 0; i < DataLength; i += 4) { a_FireworkItem.m_Colours.push_back(GetBEInt(ColourData + i)); } @@ -121,7 +121,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB } const char * FadeColourData = (a_NBT.GetData(explosiontag)); - for (int i = 0; i < DataLength; i += 4 /* Size of network int*/) + for (size_t i = 0; i < DataLength; i += 4) { a_FireworkItem.m_FadeColours.push_back(GetBEInt(FadeColourData + i)); } -- cgit v1.2.3