diff options
Diffstat (limited to '')
-rw-r--r-- | src/ByteBuffer.cpp | 32 | ||||
-rw-r--r-- | src/ByteBuffer.h | 2 | ||||
-rw-r--r-- | src/ClientHandle.cpp | 4 | ||||
-rw-r--r-- | src/ClientHandle.h | 4 | ||||
-rw-r--r-- | src/HTTPServer/HTTPMessage.cpp | 2 | ||||
-rw-r--r-- | src/Items/ItemBucket.h | 12 | ||||
-rw-r--r-- | src/OSSupport/StackTrace.cpp | 2 | ||||
-rw-r--r-- | src/Protocol/Protocol17x.cpp | 233 | ||||
-rw-r--r-- | src/StringUtils.cpp | 57 | ||||
-rw-r--r-- | src/StringUtils.h | 51 | ||||
-rw-r--r-- | src/World.h | 2 | ||||
-rw-r--r-- | src/WorldStorage/FastNBT.cpp | 79 |
12 files changed, 272 insertions, 208 deletions
diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp index e1e5867a9..f3dc44d91 100644 --- a/src/ByteBuffer.cpp +++ b/src/ByteBuffer.cpp @@ -348,8 +348,24 @@ bool cByteBuffer::ReadBEShort(short & a_Value) CHECK_THREAD CheckValid(); NEEDBYTES(2); + Int16 val; + ReadBuf(&val, 2); + val = ntohs(val); + a_Value = *(reinterpret_cast<short *>(&val)); + return true; +} + + + + + +bool cByteBuffer::ReadBEUInt16(UInt16 & a_Value) +{ + CHECK_THREAD + CheckValid(); + NEEDBYTES(2); ReadBuf(&a_Value, 2); - a_Value = (short)ntohs((u_short)a_Value); + a_Value = ntohs(a_Value); return true; } @@ -371,6 +387,20 @@ bool cByteBuffer::ReadBEInt(int & a_Value) +bool cByteBuffer::ReadBEUInt32(UInt32 & a_Value) +{ + CHECK_THREAD + CheckValid(); + NEEDBYTES(4); + ReadBuf(&a_Value, 4); + a_Value = ntohl(a_Value); + return true; +} + + + + + bool cByteBuffer::ReadBEInt64(Int64 & a_Value) { CHECK_THREAD diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h index 2a316fa32..f480ad557 100644 --- a/src/ByteBuffer.h +++ b/src/ByteBuffer.h @@ -55,7 +55,9 @@ public: bool ReadChar (char & a_Value); bool ReadByte (unsigned char & a_Value); bool ReadBEShort (short & a_Value); + bool ReadBEUInt16 (UInt16 & a_Value); bool ReadBEInt (int & a_Value); + bool ReadBEUInt32 (UInt32 & a_Value); bool ReadBEInt64 (Int64 & a_Value); bool ReadBEFloat (float & a_Value); bool ReadBEDouble (double & a_Value); diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 6fe6e99fa..387cc4628 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -1445,7 +1445,7 @@ void cClientHandle::HandlePlayerMoveLook(double a_PosX, double a_PosY, double a_ -void cClientHandle::HandleAnimation(char a_Animation) +void cClientHandle::HandleAnimation(int a_Animation) { if (cPluginManager::Get()->CallHookPlayerAnimation(*m_Player, a_Animation)) { @@ -2832,7 +2832,7 @@ void cClientHandle::PacketUnknown(UInt32 a_PacketType) -void cClientHandle::PacketError(unsigned char a_PacketType) +void cClientHandle::PacketError(UInt32 a_PacketType) { LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str()); SendDisconnect("Protocol error"); diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 5e10bb52f..03ae38cfd 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -251,10 +251,10 @@ public: // Calls that cProtocol descendants use to report state: void PacketBufferFull(void); void PacketUnknown(UInt32 a_PacketType); - void PacketError(unsigned char a_PacketType); + void PacketError(UInt32 a_PacketType); // Calls that cProtocol descendants use for handling packets: - void HandleAnimation(char a_Animation); + void HandleAnimation(int a_Animation); /** Called when the protocol receives a MC|ItemName plugin message, indicating that the player named an item in the anvil UI. */ diff --git a/src/HTTPServer/HTTPMessage.cpp b/src/HTTPServer/HTTPMessage.cpp index f6c0204ae..d59ca438e 100644 --- a/src/HTTPServer/HTTPMessage.cpp +++ b/src/HTTPServer/HTTPMessage.cpp @@ -55,7 +55,7 @@ void cHTTPMessage::AddHeader(const AString & a_Key, const AString & a_Value) } else if (Key == "content-length") { - m_ContentLength = atoi(m_Headers[Key].c_str()); + m_ContentLength = static_cast<size_t>(atol(m_Headers[Key].c_str())); } } diff --git a/src/Items/ItemBucket.h b/src/Items/ItemBucket.h index 3a533958f..871db821c 100644 --- a/src/Items/ItemBucket.h +++ b/src/Items/ItemBucket.h @@ -199,16 +199,16 @@ public: Vector3i m_Pos; BLOCKTYPE m_ReplacedBlock; - virtual bool OnNextBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, char a_EntryFace) override + virtual bool OnNextBlock(int a_CBBlockX, int a_CBBlockY, int a_CBBlockZ, BLOCKTYPE a_CBBlockType, NIBBLETYPE a_CBBlockMeta, char a_CBEntryFace) override { - if (a_BlockType != E_BLOCK_AIR) + if (a_CBBlockType != E_BLOCK_AIR) { - m_ReplacedBlock = a_BlockType; - if (!cFluidSimulator::CanWashAway(a_BlockType) && !IsBlockLiquid(a_BlockType)) + m_ReplacedBlock = a_CBBlockType; + if (!cFluidSimulator::CanWashAway(a_CBBlockType) && !IsBlockLiquid(a_CBBlockType)) { - AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, (eBlockFace)a_EntryFace); // Was an unwashawayable block, can't overwrite it! + AddFaceDirection(a_CBBlockX, a_CBBlockY, a_CBBlockZ, (eBlockFace)a_CBEntryFace); // Was an unwashawayable block, can't overwrite it! } - m_Pos.Set(a_BlockX, a_BlockY, a_BlockZ); // (Block could be washed away, replace it) + m_Pos.Set(a_CBBlockX, a_CBBlockY, a_CBBlockZ); // (Block could be washed away, replace it) return true; // Abort tracing } return false; diff --git a/src/OSSupport/StackTrace.cpp b/src/OSSupport/StackTrace.cpp index a56568457..015a53ba0 100644 --- a/src/OSSupport/StackTrace.cpp +++ b/src/OSSupport/StackTrace.cpp @@ -34,7 +34,7 @@ void PrintStackTrace(void) // Use the backtrace() function to get and output the stackTrace: // Code adapted from http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes void * stackTrace[30]; - size_t numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace)); + int numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace)); backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO); #endif } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index dac1ebde8..169367949 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -174,10 +174,10 @@ void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, cha cPacketizer Pkt(*this, 0x24); // Block Action packet Pkt.WriteInt(a_BlockX); - Pkt.WriteShort(a_BlockY); + Pkt.WriteShort(static_cast<short>(a_BlockY)); Pkt.WriteInt(a_BlockZ); - Pkt.WriteByte(a_Byte1); - Pkt.WriteByte(a_Byte2); + Pkt.WriteByte(static_cast<Byte>(a_Byte1)); + Pkt.WriteByte(static_cast<Byte>(a_Byte2)); Pkt.WriteVarInt(a_BlockType); } @@ -190,7 +190,7 @@ void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x25); // Block Break Animation packet - Pkt.WriteVarInt(a_EntityID); + Pkt.WriteVarInt(static_cast<UInt32>(a_EntityID)); Pkt.WriteInt(a_BlockX); Pkt.WriteInt(a_BlockY); Pkt.WriteInt(a_BlockZ); @@ -204,10 +204,11 @@ void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_BlockY >= 0) && (a_BlockY < 256)); cPacketizer Pkt(*this, 0x23); // Block Change packet Pkt.WriteInt(a_BlockX); - Pkt.WriteByte(a_BlockY); + Pkt.WriteByte(static_cast<Byte>(a_BlockY)); Pkt.WriteInt(a_BlockZ); Pkt.WriteVarInt(a_BlockType); Pkt.WriteByte(a_BlockMeta); @@ -228,8 +229,8 @@ void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV Pkt.WriteInt((int)a_Changes.size() * 4); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { - unsigned int Coords = itr->m_RelY | (itr->m_RelZ << 8) | (itr->m_RelX << 12); - unsigned int Blocks = itr->m_BlockMeta | (itr->m_BlockType << 4); + int Coords = itr->m_RelY | (itr->m_RelZ << 8) | (itr->m_RelX << 12); + int Blocks = static_cast<int>(itr->m_BlockMeta | (itr->m_BlockType << 4)); Pkt.WriteInt((Coords << 16) | Blocks); } // for itr - a_Changes[] } @@ -352,11 +353,13 @@ void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) void cProtocol172::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, int a_Amplifier, short a_Duration) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_EffectID >= 0) && (a_EffectID < 256)); + ASSERT((a_Amplifier >= 0) && (a_Amplifier < 256)); cPacketizer Pkt(*this, 0x1D); // Entity Effect packet Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_EffectID); - Pkt.WriteByte(a_Amplifier); + Pkt.WriteByte(static_cast<Byte>(a_EffectID)); + Pkt.WriteByte(static_cast<Byte>(a_Amplifier)); Pkt.WriteShort(a_Duration); } @@ -438,9 +441,9 @@ void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); + Pkt.WriteChar(a_RelX); + Pkt.WriteChar(a_RelY); + Pkt.WriteChar(a_RelZ); } @@ -453,9 +456,9 @@ void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); + Pkt.WriteChar(a_RelX); + Pkt.WriteChar(a_RelY); + Pkt.WriteChar(a_RelZ); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -537,9 +540,9 @@ void cProtocol172::SendHealth(void) cPacketizer Pkt(*this, 0x06); // Update Health packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteFloat((float)Player->GetHealth()); - Pkt.WriteShort(Player->GetFoodLevel()); - Pkt.WriteFloat((float)Player->GetFoodSaturationLevel()); + Pkt.WriteFloat(static_cast<float>(Player->GetHealth())); + Pkt.WriteShort(static_cast<short>(Player->GetFoodLevel())); + Pkt.WriteFloat(static_cast<float>(Player->GetFoodSaturationLevel())); } @@ -584,10 +587,10 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet Pkt.WriteInt(a_Player.GetUniqueID()); - Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteChar((char)a_World.GetDimension()); + Pkt.WriteByte(static_cast<Byte>(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 + Pkt.WriteChar(static_cast<char>(a_World.GetDimension())); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte(std::min(Server->GetMaxPlayers(), 60)); + Pkt.WriteByte(static_cast<Byte>(std::min(Server->GetMaxPlayers(), 60))); Pkt.WriteString("default"); // Level type - wtf? } m_LastSentDimension = a_World.GetDimension(); @@ -595,9 +598,9 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) // Send the spawn position: { cPacketizer Pkt(*this, 0x05); // Spawn Position packet - Pkt.WriteInt((int)a_World.GetSpawnX()); - Pkt.WriteInt((int)a_World.GetSpawnY()); - Pkt.WriteInt((int)a_World.GetSpawnZ()); + Pkt.WriteInt(static_cast<int>(a_World.GetSpawnX())); + Pkt.WriteInt(static_cast<int>(a_World.GetSpawnY())); + Pkt.WriteInt(static_cast<int>(a_World.GetSpawnZ())); } // Send player abilities: @@ -629,11 +632,11 @@ void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x10); // Spawn Painting packet - Pkt.WriteVarInt(a_Painting.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Painting.GetUniqueID())); Pkt.WriteString(a_Painting.GetName().c_str()); - Pkt.WriteInt((int)a_Painting.GetPosX()); - Pkt.WriteInt((int)a_Painting.GetPosY()); - Pkt.WriteInt((int)a_Painting.GetPosZ()); + Pkt.WriteInt(static_cast<int>(a_Painting.GetPosX())); + Pkt.WriteInt(static_cast<int>(a_Painting.GetPosY())); + Pkt.WriteInt(static_cast<int>(a_Painting.GetPosZ())); Pkt.WriteInt(a_Painting.GetDirection()); } @@ -644,19 +647,19 @@ void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length, unsigned int m_Scale) { ASSERT(m_State == 3); // In game mode? + ASSERT(a_Length + 3 <= USHRT_MAX); + ASSERT((a_X >= 0) && (a_X < 256)); + ASSERT((a_Y >= 0) && (a_Y < 256)); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(a_ID); - Pkt.WriteShort (3 + a_Length); + Pkt.WriteVarInt(static_cast<UInt32>(a_ID)); + Pkt.WriteShort (static_cast<short>(3 + a_Length)); Pkt.WriteByte(0); - Pkt.WriteByte(a_X); - Pkt.WriteByte(a_Y); + Pkt.WriteByte(static_cast<Byte>(a_X)); + Pkt.WriteByte(static_cast<Byte>(a_Y)); - for (unsigned int i = 0; i < a_Length; ++i) - { - Pkt.WriteByte(a_Colors[i]); - } + Pkt.WriteBuf(reinterpret_cast<const char *>(a_Colors), a_Length); } @@ -666,18 +669,21 @@ void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colo void cProtocol172::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decorators, unsigned int m_Scale) { ASSERT(m_State == 3); // In game mode? + ASSERT(1 + 3 * a_Decorators.size() < USHRT_MAX); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(a_ID); - Pkt.WriteShort ((short)(1 + (3 * a_Decorators.size()))); + Pkt.WriteVarInt(static_cast<UInt32>(a_ID)); + Pkt.WriteShort (static_cast<short>(1 + (3 * a_Decorators.size()))); Pkt.WriteByte(1); for (cMapDecoratorList::const_iterator it = a_Decorators.begin(); it != a_Decorators.end(); ++it) { - Pkt.WriteByte((it->GetType() << 4) | (it->GetRot() & 0xf)); - Pkt.WriteByte(it->GetPixelX()); - Pkt.WriteByte(it->GetPixelZ()); + ASSERT((it->GetPixelX() >= 0) && (it->GetPixelX() < 256)); + ASSERT((it->GetPixelZ() >= 0) && (it->GetPixelZ() < 256)); + Pkt.WriteByte(static_cast<Byte>((it->GetType() << 4) | static_cast<Byte>(it->GetRot() & 0xf))); + Pkt.WriteByte(static_cast<Byte>(it->GetPixelX())); + Pkt.WriteByte(static_cast<Byte>(it->GetPixelZ())); } } @@ -688,13 +694,14 @@ void cProtocol172::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decor void cProtocol172::SendMapInfo(int a_ID, unsigned int a_Scale) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_Scale >= 0) && (a_Scale < 256)); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(a_ID); + Pkt.WriteVarInt(static_cast<UInt32>(a_ID)); Pkt.WriteShort (2); Pkt.WriteByte(2); - Pkt.WriteByte(a_Scale); + Pkt.WriteByte(static_cast<Byte>(a_Scale)); } @@ -707,7 +714,7 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_Pickup.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Pickup.GetUniqueID())); Pkt.WriteByte(2); // Type = Pickup Pkt.WriteFPInt(a_Pickup.GetPosX()); Pkt.WriteFPInt(a_Pickup.GetPosY()); @@ -763,7 +770,7 @@ void cProtocol172::SendEntityAnimation(const cEntity & a_Entity, char a_Animatio ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Entity.GetUniqueID())); Pkt.WriteChar(a_Animation); } @@ -865,7 +872,7 @@ void cProtocol172::SendPlayerMaxSpeed(void) { Pkt.WriteShort(1); // Modifier count Pkt.WriteInt64(0x662a6b8dda3e4c1c); - Pkt.WriteInt64(0x881396ea6097278d); // UUID of the modifier + Pkt.WriteInt64(static_cast<Int64>(0x881396ea6097278d)); // UUID of the modifier Pkt.WriteDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); Pkt.WriteByte(2); } @@ -960,10 +967,11 @@ void cProtocol172::SendPluginMessage(const AString & a_Channel, const AString & void cProtocol172::SendRemoveEntityEffect(const cEntity & a_Entity, int a_EffectID) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_EffectID >= 0) && (a_EffectID < 256)); cPacketizer Pkt(*this, 0x1e); Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_EffectID); + Pkt.WriteByte(static_cast<Byte>(a_EffectID)); } @@ -1009,13 +1017,14 @@ void cProtocol172::SendExperience (void) void cProtocol172::SendExperienceOrb(const cExpOrb & a_ExpOrb) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_ExpOrb.GetReward() >= 0) && (a_ExpOrb.GetReward() < SHRT_MAX)); cPacketizer Pkt(*this, 0x11); - Pkt.WriteVarInt(a_ExpOrb.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_ExpOrb.GetUniqueID())); Pkt.WriteFPInt(a_ExpOrb.GetPosX()); Pkt.WriteFPInt(a_ExpOrb.GetPosY()); Pkt.WriteFPInt(a_ExpOrb.GetPosZ()); - Pkt.WriteShort(a_ExpOrb.GetReward()); + Pkt.WriteShort(static_cast<short>(a_ExpOrb.GetReward())); } @@ -1060,7 +1069,7 @@ void cProtocol172::SendDisplayObjective(const AString & a_Objective, cScoreboard ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3d); - Pkt.WriteByte((int) a_Display); + Pkt.WriteByte(static_cast<Byte>(a_Display)); Pkt.WriteString(a_Objective); } @@ -1074,11 +1083,11 @@ void cProtocol172::SendSoundEffect(const AString & a_SoundName, double a_X, doub cPacketizer Pkt(*this, 0x29); // Sound Effect packet Pkt.WriteString(a_SoundName); - Pkt.WriteInt((int)(a_X * 8.0)); - Pkt.WriteInt((int)(a_Y * 8.0)); - Pkt.WriteInt((int)(a_Z * 8.0)); + Pkt.WriteInt(static_cast<int>(a_X * 8.0)); + Pkt.WriteInt(static_cast<int>(a_Y * 8.0)); + Pkt.WriteInt(static_cast<int>(a_Z * 8.0)); Pkt.WriteFloat(a_Volume); - Pkt.WriteByte((Byte)(a_Pitch * 63)); + Pkt.WriteByte(static_cast<Byte>(a_Pitch * 63)); } @@ -1088,11 +1097,12 @@ void cProtocol172::SendSoundEffect(const AString & a_SoundName, double a_X, doub void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_SrcY >= 0) && (a_SrcY < 256)); cPacketizer Pkt(*this, 0x28); // Effect packet Pkt.WriteInt(a_EffectID); Pkt.WriteInt(a_SrcX); - Pkt.WriteByte(a_SrcY); + Pkt.WriteByte(static_cast<Byte>(a_SrcY)); Pkt.WriteInt(a_SrcZ); Pkt.WriteInt(a_Data); Pkt.WriteBool(false); @@ -1107,17 +1117,17 @@ void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_FallingBlock.GetUniqueID())); Pkt.WriteByte(70); // Falling block Pkt.WriteFPInt(a_FallingBlock.GetPosX()); Pkt.WriteFPInt(a_FallingBlock.GetPosY()); Pkt.WriteFPInt(a_FallingBlock.GetPosZ()); Pkt.WriteByteAngle(a_FallingBlock.GetYaw()); Pkt.WriteByteAngle(a_FallingBlock.GetPitch()); - Pkt.WriteInt(((int)a_FallingBlock.GetBlockType()) | (((int)a_FallingBlock.GetBlockMeta()) << 16)); // Or 0x10 - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedZ() * 400)); + Pkt.WriteInt((static_cast<int>(a_FallingBlock.GetBlockType()) | ((static_cast<int>(a_FallingBlock.GetBlockMeta()) << 16)))); + Pkt.WriteShort(static_cast<short>(a_FallingBlock.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast<short>(a_FallingBlock.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast<short>(a_FallingBlock.GetSpeedZ() * 400)); } @@ -1129,7 +1139,7 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteVarInt(a_Mob.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Mob.GetUniqueID())); Pkt.WriteByte((Byte)a_Mob.GetMobType()); Pkt.WriteFPInt(a_Mob.GetPosX()); Pkt.WriteFPInt(a_Mob.GetPosY()); @@ -1137,9 +1147,9 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) Pkt.WriteByteAngle(a_Mob.GetPitch()); Pkt.WriteByteAngle(a_Mob.GetHeadYaw()); Pkt.WriteByteAngle(a_Mob.GetYaw()); - Pkt.WriteShort((short)(a_Mob.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedZ() * 400)); + Pkt.WriteShort(static_cast<short>(a_Mob.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast<short>(a_Mob.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast<short>(a_Mob.GetSpeedZ() * 400)); Pkt.WriteEntityMetadata(a_Mob); Pkt.WriteByte(0x7f); // Metadata terminator } @@ -1153,8 +1163,8 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_ObjectType); + Pkt.WriteVarInt(static_cast<UInt32>(a_Entity.GetUniqueID())); + Pkt.WriteChar(a_ObjectType); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); @@ -1163,9 +1173,9 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, Pkt.WriteInt(a_ObjectData); if (a_ObjectData != 0) { - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteShort(static_cast<short>(a_Entity.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast<short>(a_Entity.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast<short>(a_Entity.GetSpeedZ() * 400)); } } @@ -1178,8 +1188,8 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); - Pkt.WriteByte(a_VehicleType); + Pkt.WriteVarInt(static_cast<UInt32>(a_Vehicle.GetUniqueID())); + Pkt.WriteChar(a_VehicleType); Pkt.WriteFPInt(a_Vehicle.GetPosX()); Pkt.WriteFPInt(a_Vehicle.GetPosY()); Pkt.WriteFPInt(a_Vehicle.GetPosZ()); @@ -1188,9 +1198,9 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp Pkt.WriteInt(a_VehicleSubType); if (a_VehicleSubType != 0) { - Pkt.WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); + Pkt.WriteShort(static_cast<short>(a_Vehicle.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast<short>(a_Vehicle.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast<short>(a_Vehicle.GetSpeedZ() * 400)); } } @@ -1211,7 +1221,7 @@ void cProtocol172::SendStatistics(const cStatManager & a_Manager) const AString & StatName = cStatInfo::GetName((eStatistic) i); Pkt.WriteString(StatName); - Pkt.WriteVarInt(Value); + Pkt.WriteVarInt(static_cast<UInt32>(Value)); } } @@ -1224,7 +1234,7 @@ void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet - Pkt.WriteVarInt((int)a_Results.size()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Results.size())); for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) { @@ -1309,7 +1319,7 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) cPacketizer Pkt(*this, 0x35); // Update tile entity packet Pkt.WriteInt(a_BlockEntity.GetPosX()); - Pkt.WriteShort(a_BlockEntity.GetPosY()); + Pkt.WriteShort(static_cast<short>(a_BlockEntity.GetPosY())); Pkt.WriteInt(a_BlockEntity.GetPosZ()); Byte Action = 0; @@ -1389,7 +1399,7 @@ void cProtocol172::SendWholeInventory(const cWindow & a_Window) cPacketizer Pkt(*this, 0x30); // Window Items packet Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Window.GetNumSlots()); + Pkt.WriteShort(static_cast<short>(a_Window.GetNumSlots())); cItems Slots; a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) @@ -1426,9 +1436,9 @@ void cProtocol172::SendWindowOpen(const cWindow & a_Window) cPacketizer Pkt(*this, 0x2d); Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteChar(a_Window.GetWindowType()); + Pkt.WriteChar(static_cast<char>(a_Window.GetWindowType())); Pkt.WriteString(a_Window.GetWindowTitle()); - Pkt.WriteChar(a_Window.GetNumNonInventorySlots()); + Pkt.WriteChar(static_cast<char>(a_Window.GetNumNonInventorySlots())); Pkt.WriteBool(true); if (a_Window.GetWindowType() == cWindow::wtAnimalChest) { @@ -1505,7 +1515,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) break; } cByteBuffer bb(PacketLen + 1); - VERIFY(m_ReceivedData.ReadToByteBuffer(bb, (int)PacketLen)); + VERIFY(m_ReceivedData.ReadToByteBuffer(bb, static_cast<size_t>(PacketLen))); m_ReceivedData.CommitRead(); UInt32 PacketType; @@ -1748,8 +1758,14 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe { short EncKeyLength, EncNonceLength; a_ByteBuffer.ReadBEShort(EncKeyLength); + if ((EncKeyLength < 0) || (EncKeyLength > MAX_ENC_LEN)) + { + LOGD("Invalid Encryption Key length: %d. Kicking client.", EncKeyLength); + m_Client->Kick("Invalid EncKeyLength"); + return; + } AString EncKey; - if (!a_ByteBuffer.ReadString(EncKey, EncKeyLength)) + if (!a_ByteBuffer.ReadString(EncKey, static_cast<size_t>(EncKeyLength))) { return; } @@ -1757,15 +1773,15 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe { return; } - AString EncNonce; - if (!a_ByteBuffer.ReadString(EncNonce, EncNonceLength)) + if ((EncNonceLength < 0) || (EncNonceLength > MAX_ENC_LEN)) { + LOGD("Invalid Encryption Nonce length: %d. Kicking client.", EncNonceLength); + m_Client->Kick("Invalid EncNonceLength"); return; } - if ((EncKeyLength > MAX_ENC_LEN) || (EncNonceLength > MAX_ENC_LEN)) + AString EncNonce; + if (!a_ByteBuffer.ReadString(EncNonce, static_cast<size_t>(EncNonceLength))) { - LOGD("Too long encryption"); - m_Client->Kick("Hacked client"); return; } @@ -1854,7 +1870,7 @@ void cProtocol172::HandlePacketAnimation(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Status); + HANDLE_READ(a_ByteBuffer, ReadChar, char, Status); HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); HANDLE_READ(a_ByteBuffer, ReadByte, Byte, BlockY); HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); @@ -2072,7 +2088,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) if (Length + 1 != (int)a_ByteBuffer.GetReadableSpace()) { LOGD("Invalid plugin message packet, payload length doesn't match packet length (exp %d, got %d)", - (int)a_ByteBuffer.GetReadableSpace() - 1, Length + static_cast<int>(a_ByteBuffer.GetReadableSpace()) - 1, Length ); return; } @@ -2086,7 +2102,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) // Read the plugin message and relay to clienthandle: AString Data; - if (!a_ByteBuffer.ReadString(Data, Length)) + if (!a_ByteBuffer.ReadString(Data, static_cast<size_t>(Length))) { return; } @@ -2274,7 +2290,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const LOGD("Protocol 1.7: Skipping garbage data at the end of a vanilla MC|AdvCdm packet, %u bytes", static_cast<unsigned>(a_PayloadLength - BytesRead) ); - a_ByteBuffer.SkipRead(a_PayloadLength - BytesRead); + a_ByteBuffer.SkipRead(static_cast<size_t>(a_PayloadLength) - BytesRead); } return; } @@ -2282,7 +2298,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const { // Read the client's brand: AString Brand; - if (a_ByteBuffer.ReadString(Brand, a_PayloadLength)) + if (a_ByteBuffer.ReadString(Brand, static_cast<size_t>(a_PayloadLength))) { m_Client->SetClientBrand(Brand); } @@ -2301,7 +2317,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const else if (a_Channel == "MC|ItemName") { AString ItemName; - if (a_ByteBuffer.ReadString(ItemName, a_PayloadLength)) + if (a_ByteBuffer.ReadString(ItemName, static_cast<size_t>(a_PayloadLength))) { m_Client->HandleAnvilItemName(ItemName); } @@ -2317,7 +2333,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const // Read the payload and send it through to the clienthandle: AString Message; - VERIFY(a_ByteBuffer.ReadString(Message, a_PayloadLength)); + VERIFY(a_ByteBuffer.ReadString(Message, static_cast<size_t>(a_PayloadLength))); m_Client->HandlePluginMessage(a_Channel, Message); } @@ -2377,7 +2393,7 @@ bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) // Read the metadata AString Metadata; - if (!a_ByteBuffer.ReadString(Metadata, MetadataLength)) + if (!a_ByteBuffer.ReadString(Metadata, static_cast<size_t>(MetadataLength))) { return false; } @@ -2546,7 +2562,7 @@ void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) } WriteShort(ItemType); - WriteByte (a_Item.m_ItemCount); + WriteChar (a_Item.m_ItemCount); WriteShort(a_Item.m_ItemDamage); if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (a_Item.m_ItemType != E_ITEM_FIREWORK_ROCKET) && (a_Item.m_ItemType != E_ITEM_FIREWORK_STAR)) @@ -2706,7 +2722,7 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt void cProtocol172::cPacketizer::WriteByteAngle(double a_Angle) { - WriteByte((char)(255 * a_Angle / 360)); + WriteChar(static_cast<char>(255 * a_Angle / 360)); } @@ -2849,7 +2865,7 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) case mtCreeper: { WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); + WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : 0); WriteByte(0x11); WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); break; @@ -2918,15 +2934,14 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) WriteByte(0x13); WriteByte(Wolf.IsBegging() ? 1 : 0); WriteByte(0x14); - WriteByte(Wolf.GetCollarColor()); + WriteByte(static_cast<Byte>(Wolf.GetCollarColor())); break; } case mtSheep: { WriteByte(0x10); - Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); + Byte SheepMetadata = static_cast<Byte>(((const cSheep &)a_Mob).GetFurColor() & 0x0f); if (((const cSheep &)a_Mob).IsSheared()) { SheepMetadata |= 0x10; @@ -2963,7 +2978,7 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) case mtWither: { WriteByte(0x54); // Int at index 20 - WriteInt(((const cWither &)a_Mob).GetWitherInvulnerableTicks()); + WriteInt(static_cast<int>(((const cWither &)a_Mob).GetWitherInvulnerableTicks())); WriteByte(0x66); // Float at index 6 WriteFloat((float)(a_Mob.GetHealth())); break; @@ -2972,14 +2987,14 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) case mtSlime: { WriteByte(0x10); - WriteByte(((const cSlime &)a_Mob).GetSize()); + WriteByte(static_cast<Byte>(((const cSlime &)a_Mob).GetSize())); break; } case mtMagmaCube: { WriteByte(0x10); - WriteByte(((const cMagmaCube &)a_Mob).GetSize()); + WriteByte(static_cast<Byte>(((const cMagmaCube &)a_Mob).GetSize())); break; } @@ -3018,7 +3033,7 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) WriteByte(0x50); // Int at index 16 WriteInt(Flags); WriteByte(0x13); // Byte at index 19 - WriteByte(Horse.GetHorseType()); + WriteByte(static_cast<Byte>(Horse.GetHorseType())); WriteByte(0x54); // Int at index 20 int Appearance = 0; Appearance = Horse.GetHorseColor(); @@ -3028,6 +3043,12 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) WriteInt(Horse.GetHorseArmour()); break; } + + default: + { + // No data to send for this mob + break; + } } // switch (a_Mob.GetType()) // Custom name: @@ -3080,7 +3101,7 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) { // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt(static_cast<UInt32>(a_Player.GetUniqueID())); Pkt.WriteString(cMojangAPI::MakeUUIDDashed(a_Player.GetClientHandle()->GetUUID())); if (a_Player.HasCustomName()) { diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 5febf5d6c..a63525356 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -23,7 +23,7 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args) ASSERT(format != nullptr); char buffer[2048]; - size_t len; + int len; #ifdef va_copy va_list argsCopy; va_copy(argsCopy, args); @@ -34,14 +34,14 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args) // MS CRT provides secure printf that doesn't behave like in the C99 standard if ((len = _vsnprintf_s(buffer, ARRAYCOUNT(buffer), _TRUNCATE, format, argsCopy)) != -1) #else // _MSC_VER - if ((len = vsnprintf(buffer, ARRAYCOUNT(buffer), format, argsCopy)) < ARRAYCOUNT(buffer)) + if ((len = vsnprintf(buffer, ARRAYCOUNT(buffer), format, argsCopy)) < static_cast<int>(ARRAYCOUNT(buffer))) #endif // else _MSC_VER { // The result did fit into the static buffer #ifdef va_copy va_end(argsCopy); #endif - str.append(buffer, len); + str.append(buffer, static_cast<size_t>(len)); return str; } #ifdef va_copy @@ -51,7 +51,6 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args) // The result did not fit into the static buffer, use a dynamic buffer: #ifdef _MSC_VER // for MS CRT, we need to calculate the result length - // MS doesn't have va_copy() and does nod need it at all len = _vscprintf(format, args); if (len == -1) { @@ -63,11 +62,11 @@ AString & AppendVPrintf(AString & str, const char * format, va_list args) #ifdef va_copy va_copy(argsCopy, args); #endif - std::vector<char> Buffer(len + 1); + std::vector<char> Buffer(static_cast<size_t>(len) + 1); #ifdef _MSC_VER - vsprintf_s((char *)&(Buffer.front()), Buffer.size(), format, argsCopy); + vsprintf_s(&(Buffer.front()), Buffer.size(), format, argsCopy); #else // _MSC_VER - vsnprintf((char *)&(Buffer.front()), Buffer.size(), format, argsCopy); + vsnprintf(&(Buffer.front()), Buffer.size(), format, argsCopy); #endif // else _MSC_VER str.append(&(Buffer.front()), Buffer.size() - 1); #ifdef va_copy @@ -85,7 +84,7 @@ AString & Printf(AString & str, const char * format, ...) str.clear(); va_list args; va_start(args, format); - std::string &retval = AppendVPrintf(str, format, args); + std::string & retval = AppendVPrintf(str, format, args); va_end(args); return retval; } @@ -108,11 +107,11 @@ AString Printf(const char * format, ...) -AString & AppendPrintf(AString &str, const char * format, ...) +AString & AppendPrintf(AString & dst, const char * format, ...) { va_list args; va_start(args, format); - std::string &retval = AppendVPrintf(str, format, args); + std::string & retval = AppendVPrintf(dst, format, args); va_end(args); return retval; } @@ -297,7 +296,6 @@ void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & -// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8) { a_UTF8.clear(); @@ -314,22 +312,22 @@ AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UT a_UTF8.push_back((char)(192 + c / 64)); a_UTF8.push_back((char)(128 + c % 64)); } - else if (c - 0xd800u < 0x800) + else if (c - 0xd800 < 0x800) { // Error, silently drop } else if (c < 0x10000) { - a_UTF8.push_back((char)(224 + c / 4096)); - a_UTF8.push_back((char)(128 + c / 64 % 64)); - a_UTF8.push_back((char)(128 + c % 64)); + a_UTF8.push_back(static_cast<char>(224 + c / 4096)); + a_UTF8.push_back(static_cast<char>(128 + (c / 64) % 64)); + a_UTF8.push_back(static_cast<char>(128 + c % 64)); } else if (c < 0x110000) { - a_UTF8.push_back((char)(240 + c / 262144)); - a_UTF8.push_back((char)(128 + c / 4096 % 64)); - a_UTF8.push_back((char)(128 + c / 64 % 64)); - a_UTF8.push_back((char)(128 + c % 64)); + a_UTF8.push_back(static_cast<char>(240 + c / 262144)); + a_UTF8.push_back(static_cast<char>(128 + (c / 4096) % 64)); + a_UTF8.push_back(static_cast<char>(128 + (c / 64) % 64)); + a_UTF8.push_back(static_cast<char>(128 + c % 64)); } else { @@ -382,7 +380,7 @@ Notice from the original file: -static const char trailingBytesForUTF8[256] = +static const Byte trailingBytesForUTF8[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -572,18 +570,18 @@ AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, siz int Count = sprintf(line, "%08x:", (unsigned)i); #endif // Remove the terminating nullptr / leftover garbage in line, after the sprintf-ed value - memset(line + Count, 32, sizeof(line) - Count); + memset(line + Count, 32, sizeof(line) - static_cast<size_t>(Count)); p = line + 10; q = p + 2 + a_BytesPerLine * 3 + 1; for (size_t j = 0; j < k; j++) { - unsigned char c = ((unsigned char *)a_Data)[i + j]; + Byte c = (reinterpret_cast<const Byte *>(a_Data))[i + j]; p[0] = HEX(c >> 4); p[1] = HEX(c & 0xf); p[2] = ' '; if (c >= ' ') { - q[0] = (char)c; + q[0] = static_cast<char>(c); } else { @@ -708,7 +706,7 @@ AString URLDecode(const AString & a_String) res.push_back(ch); continue; } - res.push_back((hi << 4) | lo); + res.push_back(static_cast<char>((hi << 4) | lo)); i += 2; } // for i - a_String[] return res; @@ -767,7 +765,8 @@ AString Base64Decode(const AString & a_Base64String) { AString res; size_t i, len = a_Base64String.size(); - int o, c; + size_t o; + int c; res.resize((len * 4) / 3 + 5, 0); // Approximate the upper bound on the result length for (o = 0, i = 0; i < len; i++) { @@ -850,7 +849,7 @@ AString Base64Encode(const AString & a_Input) short GetBEShort(const char * a_Mem) { const Byte * Bytes = (const Byte *)a_Mem; - return (Bytes[0] << 8) | Bytes[1]; + return static_cast<short>((Bytes[0] << 8) | Bytes[1]); } @@ -870,9 +869,9 @@ int GetBEInt(const char * a_Mem) void SetBEInt(char * a_Mem, Int32 a_Value) { a_Mem[0] = a_Value >> 24; - a_Mem[1] = (a_Value >> 16) & 0xff; - a_Mem[2] = (a_Value >> 8) & 0xff; - a_Mem[3] = a_Value & 0xff; + a_Mem[1] = static_cast<char>((a_Value >> 16) & 0xff); + a_Mem[2] = static_cast<char>((a_Value >> 8) & 0xff); + a_Mem[3] = static_cast<char>(a_Value & 0xff); } diff --git a/src/StringUtils.h b/src/StringUtils.h index 159e8ecac..bfe2a41fa 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -21,31 +21,40 @@ typedef std::list<AString> AStringList; -/** Add the formated string to the existing data in the string */ -extern AString & AppendVPrintf(AString & str, const char * format, va_list args) FORMATSTRING(2, 0); +/** Add the formated string to the existing data in the string. +Returns a_Dst. */ +extern AString & AppendVPrintf(AString & a_Dst, const char * format, va_list args) FORMATSTRING(2, 0); -/// Output the formatted text into the string -extern AString & Printf (AString & str, const char * format, ...) FORMATSTRING(2, 3); +/** Output the formatted text into the string. +Returns a_Dst. */ +extern AString & Printf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3); -/// Output the formatted text into string, return string by value +/** Output the formatted text into string +Returns the formatted string by value. */ extern AString Printf(const char * format, ...) FORMATSTRING(1, 2); -/// Add the formatted string to the existing data in the string -extern AString & AppendPrintf (AString & str, const char * format, ...) FORMATSTRING(2, 3); +/** Add the formatted string to the existing data in the string. +Returns a_Dst */ +extern AString & AppendPrintf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3); -/// Split the string at any of the listed delimiters, return as a stringvector +/** Split the string at any of the listed delimiters. +Return the splitted strings as a stringvector. */ extern AStringVector StringSplit(const AString & str, const AString & delim); -/// Split the string at any of the listed delimiters and trim each value, return as a stringvector +/** Split the string at any of the listed delimiters and trim each value. +Returns the splitted strings as a stringvector. */ extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim); -/// Trime whitespace at both ends of the string +/** Trims whitespace at both ends of the string. +Returns a trimmed copy of the original string. */ extern AString TrimString(const AString & str); // tolua_export -/// In-place string conversion to uppercase; returns the same string +/** In-place string conversion to uppercase. +Returns the same string object. */ extern AString & InPlaceUppercase(AString & s); -/// In-place string conversion to lowercase; returns the same string +/** In-place string conversion to lowercase. +Returns the same string object. */ extern AString & InPlaceLowercase(AString & s); /** Returns an upper-cased copy of the string */ @@ -54,28 +63,30 @@ extern AString StrToUpper(const AString & s); /** Returns a lower-cased copy of the string */ extern AString StrToLower(const AString & s); -/// Case-insensitive string comparison; returns 0 if the strings are the same +/** Case-insensitive string comparison. +Returns 0 if the strings are the same, <0 if s1 < s2 and >0 if s1 > s2. */ extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export -/// Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()] +/** Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()]. */ extern size_t RateCompareString(const AString & s1, const AString & s2); -/// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith +/** Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith */ extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export -/// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 +/** Converts a stream of BE shorts into UTF-8 string; returns a_UTF8. */ extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8); -/// Converts a UTF-8 string into a UTF-16 BE string; returns a ref to a_UTF16 +/** Converts a UTF-8 string into a UTF-16 BE string. */ extern AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length); -/// Creates a nicely formatted HEX dump of the given memory block. Max a_BytesPerLine is 120 +/** Creates a nicely formatted HEX dump of the given memory block. +Max a_BytesPerLine is 120. */ extern AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, size_t a_BytesPerLine); -/// Returns a copy of a_Message with all quotes and backslashes escaped by a backslash +/** Returns a copy of a_Message with all quotes and backslashes escaped by a backslash. */ extern AString EscapeString(const AString & a_Message); // tolua_export -/// Removes all control codes used by MC for colors and styles +/** Removes all control codes used by MC for colors and styles. */ extern AString StripColorCodes(const AString & a_Message); // tolua_export /// URL-Decodes the given string, replacing all "%HH" into the correct characters. Invalid % sequences are left intact diff --git a/src/World.h b/src/World.h index 1f58ddbb7..e7519dab8 100644 --- a/src/World.h +++ b/src/World.h @@ -803,7 +803,7 @@ public: int CreateProjectile(double a_PosX, double a_PosY, double a_PosZ, cProjectileEntity::eKind a_Kind, cEntity * a_Creator, const cItem * a_Item, const Vector3d * a_Speed = nullptr); // tolua_export /** Returns a random number from the m_TickRand in range [0 .. a_Range]. To be used only in the tick thread! */ - int GetTickRandomNumber(unsigned a_Range) { return (int)(m_TickRand.randInt(a_Range)); } + int GetTickRandomNumber(int a_Range) { return (int)(m_TickRand.randInt(a_Range)); } /** Appends all usernames starting with a_Text (case-insensitive) into Results */ void TabCompleteUserName(const AString & a_Text, AStringVector & a_Results); diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index aaef2fdfe..033a07601 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -109,7 +109,7 @@ bool cParsedNBT::ReadCompound(void) ASSERT(m_Tags.size() > 0); // Reads the latest tag as a compound - int ParentIdx = (int)m_Tags.size() - 1; + size_t ParentIdx = m_Tags.size() - 1; int PrevSibling = -1; for (;;) { @@ -120,10 +120,10 @@ bool cParsedNBT::ReadCompound(void) { break; } - m_Tags.push_back(cFastNBTTag(TagType, ParentIdx, PrevSibling)); + m_Tags.push_back(cFastNBTTag(TagType, static_cast<int>(ParentIdx), PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; + m_Tags[static_cast<size_t>(PrevSibling)].m_NextSibling = (int)m_Tags.size() - 1; } else { @@ -155,20 +155,21 @@ bool cParsedNBT::ReadList(eTagType a_ChildrenType) } // Read items: - int ParentIdx = (int)m_Tags.size() - 1; + ASSERT(m_Tags.size() > 0); + size_t ParentIdx = m_Tags.size() - 1; int PrevSibling = -1; for (int i = 0; i < Count; i++) { - m_Tags.push_back(cFastNBTTag(a_ChildrenType, ParentIdx, PrevSibling)); + m_Tags.push_back(cFastNBTTag(a_ChildrenType, static_cast<int>(ParentIdx), PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; + m_Tags[static_cast<size_t>(PrevSibling)].m_NextSibling = static_cast<int>(m_Tags.size()) - 1; } else { - m_Tags[ParentIdx].m_FirstChild = (int)m_Tags.size() - 1; + m_Tags[ParentIdx].m_FirstChild = static_cast<int>(m_Tags.size()) - 1; } - PrevSibling = (int)m_Tags.size() - 1; + PrevSibling = static_cast<int>(m_Tags.size()) - 1; RETURN_FALSE_IF_FALSE(ReadTag()); } // for (i) m_Tags[ParentIdx].m_LastChild = PrevSibling; @@ -217,16 +218,16 @@ bool cParsedNBT::ReadTag(void) return false; } NEEDBYTES(len); - Tag.m_DataLength = len; + Tag.m_DataLength = static_cast<size_t>(len); Tag.m_DataStart = m_Pos; - m_Pos += len; + m_Pos += static_cast<size_t>(len); return true; } case TAG_List: { NEEDBYTES(1); - eTagType ItemType = (eTagType)m_Data[m_Pos]; + eTagType ItemType = static_cast<eTagType>(m_Data[m_Pos]); m_Pos++; RETURN_FALSE_IF_FALSE(ReadList(ItemType)); return true; @@ -250,9 +251,9 @@ bool cParsedNBT::ReadTag(void) } len *= 4; NEEDBYTES(len); - Tag.m_DataLength = len; + Tag.m_DataLength = static_cast<size_t>(len); Tag.m_DataStart = m_Pos; - m_Pos += len; + m_Pos += static_cast<size_t>(len); return true; } @@ -276,7 +277,7 @@ int cParsedNBT::FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLen { return -1; } - if (m_Tags[a_Tag].m_Type != TAG_Compound) + if (m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound) { return -1; } @@ -285,11 +286,11 @@ int cParsedNBT::FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLen { a_NameLength = strlen(a_Name); } - for (int Child = m_Tags[a_Tag].m_FirstChild; Child != -1; Child = m_Tags[Child].m_NextSibling) + for (int Child = m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild; Child != -1; Child = m_Tags[static_cast<size_t>(Child)].m_NextSibling) { if ( - (m_Tags[Child].m_NameLength == a_NameLength) && - (memcmp(m_Data + m_Tags[Child].m_NameStart, a_Name, a_NameLength) == 0) + (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) ) { return Child; @@ -413,7 +414,7 @@ void cFastNBTWriter::EndList(void) ASSERT(m_Stack[m_CurrentStack].m_Type == TAG_List); // Update the list count: - SetBEInt((char *)(m_Result.c_str() + m_Stack[m_CurrentStack].m_Pos), m_Stack[m_CurrentStack].m_Count); + SetBEInt(const_cast<char *>(m_Result.c_str() + m_Stack[m_CurrentStack].m_Pos), m_Stack[m_CurrentStack].m_Count); --m_CurrentStack; } @@ -425,7 +426,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(a_Value); + m_Result.push_back(static_cast<char>(a_Value)); } @@ -435,8 +436,8 @@ void cFastNBTWriter::AddByte(const AString & a_Name, unsigned char a_Value) void cFastNBTWriter::AddShort(const AString & a_Name, Int16 a_Value) { TagCommon(a_Name, TAG_Short); - Int16 Value = htons(a_Value); - m_Result.append((const char *)&Value, 2); + UInt16 Value = htons(a_Value); + m_Result.append(reinterpret_cast<const char *>(&Value), 2); } @@ -446,8 +447,8 @@ void cFastNBTWriter::AddShort(const AString & a_Name, Int16 a_Value) void cFastNBTWriter::AddInt(const AString & a_Name, Int32 a_Value) { TagCommon(a_Name, TAG_Int); - Int32 Value = htonl(a_Value); - m_Result.append((const char *)&Value, 4); + UInt32 Value = htonl(a_Value); + m_Result.append(reinterpret_cast<const char *>(&Value), 4); } @@ -457,8 +458,8 @@ void cFastNBTWriter::AddInt(const AString & a_Name, Int32 a_Value) void cFastNBTWriter::AddLong(const AString & a_Name, Int64 a_Value) { TagCommon(a_Name, TAG_Long); - Int64 Value = HostToNetwork8(&a_Value); - m_Result.append((const char *)&Value, 8); + UInt64 Value = HostToNetwork8(&a_Value); + m_Result.append(reinterpret_cast<const char *>(&Value), 8); } @@ -468,8 +469,8 @@ void cFastNBTWriter::AddLong(const AString & a_Name, Int64 a_Value) void cFastNBTWriter::AddFloat(const AString & a_Name, float a_Value) { TagCommon(a_Name, TAG_Float); - Int32 Value = HostToNetwork4(&a_Value); - m_Result.append((const char *)&Value, 4); + UInt32 Value = HostToNetwork4(&a_Value); + m_Result.append(reinterpret_cast<const char *>(&Value), 4); } @@ -479,8 +480,8 @@ void cFastNBTWriter::AddFloat(const AString & a_Name, float a_Value) void cFastNBTWriter::AddDouble(const AString & a_Name, double a_Value) { TagCommon(a_Name, TAG_Double); - Int64 Value = HostToNetwork8(&a_Value); - m_Result.append((const char *)&Value, 8); + UInt64 Value = HostToNetwork8(&a_Value); + m_Result.append(reinterpret_cast<const char *>(&Value), 8); } @@ -490,8 +491,8 @@ void cFastNBTWriter::AddDouble(const AString & a_Name, double a_Value) void cFastNBTWriter::AddString(const AString & a_Name, const AString & a_Value) { TagCommon(a_Name, TAG_String); - Int16 len = htons((short)(a_Value.size())); - m_Result.append((const char *)&len, 2); + UInt16 len = htons(static_cast<short>(a_Value.size())); + m_Result.append(reinterpret_cast<const char *>(&len), 2); m_Result.append(a_Value.c_str(), a_Value.size()); } @@ -502,8 +503,8 @@ 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); - u_long len = htonl((u_long)a_NumElements); - m_Result.append((const char *)&len, 4); + u_long len = htonl(static_cast<u_long>(a_NumElements)); + m_Result.append(reinterpret_cast<const char *>(&len), 4); m_Result.append(a_Value, a_NumElements); } @@ -514,18 +515,18 @@ 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); - u_long len = htonl((u_long)a_NumElements); + u_long len = htonl(static_cast<u_long>(a_NumElements)); size_t cap = m_Result.capacity(); size_t size = m_Result.length(); if ((cap - size) < (4 + a_NumElements * 4)) { m_Result.reserve(size + 4 + (a_NumElements * 4)); } - m_Result.append((const char *)&len, 4); + m_Result.append(reinterpret_cast<const char *>(&len), 4); for (size_t i = 0; i < a_NumElements; i++) { - int Element = htonl(a_Value[i]); - m_Result.append((const char *)&Element, 4); + UInt32 Element = htonl(a_Value[i]); + m_Result.append(reinterpret_cast<const char *>(&Element), 4); } } @@ -545,8 +546,8 @@ void cFastNBTWriter::Finish(void) void cFastNBTWriter::WriteString(const char * a_Data, UInt16 a_Length) { - Int16 Len = htons(a_Length); - m_Result.append((const char *)&Len, 2); + UInt16 Len = htons(a_Length); + m_Result.append(reinterpret_cast<const char *>(&Len), 2); m_Result.append(a_Data, a_Length); } |