|
|
@ -23,7 +23,8 @@ |
|
|
|
#define HUSH_SERIALIZE_H |
|
|
|
|
|
|
|
#include "compat/endian.h" |
|
|
|
|
|
|
|
#include "span.h" |
|
|
|
#include <util/string.h> |
|
|
|
#include <algorithm> |
|
|
|
#include <array> |
|
|
|
#include <assert.h> |
|
|
@ -45,6 +46,9 @@ |
|
|
|
|
|
|
|
static const unsigned int MAX_SIZE = 0x02000000; |
|
|
|
|
|
|
|
/** Maximum amount of memory (in bytes) to allocate at once when deserializing vectors. */ |
|
|
|
static const unsigned int MAX_VECTOR_ALLOCATE = 5000000; |
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy data type to identify deserializing constructors. |
|
|
|
* |
|
|
@ -59,6 +63,12 @@ static const unsigned int MAX_SIZE = 0x02000000; |
|
|
|
struct deserialize_type {}; |
|
|
|
constexpr deserialize_type deserialize {}; |
|
|
|
|
|
|
|
//! Safely convert odd char pointer types to standard ones.
|
|
|
|
inline char* CharCast(char* c) { return c; } |
|
|
|
inline char* CharCast(unsigned char* c) { return (char*)c; } |
|
|
|
inline const char* CharCast(const char* c) { return c; } |
|
|
|
inline const char* CharCast(const unsigned char* c) { return (const char*)c; } |
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to bypass the rule against non-const reference to temporary |
|
|
|
* where it makes sense with wrappers such as CFlatData or CTxDB |
|
|
@ -238,6 +248,12 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); } |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); } |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); } |
|
|
|
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); } |
|
|
|
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); } |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); } |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
|
|
|
|
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } |
|
|
@ -254,6 +270,19 @@ template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = |
|
|
|
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); } |
|
|
|
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; } |
|
|
|
|
|
|
|
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); } |
|
|
|
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); } |
|
|
|
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); } |
|
|
|
|
|
|
|
|
|
|
|
struct DefaultFormatter |
|
|
|
{ |
|
|
|
template<typename Stream, typename T> |
|
|
|
static void Ser(Stream& s, const T& t) { Serialize(s, t); } |
|
|
|
|
|
|
|
template<typename Stream, typename T> |
|
|
|
static void Unser(Stream& s, T& t) { Unserialize(s, t); } |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|