From ff9ea15709ba60008f46df9adae747b8c6e71d68 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 7 Mar 2018 02:52:28 +0100 Subject: Make serializer/deserializer endian independent --- sibs/SafeSerializer.hpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'sibs/SafeSerializer.hpp') 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 #include @@ -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) -- cgit v1.2.3