Use sizeof(int32_t) instead of 4

This commit is contained in:
Robert Müller 2023-02-04 01:22:49 +01:00
parent 64081ad3e4
commit be6862e997
11 changed files with 52 additions and 52 deletions

View file

@ -4086,8 +4086,8 @@ int CClient::HandleChecksum(int Conn, CUuid Uuid, CUnpacker *pUnpacker)
int End = Start + Length; int End = Start + Length;
int ChecksumBytesEnd = minimum(End, (int)sizeof(m_Checksum.m_aBytes)); int ChecksumBytesEnd = minimum(End, (int)sizeof(m_Checksum.m_aBytes));
int FileStart = maximum(Start, (int)sizeof(m_Checksum.m_aBytes)); int FileStart = maximum(Start, (int)sizeof(m_Checksum.m_aBytes));
unsigned char aStartBytes[4]; unsigned char aStartBytes[sizeof(int32_t)];
unsigned char aEndBytes[4]; unsigned char aEndBytes[sizeof(int32_t)];
uint_to_bytes_be(aStartBytes, Start); uint_to_bytes_be(aStartBytes, Start);
uint_to_bytes_be(aEndBytes, End); uint_to_bytes_be(aEndBytes, End);

View file

@ -83,7 +83,7 @@ void CGhostRecorder::WriteData(int Type, const void *pData, int Size)
mem_copy(Data.m_aData, pData, Size); mem_copy(Data.m_aData, pData, Size);
if(m_LastItem.m_Type == Data.m_Type) if(m_LastItem.m_Type == Data.m_Type)
DiffItem((int *)m_LastItem.m_aData, (int *)Data.m_aData, (int *)m_pBufferPos, Size / 4); DiffItem((int *)m_LastItem.m_aData, (int *)Data.m_aData, (int *)m_pBufferPos, Size / sizeof(int32_t));
else else
{ {
FlushChunk(); FlushChunk();
@ -144,11 +144,11 @@ int CGhostRecorder::Stop(int Ticks, int Time)
// write down num shots and time // write down num shots and time
io_seek(m_File, gs_NumTicksOffset, IOSEEK_START); io_seek(m_File, gs_NumTicksOffset, IOSEEK_START);
unsigned char aNumTicks[4]; unsigned char aNumTicks[sizeof(int32_t)];
uint_to_bytes_be(aNumTicks, Ticks); uint_to_bytes_be(aNumTicks, Ticks);
io_write(m_File, aNumTicks, sizeof(aNumTicks)); io_write(m_File, aNumTicks, sizeof(aNumTicks));
unsigned char aTime[4]; unsigned char aTime[sizeof(int32_t)];
uint_to_bytes_be(aTime, Time); uint_to_bytes_be(aTime, Time);
io_write(m_File, aTime, sizeof(aTime)); io_write(m_File, aTime, sizeof(aTime));
@ -341,7 +341,7 @@ bool CGhostLoader::ReadData(int Type, void *pData, int Size)
CGhostItem Data(Type); CGhostItem Data(Type);
if(m_LastItem.m_Type == Data.m_Type) if(m_LastItem.m_Type == Data.m_Type)
UndiffItem((int *)m_LastItem.m_aData, (int *)m_pBufferPos, (int *)Data.m_aData, Size / 4); UndiffItem((int *)m_LastItem.m_aData, (int *)m_pBufferPos, (int *)Data.m_aData, Size / sizeof(int32_t));
else else
mem_copy(Data.m_aData, m_pBufferPos, Size); mem_copy(Data.m_aData, m_pBufferPos, Size);

View file

@ -16,9 +16,9 @@ struct CGhostHeader
unsigned char m_Version; unsigned char m_Version;
char m_aOwner[MAX_NAME_LENGTH]; char m_aOwner[MAX_NAME_LENGTH];
char m_aMap[64]; char m_aMap[64];
unsigned char m_aZeroes[4]; // Crc before version 6 unsigned char m_aZeroes[sizeof(int32_t)]; // Crc before version 6
unsigned char m_aNumTicks[4]; unsigned char m_aNumTicks[sizeof(int32_t)];
unsigned char m_aTime[4]; unsigned char m_aTime[sizeof(int32_t)];
SHA256_DIGEST m_MapSha256; SHA256_DIGEST m_MapSha256;
int GetTicks() const int GetTicks() const

View file

@ -29,17 +29,17 @@ struct CDemoHeader
unsigned char m_Version; unsigned char m_Version;
char m_aNetversion[64]; char m_aNetversion[64];
char m_aMapName[64]; char m_aMapName[64];
unsigned char m_aMapSize[4]; unsigned char m_aMapSize[sizeof(int32_t)];
unsigned char m_aMapCrc[4]; unsigned char m_aMapCrc[sizeof(int32_t)];
char m_aType[8]; char m_aType[8];
unsigned char m_aLength[4]; unsigned char m_aLength[sizeof(int32_t)];
char m_aTimestamp[20]; char m_aTimestamp[20];
}; };
struct CTimelineMarkers struct CTimelineMarkers
{ {
unsigned char m_aNumTimelineMarkers[4]; unsigned char m_aNumTimelineMarkers[sizeof(int32_t)];
unsigned char m_aTimelineMarkers[MAX_TIMELINE_MARKERS][4]; unsigned char m_aTimelineMarkers[MAX_TIMELINE_MARKERS][sizeof(int32_t)];
}; };
struct CMapInfo struct CMapInfo

View file

@ -498,7 +498,7 @@ CRegister::CRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, int
m_aVerifyPacketPrefix[HEADER_LEN + UUID_MAXSTRSIZE - 1] = ':'; m_aVerifyPacketPrefix[HEADER_LEN + UUID_MAXSTRSIZE - 1] = ':';
// The DDNet code uses the `unsigned` security token in memory byte order. // The DDNet code uses the `unsigned` security token in memory byte order.
unsigned char aTokenBytes[4]; unsigned char aTokenBytes[sizeof(int32_t)];
mem_copy(aTokenBytes, &SixupSecurityToken, sizeof(aTokenBytes)); mem_copy(aTokenBytes, &SixupSecurityToken, sizeof(aTokenBytes));
str_format(m_aConnlessTokenHex, sizeof(m_aConnlessTokenHex), "%08x", bytes_be_to_uint(aTokenBytes)); str_format(m_aConnlessTokenHex, sizeof(m_aConnlessTokenHex), "%08x", bytes_be_to_uint(aTokenBytes));

View file

@ -22,21 +22,21 @@ enum
struct CItemEx struct CItemEx
{ {
int m_aUuid[sizeof(CUuid) / 4]; int m_aUuid[sizeof(CUuid) / sizeof(int32_t)];
static CItemEx FromUuid(CUuid Uuid) static CItemEx FromUuid(CUuid Uuid)
{ {
CItemEx Result; CItemEx Result;
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++)
Result.m_aUuid[i] = bytes_be_to_uint(&Uuid.m_aData[i * 4]); Result.m_aUuid[i] = bytes_be_to_uint(&Uuid.m_aData[i * sizeof(int32_t)]);
return Result; return Result;
} }
CUuid ToUuid() const CUuid ToUuid() const
{ {
CUuid Result; CUuid Result;
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++)
uint_to_bytes_be(&Result.m_aData[i * 4], m_aUuid[i]); uint_to_bytes_be(&Result.m_aData[i * sizeof(int32_t)], m_aUuid[i]);
return Result; return Result;
} }
}; };

View file

