Fix binary conversion functions for big endian systems
The code was broken on big endian systems by
07e9546855
. When doing an explicit swap
one must distinguish the endianness the code runs on.
This commit is contained in:
parent
30cefc2fd3
commit
9191117120
|
@ -1,7 +1,16 @@
|
||||||
|
// assert that CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL is defined; a value of:
|
||||||
|
// - 0 means to define functions for BE namespace
|
||||||
|
// - 1 means to define functions for LE namespace
|
||||||
#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
|
#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
|
||||||
#error "Do not include binaryconversionprivate.h directly."
|
#error "Do not include binaryconversionprivate.h directly."
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
// define macro if swapping byte order is required
|
||||||
|
#if (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 && defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)) \
|
||||||
|
|| (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 1 && defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN))
|
||||||
|
#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
|
||||||
|
#endif
|
||||||
|
|
||||||
// disable warnings about sign conversions when using GCC or Clang
|
// disable warnings about sign conversions when using GCC or Clang
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
|
@ -144,7 +153,7 @@ template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILIT
|
||||||
{
|
{
|
||||||
auto dst = T();
|
auto dst = T();
|
||||||
std::memcpy(&dst, value, sizeof(T));
|
std::memcpy(&dst, value, sizeof(T));
|
||||||
#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
|
#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
|
||||||
dst = swapOrder(dst);
|
dst = swapOrder(dst);
|
||||||
#endif
|
#endif
|
||||||
return dst;
|
return dst;
|
||||||
|
@ -176,7 +185,7 @@ CPP_UTILITIES_EXPORT inline void getBytes24(std::uint32_t value, char *outputbuf
|
||||||
*/
|
*/
|
||||||
template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline void getBytes(T value, char *outputbuffer)
|
template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline void getBytes(T value, char *outputbuffer)
|
||||||
{
|
{
|
||||||
#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
|
#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
|
||||||
value = swapOrder(value);
|
value = swapOrder(value);
|
||||||
#endif
|
#endif
|
||||||
std::memcpy(outputbuffer, &value, sizeof(T));
|
std::memcpy(outputbuffer, &value, sizeof(T));
|
||||||
|
@ -206,4 +215,6 @@ CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
|
||||||
|
|
||||||
#endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
|
#endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
|
||||||
|
|
Loading…
Reference in New Issue