mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-04-29 19:44:49 +02:00
Implement serialization for big-endian platforms
This commit is contained in:
parent
711f9a4553
commit
d7cd4ff3ae
1 changed files with 46 additions and 4 deletions
|
@ -23,14 +23,11 @@
|
||||||
#define SERIALUTIL_H
|
#define SERIALUTIL_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <SDL_endian.h>
|
#include <SDL_endian.h>
|
||||||
|
|
||||||
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
|
||||||
#error "Non little endian systems not supported"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static inline int32_t
|
static inline int32_t
|
||||||
readInt32(const char **dataP)
|
readInt32(const char **dataP)
|
||||||
{
|
{
|
||||||
|
@ -39,23 +36,56 @@ readInt32(const char **dataP)
|
||||||
memcpy(&result, *dataP, 4);
|
memcpy(&result, *dataP, 4);
|
||||||
*dataP += 4;
|
*dataP += 4;
|
||||||
|
|
||||||
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
static_assert(sizeof(unsigned long) == sizeof(int32_t), "unsigned long should be 32 bits");
|
||||||
|
result = (int32_t)_byteswap_ulong((unsigned long)result);
|
||||||
|
# else
|
||||||
|
result = (int32_t)__builtin_bswap32((uint32_t)result);
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline double
|
static inline double
|
||||||
readDouble(const char **dataP)
|
readDouble(const char **dataP)
|
||||||
{
|
{
|
||||||
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||||
|
uint64_t result;
|
||||||
|
|
||||||
|
memcpy(&result, *dataP, 8);
|
||||||
|
*dataP += 8;
|
||||||
|
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
result = (uint64_t)_byteswap_uint64((unsigned __int64)result);
|
||||||
|
# else
|
||||||
|
result = __builtin_bswap64(result);
|
||||||
|
# endif
|
||||||
|
|
||||||
|
return *(double *)&result;
|
||||||
|
#else
|
||||||
double result;
|
double result;
|
||||||
|
|
||||||
memcpy(&result, *dataP, 8);
|
memcpy(&result, *dataP, 8);
|
||||||
*dataP += 8;
|
*dataP += 8;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
writeInt32(char **dataP, int32_t value)
|
writeInt32(char **dataP, int32_t value)
|
||||||
{
|
{
|
||||||
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
static_assert(sizeof(unsigned long) == sizeof(int32_t), "unsigned long should be 32 bits");
|
||||||
|
value = (int32_t)_byteswap_ulong((unsigned long)value);
|
||||||
|
# else
|
||||||
|
value = (int32_t)__builtin_bswap32((uint32_t)value);
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
memcpy(*dataP, &value, 4);
|
memcpy(*dataP, &value, 4);
|
||||||
*dataP += 4;
|
*dataP += 4;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +93,19 @@ writeInt32(char **dataP, int32_t value)
|
||||||
static inline void
|
static inline void
|
||||||
writeDouble(char **dataP, double value)
|
writeDouble(char **dataP, double value)
|
||||||
{
|
{
|
||||||
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||||
|
uint64_t valueUint = *(uint64_t *)&value;
|
||||||
|
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
valueUint = (uint64_t)_byteswap_uint64((unsigned __int64)valueUint);
|
||||||
|
# else
|
||||||
|
valueUint = __builtin_bswap64(valueUint);
|
||||||
|
# endif
|
||||||
|
|
||||||
|
memcpy(*dataP, &valueUint, 8);
|
||||||
|
#else
|
||||||
memcpy(*dataP, &value, 8);
|
memcpy(*dataP, &value, 8);
|
||||||
|
#endif
|
||||||
*dataP += 8;
|
*dataP += 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue