Merge BigEndian functionality into CustomUintFormatter

This commit is contained in:
Pieter Wuille 2020-03-11 08:30:08 -07:00
parent 7e1fc03b18
commit 769ee5fa00
2 changed files with 17 additions and 42 deletions

View file

@ -176,7 +176,7 @@ class CService : public CNetAddr
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) { inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(ip); READWRITE(ip);
READWRITE(WrapBigEndian(port)); READWRITE(Using<BigEndianFormatter<2>>(port));
} }
}; };

View file

@ -518,7 +518,7 @@ struct VarIntFormatter
} }
}; };
template<int Bytes> template<int Bytes, bool BigEndian = false>
struct CustomUintFormatter struct CustomUintFormatter
{ {
static_assert(Bytes > 0 && Bytes <= 8, "CustomUintFormatter Bytes out of range"); static_assert(Bytes > 0 && Bytes <= 8, "CustomUintFormatter Bytes out of range");
@ -527,53 +527,31 @@ struct CustomUintFormatter
template <typename Stream, typename I> void Ser(Stream& s, I v) template <typename Stream, typename I> void Ser(Stream& s, I v)
{ {
if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range"); if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
if (BigEndian) {
uint64_t raw = htobe64(v);
s.write(((const char*)&raw) + 8 - Bytes, Bytes);
} else {
uint64_t raw = htole64(v); uint64_t raw = htole64(v);
s.write((const char*)&raw, Bytes); s.write((const char*)&raw, Bytes);
} }
}
template <typename Stream, typename I> void Unser(Stream& s, I& v) template <typename Stream, typename I> void Unser(Stream& s, I& v)
{ {
static_assert(std::numeric_limits<I>::max() >= MAX && std::numeric_limits<I>::min() <= 0, "CustomUintFormatter type too small"); static_assert(std::numeric_limits<I>::max() >= MAX && std::numeric_limits<I>::min() <= 0, "CustomUintFormatter type too small");
uint64_t raw = 0; uint64_t raw = 0;
if (BigEndian) {
s.read(((char*)&raw) + 8 - Bytes, Bytes);
v = be64toh(raw);
} else {
s.read((char*)&raw, Bytes); s.read((char*)&raw, Bytes);
v = le64toh(raw); v = le64toh(raw);
} }
};
/** Serialization wrapper class for big-endian integers.
*
* Use this wrapper around integer types that are stored in memory in native
* byte order, but serialized in big endian notation. This is only intended
* to implement serializers that are compatible with existing formats, and
* its use is not recommended for new data structures.
*
* Only 16-bit types are supported for now.
*/
template<typename I>
class BigEndian
{
protected:
I& m_val;
public:
explicit BigEndian(I& val) : m_val(val)
{
static_assert(std::is_unsigned<I>::value, "BigEndian type must be unsigned integer");
static_assert(sizeof(I) == 2 && std::numeric_limits<I>::min() == 0 && std::numeric_limits<I>::max() == std::numeric_limits<uint16_t>::max(), "Unsupported BigEndian size");
}
template<typename Stream>
void Serialize(Stream& s) const
{
ser_writedata16be(s, m_val);
}
template<typename Stream>
void Unserialize(Stream& s)
{
m_val = ser_readdata16be(s);
} }
}; };
template<int Bytes> using BigEndianFormatter = CustomUintFormatter<Bytes, true>;
/** Formatter for integers in CompactSize format. */ /** Formatter for integers in CompactSize format. */
struct CompactSizeFormatter struct CompactSizeFormatter
{ {
@ -626,9 +604,6 @@ public:
} }
}; };
template<typename I>
BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
/** Formatter to serialize/deserialize vector elements using another formatter /** Formatter to serialize/deserialize vector elements using another formatter
* *
* Example: * Example: