From e96e47e4d1eb7217a0169013c9968b8eb52bbb4e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 7 Mar 2018 02:52:28 +0100 Subject: Make serializer/deserializer endian independent --- sibs/endian.hpp | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 sibs/endian.hpp (limited to 'sibs/endian.hpp') diff --git a/sibs/endian.hpp b/sibs/endian.hpp new file mode 100644 index 0000000..13e6a31 --- /dev/null +++ b/sibs/endian.hpp @@ -0,0 +1,159 @@ +#pragma once + +/* +Endian macro copied from https://github.com/xxtea/xxtea-c/blob/master/xxtea.c +License for the macro definition: +The MIT License (MIT) +Copyright (c) 2008-2015 Ma Bingyao mabingyao@gmail.com +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +#include /* This will likely define BYTE_ORDER */ +#ifndef BYTE_ORDER +#if (BSD >= 199103) +# include +#else +#if defined(linux) || defined(__linux__) +# include +#else +#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ +#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ +#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/ +#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \ +defined(vax) || defined(ns32000) || defined(sun386) || \ +defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \ +defined(__alpha__) || defined(__alpha) +#define BYTE_ORDER LITTLE_ENDIAN +#endif +#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \ + defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \ + defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\ + defined(apollo) || defined(__convex__) || defined(_CRAY) || \ + defined(__hppa) || defined(__hp9000) || \ + defined(__hp9000s300) || defined(__hp9000s700) || \ + defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc) +#define BYTE_ORDER BIG_ENDIAN +#endif +#endif /* linux */ +#endif /* BSD */ +#endif /* BYTE_ORDER */ +#if defined(WIN32) || defined(_WIN32) +#define BYTE_ORDER LITTLE_ENDIAN +#endif +#ifndef BYTE_ORDER +#ifdef __BYTE_ORDER +#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) +#ifndef LITTLE_ENDIAN +#define LITTLE_ENDIAN __LITTLE_ENDIAN +#endif +#ifndef BIG_ENDIAN +#define BIG_ENDIAN __BIG_ENDIAN +#endif +#if (__BYTE_ORDER == __LITTLE_ENDIAN) +#define BYTE_ORDER LITTLE_ENDIAN +#else +#define BYTE_ORDER BIG_ENDIAN +#endif +#endif +#endif +#endif + +#include "types.hpp" +#include + +namespace sibs +{ + #if defined(_MSC_VER) + static u16 byteswap(u16 value) + { + return _byteswap_ushort(value); + } + static u32 byteswap(u32 value) + { + return _byteswap_ulong(value); + } + static u64 byteswap(u64 value) + { + return _byteswap_uint64(value); + } + #elif defined(__GCC__) + static u16 byteswap(u16 value) + { + return __builtin_bswap16(value); + } + static u32 byteswap(u32 value) + { + return __builtin_bswap32(value); + } + static u64 byteswap(u64 value) + { + return __builtin_bswap64(value); + } + #else + static u16 byteswap(u16 value) + { + u16 result = 0; + result |= (value & 0x00FF) << 8; + result |= (value & 0xFF00) >> 8; + return result; + } + static u32 byteswap(u32 value) + { + u32 result = 0; + result |= (value & 0x000000FF) << 24; + result |= (value & 0x0000FF00) << 8; + result |= (value & 0x00FF0000) >> 8; + result |= (value & 0xFF000000) >> 24; + return result; + } + static u64 byteswap(u64 value) + { + u64 result = 0; + result |= (value & 0x00000000000000FF) << 56; + result |= (value & 0x000000000000FF00) << 40; + result |= (value & 0x0000000000FF0000) << 24; + result |= (value & 0x00000000FF000000) << 8; + result |= (value & 0x000000FF00000000) >> 8; + result |= (value & 0x0000FF0000000000) >> 24; + result |= (value & 0x00FF000000000000) >> 40; + result |= (value & 0xFF00000000000000) >> 56; + return result; + } + #endif + + static u16 byteswap16(u16 value) + { + return byteswap(value); + } + + static u32 byteswap32(u32 value) + { + return byteswap(value); + } + + static u64 byteswap64(u64 value) + { + return byteswap(value); + } + + enum class Endianness + { + LITTLE, + BIG, + MIDDLE, // TODO: Implement conversion from/to middle endian + UNKNOWN + }; + + static Endianness getEndianness() + { + u32 endian = 0x12345678; + u8 *endianByte = (u8*)&endian; + if (endianByte[0] == (u8)0x78 && endianByte[1] == (u8)0x56) + return Endianness::LITTLE; + else if (endianByte[0] == (u8)0x12 && endianByte[1] == (u8)0x34) + return Endianness::BIG; + else + return Endianness::UNKNOWN; + } +} -- cgit v1.2.3