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 --- .gitignore | 1 + project.conf | 2 +- sibs/SafeDeserializer.hpp | 33 +++++++++- sibs/SafeSerializer.hpp | 29 ++++++++- sibs/endian.hpp | 159 ++++++++++++++++++++++++++++++++++++++++++++++ sibs/types.hpp | 22 +++++++ tests/main.cpp | 17 ++++- types.hpp | 22 ------- 8 files changed, 256 insertions(+), 29 deletions(-) create mode 100644 sibs/endian.hpp create mode 100644 sibs/types.hpp delete mode 100644 types.hpp diff --git a/.gitignore b/.gitignore index 9f2d790..be24e9c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ sibs-build/ .vscode/ +*.kdev4 diff --git a/project.conf b/project.conf index 6badb64..ca50d38 100644 --- a/project.conf +++ b/project.conf @@ -1,6 +1,6 @@ [package] name = "sibs-serializer" -version = "0.1.0" +version = "0.2.0" type = "static" platforms = ["any"] tests = "tests" 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; +} diff --git a/tests/main.cpp b/tests/main.cpp index e495b3a..4b6c7a7 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -7,14 +7,25 @@ #define REQUIRE(statement) do { if(!(statement)) { fprintf(stderr, "Assertion failed:\n%s\n", #statement); exit(1); } } while(0) #define FAIL(str) do { fprintf(stderr, "Failed:\n%s\n", (str)); exit(1); } while(0) +struct TestStruct +{ + uint32_t a; + uint8_t b; +}; + int main() { + TestStruct expectedTestStruct; + expectedTestStruct.a = 0x38956326; + expectedTestStruct.b = 0x34; + sibs::SafeSerializer serializer; serializer.add((uint32_t)3563634); serializer.add((uint64_t)204232532533423632); serializer.add((uint8_t)2); serializer.add((uint8_t*)"hello", 5); - REQUIRE(serializer.getBuffer().size() == 18); + serializer.add(expectedTestStruct); + REQUIRE(serializer.getBuffer().size() == 18 + sizeof(expectedTestStruct)); sibs::SafeDeserializer deserializer(serializer.getBuffer().data(), serializer.getBuffer().size()); REQUIRE(deserializer.extract() == 3563634); @@ -24,6 +35,8 @@ int main() str[5] = '\0'; deserializer.extract((uint8_t*)str, 5); REQUIRE(strcmp(str, "hello") == 0); + TestStruct actualTestStruct = deserializer.extract(); + REQUIRE(actualTestStruct.a == expectedTestStruct.a && actualTestStruct.b == expectedTestStruct.b); REQUIRE(deserializer.empty()); try @@ -37,4 +50,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git a/types.hpp b/types.hpp deleted file mode 100644 index 0379100..0000000 --- a/types.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#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