aboutsummaryrefslogtreecommitdiff
path: root/sibs/SafeSerializer.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/SafeSerializer.hpp
parent91418b32d7b67c335eb3baffc0bf381c04f68035 (diff)
Make serializer/deserializer endian independent0.2.0
Diffstat (limited to 'sibs/SafeSerializer.hpp')
-rw-r--r--sibs/SafeSerializer.hpp29
1 files changed, 28 insertions, 1 deletions
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 <vector>
#include <cstring>
@@ -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)