mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Remove bytes_be_to_int
and int_to_bytes_be
Use `bytes_be_to_uint` and `uint_to_bytes_be` instead. As casting between `int` and `unsigned` preserves the bit representation of the value, it's not necessary to apply additional tricks to convert between `char` arrays and `int`.
This commit is contained in:
parent
b4d382d1b7
commit
218e6f7985
|
@ -3838,34 +3838,6 @@ const char *str_next_token(const char *str, const char *delim, char *buffer, int
|
|||
return tok + len;
|
||||
}
|
||||
|
||||
int bytes_be_to_int(const unsigned char *bytes)
|
||||
{
|
||||
int Result;
|
||||
unsigned char *pResult = (unsigned char *)&Result;
|
||||
for(unsigned i = 0; i < sizeof(int); i++)
|
||||
{
|
||||
#if defined(CONF_ARCH_ENDIAN_BIG)
|
||||
pResult[i] = bytes[i];
|
||||
#else
|
||||
pResult[i] = bytes[sizeof(int) - i - 1];
|
||||
#endif
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
void int_to_bytes_be(unsigned char *bytes, int value)
|
||||
{
|
||||
const unsigned char *pValue = (const unsigned char *)&value;
|
||||
for(unsigned i = 0; i < sizeof(int); i++)
|
||||
{
|
||||
#if defined(CONF_ARCH_ENDIAN_BIG)
|
||||
bytes[i] = pValue[i];
|
||||
#else
|
||||
bytes[sizeof(int) - i - 1] = pValue[i];
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
unsigned bytes_be_to_uint(const unsigned char *bytes)
|
||||
{
|
||||
return ((bytes[0] & 0xffu) << 24u) | ((bytes[1] & 0xffu) << 16u) | ((bytes[2] & 0xffu) << 8u) | (bytes[3] & 0xffu);
|
||||
|
|
|
@ -2386,29 +2386,6 @@ const char *str_next_token(const char *str, const char *delim, char *buffer, int
|
|||
*/
|
||||
int str_in_list(const char *list, const char *delim, const char *needle);
|
||||
|
||||
/*
|
||||
Function: bytes_be_to_int
|
||||
Packs 4 big endian bytes into an int
|
||||
|
||||
Returns:
|
||||
The packed int
|
||||
|
||||
Remarks:
|
||||
- Assumes the passed array is 4 bytes
|
||||
- Assumes int is 4 bytes
|
||||
*/
|
||||
int bytes_be_to_int(const unsigned char *bytes);
|
||||
|
||||
/*
|
||||
Function: int_to_bytes_be
|
||||
Packs an int into 4 big endian bytes
|
||||
|
||||
Remarks:
|
||||
- Assumes the passed array is 4 bytes
|
||||
- Assumes int is 4 bytes
|
||||
*/
|
||||
void int_to_bytes_be(unsigned char *bytes, int value);
|
||||
|
||||
/*
|
||||
Function: bytes_be_to_uint
|
||||
Packs 4 big endian bytes into an unsigned
|
||||
|
|
|
@ -4088,8 +4088,8 @@ int CClient::HandleChecksum(int Conn, CUuid Uuid, CUnpacker *pUnpacker)
|
|||
int FileStart = maximum(Start, (int)sizeof(m_Checksum.m_aBytes));
|
||||
unsigned char aStartBytes[4];
|
||||
unsigned char aEndBytes[4];
|
||||
int_to_bytes_be(aStartBytes, Start);
|
||||
int_to_bytes_be(aEndBytes, End);
|
||||
uint_to_bytes_be(aStartBytes, Start);
|
||||
uint_to_bytes_be(aEndBytes, End);
|
||||
|
||||
if(Start <= (int)sizeof(m_Checksum.m_aBytes))
|
||||
{
|
||||
|
|
|
@ -145,11 +145,11 @@ int CGhostRecorder::Stop(int Ticks, int Time)
|
|||
io_seek(m_File, gs_NumTicksOffset, IOSEEK_START);
|
||||
|
||||
unsigned char aNumTicks[4];
|
||||
int_to_bytes_be(aNumTicks, Ticks);
|
||||
uint_to_bytes_be(aNumTicks, Ticks);
|
||||
io_write(m_File, aNumTicks, sizeof(aNumTicks));
|
||||
|
||||
unsigned char aTime[4];
|
||||
int_to_bytes_be(aTime, Time);
|
||||
uint_to_bytes_be(aTime, Time);
|
||||
io_write(m_File, aTime, sizeof(aTime));
|
||||
|
||||
io_close(m_File);
|
||||
|
|
|
@ -23,12 +23,12 @@ struct CGhostHeader
|
|||
|
||||
int GetTicks() const
|
||||
{
|
||||
return bytes_be_to_int(m_aNumTicks);
|
||||
return bytes_be_to_uint(m_aNumTicks);
|
||||
}
|
||||
|
||||
int GetTime() const
|
||||
{
|
||||
return bytes_be_to_int(m_aTime);
|
||||
return bytes_be_to_uint(m_aTime);
|
||||
}
|
||||
|
||||
CGhostInfo ToGhostInfo() const
|
||||
|
|
|
@ -28,7 +28,7 @@ struct CItemEx
|
|||
{
|
||||
CItemEx Result;
|
||||
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
||||
Result.m_aUuid[i] = bytes_be_to_int(&Uuid.m_aData[i * 4]);
|
||||
Result.m_aUuid[i] = bytes_be_to_uint(&Uuid.m_aData[i * 4]);
|
||||
return Result;
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ struct CItemEx
|
|||
{
|
||||
CUuid Result;
|
||||
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
||||
int_to_bytes_be(&Result.m_aData[i * 4], m_aUuid[i]);
|
||||
uint_to_bytes_be(&Result.m_aData[i * 4], m_aUuid[i]);
|
||||
return Result;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -348,18 +348,18 @@ int CDemoRecorder::Stop()
|
|||
// add the demo length to the header
|
||||
io_seek(m_File, gs_LengthOffset, IOSEEK_START);
|
||||
unsigned char aLength[4];
|
||||
int_to_bytes_be(aLength, Length());
|
||||
uint_to_bytes_be(aLength, Length());
|
||||
io_write(m_File, aLength, sizeof(aLength));
|
||||
|
||||
// add the timeline markers to the header
|
||||
io_seek(m_File, gs_NumMarkersOffset, IOSEEK_START);
|
||||
unsigned char aNumMarkers[4];
|
||||
int_to_bytes_be(aNumMarkers, m_NumTimelineMarkers);
|
||||
uint_to_bytes_be(aNumMarkers, m_NumTimelineMarkers);
|
||||
io_write(m_File, aNumMarkers, sizeof(aNumMarkers));
|
||||
for(int i = 0; i < m_NumTimelineMarkers; i++)
|
||||
{
|
||||
unsigned char aMarker[4];
|
||||
int_to_bytes_be(aMarker, m_aTimelineMarkers[i]);
|
||||
uint_to_bytes_be(aMarker, m_aTimelineMarkers[i]);
|
||||
io_write(m_File, aMarker, sizeof(aMarker));
|
||||
}
|
||||
|
||||
|
@ -457,7 +457,7 @@ int CDemoPlayer::ReadChunkHeader(int *pType, int *pSize, int *pTick)
|
|||
unsigned char aTickdata[4];
|
||||
if(io_read(m_File, aTickdata, sizeof(aTickdata)) != sizeof(aTickdata))
|
||||
return -1;
|
||||
*pTick = bytes_be_to_int(aTickdata);
|
||||
*pTick = bytes_be_to_uint(aTickdata);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -824,11 +824,11 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const
|
|||
if(m_Info.m_Header.m_Version > gs_OldVersion)
|
||||
{
|
||||
// get timeline markers
|
||||
int Num = bytes_be_to_int(m_Info.m_TimelineMarkers.m_aNumTimelineMarkers);
|
||||
int Num = bytes_be_to_uint(m_Info.m_TimelineMarkers.m_aNumTimelineMarkers);
|
||||
m_Info.m_Info.m_NumTimelineMarkers = clamp<int>(Num, 0, MAX_TIMELINE_MARKERS);
|
||||
for(int i = 0; i < m_Info.m_Info.m_NumTimelineMarkers; i++)
|
||||
{
|
||||
m_Info.m_Info.m_aTimelineMarkers[i] = bytes_be_to_int(m_Info.m_TimelineMarkers.m_aTimelineMarkers[i]);
|
||||
m_Info.m_Info.m_aTimelineMarkers[i] = bytes_be_to_uint(m_Info.m_TimelineMarkers.m_aTimelineMarkers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1117,7 +1117,7 @@ bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, i
|
|||
io_read(File, pTimelineMarkers, sizeof(CTimelineMarkers));
|
||||
|
||||
str_copy(pMapInfo->m_aName, pDemoHeader->m_aMapName);
|
||||
pMapInfo->m_Crc = bytes_be_to_int(pDemoHeader->m_aMapCrc);
|
||||
pMapInfo->m_Crc = bytes_be_to_uint(pDemoHeader->m_aMapCrc);
|
||||
|
||||
SHA256_DIGEST Sha256 = SHA256_ZEROED;
|
||||
if(pDemoHeader->m_Version >= gs_Sha256Version)
|
||||
|
@ -1138,7 +1138,7 @@ bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, i
|
|||
}
|
||||
pMapInfo->m_Sha256 = Sha256;
|
||||
|
||||
pMapInfo->m_Size = bytes_be_to_int(pDemoHeader->m_aMapSize);
|
||||
pMapInfo->m_Size = bytes_be_to_uint(pDemoHeader->m_aMapSize);
|
||||
|
||||
io_close(File);
|
||||
return !(mem_comp(pDemoHeader->m_aMarker, gs_aHeaderMarker, sizeof(gs_aHeaderMarker)) || pDemoHeader->m_Version < gs_OldVersion);
|
||||
|
|
|
@ -47,7 +47,7 @@ int CSnapshot::GetExternalItemType(int InternalType) const
|
|||
const CSnapshotItem *pTypeItem = GetItem(TypeItemIndex);
|
||||
CUuid Uuid;
|
||||
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
||||
int_to_bytes_be(&Uuid.m_aData[i * 4], pTypeItem->Data()[i]);
|
||||
uint_to_bytes_be(&Uuid.m_aData[i * 4], pTypeItem->Data()[i]);
|
||||
|
||||
return g_UuidManager.LookupUuid(Uuid);
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ const void *CSnapshot::FindItem(int Type, int ID) const
|
|||
CUuid TypeUuid = g_UuidManager.GetUuid(Type);
|
||||
int aTypeUuidItem[sizeof(CUuid) / 4];
|
||||
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
||||
aTypeUuidItem[i] = bytes_be_to_int(&TypeUuid.m_aData[i * 4]);
|
||||
aTypeUuidItem[i] = bytes_be_to_uint(&TypeUuid.m_aData[i * 4]);
|
||||
|
||||
bool Found = false;
|
||||
for(int i = 0; i < m_NumItems; i++)
|
||||
|
@ -630,7 +630,7 @@ void CSnapshotBuilder::AddExtendedItemType(int Index)
|
|||
if(pUuidItem)
|
||||
{
|
||||
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
||||
pUuidItem[i] = bytes_be_to_int(&Uuid.m_aData[i * 4]);
|
||||
pUuidItem[i] = bytes_be_to_uint(&Uuid.m_aData[i * 4]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -397,12 +397,12 @@ protected:
|
|||
|
||||
int NumMarkers() const
|
||||
{
|
||||
return clamp<int>(bytes_be_to_int(m_TimelineMarkers.m_aNumTimelineMarkers), 0, MAX_TIMELINE_MARKERS);
|
||||
return clamp<int>(bytes_be_to_uint(m_TimelineMarkers.m_aNumTimelineMarkers), 0, MAX_TIMELINE_MARKERS);
|
||||
}
|
||||
|
||||
int Length() const
|
||||
{
|
||||
return bytes_be_to_int(m_Info.m_aLength);
|
||||
return bytes_be_to_uint(m_Info.m_aLength);
|
||||
}
|
||||
|
||||
unsigned Size() const
|
||||
|
|
|
@ -12,8 +12,8 @@ TEST(BytePacking, RoundtripInt)
|
|||
for(auto i : INT_DATA)
|
||||
{
|
||||
unsigned char aPacked[4];
|
||||
int_to_bytes_be(aPacked, i);
|
||||
EXPECT_EQ(bytes_be_to_int(aPacked), i);
|
||||
uint_to_bytes_be(aPacked, i);
|
||||
EXPECT_EQ(bytes_be_to_uint(aPacked), i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue