Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_SERIALIZE_H | #ifndef BITCOIN_SERIALIZE_H | ||||
#define BITCOIN_SERIALIZE_H | #define BITCOIN_SERIALIZE_H | ||||
#include <compat/endian.h> | #include <compat/endian.h> | ||||
#include <prevector.h> | #include <prevector.h> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <array> | |||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <cstring> | #include <cstring> | ||||
#include <ios> | #include <ios> | ||||
#include <limits> | #include <limits> | ||||
#include <map> | #include <map> | ||||
#include <memory> | #include <memory> | ||||
#include <set> | #include <set> | ||||
Show All 27 Lines | |||||
/** | /** | ||||
* Used to acquire a non-const pointer "this" to generate bodies of const | * Used to acquire a non-const pointer "this" to generate bodies of const | ||||
* serialization operations from a template | * serialization operations from a template | ||||
*/ | */ | ||||
template <typename T> inline T *NCONST_PTR(const T *val) { | template <typename T> inline T *NCONST_PTR(const T *val) { | ||||
return const_cast<T *>(val); | return const_cast<T *>(val); | ||||
} | } | ||||
//! Safely convert odd char pointer types to standard ones. | |||||
inline char *CharCast(char *c) { | |||||
return c; | |||||
} | |||||
inline char *CharCast(uint8_t *c) { | |||||
return (char *)c; | |||||
} | |||||
inline const char *CharCast(const char *c) { | |||||
return c; | |||||
} | |||||
inline const char *CharCast(const uint8_t *c) { | |||||
return (const char *)c; | |||||
} | |||||
/** | /** | ||||
* Lowest-level serialization and conversion. | * Lowest-level serialization and conversion. | ||||
* @note Sizes of these types are verified in the tests | * @note Sizes of these types are verified in the tests | ||||
*/ | */ | ||||
template <typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj) { | template <typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj) { | ||||
s.write((char *)&obj, 1); | s.write((char *)&obj, 1); | ||||
} | } | ||||
template <typename Stream> | template <typename Stream> | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | template <typename Stream> inline void Serialize(Stream &s, uint64_t a) { | ||||
ser_writedata64(s, a); | ser_writedata64(s, a); | ||||
} | } | ||||
template <typename Stream> inline void Serialize(Stream &s, float a) { | template <typename Stream> inline void Serialize(Stream &s, float a) { | ||||
ser_writedata32(s, ser_float_to_uint32(a)); | ser_writedata32(s, ser_float_to_uint32(a)); | ||||
} | } | ||||
template <typename Stream> inline void Serialize(Stream &s, double a) { | template <typename Stream> inline void Serialize(Stream &s, double a) { | ||||
ser_writedata64(s, ser_double_to_uint64(a)); | ser_writedata64(s, ser_double_to_uint64(a)); | ||||
} | } | ||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const int8_t (&a)[N]) { | |||||
s.write(a, N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const uint8_t (&a)[N]) { | |||||
s.write(CharCast(a), N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const std::array<int8_t, N> &a) { | |||||
s.write(a.data(), N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const std::array<uint8_t, N> &a) { | |||||
s.write(CharCast(a.data()), N); | |||||
} | |||||
#ifndef CHAR_EQUALS_INT8 | #ifndef CHAR_EQUALS_INT8 | ||||
// TODO Get rid of bare char | // TODO Get rid of bare char | ||||
template <typename Stream> inline void Unserialize(Stream &s, char &a) { | template <typename Stream> inline void Unserialize(Stream &s, char &a) { | ||||
a = ser_readdata8(s); | a = ser_readdata8(s); | ||||
} | } | ||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const char (&a)[N]) { | |||||
s.write(a, N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Serialize(Stream &s, const std::array<char, N> &a) { | |||||
s.write(a.data(), N); | |||||
} | |||||
#endif | #endif | ||||
template <typename Stream> inline void Unserialize(Stream &s, int8_t &a) { | template <typename Stream> inline void Unserialize(Stream &s, int8_t &a) { | ||||
a = ser_readdata8(s); | a = ser_readdata8(s); | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s, uint8_t &a) { | template <typename Stream> inline void Unserialize(Stream &s, uint8_t &a) { | ||||
a = ser_readdata8(s); | a = ser_readdata8(s); | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s, int16_t &a) { | template <typename Stream> inline void Unserialize(Stream &s, int16_t &a) { | ||||
Show All 15 Lines | template <typename Stream> inline void Unserialize(Stream &s, uint64_t &a) { | ||||
a = ser_readdata64(s); | a = ser_readdata64(s); | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s, float &a) { | template <typename Stream> inline void Unserialize(Stream &s, float &a) { | ||||
a = ser_uint32_to_float(ser_readdata32(s)); | a = ser_uint32_to_float(ser_readdata32(s)); | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s, double &a) { | template <typename Stream> inline void Unserialize(Stream &s, double &a) { | ||||
a = ser_uint64_to_double(ser_readdata64(s)); | a = ser_uint64_to_double(ser_readdata64(s)); | ||||
} | } | ||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, int8_t (&a)[N]) { | |||||
s.read(a, N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, uint8_t (&a)[N]) { | |||||
s.read(CharCast(a), N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, std::array<int8_t, N> &a) { | |||||
s.read(a.data(), N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, std::array<uint8_t, N> &a) { | |||||
s.read(CharCast(a.data()), N); | |||||
} | |||||
#ifndef CHAR_EQUALS_INT8 | |||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, char (&a)[N]) { | |||||
s.read(CharCast(a), N); | |||||
} | |||||
template <typename Stream, size_t N> | |||||
inline void Unserialize(Stream &s, std::array<char, N> &a) { | |||||
s.read(CharCast(a.data()), N); | |||||
} | |||||
#endif | |||||
template <typename Stream> inline void Serialize(Stream &s, bool a) { | template <typename Stream> inline void Serialize(Stream &s, bool a) { | ||||
char f = a; | char f = a; | ||||
ser_writedata8(s, f); | ser_writedata8(s, f); | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s, bool &a) { | template <typename Stream> inline void Unserialize(Stream &s, bool &a) { | ||||
char f = ser_readdata8(s); | char f = ser_readdata8(s); | ||||
a = f; | a = f; | ||||
▲ Show 20 Lines • Show All 696 Lines • Show Last 20 Lines |