Skip to content

Commit

Permalink
SafeFile: do endian-swap on float tag values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matoro committed Dec 16, 2023
1 parent e26d06a commit 25aaf8b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/SafeFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <common/Format.h> // Needed for CFormat
#include "CompilerSpecific.h" // Needed for __FUNCTION__

#include <cstring> // For std::memcpy

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

Expand Down Expand Up @@ -189,7 +190,11 @@ float CFileDataIO::ReadFloat() const
{
float retVal;
Read(&retVal, sizeof(float));
return retVal;
uint32_t toswap{};
std::memcpy(&toswap, &retVal, sizeof(toswap));
toswap = ENDIAN_SWAP_32(toswap);
std::memcpy(&retVal, &toswap, sizeof(retVal));
return retVal;
}


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

void CFileDataIO::WriteFloat(float value)
{
uint32_t toswap{};
std::memcpy(&toswap, &value, sizeof(toswap));
toswap = ENDIAN_SWAP_32(toswap);
std::memcpy(&value, &toswap, sizeof(value));
Write(&value, sizeof(float));
}

Expand Down

0 comments on commit 25aaf8b

Please sign in to comment.