@ -225,7 +225,7 @@ void CDemoRecorder::WriteTickMarker(int Tick, int Keyframe)
{ {
if(m_LastTickMarker == -1 || Tick - m_LastTickMarker > CHUNKMASK_TICK || Keyframe) if(m_LastTickMarker == -1 || Tick - m_LastTickMarker > CHUNKMASK_TICK || Keyframe)
{ {
unsigned char aChunk[5]; unsigned char aChunk[sizeof(int32_t) + 1];
aChunk[0] = CHUNKTYPEFLAG_TICKMARKER; aChunk[0] = CHUNKTYPEFLAG_TICKMARKER;
uint_to_bytes_be(aChunk + 1, Tick); uint_to_bytes_be(aChunk + 1, Tick);
@ -347,18 +347,18 @@ int CDemoRecorder::Stop()
// add the demo length to the header // add the demo length to the header
io_seek(m_File, gs_LengthOffset, IOSEEK_START); io_seek(m_File, gs_LengthOffset, IOSEEK_START);
unsigned char aLength[4]; unsigned char aLength[sizeof(int32_t)];
uint_to_bytes_be(aLength, Length()); uint_to_bytes_be(aLength, Length());
io_write(m_File, aLength, sizeof(aLength)); io_write(m_File, aLength, sizeof(aLength));
// add the timeline markers to the header // add the timeline markers to the header
io_seek(m_File, gs_NumMarkersOffset, IOSEEK_START); io_seek(m_File, gs_NumMarkersOffset, IOSEEK_START);
unsigned char aNumMarkers[4]; unsigned char aNumMarkers[sizeof(int32_t)];
uint_to_bytes_be(aNumMarkers, m_NumTimelineMarkers); uint_to_bytes_be(aNumMarkers, m_NumTimelineMarkers);
io_write(m_File, aNumMarkers, sizeof(aNumMarkers)); io_write(m_File, aNumMarkers, sizeof(aNumMarkers));
for(int i = 0; i < m_NumTimelineMarkers; i++) for(int i = 0; i < m_NumTimelineMarkers; i++)
{ {
unsigned char aMarker[4]; unsigned char aMarker[sizeof(int32_t)];
uint_to_bytes_be(aMarker, m_aTimelineMarkers[i]); uint_to_bytes_be(aMarker, m_aTimelineMarkers[i]);
io_write(m_File, aMarker, sizeof(aMarker)); io_write(m_File, aMarker, sizeof(aMarker));
} }
@ -454,7 +454,7 @@ int CDemoPlayer::ReadChunkHeader(int *pType, int *pSize, int *pTick)
} }
else else
{ {
unsigned char aTickdata[4]; unsigned char aTickdata[sizeof(int32_t)];
if(io_read(m_File, aTickdata, sizeof(aTickdata)) != sizeof(aTickdata)) if(io_read(m_File, aTickdata, sizeof(aTickdata)) != sizeof(aTickdata))
return -1; return -1;
*pTick = bytes_be_to_uint(aTickdata); *pTick = bytes_be_to_uint(aTickdata);

View file

@ -564,8 +564,8 @@ int CNetServer::OnSixupCtrlMsg(NETADDR &Addr, CNetChunk *pChunk, int ControlMsg,
else if(ControlMsg == NET_CTRLMSG_CONNECT) else if(ControlMsg == NET_CTRLMSG_CONNECT)
{ {
SECURITY_TOKEN MyToken = GetToken(Addr); SECURITY_TOKEN MyToken = GetToken(Addr);
unsigned char aToken[4]; unsigned char aToken[sizeof(SECURITY_TOKEN)];
mem_copy(aToken, &MyToken, 4); mem_copy(aToken, &MyToken, sizeof(aToken));
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CONNECTACCEPT, aToken, sizeof(aToken), ResponseToken, true); CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CONNECTACCEPT, aToken, sizeof(aToken), ResponseToken, true);
if(Token == MyToken) if(Token == MyToken)

View file

@ -46,8 +46,8 @@ int CSnapshot::GetExternalItemType(int InternalType) const
} }
const CSnapshotItem *pTypeItem = GetItem(TypeItemIndex); const CSnapshotItem *pTypeItem = GetItem(TypeItemIndex);
CUuid Uuid; CUuid Uuid;
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++)
uint_to_bytes_be(&Uuid.m_aData[i * 4], pTypeItem->Data()[i]); uint_to_bytes_be(&Uuid.m_aData[i * sizeof(int32_t)], pTypeItem->Data()[i]);
return g_UuidManager.LookupUuid(Uuid); return g_UuidManager.LookupUuid(Uuid);
} }
@ -69,9 +69,9 @@ const void *CSnapshot::FindItem(int Type, int ID) const
if(Type >= OFFSET_UUID) if(Type >= OFFSET_UUID)
{ {
CUuid TypeUuid = g_UuidManager.GetUuid(Type); CUuid TypeUuid = g_UuidManager.GetUuid(Type);
int aTypeUuidItem[sizeof(CUuid) / 4]; int aTypeUuidItem[sizeof(CUuid) / sizeof(int32_t)];
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++)
aTypeUuidItem[i] = bytes_be_to_uint(&TypeUuid.m_aData[i * 4]); aTypeUuidItem[i] = bytes_be_to_uint(&TypeUuid.m_aData[i * sizeof(int32_t)]);
bool Found = false; bool Found = false;
for(int i = 0; i < m_NumItems; i++) for(int i = 0; i < m_NumItems; i++)
@ -105,7 +105,7 @@ unsigned CSnapshot::Crc()
const CSnapshotItem *pItem = GetItem(i); const CSnapshotItem *pItem = GetItem(i);
int Size = GetItemSize(i); int Size = GetItemSize(i);
for(int b = 0; b < Size / 4; b++) for(size_t b = 0; b < Size / sizeof(int32_t); b++)
Crc += pItem->Data()[b]; Crc += pItem->Data()[b];
} }
return Crc; return Crc;
@ -119,8 +119,8 @@ void CSnapshot::DebugDump()
const CSnapshotItem *pItem = GetItem(i); const CSnapshotItem *pItem = GetItem(i);
int Size = GetItemSize(i); int Size = GetItemSize(i);
dbg_msg("snapshot", "\ttype=%d id=%d", pItem->Type(), pItem->ID()); dbg_msg("snapshot", "\ttype=%d id=%d", pItem->Type(), pItem->ID());
for(int b = 0; b < Size / 4; b++) for(size_t b = 0; b < Size / sizeof(int32_t); b++)
dbg_msg("snapshot", "\t\t%3d %12d\t%08x", b, pItem->Data()[b], pItem->Data()[b]); dbg_msg("snapshot", "\t\t%3d %12d\t%08x", (int)b, pItem->Data()[b], pItem->Data()[b]);
} }
} }
@ -320,13 +320,13 @@ int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData
if(!IncludeSize) if(!IncludeSize)
pItemDataDst = pData + 2; pItemDataDst = pData + 2;
if(DiffItem(pPastItem->Data(), pCurItem->Data(), pItemDataDst, ItemSize / 4)) if(DiffItem(pPastItem->Data(), pCurItem->Data(), pItemDataDst, ItemSize / sizeof(int32_t)))
{ {
*pData++ = pCurItem->Type(); *pData++ = pCurItem->Type();
*pData++ = pCurItem->ID(); *pData++ = pCurItem->ID();
if(IncludeSize) if(IncludeSize)
*pData++ = ItemSize / 4; *pData++ = ItemSize / sizeof(int32_t);
pData += ItemSize / 4; pData += ItemSize / sizeof(int32_t);
pDelta->m_NumUpdateItems++; pDelta->m_NumUpdateItems++;
} }
} }
@ -335,10 +335,10 @@ int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData
*pData++ = pCurItem->Type(); *pData++ = pCurItem->Type();
*pData++ = pCurItem->ID(); *pData++ = pCurItem->ID();
if(IncludeSize) if(IncludeSize)
*pData++ = ItemSize / 4; *pData++ = ItemSize / sizeof(int32_t);
mem_copy(pData, pCurItem->Data(), ItemSize); mem_copy(pData, pCurItem->Data(), ItemSize);
pData += ItemSize / 4; pData += ItemSize / sizeof(int32_t);
pDelta->m_NumUpdateItems++; pDelta->m_NumUpdateItems++;
} }
} }
@ -420,9 +420,9 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
{ {
if(pData + 1 > pEnd) if(pData + 1 > pEnd)
return -103; return -103;
if(*pData < 0 || *pData > INT_MAX / 4) if(*pData < 0 || (size_t)*pData > INT_MAX / sizeof(int32_t))
return -204; return -204;
ItemSize = (*pData++) * 4; ItemSize = (*pData++) * sizeof(int32_t);
} }
if(ItemSize < 0 || RangeCheck(pEnd, pData, ItemSize)) if(ItemSize < 0 || RangeCheck(pEnd, pData, ItemSize))
@ -442,7 +442,7 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
if(FromIndex != -1) if(FromIndex != -1)
{ {
// we got an update so we need to apply the diff // we got an update so we need to apply the diff
UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / 4, &m_aSnapshotDataRate[Type]); UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / sizeof(int32_t), &m_aSnapshotDataRate[Type]);
} }
else // no previous, just copy the pData else // no previous, just copy the pData
{ {
@ -451,7 +451,7 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
} }
m_aSnapshotDataUpdates[Type]++; m_aSnapshotDataUpdates[Type]++;
pData += ItemSize / 4; pData += ItemSize / sizeof(int32_t);
} }
// finish up // finish up
@ -629,8 +629,8 @@ void CSnapshotBuilder::AddExtendedItemType(int Index)
int *pUuidItem = (int *)NewItem(0, GetTypeFromIndex(Index), sizeof(Uuid)); // NETOBJTYPE_EX int *pUuidItem = (int *)NewItem(0, GetTypeFromIndex(Index), sizeof(Uuid)); // NETOBJTYPE_EX
if(pUuidItem) if(pUuidItem)
{ {
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++)
pUuidItem[i] = bytes_be_to_uint(&Uuid.m_aData[i * 4]); pUuidItem[i] = bytes_be_to_uint(&Uuid.m_aData[i * sizeof(int32_t)]);
} }
} }

