From 1591ebddd4f95f922afb5bdb73f810fc7241acd1 Mon Sep 17 00:00:00 2001 From: Nils Fenner Date: Wed, 20 Feb 2019 13:46:18 +0100 Subject: [PATCH] fix type casts in endian helper macros --- src/endianhelper.h | 114 +++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/src/endianhelper.h b/src/endianhelper.h index 37139c6..0c23732 100644 --- a/src/endianhelper.h +++ b/src/endianhelper.h @@ -44,11 +44,12 @@ # if defined(ntohs) # define _msgpack_be16(x) ntohs(x) # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400) -# define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x)) +# define _msgpack_be16(x) ( \ + static_cast(_byteswap_ushort(static_cast(x))) ) # else # define _msgpack_be16(x) ( \ - ((((quint16)x) << 8) ) | \ - ((((quint16)x) >> 8) ) ) + (((static_cast(x)) << 8) ) | \ + (((static_cast(x)) >> 8) ) ) # endif #else # define _msgpack_be16(x) ntohs(x) @@ -58,13 +59,14 @@ # if defined(ntohl) # define _msgpack_be32(x) ntohl(x) # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400) -# define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x)) +# define _msgpack_be32(x) ( \ + static_cast(_byteswap_ulong(static_cast(x))) ) # else # define _msgpack_be32(x) \ - ( ((((quint32)x) << 24) ) | \ - ((((quint32)x) << 8) & 0x00ff0000U ) | \ - ((((quint32)x) >> 8) & 0x0000ff00U ) | \ - ((((quint32)x) >> 24) ) ) + ( (((static_cast(x)) << 24) ) | \ + (((static_cast(x)) << 8) & 0x00ff0000U ) | \ + (((static_cast(x)) >> 8) & 0x0000ff00U ) | \ + (((static_cast(x)) >> 24) ) ) # endif #else # define _msgpack_be32(x) ntohl(x) @@ -78,35 +80,35 @@ # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x) #else #define _msgpack_be64(x) \ - ( ((((quint64)x) << 56) ) | \ - ((((quint64)x) << 40) & 0x00ff000000000000ULL ) | \ - ((((quint64)x) << 24) & 0x0000ff0000000000ULL ) | \ - ((((quint64)x) << 8) & 0x000000ff00000000ULL ) | \ - ((((quint64)x) >> 8) & 0x00000000ff000000ULL ) | \ - ((((quint64)x) >> 24) & 0x0000000000ff0000ULL ) | \ - ((((quint64)x) >> 40) & 0x000000000000ff00ULL ) | \ - ((((quint64)x) >> 56) ) ) + ( (((static_cast(x)) << 56) ) | \ + (((static_cast(x)) << 40) & 0x00ff000000000000ULL ) | \ + (((static_cast(x)) << 24) & 0x0000ff0000000000ULL ) | \ + (((static_cast(x)) << 8) & 0x000000ff00000000ULL ) | \ + (((static_cast(x)) >> 8) & 0x00000000ff000000ULL ) | \ + (((static_cast(x)) >> 24) & 0x0000000000ff0000ULL ) | \ + (((static_cast(x)) >> 40) & 0x000000000000ff00ULL ) | \ + (((static_cast(x)) >> 56) ) ) #endif -#define _msgpack_load16(cast, from) ((cast)( \ - (((quint16)((quint8*)(from))[0]) << 8) | \ - (((quint16)((quint8*)(from))[1]) ) )) +#define _msgpack_load16(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 8) | \ + ((static_cast(static_cast(from)[1])) ) ))) -#define _msgpack_load32(cast, from) ((cast)( \ - (((quint32)((quint8*)(from))[0]) << 24) | \ - (((quint32)((quint8*)(from))[1]) << 16) | \ - (((quint32)((quint8*)(from))[2]) << 8) | \ - (((quint32)((quint8*)(from))[3]) ) )) +#define _msgpack_load32(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 24) | \ + ((static_cast(static_cast(from)[1])) << 16) | \ + ((static_cast(static_cast(from)[2])) << 8) | \ + ((static_cast(static_cast(from)[3])) ) ))) -#define _msgpack_load64(cast, from) ((cast)( \ - (((quint64)((quint8*)(from))[0]) << 56) | \ - (((quint64)((quint8*)(from))[1]) << 48) | \ - (((quint64)((quint8*)(from))[2]) << 40) | \ - (((quint64)((quint8*)(from))[3]) << 32) | \ - (((quint64)((quint8*)(from))[4]) << 24) | \ - (((quint64)((quint8*)(from))[5]) << 16) | \ - (((quint64)((quint8*)(from))[6]) << 8) | \ - (((quint64)((quint8*)(from))[7]) ) )) +#define _msgpack_load64(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 56) | \ + ((static_cast(static_cast(from)[1])) << 48) | \ + ((static_cast(static_cast(from)[2])) << 40) | \ + ((static_cast(static_cast(from)[3])) << 32) | \ + ((static_cast(static_cast(from)[4])) << 24) | \ + ((static_cast(static_cast(from)[5])) << 16) | \ + ((static_cast(static_cast(from)[6])) << 8) | \ + ((static_cast(static_cast(from)[7])) ) ))) #else @@ -114,25 +116,25 @@ #define _msgpack_be32(x) (x) #define _msgpack_be64(x) (x) -#define _msgpack_load16(cast, from) ((cast)( \ - (((quint16)((quint8*)from)[0]) << 8) | \ - (((quint16)((quint8*)from)[1]) ) )) +#define _msgpack_load16(type, from) (static_cast( \ + ((static_cast(static_cast(from))[0]) << 8) | \ + ((static_cast(static_cast(from))[1]) ) )) -#define _msgpack_load32(cast, from) ((cast)( \ - (((quint32)((quint8*)from)[0]) << 24) | \ - (((quint32)((quint8*)from)[1]) << 16) | \ - (((quint32)((quint8*)from)[2]) << 8) | \ - (((quint32)((quint8*)from)[3]) ) )) +#define _msgpack_load32(type, from) (static_cast( \ + ((static_cast(static_cast(from))[0]) << 24) | \ + ((static_cast(static_cast(from))[1]) << 16) | \ + ((static_cast(static_cast(from))[2]) << 8) | \ + ((static_cast(static_cast(from))[3]) ) )) -#define _msgpack_load64(cast, from) ((cast)( \ - (((quint64)((quint8*)from)[0]) << 56) | \ - (((quint64)((quint8*)from)[1]) << 48) | \ - (((quint64)((quint8*)from)[2]) << 40) | \ - (((quint64)((quint8*)from)[3]) << 32) | \ - (((quint64)((quint8*)from)[4]) << 24) | \ - (((quint64)((quint8*)from)[5]) << 16) | \ - (((quint64)((quint8*)from)[6]) << 8) | \ - (((quint64)((quint8*)from)[7]) ) )) +#define _msgpack_load64(type, from) ((static_cast( \ + ((static_cast(static_cast(from))[0]) << 56) | \ + ((static_cast(static_cast(from))[1]) << 48) | \ + ((static_cast(static_cast(from))[2]) << 40) | \ + ((static_cast(static_cast(from))[3]) << 32) | \ + ((static_cast(static_cast(from))[4]) << 24) | \ + ((static_cast(static_cast(from))[5]) << 16) | \ + ((static_cast(static_cast(from))[6]) << 8) | \ + ((static_cast(static_cast(from))[7]) ) ))) #endif @@ -144,12 +146,12 @@ do { quint64 val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0) /* -#define _msgpack_load16(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); }) -#define _msgpack_load32(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); }) -#define _msgpack_load64(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); }) +#define _msgpack_load16(type, from) \ + ({ type val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); }) +#define _msgpack_load32(type, from) \ + ({ type val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); }) +#define _msgpack_load64(type, from) \ + ({ type val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); }) */ #endif /* msgpack/sysdep.h */