diff options
Diffstat (limited to 'sibs/SafeDeserializer.hpp')
-rw-r--r-- | sibs/SafeDeserializer.hpp | 33 |
1 files changed, 30 insertions, 3 deletions
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 <cstring> #include <stdexcept> @@ -31,15 +32,41 @@ namespace sibs * Throws DeserializeException on failure */ template <typename T> - 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; } /* |