Skip to content

Commit 25aaf8b

Browse files
committed
SafeFile: do endian-swap on float tag values
There are comments noting that writing raw float values may not be endian-correct and indeed it is not. WX does not provide functions for endian-swapping floats, but since amule explicitly uses exclusively single-precision (32-bit) floats, just cast it into a uint32_t for the swapping. This is a no-op on little-endian.
1 parent e26d06a commit 25aaf8b

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/SafeFile.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <common/Format.h> // Needed for CFormat
3232
#include "CompilerSpecific.h" // Needed for __FUNCTION__
3333

34+
#include <cstring> // For std::memcpy
3435

3536
#define CHECK_BOM(size, x) ((size >= 3) && (x[0] == (char)0xEF) && (x[1] == (char)0xBB) && (x[2] == (char)0xBF))
3637

@@ -189,7 +190,11 @@ float CFileDataIO::ReadFloat() const
189190
{
190191
float retVal;
191192
Read(&retVal, sizeof(float));
192-
return retVal;
193+
uint32_t toswap{};
194+
std::memcpy(&toswap, &retVal, sizeof(toswap));
195+
toswap = ENDIAN_SWAP_32(toswap);
196+
std::memcpy(&retVal, &toswap, sizeof(retVal));
197+
return retVal;
193198
}
194199

195200

@@ -306,6 +311,10 @@ void CFileDataIO::WriteHash(const CMD4Hash& value)
306311

307312
void CFileDataIO::WriteFloat(float value)
308313
{
314+
uint32_t toswap{};
315+
std::memcpy(&toswap, &value, sizeof(toswap));
316+
toswap = ENDIAN_SWAP_32(toswap);
317+
std::memcpy(&value, &toswap, sizeof(value));
309318
Write(&value, sizeof(float));
310319
}
311320

0 commit comments

Comments
 (0)