aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-10-15 23:46:47 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-10-15 23:46:53 +0200
commit7947431ec1e837b2149e94b81e505fe4516ee5c6 (patch)
treeb56f4428f30175794a25aa302b50263f1005e0b8
parent6807296fb2107276dc164b25e13426317757e734 (diff)
Fix missaligned read/write2.0.0
-rw-r--r--project.conf5
-rw-r--r--sibs/SafeDeserializer.hpp30
-rw-r--r--sibs/SafeSerializer.hpp31
3 files changed, 10 insertions, 56 deletions
diff --git a/project.conf b/project.conf
index 77f21ad..f7ac4e9 100644
--- a/project.conf
+++ b/project.conf
@@ -1,9 +1,8 @@
[package]
name = "sibs-serializer"
-version = "1.0.4"
+version = "2.0.0"
type = "static"
platforms = ["any"]
-tests = "tests"
[config]
-expose_include_dirs = ["."]
+expose_include_dirs = ["."] \ No newline at end of file
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;
}
}
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);
}
}