diff options
Diffstat (limited to 'sibs/SafeSerializer.hpp')
-rw-r--r-- | sibs/SafeSerializer.hpp | 29 |
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) |