aboutsummaryrefslogtreecommitdiff
path: root/sibs
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-01 20:56:03 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-06 07:16:10 +0200
commit6fd8decb98ce81fd71cebd0226e30b0024ffd77c (patch)
tree7428c0f539af0a36f5ffe31e59757ae3027bb96d /sibs
parentbf9ff3ab31a996a0d6628eb23291d4a4b59543ec (diff)
Initial commit
Diffstat (limited to 'sibs')
-rw-r--r--sibs/SafeDeserializer.hpp90
-rw-r--r--sibs/SafeSerializer.hpp44
2 files changed, 134 insertions, 0 deletions
diff --git a/sibs/SafeDeserializer.hpp b/sibs/SafeDeserializer.hpp
new file mode 100644
index 0000000..3d83042
--- /dev/null
+++ b/sibs/SafeDeserializer.hpp
@@ -0,0 +1,90 @@
+#pragma once
+
+#include "../types.hpp"
+#include "../utils.hpp"
+#include <cstring>
+#include <stdexcept>
+
+namespace sibs
+{
+ class DeserializeException : public std::runtime_error
+ {
+ public:
+ DeserializeException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
+ /**
+ * Endian independent deserializer
+ */
+ class SafeDeserializer
+ {
+ DISABLE_COPY(SafeDeserializer);
+ public:
+ SafeDeserializer(const u8 *_data, usize _size) :
+ data(_data),
+ size(_size)
+ {
+
+ }
+
+ /*
+ * Throws DeserializeException on failure
+ */
+ template <typename T>
+ T&& extract()
+ {
+ constexpr usize typeSize = sizeof(T);
+ verifyExtractSize(typeSize);
+ size -= typeSize;
+ T result;
+ memcpy(&result, data, typeSize);
+ data += typeSize;
+ return std::move(result);
+ }
+
+ /*
+ * Throws DeserializeException on failure
+ */
+ void extract(u8 *destination, usize destinationSize)
+ {
+ if(destinationSize > 0)
+ {
+ verifyExtractSize(destinationSize);
+ size -= destinationSize;
+ memcpy(destination, data, destinationSize);
+ data += destinationSize;
+ }
+ }
+
+ bool empty() const
+ {
+ return size == 0;
+ }
+
+ const u8* getBuffer()
+ {
+ return data;
+ }
+
+ usize getSize() const
+ {
+ return size;
+ }
+ private:
+ void verifyExtractSize(usize typeSize) const
+ {
+ if(typeSize > size)
+ {
+ std::string errMsg = "Unable to extract ";
+ errMsg += std::to_string(typeSize);
+ errMsg += " bytes, only ";
+ errMsg += std::to_string(size);
+ errMsg += " bytes left in buffer";
+ throw DeserializeException(errMsg);
+ }
+ }
+ private:
+ const u8 *data;
+ usize size;
+ };
+}
diff --git a/sibs/SafeSerializer.hpp b/sibs/SafeSerializer.hpp
new file mode 100644
index 0000000..94bcdc5
--- /dev/null
+++ b/sibs/SafeSerializer.hpp
@@ -0,0 +1,44 @@
+#pragma once
+
+#include "../types.hpp"
+#include "../utils.hpp"
+#include <vector>
+#include <cstring>
+
+namespace sibs
+{
+ /**
+ * Endian independent serializer
+ */
+ class SafeSerializer
+ {
+ DISABLE_COPY(SafeSerializer);
+ public:
+ SafeSerializer(){}
+
+ template <typename T>
+ void add(const T &data)
+ {
+ usize offset = buffer.size();
+ buffer.resize(buffer.size() + sizeof(data));
+ memcpy(&buffer[offset], &data, sizeof(data));
+ }
+
+ void add(const u8 *data, usize size)
+ {
+ if(size > 0)
+ {
+ usize offset = buffer.size();
+ buffer.resize(buffer.size() + size);
+ memcpy(&buffer[offset], data, size);
+ }
+ }
+
+ std::vector<u8>& getBuffer()
+ {
+ return buffer;
+ }
+ private:
+ std::vector<u8> buffer;
+ };
+}