View file

@ -423,7 +423,7 @@ void CTeeHistorian::RecordPlayerInput(int ClientID, uint32_t UniqueClientID, con
Buffer.Reset(); Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_INPUT_DIFF); Buffer.AddInt(-TEEHISTORIAN_INPUT_DIFF);
CSnapshotDelta::DiffItem((int *)&pPrev->m_Input, (int *)pInput, (int *)&DiffInput, sizeof(DiffInput) / sizeof(int)); CSnapshotDelta::DiffItem((int *)&pPrev->m_Input, (int *)pInput, (int *)&DiffInput, sizeof(DiffInput) / sizeof(int32_t));
if(m_Debug) if(m_Debug)
{ {
const int *pData = (const int *)&DiffInput; const int *pData = (const int *)&DiffInput;
@ -444,7 +444,7 @@ void CTeeHistorian::RecordPlayerInput(int ClientID, uint32_t UniqueClientID, con
} }
} }
Buffer.AddInt(ClientID); Buffer.AddInt(ClientID);
for(int i = 0; i < (int)(sizeof(DiffInput) / sizeof(int)); i++) for(size_t i = 0; i < sizeof(DiffInput) / sizeof(int32_t); i++)
{ {
Buffer.AddInt(((int *)&DiffInput)[i]); Buffer.AddInt(((int *)&DiffInput)[i]);
} }

