aboutsummaryrefslogtreecommitdiff
path: root/sibs/SafeSerializer.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-03-07 02:52:28 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-06 07:16:10 +0200
commitff9ea15709ba60008f46df9adae747b8c6e71d68 (patch)
tree4c2bce5309765194bb2614be92aac45db86aa0ea /sibs/SafeSerializer.hpp
parent6fd8decb98ce81fd71cebd0226e30b0024ffd77c (diff)
Make serializer/deserializer endian independent
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)