aboutsummaryrefslogtreecommitdiff
path: root/sibs/SafeSerializer.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-10-15 23:46:47 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-06 07:16:10 +0200
commite792431b0aa754c50d2edbc8f48e682e359b562c (patch)
tree1369b5b25973bc0d184291309329f04c996faf6f /sibs/SafeSerializer.hpp
parent3e89af1a434996898b76a66c5053a0a13d2c1e41 (diff)
Fix missaligned read/write
Diffstat (limited to 'sibs/SafeSerializer.hpp')
-rw-r--r--sibs/SafeSerializer.hpp31
1 files changed, 4 insertions, 27 deletions
diff --git a/sibs/SafeSerializer.hpp b/sibs/SafeSerializer.hpp
index 0aa53c5..68b6e7c 100644
--- a/sibs/SafeSerializer.hpp
+++ b/sibs/SafeSerializer.hpp
@@ -5,6 +5,7 @@
#include "../utils.hpp"
#include <vector>
#include <cstring>
+#include <algorithm>
namespace sibs
{
@@ -24,33 +25,9 @@ namespace sibs
void add(const T &data)
{
usize offset = buffer.size();
- buffer.resize(buffer.size() + sizeof(T));
+ buffer.insert(buffer.end(), (unsigned char*)&data, (unsigned char*)&data + sizeof(T));
#if BYTE_ORDER == BIG_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(T); ++i)
- {
- buffer[offset + i] = ((char*)&data)[sizeof(T) - 1 - i];
- }
- break;
- }
- }
- #else
- *(T*)&buffer[offset] = data;
+ std::reverse((char*)&buffer[offset], (char*)&buffer[offset] + sizeof(T));
#endif
}
@@ -60,7 +37,7 @@ namespace sibs
{
usize offset = buffer.size();
buffer.resize(buffer.size() + size);
- memcpy(&buffer[offset], data, size);
+ memmove(&buffer[offset], data, size);
}
}