aboutsummaryrefslogtreecommitdiff
path: root/sibs/SafeDeserializer.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-03-07 02:52:28 +0100
committerAleksi Lindeman <0xdec05eba@gmail.com>2018-03-07 02:52:35 +0100
commite96e47e4d1eb7217a0169013c9968b8eb52bbb4e (patch)
tree00a52f662e09634cad31b143c1ca99241536a9db /sibs/SafeDeserializer.hpp
parent91418b32d7b67c335eb3baffc0bf381c04f68035 (diff)
Make serializer/deserializer endian independent0.2.0
Diffstat (limited to 'sibs/SafeDeserializer.hpp')
-rw-r--r--sibs/SafeDeserializer.hpp33
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;
}
/*