From ff9ea15709ba60008f46df9adae747b8c6e71d68 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/SafeDeserializer.hpp | 33 +++++++++- sibs/SafeSerializer.hpp | 29 ++++++++- sibs/endian.hpp | 159 ++++++++++++++++++++++++++++++++++++++++++++++ sibs/types.hpp | 22 +++++++ 4 files changed, 239 insertions(+), 4 deletions(-) create mode 100644 sibs/endian.hpp create mode 100644 sibs/types.hpp (limited to 'sibs') diff --git a/sibs/SafeDeserializer.hpp b/sibs/SafeDeserializer.hpp index 3d83042..7ef6b3f 100644 --- a/sibs/SafeDeserializer.hpp +++ b/sibs/SafeDeserializer.hpp @@ -1,6 +1,7 @@ #pragma once -#include "../types.hpp" +#include "endian.hpp" +#include "types.hpp" #include "../utils.hpp" #include #include @@ -31,15 +32,41 @@ namespace sibs * Throws DeserializeException on failure */ template - T&& extract() + T extract() { constexpr usize typeSize = sizeof(T); verifyExtractSize(typeSize); size -= typeSize; T result; + #ifdef LITTLE_ENDIAN + switch(typeSize) + { + case 1: + result = *(T*)data; + break; + case 2: + *(u16*)&result = byteswap(*(u16*)data); + break; + case 4: + *(u32*)&result = byteswap(*(u32*)data); + break; + case 8: + *(u64*)&result = byteswap(*(u64*)data); + break; + default: + { + for(int i = 0; i < typeSize; ++i) + { + ((char*)&result)[i] = data[typeSize - 1 - i]; + } + break; + } + } + #else memcpy(&result, data, typeSize); + #endif data += typeSize; - return std::move(result); + return result; } /* diff --git a/sibs/SafeSerializer.hpp b/sibs/SafeSerializer.hpp index 94bcdc5..e081585 100644 --- a/sibs/SafeSerializer.hpp +++ b/sibs/SafeSerializer.hpp @@ -1,6 +1,7 @@ #pragma once -#include "../types.hpp" +#include "endian.hpp" +#include "types.hpp" #include "../utils.hpp" #include #include @@ -21,7 +22,33 @@ namespace sibs { usize offset = buffer.size(); buffer.resize(buffer.size() + sizeof(data)); + #ifdef LITTLE_ENDIAN + switch(sizeof(T)) + { + case 1: + *(T*)&buffer[offset] = data; + break; + case 2: + *(u16*)&buffer[offset] = byteswap(*(u16*)&data); + break; + case 4: + *(u32*)&buffer[offset] = byteswap(*(u32*)&data); + break; + case 8: + *(u64*)&buffer[offset] = byteswap(*(u64*)&data); + break; + default: + { + for(int i = 0; i < sizeof(data); ++i) + { + buffer[offset + i] = ((char*)&data)[sizeof(data) - 1 - i]; + } + break; + } + } + #else memcpy(&buffer[offset], &data, sizeof(data)); + #endif } void add(const u8 *data, usize size) 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; + } +} diff --git a/sibs/types.hpp b/sibs/types.hpp new file mode 100644 index 0000000..0379100 --- /dev/null +++ b/sibs/types.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace sibs +{ + typedef int8_t i8; + typedef int16_t i16; + typedef int32_t i32; + typedef int64_t i64; + + typedef uint8_t u8; + typedef uint16_t u16; + typedef uint32_t u32; + typedef uint64_t u64; + + typedef float f32; + typedef double f64; + + typedef intptr_t isize; + typedef uintptr_t usize; +} -- cgit v1.2.3