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/SafeDeserializer.hpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'sibs/SafeDeserializer.hpp') 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; } /* -- cgit v1.2.3