View file

@ -4,14 +4,14 @@
#include <base/system.h> #include <base/system.h>
static const int INT_DATA[] = {0, 1, -1, 32, 64, 256, -512, 12345, -123456, 1234567, 12345678, 123456789, 2147483647, (-2147483647 - 1)}; static const int32_t INT_DATA[] = {0, 1, -1, 32, 64, 256, -512, 12345, -123456, 1234567, 12345678, 123456789, 2147483647, (-2147483647 - 1)};
static const unsigned UINT_DATA[] = {0u, 1u, 2u, 32u, 64u, 256u, 512u, 12345u, 123456u, 1234567u, 12345678u, 123456789u, 2147483647u, 2147483648u, 4294967295u}; static const uint32_t UINT_DATA[] = {0u, 1u, 2u, 32u, 64u, 256u, 512u, 12345u, 123456u, 1234567u, 12345678u, 123456789u, 2147483647u, 2147483648u, 4294967295u};
TEST(BytePacking, RoundtripInt) TEST(BytePacking, RoundtripInt)
{ {
for(auto i : INT_DATA) for(auto i : INT_DATA)
{ {
unsigned char aPacked[4]; unsigned char aPacked[sizeof(int32_t)];
uint_to_bytes_be(aPacked, i); uint_to_bytes_be(aPacked, i);
EXPECT_EQ(bytes_be_to_uint(aPacked), i); EXPECT_EQ(bytes_be_to_uint(aPacked), i);
} }
@ -21,7 +21,7 @@ TEST(BytePacking, RoundtripUnsigned)
{ {
for(auto u : UINT_DATA) for(auto u : UINT_DATA)
{ {
unsigned char aPacked[4]; unsigned char aPacked[sizeof(uint32_t)];
uint_to_bytes_be(aPacked, u); uint_to_bytes_be(aPacked, u);
EXPECT_EQ(bytes_be_to_uint(aPacked), u); EXPECT_EQ(bytes_be_to_uint(aPacked), u);
} }