diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-10-15 23:46:47 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-06 07:16:10 +0200 |
commit | e792431b0aa754c50d2edbc8f48e682e359b562c (patch) | |
tree | 1369b5b25973bc0d184291309329f04c996faf6f /sibs/SafeDeserializer.hpp | |
parent | 3e89af1a434996898b76a66c5053a0a13d2c1e41 (diff) |
Fix missaligned read/write
Diffstat (limited to 'sibs/SafeDeserializer.hpp')
-rw-r--r-- | sibs/SafeDeserializer.hpp | 30 |
1 files changed, 4 insertions, 26 deletions
diff --git a/sibs/SafeDeserializer.hpp b/sibs/SafeDeserializer.hpp index 7eb8aaf..57d2eee 100644 --- a/sibs/SafeDeserializer.hpp +++ b/sibs/SafeDeserializer.hpp @@ -5,6 +5,7 @@ #include "../utils.hpp" #include <cstring> #include <stdexcept> +#include <algorithm> namespace sibs { @@ -37,32 +38,9 @@ namespace sibs verifyExtractSize(typeSize); size -= typeSize; T result; + memmove(&result, data, typeSize); #if BYTE_ORDER == BIG_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 - result = *(T*)data; + std::reverse((char*)&result, (char*)&result + typeSize); #endif data += typeSize; return result; @@ -77,7 +55,7 @@ namespace sibs { verifyExtractSize(destinationSize); size -= destinationSize; - memcpy(destination, data, destinationSize); + memmove(destination, data, destinationSize); data += destinationSize; } } |