From 218e6f7985bd1dc08ee901a45ec738a914af379e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 4 Feb 2023 01:02:11 +0100 Subject: [PATCH] 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`. --- src/base/system.cpp | 28 ---------------------------- src/base/system.h | 23 ----------------------- src/engine/client/client.cpp | 4 ++-- src/engine/client/ghost.cpp | 4 ++-- src/engine/client/ghost.h | 4 ++-- src/engine/shared/datafile.cpp | 4 ++-- src/engine/shared/demo.cpp | 16 ++++++++-------- src/engine/shared/snapshot.cpp | 6 +++--- src/game/client/components/menus.h | 4 ++-- src/test/bytes_be.cpp | 4 ++-- 10 files changed, 23 insertions(+), 74 deletions(-) diff --git a/src/base/system.cpp b/src/base/system.cpp index 9ffee9572..a98cad63b 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -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); diff --git a/src/base/system.h b/src/base/system.h index 538ed1a19..a5efde215 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -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 diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 22564b489..ec3eff803 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -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)) { diff --git a/src/engine/client/ghost.cpp b/src/engine/client/ghost.cpp index b9b87ff77..6d989231f 100644 --- a/src/engine/client/ghost.cpp +++ b/src/engine/client/ghost.cpp @@ -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); diff --git a/src/engine/client/ghost.h b/src/engine/client/ghost.h index 0de59870b..f3acb5472 100644 --- a/src/engine/client/ghost.h +++ b/src/engine/client/ghost.h @@ -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 diff --git a/src/engine/shared/datafile.cpp b/src/engine/shared/datafile.cpp index 395c62c25..498c038e7 100644 --- a/src/engine/shared/datafile.cpp +++ b/src/engine/shared/datafile.cpp @@ -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; } }; diff --git a/src/engine/shared/demo.cpp b/src/engine/shared/demo.cpp index 0d7c43422..2ffb6935f 100644 --- a/src/engine/shared/demo.cpp +++ b/src/engine/shared/demo.cpp @@ -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(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); diff --git a/src/engine/shared/snapshot.cpp b/src/engine/shared/snapshot.cpp index 8ad03f8f8..a088a1cfc 100644 --- a/src/engine/shared/snapshot.cpp +++ b/src/engine/shared/snapshot.cpp @@ -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]); } } diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 1e514f81f..3d2cbbadf 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -397,12 +397,12 @@ protected: int NumMarkers() const { - return clamp(bytes_be_to_int(m_TimelineMarkers.m_aNumTimelineMarkers), 0, MAX_TIMELINE_MARKERS); + return clamp(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 diff --git a/src/test/bytes_be.cpp b/src/test/bytes_be.cpp index 0f9493971..8d0282cf8 100644 --- a/src/test/bytes_be.cpp +++ b/src/test/bytes_be.cpp @@ -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); } }